Skip to content

Stop the turn-start deadline only when its condition still holds, and tell the parent - #7328

Merged
ymichael merged 2 commits into
mainfrom
bb/fix-turn-start-deadline-killing-threads-as-long-thr_uukmxggety
Nov 10, 2026
Merged

ymichael merged 2 commits into
mainfrom
bb/fix-turn-start-deadline-killing-threads-as-long-thr_uukmxggety

Conversation

@ymichael

@ymichael ymichael commented Nov 10, 2026 •

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

turnStartDeadlineSweep lists every thread that has been active for more than 5 minutes with no recent turn/completed. That includes every long turn. process then runs after at least one await (the item-sweep runner yields with setImmediate once 8 ms have passed) and checks only getActiveTurnId(...) !== null. When a long turn completes in that gap, process finds no active turn, writes provider_turn_start_deadline and requests a stop. In the reported child thread (thr_v5rh98dc7y), turn/completed was written at 15:34:55.778 and the server logged Stopping a run whose turn never started at .886, followed by no transition for stop.requested from status idle at .910. The thread had already settled to idle, so the stop was rejected. Nothing was killed: at 15:39:46 the child's background waiter (started 15:34:51) was still running and checks3.done did not exist yet. The only damage was the false "so bb stopped it" error. If the thread is still active when 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 auto or stuck. Of the requestThreadStop callers, the deadline sweep is the only one with a system reason; the others are all manual-stop.

What changed

  • apps/server/src/services/threads/thread-lifecycle.ts: the sweep's list and a new in-transaction re-check share one isAwaitingTurnStartPastDeadline condition. process returns 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 (status active, no turn/completed since the deadline). In that same transaction the thread is marked stopping and the error is appended. The stop command is dispatched only after the commit, so a turn that completes concurrently is never reported or stopped.
  • dispatchThreadStopCommand now claims the in-flight thread.stop guard itself, so its four callers (requestThreadStop, the deadline sweep, resendStopsAwaitingDaemon, reconcileDaemonReportedThreads) no longer repeat the claim.
  • The same path now queues a parent notification ("was stopped by bb because its provider did not start the turn within 5 minutes") as a failed child outcome. It reaches the parent under auto and stuck; off stays silent. No synthetic interrupted turn outcome is written: no turn exists, and the child's timeline already shows the provider_turn_start_deadline error and the system/thread/interrupted event.
  • 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.
  • No server/daemon wire change.

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 (thread idle / active): the turn completes inside the sweep's per-item host check, which runs after list and before the decision. Without the fix, the idle case writes the deadline error (exactly the production behaviour) and the active case moves the thread to stopping. Both pass with the fix.
    • tells the parent when bb stops a child whose turn never started (auto / stuck / off): without the fix, auto and stuck get no parent request. With the fix, the parent gets a child-failed request with the message, and off gets none.
  • Live, with pnpm dev on 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 in error with the deadline error, and the parent got nothing. Fixed: same child outcome, and the parent got one batched notice naming the auto and stuck children only. The live parent was itself deadline-stopped, so its queue held the notice behind the existing stopping wait; 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 server test/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's active case fails, so the re-check stays covered with the early active-turn check in front of it.

AGENT GENERATED

🤖 Generated with Claude Code

… 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
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
ymichael merged commit 233a7cc into main Nov 10, 2026
52 checks passed
@ymichael
ymichael deleted the bb/fix-turn-start-deadline-killing-threads-as-long-thr_uukmxggety branch November 10, 2026 04:16
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