Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions bin/gstack-vendored-verify
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
* The allowlist and the manifest list come from the base branch's .gstack/ship-policy.json (review_vendored), never
* from the head; the manifests themselves are read at HEAD. By default it checks the entries this branch changes
* (files added, modified or deleted since the merge base, and entries whose pin the manifest diff changes); --all checks every
* entry. --offline answers from the cache an earlier run filled, without the network. A repo makes it a required CI
* entry. --offline answers from the cache an earlier run filled, without the network. It also checks the merge base's
* pins for the files this branch changes or deletes (VENDORED_BASE), so the review plan can answer offline for a modified
* or re-pinned file; that line never fails the run. A repo makes it a required CI
* check: exit 1 means a manifest says something that is not true. Logic: lib/review-vendored.ts.
*/
import { spawnSync } from '../lib/spawn-sync';
Expand Down Expand Up @@ -62,37 +64,46 @@ if (!policy) { process.stdout.write(json ? JSON.stringify({ state: 'none' }) + '
const problems: string[] = [];
const entries: VendoredEntry[] = [];
const before = new Map<string, string>();
const prior: VendoredEntry[] = [];
for (const m of policy.manifests) {
const text = git(['show', `${head}:${m}`]);
if (text === null) { problems.push(`${m} is not in HEAD`); continue; }
const parsed = parseManifest(text, m);
problems.push(...parsed.errors);
entries.push(...parsed.entries);
const old = git(['show', `${mergeBase}:${m}`]);
if (old !== null) for (const e of parseManifest(old, m).entries) before.set(e.path, `${e.upstream}@${e.commit}:${e.upstream_path}`);
if (old !== null) for (const e of parseManifest(old, m).entries) { before.set(e.path, `${e.upstream}@${e.commit}:${e.upstream_path}`); prior.push(e); }
}
const seen = new Map<string, number>();
entries.forEach(e => seen.set(e.path.toLowerCase(), (seen.get(e.path.toLowerCase()) ?? 0) + 1));
for (const [p, n] of seen) if (n > 1) problems.push(`${p} is pinned by more than one manifest`);

let selected = entries;
if (!argv.includes('--all')) {
const diff = git(['diff', '--name-only', '-z', '--no-renames', '--diff-filter=ADMT', mergeBase, head]);
if (diff === null) { process.stderr.write(`${CTX}: git could not list the files this branch changes; fix: rerun once, then check the checkout (git status)\n`); process.exit(EXIT.refused); }
const changed = new Set(diff.split('\0').filter(Boolean));
selected = entries.filter(e => changed.has(e.path) || before.get(e.path) !== `${e.upstream}@${e.commit}:${e.upstream_path}`);
}
const res = problems.length ? null : resolveVendored({ root, rev: head, policy, entries: selected, fetch: !argv.includes('--offline'), cacheRoot: vendoredCacheRoot(resolveStateRoot()) });
const diff = git(['diff', '--name-only', '-z', '--no-renames', '--diff-filter=ADMT', mergeBase, head]);
if (diff === null) { process.stderr.write(`${CTX}: git could not list the files this branch changes; fix: rerun once, then check the checkout (git status)\n`); process.exit(EXIT.refused); }
const changed = new Set(diff.split('\0').filter(Boolean));
const selected = argv.includes('--all') ? entries : entries.filter(e => changed.has(e.path) || before.get(e.path) !== `${e.upstream}@${e.commit}:${e.upstream_path}`);
const resolve = (rev: string, list: VendoredEntry[]) => resolveVendored({ root, rev, policy, entries: list, fetch: !argv.includes('--offline'), cacheRoot: vendoredCacheRoot(resolveStateRoot()), keepGood: true });
const res = problems.length ? null : resolve(head, selected);
const baseList = prior.filter(e => changed.has(e.path));
const baseSummary = ((): { checked: number; verified: number } | { error: string } | null => {
if (!res || !baseList.length) return null;
try {
const b = resolve(mergeBase, baseList);
return b ? { checked: b.entries.length, verified: b.entries.filter(e => e.verdict === 'verified').length } : null;
} catch (err) { return { error: (err instanceof Error ? err.message : String(err)).split('\n')[0]! }; }
})();
if (!problems.length && !res) { process.stderr.write(`${CTX}: git could not list HEAD's files; fix: rerun once, then check the checkout (git status)\n`); process.exit(EXIT.refused); }
const failed = res?.entries.filter(e => e.verdict !== 'verified') ?? [];
const summary = { scope: argv.includes('--all') ? 'all' : 'changed', manifests: policy.manifests.length, entries: entries.length, checked: res?.entries.length ?? 0, verified: (res?.entries.length ?? 0) - failed.length, failed: failed.length, pins: res?.pins.length ?? 0, digest: res?.digest ?? null };
if (json) {
process.stdout.write(JSON.stringify({ state: problems.length || failed.length ? 'failed' : 'verified', ...summary, manifest_errors: problems, pin_results: res?.pins ?? [], failures: failed }) + '\n');
process.stdout.write(JSON.stringify({ state: problems.length || failed.length ? 'failed' : 'verified', ...summary, manifest_errors: problems, pin_results: res?.pins ?? [], failures: failed, ...(baseSummary ? { base: baseSummary } : {}) }) + '\n');
} else {
for (const p of problems) process.stdout.write(resultLine(CTX, 'VENDORED_MANIFEST_INVALID', p) + '\n');
for (const p of res?.pins ?? []) process.stdout.write(`VENDORED_PIN: ${p.upstream}@${p.commit.slice(0, 12)} reachable_from=${p.reachable_from ?? 'none'} files=${p.files} verified=${p.verified}\n`);
for (const f of failed.slice(0, 50)) process.stdout.write(resultLine(CTX, CODES[f.verdict as Exclude<VendoredVerdict, 'verified'>], `${f.path} <- ${f.upstream}@${f.commit.slice(0, 12)}:${f.upstream_path}${f.detail ? `; ${f.detail}` : ''}`) + '\n');
if (failed.length > 50) process.stdout.write(`VENDORED_VERIFY_FAILED: +${failed.length - 50} more (--json lists every one)\n`);
if (baseSummary && 'error' in baseSummary) process.stdout.write(`VENDORED_BASE: unavailable (${baseSummary.error}; this line never fails the run)\n`);
else if (baseSummary) process.stdout.write(`VENDORED_BASE: checked=${baseSummary.checked} verified=${baseSummary.verified} (the merge base's copies of the vendored files this branch changes or deletes; the review plan treats a modified file as vendored only when its base copy verified; this line never fails the run)\n`);
process.stdout.write(problems.length ? `VENDORED_VERIFY: failed manifests=${policy.manifests.length} manifest_errors=${problems.length}\n`
: `VENDORED_VERIFY: checked=${summary.checked} verified=${summary.verified} failed=${summary.failed} pins=${summary.pins} manifests=${summary.manifests} scope=${summary.scope} digest=${summary.digest}\n`);
}
Expand Down
1 change: 1 addition & 0 deletions changelog.d/vendored-review-keeps-replaced-own-code.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Review now keeps a file in view when a branch replaces your own code, or drops a local patch, so the file matches an upstream copy byte for byte. A changed file counts as vendored only if it was already vendored and verified at the merge base, or is new on the branch. `gstack-vendored-verify` also caches the merge base's pins for the files a branch changes or deletes, so later offline steps can answer, and a failed fetch no longer wipes earlier good answers from the cache (docs/vendored-code.md).
26 changes: 19 additions & 7 deletions docs/vendored-code.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ Every failure fails closed: the file is simply not vendored.
- Paths are repo-relative, with no `..`, `.` or empty segment, no backslash,
and no two entries that differ only in letter case.
- A file listed here and edited locally fails the check. Remove it from the
manifest to have it reviewed as your own code instead.
manifest to have it reviewed as your own code instead. Putting the upstream
bytes back later passes the check, but review still reads that change, as
below.

## The command

Expand Down Expand Up @@ -159,8 +161,12 @@ gate-tagged file is excerpted only off the gate floor, so a gate-tagged
upstream test file (anything under `tests/`, `test/` or `spec/`) stays in
review whole. When a re-pin deletes files, a deleted file that verifies at the
base's pin can be left out too; a file the branch drops from the manifest but
keeps stays in review. A file that does not verify keeps the reason it had
before. Keep the manifest outside every `review_exclude_paths` pattern: when a
keeps stays in review. A file the branch modifies counts as vendored only when
the merge base's manifests already pinned it and its base copy verified too.
So a branch that replaces your own file with upstream bytes, or drops a local
patch from a vendored file, keeps that file in review as your code, and
readers see your lines leave. A file new on the branch needs only its own
check. A file that does not verify keeps the reason it had before. Keep the manifest outside every `review_exclude_paths` pattern: when a
pattern matches it, nothing counts as verified and the plan prints
`REVIEW_VENDORED: unavailable`.

Expand All @@ -177,10 +183,16 @@ Every later step (the review dispatch, the receipt write and carry) reads the
same cache without the network, and the verified list is part of the
selection digest the receipt binds. A machine with an empty cache verifies
nothing, so its selection differs and the receipt says so; run
`gstack-vendored-verify --base <base>` there first. Reviewers still read the
manifest, every pin change and all of the repo's own code. When unverified
vendored files make a pattern keep too many files, the refusal names them as
`REVIEW_VENDORED_KEPT` and points at `gstack-vendored-verify`.
`gstack-vendored-verify --base <base>` there first. It also caches the merge
base's pins for the files the branch changes or deletes (its `VENDORED_BASE:`
line, which never fails the run), since a modified or re-pinned file needs
them. A fetch that fails fails its own run but leaves the cache's earlier good
answers in place, so a later `gstack-vendored-verify --offline` still answers.
Review keeps those files until a fetch succeeds, so the plan and the receipt's
offline check select the same files. Reviewers still read
the manifest, every pin change and all of the repo's own code. When manifest
entries that don't count as vendored make a pattern keep too many files, the
refusal counts them and points at `gstack-vendored-verify`.

How much this saves depends on the gate tags. On harness-evals#504 (1,056
upstream test files), 877 files and 4.47 MB leave review, but 161 gate-tagged
Expand Down
7 changes: 4 additions & 3 deletions lib/review-exclude.ts
Original file line number Diff line number Diff line change
Expand Up @@ -521,8 +521,9 @@ export function reviewExclude(i: ExcludeInput): Exclusion {
const why = new Map<string, number>();
for (const f of kept) why.set(reasons.get(f)!, (why.get(reasons.get(f)!) ?? 0) + 1);
const top = [...why].sort((a, b) => b[1] - a[1]).map(([r, n]) => `${r}:${n}`).join(',');
const unverified = vsel ? ('error' in vsel ? kept.length : vsel.failed.filter(e => kept.includes(e.path)).length) : 0;
const vfix = unverified ? `${unverified} of them are vendored files that did not verify (gstack-review-plan lists them as REVIEW_VENDORED_KEPT); run gstack-vendored-verify --base ${base} and fix what it names, then rerun once; ` : '';
const unverified = vsel && !('error' in vsel) ? vsel.failed.filter(e => kept.includes(e.path)).length : 0;
const vfix = vsel && 'error' in vsel ? `the vendored check could not run (${vsel.error}), so no matched file counts as vendored; run gstack-vendored-verify --base ${base} and fix what it names, then rerun once; `
: unverified ? `${unverified} of them are manifest entries that do not count as vendored (they did not verify, or the merge base has them as our own code); run gstack-vendored-verify --base ${base} and fix what it names, then rerun once; ` : '';
return { state: 'refused', code: 'REVIEW_EXCLUDE_KEEP_TOO_LARGE', lines: [`${i.context}: review_exclude_paths pattern ${p} on ${source} keeps ${kept.length} matched files in review (${top}), and the command readers run to read them would need ${bytes} argument bytes (more than ${KEEP_ARG_MAX_BYTES}), so the pattern cannot be applied and is not dropped either; fix: ${vfix}when those files are published data, add a review_content_patterns entry for them beside the pattern on ${base} as its own PR (docs/ship-policy.md#review-content-patterns), else narrow the pattern or split the PR; nothing was planned (REVIEW_EXCLUDE_KEEP_TOO_LARGE)`] };
}
applied.push(p);
Expand Down Expand Up @@ -589,7 +590,7 @@ export function excludeLines(x: Exclusion, base: string): string[] {

/** The one line every brief gets under an active exclusion; it names the selection digest the receipt binds (verify's REVIEW_EXCLUDE_RECORD). */
export function disclosureLine(r: Pick<ExcludeRecord, 'source' | 'excluded' | 'excluded_bytes' | 'applied' | 'kept' | 'digest' | 'vendored'>): string {
return `Excluded by base policy ${r.source} (selection ${r.digest}): ${r.excluded} files, ${r.excluded_bytes} bytes on this branch matching ${r.applied.join(' ') || 'no applied pattern'}. They are not in your diff; do not report on their absence. Kept despite a match: ${r.kept} files (listed in the plan).${r.vendored ? ` ${r.vendored} of the excluded files are vendored upstream files gstack verified byte for byte against the pins the base allowlists; the manifest that pins them is in your diff.` : ''}`;
return `Excluded by base policy ${r.source} (selection ${r.digest}): ${r.excluded} files, ${r.excluded_bytes} bytes on this branch matching ${r.applied.join(' ') || 'no applied pattern'}. They are not in your diff; do not report on their absence. Kept despite a match: ${r.kept} files (listed in the plan).${r.vendored ? ` ${r.vendored} of the excluded files are vendored upstream files gstack verified byte for byte against the pins the base allowlists, each one new on this branch or already vendored at the merge base; the plan's REVIEW_VENDORED_PIN lines name the pins.` : ''}`;
}
/** The selection digest a disclosure line names, or undefined. */
export const disclosureDigest = (line: string | undefined) => line?.match(/^Excluded by base policy \S+ \(selection ([0-9a-f]{64})\)/)?.[1];
Expand Down
Loading
Loading