graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
Keep the new modules under the 600-line ceiling; tier-1 baseline 2874
Rach Pradhan81ac38aeda70
diff --git a/scripts/eval/tier1-manifest.json b/scripts/eval/tier1-manifest.jsonindex e0ed8c00..43ee0eaa 100644--- a/scripts/eval/tier1-manifest.json+++ b/scripts/eval/tier1-manifest.json@@ -15,7 +15,7 @@"src/repl.zig","TUI/root.zig"],- "test_count_baseline": 2855,+ "test_count_baseline": 2874,"test_count_slack": 25,"required_invariants": [{diff --git a/src/agent.zig b/src/agent.zigindex 841ae76a..1a777b29 100644--- a/src/agent.zig+++ b/src/agent.zig@@ -395,11 +395,7 @@ pub const Agent = struct {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 done = try @import("agent_steps.zig").stepForWire(self, root);if (done) |final_text| {- // The broken-call stop is final: no retry, nudge, or open-work pass.- if (@import("tool_call_repair.zig").isLoopStop(final_text)) {- if (self.feedback) |inbox| _ = inbox.close(self.io);- 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// 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;@@ -415,8 +411,7 @@ pub const Agent = struct {// The provider round trip (request/buildBody + usage/cost recording +// Codex/Responses SSE reassembly) lives in agent_request.zig (#123,- // 600-line goal). Member-aliased so self.request(...)/etc. resolve- // unchanged.+ // 600-line goal). Member-aliased so self.request(...)/etc. resolve unchanged.pub const request = @import("agent_request.zig").request;pub const inputOverCompactThreshold = @import("agent_request.zig").inputOverCompactThreshold;pub const fullInputEstimateTokens = @import("agent_request.zig").fullInputEstimateTokens;diff --git a/src/destructive_git.zig b/src/destructive_git.zigindex 5c81054f..ca8a9d4c 100644--- a/src/destructive_git.zig+++ b/src/destructive_git.zig@@ -470,177 +470,6 @@ const Segment = struct {}};-test "#1269: destructive git in every common spelling is flagged" {- const cases = [_][]const u8{- // Spellings the substring scan already caught.- "git reset --hard HEAD~3",- "git clean -fd",- "git push --force origin main",- "git push -f",- "git branch -D worktree-docs",- "git checkout -- src/main.zig",- "git checkout .",- "git stash clear",- "git stash drop",- "git restore .",- "cd sub && git reset --hard",- "git -C /repo reset --hard",- // The spellings it missed.- "git clean -df",- "git clean -xdf",- "git clean -d -f",- "git clean --force",- "git restore -- .",- "git restore :/",- "git restore src/main.zig",- "git restore --staged --worktree .",- "git restore -SW src/a.zig",- "git restore --source=HEAD~1 src/a.zig",- "git checkout -f",- "git checkout --force main",- "git checkout :/",- "git checkout .gitignore",- "git checkout ./src/a.zig",- "git checkout '*.zig'",- "git checkout HEAD -- .",- "git checkout --ours conflicted.txt",- "git switch --discard-changes main",- "git switch -f main",- "git -C dir reset --hard",- "git reset --hard",- "git reset\t--hard origin/main",- "git -c core.editor=true reset --hard",- "git --git-dir=.git --work-tree=. reset --hard",- "git --work-tree /tmp/x clean -f",- "git -C \"$dir\" reset --hard",- "git -C $(pwd) reset --hard",- "git \"reset\" --hard",- "cd x; git clean -fdx",- "make || git reset --hard",- "(cd x && git stash drop stash@{1})",- "sh -c \"git reset --hard\"",- "bash -c 'cd x && git clean -fd'",- "echo $(git reset --hard)",- "echo `git clean -f`",- "sudo git push --force",- "GIT_DIR=x git reset --hard",- "/usr/bin/git reset --hard",- "git reset --hard 2>&1 | tail -3",- "git reset --hard>/dev/null",- "git 2>/dev/null clean -fd",- "cat <<EOF | sh\ngit reset --hard\nEOF",- "git push --force-with-lease",- "git push -uf origin main",- "git push origin +main",- "git push origin --delete old",- "git push origin :old",- "git push --mirror backup",- "git branch --delete --force x",- "git branch -d -f x",- "git branch -df x",- "git reset \\\n --hard",- // Quotes and backslashes that spell git without the literal letters.- "gi''t reset --hard",- "g\"i\"t reset --hard",- "\"g\"it clean -fdx",- "g\\it push -f",- "sh -c 'g\\it reset --hard'",- // `$(...)` inside double quotes, with quotes of its own.- "out=\"$(git -C \"$dir\" reset --hard)\"",- "msg=\"$(git -C \"$d\" clean -fd 2>&1)\"",- "echo \"$(git reset \"--hard\")\"",- "echo $(echo \")\"; git reset --hard)",- // Abbreviated long options.- "git reset --har",- "git clean --for",- "git branch --del --force z",- "git switch --discard main",- "git push --mirro x",- "git push --for",- "git push origin --del old",- "git restore --staged --wor .",- "git checkout --for main",- // Two positionals: `<tree-ish> <path>`.- "git checkout main src/main.zig",- "git checkout HEAD~1 src/a.zig",- // `&>` / `&>>` redirect, not a separator.- "git reset &>/dev/null --hard",- "git clean &>/dev/null -fd",- "git clean &>>log -fd",- // Aliases whose value is destructive.- "git config alias.x 'reset --hard' && git x",- "git config --global alias.nuke '!git clean -fdx'",- "git config set alias.x \"push --force\"",- "git -c alias.x='reset --hard' x",- "git -c 'alias.x=!git reset --hard' x",- // Case-insensitive git word (Windows).- "Git reset --hard",- "GIT.EXE clean -fd",- };- for (cases) |c| std.testing.expect(isDestructiveGit(c)) catch |err| {- std.debug.print("not flagged: {s}\n", .{c});- return err;- };-}--test "#1269: read-only and branch-only git is not flagged" {- const cases = [_][]const u8{- "git status",- "git log --oneline -5",- "git log -S\"reset --hard\"",- "git diff HEAD",- "git show HEAD:src/main.zig",- "git commit -m wip",- "git reset --soft HEAD~1",- "git reset HEAD src/a.zig",- "git checkout main",- "git checkout -b feature",- "git checkout -b feature origin/main",- "git checkout --track origin/feature",- "git checkout --detach",- "git switch main",- "git switch -c new-branch",- "git restore --staged f.txt",- "git restore --staged .",- "git restore",- "git clean -n",- "git clean -nd",- "git push",- "git push -u origin feature",- "git push origin main",- "git push origin HEAD:refs/heads/x",- "git branch -d merged-feature",- "git branch -u origin/main",- "git stash",- "git stash pop",- "git stash list",- "git stash push -m drop",- "git restore --staged f.txt 2>/dev/null",- "git stash list >stashes.txt",- "git fetch --prune",- "git -C /repo status",- "git -c color.ui=always diff",- "gitk --all",- "ls -la",- "echo reset --hard",- "",- "git checkout --det",- "git checkout --detach HEAD~1",- "git checkout --orphan gh-pages main",- "git log --grep 'reset --hard'",- "git push --follow-tags",- "git restore --staged --source=HEAD f",- "git restore --sta f",- "git config alias.st status",- "git config --global alias.lg 'log --oneline'",- "git -c alias.x=status x",- "git status &>/dev/null && echo ok",- "git status 2>&1 & git log",- "echo \"$(git status)\"",- "gitk --all &>/dev/null",- };- for (cases) |c| std.testing.expect(!isDestructiveGit(c)) catch |err| {- std.debug.print("flagged: {s}\n", .{c});- return err;- };+test {+ _ = @import("destructive_git_tests.zig");}diff --git a/src/destructive_git_tests.zig b/src/destructive_git_tests.zignew file mode 100644index 00000000..37478c30--- /dev/null+++ b/src/destructive_git_tests.zig@@ -0,0 +1,178 @@+//! Tests for destructive_git.zig (split out under the 600-line ceiling).+const std = @import("std");+const isDestructiveGit = @import("destructive_git.zig").isDestructiveGit;++test "#1269: destructive git in every common spelling is flagged" {+ const cases = [_][]const u8{+ // Spellings the substring scan already caught.+ "git reset --hard HEAD~3",+ "git clean -fd",+ "git push --force origin main",+ "git push -f",+ "git branch -D worktree-docs",+ "git checkout -- src/main.zig",+ "git checkout .",+ "git stash clear",+ "git stash drop",+ "git restore .",+ "cd sub && git reset --hard",+ "git -C /repo reset --hard",+ // The spellings it missed.+ "git clean -df",+ "git clean -xdf",+ "git clean -d -f",+ "git clean --force",+ "git restore -- .",+ "git restore :/",+ "git restore src/main.zig",+ "git restore --staged --worktree .",+ "git restore -SW src/a.zig",+ "git restore --source=HEAD~1 src/a.zig",+ "git checkout -f",+ "git checkout --force main",+ "git checkout :/",+ "git checkout .gitignore",+ "git checkout ./src/a.zig",+ "git checkout '*.zig'",+ "git checkout HEAD -- .",+ "git checkout --ours conflicted.txt",+ "git switch --discard-changes main",+ "git switch -f main",+ "git -C dir reset --hard",+ "git reset --hard",+ "git reset\t--hard origin/main",+ "git -c core.editor=true reset --hard",+ "git --git-dir=.git --work-tree=. reset --hard",+ "git --work-tree /tmp/x clean -f",+ "git -C \"$dir\" reset --hard",+ "git -C $(pwd) reset --hard",+ "git \"reset\" --hard",+ "cd x; git clean -fdx",+ "make || git reset --hard",+ "(cd x && git stash drop stash@{1})",+ "sh -c \"git reset --hard\"",+ "bash -c 'cd x && git clean -fd'",+ "echo $(git reset --hard)",+ "echo `git clean -f`",+ "sudo git push --force",+ "GIT_DIR=x git reset --hard",+ "/usr/bin/git reset --hard",+ "git reset --hard 2>&1 | tail -3",+ "git reset --hard>/dev/null",+ "git 2>/dev/null clean -fd",+ "cat <<EOF | sh\ngit reset --hard\nEOF",+ "git push --force-with-lease",+ "git push -uf origin main",+ "git push origin +main",+ "git push origin --delete old",+ "git push origin :old",+ "git push --mirror backup",+ "git branch --delete --force x",+ "git branch -d -f x",+ "git branch -df x",+ "git reset \\\n --hard",+ // Quotes and backslashes that spell git without the literal letters.+ "gi''t reset --hard",+ "g\"i\"t reset --hard",+ "\"g\"it clean -fdx",+ "g\\it push -f",+ "sh -c 'g\\it reset --hard'",+ // `$(...)` inside double quotes, with quotes of its own.+ "out=\"$(git -C \"$dir\" reset --hard)\"",+ "msg=\"$(git -C \"$d\" clean -fd 2>&1)\"",+ "echo \"$(git reset \"--hard\")\"",+ "echo $(echo \")\"; git reset --hard)",+ // Abbreviated long options.+ "git reset --har",+ "git clean --for",+ "git branch --del --force z",+ "git switch --discard main",+ "git push --mirro x",+ "git push --for",+ "git push origin --del old",+ "git restore --staged --wor .",+ "git checkout --for main",+ // Two positionals: `<tree-ish> <path>`.+ "git checkout main src/main.zig",+ "git checkout HEAD~1 src/a.zig",+ // `&>` / `&>>` redirect, not a separator.+ "git reset &>/dev/null --hard",+ "git clean &>/dev/null -fd",+ "git clean &>>log -fd",+ // Aliases whose value is destructive.+ "git config alias.x 'reset --hard' && git x",+ "git config --global alias.nuke '!git clean -fdx'",+ "git config set alias.x \"push --force\"",+ "git -c alias.x='reset --hard' x",+ "git -c 'alias.x=!git reset --hard' x",+ // Case-insensitive git word (Windows).+ "Git reset --hard",+ "GIT.EXE clean -fd",+ };+ for (cases) |c| std.testing.expect(isDestructiveGit(c)) catch |err| {+ std.debug.print("not flagged: {s}\n", .{c});+ return err;+ };+}++test "#1269: read-only and branch-only git is not flagged" {+ const cases = [_][]const u8{+ "git status",+ "git log --oneline -5",+ "git log -S\"reset --hard\"",+ "git diff HEAD",+ "git show HEAD:src/main.zig",+ "git commit -m wip",+ "git reset --soft HEAD~1",+ "git reset HEAD src/a.zig",+ "git checkout main",+ "git checkout -b feature",+ "git checkout -b feature origin/main",+ "git checkout --track origin/feature",+ "git checkout --detach",+ "git switch main",+ "git switch -c new-branch",+ "git restore --staged f.txt",+ "git restore --staged .",+ "git restore",+ "git clean -n",+ "git clean -nd",+ "git push",+ "git push -u origin feature",+ "git push origin main",+ "git push origin HEAD:refs/heads/x",+ "git branch -d merged-feature",+ "git branch -u origin/main",+ "git stash",+ "git stash pop",+ "git stash list",+ "git stash push -m drop",+ "git restore --staged f.txt 2>/dev/null",+ "git stash list >stashes.txt",+ "git fetch --prune",+ "git -C /repo status",+ "git -c color.ui=always diff",+ "gitk --all",+ "ls -la",+ "echo reset --hard",+ "",+ "git checkout --det",+ "git checkout --detach HEAD~1",+ "git checkout --orphan gh-pages main",+ "git log --grep 'reset --hard'",+ "git push --follow-tags",+ "git restore --staged --source=HEAD f",+ "git restore --sta f",+ "git config alias.st status",+ "git config --global alias.lg 'log --oneline'",+ "git -c alias.x=status x",+ "git status &>/dev/null && echo ok",+ "git status 2>&1 & git log",+ "echo \"$(git status)\"",+ "gitk --all &>/dev/null",+ };+ for (cases) |c| std.testing.expect(!isDestructiveGit(c)) catch |err| {+ std.debug.print("flagged: {s}\n", .{c});+ return err;+ };+}diff --git a/src/tool_call_repair.zig b/src/tool_call_repair.zigindex 7eb5375b..4224c2bc 100644--- a/src/tool_call_repair.zig+++ b/src/tool_call_repair.zig@@ -28,6 +28,13 @@ pub fn isLoopStop(text: []const u8) bool {return text.ptr == loop_stop_text.ptr;}+/// runTurn's check for that stop; a child also closes its feedback inbox.+pub fn endsTurn(self: anytype, text: []const u8) bool {+ if (!isLoopStop(text)) return false;+ if (self.feedback) |inbox| _ = inbox.close(self.io);+ return true;+}+const ws = " \t\r\n";/// One `<function=NAME>BODY</function>`; `start..end` also covers a@@ -112,7 +119,7 @@ fn jsonNumber(t: []const u8, fraction: bool) bool {}/// A `<parameter>` value as the JSON type its schema declares; text otherwise.-fn coerce(scratch: Allocator, raw: []const u8, prop: ?Value) Value {+pub fn coerce(scratch: Allocator, raw: []const u8, prop: ?Value) Value {const t = std.mem.trim(u8, raw, ws);if (wants(prop, "integer") and jsonNumber(t, false)) if (std.fmt.parseInt(i64, t, 10)) |n| return .{ .integer = n } else |_| {};if (wants(prop, "number") and jsonNumber(t, true)) if (std.fmt.parseFloat(f64, t)) |n| {@@ -396,241 +403,6 @@ pub fn brokenCallLoop(messages: []const Value) bool {return true;}-test "repairCalls rebuilds lost arguments from markup, typed by the schema, and strips it" {- var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);- defer arena_state.deinit();- const a = arena_state.allocator();- const tools =- \\[{"type":"function","function":{"name":"read_file","parameters":{"type":"object","properties":{"path":{"type":"string"},"start_line":{"type":["integer","null"]},"end_line":{"type":["integer","null"]}}}}}]- ;- var message = try std.json.parseFromSliceLeaky(Value, a,- \\{"role":"assistant","content":"Let me look.\n<tool_call><function=read_file><parameter=path>src/vision.zig</parameter><parameter=start_line>40</parameter><parameter=end_line>160</parameter></function></tool_call>",- \\ "tool_calls":[{"id":"c1","type":"function","function":{"name":"read_file","arguments":"{\"path\": \"src/vision.zig\", \", \"start_line\": "}}]}- , .{ .allocate = .alloc_always });- try std.testing.expect(try repairCalls(a, a, &message, tools));- const args = message.object.get("tool_calls").?.array.items[0].object.get("function").?.object.get("arguments").?.string;- const parsed = try std.json.parseFromSliceLeaky(Value, a, args, .{});- try std.testing.expectEqualStrings("src/vision.zig", parsed.object.get("path").?.string);- try std.testing.expectEqual(@as(i64, 40), parsed.object.get("start_line").?.integer);- try std.testing.expectEqual(@as(i64, 160), parsed.object.get("end_line").?.integer);- try std.testing.expectEqualStrings("Let me look.", message.object.get("content").?.string);-}--test "repairCalls leaves good arguments and their echoed markup alone; plain text is untouched" {- var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);- defer arena_state.deinit();- const a = arena_state.allocator();- var echoed = try std.json.parseFromSliceLeaky(Value, a,- \\{"role":"assistant","content":"<function=shell><parameter=command>\nls\n</parameter></function>",- \\ "tool_calls":[{"id":"c1","type":"function","function":{"name":"shell","arguments":"{\"command\":\"ls -la\"}"}}]}- , .{ .allocate = .alloc_always });- try std.testing.expect(!try repairCalls(a, a, &echoed, "[]"));- try std.testing.expectEqualStrings("{\"command\":\"ls -la\"}", callArgs(echoed, 0));- try std.testing.expectEqualStrings("<function=shell><parameter=command>\nls\n</parameter></function>", echoed.object.get("content").?.string);-- var plain = try std.json.parseFromSliceLeaky(Value, a,- \\{"role":"assistant","content":"No markup here.","tool_calls":[{"id":"c1","type":"function","function":{"name":"shell","arguments":"{}"}}]}- , .{ .allocate = .alloc_always });- try std.testing.expect(!try repairCalls(a, a, &plain, "[]"));-}--fn callArgs(message: Value, i: usize) []const u8 {- return message.object.get("tool_calls").?.array.items[i].object.get("function").?.object.get("arguments").?.string;-}--const bash_tools =- \\[{"type":"function","function":{"name":"bash","parameters":{"type":"object","properties":{"command":{"type":"string"}}}}}]-;--test "repairCalls: markup never overrides a cut-off argument it contradicts" {- var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);- defer arena_state.deinit();- const a = arena_state.allocator();- var quoted = try std.json.parseFromSliceLeaky(Value, a,- \\{"role":"assistant","content":"Never run this: <function=bash><parameter=command>rm -rf ~</parameter></function>",- \\ "tool_calls":[{"id":"c1","type":"function","function":{"name":"bash","arguments":"{\"command\":\"ls -l"}}]}- , .{ .allocate = .alloc_always });- try std.testing.expect(!try repairCalls(a, a, "ed, bash_tools));- try std.testing.expectEqualStrings("{\"command\":\"ls -l", callArgs(quoted, 0));- // The one block belongs to the good second call, not the cut-off first.- var echo = try std.json.parseFromSliceLeaky(Value, a,- \\{"role":"assistant","content":"<function=bash><parameter=command>echo second</parameter></function>",- \\ "tool_calls":[{"id":"c1","type":"function","function":{"name":"bash","arguments":"{\"command\":\"echo fir"}},- \\ {"id":"c2","type":"function","function":{"name":"bash","arguments":"{\"command\":\"echo second\"}"}}]}- , .{ .allocate = .alloc_always });- try std.testing.expect(!try repairCalls(a, a, &echo, bash_tools));- try std.testing.expectEqualStrings("{\"command\":\"echo fir", callArgs(echo, 0));-}--test "repairCalls: only broken calls take markup, and only when the counts match" {- var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);- defer arena_state.deinit();- const a = arena_state.allocator();- var message = try std.json.parseFromSliceLeaky(Value, a,- \\{"role":"assistant","content":"Run both.<function=bash><parameter=command>make test</parameter></function>",- \\ "tool_calls":[{"id":"c1","type":"function","function":{"name":"bash","arguments":"{\"command\":\"git status\"}"}},- \\ {"id":"c2","type":"function","function":{"name":"bash","arguments":"{}"}}]}- , .{ .allocate = .alloc_always });- try std.testing.expect(try repairCalls(a, a, &message, bash_tools));- try std.testing.expectEqualStrings("{\"command\":\"git status\"}", callArgs(message, 0));- try std.testing.expectEqualStrings("{\"command\":\"make test\"}", callArgs(message, 1));- try std.testing.expectEqualStrings("Run both.", message.object.get("content").?.string);-- var two = try std.json.parseFromSliceLeaky(Value, a,- \\{"role":"assistant","content":"<function=bash><parameter=command>make test</parameter></function>",- \\ "tool_calls":[{"id":"c1","type":"function","function":{"name":"bash","arguments":"{}"}},- \\ {"id":"c2","type":"function","function":{"name":"bash","arguments":""}}]}- , .{ .allocate = .alloc_always });- try std.testing.expect(!try repairCalls(a, a, &two, bash_tools));- try std.testing.expectEqualStrings("{}", callArgs(two, 0));-}--test "repairCalls: text around the markup survives, and unclosed tags are prose" {- var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);- defer arena_state.deinit();- const a = arena_state.allocator();- var prose = try std.json.parseFromSliceLeaky(Value, a,- \\{"role":"assistant","content":"The markup looks like `<function=NAME>` then params. Next I will read the file.",- \\ "tool_calls":[{"id":"c1","type":"function","function":{"name":"bash","arguments":"{}"}}]}- , .{ .allocate = .alloc_always });- try std.testing.expect(!try repairCalls(a, a, &prose, bash_tools));- try std.testing.expectEqualStrings("The markup looks like `<function=NAME>` then params. Next I will read the file.", prose.object.get("content").?.string);-- var mixed = try std.json.parseFromSliceLeaky(Value, a,- \\{"role":"assistant","content":"Format: `<function=NAME>` then params.\n<tool_call>\n<function=bash><parameter=command>ls</parameter></function>\n</tool_call>\nDone.",- \\ "tool_calls":[{"id":"c1","type":"function","function":{"name":"bash","arguments":"{}"}}]}- , .{ .allocate = .alloc_always });- try std.testing.expect(try repairCalls(a, a, &mixed, bash_tools));- try std.testing.expectEqualStrings("{\"command\":\"ls\"}", callArgs(mixed, 0));- try std.testing.expectEqualStrings("Format: `<function=NAME>` then params.\n\nDone.", mixed.object.get("content").?.string);-}--test "repairCalls: parameter tags inside a value stay in it; unknown or repeated keys refuse" {- var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);- defer arena_state.deinit();- const a = arena_state.allocator();- var printf = try std.json.parseFromSliceLeaky(Value, a,- \\{"role":"assistant","content":"<function=bash><parameter=command>printf '<parameter=x>y</parameter>' > f</parameter></function>",- \\ "tool_calls":[{"id":"c1","type":"function","function":{"name":"bash","arguments":"{}"}}]}- , .{ .allocate = .alloc_always });- try std.testing.expect(try repairCalls(a, a, &printf, bash_tools));- const parsed = try std.json.parseFromSliceLeaky(Value, a, callArgs(printf, 0), .{});- try std.testing.expectEqual(@as(usize, 1), parsed.object.count());- try std.testing.expectEqualStrings("printf '<parameter=x>y</parameter>' > f", parsed.object.get("command").?.string);-- for ([_][]const u8{- "<parameter=command>ls</parameter><parameter=x>y</parameter>",- "<parameter=command>ls</parameter>\n<parameter=command>pwd</parameter>",- }) |body| {- const content = try std.fmt.allocPrint(a, "<function=bash>{s}</function>", .{body});- var msg: std.json.ObjectMap = .empty;- try msg.put(a, "content", .{ .string = content });- var fn_obj: std.json.ObjectMap = .empty;- try fn_obj.put(a, "name", .{ .string = "bash" });- try fn_obj.put(a, "arguments", .{ .string = "{}" });- var call: std.json.ObjectMap = .empty;- try call.put(a, "function", .{ .object = fn_obj });- var calls = std.json.Array.init(a);- try calls.append(.{ .object = call });- try msg.put(a, "tool_calls", .{ .array = calls });- var message: Value = .{ .object = msg };- try std.testing.expect(!try repairCalls(a, a, &message, bash_tools));- }-}--test "repairCalls: coerce emits only finite plain JSON numbers" {- var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);- defer arena_state.deinit();- const a = arena_state.allocator();- const num = try std.json.parseFromSliceLeaky(Value, a, "{\"type\":\"number\"}", .{});- const int = try std.json.parseFromSliceLeaky(Value, a, "{\"type\":\"integer\"}", .{});- for ([_][]const u8{ "inf", "-inf", "Infinity", "1e999", "nan", "1_000", "+5", "01", "1.", ".5" }) |raw| {- try std.testing.expect(coerce(a, raw, num) == .string);- try std.testing.expect(coerce(a, raw, int) == .string);- }- try std.testing.expectEqualStrings("-1.5e3", coerce(a, "-1.5e3", num).number_string);- try std.testing.expectEqualStrings("0.25", coerce(a, " 0.25\n", num).number_string);- try std.testing.expectEqual(@as(i64, -42), coerce(a, "-42", int).integer);- try std.testing.expect(coerce(a, "99999999999999999999", int) == .string);-}--test "repairCalls: multiline string values lose only the wrapping newlines" {- var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);- defer arena_state.deinit();- const a = arena_state.allocator();- var message = try std.json.parseFromSliceLeaky(Value, a,- \\{"role":"assistant","content":"<tool_call>\n<function=write_file>\n<parameter=path>\na.txt\n</parameter>\n<parameter=content>\n one\n two\n</parameter>\n</function>\n</tool_call>",- \\ "tool_calls":[{"id":"c1","type":"function","function":{"name":"write_file","arguments":""}}]}- , .{ .allocate = .alloc_always });- try std.testing.expect(try repairCalls(a, a, &message, "[]"));- const args = message.object.get("tool_calls").?.array.items[0].object.get("function").?.object.get("arguments").?.string;- const parsed = try std.json.parseFromSliceLeaky(Value, a, args, .{});- try std.testing.expectEqualStrings("a.txt", parsed.object.get("path").?.string);- try std.testing.expectEqualStrings(" one\n two", parsed.object.get("content").?.string);-}--test "writeMimoTools collapses nullable unions to the plain type" {- var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);- defer arena_state.deinit();- const a = arena_state.allocator();- var aw: std.Io.Writer.Allocating = .init(a);- var s: std.json.Stringify = .{ .writer = &aw.writer };- try writeMimoTools(&s, a,- \\[{"type":"function","function":{"name":"read_file","parameters":{"type":"object","properties":{"start_line":{"type":["integer","null"]},"contains":{"anyOf":[{"type":"string"},{"type":"null"}],"description":"d"},"tags":{"type":"array","items":{"type":["string","null"]}}}}}}]- );- const out = try std.json.parseFromSliceLeaky(Value, a, aw.written(), .{});- const props = out.array.items[0].object.get("function").?.object.get("parameters").?.object.get("properties").?.object;- try std.testing.expectEqualStrings("integer", props.get("start_line").?.object.get("type").?.string);- try std.testing.expectEqualStrings("string", props.get("contains").?.object.get("type").?.string);- try std.testing.expect(props.get("contains").?.object.get("anyOf") == null);- try std.testing.expectEqualStrings("d", props.get("contains").?.object.get("description").?.string);- try std.testing.expectEqualStrings("string", props.get("tags").?.object.get("items").?.object.get("type").?.string);-}--test "writeMimoTools gives a typeless root schema type object, like writeOpenAITools" {- var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);- defer arena_state.deinit();- const a = arena_state.allocator();- var aw: std.Io.Writer.Allocating = .init(a);- var s: std.json.Stringify = .{ .writer = &aw.writer };- try writeMimoTools(&s, a,- \\[{"type":"function","function":{"name":"a","parameters":{"properties":{"n":{"type":["integer","null"]}}}}},- \\ {"type":"function","function":{"name":"b","parameters":{"anyOf":[{"type":"object"}]}}}]- );- const out = try std.json.parseFromSliceLeaky(Value, a, aw.written(), .{});- const first = out.array.items[0].object.get("function").?.object.get("parameters").?.object;- try std.testing.expectEqualStrings("object", first.get("type").?.string);- try std.testing.expectEqualStrings("integer", first.get("properties").?.object.get("n").?.object.get("type").?.string);- try std.testing.expect(out.array.items[1].object.get("function").?.object.get("parameters").?.object.get("type") == null);-}--test "brokenCallLoop: three all-unusable batches in a row, not two or a mixed one" {- var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);- defer arena_state.deinit();- const a = arena_state.allocator();- const call =- \\{"role":"assistant","content":"","tool_calls":[{"id":"x","type":"function","function":{"name":"read_file","arguments":"{}"}}]}- ;- const bad = "{\"role\":\"tool\",\"tool_call_id\":\"x\",\"content\":\"[error] " ++ tool_call_args.invalid_exec_message ++ "\"}";- const missing = "{\"role\":\"tool\",\"tool_call_id\":\"x\",\"content\":\"[error] missing or non-string argument: path\"}";- const good = "{\"role\":\"tool\",\"tool_call_id\":\"x\",\"content\":\"const x = 1;\"}";- const three = try std.json.parseFromSliceLeaky(Value, a, "[{\"role\":\"user\",\"content\":\"go\"}," ++ call ++ "," ++ bad ++ "," ++ call ++ "," ++ bad ++ "," ++ missing ++ "," ++ call ++ "," ++ bad ++ "]", .{});- try std.testing.expect(brokenCallLoop(three.array.items));- const two = try std.json.parseFromSliceLeaky(Value, a, "[{\"role\":\"user\",\"content\":\"go\"}," ++ call ++ "," ++ bad ++ "," ++ call ++ "," ++ bad ++ "]", .{});- try std.testing.expect(!brokenCallLoop(two.array.items));- const mixed = try std.json.parseFromSliceLeaky(Value, a, "[" ++ call ++ "," ++ bad ++ "," ++ call ++ "," ++ good ++ "," ++ bad ++ "," ++ call ++ "," ++ bad ++ "]", .{});- try std.testing.expect(!brokenCallLoop(mixed.array.items));-}--test "brokenCallLoop stop runs the batch checkpoint, then ends the turn past every nudge" {- const steps = @embedFile("agent_steps.zig");- const step = steps[std.mem.indexOf(u8, steps, "pub fn stepOpenAI").?..];- try std.testing.expect(std.mem.indexOf(u8, step, "turn_checkpoint.afterToolBatch(self)").? < std.mem.indexOf(u8, step, "return tool_call_repair.loop_stop_text").?);- const agent = @embedFile("agent.zig");- const loop = agent[std.mem.indexOf(u8, agent, "pub fn runTurn").?..];- try std.testing.expect(std.mem.indexOf(u8, loop, "isLoopStop(").? < std.mem.indexOf(u8, loop, "bounced_answer.retry(").?);- try std.testing.expect(isLoopStop(loop_stop_text));- const copy = try std.testing.allocator.dupe(u8, loop_stop_text);- defer std.testing.allocator.free(copy);- try std.testing.expect(!isLoopStop(copy));+test {+ _ = @import("tool_call_repair_tests.zig");}diff --git a/src/tool_call_repair_tests.zig b/src/tool_call_repair_tests.zignew file mode 100644index 00000000..5bf22c44--- /dev/null+++ b/src/tool_call_repair_tests.zig@@ -0,0 +1,250 @@+//! Tests for tool_call_repair.zig (split out under the 600-line ceiling).+const std = @import("std");+const Value = std.json.Value;+const tool_call_args = @import("tool_call_args.zig");+const repair = @import("tool_call_repair.zig");+const repairCalls = repair.repairCalls;+const writeMimoTools = repair.writeMimoTools;+const brokenCallLoop = repair.brokenCallLoop;+const isLoopStop = repair.isLoopStop;+const loop_stop_text = repair.loop_stop_text;+const coerce = repair.coerce;++test "repairCalls rebuilds lost arguments from markup, typed by the schema, and strips it" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const a = arena_state.allocator();+ const tools =+ \\[{"type":"function","function":{"name":"read_file","parameters":{"type":"object","properties":{"path":{"type":"string"},"start_line":{"type":["integer","null"]},"end_line":{"type":["integer","null"]}}}}}]+ ;+ var message = try std.json.parseFromSliceLeaky(Value, a,+ \\{"role":"assistant","content":"Let me look.\n<tool_call><function=read_file><parameter=path>src/vision.zig</parameter><parameter=start_line>40</parameter><parameter=end_line>160</parameter></function></tool_call>",+ \\ "tool_calls":[{"id":"c1","type":"function","function":{"name":"read_file","arguments":"{\"path\": \"src/vision.zig\", \", \"start_line\": "}}]}+ , .{ .allocate = .alloc_always });+ try std.testing.expect(try repairCalls(a, a, &message, tools));+ const args = message.object.get("tool_calls").?.array.items[0].object.get("function").?.object.get("arguments").?.string;+ const parsed = try std.json.parseFromSliceLeaky(Value, a, args, .{});+ try std.testing.expectEqualStrings("src/vision.zig", parsed.object.get("path").?.string);+ try std.testing.expectEqual(@as(i64, 40), parsed.object.get("start_line").?.integer);+ try std.testing.expectEqual(@as(i64, 160), parsed.object.get("end_line").?.integer);+ try std.testing.expectEqualStrings("Let me look.", message.object.get("content").?.string);+}++test "repairCalls leaves good arguments and their echoed markup alone; plain text is untouched" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const a = arena_state.allocator();+ var echoed = try std.json.parseFromSliceLeaky(Value, a,+ \\{"role":"assistant","content":"<function=shell><parameter=command>\nls\n</parameter></function>",+ \\ "tool_calls":[{"id":"c1","type":"function","function":{"name":"shell","arguments":"{\"command\":\"ls -la\"}"}}]}+ , .{ .allocate = .alloc_always });+ try std.testing.expect(!try repairCalls(a, a, &echoed, "[]"));+ try std.testing.expectEqualStrings("{\"command\":\"ls -la\"}", callArgs(echoed, 0));+ try std.testing.expectEqualStrings("<function=shell><parameter=command>\nls\n</parameter></function>", echoed.object.get("content").?.string);++ var plain = try std.json.parseFromSliceLeaky(Value, a,+ \\{"role":"assistant","content":"No markup here.","tool_calls":[{"id":"c1","type":"function","function":{"name":"shell","arguments":"{}"}}]}+ , .{ .allocate = .alloc_always });+ try std.testing.expect(!try repairCalls(a, a, &plain, "[]"));+}++fn callArgs(message: Value, i: usize) []const u8 {+ return message.object.get("tool_calls").?.array.items[i].object.get("function").?.object.get("arguments").?.string;+}++const bash_tools =+ \\[{"type":"function","function":{"name":"bash","parameters":{"type":"object","properties":{"command":{"type":"string"}}}}}]+;++test "repairCalls: markup never overrides a cut-off argument it contradicts" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const a = arena_state.allocator();+ var quoted = try std.json.parseFromSliceLeaky(Value, a,+ \\{"role":"assistant","content":"Never run this: <function=bash><parameter=command>rm -rf ~</parameter></function>",+ \\ "tool_calls":[{"id":"c1","type":"function","function":{"name":"bash","arguments":"{\"command\":\"ls -l"}}]}+ , .{ .allocate = .alloc_always });+ try std.testing.expect(!try repairCalls(a, a, "ed, bash_tools));+ try std.testing.expectEqualStrings("{\"command\":\"ls -l", callArgs(quoted, 0));+ // The one block belongs to the good second call, not the cut-off first.+ var echo = try std.json.parseFromSliceLeaky(Value, a,+ \\{"role":"assistant","content":"<function=bash><parameter=command>echo second</parameter></function>",+ \\ "tool_calls":[{"id":"c1","type":"function","function":{"name":"bash","arguments":"{\"command\":\"echo fir"}},+ \\ {"id":"c2","type":"function","function":{"name":"bash","arguments":"{\"command\":\"echo second\"}"}}]}+ , .{ .allocate = .alloc_always });+ try std.testing.expect(!try repairCalls(a, a, &echo, bash_tools));+ try std.testing.expectEqualStrings("{\"command\":\"echo fir", callArgs(echo, 0));+}++test "repairCalls: only broken calls take markup, and only when the counts match" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const a = arena_state.allocator();+ var message = try std.json.parseFromSliceLeaky(Value, a,+ \\{"role":"assistant","content":"Run both.<function=bash><parameter=command>make test</parameter></function>",+ \\ "tool_calls":[{"id":"c1","type":"function","function":{"name":"bash","arguments":"{\"command\":\"git status\"}"}},+ \\ {"id":"c2","type":"function","function":{"name":"bash","arguments":"{}"}}]}+ , .{ .allocate = .alloc_always });+ try std.testing.expect(try repairCalls(a, a, &message, bash_tools));+ try std.testing.expectEqualStrings("{\"command\":\"git status\"}", callArgs(message, 0));+ try std.testing.expectEqualStrings("{\"command\":\"make test\"}", callArgs(message, 1));+ try std.testing.expectEqualStrings("Run both.", message.object.get("content").?.string);++ var two = try std.json.parseFromSliceLeaky(Value, a,+ \\{"role":"assistant","content":"<function=bash><parameter=command>make test</parameter></function>",+ \\ "tool_calls":[{"id":"c1","type":"function","function":{"name":"bash","arguments":"{}"}},+ \\ {"id":"c2","type":"function","function":{"name":"bash","arguments":""}}]}+ , .{ .allocate = .alloc_always });+ try std.testing.expect(!try repairCalls(a, a, &two, bash_tools));+ try std.testing.expectEqualStrings("{}", callArgs(two, 0));+}++test "repairCalls: text around the markup survives, and unclosed tags are prose" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const a = arena_state.allocator();+ var prose = try std.json.parseFromSliceLeaky(Value, a,+ \\{"role":"assistant","content":"The markup looks like `<function=NAME>` then params. Next I will read the file.",+ \\ "tool_calls":[{"id":"c1","type":"function","function":{"name":"bash","arguments":"{}"}}]}+ , .{ .allocate = .alloc_always });+ try std.testing.expect(!try repairCalls(a, a, &prose, bash_tools));+ try std.testing.expectEqualStrings("The markup looks like `<function=NAME>` then params. Next I will read the file.", prose.object.get("content").?.string);++ var mixed = try std.json.parseFromSliceLeaky(Value, a,+ \\{"role":"assistant","content":"Format: `<function=NAME>` then params.\n<tool_call>\n<function=bash><parameter=command>ls</parameter></function>\n</tool_call>\nDone.",+ \\ "tool_calls":[{"id":"c1","type":"function","function":{"name":"bash","arguments":"{}"}}]}+ , .{ .allocate = .alloc_always });+ try std.testing.expect(try repairCalls(a, a, &mixed, bash_tools));+ try std.testing.expectEqualStrings("{\"command\":\"ls\"}", callArgs(mixed, 0));+ try std.testing.expectEqualStrings("Format: `<function=NAME>` then params.\n\nDone.", mixed.object.get("content").?.string);+}++test "repairCalls: parameter tags inside a value stay in it; unknown or repeated keys refuse" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const a = arena_state.allocator();+ var printf = try std.json.parseFromSliceLeaky(Value, a,+ \\{"role":"assistant","content":"<function=bash><parameter=command>printf '<parameter=x>y</parameter>' > f</parameter></function>",+ \\ "tool_calls":[{"id":"c1","type":"function","function":{"name":"bash","arguments":"{}"}}]}+ , .{ .allocate = .alloc_always });+ try std.testing.expect(try repairCalls(a, a, &printf, bash_tools));+ const parsed = try std.json.parseFromSliceLeaky(Value, a, callArgs(printf, 0), .{});+ try std.testing.expectEqual(@as(usize, 1), parsed.object.count());+ try std.testing.expectEqualStrings("printf '<parameter=x>y</parameter>' > f", parsed.object.get("command").?.string);++ for ([_][]const u8{+ "<parameter=command>ls</parameter><parameter=x>y</parameter>",+ "<parameter=command>ls</parameter>\n<parameter=command>pwd</parameter>",+ }) |body| {+ const content = try std.fmt.allocPrint(a, "<function=bash>{s}</function>", .{body});+ var msg: std.json.ObjectMap = .empty;+ try msg.put(a, "content", .{ .string = content });+ var fn_obj: std.json.ObjectMap = .empty;+ try fn_obj.put(a, "name", .{ .string = "bash" });+ try fn_obj.put(a, "arguments", .{ .string = "{}" });+ var call: std.json.ObjectMap = .empty;+ try call.put(a, "function", .{ .object = fn_obj });+ var calls = std.json.Array.init(a);+ try calls.append(.{ .object = call });+ try msg.put(a, "tool_calls", .{ .array = calls });+ var message: Value = .{ .object = msg };+ try std.testing.expect(!try repairCalls(a, a, &message, bash_tools));+ }+}++test "repairCalls: coerce emits only finite plain JSON numbers" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const a = arena_state.allocator();+ const num = try std.json.parseFromSliceLeaky(Value, a, "{\"type\":\"number\"}", .{});+ const int = try std.json.parseFromSliceLeaky(Value, a, "{\"type\":\"integer\"}", .{});+ for ([_][]const u8{ "inf", "-inf", "Infinity", "1e999", "nan", "1_000", "+5", "01", "1.", ".5" }) |raw| {+ try std.testing.expect(coerce(a, raw, num) == .string);+ try std.testing.expect(coerce(a, raw, int) == .string);+ }+ try std.testing.expectEqualStrings("-1.5e3", coerce(a, "-1.5e3", num).number_string);+ try std.testing.expectEqualStrings("0.25", coerce(a, " 0.25\n", num).number_string);+ try std.testing.expectEqual(@as(i64, -42), coerce(a, "-42", int).integer);+ try std.testing.expect(coerce(a, "99999999999999999999", int) == .string);+}++test "repairCalls: multiline string values lose only the wrapping newlines" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const a = arena_state.allocator();+ var message = try std.json.parseFromSliceLeaky(Value, a,+ \\{"role":"assistant","content":"<tool_call>\n<function=write_file>\n<parameter=path>\na.txt\n</parameter>\n<parameter=content>\n one\n two\n</parameter>\n</function>\n</tool_call>",+ \\ "tool_calls":[{"id":"c1","type":"function","function":{"name":"write_file","arguments":""}}]}+ , .{ .allocate = .alloc_always });+ try std.testing.expect(try repairCalls(a, a, &message, "[]"));+ const args = message.object.get("tool_calls").?.array.items[0].object.get("function").?.object.get("arguments").?.string;+ const parsed = try std.json.parseFromSliceLeaky(Value, a, args, .{});+ try std.testing.expectEqualStrings("a.txt", parsed.object.get("path").?.string);+ try std.testing.expectEqualStrings(" one\n two", parsed.object.get("content").?.string);+}++test "writeMimoTools collapses nullable unions to the plain type" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const a = arena_state.allocator();+ var aw: std.Io.Writer.Allocating = .init(a);+ var s: std.json.Stringify = .{ .writer = &aw.writer };+ try writeMimoTools(&s, a,+ \\[{"type":"function","function":{"name":"read_file","parameters":{"type":"object","properties":{"start_line":{"type":["integer","null"]},"contains":{"anyOf":[{"type":"string"},{"type":"null"}],"description":"d"},"tags":{"type":"array","items":{"type":["string","null"]}}}}}}]+ );+ const out = try std.json.parseFromSliceLeaky(Value, a, aw.written(), .{});+ const props = out.array.items[0].object.get("function").?.object.get("parameters").?.object.get("properties").?.object;+ try std.testing.expectEqualStrings("integer", props.get("start_line").?.object.get("type").?.string);+ try std.testing.expectEqualStrings("string", props.get("contains").?.object.get("type").?.string);+ try std.testing.expect(props.get("contains").?.object.get("anyOf") == null);+ try std.testing.expectEqualStrings("d", props.get("contains").?.object.get("description").?.string);+ try std.testing.expectEqualStrings("string", props.get("tags").?.object.get("items").?.object.get("type").?.string);+}++test "writeMimoTools gives a typeless root schema type object, like writeOpenAITools" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const a = arena_state.allocator();+ var aw: std.Io.Writer.Allocating = .init(a);+ var s: std.json.Stringify = .{ .writer = &aw.writer };+ try writeMimoTools(&s, a,+ \\[{"type":"function","function":{"name":"a","parameters":{"properties":{"n":{"type":["integer","null"]}}}}},+ \\ {"type":"function","function":{"name":"b","parameters":{"anyOf":[{"type":"object"}]}}}]+ );+ const out = try std.json.parseFromSliceLeaky(Value, a, aw.written(), .{});+ const first = out.array.items[0].object.get("function").?.object.get("parameters").?.object;+ try std.testing.expectEqualStrings("object", first.get("type").?.string);+ try std.testing.expectEqualStrings("integer", first.get("properties").?.object.get("n").?.object.get("type").?.string);+ try std.testing.expect(out.array.items[1].object.get("function").?.object.get("parameters").?.object.get("type") == null);+}++test "brokenCallLoop: three all-unusable batches in a row, not two or a mixed one" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const a = arena_state.allocator();+ const call =+ \\{"role":"assistant","content":"","tool_calls":[{"id":"x","type":"function","function":{"name":"read_file","arguments":"{}"}}]}+ ;+ const bad = "{\"role\":\"tool\",\"tool_call_id\":\"x\",\"content\":\"[error] " ++ tool_call_args.invalid_exec_message ++ "\"}";+ const missing = "{\"role\":\"tool\",\"tool_call_id\":\"x\",\"content\":\"[error] missing or non-string argument: path\"}";+ const good = "{\"role\":\"tool\",\"tool_call_id\":\"x\",\"content\":\"const x = 1;\"}";+ const three = try std.json.parseFromSliceLeaky(Value, a, "[{\"role\":\"user\",\"content\":\"go\"}," ++ call ++ "," ++ bad ++ "," ++ call ++ "," ++ bad ++ "," ++ missing ++ "," ++ call ++ "," ++ bad ++ "]", .{});+ try std.testing.expect(brokenCallLoop(three.array.items));+ const two = try std.json.parseFromSliceLeaky(Value, a, "[{\"role\":\"user\",\"content\":\"go\"}," ++ call ++ "," ++ bad ++ "," ++ call ++ "," ++ bad ++ "]", .{});+ try std.testing.expect(!brokenCallLoop(two.array.items));+ const mixed = try std.json.parseFromSliceLeaky(Value, a, "[" ++ call ++ "," ++ bad ++ "," ++ call ++ "," ++ good ++ "," ++ bad ++ "," ++ call ++ "," ++ bad ++ "]", .{});+ try std.testing.expect(!brokenCallLoop(mixed.array.items));+}++test "brokenCallLoop stop runs the batch checkpoint, then ends the turn past every nudge" {+ const steps = @embedFile("agent_steps.zig");+ const step = steps[std.mem.indexOf(u8, steps, "pub fn stepOpenAI").?..];+ try std.testing.expect(std.mem.indexOf(u8, step, "turn_checkpoint.afterToolBatch(self)").? < std.mem.indexOf(u8, step, "return tool_call_repair.loop_stop_text").?);+ const agent = @embedFile("agent.zig");+ const loop = agent[std.mem.indexOf(u8, agent, "pub fn runTurn").?..];+ try std.testing.expect(std.mem.indexOf(u8, loop, "endsTurn(").? < std.mem.indexOf(u8, loop, "bounced_answer.retry(").?);+ try std.testing.expect(isLoopStop(loop_stop_text));+ const copy = try std.testing.allocator.dupe(u8, loop_stop_text);+ defer std.testing.allocator.free(copy);+ try std.testing.expect(!isLoopStop(copy));+}