Skip to content

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

Python

fix(loop): a retired goal does not govern the next /loop run

Rach Pradhan1fcada72742c
diff --git a/src/goal_flow.zig b/src/goal_flow.zig
index de40b8be..32de98fb 100644
--- a/src/goal_flow.zig
+++ b/src/goal_flow.zig
@@ -9,6 +9,7 @@ const std = @import("std");
const Allocator = std.mem.Allocator;
const agent_mod = @import("agent.zig");
const goal_state = @import("goal_state.zig");
+const repl_glue = @import("repl_glue.zig");
const Agent = agent_mod.Agent;
const TodoItem = agent_mod.TodoItem;
@@ -117,6 +118,24 @@ test "--goal seeded onto a resumed session lands above every restored epoch (#31
try std.testing.expectEqual(@as(usize, 2), root.todos.items.len); // and nothing was deleted
}
+test "a leftover complete goal cannot end a fresh /loop at iteration 1 (#318)" {
+ // After a resume reconciliation - or any earlier run that retired the goal -
+ // root.goal is .complete while a NEW /loop starts. The controller read the
+ // status before the work and stopped at its first decision as `accepted`, so
+ // a run that had done nothing reported success.
+ const zero_tools = repl_glue.turnStopped(0, false);
+ try std.testing.expect(zero_tools);
+ try std.testing.expectEqual(repl_glue.ContinuationOutcome.idle, repl_glue.continuationDecision(.complete, false, zero_tools, 25).stop);
+ // Real work under the same leftover goal still earns accepted, and an
+ // ongoing run keeps its iterations - the status decides nothing either way.
+ try std.testing.expectEqual(repl_glue.ContinuationOutcome.accepted, repl_glue.continuationDecision(.complete, true, zero_tools, 25).stop);
+ try std.testing.expect(std.meta.activeTag(repl_glue.continuationDecision(.complete, false, false, 25)) == .continue_turn);
+ try std.testing.expectEqual(repl_glue.ContinuationOutcome.exhausted, repl_glue.continuationDecision(.complete, false, false, 0).stop);
+ // paused/blocked are the user's business and are untouched.
+ try std.testing.expectEqual(repl_glue.ContinuationOutcome.cancelled, repl_glue.continuationDecision(.paused, false, false, 25).stop);
+ try std.testing.expectEqual(repl_glue.ContinuationOutcome.blocked, repl_glue.continuationDecision(.blocked, false, false, 25).stop);
+}
+
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();
diff --git a/src/repl_glue.zig b/src/repl_glue.zig
index 8925e0ed..78e4abac 100644
--- a/src/repl_glue.zig
+++ b/src/repl_glue.zig
@@ -224,10 +224,10 @@ pub const ContinuationDecision = union(enum) {
/// Pure controller decision for /loop continuation (#226): continuation is
/// authorized by CONTROLLER STATE, never by the model merely stopping. An active
/// goal with the checklist still open and iterations left keeps going; a
-/// completed checklist, a paused/blocked/complete goal, or a spent iteration
-/// bound each yield the matching named terminal outcome. No budgets. `accepted`
-/// is reserved for real evidence of completion: a turn that merely did nothing
-/// stops as `idle` (#318), so the transcript label never overstates the outcome.
+/// paused or blocked goal, or a spent iteration bound, yields the matching
+/// named terminal outcome. No budgets. `accepted` is reserved for real evidence
+/// of completion: a turn that merely did nothing stops as `idle` (#318), and a
+/// goal that was already complete when the run started decides nothing at all.
pub fn continuationDecision(
goal_status: agent_mod.GoalStatus,
work_done: bool,
@@ -238,8 +238,12 @@ pub fn continuationDecision(
switch (goal_status) {
.paused => return .{ .stop = .cancelled },
.blocked => return .{ .stop = .blocked },
- .complete => return .{ .stop = .accepted },
- .active => {},
+ // A goal completed DURING this run already stopped it through work_done
+ // (attempt_completion sets root.completed) or through mainloop's
+ // flip-then-stop, so .complete here is a LEFTOVER objective from an
+ // earlier run or a resume reconciliation. It must not label a fresh
+ // /loop `accepted` at iteration 1 before anything happened (#318).
+ .complete, .active => {},
}
if (model_stopped) return .{ .stop = .idle };
if (iters_left == 0) return .{ .stop = .exhausted };
@@ -275,8 +279,9 @@ test "continuationDecision: one assertion per branch (#226)" {
try std.testing.expectEqual(ContinuationOutcome.cancelled, continuationDecision(.paused, false, false, 5).stop);
// blocked -> blocked.
try std.testing.expectEqual(ContinuationOutcome.blocked, continuationDecision(.blocked, false, false, 5).stop);
- // complete -> accepted.
- try std.testing.expectEqual(ContinuationOutcome.accepted, continuationDecision(.complete, false, false, 5).stop);
+ // a LEFTOVER complete goal governs nothing: work decides, exactly as .active.
+ try std.testing.expect(std.meta.activeTag(continuationDecision(.complete, false, false, 5)) == .continue_turn);
+ try std.testing.expectEqual(ContinuationOutcome.accepted, continuationDecision(.complete, true, false, 5).stop);
}
test "a zero-tool turn stops as idle, not accepted; a refused completion keeps the loop (#318)" {