Skip to content

Preserve stale sends by re-steering the live turn - #2379

Merged
ymichael merged 2 commits into
mainfrom
bb/post-merge-fix-enforce-shared-single-turn-admiss-thr_yzpzi26xca
Aug 25, 2026
Merged

ymichael merged 2 commits into
mainfrom
bb/post-merge-fix-enforce-shared-single-turn-admiss-thr_yzpzi26xca

Conversation

@ymichael

Copy link
Copy Markdown
Collaborator

What was wrong

A stale turn.submit could fall back to a new turn without re-reading the shared runtime's authoritative active and pending-start state. The runtime also did not enforce that state at new-turn admission, so providers without an equivalent local defense could accept overlapping logical turns; rejecting only at admission fixed safety but turned the ordinary stale-target race into a user-visible resend. This is the residual class-wide case from #2241 and #2242.

What changed

  • Enforce single-turn admission in the shared runtime before provider-session preflight and again immediately before recording a pending start.
  • After a stale steer, re-read live runtime state and retry once against a newer active turn.
  • Wait up to the existing five-second bound when a newer accepted turn has not published its ID, then steer it; start a new turn only when the thread is genuinely idle.
  • Keep recovery bounded and fail closed if the target changes again or the pending start remains unresolved.
  • Bump HOST_DAEMON_PROTOCOL_VERSION from 165 to 166 for the changed turn.submit semantics.

How you verified

  • Focused active-turn and pending-start admission regressions failed before and passed after.
  • Focused newer-active-turn and newer-pending-turn daemon regressions failed with new-turn before the recovery and passed with steer after it.
  • pnpm exec turbo run test --filter=@bb/agent-runtime --filter=@bb/host-daemon --filter=@bb/host-daemon-contract --force — 318 runtime, 556 daemon, and 51 contract tests passed.
  • pnpm exec turbo run typecheck --filter=@bb/agent-runtime --filter=@bb/host-daemon --filter=@bb/host-daemon-contract --force — all affected packages and upstream tasks passed.
  • pnpm exec turbo run build --filter=@bb/host-daemon --force — daemon and upstream builds passed.

Fixes #2241

AGENT GENERATED

@ymichael
ymichael merged commit 2ddd1b8 into main Aug 25, 2026
15 checks passed
@ymichael
ymichael deleted the bb/post-merge-fix-enforce-shared-single-turn-admiss-thr_yzpzi26xca branch August 25, 2026 04:38
SawyerHood added a commit that referenced this pull request Aug 25, 2026
…he run

Root cause: a turn submission the thread could not take was a generic
failure at every layer. `bb thread tell --mode auto` into a thread whose
turn the daemon still holds (a start whose turn/started is in flight, the
5s pending-start wait, #2379's admission assert) or whose provider refuses
input mid-run (pi while it compacts) came back as `command_failed`; the
server appended system/error, applied `run.failed` (status `error` while
the native transcript advanced), and dropped the message. The queue never
drained because it only drains from `idle` -- until `thread stop` released
the runtime. The pi bridge amplified a refusal: a rejected prompt raised a
session error (`onDone(error)` -> provider.error {settlesTurn}) and
resolved a failed prompt settlement (-> a failed `turn.boundary` with
claimIfIdle), either of which closed the OPEN turn from under pi, after
which every later send resolved to `start` and was refused identically
(the repeated error blocks). On the reporter's nightly (7e10ea4, before
prompt" from the in-process pi SDK session; on main pi runs as an RPC
child whose `prompt` answers an error only for refusals that precede its
preflight (compaction), and the post-preflight guard is swallowed. There
is no bb retry loop: each block was one new send hitting the same state.

What changed:
- provider-bridge-protocol: `BRIDGE_JSON_RPC_ERRORS.TURN_BUSY` (-32004)
  for a turn/start or turn/steer the provider refuses because it is
  mid-run (or compacting) and will not queue the input; documented in the
  protocol hygiene rules.
- provider-pi bridge: a rejected prompt/steer dispatch settles nothing --
  no session error, no prompt settlement (the rejection is the whole
  report; the live run settles itself) -- and keeps `isProcessing` as it
  was. Pi's compaction refusal, the bridge's own compaction-active steer
  refusal, and (defensively, for a pi that surfaces it) the `Agent.prompt`
  guard answer `TURN_BUSY`. The fake pi's `/busy` knob models the
  compaction refusal pi actually sends over RPC.
- agent-runtime: `AgentRuntimeTurnBusyError` (code `thread_turn_busy`) for
  the competing-start refusal and for `TURN_BUSY` rejections of turn/start
  and turn/steer; turn state is left untouched. A refused start re-marks
  the session idle like any other refusal (the runtime holds no turn for
  it), so the idle reaper can still release it; a provider that is in fact
  mid-run re-marks it busy with the turn events it emits.
- host-daemon-contract: `THREAD_TURN_BUSY_ERROR_CODE`, the one wire
  spelling, and protocol version 170 -> 171 (new turn.submit failure code
  on the wire).
- host-daemon: the pending-start refusal is an `ExpectedCommandDispatchError`
  with that code; `thread_turn_busy` is an expected RPC failure, checked
  against the runtime error's literal type at compile time.
- server: a `thread_turn_busy` turn.submit failure records
  client/turn/rejected but appends no system/error and applies no
  run.failed -- the live turn owns the status. The input is parked, never
  lost: a direct send goes to the thread's queue (raw payload, no envelope,
  no resolved mention context, arrival order); a consumed queued group is
  restored at the head of the queue with its ids and grouping
  (`restoreConsumedQueuedThreadMessagesInTransaction`) on both drain paths
  -- the idle fast path and a queued send into an active thread, which
  hands its consumed rows to `sendThreadMessage` -- so "do X" then "stop,
  do Y" keep their order. The restore assigns fresh sort keys ahead of the
  current head: the consume usually empties the queue, and a row queued
  during the round trip takes the initial key the consumed head had, so
  re-inserting the stored keys tied on `sortKey` and let the random id put
  the newcomer first or inside the group. A parent system message
  (child-completed notice) is held in `deferred_thread_messages` with its
  taxonomy, stamped with the turn that refused it; the flush skips it
  while that is still the live turn -- one attempt per turn, not one per
  sweep tick -- and moves on to the rows behind it, so a steer deferred
  behind the notice (#1650) is not parked for the rest of a long turn. The
  re-queue hook is scoped to turn.submit dispatches, matching the
  settlement exemption, and a failure handler that throws is logged rather
  than left as an unhandled rejection.
- CLI/skill/guide: `--mode auto` and the busy fallback are described on
  the discoverable surfaces.

Verification: regression tests fail on the previous source and pass after
-- pi bridge (busy steer/start answer TURN_BUSY with no turn.boundary and
no session error, including a turn/start refused during a held run),
agent-runtime (typed busy on refused start/steer, live turn kept, refused
start still reapable), host-daemon (typed expected code), db (restore
keeps ids and grouping, clears claims, lands ahead of a row queued into
the emptied queue and is what the next drain claims), server (busy send
keeps the thread active and queues the raw input; busy queued auto-send
restores the group at the head; busy queued send into an active thread
restores the group with its grouping ahead of the trailing row; busy
parent system message is held with its taxonomy, not retried while the
turn is live, steered into the next turn, and does not block a send
deferred behind it), contract (version 171). Typecheck and the full test
suites of every touched package pass via turbo.

Fixes #2370

Co-Authored-By: Claude <[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.

Prevent competing auto turns and false Codex session rebuilds

1 participant