Skip to content

Keep a thread active while a new-turn request is in flight - #7322

Merged
ymichael merged 1 commit into
mainfrom
bb/keep-a-thread-active-while-a-new-turn-request-is-thr_mrjgbn8x5q
Nov 10, 2026
Merged

ymichael merged 1 commit into
mainfrom
bb/keep-a-thread-active-while-a-new-turn-request-is-thr_mrjgbn8x5q

Conversation

@ymichael

@ymichael ymichael commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

#7310 holds a root turn/completed while a steer is on its way to the daemon. A new-turn request (turn.submit with target: start, or thread.start for edits and forks) has the same window, but nothing held it. The server applies run.started when the message is sent. If a different root turn ends while the start is still on its way to the daemon, applyTurnCompletedEvent applies run.succeeded and the thread goes idle. The next message is then sent as a second start. The daemon starts A's turn first and refuses the second with competing_turn, and the user's message is lost.

The other root turn is any turn the provider opens by itself. For Claude Code, that includes the turn the CLI runs when a background Bash command finishes (bb doesn't hold the turn for background shells). In the original trace it was the Claude Code drift fixed in #7321 (dev DB of thr_kskphedncm, thread thr_i7fdv3ia4f, events 501–515). That drift is one way to trigger it, not the cause: the server window exists without it.

What changed

  • apps/server/src/services/threads/thread-send.ts: the start path tracks its live command as in flight and settles it in onSettled. The history-replacement onCommandSettled still runs first. A root completion that arrives while a start is in flight is held, exactly as for steers:
    • if the start's turn is running when the request settles, the held completion is dropped;
    • otherwise it is applied with its usual idle follow-ups.
  • apps/server/src/internal/turn-completed-events.ts, thread-lifecycle.ts: the in-flight counter now covers steers and starts, so it is renamed from trackInFlightSteer/settleInFlightSteer to trackInFlightTurnRequest/settleInFlightTurnRequest. Its behaviour is unchanged.

Visible difference, as in #7310: if a turn ends before its own start command settles, the thread stays "running" until the start settles, usually one daemon round-trip. A start that fails still settles the thread as error with later sends left queued, the same as without the race. No wire change.

How you verified

  • Two new cases in apps/server/test/threads/thread-send-dispatch.test.ts, in the Hold the turn until in-flight steers settle #7310 describe (renamed to "a turn request in flight when a turn ends"). They use a real migrated DB and the same fake daemon, which refuses start with competing_turn while a turn is active:

    • an idle thread sends A, a provider-initiated root turn starts and ends before A reaches the daemon, then B is sent. A and B are both accepted, with B steered into A's turn;
    • the same race, but A's start fails: the thread ends in error with B queued, which matches a failed start without the race.

    On origin/main both fail with AssertionError: expected 'idle' to be 'active'. With the fix, all 52 tests in the file pass.

  • npx vitest run test/threads test/public test/internal test/system test/routes: 168 files, 1773 tests passed. The rest of the server suite passed: 4024 tests; one shell-script test in install-machine-script.test.ts timed out under load and passes on rerun (54/54). pnpm exec turbo run typecheck lint --filter=@bb/server passed.

  • Live, pnpm start:worktree with a Claude Code (haiku) thread. Each round asks Claude to run sleep 8 in the background, so the CLI later opens its own notification turn X. It then sends start A with 20–40 × 34 MB attachments, so the daemon stages A for seconds and X runs and ends while A is in flight. B is sent the moment X ends:

    • origin/main: 3 round(s) raced; 3 with a rejected message, each A accepted(t18); B rejected:competing_turn;
    • this branch: 6 round(s) raced; 0 with a rejected message, each A accepted(t15); B accepted(t15), with B steered into A's turn.

AGENT GENERATED

🤖 Generated with Claude Code

#7310 holds a root turn/completed while a steer is on its way to the daemon.
A new-turn request has the same window: the server applies run.started at
send, and if another root turn (for example a Claude Code background-task
notification turn) ends before the start reaches the daemon, the thread went
idle. The next send then went out as a second start, the daemon started A's
turn first, and refused the second message with competing_turn.

The start path now tracks its live command the same way, so a root
completion that arrives while a start is in flight is held until the start
settles. The in-flight counter is renamed from steers to turn requests.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael merged commit f864639 into main Nov 10, 2026
31 checks passed
@ymichael
ymichael deleted the bb/keep-a-thread-active-while-a-new-turn-request-is-thr_mrjgbn8x5q branch November 10, 2026 04:01
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