Repository navigation
fix(agent): route codex v2 compaction through websockets - #7202
Conversation
- 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
There was a problem hiding this comment.
💡 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".
| const fallback = await openCodexSseTransport(model, requestContext, requestSetup, options, state); | ||
| if (state) state.lastTransport = fallback.transport; | ||
| yield* fallback.eventStream; |
There was a problem hiding this comment.
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
|
There was a problem hiding this comment.
💡 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; |
There was a problem hiding this comment.
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
|
There was a problem hiding this comment.
💡 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); |
There was a problem hiding this comment.
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 👍 / 👎.
…websockets (@roboomp) # Conflicts: # packages/ai/src/providers/openai-codex-responses.ts
Repro
An
openai-codex-responsesV2 compaction probe withpreferWebsockets: truereproduced the split aswebsocketConnections: 0,sseRequests: 1, followed byresponse.failed (data_residency_mismatch). The regression scenario is exercised withbun test packages/agent/test/remote-compaction.test.ts --test-name-pattern "V2 compaction reuses|V2 compaction falls back".Cause
requestCompactionV2Streaming()inpackages/agent/src/compaction/compaction-v2-streaming.tsopened its own HTTPfetchstream instead of entering the WebSocket-first selector inpackages/ai/src/providers/openai-codex-responses.ts;SummaryOptionsand the coding-agent compaction callsites also omitted the configured WebSocket preference.Fix
preferWebsocketsthrough manual, automatic, and advisor compaction paths.Verification
bun test packages/agent/test/remote-compaction.test.tspassed: 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.tspassed: 142 tests, 669 assertions.bun run check:tspassed for all workspaces.bun checkfails onmainbecause the environment lackscmakeforaudiopus_sys; the pre-publish gate was skipped after confirming the identical failure in a cleanorigin/mainworktree.Fixes #7198