graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
test: fail if two scratch sandboxes mint different cache ids
Cursor Agenteba4bf2d5e5b
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 192b9ed3..550ef7fe 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -14,7 +14,8 @@ current is part of cutting a release.- Prompt-cache affinity is the git root, or one scratch seed when there isno `.git`. Eval sandboxes and worktrees can reuse a warm system+tools- prefix instead of paying it on every leaf cwd. ADR 0069.+ prefix instead of paying it on every leaf cwd. An offline test fails if+ two scratch trees mint different `prompt_cache_key`s. ADR 0069.- `#753`: a background-agent handle survives a recoverable API interruption.`agent_output` replays a finished report, or says the launch ended with theparent turn, instead of "it may never have started". The ids ride in thediff --git a/docs/adr/0069-cache-affinity-is-the-git-root.md b/docs/adr/0069-cache-affinity-is-the-git-root.mdindex f810f64d..6a968e91 100644--- a/docs/adr/0069-cache-affinity-is-the-git-root.md+++ b/docs/adr/0069-cache-affinity-is-the-git-root.md@@ -26,3 +26,8 @@ First request after the seed change is a one-time miss, then siblingjobs share a routing id and may evict each other's tails; the prefixstill hits when the bytes match. Revisit only if a same-session tableshows pass or wall regressing to buy the cache.++The offline guard is `affinity: two scratch sandboxes share one project+cache id` (tier 1 `cache-affinity-scratch`): two `/tmp` leaves must mint+the same UUID `projectRootId` would send. Hashing the leaf cwd is a+forced miss, asserted separately. No provider call.diff --git a/scripts/eval/tier1-manifest.json b/scripts/eval/tier1-manifest.jsonindex 396a6488..b4811142 100644--- a/scripts/eval/tier1-manifest.json+++ b/scripts/eval/tier1-manifest.json@@ -15,7 +15,7 @@"src/repl.zig","TUI/root.zig"],- "test_count_baseline": 1992,+ "test_count_baseline": 1997,"test_count_slack": 25,"required_invariants": [{@@ -62,6 +62,11 @@"id": "child-deadline","test": "childTaskPrompt: the same absolute deadline reaches every child, minus a margin","why": "d33c4a4: a timed /loop must hand its deadline down, or children outlive the run."+ },+ {+ "id": "cache-affinity-scratch",+ "test": "affinity: two scratch sandboxes share one project cache id",+ "why": "289 remasure: hashing the leaf cwd minted a unique xAI partition per eval sandbox and paid the system+tools prefix at $2/M."}],"docs_only_paths": [diff --git a/src/cache_affinity.zig b/src/cache_affinity.zigindex b77f5816..6e02ff64 100644--- a/src/cache_affinity.zig+++ b/src/cache_affinity.zig@@ -11,6 +11,42 @@ const Io = std.Io;pub const scratch_seed = "graff-scratch";+/// Salt mixed into the durable project id. Keep in one place so a rename+/// here moves `prompt_cache_key` / `x-grok-conv-id` together.+pub const cache_salt = "graff-kimi-project-cache-v1";++/// UUIDv5-shaped project cache id from an already-resolved seed. Same bytes+/// `projectRootId` sends as `prompt_cache_key` / `x-grok-conv-id`.+pub fn projectIdFromSeed(seed: []const u8, out: *[36]u8) []const u8 {+ var raw: [16]u8 = undefined;+ var digest: [32]u8 = undefined;+ var h = std.crypto.hash.sha2.Sha256.init(.{});+ h.update(cache_salt);+ h.update(seed);+ h.final(&digest);+ @memcpy(&raw, digest[0..16]);+ raw[6] = (raw[6] & 0x0f) | 0x50; // version 5: name-derived+ raw[8] = (raw[8] & 0x3f) | 0x80; // variant 1+ const hex = std.fmt.bytesToHex(raw, .lower);+ @memcpy(out[0..8], hex[0..8]);+ out[8] = '-';+ @memcpy(out[9..13], hex[8..12]);+ out[13] = '-';+ @memcpy(out[14..18], hex[12..16]);+ out[18] = '-';+ @memcpy(out[19..23], hex[16..20]);+ out[23] = '-';+ @memcpy(out[24..36], hex[20..32]);+ return out[0..36];+}++/// Project cache id for an absolute cwd: git root when one exists, otherwise+/// the shared scratch seed. Never hashes the leaf of a throwaway tree.+pub fn projectIdForCwd(io: Io, cwd_abs: []const u8, out: *[36]u8) []const u8 {+ var seed_buf: [4096]u8 = undefined;+ return projectIdFromSeed(affinitySeed(io, cwd_abs, &seed_buf), 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 {@@ -101,3 +137,60 @@ test "affinity: scratch trees without .git share one seed, not the leaf cwd" {try std.testing.expectEqualStrings(scratch_seed, affinitySeed(io, b, &b_buf));try std.testing.expect(!std.mem.eql(u8, a, scratch_seed));}++test "affinity: two scratch sandboxes share one project cache id" {+ // 289 list$: hashing the leaf cwd minted a unique prompt_cache_key per+ // eval sandbox, so the system+tools prefix never warmed. Offline — no+ // provider, no model. Would have failed on the cwd-hash.+ if (@import("builtin").os.tag == .windows) return;+ const io = std.testing.io;+ const a_base = "/tmp/graff-aff-id-a";+ const b_base = "/tmp/graff-aff-id-b";+ Io.Dir.cwd().createDirPath(io, a_base ++ "/leaf") catch return error.SkipZigTest;+ defer Io.Dir.cwd().deleteTree(io, a_base) catch {};+ Io.Dir.cwd().createDirPath(io, b_base ++ "/other") catch return error.SkipZigTest;+ defer Io.Dir.cwd().deleteTree(io, b_base) catch {};++ var a_path: [160]u8 = undefined;+ var b_path: [160]u8 = undefined;+ const a = try std.fmt.bufPrint(&a_path, "{s}/leaf", .{a_base});+ const b = try std.fmt.bufPrint(&b_path, "{s}/other", .{b_base});++ var id_a: [36]u8 = undefined;+ var id_b: [36]u8 = undefined;+ var id_seed: [36]u8 = undefined;+ try std.testing.expectEqualStrings(projectIdForCwd(io, a, &id_a), projectIdForCwd(io, b, &id_b));+ try std.testing.expectEqualStrings(projectIdFromSeed(scratch_seed, &id_seed), projectIdForCwd(io, a, &id_a));+}++test "affinity: hashing the leaf cwd is a forced miss" {+ // The 289 bug, spelled as ids: two sibling sandbox paths hash to+ // different UUIDs, and neither is the shared scratch id.+ const a = "/tmp/graff-evals/.sandboxes/task-a-r1";+ const b = "/tmp/graff-evals/.sandboxes/task-b-r1";+ var id_a: [36]u8 = undefined;+ var id_b: [36]u8 = undefined;+ var id_scratch: [36]u8 = undefined;+ try std.testing.expect(!std.mem.eql(u8, projectIdFromSeed(a, &id_a), projectIdFromSeed(b, &id_b)));+ try std.testing.expect(!std.mem.eql(u8, projectIdFromSeed(a, &id_a), projectIdFromSeed(scratch_seed, &id_scratch)));+}++test "affinity: nested repo dirs share one project cache id" {+ const io = std.testing.io;+ var tmp = std.testing.tmpDir(.{});+ defer tmp.cleanup();+ try tmp.dir.createDirPath(io, ".git");+ try tmp.dir.createDirPath(io, "a/b");++ var root_buf: [4096]u8 = undefined;+ const root = root_buf[0..try tmp.dir.realPath(io, &root_buf)];+ var child_buf: [4096]u8 = undefined;+ const child = try std.fmt.bufPrint(&child_buf, "{s}/a/b", .{root});++ var id_root: [36]u8 = undefined;+ var id_child: [36]u8 = undefined;+ var id_leaf: [36]u8 = undefined;+ try std.testing.expectEqualStrings(projectIdForCwd(io, root, &id_root), projectIdForCwd(io, child, &id_child));+ // Old hash of the leaf path would have missed the root's warm prefix.+ try std.testing.expect(!std.mem.eql(u8, projectIdFromSeed(child, &id_leaf), projectIdForCwd(io, child, &id_child)));+}diff --git a/src/http_headers.zig b/src/http_headers.zigindex 3ce9e6f1..0cccd35b 100644--- a/src/http_headers.zig+++ b/src/http_headers.zig@@ -107,35 +107,12 @@ 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);if (project_id_len == 0) {- var raw: [16]u8 = undefined;- {- var cwd_buf: [4096]u8 = undefined;- const n = Io.Dir.cwd().realPathFile(io, ".", &cwd_buf) catch blk: {- @memcpy(cwd_buf[0..1], ".");- break :blk 1;- };- const cwd = cwd_buf[0..n];- var seed_buf: [4096]u8 = undefined;- const seed = cache_affinity.affinitySeed(io, cwd, &seed_buf);- var digest: [32]u8 = undefined;- var h = std.crypto.hash.sha2.Sha256.init(.{});- h.update("graff-kimi-project-cache-v1");- h.update(seed);- h.final(&digest);- @memcpy(&raw, digest[0..16]);- raw[6] = (raw[6] & 0x0f) | 0x50; // version 5: name-derived- raw[8] = (raw[8] & 0x3f) | 0x80; // variant 1- }- const hex = std.fmt.bytesToHex(raw, .lower);- @memcpy(project_id_buf[0..8], hex[0..8]);- project_id_buf[8] = '-';- @memcpy(project_id_buf[9..13], hex[8..12]);- project_id_buf[13] = '-';- @memcpy(project_id_buf[14..18], hex[12..16]);- project_id_buf[18] = '-';- @memcpy(project_id_buf[19..23], hex[16..20]);- project_id_buf[23] = '-';- @memcpy(project_id_buf[24..36], hex[20..32]);+ var cwd_buf: [4096]u8 = undefined;+ const n = Io.Dir.cwd().realPathFile(io, ".", &cwd_buf) catch blk: {+ @memcpy(cwd_buf[0..1], ".");+ break :blk 1;+ };+ _ = cache_affinity.projectIdForCwd(io, cwd_buf[0..n], &project_id_buf);project_id_len = 36;}return project_id_buf[0..project_id_len];@@ -365,6 +342,20 @@ test "OpenRouter chat sends app attribution and no grok conv headers" {try std.testing.expect(saw_categories);}+test "projectRootId is minted from the affinity seed, not a second hash" {+ // Locks the wire: a future change that hashes cwd again inside+ // projectRootId would still pass the seed-string tests in+ // cache_affinity.zig. Offline — no provider.+ const io = std.testing.io;+ var cwd_buf: [4096]u8 = undefined;+ const n = Io.Dir.cwd().realPathFile(io, ".", &cwd_buf) catch blk: {+ cwd_buf[0] = '.';+ break :blk 1;+ };+ var want: [36]u8 = undefined;+ 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;diff --git a/src/prompt_lean_tests.zig b/src/prompt_lean_tests.zigindex 3f35aaa0..5fff92dc 100644--- a/src/prompt_lean_tests.zig+++ b/src/prompt_lean_tests.zig@@ -48,3 +48,21 @@ test "unattended one-shots are told the REAL approval map up front; attended sestry std.testing.expect(std.mem.indexOf(u8, unattended, "--yolo") != null);try std.testing.expect(std.mem.indexOf(u8, unattended, ".harness/settings.json") != null);}++test "lean prefix hash is stable and does not name a sandbox path" {+ // Second bust vector: even with a shared cache key, a prefix that+ // interpolates the cwd is a miss on every eval sandbox. Offline.+ const saved = no_local_tools.lean;+ defer no_local_tools.lean = saved;+ no_local_tools.lean = true;+ const hud = @import("prompt_cache_hud.zig");+ var a_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer a_state.deinit();+ const a = a_state.allocator();+ const lean = prompts.detectCaps();+ const first = try prompts.composeBase(a, lean);+ const second = try prompts.composeBase(a, lean);+ try std.testing.expectEqual(hud.prefixHash(first, ""), hud.prefixHash(second, ""));+ try std.testing.expect(std.mem.indexOf(u8, first, ".sandboxes") == null);+ try std.testing.expect(std.mem.indexOf(u8, first, "/tmp/") == null);+}