Skip to content

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

Python

feat(goal): structured goal state + status lifecycle — /goal pause|resume|status (#223)

Rach Pradhan49f483f63e87
diff --git a/src/agent.zig b/src/agent.zig
index dfcc684d..9fb27ded 100644
--- a/src/agent.zig
+++ b/src/agent.zig
@@ -77,6 +77,21 @@ 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 };
+
+/// A structured standing objective: the /goal text plus its lifecycle status and
+/// created/updated timestamps. Replaces the bare `?[]const u8` so /goal can
+/// pause/resume/report and persist a real state machine across resumes. The
+/// `objective` string is owned by the session arena (like the old goal string).
+pub const Goal = struct {
+ objective: []const u8,
+ status: GoalStatus = .active,
+ created_ms: i64 = 0,
+ updated_ms: i64 = 0,
+};
+
/// One agent: a message history plus the POST/tool-dispatch loop. The root
/// agent prints to stdout; subagents (sub = true) run on pool threads and
/// log through std.debug.print, which locks stderr and is thread-safe.
@@ -135,7 +150,7 @@ pub const Agent = struct {
ultracode_mode: bool = false, // persistent ultracode (multi-agent workflow) mode (/ultracode)
show_thinking: bool = true, // stream the model's reasoning live in the TUI (/thinking); off = spinner only
ai_title: bool = true, // AI-generate the tab/session title from the first prompt (/title)
- goal: ?[]const u8 = null, // persistent objective steering (/goal)
+ goal: ?Goal = null, // structured objective + status lifecycle (/goal, #223)
session_name: []const u8 = "last", // autosave/resume target (<name>.session.json)
session_title: ?[]const u8 = null, // human-readable title/rename metadata
sys_strict: []const u8 = prompts.main_system_prompt_strict,
diff --git a/src/command_catalog.zig b/src/command_catalog.zig
index ef3e8b51..6c373459 100644
--- a/src/command_catalog.zig
+++ b/src/command_catalog.zig
@@ -26,7 +26,7 @@ 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]", .desc = "set/show a standing objective (tracked as a checklist); /goal clear clears" },
+ .{ .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 <prompt>", .desc = "run an autonomous plan->act->verify pass" },
.{ .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/commands_session.zig b/src/commands_session.zig
index ccd111b2..d24192a7 100644
--- a/src/commands_session.zig
+++ b/src/commands_session.zig
@@ -63,7 +63,7 @@ fn resetConversationSteering(root: *Agent) void {
test "/clear + /new reset conversation steering — goal and ultracode_mode don't survive (#178)" {
var root: Agent = undefined;
- root.goal = "ultracode: index the statutes";
+ root.goal = .{ .objective = "ultracode: index the statutes" };
root.ultracode_mode = true;
resetConversationSteering(&root);
try std.testing.expect(root.goal == null);
@@ -122,16 +122,34 @@ pub fn tryHandle(root: *Agent, keys: *Keys, arena: Allocator, line: []const u8,
}
if (std.mem.startsWith(u8, line, "/goal")) {
const text = std.mem.trim(u8, line["/goal".len..], " \t");
- if (text.len == 0) {
- if (root.goal) |goal| try out.print("Current goal: {s}\nClear it with /goal clear.\n", .{goal}) else try out.writeAll("No active goal. Set one with /goal <objective>.\n");
+ if (text.len == 0 or std.ascii.eqlIgnoreCase(text, "status")) {
+ // bare /goal or /goal status: report the objective + lifecycle state.
+ if (root.goal) |g| {
+ try out.print("\xf0\x9f\x8e\xaf Goal: {s}\nStatus: {s}. Commands: /goal pause | resume | clear.\n", .{ g.objective, @tagName(g.status) });
+ } else try out.writeAll("No active goal. Set one with /goal <objective>.\n");
} else if (std.ascii.eqlIgnoreCase(text, "clear") or std.ascii.eqlIgnoreCase(text, "off")) {
root.goal = null;
saveSession(root, arena, root.session_name) catch {};
try out.writeAll("Goal cleared. Future turns will not get goal steering.\n");
+ } else if (std.ascii.eqlIgnoreCase(text, "pause")) {
+ if (root.goal) |*g| {
+ g.status = .paused;
+ g.updated_ms = unixMs(root.io);
+ saveSession(root, arena, root.session_name) catch {};
+ try out.print("Goal paused: {s} - turns no longer get goal steering. /goal resume to continue.\n", .{g.objective});
+ } else try out.writeAll("No active goal to pause. Set one with /goal <objective>.\n");
+ } else if (std.ascii.eqlIgnoreCase(text, "resume")) {
+ if (root.goal) |*g| {
+ g.status = .active;
+ g.updated_ms = unixMs(root.io);
+ saveSession(root, arena, root.session_name) catch {};
+ try out.print("\xf0\x9f\x8e\xaf Goal resumed: {s} - steering every turn again.\n", .{g.objective});
+ } else try out.writeAll("No goal to resume. Set one with /goal <objective>.\n");
} else {
- root.goal = try arena.dupe(u8, text);
+ const now = unixMs(root.io);
+ root.goal = .{ .objective = try arena.dupe(u8, text), .status = .active, .created_ms = now, .updated_ms = now };
saveSession(root, arena, root.session_name) catch {};
- try out.print("\xf0\x9f\x8e\xaf Goal set: {s} \xe2\x80\x94 starting now (tracked as a live checklist; it steers every turn until /goal clear).\n", .{text});
+ try out.print("\xf0\x9f\x8e\xaf Goal set: {s} \xe2\x80\x94 starting now (tracked as a live checklist; it steers every turn until /goal pause or /goal clear).\n", .{text});
}
try out.flush();
return true;
diff --git a/src/mainloop.zig b/src/mainloop.zig
index 988f1857..4a2b9d1b 100644
--- a/src/mainloop.zig
+++ b/src/mainloop.zig
@@ -135,7 +135,7 @@ pub fn run(ctx: *Ctx) !void {
// just recording it. Bare /goal (show) and /goal clear/off stay commands.
const goal_prompt: ?[]const u8 = if (!main_mod.json_mode and std.mem.startsWith(u8, line, "/goal ")) gblk: {
const g = std.mem.trim(u8, line["/goal".len..], " \t");
- if (g.len == 0 or std.ascii.eqlIgnoreCase(g, "clear") or std.ascii.eqlIgnoreCase(g, "off")) break :gblk null;
+ if (g.len == 0 or std.ascii.eqlIgnoreCase(g, "clear") or std.ascii.eqlIgnoreCase(g, "off") or std.ascii.eqlIgnoreCase(g, "pause") or std.ascii.eqlIgnoreCase(g, "resume") or std.ascii.eqlIgnoreCase(g, "status")) break :gblk null;
break :gblk g;
} else null;
if (!main_mod.json_mode) {
diff --git a/src/repl_glue.zig b/src/repl_glue.zig
index 8c48a8dc..4656d2c4 100644
--- a/src/repl_glue.zig
+++ b/src/repl_glue.zig
@@ -94,13 +94,14 @@ pub const ReplStreamSink = struct {
/// current checklist render when one exists. Returns "" when goal is null so the
/// caller can skip the append. Pass todos_render="" when there are no todos — do
/// NOT pass renderTodos()'s "(no todos)" placeholder, which would leak into the prompt.
-pub fn goalSteeringNote(arena: Allocator, goal: ?[]const u8, todos_render: []const u8) ![]const u8 {
+pub fn goalSteeringNote(arena: Allocator, goal: ?agent_mod.Goal, todos_render: []const u8) ![]const u8 {
const g = goal orelse return "";
+ if (g.status != .active) return ""; // paused/blocked/complete/budget_limited never steer (#223)
const progress: []const u8 = if (todos_render.len > 0)
try std.fmt.allocPrint(arena, "\n\nChecklist so far:\n{s}", .{todos_render})
else
"";
- return std.fmt.allocPrint(arena, "[standing goal: {s} - track this as a todo_write checklist and work through it, marking each item in_progress when you start and completed when done.]{s}", .{ g, progress });
+ return std.fmt.allocPrint(arena, "[standing goal: {s} - track this as a todo_write checklist and work through it, marking each item in_progress when you start and completed when done.]{s}", .{ g.objective, progress });
}
/// Extract a 0-100 score from an eval command's output: a `score` key (JSON or
@@ -146,7 +147,7 @@ pub fn evalSteeringNote(arena: Allocator, eval_cmd: ?[]const u8, target: u8, has
return std.fmt.allocPrint(arena, "[eval-driven loop active. A scoring command is configured. Work it as a scored improvement loop: (1) call the `eval` tool to score the current state - the harness runs the command and logs to .graff/eval-log.tsv, so do NOT run it yourself via bash; (2) read the score, best-so-far, and output; (3) find the SINGLE biggest failure (inspect any artifacts or images directly); (4) make ONE focused change targeting it; (5) call `eval` again. Continue until `eval` reports the target ({d}/100) is met.{s} Do not stop at the first passing result, and do not revert unless `eval` shows a clear regression. After each `eval`, briefly note what you changed.]", .{ target, gate });
}
-test "goalSteeringNote: goal + checklist assembly, no (no todos) leak" {
+test "goalSteeringNote: active-only gate + checklist assembly, no (no todos) leak" {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
const ar = arena.allocator();
@@ -154,14 +155,17 @@ test "goalSteeringNote: goal + checklist assembly, no (no todos) leak" {
// No goal -> empty note, caller skips the append.
try std.testing.expectEqualStrings("", try goalSteeringNote(ar, null, ""));
- // Goal, no todos -> bracket note, no checklist, and never the "(no todos)" placeholder.
- const n1 = try goalSteeringNote(ar, "close all issues", "");
+ // A paused (non-active) goal never steers (#223): empty note.
+ try std.testing.expectEqualStrings("", try goalSteeringNote(ar, .{ .objective = "close all issues", .status = .paused }, ""));
+
+ // Active goal, no todos -> bracket note, no checklist, and never the "(no todos)" placeholder.
+ const n1 = try goalSteeringNote(ar, .{ .objective = "close all issues" }, "");
try std.testing.expect(std.mem.startsWith(u8, n1, "[standing goal: close all issues - track this as a todo_write checklist"));
try std.testing.expect(std.mem.indexOf(u8, n1, "Checklist so far") == null);
try std.testing.expect(std.mem.indexOf(u8, n1, "(no todos)") == null);
- // Goal + live todos -> the rendered checklist is appended verbatim.
- const n2 = try goalSteeringNote(ar, "ship 0.0.177", "[x] wire steering\n[ ] add test");
+ // Active goal + live todos -> the rendered checklist is appended verbatim.
+ const n2 = try goalSteeringNote(ar, .{ .objective = "ship 0.0.177" }, "[x] wire steering\n[ ] add test");
try std.testing.expect(std.mem.indexOf(u8, n2, "Checklist so far:\n[x] wire steering\n[ ] add test") != null);
}
diff --git a/src/session.zig b/src/session.zig
index fbab9099..14771954 100644
--- a/src/session.zig
+++ b/src/session.zig
@@ -229,7 +229,18 @@ pub fn saveSession(root: *Agent, arena: Allocator, name: []const u8) !void {
try s.objectField("ultracode_mode");
try s.write(root.ultracode_mode);
try s.objectField("goal");
- if (root.goal) |goal| try s.write(goal) else try s.write(null);
+ if (root.goal) |g| {
+ try s.beginObject();
+ try s.objectField("objective");
+ try s.write(g.objective);
+ try s.objectField("status");
+ try s.write(@tagName(g.status));
+ try s.objectField("created_ms");
+ try s.write(g.created_ms);
+ try s.objectField("updated_ms");
+ try s.write(g.updated_ms);
+ try s.endObject();
+ } else try s.write(null);
try s.objectField("title");
if (root.session_title) |title| try s.write(title) else try s.write(sessionTitle(root));
try s.objectField("updated_ms");
@@ -244,6 +255,56 @@ pub fn saveSession(root: *Agent, arena: Allocator, name: []const u8) !void {
try Io.Dir.cwd().writeFile(root.io, .{ .sub_path = path, .data = aw.writer.buffered() });
}
+/// Parse the persisted `goal` field into a structured Goal (#223). A bare string
+/// is a legacy session -> load as .active stamped with `now_ms`; an object carries
+/// objective + status + created/updated timestamps (an unknown/missing status
+/// falls back to .active). Pure (no Io) so it round-trips in unit tests. Returns
+/// null for an empty/absent objective.
+fn goalFromValue(v: Value, now_ms: i64) ?agent_mod.Goal {
+ if (v == .string) {
+ if (v.string.len == 0) return null;
+ return .{ .objective = v.string, .status = .active, .created_ms = now_ms, .updated_ms = now_ms };
+ }
+ if (v == .object) {
+ const go = v.object;
+ const txt = if (go.get("objective")) |o| (if (o == .string and o.string.len > 0) o.string else null) else null;
+ const objective = txt orelse return null;
+ const st: agent_mod.GoalStatus = if (go.get("status")) |s| (if (s == .string) (std.meta.stringToEnum(agent_mod.GoalStatus, s.string) orelse .active) else .active) else .active;
+ const cms: i64 = if (go.get("created_ms")) |c| (if (c == .integer) c.integer else 0) else 0;
+ const ums: i64 = if (go.get("updated_ms")) |u| (if (u == .integer) u.integer else 0) else 0;
+ return .{ .objective = objective, .status = st, .created_ms = cms, .updated_ms = ums };
+ }
+ return null;
+}
+
+test "goalFromValue: legacy string -> active; object round-trips; paused stays paused (#223)" {
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+ const a = arena_state.allocator();
+
+ // Legacy bare string -> active, stamped with now_ms (backward compat).
+ const legacy = try std.json.parseFromSliceLeaky(Value, a, "\"ship 0.0.202\"", .{ .allocate = .alloc_always });
+ const g1 = goalFromValue(legacy, 4242).?;
+ try std.testing.expectEqualStrings("ship 0.0.202", g1.objective);
+ try std.testing.expectEqual(agent_mod.GoalStatus.active, g1.status);
+ try std.testing.expectEqual(@as(i64, 4242), g1.created_ms);
+
+ // New object with a paused status round-trips as paused (survives resume).
+ const paused = try std.json.parseFromSliceLeaky(Value, a, "{\"objective\":\"land #223\",\"status\":\"paused\",\"created_ms\":10,\"updated_ms\":20}", .{ .allocate = .alloc_always });
+ const g2 = goalFromValue(paused, 999).?;
+ try std.testing.expectEqualStrings("land #223", g2.objective);
+ try std.testing.expectEqual(agent_mod.GoalStatus.paused, g2.status);
+ try std.testing.expectEqual(@as(i64, 10), g2.created_ms);
+ try std.testing.expectEqual(@as(i64, 20), g2.updated_ms);
+
+ // Empty string -> null (no goal).
+ const empty = try std.json.parseFromSliceLeaky(Value, a, "\"\"", .{ .allocate = .alloc_always });
+ try std.testing.expect(goalFromValue(empty, 1) == null);
+
+ // An unknown status string falls back to active (forward-compat with future variants).
+ const unknown = try std.json.parseFromSliceLeaky(Value, a, "{\"objective\":\"x\",\"status\":\"zzz\"}", .{ .allocate = .alloc_always });
+ try std.testing.expectEqual(agent_mod.GoalStatus.active, goalFromValue(unknown, 1).?.status);
+}
/// Restore a saved session: parse the file (arena-owned), rebuild the
/// provider, and replace the live history. The wire format must still match
/// the restored provider's kind — same provider id guarantees it.
@@ -262,7 +323,7 @@ pub fn loadSession(root: *Agent, keys: Keys, arena: Allocator, name: []const u8)
const msgs = if (obj.get("messages")) |v| (if (v == .array) v.array else return error.BadSession) else return error.BadSession;
const strict = if (obj.get("strict")) |v| (v == .bool and v.bool) else false;
const ultracode_mode = if (obj.get("ultracode_mode")) |v| (v == .bool and v.bool) else false;
- const goal = if (obj.get("goal")) |v| (if (v == .string and v.string.len > 0) v.string else null) else null;
+ const goal: ?agent_mod.Goal = if (obj.get("goal")) |v| goalFromValue(v, unixMs(root.io)) else null;
const title = if (obj.get("title")) |v| (if (v == .string and v.string.len > 0) v.string else null) else null;
root.provider = try keys.providerById(pid, model);
@@ -323,7 +384,7 @@ test "hasMeaningfulState gates the blank-draft write (#184)" {
try std.testing.expect(!hasMeaningfulState(&root));
// A standing /goal alone is meaningful (goal-only sessions are still saved).
- root.goal = "ship the release";
+ root.goal = .{ .objective = "ship the release" };
try std.testing.expect(hasMeaningfulState(&root));
// One user message alone is meaningful.
diff --git a/src/session_run.zig b/src/session_run.zig
index 2aa43485..5f8b63fe 100644
--- a/src/session_run.zig
+++ b/src/session_run.zig
@@ -225,7 +225,7 @@ pub fn buildRootAgent(
const fresh_session_name = try std.fmt.allocPrint(arena, "session-{d}", .{util.unixMs(io)});
root.session_name = if (flags.resume_flag) |name| (if (!flags.new_session_flag and !flags.no_resume_flag) name else fresh_session_name) else fresh_session_name;
repl_glue.loadThinkingSettings(io, arena, &root); // {"effort":...,"fast":...} persisted by /effort and /fast
- if (flags.goal_flag) |g| root.goal = try arena.dupe(u8, g); // --goal applies to every turn (incl. --json/-p/SDK)
+ if (flags.goal_flag) |g| root.goal = .{ .objective = try arena.dupe(u8, g), .status = .active, .created_ms = util.unixMs(io), .updated_ms = util.unixMs(io) }; // --goal applies to every turn (incl. --json/-p/SDK)
if (flags.eval_cmd_flag) |c| root.eval_cmd = try arena.dupe(u8, c);
if (flags.eval_target_flag) |t| root.eval_target = t;
if (flags.eval_niche_flag) |n| root.eval_niche = try arena.dupe(u8, n);