@a2f0/agent-tool 0.1.85 → 0.1.86
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/review/pinnedReviewInput.ts +310 -10
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { gitEnvironment } from "../git/environment";
|
|
2
2
|
import { execFileSync, spawnSync } from "node:child_process";
|
|
3
3
|
|
|
4
|
-
import { type PrContext } from "../git/prContext";
|
|
4
|
+
import { MAX_BUFFER_BYTES, type PrContext } from "../git/prContext";
|
|
5
5
|
import { changedGeneratedFiles, type GeneratedFiles } from "./generatedFiles";
|
|
6
6
|
import { readReviewInstructions } from "./reviewPrompt";
|
|
7
7
|
import { withReviewInterrupts } from "./reviewerProcess";
|
|
@@ -19,12 +19,12 @@ interface PinnedReviewInput {
|
|
|
19
19
|
|
|
20
20
|
const gitEnv = gitEnvironment();
|
|
21
21
|
|
|
22
|
-
function gitOutput(rootDir: string, args: string[]): string {
|
|
22
|
+
function gitOutput(rootDir: string, args: string[], input?: string): string {
|
|
23
23
|
return execFileSync("git", args, {
|
|
24
24
|
cwd: rootDir,
|
|
25
25
|
env: gitEnv,
|
|
26
26
|
encoding: "utf8",
|
|
27
|
-
stdio: ["ignore", "pipe", "pipe"],
|
|
27
|
+
...(input === undefined ? { stdio: ["ignore", "pipe", "pipe"] } : { input, stdio: ["pipe", "pipe", "pipe"] }),
|
|
28
28
|
maxBuffer: 8 * 1024 * 1024,
|
|
29
29
|
});
|
|
30
30
|
}
|
|
@@ -69,6 +69,312 @@ function ensureChanges(
|
|
|
69
69
|
}
|
|
70
70
|
}
|
|
71
71
|
|
|
72
|
+
/** Git's own test: content is binary when its first 8000 bytes hold a NUL. */
|
|
73
|
+
const SNIFF_BYTES = 8000;
|
|
74
|
+
const RAW_RECORD = /^:(\d{6}) (\d{6}) ([0-9a-f]+) ([0-9a-f]+) ([A-Z])$/;
|
|
75
|
+
/**
|
|
76
|
+
* Media, font, document, and archive types. Only binary content under one of
|
|
77
|
+
* these extensions leaves the patch: source files never do, so a NUL byte
|
|
78
|
+
* cannot hide code from the reviewer.
|
|
79
|
+
*/
|
|
80
|
+
const ASSET_EXTENSIONS = new Set([
|
|
81
|
+
"avif", "bmp", "gif", "heic", "icns", "ico", "jpeg", "jpg", "png", "psd",
|
|
82
|
+
"tif", "tiff", "webp", "eot", "otf", "ttf", "woff", "woff2", "pdf", "aac",
|
|
83
|
+
"flac", "m4a", "mp3", "ogg", "opus", "wav", "m4v", "mov", "mp4", "webm",
|
|
84
|
+
"7z", "bz2", "gz", "tgz", "xz", "zip", "zst",
|
|
85
|
+
]);
|
|
86
|
+
/** Keeps a leading BOM, so decoding and re-encoding a path round-trips. */
|
|
87
|
+
const utf8 = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
|
|
88
|
+
/** macOS Git rewrites pathspec arguments to NFC unless told not to. */
|
|
89
|
+
const PATHSPEC_GIT = ["-c", "core.precomposeUnicode=false"];
|
|
90
|
+
|
|
91
|
+
/** One side of a changed path, absent when null. */
|
|
92
|
+
type Side =
|
|
93
|
+
| { readonly kind: "submodule"; readonly oid: string }
|
|
94
|
+
| {
|
|
95
|
+
readonly kind: "blob";
|
|
96
|
+
readonly oid: string;
|
|
97
|
+
readonly size: number;
|
|
98
|
+
readonly binary: boolean;
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
interface Asset {
|
|
102
|
+
readonly path: string;
|
|
103
|
+
readonly status: string;
|
|
104
|
+
readonly before: Side | null;
|
|
105
|
+
readonly after: Side | null;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** A changed path from `git diff --raw -z`, its bytes kept exact. */
|
|
109
|
+
interface RawChange {
|
|
110
|
+
readonly path: Buffer;
|
|
111
|
+
readonly status: string;
|
|
112
|
+
readonly sides: readonly [readonly [string, string], readonly [string, string]];
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Byte-exact string form of a path, for comparing paths Git reports. */
|
|
116
|
+
const pathKey = (path: Buffer) => path.toString("latin1");
|
|
117
|
+
|
|
118
|
+
/** The changed paths `git diff --raw` reports for these revisions and pathspecs. */
|
|
119
|
+
function rawChanges(rootDir: string, args: string[]): RawChange[] {
|
|
120
|
+
// `-z` raw records alternate `:<modes> <oids> <status>` and the path.
|
|
121
|
+
const output = execFileSync(
|
|
122
|
+
"git",
|
|
123
|
+
[
|
|
124
|
+
...PATHSPEC_GIT,
|
|
125
|
+
"diff",
|
|
126
|
+
"--raw",
|
|
127
|
+
"-z",
|
|
128
|
+
"--no-renames",
|
|
129
|
+
"--no-abbrev",
|
|
130
|
+
"--no-ext-diff",
|
|
131
|
+
"--no-textconv",
|
|
132
|
+
...args,
|
|
133
|
+
],
|
|
134
|
+
{ cwd: rootDir, env: gitEnv, maxBuffer: MAX_BUFFER_BYTES, stdio: ["ignore", "pipe", "pipe"] },
|
|
135
|
+
);
|
|
136
|
+
const fields: Buffer[] = [];
|
|
137
|
+
for (let start = 0; start < output.length; ) {
|
|
138
|
+
const end = output.indexOf(0, start);
|
|
139
|
+
if (end < 0) {
|
|
140
|
+
throw new Error("Malformed raw diff from git.");
|
|
141
|
+
}
|
|
142
|
+
fields.push(output.subarray(start, end));
|
|
143
|
+
start = end + 1;
|
|
144
|
+
}
|
|
145
|
+
if (fields.length % 2 !== 0) {
|
|
146
|
+
throw new Error("Malformed raw diff from git.");
|
|
147
|
+
}
|
|
148
|
+
const changes: RawChange[] = [];
|
|
149
|
+
for (let index = 0; index < fields.length; index += 2) {
|
|
150
|
+
const match = RAW_RECORD.exec(fields[index]!.toString("ascii"));
|
|
151
|
+
const path = fields[index + 1]!;
|
|
152
|
+
if (match === null || path.length === 0) {
|
|
153
|
+
throw new Error(`Malformed raw diff record: ${fields[index]}`);
|
|
154
|
+
}
|
|
155
|
+
const [, oldMode, newMode, oldOid, newOid, status] = match;
|
|
156
|
+
changes.push({
|
|
157
|
+
path,
|
|
158
|
+
status: status!,
|
|
159
|
+
sides: [
|
|
160
|
+
[oldMode!, oldOid!],
|
|
161
|
+
[newMode!, newOid!],
|
|
162
|
+
],
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
return changes;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Read each blob once, framed and checked as the snapshot reads its tree, and
|
|
170
|
+
* apply Git's binary test to its bytes.
|
|
171
|
+
*/
|
|
172
|
+
function readBlobs(
|
|
173
|
+
rootDir: string,
|
|
174
|
+
oids: string[],
|
|
175
|
+
): Map<string, { size: number; binary: boolean }> {
|
|
176
|
+
const blobs = new Map<string, { size: number; binary: boolean }>();
|
|
177
|
+
if (oids.length === 0) {
|
|
178
|
+
return blobs;
|
|
179
|
+
}
|
|
180
|
+
const output = execFileSync("git", ["cat-file", "--batch"], {
|
|
181
|
+
cwd: rootDir,
|
|
182
|
+
env: gitEnv,
|
|
183
|
+
input: `${oids.join("\n")}\n`,
|
|
184
|
+
maxBuffer: MAX_BUFFER_BYTES,
|
|
185
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
186
|
+
});
|
|
187
|
+
let offset = 0;
|
|
188
|
+
for (const oid of oids) {
|
|
189
|
+
const headerEnd = output.indexOf(0x0a, offset);
|
|
190
|
+
const header =
|
|
191
|
+
headerEnd < 0 ? "" : output.subarray(offset, headerEnd).toString("ascii");
|
|
192
|
+
const match = /^([0-9a-f]+) blob (\d+)$/.exec(header);
|
|
193
|
+
if (match === null || match[1] !== oid) {
|
|
194
|
+
throw new Error(`Malformed blob response for ${oid}: ${header}`);
|
|
195
|
+
}
|
|
196
|
+
const size = Number(match[2]);
|
|
197
|
+
const contentStart = headerEnd + 1;
|
|
198
|
+
const contentEnd = contentStart + size;
|
|
199
|
+
if (contentEnd >= output.length || output[contentEnd] !== 0x0a) {
|
|
200
|
+
throw new Error(`Truncated blob response for ${oid}.`);
|
|
201
|
+
}
|
|
202
|
+
const sniffed = output.subarray(
|
|
203
|
+
contentStart,
|
|
204
|
+
Math.min(contentEnd, contentStart + SNIFF_BYTES),
|
|
205
|
+
);
|
|
206
|
+
blobs.set(oid, { size, binary: sniffed.includes(0) });
|
|
207
|
+
offset = contentEnd + 1;
|
|
208
|
+
}
|
|
209
|
+
return blobs;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/** The path as a string that encodes back to the same bytes, or null. */
|
|
213
|
+
function exactUtf8(path: Buffer): string | null {
|
|
214
|
+
try {
|
|
215
|
+
const decoded = utf8.decode(path);
|
|
216
|
+
return Buffer.from(decoded, "utf8").equals(path) ? decoded : null;
|
|
217
|
+
} catch {
|
|
218
|
+
return null;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function isAssetPath(path: string): boolean {
|
|
223
|
+
const name = path.slice(path.lastIndexOf("/") + 1);
|
|
224
|
+
const dot = name.lastIndexOf(".");
|
|
225
|
+
return dot > 0 && ASSET_EXTENSIONS.has(name.slice(dot + 1).toLowerCase());
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Changed asset files to list instead of patch: an asset extension, a blob on
|
|
230
|
+
* either side that fails Git's NUL test (read from the blob, so attributes
|
|
231
|
+
* cannot decide), a path that is exact UTF-8, and no other changed path under
|
|
232
|
+
* it, compared byte for byte, which excluding it would also hide.
|
|
233
|
+
*/
|
|
234
|
+
function binaryAssets(rootDir: string, changed: RawChange[]): Asset[] {
|
|
235
|
+
// Every directory a changed path sits in, as `dir/`, byte for byte.
|
|
236
|
+
const parents = new Set(
|
|
237
|
+
changed.flatMap(({ path }) => {
|
|
238
|
+
const key = pathKey(path);
|
|
239
|
+
return [...key.matchAll(/\//g)].map((slash) => key.slice(0, slash.index + 1));
|
|
240
|
+
}),
|
|
241
|
+
);
|
|
242
|
+
const candidates = changed.flatMap((change) => {
|
|
243
|
+
const path = exactUtf8(change.path);
|
|
244
|
+
const under = pathKey(Buffer.concat([change.path, Buffer.from("/")]));
|
|
245
|
+
return path !== null && isAssetPath(path) && !parents.has(under)
|
|
246
|
+
? [{ ...change, path }]
|
|
247
|
+
: [];
|
|
248
|
+
});
|
|
249
|
+
const blobs = readBlobs(rootDir, [
|
|
250
|
+
...new Set(
|
|
251
|
+
candidates.flatMap(({ sides }) =>
|
|
252
|
+
sides
|
|
253
|
+
.filter(([mode]) => mode !== "000000" && mode !== "160000")
|
|
254
|
+
.map(([, oid]) => oid),
|
|
255
|
+
),
|
|
256
|
+
),
|
|
257
|
+
]);
|
|
258
|
+
const side = ([mode, oid]: readonly [string, string]): Side | null =>
|
|
259
|
+
mode === "000000"
|
|
260
|
+
? null
|
|
261
|
+
: mode === "160000"
|
|
262
|
+
? { kind: "submodule", oid }
|
|
263
|
+
: { kind: "blob", oid, ...blobs.get(oid)! };
|
|
264
|
+
const binary = (value: Side | null) =>
|
|
265
|
+
value?.kind === "blob" && value.binary;
|
|
266
|
+
return candidates.flatMap(({ path, status, sides }) => {
|
|
267
|
+
const before = side(sides[0]);
|
|
268
|
+
const after = side(sides[1]);
|
|
269
|
+
return binary(before) || binary(after)
|
|
270
|
+
? [{ path, status, before, after }]
|
|
271
|
+
: [];
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/** One line per listed asset, with quoted paths, so the reviewer still sees every change. */
|
|
276
|
+
function assetSummary(assets: Asset[]): string {
|
|
277
|
+
if (assets.length === 0) {
|
|
278
|
+
return "";
|
|
279
|
+
}
|
|
280
|
+
const describe = (value: Side | null) =>
|
|
281
|
+
value === null
|
|
282
|
+
? "absent"
|
|
283
|
+
: value.kind === "submodule"
|
|
284
|
+
? `submodule commit ${value.oid}`
|
|
285
|
+
: `${value.binary ? "binary" : "text"} ${value.size} bytes (blob ${value.oid})`;
|
|
286
|
+
return [
|
|
287
|
+
`# Binary files: ${assets.length} changed media, font, document, or archive file(s) with binary content (a NUL byte in a blob's first ${SNIFF_BYTES} bytes, read from the blob, not attributes), left out of the patch. Review each path, size, and purpose; the bytes are in the snapshot. Text or a submodule commit on either side follows: what binary content replaces as a deletion, and what replaces binary content as a new file.`,
|
|
288
|
+
...assets.map(
|
|
289
|
+
({ path, status, before, after }) =>
|
|
290
|
+
`${status} ${JSON.stringify(path)}: ${describe(before)} -> ${describe(after)}`,
|
|
291
|
+
),
|
|
292
|
+
].join("\n");
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/** A `--text` patch for these revisions, limited by the pathspecs when given. */
|
|
296
|
+
function textPatch(rootDir: string, revisions: string[], pathspecs: string[]): string {
|
|
297
|
+
return git(rootDir, [
|
|
298
|
+
...PATHSPEC_GIT,
|
|
299
|
+
"diff",
|
|
300
|
+
"--text",
|
|
301
|
+
"--no-textconv",
|
|
302
|
+
"--no-ext-diff",
|
|
303
|
+
...revisions,
|
|
304
|
+
"--",
|
|
305
|
+
...pathspecs,
|
|
306
|
+
]);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
const literal = (assets: Asset[], magic: string) =>
|
|
310
|
+
assets.map(({ path }) => `:(top,literal${magic})${path}`);
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* The review patch: every change through `--text`, which keeps a branch's
|
|
314
|
+
* attributes from hiding text, except binary asset files, which are listed;
|
|
315
|
+
* then, for those, any text or submodule commit on either side, patched
|
|
316
|
+
* against the empty tree, so no binary bytes reach the prompt.
|
|
317
|
+
*
|
|
318
|
+
* Before any pathspec shapes the patch, Git reports the exact paths it
|
|
319
|
+
* selects. When they differ from what is meant, as a path Git rewrites or a
|
|
320
|
+
* pathspec that reaches further would cause, nothing is listed and every
|
|
321
|
+
* change is patched in full.
|
|
322
|
+
*/
|
|
323
|
+
export function reviewDiff(
|
|
324
|
+
rootDir: string,
|
|
325
|
+
baseCommit: string,
|
|
326
|
+
headCommit: string,
|
|
327
|
+
): string {
|
|
328
|
+
const range = `${baseCommit}...${headCommit}`;
|
|
329
|
+
const changed = rawChanges(rootDir, [range]);
|
|
330
|
+
const listed = binaryAssets(rootDir, changed);
|
|
331
|
+
const readable = (value: Side | null) =>
|
|
332
|
+
value !== null && !(value.kind === "blob" && value.binary);
|
|
333
|
+
const removed = listed.filter(({ before }) => readable(before));
|
|
334
|
+
const added = listed.filter(({ after }) => readable(after));
|
|
335
|
+
const emptyTree =
|
|
336
|
+
removed.length + added.length === 0
|
|
337
|
+
? ""
|
|
338
|
+
: gitOutput(rootDir, ["hash-object", "-t", "tree", "--stdin"], "").trim();
|
|
339
|
+
const mergeBase =
|
|
340
|
+
removed.length === 0 ? "" : git(rootDir, ["merge-base", baseCommit, headCommit]);
|
|
341
|
+
const keysOf = (assets: Asset[]) =>
|
|
342
|
+
assets.map(({ path }) => pathKey(Buffer.from(path, "utf8")));
|
|
343
|
+
const listedKeys = new Set(keysOf(listed));
|
|
344
|
+
const selects = (revisions: string[], pathspecs: string[], expected: string[]) => {
|
|
345
|
+
const selected = rawChanges(rootDir, [...revisions, "--", ...pathspecs])
|
|
346
|
+
.map(({ path }) => pathKey(path))
|
|
347
|
+
.sort();
|
|
348
|
+
return selected.join("\0") === [...expected].sort().join("\0");
|
|
349
|
+
};
|
|
350
|
+
const exact =
|
|
351
|
+
listed.length === 0 ||
|
|
352
|
+
(selects(
|
|
353
|
+
[range],
|
|
354
|
+
literal(listed, ",exclude"),
|
|
355
|
+
changed.map(({ path }) => pathKey(path)).filter((key) => !listedKeys.has(key)),
|
|
356
|
+
) &&
|
|
357
|
+
(removed.length === 0 ||
|
|
358
|
+
selects([mergeBase, emptyTree], literal(removed, ""), keysOf(removed))) &&
|
|
359
|
+
(added.length === 0 ||
|
|
360
|
+
selects([emptyTree, headCommit], literal(added, ""), keysOf(added))));
|
|
361
|
+
if (!exact) {
|
|
362
|
+
return textPatch(rootDir, [range], []);
|
|
363
|
+
}
|
|
364
|
+
return [
|
|
365
|
+
// One argument per asset: past roughly ten thousand, the OS argument
|
|
366
|
+
// limit (E2BIG) fails the review rather than patching their bytes.
|
|
367
|
+
textPatch(rootDir, [range], literal(listed, ",exclude")),
|
|
368
|
+
assetSummary(listed),
|
|
369
|
+
removed.length === 0
|
|
370
|
+
? ""
|
|
371
|
+
: textPatch(rootDir, [mergeBase, emptyTree], literal(removed, "")),
|
|
372
|
+
added.length === 0 ? "" : textPatch(rootDir, [emptyTree, headCommit], literal(added, "")),
|
|
373
|
+
]
|
|
374
|
+
.filter((part) => part.length > 0)
|
|
375
|
+
.join("\n\n");
|
|
376
|
+
}
|
|
377
|
+
|
|
72
378
|
/**
|
|
73
379
|
* Resolve refs once, then derive every review artifact from those commit IDs.
|
|
74
380
|
* An interruption stops the reviewer and removes the snapshot before the
|
|
@@ -91,13 +397,7 @@ async function pinnedReviewInput<T>(
|
|
|
91
397
|
const headCommit = resolveCommit(rootDir, "HEAD");
|
|
92
398
|
ensureChanges(rootDir, baseCommit, headCommit);
|
|
93
399
|
|
|
94
|
-
const diff =
|
|
95
|
-
"diff",
|
|
96
|
-
"--text",
|
|
97
|
-
"--no-textconv",
|
|
98
|
-
"--no-ext-diff",
|
|
99
|
-
`${baseCommit}...${headCommit}`,
|
|
100
|
-
]);
|
|
400
|
+
const diff = reviewDiff(rootDir, baseCommit, headCommit);
|
|
101
401
|
// NUL-separated and untrimmed, so a path is never quoted or altered; a rename counts as both paths.
|
|
102
402
|
const changedPaths = gitOutput(rootDir, [
|
|
103
403
|
"diff",
|