Skip to content

fix(agent): route codex v2 compaction through websockets - #7202

Merged
can1357 merged 3 commits into
mainfrom
farm/65f9d9f8/fix-codex-v2-websocket-compaction
Aug 1, 2026
Merged

can1357 merged 3 commits into
mainfrom
farm/65f9d9f8/fix-codex-v2-websocket-compaction

Conversation

@roboomp

@roboomp roboomp commented Jul 31, 2026

Copy link
Copy Markdown
Collaborator

Repro

An openai-codex-responses V2 compaction probe with preferWebsockets: true reproduced the split as websocketConnections: 0, sseRequests: 1, followed by response.failed (data_residency_mismatch). The regression scenario is exercised with bun test packages/agent/test/remote-compaction.test.ts --test-name-pattern "V2 compaction reuses|V2 compaction falls back".

Cause

requestCompactionV2Streaming() in packages/agent/src/compaction/compaction-v2-streaming.ts opened its own HTTP fetch stream instead of entering the WebSocket-first selector in packages/ai/src/providers/openai-codex-responses.ts; SummaryOptions and the coding-agent compaction callsites also omitted the configured WebSocket preference.

Fix

  • Add a raw provider-native Codex compaction event stream that reuses the live session WebSocket and replays transport failures over SSE.
  • Route Codex V2 compaction through that stream while retaining the existing SSE V2 and V1 remote fallbacks.
  • Propagate preferWebsockets through manual, automatic, and advisor compaction paths.
  • Cover live-socket reuse, WebSocket-to-SSE fallback, metadata, and existing V2-to-V1 error behavior.

Verification

bun test packages/agent/test/remote-compaction.test.ts passed: 44 tests, 211 assertions. bun test packages/ai/test/openai-codex.test.ts packages/ai/test/openai-codex-stream.test.ts packages/ai/test/openai-codex-responses-lite.test.ts packages/ai/test/openai-codex-reset.test.ts passed: 142 tests, 669 assertions. bun run check:ts passed for all workspaces. bun check fails on main because the environment lacks cmake for audiopus_sys; the pre-publish gate was skipped after confirming the identical failure in a clean origin/main worktree.

Fixes #7198

- Reused the live Codex provider session for WebSocket-first V2 compaction.
- Fell back to SSE V2 on WebSocket transport failure before the existing V1 fallback.
- Propagated the configured WebSocket preference through manual, automatic, and advisor compaction paths.
- Added transport reuse and fallback regression coverage.

Fixes #7198

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5283c4c99b

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +1661 to +1663
const fallback = await openCodexSseTransport(model, requestContext, requestSetup, options, state);
if (state) state.lastTransport = fallback.transport;
yield* fallback.eventStream;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Reset compaction collection state before SSE replay

When a WebSocket compaction attempt yields response.output_item.done and then the socket fails before the terminal response.done/response.completed, this fallback starts a fresh SSE replay but the caller's collectCompactionV2Events state still contains the compaction item from the failed WebSocket attempt. The successful SSE replay then adds a second compaction item, causing finishCompactionV2Collection to reject with “expected exactly one compaction output item” even though the replay succeeded; buffer/suppress partial WebSocket compaction events until terminal, or restart the collection on fallback.

Useful? React with 👍 / 👎.

- Buffered WebSocket compaction events until terminal completion.
- Discarded buffered events when transport failure triggers an SSE replay.
- Added coverage for failure after a partial compaction output item.

Fixes #7198
@roboomp

roboomp commented Jul 31, 2026

Copy link
Copy Markdown
Collaborator Author
  • Buffered WebSocket compaction events until the attempt reaches terminal completion.
  • Discarded partial WebSocket events before starting the SSE replay, preventing duplicate compaction items.
  • Added a regression test for response.output_item.done followed by WebSocket failure and successful SSE replay; all 186 affected tests and bun run check:ts pass.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6c205fab9f

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

completed = true;
return;
}
for (const event of bufferedEvents) yield event;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve turn-state metadata from compaction WebSockets

When a mid-turn Codex V2 compaction runs over WebSocket and the server includes a response.metadata event with a refreshed x-codex-turn-state, this path only replays the buffered events into collectCompactionV2Events, which ignores metadata. The normal Codex stream updates websocketState from response.metadata, and resetOpenAICodexHistoryAfterCompaction intentionally preserves turnState for mid_turn, so the next tool-loop continuation can send the stale pre-compaction turn state; update requestContext.websocketState from metadata while draining these events.

Useful? React with 👍 / 👎.

- Applied response.metadata turn-state/models-etag refreshes while draining Codex V2 compaction WebSocket events.
- Deferred the update until the WebSocket attempt succeeds so a discarded attempt cannot leak turn state.
- Added coverage for a mid-turn compaction refreshing x-codex-turn-state.

Fixes #7198
@roboomp

roboomp commented Jul 31, 2026

Copy link
Copy Markdown
Collaborator Author
  • Applied response.metadata x-codex-turn-state/x-models-etag refreshes to requestContext.websocketState while draining Codex V2 compaction WebSocket events, matching the normal stream processor.
  • Deferred the update until the WebSocket attempt reaches terminal completion, so a discarded partial attempt cannot leak turn state into the session before an SSE replay.
  • Added a regression test where a mid-turn compaction WebSocket response refreshes x-codex-turn-state and the next same-operation request echoes it; 187 affected tests and bun run check:ts pass.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 636f9eb0a5

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

state: CodexWebSocketSessionState | undefined,
): AsyncGenerator<Record<string, unknown>> {
for await (const event of events) {
applyCodexCompactionResponseMetadata(state, event);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Roll back Codex turn state when compaction replay fails

When the provider-native compaction path is on SSE (either initial SSE or the WebSocket→SSE replay), drainCodexCompactionEvents applies response.metadata turn-state updates before the compaction collector has seen a terminal response.done/response.completed. If the stream then emits response.failed/response.incomplete or closes early, the outer compaction retry reuses the same operation and can send the x-codex-turn-state from that discarded failed attempt; restore or clear the previous state on this failure path, like the normal Codex stream failure cleanup does.

Useful? React with 👍 / 👎.

@can1357
can1357 merged commit 401ff44 into main Aug 1, 2026
19 of 20 checks passed
@can1357
can1357 deleted the farm/65f9d9f8/fix-codex-v2-websocket-compaction branch August 3, 2026 23:06
darsrc pushed a commit to darsrc/dar-oh-my-pi that referenced this pull request Oct 10, 2026
…websockets (@roboomp)

# Conflicts:
#	packages/ai/src/providers/openai-codex-responses.ts
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.

Codex V2 remote compaction uses SSE while WebSocket-first native compaction succeeds with Enterprise EU residency

2 participants