graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
Extend explicit prompt cache and resume its key
justrach75f254621f59
diff --git a/docs/adr/0009-gpt-5-6-explicit-prompt-cache-boundary.md b/docs/adr/0009-gpt-5-6-explicit-prompt-cache-boundary.mdindex 3edc884e..e9b7747f 100644--- a/docs/adr/0009-gpt-5-6-explicit-prompt-cache-boundary.md+++ b/docs/adr/0009-gpt-5-6-explicit-prompt-cache-boundary.md@@ -21,10 +21,11 @@ unsupported parameter, so its supported automatic/keyed path must remain.## Decision-- OpenAI Platform Responses requests on `gpt-5.6*` prepend one stable developer- `input_text` cache anchor after top-level instructions and mark it with an- explicit breakpoint. Codex subscription, xAI, and older models receive- neither the anchor nor Platform-only options.+- OpenAI Platform Responses requests on GPT-5.6 **and later** (`gpt-5.6*`,+ `gpt-6-astra`, …) prepend one stable developer `input_text` cache anchor+ after top-level instructions and mark it with an explicit breakpoint. Codex+ subscription, xAI, and older models receive neither the anchor nor+ Platform-only options.- Root sessions use `prompt_cache_options.mode = "implicit"`: the explicitstable prefix remains reusable while append-only conversation turns keep theuseful latest-message checkpoint. Subagents use `mode = "explicit"` so theirdiff --git a/src/agent_request_body_cache_test.zig b/src/agent_request_body_cache_test.zignew file mode 100644index 00000000..7d53b5a6--- /dev/null+++ b/src/agent_request_body_cache_test.zig@@ -0,0 +1,46 @@+const std = @import("std");+const bodies = @import("agent_request_body_responses.zig");++test "GPT-5.6 and later Platform marks the stable prefix; Codex and older routes do not" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const a = arena_state.allocator();+ var oai = try bodies.testAgentFor(a, "openai", .responses, "gpt-5.6");+ const ob = try oai.buildBody(null, false, true, true);+ defer std.testing.allocator.free(ob);+ try std.testing.expect(std.mem.indexOf(u8, ob, "\"prompt_cache_key\":\"") != null);+ try std.testing.expect(std.mem.indexOf(u8, ob, "\"prompt_cache_options\":{\"mode\":\"implicit\",\"ttl\":\"30m\"}") != null);+ try std.testing.expect(std.mem.indexOf(u8, ob, "\"role\":\"developer\",\"content\":[{\"type\":\"input_text\"") != null);+ try std.testing.expect(std.mem.indexOf(u8, ob, "\"prompt_cache_breakpoint\":{\"mode\":\"explicit\"}") != null);++ var astra = try bodies.testAgentFor(a, "openai", .responses, "gpt-6-astra");+ const ast = try astra.buildBody(null, false, true, true);+ defer std.testing.allocator.free(ast);+ try std.testing.expect(std.mem.indexOf(u8, ast, "\"prompt_cache_options\":{\"mode\":\"implicit\",\"ttl\":\"30m\"}") != null);+ try std.testing.expect(std.mem.indexOf(u8, ast, "\"prompt_cache_breakpoint\":{\"mode\":\"explicit\"}") != null);++ var worker = try bodies.testAgentFor(a, "openai", .responses, "gpt-5.6-luna");+ worker.sub = true;+ worker.label = "implement";+ const wb = try worker.buildBody(null, false, true, true);+ defer std.testing.allocator.free(wb);+ try std.testing.expect(std.mem.indexOf(u8, wb, "\"prompt_cache_options\":{\"mode\":\"explicit\",\"ttl\":\"30m\"}") != null);++ var codex = try bodies.testAgentFor(a, "codex", .responses, "gpt-5.6-sol");+ const cb = try codex.buildBody(null, false, true, true);+ defer std.testing.allocator.free(cb);+ try std.testing.expect(std.mem.indexOf(u8, cb, "prompt_cache_options") == null);+ try std.testing.expect(std.mem.indexOf(u8, cb, "prompt_cache_breakpoint") == null);++ var astra_codex = try bodies.testAgentFor(a, "codex", .responses, "gpt-6-astra");+ const ac = try astra_codex.buildBody(null, false, true, true);+ defer std.testing.allocator.free(ac);+ try std.testing.expect(std.mem.indexOf(u8, ac, "prompt_cache_options") == null);+ try std.testing.expect(std.mem.indexOf(u8, ac, "prompt_cache_breakpoint") == null);++ var older = try bodies.testAgentFor(a, "openai", .responses, "gpt-5.5");+ const old = try older.buildBody(null, false, true, true);+ defer std.testing.allocator.free(old);+ try std.testing.expect(std.mem.indexOf(u8, old, "prompt_cache_breakpoint") == null);+ try std.testing.expect(std.mem.indexOf(u8, old, "prompt_cache_options") == null);+}diff --git a/src/agent_request_body_responses.zig b/src/agent_request_body_responses.zigindex 45480bff..9dff7a3e 100644--- a/src/agent_request_body_responses.zig+++ b/src/agent_request_body_responses.zig@@ -113,7 +113,7 @@ pub fn write(self: *Agent, s: *std.json.Stringify, tools: ?[]const u8, force_too}fn supportsExplicitPromptCache(self: *const Agent) bool {- return std.mem.eql(u8, self.provider.id, "openai") and std.mem.startsWith(u8, self.provider.model, "gpt-5.6");+ return std.mem.eql(u8, self.provider.id, "openai") and @import("provider_codegraff.zig").openaiGptFamily(self.provider.model);}fn writeCacheAnchor(s: *std.json.Stringify) !void {@@ -369,36 +369,8 @@ test "#695: an empty catalog string is omitted from the body, never serialized atry std.testing.expect(std.mem.indexOf(u8, lb, "\"name\":\"bash\"") != null);}-test "GPT-5.6 Platform marks the stable prefix; Codex and older routes do not" {- var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);- defer arena_state.deinit();- const a = arena_state.allocator();- var oai = try testAgentFor(a, "openai", .responses, "gpt-5.6");- const ob = try oai.buildBody(null, false, true, true);- defer std.testing.allocator.free(ob);- try std.testing.expect(std.mem.indexOf(u8, ob, "\"prompt_cache_key\":\"") != null);- try std.testing.expect(std.mem.indexOf(u8, ob, "\"prompt_cache_options\":{\"mode\":\"implicit\",\"ttl\":\"30m\"}") != null);- try std.testing.expect(std.mem.indexOf(u8, ob, "\"role\":\"developer\",\"content\":[{\"type\":\"input_text\"") != null);- try std.testing.expect(std.mem.indexOf(u8, ob, "\"prompt_cache_breakpoint\":{\"mode\":\"explicit\"}") != null);-- var worker = try testAgentFor(a, "openai", .responses, "gpt-5.6-luna");- worker.sub = true;- worker.label = "implement";- const wb = try worker.buildBody(null, false, true, true);- defer std.testing.allocator.free(wb);- try std.testing.expect(std.mem.indexOf(u8, wb, "\"prompt_cache_options\":{\"mode\":\"explicit\",\"ttl\":\"30m\"}") != null);-- var codex = try testAgentFor(a, "codex", .responses, "gpt-5.6-sol");- const cb = try codex.buildBody(null, false, true, true);- defer std.testing.allocator.free(cb);- try std.testing.expect(std.mem.indexOf(u8, cb, "prompt_cache_options") == null);- try std.testing.expect(std.mem.indexOf(u8, cb, "prompt_cache_breakpoint") == null);-- var older = try testAgentFor(a, "openai", .responses, "gpt-5.5");- const old = try older.buildBody(null, false, true, true);- defer std.testing.allocator.free(old);- try std.testing.expect(std.mem.indexOf(u8, old, "prompt_cache_breakpoint") == null);- try std.testing.expect(std.mem.indexOf(u8, old, "prompt_cache_options") == null);+test {+ _ = @import("agent_request_body_cache_test.zig");}test "xai Responses body is bearer-clean: no codex-isms, xAI-legal fields only (#502)" {diff --git a/src/agent_ws.zig b/src/agent_ws.zigindex e837dd61..35746f12 100644--- a/src/agent_ws.zig+++ b/src/agent_ws.zig@@ -51,12 +51,9 @@ const rawNonblockStdin = Agent.rawNonblockStdin;const drainSteerStdin = Agent.drainSteerStdin;/// WS: root Responses turns when enabled and not fallen back this session-/// (codex + xai + Codegraff gateway; Platform OpenAI has no WS endpoint).+/// (codex + xai + Codegraff; Platform OpenAI GPT-6 for `response.steer`).pub fn wsEligible(self: *Agent) bool {- // Provider id is a production filter on top of the spec'd kind/flag- // algebra: only these providers actually serve a WS endpoint.- const has_ws = std.mem.eql(u8, self.provider.id, "codex") or std.mem.eql(u8, self.provider.id, "xai") or std.mem.eql(u8, self.provider.id, "codegraff");- return has_ws and transport_gate.eligible(.{ .kind = self.provider.kind, .is_sub = self.sub, .codex_ws = main_mod.g_codex_ws, .ws_off = self.ws_off, .has_out = self.out != null, .quiet = self.stream_quiet });+ return @import("agent_ws_steer.zig").providerHasWs(self.provider.id, self.provider.model) and transport_gate.eligible(.{ .kind = self.provider.kind, .is_sub = self.sub, .codex_ws = main_mod.g_codex_ws, .ws_off = self.ws_off, .has_out = self.out != null, .quiet = self.stream_quiet });}/// (#codex-ws) Idle limit on the held WS (never reuse a socket the serverdiff --git a/src/agent_ws_steer.zig b/src/agent_ws_steer.zigindex 58e379bb..489c7565 100644--- a/src/agent_ws_steer.zig+++ b/src/agent_ws_steer.zig@@ -20,6 +20,14 @@ pub fn modelSupports(model: []const u8) bool {return std.mem.startsWith(u8, model, "gpt-6");}+/// Who actually serves a Responses WebSocket. Platform OpenAI only for GPT-6+/// (mid-turn `response.steer`); GPT-5.6 stays HTTP SSE. Codex/xAI/gateway+/// keep their existing sockets. 426 still latches SSE.+pub fn providerHasWs(id: []const u8, model: []const u8) bool {+ if (std.mem.eql(u8, id, "codex") or std.mem.eql(u8, id, "xai") or std.mem.eql(u8, id, "codegraff")) return true;+ return std.mem.eql(u8, id, "openai") and modelSupports(model);+}+pub const Session = struct {live_id: []const u8 = "",successor_id: []const u8 = "",@@ -143,6 +151,16 @@ test "modelSupports is gpt-6 only" {try std.testing.expect(!modelSupports("gpt-5.6"));}+test "providerHasWs: Platform OpenAI GPT-6 only; Codex always" {+ try std.testing.expect(providerHasWs("openai", "gpt-6-astra"));+ try std.testing.expect(!providerHasWs("openai", "gpt-5.6"));+ try std.testing.expect(!providerHasWs("openai", "gpt-5.6-luna"));+ try std.testing.expect(providerHasWs("codex", "gpt-5.6-sol"));+ try std.testing.expect(providerHasWs("codex", "gpt-6-astra"));+ try std.testing.expect(providerHasWs("xai", "grok-4.6"));+ try std.testing.expect(providerHasWs("codegraff", "gpt-6-astra"));+}+test "buildSteerFrame is type + previous_response_id + input" {const gpa = std.testing.allocator;const frame = try buildSteerFrame(gpa, "resp_1", "keep it small");diff --git a/src/http_headers.zig b/src/http_headers.zigindex 53dfebbf..0e0d600e 100644--- a/src/http_headers.zig+++ b/src/http_headers.zig@@ -96,13 +96,13 @@ var project_id_buf: [36]u8 = undefined;var project_id_len: usize = 0;var project_id_lock: std.atomic.Value(bool) = .init(false);-/// Durable per-project id (affinity-seed UUIDv5, no state to persist). A new-/// session in the same repo reuses the bucket the last session wrote, so-/// turn 1 can hit the warm system+tools prefix if the provider still has-/// it. Different models do not share a cache (the server keys by model);-/// they each get this same routing id so *their* later sessions can find-/// *their* prefix. Same-project conversation tails may evict each other;-/// the expensive prefix still hits.+/// Durable per-project id (affinity-seed UUIDv5). A new session in the same+/// repo reuses the bucket the last session wrote, so turn 1 can hit the warm+/// system+tools prefix if the provider still has it. `/resume` restores a+/// saved `prompt_cache_key` (see `restoreProjectRootId`) when cwd would hash+/// differently — idle-park, worktree move. Different models do not share a+/// cache (the server keys by model); they each get this same routing id so+/// *their* later sessions can find *their* prefix.pub fn projectRootId(io: Io) []const u8 {while (project_id_lock.cmpxchgWeak(false, true, .acquire, .monotonic) != null) std.atomic.spinLoopHint();defer project_id_lock.store(false, .release);@@ -158,6 +158,16 @@ pub fn restoreSessionId(id: []const u8) void {session_id_len = 36;}+/// `/resume` / idle-park: keep the persisted `prompt_cache_key` even if this+/// process already hashed cwd. Length-checked like `restoreSessionId`.+pub fn restoreProjectRootId(id: []const u8) void {+ if (id.len != 36) return;+ while (project_id_lock.cmpxchgWeak(false, true, .acquire, .monotonic) != null) std.atomic.spinLoopHint();+ defer project_id_lock.store(false, .release);+ @memcpy(project_id_buf[0..36], id[0..36]);+ project_id_len = 36;+}+pub fn userAgent(provider: Provider) std.http.Client.Request.Headers.Value {if (std.mem.eql(u8, provider.id, "kimi")) {return .{ .override = root.kimi_user_agent };@@ -449,6 +459,16 @@ test "restoreSessionId overwrites a minted id so a parked worker keeps cache afftry std.testing.expectEqualStrings("00000000-0000-4000-8000-000000000001", sessionId(io));}+test "restoreProjectRootId overwrites a minted id so resume keeps the prompt-cache partition" {+ const io = std.testing.io;+ const 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);+}+fn headerValue(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;diff --git a/src/session.zig b/src/session.zigindex a42c9ead..7fe4c8fd 100644--- a/src/session.zig+++ b/src/session.zig@@ -485,6 +485,7 @@ pub fn loadSession(root: *Agent, keys: *Keys, arena: Allocator, name: []const u8// negative value simply leaves this process's numbering alone.protocol_seq.restore(eventSeqFromSession(obj));if (cacheKeyFromSession(obj)) |k| http_headers.restoreSessionId(k);+ if (promptCacheKeyFromSession(obj)) |k| http_headers.restoreProjectRootId(k);root.ensureStoredKeys(keys);if (std.mem.eql(u8, pid, "codex")) root.ensureModelCatalog(keys.*);