You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix(responses): use body prompt_cache_key as session_id when no session header is sent - #9145
A Responses client that names its conversation only through the body prompt_cache_key (Oh My Pi sends no session_id, session-id, thread-id or x-session-id) bound no conversation: the turn logged no_conversation, took no affinity lane, and reached the ChatGPT Codex backend without the session_id it keys prefix reuse on.
promotePromptCacheKeySession (src/server/request-log-conversation.ts) sets session_id from the body prompt_cache_key when the request carries none of session_id, session-id, thread-id or x-session-id. It uses the same marker rules (1-128 safe ASCII characters, trimmed) and the same scoping as a promoted x-session-id: raw on loopback, an opaque principal-scoped SHA-256 for authenticated admission, and nothing for an authenticated admission without a trusted principal. That scoping moved into principalScopedSessionId in caller-session-identity.ts so both promotions share it.
prepareResponsesRequest calls it once the body is read, right after the inbound cache diagnostic (which still records what the caller itself sent), only for genuine Responses inbound (inboundWire === "responses", HTTP and WebSocket frames) and never for a shared Claude cohort key. The body is only read there, so the key is set on the request's own headers. Every later reader (pool affinity, conversation log, upstream headers) then sees it. Turn admission has already run, so admission lanes are unchanged.
Explicit session headers and x-session-id always win. Chat Completions and Messages replays keep their existing identity rules.
Docs: the caller conversation section of reference/proxy-formats.md and structure/data-planes/inbound-compat.md.
request-prepare.ts sits right at the 2000-line ratchet, so the request-log-conversation import there is written a few names per line to keep it at 1999 lines.
Verification
Bun 1.4.4, Linux, branch on dev at c0f59e454.
New regressions in tests/server/caller-session-identity.test.ts:
through handleResponses to a stubbed chatgpt.com/backend-api/codex/responses, a body key with no session header reaches upstream as session_id and a conversation id is logged;
session_id, session-id and thread-id each win over the key;
a Chat Completions replay (inboundWire: "chat") is unchanged;
unit cases: empty, whitespace, unsafe, 129-character, leading -, non-string, array and non-object bodies are ignored, 128 characters is accepted, x-session-id wins, authenticated keys are principal-scoped and equal to the x-session-id promotion for the same marker, a different key gives a different id, and an unknown principal or missing admission promotes nothing.
New regressions in tests/server/caller-session-ingress.test.ts, through the real HTTP and WebSocket ingress: local admission forwards the raw key, public admission with an opencodex key forwards a stable 64-hex scoped id across turns, and an explicit session_id wins.
With the wiring in request-prepare.ts reverted, the end-to-end case sends no session_id upstream and logs no conversation (screenshots below).
bun test tests/server/caller-session-identity.test.ts tests/server/caller-session-ingress.test.ts tests/ci-workflows/file-size-ratchet.test.ts tests/ci-workflows/import-cycle-ratchet.test.ts tests/providers/xai/grok-session-identity.test.ts: 72 pass, 0 fail.
bun run typecheck, bun run structure:check, bun run privacy:scan: passed. cd docs-site && bun install --frozen-lockfile && bun run build: passed.
Full suite: OCX_TEST_MAIN_TIMEOUT_MS=3600000 bun run test --timeout=60000 --path-ignore-patterns '**/link-supervisor.test.ts' passed (exit 0). tests/clients/link-supervisor.test.ts is left out because the same 16 of its 43 tests fail on unchanged devc0f59e454 in this environment (bun run test tests/clients/link-supervisor.test.ts); it does not import anything this PR changes.
Not verified: real cached-token counts against a live ChatGPT account. The tests check the upstream session_id header that the issue's measurement shows decides reuse.
Screenshots
Before
After
Checklist
Scope stays focused and avoids unrelated cleanup.
Docs or release notes were updated when needed.
Security-sensitive changes were reviewed for secrets, auth, and unsafe defaults.
Review readiness checklist
This PR stays in draft until every box below is ticked. Tick all four boxes once the requirements are met:
Required local validation passed; commands, results, and any full-suite exception are documented.
I pushed my PR to a recent dev commit (at most 10 behind; a maintainer may still ask for the exact tip before merge).
I resolved all correct Codex and CodeRabbit findings.
My PR is ready for review.
Summary by CodeRabbit
New Features
Responses API requests can use prompt_cache_key as a conversation identity when no session identity is provided. This applies to HTTP and WebSocket requests, with identities scoped according to the existing rules.
Bug Fixes
Explicit session headers take precedence over the prompt cache key. Chat Completions and Messages behavior remains unchanged.
Documentation
Updated reference and compatibility guidance to describe the new identity behavior and its constraints.
all PR quality gates passed; the review readiness checklist is complete.
Review readiness checklist
✅ Required local validation passed; commands, results, and any full-suite exception are documented.
✅ I pushed my PR to a recent dev commit (at most 10 behind; a maintainer may still ask for the exact tip before merge).
✅ I resolved all correct Codex and CodeRabbit findings.
✅ My PR is ready for review.
✅ 4/4 boxes ticked.
This pull request has been marked Ready for Review.
The review-ready label marks this PR as ready; review automation runs independently.
Maintainers notified: @lidge-jun@Ingwannu
No actionable comments were generated in the recent review.
Walkthrough
Responses requests can now use prompt_cache_key as a session identity when no session identity header is supplied. The value follows existing validation and loopback or principal scoping rules. Chat Completions and Messages retain their existing identity behavior.
Changes: Responses session identity
Layer / File(s)
Summary
Scoped identity and key promotion src/server/caller-session-identity.ts, src/server/request-log-conversation.ts
A shared helper derives the session ID from the conversation and admission. The promotion helper validates the request body and headers, then sets session_id when promotion is eligible.
Responses ingress and validation src/server/responses/request-prepare.ts, tests/server/caller-session-identity.test.ts, tests/server/caller-session-ingress.test.ts, structure/data-planes/inbound-compat.md, docs-site/src/content/docs/reference/proxy-formats.md
Responses preparation calls promotion after cache diagnostics, except for requests in the shared cohort. Tests cover header precedence, key validation, admission scoping, and Responses and WebSocket ingress. The documentation describes the promotion rules.
sequenceDiagram
participant prepareResponsesRequest
participant promotePromptCacheKeySession
participant principalScopedSessionId
participant Headers
prepareResponsesRequest->>promotePromptCacheKeySession: headers, body, admission
promotePromptCacheKeySession->>principalScopedSessionId: prompt_cache_key and admission
principalScopedSessionId-->>promotePromptCacheKeySession: scoped session ID
promotePromptCacheKeySession->>Headers: set session_id
Loading
Possibly related PRs
lidge-jun/opencodex#7542: Both changes use the request-log conversation slot. The promoted session_id can become the native log identity and interacts with the Hermes fallback precedence.
Suggested labels:bug
Merge Risk: Minimal · up to b1137
Responses requests that carry a body prompt_cache_key and no session header now get a stable, scoped session ID upstream, which should improve prompt-cache reuse. Explicit session headers still win, and other endpoints are unchanged. No merge-blocking risk was found.
Pre-merge checks: 4
Check name
Status
Explanation
Title check
The title names the main change: the Responses request body prompt_cache_key is now used as session_id when no session header is sent. The changeset (request-log-conversation.ts promotion helper, requ…
Linked Issues check
Linked issue #9137 asks that Responses ingress use the body prompt_cache_key as upstream session_id when no session header is sent. The reviewed head implements this in promotePromptCacheKeySession (s…
Out of Scope Changes check
All changes connect to #9137. The helper promotePromptCacheKeySession and its call in prepareResponsesRequest implement the requested fallback. Moving the scoping logic into principalScopedSessionId i…
Description Check
Check skipped - CodeRabbit’s high-level summary is enabled.
Full details: Title check
Explanation
The title names the main change: the Responses request body prompt_cache_key is now used as session_id when no session header is sent. The changeset (request-log-conversation.ts promotion helper, request-prepare.ts call site, caller-session-identity.ts scoping, docs, and tests) matches this title. It is specific and does not use vague terms.
Full details: Linked Issues check
Explanation
Linked issue #9137 asks that Responses ingress use the body prompt_cache_key as upstream session_id when no session header is sent. The reviewed head implements this in promotePromptCacheKeySession (src/server/request-log-conversation.ts). The helper returns nothing unless an admission exists and the body is a non-array object. It returns nothing when session_id, session-id, thread-id or x-session-id is present. It validates the key with safeCallerSessionId (trimmed, 1-128 safe ASCII). It scopes the value with principalScopedSessionId, which is shared with the x-session-id promotion in caller-session-identity.ts. It then sets the session_id header. Per the PR description and the change summary, prepareResponsesRequest (src/server/responses/request-prepare.ts) calls this helper after the inbound cache diagnostic, only when inboundWire is responses. Turn admission is unchanged. The request-prepare.ts diff could not be read in this review, so the call site rests on the PR description and change summary. The PR reports end-to-end tests through handleResponses and real HTTP/WebSocket ingress that check the upstream session_id header and the logged conversation id. Those test results were not independently re-run here. Live cached-token reuse on ChatGPT was not verified, as the PR states. The issue's requested behavior is implemented in code.[#9137]
Full details: Out of Scope Changes check
Explanation
All changes connect to #9137. The helper promotePromptCacheKeySession and its call in prepareResponsesRequest implement the requested fallback. Moving the scoping logic into principalScopedSessionId in caller-session-identity.ts is a refactor that lets both the x-session-id and prompt_cache_key promotions share one scoping rule, so it supports the feature. The import in request-prepare.ts is split across lines to respect the file-size ratchet, a direct requirement of the same change. Tests in caller-session-identity.test.ts and caller-session-ingress.test.ts cover the new behavior. The documentation updates in reference/proxy-formats.md and structure/data-planes/inbound-compat.md describe the new rule. No unrelated change was identified from the summary.[#9137]
Finishing Touches
Generate unit tests (beta)
Create a new PR
Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #9137.
A Responses client that names its conversation only through the body
prompt_cache_key(Oh My Pi sends nosession_id,session-id,thread-idorx-session-id) bound no conversation: the turn loggedno_conversation, took no affinity lane, and reached the ChatGPT Codex backend without thesession_idit keys prefix reuse on.promotePromptCacheKeySession(src/server/request-log-conversation.ts) setssession_idfrom the bodyprompt_cache_keywhen the request carries none ofsession_id,session-id,thread-idorx-session-id. It uses the same marker rules (1-128 safe ASCII characters, trimmed) and the same scoping as a promotedx-session-id: raw on loopback, an opaque principal-scoped SHA-256 for authenticated admission, and nothing for an authenticated admission without a trusted principal. That scoping moved intoprincipalScopedSessionIdincaller-session-identity.tsso both promotions share it.prepareResponsesRequestcalls it once the body is read, right after the inbound cache diagnostic (which still records what the caller itself sent), only for genuine Responses inbound (inboundWire === "responses", HTTP and WebSocket frames) and never for a shared Claude cohort key. The body is only read there, so the key is set on the request's own headers. Every later reader (pool affinity, conversation log, upstream headers) then sees it. Turn admission has already run, so admission lanes are unchanged.x-session-idalways win. Chat Completions and Messages replays keep their existing identity rules.reference/proxy-formats.mdandstructure/data-planes/inbound-compat.md.request-prepare.tssits right at the 2000-line ratchet, so therequest-log-conversationimport there is written a few names per line to keep it at 1999 lines.Verification
Bun 1.4.4, Linux, branch on
devatc0f59e454.tests/server/caller-session-identity.test.ts:handleResponsesto a stubbedchatgpt.com/backend-api/codex/responses, a body key with no session header reaches upstream assession_idand a conversation id is logged;session_id,session-idandthread-ideach win over the key;inboundWire: "chat") is unchanged;-, non-string, array and non-object bodies are ignored, 128 characters is accepted,x-session-idwins, authenticated keys are principal-scoped and equal to thex-session-idpromotion for the same marker, a different key gives a different id, and an unknown principal or missing admission promotes nothing.tests/server/caller-session-ingress.test.ts, through the real HTTP and WebSocket ingress: local admission forwards the raw key, public admission with an opencodex key forwards a stable 64-hex scoped id across turns, and an explicitsession_idwins.request-prepare.tsreverted, the end-to-end case sends nosession_idupstream and logs no conversation (screenshots below).bun test tests/server/caller-session-identity.test.ts tests/server/caller-session-ingress.test.ts tests/ci-workflows/file-size-ratchet.test.ts tests/ci-workflows/import-cycle-ratchet.test.ts tests/providers/xai/grok-session-identity.test.ts: 72 pass, 0 fail.bun run typecheck,bun run structure:check,bun run privacy:scan: passed.cd docs-site && bun install --frozen-lockfile && bun run build: passed.OCX_TEST_MAIN_TIMEOUT_MS=3600000 bun run test --timeout=60000 --path-ignore-patterns '**/link-supervisor.test.ts'passed (exit 0).tests/clients/link-supervisor.test.tsis left out because the same 16 of its 43 tests fail on unchangeddevc0f59e454in this environment (bun run test tests/clients/link-supervisor.test.ts); it does not import anything this PR changes.Not verified: real cached-token counts against a live ChatGPT account. The tests check the upstream
session_idheader that the issue's measurement shows decides reuse.Screenshots
Checklist
Review readiness checklist
This PR stays in draft until every box below is ticked. Tick all four boxes once the requirements are met:
Required local validation passed; commands, results, and any full-suite exception are documented.
I pushed my PR to a recent dev commit (at most 10 behind; a maintainer may still ask for the exact tip before merge).
I resolved all correct Codex and CodeRabbit findings.
My PR is ready for review.
Summary by CodeRabbit
prompt_cache_keyas a conversation identity when no session identity is provided. This applies to HTTP and WebSocket requests, with identities scoped according to the existing rules.