graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
//! Root interactive/JSON event loop. `main` owns setup; `Ctx` is borrowed until `run` returns.
const std = @import("std");
const Io = std.Io;
const Value = std.json.Value;
const Allocator = std.mem.Allocator;
const main_mod = @import("main.zig");
const util = @import("util.zig");
const cancel_source = @import("cancel_source.zig"); // #728: who cancelled, for the marker
const agent_mod = @import("agent.zig");
const provider_mod = @import("provider.zig");
const ansi = @import("ansi.zig");
const style = &ansi.style;
const pricing = @import("pricing.zig");
const turn_event = @import("turn_event.zig");
const prompts = @import("prompts.zig");
const providers = @import("providers.zig");
const pickers = @import("pickers.zig");
const fleet = @import("fleet.zig");
const jobs = @import("jobs.zig");
const session = @import("session.zig");
const repl_glue = @import("repl_glue.zig");
const goal_state = @import("goal_state.zig");
const goal_flow = @import("goal_flow.zig");
const goal_pacing = @import("goal_pacing.zig");
const eval_memory = @import("eval_memory.zig");
const json_controls = @import("json_controls.zig");
const json_inbox = @import("json_inbox.zig");
const mainloop_score = @import("mainloop_score.zig");
const mainloop_trace = @import("mainloop_trace.zig");
const scoring = @import("scoring.zig");
const trace = @import("trace.zig");
const telemetry = @import("telemetry.zig");
const vision = @import("vision.zig");
const messages = @import("messages.zig");
const anim = @import("anim.zig");
const hooks = @import("hooks.zig");
const readline = @import("readline.zig");
const line_repl_disclosure = @import("line_repl_disclosure.zig");
const title_mod = @import("title.zig");
const mainloop_title = @import("mainloop_title.zig");
const mainloop_recap = @import("mainloop_recap.zig");
const review = @import("review.zig");
const terminal = @import("term.zig");
/// Borrowed pointers into main()'s stack plus immutable arena-owned prompt slices.
pub const Ctx = struct {
gpa: Allocator,
io: Io,
arena: Allocator,
root: *agent_mod.Agent,
keys: *provider_mod.Keys,
out: *Io.Writer,
in: *Io.Reader,
history: *std.ArrayList([]const u8),
linebuf: *std.ArrayList(u8),
interactive: bool,
sys_normal: []const u8, // startup-composed BASE; set_agent's reset-to-original path re-derives from this via prompts.setSystemPrompts() (#326)
};
/// Run turns until EOF/quit; main then performs final save/worktree cleanup.
pub fn run(ctx: *Ctx) !void {
@import("subagent_interactive.zig").configure(ctx.interactive);
defer @import("subagent_interactive.zig").configure(false);
// --json, serve and piped stdin have no idle wake: a turn waits for its background work.
@import("run_idle.zig").enabled = !ctx.interactive;
defer @import("run_idle.zig").enabled = false;
if (main_mod.json_mode) json_inbox.start(ctx.gpa, ctx.io, ctx.in);
defer if (main_mod.json_mode) json_inbox.stop();
var title_jobs: mainloop_title.Jobs = .{};
defer title_jobs.deinit(ctx);
var recap_jobs: mainloop_recap.Jobs = .{};
defer recap_jobs.deinit(ctx);
defer session.flushSavesAtExit(); // #273: the turn-path autosaves write in the background — no queued one may outlive this loop, on ANY exit (#364 stamps this teardown phase)
defer ctx.root.closeCodexWs(); // #424: the held WS + response-id anchor deliberately span turns, so only loop exit may free them — without this they are the gpa's only exit leaks
defer @import("line_repl_terminal.zig").release(ctx.out); // #396: registered LAST so LIFO runs it FIRST — the tty goes back before the save/telemetry/learning phases that can take seconds
var prev_turn_id: u64 = 0;
var prev_prompt_fp: [16]u8 = scoring.promptFingerprint(ctx.root.systemPrompt());
// Armed only after a clean /goal run turn and consumed by the next read (#226).
var loop_run: @import("loop_run.zig").LoopRun = .{};
while (true) {
@import("subagent_interactive.zig").line_notice = false;
title_jobs.poll(ctx);
recap_jobs.poll(ctx); // #419: a settled model recap emits here, before the next read
// Drain streamed follow-ups before reading fresh input (empty in JSON/GUI).
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 run's continuation turn
const raw_line: []const u8 = if (steer_entry) |e| blk: {
loop_run.cancel(ctx.root); // #226: a user steer/force cancels the autonomous run
if (e.force) {
try ctx.out.print("{s}↳ force ›{s} {s}\n", .{ style.yellow, style.reset, e.text });
} else {
try ctx.out.print("{s}↳ steer ›{s} {s}\n", .{ style.accent, style.reset, e.text });
}
try ctx.out.flush();
break :blk e.text;
} else if (loop_run.armed) blk: {
// #226: autonomous run continuation — synthesize the next turn from the
// continuation steering note instead of reading a new user line.
is_loop_continuation = true;
break :blk try loop_run.continuation(ctx.arena, ctx.root, util.unixMs(ctx.io), "");
} else if (ctx.interactive) blk: {
try ctx.root.prompt();
const read = (try readline.readLine(ctx.root, ctx.in, ctx.out, ctx.gpa, ctx.history, ctx.linebuf, null)) orelse break;
// #1278: the wake of the work a held run waits on continues that run.
if (try loop_run.afterRead(ctx.arena, ctx.root, util.unixMs(ctx.io), read, @import("subagent_interactive.zig").line_notice)) |resumed| {
is_loop_continuation = true;
break :blk resumed;
}
break :blk read;
} else (try json_inbox.request(ctx.arena, ctx.in)) orelse break;
title_jobs.poll(ctx);
recap_jobs.poll(ctx);
const line = std.mem.trim(u8, raw_line, " \t\r");
if (line_repl_disclosure.handleInput(ctx.root.io, ctx.out, line)) continue;
// `/goal [30m] <objective>` adopts the objective and starts ONE autonomous run: the
// plan-act-verify machine, its controller and an optional wall clock. A continuation
// turn stays in its run (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, is_loop_continuation);
const loop_prompt: ?[]const u8 = if (auto) |a| a.prompt else null;
// A fresh line starts (or ends) a run: wall clock begins here; stale todos_dirty is
// dropped, and an armed budget is echoed — a silently-eaten "5m" reads like a truncated prompt.
if (!is_loop_continuation) try goal_pacing.armAndAnnounce(&loop_run.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;
const issue_report: ?[]const u8 = if (!main_mod.json_mode) @import("issue_cmd.zig").promptFromLine(line) else null;
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 auto == null and review_prompt == null and issue_report == 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| {
try main_mod.handleCommand(ctx.root, ctx.keys, ctx.arena, pickers.command_menu[idx].name, ctx.out);
}
continue;
}
try main_mod.handleCommand(ctx.root, ctx.keys, ctx.arena, line, ctx.out);
continue;
}
// /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 (issue_report) |ir| try @import("issue_cmd.zig").userText(ctx.arena, ir) 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;
};
const parsed = json_request.?.value;
const rtype = if (parsed == .object)
(if (parsed.object.get("type")) |v| (if (v == .string) v.string else "") else "")
else
"";
if (std.mem.eql(u8, rtype, "set_model")) {
ctx.root.ensureStoredKeys(ctx.keys);
const provider_field = if (parsed.object.get("provider")) |v| (if (v == .string) v.string else "") else "";
const model_field = if (parsed.object.get("model")) |v| (if (v == .string) v.string else "") else "";
const legacy_name = if (parsed.object.get("name")) |v| (if (v == .string) v.string else "") else "";
providers.ensureControlRequestCatalogs(ctx.root, ctx.keys.*, provider_field, model_field, legacy_name);
const provider = providers.resolveProviderControlRequest(ctx.keys, ctx.arena, provider_field, model_field, legacy_name) catch |err| {
const label = providers.setModelRequestLabel(ctx.arena, provider_field, model_field, legacy_name) catch "<requested model>";
const message = switch (err) {
error.MissingKey => try std.fmt.allocPrint(ctx.arena, "no key/login for requested model '{s}'", .{label}),
error.InvalidModel => try std.fmt.allocPrint(ctx.arena, "unknown model '{s}'; refresh/list models before switching", .{label}),
error.InvalidModelRequest => "set_model needs a non-empty provider/model or legacy name",
else => try std.fmt.allocPrint(ctx.arena, "failed to switch model '{s}': {s}", .{ label, @errorName(err) }),
};
ctx.root.emit(.{ .type = "error", .message = message });
continue;
};
const note = try providers.applyProvider(ctx.root, ctx.arena, provider);
ctx.root.emit(.{ .type = "model", .ok = true, .provider = provider.id, .model = provider.model, .context = provider.context, .note = note });
continue;
}
if (std.mem.eql(u8, rtype, "compact")) {
const chars = ctx.root.manualCompact() catch |err| {
const message = switch (err) {
error.EmptySummary => "compaction failed: empty summary, history unchanged",
error.IncompleteSummary, error.InvalidCompactionResponse => "compaction failed: incomplete or invalid compacted history, history unchanged",
else => try std.fmt.allocPrint(ctx.arena, "compaction failed: {s}", .{@errorName(err)}),
};
ctx.root.emit(.{ .type = "error", .message = message });
continue;
};
ctx.root.emit(.{ .type = "compact", .ok = true, .chars = chars });
continue;
}
if (std.mem.eql(u8, rtype, "set_mode")) {
const mode = if (parsed.object.get("mode")) |v| (if (v == .string) v.string else "") else "";
if (std.mem.eql(u8, mode, "plan")) {
main_mod.plan_mode = true;
} else if (std.mem.eql(u8, mode, "normal")) {
main_mod.plan_mode = false;
} else {
ctx.root.emit(.{ .type = "error", .message = "set_mode needs mode 'plan' or 'normal'" });
continue;
}
ctx.root.emit(.{ .type = "mode", .ok = true, .mode = mode });
continue;
}
if (std.mem.eql(u8, rtype, "set_agent")) {
const id = if (parsed.object.get("id")) |v| (if (v == .string) v.string else "") else "";
if (id.len == 0) {
@import("prompt_cache_hud.zig").noteBust(.persona);
try prompts.setSystemPrompts(ctx.root, ctx.sys_normal, ctx.arena); // #326: reset to the startup base, all four variants
ctx.root.rebaseContextMeter();
ctx.root.emit(.{ .type = "agent", .ok = true, .id = id, .chars = ctx.root.sys_normal.len });
continue;
}
const prompt = fleet.agentTypePrompt(id) orelse {
const message = try std.fmt.allocPrint(ctx.arena, "unknown agent '{s}' (see /agents)", .{id});
ctx.root.emit(.{ .type = "error", .message = message });
continue;
};
const persona_base = try std.fmt.allocPrint(ctx.arena, "{s}\n\n{s}", .{ ctx.sys_normal, prompt });
@import("prompt_cache_hud.zig").noteBust(.persona);
try prompts.setSystemPrompts(ctx.root, persona_base, ctx.arena); // #326: recomputes strict + ultra too
ctx.root.rebaseContextMeter();
ctx.root.emit(.{ .type = "agent", .ok = true, .id = id, .chars = ctx.root.sys_normal.len });
continue;
}
if (std.mem.eql(u8, rtype, "set_effort")) {
const level = if (parsed.object.get("level")) |v| (if (v == .string) v.string else "") else "";
ctx.root.reasoning = std.meta.stringToEnum(main_mod.ReasoningEffort, @import("effort_route.zig").normalize(ctx.root.provider.id, ctx.root.provider.model, level)) orelse {
ctx.root.emit(.{ .type = "error", .message = "set_effort needs a level supported by the current model" });
continue;
};
ctx.root.jev_effort_pending.invalidate(ctx.root.io);
_ = repl_glue.saveThinkingSettings(ctx.root.io, ctx.root.gpa, ctx.root.reasoning, ctx.root.fast, ctx.root.ultracode_mode, ctx.root.show_thinking, ctx.root.ai_title);
ctx.root.emit(.{ .type = "effort", .ok = true, .level = @tagName(ctx.root.reasoning), .applies = ctx.root.effortApplies() });
continue;
}
if (std.mem.eql(u8, rtype, "set_fast")) {
const on = if (parsed.object.get("on")) |v| (if (v == .bool) v.bool else false) else false;
ctx.root.fast = on;
_ = repl_glue.saveThinkingSettings(ctx.root.io, ctx.root.gpa, ctx.root.reasoning, ctx.root.fast, ctx.root.ultracode_mode, ctx.root.show_thinking, ctx.root.ai_title);
ctx.root.emit(.{ .type = "fast", .ok = true, .on = on, .applies = ctx.root.provider.kind == .responses });
continue;
}
if (std.mem.eql(u8, rtype, "set_ultracode")) {
const on = if (parsed.object.get("on")) |v| (if (v == .bool) v.bool else false) else false;
ctx.root.ultracode_mode = on;
_ = repl_glue.saveThinkingSettings(ctx.root.io, ctx.root.gpa, ctx.root.reasoning, ctx.root.fast, ctx.root.ultracode_mode, ctx.root.show_thinking, ctx.root.ai_title);
ctx.root.emit(.{ .type = "ultracode", .ok = true, .on = on });
continue;
}
if (std.mem.eql(u8, rtype, "score")) {
mainloop_score.handle(ctx.root, parsed.object);
continue;
}
if (std.mem.eql(u8, rtype, "answer")) {
ctx.root.emit(.{ .type = "error", .message = "answer received with no active ask_user prompt" });
continue;
}
const t = if (parsed == .object) parsed.object.get("text") else null;
const text = if (t) |v| (if (v == .string) v.string else "") else "";
if (text.len == 0) {
ctx.root.emit(.{ .type = "error", .message = "request needs a non-empty \"text\" field" });
continue;
}
if (std.mem.eql(u8, rtype, "set_system_prompt")) {
const append = if (parsed.object.get("append")) |v| v == .bool and v.bool else false;
const new_base = if (append)
try std.fmt.allocPrint(ctx.arena, "{s}\n\n{s}", .{ ctx.root.sys_normal, text })
else
try ctx.arena.dupe(u8, text);
@import("prompt_cache_hud.zig").noteBust(.persona);
try prompts.setSystemPrompts(ctx.root, new_base, ctx.arena); // #326: recomputes strict + ultra too
ctx.root.rebaseContextMeter();
ctx.root.emit(.{ .type = "system_prompt", .ok = true, .append = append, .chars = ctx.root.sys_normal.len });
continue;
}
if (std.mem.eql(u8, rtype, "review")) review_prompt = text;
// #415: side question is answered here — billed, parent-cached, not persisted.
if (json_controls.sideQuestion(ctx.root, ctx.arena, rtype, text)) continue;
if (json_controls.applyEdit(ctx.root, rtype, parsed.object)) |ok| if (!ok) continue;
if (!json_controls.beginTurn(ctx.root, parsed.object)) continue;
break :blk text;
} else line;
ctx.root.review_mode = review_prompt != null;
const parent_system_override = ctx.root.sys_override;
if (ctx.root.review_mode)
ctx.root.sys_override = try review.systemPrompt(ctx.arena, ctx.root.sys_normal);
defer {
ctx.root.review_mode = false;
ctx.root.sys_override = parent_system_override;
}
if (ctx.root.fallback_blocked) {
const message = try std.fmt.allocPrint(ctx.arena, "saved model unavailable; sending to {s} requires explicit consent — run /fallback allow {s} or choose /model", .{ ctx.root.provider.id, ctx.root.provider.id });
if (main_mod.json_mode) json_controls.rejectTurn(ctx.root, message) else {
try ctx.out.print("{s}⚠ {s}{s}\n", .{ style.yellow, message, style.reset });
try ctx.out.flush();
}
continue;
}
if (!main_mod.json_mode) agent_mod.Agent.prepareRootTurn();
// Persistent goal steering (#318): diff-gated standing-goal plus one-shot
// notes (/goal replace|clear, emergency trim). Compaction restates the list.
var goal_msg: []const u8 = try goal_state.applyGoalSteering(ctx.arena, ctx.root, base_msg);
const eval_note = if (ctx.root.review_mode) "" else try repl_glue.evalSteeringNote(
ctx.arena,
ctx.root.eval_cmd,
ctx.root.eval_target,
ctx.root.eval_judge != null,
ctx.root.eval_verified,
ctx.root.eval_repair_pending,
eval_memory.load(ctx.root),
);
if (eval_note.len > 0) goal_msg = try std.fmt.allocPrint(ctx.arena, "{s}\n\n{s}", .{ goal_msg, eval_note });
// A /goal run asks 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: 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,
\\{s}
\\
\\[harness note: plan mode is ON — read-only. Explore with read-only
\\tools as needed, then present a concrete plan (files to change,
\\the changes themselves, sequence, risks) and ask the user to
\\approve it. Do not write files or run mutating commands — the
\\gate will deny them. The user toggles execution back on with /plan.]
, .{loop_msg});
// Promote a GUI `@[image]` attachment to a native vision block when the
// model can see (otherwise it only gets the path and resorts to OCR).
vision.stageGuiImageAttachment(ctx.root, msg);
// Generate the AI title before the root turn and publish it through the
// session-scoped nonblocking completion queue.
if (!main_mod.json_mode and !ctx.root.review_mode and ctx.root.ai_title and !ctx.root.ai_title_done) {
ctx.root.ai_title_done = true;
title_jobs.start(ctx, base_msg);
}
// Materialize the session header and keep the window title in sync.
if (!main_mod.json_mode) {
if (!ctx.root.tui_header_shown) {
// Print the fast title immediately; the detached AI title later updates
// the redrawable window title and session filename without blocking.
const turn_title = title_mod.titleFromPrompt(base_msg);
title_mod.setTerminalTitle(ctx.out, turn_title, main_mod.g_cwd_display);
try title_mod.printSessionHeader(ctx.out, turn_title, main_mod.g_cwd_display);
ctx.root.tui_header_shown = true;
} else {
// Later turns: keep the OSC tab/window title synced; header shown.
const turn_title = ctx.root.session_title orelse title_mod.titleFromPrompt(base_msg);
title_mod.setTerminalTitle(ctx.out, turn_title, main_mod.g_cwd_display);
}
}
// "ultracode" codeword or persistent /ultracode mode: opt turns into multi-agent workflow mode.
const ultracode_msg = if (ctx.root.review_mode)
pickers.UltracodeMessage{ .text = msg, .explicit = false }
else
try pickers.applyUltracodeSteering(ctx.arena, msg, base_msg, prompts.ultracodeActive(ctx.root));
if (ultracode_msg.explicit) {
if (!main_mod.json_mode) {
if (ctx.interactive) {
anim.ultracodeShine(ctx.out, ctx.io);
try ctx.out.writeAll("⚡ multi-agent workflow mode engaged\n");
} else {
try ctx.out.writeAll("⚡ ultracode — multi-agent workflow mode engaged\n");
}
try ctx.out.flush();
}
ctx.root.tracer.?.note("ultracode", msg[0..@min(msg.len, 120)]);
if (telemetry.g_telem) |t| t.ultracode();
}
var review_context = review.Context.begin(ctx.arena, ctx.root, ctx.root.review_mode);
if (ctx.root.review_mode) ctx.root.rebaseContextMeter();
defer if (review_context.restore(ctx.root)) ctx.root.rebaseContextMeter();
if (try @import("turn_dedup.zig").enqueueOrSkip(ctx.root, ctx.arena, ctx.out, ultracode_msg.text)) continue;
ctx.root.snapshots.?.turn += 1; // tag file edits in this turn (matches /rewind numbering)
ctx.root.recap_generation +%= 1; // #419: a starting turn supersedes any recap job still in flight
if (telemetry.g_telem) |t| t.beginTurn(@intCast(@min(base_msg.len, std.math.maxInt(u32))), ctx.root.provider.model);
// Trajectory: claim this turn's node id up front so subagents spawned during the turn can attach to it as their parent.
const turn_id: u64 = if (trace.g_traj) |tj| blk: {
const id = tj.nextId();
tj.setTurn(id);
break :blk id;
} else 0;
ctx.root.tools_used.clear(ctx.io); // per-turn tool log for the turn's node
ctx.root.tool_calls_this_turn = 0;
ctx.root.model_calls_this_turn = 0;
goal_state.beginTurn(ctx.root, !is_loop_continuation and !@import("subagent_interactive.zig").line_notice); // per-turn gate flags (the ARMED double-check is NOT one, #318) + the new-ask retirement of a finished checklist (#394)
ctx.root.seen_tool_keys.clearRetainingCapacity();
if (main_mod.json_mode) ctx.root.emit(.{ .type = "started", .provider = ctx.root.provider.id, .model = ctx.root.provider.model });
if (ctx.interactive and !main_mod.json_mode) {
try ctx.out.writeAll("\n");
try ctx.out.flush();
}
const turn_before = mainloop_trace.begin(ctx.root, ctx.io);
mainloop_trace.recordLive(ctx.root, base_msg, turn_id, prev_turn_id);
const turn_started = Io.Timestamp.now(ctx.io, .awake);
const turn_result = providers.runTurnWithFallback(ctx.root, ctx.keys, ctx.arena, ctx.out);
if (main_mod.json_mode) json_inbox.endTurn();
// Reuse one full-history scan for trace, terminal event, and compaction.
const post_turn_context_tokens = ctx.root.effectiveContextTokens();
mainloop_trace.record(ctx.root, ctx.io, ctx.arena, base_msg, turn_id, turn_started, turn_result, post_turn_context_tokens, turn_before, &prev_turn_id, &prev_prompt_fp);
const isolated_review = review_context.restore(ctx.root);
if (isolated_review) {
ctx.root.review_mode = false;
ctx.root.sys_override = parent_system_override;
try ctx.root.messages.append(try messages.textMessage(ctx.arena, "user", base_msg));
ctx.root.rebaseContextMeter();
}
const final_text = turn_result catch |err| switch (err) {
error.Interrupted => {
// Preserve streamed text and mark the saved assistant turn incomplete. #728: an unsourced cancel is the harness's, not the user's.
const src = cancel_source.take(ctx.root.tracer);
const partial = std.mem.trim(u8, ctx.root.partial_text.items, " \t\r\n");
const marker: []const u8 = if (partial.len > 0)
try std.fmt.allocPrint(ctx.arena, "{s}\n\n{s}", .{ partial, cancel_source.marker(src) })
else
cancel_source.marker(src);
try ctx.root.messages.append(try messages.textMessage(ctx.arena, "assistant", marker));
main_mod.g_force_interrupt = false;
if (main_mod.json_mode) {
ctx.root.emit(.{ .type = "error", .message = cancel_source.jsonMessage(src) });
} else {
try ctx.out.print("{s}{s}{s}\n", .{ style.yellow, cancel_source.chrome(src), style.reset });
try ctx.out.flush();
}
session.saveSession(ctx.root, ctx.arena, ctx.root.session_name) catch {};
continue;
},
error.StreamStalled => {
// A dead/idle stream (no SSE bytes for stream_stall_ms): the
// harness gave up, NOT the user (#134) — keep the partial but
// tag it a stall, and emit the structured error for --json.
const partial = std.mem.trim(u8, ctx.root.partial_text.items, " \t\r\n");
const marker = try @import("stream_reconnect.zig").endedEarlyText(ctx.arena, partial, true);
try ctx.root.messages.append(try messages.textMessage(ctx.arena, "assistant", marker));
if (main_mod.json_mode) {
ctx.root.emit(.{ .type = "error", .message = ctx.root.last_api_error orelse "stream stalled — ending turn" });
if (partial.len > 0) {
ctx.root.emit(.{ .type = "finalizing" });
const context_tokens = ctx.root.effectiveContextTokens();
ctx.root.emit(turn_event.fromTally(&pricing.g_cost, ctx.io, partial, context_tokens, false));
}
}
session.saveSession(ctx.root, ctx.arena, ctx.root.session_name) catch {};
continue;
},
error.StreamDropped => {
// The provider closed/reset the connection before its terminal
// event ([DONE]/finish_reason) — the harness ended the turn, NOT
// the user (#133). Keep the partial, tag it a drop (never
// "[response interrupted by user]"), and emit the --json error.
const partial = std.mem.trim(u8, ctx.root.partial_text.items, " \t\r\n");
const marker = try @import("stream_reconnect.zig").endedEarlyText(ctx.arena, partial, false);
try ctx.root.messages.append(try messages.textMessage(ctx.arena, "assistant", marker));
if (main_mod.json_mode) {
ctx.root.emit(.{ .type = "error", .message = ctx.root.last_api_error orelse "stream dropped — ending turn" });
if (partial.len > 0) {
ctx.root.emit(.{ .type = "finalizing" });
const context_tokens = ctx.root.effectiveContextTokens();
ctx.root.emit(turn_event.fromTally(&pricing.g_cost, ctx.io, partial, context_tokens, false));
}
}
session.saveSession(ctx.root, ctx.arena, ctx.root.session_name) catch {};
continue;
},
error.ApiError => {
if (telemetry.g_telem) |t| t.errorEvent("api", ctx.root.last_api_error orelse "api error");
if (main_mod.json_mode) {
ctx.root.emit(.{ .type = "error", .message = ctx.root.last_api_error orelse "api error" });
const partial = std.mem.trim(u8, ctx.root.partial_text.items, " \t\r\n");
if (partial.len > 0) {
ctx.root.emit(.{ .type = "finalizing" });
const context_tokens = ctx.root.effectiveContextTokens();
ctx.root.emit(turn_event.fromTally(&pricing.g_cost, ctx.io, partial, context_tokens, false));
}
}
// A turn can fail because the context window overflowed; if we're
// over the compaction threshold, compact now so the next turn
// isn't doomed to fail at the same size (#88). Match every other
// automatic compaction path: a failed summary may destructively
// trim only when the best meter says we're genuinely near 95%.
const recovery_meter = ctx.root.effectiveContextTokens();
if (recovery_meter >= ctx.root.provider.compactAt()) {
ctx.root.autocompact(recovery_meter);
}
try @import("turn_dedup.zig").settleFailedTurn(ctx.root, ctx.arena, err); // #1729: a retry of this prompt is a new ask
session.saveSession(ctx.root, ctx.arena, ctx.root.session_name) catch {};
continue;
},
else => |e| {
if (telemetry.g_telem) |t| t.errorEvent("turn", @errorName(e));
if (main_mod.json_mode) {
ctx.root.emit(.{ .type = "error", .message = @errorName(e) });
} else {
ctx.root.say("[turn aborted: {t}]\n", .{e}) catch {};
}
try @import("turn_dedup.zig").settleFailedTurn(ctx.root, ctx.arena, e);
session.saveSession(ctx.root, ctx.arena, ctx.root.session_name) catch {};
continue;
},
};
if (isolated_review)
try ctx.root.messages.append(try messages.textMessage(ctx.arena, "assistant", final_text));
const session_context_tokens = if (isolated_review) ctx.root.effectiveContextTokens() else post_turn_context_tokens;
// #419: heuristic session_recap precedes the terminal turn event; model recap scheduled here.
mainloop_recap.onTurnEnd(ctx, &recap_jobs, final_text);
if (main_mod.json_mode) {
const emitted_text = if (final_text.len == 0 and ctx.root.partial_text.items.len > 0)
std.mem.trim(u8, ctx.root.partial_text.items, " \t\r\n")
else
final_text;
ctx.root.emit(.{ .type = "finalizing" });
// #124: allocator-level leak telemetry (GRAFF_MEM_DEBUG=1) — arena
// capacity per turn separates a session-arena leak from gpa-side
// growth, which OS-level RSS sampling can't tell apart. Emit this
// before the terminal turn event so protocol bridges that stop at
// `complete:true` never strand a debug event for the next request.
if (main_mod.g_mem_debug) ctx.root.emit(.{
.type = "mem",
.session_arena_kb = if (main_mod.g_session_arena) |a| a.queryCapacity() / 1024 else 0,
.scratch_arena_kb = if (ctx.root.scratch_arena) |a| a.queryCapacity() / 1024 else 0,
});
ctx.root.emit(turn_event.fromTally(&pricing.g_cost, ctx.io, emitted_text, session_context_tokens, isolated_review or ctx.root.yielded != null or !ctx.root.eval_repair_pending));
}
// Apply only if already complete; poll never waits for title generation.
title_jobs.poll(ctx);
fleet.joinElites(ctx.io); // publish backgrounded fleet champions for the next turn (no-op once joined)
// turn_end lifecycle hooks (best-effort; interrupted/errored turns
// `continue` above and never reach here, so ok is always true).
hooks.runTurnEndHooks(ctx.gpa, ctx.io);
if (session_context_tokens >= ctx.root.provider.compactAt()) {
// Trim on failure only when we're genuinely against the window — at
// 80–95% a transient compaction failure can recover next turn.
ctx.root.autocompact(session_context_tokens); // loop_run.list re-carries via root.history_rewrites, incl. MID-turn rewrites this block never sees (#318)
}
// #226: controller-authorized continuation. After a cleanly-
// completed autonomous run turn the CONTROLLER decides whether to run
// another turn — not the model merely stopping (loop_run.zig). The armed
// continuation is consumed at the next read, so an interrupted/errored
// turn (which `continue`s past here) never resumes the loop; a run with
// background work still running holds for its wake instead (#1278).
if (loop_prompt != null and !main_mod.json_mode) try loop_run.afterTurn(ctx.root, ctx.io, ctx.out, is_loop_continuation);
session.saveSessionAsync(ctx.root, ctx.arena, ctx.root.session_name) catch {};
// --worktree checkpoint: commit this turn's edits to the scratch branch so the work is durable
// + rewindable across restarts. No-op when not in a worktree or when --no-autocommit is set.
jobs.worktreeAutoCommit(ctx.gpa, ctx.io, std.fmt.allocPrint(ctx.arena, "wip: {s}", .{title_mod.titleFromPrompt(base_msg)}) catch "wip: graff checkpoint");
}
}
test {
_ = @import("loop_run.zig");
}