From 12bd33b648db98b981b480572bec1ac9eae34177 Mon Sep 17 00:00:00 2001 From: Garry Tan Date: Thu, 19 Nov 2026 16:11:34 +0800 Subject: [PATCH] Pull requests that merged a release reuse their earlier test run when the release only regenerated a declared file (GST-3067) (#4483) When a pull request merges a release whose only extra change is a generated file the repo lists under `release_outputs`, its earlier remote test run now counts for it, if the clear command gets `--allow-repo-commands`. Why: gbrain paid one extra full CI run for each such pull request, several a day. Without the flag nothing changes except that the refusal now says to pass it; a hand edit to the generated file, or any other changed file, is still refused. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> --- bin/gstack-pregate | 2 +- ...ear-accepts-regenerated-release-outputs.md | 1 + docs/pregate.md | 11 +- docs/ship-policy.md | 9 +- docs/troubleshooting.md | 8 +- lib/pregate/index.ts | 18 +-- lib/pregate/remote-receipt.ts | 13 +- lib/tree-receipt.ts | 31 +++-- test/pregate-remote-release-output.test.ts | 131 ++++++++++++++++++ test/tree-receipt-release-tool.test.ts | 12 ++ 10 files changed, 206 insertions(+), 30 deletions(-) create mode 100644 changelog.d/pregate-clear-accepts-regenerated-release-outputs.md create mode 100644 test/pregate-remote-release-output.test.ts diff --git a/bin/gstack-pregate b/bin/gstack-pregate index da435de9a..1ec77e829 100755 --- a/bin/gstack-pregate +++ b/bin/gstack-pregate @@ -273,7 +273,7 @@ function cmdClear(root: string, rest: string[]): number { const checked = runs.length || runUrl || bundleLane ? buildRunCheck(root, lane, rest.includes('--allow-repo-commands')) : undefined; if (checked && 'error' in checked && (runs.length || checked.profiled)) { process.stderr.write(`gstack-pregate clear: ${checked.error}${checked.profiled ? `; ${lane} may have a run profile on the base branch, so it clears only with a verified --run` : ''}\n`); return EXIT.fail; } const evidence = checked && !('error' in checked) ? checked : undefined; - const decision = clearDecision(read.file, lane, { runUrl, runs, bundleLane, receiptPath: flag(rest, '--receipt'), evidence, flaky }, root); + const decision = clearDecision(read.file, lane, { runUrl, runs, bundleLane, receiptPath: flag(rest, '--receipt'), evidence, flaky, allowRepoCommands: rest.includes('--allow-repo-commands') }, root); if ('refused' in decision) { if (decision.refused === 'usage') { process.stderr.write(`gstack-pregate clear: ${decision.message}\n`); return EXIT.usage; } process.stderr.write(`gstack-pregate clear: receipt refused — ${decision.message}; fix: ${decision.fix ?? RESULT_CODES.PREGATE_RECEIPT_INVALID.fix} ${resultCodeSuffix('PREGATE_RECEIPT_INVALID')}\n`); diff --git a/changelog.d/pregate-clear-accepts-regenerated-release-outputs.md b/changelog.d/pregate-clear-accepts-regenerated-release-outputs.md new file mode 100644 index 000000000..2035e32ac --- /dev/null +++ b/changelog.d/pregate-clear-accepts-regenerated-release-outputs.md @@ -0,0 +1 @@ +- **`gstack-pregate clear --receipt` accepts a remote gate run from before a merged release when the release only regenerated a declared release output.** A PR that merged a release commit was refused because a generated file the policy lists in `release_outputs` was checked against the `stamp_files` list first, and `clear` never reran the release tool the way the tree receipt does. A `release_outputs` path is now judged by the release-output rule, and with `--allow-repo-commands` `clear` reruns `release_tool` on the head and accepts an output it regenerates byte for byte. Without the flag the receipt is refused as before, and the refusal now says to pass it. A hand edit to the generated file, or any path outside `release_outputs`, is still refused. diff --git a/docs/pregate.md b/docs/pregate.md index 7028612b0..d9858f862 100644 --- a/docs/pregate.md +++ b/docs/pregate.md @@ -663,7 +663,7 @@ receipt for H when the two trees differ only as a stamp does: `version-manifest`, `manifest-version`, `lockfile-root-version`, `changelog-heading`, `changelog-fragments` for the CHANGELOG under the fold, `changelog_fragments` for a folded fragment's deletion, `stamp_files`, - `stamp_paths`, `release_outputs`). `clear` prints the same + `stamp_paths`, `release_outputs`, `release_tool`). `clear` prints the same as `PREGATE_RECEIPT_SUBSTITUTED: …`, `verify` repeats it on its ok line, and the ship receipt carries the receipt string. The receipt's own tests and failed checks apply as before (its exit status is checked only when @@ -687,7 +687,14 @@ receipt for H when the two trees differ only as a stamp does: `*_KEY_VERSION` written as the next minor, #3512) (so two trees at the same version refuse such a hunk as `other`), while the built-in version-file, manifest, lockfile and CHANGELOG rules compare content and do - not check the direction of the version move. The obligation stays uncleared; rerun + not check the direction of the version move. A `release_outputs` file that is + more than those rewrites, such as a generated file a merged release commit + added rows to, is a stamp, rule `release_tool`, when the policy's + `release_tool`, rerun on pregate.json's head, writes it byte for byte (the + tree receipt's rule, GST-1176, GST-3067). The tool is repo code, so `clear` + reruns it only with `--allow-repo-commands`; without the flag the file is + refused and the refusal says to pass it. A `release_outputs` path is judged + by this rule even when `stamp_files` does not list it. The obligation stays uncleared; rerun the gate on the head, or make the extra change a stamp under the repo's rule. diff --git a/docs/ship-policy.md b/docs/ship-policy.md index e409604bf..f98e418ae 100644 --- a/docs/ship-policy.md +++ b/docs/ship-policy.md @@ -3012,9 +3012,12 @@ rule. The tool is repo code, so the receipt reruns it only with each stamped prefix; without the flag the file stays `changed`. The rerun is the Version Gate's: the stamped head (CHANGELOG folded, fragments deleted), a bare environment and a 120-second limit, so an output that depends on anything -else fails closed as `changed`. With `stamp_files` set, a file outside it -is `changed (stamp_files: not in the repo's stamp_files allowlist)` whatever -its diff. Any other byte is a change. A diff git cannot produce between the two +else fails closed as `changed`. `gstack-pregate clear --receipt +--allow-repo-commands` applies the same rule to a remote gate receipt from a +predecessor tree (GST-3067). With `stamp_files` set, a file outside it and +outside `release_outputs` is `changed (stamp_files: not in the repo's +stamp_files allowlist)` whatever its diff; a `release_outputs` path is judged +by the release-output rule above. Any other byte is a change. A diff git cannot produce between the two heads fails closed with `EVIDENCE_IDENTITY_UNKNOWN` and git's reason; it never reads as no change. diff --git a/docs/troubleshooting.md b/docs/troubleshooting.md index 883af2f7a..7a2fc0cb7 100644 --- a/docs/troubleshooting.md +++ b/docs/troubleshooting.md @@ -4803,13 +4803,17 @@ it refuses `caller_job_skipped` when its ` / ` job did no Another tree is accepted in exactly one case: it differs from `pregate.json`'s tree only in the repo's stamp rule (`.gstack/ship-policy.json` on the base -branch: `stamp_files`, `stamp_patterns`, the built-in version, manifest, -lockfile and CHANGELOG forms), judged by the tree receipt's own classifier on a +branch: `stamp_files`, `stamp_patterns`, `release_outputs`, the built-in +version, manifest, lockfile and CHANGELOG forms), judged by the tree receipt's own classifier on a `git diff` of the two trees ([the gate ran ahead of the stamp](pregate.md#remote-gate-stamp-only)). The refusal line then says `and the trees differ beyond the stamp rule of : (: , line )`, and the path it names is the one that is not a stamp: a file outside `stamp_files`, or a stamp file with a hunk that is not the old → new rewrite. +A `release_outputs` file that is more than that rewrite is a stamp (rule +`release_tool`) only when `release_tool`, rerun on the head, writes it byte for +byte; `clear` reruns it only with `--allow-repo-commands`, and without the flag +the refusal's reason includes `release_tool reruns only under --allow-repo-commands`. `the gated tree is not an object in this repository` means the gated commit is not fetched here, so the two trees cannot be compared. Three more refusals close the substitution without classifying anything: `lists the test diff --git a/lib/pregate/index.ts b/lib/pregate/index.ts index 4f4e258bd..0faea9d63 100644 --- a/lib/pregate/index.ts +++ b/lib/pregate/index.ts @@ -128,7 +128,7 @@ function carryGate(ctx: PregateContext, file: PregateFile): string | null { const check = ctx.fromRun.check as RunCheck; if (!check.gateRuns) return null; const view: PregateFile = { ...file, requires_remote: file.requires_remote!.map(r => (r === gate ? { ...r, tests: [...new Set([...carried, ...r.tests])] } : r)) }; - const d = clearByRuns(view, REMOTE_GATE_LANE, [ctx.fromRun.ref], check, ctx.repoRoot); + const d = clearByRuns(view, REMOTE_GATE_LANE, [ctx.fromRun.ref], check, ctx.repoRoot, undefined, ctx.allowRepoCommands); if (!('receipt' in d)) return `CI_CARRY: ${REMOTE_GATE_LANE} stays uncleared (${d.message})`; clearObligation(file, REMOTE_GATE_LANE, d.receipt, 'ci-carry', d.substitution); return `CI_CARRY: cleared ${REMOTE_GATE_LANE} ${d.receipt}`; @@ -275,7 +275,7 @@ function userPolicyChange(file: PregateFile): string | null { return `${absent} now holds a user-owned policy, and pregate.json was recorded under the defaults; rerun gstack-pregate`; } -export interface ClearInput { runUrl?: string; bundleLane?: string; receiptPath?: string; runs?: string[]; evidence?: RunCheck; flaky?: FlakyInput } +export interface ClearInput { runUrl?: string; bundleLane?: string; receiptPath?: string; runs?: string[]; evidence?: RunCheck; flaky?: FlakyInput; allowRepoCommands?: boolean } /** What a run clear needs: the base branch's run profiles, the verifier's dependencies, the clock, and the remote gate's inventory. */ export interface RunCheck { repo: string; deps: RunDeps; profiles: Record; gateRuns: RemoteGateRuns | null; now: Date; unchanged?: (p: RunProfile) => string | null; docsOnly?: (head: string) => boolean; inventory: (command: string, tree: string, selected?: string[]) => string[] | { error: string }; failures?: FailureDeps } export type ClearDecision = { receipt: string; attested: boolean; substitution?: PregateReceiptSubstitution; exception?: PregateException } | { refused: 'usage' | 'PREGATE_RECEIPT_INVALID'; message: string; fix?: string }; @@ -313,10 +313,10 @@ export function clearDecision(file: PregateFile, lane: string, input: ClearInput const refs = [...(input.runs ?? []), ...(input.runUrl ? [input.runUrl] : [])]; if (!refs.length) return { refused: 'usage', message: '--flaky-exception needs --run : an exception reads GitHub Actions runs' }; if (!input.flaky.pr) return { refused: 'usage', message: '--flaky-exception needs --pr , the pull request the grant binds' }; - return clearByRuns(file, lane, refs, input.evidence, repoRoot, { ...input.flaky, cases }); + return clearByRuns(file, lane, refs, input.evidence, repoRoot, { ...input.flaky, cases }, input.allowRepoCommands); } const profiled = input.evidence && (lane === REMOTE_GATE_LANE ? input.evidence.gateRuns : input.evidence.profiles[lane]); - if (input.runs?.length || (input.runUrl && profiled)) return clearByRuns(file, lane, [...(input.runs ?? []), ...(input.runUrl ? [input.runUrl] : [])], input.evidence, repoRoot); + if (input.runs?.length || (input.runUrl && profiled)) return clearByRuns(file, lane, [...(input.runs ?? []), ...(input.runUrl ? [input.runUrl] : [])], input.evidence, repoRoot, undefined, input.allowRepoCommands); if (input.bundleLane && profiled && lane !== REMOTE_GATE_LANE) return { refused: 'usage', message: `${lane} has a run profile on the base branch; clear it with --run , which verifies the run` }; if (target.lane === REMOTE_GATE_LANE) { if (!input.receiptPath) return { refused: 'usage', message: `${lane} clears only with --receipt holding the remote gate's PREGATE_REMOTE: tree=… tests=… failed=… line (a run URL is not checkable); or rerun gstack-pregate --remote` }; @@ -329,7 +329,7 @@ export function clearDecision(file: PregateFile, lane: string, input: ClearInput if (!report) return { refused: 'PREGATE_RECEIPT_INVALID', message: `${input.receiptPath} has no PREGATE_REMOTE: line; ${lane} stays uncleared` }; const own = remoteReportProblem(report, report.tree); if (own) return { refused: 'PREGATE_RECEIPT_INVALID', message: `${own}; ${lane} stays uncleared` }; - const sub = treeSubstitution(file, report.tree, lane, repoRoot); + const sub = treeSubstitution(file, report.tree, lane, repoRoot, input.allowRepoCommands); if ('refused' in sub) return sub; const substitution = sub.substitution; const problem = remoteReportProblem(report, substitution ? report.tree : file.tree); @@ -381,13 +381,13 @@ export function clearDecision(file: PregateFile, lane: string, input: ClearInput } /** The stamp-only substitution that lets a receipt for `gatedTree` clear pregate.json's tree, none when the trees are equal, or the refusal. */ -function treeSubstitution(file: PregateFile, gatedTree: string, lane: string, repoRoot: string): { substitution?: PregateReceiptSubstitution } | Extract { +function treeSubstitution(file: PregateFile, gatedTree: string, lane: string, repoRoot: string, allowRepoCommands = false): { substitution?: PregateReceiptSubstitution } | Extract { if (gatedTree === file.tree) return {}; // The stamp rule is the ship's own base branch's policy (origin/HEAD, as regate reads it), never a branch the artifact names. const base = resolveBaseBranch(repoRoot); const recorded = baseBranchOf(file.base); if (recorded && recorded !== base) return { refused: 'PREGATE_RECEIPT_INVALID', message: `${treeMismatch(gatedTree, file.tree)}; pregate.json was recorded against origin/${recorded}, not this repository's base origin/${base}, so the stamp rule that would class the difference is not the base's; ${lane} stays uncleared`, fix: baseMismatchFix(repoRoot, base, recorded) }; - const s = substituteStampOnlyTree(repoRoot, base, gatedTree, file.tree); + const s = substituteStampOnlyTree(repoRoot, base, gatedTree, file.tree, { allowRepoCommands, head: file.head }); if ('problem' in s) return { refused: 'PREGATE_RECEIPT_INVALID', message: `${s.problem}; ${lane} stays uncleared`, fix: s.fix }; return { substitution: s.substitution }; } @@ -399,7 +399,7 @@ function treeSubstitution(file: PregateFile, gatedTree: string, lane: string, re * does not run (never an inventory file); any other lane needs one verified run of the profile named after it. The run * tree must be pregate.json's tree or a stamp-only predecessor of it, exactly as a gate report's tree must. */ -function clearByRuns(file: PregateFile, lane: string, refs: string[], check: RunCheck | undefined, repoRoot: string, flaky?: FlakyInput & { cases: string[] }): ClearDecision { +function clearByRuns(file: PregateFile, lane: string, refs: string[], check: RunCheck | undefined, repoRoot: string, flaky?: FlakyInput & { cases: string[] }, allowRepoCommands = false): ClearDecision { if (!check) return { refused: 'usage', message: `--run needs the base branch's run_profiles; none could be read` }; const isGate = lane === REMOTE_GATE_LANE; const names = isGate ? check.gateRuns?.profiles ?? [] : check.profiles[lane] ? [lane] : []; @@ -442,7 +442,7 @@ function clearByRuns(file: PregateFile, lane: string, refs: string[], check: Run if (trees.length > 1) return { refused: 'PREGATE_RECEIPT_INVALID', message: `REUSE: refused reason=tree_mismatch — the runs tested different trees (${trees.map(t => t.slice(0, 12)).join(', ')}); ${lane} stays uncleared` }; const off = red && evidence.find(e => e.head_sha !== file.head || e.tree !== file.tree); if (off) return { refused: 'PREGATE_RECEIPT_INVALID', message: `FLAKY_EXCEPTION: refused reason=head_mismatch — run ${off.run_id} attempt ${off.run_attempt} tested head ${off.head_sha.slice(0, 12)} tree ${off.tree.slice(0, 12)}, not pregate.json's head ${(file.head ?? '').slice(0, 12)} tree ${file.tree.slice(0, 12)}; an exception binds the exact head and tree, never a stamp-only predecessor; ${lane} stays uncleared` }; - const sub = treeSubstitution(file, trees[0]!, lane, repoRoot); + const sub = treeSubstitution(file, trees[0]!, lane, repoRoot, allowRepoCommands); if ('refused' in sub) return { ...sub, message: `REUSE: refused reason=tree_not_stamp_only — ${sub.message}` }; const idle = evidence.filter(e => (e.tests ?? e.files.length) === 0); if (idle.length) return { refused: 'PREGATE_RECEIPT_INVALID', message: `REUSE: refused reason=no_tests — ${idle.map(e => `run ${e.run_id} (${e.profile})`).join(', ')} records no tests; ${lane} stays uncleared` }; diff --git a/lib/pregate/remote-receipt.ts b/lib/pregate/remote-receipt.ts index e866ac620..1575cd12f 100644 --- a/lib/pregate/remote-receipt.ts +++ b/lib/pregate/remote-receipt.ts @@ -12,7 +12,8 @@ * `substituteStampOnlyTree` is the one accepted substitution. The gated tree * and the head tree are diffed path by path, and every differing file is * classified by the tree receipt's own rule (lib/tree-receipt.ts - * `classifyFiles`, the changelog_fragments fold included: the repo's `stamp_files` allowlist and `stamp_patterns` + * `classifyFiles` and, under --allow-repo-commands, `regenerateReleaseOutputs`, which reruns release_tool on + * pregate.json's head for a release output that is more than version rewrites, GST-3067; the changelog_fragments fold included: the repo's `stamp_files` allowlist and `stamp_patterns` * templates from `.gstack/ship-policy.json` on the base branch, beside the * built-in version-file, manifest-version, lockfile-root-version and * changelog-heading forms). The receipt is accepted for the head only when @@ -33,7 +34,7 @@ import { spawnSync } from '../spawn-sync'; import type { PregateReceiptSubstitution } from '../headless-artifacts'; import { parseUnifiedDiff, versionAtRef } from '../restamp'; import { isUnavailable, loadPolicyFromBase, SHIP_POLICY_PATH } from '../ship-policy'; -import { classifyFiles } from '../tree-receipt'; +import { classifyFiles, regenerateReleaseOutputs } from '../tree-receipt'; import { treeMismatch } from './checks/lanes'; /** What `clear` records when a remote_gate receipt from a stamp-only predecessor tree is accepted for the head (the pregate.json shape, lib/headless-artifacts.ts). */ @@ -75,7 +76,7 @@ export function baseMismatchFix(repoRoot: string, base: string, recorded: string * problem (opening with the same tree-mismatch clause `remoteReportProblem` * prints) with the fix. */ -export function substituteStampOnlyTree(repoRoot: string, base: string, gatedTree: string, headTree: string): SubstitutionResult { +export function substituteStampOnlyTree(repoRoot: string, base: string, gatedTree: string, headTree: string, opts: { allowRepoCommands?: boolean; head?: string } = {}): SubstitutionResult { const short = (t: string) => t.slice(0, 12); const mismatch = treeMismatch(gatedTree, headTree); const rerun = `rerun the remote gate on tree ${short(headTree)} and pass its output with --receipt, or run gstack-pregate --stage tests --remote --spend-cap --allow-repo-commands`; @@ -103,7 +104,11 @@ export function substituteStampOnlyTree(repoRoot: string, base: string, gatedTre if (diff === null) return { problem: `${mismatch}; git diff ${short(gatedTree)} ${short(headTree)} failed`, fix: rerun }; const inventory = git(repoRoot, ['diff-tree', '-r', '-z', '--name-only', '--no-renames', '--ignore-submodules=none', '--end-of-options', gatedTree, headTree]); if (inventory === null) return { problem: `${mismatch}; git diff-tree ${short(gatedTree)} ${short(headTree)} failed`, fix: rerun }; - const files = classifyFiles(repoRoot, parseUnifiedDiff(diff), gatedTree, headTree, loaded.policy, versionRel, oldV, newV); + // release_tool reruns on a commit (it stages a worktree), so only on pregate.json's head and only when that commit is the recorded tree. + const headCommit = opts.head && OBJECT_ID.test(opts.head) && git(repoRoot, ['rev-parse', '--verify', '--quiet', '--end-of-options', `${opts.head}^{tree}`]) === headTree ? opts.head : null; + const regen = regenerateReleaseOutputs({ repoRoot, files: classifyFiles(repoRoot, parseUnifiedDiff(diff), gatedTree, headTree, loaded.policy, versionRel, oldV, newV), head: headCommit ?? '', policy: loaded.policy, version: newV, run: !!opts.allowRepoCommands && !!headCommit }); + const notRerun = !opts.allowRepoCommands ? 'release_tool reruns only under --allow-repo-commands: rerun this clear with it to accept the output release_tool regenerates byte for byte' : `release_tool not rerun: pregate.json's head ${(opts.head ?? 'none').slice(0, 12)} is not a commit at tree ${short(headTree)}`; + const files = regen.files.map(f => regen.pending.includes(f.path) ? { ...f, residual: `${f.residual}; ${notRerun}` } : f); if (!files.length) return { problem: `${mismatch}; the trees differ but git diff produced no file entry to classify`, fix: rerun }; const expected = new Set(inventory.split('\0').filter(Boolean)); const seen = new Set(files.map(f => f.path)); diff --git a/lib/tree-receipt.ts b/lib/tree-receipt.ts index 831728dfb..fad74cb39 100644 --- a/lib/tree-receipt.ts +++ b/lib/tree-receipt.ts @@ -5,7 +5,7 @@ * gone), classifies the diff hunk by hunk with per-file-type rules, decides * per gate whether its evidence is reusable, and refuses `eligible` when the * recorded predecessor head moved. The repo's stamp rule wins: with - * `stamp_files` set, only those files can be stamps, and `stamp_patterns` + * `stamp_files` set, only those files and the `release_outputs` can be stamps, and `stamp_patterns` * (literal `{version}` templates) join the built-in stamp forms. Tree identity alone is not gate evidence: * the receipt also compares the execution identity the gate ran under (from * a `gstack-evidence bundle`) with the current one. @@ -105,7 +105,7 @@ export function classifyFile(repoRoot: string, f: FileDiff, gated: string, curre if (f.binary) return changed('binary', 'binary content differs'); if (f.modeChanged) return changed('mode', 'file mode changed'); if (f.status !== 'M') return changed('path', `${f.status === 'A' ? 'added' : f.status === 'D' ? 'deleted' : 'renamed'}`); - if (policy.stamp_files.length && !matchesAny(policy.stamp_files, f.path)) return changed('stamp_files', 'not in the repo\'s stamp_files allowlist'); + if (policy.stamp_files.length && !matchesAny(policy.stamp_files, f.path) && !matchesAny(policy.release_outputs, f.path)) return changed('stamp_files', 'not in the repo\'s stamp_files allowlist'); const before = blob(repoRoot, gated, f.path); const after = blob(repoRoot, current, f.path); const base = path.basename(f.path); @@ -138,6 +138,25 @@ export function classifyFile(repoRoot: string, f: FileDiff, gated: string, curre return changed('other', `${f.hunks.length} hunk(s) outside every stamp rule`); } +/** + * GST-1176's rule for a release output that is more than version rewrites (a release tool that renders content) or + * that the stamp adds: it is a stamp, rule `release_tool`, when the policy's release_tool, rerun on `head` at its + * version, writes it byte for byte (the Version Gate's regeneratedAtBase). The tool is repo code, so callers set `run` + * only under --allow-repo-commands; `pending` names the release outputs that rule would have judged but did not run + * for, so the caller can say why. gstack-tree-receipt and gstack-pregate clear both apply it (GST-3067). + */ +export function regenerateReleaseOutputs(o: { repoRoot: string; files: FileVerdict[]; head: string; policy: ShipPolicy; version: string | null; run: boolean }): { files: FileVerdict[]; pending: string[] } { + const rendered = o.policy.release_tool && o.version ? o.files.filter(f => f.verdict === 'changed' && (f.rule === 'release_outputs' || (f.rule === 'path' && f.residual === 'added' && matchesAny(o.policy.release_outputs, f.path)))).map(f => f.path) : []; + if (!rendered.length || !o.run) return { files: o.files, pending: rendered }; + const regen = regeneratedAtBase({ repoRoot: o.repoRoot, head: o.head, baseVersion: blob(o.repoRoot, o.head, 'VERSION') ?? o.version!, tool: o.policy.release_tool!, outputs: rendered }); + const files = o.files.map((f): FileVerdict => { + if (!rendered.includes(f.path)) return f; + if (regen.kind === 'same' || (regen.kind === 'differs' && !regen.files.includes(f.path))) return { path: f.path, rule: 'release_tool', verdict: 'stamp' }; + return { ...f, residual: `${f.residual}; ${regen.kind === 'error' ? regen.reason : `not what release_tool generates at ${o.version}`}` }; + }); + return { files, pending: [] }; +} + export interface ReceiptOptions { repoRoot: string; gated: string; current?: string; policy: ShipPolicy; policyLabel: string; versionRel?: string; /** The base ref the gated head's own lines are read against (the fork-version bound, GST-963); without it every line keeps the old version's bound. */ @@ -185,13 +204,7 @@ export function buildReceipt(o: ReceiptOptions): TreeReceipt | { error: string; } const diffText = d.stdout.replace(/\n$/, ''); const classified = classifyFiles(o.repoRoot, parseUnifiedDiff(diffText), gatedHead, currentHead, o.policy, versionRel, oldV, newV, o.baseRef && oldV ? branchSides(o.repoRoot, o.baseRef, gatedHead, oldV) : undefined); - const rendered = o.regenerate && o.policy.release_tool && newV ? classified.filter(f => f.verdict === 'changed' && (f.rule === 'release_outputs' || (f.rule === 'path' && f.residual === 'added' && matchesAny(o.policy.release_outputs, f.path) && (!o.policy.stamp_files.length || matchesAny(o.policy.stamp_files, f.path))))).map(f => f.path) : []; - const regen = rendered.length ? regeneratedAtBase({ repoRoot: o.repoRoot, head: currentHead, baseVersion: blob(o.repoRoot, currentHead, 'VERSION') ?? newV!, tool: o.policy.release_tool!, outputs: rendered }) : null; - const files = classified.map((f): FileVerdict => { - if (!regen || !rendered.includes(f.path)) return f; - if (regen.kind === 'same' || (regen.kind === 'differs' && !regen.files.includes(f.path))) return { path: f.path, rule: 'release_tool', verdict: 'stamp' }; - return { ...f, residual: `${f.residual}; ${regen.kind === 'error' ? regen.reason : `not what release_tool generates at ${newV}`}` }; - }); + const files = regenerateReleaseOutputs({ repoRoot: o.repoRoot, files: classified, head: currentHead, policy: o.policy, version: newV, run: !!o.regenerate }).files; const residual = files.filter(f => f.verdict === 'changed').map(f => f.path); const stampFiles = files.filter(f => f.verdict === 'stamp').map(f => f.path); const stampDiff = stampFiles.length ? (git(o.repoRoot, ['diff', '--no-color', '--no-ext-diff', gatedHead, currentHead, '--', ...stampFiles]) ?? '') : ''; diff --git a/test/pregate-remote-release-output.test.ts b/test/pregate-remote-release-output.test.ts new file mode 100644 index 000000000..9d9426fa4 --- /dev/null +++ b/test/pregate-remote-release-output.test.ts @@ -0,0 +1,131 @@ +/** + * GST-3067 (GBRA-108, gbrain#9679): a PR whose head merged a release commit differs from the tree the remote gate + * tested in the version files and in a generated file the release regenerated. That file is a declared + * release_outputs path, but `gstack-pregate clear --receipt` refused it through the stamp_files allowlist before the + * release-output rule ran, and never reran release_tool the way the tree receipt does (GST-1176). With + * --allow-repo-commands a release output that release_tool regenerates byte for byte at the head is a stamp; a row the + * PR wrote by hand, a path outside release_outputs, and the same merge without the flag are still refused. + * Synthetic repo only: the release tool renders one row per released CHANGELOG heading. + */ +import { afterEach, describe, expect, test } from 'bun:test'; +import * as fs from 'node:fs'; +import * as path from 'node:path'; +import { EXIT } from '../lib/headless-artifacts'; +import { buildContext } from '../lib/pregate/context'; +import { runPregate } from '../lib/pregate/index'; +import { BASE_PREGATE, cleanup, commit, makeFixtureRepo, readFile, runBin, type FixtureRepo } from './helpers/pregate-fixture'; + +const repos: FixtureRepo[] = []; +afterEach(() => { while (repos.length) cleanup(repos.pop()!); }); + +const LANE = 'pregate.json/remote_gate'; +const PREGATE_FILE = '.gstack/tmp/pregate.json'; +const OUTPUT = 'src/rows.generated.ts'; +const POLICY = { + bump: 'patch', queue_mode: 'stamp-at-merge', changelog: 'CHANGELOG.md', + stamp_files: ['VERSION', 'package.json', 'CHANGELOG.md'], + release_tool: 'node scripts/build-rows.js', release_outputs: [OUTPUT], +}; +const BUILD_ROWS = [ + "const fs = require('fs');", + 'const rows = [];', + 'let v = null;', + "for (const l of fs.readFileSync('CHANGELOG.md', 'utf8').split('\\n')) {", + ' const h = /^## \\[(\\d[^\\]]*)\\]/.exec(l);', + ' if (h) { v = h[1]; continue; }', + " if (l.startsWith('## ')) { v = null; continue; }", + " if (v && l.startsWith('- ')) rows.push(\" { version: '\" + v + \"', change: '\" + l.slice(2) + \"' },\");", + '}', + "fs.writeFileSync('src/rows.generated.ts', 'export const ROWS = [\\n' + rows.map(r => r + '\\n').join('') + '];\\n');", + '', +].join('\n'); +const CHANGELOG_BASE = '# Changelog\n\n## [Unreleased]\n\n- add rows\n\n## [1.0.0] - 2026-11-01\n\n- first\n'; +const CHANGELOG_RELEASED = '# Changelog\n\n## [1.0.1] - 2026-11-18\n\n- add rows\n\n## [1.0.0] - 2026-11-01\n\n- first\n'; +const ROWS_BASE = "export const ROWS = [\n { version: '1.0.0', change: 'first' },\n];\n"; +const ROWS_RELEASED = "export const ROWS = [\n { version: '1.0.1', change: 'add rows' },\n { version: '1.0.0', change: 'first' },\n];\n"; + +/** The PR's real change is the gated head; main then takes a release commit, and the PR merges it. */ +function releaseMergeRepo(): { repo: FixtureRepo; gatedTree: string } { + const repo = makeFixtureRepo({ + prefix: 'gstack-pregate-release-output-', + pregate: { ...BASE_PREGATE, remote_gate: 'node scripts/remote-gate.js', dependencies: { ...BASE_PREGATE.dependencies, 'CHANGELOG.md': [], VERSION: [], [OUTPUT]: [], 'scripts/**': [] } }, + baseFiles: { + '.gstack/ship-policy.json': JSON.stringify(POLICY, null, 2) + '\n', VERSION: '1.0.0\n', 'CHANGELOG.md': CHANGELOG_BASE, + 'scripts/build-rows.js': BUILD_ROWS, [OUTPUT]: ROWS_BASE, + 'scripts/remote-gate.js': "console.log('PREGATE_REMOTE: tree=' + process.env.PREGATE_TREE + ' tests=3 failed=0');\n", + }, + }); + repos.push(repo); + const gated = commit(repo, { 'lib/util.js': 'exports.add = (a, b) => a + b + 0;\n' }, 'the real change (gated)'); + repo.git('checkout', '-q', 'main'); + const pkg = JSON.parse(readFile(repo, 'package.json')); + pkg.version = '1.0.1'; + commit(repo, { VERSION: '1.0.1\n', 'package.json': JSON.stringify(pkg, null, 2) + '\n', 'CHANGELOG.md': CHANGELOG_RELEASED, [OUTPUT]: ROWS_RELEASED }, 'release 1.0.1'); + repo.git('push', '-q', 'origin', 'main'); + repo.git('checkout', '-q', 'feature'); + repo.git('merge', '-q', '--no-edit', 'origin/main'); + return { repo, gatedTree: repo.git('rev-parse', gated + '^{tree}') }; +} + +async function pregateAt(repo: FixtureRepo, stage: 'tests' | 'all' = 'tests') { + const loaded = buildContext({ repoRoot: repo.work, base: 'main', policyFrom: 'base', allowRepoCommands: true, env: { ...process.env, ...repo.env }, progress: () => {}, cores: 4, testBackend: 'local' }); + if (!('ctx' in loaded)) throw new Error('fixture context refused'); + const file = await runPregate(loaded.ctx, { stage }); + expect(file.requires_remote).toEqual([expect.objectContaining({ lane: LANE, receipt: null })]); + fs.mkdirSync(path.join(repo.work, '.gstack/tmp'), { recursive: true }); + fs.writeFileSync(path.join(repo.work, PREGATE_FILE), JSON.stringify(file)); + return file; +} + +const receiptFile = (repo: FixtureRepo, tree: string) => { const f = path.join(repo.root, 'gate.log'); fs.writeFileSync(f, 'gate log\nPREGATE_REMOTE: tree=' + tree + ' tests=3 failed=0\n'); return f; }; +const clear = (repo: FixtureRepo, args: string[]) => runBin(repo, 'gstack-pregate', ['clear', '--remote', LANE, ...args]); +const pregateJson = (repo: FixtureRepo) => JSON.parse(fs.readFileSync(path.join(repo.work, PREGATE_FILE), 'utf8')); + +describe.skipIf(process.platform === 'win32')('clear --receipt over a merged release commit that regenerated a declared release output (GST-3067)', () => { + test('with --allow-repo-commands the regenerated release output is a stamp and the receipt clears', async () => { + const { repo, gatedTree } = releaseMergeRepo(); + const file = await pregateAt(repo, 'all'); + const r = clear(repo, ['--receipt', receiptFile(repo, gatedTree), '--allow-repo-commands']); + expect(r.err).toBe(''); + expect(r.status).toBe(EXIT.ok); + expect(r.out).toContain('PREGATE_RECEIPT_SUBSTITUTED: gated tree ' + gatedTree.slice(0, 12) + ' accepted for tree ' + file.tree.slice(0, 12)); + expect(r.out).toContain(OUTPUT + ' (release_tool)'); + expect(pregateJson(repo).requires_remote[0].substitution.files).toContainEqual({ path: OUTPUT, rule: 'release_tool' }); + const verify = runBin(repo, 'gstack-pregate', ['verify']); + expect(verify.err).toBe(''); + expect(verify.status).toBe(EXIT.ok); + expect(verify.out).toContain(OUTPUT + ' (release_tool)'); + }, 120_000); + + test('without --allow-repo-commands the same merge is refused, and the refusal names the flag', async () => { + const { repo, gatedTree } = releaseMergeRepo(); + await pregateAt(repo); + const r = clear(repo, ['--receipt', receiptFile(repo, gatedTree)]); + expect(r.status).toBe(EXIT.fail); + expect(r.err).toContain(OUTPUT + ' (release_outputs: '); + expect(r.err).toContain('--allow-repo-commands'); + expect(pregateJson(repo).requires_remote[0].receipt).toBeNull(); + }, 120_000); + + test('a row the PR added to the generated file by hand, which release_tool does not write, is refused', async () => { + const { repo, gatedTree } = releaseMergeRepo(); + commit(repo, { [OUTPUT]: ROWS_RELEASED.replace('];', " { version: '1.0.1', change: 'hand-written' },\n];") }, 'a hand-added row'); + await pregateAt(repo); + const r = clear(repo, ['--receipt', receiptFile(repo, gatedTree), '--allow-repo-commands']); + expect(r.status).toBe(EXIT.fail); + expect(r.err).toContain(OUTPUT + ' (release_outputs: '); + expect(r.err).toContain('not what release_tool generates at 1.0.1'); + expect(pregateJson(repo).requires_remote[0].receipt).toBeNull(); + }, 120_000); + + test('a path outside release_outputs is still refused by the stamp_files allowlist', async () => { + const { repo, gatedTree } = releaseMergeRepo(); + commit(repo, { 'lib/retry.js': 'exports.MAX_RETRIES = 4;\nexports.TIMEOUT_MS = 5000;\nexports.retry = (fn) => fn();\n' }, 'a real edit after the gate'); + await pregateAt(repo); + const r = clear(repo, ['--receipt', receiptFile(repo, gatedTree), '--allow-repo-commands']); + expect(r.status).toBe(EXIT.fail); + expect(r.err).toContain("lib/retry.js (stamp_files: not in the repo's stamp_files allowlist, line 1)"); + expect(r.err).not.toContain(OUTPUT + ' ('); + expect(pregateJson(repo).requires_remote[0].receipt).toBeNull(); + }, 120_000); +}); diff --git a/test/tree-receipt-release-tool.test.ts b/test/tree-receipt-release-tool.test.ts index 392860c56..849434935 100644 --- a/test/tree-receipt-release-tool.test.ts +++ b/test/tree-receipt-release-tool.test.ts @@ -78,6 +78,18 @@ describe('tree receipt: a release output release_tool regenerates (GST-1176)', ( expect(r.out).toMatch(/^TREE: same-modulo-stamps$/m); }); + test('with stamp_files set, a release output outside it is judged by the release-output rule, not refused by the allowlist (GST-3067)', () => { + const { repo, merged, stamped } = harn7({ ...POLICY, stamp_files: ['VERSION', 'package.json', 'CHANGELOG.md'] }); + const plain = JSON.parse(receipt(repo, ['--gated', merged, '--current', stamped, '--json']).out); + expect(plain.files.find((f: { path: string }) => f.path === 'TODOS.md')).toMatchObject({ verdict: 'changed', rule: 'release_outputs' }); + const r = receipt(repo, ['--gated', merged, '--current', stamped, '--allow-repo-commands']); + expect(r.out).toMatch(/^ stamp TODOS\.md \(release_tool\)$/m); + expect(r.out).toMatch(/^TREE: same-modulo-stamps$/m); + fs.appendFileSync(path.join(repo.work, 'fold.js'), '// an edit outside both lists\n'); + repo.git('commit', '-qam', 'an edit outside both lists'); + expect(receipt(repo, ['--gated', merged, '--current', 'HEAD', '--allow-repo-commands']).out).toMatch(/^ changed fold\.js \(stamp_files: not in the repo's stamp_files allowlist\)$/m); + }); + test('a release_tool that fails leaves the release output changed, with its exit named', () => { const { repo, merged, stamped } = harn7({ ...POLICY, release_tool: 'node fold.js && exit 3' }); const j = JSON.parse(receipt(repo, ['--gated', merged, '--current', stamped, '--allow-repo-commands', '--json']).out);