graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
codex: partition the prompt cache by account, not by repo
justrachf4cae1ad3713
diff --git a/docs/adr/0028-codex-session-id-is-the-cache-key.md b/docs/adr/0028-codex-session-id-is-the-cache-key.mdindex 5c4da58d..bc0ad264 100644--- a/docs/adr/0028-codex-session-id-is-the-cache-key.md+++ b/docs/adr/0028-codex-session-id-is-the-cache-key.md@@ -19,7 +19,8 @@ The first-party client never splits those two ids.- Codex `session_id` (HTTP extra headers and the WS handshake) is the samevalue as the Responses body's `prompt_cache_key` (`requestCacheKey`:- project root for main/`/btw`, four role lanes for children).+ project root for main/`/btw`, four role lanes for children; ADR 0223: the+ account base when the codex provider has an account id).- `sessionId()` stays a per-process UUIDv4 for the persisted graff sessionrecord. It is not the ChatGPT cache partition.- `store: false` is unchanged (openai/codex also sends `store: false`).diff --git a/docs/adr/0069-cache-affinity-is-the-git-root.md b/docs/adr/0069-cache-affinity-is-the-git-root.mdindex 2c6af8f7..453a0f0e 100644--- a/docs/adr/0069-cache-affinity-is-the-git-root.md+++ b/docs/adr/0069-cache-affinity-is-the-git-root.md@@ -44,3 +44,6 @@ Request serialization is byte-identical across those sibling keys for thesupported root and child lanes. This establishes cache eligibility, not aprovider cache-hit or latency guarantee; automatic-cache providers may ignorethe routing key and live comparisons must measure their returned usage.++Codex with a ChatGPT account id (2026-09-30, ADR 0223): the root base is the+account, not the git root, because that prefix carries no cwd.diff --git a/docs/adr/0223-codex-cache-partition-is-the-account.md b/docs/adr/0223-codex-cache-partition-is-the-account.mdnew file mode 100644index 00000000..b15fab0e--- /dev/null+++ b/docs/adr/0223-codex-cache-partition-is-the-account.md@@ -0,0 +1,45 @@+# 0223. The codex cache partition is the account, not the repo++Status: accepted 2026-09-30++## Context++ADR 0028 made the codex `session_id` header the Responses+`prompt_cache_key`, and ADR 0069 seeded that key on the git root (or one+shared scratch seed). The system prompt and the tool catalog carry no cwd:+a session in any repository sends the same prefix bytes. Every repository+still got its own key, so turn 1 in a repository the account had not used+recently read that prefix cold, even when another repository had just warmed+it. Eval sandboxes are each their own git repository, which made the cold+first read the largest single cost gap against a reference harness that+warms its own prefix with an extra request per thread.++A hill-climb round against that harness, with the order of the arms then+reversed, measured one key per account: turn-1 cache reads rose, cost per+task fell on the train and the held-out split, and pass rate held.++## Decision++- For the codex provider with a ChatGPT account id, the root partition base+ is a name-derived UUID of the account (`cache_affinity.accountRootId`).+ Root labels (`main`, `/btw`) use it; children keep their four role lanes on+ it (`<base>-child-N`, ADR 0069's fan-out rule).+- `http_headers.requestCacheKey` takes the Provider. Every request path uses+ it (HTTP, HTTP/2, the WebSocket handshake, the Responses body, the+ non-streaming post, and server compaction), so header and body stay equal.+- Other providers, and codex without an account id, keep the project key.++## Consequences++One account's sessions share one routing key on this backend. Parallel+sessions concentrate on it; subagent fan-out still spreads over the child+lanes. Revisit if traces show conversation-tail cache reads dropping under+concurrency.++The persisted session `prompt_cache_key` stays the project id. Resume does+not need it for codex: the account key is derived per request.++Offline guards: `cache_key_tests.zig` (account partition, `/btw` and role+lanes on it, another account is another partition, other providers and a+missing account keep the project key, `session_id` header agreement) and the+catalog-wide `spec_prompt_cache_conformance.zig`.diff --git a/docs/adr/README.md b/docs/adr/README.mdindex 15b3eca8..9061ac1e 100644--- a/docs/adr/README.md+++ b/docs/adr/README.md@@ -247,6 +247,7 @@ record only when you need the evidence or the edge cases.| [0218](0218-background-jobs-survive-a-crash.md) | A finite root job started in the background writes its output to `.graff/job-output/<id>/` with an exit receipt from its own shell, so it outlives a crashed graff. Loading the session re-attaches this session's unreported jobs under their old ids, reports what ended while graff was down, and sweeps other sessions' stale captures. Foreground commands, servers, subagent jobs and Windows keep pipes. || [0219](0219-claude-5-5-era-request-contract.md) | Claude Opus 5.5, Sonnet 5.5 and Fable 5.1 are never forced to a tool, get `drop_block` thinking binding (with its beta header), take `/effort` as `output_config.effort` (left off at graff's default), and get 64K/128K `max_tokens`. Claude errors take the same retry-without ladder as other providers; a refusal ends the turn outside history. Default model `claude-opus-5-5`, ladder opus-5-5 → sonnet-5-5. || [0220](0220-claude-prompt-cache-forks-and-pricing.md) | On the Anthropic API, compaction's note and summary fork the conversation while its cache is warm. They use the same tools, system prompt and history, with the instruction appended, so they read the history instead of rewriting it. Otherwise the old shape runs without breakpoints. A turn-opening request keeps the TTL of the last gap inside a turn. 1-hour writes bill at 2x. 5.5-era models get graff's effort as shown; titles and recaps run at low. A second breakpoint marks where the previous request wrote. |+| [0223](0223-codex-cache-partition-is-the-account.md) | Codex with a ChatGPT account id partitions the prompt cache by account, not repo: the system+tools prefix carries no cwd, so turn 1 in any repo reads the prefix another repo warmed. Children keep their role lanes on the account base; other providers keep the project key (ADR 0069). |## When to write onediff --git a/src/agent_request.zig b/src/agent_request.zigindex f51af1e0..4cb16580 100644--- a/src/agent_request.zig+++ b/src/agent_request.zig@@ -234,7 +234,7 @@ pub fn request(self: *Agent, tools_in: ?[]const u8) !std.json.ObjectMap {var retry_limit: ?usize = null;while (true) : (attempt += 1) {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, self.provider, &conv_buf);usage_attempts.begin();const attempt_body = if (live)self.postLive(body)diff --git a/src/agent_request_body.zig b/src/agent_request_body.zigindex 92ffab49..22d4dad2 100644--- a/src/agent_request_body.zig+++ b/src/agent_request_body.zig@@ -185,9 +185,9 @@ pub fn buildBody(self: *Agent, tools_in: ?[]const u8, force_tool: bool, stream:// Sticky cache partition. Same value as x-grok-conv-id (xAI// official maximize-hits). Root is the project id; children// share a role lane so sibling scouts reuse system+tools.- try http_headers.writeRequestCacheKey(&s, self.io, self.label, self, self.provider.id);+ try http_headers.writeRequestCacheKey(&s, self.io, self.label, self, self.provider);var session_key: [96]u8 = undefined; // #1284: OpenRouter's sticky session + Anthropic breakpoint- try @import("openrouter_cache.zig").write(&s, self.provider.id, self.provider.model, http_headers.requestCacheKey(self.io, self.label, self, self.provider.id, &session_key), @import("cache_ttl.zig").control(self));+ try @import("openrouter_cache.zig").write(&s, self.provider.id, self.provider.model, http_headers.requestCacheKey(self.io, self.label, self, self.provider, &session_key), @import("cache_ttl.zig").control(self));// Provider-specific thinking controls and reasoning effort.try @import("zai_wire.zig").writeChatExtras(&s, self.provider.id, self.provider.model, self.sendReasoningEffort(), @import("effort_route.zig").wireEffort(self.provider.id, self.provider.model, @tagName(self.reasoning)));// --output-schema: structured outputs (xAI docs' response_format).diff --git a/src/agent_request_body_responses.zig b/src/agent_request_body_responses.zigindex 3fec847a..67d836d7 100644--- a/src/agent_request_body_responses.zig+++ b/src/agent_request_body_responses.zig@@ -26,7 +26,7 @@ pub fn write(self: *Agent, s: *std.json.Stringify, tools: ?[]const u8, force_too// prefix lanes so repeated workflow roles can reuse the stable prompt.var ckbuf: [96]u8 = undefined;try s.objectField("prompt_cache_key");- try s.write(http_headers.requestCacheKey(self.io, self.label, self, self.provider.id, &ckbuf));+ try s.write(http_headers.requestCacheKey(self.io, self.label, self, self.provider, &ckbuf));const explicit_cache = supportsExplicitPromptCache(self);if (explicit_cache) {try s.objectField("prompt_cache_options");diff --git a/src/agent_server_compact.zig b/src/agent_server_compact.zigindex febd7f89..fc5daec8 100644--- a/src/agent_server_compact.zig+++ b/src/agent_server_compact.zig@@ -212,7 +212,7 @@ fn compactStandalone(self: *Agent) !usize {var compact_provider = self.provider;compact_provider.url = try compactEndpoint(self.arena, self.provider.url);var conv_buf: [96]u8 = undefined;- const conv = @import("http_headers.zig").promptCacheKey(self.io, self.label, self, &conv_buf);+ const conv = @import("http_headers.zig").requestCacheKey(self.io, self.label, self, self.provider, &conv_buf);const response_body = try http.postWatched(self.gpa, self.io, self.client, compact_provider, body, conv);defer self.gpa.free(response_body);const response = std.json.parseFromSliceLeaky(std.json.Value, self.arena, response_body, .{ .allocate = .alloc_always }) catch {@@ -475,7 +475,7 @@ pub fn explicitCompact(self: *Agent) bool {cp.url = compact_url;if (!main_mod.json_mode and @import("repl.zig").g_debug) self.say("[compacting server-side: {d} item(s)…]\n", .{self.messages.items.len}) catch {};var conv_buf: [96]u8 = undefined;- const conv = @import("http_headers.zig").promptCacheKey(self.io, self.label, self, &conv_buf);+ const conv = @import("http_headers.zig").requestCacheKey(self.io, self.label, self, self.provider, &conv_buf);const resp = http.postWatched(self.gpa, self.io, self.client, cp, body, conv) catch |err| {if (self.tracer) |tr| tr.note("compact", @errorName(err));return false;diff --git a/src/agent_stream.zig b/src/agent_stream.zigindex ae2ac033..e912c0e5 100644--- a/src/agent_stream.zig+++ b/src/agent_stream.zig@@ -86,7 +86,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.requestCacheKey(self.io, self.label, self, provider.id, &conv_buf);+ const conv = http_headers.requestCacheKey(self.io, self.label, self, provider, &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_stream_h2.zig b/src/agent_stream_h2.zigindex 63c7c3b8..1ee1baab 100644--- a/src/agent_stream_h2.zig+++ b/src/agent_stream_h2.zig@@ -46,7 +46,7 @@ pub fn postStream(self: *Agent, body: []const u8, poll_stdin: bool) !?[]u8 {defer 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.requestCacheKey(self.io, self.label, self, provider.id, &conv_buf);+ const conv = http_headers.requestCacheKey(self.io, self.label, self, provider, &conv_buf);const extra = http_headers.providerHeadersWithConv(self.io, provider, bearer, &headers_buf, conv);var name_store: [16][64]u8 = undefined;diff --git a/src/agent_ws.zig b/src/agent_ws.zigindex b5228886..1b4d6ba0 100644--- a/src/agent_ws.zig+++ b/src/agent_ws.zig@@ -351,7 +351,7 @@ 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.requestCacheKey(self.io, self.label, self, provider.id, &conv_buf);+ const conv = http_headers.requestCacheKey(self.io, self.label, self, provider, &conv_buf);var hdrs: [7]ws.Header = undefined;var hn: usize = 1;hdrs[0] = .{ .name = "Authorization", .value = bearer };diff --git a/src/cache_affinity.zig b/src/cache_affinity.zigindex dd272a5b..cb8881b5 100644--- a/src/cache_affinity.zig+++ b/src/cache_affinity.zig@@ -47,6 +47,17 @@ pub fn projectIdForCwd(io: Io, cwd_abs: []const u8, out: *[36]u8) []const u8 {return projectIdFromSeed(affinitySeed(io, cwd_abs, &seed_buf), out);}+/// ADR 0223: the ChatGPT backend's root partition is the signed-in account,+/// not the repo. The system+tools prefix carries no cwd, so every repo sends+/// the same bytes; a per-repo key made turn 1 in each new repo a cold read of+/// them. Null (keep the project key) for other providers or no account id.+pub fn accountRootId(provider_id: []const u8, account: []const u8, out: *[36]u8) ?[]const u8 {+ if (!std.mem.eql(u8, provider_id, "codex") or account.len == 0) return null;+ var seed_buf: [256]u8 = undefined;+ const seed = std.fmt.bufPrint(&seed_buf, "chatgpt-account:{s}", .{account}) catch return null;+ return projectIdFromSeed(seed, out);+}+/// Directory that owns `.git` (file or dir), walking parents of `cwd_abs`./// `buf` holds the returned path. Null if the walk hits the filesystem root.pub fn gitRootOf(io: Io, cwd_abs: []const u8, buf: []u8) ?[]const u8 {diff --git a/src/cache_key_tests.zig b/src/cache_key_tests.zignew file mode 100644index 00000000..5da33e14--- /dev/null+++ b/src/cache_key_tests.zig@@ -0,0 +1,121 @@+//! Prompt-cache partition keys (ADR 0028 / 0069 / 0223), moved off+//! http_headers.zig for the 600-line cap: project and role-lane boundaries,+//! and the ChatGPT-account partition for codex.++const std = @import("std");+const h = @import("http_headers.zig");+const cache_affinity = @import("cache_affinity.zig");+const Provider = @import("provider.zig").Provider;++const openai: Provider = .{ .id = "openai", .kind = .responses, .auth = .bearer, .url = "", .api_key = "k", .model = "m", .context = 1 };+const xai: Provider = .{ .id = "xai", .kind = .responses, .auth = .bearer, .url = "", .api_key = "k", .model = "m", .context = 1 };++fn codexFor(account: []const u8) Provider {+ return .{ .id = "codex", .kind = .responses, .auth = .bearer, .url = "", .api_key = "k", .model = "m", .context = 1, .account = account };+}++test "project and prefix cache keys preserve conversation and sharing boundaries" {+ var fake: usize = 0;+ var sibling_fake: usize = 1;+ const agent: *const anyopaque = @ptrCast(&fake);+ const sibling: *const anyopaque = @ptrCast(&sibling_fake);+ var buf: [96]u8 = undefined;+ const a = h.projectCacheKey(std.testing.io, "main", agent, &buf);+ try std.testing.expectEqual(@as(usize, 36), a.len);+ // Not the version nibble: the project id is process state that session+ // tests restore; the mint path has its own test in http_headers.zig.+ var buf2: [96]u8 = undefined;+ const b = h.projectCacheKey(std.testing.io, "main", agent, &buf2);+ try std.testing.expectEqualStrings(a, b); // durable: no per-process randomness+ try std.testing.expectEqualStrings(a, h.projectRootId(std.testing.io));+ try std.testing.expect(!std.mem.eql(u8, a, h.sessionId(std.testing.io)));++ // Live keys (promptCacheKey) share a role lane — xAI cache is per-server+ // and prefix-matched; a unique suffix per sibling is a forced miss.+ var buf3: [96]u8 = undefined;+ var buf4: [96]u8 = undefined;+ const sub = h.promptCacheKey(std.testing.io, "sub", agent, &buf3);+ const sibling_sub = h.promptCacheKey(std.testing.io, "sub", sibling, &buf4);+ try std.testing.expectEqualStrings(sub, sibling_sub);+ try std.testing.expect(!std.mem.eql(u8, sub, a));++ var btw_buf: [96]u8 = undefined;+ try std.testing.expectEqualStrings(a, h.projectCacheKey(std.testing.io, "btw", sibling, &btw_buf));++ // OpenAI/Codex prefix affinity is stable for a workflow role, but spreads+ // different roles over four lanes to stay below the per-key traffic guide.+ var pbuf1: [96]u8 = undefined;+ var pbuf2: [96]u8 = undefined;+ const prefix1 = h.promptPrefixCacheKey(std.testing.io, "implement", &pbuf1);+ const prefix2 = h.promptPrefixCacheKey(std.testing.io, "implement", &pbuf2);+ try std.testing.expectEqualStrings(prefix1, prefix2);+ try std.testing.expect(std.mem.startsWith(u8, prefix1, a));+ try std.testing.expect(!std.mem.eql(u8, prefix1, a));++ // OpenAI and xAI scouts share a role lane (header and body use this).+ var oai1: [96]u8 = undefined;+ var oai2: [96]u8 = undefined;+ const oai_lane = h.requestCacheKey(std.testing.io, "implement", agent, openai, &oai1);+ try std.testing.expectEqualStrings(oai_lane, h.requestCacheKey(std.testing.io, "implement", sibling, openai, &oai2));+ var xai1: [96]u8 = undefined;+ var xai2: [96]u8 = undefined;+ const xai_lane = h.requestCacheKey(std.testing.io, "implement", agent, xai, &xai1);+ try std.testing.expectEqualStrings(xai_lane, h.requestCacheKey(std.testing.io, "implement", sibling, xai, &xai2));+ try std.testing.expectEqualStrings(oai_lane, xai_lane);++ // rlm's subagent("task") defaults description to "subagent" — siblings+ // share one prefix lane on every provider, including xAI.+ var rlm1: [96]u8 = undefined;+ var rlm2: [96]u8 = undefined;+ const rlm_lane = h.requestCacheKey(std.testing.io, "subagent", agent, xai, &rlm1);+ try std.testing.expectEqualStrings(rlm_lane, h.requestCacheKey(std.testing.io, "subagent", sibling, xai, &rlm2));+ try std.testing.expect(std.mem.indexOf(u8, rlm_lane, "-child-") != null);+}++test "codex with an account id partitions by account, not by repo" {+ var fake: usize = 0;+ var sibling_fake: usize = 1;+ const agent: *const anyopaque = @ptrCast(&fake);+ const sibling: *const anyopaque = @ptrCast(&sibling_fake);+ const io = std.testing.io;+ var want_buf: [36]u8 = undefined;+ const want = cache_affinity.accountRootId("codex", "acct-a", &want_buf).?;+ // The root key is the account's, whatever repo this process runs in.+ var r1: [96]u8 = undefined;+ const root = h.requestCacheKey(io, "main", agent, codexFor("acct-a"), &r1);+ try std.testing.expectEqualStrings(want, root);+ try std.testing.expect(!std.mem.eql(u8, root, h.projectRootId(io)));+ try std.testing.expectEqual(@as(u8, '5'), root[14]); // still a name-derived UUID+ // /btw shares it; children take a role lane on the account base.+ var b1: [96]u8 = undefined;+ try std.testing.expectEqualStrings(root, h.requestCacheKey(io, "btw", sibling, codexFor("acct-a"), &b1));+ var c1: [96]u8 = undefined;+ var c2: [96]u8 = undefined;+ const lane = h.requestCacheKey(io, "implement", agent, codexFor("acct-a"), &c1);+ try std.testing.expectEqualStrings(lane, h.requestCacheKey(io, "implement", sibling, codexFor("acct-a"), &c2));+ try std.testing.expect(std.mem.startsWith(u8, lane, root) and std.mem.indexOf(u8, lane, "-child-") != null);+ // Another account is another partition.+ var o1: [96]u8 = undefined;+ try std.testing.expect(!std.mem.eql(u8, root, h.requestCacheKey(io, "main", agent, codexFor("acct-b"), &o1)));+ // No account id (or another provider): the project key, as before.+ var n1: [96]u8 = undefined;+ var p1: [96]u8 = undefined;+ try std.testing.expectEqualStrings(h.promptCacheKey(io, "main", agent, &p1), h.requestCacheKey(io, "main", agent, codexFor(""), &n1));+ var x1: [96]u8 = undefined;+ var xp: Provider = xai;+ xp.account = "acct-a";+ try std.testing.expectEqualStrings(h.promptCacheKey(io, "main", agent, &p1), h.requestCacheKey(io, "main", agent, xp, &x1));+}++test "codex account key: the session_id header carries the same value" {+ var fake: usize = 0;+ const agent: *const anyopaque = @ptrCast(&fake);+ var kbuf: [96]u8 = undefined;+ const key = h.requestCacheKey(std.testing.io, "main", agent, codexFor("acct-a"), &kbuf);+ var hbuf: [12]std.http.Header = undefined;+ const headers = h.providerHeadersWithConv(std.testing.io, codexFor("acct-a"), "Bearer k", &hbuf, key);+ for (headers) |hd| {+ if (std.mem.eql(u8, hd.name, "session_id")) return std.testing.expectEqualStrings(key, hd.value);+ }+ return error.SessionIdHeaderMissing;+}diff --git a/src/http_headers.zig b/src/http_headers.zigindex 6e6e39ac..4a125691 100644--- a/src/http_headers.zig+++ b/src/http_headers.zig@@ -66,17 +66,23 @@ pub fn promptCacheKey(io: Io, label: []const u8, agent: *const anyopaque, buf: [/// enough sharing for repeated workflow roles, without pushing one key past/// OpenAI's documented ~15 requests/minute cache-routing guidance during fanout.pub fn promptPrefixCacheKey(io: Io, label: []const u8, buf: []u8) []const u8 {- const base = projectRootId(io);+ return partitionKey(projectRootId(io), label, buf);+}++/// The root key or a child's role lane on one partition base.+fn partitionKey(base: []const u8, label: []const u8, buf: []u8) []const u8 {if (std.mem.eql(u8, label, "main") or sharesParentCache(label)) return std.fmt.bufPrint(buf, "{s}", .{base}) catch base;- const lane = std.hash.Wyhash.hash(0, label) & 3;- return std.fmt.bufPrint(buf, "{s}-child-{d}", .{ base, lane }) catch base;+ return std.fmt.bufPrint(buf, "{s}-child-{d}", .{ base, std.hash.Wyhash.hash(0, label) & 3 }) catch base;}-/// Partition for one request. Header (`x-grok-conv-id`) and body-/// (`prompt_cache_key`) must be the same value — xAI treats them as the-/// same sticky-routing id. Role lanes for every provider, including xAI.-pub fn requestCacheKey(io: Io, label: []const u8, agent: *const anyopaque, provider_id: []const u8, buf: []u8) []const u8 {- _ = provider_id;+/// Partition for one request. Header (`x-grok-conv-id` / codex `session_id`)+/// and body (`prompt_cache_key`) must be the same value. Role lanes for every+/// provider; codex with an account id partitions by account (ADR 0223).+pub fn requestCacheKey(io: Io, label: []const u8, agent: *const anyopaque, provider: Provider, buf: []u8) []const u8 {+ // 64 bytes hold the base plus a lane suffix, so partitionKey never falls+ // back to returning the stack-local `account`.+ var account: [36]u8 = undefined;+ if (buf.len >= 64) if (cache_affinity.accountRootId(provider.id, provider.account, &account)) |base| return partitionKey(base, label, buf);return promptCacheKey(io, label, agent, buf);}@@ -85,11 +91,11 @@ pub fn requestCacheKey(io: Io, label: []const u8, agent: *const anyopaque, provi/// unknown field ("Unknown name \"prompt_cache_key\": Cannot find field."),/// and it caches implicitly, so it is the one openai-wire provider that/// carries no partition key. Everything else must still pin one.-pub fn writeRequestCacheKey(s: anytype, io: Io, label: []const u8, agent: *const anyopaque, provider_id: []const u8) !void {- if (std.mem.eql(u8, provider_id, "google")) return;+pub fn writeRequestCacheKey(s: anytype, io: Io, label: []const u8, agent: *const anyopaque, provider: Provider) !void {+ if (std.mem.eql(u8, provider.id, "google")) return;var buf: [96]u8 = undefined;try s.objectField("prompt_cache_key");- try s.write(requestCacheKey(io, label, agent, provider_id, &buf));+ try s.write(requestCacheKey(io, label, agent, provider, &buf));}var project_id_buf: [36]u8 = undefined;@@ -385,63 +391,6 @@ test "projectRootId is minted from the affinity seed, not a second hash" {try std.testing.expectEqualStrings(projectRootId(io), cache_affinity.projectIdForCwd(io, cwd_buf[0..n], &want));}-test "project and prefix cache keys preserve conversation and sharing boundaries" {- var fake: usize = 0;- var sibling_fake: usize = 1;- const agent: *const anyopaque = @ptrCast(&fake);- const sibling: *const anyopaque = @ptrCast(&sibling_fake);- var buf: [96]u8 = undefined;- const a = projectCacheKey(std.testing.io, "main", agent, &buf);- try std.testing.expectEqual(@as(usize, 36), a.len);- try std.testing.expectEqual(@as(u8, '5'), a[14]); // version nibble: name-derived- var buf2: [96]u8 = undefined;- const b = projectCacheKey(std.testing.io, "main", agent, &buf2);- try std.testing.expectEqualStrings(a, b); // durable: no per-process randomness- try std.testing.expectEqualStrings(a, projectRootId(std.testing.io));- try std.testing.expect(!std.mem.eql(u8, a, sessionId(std.testing.io)));-- // Live keys (promptCacheKey) share a role lane — xAI cache is per-server- // and prefix-matched; a unique suffix per sibling is a forced miss.- var buf3: [96]u8 = undefined;- var buf4: [96]u8 = undefined;- const sub = promptCacheKey(std.testing.io, "sub", agent, &buf3);- const sibling_sub = promptCacheKey(std.testing.io, "sub", sibling, &buf4);- try std.testing.expectEqualStrings(sub, sibling_sub);- try std.testing.expect(!std.mem.eql(u8, sub, a));-- var btw_buf: [96]u8 = undefined;- try std.testing.expectEqualStrings(a, projectCacheKey(std.testing.io, "btw", sibling, &btw_buf));-- // OpenAI/Codex prefix affinity is stable for a workflow role, but spreads- // different roles over four lanes to stay below the per-key traffic guide.- var pbuf1: [96]u8 = undefined;- var pbuf2: [96]u8 = undefined;- const prefix1 = promptPrefixCacheKey(std.testing.io, "implement", &pbuf1);- const prefix2 = promptPrefixCacheKey(std.testing.io, "implement", &pbuf2);- try std.testing.expectEqualStrings(prefix1, prefix2);- try std.testing.expect(std.mem.startsWith(u8, prefix1, a));- try std.testing.expect(!std.mem.eql(u8, prefix1, a));-- // OpenAI and xAI scouts share a role lane (header and body use this).- var oai1: [96]u8 = undefined;- var oai2: [96]u8 = undefined;- const oai_lane = requestCacheKey(std.testing.io, "implement", agent, "openai", &oai1);- try std.testing.expectEqualStrings(oai_lane, requestCacheKey(std.testing.io, "implement", sibling, "openai", &oai2));- var xai1: [96]u8 = undefined;- var xai2: [96]u8 = undefined;- const xai_lane = requestCacheKey(std.testing.io, "implement", agent, "xai", &xai1);- try std.testing.expectEqualStrings(xai_lane, requestCacheKey(std.testing.io, "implement", sibling, "xai", &xai2));- try std.testing.expectEqualStrings(oai_lane, xai_lane);-- // rlm's subagent("task") defaults description to "subagent" — siblings- // share one prefix lane on every provider, including xAI.- var rlm1: [96]u8 = undefined;- var rlm2: [96]u8 = undefined;- const rlm_lane = requestCacheKey(std.testing.io, "subagent", agent, "xai", &rlm1);- try std.testing.expectEqualStrings(rlm_lane, requestCacheKey(std.testing.io, "subagent", sibling, "xai", &rlm2));- try std.testing.expect(std.mem.indexOf(u8, rlm_lane, "-child-") != null);-}-test "x-grok-conv-id with no explicit conv still uses the project root id" {const io = std.testing.io;const xai: Provider = .{ .id = "xai", .kind = .openai, .auth = .bearer, .url = "", .api_key = "k", .model = "grok-4.6", .context = 500_000 };@@ -470,12 +419,15 @@ test "restoreSessionId overwrites a minted id so a parked worker keeps cache afftest "restoreProjectRootId overwrites a minted id so resume keeps the prompt-cache partition" {const io = std.testing.io;- const before = projectRootId(io);+ // A copy: projectRootId is a view of the buffer the restore overwrites.+ var before: [36]u8 = undefined;+ @memcpy(&before, projectRootId(io));restoreProjectRootId("22222222-2222-4222-8222-222222222222");try std.testing.expectEqualStrings("22222222-2222-4222-8222-222222222222", projectRootId(io));restoreProjectRootId("short");try std.testing.expectEqualStrings("22222222-2222-4222-8222-222222222222", projectRootId(io));- restoreProjectRootId(before);+ restoreProjectRootId(&before);+ try std.testing.expectEqualStrings(&before, projectRootId(io));}fn headerValue(headers: []const std.http.Header, name: []const u8) ?[]const u8 {diff --git a/src/spec_prompt_cache_conformance.zig b/src/spec_prompt_cache_conformance.zigindex e8e6ea7c..676b7402 100644--- a/src/spec_prompt_cache_conformance.zig+++ b/src/spec_prompt_cache_conformance.zig@@ -153,7 +153,7 @@ test "prompt cache: xAI Responses header and body agree; OpenAI Responses has adefer 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.requestCacheKey(ca.io, ca.label, &ca, "codex", &ckbuf), body_key);+ try std.testing.expectEqualStrings(http_headers.requestCacheKey(ca.io, ca.label, &ca, ca.provider, &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);diff --git a/src/test_hooks.zig b/src/test_hooks.zigindex 26919cfb..3d90fa57 100644--- a/src/test_hooks.zig+++ b/src/test_hooks.zig@@ -330,6 +330,7 @@ test {_ = retry_hint;_ = agent_compact_summary_test;_ = side_question_tests;+ _ = @import("cache_key_tests.zig"); // ADR 0223: moved off http_headers.zig (600-line cap)_ = json_controls;_ = effort_route;_ = agent_request_encrypted_tests;