Skip to content

Ignore a stop result that a newer turn request overtook - #6922

Closed
ymichael wants to merge 1 commit into
mainfrom
bb/ignore-stale-stop-results-thr_wefex9vhsi
Closed

ymichael wants to merge 1 commit into
mainfrom
bb/ignore-stale-stop-results-thr_wefex9vhsi

Conversation

@ymichael

@ymichael ymichael commented Nov 3, 2026 •

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

A thread.stop result was applied to whatever run the thread had when the result arrived, not to the run the stop was sent for. Most stops first mark the thread stopping, and a stopping thread refuses sends, so nothing can come after them. The re-stop that a same-instance daemon reconnect sends for a thread it already stopped (reconcileDaemonReportedThreads) leaves the thread idle, so the user can send a new message while that stop is still in flight. When the stop is then acknowledged, settleThreadStopCommandResult settles the thread's background tasks and finalizes it: the new run goes active → idle, its turn is interrupted, and its background tasks are marked done while the provider still runs them. This is the stale-stop guard from #6552, split out on its own (investigation in the parent thread of this work).

What changed

  • settleThreadStopCommandResult ignores a stop result (success or failure, interrupt or release) when a turn was requested at or after the time the stop was sent. It has no effect: no finalize, no background-task settlement, no interaction interrupt.
  • Exception: if the thread is stopping again, the result still applies. When the user stops the newer run while the old stop is in flight, the new stop is not dispatched (the old one holds the in-flight claim), so the old result is the only thing that ends it. A stopping thread refuses sends, so a request newer than an ordinary stop cannot exist.
  • New @bb/db query hasTurnRequestSince (one thread's client/turn/requested rows, by created_at).
  • Unlike Settle every turn input and every started turn #6552's version, this does not depend on the input ledger.

No wire, schema, CLI or SDK change.

How you verified

Two regression tests in apps/server/test/internal/background-task-reconciliation.test.ts, both driving the real reconnect flow (session open with the thread active → re-stop queued → user sends → new turn starts with a running background task → old stop acknowledged):

  • "leaves a run requested after a reconnect re-stop running when that stop is acknowledged"
    • before (origin/main): fails, thread is idle
    • with a guard that skips only the finalize: fails, the new run's task gets item/backgroundTask/completed
    • after: passes, thread active, task still open
  • "finishes a stop the user requested on that newer run when the older re-stop is acknowledged"
    • without the stopping exception: fails, thread stuck stopping
    • after: passes, thread idle

Commands:

  • pnpm exec turbo run typecheck lint --filter='...[origin/main]': 11/11 tasks pass
  • pnpm exec turbo run test --filter=@bb/server: 3870 passed, 1 failed in test/app/install-machine-script.test.ts. That file fails on origin/main as well, with a different test each run; unrelated.

🤖 Generated with Claude Code

AGENT GENERATED

A stop sent while the thread is not marked stopping (the re-stop on a
same-instance reconnect) can be acknowledged after the user has already
sent a new message. Its result then finalized the new run: the thread
went idle, its turn was interrupted and its background tasks settled.
A stop result now has no effect when a turn was requested after the stop
was sent, unless the thread is stopping again, in which case the result
still ends that stop.

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 stale stop reply changes nothing visible (the daemon answers the stop before starting the next turn). The real effect found was a ~1.75 s idle flicker of the new run in a rare reconnect case, which doesn't justify two new guards in the core stop and completion paths.

@ymichael ymichael closed this Nov 3, 2026
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