Skip to content

Show a steered queue-mode send once instead of twice - #7531

Merged
ymichael merged 1 commit into
mainfrom
bb/fix-thread-submission-race-condition-thr_kk5mynmymg
Nov 13, 2026
Merged

ymichael merged 1 commit into
mainfrom
bb/fix-thread-submission-race-condition-thr_kk5mynmymg

Conversation

@ymichael

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

With Enter set to queue, a message sent to a Claude Code thread whose turn is held open only by background agents (main run idle) is steered by the server. That is by design since #5564. The client had predicted a queued send. When the sent reply arrived, applySendThreadMessageSuccess removed the queue placeholder and appended an optimistic user row to every timeline cache. It never checked whether the realtime timeline refresh had already delivered the real row. When the refresh won the race, the message showed twice, back to back. One copy disappeared on the next timeline refresh, usually the agent's reply.

What changed

  • Contract: sendMessageResponseSchema's sent variant gains messageSequence: number | null. It is the thread event sequence of the delivered message's client/turn/requested event, comparable with a timeline's maxSeq. It is null when the send recorded no message (/clear, or a drained row that vanished). sendQueuedMessageResponseSchema gets its own schema with the old shape: that path inserts its optimistic row before the request and never after, so it needs no position.
  • Server:
    • sendThreadMessage returns the recorded sequence for start, steer and reprovision dispatch. dispatchTurnDuringReprovision now returns { messageSequence } | null instead of a boolean.
    • requestThreadProvision and requestThreadTargetReprovision return { context, messageSequence }, so a thread's first dispatch reports it too.
    • DispatchAttemptOutcome's dispatched variant carries the sequence through to acceptThreadSendRequest.
  • App: insertOptimisticTimelineRowUnlessDelivered adds the temporary row only to timeline caches whose maxSeq is below messageSequence, or to all caches when it is null. The timeline-row update predicate now receives the cached response instead of the query key.
  • Docs and SDK: the bb thread tell guide documents messageSequence in --json output. The plugin SDK is bumped to 0.6.144 because its bundled types include the server contract.
  • No daemon wire change.

How you verified

  • New app test table in thread-runtime-cache-owner.test.ts:

    1. The timeline already contains the message → one copy.
    2. An unrelated refresh landed before the message → copy added.
    3. No position → copy added.

    Against the original client code, case 1 fails with the duplicate.

  • Server send tests now assert that messageSequence equals the recorded client/turn/requested sequence. This includes the held-turn queue→steer case and the reprovision dispatch.

  • Locally:

    • Server test/threads and test/public pass (119 files, 1317 tests).
    • App (7067), CLI (937), SDK (137) and side-chat (27) tests pass.
    • turbo run typecheck lint passes for the affected packages; it was re-run after rebasing.
    • Plugin SDK build:types passes.
  • Live: I ran pnpm start:worktree with a Claude Code thread held open by a background agent and Enter set to queue. The main and branch app bundles ran behind identical proxies that delay the send reply by 0.3 s. Real Enter on desktop (1440×900) and a real tap on send on phone (390×844), sampling timeline user rows every 30 ms:

    • Main: 1 row @~180 ms → 2 back to back @~490 ms → 1 @~1.3 s.
    • Branch: 1 row throughout.
    • bb thread tell --mode queue --json returned messageSequence: 81, and bb thread log shows event 81 is that message's steer.
  • Not changed: the "Queue 1" placeholder that shows alongside the steered message while the request is in flight.

AGENT GENERATED

🤖 Generated with Claude Code

With Enter set to queue, a send to a thread whose turn is held open only by
background agents is steered by the server. The app had predicted a queued
send, so on the "sent" reply it appended a temporary user row, even when the
realtime timeline refresh had already delivered the real one. The message
showed twice until the next timeline refresh.

The send reply now carries messageSequence, the thread event sequence of the
delivered message (null when nothing was recorded, such as /clear). The app
adds the temporary row only to timeline caches whose maxSeq has not reached it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael merged commit 5485f48 into main Nov 13, 2026
51 checks passed
@ymichael
ymichael deleted the bb/fix-thread-submission-race-condition-thr_kk5mynmymg branch November 13, 2026 21:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant