graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
fix(mcp): stop filing WriteFailed as a codedb-pro bug
justrachdc84323545f2
diff --git a/src/codedbpro_paths.zig b/src/codedbpro_paths.zigindex 7ad6f32b..8f1b92b7 100644--- a/src/codedbpro_paths.zig+++ b/src/codedbpro_paths.zig@@ -36,9 +36,19 @@ pub fn callerError(text: []const u8) bool {"path not found","no content provided","missing content",+ "missing 'hash'","is a directory","permission denied","path too long",+ // Transport / client-cap names from `reg.call catch |err| @errorName(err)`.+ // Dead daemon (binary replaced mid-session) or a line past graff's+ // 1 MiB stdio reader — not a CodeDB Pro tool bug (#527, #528, #552).+ "WriteFailed",+ "StreamTooLong",+ "McpClosed",+ "McpResponseTooLarge",+ "McpHandshakeTimeout",+ "BrokenPipe",}) |needle| {if (std.mem.indexOf(u8, text, needle) != null) return true;}@@ -97,6 +107,9 @@ test "prepareInput resolves CodeDB Pro paths against the agent cwd" {test "caller errors are not filed as CodeDB Pro defects" {try std.testing.expect(callerError("{\"ok\":false,\"error\":\"path not found\"}"));try std.testing.expect(callerError("{\"ok\":false,\"error\":\"no content provided\"}"));- try std.testing.expect(!callerError("StreamTooLong"));+ try std.testing.expect(callerError("{\"ok\":false,\"error\":\"memo get: missing 'hash'\"}"));+ try std.testing.expect(callerError("WriteFailed"));+ try std.testing.expect(callerError("McpResponseTooLarge"));+ try std.testing.expect(callerError("McpClosed"));try std.testing.expect(!callerError("lint: failed to spawn eslint"));}diff --git a/src/mcp_rpc.zig b/src/mcp_rpc.zigindex 48ca35a5..ed5dc316 100644--- a/src/mcp_rpc.zig+++ b/src/mcp_rpc.zig@@ -110,10 +110,7 @@ fn stdioRequestBounded(server: *Server, a: Allocator, io: Io, params: []const u8server.next_id += 1;{const stdio = &server.transport.stdio;- const w = &stdio.stdin_writer.interface;- try w.writeAll(try mcp_protocol.buildRequest(a, id, method, params, false));- try w.writeByte('\n');- try w.flush();+ try mcp_stdio.writeRequest(&stdio.stdin_writer.interface, try mcp_protocol.buildRequest(a, id, method, params, false));}var done_buf: [2]StdioProbeDone = undefined;var select: Io.Select(StdioProbeDone) = .init(io, &done_buf);@@ -183,14 +180,11 @@ pub fn request(server: *Server, response_alloc: Allocator, params: []const u8, mswitch (server.transport) {.stdio => |*stdio| {- const w = &stdio.stdin_writer.interface;- try w.writeAll(body);- try w.writeByte('\n');- try w.flush();+ try mcp_stdio.writeRequest(&stdio.stdin_writer.interface, body);const r = &stdio.stdout_reader.interface;while (true) {- const line = (try r.takeDelimiter('\n')) orelse return error.McpClosed;+ const line = try mcp_stdio.takeLine(r);if (mcp_http.matchingResponse(response_alloc, line, id)) |parsed| return parsed;}},@@ -321,7 +315,7 @@ fn stdioProbeReadTask(server: *Server, response_alloc: Allocator, id: i64) anyerconst stdio = &server.transport.stdio;const r = &stdio.stdout_reader.interface;while (true) {- const line = (try r.takeDelimiter('\n')) orelse return error.McpClosed;+ const line = try mcp_stdio.takeLine(r);if (mcp_http.matchingResponse(response_alloc, line, id)) |parsed| return parsed;}}@@ -567,7 +561,7 @@ test "connectStdio (#275): a silent stdio server times out instead of hanging sttest "a late reply for a stale id cannot be mistaken for the id actually awaited" {// The exact loop body `request`'s stdio branch and `stdioProbeReadTask`- // both use: takeDelimiter, then filter by id via+ // both use: mcp_stdio.takeLine, then filter by id via// mcp_http.matchingResponse, skipping any line whose id doesn't match.// Proves that if a `server/discover` probe times out but its reply// arrives later, it cannot be mistaken for the `initialize` response@@ -583,7 +577,10 @@ test "a late reply for a stale id cannot be mistaken for the id actually awaited);var found: ?Value = null;while (true) {- const line = (try reader.takeDelimiter('\n')) orelse break;+ const line = mcp_stdio.takeLine(&reader) catch |err| switch (err) {+ error.McpClosed => break,+ else => return err,+ };if (mcp_http.matchingResponse(a, line, 2)) |parsed| {found = parsed;break;diff --git a/src/mcp_stdio.zig b/src/mcp_stdio.zigindex 7f91193f..e4b21fc2 100644--- a/src/mcp_stdio.zig+++ b/src/mcp_stdio.zig@@ -1,9 +1,35 @@-//! MCP stdio child-process shutdown.+//! MCP stdio child-process shutdown and newline-delimited request/response.const std = @import("std");const builtin = @import("builtin");const Io = std.Io;+/// One newline-delimited JSON-RPC line. A payload that fills graff's 1 MiB+/// stdio reader used to surface as the opaque `WriteFailed` that auto-filed+/// #527/#528/#552; map the cap to a diagnosable error instead.+pub fn takeLine(r: *Io.Reader) ![]u8 {+ const line = r.takeDelimiter('\n') catch |err| switch (err) {+ error.StreamTooLong => return error.McpResponseTooLarge,+ else => return err,+ };+ return line orelse error.McpClosed;+}++/// Write one newline-delimited request. A dead child (binary replaced+/// mid-session) used to fail the write as `WriteFailed`.+pub fn writeRequest(w: *Io.Writer, body: []const u8) !void {+ w.writeAll(body) catch |err| return mapClosed(err);+ w.writeByte('\n') catch |err| return mapClosed(err);+ w.flush() catch |err| return mapClosed(err);+}++fn mapClosed(err: anyerror) anyerror {+ return switch (err) {+ error.WriteFailed => error.McpClosed,+ else => err,+ };+}+const shutdown_grace = std.Io.Duration.fromMilliseconds(100);fn waitChild(child: *std.process.Child, io: Io) std.process.Child.WaitError!std.process.Child.Term {@@ -54,3 +80,9 @@ pub fn stopChild(io: Io, child: *std.process.Child) void {},}}++test "takeLine: one JSON-RPC line" {+ var reader: Io.Reader = .fixed("{\"ok\":true}\nnext\n");+ const line = try takeLine(&reader);+ try std.testing.expectEqualStrings("{\"ok\":true}", line);+}