Skip to content

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

Python

MCP cache: lock the read-modify-write; Linux stat for the shared dir

justrach2ab748e53a8c
diff --git a/scripts/test-mcp-lazy.py b/scripts/test-mcp-lazy.py
index 4480b295..4b0708e8 100644
--- a/scripts/test-mcp-lazy.py
+++ b/scripts/test-mcp-lazy.py
@@ -99,13 +99,16 @@ def main():
run(binary, work, env, [{'text': 'hello'}])
assert len(starts('lazy')) == 1, events(logs['lazy'], 'lazy')
cache = json.loads((temp / '.codegraff/mcp-list-cache.json').read_text())
- assert any(str(temp / 'server.py') in k for k in cache['servers']), cache
+ # Both servers connect at once; each must keep its own entry.
+ for n in ('lazy', 'pinned'):
+ assert any(str(logs[n]) in k for k in cache['servers']), (n, cache)
print('PASS a new server starts once so its tools can be learned and cached', flush=True)
# 2. Later session, no MCP call: tools advertised from cache, process never spawned.
before = len(starts('lazy'))
model = run(binary, work, env, [{'text': 'hello again'}])
- assert len(starts('lazy')) == before, 'the lazy server started without being used'
+ assert len(starts('lazy')) == before, 'the lazy server started without being used\n' + \
+ (temp / '.codegraff/mcp-list-cache.json').read_text()[:1500] + '\n' + model.stderr[-2500:]
assert '[mcp:lazy] ready — 1 tool(s), starts on first use' in model.stderr, model.stderr[-800:]
assert len(starts('pinned')) == 2, 'a "startup":"eager" server must still start with every session'
print('PASS a cached server is registered from its cached tools but not spawned; "startup": "eager" still starts at boot', flush=True)
diff --git a/scripts/test-mcp-legacy-fallback.py b/scripts/test-mcp-legacy-fallback.py
index c4c961b8..723f83a3 100644
--- a/scripts/test-mcp-legacy-fallback.py
+++ b/scripts/test-mcp-legacy-fallback.py
@@ -202,6 +202,9 @@ def run(graff: Path) -> None:
"GRAFF_FLEET": "off",
"GRAFF_NO_SMOLIFY": "1",
"GRAFF_NO_TELEMETRY": "1",
+ # Private home: no user servers, no cached era from a past run.
+ "HOME": str(workspace),
+ "USERPROFILE": str(workspace),
}
)
completed = subprocess.run(
diff --git a/scripts/test-mcp-stdio-probe.py b/scripts/test-mcp-stdio-probe.py
index 14042929..6b5deb8f 100644
--- a/scripts/test-mcp-stdio-probe.py
+++ b/scripts/test-mcp-stdio-probe.py
@@ -164,6 +164,11 @@ def run_graff(graff: Path, fixture: Path, probe: bool) -> subprocess.CompletedPr
"GRAFF_FLEET": "off",
"GRAFF_NO_SMOLIFY": "1",
"GRAFF_NO_TELEMETRY": "1",
+ # A private home per run: the user's own servers stay out, and
+ # the tools/list cache the gate-off run writes cannot turn the
+ # gate-on run into a cache hit that skips the probe.
+ "HOME": str(workspace),
+ "USERPROFILE": str(workspace),
}
)
# The probe is ON unless GRAFF_MCP_PROBE=0, so "gate off" has to say
diff --git a/src/mcp_cache.zig b/src/mcp_cache.zig
index 5326e2d3..f0fd0261 100644
--- a/src/mcp_cache.zig
+++ b/src/mcp_cache.zig
@@ -162,6 +162,15 @@ pub fn store(
const tools = if (result == .object) result.object.get("tools") else null;
if (tools == null or tools.? != .array) return;
const ttl = ttlFromResult(result);
+ const path = cachePath(arena, home);
+ if (path.len == 0) return;
+ if (std.fs.path.dirname(path)) |dir| Io.Dir.cwd().createDirPath(io, dir) catch {};
+ // Servers connect concurrently (and sessions run side by side): without a
+ // lock two read-modify-writes race and the later one drops the other's
+ // entry, so that server looks uncached and starts eagerly next time.
+ const lock_path = std.fmt.allocPrint(arena, "{s}.lock", .{path}) catch return;
+ const lock = Io.Dir.cwd().createFile(io, lock_path, .{ .truncate = false, .lock = .exclusive }) catch return;
+ defer lock.close(io);
var root_map: std.json.ObjectMap = .empty;
if (readFile(io, arena, home)) |data| {
@@ -184,9 +193,6 @@ pub fn store(
var aw: Io.Writer.Allocating = .init(arena);
var s: std.json.Stringify = .{ .writer = &aw.writer };
s.write(Value{ .object = root_map }) catch return;
- const path = cachePath(arena, home);
- if (path.len == 0) return;
- if (std.fs.path.dirname(path)) |dir| Io.Dir.cwd().createDirPath(io, dir) catch {};
credential_store.replaceFile(io, Io.Dir.cwd(), path, aw.writer.buffered(), .default_file) catch {};
}
@@ -268,6 +274,34 @@ test "loadEra survives tools TTL so Auto can skip the modern probe" {
try std.testing.expectEqual(mcp_rpc.Era.legacy, loadEra(io, a, home, key));
}
+test "concurrent stores keep every server's entry" {
+ const io = std.testing.io;
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+ const a = arena_state.allocator();
+ const home = try std.fmt.allocPrint(a, ".zig-cache/tmp/{s}", .{&tmp.sub_path});
+ const Worker = struct {
+ fn run(h: []const u8, i: usize) void {
+ var ta = std.heap.ArenaAllocator.init(std.heap.page_allocator);
+ defer ta.deinit();
+ const wa = ta.allocator();
+ var result: std.json.ObjectMap = .empty;
+ result.put(wa, "tools", .{ .array = std.json.Array.init(wa) }) catch return;
+ const key = std.fmt.allocPrint(wa, "cmd:server {d}", .{i}) catch return;
+ store(std.testing.io, wa, h, key, .legacy, "2025-11-25", .{ .object = result }, 1_000);
+ }
+ };
+ var threads: [8]std.Thread = undefined;
+ for (&threads, 0..) |*t, i| t.* = try std.Thread.spawn(.{}, Worker.run, .{ home, i });
+ for (threads) |t| t.join();
+ for (0..threads.len) |i| {
+ const key = try std.fmt.allocPrint(a, "cmd:server {d}", .{i});
+ try std.testing.expect(lookupAnyAge(io, a, home, key) != null);
+ }
+}
+
test "HTTP cache hit skips handshake; stdio legacy still initializes" {
try std.testing.expect(!handshakeOnCacheHit(.modern, true));
try std.testing.expect(!handshakeOnCacheHit(.modern, false));
diff --git a/src/mcp_share.zig b/src/mcp_share.zig
index 8289e7a0..baca324e 100644
--- a/src/mcp_share.zig
+++ b/src/mcp_share.zig
@@ -97,12 +97,26 @@ pub fn socketPath(a: Allocator, home: []const u8, key: []const u8) ![]const u8 {
fn privateDir(a: Allocator, dir: []const u8, uid: std.c.uid_t) !void {
const z = try a.dupeSentinel(u8, dir, 0);
_ = std.c.mkdir(z, 0o700);
- var st: std.c.Stat = undefined;
- if (std.c.fstatat(std.c.AT.FDCWD, z, &st, std.c.AT.SYMLINK_NOFOLLOW) != 0) return error.SharedMcpDirUnavailable;
+ const st = try lstatOwner(z);
const is_dir = (st.mode & std.c.S.IFMT) == std.c.S.IFDIR;
if (!is_dir or st.uid != uid or (st.mode & 0o077) != 0) return error.SharedMcpDirNotPrivate;
}
+/// Mode and owner of `z` itself, not a symlink's target. Linux libc has no
+/// `fstatat` binding here, so it goes through `statx`.
+fn lstatOwner(z: [*:0]const u8) !struct { mode: u32, uid: std.c.uid_t } {
+ if (builtin.os.tag == .linux) {
+ const linux = std.os.linux;
+ var sx: linux.Statx = undefined;
+ const rc = linux.statx(linux.AT.FDCWD, z, linux.AT.SYMLINK_NOFOLLOW, .{ .TYPE = true, .MODE = true, .UID = true }, &sx);
+ if (linux.errno(rc) != .SUCCESS) return error.SharedMcpDirUnavailable;
+ return .{ .mode = sx.mode, .uid = sx.uid };
+ }
+ var st: std.c.Stat = undefined;
+ if (std.c.fstatat(std.c.AT.FDCWD, z, &st, std.c.AT.SYMLINK_NOFOLLOW) != 0) return error.SharedMcpDirUnavailable;
+ return .{ .mode = st.mode, .uid = st.uid };
+}
+
// ── JSON-RPC id plumbing (pure; unit-tested) ──────────────────────────────
pub const Kind = enum { request, notification, response, server_request, other };
@@ -470,10 +484,8 @@ test "socketPath fits the socket limit for any HOME, in a private directory" {
try std.testing.expect(p1.len < 104); // sockaddr_un sun_path on macOS
const p2 = try socketPath(a, "/home/other", "0123456789abcdef");
try std.testing.expect(!std.mem.eql(u8, p1, p2)); // two homes never share a broker
- var st: std.c.Stat = undefined;
- const dir = try a.dupeSentinel(u8, std.fs.path.dirname(p1).?, 0);
- try std.testing.expectEqual(@as(c_int, 0), std.c.fstatat(std.c.AT.FDCWD, dir, &st, std.c.AT.SYMLINK_NOFOLLOW));
- try std.testing.expectEqual(@as(@TypeOf(st.mode), 0), st.mode & 0o077);
+ const st = try lstatOwner(try a.dupeSentinel(u8, std.fs.path.dirname(p1).?, 0));
+ try std.testing.expectEqual(@as(u32, 0), st.mode & 0o077);
}
test "wantsShared: only an explicit true on a stdio entry" {