graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
stdin: read ACP and --json records past the 64 KiB buffer
Rach Pradhan78ebb062a493
diff --git a/src/acp_inbox.zig b/src/acp_inbox.zigindex 5937677b..aa831466 100644--- a/src/acp_inbox.zig+++ b/src/acp_inbox.zig@@ -5,6 +5,11 @@ const Agent = @import("agent.zig").Agent;const proto = @import("acp_protocol.zig");const cancel_source = @import("cancel_source.zig");const acp_ask = @import("acp_ask.zig");+const stdin_line = @import("stdin_line.zig");++test {+ _ = stdin_line;+}pub const Inbox = struct {permission: ?*@import("acp_permission.zig").Bridge = null,@@ -151,8 +156,16 @@ pub const Inbox = struct {fn pump(self: *Inbox) void {while (true) {- const line = (self.reader.takeDelimiter('\n') catch break) orelse break;- self.accept(line) catch break;+ // Not takeDelimiter: a prompt with an attachment outgrows the stdin+ // buffer, and its StreamTooLong used to end the session here.+ switch (stdin_line.take(self.reader, self.gpa, stdin_line.max_bytes) catch break) {+ .eof => break,+ .too_long => std.debug.print("acp: dropped an input record over {d} bytes\n", .{stdin_line.max_bytes}),+ .line => |line| {+ defer self.gpa.free(line);+ self.accept(line) catch break;+ },+ }}self.mutex.lockUncancelable(self.io);self.eof = true;diff --git a/src/json_inbox.zig b/src/json_inbox.zigindex 90f40a3f..dab0396a 100644--- a/src/json_inbox.zig+++ b/src/json_inbox.zig@@ -7,6 +7,7 @@ const Allocator = std.mem.Allocator;const Value = std.json.Value;const Agent = @import("agent.zig").Agent;const cancel_source = @import("cancel_source.zig"); // #728+const stdin_line = @import("stdin_line.zig");const cancelled_answer = "{\"type\":\"answer\",\"cancelled\":true}";@@ -119,8 +120,17 @@ fn readerTask() void {const gpa = inbox_gpa.?;const reader = inbox_reader.?;while (true) {- const raw = reader.takeDelimiter('\n') catch break;- const line = raw orelse break;+ // Not takeDelimiter: a request with an image outgrows the stdin buffer,+ // and its StreamTooLong used to end the session here.+ const line = switch (stdin_line.take(reader, gpa, stdin_line.max_bytes) catch break) {+ .eof => break,+ .too_long => {+ std.debug.print("graff: dropped a --json input record over {d} bytes\n", .{stdin_line.max_bytes});+ continue;+ },+ .line => |l| l,+ };+ defer gpa.free(line);if (isCancel(gpa, line)) {mutex.lockUncancelable(io);const active = turn_active.load(.acquire);diff --git a/src/stdin_line.zig b/src/stdin_line.zignew file mode 100644index 00000000..e345239e--- /dev/null+++ b/src/stdin_line.zig@@ -0,0 +1,88 @@+//! One newline-framed record from a protocol stdin (ACP, `--json`). A record+//! is bounded by `max_bytes`, not by the reader's buffer: prompts with pasted+//! files or base64 image attachments are routinely larger than the 64 KiB+//! stdin buffer. Reader.takeDelimiter reports that as StreamTooLong, which the+//! inbox pumps treated as end of input, so the session exited silently.+const std = @import("std");+const Io = std.Io;+const Allocator = std.mem.Allocator;++pub const max_bytes: usize = 64 * 1024 * 1024;++pub const Record = union(enum) {+ /// Owned by the caller's allocator.+ line: []u8,+ /// Over the limit; consumed through its newline and dropped.+ too_long,+ eof,+};++pub fn take(reader: *Io.Reader, gpa: Allocator, limit: usize) error{ ReadFailed, OutOfMemory }!Record {+ var aw: Io.Writer.Allocating = .init(gpa);+ defer aw.deinit();+ const n = reader.streamDelimiterLimit(&aw.writer, '\n', .limited(limit)) catch |err| switch (err) {+ error.StreamTooLong => {+ _ = reader.discardDelimiterInclusive('\n') catch |e| switch (e) {+ error.EndOfStream => return .eof,+ error.ReadFailed => return error.ReadFailed,+ };+ return .too_long;+ },+ error.WriteFailed => return error.OutOfMemory,+ error.ReadFailed => return error.ReadFailed,+ };+ // streamDelimiterLimit leaves the newline in the reader; a final record+ // without one ends at EOF instead.+ _ = reader.takeByte() catch |err| switch (err) {+ error.EndOfStream => if (n == 0) return .eof,+ error.ReadFailed => return error.ReadFailed,+ };+ return .{ .line = try aw.toOwnedSlice() };+}++test "records larger than the reader buffer are returned whole" {+ const gpa = std.testing.allocator;+ const big = try gpa.alloc(u8, 200 * 1024);+ defer gpa.free(big);+ @memset(big, 'a');+ const input = try std.mem.concat(gpa, u8, &.{ "{\"id\":1}\n", big, "\n{\"id\":2}" });+ defer gpa.free(input);+ // A 64-byte buffer in front of the source, like the 64 KiB stdin buffer in+ // front of a 200 KiB+ ACP prompt.+ var src: Io.Reader = .fixed(input);+ var buf: [64]u8 = undefined;+ var ri: std.testing.ReaderIndirect = .init(&src, &buf);+ const r = &ri.interface;+ const first = try take(r, gpa, max_bytes);+ defer gpa.free(first.line);+ try std.testing.expectEqualStrings("{\"id\":1}", first.line);+ const second = try take(r, gpa, max_bytes);+ defer gpa.free(second.line);+ try std.testing.expectEqual(big.len, second.line.len);+ const third = try take(r, gpa, max_bytes);+ defer gpa.free(third.line);+ try std.testing.expectEqualStrings("{\"id\":2}", third.line);+ try std.testing.expect((try take(r, gpa, max_bytes)) == .eof);+}++test "an over-limit record is dropped and the next one still arrives" {+ const gpa = std.testing.allocator;+ var r: Io.Reader = .fixed("0123456789abcdef\n{\"ok\":true}\n");+ try std.testing.expect((try take(&r, gpa, 8)) == .too_long);+ const next = try take(&r, gpa, 8 * 1024);+ defer gpa.free(next.line);+ try std.testing.expectEqualStrings("{\"ok\":true}", next.line);+ try std.testing.expect((try take(&r, gpa, 8)) == .eof);+}++test "empty lines are records, and a trailing newline is not an extra one" {+ const gpa = std.testing.allocator;+ var r: Io.Reader = .fixed("\nx\n");+ const empty = try take(&r, gpa, 1024);+ defer gpa.free(empty.line);+ try std.testing.expectEqual(@as(usize, 0), empty.line.len);+ const x = try take(&r, gpa, 1024);+ defer gpa.free(x.line);+ try std.testing.expectEqualStrings("x", x.line);+ try std.testing.expect((try take(&r, gpa, 1024)) == .eof);+}