Skip to content

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

Python

turn: stop every mid-task runaway reply (two rewinds per turn) (#1653)

Rach3b9131e2cd0e
diff --git a/src/agent.zig b/src/agent.zig
index c9d3c7b4..aa820a89 100644
--- a/src/agent.zig
+++ b/src/agent.zig
@@ -223,7 +223,7 @@ pub const Agent = struct {
compact_pin_degraded: bool = false, // #581: pinned suffix over budget — skip+say once per unresolved turn
compact_stall: @import("compact_cut.zig").Stall = .{}, // #706: same-boundary no-progress cuts
empty_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)
+ length_cutoffs: u8 = 0, // cut-off or runaway replies 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 pointer
named_work_settled: []const u8 = "", // mention already nudged or tooled
diff --git a/src/agent_length_cutoff.zig b/src/agent_length_cutoff.zig
index 16e131b3..b8030415 100644
--- a/src/agent_length_cutoff.zig
+++ b/src/agent_length_cutoff.zig
@@ -4,11 +4,14 @@
//! 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:
+//! both rewinding the reply and asking for the next action:
//! * `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.
+//! A turn gets `max_rewinds` rewinds; a runaway after that still stops the
+//! stream, and the turn ends like a model loop (agent_model_loop.finishError)
+//! instead of streaming to the output 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.
@@ -27,6 +30,10 @@ pub const note = "Your previous reply ran long without a tool call and was cut o
/// that end in a tool call stay well under it.
pub const runaway_bytes: usize = 24 * 1024;
+/// Rewinds per turn. Argon can run away again after a rewind (a second
+/// runaway in one turn streamed 180 KB for 145 s to the output cap).
+pub const max_rewinds: u8 = 2;
+
/// 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.
@@ -43,10 +50,14 @@ pub fn cutAtLimit(root: std.json.ObjectMap) bool {
return false;
}
-/// Mid-task (the turn has used tools) and not yet spent this turn.
+/// Mid-task: the turn has used tools, outside compaction and schema replies.
+fn midTask(self: *const Agent) bool {
+ return self.tool_calls_this_turn > 0 and !self.compaction_request and self.output_schema == null;
+}
+
+/// Mid-task with a rewind left 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;
+ return self.length_cutoffs < max_rewinds and midTask(self);
}
/// Rewind a mid-task reply the provider cut at its output limit and add the
@@ -57,8 +68,9 @@ pub fn handle(self: *Agent, root: std.json.ObjectMap, hist_len: usize) !bool {
}
/// The stream stopped itself (`error.ReplyRunaway`): rewind the same way.
+/// False with no rewind left: finishError ends the turn.
pub fn recoverRunaway(self: *Agent, err: anyerror, hist_len: usize) !bool {
- if (err != error.ReplyRunaway) return false;
+ if (err != error.ReplyRunaway or self.length_cutoffs >= max_rewinds) return false;
self.partial_text.clearRetainingCapacity();
if (self.streamed_text) if (self.out) |w| {
w.writeAll("\n") catch {};
@@ -71,7 +83,7 @@ pub fn recoverRunaway(self: *Agent, err: anyerror, hist_len: usize) !bool {
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;
+ self.length_cutoffs += 1;
const keep = @min(hist_len, self.messages.items.len);
self.closeCodexWs();
self.messages.shrinkRetainingCapacity(keep);
@@ -97,7 +109,7 @@ pub const ReplySize = struct {
}
self.text += text_len;
self.reasoning += reasoningDelta(kind, event).len;
- if (!armed(agent)) return false;
+ if (!midTask(agent)) return false;
return self.text > runaway_bytes or (summaryReasoning(agent.provider.model) and self.reasoning > runaway_bytes);
}
};
@@ -214,7 +226,7 @@ test "ReplySize: text past the limit stops a mid-task reply; a tool call, an una
var agent: Agent = undefined;
agent.provider.kind = .openai;
agent.provider.model = "codegraff/gemini-4-argon";
- agent.length_cutoff_used = false;
+ agent.length_cutoffs = 0;
agent.tool_calls_this_turn = 1;
agent.compaction_request = false;
agent.output_schema = null;
@@ -230,15 +242,18 @@ test "ReplySize: text past the limit stops a mid-task reply; a tool call, an una
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.
+ // No tool call yet this turn: 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;
+
+ // Rewinds spent: still stopped (the turn then ends instead of rewinding).
+ agent.length_cutoffs = max_rewinds;
var spent: ReplySize = .{};
- try std.testing.expect(!spent.feed(&agent, text_ev, runaway_bytes * 2));
- agent.length_cutoff_used = false;
+ try std.testing.expect(spent.feed(&agent, text_ev, runaway_bytes * 2));
+ try std.testing.expect(!try recoverRunaway(&agent, error.ReplyRunaway, 0));
+ agent.length_cutoffs = 0;
// Reasoning counts only for summary-reasoning (Gemini) models.
var big: std.ArrayList(u8) = .empty;
diff --git a/src/agent_model_loop.zig b/src/agent_model_loop.zig
index 73dd77b1..4e942de2 100644
--- a/src/agent_model_loop.zig
+++ b/src/agent_model_loop.zig
@@ -140,7 +140,8 @@ pub fn checkResponse(agent: *Agent, root: std.json.ObjectMap) !void {
/// only unseen text plus a short marker. The ordinary final event remains the
/// caller's responsibility, exactly as for an ordinary completed answer.
pub fn finishError(agent: *Agent, err: anyerror) ![]const u8 {
- if (err != error.ModelLoop) return err;
+ // A runaway with no rewind left ends the turn the same way (agent_length_cutoff.zig).
+ if (err != error.ModelLoop and err != error.ReplyRunaway) return err;
return finish(agent);
}
diff --git a/src/agent_turn.zig b/src/agent_turn.zig
index 64bcf161..33f802a5 100644
--- a/src/agent_turn.zig
+++ b/src/agent_turn.zig
@@ -16,7 +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 turn
+ self.length_cutoffs = 0; // agent_length_cutoff.zig: rewinds this turn
self.completed = null;
self.yielded = null;
self.read_miss.reset();