Repository navigation
feat(agent): key Codex sessions by Codex's own session-id header (#1311) - #1322
Conversation
|
Behavior change: approved by a human developer. @amiddavid approved the session-key change on 2026-11-24, in the session that owns this PR. Reason: no one uses Codex through the proxy today, so the one-time key change moves no live session.
|
Codex sessions used a content hash, which changes on compaction. Add agent/codex: a pure function, Identify(http.Header), that returns the session-id, thread-id and x-codex-parent-thread-id headers when the User-Agent or the originator starts with "codex". The proxy uses the session id as the session key, on any route. Assisted-By: Claude Opus 5.5 <[email protected]> Signed-off-by: DAVID AMID <[email protected]>
Cite #1317, #1318 and #1322 per client. Record Cortex facts from its plugin: host session id, no thread tracker, no signal route. Note that the bifrost adapter needs the transport to pass headers. Assisted-By: Claude Opus 5.5 <[email protected]> Signed-off-by: DAVID AMID <[email protected]>
31db98d to
7ddd1b2
Compare
amiddavid
left a comment
There was a problem hiding this comment.
| Reviewer | Kind | Started by | Context | Review | Round | Head | Verdict |
|---|---|---|---|---|---|---|---|
| Reviewer 1 (glm, user of @amiddavid) | GLM rits/zai-org/glm-5-3 via Claude Code 2.1.331 | offload-review skill, @amiddavid's session | fresh | 1 of 2 | 1 of 2 | 7ddd1b241bcf |
approve |
Verdict: approve
Findings
None.
Non-blocking
- proxy/clientsignal.go:192 — stale comment in
signal(). It still says Codex is "the one client whose hook id differs from the proxy's session key". Since this PR, Codex's hook id equals the session key. The conclusion (the hook id is a safe campaign key) still holds, but the stated reason no longer does. Reword it. - keepalive/keeper.go:41 and keepalive/keeper.go:440 — stale comments. Both still name "Codex's session-id header" as the example of a client alias, that is, an id that differs from the key. For Codex it no longer differs. Update the example.
- docs/design.md:296 — the Session keying list is not updated. It names every explicit id source (header, metadata ids, Bob's
X-Task-Id) but not Codex'ssession-idheader. Add it, so the map matches the code. - proxy/compactioncfg.go:183 — a third Codex detector.
clientOfmatches the User-Agent start only.codex.Identifyalso matches theoriginatorheader. So an app-server client with a codex originator and its own User-Agent gets the Codex session key, but not the Codex compaction point. The split looks deliberate, but three rules for one question can drift. Consider sharing one rule. - proxy/proxy.go:938 —
/compactreads onlyx-context-guru-session. Codex traffic replayed through/compactwithout that header keys by content hash, so the replay splits the session at a compaction. The live path does not. A replayer can set the header, so this is a fidelity note, not a defect. - agent/codex/codex.go —
Name,AgentIDandParentIDare exported but unused. The PR says #1312 and #1318 will use them. Fine, but they are dead surface until then.
What I checked
- The full diff (9 files, 1 commit) and the code around each change.
- Fail open.
Identifyis a pure function; it cannot panic. An unusablesession-idvalue failssafeExplicitand falls back to the content hash. A panic in the route agent'sSessionIDgives"", and the Codex id still applies. - Precedence.
x-context-guru-sessionwins, thenmetadata.user_id/taskId, then the route agent's id (Bob'sX-Task-Id), then the Codex id.TestCodexSessionIDLosesToTheContextGuruHeadercovers the header win. - Hooks and the keep-alive.
SessionEndandSessionStart compactname the scoped session-id. Entries now key by that id directly.RetireSessionmatchesKey.SessionorClient, so both old (alias) and new (direct) entries retire. A Codex turn whose User-Agent is stripped keys by content hash, andclientSessionAliasstill catches its end mark. - A stable id across compaction.
apply's boundary restart (apply.go:609) and the compact event handle a transcript that shrinks under one id. Claude Code's metadata id already runs this path. - Tenant safety. The id is client-supplied, but
session.Scopedkeeps it inside the tenant namespace, the same asx-context-guru-session. - The caller gate. Prefix match on User-Agent or
originator, case-insensitive. Mid-text matches are refused (tested). A non-Codex caller that sendssession-iddoes not move its session (tested). - Data dependence. The row file and the generated page carry the same text. The level stays metadata only. The PR names the change at the top with the human's approval.
- The import guard.
agent/imports_test.golistsagent/codex. The package imports no host. - Tests. The new unit tests cover each id, the originator-only case and refusal cases. The new proxy tests cover the compaction-stable key, route independence, the content-hash fallback and the header win. The helpers they use exist in
bobsession_test.go; no name collisions in package proxy. I did not run them (live tests not approved). - I did not read the other review or the PR comments.
amiddavid
left a comment
There was a problem hiding this comment.
| Reviewer | Kind | Started by | Context | Review | Round | Head | Verdict |
|---|---|---|---|---|---|---|---|
| Reviewer 2 (glm, user of @amiddavid) | GLM rits/zai-org/glm-5-3 via Claude Code 2.1.331 | offload-review skill, @amiddavid's session | fresh | 2 of 2 | 1 of 2 | 7ddd1b241bcf |
approve |
Verdict: approve
Findings
None. I found no wrong behavior, no data loss, no broken fail-open path, and no missing test for the new behavior.
Non-blocking
- proxy/clientsignal.go:192 — stale comment in
keeper.signal. It still says Codex is "the one client whose hook id differs from the proxy's session key". After this PR they are equal for Codex. The code is right (the conclusion, keying by the hook id, holds even more strongly now). Update the comment in a later docs pass. - docs/design.md:292 — the "Session keying" section is now stale. It lists the explicit id sources and names only Bob's
X-Task-Idas a header source. The Codexsession-id(caller-gated, any route) is a second one. The PR updated the data-dependence docs but not this section. - agent/codex/codex.go:15 —
Nameis exported but unused. No caller reads it. Keep it only if a host needs it soon; otherwise drop it. - proxy/compactioncfg.go:183 — two Codex detectors with different rules.
clientOfmatches the User-Agent prefix only;codex.Identifyalso matches theoriginatorheader. An app on the Codex app server with its own User-Agent and acodex…originator now keys its session bysession-idbut still gets the table compaction point. The narrowness inclientOflooks deliberate (a wrong match gives a stranger another client's C), so this is a note, not a defect. - proxy/clientsignal_test.go:192 — the test name still says "Codex". The comment was updated to "a caller that sends session-id but keys by something else", but
TestCodexSessionEndMatchesTheSessionIDHeadernow describes a non-Codex caller. Rename when convenient.
What I checked
- Read the full diff (9 files) and the code around each change. Read
CLAUDE.mdanddocs/design.md. - Precedence chain.
explicitSessionOf(proxy/proxy.go:796) adds the Codex id after the route agent's id. The final order isx-context-guru-session>metadata.user_id/metadata.taskId> agent id > Codex id, because host ids go last inapply.ExplicitSession(apply/apply.go:358) andsession.Explicit(session/session.go:132). The nested second resolution (sessionIDFrom, apply/apply.go:1521;bodyResponsesOpts, apply/responses.go:525) treatsexplicitSessionOf's result as the header candidate, so the order holds on both wires. No component injectsmetadata, so the double pass cannot reorder anything. - Tenant safety. The Codex id is validated by
safeExplicitand tenant-prefixed bysession.Scoped(session/session.go:240), like any explicit id. A tenant cannot name another tenant's session with the header. - Fail open.
codex.Identifyis a pure function over headers. It cannot panic or fail; a non-Codex caller or a missing header gives"", which is exactly the old content-hash behavior.agentSessionID's panic recovery is unchanged. - Keep-alive interplay. For Codex the scoped hook id now equals the session key, so
clientSessionAliasreturns"". The core retires onKey.SessionORRecord.Client(keepalive/keeper.go:439-442), andendedAfterchecks both ids (proxy/keepalive.go:368-371), soSessionEndandSessionStart compactevents still retire Codex entries, now on the primary key instead of the alias. - Route independence.
codexSessionIDreads onlyr.Header, notupPath, so all chat routes give the same key. The gate is the caller's claim (User-Agent ororiginatorstarts withcodex), documented inagent/codex/codex.goand in the PR body with the codex-rs source files. - Library boundary.
agent/codeximports onlynet/httpandstrings.agent/imports_test.gonow lists it, and the proxy imports it, sogo list -deps ./...covers the guard. - Data dependence. The
session-trackerrow file and the generateddocs/data-dependence.mdpage carry the same new sentence. Metadata only, no level change. The PR names the behavior change and the human approval at the top. - Tests. Read all new tests.
proxy/codexsession_test.goproves the fix end to end: the same key after a changed first turn (a compaction), any route, a non-Codex caller ignored, the content-hash fallback without the header, andx-context-guru-sessionwinning.agent/codex/codex_test.gocovers each id, the originator-only case, subagent headers, blank values, and refused callers. Notime.Sleepin the new tests; they wait on conditions. - Live tests were not approved, so I read the code only and did not run builds or tests.
…1311) Codex's hook id now equals its session key. Reword the comments that named Codex as the client whose id differs. Add Codex's session-id to the Session keying section. Rename the test that now covers a non-Codex caller. Assisted-By: Claude Opus 5.5 <[email protected]> Signed-off-by: DAVID AMID <[email protected]>
|
Round 1 replies, both reviewers. Fixed in Fixed
Not changed, with reason
Tests (macOS, Go 1.27.4, race): |
amiddavid
left a comment
There was a problem hiding this comment.
| Reviewer | Kind | Started by | Context | Review | Round | Head | Verdict |
|---|---|---|---|---|---|---|---|
| Reviewer 2 (glm, user of @amiddavid) | GLM rits/zai-org/glm-5-3 via Claude Code 2.1.331 | offload-review skill, @amiddavid's session | resumed | 2 of 2 | 2 of 2 | f029739667e2 |
approve |
Verdict: approve
Findings
None. The new commit changes comments, docs and one test name only. No code path changes.
Round-1 findings: status
Mine:
proxy/clientsignal.go:192stale comment — fixed. The comment now says Codex's hook id equals the session key (#1311) and the hook id is a safe campaign key either way. I checked the claim against the code: campaign decisions are gated off the OpenAI route (provider != bschemas.OpenAI, proxy/keepalive.go:384), and Codex uses that route. The comment is now right.docs/design.mdSession keying — fixed. The section now lists Codex'ssession-idafter Bob'sX-Task-Idand before the plugin id, which matches the code's precedence (host ids last, agent id before Codex id). The gate (User-Agent ororiginatorstarts withcodex, any route) matchescodex.Identify.codex.Nameunused — kept, with reason. The author says #1312 (agent.Identify) and #1318 use the exported fields. The reason is right; the fields are a library API for named follow-up PRs. Accept.- Two Codex detectors — rejected, reason checked and accepted. The author says #1312 makes
agent.Identifythe one source andcompactionpoint.ClientOfwill take its result; the narrow rule inclientOfstays until then, because a wrong match gives a stranger another client's compaction point. That matches what both reviews read incompactioncfg.go. I cannot show the reason is wrong. Accept. - Test name said "Codex" — fixed. Renamed to
TestSessionEndMatchesTheSessionIDHeaderOfANonCodexCaller, comment updated with it. I grepped the repo: nothing references the old name.
Reviewer 1's findings: the three comment/doc items are the same as mine and are fixed the same way. The two extra items reviewer 1 raised:
keepalive/keeper.go:41and:440stale alias examples — fixed. Both comments now describe "a client that sends an id but is keyed by another; Codex no longer, since its session-id is the key". Accurate./compactreads onlyx-context-guru-session— rejected, reason accepted. Pre-existing behavior, out of scope; a replay can set the header. I verified in round 1 that/compacttakes the header only, unchanged by this PR.
Non-blocking
docs/design.md:298— the new sentence says "so the bifrost adapter and Cortex can call it on their own paths". The adapter is not wired yet (the PR body says so). "Can call" is true of the library function, so this is accurate, but a reader may take it as "does call". Minor wording only.
What I checked
- Last review: round 1, 7ddd1b2, 2026-11-24T09:10:02Z.
- New commits read: 1 (
f0297396"docs: update stale Codex session comments and the session keying doc (#1311)"). - Read in full: 2 reviews, 2 comments, 0 inline comments (from the three saved files).
- New items since my last review:
- Commit
f0297396: read the full diff (4 files, 14 insertions, 9 deletions). Every hunk is a comment, a doc line, or the test rename. I read each changed file around the change:docs/design.md(Session keying section),keepalive/keeper.go(kaEntry.clientandRecord.Clientcomments),proxy/clientsignal.go(signal's campaign-key comment),proxy/clientsignal_test.go(renamed test and comment). No code behavior changes; the rename has no other references in the repo. - Comment by the PR author at 09:17:23 (round-1 replies): read in full, each fixed/rejected item checked as above.
- Comment at 08:54:32 (human approval of the behavior change): read; it predates my last review and changes nothing in this round.
- Commit
- The head is
f029739667e2, the head this review names.git statusshows no changed tracked files in the worktree. - Live tests were not approved, so I read the code only and did not run builds or tests.
amiddavid
left a comment
There was a problem hiding this comment.
| Reviewer | Kind | Started by | Context | Review | Round | Head | Verdict |
|---|---|---|---|---|---|---|---|
| Reviewer 1 (glm, user of @amiddavid) | GLM rits/zai-org/glm-5-3 via Claude Code 2.1.331 | offload-review skill, @amiddavid's session | resumed | 1 of 2 | 2 of 2 | f029739667e2 |
approve |
Verdict: approve
Findings
None.
Round 1 findings, checked at the new head
- Stale comment in
signal()(clientsignal.go:192) — fixed. The comment now says Codex's hook id equals the session key (#1311) and the hook id is a safe key either way. I read the reworded comment; it is accurate. - Stale alias examples in keepalive/keeper.go:41 and :440 — fixed. Both now describe the alias as "a client that sends an id but is keyed by another; Codex no longer, since its session-id is the key". Accurate.
- docs/design.md Session keying list — fixed. The list now names Codex's
session-idafter Bob'sX-Task-Id, with the caller gate and route rule. The order matches the code (host ids last, Bob's before Codex's). - Third Codex detector (compactioncfg.go:183) — rejected by the author, with a reason. The reason is right: a wrong match gives a stranger another client's compaction point, and #1312's
agent.Identifywill become the one source. Accepted; no defect stays. /compactreads onlyx-context-guru-session— rejected by the author: a replayer can set the header, and it is out of scope. I said the same in round 1. Accepted.- Unused
Name,AgentID,ParentID— rejected by the author: #1312 and #1318 use them. Accepted.
The other reviewer raised the same items (their items 1, 2, 4, 5 match my 1, 3, 4, 6). I checked each fix myself, as above. Their item on the test name (TestCodexSessionEndMatchesTheSessionIDHeader described a non-Codex caller) is fixed too: the test is renamed to TestSessionEndMatchesTheSessionIDHeaderOfANonCodexCaller and its comment matches. The test body is unchanged and still valid.
Non-blocking
- docs/design.md:299 — "so the bifrost adapter and Cortex can call it on their own paths". The bifrost adapter is not wired (the PR body says its transport never passes request headers). The sentence states what the function allows, not what is wired, but a reader may take it as wired. Consider "can call it once their transports pass headers".
What I checked
- Last review: round 1, 7ddd1b2, 2026-11-24T09:09:29Z.
- New commits read: 1 (
f0297396docs: update stale Codex session comments and the session keying doc). - Read in full: 2 reviews, 2 comments, 0 inline comments (from the three files in my folder).
- New items since my last review: reviewer 2's round-1 review (approve, no findings; overlap checked above), the author's round-1 reply comment (fix list and three rejections, each checked above), and commit
f0297396(reviewed below). - The new commit is docs-only: 3 comment rewordings, 1 design.md sentence, 1 test rename. No code path changes. I read each hunk in its file, not only the diff.
- The reworded comments match the behavior I traced in round 1: for Codex the scoped hook id equals the session key, and
RetireSessionmatchesKey.SessionorClient. - The renamed test still exercises the alias path (no Codex User-Agent, content-hash key, header-id alias), which is the path that remains for a non-Codex sender of
session-id. - Live tests were not approved, so I read the code only.
|
Proxy/library split: confirmed by the proxy-boundary session (#1309, #1315). OK to hand off at head
|
Cite #1317, #1318 and #1322 per client. Record Cortex facts from its plugin: host session id, no thread tracker, no signal route. Note that the bifrost adapter needs the transport to pass headers. Assisted-By: Claude Opus 5.5 <[email protected]> Signed-off-by: DAVID AMID <[email protected]>
Cite #1317, #1318 and #1322 per client. Record Cortex facts from its plugin: host session id, no thread tracker, no signal route. Note that the bifrost adapter needs the transport to pass headers. Assisted-By: Claude Opus 5.5 <[email protected]> Signed-off-by: DAVID AMID <[email protected]>
docs: add rules for client-specific code and a client coverage table Add "Client-specific code" to CLAUDE.md, a client-rule check to the offload-review prompt, a third refactor-scan pattern, and a wider step 5 in RELEASING.md. Add docs/client-coverage.md and a test that fails when a registered agent has no row. Closes #1321 docs: fill the client coverage table from the open PRs and Cortex Cite #1317, #1318 and #1322 per client. Record Cortex facts from its plugin: host session id, no thread tracker, no signal route. Note that the bifrost adapter needs the transport to pass headers. docs: mark Cortex wait cause n/a and note the thread gap owner docs: leave the Cortex follow-up issue to the Cortex integration owner docs: update the client coverage table for merged and open PRs docs: address round 1 notes on the client coverage test and rules Anchor the table scan on the By client heading, map each pending row to its issue, name one Identify, say why Pi has no plain-string sign, and note that dashboard-only labels have no rules. test: fix the heading fixtures of the client coverage parser test Find the heading at the start of the page. Prefix the no-table case with a title so the next-heading guard runs. Drop a dead empty-rows check. docs: add the client coverage page to the nav docs: state client rules without issue numbers Write each rule as a general rule with an example. State the coverage table as facts on main. Map pending rows to a reason, not an issue. No behavior change: rules, skill prompts and docs, plus one Go test. The owner checked the text-only commits by hand (2026-11-24). Approvals still count (approval-still-valid). Local tests waived (owner rule). Assisted-By: Claude Opus 5.5 Signed-off-by: DAVID AMID <[email protected]>
Behavior change (approved by the human developer)
session-idheader. Session counts, per-session state and keep-alive keys change once for Codex users.Summary
Closes #1311.
session-idheader.What changed
agent/codex: one pure function,codex.Identify(h http.Header) (Fields, bool). No registry. No route path.trueonly if the User-Agent or theoriginatorheader starts withcodex(any case). A nil or empty header givesfalse.Fields:SessionID(session-id),AgentID(thread-id),ParentID(x-codex-parent-thread-id, subagents only).proxy/proxy.go,explicitSessionOf) only passesr.Headerand usesSessionID. It is tried after the route agent's id and afterx-context-guru-session. With no Codex headers the old behavior holds (body ids, then content hash).clientSessionAlias) is unchanged. For Codex it now equals the key. Comments updated.issue-1312'sagent.Identifywill callcodex.Identify. Whichever PR merges second adapts.Why this id
Source:
openai/codexatf64f003d(2026-11-24,codex-rs).codex-api/src/requests/headers.rs,build_session_headers: setssession-idandthread-idon every model request.core/src/client.rs,responses_session_id: for a root session the header is the session id. A subagent sends the root's id, so the whole agent tree shares one session key.core/tests/suite/prompt_cache_key.rs:session-idandprompt_cache_keyequal the session id on every turn.prompt_cache_keyrejected: a host can set it to a custom value or omit it (core/src/thread_manager.rs). It is also in the body.thread-idrejected as the session key: it differs per thread. It is returned asAgentIDso subagent threads can be told apart (fix(keepalive): read Codex and OpenCode subagents, stop reading human for other clients #1318).login/src/auth/default_client.rs. The User-Agent starts with the originator (codex_cli_rs,codex_exec,codex-tui,codex_vscode). An app-server client sets its own originator, so that header is checked too.Other hosts
codex.Identify(pctx.Headers). UseSessionIDas the key. If it is empty orfalse, usepctx.SessionID. A host with no headers keeps today's behavior.resolveSessiongets only aBifrostContextwith an explicit id set by the transport. It never sees request headers. Wiring needs the transport to put the headers in the context. Same gap as Bob'sX-Task-Id. Whenagent.Identifylands, the adapter can call it on the same terms.Data dependence
Metadata only: one id from a header. No change at any storage level (off, metadata only, everything). Row updated:
docs/data-dependence/features/session-tracker.md.Hosted vs local
No difference. Both use the same function. The id is tenant-scoped by the existing
session.Scoped.How it was tested
macOS, Go 1.27.4.
go build ./...,go vet ./agent/... ./proxy/: clean.CGO_ENABLED=1 go test -race: pass for./agent/...,./session/...,./apply/...,./adapters/..., and the proxy session, Codex, hook and alias tests../proxy/(before the last rework): one failure,TestMetricsCallLatency(a p99 timing check). It fails the same way on an unchangedorigin/mainhere (p99 7.2 ms against the limit).agent/codex/codex_test.go(each id, originator-only, subagent, nil/empty header, other callers refused) andproxy/codexsession_test.go(same key after a compaction, any route, non-Codex caller ignored, no header keeps the hash,x-context-guru-sessionwins).Open questions
session-id, so they share the key.AgentIDandParentIDare there for fix(keepalive): read Codex and OpenCode subagents, stop reading human for other clients #1318 to split threads.