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 standing goal's double-check re-arms; --goal re-activates on resume

Rach Pradhan284d249888b9
diff --git a/src/agent_compact.zig b/src/agent_compact.zig
index d8f6a4b2..071b4705 100644
--- a/src/agent_compact.zig
+++ b/src/agent_compact.zig
@@ -420,8 +420,14 @@ pub fn emergencyTrim(self: *Agent) usize {
}
// #163: no clean user turn to cut at (a runaway tool loop). Don't wedge the
// session — reclaim context by truncating the oldest tool outputs in place,
- // keeping every call/output pair valid. Nonzero = recovered.
- return if (trimOldestToolOutputs(self) > 0) 1 else 0;
+ // keeping every call/output pair valid. Nonzero = recovered. This too is a
+ // rewrite: the stubbed outputs may include the last todo_write render the
+ // suppressed /loop note points the model at (#318).
+ if (trimOldestToolOutputs(self) > 0) {
+ self.history_rewrites +%= 1;
+ return 1;
+ }
+ return 0;
}
/// Auto-compaction with recovery. compact() summarizes the whole history in
diff --git a/src/agent_tools.zig b/src/agent_tools.zig
index 4c5bdcc2..9fec6d6f 100644
--- a/src/agent_tools.zig
+++ b/src/agent_tools.zig
@@ -287,6 +287,7 @@ pub fn handleMeta(self: *Agent, call: ToolCall) !ExecResult {
"completion blocked: the latest verifier contradicted the plan; repair the failure and run eval until it meets the target"
else
"completion blocked: workspace state is not verified; run eval and meet the target after the final change";
+ self.completion_refused = true; // blocked, not silence: attempt_completion skips the tool counter, and /loop must give the model a turn to run eval (#318)
return .{ .text = message, .is_error = true };
}
if (try goal_state.completionGate(self.arena, self)) |refusal| {
diff --git a/src/goal_flow.zig b/src/goal_flow.zig
index 8aba6fa0..ba9b861e 100644
--- a/src/goal_flow.zig
+++ b/src/goal_flow.zig
@@ -41,7 +41,17 @@ pub fn standingGoalFromFlag(objective: []const u8, old: ?agent_mod.Goal, todos:
/// items the returned note must be queued (pending_goal_note, always null right
/// after a load) so neither the model nor the user loses the boundary silently.
pub fn reapplyFlagGoal(arena: Allocator, root: *Agent, objective: []const u8, now_ms: i64) !?[]const u8 {
- if (root.goal) |g| if (g.standing and std.mem.eql(u8, g.objective, objective)) return null;
+ if (root.goal) |*g| {
+ if (g.standing and std.mem.eql(u8, g.objective, objective)) {
+ // Same policy, same goal - but the flag passed TODAY outranks a saved
+ // pause: --goal on the command line means steer. Without this, a
+ // paused standing goal came back paused and the flag produced no
+ // steering and no completion gate at all, silently (#318).
+ g.status = .active;
+ g.updated_ms = now_ms;
+ return null;
+ }
+ }
const displaced: ?agent_mod.Goal = if (root.goal) |old| (if (old.status == .complete) null else old) else null;
root.goal = standingGoalFromFlag(objective, root.goal, root.todos.items, now_ms);
root.goal_note_fp = 0; // re-state the (now standing) note on the next turn
@@ -228,6 +238,7 @@ fn flowRoot(arena: Allocator) Agent {
root.goal_note_fp = 0;
root.pending_goal_note = null;
root.history_rewrites = 0;
+ root.tool_calls_this_turn = 0;
return root;
}
@@ -257,9 +268,12 @@ test "a --goal standing objective outlives the model's own completion (#318)" {
try std.testing.expectEqual(agent_mod.GoalStatus.active, root.goal.?.status);
try std.testing.expect(root.completed != null);
try std.testing.expect(goal_state.goalActive(&root));
+ // The accepted claim CONSUMED the arm: a goal that never retires must not
+ // ride one refusal forever, or every later claim passes unchecked.
+ try std.testing.expect(!root.completion_gate_armed);
- // Open work refuses with the standing consequence: the items stay active.
- goal_state.resetCompletionGate(&root);
+ // So the NEXT claim starts a fresh double-check cycle, no reset needed:
+ // open work refuses with the standing consequence (the items stay active).
try root.todos.append(ar, .{ .content = "an open item", .status = "pending", .epoch = 1 });
const r2 = try goal_state.completionGate(ar, &root);
try std.testing.expect(r2 != null and std.mem.indexOf(u8, r2.?, "stay active") != null);
@@ -543,6 +557,14 @@ test "re-applying --goal on resume keeps the same objective's own checklist (#31
try std.testing.expectEqual(@as(u64, 1), root.goal.?.epoch);
try std.testing.expectEqual(@as(i64, 7), root.goal.?.created_ms); // the restored goal, not a re-mint
try std.testing.expectEqualStrings("[x] a\n[ ] b", goal_state.renderCurrent(&root));
+ // A saved pause does not survive the flag: --goal typed TODAY means steer.
+ // Restored verbatim, a paused standing goal produced no steering and no
+ // completion gate at all, silently, in exactly the flag's headless flows.
+ root.goal.?.status = .paused;
+ try std.testing.expect((try reapplyFlagGoal(ar, &root, "keep the tree green", 10)) == null);
+ try std.testing.expectEqual(agent_mod.GoalStatus.active, root.goal.?.status);
+ try std.testing.expectEqual(@as(u64, 1), root.goal.?.epoch); // still the same goal, not a re-mint
+ try std.testing.expect(goal_state.goalActive(&root));
// A DIFFERENT flag objective displaces a live restored /goal like any
// supersession: new standing goal above every epoch, and the note reports
diff --git a/src/goal_persist_tests.zig b/src/goal_persist_tests.zig
index 2823ed56..f422c811 100644
--- a/src/goal_persist_tests.zig
+++ b/src/goal_persist_tests.zig
@@ -374,3 +374,26 @@ test "/loop: a refused completion is work, and a leftover complete goal decides
root.tool_calls_this_turn = 0;
try std.testing.expectEqual(repl_glue.ContinuationOutcome.idle, goal_flow.loopTurnDecision(&root, 25).stop);
}
+
+test "a fresh /loop cannot stop accepted off a checklist finished before it (#318)" {
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+ const ar = arena_state.allocator();
+ var root = blankRoot(ar);
+ // Run A drove the standing goal's checklist to all-done (todos_dirty=true)
+ // and stopped accepted. The goal never retires, so its epoch stays live and
+ // allDone stays true into run B.
+ root.goal = goal_flow.standingGoalFromFlag("keep the tree green", null, root.todos.items, 7);
+ try root.todos.append(ar, .{ .content = "done last run", .status = "completed", .epoch = 1 });
+ root.todos_dirty = true;
+ root.tool_calls_this_turn = 1;
+ // Stale evidence reads as work_done -> accepted on B's very first decision...
+ try std.testing.expectEqual(repl_glue.ContinuationOutcome.accepted, goal_flow.loopTurnDecision(&root, 25).stop);
+ // ...which is why mainloop clears todos_dirty when a FRESH /loop arms: the
+ // new prompt starts with no completion evidence of its own, and only a
+ // todo_write (or attempt_completion) made DURING the run can stop it.
+ root.todos_dirty = false;
+ try std.testing.expect(std.meta.activeTag(goal_flow.loopTurnDecision(&root, 25)) == .continue_turn);
+ goal_state.noteTodoWrite(&root); // the model re-plans and finishes in-run
+ try std.testing.expectEqual(repl_glue.ContinuationOutcome.accepted, goal_flow.loopTurnDecision(&root, 25).stop);
+}
diff --git a/src/goal_state.zig b/src/goal_state.zig
index ad03a3c8..b238122d 100644
--- a/src/goal_state.zig
+++ b/src/goal_state.zig
@@ -169,7 +169,15 @@ pub fn reconcileRestored(root: *Agent) bool {
/// can (#318). Pure (no Io) so the rule is unit-tested; the caller passes the
/// clock reading.
pub fn retireOnCompletion(root: *Agent, now_ms: i64) bool {
- if (!goalActive(root) or root.goal.?.standing) return false;
+ if (!goalActive(root)) return false;
+ if (root.goal.?.standing) {
+ // The accepted claim consumed this double-check cycle. A /goal objective
+ // retires here, making its arm moot; a standing goal lives on, so without
+ // this reset the arm stayed stuck and every LATER claim passed unchecked -
+ // the unearned accepted, displaced by one cycle (#318).
+ resetCompletionGate(root);
+ return false;
+ }
root.goal.?.status = .complete;
root.goal.?.updated_ms = now_ms;
return true;
diff --git a/src/mainloop.zig b/src/mainloop.zig
index 418587ee..cc5af8b5 100644
--- a/src/mainloop.zig
+++ b/src/mainloop.zig
@@ -114,6 +114,9 @@ pub fn run(ctx: *Ctx) !void {
std.mem.trim(u8, line["/loop".len..], " \t")
else
null;
+ // A checklist finished BEFORE this /loop is not evidence its prompt is done:
+ // stale todos_dirty ended a standing goal's next run at iteration 1 as accepted (#318).
+ if (loop_prompt != null and !is_loop_continuation) ctx.root.todos_dirty = false;
var review_prompt: ?[]const u8 = if (!main_mod.json_mode) review.promptFromLine(line) else null;
// /goal <objective>: set the standing goal AND run it as the first turn
// right away — codex/opencode both start the loop on a goal instead of