graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
feat(goal): /goal and /loop are one autonomous run
Rach Pradhan010b9ad682b9
diff --git a/README.md b/README.mdindex d672a1c4..c1ae4e68 100644--- a/README.md+++ b/README.md@@ -464,8 +464,8 @@ other animation names remain available./fast toggle Codex priority service tier for lower latency/ultracode toggle persistent multi-agent workflow mode/animation choose a thinking animation (`/animation` lists every option)-/goal [text] set/show a standing objective; /goal pause|resume|status|clear-/loop [30m] <prompt> work autonomously toward the prompt, stopping with a named outcome; an optional 30s/30m/2h budget paces the run+/goal [30m] <text> set a standing objective and work it autonomously; /goal pause|resume|status|clear+/loop [30m] <text> the same autonomous run, without adopting a standing objective/review <target> one read-only review pass; no edits, delegation, workflows, or web access/rewind [n] list past prompts; /rewind <n> drops prompt n+after & reverts its file edits/image <path> attach an image to your next message (vision models only)@@ -484,19 +484,21 @@ exit | /exit | ctrl-d | ctrl-c(empty) quit`/plan`, `/yolo`, and `/strict` change how the permission gate behaves for thesession. See [Permission modes](#permission-modes).-`/goal` sets a standing objective that steers every turn as a live checklist.-`/goal pause` stops the steering without losing the objective, `/goal resume`-turns it back on, and `/goal status` shows the objective and its current state.-`/loop <prompt>` works autonomously toward the prompt, running turn after turn-(plan, act, verify) instead of pausing for confirmation between routine steps,-and stops on its own with a named outcome: accepted once the work is done, idle-when the model stops making tool progress without claiming completion, cancelled-or blocked when you step in or it needs you, exhausted when a safety limit is-hit, and expired when a time budget runs out.-Start the prompt with a duration to give the run one: `/loop 30m fix the flaky-test` (also `45s`, `2h`). Each continuation turn then tells the model where it-stands: which continuation it is on, how long the run has taken, how much of the-budget is left, and one phase hint (explore, implement, finish, wrap up).+`/goal <objective>` sets a standing objective that steers every turn as a live+checklist, and starts working it right away: it runs turn after turn (plan, act,+verify) instead of pausing for confirmation between routine steps. `/goal pause`+stops the steering without losing the objective, `/goal resume` turns it back on,+and `/goal status` shows the objective and its current state.+`/loop <prompt>` is the same autonomous run for a one-off task, without adopting+a standing objective.+Either way the run stops on its own with a named outcome: accepted once the work+is done, idle when the model stops making tool progress without claiming+completion, cancelled or blocked when you step in or it needs you, exhausted when+a safety limit is hit, and expired when a time budget runs out.+Start with a duration to give the run one: `/goal 30m fix the flaky test` (also+`45s`, `2h`). Each continuation turn then tells the model where it stands: which+continuation it is on, how long the run has taken, how much of the budget is+left, and one phase hint (explore, implement, finish, wrap up).Nothing is enforced except the stop itself, and the model is never cut offmid-turn. Subagents spawned during a timed run are told the parent's remainingtime, minus a margin for the parent to integrate their results.diff --git a/src/command_catalog.zig b/src/command_catalog.zigindex 2d7fc6df..9995b5fa 100644--- a/src/command_catalog.zig+++ b/src/command_catalog.zig@@ -26,8 +26,8 @@ pub const commands = [_]Item{.{ .name = "/clear", .desc = "wipe the conversation and start fresh" },.{ .name = "/new", .desc = "start a fresh autosaved session" },.{ .name = "/rename", .usage = "/rename <title>", .desc = "set the current session title" },- .{ .name = "/goal", .usage = "/goal [text|pause|resume|status|clear]", .desc = "set/show a standing objective (tracked as a checklist); pause/resume steering, status shows state, clear removes it" },- .{ .name = "/loop", .usage = "/loop [30m] <prompt>", .desc = "run an autonomous plan->act->verify pass; an optional 30s/30m/2h budget paces it and stops it" },+ .{ .name = "/goal", .usage = "/goal [30m] [text|pause|resume|status|clear]", .desc = "set a standing objective and work it autonomously; an optional 30s/30m/2h budget paces the run; pause/resume steering, status shows state, clear removes it" },+ .{ .name = "/loop", .usage = "/loop [30m] <prompt>", .desc = "the same autonomous run as /goal, without adopting a standing objective" },.{ .name = "/review", .usage = "/review <target or instructions>", .desc = "run one isolated read-only review pass; no edits, delegation, or workflows" },.{ .name = "/plan", .desc = "toggle plan mode: read-only explore + propose; writes/edits denied" },.{ .name = "/ultracode", .desc = "toggle persistent workflow mode; bare opens an on/off picker, or /ultracode on|off" },diff --git a/src/goal_pacing.zig b/src/goal_pacing.zigindex f9be77e2..a4c90717 100644--- a/src/goal_pacing.zig+++ b/src/goal_pacing.zig@@ -71,6 +71,49 @@ pub fn loopBudgetFromLine(line: []const u8) ?LoopBudget {return parseLoopBudget(line["/loop".len..]);}+/// One autonomous run, however it was asked for. `/goal <objective>` and+/// `/loop <prompt>` are the same machine: plan, act, verify, and let the+/// controller decide whether another turn is authorized. They differ in one+/// bit - whether the objective becomes STANDING - so they stopped being two+/// commands and became one with a flag. Both take the same optional duration+/// prefix (`/goal 30m ship it`).+pub const Autonomous = struct {+ /// The text the turn actually runs on, duration prefix removed.+ prompt: []const u8,+ deadline_ms_delta: ?i64 = null,+ /// The `/goal ...` command to apply before the first turn (duration+ /// stripped, so the objective never records "30m"). Null for `/loop`,+ /// which runs autonomously without adopting a standing objective.+ goal_line: ?[]const u8 = null,++ /// The wall-clock half, for arming the run.+ pub fn budget(self: Autonomous) LoopBudget {+ return .{ .deadline_ms_delta = self.deadline_ms_delta, .prompt = self.prompt };+ }+};++/// Parse an autonomous invocation off an input line. `/goal` with a lifecycle+/// word (pause/resume/status/clear) or bare is NOT one - those stay commands,+/// which is why the objective parse still lives in repl_glue.goalPromptFromLine+/// and is passed in here as `goal_objective`.+pub fn autonomousFromLine(arena: Allocator, line: []const u8, goal_objective: ?[]const u8) !?Autonomous {+ if (goal_objective) |obj| {+ const b = parseLoopBudget(obj);+ return .{+ .prompt = b.prompt,+ .deadline_ms_delta = b.deadline_ms_delta,+ // Rebuild the command only when a duration was stripped, so the+ // common path stays allocation-free and byte-identical.+ .goal_line = if (b.deadline_ms_delta == null)+ line+ else+ try std.fmt.allocPrint(arena, "/goal {s}", .{b.prompt}),+ };+ }+ const b = loopBudgetFromLine(line) orelse return null;+ return .{ .prompt = b.prompt, .deadline_ms_delta = b.deadline_ms_delta };+}+/// The wall clock of ONE /loop run: when it started and when it must stop./// Run-local like LoopListGate and never persisted - a fresh run, a user steer/// and a stop all reset it. The deadline is mirrored onto the Agent so adiff --git a/src/goal_pacing_autonomous_test.zig b/src/goal_pacing_autonomous_test.zignew file mode 100644index 00000000..3b168d14--- /dev/null+++ b/src/goal_pacing_autonomous_test.zig@@ -0,0 +1,79 @@+//! `/goal` and `/loop` are one autonomous run (goal_pacing.autonomousFromLine).+//! These pin the seam mainloop depends on: which lines start a run, which stay+//! commands, where the duration goes, and what the /goal command line becomes+//! once a duration has been stripped out of it.+//!+//! Kept out of goal_pacing.zig (line cap) and reached through the `test { _ =+//! ... }` hook in main.zig - without that line these silently never run.++const std = @import("std");+const goal_pacing = @import("goal_pacing.zig");+const repl_glue = @import("repl_glue.zig");++/// mainloop's exact call shape: the objective parse decides command-vs-run,+/// then autonomousFromLine turns whichever it was into one run descriptor.+fn parse(arena: std.mem.Allocator, line: []const u8) !?goal_pacing.Autonomous {+ return goal_pacing.autonomousFromLine(arena, line, repl_glue.goalPromptFromLine(line));+}++test "/goal and /loop produce the same run; only /goal adopts the objective" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const ar = arena_state.allocator();++ const g = (try parse(ar, "/goal ship phase 2")).?;+ try std.testing.expectEqualStrings("ship phase 2", g.prompt);+ try std.testing.expect(g.deadline_ms_delta == null);+ try std.testing.expectEqualStrings("/goal ship phase 2", g.goal_line.?); // the command runs first++ const l = (try parse(ar, "/loop ship phase 2")).?;+ try std.testing.expectEqualStrings("ship phase 2", l.prompt); // same prompt, same machine+ try std.testing.expect(l.goal_line == null); // but no standing objective is adopted+}++test "a duration prefix works on both, and never lands in the objective" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const ar = arena_state.allocator();++ const g = (try parse(ar, "/goal 30m fix the flaky test")).?;+ try std.testing.expectEqual(@as(?i64, 30 * std.time.ms_per_min), g.deadline_ms_delta);+ try std.testing.expectEqualStrings("fix the flaky test", g.prompt);+ // The rebuilt command is what /goal records: "30m" is the run's budget, not+ // part of what the user is trying to achieve.+ try std.testing.expectEqualStrings("/goal fix the flaky test", g.goal_line.?);+ try std.testing.expectEqualStrings("fix the flaky test", repl_glue.goalPromptFromLine(g.goal_line.?).?);++ const l = (try parse(ar, "/loop 45s ship it")).?;+ try std.testing.expectEqual(@as(?i64, 45 * std.time.ms_per_s), l.deadline_ms_delta);+ try std.testing.expectEqualStrings("ship it", l.prompt);+}++test "lifecycle words and bare invocations stay commands, not runs" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const ar = arena_state.allocator();++ for ([_][]const u8{ "/goal status", "/goal pause", "/goal RESUME", "/goal clear", "/goal ", "/goal" }) |line|+ try std.testing.expect((try parse(ar, line)) == null);+ // An objective that merely STARTS with a lifecycle word is still a run.+ const c = (try parse(ar, "/goal clear the flaky tests")).?;+ try std.testing.expectEqualStrings("clear the flaky tests", c.prompt);+ // Ordinary prompts and other commands are not autonomous runs either.+ for ([_][]const u8{ "just answer this", "/model", "/loop" }) |line|+ try std.testing.expect((try parse(ar, line)) == null);+}++test "a /loop continuation line re-parses as a plain run, never as a budget" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const ar = arena_state.allocator();+ // What mainloop synthesizes for continuation turns: the steering note, then+ // the pacing line. It must stay one run with no NEW deadline (the clock was+ // armed by the original line and is not re-armed on continuations).+ const line = "/loop [continuing autonomously (/loop): keep working the checklist]\n[pace: continuation 2 of 25, 1m elapsed.]";+ const a = (try parse(ar, line)).?;+ try std.testing.expect(a.deadline_ms_delta == null);+ try std.testing.expect(a.goal_line == null);+ try std.testing.expect(std.mem.startsWith(u8, a.prompt, "[continuing autonomously"));+}diff --git a/src/main.zig b/src/main.zigindex 03ee7b7a..6822c1ea 100644--- a/src/main.zig+++ b/src/main.zig@@ -591,6 +591,7 @@ test { // pull in tests from imported modules (mcp.zig)// they silently compile to nothing and the suite still reports green._ = @import("scoring_slot_test.zig");_ = @import("readline_history.zig");+ _ = @import("goal_pacing_autonomous_test.zig");_ = @import("goal_state.zig");_ = @import("goal_persist_tests.zig");_ = @import("goal_flow.zig");diff --git a/src/mainloop.zig b/src/mainloop.zigindex d63d0e46..d313c7e3 100644--- a/src/mainloop.zig+++ b/src/mainloop.zig@@ -114,23 +114,23 @@ pub fn run(ctx: *Ctx) !void {title_jobs.poll(ctx);const line = std.mem.trim(u8, raw_line, " \t\r");if (line.len == 0) continue;- // `/loop [<n>s|m|h] <prompt>`: an unambiguous duration arms this run's wall clock; anything else is prompt.- const loop_budget: ?goal_pacing.LoopBudget = if (main_mod.json_mode) null else goal_pacing.loopBudgetFromLine(line);- const loop_prompt: ?[]const u8 = if (loop_budget) |b| b.prompt else null;- // A fresh line starts (or ends) a /loop run: the wall clock begins here, stale todos_dirty is+ // `/goal [30m] <objective>` and `/loop [30m] <prompt>` are ONE autonomous run: same+ // plan-act-verify machine, same controller, same optional wall clock. Only /goal also+ // adopts a standing objective (goal_pacing.autonomousFromLine).+ const goal_objective: ?[]const u8 = if (main_mod.json_mode) null else repl_glue.goalPromptFromLine(line);+ const auto: ?goal_pacing.Autonomous = if (main_mod.json_mode) null else try goal_pacing.autonomousFromLine(ctx.arena, line, goal_objective);+ const loop_prompt: ?[]const u8 = if (auto) |a| a.prompt else null;+ // A fresh line starts (or ends) a run: the wall clock begins here, stale todos_dirty is// dropped (a checklist finished BEFORE this prompt ended the next run at iteration 1, #318),// and an armed budget is echoed - a silently-eaten "5m" reads exactly like a truncated prompt.- if (!is_loop_continuation) try goal_pacing.armAndAnnounce(&loop_clock, ctx.root, ctx.out, ctx.arena, util.unixMs(ctx.io), loop_budget);+ if (!is_loop_continuation) try goal_pacing.armAndAnnounce(&loop_clock, ctx.root, ctx.out, ctx.arena, util.unixMs(ctx.io), if (auto) |a| a.budget() else null);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 just recording it (the parse itself is in repl_glue).- const goal_prompt: ?[]const u8 = if (main_mod.json_mode) null else repl_glue.goalPromptFromLine(line);if (!main_mod.json_mode) {const l = if (line.len > 0 and line[0] == '/') line[1..] else line;if (std.mem.eql(u8, l, "exit") or std.mem.eql(u8, l, "quit") or std.mem.eql(u8, l, "q")) break;}- if (!main_mod.json_mode and repl_glue.isSlashCommandLine(line) and loop_prompt == null and goal_prompt == null and review_prompt == null) {+ if (!main_mod.json_mode and repl_glue.isSlashCommandLine(line) and auto == null and review_prompt == null) {// Bare "/" on a TTY: open the filterable command menu.if (ctx.interactive and line.len == 1) {if (pickers.listPicker(ctx.root, ctx.arena, ctx.out, "Command ›", &pickers.command_menu)) |idx| {@@ -142,14 +142,14 @@ pub fn run(ctx: *Ctx) !void {continue;}- // /goal <objective>: apply the goal (handleCommand sets root.goal, saves,- // and prints), then fall through to run a turn on it immediately.- if (goal_prompt != null) try main_mod.handleCommand(ctx.root, ctx.keys, ctx.arena, line, ctx.out);+ // /goal <objective>: adopt the standing goal first (handleCommand sets+ // root.goal, saves, prints), then run it on the autonomous machine below.+ if (auto) |a| if (a.goal_line) |gl| try main_mod.handleCommand(ctx.root, ctx.keys, ctx.arena, gl, ctx.out);// JSON lines may run a user turn, mutate the system prompt, or append a score.var json_request: ?std.json.Parsed(Value) = null;defer if (json_request) |*request| request.deinit();- const base_msg: []const u8 = if (review_prompt) |rp| rp else if (loop_prompt) |lp| lp else if (goal_prompt) |gp| gp else if (main_mod.json_mode) blk: {+ const base_msg: []const u8 = if (review_prompt) |rp| rp else if (loop_prompt) |lp| lp else if (main_mod.json_mode) blk: {json_request = std.json.parseFromSlice(Value, ctx.gpa, line, .{ .allocate = .alloc_if_needed }) catch {ctx.root.emit(.{ .type = "error", .message = "invalid JSON (expect {\"type\":\"user\",\"text\":\"...\"})" });continue;@@ -319,11 +319,11 @@ pub fn run(ctx: *Ctx) !void {);if (eval_note.len > 0) goal_msg = try std.fmt.allocPrint(ctx.arena, "{s}\n\n{s}", .{ goal_msg, eval_note });- // /loop asks the model to work autonomously through plan→act→verify.+ // /goal and /loop both ask the model to work autonomously (plan→act→verify).const loop_msg: []const u8 = if (loop_prompt != null) try std.fmt.allocPrint(ctx.arena,\\{s}\\- \\[harness note: /loop was used. Work autonomously until the prompt is satisfied: make a brief plan, execute it with tools, verify the result, and only stop when you can report completion or you need a required human decision. Keep iterations tight and avoid asking for confirmation between routine steps. Track multi-step work with todo_write and finish by calling attempt_completion with the final result - that ends the loop. A turn that uses no tools also ends the loop, so if the prompt needs no tool work, just answer it.]+ \\[harness note: this is an autonomous run. Work until the prompt is satisfied: make a brief plan, execute it with tools, verify the result, and only stop when you can report completion or you need a required human decision. Keep iterations tight and avoid asking for confirmation between routine steps. Track multi-step work with todo_write and finish by calling attempt_completion with the final result - that ends the run. A turn that uses no tools also ends it, so if the prompt needs no tool work, just answer it.], .{goal_msg}) else goal_msg;const msg: []const u8 = if (!main_mod.plan_mode or ctx.root.review_mode) loop_msg else try std.fmt.allocPrint(ctx.arena,@@ -582,7 +582,7 @@ pub fn run(ctx: *Ctx) !void {// Only work_done reaches `accepted`, so the loop drove the// goal to done; a --goal standing objective outlives it (#318).if (outcome == .accepted) _ = goal_flow.acceptLoopOutcome(ctx.root);- try ctx.out.print("{s}↩ /loop stopped — {s}{s}\n", .{ style.dim, @tagName(outcome), style.reset });+ try ctx.out.print("{s}↩ run stopped — {s}{s}\n", .{ style.dim, @tagName(outcome), style.reset });try ctx.out.flush();},}