Skip to content

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

Python

Preserve oversized tool results before wire normalization

justrach849d26fb53fd
diff --git a/src/messages.zig b/src/messages.zig
index 7b0cdbcc..f8a5d0aa 100644
--- a/src/messages.zig
+++ b/src/messages.zig
@@ -2,8 +2,7 @@
//! user/assistant/tool_result messages in each provider's shape, scrub invalid
//! UTF-8 so content never serializes as a byte-int array, and coerce resumed
//! Responses/OpenAI tool outputs back to strings (#95/#99). Split out of
-//! main.zig (600-line goal). Back-imports main for Provider (.Kind) and
-//! utf8Prefix (used by the Responses output cap).
+//! main.zig (600-line goal). Oversized outputs keep recoverable handles.
const std = @import("std");
const Io = std.Io;
@@ -12,9 +11,7 @@ const Allocator = std.mem.Allocator;
const root = @import("main.zig");
const provider_mod = @import("provider.zig");
-const util = @import("util.zig");
const Provider = provider_mod.Provider;
-const utf8Prefix = util.utf8Prefix;
pub fn textMessage(arena: Allocator, role: []const u8, text: []const u8) !Value {
var msg: std.json.ObjectMap = .empty;
@@ -237,10 +234,7 @@ pub fn normalizeResponsesHistory(arena: Allocator, messages: *std.json.Array) vo
const out = m.object.get("output") orelse continue;
if (out == .string and out.string.len <= responses_output_cap) continue; // valid + within cap
const s = if (out == .string) out.string else jsonValueString(arena, out);
- const capped = if (s.len > responses_output_cap)
- (std.fmt.allocPrint(arena, "{s}\n[truncated: the Responses API caps tool output at {d} chars — read/fetch a smaller range]", .{ utf8Prefix(s, responses_output_cap - 112), responses_output_cap }) catch utf8Prefix(s, responses_output_cap))
- else
- s;
+ const capped = @import("tool_spill.zig").capWithHandle(arena, s, responses_output_cap);
m.object.put(arena, "output", .{ .string = capped }) catch {};
}
}
diff --git a/src/tool_preview_tests.zig b/src/tool_preview_tests.zig
index d9f1f2df..b03a5f3c 100644
--- a/src/tool_preview_tests.zig
+++ b/src/tool_preview_tests.zig
@@ -22,6 +22,89 @@ fn render(a: std.mem.Allocator, tmp: *std.testing.TmpDir, text: []const u8) ![]c
return got.text;
}
+test "normalizeResponsesHistory spills full oversized results once before the wire cap" {
+ const messages = @import("messages.zig");
+ const spill = @import("tool_spill.zig");
+ const reader = @import("result_read.zig");
+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena.deinit();
+ const a = arena.allocator();
+ var tmp = std.testing.tmpDir(.{ .iterate = true });
+ defer tmp.cleanup();
+ handle.resetForTest();
+ defer handle.resetForTest();
+ spill.resetForTest();
+ defer spill.resetForTest();
+ spill.enable(.{ .io = std.testing.io, .dir = tmp.dir, .base_abs = "" });
+ var history = std.json.Array.init(a);
+ const needle = "RECOVERABLE_MIDDLE_1526";
+ const footer = "\nTests: 19 passed, 1 failed\n[exit code 1]\n";
+ // The smaller result passed tool-time handling's 64 KiB threshold, but
+ // still hit the old 16 KiB wire cut. Also cover much larger legacy text.
+ for ([_]usize{ 32 * 1024, 260 * 1024 }, 0..) |size, i| {
+ const full = try a.alloc(u8, size);
+ @memset(full, 'x');
+ @memcpy(full[size / 2 ..][0..needle.len], needle);
+ @memcpy(full[size - footer.len ..], footer);
+ try history.append(try messages.toolResultMessage(a, .responses, "call", full, false));
+ messages.normalizeResponsesHistory(a, &history);
+ const stub = history.items[i].object.get("output").?.string;
+ try std.testing.expect(stub.len <= 16384);
+ try std.testing.expect(std.unicode.utf8ValidateSlice(stub));
+ try std.testing.expect(std.mem.indexOf(u8, stub, needle) == null);
+ try std.testing.expect(std.mem.indexOf(u8, stub, footer) != null);
+ try std.testing.expectEqualStrings("call", history.items[i].object.get("call_id").?.string);
+ const id = try std.fmt.allocPrint(a, "tr_{d}", .{i});
+ try std.testing.expect(std.mem.indexOf(u8, stub, id) != null);
+ const rel = try reader.resolveRel(a, id);
+ const stored = try tmp.dir.readFileAlloc(std.testing.io, rel, a, .limited(1 << 20));
+ try std.testing.expectEqualStrings(full, stored);
+ messages.normalizeResponsesHistory(a, &history);
+ try std.testing.expectEqualStrings(stub, history.items[i].object.get("output").?.string);
+ }
+ var dir = try tmp.dir.openDir(std.testing.io, handle.handles_dir, .{ .iterate = true });
+ defer dir.close(std.testing.io);
+ var it = dir.iterate();
+ var count: usize = 0;
+ while (try it.next(std.testing.io)) |_| count += 1;
+ try std.testing.expectEqual(@as(usize, 2), count);
+}
+
+test "normalizeResponsesHistory reports unavailable storage without inventing a handle" {
+ const messages = @import("messages.zig");
+ const spill = @import("tool_spill.zig");
+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena.deinit();
+ const a = arena.allocator();
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ handle.resetForTest();
+ defer handle.resetForTest();
+ const saved_budget = handle.run_cap_bytes;
+ defer handle.run_cap_bytes = saved_budget;
+ spill.resetForTest();
+ defer spill.resetForTest();
+ const full = try a.alloc(u8, 32768);
+ @memset(full, 'x');
+ for ([_]bool{ false, true }) |wired| {
+ if (wired) {
+ spill.enable(.{ .io = std.testing.io, .dir = tmp.dir, .base_abs = "" });
+ handle.run_cap_bytes = 1;
+ }
+ var history = std.json.Array.init(a);
+ try history.append(try messages.toolResultMessage(a, .responses, "call", full, false));
+ messages.normalizeResponsesHistory(a, &history);
+ const stub = history.items[0].object.get("output").?.string;
+ try std.testing.expect(stub.len <= 16384);
+ try std.testing.expect(std.mem.indexOf(u8, stub, "truncated") != null);
+ try std.testing.expect(std.mem.indexOf(u8, stub, "handle could be written") != null);
+ try std.testing.expect(std.mem.indexOf(u8, stub, "handle tr_") == null);
+ messages.normalizeResponsesHistory(a, &history);
+ try std.testing.expectEqualStrings(stub, history.items[0].object.get("output").?.string);
+ }
+ try std.testing.expectError(error.FileNotFound, tmp.dir.statFile(std.testing.io, handle.handles_dir ++ "/tr_0.txt", .{}));
+}
+
test "spill preview retains first omitted error using original source offsets" {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
diff --git a/src/tool_spill.zig b/src/tool_spill.zig
index e36ac616..792eaaed 100644
--- a/src/tool_spill.zig
+++ b/src/tool_spill.zig
@@ -131,6 +131,25 @@ pub const Note = struct {
}
};
+/// Wire admission can have a tighter limit than tool-time handling (#1526).
+/// Use the already-wired workspace sink to preserve the original as a regular
+/// read_tool_result handle, including for results without a durable session.
+/// The bounded replacement makes repeated normalization a no-op.
+pub fn capWithHandle(arena: Allocator, full: []const u8, cap: usize) []const u8 {
+ if (full.len <= cap) return full;
+ if (g_sink) |sink| {
+ const handled = @import("tool_handle.zig").forResult(arena, arena, .{
+ .io = sink.io,
+ .dir = sink.dir,
+ }, full, cap) catch null;
+ if (handled) |result| return result.text;
+ }
+ const marker = "[tool result truncated: no recoverable handle could be written; narrow the result and retry]";
+ if (marker.len + 1 >= cap) return util.utf8Prefix(marker, cap);
+ const body = output_elide.headTail(arena, full, cap - marker.len - 1) catch "";
+ return std.fmt.allocPrint(arena, "{s}\n{s}", .{ body, marker }) catch marker;
+}
+
/// Write `full` to this session's artifact dir; the ABSOLUTE path on success.
/// Null — i.e. plain truncation — when no durable session is wired, when the
/// per-session budget is spent, or when any part of the write fails. Public