graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
feat(compact): tell the model what survives a compaction (#411)
Rach Pradhanea1e62146c0b
diff --git a/src/agent_compact.zig b/src/agent_compact.zigindex 22170b3d..170ca32d 100644--- a/src/agent_compact.zig+++ b/src/agent_compact.zig@@ -10,9 +10,8 @@ const context_tokens = @import("context_tokens.zig");const main_mod = @import("main.zig");const agent_mod = @import("agent.zig");const Agent = agent_mod.Agent;-const prompts = @import("prompts.zig");-const compact_instruction = prompts.compact_instruction;const goal_flow = @import("goal_flow.zig");+const compact_note = @import("compact_note.zig"); // #411: both halves of "what survives a compaction"const messages_mod = @import("messages.zig");const textMessage = messages_mod.textMessage;@@ -161,7 +160,7 @@ pub fn compact(self: *Agent) anyerror!usize {}};- try self.messages.append(try textMessage(compact_arena, "user", compact_instruction));+ try self.messages.append(try textMessage(compact_arena, "user", try compact_note.summaryRequest(compact_arena, self)));// #174: establish the synthetic summary turn before pruning Responses// reasoning. An active tool loop's reasoning is newer than the real user// turn and must remain while that loop is in flight, but it becomes prior-@@ -195,7 +194,7 @@ pub fn compact(self: *Agent) anyerror!usize {}var fresh = std.json.Array.init(self.arena);- try fresh.append(try textMessage(self.arena, "user", try handoffMessage(self, summary)));+ try fresh.append(try textMessage(self.arena, "user", try handoffMessage(self, summary, live_messages.items[0..recent_start])));// Preserve a valid recent suffix verbatim (up to ~8k estimated tokens),// including its user boundary and paired tool calls/results.for (recent_messages) |message| try fresh.append(message);@@ -225,13 +224,14 @@ pub fn compact(self: *Agent) anyerror!usize {/// Without a pin, compaction would summarize the mandate away with nothing/// left to restate it - childHandoff below restates it verbatim instead of/// re-deriving it, so it can never drift or compound across compactions.-pub fn handoffMessage(self: *Agent, summary: []const u8) ![]const u8 {+/// `discarded` is the history this summary replaces, read only for the #409 artifact paths #411 re-states out of it.+pub fn handoffMessage(self: *Agent, summary: []const u8, discarded: []const Value) ![]const u8 {const base = if (self.sub)(if (self.task_prompt) |tp| try childHandoff(self, tp, summary) else try rootHandoff(self, summary))elsetry rootHandoff(self, summary);- const standing = (try goal_flow.compactionSnapshot(self.arena, self)) orelse return base;- return std.fmt.allocPrint(self.arena, "{s}\n\n{s}", .{ base, standing });+ const standing = try goal_flow.compactionSnapshot(self.arena, self);+ return compact_note.handoff(self.arena, self, base, standing, discarded);}fn rootHandoff(self: *Agent, summary: []const u8) ![]const u8 {diff --git a/src/agent_compact_summary_test.zig b/src/agent_compact_summary_test.zigindex f63e40ee..f7fa73fd 100644--- a/src/agent_compact_summary_test.zig+++ b/src/agent_compact_summary_test.zig@@ -5,6 +5,9 @@ const std = @import("std");const Agent = @import("agent.zig").Agent;const compact = @import("agent_compact.zig");const repeatedEmptySummaryFailure = compact.repeatedEmptySummaryFailure;+const compact_note = @import("compact_note.zig");+const session_transcript = @import("session_transcript.zig");+const compact_instruction = @import("prompts.zig").compact_instruction;test "repeated empty summaries unlock bounded recovery (#379)" {var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);@@ -49,3 +52,42 @@ test "repeated empty summaries unlock bounded recovery (#379)" {agent.compact_summary_failures = 1;try std.testing.expect(!repeatedEmptySummaryFailure(&agent, error.EmptySummary));}++// #411 appended a "what persists" note to the summary REQUEST. #379 classifies+// the RESPONSE to that request, so the two must not interact: the request still+// LEADS with the byte-identical instruction, carries none of the after-the-fact+// ground truth, and two consecutive unusable replies still escalate exactly as+// they did. A note that changed the request's head is the shape of regression+// this guards - it would be invisible in #411's own tests.+test "#411's request note leaves #379's empty-summary escalation exactly as it was" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const a = arena_state.allocator();+ session_transcript.resetForTest();+ defer session_transcript.resetForTest();++ var agent: Agent = undefined;+ agent.provider = .{ .id = "codex", .kind = .responses, .auth = .bearer, .url = "", .api_key = "", .model = "gpt-5", .context = 100_000 };+ agent.messages = std.json.Array.init(a);+ agent.sub = false;+ agent.review_mode = false;+ agent.snapshots = null;+ agent.session_name = "";+ agent.strict = false;+ agent.sys_normal = "";+ agent.sys_strict = "";+ agent.tools_responses = "";+ agent.last_context_tokens = 85_000;+ agent.context_local_tokens = agent.fullRequestEstimateTokens();+ agent.compact_summary_failures = 0;++ const request = try compact_note.summaryRequest(a, &agent);+ try std.testing.expect(std.mem.startsWith(u8, request, compact_instruction));+ try std.testing.expect(std.mem.indexOf(u8, request, "durable state, re-derived") == null);+ try std.testing.expect(!repeatedEmptySummaryFailure(&agent, error.EmptySummary));+ try std.testing.expect(repeatedEmptySummaryFailure(&agent, error.EmptySummary));+ // And a usable summary still ends the streak, which is what stops a healthy+ // session accumulating its way into an emergency trim.+ agent.compact_summary_failures = 0;+ try std.testing.expect(!repeatedEmptySummaryFailure(&agent, error.ApiError));+}diff --git a/src/agent_compact_test.zig b/src/agent_compact_test.zigindex 600c96aa..1952f5fa 100644--- a/src/agent_compact_test.zig+++ b/src/agent_compact_test.zig@@ -440,19 +440,16 @@ test "a compaction handoff carries the live checklist across the summary (#318)"var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);defer arena_state.deinit();const a = arena_state.allocator();- var agent: Agent = undefined;- agent.arena = a;- agent.task_prompt = null; // this test flips agent.sub to true below (#B3)- agent.sub = false;- agent.review_mode = false;- agent.todos = .empty;+ // Root agent, every field handoffMessage reads initialized (incl. #411's+ // ledger + session name); this test flips agent.sub to true below (#B3).+ var agent = th.subAgent(a, false);agent.goal = .{ .objective = "ship the epoch fix", .epoch = 2 };try agent.todos.append(a, .{ .content = "write the helper", .status = "completed", .epoch = 2 });try agent.todos.append(a, .{ .content = "wire it into compact", .status = "completed", .epoch = 2 });try agent.todos.append(a, .{ .content = "test it", .status = "pending", .epoch = 2 });try agent.todos.append(a, .{ .content = "parked by an older goal", .status = "pending", .epoch = 1 });- const handoff = try handoffMessage(&agent, "the model summarized the earlier work");+ const handoff = try handoffMessage(&agent, "the model summarized the earlier work", &.{});// The summary and its framing are unchanged...try std.testing.expect(std.mem.indexOf(u8, handoff, "the model summarized the earlier work") != null);try std.testing.expect(std.mem.indexOf(u8, handoff, "Continue assisting the user based on this summary.") != null);@@ -476,12 +473,12 @@ test "a compaction handoff carries the live checklist across the summary (#318)"// A subagent shares the Agent struct but not the goal, so its handoff is// byte-identical to the pre-#318 text - as is a session with no goal.agent.sub = true;- const plain = try handoffMessage(&agent, "the model summarized the earlier work");+ const plain = try handoffMessage(&agent, "the model summarized the earlier work", &.{});try std.testing.expect(std.mem.indexOf(u8, plain, "standing state") == null);try std.testing.expect(std.mem.endsWith(u8, plain, "Continue assisting the user based on this summary."));agent.sub = false;agent.goal = null;- try std.testing.expectEqualStrings(plain, try handoffMessage(&agent, "the model summarized the earlier work"));+ try std.testing.expectEqualStrings(plain, try handoffMessage(&agent, "the model summarized the earlier work", &.{}));}test "an emergency trim re-queues the standing state, never over a user note (#318)" {@@ -553,7 +550,7 @@ test "a compacting subagent's handoff restates its task prompt verbatim" {const a = arena_state.allocator();var agent = th.subAgent(a, true);agent.task_prompt = "AUDIT src/foo.zig and report every unguarded json deref";- const handoff = try handoffMessage(&agent, "the model summarized the earlier work");+ const handoff = try handoffMessage(&agent, "the model summarized the earlier work", &.{});try std.testing.expect(std.mem.indexOf(u8, handoff, agent.task_prompt.?) != null and std.mem.indexOf(u8, handoff, "the model summarized the earlier work") != null and std.mem.indexOf(u8, handoff, "still your mandate") != null);}test "the root handoff is byte-identical to before the child pin" {@@ -561,7 +558,7 @@ test "the root handoff is byte-identical to before the child pin" {defer arena_state.deinit();const a = arena_state.allocator();var agent = th.subAgent(a, false);- const handoff = try handoffMessage(&agent, "the model summarized the earlier work");+ const handoff = try handoffMessage(&agent, "the model summarized the earlier work", &.{});try std.testing.expectEqualStrings("Context: the earlier conversation was compacted to save space.\nSummary of the earlier work:\n\nthe model summarized the earlier work\n\nContinue assisting the user based on this summary.", handoff);}test "pinChildTask captures the mandate once, never re-pins, and ignores a root agent or an unrecognised head" {@@ -588,7 +585,7 @@ test "an oversized task prompt is head-capped in the child handoff" {var agent = th.subAgent(a, true);const big = util.repeatBytes("T", 20000);agent.task_prompt = &big;- const handoff = try handoffMessage(&agent, "summary text");+ const handoff = try handoffMessage(&agent, "summary text", &.{});try std.testing.expect(handoff.len < 12_000 and std.mem.indexOf(u8, handoff, "task prompt truncated for the handoff") != null and std.mem.indexOf(u8, handoff, big[0..100]) != null and std.mem.indexOf(u8, handoff, &big) == null);}test "emergencyCutIndex finds no cut in a subagent-shaped history, so the pinned mandate survives an emergency trim" {diff --git a/src/agent_compact_test_support.zig b/src/agent_compact_test_support.zigindex 45f45ac4..8bfd0814 100644--- a/src/agent_compact_test_support.zig+++ b/src/agent_compact_test_support.zig@@ -24,6 +24,11 @@ pub fn subAgent(a: std.mem.Allocator, sub: bool) Agent {agent.todos = .empty;agent.goal = null;agent.task_prompt = null;+ // #411: the durable-state note reads the /rewind ledger and the session+ // name on every handoff, including the null paths, so both have to be real+ // here or an `undefined` pointer gets dereferenced instead of skipped.+ agent.snapshots = null;+ agent.session_name = "";// compactPrelude reports the token figure compact() prints, so the estimate// path has to be reachable: system prompt, tool json, provider context and// the two meter anchors it reads.diff --git a/src/compact_note.zig b/src/compact_note.zignew file mode 100644index 00000000..a45e4a47--- /dev/null+++ b/src/compact_note.zig@@ -0,0 +1,372 @@+//! #411: what SURVIVES a compaction, told to the model twice.+//!+//! THE PROBLEM. compact() replaces the history with a summary and never says+//! what the summary does NOT have to carry. The model therefore treats the+//! compaction as total loss and spends the summary hoarding contents - pasted+//! file bodies, quoted tool output - which is both the most expensive thing it+//! can put in a summary and the least necessary, because every one of those+//! bytes is still on disk. Worse, the state the harness itself keeps (the goal,+//! the checklist, the transcript, the spilled artifacts) came back only as the+//! model's recollection of it, which drifts a little more at every compaction.+//!+//! THE TWO HALVES, from prime-agent's compaction design.+//!+//! BEFORE - `summaryRequest`. The summarization request carries a note saying+//! what persists: the files, the goal/checklist, and the append-only transcript+//! (#441). It asks for NAMES rather than contents, and points the summary at+//! what disk genuinely cannot give back - decisions, dead ends, constraints,+//! the state of unfinished work.+//!+//! AFTER - `durableState`. The new history head carries the harness's own+//! ground truth: the files this session modified, the tool outputs spilled to+//! disk by the history that was just discarded, and the transcript path. This+//! is STATE, not conversation. It is re-derived from live harness state at+//! every compaction rather than copied forward, so a later summary that+//! paraphrases it away costs nothing: the next compaction regenerates it+//! exactly, from the ledger and from disk, and it cannot drift.+//!+//! HONESTY RULES. Every field is omitted unless it is real. The transcript line+//! comes from `session_transcript.activePath`, which returns null when no+//! transcript is live, so the note can never cite a file that does not exist.+//! The artifact paths are read back out of the #409 markers in the discarded+//! messages themselves, so each one was written by a spill that succeeded. The+//! file list is `/rewind`'s snapshot ledger, which is exact for the tools that+//! take snapshots and blind to bash - and says so, rather than presenting a+//! partial list as the session's whole diff.++const std = @import("std");+const Allocator = std.mem.Allocator;+const Value = std.json.Value;++const agent_mod = @import("agent.zig");+const Agent = agent_mod.Agent;+const compact_instruction = @import("prompts.zig").compact_instruction;+const session_transcript = @import("session_transcript.zig");+const tool_spill = @import("tool_spill.zig");++/// Most modified paths named before the list is elided. A compaction runs at+/// the point context is scarce, so this block has to stay a note rather than+/// becoming a manifest.+const max_files: usize = 24;++/// Most spilled artifacts named. Same reason, and the marker for each one is a+/// full absolute path.+const max_handles: usize = 12;++const persists_note =+ \\What SURVIVES this compaction, so you do not spend the summary preserving it:+ \\every file on disk is exactly as you left it, and any goal or todo checklist+ \\is harness state that is restated to you in full immediately after this+ \\summary. Record NAMES, not contents: file paths, directories, artifact paths,+ \\the command lines that worked, identifiers. Spend the words instead on what+ \\disk cannot give back - the decisions and why they were made, what was tried+ \\and failed, the constraints the user set, and the exact state of the+ \\unfinished work.+;++/// The user message compact() sends to ask for the handoff summary: the+/// instruction, unchanged, plus the note above. Unchanged matters - #379+/// classifies the RESPONSE to this request, and an empty or truncated reply is+/// still exactly as unusable as it was before the note existed.+pub fn summaryRequest(arena: Allocator, root: *Agent) ![]const u8 {+ const path = session_transcript.activePath(root, arena) orelse+ return std.fmt.allocPrint(arena, "{s}\n\n{s}", .{ compact_instruction, persists_note });+ return std.fmt.allocPrint(arena,+ \\{s}+ \\+ \\{s}+ \\The complete conversation, including every message this summary replaces,+ \\also stays on disk at {s} ({d} messages, one JSON object per line). It is+ \\greppable, so an exact wording you leave out is recoverable rather than+ \\lost - which is another reason to summarize rather than transcribe.+ , .{ compact_instruction, persists_note, path, session_transcript.lineCount() });+}++/// The harness's own answer to "what is still here", assembled from live state+/// at the moment of the compaction. `discarded` is the slice of history the+/// summary replaces, read (not kept) for the artifact paths its markers cite.+/// Null when there is nothing true to report - which keeps a plain session's+/// handoff byte-identical to what it has always been.+pub fn durableState(arena: Allocator, root: *Agent, discarded: []const Value) !?[]const u8 {+ // A subagent has no durable anything: its history is never persisted, it+ // gets no transcript, it never spills, and the /rewind ledger is the root's.+ // A /review turn shares the Agent struct but not the session's work.+ if (root.sub or root.review_mode) return null;+ var lines: std.ArrayList([]const u8) = .empty;+ if (try fileLine(arena, root)) |line| try lines.append(arena, line);+ if (try handleLine(arena, discarded)) |line| try lines.append(arena, line);+ if (try transcriptLine(arena, root)) |line| try lines.append(arena, line);+ if (lines.items.len == 0) return null;+ return try std.fmt.allocPrint(arena,+ \\[durable state, re-derived by the harness at this compaction rather than+ \\recalled, so it cannot drift:+ \\{s}+ \\Everything named above exists on disk right now: read it back instead of+ \\re-deriving it, and do not re-run a tool whose output is already there.]+ , .{try std.mem.join(arena, "\n", lines.items)});+}++/// compact()'s new history head: the model's handoff text, then the standing+/// goal/checklist state (#318), then the durable-state note. Joined with blank+/// lines, and byte-identical to `base` when neither block has anything to say.+pub fn handoff(arena: Allocator, root: *Agent, base: []const u8, standing: ?[]const u8, discarded: []const Value) ![]const u8 {+ var out = base;+ if (standing) |s| out = try std.fmt.allocPrint(arena, "{s}\n\n{s}", .{ out, s });+ if (try durableState(arena, root, discarded)) |d| out = try std.fmt.allocPrint(arena, "{s}\n\n{s}", .{ out, d });+ return out;+}++fn fileLine(arena: Allocator, root: *Agent) !?[]const u8 {+ const snaps = root.snapshots orelse return null;+ const paths = snaps.modifiedPaths(arena);+ if (paths.len == 0) return null;+ const shown = @min(paths.len, max_files);+ const joined = try std.mem.join(arena, ", ", paths[0..shown]);+ const more = if (paths.len > shown)+ try std.fmt.allocPrint(arena, ", and {d} more", .{paths.len - shown})+ else+ "";+ // The parenthetical is the honest bound, not boilerplate: presenting this+ // ledger as the session's whole diff would be a lie whenever a `sed`, a+ // `git apply` or a companion CLI did the writing.+ return try std.fmt.allocPrint(arena, "- files this session modified ({d}; write_file/edit_file/imagegen only - edits made through bash are not tracked): {s}{s}", .{ paths.len, joined, more });+}++/// Only the handles THIS compaction is discarding, deliberately. Re-harvesting+/// the previous note's list would fold each compaction's handles into the next+/// without bound - #B3's task-pin rule, one level down. The artifacts stay on+/// disk regardless, and the transcript line above says where every old marker+/// can still be grepped in full.+fn handleLine(arena: Allocator, discarded: []const Value) !?[]const u8 {+ var paths: std.ArrayList([]const u8) = .empty;+ for (discarded) |m| try collectHandles(arena, m, &paths);+ if (paths.items.len == 0) return null;+ const shown = @min(paths.items.len, max_handles);+ const joined = try std.mem.join(arena, ", ", paths.items[0..shown]);+ const more = if (paths.items.len > shown)+ try std.fmt.allocPrint(arena, ", and {d} more", .{paths.items.len - shown})+ else+ "";+ return try std.fmt.allocPrint(arena, "- full tool outputs the discarded history spilled to disk (#409), still readable: {s}{s}", .{ joined, more });+}++fn transcriptLine(arena: Allocator, root: *Agent) !?[]const u8 {+ const path = session_transcript.activePath(root, arena) orelse return null;+ return try std.fmt.allocPrint(arena, "- this conversation's full transcript, including everything the summary above replaced: {s} ({d} messages, one JSON object per line) - grep it for an exact wording the summary paraphrased", .{ path, session_transcript.lineCount() });+}++/// Walk a message's JSON for #409 spill markers. Recursive because the marker+/// sits in a different field in each wire format (`output`, `content`, or a+/// `tool_result` block inside a content array), and a shape-blind walk cannot+/// be broken by a format this file has not heard of.+fn collectHandles(arena: Allocator, value: Value, out: *std.ArrayList([]const u8)) !void {+ switch (value) {+ .string => |s| if (handlePath(s)) |p| {+ for (out.items) |seen| if (std.mem.eql(u8, seen, p)) return;+ try out.append(arena, p);+ },+ .array => |a| for (a.items) |item| try collectHandles(arena, item, out),+ .object => |o| {+ var it = o.iterator();+ while (it.next()) |entry| try collectHandles(arena, entry.value_ptr.*, out);+ },+ else => {},+ }+}++/// The artifact path inside a spill marker, or null for any other text. All+/// three fences must be present in order: a tool output that merely happens to+/// contain "bytes are at" is not a marker.+fn handlePath(s: []const u8) ?[]const u8 {+ const head = std.mem.indexOf(u8, s, tool_spill.marker_head) orelse return null;+ const rest = s[head..];+ const open = std.mem.indexOf(u8, rest, tool_spill.marker_path_open) orelse return null;+ const tail = rest[open + tool_spill.marker_path_open.len ..];+ const close = std.mem.indexOf(u8, tail, tool_spill.marker_path_close) orelse return null;+ return if (close == 0) null else tail[0..close];+}++// ── tests ────────────────────────────────────────────────────────────────++const testing = std.testing;+const textMessage = @import("messages.zig").textMessage;+const snapshots_mod = @import("snapshots.zig");++/// The fields the two halves read, and nothing else - every one of them is+/// consulted on the null path too, so an `undefined` here is a crash rather+/// than a silent pass.+fn noteAgent(sub: bool) Agent {+ var root: Agent = undefined;+ root.sub = sub;+ root.review_mode = false;+ root.snapshots = null;+ root.session_name = "";+ root.gpa = testing.allocator;+ root.io = testing.io;+ return root;+}++fn ledger() snapshots_mod.Snapshots {+ return .{ .gpa = testing.allocator, .io = testing.io };+}++test "the summary request says what persists and asks for names, not contents (#411)" {+ var arena_state = std.heap.ArenaAllocator.init(testing.allocator);+ defer arena_state.deinit();+ const a = arena_state.allocator();+ session_transcript.resetForTest();+ defer session_transcript.resetForTest();++ var root = noteAgent(false);+ const req = try summaryRequest(a, &root);+ // The instruction the model has always been given still LEADS the request:+ // #379 classifies the response to it, and that classification must not+ // start depending on a note that was appended after the fact.+ try testing.expect(std.mem.startsWith(u8, req, compact_instruction));+ try testing.expect(std.mem.indexOf(u8, req, "Record NAMES, not contents") != null);+ try testing.expect(std.mem.indexOf(u8, req, "restated to you in full") != null);+ // The ground truth itself belongs on the FAR side of the summary, where the+ // model cannot summarize it away. It must not be in the request.+ try testing.expect(std.mem.indexOf(u8, req, "durable state, re-derived") == null);+ // Nothing is live here, so the request cites no file at all.+ try testing.expect(std.mem.indexOf(u8, req, ".transcript.jsonl") == null);+}++test "a live transcript is named in both halves, by real path and message count (#411)" {+ const gpa = testing.allocator;+ var arena_state = std.heap.ArenaAllocator.init(gpa);+ defer arena_state.deinit();+ const a = arena_state.allocator();+ session_transcript.resetForTest();+ tool_spill.resetForTest();+ defer session_transcript.resetForTest();+ defer tool_spill.resetForTest();+ var tmp = testing.tmpDir(.{ .iterate = true });+ defer tmp.cleanup();++ var root = noteAgent(false);+ root.session_name = "n411";+ root.messages = std.json.Array.init(a);+ try root.messages.append(try textMessage(a, "user", "why did the build fail?"));+ try root.messages.append(try textMessage(a, "assistant", "the link order"));+ session_transcript.record(&root, tmp.dir, "n411");++ const req = try summaryRequest(a, &root);+ try testing.expect(std.mem.indexOf(u8, req, ".graff/sessions/n411.transcript.jsonl") != null);+ try testing.expect(std.mem.indexOf(u8, req, "(2 messages") != null);++ const note = (try durableState(a, &root, &.{})).?;+ try testing.expect(std.mem.indexOf(u8, note, ".graff/sessions/n411.transcript.jsonl") != null);+ try testing.expect(std.mem.indexOf(u8, note, "(2 messages") != null);+ try testing.expect(std.mem.indexOf(u8, note, "grep it") != null);++ // A session whose transcript is not the live one omits the line entirely,+ // rather than citing a path derived from its own name that nothing wrote.+ // With nothing else durable, that leaves no note at all.+ root.session_name = "someone-else";+ try testing.expect((try durableState(a, &root, &.{})) == null);+ try testing.expect(std.mem.indexOf(u8, try summaryRequest(a, &root), ".transcript.jsonl") == null);+}++test "the note names the files modified and the artifacts the discarded history spilled (#411)" {+ const gpa = testing.allocator;+ var arena_state = std.heap.ArenaAllocator.init(gpa);+ defer arena_state.deinit();+ const a = arena_state.allocator();+ session_transcript.resetForTest();+ tool_spill.resetForTest();+ defer session_transcript.resetForTest();+ defer tool_spill.resetForTest();+ var tmp = testing.tmpDir(.{ .iterate = true });+ defer tmp.cleanup();+ tool_spill.enable(.{ .io = testing.io, .dir = tmp.dir, .base_abs = "" });++ var snaps = ledger();+ defer snaps.deinit();+ snaps.record("src/a.zig", .absent);+ snaps.record("src/b.zig", .{ .content = "old bytes" });+ snaps.record("src/a.zig", .{ .content = "newer bytes" }); // edited twice, named once+ var root = noteAgent(false);+ root.snapshots = &snaps;++ // A marker written by the REAL spill writer, so the reader is proved against+ // the format that actually ships rather than against a copy of it here.+ const big = try a.alloc(u8, 4096);+ @memset(big, 'x');+ const spill_note: tool_spill.Note = .{ .fallback = "[truncated]", .session = "s411" };+ const marker = spill_note.text(a, big, 1024);+ try testing.expect(std.mem.indexOf(u8, marker, "tool-0.txt") != null); // the spill really happened+ var fco: std.json.ObjectMap = .empty;+ try fco.put(a, "type", .{ .string = "function_call_output" });+ try fco.put(a, "output", .{ .string = marker });+ var discarded = std.json.Array.init(a);+ try discarded.append(try textMessage(a, "user", "run the big one"));+ try discarded.append(.{ .object = fco });++ const note = (try durableState(a, &root, discarded.items)).?;+ try testing.expect(std.mem.indexOf(u8, note, "src/b.zig") != null);+ try testing.expectEqual(@as(usize, 1), std.mem.count(u8, note, "src/a.zig"));+ try testing.expect(std.mem.indexOf(u8, note, "(2; write_file") != null);+ // The bound is stated, not implied: this ledger cannot see a bash edit.+ try testing.expect(std.mem.indexOf(u8, note, "bash are not tracked") != null);+ // The handle is the artifact PATH, extracted from the marker - not the+ // marker prose, which would be re-pasting the very bytes compaction dropped.+ try testing.expect(std.mem.indexOf(u8, note, "tool-0.txt") != null);+ try testing.expect(std.mem.indexOf(u8, note, "read or grep") == null);+ // A history with no spill in it gets no handle line.+ var plain = std.json.Array.init(a);+ try plain.append(try textMessage(a, "assistant", "the FULL story bytes are at the office"));+ const no_handles = (try durableState(a, &root, plain.items)).?;+ try testing.expect(std.mem.indexOf(u8, no_handles, "spilled to disk") == null);+}++test "a subagent gets no durable-state note, whatever is hung on it (#411)" {+ const gpa = testing.allocator;+ var arena_state = std.heap.ArenaAllocator.init(gpa);+ defer arena_state.deinit();+ const a = arena_state.allocator();+ session_transcript.resetForTest();+ defer session_transcript.resetForTest();++ var snaps = ledger();+ defer snaps.deinit();+ snaps.record("src/child.zig", .absent);+ var sub = noteAgent(true);+ sub.snapshots = &snaps;+ sub.session_name = "n411";+ var discarded = std.json.Array.init(a);+ try discarded.append(try textMessage(a, "user", tool_spill.marker_head ++ " — the FULL 9" ++ tool_spill.marker_path_open ++ "/tmp/x.txt" ++ tool_spill.marker_path_close ++ " it]"));+ // A child's history is never persisted and its outputs are never spilled,+ // so every field here would be a claim about state it does not own.+ try testing.expect((try durableState(a, &sub, discarded.items)) == null);+ try testing.expectEqualStrings("BASE", try handoff(a, &sub, "BASE", null, discarded.items));+ // A /review turn shares the Agent struct but not the session's work.+ sub.sub = false;+ sub.review_mode = true;+ try testing.expect((try durableState(a, &sub, discarded.items)) == null);+}++test "the durable-state note rides the new history head, last, and an empty one changes nothing (#411)" {+ const gpa = testing.allocator;+ var arena_state = std.heap.ArenaAllocator.init(gpa);+ defer arena_state.deinit();+ const a = arena_state.allocator();+ session_transcript.resetForTest();+ defer session_transcript.resetForTest();++ var root = noteAgent(false);+ // Nothing durable to report: the handoff is byte-identical to the text a+ // plain session has always had.+ try testing.expectEqualStrings("BASE", try handoff(a, &root, "BASE", null, &.{}));++ var snaps = ledger();+ defer snaps.deinit();+ snaps.record("src/z.zig", .absent);+ root.snapshots = &snaps;+ const out = try handoff(a, &root, "BASE", "[standing state: goal]", &.{});+ try testing.expect(std.mem.startsWith(u8, out, "BASE\n\n[standing state: goal]"));+ try testing.expect(std.mem.indexOf(u8, out, "src/z.zig") != null);+ // Ground truth is the LAST thing read before the model continues.+ const standing_at = std.mem.indexOf(u8, out, "[standing state: goal]").?;+ try testing.expect(std.mem.indexOf(u8, out, "durable state, re-derived").? > standing_at);+}diff --git a/src/snapshots.zig b/src/snapshots.zigindex 1b5071a1..c9714213 100644--- a/src/snapshots.zig+++ b/src/snapshots.zig@@ -113,6 +113,30 @@ pub const Snapshots = struct {return out;}+ /// #411: the distinct paths this session has actually modified, in+ /// first-modified order, duped into `arena`. This ledger is the only+ /// EXACT answer the harness has to "what did I change this session" —+ /// which is also its bound: it records write_file/edit_file/imagegen, so+ /// an edit made through bash is not in it, and the note that prints these+ /// says so rather than implying the list is the whole diff. A `/rewind`+ /// has already dropped the snapshots it undid, so a rewound file+ /// correctly stops being listed.+ pub fn modifiedPaths(self: *Snapshots, arena: Allocator) []const []const u8 {+ self.mutex.lockUncancelable(self.io);+ defer self.mutex.unlock(self.io);+ var out: std.ArrayList([]const u8) = .empty;+ for (self.list.items) |snap| {+ var seen = false;+ for (out.items) |p| if (std.mem.eql(u8, p, snap.path)) {+ seen = true;+ };+ if (seen) continue;+ const owned = arena.dupe(u8, snap.path) catch return out.items;+ out.append(arena, owned) catch return out.items;+ }+ return out.items;+ }+pub fn deinit(self: *Snapshots) void {for (self.list.items) |s| {self.gpa.free(s.path);diff --git a/src/tool_spill.zig b/src/tool_spill.zigindex c98aa308..692b1c46 100644--- a/src/tool_spill.zig+++ b/src/tool_spill.zig@@ -108,6 +108,13 @@ pub fn safeName(session: []const u8) bool {return !std.mem.eql(u8, session, ".") and !std.mem.eql(u8, session, "..");}+/// The three fences #411's post-compaction note reads a spilled artifact's path+/// back out of a marker with. `Note.text` below is BUILT from them, so the+/// reader cannot drift from the writer: change the wording and both move.+pub const marker_head = "[tool output truncated at this model's per-result cap";+pub const marker_path_open = " bytes are at ";+pub const marker_path_close = "; read or grep";+/// What replaces the elided bytes. `session` empty (a subagent, an unwired/// process) means plain truncation with `fallback`.pub const Note = struct {@@ -119,7 +126,7 @@ pub const Note = struct {/// than growing an output the cap just shrank.pub fn text(self: Note, arena: Allocator, full: []const u8, cap: usize) []const u8 {const path = spill(arena, self.session, full) orelse return self.fallback;- const marker = std.fmt.allocPrint(arena, "[tool output truncated at this model's per-result cap — the FULL {d} bytes are at {s}; read or grep that file for the slice you need instead of re-running the tool (#409)]", .{ full.len, path }) catch return self.fallback;+ const marker = std.fmt.allocPrint(arena, marker_head ++ " — the FULL {d}" ++ marker_path_open ++ "{s}" ++ marker_path_close ++ " that file for the slice you need instead of re-running the tool (#409)]", .{ full.len, path }) catch return self.fallback;return if (marker.len + 1 > cap) self.fallback else marker;}};