Skip to content

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

Python

Review a large added file as a marked diff excerpt (#1337)

justrach22d45fde9b4c
diff --git a/src/main.zig b/src/main.zig
index 448a6f04..d06c72c4 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -53,7 +53,7 @@ test {
_ = .{ @import("http2_buffered.zig"), @import("clipboard_native.zig"), @import("clipboard_edge_tests.zig"), @import("clipboard_failure_tests.zig"), @import("process_runner_clipboard_tests.zig") };
_ = @import("server_orphan.zig");
_ = @import("repo_transaction.zig");
- _ = .{ @import("pr_command.zig"), @import("pr_evidence.zig"), @import("pr_verify.zig"), @import("pr_acceptance.zig"), @import("artifact_repository.zig"), @import("artifact_claim_ledger.zig"), @import("artifact_claim_store.zig"), @import("artifact_claim_stale.zig") };
+ _ = .{ @import("pr_command.zig"), @import("pr_evidence.zig"), @import("pr_review_input_tests.zig"), @import("pr_verify.zig"), @import("pr_acceptance.zig"), @import("artifact_repository.zig"), @import("artifact_claim_ledger.zig"), @import("artifact_claim_store.zig"), @import("artifact_claim_stale.zig") };
_ = @import("argstream_citation_tests.zig"); // unit_tests' root is main.zig only, so reference every split-out module or its tests silently never run
_ = @import("codex_node_repl.zig");
_ = pricing;
diff --git a/src/pr_claim_review.zig b/src/pr_claim_review.zig
index 83fac3fd..afc17fc0 100644
--- a/src/pr_claim_review.zig
+++ b/src/pr_claim_review.zig
@@ -119,6 +119,7 @@ pub fn review(self: *Agent, target: evidence.Target, base_name: ?[]const u8, cre
\\This review does not certify execution or CI coverage beyond the supplied observations.
\\If support_omitted is true, use support_limit to identify the missing evidence and return unresolved when it is required.
\\A changed file with after_omitted=true has only a committed context diff, not complete proposed-head source.
+ \\A changed file with change_truncated=true has only the start of its diff; judge only what the excerpt shows.
\\If required behavior, callers, or tests lie outside that excerpt, return unresolved even when checks pass.
\\If required callers or tests are missing, return unresolved. If a claim exceeds supported scope,
\\return unsupported with a concrete counterexample or missing coverage. Do not accept claims
diff --git a/src/pr_review_input.zig b/src/pr_review_input.zig
index f1c6a358..6ce050c0 100644
--- a/src/pr_review_input.zig
+++ b/src/pr_review_input.zig
@@ -10,7 +10,7 @@ fn repositoryPath(arena: A, dir: []const u8, name: []const u8) ![]const u8 {
return std.mem.trimStart(u8, path, "/");
}
-pub const File = struct { path: []const u8, before: ?[]const u8, after: ?[]const u8, change: ?[]const u8 = null, after_omitted: bool = false };
+pub const File = struct { path: []const u8, before: ?[]const u8, after: ?[]const u8, change: ?[]const u8 = null, after_omitted: bool = false, change_truncated: bool = false };
pub const Input = struct {
version: u8 = 3,
base: []const u8,
@@ -51,13 +51,25 @@ pub fn diffsOnly(arena: A, input: Input) !Input {
}
pub const max_files = 32;
+/// One file's diff beyond this keeps its start only (#1337): a new capture
+/// fixture is all added lines, and must not make the review impossible.
+pub const max_file_diff = 32 * 1024;
+
+fn truncateDiff(arena: A, change: []const u8) ![]const u8 {
+ var end: usize = max_file_diff;
+ while (end > 0 and change[end - 1] != '\n') end -= 1;
+ if (end == 0) end = max_file_diff;
+ while (end > 0 and !std.unicode.utf8ValidateSlice(change[0..end])) end -= 1;
+ return std.fmt.allocPrint(arena, "{s}[diff truncated: {d} of {d} bytes shown]\n", .{ change[0..end], end, change.len });
+}
+
pub const max_bytes = 128 * 1024;
-fn capture(gpa: A, io: std.Io, arena: A, cwd: []const u8, args: []const []const u8) ![]const u8 {
+pub fn capture(gpa: A, io: std.Io, arena: A, cwd: []const u8, args: []const []const u8) ![]const u8 {
return evidence.capture(gpa, io, arena, .{ .cwd = cwd, .selector = "" }, args);
}
-fn raw(gpa: A, io: std.Io, arena: A, cwd: []const u8, args: []const []const u8) ![]const u8 {
+pub fn raw(gpa: A, io: std.Io, arena: A, cwd: []const u8, args: []const []const u8) ![]const u8 {
const runner = @import("process_runner.zig");
const result = try runner.runCappedWithOptions(gpa, io, args, max_bytes + 1, 2048, 15_000, .{ .cwd = .{ .path = cwd } });
defer gpa.free(result.stdout);
@@ -184,12 +196,18 @@ pub fn gather(gpa: A, io: std.Io, arena: A, cwd: []const u8, base: []const u8, h
if (files.items.len >= max_files) return error.ReviewTooLarge;
// A context diff carries the old lines; repeating the complete base
// blob would charge unchanged source twice and crowd out test evidence.
- const change = try raw(gpa, io, arena, cwd, &.{ "git", "diff", "--no-ext-diff", "--no-renames", "--unified=3", base, head, "--", path });
+ const full = raw(gpa, io, arena, cwd, &.{ "git", "diff", "--no-ext-diff", "--no-renames", "--unified=3", base, head, "--", path }) catch |err| blk: {
+ if (err != error.ReviewTooLarge) return err;
+ break :blk null; // over 128 KiB on its own
+ };
+ const truncated = full == null or full.?.len > max_file_diff;
+ const change = if (full) |d| (if (truncated) try truncateDiff(arena, d) else d) else "[diff truncated: this file's diff exceeds 128 KiB]\n";
if (size + change.len > max_bytes) return error.ReviewTooLarge;
size += change.len;
- try files.append(arena, .{ .path = path, .before = null, .after = null, .change = change });
+ try files.append(arena, .{ .path = path, .before = null, .after = null, .change = change, .after_omitted = truncated, .change_truncated = truncated });
}
for (files.items) |*file| {
+ if (file.change_truncated) continue; // its excerpt is all the review gets
var after_omitted = false;
const after = blob(gpa, io, arena, cwd, head, file.path) catch |err| blk: {
if (err != error.ReviewTooLarge) return err;
@@ -320,7 +338,9 @@ test "claim review budgets changed hunks instead of the complete base blob" {
_ = try capture(gpa, io, a, cwd, &.{ "git", "add", "." });
_ = try capture(gpa, io, a, cwd, &.{ "git", "-c", "user.name=Fixture", "-c", "[email protected]", "-c", "commit.gpgsign=false", "commit", "-qm", "oversized" });
const large_head = try capture(gpa, io, a, cwd, &.{ "git", "rev-parse", "HEAD" });
- try std.testing.expectError(error.ReviewTooLarge, gather(gpa, io, a, cwd, head, large_head, "claim"));
+ // #1337: a diff over the budget on its own is reviewed as a marked excerpt.
+ const excerpt = try gather(gpa, io, a, cwd, head, large_head, "claim");
+ try std.testing.expect(excerpt.files[0].change_truncated and excerpt.files[0].after == null);
}
test "claim review uses committed context when changed head source exceeds the budget" {
@@ -533,49 +553,3 @@ test "claim review includes committed nested package runners and workflow reacha
try std.testing.expect(try support.add("apps/client/package.json") == null);
try std.testing.expect(support.omitted and files.items.len == 0 and size == max_bytes);
}
-
-test "#1345 an early large file cannot crowd out a later file's diff" {
- const io = std.testing.io;
- var temp = std.testing.tmpDir(.{});
- defer temp.cleanup();
- var scratch = std.heap.ArenaAllocator.init(std.testing.allocator);
- defer scratch.deinit();
- const a = scratch.allocator();
- const gpa = std.testing.allocator;
- var path: [std.fs.max_path_bytes]u8 = undefined;
- const cwd = path[0..try temp.dir.realPath(io, &path)];
- _ = try capture(gpa, io, a, cwd, &.{ "git", "init", "-q" });
- const large = try a.alloc(u8, 100 * 1024);
- @memset(large, 'a');
- for (large, 0..) |*byte, i| if (i % 80 == 79) {
- byte.* = '\n';
- };
- large[0] = 'x';
- try temp.dir.writeFile(io, .{ .sub_path = "alpha.txt", .data = large });
- try temp.dir.writeFile(io, .{ .sub_path = "beta.txt", .data = "one\ntwo\nthree\n" });
- const commit = &[_][]const u8{ "git", "-c", "user.name=Fixture", "-c", "[email protected]", "-c", "commit.gpgsign=false", "commit", "-qam", "c" };
- _ = try capture(gpa, io, a, cwd, &.{ "git", "add", "." });
- _ = try capture(gpa, io, a, cwd, commit);
- const base = try capture(gpa, io, a, cwd, &.{ "git", "rev-parse", "HEAD" });
- large[0] = 'y';
- try temp.dir.writeFile(io, .{ .sub_path = "alpha.txt", .data = large });
- try temp.dir.writeFile(io, .{ .sub_path = "beta.txt", .data = "one\nTWO\nthree\n" });
- _ = try capture(gpa, io, a, cwd, commit);
- const head = try capture(gpa, io, a, cwd, &.{ "git", "rev-parse", "HEAD" });
- // Size the claim so alpha's complete source would fill the budget exactly
- // to within a few bytes, leaving no room for beta's small diff.
- const alpha_diff = try raw(gpa, io, a, cwd, &.{ "git", "diff", "--no-ext-diff", "--no-renames", "--unified=3", base, head, "--", "alpha.txt" });
- const body = try a.alloc(u8, max_bytes - alpha_diff.len - large.len - 8);
- @memset(body, 'c');
- const input = try gather(gpa, io, a, cwd, base, head, body);
- try std.testing.expectEqual(@as(usize, 2), input.files.len);
- try std.testing.expect(input.files[0].after == null and input.files[0].after_omitted);
- try std.testing.expect(std.mem.indexOf(u8, input.files[0].change.?, "+yaaa") != null);
- try std.testing.expectEqualStrings("one\nTWO\nthree\n", input.files[1].after.?);
- try std.testing.expect(std.mem.indexOf(u8, input.files[1].change.?, "+TWO") != null);
-
- const slim = try diffsOnly(a, input);
- try std.testing.expectEqual(@as(usize, 2), slim.files.len);
- for (slim.files) |file| try std.testing.expect(file.after == null and file.after_omitted and file.change != null);
- try std.testing.expect(slim.support_omitted);
-}
diff --git a/src/pr_review_input_tests.zig b/src/pr_review_input_tests.zig
new file mode 100644
index 00000000..95f6034e
--- /dev/null
+++ b/src/pr_review_input_tests.zig
@@ -0,0 +1,86 @@
+//! Budget regressions for pr_review_input (#1337, #1345), split out to keep
+//! that file under the line cap.
+
+const std = @import("std");
+const inputs = @import("pr_review_input.zig");
+
+test "#1345 an early large file cannot crowd out a later file's diff" {
+ const io = std.testing.io;
+ var temp = std.testing.tmpDir(.{});
+ defer temp.cleanup();
+ var scratch = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer scratch.deinit();
+ const a = scratch.allocator();
+ const gpa = std.testing.allocator;
+ var path: [std.fs.max_path_bytes]u8 = undefined;
+ const cwd = path[0..try temp.dir.realPath(io, &path)];
+ _ = try inputs.capture(gpa, io, a, cwd, &.{ "git", "init", "-q" });
+ const large = try a.alloc(u8, 100 * 1024);
+ @memset(large, 'a');
+ for (large, 0..) |*byte, i| if (i % 80 == 79) {
+ byte.* = '\n';
+ };
+ large[0] = 'x';
+ try temp.dir.writeFile(io, .{ .sub_path = "alpha.txt", .data = large });
+ try temp.dir.writeFile(io, .{ .sub_path = "beta.txt", .data = "one\ntwo\nthree\n" });
+ const commit = &[_][]const u8{ "git", "-c", "user.name=Fixture", "-c", "[email protected]", "-c", "commit.gpgsign=false", "commit", "-qam", "c" };
+ _ = try inputs.capture(gpa, io, a, cwd, &.{ "git", "add", "." });
+ _ = try inputs.capture(gpa, io, a, cwd, commit);
+ const base = try inputs.capture(gpa, io, a, cwd, &.{ "git", "rev-parse", "HEAD" });
+ large[0] = 'y';
+ try temp.dir.writeFile(io, .{ .sub_path = "alpha.txt", .data = large });
+ try temp.dir.writeFile(io, .{ .sub_path = "beta.txt", .data = "one\nTWO\nthree\n" });
+ _ = try inputs.capture(gpa, io, a, cwd, commit);
+ const head = try inputs.capture(gpa, io, a, cwd, &.{ "git", "rev-parse", "HEAD" });
+ // Size the claim so alpha's complete source would fill the budget exactly
+ // to within a few bytes, leaving no room for beta's small diff.
+ const alpha_diff = try inputs.raw(gpa, io, a, cwd, &.{ "git", "diff", "--no-ext-diff", "--no-renames", "--unified=3", base, head, "--", "alpha.txt" });
+ const body = try a.alloc(u8, inputs.max_bytes - alpha_diff.len - large.len - 8);
+ @memset(body, 'c');
+ const input = try inputs.gather(gpa, io, a, cwd, base, head, body);
+ try std.testing.expectEqual(@as(usize, 2), input.files.len);
+ try std.testing.expect(input.files[0].after == null and input.files[0].after_omitted);
+ try std.testing.expect(std.mem.indexOf(u8, input.files[0].change.?, "+yaaa") != null);
+ try std.testing.expectEqualStrings("one\nTWO\nthree\n", input.files[1].after.?);
+ try std.testing.expect(std.mem.indexOf(u8, input.files[1].change.?, "+TWO") != null);
+
+ const slim = try inputs.diffsOnly(a, input);
+ try std.testing.expectEqual(@as(usize, 2), slim.files.len);
+ for (slim.files) |file| try std.testing.expect(file.after == null and file.after_omitted and file.change != null);
+ try std.testing.expect(slim.support_omitted);
+}
+
+test "#1337 a large added fixture is reviewed as a marked excerpt, not refused" {
+ const io = std.testing.io;
+ var temp = std.testing.tmpDir(.{});
+ defer temp.cleanup();
+ var scratch = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer scratch.deinit();
+ const a = scratch.allocator();
+ const gpa = std.testing.allocator;
+ var path: [std.fs.max_path_bytes]u8 = undefined;
+ const cwd = path[0..try temp.dir.realPath(io, &path)];
+ const commit = &[_][]const u8{ "git", "-c", "user.name=Fixture", "-c", "[email protected]", "-c", "commit.gpgsign=false", "commit", "-qm", "c" };
+ _ = try inputs.capture(gpa, io, a, cwd, &.{ "git", "init", "-q" });
+ try temp.dir.writeFile(io, .{ .sub_path = "lib.rs", .data = "fn a() {}\n" });
+ _ = try inputs.capture(gpa, io, a, cwd, &.{ "git", "add", "." });
+ _ = try inputs.capture(gpa, io, a, cwd, commit);
+ const base = try inputs.capture(gpa, io, a, cwd, &.{ "git", "rev-parse", "HEAD" });
+ const fixture = try a.alloc(u8, 90 * 1024);
+ @memset(fixture, 'f');
+ for (fixture, 0..) |*byte, i| if (i % 80 == 79) {
+ byte.* = '\n';
+ };
+ try temp.dir.writeFile(io, .{ .sub_path = "capture.txt", .data = fixture });
+ try temp.dir.writeFile(io, .{ .sub_path = "lib.rs", .data = "fn a() { b() }\n" });
+ _ = try inputs.capture(gpa, io, a, cwd, &.{ "git", "add", "." });
+ _ = try inputs.capture(gpa, io, a, cwd, commit);
+ const head = try inputs.capture(gpa, io, a, cwd, &.{ "git", "rev-parse", "HEAD" });
+ const input = try inputs.gather(gpa, io, a, cwd, base, head, "claim");
+ try std.testing.expectEqual(@as(usize, 2), input.files.len);
+ try std.testing.expectEqualStrings("capture.txt", input.files[0].path);
+ try std.testing.expect(input.files[0].change_truncated and input.files[0].after == null);
+ try std.testing.expect(input.files[0].change.?.len <= inputs.max_file_diff + 64);
+ try std.testing.expect(std.mem.indexOf(u8, input.files[0].change.?, "[diff truncated:") != null);
+ try std.testing.expectEqualStrings("fn a() { b() }\n", input.files[1].after.?);
+}