Skip to content

A transient provider failure bricks the session: make failed recoverable (history-repaired retry, not a dead end) #51

Closed

Description

@JAORMX

Summary

A transient provider failure permanently bricks the session. When a stream fails (e.g. OpenRouter's gpt-5.5 incident on 2026-06-11: response.failed / server_error: internal stream ended unexpectedly), the run terminates with StopError and the session lands in failed — a state with no outgoing transitions. The next prompt errors with session: illegal state transition: RecordUserPrompt from "failed" and the user loses their entire conversation context to a single upstream 5xx.

This is the known, deliberately-deferred "failed-recoverability" gap (CLAUDE.md: "a failed session is NOT resumable and still surfaces the illegal transition"; the compaction-orphan fix removed one trigger but explicitly deferred recoverability). Today's incident demonstrated its real cost.

Observed (live, 2026-06-11)

During an OpenRouter upstream incident (verified independent of mecatl: raw curl streaming requests failed 2/3 with the same response.failed, non-streaming fine, pre-batch build identical):

  1. First model call of a turn fails: agent: start stream: response failed: server_error: internal stream ended unexpectedly (llmresilience retried its 3 attempts; all hit the incident).
  2. Session → failed.
  3. Every subsequent prompt: agent: record user prompt: session: illegal state transition: RecordUserPrompt from "failed".
  4. The only escape is abandoning the session — all accumulated context gone. In one observed case the failure hit AFTER a successful tool-call turn, so real work was lost too.

The resilience layer did its job (3 attempts); the problem is the terminal state when all attempts fail.

Why it's currently this way (constraints a fix must respect)

  • failed was made non-resumable because a failure could leave unpaired history (a mid-dispatch death orphans tool_use/function_call items) — replaying it draws a provider HTTP 400, which fails again: an unrecoverable loop. The cancel path already solved the same problem: Session.Interrupt history-repairs (closes orphaned tool calls with synthetic error results) before returning to idle.
  • Some failures ARE history-poisoned (e.g. the historical compaction-orphan → provider 400). Recovery must not promise success — it must make RETRY possible, with history made structurally valid (session.ValidateToolPairing).
  • Reopen stays completed-only; the cancelled path stays on Interrupt. A failed-recovery seam should be a sibling, not a widening of those.

Proposed direction (for the architect — not prescriptive)

  • A new Session aggregate method (e.g. Recover(): failed → idle) that history-repairs exactly like Interrupt (synthetic error results for orphaned calls, ValidateToolPairing self-check) and clears the stop reason.
  • Drive it from the service layer's loadAndReopen (the single run-entry funnel: completed→Reopen, cancelled→Interrupt, failed→Recover), so every wire surface (gRPC Converse, HTTP SSE, ACP) gets it for free.
  • The recovered session must replay cleanly: the failed turn's partial state (if any was recorded) must not leave dangling items.
  • Tests: state-machine transitions; an e2e through server.Service proving a failed session accepts the next prompt and runs (mockllm: first run errors terminally, second run succeeds); an adversarial case where the failure orphaned a tool call mid-dispatch and the repair closes it; guard that a still-poisoned history (if irreparable) fails the run, not the recovery.
  • Update the stale docs: CLAUDE.md's "failed is NOT resumable" gotcha, the run-entry-path bullet, and any design notes that record recoverability as deferred.

Context

Found during the 2026-06-11 OpenRouter gpt-5.5 streaming incident, debugging live sessions. Sibling of the resilience work (mid-stream stall watchdog, breaker) — this is the last leg: when resilience is exhausted, the session should degrade to "retryable," not "dead."

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions