graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
chat: announce loaded tools on more Codegraff models, retype their arguments
justrach241ce0a89e34
diff --git a/docs/adr/0221-chatgpt-new-sign-in.md b/docs/adr/0221-chatgpt-new-sign-in.mdindex 08fc3a9d..9c127541 100644--- a/docs/adr/0221-chatgpt-new-sign-in.md+++ b/docs/adr/0221-chatgpt-new-sign-in.md@@ -169,6 +169,19 @@ process rather than restarting anything.transcript leaves it out. A wire change drops it (`history_translate`).Hot-context updates still ride a system message on chat wires, so onDeepSeek each project-instructions change costs one miss.+- **Claude on the Codegraff route announces loaded tools in a user message+ too.** There a changed `tools` cost the whole cached prompt: a probe that+ appended one tool read nothing back, and a system message re-billed the+ conversation. Claude calls an announced tool, but like MiMo it returns the+ values as strings, so `retypeArgs` now runs on every announcing chat route.+ It reads the root catalog, and a direct call to a deferred tool loads it+ without a rebuild (`native_fold.gateExec`, `mcp_schema_gate.autoLoad`):+ before a request the root now rebuilds the catalog when a loaded tool is+ missing from it (`additional_tools.staleCatalog`), which leaves the wire+ unchanged. Claude on Anthropic's own API still appends to `tools`. Its fix+ is a `tool_addition` block in a mid-conversation system message (beta+ `inline-tools-2026-09-15`), which declares the tool, so its arguments+ come back typed, and it needs a live check first.- **The first request carries every MCP server that is ready.** `-p` startsMCP in the background and never waits out a handshake (ADR 0035). A serverrestored from cache was ready before the first request, but that requestdiff --git a/src/additional_tools.zig b/src/additional_tools.zigindex c8ba9920..580ea515 100644--- a/src/additional_tools.zig+++ b/src/additional_tools.zig@@ -25,6 +25,12 @@//! (session_wake.isNotice). The model calls announced tools with typed//! arguments, as it does declared ones.//!+//! Claude through Codegraff loses the whole prompt to a changed `tools` (a+//! probe that appended one tool went from a full hit to none), and a system+//! message there re-bills the conversation, so its announcement is user-role+//! too. It calls announced tools but, like MiMo, returns their values as+//! strings, so retypeArgs runs on every chat route here.+//!//! 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.@@ -49,16 +55,25 @@ 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) or deepseekRoute(p),+ .openai => @import("effort_route.zig").mimoRoute(p.id, p.model) or deepseekRoute(p) or claudeRoute(p),.anthropic, .interactions => false,};}+/// The model id without a family prefix (`vendor/model`).+fn modelName(model: []const u8) []const u8 {+ return if (std.mem.lastIndexOfScalar(u8, model, '/')) |slash| model[slash + 1 ..] else model;+}+/// DeepSeek models, direct or through Codegraff.fn deepseekRoute(p: Provider) bool {- const name = if (std.mem.lastIndexOfScalar(u8, p.model, '/')) |slash| p.model[slash + 1 ..] else p.model;return (std.mem.eql(u8, p.id, "deepseek") or std.mem.eql(u8, p.id, "codegraff")) and- std.ascii.startsWithIgnoreCase(name, "deepseek-");+ std.ascii.startsWithIgnoreCase(modelName(p.model), "deepseek-");+}++/// Claude models through Codegraff's chat wire.+fn claudeRoute(p: Provider) bool {+ return std.mem.eql(u8, p.id, "codegraff") and std.ascii.startsWithIgnoreCase(modelName(p.model), "claude-");}/// The loaded tail as tool entries for the agent's wire: exactly what the@@ -108,6 +123,21 @@ fn announced(arena: Allocator, messages: []const Value, name: []const u8) bool {return false;}+/// A direct call to a deferred tool loads it without a catalog rebuild+/// (native_fold.gateExec, mcp_schema_gate.autoLoad). The announcement renders+/// the tail itself, but retypeArgs reads the catalog, so the root rebuilds it+/// when a loaded tool is missing. `tools` on the wire stays the same.+pub fn staleCatalog(self: *Agent) bool {+ if (self.sub or !active(self.provider)) return false;+ const arena = self.scratchAlloc();+ const catalog = self.toolsJson();+ for (tailEntries(self, arena) catch return false) |t| {+ const needle = std.fmt.allocPrint(arena, "\"name\":\"{s}\"", .{toolName(t)}) catch return false;+ if (std.mem.indexOf(u8, catalog, needle) == null) return true;+ }+ return false;+}+fn inTail(tail: []const Value, name: []const u8) bool {for (tail) |t| if (std.mem.eql(u8, toolName(t), name)) return true;return false;@@ -130,7 +160,7 @@ pub fn sync(self: *Agent) void {/// A developer-role `additional_tools` item on Responses. The chat wire has no/// such item, so there it is a message with one definition per line: system-/// on MiMo, user on DeepSeek.+/// on MiMo, user elsewhere.fn announcement(arena: Allocator, p: Provider, tools: std.json.Array) !Value {var item: std.json.ObjectMap = .empty;if (p.kind == .responses) {@@ -143,7 +173,7 @@ fn announcement(arena: Allocator, p: Provider, tools: std.json.Array) !Value {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 = if (deepseekRoute(p)) "user" else "system" });+ try item.put(arena, "role", .{ .string = if (@import("effort_route.zig").mimoRoute(p.id, p.model)) "system" else "user" });try item.put(arena, "content", .{ .string = aw.writer.buffered() });try item.put(arena, origin_key, .{ .string = item_type });return .{ .object = item };diff --git a/src/additional_tools_tests.zig b/src/additional_tools_tests.zigindex 3ee70cad..1b777e56 100644--- a/src/additional_tools_tests.zig+++ b/src/additional_tools_tests.zig@@ -172,7 +172,7 @@ test "MiMo chat: a tool load leaves tools byte-identical and adds one system anntry std.testing.expectEqual(@as(usize, 0), items(agent.messages.items));}-test "DeepSeek chat: a tool load leaves tools byte-identical and adds one user announcement that is not a prompt" {+test "DeepSeek and Claude chat: a tool load leaves tools byte-identical and adds one user announcement that is not a prompt" {var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);defer arena_state.deinit();const a = arena_state.allocator();@@ -186,8 +186,12 @@ test "DeepSeek chat: a tool load leaves tools byte-identical and adds one user anative_fold.enabled = true;mcp_schema_gate.g_stable_catalog = true;- // DeepSeek's own API and the gateway, Pro and Flash.- for ([_][2][]const u8{ .{ "deepseek", "deepseek-v4-pro" }, .{ "codegraff", "deepseek-v4-flash" } }) |route| {+ // Claude keeps its tail in `tools` on its own API and elsewhere until each is checked.+ try std.testing.expect(!additional_tools.active((try testAgentFor(a, "anthropic", .anthropic, "claude-sonnet-5-5")).provider));+ try std.testing.expect(!additional_tools.active((try testAgentFor(a, "openrouter", .openai, "anthropic/claude-sonnet-5.5")).provider));++ // DeepSeek's own API and the gateway, Pro and Flash, and Claude on the gateway.+ for ([_][2][]const u8{ .{ "deepseek", "deepseek-v4-pro" }, .{ "codegraff", "deepseek-v4-flash" }, .{ "codegraff", "claude-sonnet-5-5" } }) |route| {native_fold.clearLoadedSession();var agent = try testAgentFor(a, route[0], .openai, route[1]);try std.testing.expect(additional_tools.active(agent.provider));@@ -202,7 +206,7 @@ test "DeepSeek chat: a tool load leaves tools byte-identical and adds one user aadditional_tools.sync(&agent);try std.testing.expectEqual(@as(usize, 1), items(agent.messages.items));const note = agent.messages.items[agent.messages.items.len - 1];- // User-role: DeepSeek moves system messages up into the system prompt.+ // User-role: on V4 Pro and the Claude route a system message would re-bill the conversation.try std.testing.expectEqualStrings("user", note.object.get("role").?.string);try std.testing.expect(std.mem.indexOf(u8, note.object.get("content").?.string, "\"name\":\"workflow\"") != null);// Harness text, not something the human typed.@@ -222,3 +226,41 @@ test "DeepSeek chat: a tool load leaves tools byte-identical and adds one user afor (msgs.items) |m| try std.testing.expect(std.mem.indexOf(u8, try std.json.Stringify.valueAlloc(a, m, .{}), "workflow") == null);}}++test "Claude chat: a direct call's load reaches the catalog, so its string arguments get their types" {+ 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, "claude-sonnet-5-5");+ agent.invalidateRootTools();+ try agent.ensureRootTools(.openai);+ try std.testing.expect(!additional_tools.staleCatalog(&agent));++ // The model calls agent_output without loading it: the call loads it, the catalog is not rebuilt.+ try std.testing.expect(native_fold.gateExec(a, "agent_output", false) == null);+ try std.testing.expect(additional_tools.staleCatalog(&agent));+ agent.invalidateRootTools();+ try agent.ensureRootTools(.openai);+ try std.testing.expect(!additional_tools.staleCatalog(&agent));++ // Claude sent an undeclared tool's values as strings; the rebuilt catalog types them.+ var message = try std.json.parseFromSliceLeaky(std.json.Value, a,+ \\{"role":"assistant","content":"","tool_calls":[{"id":"c1","type":"function","function":{"name":"agent_output","arguments":"{\"id\": \"1\", \"wait_ms\": \"300000\"}"}}]}+ , .{});+ try std.testing.expect(try @import("tool_call_repair.zig").retypeArgs(a, a, &message, agent.toolsJson()));+ const text = message.object.get("tool_calls").?.array.items[0].object.get("function").?.object.get("arguments").?.string;+ const args = try std.json.parseFromSliceLeaky(std.json.Value, a, text, .{});+ try std.testing.expectEqual(@as(i64, 1), args.object.get("id").?.integer);+ try std.testing.expectEqual(@as(i64, 300000), args.object.get("wait_ms").?.integer);+}diff --git a/src/agent_request.zig b/src/agent_request.zigindex 32930e52..3e30d2d8 100644--- a/src/agent_request.zig+++ b/src/agent_request.zig@@ -100,18 +100,17 @@ pub fn request(self: *Agent, tools_in: ?[]const u8) !std.json.ObjectMap {http.waitForClientReady(self.io);if (http.takeCaWarmFailure()) if (self.tracer) |tr| tr.note("ca_prewarm_failed", "CA bundle rescan failed; request will use lazy TLS initialization");var tools_now = tools_in;- if (self.registry) |reg| {- const stale = self.toolsJson();- if (@import("mcp_pages.zig").beforeRequest(reg)) { // joins deferred starts, re-lists changed servers- self.invalidateRootTools();- try self.ensureRootTools(self.provider.kind);- // The caller's snapshot is the catalog that just went stale. Send the- // rebuilt one, or servers that joined miss this request and change- // `tools` on the next, throwing away the prompt cache (ADR 0221).- if (tools_now) |t| if (stale.len > 0 and t.ptr == stale.ptr) {- tools_now = self.toolsJson();- };- }+ const stale = self.toolsJson();+ const joined = if (self.registry) |reg| @import("mcp_pages.zig").beforeRequest(reg) else false; // joins deferred starts, re-lists changed servers+ if (joined or @import("additional_tools.zig").staleCatalog(self)) {+ self.invalidateRootTools();+ try self.ensureRootTools(self.provider.kind);+ // The caller's snapshot is the catalog that just went stale. Send the+ // rebuilt one, or servers that joined miss this request and change+ // `tools` on the next, throwing away the prompt cache (ADR 0221).+ if (tools_now) |t| if (stale.len > 0 and t.ptr == stale.ptr) {+ tools_now = self.toolsJson();+ };}var budget_permit: ?run_budget_mod.Permit = null;if (self.run_budget) |budget| {diff --git a/src/agent_steps.zig b/src/agent_steps.zigindex 28f62d2f..58320a6c 100644--- a/src/agent_steps.zig+++ b/src/agent_steps.zig@@ -266,10 +266,11 @@ 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- }+ // Announced tools come back untyped where the wire types only declared ones (MiMo, Claude).+ if (@import("additional_tools.zig").active(self.provider))+ _ = try tool_call_repair.retypeArgs(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 {diff --git a/src/tool_call_repair.zig b/src/tool_call_repair.zigindex ca656f15..9f2f0f03 100644--- a/src/tool_call_repair.zig+++ b/src/tool_call_repair.zig@@ -305,10 +305,11 @@ 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.+/// MiMo's and Claude's parsers type 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;