graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
MiMo: loaded tools ride a system message, tools stays frozen
justrach54117606270c
diff --git a/src/additional_tools.zig b/src/additional_tools.zigindex 8599edc7..af421fa3 100644--- a/src/additional_tools.zig+++ b/src/additional_tools.zig@@ -1,19 +1,25 @@-//! Deferred tools on the ChatGPT plan's new sign-in (ADR 0221), loaded the way-//! OpenAI's prompt-caching guide asks: without touching `tools`.+//! Deferred tools loaded without touching `tools` (ADR 0221), the way OpenAI's+//! prompt-caching guide asks.//!//! The stable catalog (ADR 0011) appends each loaded tool to the END of the//! `tools` array. OpenAI renders tools ahead of every message and checks cache//! breakpoints only at message ends, so any change to `tools` makes the whole//! conversation miss the cache on the next request. Codex hides that tail-//! behind hosted tool search (`defer_loading`), which this route refuses.-//! OpenAI's documented alternative is a developer-role `additional_tools`-//! input item: it adds the loaded definitions where the load happened, after-//! the prefix it would otherwise invalidate.+//! behind hosted tool search (`defer_loading`), which the ChatGPT plan's new+//! sign-in refuses. OpenAI's documented alternative is a developer-role+//! `additional_tools` input item: it adds the loaded definitions where the+//! load happened, after the prefix it would otherwise invalidate.//!-//! So on this route the tail never reaches `tools`, and before each request-//! `sync` announces every loaded tool that no `additional_tools` item in the-//! history carries yet: once per tool, and again only if compaction pruned-//! the item that carried it.+//! MiMo's chat wire is worse off: it renders `tools` ahead of the system+//! prompt, so one appended tool re-bills the whole prompt (a probe that+//! appended one went from a full cache hit to none). It has no such item, but+//! it calls a tool announced in a system message. Its parser types arguments+//! only for declared tools, so tool_call_repair.retypeArgs restores the+//! announced tools' argument types.+//!+//! So on these routes the tail never reaches `tools`, and before each request+//! `sync` announces every loaded tool that no announcement in the history+//! carries yet: once per tool, and again only if compaction pruned it.const std = @import("std");const Allocator = std.mem.Allocator;@@ -25,42 +31,64 @@ const Provider = @import("provider.zig").Provider;const mcp = @import("mcp.zig");const mcp_schema_gate = @import("mcp_schema_gate.zig");const native_fold = @import("native_fold.zig");+const origin_key = @import("session_wake.zig").origin_key;pub const item_type = "additional_tools";+const chat_header = "Additional tools are now available. Call them exactly like the tools in your tool list:\n<tools>\n";-/// Routes that load deferred tools through `additional_tools` items.-pub fn active(provider_id: []const u8, kind: Provider.Kind) bool {- return kind == .responses and std.mem.eql(u8, provider_id, "chatgpt-new") and mcp_schema_gate.g_stable_catalog;+/// Routes that load deferred tools through announcements instead of `tools`.+pub fn active(p: Provider) bool {+ if (!mcp_schema_gate.g_stable_catalog) return false;+ return switch (p.kind) {+ .responses => std.mem.eql(u8, p.id, "chatgpt-new"),+ .openai => @import("effort_route.zig").mimoRoute(p.id, p.model),+ .anthropic, .interactions => false,+ };}-/// The loaded tail as Responses tool entries: exactly what the stable catalog-/// would append to `tools`.+/// The loaded tail as tool entries for the agent's wire: exactly what the+/// stable catalog would append to `tools`.fn tailEntries(self: *Agent, arena: Allocator) ![]const Value {const connected: []const mcp.Tool = if (self.registry) |reg| try reg.snapshotTools(arena) else &.{};var aw: Io.Writer.Allocating = .init(arena);var s: std.json.Stringify = .{ .writer = &aw.writer };try s.beginArray();- try native_fold.renderLoadedTail(&s, .responses, arena, connected);+ try native_fold.renderLoadedTail(&s, self.provider.kind, arena, connected);try s.endArray();const v = try std.json.parseFromSliceLeaky(Value, arena, aw.writer.buffered(), .{ .allocate = .alloc_always });return if (v == .array) v.array.items else &.{};}+/// A tool entry's name; chat entries nest it under `function`.fn toolName(tool: Value) []const u8 {if (tool != .object) return "";- const n = tool.object.get("name") orelse return "";+ const f = tool.object.get("function") orelse tool;+ if (f != .object) return "";+ const n = f.object.get("name") orelse return "";return if (n == .string) n.string else "";}+/// A Responses `additional_tools` item, or a chat announcement (tagged by origin).pub fn isItem(m: Value) bool {if (m != .object) return false;- const t = m.object.get("type") orelse return false;+ const t = m.object.get("type") orelse m.object.get(origin_key) orelse return false;return t == .string and std.mem.eql(u8, t.string, item_type);}-fn announced(messages: []const Value, name: []const u8) bool {- for (messages) |m| if (isItem(m)) if (m.object.get("tools")) |list| if (list == .array) {- for (list.array.items) |t| if (std.mem.eql(u8, toolName(t), name)) return true;+fn announced(arena: Allocator, messages: []const Value, name: []const u8) bool {+ for (messages) |m| if (isItem(m)) {+ if (m.object.get("tools")) |list| if (list == .array) {+ for (list.array.items) |t| if (std.mem.eql(u8, toolName(t), name)) return true;+ };+ // A chat announcement carries one definition per line.+ const c = m.object.get("content") orelse continue;+ if (c != .string) continue;+ var lines = std.mem.splitScalar(u8, c.string, '\n');+ while (lines.next()) |line| {+ if (!std.mem.startsWith(u8, line, "{")) continue;+ const t = std.json.parseFromSliceLeaky(Value, arena, line, .{}) catch continue;+ if (std.mem.eql(u8, toolName(t), name)) return true;+ }};return false;}@@ -71,21 +99,38 @@ fn inTail(tail: []const Value, name: []const u8) bool {}/// Before a request: announce each loaded tool the history does not carry yet.-/// On any other route, remove the items instead. Other wires do not know them,-/// and there the catalog tail carries the loaded tools.+/// On any other route, remove the announcements instead. Other wires do not+/// know them, and there the catalog tail carries the loaded tools.pub fn sync(self: *Agent) void {if (self.sub) return;- if (!active(self.provider.id, self.provider.kind)) return dropItems(&self.messages);+ if (!active(self.provider)) return dropItems(&self.messages);const arena = self.messageMutationAlloc();const tail = tailEntries(self, arena) catch return;var fresh = std.json.Array.init(arena);- for (tail) |t| if (!announced(self.messages.items, toolName(t))) fresh.append(t) catch return;+ for (tail) |t| if (!announced(arena, self.messages.items, toolName(t))) fresh.append(t) catch return;if (fresh.items.len == 0) return;+ const item = announcement(arena, self.provider.kind, fresh) catch return;+ self.messages.append(item) catch {};+}++/// A developer-role `additional_tools` item on Responses. The chat wire has no+/// such item, so there it is a system message with one definition per line.+fn announcement(arena: Allocator, kind: Provider.Kind, tools: std.json.Array) !Value {var item: std.json.ObjectMap = .empty;- item.put(arena, "type", .{ .string = item_type }) catch return;- item.put(arena, "role", .{ .string = "developer" }) catch return;- item.put(arena, "tools", .{ .array = fresh }) catch return;- self.messages.append(.{ .object = item }) catch {};+ if (kind == .responses) {+ try item.put(arena, "type", .{ .string = item_type });+ try item.put(arena, "role", .{ .string = "developer" });+ try item.put(arena, "tools", .{ .array = tools });+ return .{ .object = item };+ }+ var aw: Io.Writer.Allocating = .init(arena);+ try aw.writer.writeAll(chat_header);+ for (tools.items) |t| try aw.writer.print("{s}\n", .{try std.json.Stringify.valueAlloc(arena, t, .{})});+ try aw.writer.writeAll("</tools>");+ try item.put(arena, "role", .{ .string = "system" });+ try item.put(arena, "content", .{ .string = aw.writer.buffered() });+ try item.put(arena, origin_key, .{ .string = item_type });+ return .{ .object = item };}fn dropItems(messages: *std.json.Array) void {@@ -95,8 +140,8 @@ fn dropItems(messages: *std.json.Array) void {}}-/// The request's `tools` without the loaded tail. Always re-serialized on this-/// route, even with nothing loaded, so the bytes stay identical from the first+/// The request's `tools` without the loaded tail. Always re-serialized on these+/// routes, even with nothing loaded, so the bytes stay identical from the first/// request to the last.pub fn stripTail(self: *Agent, arena: Allocator, tools_json: []const u8) []const u8 {const tail = tailEntries(self, arena) catch return tools_json;diff --git a/src/additional_tools_tests.zig b/src/additional_tools_tests.zigindex 5c941b73..a3a0e9ef 100644--- a/src/additional_tools_tests.zig+++ b/src/additional_tools_tests.zig@@ -1,5 +1,6 @@//! Tests for additional_tools.zig (ADR 0221): a load on the ChatGPT plan-//! route must leave `tools` byte-identical and announce the tool in the input.+//! route or MiMo's chat wire must leave `tools` byte-identical and announce+//! the tool in the conversation instead.const std = @import("std");const additional_tools = @import("additional_tools.zig");@@ -104,6 +105,69 @@ test "ChatGPT plan: a pruned item is announced again; other routes drop the itemagent.invalidateRootTools();try agent.ensureRootTools(.responses);try std.testing.expect(std.mem.indexOf(u8, agent.tools_responses, "\"name\":\"workflow\"") != null);- try std.testing.expect(!additional_tools.active("codex", .responses));- try std.testing.expect(!additional_tools.active("chatgpt-new", .openai));+ try std.testing.expect(!additional_tools.active(agent.provider));+ var chat = agent.provider;+ chat.id = "chatgpt-new";+ chat.kind = .openai;+ try std.testing.expect(!additional_tools.active(chat));+}++/// A chat body's top-level `tools`: it precedes `messages`, where the+/// announcement's own escaped definitions live.+fn chatToolsOf(body: []const u8) []const u8 {+ const start = (std.mem.indexOf(u8, body, "\"tools\":") orelse return "") + "\"tools\":".len;+ const end = std.mem.indexOfPos(u8, body, start, ",\"messages\":") orelse return "";+ return body[start..end];+}++test "MiMo chat: a tool load leaves tools byte-identical and adds one system announcement" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const a = arena_state.allocator();+ const saved_fold = native_fold.enabled;+ const saved_stable = mcp_schema_gate.g_stable_catalog;+ defer {+ native_fold.enabled = saved_fold;+ mcp_schema_gate.g_stable_catalog = saved_stable;+ native_fold.clearLoadedSession();+ }+ native_fold.enabled = true;+ mcp_schema_gate.g_stable_catalog = true;+ native_fold.clearLoadedSession();++ var agent = try testAgentFor(a, "codegraff", .openai, "mimo-v2.6-pro");+ try std.testing.expect(additional_tools.active(agent.provider));+ agent.invalidateRootTools();+ try agent.ensureRootTools(.openai);+ additional_tools.sync(&agent);+ try std.testing.expectEqual(@as(usize, 0), items(agent.messages.items));+ const before = try agent.buildBody(agent.tools_openai, false, true, true);+ defer std.testing.allocator.free(before);++ native_fold.markLoaded("workflow");+ agent.invalidateRootTools();+ try agent.ensureRootTools(.openai);+ try std.testing.expect(std.mem.indexOf(u8, agent.tools_openai, "\"name\":\"workflow\"") != null);+ additional_tools.sync(&agent);+ try std.testing.expectEqual(@as(usize, 1), items(agent.messages.items));+ const note = agent.messages.items[agent.messages.items.len - 1].object;+ try std.testing.expectEqualStrings("system", note.get("role").?.string);+ try std.testing.expect(std.mem.indexOf(u8, note.get("content").?.string, "\"name\":\"workflow\"") != null);+ const after = try agent.buildBody(agent.tools_openai, false, true, true);+ defer std.testing.allocator.free(after);++ try std.testing.expect(chatToolsOf(before).len > 2);+ try std.testing.expectEqualStrings(chatToolsOf(before), chatToolsOf(after));+ try std.testing.expect(std.mem.indexOf(u8, chatToolsOf(after), "\"name\":\"workflow\"") == null);+ try std.testing.expect(std.mem.indexOf(u8, after, "_graff_origin") == null); // the tag never reaches the wire++ // Announced once; a pruned announcement comes back; another chat model drops it.+ additional_tools.sync(&agent);+ try std.testing.expectEqual(@as(usize, 1), items(agent.messages.items));+ _ = agent.messages.pop();+ additional_tools.sync(&agent);+ try std.testing.expectEqual(@as(usize, 1), items(agent.messages.items));+ agent.provider.model = "deepseek-v4-pro";+ additional_tools.sync(&agent);+ try std.testing.expectEqual(@as(usize, 0), items(agent.messages.items));}diff --git a/src/agent_request_body.zig b/src/agent_request_body.zigindex 92ffab49..467dd29b 100644--- a/src/agent_request_body.zig+++ b/src/agent_request_body.zig@@ -147,9 +147,11 @@ pub fn buildBody(self: *Agent, tools_in: ?[]const u8, force_tool: bool, stream:}}if (tools) |t| {+ // ADR 0221: on MiMo the loaded tail rides a system message instead (additional_tools.zig).+ const at = @import("additional_tools.zig");try s.objectField("tools");// #261 follow-up: the rest need the root-schema repair too.- try @import("tool_call_repair.zig").writeChatTools(&s, self.scratchAlloc(), t, self.provider.id, self.provider.model);+ try @import("tool_call_repair.zig").writeChatTools(&s, self.scratchAlloc(), if (at.active(self.provider)) at.stripTail(self, self.scratchAlloc(), t) else t, self.provider.id, self.provider.model);if (force_tool or @import("meta_wire.zig").restricted(self.provider.id, self.provider.model)) {try s.objectField("tool_choice"); // #751: Meta is auto-onlytry s.write(@import("meta_wire.zig").toolChoice(force_tool, self.provider.id, self.provider.model));diff --git a/src/agent_request_body_responses.zig b/src/agent_request_body_responses.zigindex cac34f99..c61c20ad 100644--- a/src/agent_request_body_responses.zig+++ b/src/agent_request_body_responses.zig@@ -71,7 +71,7 @@ pub fn write(self: *Agent, s: *std.json.Stringify, tools: ?[]const u8, force_toonext = codex_tool_search.spliceWebSearch(self.scratchAlloc(), next) catch next;}// ADR 0221: no hosted tool search here; the loaded tail rides additional_tools items instead.- if (additional_tools.active(self.provider.id, self.provider.kind)) next = additional_tools.stripTail(self, self.scratchAlloc(), next);+ if (additional_tools.active(self.provider)) next = additional_tools.stripTail(self, self.scratchAlloc(), next);break :blk next;};const async_payload = try @import("async_tool_policy.zig").decorate(self.scratchAlloc(), self.provider, payload, self.async_tools_armed and !self.sub and !self.compaction_request and !self.server_compaction_request);diff --git a/src/agent_steps.zig b/src/agent_steps.zigindex c5832fa0..28f62d2f 100644--- a/src/agent_steps.zig+++ b/src/agent_steps.zig@@ -266,8 +266,10 @@ pub fn stepOpenAI(self: *Agent, root: std.json.ObjectMap) !?[]const u8 {return error.ApiError;};// 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))+ if (@import("effort_route.zig").mimoRoute(self.provider.id, self.provider.model)) {_ = try tool_call_repair.repairCalls(self.arena, self.scratchAlloc(), &message, self.toolsJson());+ _ = try tool_call_repair.retypeArgs(self.arena, self.scratchAlloc(), &message, self.toolsJson()); // announced tools come back untyped+ }// #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 {diff --git a/src/tool_call_repair.zig b/src/tool_call_repair.zigindex 5e37a920..ca656f15 100644--- a/src/tool_call_repair.zig+++ b/src/tool_call_repair.zig@@ -305,6 +305,43 @@ pub fn repairCalls(arena: Allocator, scratch: Allocator, message: *Value, tools_return true;}+/// MiMo's parser types a call's arguments from the declared `tools`. A tool+/// announced in a message instead (additional_tools.zig) comes back with every+/// value a string: "5" for an integer, "[\"a\"]" for an array. Re-type each+/// string whose schema in `tools_raw` does not allow one; true when a call changed.+pub fn retypeArgs(arena: Allocator, scratch: Allocator, message: *Value, tools_raw: []const u8) !bool {+ if (message.* != .object) return false;+ const tool_calls = message.object.getPtr("tool_calls") orelse return false;+ if (tool_calls.* != .array) return false;+ const catalog = std.json.parseFromSliceLeaky(Value, scratch, tools_raw, .{ .allocate = .alloc_always }) catch return false;+ var changed = false;+ for (tool_calls.array.items) |*tc| {+ if (tc.* != .object) continue;+ const f = tc.object.getPtr("function") orelse continue;+ if (f.* != .object) continue;+ const name = if (f.object.get("name")) |n| (if (n == .string) n.string else continue) else continue;+ const props = properties(catalog, name) orelse continue;+ const args = if (f.object.get("arguments")) |a| (if (a == .string) a.string else continue) else continue;+ const parsed = tool_call_args.parse(scratch, args);+ if (!parsed.valid or parsed.input != .object) continue;+ var retyped = false;+ var it = parsed.input.object.iterator();+ while (it.next()) |kv| {+ if (kv.value_ptr.* != .string) continue;+ const prop = props.get(kv.key_ptr.*) orelse continue;+ if (wants(prop, "string")) continue;+ const v = coerce(scratch, kv.value_ptr.string, prop);+ if (v == .string) continue;+ kv.value_ptr.* = v;+ retyped = true;+ }+ if (!retyped) continue;+ try f.object.put(arena, "arguments", .{ .string = try std.json.Stringify.valueAlloc(arena, parsed.input, .{}) });+ changed = true;+ }+ return changed;+}+var inline_seq = std.atomic.Value(u64).init(0);/// #1247: a reply with no structured tool calls whose `content` carriesdiff --git a/src/tool_call_repair_tests.zig b/src/tool_call_repair_tests.zigindex 5b306219..840496c9 100644--- a/src/tool_call_repair_tests.zig+++ b/src/tool_call_repair_tests.zig@@ -10,6 +10,27 @@ const isLoopStop = repair.isLoopStop;const loop_stop_text = repair.loop_stop_text;const coerce = repair.coerce;+test "retypeArgs gives an announced tool's string arguments their schema types" {+ 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":"list_comments","parameters":{"type":"object","properties":{"issue_ids":{"type":"array","items":{"type":"string"}},"limit":{"type":"integer"},"include_resolved":{"type":["boolean","null"]},"query":{"type":"string"}}}}}]+ ;+ // MiMo's reply for a tool announced in a message, not declared (2026-09-30).+ var message = try std.json.parseFromSliceLeaky(Value, a,+ \\{"role":"assistant","content":"","tool_calls":[{"id":"c1","type":"function","function":{"name":"list_comments","arguments":"{\"issue_ids\": \"[\\\"ISS-1\\\", \\\"ISS-2\\\"]\", \"limit\": \"5\", \"include_resolved\": \"false\", \"query\": \"42\"}"}}]}+ , .{});+ try std.testing.expect(try repair.retypeArgs(a, a, &message, tools));+ const text = message.object.get("tool_calls").?.array.items[0].object.get("function").?.object.get("arguments").?.string;+ const args = try std.json.parseFromSliceLeaky(Value, a, text, .{});+ try std.testing.expectEqual(@as(usize, 2), args.object.get("issue_ids").?.array.items.len);+ try std.testing.expectEqual(@as(i64, 5), args.object.get("limit").?.integer);+ try std.testing.expectEqual(false, args.object.get("include_resolved").?.bool);+ try std.testing.expectEqualStrings("42", args.object.get("query").?.string); // a string parameter stays one+ try std.testing.expect(!try repair.retypeArgs(a, a, &message, tools)); // already typed+}+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();