graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
fix(codex): session_id header is the prompt_cache_key (openai/codex)
justrached50424cd94c
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 8c5b148e..175f9a92 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -20,6 +20,8 @@ current is part of cutting a release.- Kimi Coding keeps User-Agent `graff/<version>` (Moonshot forbids spoofing)and matches kimi-code's X-Msh device fields: hostname, `Windows ${release}${arch}` / `macOS ${product} ${arch}`, kernel `os.release()` (#617)+- Codex `session_id` header is the same value as `prompt_cache_key` (openai/codex+ ModelClient default), not a per-process random UUID## v0.0.275 (2026-08-24)diff --git a/docs/adr/0024-codex-session-id-is-the-cache-key.md b/docs/adr/0024-codex-session-id-is-the-cache-key.mdnew file mode 100644index 00000000..6682e189--- /dev/null+++ b/docs/adr/0024-codex-session-id-is-the-cache-key.md@@ -0,0 +1,31 @@+# 0024. Codex session_id is the prompt_cache_key++Status: accepted 2026-08-25++## Context++[openai/codex](https://github.com/openai/codex) `ModelClient::prompt_cache_key`+defaults to `CodexResponsesMetadata.session_id`. The same string is sent as the+`session_id` header (`build_session_headers`). A hit needs that key plus an+identical instructions/tools prefix.++Graff already sent a durable cwd `prompt_cache_key` (ADR 0009). The ChatGPT+`session_id` header was a **per-process UUIDv4**. Trajectories of a live Codex+session (`gpt-5.6-sol`) still recorded multi-million `cache_read_tokens` on+later calls in a turn, and 0/0 on turns whose usage never landed in `g_cost`.+The first-party client never splits those two ids.++## Decision++- Codex `session_id` (HTTP extra headers and the WS handshake) is the same+ value as the Responses body's `prompt_cache_key` (`requestCacheKey`:+ project root for main/`/btw`, four role lanes for children).+- `sessionId()` stays a per-process UUIDv4 for the persisted graff session+ record. It is not the ChatGPT cache partition.+- `store: false` is unchanged (openai/codex also sends `store: false`).++## Cost++ChatGPT session telemetry now groups by project/lane rather than process.+That is the cache contract. Do not restore a random `session_id` header+without measuring a live `cached_tokens` regression.diff --git a/docs/adr/README.md b/docs/adr/README.mdindex ad6dcb3a..6c315819 100644--- a/docs/adr/README.md+++ b/docs/adr/README.md@@ -34,6 +34,7 @@ record only when you need the evidence or the edge cases.| [0021](0021-transcript-is-the-task-not-the-bus.md) | Transcript is the task, not the event bus: bookkeeping is silent, todos are WORKING, subagents are scouts. || [0022](0022-foreground-bash-auto-backgrounds.md) | Root foreground `bash` auto-backgrounds after 120s (or `timeout` ms); it is not killed. Subagents still kill at 120s (#93). || [0023](0023-kimi-identity-is-graff.md) | Kimi Coding User-Agent is `graff/<version>`, not a spoofed `kimi-code-cli` token. X-Msh device fields follow kimi-code's shapes. |+| [0024](0024-codex-session-id-is-the-cache-key.md) | Codex HTTP/WS `session_id` is the `prompt_cache_key` (openai/codex ModelClient default), not a per-process random UUID. |## When to write onediff --git a/src/agent_stream.zig b/src/agent_stream.zigindex a4dc93e0..5530b621 100644--- a/src/agent_stream.zig+++ b/src/agent_stream.zig@@ -75,7 +75,7 @@ pub fn postStreamWithClient(self: *Agent, client: *std.http.Client, body: []consdefer if (bearer.len > 0) gpa.free(bearer);var headers_buf: [12]std.http.Header = undefined;var conv_buf: [96]u8 = undefined;- const conv = http_headers.promptCacheKey(self.io, self.label, self, &conv_buf);+ const conv = http_headers.requestCacheKey(self.io, self.label, self, provider.id, &conv_buf);const extra = http_headers.providerHeadersWithConv(self.io, provider, bearer, &headers_buf, conv);self.md_buf.clearRetainingCapacity(); // fresh markdown state per streamdiff --git a/src/agent_ws.zig b/src/agent_ws.zigindex 21f20660..a8ed3baa 100644--- a/src/agent_ws.zig+++ b/src/agent_ws.zig@@ -361,12 +361,12 @@ pub fn postResponsesWs(self: *Agent, body: []const u8) ![]u8 {// pair: x-grok-session-id (durable project) + x-grok-conv-id (root/sub// partition, same value as the Responses body's prompt_cache_key).var conv_buf: [96]u8 = undefined;- const conv = http_headers.promptCacheKey(self.io, self.label, self, &conv_buf);+ const conv = http_headers.requestCacheKey(self.io, self.label, self, provider.id, &conv_buf);var hdrs: [7]ws.Header = undefined;var hn: usize = 1;hdrs[0] = .{ .name = "Authorization", .value = bearer };if (std.mem.eql(u8, provider.id, "codex")) {- hdrs[1] = .{ .name = "session_id", .value = http_headers.sessionId(self.io) };+ hdrs[1] = .{ .name = "session_id", .value = conv };hdrs[2] = .{ .name = "chatgpt-account-id", .value = provider.account };hdrs[3] = .{ .name = "OpenAI-Beta", .value = "responses_websockets=2026-02-06" };hdrs[4] = .{ .name = "originator", .value = "codex_cli_rs" };@@ -418,7 +418,7 @@ pub fn postResponsesWs(self: *Agent, body: []const u8) ![]u8 {}// Hold ONE WS across the turn's tool loop (codex-style): the first request// dials; subsequent requests reuse it and send previous_response_id + delta.- // The open connection IS the sticky context (no x-codex-turn-state to echo).+ // session_id matches prompt_cache_key (openai/codex ModelClient default).//// (#401) …and which of the two it is decides the FIRST-frame budget below: a// socket dialed just now finished a TLS + upgrade handshake millisecondsdiff --git a/src/cli.zig b/src/cli.zigindex 6d78b3b9..073b0ed4 100644--- a/src/cli.zig+++ b/src/cli.zig@@ -24,6 +24,7 @@ pub const changelog_text =\\0.0.276\\ • foreground bash auto-backgrounds after 120s instead of waiting forever (#620)\\ • Kimi identity: graff/<version> User-Agent; device headers match kimi-code (#617)+ \\ • Codex session_id header matches prompt_cache_key (openai/codex)\\\\0.0.275\\ • OpenRouter requests carry app attribution (categories: cli-agent, programming-app)diff --git a/src/http_headers.zig b/src/http_headers.zigindex f50fb218..0149ab01 100644--- a/src/http_headers.zig+++ b/src/http_headers.zig@@ -12,10 +12,9 @@ var session_id_len: usize = 0;/// with, and the critical section is a single one-time 36-byte format.var session_id_lock: std.atomic.Value(bool) = .init(false);-/// A UUIDv4 minted once per process. Codex still sends this as the `session_id`-/// header (ChatGPT backend identity). Prompt-cache affinity is `projectRootId`,-/// not this value — a per-process random key made every new session's first-/// call a cold partition.+/// A UUIDv4 minted once per process. Persisted on the graff session record.+/// Codex's HTTP `session_id` is the prompt-cache key (openai/codex ModelClient+/// defaults `prompt_cache_key` to `session_id`), not this value.pub fn sessionId(io: Io) []const u8 {while (session_id_lock.cmpxchgWeak(false, true, .acquire, .monotonic) != null) std.atomic.spinLoopHint();defer session_id_lock.store(false, .release);@@ -217,7 +216,8 @@ pub fn providerHeadersWithConv(io: Io, provider: Provider, bearer: []const u8, bcount += 1;buf[count] = .{ .name = "originator", .value = "codex_cli_rs" };count += 1;- buf[count] = .{ .name = "session_id", .value = sessionId(io) };+ // openai/codex ModelClient: prompt_cache_key defaults to session_id.+ buf[count] = .{ .name = "session_id", .value = conv_id orelse projectRootId(io) };count += 1;}if (wantsGrokConvId(provider.id)) {@@ -251,13 +251,8 @@ test "session_id is a stable per-process UUIDv4, not a shared constant" {var buf: [12]std.http.Header = undefined;const p: Provider = .{ .id = "codex", .kind = .responses, .auth = .bearer, .url = "", .api_key = "k", .model = "gpt-5.6", .context = 272_000, .account = "acct" };const headers = providerHeaders(io, p, "Bearer k", &buf);- for (headers) |h| {- if (std.mem.eql(u8, h.name, "session_id")) {- try std.testing.expectEqualStrings(a, h.value);- return;- }- }- return error.SessionIdHeaderMissing;+ try std.testing.expectEqualStrings(projectRootId(io), headerValue(headers, "session_id") orelse return error.SessionIdHeaderMissing);+ try std.testing.expect(!std.mem.eql(u8, a, headerValue(headers, "session_id").?));}test "official OpenAI Responses uses Platform headers, not ChatGPT backend identity" {diff --git a/src/spec_prompt_cache_conformance.zig b/src/spec_prompt_cache_conformance.zigindex ee9a1c53..4994706a 100644--- a/src/spec_prompt_cache_conformance.zig+++ b/src/spec_prompt_cache_conformance.zig@@ -40,6 +40,11 @@ fn agentFor(arena: std.mem.Allocator, spec: provider_mod.ProviderSpec, label: []};}+fn headerNamed(headers: []const std.http.Header, name: []const u8) ?[]const u8 {+ for (headers) |h| if (std.mem.eql(u8, h.name, name)) return h.value;+ return null;+}+fn cacheKeyIn(body: []const u8) ?[]const u8 {const needle = "\"prompt_cache_key\":\"";const start = std.mem.indexOf(u8, body, needle) orelse return null;@@ -140,9 +145,12 @@ test "prompt cache: xAI Responses header and body agree; OpenAI Responses has avar ca = try agentFor(a, cd, "main", .responses);const cb = try ca.buildBody(null, false, true, true);defer std.testing.allocator.free(cb);+ const body_key = cacheKeyIn(cb) orelse return error.MissingPromptCacheKey;var ckbuf: [96]u8 = undefined;- try std.testing.expectEqualStrings(http_headers.promptCacheKey(ca.io, ca.label, &ca, &ckbuf), cacheKeyIn(cb) orelse return error.MissingPromptCacheKey);- try std.testing.expect(!std.mem.eql(u8, http_headers.sessionId(ca.io), cacheKeyIn(cb).?));+ try std.testing.expectEqualStrings(http_headers.requestCacheKey(ca.io, ca.label, &ca, "codex", &ckbuf), body_key);+ var cbuf: [12]std.http.Header = undefined;+ const ch = http_headers.providerHeadersWithConv(ca.io, ca.provider, "Bearer k", &cbuf, body_key);+ try std.testing.expectEqualStrings(body_key, headerNamed(ch, "session_id") orelse return error.SessionIdHeaderMissing);}test "prompt cache: effort is never auto-flipped (prefix stays)" {