Repository navigation
Stop the turn-start deadline only when its condition still holds, and tell the parent - #7328
Merged
ymichael merged 2 commits intoNov 10, 2026
Conversation
… tell the parent The turn-start deadline sweep listed every thread active for more than five minutes, then decided to stop it after an await with only an active-turn check. A long turn that completed between list and process was reported as a turn that never started. Re-check the full condition and mark the thread stopping in the same transaction as the error append. A deadline stop ends a child's run before any turn starts, so no turn outcome reached the parent. Notify the parent as a failed child under auto and stuck. Co-Authored-By: Claude Opus 5.5 <[email protected]>
ymichael
marked this pull request as ready for review
November 10, 2026 04:03
… in one place The sweep now returns before opening a write transaction when a turn is running, which is the common case for every long turn it lists. The in-transaction re-check keeps only the listing condition: the active-turn check runs synchronously just before it, and a status change always moves statusChangedAt past the deadline, so the separate statusChangedAt match was redundant. dispatchThreadStopCommand now claims the in-flight thread.stop guard itself, so its four callers no longer repeat the claim. Co-Authored-By: Claude Opus 5.5 <[email protected]>
ymichael
deleted the
bb/fix-turn-start-deadline-killing-threads-as-long-thr_uukmxggety
branch
November 10, 2026 04:16
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
turnStartDeadlineSweeplists every thread that has beenactivefor more than 5 minutes with no recentturn/completed. That includes every long turn.processthen runs after at least one await (the item-sweep runner yields withsetImmediateonce 8 ms have passed) and checks onlygetActiveTurnId(...) !== null. When a long turn completes in that gap,processfinds no active turn, writesprovider_turn_start_deadlineand requests a stop. In the reported child thread (thr_v5rh98dc7y),turn/completedwas written at 15:34:55.778 and the server loggedStopping a run whose turn never startedat .886, followed byno transition for stop.requested from status idleat .910. The thread had already settled toidle, so the stop was rejected. Nothing was killed: at 15:39:46 the child's background waiter (started 15:34:51) was still running andchecks3.donedid not exist yet. The only damage was the false "so bb stopped it" error. If the thread is stillactivewhen the turn completes (for example, a completion deferred by an in-flight steer), the same race does dispatch a real stop.Separately, a deadline stop that does happen ends the child's run before any turn starts. It writes no turn outcome, so the parent is never notified under
autoorstuck. Of therequestThreadStopcallers, the deadline sweep is the only one with a system reason; the others are allmanual-stop.What changed
apps/server/src/services/threads/thread-lifecycle.ts: the sweep'slistand a new in-transaction re-check share oneisAwaitingTurnStartPastDeadlinecondition.processreturns early, without opening a write transaction, while a turn is running, which is the common case for every long turn the sweep lists. Otherwise, inside one immediate transaction that runs synchronously right after that check, the stop goes ahead only if the listing condition still holds (statusactive, noturn/completedsince the deadline). In that same transaction the thread is markedstoppingand the error is appended. The stop command is dispatched only after the commit, so a turn that completes concurrently is never reported or stopped.dispatchThreadStopCommandnow claims the in-flightthread.stopguard itself, so its four callers (requestThreadStop, the deadline sweep,resendStopsAwaitingDaemon,reconcileDaemonReportedThreads) no longer repeat the claim.failedchild outcome. It reaches the parent underautoandstuck;offstays silent. No synthetic interrupted turn outcome is written: no turn exists, and the child's timeline already shows theprovider_turn_start_deadlineerror and thesystem/thread/interruptedevent.bb guide threads: the parent-notification paragraph now says that a child failing before a turn starts, including a deadline stop, is reported as failed.Not changed:
interruptActiveThreads(host loss) also skips the parent when the thread has no active turn. Reconnect reconciliation revives errored threads that the daemon still reports active, so notifying there would send false alarms on connection blips. That needs its own decision.How you verified
apps/server/test/threads/turn-start-deadline.test.ts, with the real DB and the real sweep:leaves a long turn alone when it completes after the sweep lists it(threadidle/active): the turn completes inside the sweep's per-item host check, which runs afterlistand before the decision. Without the fix, theidlecase writes the deadline error (exactly the production behaviour) and theactivecase moves the thread tostopping. Both pass with the fix.tells the parent when bb stops a child whose turn never started(auto/stuck/off): without the fix,autoandstuckget no parent request. With the fix, the parent gets achild-failedrequest with the message, andoffgets none.pnpm devon a fresh checkout-specific data dir and the scripted echo provider (swallowTurnStart) installed as a local plugin: one parent and three children (auto,stuck,off). Unfixed: all three children ended inerrorwith the deadline error, and the parent got nothing. Fixed: same child outcome, and the parent got one batched notice naming theautoandstuckchildren only. The live parent was itself deadline-stopped, so its queue held the notice behind the existingstoppingwait; delivery to an idle parent is covered by the unit test. The race (bug 1) cannot be hit deterministically live; the unit test forces it, and the production logs above confirm it.turbo run typecheck lint --filter=@bb/server --filter=@bb/templates,turbo run lint:type-aware --filter=@bb/server, and servertest/threads,test/services/threads,test/services/system,test/internal,test/system(139 files, 1462 tests) all pass after the follow-up commit. With the in-transaction re-check disabled, the race test'sactivecase fails, so the re-check stays covered with the early active-turn check in front of it.🤖 Generated with Claude Code