diff --git a/changelog.d/receipt-accepts-docs-audit-after-base-merge.md b/changelog.d/receipt-accepts-docs-audit-after-base-merge.md new file mode 100644 index 0000000000..a876e79036 --- /dev/null +++ b/changelog.d/receipt-accepts-docs-audit-after-base-merge.md @@ -0,0 +1 @@ +**`gstack-ship-receipt write` accepts a docs audit that ran before a base merge.** When /ship's docs audit edited a docs file, the branch then merged in its base (bringing new docs files or a new `review_exclude_paths` pattern), and a re-audit at the merged head made no edits, write refused with `RECEIPT_INVALID field=review_exclude` and no documented way out. Write now skips files under the patterns that only the merged base changed, still checks the earlier audits a no-edit re-audit carries, and accepts the reviewed selection when only the base policy moved. A docs file the PR itself changes after the last audit still refuses. (GST-3260) diff --git a/docs/ship-policy.md b/docs/ship-policy.md index f16a01c5c4..50fa3c2c1d 100644 --- a/docs/ship-policy.md +++ b/docs/ship-policy.md @@ -1707,7 +1707,7 @@ recorded by accident, but it does not read the review ledger: a write given the `expected=` digest passes without a review, and only the review run shows that the reviewers read that selection. Write, carry and the queue make one exception (GST-1408, GST-1415, GST-1434, GST-1446): when the digests differ but the recorded selection and HEAD's give the PR's files the same selection, meaning the same applied patterns, the same excluded files and the same kept files with their reasons, write records HEAD's digest, carry carries the receipt and records HEAD's digest, and the queue (`ready`, train build, land) binds it. So a pattern the base gains, adds, removes or reverts that changes nothing about the PR's files does not void the review. No recorded selection (`none`) counts as the empty one. A receipt's record counts only when its digest is built from its list hash. Write gets only a digest, so it rebuilds the recorded selection: the digest binds when the `review_exclude_paths` the base held at some commit that changed the policy file, combined with HEAD's list hash, gives that digest. It prints `REVIEW_EXCLUDE_POLICY_MOVED:`. A digest no such policy gives, an omitted `--review-exclude`, a selection that differs over the PR's files and an unreadable base policy still refuse. -Write makes one exception for Step 14.5's docs audit, which runs after the review (GST-1429): with `--docs-post -post.json` it accepts the reviewed digest when that digest is HEAD's selection with every file under a pattern that changed since the snapshot's HEAD reset to it, each such file is uncommitted in the snapshot with the content HEAD holds, each is excluded or kept only because code names it, and every other file is excluded or kept alike on both sides. With `--docs-pre ` only the files whose content changed between the audit's two snapshots reset, to their Prepare content, so uncommitted docs the reviewers read stay as they are; content compares as git stores it or as it checks out (autocrlf, filters). It prints `REVIEW_EXCLUDE_DOCS_AUDIT:` and records HEAD's digest. Any other change to the selection, an audit file the exclusion keeps for another reason, an edit after the snapshot and a missing or unreadable snapshot still refuse. When one `/ship` run audits twice (a re-audit after a base merge and restamp), every snapshot names the run (`--run `), each post snapshot names its Prepare snapshot (`--pre`, recording the files the audit edited), and the re-audit's post snapshot carries the earlier one (`--carry `; GST-1616). Write then also resets each file an earlier audit of the run edited, to its content before that audit, when HEAD (or the re-audit's Prepare snapshot) holds what the audit left; a file no audit edited still refuses, and a carried audit of another run is refused by the snapshot and by write. +Write makes one exception for Step 14.5's docs audit, which runs after the review (GST-1429): with `--docs-post -post.json` it accepts the reviewed digest when that digest is HEAD's selection with every file under a pattern that changed since the snapshot's HEAD reset to it, each such file is uncommitted in the snapshot with the content HEAD holds, each is excluded or kept only because code names it, and every other file is excluded or kept alike on both sides. With `--docs-pre ` only the files whose content changed between the audit's two snapshots reset, to their Prepare content, so uncommitted docs the reviewers read stay as they are; content compares as git stores it or as it checks out (autocrlf, filters). It prints `REVIEW_EXCLUDE_DOCS_AUDIT:` and records HEAD's digest. Any other change to the selection, an audit file the exclusion keeps for another reason, an edit after the snapshot and a missing or unreadable snapshot still refuse. When one `/ship` run audits twice (a re-audit after a base merge and restamp), every snapshot names the run (`--run `), each post snapshot names its Prepare snapshot (`--pre`, recording the files the audit edited), and the re-audit's post snapshot carries the earlier one (`--carry `; GST-1616). Write then also resets each file an earlier audit of the run edited, to its content before that audit, when HEAD (or the re-audit's Prepare snapshot) holds what the audit left; a file no audit edited still refuses, and a carried audit of another run is refused by the snapshot and by write. After a base merge, write does not count a file under a pattern that only the merged base changed (the PR's own three-dot diff leaves it alone), a re-audit that edited nothing still binds through the audits it carries, and the rebuilt selection binds the reviewed digest even when the merge moved the base policy (for example, added a pattern); a docs file the PR itself changes after the last audit still refuses (GST-3260). **Adopting it.** (1) Open a PR on the base that adds the patterns and `min_gstack_version` at least `REVIEW_EXCLUDE_KEY_VERSION`, and ship it like any diff --git a/docs/troubleshooting.md b/docs/troubleshooting.md index e93a72413b..c954368f3b 100644 --- a/docs/troubleshooting.md +++ b/docs/troubleshooting.md @@ -4141,7 +4141,9 @@ removing files under a pattern, pass that run's `--docs-post -post.jso `--docs-pre` as well instead of rerunning the review; the refusal's `the docs-audit exception does not apply:` clause says why when it cannot ([ship-policy.md](ship-policy.md#review-exclude-paths), GST-1429). When the run audited twice (a re-audit after a base merge), pass the last audit's pair; its post snapshot must carry the earlier audit (`--carry `, GST-1616), or the -earlier audit's files stay in the selection and write refuses as before. +earlier audit's files stay in the selection and write refuses as before. The last audit may have edited nothing: write still binds +through the audits it carries, ignores docs files only the merged base changed, and accepts a base policy that gained a pattern +in that merge (GST-3260). Write, carry and the queue bind a digest that differs (`recorded=none`, written before the base gained `review_exclude_paths`, or a digest from an older policy) when the recorded selection and diff --git a/lib/ship-receipt-depth.ts b/lib/ship-receipt-depth.ts index 66128dca07..65b32416f7 100644 --- a/lib/ship-receipt-depth.ts +++ b/lib/ship-receipt-depth.ts @@ -345,7 +345,10 @@ const treatment = (x: ActiveExclusion, p: string) => (x.excluded.has(p) ? 'exclu * them left. When the whole carried list does not rebuild the reviewed selection, write retries without the oldest carried audits, * one at a time down to none (a review that ran between two audits already read the earlier one's edits); only that digest * mismatch retries, so an integrity refusal (an edit after an audit, a file changed between audits, a foreign HEAD) ends it, and - * the refusal names the whole list's reason. Content compares as git stores it or as it checks out (autocrlf, filters). Anything else, an unreadable + * the refusal names the whole list's reason. A file the merged base changed since an audit's HEAD and the PR's own three-dot diff + * leaves alone holds no PR edit, so it is not counted as changed; a latest audit that changed nothing still binds through the audits + * it carries; and the rebuilt selection binds the reviewed digest as carryExcludeBinds does, so a base policy change alone does not + * void it (GST-3260). Content compares as git stores it or as it checks out (autocrlf, filters). Anything else, an unreadable * snapshot included, refuses with `why`. */ export function writeExcludeBinds(root: string, base: string, head: string, review: { exclude: Exclusion; tagged: ReadonlySet | null }, flagged: string | undefined, docsPost: string | undefined, docsPre?: string): { ok: true; line?: string } | { ok: false; why?: string } { @@ -374,10 +377,13 @@ export function writeExcludeBinds(root: string, base: string, head: string, revi if (earlier.length && !pre) return no(`it carries audit ${earlier.map(e => e.audit_id).join(',')}, so write needs --docs-pre `); const at = post.head; if (!git(root, ['merge-base', '--is-ancestor', at, head]).ok) return no(`its HEAD ${short(at)} is not ${short(head)} or an ancestor of it`); - const since = (rev: string) => { const d = spawnSync('git', ['diff', '--name-only', '-z', '--no-renames', rev, head, '--', ...x.record.patterns.map(p => `:(top,glob)${p}`)], { cwd: root, encoding: 'utf8', timeout: 60_000 }); return d.status === 0 ? d.stdout.split('\0').filter(Boolean).sort() : null; }; + const { ref, sha } = writeBase(root, base); + const names = (range: string[]) => { const d = spawnSync('git', ['diff', '--name-only', '-z', '--no-renames', ...range, '--', ...x.record.patterns.map(p => `:(top,glob)${p}`)], { cwd: root, encoding: 'utf8', timeout: 60_000 }); return d.status === 0 ? d.stdout.split('\0').filter(Boolean).sort() : null; }; + const own = names([`${sha ?? ref}...${head}`]); + const since = (rev: string) => { const moved = names([rev, head]); const merged = names([`${rev}...${sha ?? ref}`]); return moved && merged && own ? moved.filter(p => own.includes(p) || !merged.includes(p)) : null; }; const changed = since(at); if (!changed) return no(`git could not list the files under review_exclude_paths that changed since ${short(at)}`); - if (!changed.length) return no(`no file under review_exclude_paths changed since its HEAD ${short(at)}, so the docs audit did not change the selection`); + if (!changed.length && !earlier.length) return no(`no file under review_exclude_paths changed since its HEAD ${short(at)}, so the docs audit did not change the selection`); const blob = (rev: string, p: string) => { const b = git(root, ['rev-parse', '--verify', '--quiet', `${rev}:${p}`]); return b.ok ? b.out : null; }; const holds = (rev: string, p: string, want: unknown) => { const id = blob(rev, p); @@ -411,7 +417,7 @@ export function writeExcludeBinds(root: string, base: string, head: string, revi if (!kept) return no(`${p} at ${auditFiles.includes(p) ? `this audit's Prepare snapshot` : 'HEAD'} is not the content audit ${audit} recorded (an edit after that audit)`); } const files = [...resets.keys()].sort(); - if (!files.length) return no(`--docs-pre ${docsPre}: the audit changed none of ${changed.join(',')}, so it did not change the selection`); + if (!files.length) return no(`--docs-pre ${docsPre}: the audit changed none of ${changed.join(',') || 'the files under review_exclude_paths'}, so it did not change the selection`); const gitDir = git(root, ['rev-parse', '--absolute-git-dir']).out; const index = path.join(gitDir, `gstack-receipt-${process.pid}-${randomBytes(4).toString('hex')}.index`); const env = { ...process.env, GIT_INDEX_FILE: index, GIT_AUTHOR_NAME: 'gstack-ship-receipt', GIT_AUTHOR_EMAIL: 'ship-receipt@gstack.local', GIT_COMMITTER_NAME: 'gstack-ship-receipt', GIT_COMMITTER_EMAIL: 'ship-receipt@gstack.local', GIT_AUTHOR_DATE: '2000-01-01T00:00:00Z', GIT_COMMITTER_DATE: '2000-01-01T00:00:00Z' }; @@ -432,10 +438,9 @@ export function writeExcludeBinds(root: string, base: string, head: string, revi if (!c.ok) return no(`git could not build HEAD with ${files.join(',')} reset to before the audit`); reset = c.out; } finally { fs.rmSync(index, { force: true }); } - const { ref, sha } = writeBase(root, base); const before = exclusionAt(root, ref, sha, reset, collectCommit(root, sha ?? ref, reset), review.tagged, 'gstack-ship-receipt'); if (before.state !== 'active') return no(`the selection with ${files.join(',')} reset to before the audit cannot be read`); - if (before.record.digest !== given) return { retry: true, ...no(`the reviewed digest ${short(given)} is not the selection HEAD has with the files under review_exclude_paths that changed since the snapshot reset to ${pre ? 'their --docs-pre content' : short(at)}${left.size ? ` and the files audit ${carried.map(e => e.audit_id).join(',')} edited reset to before that audit` : ''} (${short(before.record.digest)}), so more than the docs audit changed what the reviewers read`) }; + if (!carryExcludeBinds(before, { review_exclude: reviewedRecord(root, base, before, given) })) return { retry: true, ...no(`the reviewed digest ${short(given)} is not the selection HEAD has with the files under review_exclude_paths that changed since the snapshot reset to ${pre ? 'their --docs-pre content' : short(at)}${left.size ? ` and the files audit ${carried.map(e => e.audit_id).join(',')} edited reset to before that audit` : ''} (${short(before.record.digest)}), so more than the docs audit changed what the reviewers read`) }; for (const p of files) for (const [side, s] of [['the review', before], ['HEAD', x]] as const) { const t = treatment(s, p); if (!['absent', 'excluded', 'named-by-code'].includes(t)) return no(`${p} is ${t} at ${side}: the docs audit binds only docs the exclusion hides or keeps because code names them`); diff --git a/test/review-exclude.test.ts b/test/review-exclude.test.ts index 703ec4661f..e3f3431007 100644 --- a/test/review-exclude.test.ts +++ b/test/review-exclude.test.ts @@ -904,6 +904,55 @@ describe('the receipt binds the selection the reviewers read', () => { refusedWith(writeWith(again), again, "docs/guide.md at this audit's Prepare snapshot is not the content audit a1 recorded"); binds(twoAudits({ a1: { 'docs/x.md': '# x\n' }, rereview: true }), 'docs/new.md', ''); }, 480_000); + + // Value: protects=a re-audit that edits nothing at a base-merge head binds the reviewed digest through the audit it carries, + // while a docs file the PR itself changes after that re-audit still refuses (GST-3260: gbrain #9975 refused RECEIPT_INVALID + // field=review_exclude after audit a1, a base merge that added docs files and moved the policy from 5 to 6 patterns, and a + // no-edit re-audit carrying a1, with no documented way out); fails_when=write stops at the empty re-audit without trying the + // carried audit, reads a docs file only the base merge changed as an unnamed audit file, compares the rebuilt selection only + // against the moved policy's digest, or drops a docs file the PR changed after the last audit; why_new=the GST-1616 rows + // merge a base that adds no file under a pattern, keep the policy, and end on a re-audit with edits; seam=gstack-docs-candidate + // snapshot --run --pre --carry, then gstack-ship-receipt write --review-exclude --docs-post --docs-pre; no_claim=a live /ship run + test('the #9975 shape: a no-edit re-audit after a base merge that adds docs files and a pattern binds through the carried audit', () => { + const FIVE = ['docs/**', 'test/fixtures/goldens/**', 'receipts/**', 'data/**', 'vendor/**']; + const merged = (after: Record, reaudit = true) => { + const repo = makeFixtureRepo({ prefix: 'gstack-exclude-merged-', baseFiles: { '.gstack/ship-policy.json': policyText(FIVE), 'docs/guides/setup.md': '# setup\n', 'lib/index.js': "exports.k = 'docs/key-files.md';\n" } }); + repos.push(repo); + commit(repo, BRANCH); + const reviewed = JSON.parse(plan(repo, ['--json']).out).review_exclude.digest as string; + const snapshot = (id: string, out: string, extra: string[] = []) => { + const file = path.join(repo.root, out); + const s = runBin(repo, 'gstack-docs-candidate', ['snapshot', '--out', file, '--audit-id', id, '--mode', 'edit', '--base', repo.git('rev-parse', 'origin/main'), '--docs', 'docs', '--run', RUN, ...extra]); + expect({ status: s.status, err: s.err }).toMatchObject({ status: 0 }); + return file; + }; + const pre1 = snapshot('a1', 'a1.json'); + write(repo.work, 'docs/guides/setup.md', '# setup\n\nUpdated by the docs audit.\n'); + const post1 = snapshot('a1', 'a1-post.json', ['--pre', pre1]); + commit(repo, {}, 'docs audit a1'); + onBase(repo, { '.gstack/ship-policy.json': policyText([...FIVE, 'assets/**']), 'docs/architecture/key-files/commands-2-continued.md': '# commands, continued\n' }); + const pre = reaudit ? snapshot('a2', 'a2.json') : pre1; + const post = reaudit ? snapshot('a2', 'a2-post.json', ['--pre', pre, '--carry', post1]) : post1; + if (Object.keys(after).length) commit(repo, after, 'docs edit after the re-audit'); + return { repo, reviewed, pre, post, body: path.join(repo.root, 'body.md') }; + }; + const writeWith = (a: ReturnType) => writeReceipt(a.repo, a.body, ['--review-exclude', a.reviewed, '--docs-post', a.post, '--docs-pre', a.pre, ...targetedAtHead(a.repo)]); + const a = merged({}); + const w = writeWith(a); + expect({ status: w.status, err: w.status === EXIT.ok ? '' : w.err }).toEqual({ status: EXIT.ok, err: '' }); + expect(w.err + w.out).toContain(`REVIEW_EXCLUDE_DOCS_AUDIT: reviewed=${a.reviewed.slice(0, 12)} `); + expect(w.err + w.out).toContain('files=docs/guides/setup.md audits=a1,a2 ('); + expect(receiptIn(a.body).review_exclude!.digest).not.toBe(a.reviewed); + const late = merged({ 'docs/parity.md': '# parity\nedited after the re-audit\n' }); + const refused = writeWith(late); + expect({ status: refused.status, written: fs.existsSync(late.body) }).toEqual({ status: EXIT.fail, written: false }); + expect(refused.err).toContain(`recorded=${late.reviewed} `); + expect(refused.err).toContain("docs/parity.md changed after the snapshot's HEAD"); + const both = merged({ 'docs/architecture/key-files/commands-2-continued.md': '# commands, continued\nedited by the PR after the base merge\n' }, false); + const refusedBoth = writeWith(both); + expect({ status: refusedBoth.status, written: fs.existsSync(both.body) }).toEqual({ status: EXIT.fail, written: false }); + expect(refusedBoth.err).toContain("docs/architecture/key-files/commands-2-continued.md changed after the snapshot's HEAD"); + }, 360_000); }); });