graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
fix(goal): reconcile a finished checklist with a still-active goal on resume
Rach Pradhan6cb97a96f1a1
diff --git a/src/goal_persist_tests.zig b/src/goal_persist_tests.zignew file mode 100644index 00000000..e4a0c5df--- /dev/null+++ b/src/goal_persist_tests.zig@@ -0,0 +1,135 @@+//! Persistence round-trip tests for the #318 goal/checklist state: what+//! saveSession writes, what loadSession restores, and what goal_state makes of+//! it. Kept out of session.zig (600-line goal) and reached through the+//! `test { _ = ... }` hook in main.zig, mirroring scoring_slot_test.zig.+//!+//! #318 re-entered through this door: todos became durable, so a checklist+//! finished in a PREVIOUS session was read as evidence that the CURRENT prompt+//! was done, and a goal that was really over came back steering an all-[x] list.++const std = @import("std");+const Allocator = std.mem.Allocator;+const agent_mod = @import("agent.zig");+const session = @import("session.zig");+const goal_state = @import("goal_state.zig");+const Agent = agent_mod.Agent;++/// Restore `json` onto `root` the way loadSession does (session.zig): parse the+/// goal, refill the epoch-stamped checklist, drop this-process freshness, then+/// reconcile. The parsing and the reconciliation are the real functions; only+/// the four-statement order is restated here.+fn resumeSession(arena: Allocator, root: *Agent, json: []const u8) !void {+ const parsed = try std.json.parseFromSliceLeaky(std.json.Value, arena, json, .{ .allocate = .alloc_always });+ root.goal = if (parsed.object.get("goal")) |v| session.goalFromValue(v, 1_000) else null;+ root.todos.clearRetainingCapacity();+ if (parsed.object.get("todos")) |tv| try session.appendTodosFromValue(arena, &root.todos, tv);+ root.todos_dirty = false;+ _ = goal_state.reconcileRestored(root);+}++/// A root agent in the state loadSession hands back: no live turn behind it.+fn blankRoot(arena: Allocator) Agent {+ var root: Agent = undefined;+ root.arena = arena;+ root.sub = false;+ root.review_mode = false;+ root.todos = .empty;+ root.goal = null;+ root.todos_dirty = false;+ root.completion_gate_armed = false;+ return root;+}++test "resume: an active goal whose checklist is already finished retires (#318)" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const ar = arena_state.allocator();+ var root = blankRoot(ar);+ // Exactly the shape saveSession writes (session.zig): the model finished the+ // work but quit without calling attempt_completion, so the goal is still+ // .active on disk. Epoch 1 carries an item parked by an earlier goal.+ try resumeSession(ar, &root,+ \\{"goal":{"objective":"ship phase 2","status":"active","epoch":2,"created_ms":1,"updated_ms":2},+ \\ "todos":[{"content":"phase 3 audit","status":"pending","epoch":1},+ \\ {"content":"fix the tests","status":"completed","epoch":2},+ \\ {"content":"verify in the browser","status":"completed","epoch":2}]}+ );+ try std.testing.expectEqual(agent_mod.GoalStatus.complete, root.goal.?.status);+ try std.testing.expect(!goal_state.goalActive(&root)); // no steering note on the first resumed turn+ try std.testing.expectEqualStrings("", goal_state.renderCurrent(&root)); // and no all-[x] list to re-paste+ // Reconciliation retires, it never deletes: the parked epoch-1 item and the+ // finished epoch-2 list both survive the round trip (#318 D-PARK).+ try std.testing.expectEqual(@as(usize, 3), root.todos.items.len);+ try std.testing.expectEqual(@as(usize, 1), goal_state.parkedOpenCount(root.todos.items, 2));+ // The retired goal is not fresh completion evidence either: the first /loop+ // turn of the resumed session must still do real work to stop as accepted.+ try std.testing.expect(!goal_state.checklistFinished(&root));+}++test "resume: open items keep the goal active and the checklist current (#318)" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const ar = arena_state.allocator();+ var root = blankRoot(ar);+ try resumeSession(ar, &root,+ \\{"goal":{"objective":"ship phase 2","status":"active","epoch":2,"created_ms":1,"updated_ms":2},+ \\ "todos":[{"content":"fix the tests","status":"completed","epoch":2},+ \\ {"content":"verify in the browser","status":"pending","epoch":2}]}+ );+ try std.testing.expectEqual(agent_mod.GoalStatus.active, root.goal.?.status); // real work is left+ try std.testing.expect(goal_state.goalActive(&root));+ try std.testing.expectEqual(@as(usize, 1), goal_state.openCount(root.todos.items, 2));+ try std.testing.expectEqualStrings("[x] fix the tests\n[ ] verify in the browser", goal_state.renderCurrent(&root));+ try std.testing.expect(!goal_state.checklistFinished(&root));+ // Finishing the restored list only counts once todo_write has run in THIS+ // process - the /loop allDone arm stays inert until then.+ root.todos.items[1].status = "completed";+ try std.testing.expect(goal_state.allDone(root.todos.items, 2));+ try std.testing.expect(!goal_state.checklistFinished(&root));+ goal_state.noteTodoWrite(&root);+ try std.testing.expect(goal_state.checklistFinished(&root));+}++test "resume: a finished epoch-0 leftover cannot end a bare /loop (#318)" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const ar = arena_state.allocator();+ var root = blankRoot(ar);+ // No goal at all, and a checklist the previous session finished. Before the+ // freshness flag this made /loop stop at iteration 1 as `accepted` without+ // the model doing anything.+ try resumeSession(ar, &root,+ \\{"goal":null,+ \\ "todos":[{"content":"old work","status":"completed","epoch":0}]}+ );+ try std.testing.expect(root.goal == null); // nothing to reconcile+ try std.testing.expect(goal_state.allDone(root.todos.items, 0));+ try std.testing.expect(!goal_state.checklistFinished(&root));+ goal_state.noteTodoWrite(&root); // the model restates its plan: now it counts+ try std.testing.expect(goal_state.checklistFinished(&root));+}++test "resume: a paused or already-complete goal is restored verbatim (#318)" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const ar = arena_state.allocator();+ var root = blankRoot(ar);+ // Paused with a finished checklist: the user stepped in, and reconciliation+ // must not decide the objective is over on their behalf. /goal resume then+ // finds its checklist exactly where it was left.+ try resumeSession(ar, &root,+ \\{"goal":{"objective":"ship phase 2","status":"paused","epoch":2,"created_ms":1,"updated_ms":2},+ \\ "todos":[{"content":"fix the tests","status":"completed","epoch":2}]}+ );+ try std.testing.expectEqual(agent_mod.GoalStatus.paused, root.goal.?.status);+ try std.testing.expectEqual(@as(u64, 2), goal_state.currentEpoch(root.goal)); // paused goals still own their epoch+ // A goal completed before the quit stays complete, with its list parked.+ var root2 = blankRoot(ar);+ try resumeSession(ar, &root2,+ \\{"goal":{"objective":"ship phase 2","status":"complete","epoch":2,"created_ms":1,"updated_ms":2},+ \\ "todos":[{"content":"leftover","status":"pending","epoch":2}]}+ );+ try std.testing.expectEqual(agent_mod.GoalStatus.complete, root2.goal.?.status);+ try std.testing.expectEqual(@as(u64, 0), goal_state.currentEpoch(root2.goal)); // dead epoch: later writes are unscoped+ try std.testing.expectEqualStrings("", goal_state.renderCurrent(&root2));+}diff --git a/src/main.zig b/src/main.zigindex 07df2a99..351cdf2f 100644--- a/src/main.zig+++ b/src/main.zig@@ -593,4 +593,5 @@ test { // pull in tests from imported modules (mcp.zig)_ = @import("scoring_slot_test.zig");_ = @import("readline_history.zig");_ = @import("goal_state.zig");+ _ = @import("goal_persist_tests.zig");}diff --git a/src/session.zig b/src/session.zigindex fd8f44ac..52eeaad1 100644--- a/src/session.zig+++ b/src/session.zig@@ -17,6 +17,7 @@ const Allocator = std.mem.Allocator;const agent_mod = @import("agent.zig");const provider_mod = @import("provider.zig");const util = @import("util.zig");+const goal_state = @import("goal_state.zig");const Agent = agent_mod.Agent;const Keys = provider_mod.Keys;const unixMs = util.unixMs;@@ -294,7 +295,7 @@ pub fn saveSession(root: *Agent, arena: Allocator, name: []const u8) !void {/// objective + status + created/updated timestamps (an unknown/missing status/// falls back to .active). Pure (no Io) so it round-trips in unit tests. Returns/// null for an empty/absent objective.-fn goalFromValue(v: Value, now_ms: i64) ?agent_mod.Goal {+pub fn goalFromValue(v: Value, now_ms: i64) ?agent_mod.Goal {if (v == .string) {if (v.string.len == 0) return null;return .{ .objective = v.string, .status = .active, .created_ms = now_ms, .updated_ms = now_ms };@@ -315,7 +316,7 @@ fn goalFromValue(v: Value, now_ms: i64) ?agent_mod.Goal {/// Parse the persisted `todos` array (#318). The caller clears the list first/// so nothing from a previous conversation survives a resume. Pure (no Io) so/// it round-trips in unit tests.-fn appendTodosFromValue(arena: Allocator, todos: *std.ArrayList(agent_mod.TodoItem), v: Value) !void {+pub fn appendTodosFromValue(arena: Allocator, todos: *std.ArrayList(agent_mod.TodoItem), v: Value) !void {if (v != .array) return;for (v.array.items) |item| {if (item != .object) continue;@@ -472,6 +473,9 @@ pub fn loadSession(root: *Agent, keys: *Keys, arena: Allocator, name: []const u8root.goal = goal;root.todos.clearRetainingCapacity(); // never inherit another conversation's checklist (#318)if (obj.get("todos")) |tv| try appendTodosFromValue(arena, &root.todos, tv);+ root.todos_dirty = false; // restored todos are persisted state, never this-process completion evidence (#318)+ // #318: retire a restored-active goal whose checklist is already finished.+ if (goal_state.reconcileRestored(root)) if (root.tracer) |t| t.note("goal", "reconciled-complete");// Steering gate state belongs to the previous conversation (#318): drop any// queued one-shot note and force a full goal-note re-statement next turn.root.pending_goal_note = null;