Skip to content

Stop turns that start after their run was stopped, live and on reconnect - #6925

Closed
ymichael wants to merge 1 commit into
mainfrom
bb/fix-thread-stop-state-consistency-thr_vuf5cgggjn
Closed

ymichael wants to merge 1 commit into
mainfrom
bb/fix-thread-stop-state-consistency-thr_vuf5cgggjn

Conversation

@ymichael

@ymichael ymichael commented Nov 3, 2026 •

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

A turn that started after its run was stopped could keep running unseen. This happened live and again on reconnect.

  • Live: a stop that reaches the daemon while turn.submit is still resuming the thread finds no hosted thread. It returns { providerCheckpointId: null } without sending a provider stop. The turn then starts and the daemon forwards turn/started. The new daemon test in thread-stop-races.test.ts reproduces this. The server already treated a turn/started that follows a stop (with no newer request) as stale. It dropped the event without reviving the thread, but it also never stopped the turn, so the turn ran behind an idle thread.
  • Reconnect: an error thread that the daemon reported as live was always revived to active. A turn the user stopped, whose stop never reached the daemon, came back. The session-open report carried only thread IDs, so the server could not tell an old stopped turn from a newer one. The idle-thread branch had its own check (the latest of turn/started and the stop), which revived exactly the late turn the live path dropped.

These are items 3 and 5b of the investigation into #6552's stop fixes against main.

What changed

One rule, live and on reconnect: a turn belongs to a stopped run when a manual stop is newer than the request that turn answers. That request is the latest client/turn/requested before the turn started, skipping requests that were later rejected, since a rejected request never starts a turn. For a pending start, or a turn the server has not stored yet, it is the latest such request.

  • @bb/db hasStopSinceTurnRequest is the one definition. It takes which stop reasons count and whether the stop must come before the turn started. It replaces hasThreadStopBeforeTurnStarted (server events.ts) and listThreadIdsStoppedSinceLastTurnStart.
    • Drop decision (unchanged scope): any stop reason, before the turn started. This still decides whether a turn/started revives the thread and whether a non-interrupted turn/completed is applied.
    • Live stop decision: manual stops only, same window. So every stopped turn is also a dropped one. A matching top-level turn/started now gets a thread.stop.
    • Reconnect decision: manual stops, up to now. Idle, starting and error threads whose reported turn matches are stopped again instead of revived.
  • wasTurnManuallyStopped stays separate. It asks whether a stop came during a turn, to suppress a directory continuation, and doesn't anchor on requests. Folding it in would change what it means.
  • Stop dispatch reuses the existing in-flight thread.stop guard, so a duplicate stop isn't sent.

Wire change: each activeThreads entry in the session-open request now carries activeTurnId (null while a start is still pending). The daemon fills it from the runtime's active turn. HOST_DAEMON_PROTOCOL_VERSION goes from 260 to 261. Only the exact version is compatible, so a 260 daemon is refused and updates. No back-compat path is kept.

What users see differently:

  • A turn that the provider starts by itself after a manual stop, with no new request in between, is now stopped, both live and on reconnect. Main dropped it live, so the thread showed idle while the turn ran, and revived it to active on reconnect.
  • After a manual stop, a stopped turn that the daemon still runs behind an error thread is stopped on reconnect, and the thread stays error. Main revived it to active.
  • The one behaviour change from sharing the definition is in the drop check: rejected requests are now skipped. If a late turn arrives after a stop and the only newer request was rejected, the turn is now dropped and stopped. Main revived it.
  • Unchanged: an active thread whose newer request may have reached the daemon is not stopped again on reconnect. Threads cut off by a lost connection still revive on reconnect, because only manual stops count.

Related work: #6913 (external, open) fixes the same kind of race one layer down, inside the ACP bridge. It makes a thread/stop that the bridge answered fail an in-flight session start. The race reproduced here happens before the daemon hosts the thread, so the stop never reaches any bridge and #6913 can't see it. #6913 also covers only ACP providers. The two complement each other: #6913 prevents some late starts at the source, and this PR stops any that get through, for every provider.

How you verified

New tests fail on main and pass with this change:

  • apps/server/test/public/public-thread-stop-runtime.test.ts: "stops a turn the daemon starts after its run was stopped" (live).
  • apps/server/test/internal/background-task-reconciliation.test.ts:
    • "re-stops rather than revives a stopped turn the daemon still runs behind an error thread" (reconnect, error thread).
    • "re-stops a turn that started after a stop unless a newer request explains it (newer request: false)" (reconnect, idle thread).
  • apps/host-daemon/test/command/thread-stop-races.test.ts: a stop misses a turn whose submit is still resuming. The late turn is reported live with its activeTurnId, and a second stop interrupts it.

Guards that pass before and after:

  • "does not re-stop an active thread whose newer request may have reached the daemon".
  • The newer request: true case, which revives. This replaces the old "revives rather than stops a thread that started a new turn after its last stop", whose seed had no request between the stop and the new turn. That was the late-start case itself.

Other checks:

  • packages/db/test/data/events.test.ts: an 11-case table for hasStopSinceTurnRequest, covering both windows, the reason filter, rejected requests, and unknown or pending turns.
  • pnpm exec turbo run typecheck lint --filter='...[origin/main]' passed, 136/136 tasks.
  • pnpm exec turbo run test passed for @bb/server (3873), @bb/host-daemon (845), @bb/db (651) and @bb/host-daemon-contract (91). The @bb/db count was taken before the shared-query table was added; the table was then run on its own. After the shared-query change, @bb/server test/internal, test/threads and the stop-runtime suite were re-run: 841 passed.

🤖 Generated with Claude Code

AGENT GENERATED

A stop that reaches the daemon while turn.submit is still resuming the
thread finds nothing hosted and returns without stopping anything. The
turn then starts and the daemon forwards turn/started. The server
dropped that event without reviving the thread or stopping the turn, so
the turn ran behind an idle thread. On reconnect, an error thread that
the daemon reported as live was always revived. That brought back a
turn the user had stopped whose stop never landed, because the
session-open report carried only thread IDs.

One rule now covers both paths: a turn belongs to a stopped run when a
manual stop is newer than the request that turn answers. That request is
the latest client/turn/requested before the turn started that was not
rejected; for a turn the server has not stored yet, it is the latest
such request.
- Live: a root turn/started that matches the rule gets a thread.stop.
- Reconnect: idle, starting and error threads whose reported turn
  matches the rule are re-stopped instead of revived.

hasStopSinceTurnRequest in @bb/db is the one definition behind the
server's drop decision (any stop reason, before the turn started), the
live stop decision (manual stops only, same window) and the reconnect
decision (manual stops, up to now). It replaces
hasThreadStopBeforeTurnStarted in events.ts and
listThreadIdsStoppedSinceLastTurnStart. The drop check now skips
rejected requests too, so a late turn after a stop whose only newer
request was rejected is dropped and stopped instead of revived.

Protocol: the daemon reports activeTurnId (nullable while a start is
pending) for each active thread at session open.
HOST_DAEMON_PROTOCOL_VERSION goes from 260 to 261.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael

ymichael commented Nov 3, 2026

Copy link
Copy Markdown
Collaborator Author

Closing: live QA against a real daemon showed the live case doesn't happen on main (the daemon runs a thread's stop after its submit and interrupts the turn), and provider-started turns after a manual stop don't occur with Claude Code or Codex. The remaining reconnect case needs two simultaneous failures and doesn't justify a daemon protocol bump.

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