Skip to content

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

Python

experiment(mcp): stable-catalog v2 — loaded tools append in load order

Rach Pradhan18ff86c2427c
diff --git a/src/mcp_schema_gate.zig b/src/mcp_schema_gate.zig
index 5c3d647e..a58b0bc2 100644
--- a/src/mcp_schema_gate.zig
+++ b/src/mcp_schema_gate.zig
@@ -124,6 +124,9 @@ const Node = struct {
/// so a repeated load is a pointer copy rather than a re-serialization.
/// This is the per-session schema cache the issue asks for.
rendered: []const u8,
+ /// Load order (0 = never loaded): the stable-catalog tail appends in this
+ /// order so a new load changes only the catalog's tail bytes (#476).
+ seq: usize,
next: ?*Node,
};
@@ -162,11 +165,25 @@ fn publish(arena: Allocator, name: []const u8, rendered: []const u8) !*Node {
const node = try arena.create(Node);
// The name is COPIED, not aliased: `tool.qualified_name` belongs to the
// registry's arena, and this list has to outlive any registry rebuild.
- node.* = .{ .name = try arena.dupe(u8, name), .rendered = rendered, .next = g_head.load(.acquire) };
+ node.* = .{ .name = try arena.dupe(u8, name), .rendered = rendered, .seq = nextSeq(), .next = g_head.load(.acquire) };
g_head.store(node, .release);
return node;
}
+var g_seq: usize = 0;
+fn nextSeq() usize {
+ g_seq += 1;
+ return g_seq;
+}
+
+/// A loaded tool's position in load order; null when not loaded. schema.zig's
+/// stable-catalog tail sorts by this so loads are append-only.
+pub fn loadSeq(qualified: []const u8) ?usize {
+ var cur = g_head.load(.acquire);
+ while (cur) |n| : (cur = n.next) if (std.mem.eql(u8, n.name, qualified)) return n.seq;
+ return null;
+}
+
// --- policy ----------------------------------------------------------------
/// Resolve the session policy from the environment and the merged MCP config.
diff --git a/src/mcp_schema_gate_tests.zig b/src/mcp_schema_gate_tests.zig
index a22cb1c5..963834de 100644
--- a/src/mcp_schema_gate_tests.zig
+++ b/src/mcp_schema_gate_tests.zig
@@ -282,7 +282,7 @@ test "query mode: keywords load the matching deferred tools, a miss lists what e
try 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)" {
+test "stable catalog: a load only appends tail bytes, and the tool stays visible (#476)" {
withDefaults();
defer withDefaults();
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
@@ -290,21 +290,26 @@ test "stable catalog: a load does not change the rendered catalog (#476 experime
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 }};
+ const schema_mod = @import("schema.zig");
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
+ // Adoption stays (the v1 full-skip experiment failed on this): the loaded
+ // tool IS in the catalog…
+ try testing.expect(std.mem.indexOf(u8, after, "mcp__fat__t0") != null);
+ // …but only as tail bytes: everything up to the closing bracket is the
+ // pre-load catalog verbatim, so the provider's prefix cache survives.
+ try testing.expect(std.mem.startsWith(u8, after, before[0 .. before.len - 1]));
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
+ try testing.expect(std.mem.indexOf(u8, normal, "mcp__fat__t0") != null); // default: loaded tools rejoin in place
}
test "an unknown name errors and lists what is actually deferred; no args just lists" {
diff --git a/src/native_fold.zig b/src/native_fold.zig
index 0a86f1ad..7c6324b3 100644
--- a/src/native_fold.zig
+++ b/src/native_fold.zig
@@ -93,6 +93,36 @@ pub fn blocked(name: []const u8) bool {
return enabled and isFolded(name) and !isLoaded(name);
}
+/// The session's loaded folded natives, in load order (schema.zig's stable
+/// tail appends them after the stable head — load order keeps it append-only).
+pub fn loadedNames() []const []const u8 {
+ return g_loaded[0..g_loaded_len];
+}
+
+/// GRAFF_STABLE_CATALOG tail (schema.zig renderRootTools, #476): every tool
+/// whose schema has loaded this session, appended in LOAD ORDER after the
+/// stable catalog head — folded natives first, then MCP tools by gate seq.
+/// Append-only is the point: a load changes only the tools array's tail
+/// bytes, and the provider's prefix cache (tools serialize before messages)
+/// survives what a mid-array re-insertion would bust.
+pub fn renderLoadedTail(s: *std.json.Stringify, kind: @import("provider.zig").Provider.Kind, out: Allocator, mcp_tools: []const @import("mcp.zig").Tool) !void {
+ const schema_mod = @import("schema.zig");
+ const mcp = @import("mcp.zig");
+ for (loadedNames()) |name| {
+ const spec = (try findRootSpec(out, name)) orelse continue;
+ try schema_mod.writeToolEntry(s, kind, spec.name, spec.desc, .{ .raw = spec.schema });
+ }
+ var loaded: std.ArrayList(mcp.Tool) = .empty;
+ for (mcp_tools) |m| if (mcp_schema_gate.policyDeferred(mcp_tools, m) and mcp_schema_gate.loadSeq(m.qualified_name) != null)
+ try loaded.append(out, m);
+ std.mem.sort(mcp.Tool, loaded.items, {}, struct {
+ fn lt(_: void, a: mcp.Tool, b: mcp.Tool) bool {
+ return mcp_schema_gate.loadSeq(a.qualified_name).? < mcp_schema_gate.loadSeq(b.qualified_name).?;
+ }
+ }.lt);
+ for (loaded.items) |m| try schema_mod.writeToolEntry(s, kind, m.qualified_name, m.description, .{ .value = m.input_schema });
+}
+
/// 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
diff --git a/src/schema.zig b/src/schema.zig
index 2ad524d7..7be01d4b 100644
--- a/src/schema.zig
+++ b/src/schema.zig
@@ -306,24 +306,22 @@ pub fn renderRootTools(
else
try writeToolEntry(&s, kind, t.name, t.desc, .{ .raw = t.schema });
}
- // 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.
- // GRAFF_STABLE_CATALOG keeps skipping policy-deferred tools even after
- // they load — a re-rendered catalog busts the provider's prefix cache.
+ // Deferred tools ship no entries (codex tool_search pattern); the meta
+ // tool's desc lists them, exec.zig layer 2 guards premature calls.
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 });
+ // GRAFF_STABLE_CATALOG (#476): loaded tools append in LOAD ORDER after the
+ // stable head — loads change only tail bytes, the prefix cache survives.
+ if (mcp_schema_gate.g_stable_catalog) try native_fold.renderLoadedTail(&s, kind, out, mcp_tools);
try s.endArray();
return aw.toOwnedSlice();
}
/// Root tool catalog for the current session: optional tools are absent unless
/// enabled and usable, avoiding schema tokens for calls that cannot succeed.
-/// #330 layer 1, root half: the gate then drops the host-touching tools from
-/// whichever catalog was chosen, so they are never advertised to a provider.
-/// MCP tools are appended afterwards by renderRootTools and stay untouched.
-/// #352 layer 1, root half: the additive gate then appends each optional tool
-/// whose backing capability startup actually found on this machine.
+/// #330 drops host-touching tools from the chosen catalog; #352 appends each
+/// optional tool whose backing capability startup found. MCP tools are
+/// appended afterwards by renderRootTools and stay untouched.
pub fn effectiveRootSpecs(arena: Allocator) ![]const ToolSpec {
const chosen: []const ToolSpec = if (root.g_clock_sleep)
(if (learn_store.active_agent_loaded) &root_specs else &root_specs_without_learning)
@@ -342,7 +340,7 @@ fn writeSchema(s: *std.json.Stringify, schema: Schema) !void {
}
}
-fn writeToolEntry(s: *std.json.Stringify, kind: Provider.Kind, name: []const u8, desc: []const u8, schema: Schema) !void {
+pub fn writeToolEntry(s: *std.json.Stringify, kind: Provider.Kind, name: []const u8, desc: []const u8, schema: Schema) !void {
switch (kind) {
.anthropic => {
try s.beginObject();