Skip to content

Deliver messages to Claude Code turns held open only by background work - #5564

Merged
SawyerHood merged 7 commits into
get-bb:mainfrom
patleeman:fix/claude-open-to-messages-while-background
Oct 19, 2026
Merged

SawyerHood merged 7 commits into
get-bb:mainfrom
patleeman:fix/claude-open-to-messages-while-background

Conversation

@patleeman

@patleeman patleeman commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

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.boundary on the SDK result (plugins/provider-claude-code/src/delta-translation.ts, hasCompletionBlockingClaudeTasks), so the thread stays active. A queue-if-active send (bb thread send --mode queue, sdk.threads.send, embedded chats, retries, or Enter when it is set to queue) is something resolveDispatchAttemptKind treats as start-turn on an active thread. dispatch-attempt.ts then waits with thread-busy until 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

  • provider-bridge-protocol: adds the delta turn.mainRun { state: "idle" | "running" }. The assembler turns it into the thread event turn/mainRun/updated on the open turn. This is an additive delta kind, so the grammar snapshot is updated and PROVIDER_BRIDGE_PROTOCOL_VERSION stays at 2.
  • domain / thread-view: adds the turn/mainRun/updated event schema and its scope policy (turn). The event is treated as timeline noise, so it is not rendered.
  • Claude Code bridge: when it holds the turn for background work, it sends idle once. When the next top-level main run starts (an assistant or stream_event message without a parent tool call), it sends running. Turn completion is unchanged, so Keep Claude child threads active while subagents run #633's guarantee holds: no turn/completed while background agents or workflows run.
  • db: adds isStoredTurnMainRunIdle, which reads the latest turn/mainRun/updated event for a turn.
  • server:
    • dispatch-attempt.ts: when the active turn's main run is idle, a queue-if-active message is sent as steer-if-active, so it is delivered into the held turn.
    • queued-messages.ts: thread-busy rows become eligible while the main run is idle.
    • internal/events.ts: an idle event wakes the queue drain.
    • While the main run is working, behavior is unchanged: Enter still queues.
  • The thread keeps showing as running while it accepts messages. Its status stays active and the background-agent indicator is unchanged.
  • Wire change: a new thread event on the daemon→server lane, so HOST_DAEMON_PROTOCOL_VERSION goes from 241 to 242. The plugin SDK goes from 0.6.55 to 0.6.56 for the turn.mainRun delta.
  • What reaches the held turn: queue-if-active sends and thread-busy queued 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 (createQueuedMessageForThread wakes the drain when the main run is idle), not only rows queued before the main run went idle.
  • Deliberate trade-offs: a message delivered into a held turn is a steer, not a new turn. It skips a pending restartBeforeNextTurn Claude 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 see attempt: "join-turn" for these messages. A drained queued row shows with the Steer label in the timeline.
  • Docs: docs/provider-bridge-protocol.md documents held turns.
  • Recordings: none of the Claude Code recordings exercise a held turn. pnpm rerecord --provider claude-code produced only unrelated churn, so no recording changes are committed.

How you verified

  • New server test turn held open for background work in apps/server/test/threads/thread-send-dispatch.test.ts. It fails before the server change and passes after. It checks that:
    • while the main run works, queue-if-active still queues as thread-busy;
    • once the main run goes idle, the queued row drains into the held turn and a new Enter is sent right away as a steer, while the thread stays active;
    • after running, Enter queues again.
  • Extended Claude Code bridge tests in task-translation.test.ts and delta-translation.test.ts:
  • Maintainer follow-up: added tests that sub-agent assistant/stream_event messages don't flip the held turn to running, that the assembler drops turn.mainRun with no open turn, and that the event never changes the built timeline; isStoredTurnMainRunIdle now uses json_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 test for @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/server scrollback 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.

AGENT GENERATED

🤖 Generated with Claude Code

@patleeman
patleeman force-pushed the fix/claude-open-to-messages-while-background branch 2 times, most recently from 6c12f92 to af8aa85 Compare October 17, 2026 00:31
patleeman and others added 2 commits October 19, 2026 14:01
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]>
@patleeman
patleeman force-pushed the fix/claude-open-to-messages-while-background branch from af8aa85 to ccd8301 Compare October 19, 2026 18:03
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.
@SawyerHood
SawyerHood merged commit 8e863c9 into get-bb:main Oct 19, 2026
46 checks passed
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]>
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.

2 participants