Skip to content

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

Python

fix(loop): stop pasting the same checklist into every continuation turn

Rach Pradhand9d0da74d100
diff --git a/src/goal_flow.zig b/src/goal_flow.zig
index 5c2ca46e..2807830e 100644
--- a/src/goal_flow.zig
+++ b/src/goal_flow.zig
@@ -124,6 +124,49 @@ pub fn loopTurnDecision(root: *Agent, iters_left: u32) repl_glue.ContinuationDec
return repl_glue.continuationDecision(gstatus, work_done, model_stopped, iters_left);
}
+/// The steering appended to each autonomous /loop continuation turn (the
+/// continuation_steering_item analog): keep working the checklist, verify, and
+/// stop only when done or blocked - not a per-turn user note. Moved here from
+/// repl_glue.zig, which is at the 600-line cap.
+/// `paste_list` is the diff-gate's verdict (goal_state.steeringGate over the
+/// render). It used to be unconditional, so a 25-iteration /loop wrote up to 25
+/// near-identical checklist copies into root.messages - autosaved, and fed
+/// verbatim into the next compaction's summary input. That is the exact
+/// re-pasting this branch removed from the goal note, left behind in its one
+/// remaining every-turn paster (#318). When the list has not changed the model
+/// is still pointed at it, just not handed another copy; the harness restates
+/// the full list on the first continuation after a compaction, where the old
+/// copies died with the old history.
+pub fn continuationSteeringNote(arena: Allocator, todos_render: []const u8, paste_list: bool) ![]const u8 {
+ if (todos_render.len == 0)
+ return "[continuing autonomously (/loop): make the next concrete step toward the goal, then verify it. Do not ask for confirmation between routine steps. Stop only when the work is complete or you hit a blocker that needs the user.]";
+ if (!paste_list)
+ return "[continuing autonomously (/loop): keep working the current checklist (unchanged, see your latest todo_write result, or call todo_read) - do the next incomplete item, mark it in_progress then completed, and verify. Do not ask for confirmation between routine steps. Stop only when every item is done or you are blocked.]";
+ return std.fmt.allocPrint(arena, "[continuing autonomously (/loop): keep working the checklist below — do the next incomplete item, mark it in_progress then completed, and verify. Do not ask for confirmation between routine steps. Stop only when every item is done or you are blocked.\n\nChecklist so far:\n{s}]", .{todos_render});
+}
+
+/// Loop-LOCAL diff-gate state for the checklist copy in the /loop continuation
+/// prompt (#318). Deliberately not an Agent field and never persisted: it
+/// describes what THIS run has already handed the model, so a fresh run, a
+/// user steer, a stop and a compaction all reset it.
+pub const LoopListGate = struct {
+ fp: u64 = 0,
+ age: u32 = 0,
+
+ pub fn reset(self: *LoopListGate) void {
+ self.* = .{};
+ }
+
+ /// This continuation turn's steering note, pasting the render only when the
+ /// model has not just been handed the same list.
+ pub fn note(self: *LoopListGate, arena: Allocator, todos_render: []const u8) ![]const u8 {
+ const gate = goal_state.steeringGate(todos_render, self.fp, self.age, goal_state.refresh_turns);
+ self.fp = gate.fp;
+ self.age = gate.age;
+ return continuationSteeringNote(arena, todos_render, gate.inject);
+ }
+};
+
/// The goal-side effect of a /loop run that stopped as `accepted`: the loop
/// drove the objective to done, so it retires. Returns true when it flipped.
/// A standing --goal is the user's policy for the whole session and outlives
@@ -383,3 +426,51 @@ test "a compaction does not move the goal note's diff-gate fingerprint (#318)" {
try std.testing.expect(std.mem.indexOf(u8, n, "REPLACES") != null);
}
}
+
+test "continuationSteeringNote: checklist, generic, and the suppressed variant (#226/#318)" {
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+ const ar = arena_state.allocator();
+
+ const empty = try continuationSteeringNote(ar, "", true);
+ try std.testing.expect(std.mem.indexOf(u8, empty, "Checklist so far") == null);
+ try std.testing.expect(std.mem.indexOf(u8, empty, "continuing autonomously") != null);
+ const withlist = try continuationSteeringNote(ar, "[ ] add test", true);
+ try std.testing.expect(std.mem.indexOf(u8, withlist, "Checklist so far:\n[ ] add test") != null);
+
+ // Suppressed: the model is still told to work the checklist and where to
+ // find it, but gets no second copy of a list it has not changed.
+ const quiet = try continuationSteeringNote(ar, "[ ] add test", false);
+ try std.testing.expect(std.mem.indexOf(u8, quiet, "add test") == null);
+ try std.testing.expect(std.mem.indexOf(u8, quiet, "Checklist so far") == null);
+ try std.testing.expect(std.mem.indexOf(u8, quiet, "keep working the current checklist (unchanged") != null);
+ try std.testing.expect(std.mem.indexOf(u8, quiet, "todo_read") != null);
+}
+
+test "LoopListGate pastes the checklist once per change, not once per turn (#318)" {
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+ const ar = arena_state.allocator();
+ var gate: LoopListGate = .{};
+
+ // Turn 1 of a run carries the list; the next turns do not, so a 25-turn
+ // /loop no longer writes 25 near-identical copies into root.messages (all
+ // autosaved, and all fed verbatim into the next compaction's summary).
+ try std.testing.expect(std.mem.indexOf(u8, try gate.note(ar, "[ ] a\n[ ] b"), "[ ] a") != null);
+ var i: usize = 0;
+ while (i < 3) : (i += 1)
+ try std.testing.expect(std.mem.indexOf(u8, try gate.note(ar, "[ ] a\n[ ] b"), "[ ] a") == null);
+ // A real change to the list is carried immediately.
+ try std.testing.expect(std.mem.indexOf(u8, try gate.note(ar, "[x] a\n[ ] b"), "[x] a") != null);
+ // An unchanged list is re-stated after the refresh interval, in case it
+ // aged out of the model's effective attention.
+ i = 0;
+ var restated = false;
+ while (i < goal_state.refresh_turns) : (i += 1)
+ restated = restated or std.mem.indexOf(u8, try gate.note(ar, "[x] a\n[ ] b"), "[x] a") != null;
+ try std.testing.expect(restated);
+ // reset() is what a fresh run, a user steer, a stop and a compaction do:
+ // the very next continuation carries the list in full again.
+ gate.reset();
+ try std.testing.expect(std.mem.indexOf(u8, try gate.note(ar, "[x] a\n[ ] b"), "[x] a") != null);
+}
diff --git a/src/mainloop.zig b/src/mainloop.zig
index b514d7e9..0f861242 100644
--- a/src/mainloop.zig
+++ b/src/mainloop.zig
@@ -69,6 +69,7 @@ pub fn run(ctx: *Ctx) !void {
const loop_iter_cap: u32 = 25; // hard per-/loop iteration bound (never-completing-model guard)
var loop_iters_left: u32 = 0; // continuation turns still authorized this /loop run
var loop_continue_armed = false; // a continuation turn is queued for the next readline
+ var loop_list: goal_flow.LoopListGate = .{}; // diff-gate for the checklist copy in continuation prompts (#318)
while (true) {
// Titles can land between interactions but never join the response path.
@@ -81,6 +82,7 @@ pub fn run(ctx: *Ctx) !void {
const raw_line: []const u8 = if (steer_entry) |e| blk: {
loop_iters_left = 0; // #226: a user steer/force cancels the autonomous /loop run
loop_continue_armed = false;
+ loop_list.reset();
if (e.force) {
try ctx.out.print("{s}↳ force ›{s} {s}\n", .{ style.yellow, style.reset, e.text });
} else {
@@ -95,7 +97,9 @@ pub fn run(ctx: *Ctx) !void {
loop_continue_armed = false;
is_loop_continuation = true;
const todos_render = goal_state.renderCurrent(ctx.root); // current epoch only: parked work never re-enters a /loop turn (#318)
- const note = try repl_glue.continuationSteeringNote(ctx.arena, todos_render);
+ // Gated: these prompts persist in root.messages, autosave, and are
+ // compaction input, so the list is pasted only when it changed (#318).
+ const note = try loop_list.note(ctx.arena, todos_render);
break :blk try std.fmt.allocPrint(ctx.arena, "/loop {s}", .{note});
} else if (ctx.interactive) blk: {
try ctx.root.prompt();
@@ -550,18 +554,20 @@ pub fn run(ctx: *Ctx) !void {
// 80–95% a transient compaction failure can recover next turn.
const near_cap = ctx.root.provider.nearContextLimit(session_context_tokens);
ctx.root.compactOrRecover(near_cap);
+ loop_list.reset(); // the pasted copies died with the old history - re-carry the list once, mirroring goal_note_fp (#318)
}
// #226: /loop controller-authorized continuation. After a cleanly-
// completed autonomous /loop turn the CONTROLLER decides whether to run
- // another turn — not the model merely stopping. Keep going while the goal
- // is active and the work isn't asserted done (attempt_completion set
- // root.completed, or the checklist is finished); otherwise stop with a
- // NAMED terminal outcome. `loop_continue_armed` is consumed at the next
- // readline, so an interrupted/errored turn (which `continue`s past here)
- // never resumes the loop.
+ // another turn — not the model merely stopping; the rule itself is
+ // goal_flow.loopTurnDecision. `loop_continue_armed` is consumed at the
+ // next readline, so an interrupted/errored turn (which `continue`s past
+ // here) never resumes the loop.
if (loop_prompt != null and !main_mod.json_mode) {
- if (!is_loop_continuation) loop_iters_left = loop_iter_cap; // fresh /loop run: arm the bound
+ if (!is_loop_continuation) {
+ loop_iters_left = loop_iter_cap; // fresh /loop run: arm the bound
+ loop_list.reset(); // and a clean gate: its first continuation carries the list in full
+ }
switch (goal_flow.loopTurnDecision(ctx.root, loop_iters_left)) {
.continue_turn => {
loop_iters_left -= 1; // consume one credit for the queued continuation
@@ -570,6 +576,7 @@ pub fn run(ctx: *Ctx) !void {
.stop => |outcome| {
loop_iters_left = 0;
loop_continue_armed = false;
+ loop_list.reset();
// Only work_done reaches `accepted`, so the loop drove the
// goal to done; a --goal standing objective outlives it (#318).
if (outcome == .accepted) _ = goal_flow.acceptLoopOutcome(ctx.root);
diff --git a/src/repl_glue.zig b/src/repl_glue.zig
index 0ef2e380..260eac72 100644
--- a/src/repl_glue.zig
+++ b/src/repl_glue.zig
@@ -260,15 +260,6 @@ pub fn turnStopped(tool_calls: u64, completion_refused: bool) bool {
return tool_calls == 0 and !completion_refused;
}
-/// The steering appended to each autonomous /loop continuation turn (the
-/// continuation_steering_item analog): keep working the checklist, verify, and
-/// stop only when done or blocked — not a per-turn user note.
-pub fn continuationSteeringNote(arena: Allocator, todos_render: []const u8) ![]const u8 {
- if (todos_render.len == 0)
- return "[continuing autonomously (/loop): make the next concrete step toward the goal, then verify it. Do not ask for confirmation between routine steps. Stop only when the work is complete or you hit a blocker that needs the user.]";
- return std.fmt.allocPrint(arena, "[continuing autonomously (/loop): keep working the checklist below — do the next incomplete item, mark it in_progress then completed, and verify. Do not ask for confirmation between routine steps. Stop only when every item is done or you are blocked.\n\nChecklist so far:\n{s}]", .{todos_render});
-}
-
test "continuationDecision: one assertion per branch (#226)" {
// work asserted or the checklist finished -> accepted, regardless of iters.
try std.testing.expectEqual(ContinuationOutcome.accepted, continuationDecision(.active, true, false, 5).stop);
@@ -299,17 +290,6 @@ test "a zero-tool turn stops as idle, not accepted; a refused completion keeps t
try std.testing.expect(std.meta.activeTag(continuationDecision(.active, false, turnStopped(0, true), 5)) == .continue_turn);
}
-test "continuationSteeringNote: renders checklist when present, generic otherwise (#226)" {
- var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
- defer arena.deinit();
- const ar = arena.allocator();
- const empty = try continuationSteeringNote(ar, "");
- try std.testing.expect(std.mem.indexOf(u8, empty, "Checklist so far") == null);
- try std.testing.expect(std.mem.indexOf(u8, empty, "continuing autonomously") != null);
- const withlist = try continuationSteeringNote(ar, "[ ] add test");
- try std.testing.expect(std.mem.indexOf(u8, withlist, "Checklist so far:\n[ ] add test") != null);
-}
-
/// repl.TurnFn — run a full ROOT agent turn (tools + MCP) for `graff repl`, so
/// the model can read files, run bash, search the codebase, etc. — not a bare
/// completion. Auto-approves tools (yolo: the chat repl has no permission UI),