Skip to content

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

Python

mcp: loaded schemas outlive the arena that loaded them

justrachb871340c6b52
diff --git a/src/mcp_schema_gate.zig b/src/mcp_schema_gate.zig
index 676388bc..1dd1a9f0 100644
--- a/src/mcp_schema_gate.zig
+++ b/src/mcp_schema_gate.zig
@@ -31,10 +31,9 @@
//! 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
-//! meta tool, handled inline — and pool threads only ever read it, always
-//! after the spawn that publishes the write.
+//! Session state (which schemas have been loaded) is an append-only list in
+//! its own process arena: writers lock (a direct MCP call auto-loads from a
+//! pool thread), and readers follow the atomically published head.
const std = @import("std");
const Io = std.Io;
@@ -161,14 +160,21 @@ pub fn isLoaded(name: []const u8) bool {
return find(name) != null;
}
-/// Publish one loaded schema. Single-writer (the orchestrator thread); the
-/// release store is what makes the new node visible to a pool thread that
-/// later acquires the head.
-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, .seq = nextSeq(), .next = g_head.load(.acquire) };
+/// Nodes outlive every turn arena (a scripted REPL freed its conversation arena
+/// before the final save walked the list); `reset` only drops the head. The
+/// spin-lock serializes writers: exec.zig auto-loads from pool threads.
+var g_nodes: std.heap.ArenaAllocator = .init(std.heap.page_allocator);
+var g_nodes_lock: std.atomic.Value(bool) = .init(false);
+
+/// Publish a copy of one loaded schema (its name is the registry's, `rendered`
+/// the caller's); the release store makes it visible to pool threads.
+fn publish(name: []const u8, rendered: []const u8) !*Node {
+ while (g_nodes_lock.cmpxchgWeak(false, true, .acquire, .monotonic) != null) std.atomic.spinLoopHint();
+ defer g_nodes_lock.store(false, .release);
+ if (find(name)) |node| return node; // another thread got there first
+ const a = g_nodes.allocator();
+ const node = try a.create(Node);
+ node.* = .{ .name = try a.dupe(u8, name), .rendered = try a.dupe(u8, rendered), .seq = nextSeq(), .next = g_head.load(.acquire) };
g_head.store(node, .release);
return node;
}
@@ -362,9 +368,7 @@ fn renderEntry(arena: Allocator, tool: mcp.Tool) ![]const u8 {
/// Render, or reuse this session's cached render, and mark the tool enabled.
fn enable(arena: Allocator, tool: mcp.Tool) ![]const u8 {
if (find(tool.qualified_name)) |node| return node.rendered;
- const rendered = try renderEntry(arena, tool);
- const node = try publish(arena, tool.qualified_name, rendered);
- return node.rendered;
+ return (try publish(tool.qualified_name, try renderEntry(arena, tool))).rendered;
}
/// The pure core of the `load_tool_schemas` tool: everything except touching
diff --git a/src/mcp_schema_gate_tests.zig b/src/mcp_schema_gate_tests.zig
index ec80d1a2..6b698436 100644
--- a/src/mcp_schema_gate_tests.zig
+++ b/src/mcp_schema_gate_tests.zig
@@ -419,10 +419,12 @@ test "mixed native MCP loads preserve earlier emitted tool entries" {
const loaded = try gate.loadInto(a, fat, load);
try testing.expectEqual(@as(usize, 1), loaded.loaded);
const routes = [_]struct { id: []const u8, kind: @import("provider.zig").Provider.Kind, model: []const u8 }{
- // MiMo and DeepSeek send the loaded tail as a message instead (additional_tools_tests.zig).
+ // Routes that announce loaded tools instead (MiMo, DeepSeek, Claude on
+ // Codegraff, Codex and the OpenAI API on hosted-search models) are
+ // covered in additional_tools_tests.zig.
.{ .id = "zai", .kind = .openai, .model = "glm-5.3" },
.{ .id = "xai", .kind = .responses, .model = "grok-4.6" },
- .{ .id = "codex", .kind = .responses, .model = "gpt-6-sol" },
+ .{ .id = "codex", .kind = .responses, .model = "gpt-5.3-codex" },
};
var before: [routes.len][]const u8 = undefined;
var catalogs: [routes.len][]const u8 = undefined;
@@ -456,3 +458,25 @@ test "mixed native MCP loads preserve earlier emitted tool entries" {
try testing.expect(std.mem.indexOf(u8, body, "todo_read") != null);
}
}
+
+test "loaded schemas outlive the arena that loaded them (the scripted REPL's final save)" {
+ gate.reset();
+ defer gate.reset();
+ const saved = gate.g_policy;
+ defer gate.g_policy = saved;
+ gate.g_policy = .{};
+ var turn = std.heap.ArenaAllocator.init(testing.allocator);
+ const tools = try fixture(turn.allocator(), "gone", 2, 100);
+ const load = try std.json.parseFromSliceLeaky(Value, turn.allocator(), "{\"server\":\"gone\"}", .{});
+ try testing.expectEqual(@as(usize, 2), (try gate.loadInto(turn.allocator(), tools, load)).loaded);
+ turn.deinit(); // the turn's arena is gone; the loaded list is not
+ try testing.expect(gate.loadSeq("mcp__gone__t0") != null);
+
+ var later = std.heap.ArenaAllocator.init(testing.allocator);
+ defer later.deinit();
+ const again = try fixture(later.allocator(), "gone", 2, 100);
+ const reload = try std.json.parseFromSliceLeaky(Value, later.allocator(), "{\"server\":\"gone\"}", .{});
+ const r = try gate.loadInto(later.allocator(), again, reload);
+ try testing.expectEqual(@as(usize, 0), r.loaded); // already loaded: the kept render is reused
+ try testing.expect(std.mem.indexOf(u8, r.text, "mcp__gone__t0") != null);
+}