Repository navigation
fix(agents): send session_id affinity header to ChatGPT Responses backend - #100233
Conversation
|
Codex review: needs maintainer review before merge. Reviewed July 6, 2026, 11:05 PM ET / 03:05 UTC. Summary PR surface: Source +21, Tests +118. Total +139 across 3 files. Reproducibility: yes. for a source-level reproduction: current main has Review metrics: 1 noteworthy metric.
Root-cause cluster Members:
Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything. Merge readiness Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch. Rank-up moves:
Risk before merge
Maintainer options:
Next step before merge
Security Review detailsBest possible solution: Land the focused native ChatGPT Responses header fix once exact-head checks and mergeability are acceptable; keep broader provider affinity and byte-stability work in the adjacent items. Do we have a high-confidence way to reproduce the issue? Yes for a source-level reproduction: current main has Is this the best way to solve the issue? Yes. Threading the existing session id into the native Responses client header builder is the narrowest maintainable fix for this path, and the native-backend gate avoids duplicating broader OpenRouter or Fireworks affinity work. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning high; reviewed against e3c40efeb09f. Label changesLabel justifications:
Evidence reviewedPR surface: Source +21, Tests +118. Total +139 across 3 files. View PR surface stats
What I checked:
Likely related people:
What the crustacean ranks mean
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. How this review workflow works
Review history (5 earlier review cycles)
|
|
Addressed the review: the generated |
|
@clawsweeper re-review |
|
🦞🧹 I asked ClawSweeper to review this item again. |
|
Maintainer review + land prep. Review: bug confirmed on Fixup pushed ( The new default outbound header for native ChatGPT Responses users is accepted as a maintainer decision (per ClawSweeper's compat flag). Landing after CI on the new head. |
…kend The ChatGPT backend routes requests by session_id (codex-cli sends it); without it each request lands on an arbitrary machine and the prompt cache misses. Measured on live traffic: 56.0% -> 81.8% TRUE cache hit rate (12-turn A/B, identical prompts).
buildOpenAIClientHeaders only skipped generating a session_id header when the exact lowercase key was already present, so a caller-supplied Session_ID or SESSION_ID header would end up on the wire alongside our generated one instead of winning per the caller-wins header contract. Check all resolved header keys case-insensitively before injecting.
Fixes #100232
Problem
The agent-runtime OpenAI Responses transport (
createOpenAIResponsesClient→buildOpenAIClientHeaders) never sends asession_idheader. The ChatGPT backend(chatgpt.com/backend-api) routes requests by
session_id— codex-cli sends it on everyrequest (thread id), and OpenClaw's own SSE/WS provider path (
openai-chatgpt-responses.ts)already sets it. Without it, each HTTP request from a session can land on a different
backend machine, whose local prompt cache doesn't hold the session's prefix.
Observed on live traffic (gpt-5.5, ChatGPT OAuth): cold-write streaks and interleaved cache
lineages —
cached_tokensalternating between two values (e.g. ~28k and ~57k) acrossconsecutive turns of one session, i.e. two machines each holding a different partial prefix.
Aggregate TRUE cache hit rate ~47-56% vs codex-cli's ~92-96% on the same account/model.
Fix
buildOpenAIClientHeaders()takes an optionalsessionId; whenusesNativeOpenAICodexResponsesBackend(model)and nosession_idwas set bycaller/turn headers (caller-wins preserved), it adds
session_id: <sessionId>. Threadedfrom
options?.sessionIdat the single Responses-client call site. Scoped strictly to thenative ChatGPT/Codex backend — api.openai.com and proxy endpoints are untouched.
Measurements (12 identical prompts per leg, fresh session each, same account/hour)
previous_response_id+delta continuation, out of scope here.)A separate controlled A/B (identical tool-call sequences, same session shape) comparing
stock vs. the combined local fix set measured 45.9% → 92.0% TRUE hit rate — 6.5x fewer
uncached input tokens for identical work (stock session
3017333e: 466,088 uncached inputtokens; fixed session
b97d343e: 71,478). This isolates the session_id header's owncontribution (56.0% → 81.8%, table above) from an additional byte-stable-prefix fix tracked
separately in a follow-up PR.
Also adds a NOTE comment (no behavior change) at the WS
websocketRequestIdfallback inopenai-chatgpt-responses.ts, which mints a fresh random id per request whenoptions.sessionIdis absent — forfeiting affinity on that transport; worth a follow-up.Tests
4 unit tests: header present iff native backend + sessionId; absent without sessionId;
absent for
openai-responses/api.openai.com; caller-suppliedsession_idnot overwritten.Typecheck (
run-tsgo.mjs -p tsconfig.core.json) clean. (Vitest currently fails to launchon this dev box for unrelated toolchain reasons — rolldown native binding; tests were
written against the existing patterns in
openai-transport-stream.test.ts.)Reported-by: @Peetiegonzalez
cc: @Peetiegonzalez
What Problem This Solves
The ChatGPT Responses backend (chatgpt.com/backend-api) routes HTTP requests by
session_id.buildOpenAIClientHeaders()never sent one, so consecutive requests from the same OpenClawsession could land on different backend machines, each holding a different partial prompt-cache
prefix — visible as interleaved cache lineages and cold-write streaks on live traffic. codex-cli
(external reference client) and OpenClaw's own WS/SSE path already send this header; only the
HTTP Responses transport used by the
openclawagent runtime was missing it. See "Problem" abovefor the full mechanism and code path.
Evidence
Measured, not estimated: 12 identical prompts per leg, fresh session each, same account and same
hour, ChatGPT-OAuth gpt-5.5,
TRUE hit% = cache_read / (cache_read + input)computed from eachcall's provider
usagefields.rate (+25.8 points), cache lineage streaks disappear.
TRUE hit rate, 6.5x fewer uncached input tokens for identical 12-turn work (466,088 -> 71,478
uncached tokens).
regression), and cross-provider blast-radius analysis:
https://raw.githubusercontent.com/Marvinthebored/openclaw/proof-assets-session-affinity-header/pr-proof/100233-session-affinity-header/EVIDENCE.md