graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
feat(cache): send grok-build's x-grok-session-id with the project id
Cursor Agent89839330a5ab
diff --git a/docs/adr/0010-prompt-cache-max-is-visible.md b/docs/adr/0010-prompt-cache-max-is-visible.mdindex 74a2b0e1..094789f1 100644--- a/docs/adr/0010-prompt-cache-max-is-visible.md+++ b/docs/adr/0010-prompt-cache-max-is-visible.md@@ -40,4 +40,6 @@ Live Grok 4.6 (Responses, SuperGrok OAuth, 2026-08-19): same-processappend-only turn 2 cached 3,712 of turn 1's 3,721 input tokens. A newprocess in the same folder started cold at 128 — the official first-requestwrite, not a routing miss. That is the xAI contract working: sticky-`x-grok-conv-id` / `prompt_cache_key`, exact prefix, reasoning replay.+`x-grok-conv-id` / `x-grok-session-id` / `prompt_cache_key`, exact prefix,+reasoning replay. Session-id is grok-build's extra sticky header: always+the project id, even when a child isolates `x-grok-conv-id`.diff --git a/src/agent_ws.zig b/src/agent_ws.zigindex 0750848e..21f20660 100644--- a/src/agent_ws.zig+++ b/src/agent_ws.zig@@ -357,9 +357,9 @@ pub fn postResponsesWs(self: *Agent, body: []const u8) ![]u8 {defer gpa.free(frame);const bearer = try std.fmt.allocPrint(arena, "Bearer {s}", .{provider.api_key});- // ChatGPT tail is codex-only (#502); xAI takes x-grok-conv-id instead —- // the project cache id (root/sub isolated), same sticky value as the- // Responses body's prompt_cache_key, not the per-process session id.+ // ChatGPT tail is codex-only (#502); xAI takes grok-build's affinity+ // 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);var hdrs: [7]ws.Header = undefined;@@ -373,7 +373,7 @@ pub fn postResponsesWs(self: *Agent, body: []const u8) ![]u8 {hdrs[5] = .{ .name = "User-Agent", .value = "codex_cli_rs/0.1 (graff)" };hn = 6;} else if (http_headers.wantsGrokConvId(provider.id)) {- hdrs[1] = .{ .name = "session_id", .value = http_headers.sessionId(self.io) };+ hdrs[1] = .{ .name = "x-grok-session-id", .value = http_headers.projectRootId(self.io) };hdrs[2] = .{ .name = "x-grok-conv-id", .value = conv };hn = 3;}diff --git a/src/grok_spec_conformance.zig b/src/grok_spec_conformance.zigindex 4d783a07..299bb0dd 100644--- a/src/grok_spec_conformance.zig+++ b/src/grok_spec_conformance.zig@@ -77,6 +77,17 @@ test "grok spec: Chat Completions x-grok-conv-id is stable for root and isolatedtry std.testing.expectEqualStrings(va, vb);try std.testing.expectEqualStrings(child_id, vc);try std.testing.expect(!std.mem.eql(u8, va, vc));+ // grok-build: session-id stays the conversation; conv-id isolates a child.+ const sa = blk: {+ for (a) |h| if (std.mem.eql(u8, h.name, "x-grok-session-id")) break :blk h.value;+ return error.MissingGrokSessionId;+ };+ const sc = blk: {+ for (c) |h| if (std.mem.eql(u8, h.name, "x-grok-session-id")) break :blk h.value;+ return error.MissingGrokSessionId;+ };+ try std.testing.expectEqualStrings(root_id, sa);+ try std.testing.expectEqualStrings(root_id, sc);}// The !live request path: Agent.request → promptCacheKey → postWatched → post.diff --git a/src/http_headers.zig b/src/http_headers.zigindex cad7ea25..42fc527f 100644--- a/src/http_headers.zig+++ b/src/http_headers.zig@@ -146,7 +146,7 @@ pub fn providerHeaders(io: Io, provider: Provider, bearer: []const u8, buf: *[12}/// Same as `providerHeaders`, with an explicit conversation id for xAI.-/// Null falls back to the process session id (one-off posts: title, compact).+/// Null falls back to `projectRootId` (one-off posts: title, compact).pub fn providerHeadersWithConv(io: Io, provider: Provider, bearer: []const u8, buf: *[12]std.http.Header, conv_id: ?[]const u8) []const std.http.Header {var count: usize = 0;switch (provider.auth) {@@ -185,7 +185,13 @@ pub fn providerHeadersWithConv(io: Io, provider: Provider, bearer: []const u8, bcount += 1;}if (wantsGrokConvId(provider.id)) {- buf[count] = .{ .name = "x-grok-conv-id", .value = conv_id orelse projectRootId(io) };+ const root_id = projectRootId(io);+ buf[count] = .{ .name = "x-grok-conv-id", .value = conv_id orelse root_id };+ count += 1;+ // grok-build always sends session + conv. Session stays the durable+ // project id so a child partition still routes onto the same server+ // family; conv-id / prompt_cache_key isolate the prefix itself.+ buf[count] = .{ .name = "x-grok-session-id", .value = root_id };count += 1;}return buf[0..count];@@ -267,6 +273,7 @@ test "x-grok-conv-id with no explicit conv still uses the project root id" {var buf: [12]std.http.Header = undefined;const headers = providerHeadersWithConv(io, xai, "Bearer k", &buf, null);try std.testing.expectEqualStrings(projectRootId(io), headerValue(headers, "x-grok-conv-id") orelse return error.MissingGrokConvId);+ try std.testing.expectEqualStrings(projectRootId(io), headerValue(headers, "x-grok-session-id") orelse return error.MissingGrokSessionId);}test "adoptSessionId validates length and never overwrites a minted id" {@@ -302,17 +309,21 @@ test "xAI Chat Completions headers carry a stable per-conversation x-grok-conv-ivar buf: [12]std.http.Header = undefined;const first = providerHeadersWithConv(io, xai, "Bearer k", &buf, root_id);try std.testing.expectEqualStrings(root_id, headerValue(first, "x-grok-conv-id") orelse return error.MissingGrokConvId);+ try std.testing.expectEqualStrings(root_id, headerValue(first, "x-grok-session-id") orelse return error.MissingGrokSessionId);var buf2: [12]std.http.Header = undefined;const second = providerHeadersWithConv(io, xai, "Bearer k", &buf2, root_id);try std.testing.expectEqualStrings(root_id, headerValue(second, "x-grok-conv-id").?);+ try std.testing.expectEqualStrings(root_id, headerValue(second, "x-grok-session-id").?);var buf3: [12]std.http.Header = undefined;const child_headers = providerHeadersWithConv(io, xai, "Bearer k", &buf3, child_id);try std.testing.expectEqualStrings(child_id, headerValue(child_headers, "x-grok-conv-id").?);+ try std.testing.expectEqualStrings(root_id, headerValue(child_headers, "x-grok-session-id").?);const anth: Provider = .{ .id = "anthropic", .kind = .anthropic, .auth = .x_api_key, .url = "", .api_key = "k", .model = "claude", .context = 200_000 };var abuf: [12]std.http.Header = undefined;const ah = providerHeadersWithConv(io, anth, "", &abuf, root_id);try std.testing.expect(headerValue(ah, "x-grok-conv-id") == null);+ try std.testing.expect(headerValue(ah, "x-grok-session-id") == null);}test "xai login tokens send X-XAI-Token-Auth; API keys do not" {diff --git a/src/prompt_cache_hud.zig b/src/prompt_cache_hud.zigindex a0a3fe75..53cc0ebd 100644--- a/src/prompt_cache_hud.zig+++ b/src/prompt_cache_hud.zig@@ -205,9 +205,9 @@ pub fn render(w: *Io.Writer) !void {if (g_xai and g_aff_len > 0) {try w.print(" xAI {s} (automatic prefix cache)\n", .{if (g_responses) "responses" else "chat"});try w.print(" affinity {s}\n", .{g_aff[0..g_aff_len]});- try w.writeAll(" x-grok-conv-id + prompt_cache_key, same value\n");+ try w.writeAll(" x-grok-conv-id + x-grok-session-id + prompt_cache_key\n");} else {- try w.writeAll(" xAI x-grok-conv-id + prompt_cache_key (same project id, automatic)\n");+ try w.writeAll(" xAI x-grok-conv-id + x-grok-session-id + prompt_cache_key\n");}try w.writeAll(\\ remaining max