Skip to content

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

Python

test(compact): prove compact() actually calls pinChildTask (G2)

Rach Pradhana7fb62549824
diff --git a/src/agent_compact.zig b/src/agent_compact.zig
index 434ca4d8..83db7667 100644
--- a/src/agent_compact.zig
+++ b/src/agent_compact.zig
@@ -80,12 +80,25 @@ pub fn summaryResponseComplete(self: *Agent, root: std.json.ObjectMap) bool {
};
}
+/// The pure prelude of compact(): the early return for an already-empty
+/// history and the one-time task-prompt pin for a sub-agent (#B3). Split out
+/// of compact() because everything after this point needs a live Io and a
+/// network summarization request, so a plain unit test can never reach
+/// compact() itself - it can drive this slice directly instead, which is the
+/// actual proof that compact() is wired to call pinChildTask: delete the call
+/// below and the compactPrelude test goes red, whereas pinChildTask's own
+/// isolated tests do not notice.
+pub fn compactPrelude(self: *Agent) bool {
+ if (self.messages.items.len == 0) return false;
+ pinChildTask(self);
+ return true;
+}
+
pub fn compact(self: *Agent) anyerror!usize {
- if (self.messages.items.len == 0) {
+ if (!compactPrelude(self)) {
if (!main_mod.json_mode) try self.say("nothing to compact\n", .{});
return 0;
}
- pinChildTask(self);
self.last_request_context_overflow = false;
if (!main_mod.json_mode) try self.say("[compacting ~{d} tokens…]\n", .{self.effectiveContextTokens()});
// #163: reclaim room BEFORE the summarization request so it fits under the
diff --git a/src/agent_compact_test_support.zig b/src/agent_compact_test_support.zig
index 8f17c6b1..dd32e614 100644
--- a/src/agent_compact_test_support.zig
+++ b/src/agent_compact_test_support.zig
@@ -1,11 +1,18 @@
//! Shared builders for agent_compact_test.zig's subagent-task-pin tests
-//! (#B3). Kept out of agent_compact_test.zig to stay under its 600-line cap;
-//! this file has no `test` blocks of its own so it needs no main.zig hook.
+//! (#B3), plus a compact()-wiring test of its own (G2). Kept out of
+//! agent_compact_test.zig to stay under its 600-line cap. It carries a
+//! `test` block despite not being referenced from src/main.zig's test{}
+//! block: any file reached through a *used* @import (agent_compact_test.zig
+//! calls the builders below) is fully analyzed, and `zig test` collects test
+//! declarations from every analyzed file, not just ones a main.zig `_ = x;`
+//! line names directly - verified empirically by adding a deliberately
+//! failing probe test here and confirming `zig build test` reported it.
const std = @import("std");
const Value = std.json.Value;
const Agent = @import("agent.zig").Agent;
const textMessage = @import("messages.zig").textMessage;
+const compact = @import("agent_compact.zig");
/// A subagent-or-root Agent with just enough fields set for handoffMessage /
/// pinChildTask to run without touching uninitialized memory.
@@ -39,3 +46,24 @@ pub fn paddedSubHistory(a: std.mem.Allocator, n: usize, pad: []const u8) !std.js
for (0..n) |_| try out.appendSlice(&.{ try textMessage(a, "assistant", pad), try toolResultMsg(a, "result") });
return out;
}
+
+// G2: compact() has exactly one call to pinChildTask, made through
+// compactPrelude - the extracted pure prelude (early-empty-return + pin)
+// that runs before compact() needs a live Io for its summarization request.
+// A plain unit test cannot reach compact() itself, but it CAN drive
+// compactPrelude directly, and that is a real proof of the wiring: delete
+// the `pinChildTask(self);` line inside compactPrelude and this test goes
+// red, whereas pinChildTask's own isolated tests above do not notice.
+test "compactPrelude actually pins a subagent's task - the wiring compact() depends on (G2)" {
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+ const a = arena_state.allocator();
+ var agent = subAgent(a, true);
+ agent.messages = try msg1(a, "user", "TASK X");
+ try std.testing.expect(compact.compactPrelude(&agent));
+ try std.testing.expectEqualStrings("TASK X", agent.task_prompt.?);
+
+ var empty = subAgent(a, true);
+ empty.messages = std.json.Array.init(a);
+ try std.testing.expect(!compact.compactPrelude(&empty));
+}