graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
feat(mcp): GRAFF_STABLE_CATALOG experiment + no-identical-repeat prompt line
Rach Pradhanb3b785b33704
diff --git a/src/mcp_schema_gate.zig b/src/mcp_schema_gate.zigindex 41f00bbc..5c3d647e 100644--- a/src/mcp_schema_gate.zig+++ b/src/mcp_schema_gate.zig@@ -99,6 +99,23 @@ pub const Policy = struct {pub var g_policy: Policy = .{};+/// GRAFF_STABLE_CATALOG=1 experiment (#476): a loaded tool's schema rides the+/// load_tool_schemas RESULT in the conversation, so the catalog does not need+/// to re-render it — skipping policy-deferred tools even after load keeps the+/// request prefix byte-identical all session, which is what provider prefix+/// caching charges against. Every load otherwise busts the cache (measured:+/// 0% cache on the calls right after each load).+pub var g_stable_catalog = false;++/// Deferred by POLICY, ignoring load state — the stable-catalog render rule+/// and (in stable mode) the listing, which must not change as tools load.+pub fn policyDeferred(all: []const mcp.Tool, tool: mcp.Tool) bool {+ if (!g_policy.enabled) return false;+ const server = serverOf(tool.qualified_name);+ if (pinnedEager(server)) return false;+ return serverCost(all, server) > g_policy.budget;+}+// --- session state: which schemas have been loaded -------------------------const Node = struct {@@ -453,9 +470,12 @@ fn matchQuery(arena: Allocator, all: []const mcp.Tool, query: []const u8) ![]usi/// listing), and `query` covers discovery beyond it (#476).pub fn descWithListing(arena: Allocator, all: []const mcp.Tool) ![]const u8 {if (!anyDeferred(all)) return tool_desc;+ // Stable-catalog mode lists by POLICY, not load state: a listing that+ // shrinks as tools load would change the prefix this mode exists to fix.+ const deferredRule: *const fn ([]const mcp.Tool, mcp.Tool) bool = if (g_stable_catalog) &policyDeferred else &isDeferred;var servers: std.ArrayList([]const u8) = .empty;for (all) |t| {- if (!isDeferred(all, t)) continue;+ if (!deferredRule(all, t)) continue;const sv = serverOf(t.qualified_name);const seen = for (servers.items) |s| {if (std.mem.eql(u8, s, sv)) break true;@@ -469,7 +489,7 @@ pub fn descWithListing(arena: Allocator, all: []const mcp.Tool) ![]const u8 {try aw.writer.print(" {s} (", .{sv});var first = true;for (all) |t| {- if (!isDeferred(all, t)) continue;+ if (!deferredRule(all, t)) continue;if (!std.mem.eql(u8, serverOf(t.qualified_name), sv)) continue;if (!first) try aw.writer.writeAll(", ");try aw.writer.writeAll(shortName(t.qualified_name));diff --git a/src/mcp_schema_gate_tests.zig b/src/mcp_schema_gate_tests.zigindex 6036880c..a22cb1c5 100644--- a/src/mcp_schema_gate_tests.zig+++ b/src/mcp_schema_gate_tests.zig@@ -282,6 +282,31 @@ test "query mode: keywords load the matching deferred tools, a miss lists what etry testing.expect(std.mem.indexOf(u8, none.text, "mcp__fat__t0") != null); // the listing follows}+test "stable catalog: a load does not change the rendered catalog (#476 experiment)" {+ withDefaults();+ defer withDefaults();+ var arena_state = std.heap.ArenaAllocator.init(testing.allocator);+ defer arena_state.deinit();+ const arena = arena_state.allocator();+ const fat = try fixture(arena, "fat", 3, 2000);+ const meta_spec = [_]@import("schema.zig").ToolSpec{.{ .name = gate.tool_name, .desc = gate.tool_desc, .schema = gate.tool_schema }};++ gate.g_stable_catalog = true;+ defer gate.g_stable_catalog = false;+ const schema_mod = @import("schema.zig");+ const before = try schema_mod.renderRootTools(arena, .anthropic, &meta_spec, fat);+ const req = try std.json.parseFromSliceLeaky(Value, arena, "{\"tools\":[\"mcp__fat__t0\"]}", .{ .allocate = .alloc_always });+ const loaded = try gate.loadInto(arena, fat, req);+ try testing.expect(!loaded.is_error);+ try testing.expectEqual(@as(usize, 1), loaded.loaded);+ const after = try schema_mod.renderRootTools(arena, .anthropic, &meta_spec, fat);+ try testing.expectEqualStrings(before, after); // the whole point: prefix cache survives a load++ gate.g_stable_catalog = false;+ const normal = try schema_mod.renderRootTools(arena, .anthropic, &meta_spec, fat);+ try testing.expect(std.mem.indexOf(u8, normal, "mcp__fat__t0") != null); // default: loaded tools rejoin the catalog+}+test "an unknown name errors and lists what is actually deferred; no args just lists" {withDefaults();defer withDefaults();diff --git a/src/native_fold.zig b/src/native_fold.zigindex b76a3028..0a86f1ad 100644--- a/src/native_fold.zig+++ b/src/native_fold.zig@@ -93,6 +93,16 @@ pub fn blocked(name: []const u8) bool {return enabled and isFolded(name) and !isLoaded(name);}+/// Whether the root catalog skips this native spec at render: folded and not+/// yet loaded — or, under GRAFF_STABLE_CATALOG, folded at all (the loaded+/// schema already rides the load result; a re-render would bust the prefix+/// cache the mode exists to protect, #476).+pub fn catalogSkips(name: []const u8) bool {+ if (!enabled) return false;+ if (mcp_schema_gate.g_stable_catalog) return isFolded(name);+ return blocked(name);+}+/// Same refusal style as mcp_schema_gate.refusalText: name the fix, not just/// the block, so the model loads instead of retrying.pub fn refusalText(gpa: Allocator, name: []const u8) ![]u8 {diff --git a/src/prompt_snapshot_tests.zig b/src/prompt_snapshot_tests.zigindex f0be1bbb..e2c6925f 100644--- a/src/prompt_snapshot_tests.zig+++ b/src/prompt_snapshot_tests.zig@@ -109,7 +109,9 @@ const golden_full_prompt =\\blocks you, ask; otherwise decide and go. Match the verification to the\\ask: make the requested thing work and prove it — do not add unrequested\\tests, coverage, or review passes; thoroughness past the ask is turns,- \\tokens, and diff noise the user did not order.+ \\tokens, and diff noise the user did not order. And never repeat a tool+ \\call with identical parameters once you have a usable result — the answer+ \\will not change; reread only on stale source, ambiguity, or failure.\\\\Before a large chunk of work, give a one- or two-sentence heads-up on what\\you are about to do; on long tasks, drop a brief note as each phase lands.diff --git a/src/prompt_text.zig b/src/prompt_text.zigindex 0a56aea0..9bc4e21e 100644--- a/src/prompt_text.zig+++ b/src/prompt_text.zig@@ -153,7 +153,9 @@ pub const work_note =\\blocks you, ask; otherwise decide and go. Match the verification to the\\ask: make the requested thing work and prove it — do not add unrequested\\tests, coverage, or review passes; thoroughness past the ask is turns,- \\tokens, and diff noise the user did not order.+ \\tokens, and diff noise the user did not order. And never repeat a tool+ \\call with identical parameters once you have a usable result — the answer+ \\will not change; reread only on stale source, ambiguity, or failure.;/// Always present: narration is a habit, not a capability.diff --git a/src/schema.zig b/src/schema.zigindex 5f0f5b5f..2ad524d7 100644--- a/src/schema.zig+++ b/src/schema.zig@@ -300,7 +300,7 @@ pub fn renderRootTools(try s.beginArray();for (specs) |t| {if (mcp_schema_gate.hiddenSpec(t.name, mcp_tools) and !native_fold.anyFolded()) continue; // #416: load_tool_schemas hides only with nothing deferred OR folded- if (native_fold.blocked(t.name)) continue; // zero-stub like the MCP half: folded natives ride the meta tool's listing (#476)+ if (native_fold.catalogSkips(t.name)) continue; // zero-stub like the MCP half: folded natives ride the meta tool's listing (#476)if (std.mem.eql(u8, t.name, mcp_schema_gate.tool_name))try writeToolEntry(&s, kind, t.name, try mcp_schema_gate.descWithListing(out, mcp_tools), .{ .raw = t.schema })else@@ -309,7 +309,9 @@ pub fn renderRootTools(// Deferred tools ship no catalog entries at all (codex tool_search// pattern): the meta tool's description lists what exists, exec.zig's// layer-2 refusal still guards a call that arrives before its load.- for (mcp_tools) |m| if (!mcp_schema_gate.isDeferred(mcp_tools, m))+ // GRAFF_STABLE_CATALOG keeps skipping policy-deferred tools even after+ // they load — a re-rendered catalog busts the provider's prefix cache.+ for (mcp_tools) |m| if (!mcp_schema_gate.isDeferred(mcp_tools, m) and !(mcp_schema_gate.g_stable_catalog and mcp_schema_gate.policyDeferred(mcp_tools, m)))try writeToolEntry(&s, kind, m.qualified_name, m.description, .{ .value = m.input_schema });try s.endArray();return aw.toOwnedSlice();diff --git a/src/session_settings.zig b/src/session_settings.zigindex 3f1d74ee..b3b7d4c4 100644--- a/src/session_settings.zig+++ b/src/session_settings.zig@@ -21,6 +21,7 @@ const ws = @import("ws.zig");const agent_ws = @import("agent_ws.zig"); // codex_ws_idle_ms override (#codex-ws)const agent_request = @import("agent_request.zig"); // GRAFF_REQ_STATS → g_req_stats (token-diet measurement)const native_fold = @import("native_fold.zig"); // GRAFF_NO_NATIVE_FOLD → enabled+const mcp_schema_gate = @import("mcp_schema_gate.zig"); // GRAFF_STABLE_CATALOG → g_stable_catalogconst no_local_tools = @import("no_local_tools.zig"); // #330: GRAFF_NO_LOCAL_TOOLSconst tool_handle = @import("tool_handle.zig"); // #440: GRAFF_TOOL_HANDLE_BYTESconst provider_mod = @import("provider.zig");@@ -97,6 +98,11 @@ pub fn applyEnvKnobs(arena: Allocator, environ_map: anytype) !void {// GRAFF_NO_NATIVE_FOLD: presence-based — restore full power-tool schemas// in every request (the pre-fold interactive surface).if (environ_map.get("GRAFF_NO_NATIVE_FOLD") != null) native_fold.enabled = false;+ // GRAFF_STABLE_CATALOG: presence-based experiment (#476) — policy-deferred+ // tools stay out of the catalog even after load_tool_schemas enables them,+ // keeping the request prefix byte-identical all session for the provider's+ // prefix cache. The loaded schema rides the load result in-conversation.+ if (environ_map.get("GRAFF_STABLE_CATALOG") != null) mcp_schema_gate.g_stable_catalog = true;// (#codex-ws) GRAFF_CODEX_WS_IDLE_SECS raises/lowers the held-WS idle limit// (default 4 min — the backend killed ours within 8.5 min idle; opencode// pools at 5). Mirrors GRAFF_STREAM_STALL_SECS above: seconds, ignored if