mirror of
https://github.com/garrytan/gstack.git
synced 2026-11-26 18:04:31 +01:00
fix(review-exclude): ready binds an older digest when no selection holds a PR file; name scan greps consumers only (GST-1434, GST-1435) (#4036)
* fix(review-exclude): ready binds an older digest when no selection holds a PR file; the name scan greps consumers only (GST-1434, GST-1435) GST-1434: gstack-ship-queue ready (and train build, land) refused any receipt whose recorded review_exclude digest differed from the base policy's, so a policy edit on main voided every receipted PR even when no PR file fell under a pattern (harness-evals #274, #276 after #278). excludeMismatch now uses carryExcludeBinds, the predicate carry uses since #4030: a digest change binds when neither the recorded selection nor HEAD's holds a file the PR changed; anything else still refuses. GST-1435: the named-by-code scan ran git grep over the whole tree, so on harness-evals #269 it searched 30 MB of receipts for 4,354 candidate paths, ran 278 s past its 120 s limit and kept every file. It now lists each tree, keeps the consumer files (code and tests outside the candidates) and greps only those through literal pathspecs in 24 KB chunks. A git failure or timeout still keeps every candidate. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * fix: pre-landing review fixes grepFixed keeps every candidate when a consumer's name does not survive UTF-8 decoding, since its literal pathspec would match nothing. Review-Finding: p1/outside-voice#1 reader=outside-voice Review-Finding: p1/adversarial-native#1 reader=adversarial-native Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * docs: the queue's review_exclude check follows carry's rule (GST-1434) Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> --------- Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
This commit is contained in:
1 parent
467be16ab2
commit
6278ee4fb7
7 files changed
+147
-64
No files matched your search
+36
-22
@@ -79,6 +79,18 @@ export const recordSelectsNoFile = (r: Pick<ExcludeRecord, 'patterns_sha256' | '
|
||||
const shellQuote = (s: string) => `'${s.replace(/'/g, "'\\''")}'`;
|
||||
const literal = (p: string) => `:(top,literal)${p}`;
|
||||
const argBytes = (paths: Iterable<string>) => { let n = 0; for (const p of paths) n += Buffer.byteLength(shellQuote(literal(p))) + 1; return n; };
|
||||
/** `paths` in order, split so each chunk's literal pathspecs stay within KEEP_ARG_MAX_BYTES (one path past it is its own chunk). */
|
||||
function argChunks(paths: readonly string[]): string[][] {
|
||||
const chunks: string[][] = [];
|
||||
let bytes = Infinity;
|
||||
for (const p of paths) {
|
||||
const n = argBytes([p]);
|
||||
if (bytes + n > KEEP_ARG_MAX_BYTES) { chunks.push([]); bytes = 0; }
|
||||
chunks[chunks.length - 1]!.push(p);
|
||||
bytes += n;
|
||||
}
|
||||
return chunks;
|
||||
}
|
||||
|
||||
function git(root: string, args: string[], input?: string): { ok: boolean; status: number | null; out: string; err: string } {
|
||||
const r = spawnSync('git', ['-c', 'core.quotePath=false', ...args], { cwd: root, input, encoding: 'utf8', timeout: 120_000, maxBuffer: MAX_BUFFER });
|
||||
@@ -183,15 +195,7 @@ function binaryContent(i: ExcludeInput, files: ReadonlyMap<string, Matched>, cla
|
||||
function attributeBinaryLines(i: ExcludeInput, paths: readonly string[], unread: Set<string>): Record<string, string[]> {
|
||||
const out: Record<string, string[]> = {};
|
||||
const range = i.head ? [i.mergeBase, i.head] : [i.mergeBase];
|
||||
const chunks: string[][] = [];
|
||||
let bytes = Infinity;
|
||||
for (const p of [...paths].sort((a, b) => Buffer.compare(Buffer.from(a), Buffer.from(b)))) {
|
||||
const n = argBytes([p]);
|
||||
if (bytes + n > KEEP_ARG_MAX_BYTES) { chunks.push([]); bytes = 0; }
|
||||
chunks[chunks.length - 1]!.push(p);
|
||||
bytes += n;
|
||||
}
|
||||
for (const chunk of chunks) {
|
||||
for (const chunk of argChunks([...paths].sort((a, b) => Buffer.compare(Buffer.from(a), Buffer.from(b))))) {
|
||||
const r = git(i.root, ['diff', '--text', '--no-textconv', '--no-ext-diff', '--no-renames', '--src-prefix=a/', '--dst-prefix=b/', '-U0', ...range, '--', ...chunk.map(literal)]);
|
||||
const sections = r.ok ? r.out.split(/^(?=diff --git )/m).filter(s => s.startsWith('diff --git ')) : [];
|
||||
const aligned = sections.length === chunk.length && chunk.every((p, k) => /["\\\x00-\x1f\x7f]/.test(p) || sections[k]!.startsWith(`diff --git a/${p} b/${p}\n`));
|
||||
@@ -236,7 +240,13 @@ function pathReason(i: ExcludeInput, p: string, m: Matched, binary: ReadonlySet<
|
||||
const consumer = (i: ExcludeInput, f: string, candidates: ReadonlySet<string>) =>
|
||||
!candidates.has(f) && !f.startsWith('.gstack/tmp/') && contentKind(i, f, new Set()) === null;
|
||||
|
||||
/** An uncapped `git grep -F -o -f` of fixed strings at the head (or the working tree) and `revs`: string -> the consumer files holding it; null when git grep failed. */
|
||||
/**
|
||||
* An uncapped `git grep -F -o -f` of fixed strings at the head (or the working tree) and `revs`: string -> the consumer files
|
||||
* holding it; null when git could not list a tree, a consumer's name did not survive UTF-8 decoding (its literal pathspec would
|
||||
* match nothing), or git grep failed (a timeout included). Each tree's file list is filtered to
|
||||
* consumers first and grepped through literal pathspecs, so the data under the patterns is never read (GST-1435: a whole-tree
|
||||
* grep for 4,354 candidate paths through 30 MB of receipts ran 278 s).
|
||||
*/
|
||||
function grepFixed(i: ExcludeInput, strings: readonly string[], candidates: ReadonlySet<string>, revs: readonly string[]): Map<string, Set<string>> | null {
|
||||
const found = new Map<string, Set<string>>();
|
||||
if (!strings.length) return found;
|
||||
@@ -244,18 +254,22 @@ function grepFixed(i: ExcludeInput, strings: readonly string[], candidates: Read
|
||||
try {
|
||||
const list = path.join(dir, 'patterns');
|
||||
fs.writeFileSync(list, strings.join('\n') + '\n');
|
||||
const runs = [i.head ? { at: i.head, args: [i.head, '--'] } : { at: '', args: ['--'] }, ...revs.map(rev => ({ at: rev, args: [rev, '--'] }))];
|
||||
for (const run of runs) {
|
||||
const r = git(i.root, ['grep', '-I', '-F', '-o', '--null', '--no-line-number', '--no-column', '--no-color', '-f', list, ...run.args]);
|
||||
if (r.status !== 0 && r.status !== 1) return null;
|
||||
for (const line of r.out.split('\n')) {
|
||||
const parts = line.split('\0');
|
||||
const where = parts[0];
|
||||
const match = parts.length > 1 ? parts[parts.length - 1] : undefined;
|
||||
if (!where || !match) continue;
|
||||
const file = run.at ? where.slice(run.at.length + 1) : where;
|
||||
if (!consumer(i, file, candidates)) continue;
|
||||
(found.get(match) ?? found.set(match, new Set()).get(match)!).add(file);
|
||||
for (const at of [i.head ?? '', ...revs]) {
|
||||
const ls = git(i.root, at ? ['ls-tree', '-r', '-z', '--name-only', at] : ['ls-files', '-z']);
|
||||
if (!ls.ok) return null;
|
||||
const files = [...new Set(ls.out.split('\0'))].filter(f => f && consumer(i, f, candidates));
|
||||
if (files.some(f => f.includes('\uFFFD'))) return null;
|
||||
for (const chunk of argChunks(files)) {
|
||||
const r = git(i.root, ['grep', '-I', '-F', '-o', '--null', '--no-line-number', '--no-column', '--no-color', '-f', list, ...(at ? [at] : []), '--', ...chunk.map(literal)]);
|
||||
if (r.status !== 0 && r.status !== 1) return null;
|
||||
for (const line of r.out.split('\n')) {
|
||||
const parts = line.split('\0');
|
||||
const where = parts[0];
|
||||
const match = parts.length > 1 ? parts[parts.length - 1] : undefined;
|
||||
if (!where || !match) continue;
|
||||
const file = at ? where.slice(at.length + 1) : where;
|
||||
(found.get(match) ?? found.set(match, new Set()).get(match)!).add(file);
|
||||
}
|
||||
}
|
||||
}
|
||||
return found;
|
||||
|
||||
@@ -279,20 +279,13 @@ function headInputs(root: string, baseRef: string, head: string, r?: ShipReceipt
|
||||
return { baseSha, c, scope: scopeOfRange(root, BIN_DIR, baseRev, head, merged), x: exclusionAt(root, baseRef, baseSha.ok ? baseSha.out : null, head, c, tagged, 'gstack-ship-queue') };
|
||||
}
|
||||
|
||||
/**
|
||||
* The queue's exclusion check: the digest the receipt recorded (none when absent) against the one `head` has under the current
|
||||
* base policy. Only the digest counts (the policy's patterns and the excluded list), so a base merge or a moved base that leaves
|
||||
* both unchanged binds; a changed selection needs the review rerun (write checks the same digest and does not read the
|
||||
* review ledger). A receipt with no digest also binds when no file the head changed matches any current pattern, excluded
|
||||
* or kept (GST-1408): the selection then equals the full diff that receipt's review read, so a pattern the base gained later
|
||||
* changes nothing the reviewers saw.
|
||||
*/
|
||||
/** No file the head changed matches any pattern the base policy declares, so each reviewer reads the full diff (GST-1408). */
|
||||
const selectsNoFile = (x: Exclusion) => x.state === 'none' || x.state === 'head-only' || (x.state === 'active' && x.record.unmatched.length === x.record.patterns.length);
|
||||
/**
|
||||
* Carry's exclusion check: the recorded digest against the one HEAD has under the current base policy, except that a policy
|
||||
* change alone does not void the receipt (GST-1415): when neither the receipt's selection nor HEAD's holds a file the PR
|
||||
* changed, each reviewer read the full diff and still would, whatever the patterns. A policy that cannot be read never binds.
|
||||
* Carry's and the queue's exclusion check: the recorded digest against the one HEAD has under the current base policy, except
|
||||
* that a policy change alone does not void the receipt (GST-1408, GST-1415, GST-1434): when neither the receipt's selection
|
||||
* (none recorded, or an empty one) nor HEAD's holds a file the PR changed, each reviewer read the full diff and still would,
|
||||
* whatever the patterns. A policy that cannot be read never binds.
|
||||
*/
|
||||
export function carryExcludeBinds(x: Exclusion, r: ShipReceipt): boolean {
|
||||
if (x.state === 'refused') return false;
|
||||
@@ -389,14 +382,14 @@ export function writeExcludeBinds(root: string, base: string, head: string, revi
|
||||
}
|
||||
return { ok: true, line: `REVIEW_EXCLUDE_DOCS_AUDIT: reviewed=${short(given)} head=${short(x.record.digest)} snapshot=${short(at)} files=${auditFiles.join(',')} (the selection changed only by the Step 14.5 docs audit's files under review_exclude_paths; the receipt records HEAD's)` };
|
||||
}
|
||||
/** The queue's refusal when carryExcludeBinds does not bind the receipt at `head`; null when it does. */
|
||||
export function excludeMismatch(x: Exclusion, r: ShipReceipt, head: string, base: string): string | null {
|
||||
if (carryExcludeBinds(x, r)) return null;
|
||||
const expected = x.state === 'active' ? x.record.digest : 'none';
|
||||
const recorded = r.review_exclude?.digest ?? 'none';
|
||||
if (expected === recorded) return null;
|
||||
if (recorded === 'none' && selectsNoFile(x)) return null;
|
||||
const source = x.state === 'active' ? x.record.source : r.review_exclude?.source ?? base;
|
||||
const h = head.slice(0, 12);
|
||||
return `field=review_exclude expected=${expected} recorded=${recorded} source=${source}: the receipt records another selection than ${base}'s review_exclude_paths gives ${h}; the receipt is unchanged; fix: rerun /ship's review on ${h} (gstack-review-dispatch pass --base ${base.replace(/^(refs\/remotes\/)?origin\//, '')}), then gstack-ship-receipt write --pr <n> --review-exclude <d> --body <body> with the digest gstack-review-dispatch verify --ledger <ledger> prints on REVIEW_EXCLUDE_RECORD: (write checks the digest against the base policy and does not read the review ledger, so only the rerun shows the reviewers read this selection)`;
|
||||
return `field=review_exclude expected=${expected} recorded=${recorded} source=${source}: the receipt records another selection than ${base}'s review_exclude_paths gives ${h}, and one of the two holds a file the PR changed; the receipt is unchanged; fix: rerun /ship's review on ${h} (gstack-review-dispatch pass --base ${base.replace(/^(refs\/remotes\/)?origin\//, '')}), then gstack-ship-receipt write --pr <n> --review-exclude <d> --body <body> with the digest gstack-review-dispatch verify --ledger <ledger> prints on REVIEW_EXCLUDE_RECORD: (write checks the digest against the base policy and does not read the review ledger, so only the rerun shows the reviewers read this selection)`;
|
||||
}
|
||||
/** The receipt's recorded gate scan when it names this head; otherwise null (unavailable). */
|
||||
const boundScan = (r: ShipReceipt, head: string): number | null => {
|
||||
|
||||
Reference in new issue
Block a user