Skip to content

graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs

Python

Compaction replies stay off every frontend; checkpoints keep the user's task

justrachb6e94ab6bed6
diff --git a/src/additional_tests.zig b/src/additional_tests.zig
index ec60ec8e..77bb5645 100644
--- a/src/additional_tests.zig
+++ b/src/additional_tests.zig
@@ -8,6 +8,7 @@ test {
_ = @import("agent_request_body_responses.zig");
_ = @import("agent_server_compact_tests.zig");
_ = @import("agent_server_compact_route_tests.zig");
+ _ = @import("compaction_quiet_delivery_tests.zig");
_ = @import("provider_codegraff_tests.zig");
_ = @import("agent_responses.zig");
_ = @import("session_branch.zig");
diff --git a/src/agent_empty_completion.zig b/src/agent_empty_completion.zig
index f15233e4..e7a7e01a 100644
--- a/src/agent_empty_completion.zig
+++ b/src/agent_empty_completion.zig
@@ -162,6 +162,30 @@ pub fn handle(self: *Agent, final_text: []const u8, hist_len: usize) !bool {
pub const PendingWork = struct {
nudged: bool = false,
idle: @import("run_idle.zig").Hold = .{},
+ /// The checklist as this turn found it (#1582). An informational request
+ /// that leaves an earlier task's checklist untouched answered a new
+ /// question; that checklist is kept, not resumed.
+ checklist_at_start: ?u64 = null,
+
+ pub fn begin(self: *const Agent) PendingWork {
+ return .{ .checklist_at_start = checklistFingerprint(self) };
+ }
+
+ fn checklistFingerprint(self: *const Agent) u64 {
+ var h = std.hash.Wyhash.init(0x1582);
+ for (self.todos.items) |t| {
+ h.update(t.content);
+ h.update(t.status);
+ h.update(std.mem.asBytes(&t.epoch));
+ }
+ return h.final();
+ }
+
+ fn carriedOver(state: *const PendingWork, self: *const Agent) bool {
+ const start = state.checklist_at_start orelse return false;
+ return start == checklistFingerprint(self) and
+ @import("task_intent.zig").current(self) == .informational;
+ }
pub const note = "Open-work reconciliation: your plain final reply would end root execution, but the current checklist is unfinished. " ++
"If the user still wants this task done, continue actionable independent work now; collect required background results with agent_output/bash_output and wait_ms>0 when needed, rather than polling or promising future work. " ++
@@ -187,7 +211,7 @@ pub const PendingWork = struct {
/// provider ignores the reminder or the remaining budget cannot buy it.
pub fn finish(state: *PendingWork, self: *Agent, text: []const u8) !?[]const u8 {
if (!self.sub and Agent.esc_cancel.load(.acquire)) return error.Interrupted;
- const count = open(self);
+ const count = if (state.carriedOver(self)) 0 else open(self);
// The reminder first, so independent work runs beside the background
// work; the wait applies once the model stops again.
if (count > 0 and !state.nudged and canRequest(self)) {
@@ -423,3 +447,25 @@ test "exact reply bypasses only fake done while mixed requests and empty replies
try std.testing.expectEqual(@as(u8, if (empty) 1 else 0), self.empty_completion_retries);
}
}
+
+test "#1582 an informational question does not resume an earlier task's untouched checklist" {
+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena.deinit();
+ var self = pendingFixture(arena.allocator());
+ try self.todos.append(self.arena, .{ .content = "ship the parser fix", .status = "in_progress" });
+ try self.messages.append(try messages.textMessage(self.arena, "user", "Explain how the cache works"));
+ var state = PendingWork.begin(&self);
+ try std.testing.expectEqualStrings("It is an LRU.", (try state.finish(&self, "It is an LRU.")).?);
+ try std.testing.expectEqual(@as(usize, 1), self.messages.items.len); // no reminder
+ try std.testing.expectEqualStrings("in_progress", self.todos.items[0].status); // kept, not resumed
+
+ // An informational turn that changed the checklist owns it.
+ state = PendingWork.begin(&self);
+ self.todos.items[0].status = "pending";
+ try std.testing.expect((try state.finish(&self, "Explained.")) == null);
+
+ // A work request still reconciles the same untouched checklist.
+ try self.messages.append(try messages.textMessage(self.arena, "user", "Keep going and fix it"));
+ state = PendingWork.begin(&self);
+ try std.testing.expect((try state.finish(&self, "Stopping here.")) == null);
+}
diff --git a/src/agent_model_loop.zig b/src/agent_model_loop.zig
index 0df5f192..d0f287fd 100644
--- a/src/agent_model_loop.zig
+++ b/src/agent_model_loop.zig
@@ -59,7 +59,7 @@ pub fn delta(kind: Kind, value: Value) []const u8 {
}
pub fn emitText(agent: *Agent, text: []const u8) void {
- if (text.len == 0) return;
+ if (text.len == 0 or agent.compaction_request) return;
if (agent.out == null) {
if (main_mod.unattended and !main_mod.json_mode and !agent.sub) {
if (main_mod.g_out) |w| {
diff --git a/src/agent_stream.zig b/src/agent_stream.zig
index ff5a351f..0e221704 100644
--- a/src/agent_stream.zig
+++ b/src/agent_stream.zig
@@ -508,6 +508,8 @@ pub fn printDelta(self: *Agent, raw_line: []const u8) void {
// Reasoning/thinking deltas: deepseek streams reasoning_content, anthropic
// a thinking_delta, codex a summary delta. Presentation is the sink's call:
// the wire's `reasoning` event, or the live "Thinking" block / spinner.
+ // A compaction request's reply is bookkeeping on every frontend (#1582).
+ if (self.compaction_request) return;
const sink = engine_sink.forAgent(self);
const reasoning = reasoningDelta(self.provider.kind, obj);
if (reasoning.len != 0 or text.len != 0) self.traceFirstToken();
diff --git a/src/agent_turn.zig b/src/agent_turn.zig
index 96bb41c6..81e09337 100644
--- a/src/agent_turn.zig
+++ b/src/agent_turn.zig
@@ -14,7 +14,7 @@ pub fn run(self: *Agent, root_turn_prepared: *std.atomic.Value(bool)) anyerror![
defer @import("agent_async_tools.zig").reset(self);
if (!self.sub) @import("peer_idle.zig").noteTurnStart();
defer if (!self.sub) @import("peer_idle.zig").noteTurnEnd();
- var pending_work: empty_completion.PendingWork = .{};
+ var pending_work = empty_completion.PendingWork.begin(self);
var bounced_answer: empty_completion.BounceAnswer = .{};
self.completed = null;
self.yielded = null;
diff --git a/src/compaction_quiet_delivery_tests.zig b/src/compaction_quiet_delivery_tests.zig
new file mode 100644
index 00000000..8e18373b
--- /dev/null
+++ b/src/compaction_quiet_delivery_tests.zig
@@ -0,0 +1,80 @@
+//! A compaction request runs tools-off and its reply is discarded once the
+//! compaction item is installed. That reply must never reach a frontend as an
+//! answer: not the terminal, not the --json wire, not ACP (#1582).
+
+const std = @import("std");
+const Io = std.Io;
+const main_mod = @import("main.zig");
+const Agent = @import("agent.zig").Agent;
+const engine_sink = @import("engine_sink.zig");
+const deinitMarkdown = @import("agent_render.zig").deinitMarkdown;
+
+const text_line = "data: {\"type\":\"response.output_text.delta\",\"delta\":\"I do not have terminal tools.\"}";
+const reasoning_line = "data: {\"type\":\"response.reasoning_summary_text.delta\",\"delta\":\"maintenance thoughts\"}";
+
+fn wireAgent(w: *Io.Writer) Agent {
+ @import("line_repl_disclosure.zig").reset(std.testing.io);
+ return .{
+ .gpa = std.testing.allocator,
+ .arena = std.testing.allocator,
+ .io = std.testing.io,
+ .client = undefined,
+ .provider = .{ .id = "test", .kind = .responses, .auth = .bearer, .url = "", .api_key = "", .model = "test", .context = 0 },
+ .messages = undefined,
+ .sub = false,
+ .label = "test",
+ .out = w,
+ };
+}
+
+test "a compaction reply never reaches the --json wire that ACP translates" {
+ const saved_json = main_mod.json_mode;
+ defer main_mod.json_mode = saved_json;
+ main_mod.json_mode = true;
+ var aw: Io.Writer.Allocating = .init(std.testing.allocator);
+ defer aw.deinit();
+ var a = wireAgent(&aw.writer);
+ defer deinitMarkdown(&a);
+ a.stream_quiet = true;
+ a.compaction_request = true;
+ a.printDelta(reasoning_line);
+ a.printDelta(text_line);
+ try std.testing.expectEqualStrings("", aw.written());
+ try std.testing.expect(!a.streamed_text);
+ @import("agent_model_loop.zig").emitText(&a, "stopped: model loop");
+ try std.testing.expectEqualStrings("", aw.written());
+
+ // The ordinary request after it still streams.
+ a.stream_quiet = false;
+ a.compaction_request = false;
+ a.printDelta(reasoning_line);
+ a.printDelta(text_line);
+ try std.testing.expect(std.mem.indexOf(u8, aw.written(), "\"type\":\"reasoning\"") != null);
+ try std.testing.expect(std.mem.indexOf(u8, aw.written(), "I do not have terminal tools.") != null);
+ try std.testing.expect(a.streamed_text);
+}
+
+fn record(ctx: *anyopaque, ev: engine_sink.Stamped) void {
+ const count: *usize = @ptrCast(@alignCast(ctx));
+ switch (ev.event) {
+ .reasoning_delta, .text_delta => count.* += 1,
+ else => {},
+ }
+}
+
+test "an injected frontend sink sees no compaction prose either" {
+ var aw: Io.Writer.Allocating = .init(std.testing.allocator);
+ defer aw.deinit();
+ var a = wireAgent(&aw.writer);
+ defer deinitMarkdown(&a);
+ var seen: usize = 0;
+ const vt: engine_sink.VTable = .{ .emit = record, .durable = false };
+ a.sink = .{ .ctx = &seen, .vt = &vt };
+ a.compaction_request = true;
+ a.printDelta(reasoning_line);
+ a.printDelta(text_line);
+ try std.testing.expectEqual(@as(usize, 0), seen);
+ a.compaction_request = false;
+ a.printDelta(text_line);
+ try std.testing.expectEqual(@as(usize, 1), seen);
+}
diff --git a/src/task_intent.zig b/src/task_intent.zig
index 6bee0c9e..b359cd99 100644
--- a/src/task_intent.zig
+++ b/src/task_intent.zig
@@ -38,9 +38,20 @@ fn oneOf(word: []const u8, choices: []const []const u8) bool {
/// not a natural-language permission boundary or an enforced tool allowlist.
pub fn classify(text: []const u8) Intent {
var informational = false;
+ // "file an issue", "open a GitHub issue", "report it": publishing is work
+ // to do, so an explanation that also asks for one stays general (#1581).
+ var publish_window: u8 = 0;
+ var after_report = false;
var words = std.mem.tokenizeAny(u8, text, " \t\r\n,;:!?()[]{}\"`");
while (words.next()) |raw| {
const word = std.mem.trim(u8, raw, ".");
+ if (after_report and oneOf(word, &.{ "it", "this", "that", "them" })) return .general;
+ if (publish_window > 0) {
+ if (oneOf(word, &.{ "issue", "issues", "ticket", "bug", "pr" })) return .general;
+ publish_window -= 1;
+ }
+ after_report = std.ascii.eqlIgnoreCase(word, "report");
+ if (oneOf(word, &.{ "file", "open", "raise", "post", "submit", "publish", "report" })) publish_window = 3;
if (oneOf(word, &.{ "fix", "edit", "implement", "add", "remove", "delete", "rename", "refactor", "write", "create", "update", "patch", "build", "test", "lint", "run", "merge", "push", "deploy", "commit" })) return .general;
if (oneOf(word, &.{ "summarize", "summarise", "summary", "explain", "describe", "overview", "map", "research", "investigate", "lookup" })) informational = true;
if (std.ascii.eqlIgnoreCase(word, "read-only")) informational = true;
@@ -54,7 +65,25 @@ pub fn current(self: *const Agent) Intent {
pub const read_nudge = "You named a source file but have not inspected it. Read the named path if its contents are needed, then answer the informational request. Do not edit it merely to satisfy a completion check.";
-pub const checkpoint = "Summary scope checkpoint: review the map and targeted evidence already gathered in this turn. A broad batch of reads also counts as exploration; do not follow it with a shell loop reading every remaining file. For repeated scaffolding, explain the sampled pattern and its limits. Can you now explain the repository's purpose, architecture, and important constraints? If so, answer concisely now. Otherwise read only the specific missing evidence. Do not start implementation, a test suite, or a separate citation pass solely to finish a summary. Respect any newer user request that changes the scope.";
+/// Internal reminders arrive in the user role, so each one says it is not a
+/// new request and points back at the human's own (#1581).
+const reminder_prefix = "Internal reminder, not a new user request. ";
+
+pub const checkpoint = reminder_prefix ++ "Summary scope checkpoint: review the map and targeted evidence already gathered in this turn. A broad batch of reads also counts as exploration; do not follow it with a shell loop reading every remaining file. For repeated scaffolding, explain the sampled pattern and its limits. Can you now explain the repository's purpose, architecture, and important constraints? If so, answer concisely now. Otherwise read only the specific missing evidence. Do not start implementation, a test suite, or a separate citation pass solely to finish a summary. Respect any newer user request that changes the scope.";
+
+/// Every other informational request: bound the exploration without
+/// replacing the question the user asked with a repository overview.
+pub const scope_checkpoint = reminder_prefix ++ "Scope checkpoint: review the evidence already gathered in this turn. Can you now answer the user's latest request, including every deliverable it named? If so, finish it now. Otherwise read only the specific missing evidence. Do not start a repository overview, a test suite, or a separate citation pass unless the user asked for one.";
+
+/// Only a request for a summary or overview gets the repository checkpoint.
+fn summaryRequest(text: []const u8) bool {
+ var words = std.mem.tokenizeAny(u8, text, " \t\r\n,;:!?()[]{}\"`");
+ while (words.next()) |raw| {
+ const word = std.mem.trim(u8, raw, ".");
+ if (oneOf(word, &.{ "summarize", "summarise", "summary", "overview", "map" })) return true;
+ }
+ return false;
+}
pub const State = struct {
nudged: bool = false,
@@ -69,7 +98,8 @@ pub const State = struct {
try state.review_progress.beforeRequest(self);
if (state.nudged or self.sub or current(self) != .informational) return;
if (self.model_calls_this_turn < 4 and self.tool_calls_this_turn < 6) return;
- var note = try @import("named_work.zig").userNudge(self.arena, self.provider.kind, checkpoint);
+ const text = if (summaryRequest(@import("messages.zig").latestUserText(self.messages.items))) checkpoint else scope_checkpoint;
+ var note = try @import("named_work.zig").userNudge(self.arena, self.provider.kind, text);
try note.object.put(self.arena, @import("session_wake.zig").origin_key, .{ .string = "notification" });
try self.messages.append(note);
state.nudged = true;
@@ -139,3 +169,38 @@ test "summary checkpoint is once, preserves the human request, and respects a ne
try std.testing.expect(state.nudged);
try std.testing.expectEqual(@as(usize, 5), agent.messages.items.len);
}
+
+test "#1581 an explanation that also asks to publish stays actionable" {
+ for ([_][]const u8{
+ "Explain why the build fails and file an issue",
+ "Investigate the crash and open a GitHub issue for it",
+ "Explain the error and report it",
+ "Research the regression, then raise a ticket",
+ }) |text| try std.testing.expectEqual(Intent.general, classify(text));
+ try std.testing.expectEqual(Intent.informational, classify("Explain the bug report format"));
+ try std.testing.expectEqual(Intent.informational, classify("Describe the file layout"));
+}
+
+test "#1581 only a summary request gets the repository checkpoint" {
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+ const a = arena_state.allocator();
+ const messages = @import("messages.zig");
+ var agent: Agent = undefined;
+ agent.arena = a;
+ agent.messages = .init(a);
+ agent.sub = false;
+ agent.review_mode = false;
+ agent.tracer = null;
+ agent.provider.kind = .openai;
+ agent.model_calls_this_turn = 4;
+ agent.tool_calls_this_turn = 0;
+ try agent.messages.append(try messages.textMessage(a, "user", "Investigate why login fails"));
+ var state = State.begin(&agent);
+ try state.beforeRequest(&agent);
+ const sent = try std.json.Stringify.valueAlloc(a, agent.messages.items[1], .{});
+ try std.testing.expect(std.mem.indexOf(u8, sent, "Internal reminder, not a new user request.") != null);
+ try std.testing.expect(std.mem.indexOf(u8, sent, "every deliverable it named") != null);
+ try std.testing.expect(std.mem.indexOf(u8, sent, "repository's purpose") == null);
+ try std.testing.expectEqualStrings("Investigate why login fails", messages.latestUserText(agent.messages.items));
+}