Repository navigation
Conversation
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]>
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. |
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
A turn that started after its run was stopped could keep running unseen. This happened live and again on reconnect.
turn.submitis still resuming the thread finds no hosted thread. It returns{ providerCheckpointId: null }without sending a provider stop. The turn then starts and the daemon forwardsturn/started. The new daemon test inthread-stop-races.test.tsreproduces this. The server already treated aturn/startedthat 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.errorthread that the daemon reported as live was always revived toactive. 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 ofturn/startedand 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/requestedbefore 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/dbhasStopSinceTurnRequestis the one definition. It takes which stop reasons count and whether the stop must come before the turn started. It replaceshasThreadStopBeforeTurnStarted(serverevents.ts) andlistThreadIdsStoppedSinceLastTurnStart.turn/startedrevives the thread and whether a non-interruptedturn/completedis applied.turn/startednow gets athread.stop.wasTurnManuallyStoppedstays 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.thread.stopguard, so a duplicate stop isn't sent.Wire change: each
activeThreadsentry in the session-open request now carriesactiveTurnId(nullwhile a start is still pending). The daemon fills it from the runtime's active turn.HOST_DAEMON_PROTOCOL_VERSIONgoes 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:
errorthread is stopped on reconnect, and the thread stayserror. Main revived it toactive.Related work: #6913 (external, open) fixes the same kind of race one layer down, inside the ACP bridge. It makes a
thread/stopthat 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: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 itsactiveTurnId, and a second stop interrupts it.Guards that pass before and after:
newer request: truecase, 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 forhasStopSinceTurnRequest, 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 testpassed for@bb/server(3873),@bb/host-daemon(845),@bb/db(651) and@bb/host-daemon-contract(91). The@bb/dbcount was taken before the shared-query table was added; the table was then run on its own. After the shared-query change,@bb/servertest/internal,test/threadsand the stop-runtime suite were re-run: 841 passed.🤖 Generated with Claude Code