graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
fix(mcp): size the probe deadline for a child's cold start, not a pipe write (#327)
justrach0eb7b3c48108
diff --git a/scripts/test-mcp-stdio-probe.py b/scripts/test-mcp-stdio-probe.pyindex b47a9009..d2044ed5 100644--- a/scripts/test-mcp-stdio-probe.py+++ b/scripts/test-mcp-stdio-probe.py@@ -2,7 +2,7 @@"""Offline end-to-end proof for graff's GRAFF_MCP_PROBE-gated stdio`server/discover` probe. Two scenarios:-1. Gate off (default): a stdio server never sees anything but the exact+1. Gate off (GRAFF_MCP_PROBE=0): a stdio server never sees anything but the exactpre-migration sequence (initialize first, no server/discover line everwritten) — proves the default behavior is untouched.2. Gate on, against a server that silently drops `server/discover` (the@@ -166,10 +166,10 @@ def run_graff(graff: Path, fixture: Path, probe: bool) -> subprocess.CompletedPr"GRAFF_NO_TELEMETRY": "1",})- if probe:- env["GRAFF_MCP_PROBE"] = "1"- else:- env.pop("GRAFF_MCP_PROBE", None)+ # The probe is ON unless GRAFF_MCP_PROBE=0, so "gate off" has to say+ # so explicitly — merely unsetting the variable ran scenario 1 with the+ # probe enabled and asserted nothing it claimed to.+ env["GRAFF_MCP_PROBE"] = "1" if probe else "0"return subprocess.run([str(graff), "--json", "--yolo", "--model", "claude"],cwd=workspace,@@ -178,7 +178,7 @@ def run_graff(graff: Path, fixture: Path, probe: bool) -> subprocess.CompletedPrtext=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE,- timeout=20,+ timeout=40, # room for the 5s probe bound plus a loaded CI boxcheck=False,)diff --git a/src/mcp_rpc.zig b/src/mcp_rpc.zigindex d5ff73c9..e6304665 100644--- a/src/mcp_rpc.zig+++ b/src/mcp_rpc.zig@@ -81,16 +81,13 @@ pub fn applyHandshakeTimeoutEnv(environ_map: anytype) void {if (secs > 0) stdio_handshake_timeout_ms = @intCast(@min(secs, 86_400) * 1000);}-/// GRAFF_MCP_PROBE_MS raises/lowers `stdio_probe_timeout_ms` (default 600).-/// The escape hatch for #327: a server that is slow to its FIRST answer gets-/// classified legacy for the session even though it speaks the modern-/// protocol. Measured, not hypothetical — a second concurrent codedb-pro-/// process takes ~1.5s to answer its first request where the first takes 2ms,-/// which is why the companion lands on legacy exactly when a workspace stdio-/// server connected before it. The default stays low because a genuinely-/// legacy server pays the whole bound before falling back; raise it when the-/// connect line reports a probe-deadline fallback. Milliseconds; ignored if-/// unparseable or 0; clamped to <=60s.+/// GRAFF_MCP_PROBE_MS overrides `stdio_probe_timeout_ms` (default 5000, see+/// there for how that number is derived). Raise it for a server slower to its+/// first answer than the default allows — the connect line names the+/// probe-deadline fallback when that happens — or lower it to skip the+/// startup wait a server that answers `server/discover` with silence rather+/// than an error costs. Milliseconds; ignored if unparseable or 0; clamped to+/// <=60s.pub fn applyProbeTimeoutEnv(environ_map: anytype) void {const v = environ_map.get("GRAFF_MCP_PROBE_MS") orelse return;const ms = std.fmt.parseInt(u64, std.mem.trim(u8, v, " \t"), 10) catch return;@@ -235,16 +232,30 @@ fn connectLegacy(server: *Server, a: Allocator, session_alloc: Allocator, bound_return handshakeRequest(server, a, bound_io, "{}", "tools/list");}-/// A stdio server is a LOCAL process, so a reply is a pipe write away: 3s was-/// a network-shaped budget and made the spec's SHOULD-probe unaffordable to-/// run by default. A server that has not answered `server/discover` in this-/// long is treated as legacy, which is exactly the behavior graff had before-/// the probe existed - so the cost of guessing wrong is latency, never-/// function (stdio spec: fall back on "no response within a reasonable+/// How long the `server/discover` probe waits before calling a stdio server+/// legacy (stdio spec: fall back on "no response within a reasonable/// timeout", never keyed to a specific error code).-/// A `var` only so a test can widen it off a loaded CI box; production never-/// writes it.-pub var stdio_probe_timeout_ms: i64 = 600;+///+/// This bound is NOT pipe latency. The probe is the first thing written to a+/// child graff spawned microseconds ago, so what it has to cover is that+/// child's COLD START — interpreter boot, module load, index warm-up — under+/// whatever load the machine already carries. 600ms was sized for the pipe+/// write and was wrong for the process behind it: a second concurrent+/// codedb-pro answers its first request in ~1.5s where the first takes 2ms,+/// and an npx-launched server routinely needs longer, so the most common real+/// setup (a workspace server plus the auto-connected companion) missed the+/// deadline on EVERY run and spent the whole session on legacy. That was #327.+///+/// The costs are asymmetric, which is what sets the value: missing the+/// deadline degrades the connection for its entire life, while overshooting+/// costs a one-time startup wait — and only for a server that answers nothing+/// at all, since one that replies with a JSON-RPC error (what every SDK-built+/// legacy server does for an unknown method) is classified the instant that+/// error lands, however large this is. 5s covers the measured slow starts with+/// room to spare and stays well under the 15s `stdio_handshake_timeout_ms`+/// graff already spends on the same child's `initialize`. `GRAFF_MCP_PROBE_MS`+/// moves it either way.+pub var stdio_probe_timeout_ms: i64 = 5_000;fn stdioProbeReadTask(server: *Server, response_alloc: Allocator, id: i64) anyerror!Value {const stdio = &server.transport.stdio;@@ -412,8 +423,8 @@ pub fn probeStdioResilient(server: *Server, a: Allocator, io: Io) !StdioProbeOut};}-/// Connect a stdio server. Always legacy — the gated `server/discover`-/// probe (`GRAFF_MCP_PROBE=1`, default off) is orchestrated by mcp.zig's+/// Connect a stdio server. Always legacy — the `server/discover` probe+/// (on unless `GRAFF_MCP_PROBE=0`) is orchestrated by mcp.zig's/// `startServer` instead of here: only it has the argv/env needed to/// respawn a server whose process closes during the probe. This is/// byte-identical to graff's pre-migration behavior either way.diff --git a/src/mcp_rpc_tests.zig b/src/mcp_rpc_tests.zigindex 24dd5417..8b5f933b 100644--- a/src/mcp_rpc_tests.zig+++ b/src/mcp_rpc_tests.zig@@ -208,3 +208,28 @@ test "probeStdioResilient (#327): retries, then downgrades only with a visible r}try std.testing.expect(std.mem.indexOf(u8, mcp_rpc.LegacyReason.no_modern_version.note(), modern_protocol) != null);}++test "probeStdio (#327): the DEFAULT deadline covers a cold-starting server" {+ if (builtin.os.tag == .windows) return error.SkipZigTest; // /bin/sh+ const io = std.testing.io;+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const a = arena_state.allocator();++ // The reported symptom itself, and the one no amount of labelling fixed:+ // the deadline has to be survivable by a child that is still booting. A+ // second concurrent codedb-pro answers its first request in ~1.5s; this+ // fixture stands in for it at 1.2s. Deliberately NO override of+ // `stdio_probe_timeout_ms` — the value under test is the shipped default,+ // which at 600ms classified this server (and so the auto-connected+ // companion, on every single run) as legacy for the whole session.+ try std.testing.expect(mcp_rpc.stdio_probe_timeout_ms >= 3_000);++ var spawned = try spawnReplying(a, io,+ \\read line; sleep 1.2; printf '%s\n' '{"jsonrpc":"2.0","id":1,"result":{"supportedVersions":["2026-07-28"]}}'; cat >/dev/null+ );+ defer mcp_stdio.stopChild(io, &spawned.child);++ try std.testing.expectEqual(mcp_rpc.StdioProbeOutcome.modern, try mcp_rpc.probeStdio(spawned.server, a, io));+ try std.testing.expect(spawned.server.probe_fallback == null);+}