Repository navigation
Deliver messages to Claude Code turns held open only by background work - #5564
Merged
SawyerHood merged 7 commits intoOct 19, 2026
Merged
SawyerHood merged 7 commits into
SawyerHood merged 7 commits into
Conversation
patleeman
force-pushed
the
fix/claude-open-to-messages-while-background
branch
2 times, most recently
from
October 17, 2026 00:31
6c12f92 to
af8aa85
Compare
Claude Code keeps a logical turn open while background agents or workflows run (get-bb#633), so the thread stayed active and a normal Enter (queue-if-active) waited as thread-busy until every background task finished, even though the main run was idle. The Claude Code bridge now sends a turn.mainRun delta when it holds the turn after the SDK result (idle) and when the next main run starts (running). The assembler records it as turn/mainRun/updated. While the active turn's main run is idle, dispatch delivers queue-if-active messages into the held turn as steers, and thread-busy rows drain when the main run goes idle. The thread still shows as running, and the turn still does not complete until background work finishes. Bumps HOST_DAEMON_PROTOCOL_VERSION to 242 for the new thread event. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
patleeman
force-pushed
the
fix/claude-open-to-messages-while-background
branch
from
October 19, 2026 18:03
af8aa85 to
ccd8301
Compare
isStoredTurnMainRunIdle compared the latest turn/mainRun/updated row's state in JavaScript after JSON.parse and hand-written narrowing. Compare it in SQL with json_extract, as the rest of events.ts does.
Add the missing negative case: an assistant or stream_event message that carries a parent tool call id (in the SDK message or the translation context) must not report the main run as running again; the next top-level assistant message does. Cover the assembler dropping turn.mainRun without an open turn, and the timeline building the same rows with or without turn/mainRun/updated events.
A thread-busy row queued before the main run went idle was drained into the held turn on the idle transition, but a row created while the turn was already held waited until the turn completed or the main run went idle again, because creating a queued message only woke the queue for an idle thread. Wake it when the active turn's main run is idle too.
ymichael
added a commit
that referenced
this pull request
Nov 13, 2026
## 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](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Human comments
What was wrong
Claude Code keeps one logical turn open while background agents or workflows run. #633 added this so a child thread is not reported done while its sub-agents still work. The bridge holds back
turn.boundaryon the SDKresult(plugins/provider-claude-code/src/delta-translation.ts,hasCompletionBlockingClaudeTasks), so the thread staysactive. Aqueue-if-activesend (bb thread send --mode queue,sdk.threads.send, embedded chats, retries, or Enter when it is set to queue) is somethingresolveDispatchAttemptKindtreats asstart-turnon an active thread.dispatch-attempt.tsthen waits withthread-busyuntil every background task finishes, even though the main run is idle and Claude Code would accept a new prompt right away. Smart Queue made no decisions here; this is core dispatch.What changed
turn.mainRun { state: "idle" | "running" }. The assembler turns it into the thread eventturn/mainRun/updatedon the open turn. This is an additive delta kind, so the grammar snapshot is updated andPROVIDER_BRIDGE_PROTOCOL_VERSIONstays at 2.turn/mainRun/updatedevent schema and its scope policy (turn). The event is treated as timeline noise, so it is not rendered.idleonce. When the next top-level main run starts (anassistantorstream_eventmessage without a parent tool call), it sendsrunning. Turn completion is unchanged, so Keep Claude child threads active while subagents run #633's guarantee holds: noturn/completedwhile background agents or workflows run.isStoredTurnMainRunIdle, which reads the latestturn/mainRun/updatedevent for a turn.dispatch-attempt.ts: when the active turn's main run is idle, aqueue-if-activemessage is sent assteer-if-active, so it is delivered into the held turn.queued-messages.ts:thread-busyrows become eligible while the main run is idle.internal/events.ts: anidleevent wakes the queue drain.activeand the background-agent indicator is unchanged.HOST_DAEMON_PROTOCOL_VERSIONgoes from 241 to 242. The plugin SDK goes from 0.6.55 to 0.6.56 for theturn.mainRundelta.queue-if-activesends andthread-busyqueued rows, including rows queued from the composer when Enter is set to queue. Enter's default already steers into an active thread and is unchanged. A row queued while the turn is already held is delivered right away (createQueuedMessageForThreadwakes the drain when the main run is idle), not only rows queued before the main run went idle.restartBeforeNextTurnClaude session restart on purpose, since restarting would kill the background agents holding the turn open; the restart still happens before the next real turn. Dispatch hooks seeattempt: "join-turn"for these messages. A drained queued row shows with the Steer label in the timeline.docs/provider-bridge-protocol.mddocuments held turns.pnpm rerecord --provider claude-codeproduced only unrelated churn, so no recording changes are committed.How you verified
turn held open for background workinapps/server/test/threads/thread-send-dispatch.test.ts. It fails before the server change and passes after. It checks that:queue-if-activestill queues asthread-busy;idle, the queued row drains into the held turn and a new Enter is sent right away as a steer, while the thread staysactive;running, Enter queues again.task-translation.test.tsanddelta-translation.test.ts:resultsendsidleonce and noturn/completed(Keep Claude child threads active while subagents run #633 guarantee);runningfirst;resultcompletes the turn.assistant/stream_eventmessages don't flip the held turn torunning, that the assembler dropsturn.mainRunwith no open turn, and that the event never changes the built timeline;isStoredTurnMainRunIdlenow usesjson_extract. End-to-end with Claude Code 2.1.303: a message sent while only a background agent ran was answered in ~3s (main: waited ~175s for the agent).pnpm exec turbo run typecheck lint testfor@bb/server,@bb/domain,@bb/provider-bridge-protocol,@bb/thread-view,@bb/db,@bb/host-daemon-contract,@bb/host-daemon,@bb/agent-runtime,@bb/provider-parity,bb-plugin-provider-claude-code,@bb/app,@bb/client-core. Everything passes except one@bb/serverscrollback timing test (public-thread-timeline-scrollback), which is flaky: it failed once in the full run and passed in two runs of that file on its own.🤖 Generated with Claude Code