Skip to content

feat(agent): key Codex sessions by Codex's own session-id header (#1311) - #1322

Merged
amiddavid merged 2 commits into
mainfrom
feat/codex-session
Nov 24, 2026
Merged

amiddavid merged 2 commits into
mainfrom
feat/codex-session

Conversation

@amiddavid

@amiddavid amiddavid commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Behavior change (approved by the human developer)

  • Who it affects: Codex users (CLI, exec, IDE extension, apps on the Codex app server) on any route.
  • Old: a Codex session was keyed by a content hash. A compaction changed the hash and split one conversation into two sessions.
  • New: the session is keyed by Codex's own session-id header. Session counts, per-session state and keep-alive keys change once for Codex users.
  • Approval: the human approved it (no active Codex users, so the one-time key change is fine). No off switch.

Summary

Closes #1311.

  • A Codex session keeps one key through compaction and resume.
  • The key is the session-id header.
  • Other clients are not affected.

What changed

  • New package agent/codex: one pure function, codex.Identify(h http.Header) (Fields, bool). No registry. No route path.
  • It returns true only if the User-Agent or the originator header starts with codex (any case). A nil or empty header gives false.
  • Fields: SessionID (session-id), AgentID (thread-id), ParentID (x-codex-parent-thread-id, subagents only).
  • The proxy (proxy/proxy.go, explicitSessionOf) only passes r.Header and uses SessionID. It is tried after the route agent's id and after x-context-guru-session. With no Codex headers the old behavior holds (body ids, then content hash).
  • The hook alias path (clientSessionAlias) is unchanged. For Codex it now equals the key. Comments updated.
  • Docs: the Session tracker row says Codex reads its header. The page is regenerated.
  • issue-1312's agent.Identify will call codex.Identify. Whichever PR merges second adapts.

Why this id

Source: openai/codex at f64f003d (2026-11-24, codex-rs).

  • codex-api/src/requests/headers.rs, build_session_headers: sets session-id and thread-id on 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-id and prompt_cache_key equal the session id on every turn.
  • Compaction runs inside one session, so the id holds. A resume keeps it. A fork starts a new session.
  • prompt_cache_key rejected: a host can set it to a custom value or omit it (core/src/thread_manager.rs). It is also in the body.
  • thread-id rejected as the session key: it differs per thread. It is returned as AgentID so subagent threads can be told apart (fix(keepalive): read Codex and OpenCode subagents, stop reading human for other clients #1318).
  • Client mark: 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

  • Cortex: one call, codex.Identify(pctx.Headers). Use SessionID as the key. If it is empty or false, use pctx.SessionID. A host with no headers keeps today's behavior.
  • Bifrost adapter: not wired. resolveSession gets only a BifrostContext with 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's X-Task-Id. When agent.Identify lands, 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.
  • Full ./proxy/ (before the last rework): one failure, TestMetricsCallLatency (a p99 timing check). It fails the same way on an unchanged origin/main here (p99 7.2 ms against the limit).
  • New tests: agent/codex/codex_test.go (each id, originator-only, subagent, nil/empty header, other callers refused) and proxy/codexsession_test.go (same key after a compaction, any route, non-Codex caller ignored, no header keeps the hash, x-context-guru-session wins).
  • No live Codex run, no benchmark.

Open questions

@amiddavid

Copy link
Copy Markdown
Collaborator Author

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]>
amiddavid added a commit that referenced this pull request Nov 24, 2026
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]>

@amiddavid amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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's session-id header. Add it, so the map matches the code.
  • proxy/compactioncfg.go:183 — a third Codex detector. clientOf matches the User-Agent start only. codex.Identify also matches the originator header. 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 — /compact reads only x-context-guru-session. Codex traffic replayed through /compact without 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, AgentID and ParentID are 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. Identify is a pure function; it cannot panic. An unusable session-id value fails safeExplicit and falls back to the content hash. A panic in the route agent's SessionID gives "", and the Codex id still applies.
  • Precedence. x-context-guru-session wins, then metadata.user_id / taskId, then the route agent's id (Bob's X-Task-Id), then the Codex id. TestCodexSessionIDLosesToTheContextGuruHeader covers the header win.
  • Hooks and the keep-alive. SessionEnd and SessionStart compact name the scoped session-id. Entries now key by that id directly. RetireSession matches Key.Session or Client, so both old (alias) and new (direct) entries retire. A Codex turn whose User-Agent is stripped keys by content hash, and clientSessionAlias still 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.Scoped keeps it inside the tenant namespace, the same as x-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 sends session-id does 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.go lists agent/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 amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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-Id as a header source. The Codex session-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 — Name is 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. clientOf matches the User-Agent prefix only; codex.Identify also matches the originator header. An app on the Codex app server with its own User-Agent and a codex… originator now keys its session by session-id but still gets the table compaction point. The narrowness in clientOf looks 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 TestCodexSessionEndMatchesTheSessionIDHeader now 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.md and docs/design.md.
  • Precedence chain. explicitSessionOf (proxy/proxy.go:796) adds the Codex id after the route agent's id. The final order is x-context-guru-session > metadata.user_id / metadata.taskId > agent id > Codex id, because host ids go last in apply.ExplicitSession (apply/apply.go:358) and session.Explicit (session/session.go:132). The nested second resolution (sessionIDFrom, apply/apply.go:1521; bodyResponsesOpts, apply/responses.go:525) treats explicitSessionOf's result as the header candidate, so the order holds on both wires. No component injects metadata, so the double pass cannot reorder anything.
  • Tenant safety. The Codex id is validated by safeExplicit and tenant-prefixed by session.Scoped (session/session.go:240), like any explicit id. A tenant cannot name another tenant's session with the header.
  • Fail open. codex.Identify is 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 clientSessionAlias returns "". The core retires on Key.Session OR Record.Client (keepalive/keeper.go:439-442), and endedAfter checks both ids (proxy/keepalive.go:368-371), so SessionEnd and SessionStart compact events still retire Codex entries, now on the primary key instead of the alias.
  • Route independence. codexSessionID reads only r.Header, not upPath, so all chat routes give the same key. The gate is the caller's claim (User-Agent or originator starts with codex), documented in agent/codex/codex.go and in the PR body with the codex-rs source files.
  • Library boundary. agent/codex imports only net/http and strings. agent/imports_test.go now lists it, and the proxy imports it, so go list -deps ./... covers the guard.
  • Data dependence. The session-tracker row file and the generated docs/data-dependence.md page 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.go proves 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, and x-context-guru-session winning. agent/codex/codex_test.go covers each id, the originator-only case, subagent headers, blank values, and refused callers. No time.Sleep in 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]>
@amiddavid

Copy link
Copy Markdown
Collaborator Author

Round 1 replies, both reviewers. Fixed in f029739667e2.

Fixed

  • Stale comment in proxy/clientsignal.go (signal): reworded. Codex's hook id now equals the key.
  • Stale Codex alias examples in keepalive/keeper.go (both): reworded.
  • docs/design.md Session keying: added Codex's session-id (caller-gated by codex.Identify, any route).
  • Test name: renamed to TestSessionEndMatchesTheSessionIDHeaderOfANonCodexCaller, with its comment.

Not changed, with reason

Tests (macOS, Go 1.27.4, race): ./keepalive/, ./agent/..., and the proxy session, hook and alias tests pass.

@amiddavid amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. proxy/clientsignal.go:192 stale 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.
  2. docs/design.md Session keying — fixed. The section now lists Codex's session-id after Bob's X-Task-Id and before the plugin id, which matches the code's precedence (host ids last, agent id before Codex id). The gate (User-Agent or originator starts with codex, any route) matches codex.Identify.
  3. codex.Name unused — 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.
  4. Two Codex detectors — rejected, reason checked and accepted. The author says #1312 makes agent.Identify the one source and compactionpoint.ClientOf will take its result; the narrow rule in clientOf stays until then, because a wrong match gives a stranger another client's compaction point. That matches what both reviews read in compactioncfg.go. I cannot show the reason is wrong. Accept.
  5. 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:41 and :440 stale 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.
  • /compact reads only x-context-guru-session — rejected, reason accepted. Pre-existing behavior, out of scope; a replay can set the header. I verified in round 1 that /compact takes 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.client and Record.Client comments), 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.
  • The head is f029739667e2, the head this review names. git status shows 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 amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

  1. 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.
  2. 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.
  3. docs/design.md Session keying list — fixed. The list now names Codex's session-id after Bob's X-Task-Id, with the caller gate and route rule. The order matches the code (host ids last, Bob's before Codex's).
  4. 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.Identify will become the one source. Accepted; no defect stays.
  5. /compact reads only x-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.
  6. 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 (f0297396 docs: 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 RetireSession matches Key.Session or Client.
  • 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.

@amiddavid

Copy link
Copy Markdown
Collaborator Author

Proxy/library split: confirmed by the proxy-boundary session (#1309, #1315). OK to hand off at head f0297396.

@amiddavid
amiddavid merged commit d6cc528 into main Nov 24, 2026
9 checks passed
@github-project-automation github-project-automation Bot moved this from New/ToDo to Done in Rossoctl Issue Prioritization Nov 24, 2026
@amiddavid
amiddavid deleted the feat/codex-session branch November 24, 2026 10:35
amiddavid added a commit that referenced this pull request Nov 24, 2026
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]>
amiddavid added a commit that referenced this pull request Nov 24, 2026
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]>
amiddavid added a commit that referenced this pull request Nov 25, 2026
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

fix(session): key Codex sessions by Codex's own session id, not a content hash

1 participant