graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
feat(loop): controller-authorized /loop continuation gated on goal state (#226)
Rach Pradhan47159c43faff
diff --git a/src/agent.zig b/src/agent.zigindex 9fb27ded..fe8d4922 100644--- a/src/agent.zig+++ b/src/agent.zig@@ -77,9 +77,9 @@ pub const TodoItem = struct {status: []const u8,};-/// Governed-run status for a standing /goal (#223). Frozen enum — `budget_limited`-/// is reserved for #224's per-goal budget accounting. Only `.active` steers turns.-pub const GoalStatus = enum { active, paused, blocked, complete, budget_limited };+/// Governed-run status for a standing /goal (#223). Only `.active` steers turns;+/// pause/resume and the /loop continuation gate (#226) key off the others.+pub const GoalStatus = enum { active, paused, blocked, complete };/// A structured standing objective: the /goal text plus its lifecycle status and/// created/updated timestamps. Replaces the bare `?[]const u8` so /goal candiff --git a/src/mainloop.zig b/src/mainloop.zigindex 4a2b9d1b..a9330f2b 100644--- a/src/mainloop.zig+++ b/src/mainloop.zig@@ -94,6 +94,15 @@ fn applyAiTitle(ctx: *Ctx, f: *Io.Future(?[]const u8)) void {}}+/// #226: true only when the model has a live checklist and every item is marked+/// completed — a "work asserted done" signal for the /loop continuation gate+/// (alongside attempt_completion / root.completed). An empty checklist is NOT done.+fn allTodosDone(root: *agent_mod.Agent) bool {+ if (root.todos.items.len == 0) return false;+ for (root.todos.items) |t| if (!std.mem.eql(u8, t.status, "completed")) return false;+ return true;+}+/// The interactive-REPL / --json-protocol turn loop itself. Runs until EOF,/// `exit`/`quit`/`q`, or a plain `break` out of the raw-line read. main()/// resumes right after this returns and does its own final-save/worktree@@ -104,6 +113,14 @@ pub fn run(ctx: *Ctx) !void {var prev_turn_id: u64 = 0;var prev_prompt_fp: [16]u8 = scoring.promptFingerprint(ctx.root.systemPrompt());+ // #226: /loop controller-authorized continuation state. `loop_continue_armed`+ // is set only after a CLEANLY-COMPLETED autonomous /loop turn and consumed at+ // the next readline — so any interrupt/error (which `continue`s past the arming+ // site) auto-stops the loop with no per-error-branch handling.+ const loop_iter_cap: u32 = 25; // hard per-/loop iteration bound (never-completing-model guard)+ var loop_iters_left: u32 = 0; // continuation turns still authorized this /loop run+ var loop_continue_armed = false; // a continuation turn is queued for the next readline+while (true) {// Steering drain: prompts typed while the previous turn streamed// were captured into g_steer_queue. Run them now, one after@@ -112,7 +129,10 @@ pub fn run(ctx: *Ctx) !void {repl_glue.resetSteerPartial();const steer_entry: ?repl_glue.SteerEntry = repl_glue.popSteer();defer if (steer_entry) |e| std.heap.page_allocator.free(e.text);+ var is_loop_continuation = false; // #226: this iteration is an autonomous /loop continuation turnconst raw_line: []const u8 = if (steer_entry) |e| blk: {+ loop_iters_left = 0; // #226: a user steer/force cancels the autonomous /loop run+ loop_continue_armed = false;if (e.force) {try ctx.out.print("{s}↳ force ›{s} {s}\n", .{ style.yellow, style.reset, e.text });} else {@@ -120,6 +140,15 @@ pub fn run(ctx: *Ctx) !void {}try ctx.out.flush();break :blk e.text;+ } else if (loop_continue_armed) blk: {+ // #226: autonomous /loop continuation — synthesize the next turn from the+ // continuation steering note instead of reading a new user line. Consumed+ // here, so an interrupted/errored turn does not resume the loop.+ loop_continue_armed = false;+ is_loop_continuation = true;+ const todos_render = if (ctx.root.todos.items.len > 0) ctx.root.renderTodos() else "";+ const note = try repl_glue.continuationSteeringNote(ctx.arena, todos_render);+ break :blk try std.fmt.allocPrint(ctx.arena, "/loop {s}", .{note});} else if (ctx.interactive) blk: {try ctx.root.prompt();break :blk (try readline.readLine(ctx.root, ctx.in, ctx.out, ctx.gpa, ctx.history, ctx.linebuf)) orelse break;@@ -708,6 +737,35 @@ pub fn run(ctx: *Ctx) !void {const near_cap = ctx.root.provider.context > 0 and ctx.root.last_context_tokens * 100 >= ctx.root.provider.context * 95;ctx.root.compactOrRecover(near_cap);}++ // #226: /loop controller-authorized continuation. After a cleanly-+ // completed autonomous /loop turn the CONTROLLER decides whether to run+ // another turn — not the model merely stopping. Keep going while the goal+ // is active and the work isn't asserted done (attempt_completion set+ // root.completed, or the checklist is finished); otherwise stop with a+ // NAMED terminal outcome. `loop_continue_armed` is consumed at the next+ // readline, so an interrupted/errored turn (which `continue`s past here)+ // never resumes the loop.+ if (loop_prompt != null and !main_mod.json_mode) {+ if (!is_loop_continuation) loop_iters_left = loop_iter_cap; // fresh /loop run: arm the bound+ const model_done = ctx.root.completed != null or allTodosDone(ctx.root);+ const gstatus: agent_mod.GoalStatus = if (ctx.root.goal) |g| g.status else .active;+ switch (repl_glue.continuationDecision(gstatus, model_done, loop_iters_left)) {+ .continue_turn => {+ loop_iters_left -= 1; // consume one credit for the queued continuation+ loop_continue_armed = true;+ },+ .stop => |outcome| {+ loop_iters_left = 0;+ loop_continue_armed = false;+ if (outcome == .accepted) if (ctx.root.goal) |*g| {+ if (g.status == .active) g.status = .complete; // the loop drove the goal to done+ };+ try ctx.out.print("{s}↩ /loop stopped — {s}{s}\n", .{ style.dim, @tagName(outcome), style.reset });+ try ctx.out.flush();+ },+ }+ }// opencode-style continuous autosave: persist after every turn so a// crash or quit never loses the thread — last.session.json, the same// file /resume reads. Best-effort; a write failure never breaks the loop.diff --git a/src/repl_glue.zig b/src/repl_glue.zigindex 4656d2c4..799de049 100644--- a/src/repl_glue.zig+++ b/src/repl_glue.zig@@ -169,6 +169,77 @@ test "goalSteeringNote: active-only gate + checklist assembly, no (no todos) leatry std.testing.expect(std.mem.indexOf(u8, n2, "Checklist so far:\n[x] wire steering\n[ ] add test") != null);}+/// #226 continuation gate — the outcome when a /loop turn finishes: the loop+/// either runs another turn or stops with a NAMED terminal state (surfaced in+/// the transcript so #219's ledger can later record it verbatim). Budget-free.+pub const ContinuationOutcome = enum {+ accepted, // the goal's checklist is complete (or the goal itself is complete)+ exhausted, // hit the hard per-/loop iteration bound with work still open+ blocked, // the goal is blocked and needs the user+ cancelled, // the goal was paused — the user stepped in+};++pub const ContinuationDecision = union(enum) {+ continue_turn, // run another /loop turn without reading a new user line+ stop: ContinuationOutcome, // return control to the prompt with this named outcome+};++/// 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.+pub fn continuationDecision(+ goal_status: agent_mod.GoalStatus,+ todos_all_completed: bool,+ iters_left: u32,+) ContinuationDecision {+ if (todos_all_completed) return .{ .stop = .accepted };+ switch (goal_status) {+ .paused => return .{ .stop = .cancelled },+ .blocked => return .{ .stop = .blocked },+ .complete => return .{ .stop = .accepted },+ .active => {},+ }+ if (iters_left == 0) return .{ .stop = .exhausted };+ return .continue_turn;+}++/// The steering appended to each autonomous /loop continuation turn (the+/// continuation_steering_item analog): keep working the checklist, verify, and+/// stop only when done or blocked — not a per-turn user note.+pub fn continuationSteeringNote(arena: Allocator, todos_render: []const u8) ![]const u8 {+ if (todos_render.len == 0)+ return "[continuing autonomously (/loop): make the next concrete step toward the goal, then verify it. Do not ask for confirmation between routine steps. Stop only when the work is complete or you hit a blocker that needs the user.]";+ return std.fmt.allocPrint(arena, "[continuing autonomously (/loop): keep working the checklist below — do the next incomplete item, mark it in_progress then completed, and verify. Do not ask for confirmation between routine steps. Stop only when every item is done or you are blocked.\n\nChecklist so far:\n{s}]", .{todos_render});+}++test "continuationDecision: one assertion per branch (#226)" {+ // todos all complete -> accepted, regardless of status/iters.+ try std.testing.expectEqual(ContinuationOutcome.accepted, continuationDecision(.active, true, 5).stop);+ // active + open todos + iterations left -> continue.+ try std.testing.expect(std.meta.activeTag(continuationDecision(.active, false, 5)) == .continue_turn);+ // active + open todos + iteration bound spent -> exhausted.+ try std.testing.expectEqual(ContinuationOutcome.exhausted, continuationDecision(.active, false, 0).stop);+ // paused -> cancelled (the user stepped in).+ try std.testing.expectEqual(ContinuationOutcome.cancelled, continuationDecision(.paused, false, 5).stop);+ // blocked -> blocked.+ try std.testing.expectEqual(ContinuationOutcome.blocked, continuationDecision(.blocked, false, 5).stop);+ // complete -> accepted.+ try std.testing.expectEqual(ContinuationOutcome.accepted, continuationDecision(.complete, false, 5).stop);+}++test "continuationSteeringNote: renders checklist when present, generic otherwise (#226)" {+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena.deinit();+ const ar = arena.allocator();+ const empty = try continuationSteeringNote(ar, "");+ try std.testing.expect(std.mem.indexOf(u8, empty, "Checklist so far") == null);+ try std.testing.expect(std.mem.indexOf(u8, empty, "continuing autonomously") != null);+ const withlist = try continuationSteeringNote(ar, "[ ] add test");+ try std.testing.expect(std.mem.indexOf(u8, withlist, "Checklist so far:\n[ ] add test") != null);+}+/// repl.TurnFn — run a full ROOT agent turn (tools + MCP) for `graff repl`, so/// the model can read files, run bash, search the codebase, etc. — not a bare/// completion. Auto-approves tools (yolo: the chat repl has no permission UI),