graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
Reserve changed hunks before complete sources in PR claim review (#1345)
justrachbafa8d11da91
diff --git a/src/pr_claim_review.zig b/src/pr_claim_review.zigindex 057ec3e6..83fac3fd 100644--- a/src/pr_claim_review.zig+++ b/src/pr_claim_review.zig@@ -45,6 +45,20 @@ fn repoField(self: *Agent, target: evidence.Target, field: []const u8, query: []return evidence.capture(self.gpa, self.io, self.arena, selected, args);}+const max_packet = 256 * 1024;++fn reviewPacket(arena: std.mem.Allocator, repo: []const u8, base: []const u8, input: inputs.Input, local: []const @import("pr_local_checks.zig").State.Receipt, ci: @import("pr_publish.zig").HeadStatus, pr_checks: []const u8) ![]u8 {+ return std.json.Stringify.valueAlloc(arena, .{+ .repository = repo,+ .base_tip = base,+ .committed_inputs = input,+ .observed_local_checks = local,+ .local_check_limit = if (local.len == 0) "no local check receipt was observed for this repository in the current or restored session" else "receipts are bounded observations; head_after and tracked_tree_clean_after do not prove immutable execution",+ .observed_head_ci = @tagName(ci),+ .observed_pr_checks = pr_checks,+ }, .{});+}+pub fn review(self: *Agent, target: evidence.Target, base_name: ?[]const u8, creating: bool, head: []const u8, body: []const u8, ci: @import("pr_publish.zig").HeadStatus) !Result {const repo = if (creating) try repoField(self, target, "url", ".url") else blk: {const pr = @import("artifact_repository.zig").pullRequest(self.arena, self.io, target) orelse return error.UnknownRepository;@@ -66,16 +80,15 @@ pub fn review(self: *Agent, target: evidence.Target, base_name: ?[]const u8, crefor (self.publication_checks.recent.items) |receipt| {if (std.mem.eql(u8, receipt.repository, repository)) try local.append(self.arena, receipt);}- const packet = try std.json.Stringify.valueAlloc(self.arena, .{- .repository = repo,- .base_tip = base,- .committed_inputs = input,- .observed_local_checks = local.items,- .local_check_limit = if (local.items.len == 0) "no local check receipt was observed for this repository in the current or restored session" else "receipts are bounded observations; head_after and tracked_tree_clean_after do not prove immutable execution",- .observed_head_ci = @tagName(ci),- .observed_pr_checks = pr_checks,- }, .{});- if (packet.len > 256 * 1024) return error.ReviewTooLarge;+ var committed = input;+ var packet = try reviewPacket(self.arena, repo, base, committed, local.items, ci, pr_checks);+ // #1345: complete sources are context, not the change. Keep the diffs and+ // mark the rest omitted before refusing a review for its size.+ if (packet.len > max_packet) {+ committed = try inputs.diffsOnly(self.arena, input);+ packet = try reviewPacket(self.arena, repo, base, committed, local.items, ci, pr_checks);+ }+ if (packet.len > max_packet) return error.ReviewTooLarge;var hash: [32]u8 = undefined;std.crypto.hash.sha2.Sha256.hash(packet, &hash, .{});const key = std.fmt.bytesToHex(hash, .lower);diff --git a/src/pr_review_input.zig b/src/pr_review_input.zigindex 1686c5bf..f1c6a358 100644--- a/src/pr_review_input.zig+++ b/src/pr_review_input.zig@@ -31,6 +31,25 @@ pub fn digest(arena: A, input: Input) ![64]u8 {return std.fmt.bytesToHex(value, .lower);}+/// The same review with every complete source dropped: changed files keep+/// only their committed diffs and supporting files are omitted (#1345). Used+/// when the full packet would exceed its transport limit.+pub fn diffsOnly(arena: A, input: Input) !Input {+ var files: std.ArrayList(File) = .empty;+ for (input.files) |file| {+ if (file.change == null) continue;+ var kept = file;+ kept.after_omitted = kept.after_omitted or kept.after != null;+ kept.after = null;+ try files.append(arena, kept);+ }+ var out = input;+ out.files = files.items;+ out.support_omitted = true;+ out.support_limit = "review packet exceeded 256 KiB; complete sources and supporting files omitted, committed diffs kept";+ return out;+}+pub const max_files = 32;pub const max_bytes = 128 * 1024;@@ -157,36 +176,48 @@ pub fn gather(gpa: A, io: std.Io, arena: A, cwd: []const u8, base: []const u8, hvar paths = std.mem.splitScalar(u8, names, 0);var files: std.ArrayList(File) = .empty;var size = body.len;+ // Reserve every changed hunk first (#1345): complete sources are optional+ // context, so an early large file must not crowd out a later file's diff.+ // Only the diffs themselves can make a review too large.while (paths.next()) |path| {if (path.len == 0) continue;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 });+ if (size + change.len > max_bytes) return error.ReviewTooLarge;+ size += change.len;+ try files.append(arena, .{ .path = path, .before = null, .after = null, .change = change });+ }+ for (files.items) |*file| {var after_omitted = false;- const after = blob(gpa, io, arena, cwd, head, path) catch |err| blk: {+ const after = blob(gpa, io, arena, cwd, head, file.path) catch |err| blk: {if (err != error.ReviewTooLarge) return err;after_omitted = true;break :blk null;};// In mixed changes, reserve room for source and test reachability// instead of repeating complete documentation pages.- if (after != null and has_source_change and std.mem.endsWith(u8, path, ".md")) after_omitted = true;- // A context diff carries the old lines; repeating the complete base- // blob would charge unchanged source twice and crowd out test evidence.- var change = try raw(gpa, io, arena, cwd, &.{ "git", "diff", "--no-ext-diff", "--no-renames", "--unified=3", base, head, "--", path });- if (size + change.len > max_bytes) return error.ReviewTooLarge;+ if (after != null and has_source_change and std.mem.endsWith(u8, file.path, ".md")) after_omitted = true;if (after) |text| {- if (size + change.len + text.len > max_bytes) after_omitted = true;+ if (size + text.len > max_bytes) after_omitted = true;}if (after_omitted) {// Preserve more committed context when it fits. Never label this// excerpt as the complete proposed-head source.- if (raw(gpa, io, arena, cwd, &.{ "git", "diff", "--no-ext-diff", "--no-renames", "--unified=20", base, head, "--", path })) |expanded| {- if (size + expanded.len <= max_bytes) change = expanded;+ if (raw(gpa, io, arena, cwd, &.{ "git", "diff", "--no-ext-diff", "--no-renames", "--unified=20", base, head, "--", file.path })) |expanded| {+ if (size - file.change.?.len + expanded.len <= max_bytes) {+ size = size - file.change.?.len + expanded.len;+ file.change = expanded;+ }} else |err| {if (err != error.ReviewTooLarge) return err;}+ } else if (after) |text| {+ size += text.len;+ file.after = text;}- size += change.len + if (after_omitted) @as(usize, 0) else if (after) |text| text.len else 0;- try files.append(arena, .{ .path = path, .before = null, .after = if (after_omitted) null else after, .change = change, .after_omitted = after_omitted });+ file.after_omitted = after_omitted;}if (files.items.len == 0) return error.NoChangedFiles;var support: Support = .{ .gpa = gpa, .io = io, .arena = arena, .cwd = cwd, .head = head, .files = &files, .size = &size, .seen = std.StringHashMap(void).init(arena) };@@ -502,3 +533,49 @@ test "claim review includes committed nested package runners and workflow reachatry 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/publish_gate.zig b/src/publish_gate.zigindex a11699c9..fd2779b5 100644--- a/src/publish_gate.zig+++ b/src/publish_gate.zig@@ -61,7 +61,7 @@ fn observe(self: *Agent, cmd: []const u8) !?ExecResult {if (!draft) {const review = @import("pr_claim_review.zig").review(self, target, command.flag("--base", "-B"), creating, ev.head_sha, ev.body, ev.head_status) catch |err|return .{ .text = if (err == error.ReviewTooLarge)- "PR publication preflight: claim review input exceeded its size limit (128 KiB committed source budget or 256 KiB review packet); write NOT performed. Narrow the change or provide smaller relevant evidence."+ "PR publication preflight: the PR's own diff exceeds the claim review limit (128 KiB of changed hunks, 32 changed files, or a 256 KiB packet even with complete sources omitted); write NOT performed. Split the PR or keep it a draft. Large unchanged source around a small edit does not count toward this limit."else"PR publication preflight: claim review could not establish readiness from the committed source and tests; write NOT performed. Keep a draft while evidence is unresolved.", .is_error = true };if (review.verdict != .supported) return .{ .text = try std.fmt.allocPrint(self.arena, "PR publication preflight: claim review is {s}: {s}. Write NOT performed; keep a draft or fix the unsupported claim and coverage.", .{ @tagName(review.verdict), review.reason }), .is_error = true };