Skip to content

fix(ai): continue cached OpenAI WebSocket turns - #122483

Merged
steipete merged 1 commit into
mainfrom
steipete/slow-token-streaming-34516b
Aug 12, 2026
Merged

steipete merged 1 commit into
mainfrom
steipete/slow-token-streaming-34516b

Conversation

@steipete

@steipete steipete commented Aug 12, 2026 •

Copy link
Copy Markdown
Contributor

Closes #122482
Related: #122194

What Problem This Solves

Fixes an issue where direct OpenAI API-key users who selected cached WebSocket transport would reuse the socket but still resend the full conversation on each warm turn. Real provider output was rejected as changed history, so previous_response_id continuation never activated.

Why This Change Was Made

Continuation matching now compares JSON values independently of object insertion order while preserving normal JSON omission semantics. At the assistant replay boundary, it removes only provider delivery metadata that reconstructed history cannot contain (id, status, output_text.annotations, and output_text.logprobs) while keeping semantic fields such as role, phase, text, reasoning shape, and tool-call data fail-closed.

The direct API-key default remains SSE. This PR does not add WebSocket preconnect/prewarm or the broader SSE continuation cache proposed in #122194.

Production LOC: +31/-14 (net +17) | Tests: +43/-7 (net +36) | Docs: +2/-2 (net 0)

User Impact

Explicit websocket-cached and auto users on the built-in OpenAI Responses runtime now get the continuation behavior those modes promise: one session socket, previous_response_id, and suffix-only input when history is unchanged. Semantic history or request changes still invalidate continuation and send a full request.

The provider reference now accurately documents SSE as the direct API-key default and lists all supported explicit transport modes.

Evidence

  • Pre-fix production-path regression: a realistic completed OpenAI response with citation annotations, logprobs, and provider field order caused the second request to omit previous_response_id; the new regression fails on the old comparator and passes with this fix.
  • Focused exact-head tests: node scripts/run-vitest.mjs packages/ai/src/transports/openai-responses-websocket.test.ts packages/ai/src/transports/openai-responses-websocket-client.test.ts -> 2 files, 29 tests passed.
  • Docs/static proof: full MDX check (770 files), docs/source formatting, and git diff --check passed.
  • Changed-surface gate: core, coreTests, and docs passed locally after the remote Crabbox/Testbox backend was unavailable; core/test typechecks, lint, max-lines ratchet, deadcode, plugin boundaries, import cycles, schema and package guards were green.
  • Exact-head pull-request CI: run 31569182042 completed successfully on dabe607efda854be74cc70e7432e19c321f15e6c.
  • Authenticated live OpenAI A/B on the reviewed head: 48/48 complete; no WebSocket fallback, warning, or failure; cached WebSocket was the actual transport for 16/16 sequential turns; 12/12 warm turns reused the connection and continued with previous_response_id.
  • Warm sequential medians: first provider event 135.839 ms cached WebSocket vs 333 ms SSE; total duration 1082.076 ms vs 1109.844 ms; TTFT was comparable at 963.535 ms vs 920.231 ms, so this is a correctness/warm-start repair rather than a claim that WebSocket is universally faster.
  • Final Codex autoreview: clean; TruffleHog clean; patch correct 0.98.
  • Dependency contract review: latest sibling Codex typed continuation/suffix logic plus installed OpenAI SDK Responses WebSocket serialization and output types were inspected directly.

AI-assisted: yes. The implementation, 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 06:12
@openclaw-barnacle openclaw-barnacle Bot added size: S maintainer Maintainer-authored PR labels Aug 12, 2026
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. labels Aug 12, 2026
@clawsweeper

clawsweeper Bot commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed August 12, 2026, 2:19 AM ET / 06:19 UTC.

ClawSweeper review

What this changes

The PR normalizes cached OpenAI Responses WebSocket replay comparisons so equivalent reconstructed turns continue with a previous response ID and only newly appended input.

Regression provenance

Possible regression — probable (reproduction; reviewed change). No predecessor PR is attributed.

Merge readiness

⚠️ Ready for maintainer review - 1 item remains

Keep open for normal maintainer merge review: this is a focused repair at the cached-WebSocket owner, with no actionable patch defect found, but exact-head CI and independent SDK-source inspection remain incomplete in this environment.

Priority: P2
Reviewed head: dabe607efda854be74cc70e7432e19c321f15e6c

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A narrowly scoped owner-boundary repair with meaningful integration coverage and recorded live evidence; independent SDK-source and structured-autoreview checks remain unavailable here.
Proof confidence 🐚 platinum hermit (4/6) Not applicable: The external-contributor proof gate does not apply to this maintainer-authored PR; its body nevertheless records an authenticated live OpenAI continuation run.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: The external-contributor proof gate does not apply to this maintainer-authored PR; its body nevertheless records an authenticated live OpenAI continuation run.
Evidence reviewed 7 items Current failure owner: Current main uses raw JSON serialization for both request and replay comparisons, so insertion order and provider-only response fields can reject continuation.
Replay contract: The request replay builder reconstructs assistant output with empty annotations while preserving text, role, status, identifier, and phase, establishing the source-level mismatch the patch addresses.
Patch boundary: The branch canonicalizes JSON wire values and removes only annotations and logprobs from assistant output replay; phase and other semantic fields remain compared fail-closed.
Findings None None.
Security None None.

How this fits together

The OpenAI Responses transport builds an agent conversation request, then selects SSE or the official OpenAI WebSocket path. Cached WebSockets compare the reconstructed conversation against the prior completed response to choose suffix continuation or a full-history request.

flowchart LR
  A[Agent conversation] --> B[Responses request builder]
  B --> C[Cached WebSocket transport]
  C --> D{Replay matches?}
  D -->|yes| E[Previous response ID and suffix]
  D -->|no| F[Full history request]
  E --> G[OpenAI Responses API]
  F --> G
Loading

Before merge

  • Resolve merge risk (P1) - The OpenAI SDK 6.49.0 source/types were unavailable locally and network access was unavailable; autoreview also could not start because TruffleHog is absent. Exact-head CI and the PR's recorded live proof should remain the merge evidence.
Agent review details

Security

None.

PR surface

Source +17, Tests +36, Docs 0. Total +53 across 4 files.

View PR surface stats
Area Files Added Removed Net
Source 1 31 14 +17
Tests 2 43 7 +36
Docs 1 2 2 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 4 76 23 +53

Review metrics

Metric Value Why it matters
Production versus test delta production +17, tests +36, docs 0 net The added production code is the canonical JSON-wire comparator and narrow replay metadata normalization; the remaining change is focused regression coverage.

Stored data model

Persistent data-model change detected: serialized state: packages/ai/src/transports/openai-responses-websocket.test.ts, serialized state: packages/ai/src/transports/openai-responses-websocket.ts, vector/embedding metadata: packages/ai/src/transports/openai-responses-websocket-client.test.ts, vector/embedding metadata: packages/ai/src/transports/openai-responses-websocket.test.ts. Confirm migration or upgrade compatibility proof before merge.

Merge-risk options

Maintainer options:

  1. Decide the mitigation before merge
    Merge only after exact-head CI is green, retaining the narrow delivery-metadata whitelist and the existing full-history fallback for every other replay difference.
  2. Pause or close
    Do not merge this PR until maintainers decide whether the risk is worth taking.

Technical review

Best possible solution:

Merge only after exact-head CI is green, retaining the narrow delivery-metadata whitelist and the existing full-history fallback for every other replay difference.

Do we have a high-confidence way to reproduce the issue?

Yes, from source: current main compares provider output against reconstructed replay with raw JSON serialization, while the replay builder deliberately emits empty annotations; the supplied issue also gives a focused two-turn path.

Is this the best way to solve the issue?

Yes. The cache owner makes the continuation decision, and this repair normalizes only known reconstruction differences while retaining fail-closed checks for semantic changes.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against b8ddddd702aa.

Labels

Label changes:

  • add P2: This repairs a bounded continuation regression for explicitly selected OpenAI WebSocket modes without preventing normal responses.
  • add rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🐚 platinum hermit.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Not applicable: The external-contributor proof gate does not apply to this maintainer-authored PR; its body nevertheless records an authenticated live OpenAI continuation run.

Label justifications:

  • P2: This repairs a bounded continuation regression for explicitly selected OpenAI WebSocket modes without preventing normal responses.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Not applicable: The external-contributor proof gate does not apply to this maintainer-authored PR; its body nevertheless records an authenticated live OpenAI continuation run.

Evidence

What I checked:

Likely related people:

  • steipete: Current comparison history, the submitted repair, and the related merged continuation work are all attributed to Peter Steinberger in local history. (role: recent area contributor; confidence: high; commits: 38a5390e2e29, f09a33ce4124, dabe607efda8; files: packages/ai/src/transports/openai-responses-websocket.ts, packages/ai/src/transports/openai-responses-client.ts)

Rank-up moves

Optional improvements that raise the rating; they are not merge blockers.

  • Let exact-head CI finish before merging.

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

Workflow

  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

@steipete
steipete merged commit 0c8c8d9 into main Aug 12, 2026
160 of 161 checks passed
@steipete
steipete deleted the steipete/slow-token-streaming-34516b branch August 12, 2026 06:25
@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 P2 Normal backlog priority with limited blast radius. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: S status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Cached OpenAI WebSocket turns resend full history

1 participant