graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
End a turn that repeats an identical tool call (#1706)
Rach Pradhan033c1cd0f9a0
diff --git a/evals/harness_behavior.jsonl b/evals/harness_behavior.jsonlindex 73188640..9702c6ca 100644--- a/evals/harness_behavior.jsonl+++ b/evals/harness_behavior.jsonl@@ -134,3 +134,5 @@{"id": "headless-server-does-not-hold", "why": "Run until idle: a server never reports, so a turn that leaves one running in the background ends instead of waiting on it.", "env": {"GRAFF_NO_CODEDB_GUARD": "1", "GRAFF_HEARTBEAT_SECS": "1"}, "prompt": "Start the dev server in the background and confirm it launched.", "script": [{"tool": "bash", "arguments": {"command": "python3 -m http.server 0 --bind 127.0.0.1", "run_in_background": true}}, {"text": "The dev server is running."}], "assert": [{"request_count": 2}, {"final_text_contains": "The dev server is running."}]}{"id": "claude-conversation-import-resume", "why": "Imported history uses the configured graff route without Claude credentials, retaining dialogue and historical tool results.", "env": {"ANTHROPIC_API_KEY": ""}, "args": ["--resume", "claude-import-fixture"], "claude_transcripts": {"import-fixture.jsonl": "{\"type\": \"user\", \"uuid\": \"u1\", \"message\": {\"content\": \"import-retained-question-1495\"}}\n{\"type\": \"assistant\", \"uuid\": \"a1\", \"message\": {\"model\": \"claude-sonnet-4-5\", \"content\": [{\"type\": \"thinking\", \"thinking\": \"import-private-thinking-1495\", \"signature\": \"signed\"}, {\"type\": \"text\", \"text\": \"import-retained-answer-1495\"}, {\"type\": \"tool_use\", \"id\": \"tool-import-1495\", \"name\": \"Bash\", \"input\": {\"command\": \"echo imported\"}}]}}\n{\"type\": \"user\", \"message\": {\"content\": [{\"type\": \"tool_result\", \"tool_use_id\": \"tool-import-1495\", \"content\": \"import-retained-result-1495\"}]}}\n{\"type\": \"assistant\", \"isSidechain\": true, \"message\": {\"content\": [{\"type\": \"text\", \"text\": \"import-sidechain-1495\"}]}}\n{\"type\": \"assistant\", \"message\": {\"content\": [{\"type\": \"text\", \"text\": \"import-finished-1495\"}]}}\n"}, "controls_before_prompt": [{"type": "set_model", "provider": "lmstudio", "model": "mock"}], "prompt": "Continue the imported task.", "script": [{"text": "Imported history is available."}], "assert": [{"request_contains": {"index": 0, "text": "import-retained-question-1495"}}, {"request_contains": {"index": 0, "text": "import-retained-answer-1495"}}, {"request_contains": {"index": 0, "text": "import-retained-result-1495"}}, {"request_contains": {"index": 0, "text": "import-finished-1495"}}, {"request_lacks": {"index": 0, "text": "import-private-thinking-1495"}}, {"request_lacks": {"index": 0, "text": "import-sidechain-1495"}}, {"final_text_contains": "Imported history is available."}]}{"id": "screenshot-handoff-preserves-open-work-1531", "why": "#1531: a screenshot needed on the next prompt must end the current turn without a pause question, completion claim, verifier approval, or open-work retry, and resume with the unfinished checklist intact.", "args": ["--no-lean", "--goal", "fix the control after visual identification", "--eval", "printf \"0\\n\""], "prompt": "The question interface accepts text only. A screenshot is essential to identify the control. Stop this turn so I can attach it to my next prompt.", "second_prompt": "Continue with the new input. Read back the unfinished checklist before proceeding.", "script": [{"tool": "todo_write", "arguments": {"todos": [{"content": "identify the control from the screenshot", "status": "in_progress"}]}}, {"tool": "eval", "arguments": {}}, {"tool": "yield_turn", "arguments": {"message": "Stopping this turn so you can attach the screenshot to your next prompt. The task remains unfinished."}}, {"tool": "todo_read", "arguments": {}}, {"tool": "yield_turn", "arguments": {"message": "The checklist still has the unfinished visual identification item; this turn is stopping until the missing input arrives."}}], "assert": [{"request_contains": {"index": 0, "text": "explicitly supports attachments in question replies"}}, {"request_contains": {"index": 0, "text": "Do not ask for permission to pause"}}, {"request_lacks": {"index": 0, "text": "typed text and/or pasted images"}}, {"event": {"type": "turn", "api_calls": 3, "complete": true, "text": "Stopping this turn so you can attach the screenshot to your next prompt. The task remains unfinished."}}, {"events_at_least": {"match": {"type": "turn"}, "count": 2}}, {"events_at_most": {"match": {"type": "turn"}, "count": 2}}, {"no_event": {"type": "ask_user"}}, {"no_event": {"type": "tool_call", "name": "attempt_completion"}}, {"request_contains": {"index": 4, "text": "identify the control from the screenshot"}}, {"request_contains": {"index": 4, "text": "in_progress"}}, {"request_lacks": {"index": 4, "text": "Open-work reconciliation"}}, {"final_text_contains": "checklist still has the unfinished"}]}+{"id": "repeat-identical-call-stops-the-turn", "why": "#1706: the same call with the same result must not repeat without bound. The 4th identical batch carries the no-progress notice (the 3rd does not), and the 8th ends the turn with the stop text, so the 9th request is never sent.", "prompt": "Check the box.", "script": [{"tool": "bash", "arguments": {"command": "echo ok"}}, {"tool": "bash", "arguments": {"command": "echo ok"}}, {"tool": "bash", "arguments": {"command": "echo ok"}}, {"tool": "bash", "arguments": {"command": "echo ok"}}, {"tool": "bash", "arguments": {"command": "echo ok"}}, {"tool": "bash", "arguments": {"command": "echo ok"}}, {"tool": "bash", "arguments": {"command": "echo ok"}}, {"tool": "bash", "arguments": {"command": "echo ok"}}, {"tool": "bash", "arguments": {"command": "echo ok"}}, {"tool": "bash", "arguments": {"command": "echo ok"}}, {"tool": "bash", "arguments": {"command": "echo ok"}}, {"tool": "bash", "arguments": {"command": "echo ok"}}, {"text": "never reached"}], "assert": [{"request_lacks": {"index": 3, "text": "times in a row"}}, {"request_contains": {"index": 4, "text": "times in a row"}}, {"request_count": 8}, {"final_text_contains": "Stopped: the model repeated the same tool call"}]}+{"id": "varying-calls-do-not-trip-the-repeat-guard", "why": "#1706 counterfactual: ten calls that differ in input all run, no notice is sent, and the turn ends on the model's own reply.", "prompt": "Run the steps.", "script": [{"tool": "bash", "arguments": {"command": "echo step 0"}}, {"tool": "bash", "arguments": {"command": "echo step 1"}}, {"tool": "bash", "arguments": {"command": "echo step 2"}}, {"tool": "bash", "arguments": {"command": "echo step 3"}}, {"tool": "bash", "arguments": {"command": "echo step 4"}}, {"tool": "bash", "arguments": {"command": "echo step 5"}}, {"tool": "bash", "arguments": {"command": "echo step 6"}}, {"tool": "bash", "arguments": {"command": "echo step 7"}}, {"tool": "bash", "arguments": {"command": "echo step 8"}}, {"tool": "bash", "arguments": {"command": "echo step 9"}}, {"text": "all steps ran"}], "assert": [{"request_count": 11}, {"request_lacks": {"index": -1, "text": "times in a row"}}, {"final_text_contains": "all steps ran"}]}diff --git a/src/agent.zig b/src/agent.zigindex 6eec14be..efb1674b 100644--- a/src/agent.zig+++ b/src/agent.zig@@ -130,6 +130,7 @@ pub const Agent = struct {tools_used: trace.ToolSink = .{}, // external tool calls this agent made (per turn for the root)tool_calls_this_turn: u64 = 0,read_miss: @import("read_miss.zig").Tracker = .{}, // #1116: same-prefix / incrementing read_file misses+ tool_repeat: @import("tool_repeat.zig").Tracker = .{}, // #1706: identical batches in a row end the turnmodel_calls_this_turn: u64 = 0,handle_note_shown: bool = false, // #541: the handle-protocol lesson rides this agent's FIRST handle, onceseen_tool_keys: std.ArrayList([]const u8) = .empty, // root-only per-turn dedupe keysdiff --git a/src/agent_tools.zig b/src/agent_tools.zigindex 83425230..93d30322 100644--- a/src/agent_tools.zig+++ b/src/agent_tools.zig@@ -176,6 +176,7 @@ pub fn runTools(self: *Agent, calls: []const ToolCall) ![]ExecResult {};// Show a compact ✓/✗ + preview for each non-meta call (no-op for subs).for (calls, results) |call, r| self.sayToolResult(call, r);+ try @import("tool_repeat.zig").observe(self, calls, results); // #1706, after display: the notice is for the modelreturn results;}diff --git a/src/agent_turn.zig b/src/agent_turn.zigindex 97f4fcc9..2fa59caf 100644--- a/src/agent_turn.zig+++ b/src/agent_turn.zig@@ -23,6 +23,7 @@ pub fn run(self: *Agent, root_turn_prepared: *std.atomic.Value(bool)) anyerror![self.completed = null;self.yielded = null;self.read_miss.reset();+ self.tool_repeat.reset();self.mcp_context.begin(self.io);@import("named_work.zig").beginTurn(self);try self.ensureRootTools(self.provider.kind);@@ -90,6 +91,12 @@ pub fn run(self: *Agent, root_turn_prepared: *std.atomic.Value(bool)) anyerror![const reply_start = self.messages.items.len;defer @import("agent_length_cutoff.zig").discardEmptyReply(self, root, reply_start);const done = try length.step(self, root);+ if (done == null and self.tool_repeat.stopped()) { // #1706: every wire's tool batch lands here+ const stop = @import("tool_repeat.zig").stop_text;+ try self.say("{s}\n", .{stop});+ _ = @import("tool_call_repair.zig").endsTurn(self, stop);+ return review_deadline.finish(stop);+ }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/nudgediff --git a/src/tool_call_repair.zig b/src/tool_call_repair.zigindex 9f2f0f03..c7e1c8de 100644--- a/src/tool_call_repair.zig+++ b/src/tool_call_repair.zig@@ -25,7 +25,7 @@ pub const loop_stop_text = "Stopped: the model sent tool calls with unusable arg/// The turn loop ends on this text outright: no retry, nudge, or open-work pass.pub fn isLoopStop(text: []const u8) bool {- return text.ptr == loop_stop_text.ptr;+ return text.ptr == loop_stop_text.ptr or text.ptr == @import("tool_repeat.zig").stop_text.ptr;}/// runTurn's check for that stop; a child also closes its feedback inbox.@@ -507,5 +507,5 @@ pub fn brokenCallLoop(messages: []const Value) bool {}test {- _ = @import("tool_call_repair_tests.zig");+ _ = .{ @import("tool_call_repair_tests.zig"), @import("tool_repeat.zig") };}diff --git a/src/tool_repeat.zig b/src/tool_repeat.zignew file mode 100644index 00000000..9f57cf1c--- /dev/null+++ b/src/tool_repeat.zig@@ -0,0 +1,136 @@+//! Identical tool-batch loop guard (#1706).+//!+//! A model can repeat one call with the same input, get the same result, and+//! keep going until a wall clock kills the run. Nothing else ends that turn:+//! `tool_call_repair.brokenCallLoop` only covers unusable arguments, and the+//! dedupe gate in `agent_tools.rejectToolCall` is opt-in. Each executed batch+//! folds into a fingerprint of its normalized calls and their results; the+//! `nudge_at`-th identical batch in a row tells the model so, and the+//! `stop_at`-th ends the turn. A batch still waiting on work is never counted.+//! Reset at `runTurn`.++const std = @import("std");+const tools = @import("tools.zig");++pub const nudge_at: u8 = 4;+pub const stop_at: u8 = 8;++pub const nudge_text = std.fmt.comptimePrint("[graff] This call ran with this input and returned this result {d} times in a row. Repeating it will not make progress: take a different step, or finish with what you have.", .{nudge_at});++pub const stop_text = std.fmt.comptimePrint("Stopped: the model repeated the same tool call with the same result {d} times in a row, so the turn ended instead of running it again. Send a new message to continue, or switch models.", .{stop_at});++/// Turn-scoped run of identical batches. No heap.+pub const Tracker = struct {+ last: u64 = 0,+ run: u8 = 0,++ pub fn reset(self: *Tracker) void {+ self.* = .{};+ }++ /// Folds one batch; a null fingerprint (work still pending) breaks the run.+ pub fn note(self: *Tracker, fp: ?u64) u8 {+ const f = fp orelse {+ self.* = .{};+ return 0;+ };+ if (self.run > 0 and f == self.last) {+ self.run +|= 1;+ } else {+ self.last = f;+ self.run = 1;+ }+ return self.run;+ }++ pub fn stopped(self: *const Tracker) bool {+ return self.run >= stop_at;+ }+};++/// Null when any result is pending or cancelled: waiting is not repeating.+pub fn fingerprint(keys: []const []const u8, results: []const tools.ExecResult) ?u64 {+ var h = std.hash.Wyhash.init(0x1706);+ for (keys, results) |key, r| {+ if (r.pending or r.cancelled) return null;+ h.update(key);+ h.update(&.{ 0, @intFromBool(r.is_error) });+ h.update(r.text);+ h.update(&.{0});+ }+ return h.final();+}++/// After a batch ran: count it, and on the nudge append the notice to the+/// last result so the model reads it with what it just got back.+pub fn observe(self: anytype, calls: []const tools.ToolCall, results: []tools.ExecResult) !void {+ if (calls.len == 0) return;+ const keys = try self.arena.alloc([]const u8, calls.len);+ for (calls, keys) |call, *key| key.* = try self.toolDedupeKey(call);+ if (self.tool_repeat.note(fingerprint(keys, results)) != nudge_at) return;+ const last = &results[results.len - 1];+ last.text = try std.fmt.allocPrint(self.arena, "{s}\n\n{s}", .{ last.text, nudge_text });+}++test "identical batches build a run that stops at stop_at" {+ var t: Tracker = .{};+ const keys = [_][]const u8{"shell\n{\"command\":\"echo ok\"}"};+ const ok = [_]tools.ExecResult{.{ .text = "ok\n", .is_error = false }};+ var i: u8 = 1;+ while (i < stop_at) : (i += 1) {+ try std.testing.expectEqual(i, t.note(fingerprint(&keys, &ok)));+ try std.testing.expect(!t.stopped());+ }+ try std.testing.expectEqual(stop_at, t.note(fingerprint(&keys, &ok)));+ try std.testing.expect(t.stopped());+ t.reset();+ try std.testing.expect(!t.stopped());+}++test "a different input, result, or error flag starts a new run" {+ var t: Tracker = .{};+ const a = [_][]const u8{"shell\n{\"command\":\"echo ok\"}"};+ const b = [_][]const u8{"shell\n{\"command\":\"echo ko\"}"};+ const ok = [_]tools.ExecResult{.{ .text = "ok\n", .is_error = false }};+ const ok_err = [_]tools.ExecResult{.{ .text = "ok\n", .is_error = true }};+ const other = [_]tools.ExecResult{.{ .text = "ok 2\n", .is_error = false }};+ _ = t.note(fingerprint(&a, &ok));+ try std.testing.expectEqual(@as(u8, 2), t.note(fingerprint(&a, &ok)));+ try std.testing.expectEqual(@as(u8, 1), t.note(fingerprint(&b, &ok)));+ try std.testing.expectEqual(@as(u8, 1), t.note(fingerprint(&b, &other)));+ try std.testing.expectEqual(@as(u8, 1), t.note(fingerprint(&b, &ok_err)));+}++test "a pending or cancelled result never counts and breaks the run" {+ var t: Tracker = .{};+ const keys = [_][]const u8{"agent_output\n{\"id\":\"a1\"}"};+ const done = [_]tools.ExecResult{.{ .text = "still running", .is_error = false }};+ const waiting = [_]tools.ExecResult{.{ .text = "still running", .is_error = false, .pending = true }};+ const cancelled = [_]tools.ExecResult{.{ .text = "still running", .is_error = false, .cancelled = true }};+ _ = t.note(fingerprint(&keys, &done));+ _ = t.note(fingerprint(&keys, &done));+ try std.testing.expectEqual(@as(u8, 0), t.note(fingerprint(&keys, &waiting)));+ try std.testing.expectEqual(@as(u8, 1), t.note(fingerprint(&keys, &done)));+ try std.testing.expectEqual(@as(u8, 0), t.note(fingerprint(&keys, &cancelled)));+}++test "observe appends the notice on the nudge batch only" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const Fake = struct {+ arena: std.mem.Allocator,+ tool_repeat: Tracker = .{},+ fn toolDedupeKey(_: *@This(), call: tools.ToolCall) ![]const u8 {+ return call.name;+ }+ };+ var fake: Fake = .{ .arena = arena_state.allocator() };+ const calls = [_]tools.ToolCall{.{ .id = "c", .name = "shell", .input = .null }};+ var i: u8 = 1;+ while (i <= nudge_at + 1) : (i += 1) {+ var results = [_]tools.ExecResult{.{ .text = "ok\n", .is_error = false }};+ try observe(&fake, &calls, &results);+ const noticed = std.mem.indexOf(u8, results[0].text, nudge_text) != null;+ try std.testing.expectEqual(i == nudge_at, noticed);+ }+}