Skip to content

fix(ai): recover missing OpenAI WebSocket response state - #122727

Merged
steipete merged 2 commits into
mainfrom
steipete/openai-ws-error-recovery
Aug 12, 2026
Merged

steipete merged 2 commits into
mainfrom
steipete/openai-ws-error-recovery

Conversation

@steipete

Copy link
Copy Markdown
Contributor

Closes #122717
Related: #122483
Related: #122542

What Problem This Solves

Fixes an issue where direct OpenAI API-key users on an explicit Responses WebSocket mode would receive a terminal agent error when the provider rejected a stale or unavailable previous_response_id, even though the provider explicitly reported that generation had not started and a full-history retry was safe.

The same flattening also hid websocket_connection_limit_reached, so an expired provider connection could fail the turn instead of recovering through the existing SSE fallback.

Why This Change Was Made

OpenAI SDK 6.49.0 declares a flat ResponseErrorEvent, but the live Responses WebSocket service currently sends a wrapped frame with nested error.code and HTTP status. The SDK type-casts that frame without runtime normalization, leaving the structured JSON in WebSocketError.message. OpenClaw previously discarded it and projected every post-send failure as unknown-outcome ambiguity.

This repair normalizes both documented and observed SDK shapes, preserves code/status/param, and allowlists only previous_response_not_found and websocket_connection_limit_reached as safe retries. Those codes close the rejected socket without poisoning the cooldown, rebuild the canonical full-history request, and continue the same user turn over SSE. Every other post-dispatch failure remains replay-unsafe and fails closed.

The nearby provider documentation now matches the shipped product: direct API-key Responses defaults to SSE; auto and websocket-cached reuse a session socket; transient websocket and cached modes retain pre-dispatch SSE fallback; there is no automatic reconnect/retry before dispatch.

Production LOC: +85/-6 (net +79) | Tests: +153/-4 (net +149) | Docs: +24/-17 (net +7)

User Impact

An OpenAI WebSocket session can recover transparently when its prior response state is gone or the provider's 60-minute connection limit is reached. The current user turn completes from full history instead of returning an ambiguity error, and later turns start with a fresh WebSocket state.

Direct API-key defaults do not change: SSE remains the default. This does not ship preconnect or generate:false prewarm; the closed experiment in #122542 remains rejected by latency evidence.

Evidence

Regression and focused proof

  • Pre-fix boundary regression: both official safe-code cases failed with stopReason: "error" because the wrapped error was flattened into post-dispatch ambiguity.
  • node scripts/run-vitest.mjs packages/ai/src/transports/openai-responses-websocket.test.ts packages/ai/src/transports/openai-responses-websocket-client.test.ts packages/ai/src/transports/openai-responses-client.continuation.test.ts packages/ai/src/transports/openai-responses-continuation.test.ts
  • Result: 4 files, 46 tests passed.
  • Targeted oxfmt and oxlint: clean.
  • git diff --check: clean.
  • pnpm docs:list: clean.

Authenticated live OpenAI proof

A direct product-path call on this head deliberately injected a nonexistent response ID into the finalized first payload. The live service returned its current wrapped HTTP 400 previous_response_not_found frame. The same OpenClaw stream call then:

  • rebuilt the payload through buildRequest("full-history");
  • sent no rejected previous_response_id on the SSE retry;
  • emitted the typed continuation-rejected prompt observation;
  • completed over SSE with stopReason=stop and exactly OK;
  • emitted zero warnings.

Observed result:

{
  "stopReason": "stop",
  "text": "OK",
  "payloadBuilds": 2,
  "safeFallbackObserved": true,
  "completedOverSse": true,
  "warningCount": 0
}

A separate raw protocol probe confirmed the service's exact wrapped shape and that a subsequent full request on a fresh connection succeeds.

Dependency and sibling contract

Personally inspected current sibling Codex at 91d6f48992ad:

  • codex-rs/codex-api/src/endpoint/responses_websocket.rs:158-162 defines the two safe codes.
  • responses_websocket.rs:582-648 parses the wrapped error frame and maps those codes to retryable outcomes.
  • responses_websocket.rs:718-725 performs that mapping before ordinary Responses event decoding.
  • codex-rs/core/src/client.rs:1663-1750 owns continuation request and failed-connection replacement.

Also inspected OpenAI SDK 6.49.0:

  • resources/responses/internal-base.ts:24-34 stores a typed ResponseErrorEvent on WebSocketError.
  • resources/responses/ws-base.ts:341-378 JSON-parses and type-casts without runtime validation.
  • The generated ResponseErrorEvent type documents a flat shape, while the live service returned the nested wrapper captured above.

Review

Codex autoreview (gpt-5.6-sol, high): TruffleHog clean; no accepted/actionable findings; patch correctness 0.97.

Provenance: clear. The generic error flattening and ambiguity classification were introduced in f09a33ce4124 by Peter Steinberger in #121687. The current repair is also maintainer-authored.

AI-assisted: yes. The implementation, dependency review, focused tests, live provider proof, and final diff were reviewed by the maintainer.

@clawsweeper

clawsweeper Bot commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

@openclaw-barnacle openclaw-barnacle Bot added the docs Improvements or additions to documentation label Aug 12, 2026
@steipete
steipete marked this pull request as ready for review August 12, 2026 17:04
@openclaw-barnacle openclaw-barnacle Bot added size: M maintainer Maintainer-authored PR labels Aug 12, 2026
@steipete

Copy link
Copy Markdown
Contributor Author

Land-ready evidence on f25eacf1cd2f6500a6921022fafef8f9a5d3b0e9:

  • Focused owner proof: 4 files / 46 tests passed.
  • Exact failed CI gate was repaired by making the generic server error class module-local; check-deadcode-exports.mts now reports zero unused exports.
  • Refreshed exact-head CI: run 31621426246 is green.
  • Authenticated direct OpenAI proof: the live wrapped HTTP 400 previous_response_not_found frame triggered one full-history rebuild and same-turn SSE recovery; result was exactly OK, with no rejected response ID on the retry and zero warnings.
  • Codex autoreview: TruffleHog clean, no actionable findings, patch correctness 0.97; the follow-up one-line dead-code fix re-review was also clean at 0.99.
  • Proof gap: websocket_connection_limit_reached is covered by the same official Codex contract and table-driven boundary regression but was not induced against the live API because the server controls the 60-minute limit.

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Command router queued. I will update this comment with the next step.

@steipete
steipete merged commit 45a5903 into main Aug 12, 2026
104 checks passed
@steipete
steipete deleted the steipete/openai-ws-error-recovery branch August 12, 2026 17:34
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Improvements or additions to documentation maintainer Maintainer-authored PR size: M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: OpenAI WebSocket missing-response errors cannot recover

1 participant