Skip to content

graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs

Python

feat(mcp): defer every server by default — retire the 4 KiB eager budget

Rach Pradhane28c02517277
diff --git a/src/mcp_schema_gate.zig b/src/mcp_schema_gate.zig
index 4625ea14..bbf28457 100644
--- a/src/mcp_schema_gate.zig
+++ b/src/mcp_schema_gate.zig
@@ -24,11 +24,12 @@
//! consent prompt is reached in exactly the cases it was reached before, in
//! the same order, and deferral can only ever subtract a call that consent
//! already allowed — never add one it did not.
-//! - **Small servers behave exactly as they do today.** The default is
-//! size-based: a server whose tools cost at most `default_budget` bytes of
-//! description + schema stays EAGER, because the load round trip re-emits
-//! those same bytes and would be a wash. Only servers above the threshold
-//! are deferred, plus whatever the user pins either way.
+//! - **Deferral is universal.** Every server's tools ship as placeholders
+//! until `load_tool_schemas` unfolds them — the measured interactive
+//! anatomy (#476) is that most sessions never call most servers, so eager
+//! bytes are re-sent cost with no payoff, and discovery has proven
+//! reliable. `GRAFF_MCP_SCHEMA_BUDGET` restores the old size-based
+//! threshold; `GRAFF_MCP_EAGER` pins specific servers either way.
//!
//! Session state (which schemas have been loaded) is a lock-free append-only
//! list: the only writer is the orchestrator thread — `load_tool_schemas` is a
@@ -48,12 +49,15 @@ const util = @import("util.zig");
const ExecResult = tools_mod.ExecResult;
/// Per-server budget, in bytes of description + serialized schema, under which
-/// a server is served eagerly. 4 KiB is roughly 1,000 tokens: below that the
-/// `load_tool_schemas` round trip (a tool call plus a result that re-emits the
-/// very schemas it saved) costs about what deferral saves, so the indirection
-/// only pays above it. It also means every small server keeps working byte for
-/// byte as it did before #416.
-pub const default_budget: usize = 4096;
+/// a server is served eagerly. Zero by default: EVERY server defers. The old
+/// 4 KiB threshold assumed the load round trip was a wash below ~1,000 tokens,
+/// but that only holds for sessions that call the tool — the measured
+/// interactive anatomy (#476) is that most sessions never touch most servers,
+/// so eager bytes are pure re-sent cost and discovery via load_tool_schemas
+/// has proven reliable (the codedbpro pin removal shipped with zero discovery
+/// turns). GRAFF_MCP_SCHEMA_BUDGET restores a size threshold;
+/// GRAFF_MCP_EAGER pins specific servers.
+pub const default_budget: usize = 0;
/// How much of an MCP tool's description survives into the deferred listing.
/// Enough for a model to tell tools apart and decide what to load; a real MCP
diff --git a/src/mcp_schema_gate_tests.zig b/src/mcp_schema_gate_tests.zig
index 17cdd652..f16b68c3 100644
--- a/src/mcp_schema_gate_tests.zig
+++ b/src/mcp_schema_gate_tests.zig
@@ -70,20 +70,27 @@ test "jsonBytes tracks the real serialized size closely enough to budget with" {
try testing.expect(estimate <= actual + actual / 10);
}
-test "a small server stays eager, an expensive one is deferred (#416 default)" {
+test "every server defers by default; the budget knob restores size-based eagerness (#476)" {
withDefaults();
defer withDefaults();
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
defer arena_state.deinit();
const arena = arena_state.allocator();
+ // Universal deferral: even a tiny server ships placeholders by default.
const small = try fixture(arena, "small", 2, 40);
- try testing.expect(gate.serverCost(small, "small") <= gate.default_budget);
+ try testing.expect(gate.serverCost(small, "small") > gate.default_budget);
+ for (small) |t| try testing.expect(gate.isDeferred(small, t));
+ try testing.expect(gate.anyDeferred(small));
+
+ // The old size-based policy stays reachable via GRAFF_MCP_SCHEMA_BUDGET.
+ gate.g_policy.budget = 4096;
+ try testing.expect(gate.serverCost(small, "small") <= gate.g_policy.budget);
for (small) |t| try testing.expect(!gate.isDeferred(small, t));
try testing.expect(!gate.anyDeferred(small));
const fat = try fixture(arena, "fat", 8, 2000);
- try testing.expect(gate.serverCost(fat, "fat") > gate.default_budget);
+ try testing.expect(gate.serverCost(fat, "fat") > gate.g_policy.budget);
for (fat) |t| try testing.expect(gate.isDeferred(fat, t));
try testing.expect(gate.anyDeferred(fat));
}
@@ -276,6 +283,8 @@ test "an unknown name errors and lists what is actually deferred; no args just l
try testing.expect(std.mem.indexOf(u8, list.text, "second line nobody needs up front") == null);
// Nothing deferred at all: say so rather than printing an empty list.
+ // (Reachable via the budget knob now that every server defers by default.)
+ gate.g_policy.budget = 4096;
const small = try fixture(arena, "small", 1, 4);
const quiet = try gate.loadInto(arena, small, none);
try testing.expect(!quiet.is_error);
@@ -289,6 +298,7 @@ test "an eager tool is never blocked, and the load tool is advertised only when
defer arena_state.deinit();
const arena = arena_state.allocator();
const small = try fixture(arena, "small", 2, 40);
+ gate.g_policy.budget = 4096; // make "small" eager explicitly — the default defers everything (#476)
try testing.expect(!gate.blocked(small, "mcp__small__t0"));
try testing.expect(!gate.hiddenSpec("bash", small)); // only the load tool is ever hidden
try testing.expect(gate.hiddenSpec(gate.tool_name, small)); // nothing deferred -> not advertised
diff --git a/src/tool_schema_tests.zig b/src/tool_schema_tests.zig
index 8f1eef24..00f219a7 100644
--- a/src/tool_schema_tests.zig
+++ b/src/tool_schema_tests.zig
@@ -237,7 +237,8 @@ test "#416: deferring a real MCP server's schemas cuts the served catalog in hal
gate.g_policy = .{ .eager = &.{"*"} };
const eager = try schema.renderRootTools(arena, .anthropic, &.{}, tools);
- // The default: this server is 2.3x over the 4 KiB budget, so it defers.
+ // The default: every server defers (budget 0, #476) — this one is 2.3x
+ // over even the old 4 KiB budget, so it deferred under that policy too.
gate.g_policy = .{};
try testing.expect(gate.serverCost(tools, "smolify") > gate.default_budget);
const deferred = try schema.renderRootTools(arena, .anthropic, &.{}, tools);