Skip to content

fix(responses): use body prompt_cache_key as session_id when no session header is sent - #9145

Open
shu-chen-dot-ca wants to merge 1 commit into
lidge-jun:devfrom
shu-chen-dot-ca:fix-9137-prompt-cache-key-session
Open

shu-chen-dot-ca wants to merge 1 commit into
lidge-jun:devfrom
shu-chen-dot-ca:fix-9137-prompt-cache-key-session

Conversation

@shu-chen-dot-ca

@shu-chen-dot-ca shu-chen-dot-ca commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Closes #9137.

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 dev c0f59e454 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
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.

@github-actions

Copy link
Copy Markdown
Contributor

✅ Deterministic PR hygiene checks passed.

@github-actions github-actions Bot added the bug Something isn't working label Nov 25, 2026
@github-actions

github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

✅ READY

  • 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

Hygiene

✅ Deterministic PR hygiene checks passed.

@github-actions
github-actions Bot marked this pull request as draft November 25, 2026 18:32
@coderabbitai

coderabbitai Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

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.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix · Severity of issue fixed: Medium

Sequence Diagram(s)

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: Passed 4

Check name Status Explanation
Title check Passed 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 Passed 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 Passed 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 Passed 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.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between d7e8015 and b113788.

Files selected for processing: 7

  • docs-site/src/content/docs/reference/proxy-formats.md
  • src/server/caller-session-identity.ts
  • src/server/request-log-conversation.ts
  • src/server/responses/request-prepare.ts
  • structure/data-planes/inbound-compat.md
  • tests/server/caller-session-identity.test.ts
  • tests/server/caller-session-ingress.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@github-actions
github-actions Bot marked this pull request as ready for review November 25, 2026 18:45

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working review-ready

Projects

None yet

1 participant