Skip to content

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

Python

todos: a newer user message can cancel stale verification items

justrach6fbbec30294b
diff --git a/src/agent.zig b/src/agent.zig
index ff8ffa9f..241ff6c9 100644
--- a/src/agent.zig
+++ b/src/agent.zig
@@ -33,6 +33,13 @@ pub const TodoItem = struct {
status: []const u8,
epoch: u64 = 0, // the goal epoch that authored this item (#318); 0 = no goal
retired: bool = false, // a LATER user ask retired this finished item (#394): kept as the session's archive, invisible to every epoch-scoped query
+ ask_fp: u64 = 0, // #1545: fingerprint of the user request this item was first written under; "cancelled" needs a newer one (0 = legacy/restored)
+
+ /// Completed or cancelled: no longer open work. A cancelled item is parked,
+ /// never verified (goal_verify.taskVerified still refuses it).
+ pub fn closed(t: TodoItem) bool {
+ return std.mem.eql(u8, t.status, "completed") or std.mem.eql(u8, t.status, "cancelled");
+ }
};
/// Governed-run status for a standing /goal (#223). Only `.active` steers turns;
/// pause/resume and the run continuation gate (#226) key off the others.
diff --git a/src/goal_state.zig b/src/goal_state.zig
index e83b9ecd..6648229a 100644
--- a/src/goal_state.zig
+++ b/src/goal_state.zig
@@ -54,7 +54,7 @@ pub fn currentEpoch(goal: ?agent_mod.Goal) u64 {
pub fn openCount(todos: []const TodoItem, epoch: u64) usize {
var n: usize = 0;
for (todos) |t| {
- if (t.epoch == epoch and !t.retired and !std.mem.eql(u8, t.status, "completed")) n += 1;
+ if (t.epoch == epoch and !t.retired and !t.closed()) n += 1; // #1545: cancelled is parked, not open
}
return n;
}
@@ -67,7 +67,7 @@ pub fn allDone(todos: []const TodoItem, epoch: u64) bool {
var seen = false;
for (todos) |t| {
if (t.epoch != epoch or t.retired) continue;
- if (!std.mem.eql(u8, t.status, "completed")) return false;
+ if (!t.closed()) return false; // #1545: a cancelled item closes, but never verifies (goal_verify)
seen = true;
}
return seen;
@@ -304,8 +304,7 @@ pub fn renderTodos(self: *Agent, epoch: u64) []const u8 {
"[x]"
else if (std.mem.eql(u8, t.status, "in_progress"))
"[~]"
- else
- "[ ]";
+ else if (std.mem.eql(u8, t.status, "cancelled")) "[-]" else "[ ]";
w.print("{s} {s}\n", .{ mark, t.content }) catch break;
}
return std.mem.trimEnd(u8, aw.writer.buffered(), "\n");
diff --git a/src/goal_todo.zig b/src/goal_todo.zig
index 09cc00f9..89fd389b 100644
--- a/src/goal_todo.zig
+++ b/src/goal_todo.zig
@@ -45,7 +45,7 @@ pub fn clearEpochForReplaceEx(todos: *std.ArrayList(TodoItem), epoch: u64, incom
var i: usize = 0;
while (i < todos.items.len) {
const t = todos.items[i];
- const done = std.mem.eql(u8, t.status, "completed");
+ const done = t.closed(); // completed or cancelled (#1545): omitted, it stays as history
const mentioned = mentions(incoming, t.content);
const verify = !done and goal_verify_kind.isVerification(t.content);
if (t.epoch != epoch or (done and !mentioned) or (verify and !mentioned)) {
@@ -128,21 +128,69 @@ pub fn applyTodoWrite(root: *Agent, list: ?Value) !WriteResult {
}
if (incoming.items.len == 0)
return .{ .text = "todo_write had no usable items; the list is unchanged. Each item needs a content string and a status. Send your full remaining plan - completed items you omit are kept automatically.", .rejected = true };
+ // #1545: the replace removes re-listed items, so read their prior entry first.
+ const priors = try root.gpa.alloc(?TodoItem, incoming.items.len);
+ defer root.gpa.free(priors);
+ for (incoming.items, priors) |c, *p| p.* = priorItem(root.todos.items, epoch, c);
+ const ask_now = askFingerprint(root);
const cleared = clearEpochForReplaceEx(&root.todos, epoch, incoming.items);
goal_state.noteTodoWrite(root); // list changed; open verification still fails allDone (#844)
+ var refused_cancel: usize = 0;
if (asArray(list)) |items| {
+ var k: usize = 0;
for (items) |item| {
const content = contentOf(item) orelse continue;
- const status = if (item.object.get("status")) |st| (if (st == .string) st.string else "pending") else "pending";
+ const prior = priors[k];
+ k += 1;
+ var status = if (item.object.get("status")) |st| (if (st == .string) st.string else "pending") else "pending";
+ if (!cancelAllowed(status, content, prior, ask_now)) {
+ status = if (prior) |p| p.status else "pending";
+ refused_cancel += 1;
+ }
try root.todos.append(root.arena, .{
.content = try root.arena.dupe(u8, content),
.status = try root.arena.dupe(u8, status),
.epoch = epoch,
+ .ask_fp = if (prior) |p| p.ask_fp else ask_now, // the request it was FIRST written under
});
}
}
+ var res = try writeResult(root, epoch, incoming.items, cleared);
+ if (refused_cancel > 0) {
+ const note = try std.fmt.allocPrint(root.arena, "\n({d} verification item(s) were not cancelled: a verification item can be cancelled only after a newer user message changes scope; they keep their previous status)", .{refused_cancel});
+ res.text = try std.mem.concat(root.arena, u8, &.{ res.text, note });
+ res.model_text = try std.mem.concat(root.arena, u8, &.{ res.reply(), note });
+ }
+ return res;
+}
+
+/// #1545: cancelling ordinary work is always allowed (omitting it drops it
+/// anyway). A verification item is the #844 obligation, so only the USER can
+/// lift it: the latest user request must differ from the one it was written
+/// under. A brand-new item has nothing to cancel.
+fn cancelAllowed(status: []const u8, content: []const u8, prior: ?TodoItem, ask_now: u64) bool {
+ if (!std.mem.eql(u8, status, "cancelled")) return true;
+ if (!goal_verify_kind.isVerification(content)) return true;
+ const p = prior orelse return false;
+ return p.ask_fp != ask_now;
+}
+
+/// Fingerprint of the latest real user request (notices excluded). Compaction
+/// keeps the current turn's opening message verbatim, so it is stable within
+/// a request and changes when the user sends a new one or steers mid-run.
+fn askFingerprint(root: *const Agent) u64 {
+ const t = @import("messages.zig").latestUserText(root.messages.items);
+ return std.hash.Wyhash.hash(0x1545, t) | 1; // never 0: 0 marks a legacy item
+}
+
+fn priorItem(todos: []const TodoItem, epoch: u64, content: []const u8) ?TodoItem {
+ for (todos) |t| if (t.epoch == epoch and !t.retired and std.mem.eql(u8, t.content, content)) return t;
+ return null;
+}
+
+fn writeResult(root: *Agent, epoch: u64, incoming: []const []const u8, cleared: ReplaceClear) !WriteResult {
const rendered = goal_state.renderTodos(root, epoch);
- const reply = try @import("todo_reply.zig").text(root.arena, root.todos.items, epoch, incoming.items, cleared.dropped_open, cleared.kept_verify);
+ const reply = try @import("todo_reply.zig").text(root.arena, root.todos.items, epoch, incoming, cleared.dropped_open, cleared.kept_verify);
if (cleared.dropped_open == 0 and cleared.kept_verify == 0) return .{ .text = rendered, .model_text = reply };
if (cleared.kept_verify > 0 and cleared.dropped_open == 0)
return .{
@@ -180,6 +228,7 @@ fn todoRoot(arena: std.mem.Allocator) Agent {
root.review_mode = false;
root.todos = .empty;
root.goal = null;
+ root.messages = std.json.Array.init(arena); // #1545: applyTodoWrite fingerprints the latest user request
root.todos_dirty = false;
root.completion_gate_armed = false;
return root;
diff --git a/src/goal_verify.zig b/src/goal_verify.zig
index 90b8b264..a76d627a 100644
--- a/src/goal_verify.zig
+++ b/src/goal_verify.zig
@@ -19,12 +19,23 @@ pub fn unresolvedCount(agent: *const Agent) usize {
var n: usize = 0;
for (agent.todos.items) |t| {
if (t.epoch != epoch or t.retired) continue;
- if (std.mem.eql(u8, t.status, "completed")) continue;
+ if (t.closed()) continue; // #1545: cancelled after a scope change is parked, not owed
if (isVerification(t.content)) n += 1;
}
return n;
}
+/// #1545: a verification item the user moved away from is parked, which ends
+/// the obligation to finish it but is never evidence that the task verified.
+fn cancelledVerification(agent: *const Agent) bool {
+ const epoch = goal_state.currentEpoch(agent.goal);
+ for (agent.todos.items) |t| {
+ if (t.epoch != epoch or t.retired) continue;
+ if (std.mem.eql(u8, t.status, "cancelled") and isVerification(t.content)) return true;
+ }
+ return false;
+}
+
pub fn hasUnresolved(agent: *const Agent) bool {
return unresolvedCount(agent) > 0;
}
@@ -33,7 +44,7 @@ pub fn completionGate(arena: Allocator, agent: *Agent) !?[]const u8 {
if (agent.review_mode or agent.sub) return goal_state.completionGate(arena, agent);
if (hasUnresolved(agent)) {
const rendered = goal_state.renderTodos(agent, goal_state.currentEpoch(agent.goal));
- return try std.fmt.allocPrint(arena, "completion deferred: {d} unresolved verification item(s) remain:\n{s}\nDropping or summarizing them does not satisfy completion. Finish the verification, or have the user change scope.", .{ unresolvedCount(agent), rendered });
+ return try std.fmt.allocPrint(arena, "completion deferred: {d} unresolved verification item(s) remain:\n{s}\nDropping or summarizing them does not satisfy completion. Finish the verification, or have the user change scope. If a newer user message already moved away from this work, set those items to status \"cancelled\" with todo_write (allowed only after a newer user message).", .{ unresolvedCount(agent), rendered });
}
return goal_state.completionGate(arena, agent);
}
@@ -42,7 +53,7 @@ pub fn completionGate(arena: Allocator, agent: *Agent) !?[]const u8 {
/// goal/eval/verification obligation is vacuously verified so recipe
/// telemetry for chat turns stays usable.
pub fn taskVerified(agent: *const Agent) bool {
- if (hasUnresolved(agent)) return false;
+ if (hasUnresolved(agent) or cancelledVerification(agent)) return false;
if (agent.eval_cmd != null) return agent.eval_verified and !agent.eval_repair_pending;
if (goal_state.goalActive(@constCast(agent))) {
const epoch = goal_state.currentEpoch(agent.goal);
@@ -59,6 +70,7 @@ fn todoRoot(arena: Allocator) Agent {
root.sub = false;
root.review_mode = false;
root.todos = .empty;
+ root.messages = std.json.Array.init(arena); // #1545: applyTodoWrite fingerprints the latest user request
root.goal = null;
root.todos_dirty = false;
root.completion_gate_armed = false;
@@ -150,3 +162,69 @@ test "taskVerified is true for ordinary chat and false with open verification" {
root.eval_verified = false;
try std.testing.expect(!taskVerified(&root));
}
+
+test "#1545 a verification item cannot be cancelled within the request that wrote it" {
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+ const ar = arena_state.allocator();
+ var root = todoRoot(ar);
+ const textMessage = @import("messages.zig").textMessage;
+ try root.messages.append(try textMessage(ar, "user", "implement the feature and run the tests"));
+ _ = try goal_todo.applyTodoWrite(&root, try todosArg(ar,
+ \\{"todos":[{"content":"write the helper","status":"completed"},
+ \\ {"content":"run the tests","status":"pending"}]}
+ ));
+ // Same request, no user message since: the #844 obligation stands.
+ const refused = try goal_todo.applyTodoWrite(&root, try todosArg(ar,
+ \\{"todos":[{"content":"run the tests","status":"cancelled"}]}
+ ));
+ try std.testing.expect(std.mem.indexOf(u8, refused.reply(), "were not cancelled") != null);
+ try std.testing.expectEqual(@as(usize, 1), unresolvedCount(&root));
+ const gate = (try completionGate(ar, &root)).?;
+ try std.testing.expect(std.mem.indexOf(u8, gate, "cancelled") != null); // the gate names the way out
+}
+
+test "#1545 after a newer user message, a cancelled verification item is parked, not owed and not verified" {
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+ const ar = arena_state.allocator();
+ var root = todoRoot(ar);
+ const textMessage = @import("messages.zig").textMessage;
+ try root.messages.append(try textMessage(ar, "user", "implement the feature and run the tests"));
+ _ = try goal_todo.applyTodoWrite(&root, try todosArg(ar,
+ \\{"todos":[{"content":"write the helper","status":"completed"},
+ \\ {"content":"run the tests","status":"pending"}]}
+ ));
+ // The user changes scope; the old verification is no longer the task.
+ try root.messages.append(try textMessage(ar, "user", "stop that, file an issue about it instead"));
+ const res = try goal_todo.applyTodoWrite(&root, try todosArg(ar,
+ \\{"todos":[{"content":"run the tests","status":"cancelled"},
+ \\ {"content":"file the issue","status":"completed"}]}
+ ));
+ try std.testing.expect(std.mem.indexOf(u8, res.reply(), "were not cancelled") == null);
+ try std.testing.expect(std.mem.indexOf(u8, res.reply(), "1 cancelled") != null);
+ try std.testing.expect(std.mem.indexOf(u8, res.text, "[-] run the tests") != null);
+ try std.testing.expectEqual(@as(usize, 0), unresolvedCount(&root)); // no longer blocks completion
+ try std.testing.expect(!taskVerified(&root)); // ...but never reads as verified
+ // Omitted later, the cancelled item stays as history instead of reviving the obligation.
+ _ = try goal_todo.applyTodoWrite(&root, try todosArg(ar,
+ \\{"todos":[{"content":"file the issue","status":"completed"}]}
+ ));
+ try std.testing.expectEqual(@as(usize, 0), unresolvedCount(&root));
+}
+
+test "#1545 ordinary work can be cancelled any time; a brand-new verification item cannot start cancelled" {
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+ const ar = arena_state.allocator();
+ var root = todoRoot(ar);
+ const textMessage = @import("messages.zig").textMessage;
+ try root.messages.append(try textMessage(ar, "user", "ship it"));
+ const res = try goal_todo.applyTodoWrite(&root, try todosArg(ar,
+ \\{"todos":[{"content":"sketch an alternative","status":"cancelled"},
+ \\ {"content":"verify the fix","status":"cancelled"}]}
+ ));
+ try std.testing.expect(std.mem.indexOf(u8, res.text, "[-] sketch an alternative") != null);
+ try std.testing.expect(std.mem.indexOf(u8, res.text, "[ ] verify the fix") != null);
+ try std.testing.expectEqual(@as(usize, 1), unresolvedCount(&root));
+}
diff --git a/src/schema.zig b/src/schema.zig
index 486d7fbb..dbd663d3 100644
--- a/src/schema.zig
+++ b/src/schema.zig
@@ -103,9 +103,9 @@ pub const base_specs = [_]ToolSpec{
const meta_specs = [_]ToolSpec{
.{
.name = "todo_write",
- .desc = "Replace the task list for the current standing goal (items from a replaced, cleared or completed goal are parked - kept in the session, but not part of this list and not replaced by this call). An item you already marked completed is kept as history when you leave it out, for as long as the current request is being worked; list it again (with any status) to change or remove it that way. Once every item is completed, the next thing the user asks starts a clean list - finished work is not carried into it. Omitted pending/in_progress items are dropped. Every call needs at least one item: a call with none leaves the list untouched. Use to plan and track multi-step work; skip it for trivial single-step tasks. Each item has content and status (pending|in_progress|completed). Write steps small enough to verify individually; keep exactly ONE item in_progress at a time — mark it in_progress when you start and completed as it lands, never a batch flipped at the end.",
+ .desc = "Replace the task list for the current standing goal (items from a replaced, cleared or completed goal are parked - kept in the session, but not part of this list and not replaced by this call). An item you already marked completed is kept as history when you leave it out, for as long as the current request is being worked; list it again (with any status) to change or remove it that way. Once every item is completed, the next thing the user asks starts a clean list - finished work is not carried into it. Omitted pending/in_progress items are dropped. Every call needs at least one item: a call with none leaves the list untouched. Use to plan and track multi-step work; skip it for trivial single-step tasks. Each item has content and status (pending|in_progress|completed|cancelled). Use cancelled only for work the user has moved away from: it parks the item without counting it as done, and a verification item can be cancelled only after a newer user message changes scope. Write steps small enough to verify individually; keep exactly ONE item in_progress at a time — mark it in_progress when you start and completed as it lands, never a batch flipped at the end.",
.schema =
- \\{"type": "object", "properties": {"todos": {"type": "array", "items": {"type": "object", "properties": {"content": {"type": "string"}, "status": {"type": "string", "enum": ["pending", "in_progress", "completed"]}}, "required": ["content", "status"]}}}, "required": ["todos"]}
+ \\{"type": "object", "properties": {"todos": {"type": "array", "items": {"type": "object", "properties": {"content": {"type": "string"}, "status": {"type": "string", "enum": ["pending", "in_progress", "completed", "cancelled"]}}, "required": ["content", "status"]}}}, "required": ["todos"]}
,
},
.{
diff --git a/src/todo_reply.zig b/src/todo_reply.zig
index b62b1bec..a0b7f221 100644
--- a/src/todo_reply.zig
+++ b/src/todo_reply.zig
@@ -14,19 +14,24 @@ pub fn text(arena: Allocator, todos: []const TodoItem, epoch: u64, incoming: []c
var done: usize = 0;
var doing: usize = 0;
var open: usize = 0;
+ var cancelled: usize = 0;
for (todos) |t| {
if (t.epoch != epoch or t.retired) continue;
if (std.mem.eql(u8, t.status, "completed")) {
done += 1;
} else if (std.mem.eql(u8, t.status, "in_progress")) {
doing += 1;
+ } else if (std.mem.eql(u8, t.status, "cancelled")) {
+ cancelled += 1; // #1545: parked after a scope change, not pending work
} else {
open += 1;
}
}
var aw: std.Io.Writer.Allocating = .init(arena);
const w = &aw.writer;
- try w.print("Todo list saved: {d} done, {d} in progress, {d} pending.", .{ done, doing, open });
+ try w.print("Todo list saved: {d} done, {d} in progress, {d} pending", .{ done, doing, open });
+ if (cancelled > 0) try w.print(", {d} cancelled", .{cancelled});
+ try w.writeAll(".");
var kept_header = false;
for (todos) |t| {
if (t.epoch != epoch or t.retired or mentions(incoming, t.content)) continue;
@@ -42,6 +47,7 @@ pub fn text(arena: Allocator, todos: []const TodoItem, epoch: u64, incoming: []c
fn mark(status: []const u8) []const u8 {
if (std.mem.eql(u8, status, "completed")) return "[x]";
if (std.mem.eql(u8, status, "in_progress")) return "[~]";
+ if (std.mem.eql(u8, status, "cancelled")) return "[-]";
return "[ ]";
}