graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
fix(goal): a new objective must not land on a parked goal's epoch
Rach Pradhana26670d31eec
diff --git a/src/commands_session.zig b/src/commands_session.zigindex 03da2510..f182ea55 100644--- a/src/commands_session.zig+++ b/src/commands_session.zig@@ -178,7 +178,7 @@ pub fn tryHandle(root: *Agent, keys: *Keys, arena: Allocator, line: []const u8,} else try out.writeAll("No goal to resume. Set one with /goal <objective>.\n");} else {const now = unixMs(root.io);- const epoch = goal_state.nextEpoch(root.goal);+ const epoch = goal_state.nextEpoch(root.goal, root.todos.items); // above every parked epoch too, or a cleared goal's list becomes this one's (#318)// A COMPLETE goal is already retired and its checklist already parked// (#318 D1), so the next /goal is not superseding live work: it takes// the adoption branch, exactly like a first /goal.diff --git a/src/goal_flow.zig b/src/goal_flow.zignew file mode 100644index 00000000..b3f45874--- /dev/null+++ b/src/goal_flow.zig@@ -0,0 +1,83 @@+//! Goal lifecycle FLOW (#318): the pure helpers a /goal transition needs, plus+//! the scenario tests that walk a whole sequence (set -> work -> clear -> set)+//! instead of one function in isolation. Split out of goal_state.zig, which is+//! at this repo's 600-line file cap. Reached through the `test { _ = ... }`+//! hook in main.zig - without that line these tests silently compile to+//! nothing and the suite still reports green.++const std = @import("std");+const Allocator = std.mem.Allocator;+const agent_mod = @import("agent.zig");+const goal_state = @import("goal_state.zig");+const Agent = agent_mod.Agent;+const TodoItem = agent_mod.TodoItem;++/// A root agent in the shape the goal helpers read; no live turn behind it.+fn flowRoot(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;+ root.completion_refused = false;+ return root;+}++test "a cleared goal's finished checklist cannot be reborn as the next goal's list (#318)" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const ar = arena_state.allocator();+ var root = flowRoot(ar);++ // /goal A, a checklist, and the model finishes every item.+ const epoch_a = goal_state.nextEpoch(root.goal, root.todos.items);+ root.goal = .{ .objective = "phase A", .epoch = epoch_a };+ try root.todos.append(ar, .{ .content = "A1", .status = "completed", .epoch = epoch_a });+ goal_state.noteTodoWrite(&root); // todos_dirty is now true and NOTHING in /goal clear or set resets it+ try std.testing.expect(goal_state.checklistFinished(&root));++ // /goal clear: the goal goes, its items park at epoch 1 (retention, #318).+ root.goal = null;+ goal_state.resetCompletionGate(&root);++ // /goal B. nextEpoch used to read the live goal only, so this was epoch 1+ // again and B inherited A's finished list: born done, the first+ // attempt_completion accepted with zero work, and a /loop under B stopped+ // at iteration 1 as `accepted` while flipping B to complete.+ const epoch_b = goal_state.nextEpoch(root.goal, root.todos.items);+ try std.testing.expectEqual(@as(u64, 2), epoch_b);+ goal_state.adoptTodos(root.todos.items, goal_state.currentEpoch(root.goal), epoch_b); // the /goal set branch+ root.goal = .{ .objective = "phase B", .epoch = epoch_b };+ try std.testing.expect(!goal_state.hasCurrent(root.todos.items, epoch_b)); // B starts with no plan+ const refusal = try goal_state.completionGate(ar, &root);+ try std.testing.expect(refusal != null and std.mem.indexOf(u8, refusal.?, "no checklist") != null);+ try std.testing.expect(!goal_state.checklistFinished(&root)); // and no all-done list for /loop to read+ // A's work is retained and parked, exactly where it was left.+ try std.testing.expectEqual(@as(usize, 1), root.todos.items.len);+ try std.testing.expectEqual(@as(u64, 1), root.todos.items[0].epoch);+}++test "a cleared goal's OPEN items never become the next goal's work (#318)" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const ar = arena_state.allocator();+ var root = flowRoot(ar);+ root.goal = .{ .objective = "phase A", .epoch = 1 };+ try root.todos.append(ar, .{ .content = "audit the old phase", .status = "pending", .epoch = 1 });+ root.goal = null; // /goal clear parks the unfinished item++ const epoch_b = goal_state.nextEpoch(root.goal, root.todos.items);+ goal_state.adoptTodos(root.todos.items, goal_state.currentEpoch(root.goal), epoch_b);+ root.goal = .{ .objective = "phase B", .epoch = epoch_b };+ // The colliding epoch made A's leftover item read as B's open work - #318's+ // literal reattachment complaint, reborn one /goal clear later.+ try std.testing.expectEqual(@as(usize, 0), goal_state.openCount(root.todos.items, epoch_b));+ try std.testing.expectEqual(@as(usize, 1), goal_state.parkedOpenCount(root.todos.items, epoch_b));+ const refusal = try goal_state.completionGate(ar, &root);+ try std.testing.expect(refusal != null and std.mem.indexOf(u8, refusal.?, "no checklist") != null);+ try std.testing.expect(std.mem.indexOf(u8, refusal.?, "open checklist item") == null);+ try std.testing.expectEqualStrings("", goal_state.renderCurrent(&root)); // B never renders A's list+}diff --git a/src/goal_persist_tests.zig b/src/goal_persist_tests.zigindex e4a0c5df..1a39e670 100644--- a/src/goal_persist_tests.zig+++ b/src/goal_persist_tests.zig@@ -109,6 +109,30 @@ test "resume: a finished epoch-0 leftover cannot end a bare /loop (#318)" {try std.testing.expect(goal_state.checklistFinished(&root));}+test "resume: a /goal set after restoring parked items lands above them (#318)" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const ar = arena_state.allocator();+ var root = blankRoot(ar);+ // The shape /goal clear leaves on disk: no goal, and its checklist parked at+ // epoch 1. Nothing persists the epoch counter, so the next /goal re-derives+ // it - from the live goal alone that was epoch 1 again, and the restored+ // list became the new objective's work the moment it was set.+ try resumeSession(ar, &root,+ \\{"goal":null,+ \\ "todos":[{"content":"leftover from the cleared goal","status":"pending","epoch":1},+ \\ {"content":"and its finished item","status":"completed","epoch":1}]}+ );+ try std.testing.expect(root.goal == null);+ const epoch = goal_state.nextEpoch(root.goal, root.todos.items);+ try std.testing.expectEqual(@as(u64, 2), epoch);+ goal_state.adoptTodos(root.todos.items, goal_state.currentEpoch(root.goal), epoch); // the /goal set branch+ root.goal = .{ .objective = "a brand new objective", .epoch = epoch };+ try std.testing.expect(!goal_state.hasCurrent(root.todos.items, epoch)); // it starts with no plan of its own+ try std.testing.expectEqual(@as(usize, 1), goal_state.parkedOpenCount(root.todos.items, epoch));+ 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();diff --git a/src/goal_state.zig b/src/goal_state.zigindex 2206d73d..ee6b519a 100644--- a/src/goal_state.zig+++ b/src/goal_state.zig@@ -19,9 +19,19 @@ const Agent = agent_mod.Agent;const TodoItem = agent_mod.TodoItem;const repl_glue = @import("repl_glue.zig");-/// Epoch for a goal that replaces `old` (0 is the no-goal/legacy epoch).-pub fn nextEpoch(old: ?agent_mod.Goal) u64 {- return if (old) |g| g.epoch + 1 else 1;+/// Epoch for a goal that replaces `old`, above every epoch already present in+/// `todos` (0 is the no-goal/legacy epoch, so the first goal gets 1). Deriving+/// it from the live goal alone was not enough (#318): /goal clear nulls the goal+/// while its items keep their epoch, so the NEXT goal was born on epoch 1 again+/// and silently adopted the cleared goal's parked checklist - born done when+/// that list was finished, steering someone else's open items when it was not.+/// Parked epochs are the high-water mark, so a new objective always lands above+/// them. The same collision is reachable across a resume (goal null on disk,+/// parked items restored), which is why this reads the todos and not a counter.+pub fn nextEpoch(old: ?agent_mod.Goal, todos: []const TodoItem) u64 {+ var top: u64 = if (old) |g| g.epoch else 0;+ for (todos) |t| top = @max(top, t.epoch);+ return top + 1;}/// Epoch new todos are stamped with: the standing goal's while it still owns the@@ -271,9 +281,18 @@ pub fn renderCurrent(root: *Agent) []const u8 {return renderTodos(root, epoch);}-test "epochs: nextEpoch monotonic from the replaced goal; currentEpoch 0 without a goal" {- try std.testing.expectEqual(@as(u64, 1), nextEpoch(null));- try std.testing.expectEqual(@as(u64, 4), nextEpoch(.{ .objective = "a", .epoch = 3 }));+test "epochs: nextEpoch clears the live goal AND every parked epoch; currentEpoch 0 without a goal" {+ const parked = [_]TodoItem{+ .{ .content = "parked by a cleared goal", .status = "pending", .epoch = 4 },+ .{ .content = "older", .status = "completed", .epoch = 2 },+ };+ try std.testing.expectEqual(@as(u64, 1), nextEpoch(null, &.{}));+ try std.testing.expectEqual(@as(u64, 4), nextEpoch(.{ .objective = "a", .epoch = 3 }, &.{}));+ // No live goal (/goal clear) but parked items: the next goal lands ABOVE+ // them instead of colliding with epoch 1 and adopting someone else's list.+ try std.testing.expectEqual(@as(u64, 5), nextEpoch(null, &parked));+ // A live goal below the high-water mark does not drag the next epoch down.+ try std.testing.expectEqual(@as(u64, 5), nextEpoch(.{ .objective = "a", .epoch = 1 }, &parked));try std.testing.expectEqual(@as(u64, 0), currentEpoch(null));try std.testing.expectEqual(@as(u64, 7), currentEpoch(.{ .objective = "a", .epoch = 7 }));// A completed goal is a dead epoch: later todo_write calls are unscopeddiff --git a/src/main.zig b/src/main.zigindex 351cdf2f..dca5673f 100644--- a/src/main.zig+++ b/src/main.zig@@ -594,4 +594,5 @@ test { // pull in tests from imported modules (mcp.zig)_ = @import("readline_history.zig");_ = @import("goal_state.zig");_ = @import("goal_persist_tests.zig");+ _ = @import("goal_flow.zig");}