Skip to content

Keep a new message's thread running when its turn start times out - #7031

Merged
ymichael merged 1 commit into
mainfrom
bb/a-timed-out-turn-start-fails-a-live-thread-thr_24uvdtnf8n
Nov 4, 2026
Merged

ymichael merged 1 commit into
mainfrom
bb/a-timed-out-turn-start-fails-a-live-thread-thr_24uvdtnf8n

Conversation

@ymichael

@ymichael ymichael commented Nov 4, 2026

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

When a new message's turn/start got no answer from the provider bridge within the daemon's 30 s JSON-RPC timeout, runtime.runTurn (packages/agent-runtime/src/runtime.ts) rolled back its turn-start state (pendingTurnStarts, markHostedProviderSessionIdle) and rethrew the timeout. The daemon reported it as command_failed, and settleThreadCommandFailure treated that as a refusal: it wrote client/turn/rejected plus system/error ("Command turn.submit failed / JSON-RPC request timed out: turn/start") and failed the run. The provider still had the request. Once it recovered it started the turn, accepted the message and finished, and reconciliation brought the thread back to idle. The false error row and the "failed" copy of the message stayed. #7010 fixed the same bug for steers and listed this turn/start case as a follow-up.

What changed

A timed-out turn start now counts as unconfirmed, reusing #7010's path:

  • packages/agent-runtime: runTurn returns { status: "started" | "unconfirmed" }. On JsonRpcRequestTimeoutError it keeps the pending turn start, so the thread stays live, competing starts are refused and the 2-minute watchdog still applies. It also records the runtime config the provider was sent. Other errors still roll back and throw. startThread with initial input treats an unconfirmed start as a constructed session.

  • apps/host-daemon: an unconfirmed start is reported as turn_start_unconfirmed (TURN_START_UNCONFIRMED_ERROR_CODE in @bb/host-daemon-contract). Staged attachments are kept for unconfirmed steers and starts, because the provider may still read them.

  • apps/server (unconfirmed-steers.ts is renamed to unconfirmed-turn-requests.ts): for turn_start_unconfirmed the server writes nothing and the thread stays active. A late turn/started / turn/input/accepted then runs the turn as usual. If the run has already ended when the report arrives, the request is rejected at once.

  • What settles a start that really was lost. Two things, both read from persisted events at settlement time, so neither depends on server memory and both survive a server restart:

    1. The provider process exits while the start is pending. This is the existing thread-scoped system/error provider_process_exited.
    2. The provider answers the timed-out request with an error. bridge-kit's sendJsonRpcRequest gets an optional afterTimeout handler. The runtime uses it to clear the pending start and emit system/error provider_turn_start_failed. Without this, killing only Codex's app-server left the bridge alive, the bridge's late error went unread, and the thread stayed active (seen live on an intermediate build of this branch).

    On either signal the server fails the run (as it already did for provider_process_exited). It rejects the latest unresolved client/turn/requested after the last completed root turn with reason: turn_start_unconfirmed ("The agent did not start a turn for this message"). The runtime allows only one pending start per thread, so that request is the pending start. Telemetry counts provider_turn_start_failed as start_failed, as the old thread_command_failed was counted.

  • Why not the 2-minute watchdog: it fires on elapsed time only. A provider frozen past 2 minutes (machine sleep, CPU starvation, this repro) still starts the turn afterwards, so settling on the watchdog would bring back the false failure this PR removes. It still warns, and stop still interrupts.

  • plugins/provider-codex: turn/start and thread/compact/start no longer have a 60 s child timeout. When that timeout fired, the bridge rolled back the turn-start request id, so a late turn lost its acceptance. The connection still rejects the request if Codex exits.

  • packages/db: findLatestUnresolvedClientTurnRequestIdAfterSequence.

  • tests/scripted-echo-provider: turnStartErrorAfterMs option for the runtime test.

Real refusals (provider_rpc_error, competing_turn, a turn/start error answered within the timeout, and so on) settle exactly as before.

Steers: they keep #7010's in-memory set. The same DB query can't replace it. At a turn's end, an unresolved steer can still be legitimately in flight: the daemon may be about to start it as the next turn after a stale-steer answer. Rejecting every unresolved steer at that point would write false rejections. That gap is unchanged: after a server restart, a pending unconfirmed steer stays unresolved.

Wire compatibility: no HOST_DAEMON_PROTOCOL_VERSION bump. turn_start_unconfirmed is a new value of the free-form errorCode, and provider_turn_start_failed is a new value of the free-form system/error.code. An older server settles the first as command_failed, which is today's behaviour, and shows the second as an ordinary error row. An older daemon keeps sending command_failed.

How you verified

Live on pnpm start:worktree with real Codex and Claude Code. BEFORE ran on origin/main (a67ada1, before #7010). AFTER ran on this branch with the same data dir, shared via bb connect expose. The only faults were real process freezes and kills (kill -STOP / kill -CONT / kill -9 on literal PIDs).

Thread Build What happened
BEFORE (main): Codex turn/start while app-server frozen main Frozen 45 s. client/turn/rejected + system/error "JSON-RPC request timed out: turn/start" at 30 s. After CONT, Codex started the turn, accepted the message and replied; the error row and the failed message stayed.
BEFORE (main): Claude Code turn/start while claude frozen main Same at 30 s (the message changed the reasoning level, so the bridge waits on claude). After CONT the turn ran; the error row stayed.
AFTER (fix): Codex turn/start while app-server frozen 90s branch Past both the 30 s runtime timeout and the old 60 s bridge timeout. Sampled at 20/35/60/85 s: active, 0 rejections and 0 errors. Daemon logged turn_start_unconfirmed at 30 s. On CONT, turn/started + turn/input/accepted arrived at once, the reply came back, and the thread went idle.
AFTER (fix): Claude Code turn/start while claude frozen 70s branch Same result: active with no rejection or error through the freeze, accepted on CONT, idle.
AFTER (fix): Codex turn/start lost (app-server frozen, then killed) branch active with no rejection or error at 40 s. kill -9 of the app-server produced one system/error provider_turn_start_failed ("…codex app-server exited (code null, signal SIGKILL)"), the message rejected as turn_start_unconfirmed, and the thread error.
AFTER (fix): Claude Code turn/start lost (bridge frozen, then killed) branch active at 40 s. kill -9 of the frozen bridge produced the existing provider_process_exited error, the message rejected as turn_start_unconfirmed, and the thread error.

Tests (each new one fails without the change):

  • packages/agent-runtime/src/runtime.turn-start-watchdog.test.ts: a real scripted bridge that never answers turn/start gives unconfirmed at 30 s, the thread stays live and a competing start is refused. On main's runtime: promise rejected "JsonRpcRequestTimeoutError…" instead of resolving. A second test: a bridge that answers the timed-out request with an error later produces provider_turn_start_failed and clears the pending start.
  • apps/host-daemon/test/command/thread-dispatch.test.ts: an unconfirmed start is reported as turn_start_unconfirmed and keeps its staged attachment.
  • apps/server/test/threads/thread-send-dispatch.test.ts › unconfirmed turn starts: a late start keeps the thread active with no rejection or error, then idle. provider_process_exited and provider_turn_start_failed each produce exactly one turn_start_unconfirmed rejection and their own error row, and fail the run. All three fail on the old settlement (expected 'error' to be 'active').
  • pnpm exec turbo run typecheck lint for server, host-daemon, agent-runtime, db, host-daemon-contract, provider-bridge-protocol, provider-codex and scripted-echo-provider. Run locally: the touched server, daemon and runtime test files; the agent-runtime input-accepted, lifecycle and process-lifecycle suites; the daemon command tests; the Codex bridge tests; and provider-bridge-protocol. CI runs the full suites.

🤖 Generated with Claude Code

AGENT GENERATED

A turn/start that gets no answer within the daemon's 30 s JSON-RPC
timeout is now unconfirmed rather than refused: the runtime keeps its
pending turn start, the daemon reports turn_start_unconfirmed and keeps
staged attachments, and the server writes no rejection or system error
and leaves the thread active. A late turn/started settles it normally.
A start that is really lost is settled from persisted events when the
provider process exits or the provider answers the timed-out request
with an error (provider_turn_start_failed): the latest unresolved
request since the last completed turn is rejected as
turn_start_unconfirmed and the run fails. The Codex bridge no longer
gives up on turn/start or compaction starts after 60 s.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael merged commit 42964f4 into main Nov 4, 2026
50 checks passed
@ymichael
ymichael deleted the bb/a-timed-out-turn-start-fails-a-live-thread-thr_24uvdtnf8n branch November 4, 2026 20:38
ymichael added a commit that referenced this pull request Nov 4, 2026
## Human comments

## What was wrong

#7011 (updateWhenIdle settings) reverted two PRs that had merged about
30 minutes before its commit was created: #7028 (merge child notices
while the parent is blocked, skip `/compact` turns) and #7030 (count
only the latest workflow run in GitHub PR check status). Its body
mentions neither, so it looks accidental.

- #7011 is a single commit, `6b17f1bc04`, whose parent is #7028's merge
commit `920804af63`. Even so, the commit's own diff rewrites 11 files to
their earlier content:
- In #7028's 9 files it is an exact inverse of #7028, apart from four
exports #7011 added to `packages/db/src/data/index.ts`.
- #7030's 3 files in `plugins/github-pr-status/host/` went back to their
pre-#7030 content.
- That pattern fits a branch whose files came from an older checkout and
were committed onto a newer base.
- CI stayed green because each revert also removed the matching tests.
- No other PR was affected. Every file the commit touched was compared
with its earlier versions on main; only these two PRs match.

What users got back after #7011:
- **#7028:** a parent blocked on a question, a starting turn or an
offline host collects a separate notice for every child update again (on
Nov 3 one parent got ten from one child). `/compact` turns on a child
send the parent "No final output was recorded."
- **#7030:** bb shows green PRs as failing ("22 passed, 4 failed") when
GitHub cancelled a duplicate CI run.

## What changed

- Cherry-picks `b0e91c38f8` (#7030) and `920804af63` (#7028) onto
current main with `-x`. #7030 applies unchanged.
- #7028 conflicted only in `coalesced-system-notices.ts`, because #7029
(merged after #7011) added withdrawing a pending notice there. The
result is #7028's version with #7029's withdraw path kept: the pending
lookup has no `now` and a merge writes `windowEndsAt`. This is the
combination #7029 was tested with before #7011 landed.
`child-thread-notifications.ts` resolved to main's existing version,
which already guards on a scheduled row.
- Restores the #7029 test that depends on #7028: an ownership change
undone after its 2 s window but before delivery is withdrawn. Its test
helper comes back with it.
- `internal/events.ts` and `db/data/events.ts` merged cleanly with the
later #7031 and #7033 changes.
- No daemon wire, CLI, config or plugin SDK surface change;
`check-plugin-sdk-version` reports the surface unchanged.

## How you verified

- With main's current code and the restored tests, 7 tests fail. With
this branch they pass:
- `thread-ownership-notices`: "withdraw a move undone after its window
but before delivery"
- `system-notice-dispatch`: "merge later outcomes into a row still
waiting on the parent"
- `child-completion-turn-output`: "does not notify the parent for a turn
that only compacted context"
- `github-pr-status` `git-host` / `batch`: superseded workflow runs,
including on a later checks page and for drafts found through `gh pr
view`
- Server: ownership notices, notice dispatch, child completion output,
notice kinds, child outcomes, parenting, plugin events, queue updates,
sidebar status and child notifications all pass (112 tests).
- `turbo run test` for the GitHub PR Status plugin, `@bb/db` and
`@bb/templates` passes (10/10 tasks). `turbo run typecheck` for server,
db, app and the plugin passes (10/10 tasks).

> AGENT GENERATED

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
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