graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
Keep the repeat-guard count across a worker's re-ask (#1706)
Rach Pradhand6d37d867b13
diff --git a/evals/harness_behavior.jsonl b/evals/harness_behavior.jsonlindex d74d2ed8..f4634067 100644--- a/evals/harness_behavior.jsonl+++ b/evals/harness_behavior.jsonl@@ -136,3 +136,6 @@{"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"}]}+{"id": "repeat-identical-call-stops-a-workflow-synthesis-child", "why": "#1706: the guard sits in the shared turn loop, so a workflow child gets it too. The sweep phase reports, then the synthesis child repeats one call with one result; its 8th batch ends its turn and the parent reads the stop text as the synthesis result instead of the child running on.", "args": [], "prompt": "how does configuration discovery work", "script": [{"tool": "workflow", "arguments": {"phases": [{"title": "sweep config sources", "tasks": [{"description": "sweep-config", "prompt": "search for configuration discovery behavior", "isolation": "shared_cwd"}]}, {"title": "synthesize findings", "tasks": [{"description": "summary", "prompt": "summarize the findings from {{prev}}", "isolation": "shared_cwd"}]}]}}, {"text": "SWEEP-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": "the parent read the synthesis stop"}], "assert": [{"request_lacks": {"index": 5, "text": "times in a row"}}, {"request_contains": {"index": 6, "text": "times in a row"}}, {"request_count": 11}, {"request_contains": {"index": -1, "text": "Stopped: the model repeated the same tool call"}}, {"final_text_contains": "the parent read the synthesis stop"}], "timeout_s": 120}+{"id": "repeat-guard-survives-a-worker-retry", "why": "#1706: a worker's transient-failure re-ask resumes the same history through a fresh runTurn. The identical-batch count must carry over, or each re-ask buys the loop up to seven more batches. Five batches, a failed ask, then the 8th batch overall ends the worker.", "args": [], "prompt": "how does the loader decide which config source wins", "script": [{"tool": "workflow", "arguments": {"phases": [{"title": "sweep config sources", "tasks": [{"description": "sweep-config", "prompt": "search for how the loader picks a config source", "isolation": "shared_cwd"}]}]}}, {"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"}}, {"http_status": 418, "error": "scripted flake"}, {"tool": "bash", "arguments": {"command": "echo ok"}}, {"tool": "bash", "arguments": {"command": "echo ok"}}, {"tool": "bash", "arguments": {"command": "echo ok"}}, {"text": "the parent read the worker stop"}], "assert": [{"request_count": 11}, {"request_contains": {"index": -1, "text": "Stopped: the model repeated the same tool call"}}, {"final_text_contains": "the parent read the worker stop"}], "timeout_s": 120}+{"id": "repeat-guard-survives-a-stream-retry", "why": "#1706: a request-layer retry after a mid-stream failure stays inside the turn and must not reset the identical-batch count. Five batches, one failed stream, then the 8th batch overall ends the turn.", "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"}}, {"text": "partial", "stream_error": {"type": "api_error", "message": "Internal error during token parsing"}}, {"tool": "bash", "arguments": {"command": "echo ok"}}, {"tool": "bash", "arguments": {"command": "echo ok"}}, {"tool": "bash", "arguments": {"command": "echo ok"}}, {"text": "never reached"}], "assert": [{"request_count": 9}, {"final_text_contains": "Stopped: the model repeated the same tool call"}]}diff --git a/src/subagent_run.zig b/src/subagent_run.zigindex 9ba16c23..f932c6f9 100644--- a/src/subagent_run.zig+++ b/src/subagent_run.zig@@ -421,6 +421,7 @@ pub fn runSub(ctx: ToolCtx, kind: []const u8, label: []const u8, prompt: []constif (subagent_retry.pastDeadline(util.unixMs(ctx.io), ctx.loop_deadline_ms)) break :ask r;subagent_retry.traceAttempt(ctx.tracer, sub_id, attempts, fail_kind, agent.last_api_error);agent.sleepInterruptible(@import("retry_jitter.zig").ms(agent.io, subagent_retry.backoffMs(fail_kind, attempts))) catch break :ask r; // #1274+ agent.tool_repeat.carryOver(); // #1706: the re-ask resumes this history, so a looping worker keeps its count}};// A worker that ran keeps its report and usage when its state can't bediff --git a/src/tool_repeat.zig b/src/tool_repeat.zigindex 26715514..50199ee1 100644--- a/src/tool_repeat.zig+++ b/src/tool_repeat.zig@@ -9,7 +9,9 @@//! `stop_at`-th ends the turn. A batch still waiting on work is never counted://! a pending or cancelled result, or a shell job poll that reports the job is//! still running (`[job N: running…]`), which is how a model waits on CI or a-//! build. Reset at `runTurn`.+//! build. Reset at `runTurn`, except when a worker's transient-failure re-ask+//! resumes the same history (`carryOver`): a fresh count there would let a+//! looping worker repeat up to `stop_at - 1` more batches per ask.const std = @import("std");const tools = @import("tools.zig");@@ -25,11 +27,22 @@ pub const stop_text = std.fmt.comptimePrint("Stopped: the model repeated the sampub const Tracker = struct {last: u64 = 0,run: u8 = 0,+ carry: bool = false,pub fn reset(self: *Tracker) void {+ if (self.carry) {+ self.carry = false;+ return;+ }self.* = .{};}+ /// The next `reset` keeps the run: the coming `runTurn` resumes this+ /// turn's history rather than starting a new one.+ pub fn carryOver(self: *Tracker) void {+ self.carry = true;+ }+/// Folds one batch; a null fingerprint (work still pending) breaks the run.pub fn note(self: *Tracker, fp: ?u64) u8 {const f = fp orelse {@@ -96,6 +109,21 @@ test "identical batches build a run that stops at stop_at" {try std.testing.expect(!t.stopped());}+test "a carried run survives one reset, then resets clear it again" {+ 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) _ = t.note(fingerprint(&keys, &ok));+ t.carryOver();+ t.reset();+ 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());+ try std.testing.expectEqual(@as(u8, 1), t.note(fingerprint(&keys, &ok)));+}+test "a different input, result, or error flag starts a new run" {var t: Tracker = .{};const a = [_][]const u8{"shell\n{\"command\":\"echo ok\"}"};