graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
Merge branch 'feat/391-pre-compaction-notes' into integration/2026-08-06-batch
Rach Pradhan4d14719030d4
diff --cc src/agent_compact.zigindex 65c06cdf,8ef532eb..6e5d3e9a--- a/src/agent_compact.zig+++ b/src/agent_compact.zig@@@ -10,8 -10,10 +10,9 @@@ const context_tokens = @import("contextconst 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 handoff_note = @import("compact_handoff_note.zig"); // #411: both halves of "what survives a compaction"+ const compact_note_glue = @import("compact_note_glue.zig"); // #391const messages_mod = @import("messages.zig");const textMessage = messages_mod.textMessage;diff --cc src/prompts.zigindex 8fa4bc78,52ea3850..23340505--- a/src/prompts.zig+++ b/src/prompts.zig@@@ -157,12 -153,19 +158,25 @@@ pub fn baseForSession(arena: Allocator/// unit test — the same arming discipline playbook.g_root_inject uses.var g_transcript_note: []const u8 = "";+/// #445: has THIS process's root session compacted at least once? False at+/// startup, set once by noteSessionCompacted, never lowered again. Public only+/// so a test can save and restore it, exactly like playbook.g_root_inject —+/// noteSessionCompacted is the sole production writer.+pub var g_session_compacted: bool = false;++ /// #391: the session whose pre-compaction notes ride the ROOT's prompt.+ /// Empty for every non-root agent and every unit test — the arming discipline+ /// g_transcript_note and playbook.g_root_inject both use, and what keeps a+ /// bare `Agent` with an `undefined` Io out of the filesystem.+ var g_note_session: []const u8 = "";++ /// Armed by setRootSystemPrompts, and re-armed by the note writer itself so a+ /// `/save <name>` mid-session cannot leave the injection reading a store the+ /// writer has stopped writing to.+ pub fn armCompactNotes(session_name: []const u8) void {+ g_note_session = session_name;+ }+/// One line naming the durable transcript. Deliberately NOT described as/// JSONL: `.graff/sessions/<name>.session.json` is a single JSON object (the/// JSONL files are the `.graff/traces` event streams the paragraph above@@@ -253,84 -261,17 +272,95 @@@ pub fn setRootSystemPrompts(agent: *Age// #410: only the root has a durable session, and only a session file this// process can still open is worth a line of context — with the native file// and shell tools removed (#330) the path is unreadable from here.- armSessionTranscript(arena, agent.session_name, detectCaps());+ // #445: and a session that has not compacted yet gets nothing at all. A+ // RESUMED session starts false too: the earlier compactions belong to a+ // dead process, and what that process left in the file is exactly the+ // context this one loaded, so the line would again describe what is here.+ armSessionTranscript(arena, agent.session_name, detectCaps(), g_session_compacted);+ armCompactNotes(agent.session_name); // #391: same session, same one-time armingreturn setSystemPrompts(agent, base, arena);}/// #410's arming step, split out so the funnel is testable without the/// filesystem read setRootSystemPrompts also performs (playbook.composeRoot).-/// An empty `session_name`, or a session whose file this process can no longer-/// open, both arm to "" — the line is only worth context when it is actionable.-pub fn armSessionTranscript(arena: Allocator, session_name: []const u8, caps: Caps) void {- g_transcript_note = if (caps.local_tools) sessionTranscriptNote(arena, session_name) else "";+/// An empty `session_name`, a session whose file this process cannot open, and+/// a session that has not compacted yet all arm to "" — the line is only worth+/// context when it is both actionable and not already redundant.+pub fn armSessionTranscript(arena: Allocator, session_name: []const u8, caps: Caps, compacted: bool) void {+ g_transcript_note = if (transcriptLineWanted(caps, compacted)) sessionTranscriptNote(arena, session_name) else "";+}++/// #445: the transcript line's gate, in one predicate, ANDing two conditions+/// that are different in kind and neither of which is sufficient alone:+///+/// - `caps.local_tools` is a CAPABILITY, read from the same catalog every+/// other segment gate is read from: with #330's native file and shell+/// tools removed, nothing in the session can open the path the line names.+/// - `compacted` is session STATE, and it is why this predicate exists.+/// Before the first compaction the live context is a SUPERSET of the+/// transcript file — the line can only point the model at wording it can+/// already see, and compaction rewrites the file in place anyway, so its+/// marginal value there is ~zero. Live A/B evals put the #421/#410 prompt+/// additions at +960 chars ≈ +240 input tokens on every single call at+/// full capability, which the overwhelming majority of sessions (the ones+/// that never compact at all) were paying for nothing. After a compaction+/// the file holds wording the window no longer does: the case #410 is for.+///+/// Deliberately NOT a `Caps` field. `Caps` is what the tool catalog reports,+/// and `detectCaps()` is settled before startup.buildSystemPrompt runs; this+/// flips mid-session, by definition after the first request. Folding it in+/// would make both of those statements false, and would put a non-segment+/// condition into the predicate `composeBase`/`full()` short-circuit on.+pub fn transcriptLineWanted(caps: Caps, compacted: bool) bool {+ return caps.has(.local_tools) and compacted;+}++/// #445: the compaction boundary — the one moment the line starts earning its+/// tokens, and the one moment mutating the system prompt is free. Compaction+/// has already replaced the history sitting behind any cached KV prefix, so+/// re-composing here adds no SECOND invalidation point; deferring to the next+/// turn boundary instead would throw away a live cache for the same text.+///+/// Root-only (a subagent has no durable session file of its own, and must not+/// flip the flag for the root) and idempotent: a session that compacts ten+/// times re-derives its prompt once. Best-effort, exactly like+/// playbook_glue.refreshRoot — a failed re-compose leaves the previous, still+/// valid prompt in place and the next mutation through the funnel picks it up.+pub fn noteSessionCompacted(agent: *Agent, arena: Allocator) void {+ if (agent.sub or g_session_compacted) return;+ g_session_compacted = true;+ armSessionTranscript(arena, agent.session_name, detectCaps(), true);+ if (agent.sys_base.len == 0) return; // never went through the funnel: nothing to re-derive from+ setSystemPrompts(agent, agent.sys_base, arena) catch {};+}++/// #445's inverse boundary, and it is NOT optional: one process can host+/// several root conversations. `/new` mints a fresh `session_name` and `/clear`+/// empties the history and saves immediately, so in both cases the durable file+/// again holds no more than the live window — the exact state the line is not+/// worth its tokens in. Without this, one compaction armed the line for the+/// remaining life of the process and a user who compacted, then hit `/new`,+/// paid for it forever pointing at a file with nothing to recover.+///+/// Same idiom as noteSessionCompacted: root-only, a no-op when already false,+/// and best-effort. Call it AFTER any `session_name` reassignment, so the+/// re-arm reads the conversation that actually exists now.+pub fn resetSessionCompacted(agent: *Agent, arena: Allocator) void {- if (agent.sub or !g_session_compacted) return;++ if (agent.sub) return;++ // #391 + #445 integration: BOTH armings key on session_name, and the three++ // doors that reach here (/new, /clear, /resume) can repoint it. The note++ // store has to follow even when this session never compacted — otherwise a++ // `/new` after a note was written leaves g_note_session on the PREVIOUS++ // name and injects the old conversation's notes into the new one, which is++ // the same stale-identity bug this function exists to prevent for the++ // transcript line, one store over.++ const note_moved = !std.mem.eql(u8, g_note_session, agent.session_name);++ armCompactNotes(agent.session_name);++ if (!g_session_compacted and !note_moved) return;+ g_session_compacted = false;+ armSessionTranscript(arena, agent.session_name, detectCaps(), false);+ if (agent.sys_base.len == 0) return;+ setSystemPrompts(agent, agent.sys_base, arena) catch {};}pub const sub_system_prompt =