From b79843c5d7021c870dc31d7bd9c3eadbfdf95bf2 Mon Sep 17 00:00:00 2001 From: Garry Tan Date: Tue, 17 Nov 2026 22:10:23 +0800 Subject: [PATCH] Keep a pull request's review count intact when a posted description brings its own run history (GST-2707) (#4440) When `/ship` posts a pull request's description, the count of earlier review rounds now always comes from the PR's real history. Before this fix, a description that brought extra history blocks of its own could make gstack count one round where there were three. Why: the per-PR round cap (R51) depends on that count, and it's the last gap before crews can stop counting rounds by hand. Nothing to do as a reader. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> Review-Finding: p1/specialist-batched#1 reader=red-team Review-Finding: p1/specialist-batched#2 reader=testing --- changelog.d/strip-incoming-receipt-runs.md | 1 + docs/review-policy.md | 4 ++- lib/gstack-post.ts | 27 ++++++++++++----- test/pr-review-history.test.ts | 35 ++++++++++++++++++++++ 4 files changed, 59 insertions(+), 8 deletions(-) create mode 100644 changelog.d/strip-incoming-receipt-runs.md diff --git a/changelog.d/strip-incoming-receipt-runs.md b/changelog.d/strip-incoming-receipt-runs.md new file mode 100644 index 000000000..05324cf8e --- /dev/null +++ b/changelog.d/strip-incoming-receipt-runs.md @@ -0,0 +1 @@ +- **A PR's review-round count can no longer be overwritten by the body `/ship` posts.** When `/ship` posted a PR body whose Agent notes held their own `gstack-receipt-runs` blocks, gstack removed only the first one, so a second block placed before the receipt became the history `gstack-ship-receipt history` counted (one round where there were three). A post now drops every runs block it brings and keeps exactly one. That one is the live body's history, placed right after the posted receipt, or none when the PR had no earlier runs. A post whose notes hold no receipt yet keeps the live started stubs in place of the runs block it brings. See docs/review-policy.md#rounds-per-pull-request. diff --git a/docs/review-policy.md b/docs/review-policy.md index 79ceb2c37..f38b993b1 100644 --- a/docs/review-policy.md +++ b/docs/review-policy.md @@ -159,7 +159,9 @@ caught on another crew's PRs. run still reviews its head in full; what's lost is the cap on earlier runs, so check the PR body by hand when that line shows. A `/ship` body post keeps the live `gstack-receipt-runs` block (`lib/gstack-post.ts`), so - earlier runs and started stubs stay countable. + earlier runs and started stubs stay countable. Any runs block the posted + body brings in its own Agent notes is dropped first, so only the live + history is counted. 2. **Runs that stop count too.** After each Step 9 pass, `gstack-ship-receipt stub --pr --run --review-loop ` writes a `status: started` entry, so a run that stops before its receipt still counts. The run's receipt diff --git a/lib/gstack-post.ts b/lib/gstack-post.ts index 425979041..fd56b04da 100644 --- a/lib/gstack-post.ts +++ b/lib/gstack-post.ts @@ -36,7 +36,7 @@ import { CI_RUN_WORD_BEFORE } from "./redact-patterns"; import { agentNotesLines, splitAgentNotes } from "./agent-notes"; import { checkTitle, loadPlainTitlePolicy, PLAIN_TITLE_POLICY_PATH, type PlainTitlePolicy } from "./plain-title"; import { bodyFindingLine, checkBodyShape, foldSections, unfoldSections } from "./pr-body-shape"; -import { RECEIPT_FENCE, RUNS_FENCE, extractReceipt, placeReceipt, renderReceiptBlock, validateReceipt } from "./ship-receipt"; +import { RECEIPT_FENCE, RUNS_FENCE, RUNS_RE, extractReceipt, placeReceipt, renderReceiptBlock, validateReceipt } from "./ship-receipt"; import type { ShipReceipt } from "./headless-artifacts"; import { canonicalRemote } from "./remote-identity"; import { parseChangelog } from "./changelog-check"; @@ -300,20 +300,33 @@ export function mergeBodySections(current: string, incoming: string): { body: st const RUNS_BLOCK_RE = new RegExp("^```" + RUNS_FENCE + "[ \\t]*\\r?\\n[\\s\\S]*?\\r?\\n```[ \\t]*(?:\\r?\\n|$)", "m"); +/** Every runs block the history reader (lib/ship-receipt.ts receiptRuns) would recognize, not only one at a line start. */ +const READER_RUNS_RE = new RegExp(RUNS_RE.source, "g"); + /** - * A /ship body post (an Agent notes section holding a receipt) keeps the live body's run history (GST-2706): the merge - * replaces the notes, so the runs block placeReceipt would write on the live body (earlier runs, started stubs and the - * replaced receipt) goes back right after the posted receipt, and gstack-ship-receipt history still counts every run. + * A post that brings its own Agent notes section keeps the live body's run history and none of the file's (GST-2706, + * GST-2707): the merge replaces the notes, so every runs block the file's notes hold is removed, since the history + * reader takes the first one it finds, and a block placed before the receipt would stand in for the real history. The + * runs block placeReceipt would write on the live body (earlier runs, started stubs and the replaced receipt) then goes + * back, once, right after the posted receipt; with no live history there is none, and gstack-ship-receipt history + * counts the posted receipt alone. Notes with no receipt (a body between a Step 9 stub and the receipt write) get the + * live body's runs block in place of the first block they bring, so their stubs are kept and never replaced. */ function keepRuns(current: string, next: Section[], m: ReturnType): ReturnType { + if (!next.some(n => n.key === NOTES_KEY)) return m; const posted = next.some(n => n.key === NOTES_KEY && extractReceipt(n.text).found) ? extractReceipt(m.body).receipt : undefined; const notesAt = (body: string) => splitAgentNotes(body).notes === null ? 0 : splitAgentNotes(body).readable.length; const placed = posted ? placeReceipt(current, posted) : ""; const runs = RUNS_BLOCK_RE.exec(placed.slice(notesAt(placed)))?.[0]; const at = notesAt(m.body); - const notes = m.body.slice(at).replace(RUNS_BLOCK_RE, ""); - const end = runs && /```gstack-ship-receipt[ \t]*\r?\n[\s\S]*?\r?\n```[ \t]*(?:\r?\n|$)/.exec(notes); - return end ? { ...m, body: m.body.slice(0, at) + notes.slice(0, end.index + end[0].length) + "\n" + runs + notes.slice(end.index + end[0].length) } : m; + if (!posted) { + const live = RUNS_RE.exec(current.slice(notesAt(current)))?.[0] ?? ""; + let first = true; + return { ...m, body: m.body.slice(0, at) + m.body.slice(at).replace(READER_RUNS_RE, () => first ? (first = false, live) : "") }; + } + const notes = m.body.slice(at).replace(READER_RUNS_RE, ""); + const end = runs ? /```gstack-ship-receipt[ \t]*\r?\n[\s\S]*?\r?\n```[ \t]*(?:\r?\n|$)/.exec(notes) : null; + return { ...m, body: m.body.slice(0, at) + (end ? notes.slice(0, end.index + end[0].length) + "\n" + runs + notes.slice(end.index + end[0].length) : notes) }; } function mergeSplit(now: Section[], next: Section[], incoming: string): { body: string; replaced: string[]; kept: string[]; added: string[]; dropped: string[] } { diff --git a/test/pr-review-history.test.ts b/test/pr-review-history.test.ts index 7035d75bf..3f5e83918 100644 --- a/test/pr-review-history.test.ts +++ b/test/pr-review-history.test.ts @@ -85,6 +85,41 @@ describe('prReviewHistory', () => { expect(prReviewHistory(mergeBodySections(posted, composed).body)).toEqual(prReviewHistory(posted)); }); + // Value: protects=the PR's review count from a posted body whose notes carry their own runs blocks: the reader takes the + // first block it finds, so one placed before the receipt would replace the real history (Sol audit of #4365, GST-2707); + // fails_when=keepRuns removes only the first incoming block, or returns the file's history when the live body has none; + // seam=none (mergeBodySections); no_claim=the gh round trip + test('a /ship body post drops every runs block it brings and keeps only the live history (GST-2707)', () => { + const fake = (c: string) => '```gstack-receipt-runs\n' + JSON.stringify({ head: sha(c), run: `fake-${c}`, review_loop: loop({ passes: 0, rounds: 0 }) }) + '\n```\n'; + const composed = placeReceipt('## Summary\nnew prose\n', receipt(sha('c'), { passes: 1, rounds: 1 }, { run: 'r2' })); + const injected = composed.replace('```gstack-ship-receipt', fake('d') + '\n' + fake('e') + '\n```gstack-ship-receipt'); + expect(receiptRuns(injected).map(r => r.run)).toEqual(['fake-d']); + let live = placeReceipt('## Summary\nprose\n', receipt(sha('a'), { passes: 1, rounds: 1 }, { run: 'r1' })); + live = placeRunEntry(live, { head: sha('b'), run: 'r3', status: 'started', review_loop: loop({ passes: 1, rounds: 1 }) }); + const posted = mergeBodySections(live, injected).body; + expect(prReviewHistory(posted)).toMatchObject({ full: 3, runs: 2, stubs: 1, last_full: sha('c') }); + expect(receiptRuns(posted).map(r => r.run)).toEqual(['r3', 'r1']); + expect(posted.match(/```gstack-receipt-runs/g)).toHaveLength(1); + expect(prReviewHistory(mergeBodySections(posted, injected).body)).toEqual(prReviewHistory(posted)); + const fresh = mergeBodySections('## Summary\nprose\n', injected).body; + expect(fresh).not.toContain('gstack-receipt-runs'); + expect(prReviewHistory(fresh)).toMatchObject({ full: 1, runs: 1, stubs: 0 }); + }); + + // Value: protects=a PR's started stubs when a post's Agent notes carry no receipt yet (the body between a Step 9 stub and + // the receipt write); fails_when=keepRuns drops the live runs block, or lets a block the file brings stand in for it; + // seam=none (mergeBodySections); no_claim=the gh round trip + test('a post whose notes hold no receipt keeps the live stubs and none of its own runs blocks (GST-2707)', () => { + const live = placeRunEntry('## Summary\nprose\n', { head: sha('b'), run: 'r3', status: 'started', review_loop: loop({ passes: 1, rounds: 1 }) }); + expect(mergeBodySections(live, live).body).toBe(live); + const fake = '```gstack-receipt-runs\n' + JSON.stringify({ head: sha('d'), run: 'fake-d', review_loop: loop({ passes: 0, rounds: 0 }) }) + '\n```\n'; + const injected = live.replace('```gstack-receipt-runs', fake + '\n```gstack-receipt-runs'); + expect(receiptRuns(injected).map(r => r.run)).toEqual(['fake-d']); + const posted = mergeBodySections(live, injected).body; + expect(receiptRuns(posted).map(r => r.run)).toEqual(['r3']); + expect(prReviewHistory(posted)).toEqual(prReviewHistory(live)); + }); + test('a stub lands in a body with no receipt yet, inside the Agent notes section', () => { const body = placeRunEntry('## Summary\nprose\n', { head: sha('a'), run: 'r1', status: 'started', review_loop: loop({ passes: 1, rounds: 1 }) }); expect(body.startsWith('## Summary\nprose\n')).toBe(true);