graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
turn: recover a mid-task reply that runs out of budget without a tool call
justrach0ecb9575e569
diff --git a/src/agent.zig b/src/agent.zigindex 241ff6c9..c9d3c7b4 100644--- a/src/agent.zig+++ b/src/agent.zig@@ -223,6 +223,7 @@ pub const Agent = struct {compact_pin_degraded: bool = false, // #581: pinned suffix over budget — skip+say once per unresolved turncompact_stall: @import("compact_cut.zig").Stall = .{}, // #706: same-boundary no-progress cutsempty_completion_retries: u8 = 0, // degenerate empty completions re-asked this turn (agent_empty_completion.zig)+ length_cutoff_used: bool = false, // a cut-off or runaway reply was already rewound this turn (agent_length_cutoff.zig)named_work_nudges: u8 = 0, // named-file / zero-tool nudge this turn (named_work.zig)named_work_task: []const u8 = "", // this turn's user text; arena-owned, never a child pointernamed_work_settled: []const u8 = "", // mention already nudged or tooleddiff --git a/src/agent_empty_completion.zig b/src/agent_empty_completion.zigindex 6e7aef6f..72d22e3a 100644--- a/src/agent_empty_completion.zig+++ b/src/agent_empty_completion.zig@@ -204,7 +204,7 @@ pub const PendingWork = struct {return goals.openCount(self.todos.items, goals.currentEpoch(self.goal));}- fn canRequest(self: *const Agent) bool {+ pub fn canRequest(self: *const Agent) bool {if (self.run_budget) |b| if (!b.canAfford(1)) return false;if (main_mod.max_tool_calls) |max| if (self.tool_calls_this_turn >= max) return false;const cap = @import("turn_chrome.zig").max_turn_model_calls;diff --git a/src/agent_length_cutoff.zig b/src/agent_length_cutoff.zignew file mode 100644index 00000000..16e131b3--- /dev/null+++ b/src/agent_length_cutoff.zig@@ -0,0 +1,253 @@+//! A mid-task reply that runs out of output budget without calling a tool.+//!+//! After tool work, a model can deliberate in visible text (or, where the+//! provider streams reasoning summaries, in reasoning) until the output cap+//! stops it: minutes of "wait, let me re-check the spec" with no tool call,+//! after which the turn ended as if that text were the answer. Two stops,+//! both rewinding the reply and asking for the next action once per turn:+//! * `handle`: the provider already cut the reply at its output limit;+//! * `ReplySize` + `recoverRunaway`: the reply passed `runaway_bytes` of text+//! (or of summary reasoning) before any tool call, so the stream is+//! stopped early instead of waiting for the cap.+//! The cut reply is never the answer, and keeping it would re-bill its tokens+//! as input on every later call. A turn with no tool call yet is left alone:+//! a long first answer stays as written.++const std = @import("std");+const Value = std.json.Value;+const Agent = @import("agent.zig").Agent;+const Kind = @import("provider.zig").Provider.Kind;+const messages = @import("messages.zig");++pub const note = "Your previous reply ran long without a tool call and was cut off. Do not repeat or continue it. If work remains, make the next tool call now; if the task is done, finish with a short summary.";++/// Text (or summary reasoning) before the first tool call that ends a reply+/// early. Completed text replies this long are very rare; replies cut at the+/// output cap with no tool call are far more common, and reasoning summaries+/// that end in a tool call stay well under it.+pub const runaway_bytes: usize = 24 * 1024;++/// The provider stopped because it ran out of output budget, in any of the+/// response shapes graff reads: chat finish_reason, Anthropic stop_reason,+/// Responses incomplete_details, Interactions status.+pub fn cutAtLimit(root: std.json.ObjectMap) bool {+ if (root.get("choices")) |choices| if (choices == .array and choices.array.items.len > 0) {+ const c0 = choices.array.items[0];+ if (c0 == .object) if (c0.object.get("finish_reason")) |fr|+ if (fr == .string and std.mem.eql(u8, fr.string, "length")) return true;+ };+ if (strIn(root, "stop_reason")) |s| if (std.mem.eql(u8, s, "max_tokens")) return true;+ if (root.get("incomplete_details")) |d| if (d == .object) if (strIn(d.object, "reason")) |r|+ if (std.mem.eql(u8, r, "max_output_tokens")) return true;+ if (strIn(root, "status")) |s| if (std.mem.eql(u8, s, "incomplete")) return true;+ return false;+}++/// Mid-task (the turn has used tools) and not yet spent this turn.+fn armed(self: *const Agent) bool {+ return !self.length_cutoff_used and self.tool_calls_this_turn > 0 and+ !self.compaction_request and self.output_schema == null;+}++/// Rewind a mid-task reply the provider cut at its output limit and add the+/// note. False leaves the reply to the normal path.+pub fn handle(self: *Agent, root: std.json.ObjectMap, hist_len: usize) !bool {+ if (!armed(self) or !cutAtLimit(root)) return false;+ return rewind(self, hist_len, "[reply hit the output limit before any tool call — dropped it and asked for the next step]\n");+}++/// The stream stopped itself (`error.ReplyRunaway`): rewind the same way.+pub fn recoverRunaway(self: *Agent, err: anyerror, hist_len: usize) !bool {+ if (err != error.ReplyRunaway) return false;+ self.partial_text.clearRetainingCapacity();+ if (self.streamed_text) if (self.out) |w| {+ w.writeAll("\n") catch {};+ w.flush() catch {};+ };+ self.streamed_text = false;+ self.streamed_args = .none;+ return rewind(self, hist_len, "[reply ran long without a tool call — stopped it and asked for the next step]\n");+}++fn rewind(self: *Agent, hist_len: usize, line: []const u8) !bool {+ if (!@import("agent_empty_completion.zig").PendingWork.canRequest(self)) return false;+ self.length_cutoff_used = true;+ const keep = @min(hist_len, self.messages.items.len);+ self.closeCodexWs();+ self.messages.shrinkRetainingCapacity(keep);+ try self.messages.append(try messages.userNote(self.arena, self.provider.kind, note));+ try self.say("{s}", .{line});+ if (self.tracer) |tr| tr.note("length_cutoff", "mid-task reply ran out of budget without a tool call; rewound once");+ return true;+}++/// One streamed reply's text and reasoning bytes until its first tool call.+pub const ReplySize = struct {+ text: usize = 0,+ reasoning: usize = 0,+ tool_seen: bool = false,++ /// True once the reply should be stopped as a runaway.+ pub fn feed(self: *ReplySize, agent: *const Agent, event: Value, text_len: usize) bool {+ if (self.tool_seen) return false;+ const kind = agent.provider.kind;+ if (startsTool(kind, event)) {+ self.tool_seen = true;+ return false;+ }+ self.text += text_len;+ self.reasoning += reasoningDelta(kind, event).len;+ if (!armed(agent)) return false;+ return self.text > runaway_bytes or (summaryReasoning(agent.provider.model) and self.reasoning > runaway_bytes);+ }+};++/// Providers that stream reasoning summaries (gemini-*): a long summary is a+/// runaway. Raw-reasoning models often pass the limit before a good tool+/// call, so only their visible text counts.+fn summaryReasoning(model: []const u8) bool {+ const leaf = if (std.mem.lastIndexOfScalar(u8, model, '/')) |i| model[i + 1 ..] else model;+ return std.mem.startsWith(u8, leaf, "gemini");+}++fn startsTool(kind: Kind, v: Value) bool {+ switch (kind) {+ .openai => {+ const d = chatDelta(v) orelse return false;+ return get(d, "tool_calls") != null or get(d, "function_call") != null;+ },+ .anthropic => {+ if (!std.mem.eql(u8, str(get(v, "type")), "content_block_start")) return false;+ return std.mem.eql(u8, str(get(get(v, "content_block") orelse return false, "type")), "tool_use");+ },+ .responses => {+ const ty = str(get(v, "type"));+ if (std.mem.eql(u8, ty, "response.function_call_arguments.delta")) return true;+ if (!std.mem.eql(u8, ty, "response.output_item.added")) return false;+ return std.mem.eql(u8, str(get(get(v, "item") orelse return false, "type")), "function_call");+ },+ .interactions => {+ if (get(v, "delta")) |d| {+ const ty = str(get(d, "type"));+ if (std.mem.eql(u8, ty, "function_call") or std.mem.eql(u8, ty, "arguments_delta")) return true;+ }+ if (get(v, "step")) |s| return std.mem.eql(u8, str(get(s, "type")), "function_call");+ return false;+ },+ }+}++fn reasoningDelta(kind: Kind, v: Value) []const u8 {+ switch (kind) {+ .openai => {+ const d = chatDelta(v) orelse return "";+ const r = str(get(d, "reasoning_content"));+ return if (r.len > 0) r else str(get(d, "reasoning"));+ },+ .anthropic => {+ if (!std.mem.eql(u8, str(get(v, "type")), "content_block_delta")) return "";+ const d = get(v, "delta") orelse return "";+ return if (std.mem.eql(u8, str(get(d, "type")), "thinking_delta")) str(get(d, "thinking")) else "";+ },+ .responses => return if (std.mem.eql(u8, str(get(v, "type")), "response.reasoning_summary_text.delta")) str(get(v, "delta")) else "",+ .interactions => {+ const d = get(v, "delta") orelse return "";+ if (!std.mem.eql(u8, str(get(d, "type")), "thought_summary")) return "";+ const content = str(get(get(d, "content") orelse return str(get(d, "text")), "text"));+ return if (content.len > 0) content else str(get(d, "text"));+ },+ }+}++fn chatDelta(v: Value) ?Value {+ const choices = get(v, "choices") orelse return null;+ if (choices != .array or choices.array.items.len == 0) return null;+ return get(choices.array.items[0], "delta");+}++fn get(v: Value, key: []const u8) ?Value {+ return if (v == .object) v.object.get(key) else null;+}++fn str(v: ?Value) []const u8 {+ const x = v orelse return "";+ return if (x == .string) x.string else "";+}++fn strIn(obj: std.json.ObjectMap, name: []const u8) ?[]const u8 {+ const v = obj.get(name) orelse return null;+ return if (v == .string) v.string else null;+}++fn parse(arena: std.mem.Allocator, raw: []const u8) !Value {+ return std.json.parseFromSliceLeaky(Value, arena, raw, .{});+}++test "cutAtLimit: chat, Anthropic, Responses and Interactions limit stops; normal stops are not" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const a = arena_state.allocator();+ for ([_][]const u8{+ \\{"choices":[{"index":0,"message":{"role":"assistant","content":"wait..."},"finish_reason":"length"}]}+ ,+ \\{"type":"message","stop_reason":"max_tokens","content":[{"type":"text","text":"wait..."}]}+ ,+ \\{"status":"incomplete","incomplete_details":{"reason":"max_output_tokens"},"output":[]}+ ,+ \\{"status":"incomplete","outputs":[]}+ }) |raw| try std.testing.expect(cutAtLimit((try parse(a, raw)).object));+ for ([_][]const u8{+ \\{"choices":[{"index":0,"message":{"role":"assistant","content":"done"},"finish_reason":"stop"}]}+ ,+ \\{"choices":[]}+ ,+ \\{"type":"message","stop_reason":"end_turn","content":[]}+ ,+ \\{"status":"completed","output":[]}+ }) |raw| try std.testing.expect(!cutAtLimit((try parse(a, raw)).object));+}++test "ReplySize: text past the limit stops a mid-task reply; a tool call, an unarmed turn, or raw reasoning does not" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const a = arena_state.allocator();+ var agent: Agent = undefined;+ agent.provider.kind = .openai;+ agent.provider.model = "codegraff/gemini-4-argon";+ agent.length_cutoff_used = false;+ agent.tool_calls_this_turn = 1;+ agent.compaction_request = false;+ agent.output_schema = null;++ const text_ev = try parse(a, "{\"choices\":[{\"index\":0,\"delta\":{\"content\":\"wait\"}}]}");+ var size: ReplySize = .{};+ try std.testing.expect(!size.feed(&agent, text_ev, runaway_bytes));+ try std.testing.expect(size.feed(&agent, text_ev, 1));++ // A tool call that started first ends the count for this reply.+ const tool_ev = try parse(a, "{\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0}]}}]}");+ var with_tool: ReplySize = .{};+ try std.testing.expect(!with_tool.feed(&agent, tool_ev, 0));+ try std.testing.expect(!with_tool.feed(&agent, text_ev, runaway_bytes * 2));++ // No tool call yet this turn, or already rewound once: never stopped.+ agent.tool_calls_this_turn = 0;+ var first: ReplySize = .{};+ try std.testing.expect(!first.feed(&agent, text_ev, runaway_bytes * 2));+ agent.tool_calls_this_turn = 1;+ agent.length_cutoff_used = true;+ var spent: ReplySize = .{};+ try std.testing.expect(!spent.feed(&agent, text_ev, runaway_bytes * 2));+ agent.length_cutoff_used = false;++ // Reasoning counts only for summary-reasoning (Gemini) models.+ var big: std.ArrayList(u8) = .empty;+ try big.appendNTimes(a, 'x', runaway_bytes + 1);+ const reason_raw = try std.fmt.allocPrint(a, "{{\"choices\":[{{\"index\":0,\"delta\":{{\"reasoning_content\":\"{s}\"}}}}]}}", .{big.items});+ const reason_ev = try parse(a, reason_raw);+ var gem: ReplySize = .{};+ try std.testing.expect(gem.feed(&agent, reason_ev, 0));+ agent.provider.model = "codegraff/deepseek-flash";+ var raw: ReplySize = .{};+ try std.testing.expect(!raw.feed(&agent, reason_ev, 0));+}diff --git a/src/agent_model_loop.zig b/src/agent_model_loop.zigindex d0f287fd..73dd77b1 100644--- a/src/agent_model_loop.zig+++ b/src/agent_model_loop.zig@@ -12,13 +12,17 @@ pub const marker = "stopped: model loop";pub const Stream = struct {detector: Detector = .{},+ /// This reply's size before any tool call (agent_length_cutoff.zig).+ size: @import("agent_length_cutoff.zig").ReplySize = .{},- /// Parse only actual assistant text. Never inspect reasoning or tool args.+ /// The loop detector reads only assistant text; the runaway size check also+ /// counts reasoning and stops counting once a tool call starts./// Capture works even when the frontend doesn't paint streaming deltas.pub fn event(self: *Stream, agent: *Agent, payload: []const u8, paint: bool) !void {const parsed = std.json.parseFromSlice(Value, agent.gpa, payload, .{}) catch return;defer parsed.deinit();const text = delta(agent.provider.kind, parsed.value);+ if (self.size.feed(agent, parsed.value, text.len)) return error.ReplyRunaway;if (text.len == 0) return;const enabled = !agent.compaction_request and agent.output_schema == null;const n = if (enabled) self.detector.feed(text) else text.len;diff --git a/src/agent_request.zig b/src/agent_request.zigindex 2beda2e0..8e63e9ab 100644--- a/src/agent_request.zig+++ b/src/agent_request.zig@@ -257,7 +257,7 @@ pub fn request(self: *Agent, tools_in: ?[]const u8) !std.json.ObjectMap {postWatched(self.gpa, self.io, self.client, self.provider, body, conv);if (attempt_body) |ok| break :blk ok else |err| {if (@import("agent_async_tools.zig").started(self)) return err;- if (err == error.ModelLoop) return err; // #743: repeated prose ends the turn WITHOUT retry (tier2 model-loop-bounded-prose)+ if (err == error.ModelLoop or err == error.ReplyRunaway) return err; // #743: repeated prose ends the turn WITHOUT retry (tier2 model-loop-bounded-prose)if (self.streamed_text) if (self.out) |w| {w.writeAll("\n") catch {};w.flush() catch {};diff --git a/src/agent_turn.zig b/src/agent_turn.zigindex 81e09337..64bcf161 100644--- a/src/agent_turn.zig+++ b/src/agent_turn.zig@@ -16,6 +16,7 @@ pub fn run(self: *Agent, root_turn_prepared: *std.atomic.Value(bool)) anyerror![defer if (!self.sub) @import("peer_idle.zig").noteTurnEnd();var pending_work = empty_completion.PendingWork.begin(self);var bounced_answer: empty_completion.BounceAnswer = .{};+ self.length_cutoff_used = false; // agent_length_cutoff.zig: once per turnself.completed = null;self.yielded = null;self.read_miss.reset();@@ -74,11 +75,15 @@ pub fn run(self: *Agent, root_turn_prepared: *std.atomic.Value(bool)) anyerror![}}const hist_len = self.messages.items.len;- const root = self.request(if (self.text_only) null else self.toolsJson()) catch |err| return review_deadline.finish(try @import("agent_model_loop.zig").finishError(self, err));+ const root = self.request(if (self.text_only) null else self.toolsJson()) catch |err| {+ if (try @import("agent_length_cutoff.zig").recoverRunaway(self, err, hist_len)) continue;+ return review_deadline.finish(try @import("agent_model_loop.zig").finishError(self, err));+ };const done = try @import("agent_steps.zig").stepForWire(self, root);if (done) |final_text| {if (self.yielded != null) return review_deadline.finish(final_text);if (@import("tool_call_repair.zig").endsTurn(self, final_text)) return review_deadline.finish(final_text); // final: no retry/nudge+ if (try @import("agent_length_cutoff.zig").handle(self, root, hist_len)) continue;// Retry empty replies; reconcile plain finals with live work (#745).if (try bounced_answer.retry(self, final_text, hist_len)) continue;if (try @import("named_work.zig").handle(self, final_text)) continue;diff --git a/src/agent_ws.zig b/src/agent_ws.zigindex fb7984f1..b5db7c5a 100644--- a/src/agent_ws.zig+++ b/src/agent_ws.zig@@ -119,7 +119,7 @@ pub fn postLive(self: *Agent, body: []const u8) ![]u8 {if (!wsEligible(self)) return self.postStream(body);const response = postResponsesWs(self, body) catch |e| {if (@import("agent_async_tools.zig").started(self)) return e;- if (e == error.Interrupted or e == error.StreamStalled or e == error.ModelLoop or e == error.Steered) return e;+ if (e == error.Interrupted or e == error.StreamStalled or e == error.ModelLoop or e == error.ReplyRunaway or e == error.Steered) return e;// Preemptive idle expiry is not a failed transport attempt; it only asks// request() to rebuild the already-created delta as full input.if (e == error.CodexWsReanchor) return e;diff --git a/src/main.zig b/src/main.zigindex a2b160f7..39cf71db 100644--- a/src/main.zig+++ b/src/main.zig@@ -564,7 +564,7 @@ test { // ── Unit tests (`zig build test`): pull in tests from imported modu_ = .{ @import("mcp_server_tests.zig"), @import("main_test.zig"), @import("artifact_claim_command.zig"), @import("cli_path_hint.zig"), @import("run_budget.zig") };_ = .{ @import("session_catalog.zig"), @import("session_prompt.zig"), @import("session_discovery.zig"), @import("session_workspaces.zig"), @import("commands_resume.zig"), @import("peer_live.zig"), @import("presence_tests.zig"), @import("mcp_rpc_tests.zig") };_ = .{ @import("prompt_astra.zig"), @import("prompt_guidance.zig"), @import("oauth_chatgpt.zig"), @import("chatgpt_reauth.zig") }; // per-model guidance (Astra, GPT-5.6); ChatGPT plan sign-in (ADR 0253)- _ = .{ @import("agent_empty_completion.zig"), @import("agent_model_loop.zig"), @import("publication_policy_tests.zig"), @import("jobs_completion_tests.zig") };+ _ = .{ @import("agent_empty_completion.zig"), @import("agent_length_cutoff.zig"), @import("agent_model_loop.zig"), @import("publication_policy_tests.zig"), @import("jobs_completion_tests.zig") };_ = .{ @import("provider_routing_tests.zig"), @import("codegraff_messages_tests.zig"), @import("tool_result_empty_tests.zig"), @import("history_wire.zig"), @import("path_lookup.zig"), @import("agents_prepare.zig"), @import("catalog_price.zig"), @import("readline_paste_number_tests.zig"), @import("test_hooks.zig"), @import("peer_wake_loop.zig") };_ = .{ @import("list_dir_nearmiss.zig"), @import("tool_preview_tests.zig") };_ = @import("repo_map.zig");