Skip to content

feat(runtime)!: retry a turn after a transient provider fault and settle paused on exhaustion (RR4, ADR-0040) - #120

Merged
lg2m merged 2 commits into
fix/rr5-retry-afterfrom
feat/rr4-turn-recovery
Sep 22, 2026
Merged

lg2m merged 2 commits into
fix/rr5-retry-afterfrom
feat/rr4-turn-recovery

Conversation

@lg2m

@lg2m lg2m commented Sep 22, 2026

Copy link
Copy Markdown
Collaborator

Stacked on #118 (RR5). RR4 of docs/plans/run-reliability.md; audit finding R01. Protocol bump; independent review requested per workflow.md § 4 (touches sessions/).

Behaviour

A provider fault after the provider's own retry ledger was spent ended the run as Failed, even when every turn so far was durable and the fault was the provider's moment (529, 503, gateway 500, mid-stream connection loss) rather than the request's. Twelve audited runs — 4.5 h of completed turns — died this way; the operator's recovery was to re-prompt "continue" with worse context than the loop already had. Codex, OpenCode, Pi, and fx all retry the turn.

Now the run loop owns turn recovery (ADR-0040; the provider keeps ADR-0005's ownership of resends within one request):

  • On ProviderUnavailable / ProviderRateLimited / ProviderTransport, or a stream that ends after events without a terminal event: commit the partial assistant turn, emit TurnRetrying, sleep under TurnRecoveryPolicy (2 s → 60 s; cancellation and the run deadline cut it short), re-issue the turn with a continue notice after any partial text.
  • Allowance MAX_TURN_RETRIES = 5 per turn, reset on a completed turn — a 3-hour run survives many isolated blips; a down provider pauses the run in ~2 min.
  • Exhaustion settles RunOutcome::Paused, not Failed: transcript durable, next prompt continues with a notice naming the pause.
  • Auth / invalid request / protocol / output truncation still fail at once.

Protocol 25 → 26

run_turn_retrying event; paused outcome, status, RunPause. MAX_TURN_RETRIES lives in qq-protocol so clients render against the same bound. v26/ goldens (event_run_turn_retrying, event_run_finished_paused); harbor traces bumped. No store schema change; 'paused' joins every terminal-status SQL/match list (two of which had also omitted 'budget_exhausted' — a latent bug, now fixed).

Surfaces: client reducer warns per retry and on pause; TUI renders paused like budget_exhausted; headless maps paused → task_failed (exit 1) with the pause named, so supervisors see no new code; a paused sub-agent returns a tool error to its parent; a paused compaction step stops the fold.

Verification

  • transient_faults_retry_the_turn_and_exhaustion_pauses: three transient kinds × 6 sends → paused; auth fails once with no resend; mid-stream 529 ×2 then completion yields part0 part1 done with the continue notice in the third request.
  • the_turn_retry_allowance_resets_on_a_completed_turn: 9 faults across 9 turns, never a second attempt, run completes.
  • a_retry_sleep_yields_to_cancellation_and_the_deadline (paused clock).
  • Session fixtures that asserted Failed for an offline provider now assert Paused and n + MAX_TURN_RETRIES sends; loaded_runtime uses a 1 ms policy so the suite stays ~15 s. Replaces the_run_loop_never_resends_a_turn.
  • cargo test --workspace --exclude xtask green (qq-core 690, qq 172, qq-tui 308, protocol 48+…); minimal provider profile green; clippy -D warnings and fmt clean.

Docs

ADR-0040 accepted; ADR-0005 marked superseded in part; architecture.md § run loop and perf table; protocol.md version history, event table, outcomes; headless-contract.md exit table; ledger and root rows.

…tle paused on exhaustion (RR4, ADR-0040)

A provider fault after the provider's own retry ledger was spent ended the
run as Failed, even when every turn so far was durable and the fault was
the provider's moment rather than the request's. Twelve audited runs
(4.5 h of completed turns) died this way on 529/503/500 and mid-stream
transport loss; the operator's recovery was to re-prompt "continue" with
worse context than the loop already had. Every reference harness retries
the turn.

The run loop now owns turn recovery (ADR-0040; the provider keeps
ADR-0005's ownership of resends within one request). On
ProviderUnavailable, ProviderRateLimited, or ProviderTransport, or a
stream that ends after events without a terminal event, the loop commits
the partial assistant turn, emits TurnRetrying, sleeps under
TurnRecoveryPolicy (2 s doubling to 60 s; cancellation and the run deadline
cut it short), and re-issues the turn with a continue notice after any
partial text. The allowance is MAX_TURN_RETRIES (5) per turn and resets on
a completed turn. Exhaustion settles RunOutcome::Paused: durable transcript,
session accepts the next prompt with a notice naming the pause.
Non-transient kinds still fail at once.

PROTOCOL_VERSION 25 -> 26: run_turn_retrying event; paused outcome, status,
and RunPause; v26 goldens and harbor traces. No store schema change;
'paused' joins every terminal-status list (two of which had also omitted
'budget_exhausted'). Client reducer warns per retry; TUI renders paused
like budget_exhausted; headless maps paused to task_failed with the pause
named; a paused child returns a tool error to its parent; a paused
compaction step stops the fold.

BREAKING: protocol bump; older clients reject the new event and outcome
tags. Direct-runtime callers see the last fault as Failed with "paused
after N turn retries" appended.

Tests: transient_faults_retry_the_turn_and_exhaustion_pauses,
the_turn_retry_allowance_resets_on_a_completed_turn,
a_retry_sleep_yields_to_cancellation_and_the_deadline; session fixtures
that asserted Failed for an offline provider now assert Paused and the
extra sends. Replaces the_run_loop_never_resends_a_turn.
@lg2m
lg2m added this pull request to stack #121 September 22, 2026 04:21
… ADR-0040

The perf fixture's faulting provider fails every stream before its first
event and the gate asserted exactly one provider entry per failed run. With
run-owned turn recovery the same run now sends 1 + MAX_TURN_RETRIES times
and settles paused, which is the value the gate should protect: anything
above it means a second retry owner above the loop. The fixture uses a 1 ms
recovery policy so the benchmark measures sends, not sleeps.
@lg2m
lg2m merged commit 381a06c into main Sep 22, 2026
4 checks passed
@lg2m
lg2m deleted the feat/rr4-turn-recovery branch September 22, 2026 05:28
lg2m added a commit that referenced this pull request Sep 22, 2026
…rotocol 27

Protocol 26 was claimed by #120 (turn recovery, ADR-0040) while #122 was open, so the effort pin moves to protocol 27. v26/ goldens are #120's; v27/ is regenerated from them plus command_set_session_effort and receipt_session_effort_set. Harbor trace fixtures bump 26 -> 27. Store schema stays 34 (main is on 33). MAX_TURN_RETRIES kept.

Also picks up #126-#128 (RR6, RR7, onboarding); no overlap.

Verified on the identical tree as a rebase: cargo test --workspace, fmt --check, clippy -D warnings all green.
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