Skip to content

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

Python

fix(goal): a checklist from a previous session is not evidence this one is done

Rach Pradhan5c1b6dcfeadb
diff --git a/src/agent.zig b/src/agent.zig
index f2bf18dd..e04a383c 100644
--- a/src/agent.zig
+++ b/src/agent.zig
@@ -134,6 +134,7 @@ pub const Agent = struct {
pending_goal_note: ?[]const u8 = null, // one-shot supersession note for the next turn (/goal replace|clear)
completion_gate_armed: bool = false, // attempt_completion was refused; the promised second call closes the goal. Persists ACROSS turns (a model emits one per turn) until the checklist or goal changes (#318)
completion_refused: bool = false, // a refused attempt_completion this turn: work the model must react to, so /loop must not read the turn as zero-tool (#318)
+ todos_dirty: bool = false, // todo_write ran in THIS process: a checklist restored from disk is persisted state, never evidence that the current prompt is done (#318)
session_name: []const u8 = "last", // autosave/resume target (<name>.session.json)
session_title: ?[]const u8 = null, // human-readable title/rename metadata
sys_strict: []const u8 = prompts.main_system_prompt_strict,
diff --git a/src/agent_tools.zig b/src/agent_tools.zig
index b5d20bc5..9e09f121 100644
--- a/src/agent_tools.zig
+++ b/src/agent_tools.zig
@@ -315,7 +315,7 @@ pub fn handleMeta(self: *Agent, call: ToolCall) !ExecResult {
if (std.mem.eql(u8, call.name, "todo_write")) {
const epoch = goal_state.currentEpoch(self.goal); // items belong to the goal that authored them (#318)
_ = goal_state.clearEpoch(&self.todos, epoch); // replace THIS epoch's list; parked items from earlier goals survive
- goal_state.resetCompletionGate(self); // a new checklist is new evidence for the completion double-check
+ goal_state.noteTodoWrite(self); // new evidence for the completion double-check, and the list is now this-process evidence (#318)
if (call.input.object.get("todos")) |list| if (list == .array) {
for (list.array.items) |item| {
if (item != .object) continue;
diff --git a/src/commands_session.zig b/src/commands_session.zig
index 1b75a184..03da2510 100644
--- a/src/commands_session.zig
+++ b/src/commands_session.zig
@@ -59,6 +59,7 @@ const setTerminalTitle = title_mod.setTerminalTitle;
fn resetConversationSteering(root: *Agent) void {
root.goal = null;
root.completion_gate_armed = false; // a dropped goal re-arms the completion double-check (#318)
+ root.todos_dirty = false; // the conversation's checklist dies with it; nothing survives as /loop evidence (#318)
root.ultracode_mode = false;
root.pending_goal_note = null; // a queued supersession note dies with the conversation (#318)
root.goal_note_fp = 0;
diff --git a/src/goal_state.zig b/src/goal_state.zig
index 245dc62f..2206d73d 100644
--- a/src/goal_state.zig
+++ b/src/goal_state.zig
@@ -87,9 +87,15 @@ pub fn clearEpoch(todos: *std.ArrayList(TodoItem), epoch: u64) usize {
/// exists adopts it: the user just formalized the objective the plan was already
/// serving. Only `from_epoch` items move - work parked by a finished or
/// superseded goal stays parked and is never resurrected into the new objective.
+/// Only OPEN items are adopted: a completed item is finished work, and carrying
+/// it into the new epoch made the fresh goal born "done" - openCount 0 with a
+/// checklist present, so the completion gate's confirm-once arm was skipped and
+/// the goal was completable before any of its work happened (#318). Completed
+/// items keep their old epoch, i.e. they park.
pub fn adoptTodos(todos: []TodoItem, from_epoch: u64, to_epoch: u64) void {
for (todos) |*t| {
- if (t.epoch == from_epoch) t.epoch = to_epoch;
+ if (t.epoch != from_epoch or std.mem.eql(u8, t.status, "completed")) continue;
+ t.epoch = to_epoch;
}
}
@@ -111,6 +117,39 @@ pub fn noteCompletionRefused(root: *Agent) void {
root.completion_refused = true;
}
+/// Did the checklist finish in THIS process? `allDone` alone is not evidence:
+/// todos persist, so a checklist completed in a PREVIOUS session made the next
+/// /loop stop at iteration 1 as `accepted` and flipped the restored goal to
+/// complete with zero work done (#318). The freshness flag also covers the
+/// goal == null case, where an all-completed epoch-0 leftover satisfies allDone
+/// on a bare /loop. Only todo_write sets it; loadSession, /clear and /new clear it.
+pub fn checklistFinished(root: *const Agent) bool {
+ if (!root.todos_dirty) return false;
+ return allDone(root.todos.items, currentEpoch(root.goal));
+}
+
+/// todo_write just rewrote the current epoch's checklist: the completion
+/// double-check gets fresh evidence to check against, and the list becomes
+/// this-process evidence for /loop termination (#318).
+pub fn noteTodoWrite(root: *Agent) void {
+ resetCompletionGate(root);
+ root.todos_dirty = true;
+}
+
+/// Load-time reconciliation (#318): a session that finished its checklist but
+/// quit without calling attempt_completion persists {goal .active, all-[x]
+/// list} - models routinely finish work without that call. Restoring it verbatim
+/// re-steers the resumed session at work already done, which is #318's literal
+/// complaint surviving the resume boundary. Retire the goal instead. Returns
+/// true when it reconciled so the caller can note it. `allDone` is false on an
+/// empty epoch (#226), so a goal that never wrote a checklist is left alone.
+pub fn reconcileRestored(root: *Agent) bool {
+ const g = root.goal orelse return false;
+ if (g.status != .active or !allDone(root.todos.items, g.epoch)) return false;
+ root.goal.?.status = .complete;
+ return true;
+}
+
/// Reset the completion double-check because the evidence changed: todo_write
/// wrote a new current-epoch checklist, or /goal set|replace|clear moved the
/// objective. Clearing the arm makes the NEXT attempt_completion get checked
@@ -279,7 +318,7 @@ test "parking retains: superseded items are counted and kept, never deleted (#31
try std.testing.expectEqualStrings("phase3 audit", todos.items[0].content);
}
-test "adoptTodos moves the current epoch only; parked work is never resurrected" {
+test "adoptTodos takes OPEN current-epoch items only; finished and parked work stays put" {
var todos = [_]TodoItem{
.{ .content = "a", .status = "pending", .epoch = 0 },
.{ .content = "b", .status = "completed", .epoch = 0 },
@@ -287,8 +326,84 @@ test "adoptTodos moves the current epoch only; parked work is never resurrected"
};
adoptTodos(&todos, 0, 5);
try std.testing.expectEqual(@as(u64, 5), todos[0].epoch);
- try std.testing.expectEqual(@as(u64, 5), todos[1].epoch);
+ try std.testing.expectEqual(@as(u64, 0), todos[1].epoch); // finished work is not the new goal's plan
try std.testing.expectEqual(@as(u64, 3), todos[2].epoch);
+ // The new goal inherits only real remaining work, so its accounting is honest.
+ try std.testing.expectEqual(@as(usize, 1), openCount(&todos, 5));
+ try std.testing.expect(!allDone(&todos, 5));
+}
+
+test "a goal formalized over an already-finished list adopts nothing and is not born done (#318)" {
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+ const ar = arena_state.allocator();
+ var root: Agent = undefined;
+ root.sub = false;
+ root.review_mode = false;
+ root.arena = ar;
+ root.todos = .empty;
+ root.completion_gate_armed = false;
+ root.goal = null;
+ try root.todos.append(ar, .{ .content = "done before the goal", .status = "completed", .epoch = 0 });
+ adoptTodos(root.todos.items, currentEpoch(root.goal), 1);
+ root.goal = .{ .objective = "ship", .epoch = 1 };
+ try std.testing.expect(!hasCurrent(root.todos.items, 1)); // nothing adopted
+ // Without this the goal would satisfy "checklist present, 0 open" on turn 1
+ // and complete before any of its work happened; now the gate asks first.
+ const r = try completionGate(ar, &root);
+ try std.testing.expect(r != null and std.mem.indexOf(u8, r.?, "no checklist") != null);
+}
+
+test "checklistFinished demands THIS process's todo_write; a restored all-[x] list is inert (#318)" {
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+ const ar = arena_state.allocator();
+ var root: Agent = undefined;
+ root.arena = ar;
+ root.todos = .empty;
+ root.goal = null;
+ root.todos_dirty = false; // exactly what loadSession leaves behind
+ try root.todos.append(ar, .{ .content = "old work", .status = "completed", .epoch = 0 });
+ // The goal == null variant: a bare /loop must not stop at iteration 1 just
+ // because a previous session left a finished epoch-0 list on disk.
+ try std.testing.expect(allDone(root.todos.items, 0));
+ try std.testing.expect(!checklistFinished(&root));
+ root.goal = .{ .objective = "ship", .epoch = 2, .status = .active };
+ try root.todos.append(ar, .{ .content = "restored item", .status = "completed", .epoch = 2 });
+ try std.testing.expect(!checklistFinished(&root)); // same for a restored goal's own list
+ // todo_write in this process is the evidence, and it clears the gate arm too.
+ root.completion_gate_armed = true;
+ noteTodoWrite(&root);
+ try std.testing.expect(root.todos_dirty and !root.completion_gate_armed);
+ try std.testing.expect(checklistFinished(&root));
+ // An open item in the current epoch still blocks it.
+ try root.todos.append(ar, .{ .content = "new work", .status = "pending", .epoch = 2 });
+ try std.testing.expect(!checklistFinished(&root));
+}
+
+test "reconcileRestored retires a restored-active goal whose checklist is already done (#318)" {
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+ const ar = arena_state.allocator();
+ var root: Agent = undefined;
+ root.arena = ar;
+ root.todos = .empty;
+ root.goal = null;
+ try std.testing.expect(!reconcileRestored(&root)); // no goal
+ root.goal = .{ .objective = "ship", .epoch = 2, .status = .active };
+ try std.testing.expect(!reconcileRestored(&root)); // no checklist is not "done" (#226)
+ try root.todos.append(ar, .{ .content = "a", .status = "completed", .epoch = 2 });
+ try root.todos.append(ar, .{ .content = "b", .status = "pending", .epoch = 2 });
+ try std.testing.expect(!reconcileRestored(&root)); // still open work
+ root.todos.items[1].status = "completed";
+ try std.testing.expect(reconcileRestored(&root));
+ try std.testing.expectEqual(agent_mod.GoalStatus.complete, root.goal.?.status);
+ try std.testing.expect(!reconcileRestored(&root)); // idempotent: only an ACTIVE goal reconciles
+ // A retired goal stops steering and stops rendering its parked list.
+ try std.testing.expectEqualStrings("", renderCurrent(&root));
+ // Parked work from an earlier goal never triggers reconciliation.
+ root.goal = .{ .objective = "next", .epoch = 3, .status = .active };
+ try std.testing.expect(!reconcileRestored(&root));
}
test "renderTodos is epoch-scoped; renderCurrent goes quiet on a dead epoch (#318)" {
diff --git a/src/mainloop.zig b/src/mainloop.zig
index c7350487..4cdb3b4e 100644
--- a/src/mainloop.zig
+++ b/src/mainloop.zig
@@ -567,7 +567,7 @@ pub fn run(ctx: *Ctx) !void {
// (codex RegularTask semantics: no /loop burns 25 continuations on a
// one-turn prompt); a refused attempt_completion is work, not silence.
const work_done = ctx.root.completed != null or
- goal_state.allDone(ctx.root.todos.items, goal_state.currentEpoch(ctx.root.goal));
+ goal_state.checklistFinished(ctx.root); // a checklist restored from disk is not evidence (#318)
const model_stopped = repl_glue.turnStopped(ctx.root.tool_calls_this_turn, ctx.root.completion_refused);
const gstatus: agent_mod.GoalStatus = if (ctx.root.goal) |g| g.status else .active;
switch (repl_glue.continuationDecision(gstatus, work_done, model_stopped, loop_iters_left)) {