graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
Keep head and tail of over-cap tool output (#1271)
Rach Pradhan0da3c164c4a4
diff --git a/src/exec_bash.zig b/src/exec_bash.zigindex ca184db7..3832104c 100644--- a/src/exec_bash.zig+++ b/src/exec_bash.zig@@ -268,9 +268,10 @@ fn formatCapped(gpa: Allocator, cmd: []const u8, run: jobs.CappedRun) !ToolOutpuerrdefer aw.deinit();const w = &aw.writer;if (run.stdout.len > 0) try w.writeAll(run.stdout);- if (run.stdout_truncated) try w.print("\n[stdout truncated at {d} KB]", .{bash_stdout_cap / 1024});+ // #1271: the capture kept head and tail; say the MIDDLE went, not the end.+ if (run.stdout_truncated) try w.print("\n[stdout over the {d} KB cap: head and tail kept, middle truncated]", .{bash_stdout_cap / 1024});if (run.stderr.len > 0) try w.print("\n[stderr]\n{s}", .{run.stderr});- if (run.stderr_truncated) try w.print("\n[stderr truncated at {d} KB]", .{bash_stderr_cap / 1024});+ if (run.stderr_truncated) try w.print("\n[stderr over the {d} KB cap: head and tail kept, middle truncated]", .{bash_stderr_cap / 1024});if (run.cancelled) {try w.writeAll("\n[cancelled by user; local process group killed]\n");try w.writeAll(cancel_hint);@@ -383,6 +384,7 @@ fn execUnchecked(ctx: ToolCtx, call: tools.ToolCall) !ToolOutput {var opts = jobs.toolRunOptions(ctx.agent_cwd);var live = exec_bash_stream.Ctx{ .io = io };exec_bash_stream.attach(&opts, false, &live); // subagents stay quiet (#93)+ opts.keep_tail = true; // #1271: the ending is where failures landconst run = try jobs.runCappedWithOptions(gpa, io, &sh, bash_stdout_cap, bash_stderr_cap, subagent_deadline_ms, opts);defer gpa.free(run.stdout);defer gpa.free(run.stderr);diff --git a/src/output_elide.zig b/src/output_elide.zignew file mode 100644index 00000000..c2e8e4a9--- /dev/null+++ b/src/output_elide.zig@@ -0,0 +1,162 @@+//! #1271: over-cap output keeps its head AND its tail. Test summaries, final+//! errors and exit reasons sit at the END of a build or test run, so a cap that+//! kept only the first bytes threw away exactly the part the model needed — and+//! a subagent has no spill file to go back to.+//!+//! Split policy: the head gets a third of the budget and the tail the other two+//! thirds. The head still shows which command ran and how it started; the tail+//! is biased larger because that is where failures and summaries land.+//!+//! Shared by the capped subprocess capture (process_runner.zig, bounded by+//! `TailRing` so an overflowing child never costs more than its cap) and the+//! per-result cap (tool_spill.truncateStrField). The gap wording follows+//! handle_preview's `[... N bytes omitted ...]` excerpt marker.++const std = @import("std");+const Allocator = std.mem.Allocator;+const utf8Prefix = @import("util.zig").utf8Prefix;++pub const gap_fmt = "[... {d} bytes truncated ...]";++/// Worst case for "\n" ++ gap ++ "\n" with a 20-digit count.+pub const gap_reserve: usize = 2 + gap_fmt.len - 3 + 20;++/// Below this, a head/tail split leaves too little of either to be useful and+/// callers keep the old plain prefix.+pub const min_budget: usize = 128;++/// Bytes of a `budget` (already net of the gap) that go to the head.+pub fn headShare(budget: usize) usize {+ return budget / 3;+}++/// The last <= `max` bytes of `s`, never starting inside a UTF-8 sequence.+pub fn utf8Suffix(s: []const u8, max: usize) []const u8 {+ if (s.len <= max) return s;+ return dropLeadingContinuation(s[s.len - max ..]);+}++/// `t` without the (at most 3) continuation bytes a cut left at its start.+pub fn dropLeadingContinuation(t: []const u8) []const u8 {+ var out = t;+ var skips: usize = 0;+ while (skips < 3 and out.len > 0 and (out[0] & 0xC0) == 0x80) : (skips += 1) out = out[1..];+ return out;+}++/// `head ++ "\n" ++ gap ++ "\n" ++ tail`, where `dropped` is what `full`+/// loses between the two. Never longer than head + tail + gap_reserve.+pub fn join(gpa: Allocator, head: []const u8, tail: []const u8, dropped: usize) ![]u8 {+ return std.fmt.allocPrint(gpa, "{s}\n" ++ gap_fmt ++ "\n{s}", .{ head, dropped, tail });+}++/// `s` cut to at most `budget` bytes, keeping head and tail around a gap+/// marker. Returns `s` itself when it already fits, and a plain UTF-8 prefix+/// when `budget` is too small for a useful split. Only the split is allocated,+/// so pass an arena (tool_spill does).+pub fn headTail(gpa: Allocator, s: []const u8, budget: usize) ![]const u8 {+ if (s.len <= budget) return s;+ if (budget < min_budget) return utf8Prefix(s, budget);+ const avail = budget - gap_reserve;+ const head = utf8Prefix(s, headShare(avail));+ const tail = utf8Suffix(s, avail - head.len);+ return join(gpa, head, tail, s.len - head.len - tail.len);+}++/// The capture's version of `join`: `head`, the gap, and what `ring` retained+/// of the `after_head` bytes that arrived once the head was full.+pub fn joinRing(gpa: Allocator, head: []const u8, ring: *const TailRing, after_head: usize) ![]u8 {+ const tmp = try gpa.alloc(u8, ring.len);+ defer gpa.free(tmp);+ const tail = dropLeadingContinuation(ring.copyTo(tmp));+ return join(gpa, head, tail, after_head - tail.len);+}++/// Fixed-size ring holding the most recent `buf.len` bytes pushed through it.+/// Memory is allocated once up front; pushing never grows it.+pub const TailRing = struct {+ buf: []u8,+ start: usize = 0,+ len: usize = 0,++ pub fn init(gpa: Allocator, size: usize) !TailRing {+ return .{ .buf = try gpa.alloc(u8, size) };+ }++ pub fn deinit(self: *TailRing, gpa: Allocator) void {+ gpa.free(self.buf);+ self.* = undefined;+ }++ pub fn push(self: *TailRing, bytes: []const u8) void {+ const n = self.buf.len;+ if (n == 0) return;+ if (bytes.len >= n) {+ @memcpy(self.buf, bytes[bytes.len - n ..]);+ self.start = 0;+ self.len = n;+ return;+ }+ const end = (self.start + self.len) % n;+ const first = @min(bytes.len, n - end);+ @memcpy(self.buf[end..][0..first], bytes[0..first]);+ @memcpy(self.buf[0 .. bytes.len - first], bytes[first..]);+ const total = self.len + bytes.len;+ if (total > n) {+ self.start = (self.start + total - n) % n;+ self.len = n;+ } else self.len = total;+ }++ /// The retained bytes in order, copied into `out` (at least `len` long).+ pub fn copyTo(self: TailRing, out: []u8) []u8 {+ const n = self.buf.len;+ const first = @min(self.len, n - self.start);+ @memcpy(out[0..first], self.buf[self.start..][0..first]);+ @memcpy(out[first..self.len], self.buf[0 .. self.len - first]);+ return out[0..self.len];+ }+};++test "#1271: headTail keeps the sentinel at the end of 3 MB and stays in budget" {+ const a = std.testing.allocator;+ const big = try a.alloc(u8, 3 * 1024 * 1024);+ defer a.free(big);+ @memset(big, 'x');+ @memcpy(big[0..5], "START");+ const sentinel = "FINAL-SENTINEL-1271";+ @memcpy(big[big.len - sentinel.len ..], sentinel);+ const got = try headTail(a, big, 4096);+ defer a.free(got);+ try std.testing.expect(got.len <= 4096);+ try std.testing.expect(std.mem.startsWith(u8, got, "START"));+ try std.testing.expect(std.mem.endsWith(u8, got, sentinel));+ try std.testing.expect(std.mem.indexOf(u8, got, "bytes truncated ...]") != null);+}++test "#1271: headTail never splits a UTF-8 sequence and small budgets keep a prefix" {+ const a = std.testing.allocator;+ const s = &@import("util.zig").repeatBytes("é", 400); // 800 bytes of 2-byte sequences+ const got = try headTail(a, s, 301);+ defer a.free(got);+ try std.testing.expect(got.len <= 301);+ try std.testing.expect(std.unicode.utf8ValidateSlice(got));+ try std.testing.expectEqualStrings(&@import("util.zig").repeatBytes("é", 10), try headTail(a, s, 21)); // prefix, not allocated+ try std.testing.expectEqualStrings("é", utf8Suffix("aé", 1 + 1)); // skips nothing: 'é' is 2 bytes+ try std.testing.expectEqualStrings("", utf8Suffix("é", 1)); // lone continuation byte dropped+}++test "#1271: TailRing keeps only the most recent bytes across wraps" {+ const a = std.testing.allocator;+ var ring = try TailRing.init(a, 8);+ defer ring.deinit(a);+ var out: [8]u8 = undefined;+ ring.push("abc");+ try std.testing.expectEqualStrings("abc", ring.copyTo(&out));+ ring.push("defgh");+ try std.testing.expectEqualStrings("abcdefgh", ring.copyTo(&out));+ ring.push("ijk");+ try std.testing.expectEqualStrings("defghijk", ring.copyTo(&out));+ ring.push("0123456789");+ try std.testing.expectEqualStrings("23456789", ring.copyTo(&out));+}diff --git a/src/process_runner.zig b/src/process_runner.zigindex bc39aee0..53d28b43 100644--- a/src/process_runner.zig+++ b/src/process_runner.zig@@ -11,6 +11,7 @@ const posix_process_groups = switch (builtin.os.tag) {else => true,};const tool_pulse = @import("tool_pulse.zig");+const output_elide = @import("output_elide.zig");const GroupId = if (posix_process_groups) std.posix.pid_t else void;const WindowsJobHandle = if (builtin.os.tag == .windows) ?std.os.windows.HANDLE else void;@@ -111,6 +112,14 @@ pub const CappedRunOptions = struct {/// Clipboard export is not a user turn. Esc would kill osascript and/// Ctrl-V would report a cancelled clipboard read (#1146).ignore_cancel: bool = false,+ /// #1271: over the cap, keep the first third and the LAST two thirds of+ /// each stream around a `[... N bytes truncated ...]` gap instead of only+ /// the first `cap` bytes. The tail lives in a fixed ring (never more than+ /// the cap), and the joined result still fits the cap. Off by default:+ /// programmatic callers parse the capture or reject a truncated one, and+ /// the bash tool is the reader that needs the ending (test summaries,+ /// final errors).+ keep_tail: bool = false,};/// #253: RLIMIT_NOFILE as this process actually observes it. main() calls@@ -275,6 +284,9 @@ pub fn runCappedWithOptions(gpa: Allocator, io: Io, argv: []const []const u8, stvar saved: [2]?[]u8 = .{ null, null };errdefer for (saved) |item| if (item) |bytes| gpa.free(bytes);var streamed: [2]usize = .{ 0, 0 };+ var rings: [2]?output_elide.TailRing = .{ null, null };+ defer for (&rings) |*ring| if (ring.*) |*r| r.deinit(gpa);+ var after_head: [2]usize = .{ 0, 0 };var esc_killed = false;var timed_out = false;@@ -290,7 +302,7 @@ pub fn runCappedWithOptions(gpa: Allocator, io: Io, argv: []const []const u8, sterror.Timeout => {},else => |other| return other,};- for (readers, caps, &saved, &streamed, [_]u8{ 0, 1 }) |reader, cap, *item, *seen, which| {+ for (readers, caps, &saved, &streamed, &rings, &after_head, [_]u8{ 0, 1 }) |reader, cap, *item, *seen, *ring, *tailed, which| {const buffered = reader.buffered();// Fresh bytes this tick: anything past the streamed mark, or any// residue at all once capping started tossing what it saved.@@ -302,7 +314,22 @@ pub fn runCappedWithOptions(gpa: Allocator, io: Io, argv: []const []const u8, stseen.* = end;}}- if (item.* == null and buffered.len > cap) item.* = try gpa.dupe(u8, buffered[0..cap]);+ if (item.* == null) {+ if (buffered.len > cap) {+ const split = options.keep_tail and cap >= output_elide.min_budget;+ const avail = cap -| output_elide.gap_reserve;+ const head_len = if (split) @import("util.zig").utf8Prefix(buffered, output_elide.headShare(avail)).len else cap;+ item.* = try gpa.dupe(u8, buffered[0..head_len]);+ if (split) {+ ring.* = try output_elide.TailRing.init(gpa, avail - head_len);+ ring.*.?.push(buffered[head_len..]);+ tailed.* = buffered.len - head_len;+ }+ }+ } else if (ring.*) |*r| {+ r.push(buffered);+ tailed.* += buffered.len;+ }if (item.* != null) reader.toss(buffered.len);}if (!eof) {@@ -334,6 +361,11 @@ pub fn runCappedWithOptions(gpa: Allocator, io: Io, argv: []const []const u8, st}break :blk term;};+ for (&saved, &rings, after_head) |*item, *ring, tailed| if (ring.*) |*r| {+ const joined = try output_elide.joinRing(gpa, item.*.?, r, tailed);+ gpa.free(item.*.?);+ item.* = joined;+ };const stdout = if (saved[0]) |bytes| bytes else try gpa.dupe(u8, readers[0].buffered());errdefer if (saved[0] == null) gpa.free(stdout);const stderr = if (saved[1]) |bytes| bytes else try gpa.dupe(u8, readers[1].buffered());@@ -498,3 +530,19 @@ test "runCapped streams stdout before the child exits" {try std.testing.expect(std.mem.indexOf(u8, r.stdout, "STREAM_MARK") != null);try std.testing.expect(std.mem.indexOf(u8, r.stdout, "DONE") != null);}++test "#1271: keep_tail keeps the end of 3 MB of output within the 1 MB cap" {+ if (builtin.os.tag == .windows or builtin.os.tag == .wasi) return error.SkipZigTest;+ const gpa = std.testing.allocator;+ const cap: usize = 1024 * 1024;+ const script = "printf START; head -c 3145728 /dev/zero | tr '\\000' x; printf FINAL-SENTINEL-1271; printf ERR-TAIL >&2";+ const r = try runCappedWithOptions(gpa, std.testing.io, &.{ "/bin/sh", "-c", script }, cap, 4096, 30_000, .{ .keep_tail = true });+ defer gpa.free(r.stdout);+ defer gpa.free(r.stderr);+ try std.testing.expect(r.stdout_truncated);+ try std.testing.expect(r.stdout.len <= cap);+ try std.testing.expect(std.mem.startsWith(u8, r.stdout, "START"));+ try std.testing.expect(std.mem.endsWith(u8, r.stdout, "FINAL-SENTINEL-1271"));+ try std.testing.expect(std.mem.indexOf(u8, r.stdout, "bytes truncated ...]") != null);+ try std.testing.expectEqualStrings("ERR-TAIL", r.stderr);+}diff --git a/src/test_hooks.zig b/src/test_hooks.zigindex a44f625e..72fc2a09 100644--- a/src/test_hooks.zig+++ b/src/test_hooks.zig@@ -394,4 +394,5 @@ test {_ = @import("idle_wake_sources.zig"); // shared idle auto-turn sources for REPL / TUI / ACP_ = @import("tool_env.zig"); // #1267/#1268: tool children's environment_ = @import("destructive_git.zig"); // #1269: destructive-git tokenizer tables+ _ = @import("output_elide.zig"); // #1271: over-cap output keeps head and tail}diff --git a/src/tool_spill.zig b/src/tool_spill.zigindex 692b1c46..bfe8ae61 100644--- a/src/tool_spill.zig+++ b/src/tool_spill.zig@@ -27,7 +27,7 @@ const Value = std.json.Value;const Allocator = std.mem.Allocator;const util = @import("util.zig");-const utf8Prefix = util.utf8Prefix;+const output_elide = @import("output_elide.zig");const session_index = @import("session_index.zig");/// Ceiling on what one session may leave on disk. Every artifact is a tool@@ -259,9 +259,12 @@ fn truncateStrField(arena: Allocator, o: *std.json.ObjectMap, key: []const u8, c// The full bytes go to an artifact first (when the session is durable), so// the marker below can point at them instead of only announcing the loss.const marker = note.text(arena, v.string, cap);- // Keep the prefix short enough that prefix + '\n' + marker <= cap, so the- // marker never grows an output that was only barely over the cap.- const stub = std.fmt.allocPrint(arena, "{s}\n{s}", .{ utf8Prefix(v.string, cap -| (marker.len + 1)), marker }) catch return 0;+ // Keep the body short enough that body + '\n' + marker <= cap, so the+ // marker never grows an output that was only barely over the cap. #1271:+ // the body is head AND tail around a gap (output_elide.zig), because the+ // ending of a test run or build is usually what the model needs.+ const body = output_elide.headTail(arena, v.string, cap -| (marker.len + 1)) catch return 0;+ const stub = std.fmt.allocPrint(arena, "{s}\n{s}", .{ body, marker }) catch return 0;o.put(arena, key, .{ .string = stub }) catch return 0;return orig -| stub.len;}@@ -323,6 +326,28 @@ test "no durable session (#409): the cap stays a plain truncation" {try std.testing.expectEqualStrings("[truncated]", note.text(a, &util.repeatBytes("x", 4096), 1024));}+test "#1271: a 3 MB tool output ending in a sentinel keeps the sentinel under the cap" {+ resetForTest();+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const a = arena_state.allocator();+ const sentinel = "FINAL-SENTINEL-1271";+ const big = try a.alloc(u8, 3 * 1024 * 1024);+ @memset(big, 'x');+ @memcpy(big[big.len - sentinel.len ..], sentinel);+ var tool: std.json.ObjectMap = .empty;+ try tool.put(a, "role", .{ .string = "tool" });+ try tool.put(a, "content", .{ .string = big });+ var m: Value = .{ .object = tool };+ const cap: usize = 16 * 1024;+ try std.testing.expect(truncateToolOutput(a, &m, cap, .{ .fallback = "[truncated]" }) > 0);+ const stub = m.object.get("content").?.string;+ try std.testing.expect(stub.len <= cap);+ try std.testing.expect(std.mem.indexOf(u8, stub, sentinel) != null);+ try std.testing.expect(std.mem.indexOf(u8, stub, "bytes truncated ...]") != null);+ try std.testing.expect(std.mem.endsWith(u8, stub, "[truncated]"));+}+test "spill writes the full output and the marker points at it (#409)" {const io = std.testing.io;var tmp = std.testing.tmpDir(.{ .iterate = true });@@ -335,7 +360,7 @@ test "spill writes the full output and the marker points at it (#409)" {enable(.{ .io = io, .dir = tmp.dir, .base_abs = "/work" });const cap: usize = 1024;- // A needle past the cap: only the artifact can still hold it. It has to be+ // A needle in the elided middle: only the artifact can still hold it. It has to be// a STRING, not a bare 'N' — the marker embeds the artifact's real absolute// path, and std.testing.tmpDir names its directory with 16 random// base64-url characters. One of those is 'N' about 22% of the time, so a@@ -344,7 +369,7 @@ test "spill writes the full output and the marker points at it (#409)" {const needle = "NEEDLE409";const big = try a.alloc(u8, 8192);@memset(big, 'x');- @memcpy(big[8000..][0..needle.len], needle);+ @memcpy(big[4000..][0..needle.len], needle); // mid-output: head and tail are kept (#1271)var fco: std.json.ObjectMap = .empty;try fco.put(a, "type", .{ .string = "function_call_output" });