graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
Run tool calls a server left in content as inline markup
justrach95c897898872
diff --git a/src/agent_steps.zig b/src/agent_steps.zigindex 2ec0f170..0a91757c 100644--- a/src/agent_steps.zig+++ b/src/agent_steps.zig@@ -258,6 +258,8 @@ pub fn stepOpenAI(self: *Agent, root: std.json.ObjectMap) !?[]const u8 {// Lost arguments rebuilt from the reply's own tool-call markup (MiMo only).if (@import("effort_route.zig").mimoRoute(self.provider.id, self.provider.model))_ = try tool_call_repair.repairCalls(self.arena, self.scratchAlloc(), &message, self.toolsJson());+ // #1247: calls the server's parser left in `content` as markup.+ _ = try tool_call_repair.recoverInlineCalls(self.arena, self.scratchAlloc(), &message, self.toolsJson());const msg_obj = tools_mod.json_args.object(message) orelse {try self.sayApiError("api error: choice message was not an object", .{});return error.ApiError;diff --git a/src/tool_call_repair.zig b/src/tool_call_repair.zigindex 4224c2bc..ed766084 100644--- a/src/tool_call_repair.zig+++ b/src/tool_call_repair.zig@@ -305,6 +305,71 @@ pub fn repairCalls(arena: Allocator, scratch: Allocator, message: *Value, tools_return true;}+var inline_seq = std.atomic.Value(u64).init(0);++/// #1247: a reply with no structured tool calls whose `content` carries+/// `<tool_call><function=NAME>…</function></tool_call>` blocks, because the+/// serving stack's parser missed them. Each block that is wrapped in+/// `<tool_call>`, sits outside backtick code, names a tool in `tools_raw`,+/// and has arguments that parse becomes a real call; its markup leaves+/// `content`. Anything else stays text, so quoted markup never runs.+pub fn recoverInlineCalls(arena: Allocator, scratch: Allocator, message: *Value, tools_raw: []const u8) !bool {+ if (message.* != .object) return false;+ const content = message.object.get("content") orelse return false;+ if (content != .string or std.mem.indexOf(u8, content.string, "<tool_call>") == null) return false;+ if (message.object.get("tool_calls")) |tcs| if (tcs == .array and tcs.array.items.len > 0) return false;+ const found = try blocks(scratch, content.string);+ if (found.len == 0) return false;+ const catalog = std.json.parseFromSliceLeaky(Value, scratch, tools_raw, .{ .allocate = .alloc_always }) catch return false;+ var calls = std.json.Array.init(arena);+ for (found) |*b| {+ const text = content.string;+ if (!std.mem.startsWith(u8, text[b.start..], "<tool_call>") or !std.mem.endsWith(u8, text[0..b.end], "</tool_call>")) continue;+ if (inCode(text[0..b.start])) continue;+ const props = declared(catalog, b.name) orelse continue;+ const args: std.json.ObjectMap = if (std.mem.trim(u8, b.body, ws).len == 0)+ .empty+ else+ try argumentsFrom(scratch, b.body, props) orelse continue;+ var function: std.json.ObjectMap = .empty;+ try function.put(arena, "name", .{ .string = try arena.dupe(u8, b.name) });+ try function.put(arena, "arguments", .{ .string = try std.json.Stringify.valueAlloc(arena, Value{ .object = args }, .{}) });+ var call: std.json.ObjectMap = .empty;+ try call.put(arena, "id", .{ .string = try std.fmt.allocPrint(arena, "call_inline_{d}", .{inline_seq.fetchAdd(1, .monotonic) + 1}) });+ try call.put(arena, "type", .{ .string = "function" });+ try call.put(arena, "function", .{ .object = function });+ try calls.append(.{ .object = call });+ b.state = .repaired;+ }+ if (calls.items.len == 0) return false;+ const rest = try stripRepaired(arena, content.string, found);+ try message.object.put(arena, "content", if (rest.len == 0) Value.null else Value{ .string = rest });+ try message.object.put(arena, "tool_calls", .{ .array = calls });+ return true;+}++/// Whether text ending here is inside a ``` fence or an inline `code` span.+fn inCode(before: []const u8) bool {+ const fences = std.mem.count(u8, before, "```");+ if (fences % 2 == 1) return true;+ return (std.mem.count(u8, before, "`") - 3 * fences) % 2 == 1;+}++/// The named tool's `properties` in a chat-completions catalog: an empty map+/// for a tool without parameters, null when the catalog has no such tool.+fn declared(catalog: Value, name: []const u8) ?std.json.ObjectMap {+ if (catalog != .array) return null;+ for (catalog.array.items) |tool| {+ if (tool != .object) continue;+ const f = tool.object.get("function") orelse tool;+ if (f != .object) continue;+ const n = f.object.get("name") orelse continue;+ if (n != .string or !std.mem.eql(u8, n.string, name)) continue;+ return properties(catalog, name) orelse .empty;+ }+ return null;+}+/// `schema` with nullable unions collapsed to the plain type, recursively.fn collapseNullable(arena: Allocator, schema: *Value) !void {if (schema.* != .object) return;diff --git a/src/tool_call_repair_tests.zig b/src/tool_call_repair_tests.zigindex 5bf22c44..5b306219 100644--- a/src/tool_call_repair_tests.zig+++ b/src/tool_call_repair_tests.zig@@ -248,3 +248,75 @@ test "brokenCallLoop stop runs the batch checkpoint, then ends the turn past evedefer std.testing.allocator.free(copy);try std.testing.expect(!isLoopStop(copy));}++const inline_tools =+ \\[{"type":"function","function":{"name":"read_file","parameters":{"type":"object","properties":{"path":{"type":"string"},"start_line":{"type":["integer","null"]}}}}},+ \\ {"type":"function","function":{"name":"todo_read","parameters":{"type":"object","properties":{}}}}]+;++fn inlineMessage(a: std.mem.Allocator, content: []const u8) !Value {+ var msg: std.json.ObjectMap = .empty;+ try msg.put(a, "role", .{ .string = "assistant" });+ try msg.put(a, "content", .{ .string = content });+ return .{ .object = msg };+}++fn callName(message: Value, i: usize) []const u8 {+ return message.object.get("tool_calls").?.array.items[i].object.get("function").?.object.get("name").?.string;+}++test "recoverInlineCalls (#1247): markup after prose becomes calls, parameters in any order" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const a = arena_state.allocator();+ var message = try inlineMessage(a, "Reading both.\n<tool_call><function=read_file><parameter=start_line>40</parameter><parameter=path>a.zig</parameter></function></tool_call>\n" +++ "<tool_call>\n<function=todo_read>\n</function>\n</tool_call>");+ try std.testing.expect(try repair.recoverInlineCalls(a, a, &message, inline_tools));+ try std.testing.expectEqual(@as(usize, 2), message.object.get("tool_calls").?.array.items.len);+ try std.testing.expectEqualStrings("read_file", callName(message, 0));+ const args = try std.json.parseFromSliceLeaky(Value, a, callArgs(message, 0), .{});+ try std.testing.expectEqualStrings("a.zig", args.object.get("path").?.string);+ try std.testing.expectEqual(@as(i64, 40), args.object.get("start_line").?.integer);+ try std.testing.expectEqualStrings("todo_read", callName(message, 1));+ try std.testing.expectEqualStrings("{}", callArgs(message, 1));+ const id0 = message.object.get("tool_calls").?.array.items[0].object.get("id").?.string;+ const id1 = message.object.get("tool_calls").?.array.items[1].object.get("id").?.string;+ try std.testing.expect(!std.mem.eql(u8, id0, id1));+ try std.testing.expectEqualStrings("Reading both.", message.object.get("content").?.string);++ var only = try inlineMessage(a, "<tool_call><function=todo_read></function></tool_call>");+ try std.testing.expect(try repair.recoverInlineCalls(a, a, &only, inline_tools));+ try std.testing.expect(only.object.get("content").? == .null);+}++test "recoverInlineCalls (#1247): quoted, unwrapped, unknown, or malformed markup stays text" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const a = arena_state.allocator();+ const block = "<tool_call><function=read_file><parameter=path>a.zig</parameter></function></tool_call>";+ for ([_][]const u8{+ "Example: `" ++ block ++ "`",+ "```\n" ++ block ++ "\n```",+ "<function=read_file><parameter=path>a.zig</parameter></function>",+ "<tool_call><function=shell><parameter=command>ls</parameter></function></tool_call>",+ "<tool_call><function=read_file><parameter=nope>x</parameter></function></tool_call>",+ "<tool_call><function=read_file><parameter=path>a.zig</parameter></tool_call>",+ }) |content| {+ var message = try inlineMessage(a, content);+ try std.testing.expect(!try repair.recoverInlineCalls(a, a, &message, inline_tools));+ try std.testing.expectEqualStrings(content, message.object.get("content").?.string);+ try std.testing.expect(message.object.get("tool_calls") == null);+ }+}++test "recoverInlineCalls (#1247): a reply that already has structured calls is left to repairCalls" {+ 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><function=read_file><parameter=path>a.zig</parameter></function></tool_call>",+ \\ "tool_calls":[{"id":"c1","type":"function","function":{"name":"read_file","arguments":"{\"path\":\"a.zig\"}"}}]}+ , .{ .allocate = .alloc_always });+ try std.testing.expect(!try repair.recoverInlineCalls(a, a, &message, inline_tools));+ try std.testing.expectEqual(@as(usize, 1), message.object.get("tool_calls").?.array.items.len);+}