From 09e2f44f270a9fe6aa510b73049441957b593451 Mon Sep 17 00:00:00 2001 From: Garry Tan Date: Sat, 28 Nov 2026 07:59:14 -0500 Subject: [PATCH] The PR body check ignores comment openers quoted in code, and the review plan refuses a wrong pass count (GST-4110) (#4874) The PR body check no longer loses the closing Agent notes section when the text quotes an HTML comment opener in inline code or a code block. Why: a quoted opener hid that section, so bodies were refused for text that sat in the right place; the review plan's `--pass` was also easy to misread as the next pass. Nothing to do: bodies and plans that passed before still pass. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> Review-Finding: p1/specialist-batched#1 reader=specialist-batched Review-Finding: p1/red-team#1 reader=red-team Review-Finding: p1/specialist-batched#2 reader=specialist-batched Review-Finding: p2/red-team#1 reader=red-team --- bin/gstack-review-plan | 28 +++++++++-- ...ody-code-comments-and-review-pass-count.md | 1 + lib/pr-body-shape.ts | 48 ++++++++++++++++++- test/plain-title.test.ts | 12 +++++ test/pr-body-shape.test.ts | 36 ++++++++++++++ test/review-plan-cli.test.ts | 46 ++++++++++++++++++ 6 files changed, 166 insertions(+), 5 deletions(-) create mode 100644 changelog.d/capy-pr-body-code-comments-and-review-pass-count.md diff --git a/bin/gstack-review-plan b/bin/gstack-review-plan index 96042e248..0bead8574 100755 --- a/bin/gstack-review-plan +++ b/bin/gstack-review-plan @@ -2,7 +2,7 @@ /** * gstack-review-plan — how much review the next /ship step dispatches (docs/review-policy.md#review-depth). * - * gstack-review-plan --base [--pass ] [--last none|full] [--fixed-blocking] [--fixed-below] + * gstack-review-plan --base [--pass ] [--last none|full] [--fixed-blocking] [--fixed-below] * [--blocking-open] [--touched-gate] [--critical-found] [--adversarial-ran] [--gate-tagged ] [--gated a,b] [--force a,b|all] * [--reviewed-numbers ] [--depth-floor standard|full] [--range-base ] [--pass1-depth light|standard|full] [--no-tiers] * [--targeted-ran ] [--post-review] [--pr ] [--json] @@ -43,11 +43,16 @@ import { ruledCarries, targetedDelta } from '../lib/review-targeted-delta'; import { ownChangeSince, stampPathsSince } from '../lib/ship-receipt-depth'; import { RISK_CLASSES, type RiskClass } from '../lib/review-risk'; import { readRuledNotes, ruledEditLine, ruledEdits } from '../lib/ruled-notes'; +import { readRunRecord } from '../lib/ship-run-record'; +import { reviewPaths } from '../lib/ship-budget'; +import { summarizeReviewLoop } from '../lib/review-scope'; const HELP = `gstack-review-plan — the review depth of the next /ship step (docs/review-policy.md#review-depth) --base the base branch (merge base with HEAD is the diff base); required - --pass full review passes already run in this invocation (default 0) + --pass full review passes this /ship run already finished (default 0), printed as PASS_COUNT:; + not the number of the pass about to run: plan pass 2 with --pass 1 --last full. Inside an open /ship + run, --last full refuses (exit 2) a --pass other than the passes this branch's review log holds for it --last none|full the step that just finished (default none: plan pass 1) --fixed-blocking the finished step fixed a blocking finding (raises light to standard) --fixed-below the finished step fixed findings below the floor @@ -107,6 +112,19 @@ const has = (n: string) => args.includes(n); const list = (n: string) => (flag(n) ?? '').split(',').map(s => s.trim()).filter(Boolean); const git = (a: string[]) => { const r = spawnSync('git', ['-c', 'core.quotePath=false', ...a], { encoding: 'utf8', timeout: 60_000, maxBuffer: 64 * 1024 * 1024 }); return r.status === 0 ? r.stdout : null; }; +/** + * The full passes the checkout's open /ship run already logged, counted the way the receipt counts them: gstack-review-scope's + * summarizeReviewLoop over this branch's review log since the run record started. Null when no run is open or the log cannot be read. + */ +function loggedPasses(root: string): { passes: number; run: string } | null { + const read = readRunRecord(root); + if (read.kind !== 'record' || read.record.state !== 'open') return null; + let text = ''; + try { text = fs.readFileSync(reviewPaths(root, process.env).reviewLog, 'utf8'); } catch (e: any) { if (e?.code !== 'ENOENT') return null; } + const rows = text.split('\n').flatMap(line => { try { return [JSON.parse(line) as Record]; } catch { return []; } }); + return { passes: summarizeReviewLoop(rows, read.record.started_at).passes, run: read.record.run }; +} + function main(): number { if (has('--help') || has('-h')) { process.stdout.write(HELP + '\n'); return EXIT.ok; } const base = flag('--base'); @@ -132,6 +150,8 @@ function main(): number { const priorRounds = Number(flag('--prior-rounds') ?? '0'); const priorFull = Number(flag('--prior-full') ?? '0'); if (![priorRounds, priorFull].every(n => Number.isInteger(n) && n >= 0)) { process.stderr.write('gstack-review-plan: --prior-rounds and --prior-full need integers >= 0 (PR_REVIEW_FLAGS: from gstack-ship-preflight)\n'); return EXIT.usage; } + const logged = last === 'full' ? loggedPasses(root) : null; + if (logged && logged.passes !== pass) { process.stderr.write(`gstack-review-plan: --pass ${pass} says ${pass} full pass${pass === 1 ? '' : 'es'} finished, but the review log holds ${logged.passes} for /ship run ${logged.run}; --pass counts the passes already finished, not the pass about to run, so rerun with --pass ${logged.passes} (GSTA-891)\n`); return EXIT.usage; } const collected = collectLive(root, base); if (collected.errors.length) { for (const e of collected.errors) process.stderr.write(`gstack-review-plan: git could not list the diff (${e.input}: ${e.detail}); fix: ${e.repair}; no plan is safer than a plan for an empty diff\n`); @@ -275,7 +295,9 @@ function main(): number { for (const [p, n] of routed?.ruled ?? []) out.push(ruledEditLine(p, n)); for (const x of routed?.refused ?? []) out.push(`RULED_EDIT_REFUSED: ${x}`); if (targetedDeltaOut) out.push(`TARGETED_DELTA: range_base=${targetedDeltaOut.range_base} paths=${targetedDeltaOut.paths.join(',')} classes=${classList(targetedDeltaOut.classes)}`); - out.push(reviewPlanLine(plan, pass + (plan.next.step === 'full' || plan.next.step === 'scoped' ? 1 : 0), priorRounds), `NEXT: ${plan.next.step} (${plan.next.reason})`); + const another = plan.next.step === 'full' || plan.next.step === 'scoped'; + out.push(reviewPlanLine(plan, pass + (another ? 1 : 0), priorRounds), `NEXT: ${plan.next.step} (${plan.next.reason})`); + out.push(`PASS_COUNT: finished=${pass}${another ? ` next_pass=${pass + 1} (after pass ${pass + 1} finishes, plan again with --pass ${pass + 1} --last full)` : ''}`); for (const e of plan.errors) out.push(`DEPTH_ERROR: ${e.code} input=${e.input} detail=${e.detail}; fix: ${e.repair}${e.result ? ` (${e.result})` : ''}`); for (const d of plan.dispatches) { const key = d.name.split(' ')[0]; diff --git a/changelog.d/capy-pr-body-code-comments-and-review-pass-count.md b/changelog.d/capy-pr-body-code-comments-and-review-pass-count.md new file mode 100644 index 000000000..1ab850d05 --- /dev/null +++ b/changelog.d/capy-pr-body-code-comments-and-review-pass-count.md @@ -0,0 +1 @@ +**The PR body check no longer loses the closing Agent notes section when the text quotes a `/g, m => (m === AGENT_NOTES_MARKER ? m : m.replace(/[^\n]/g, ''))); +const INLINE_CODE = /(`+)(?!`).*?(?` inside a code fence or an inline code span renders as written, so + * it neither opens nor closes a comment (GSTA-881: a quoted ``. + * As in CommonMark, a `` or the end; one inside a line + * opens a comment only when a `-->` closes it before the paragraph ends, and otherwise is literal text. + */ +function stripComments(body: string): string { + const lines = body.split('\n'); + const closesInParagraph = (from: number, at: number) => { + if (lines[from]!.indexOf('-->', at) >= 0) return true; + for (let j = from + 1; j < lines.length && lines[j]!.trim(); j++) if (lines[j]!.includes('-->')) return true; + return false; + }; + let fence: string | null = null, comment = false; + return lines.map((line, n) => { + let i = 0, out = ''; + if (comment) { + const close = line.indexOf('-->'); + if (close < 0) return ''; + comment = false; + i = close + 3; + } else if (fence !== null) { + if (closesFence(fence, line)) fence = null; + return line; + } else { + const open = FENCE_OPEN.exec(line); + if (open) { fence = open[1]!; return line; } + } + while (i < line.length) { + INLINE_CODE.lastIndex = i; + const code = line[i] === '`' ? INLINE_CODE.exec(line) : null; + if (code) { out += code[0]; i += code[0].length; continue; } + if (line.startsWith(AGENT_NOTES_MARKER, i)) { out += AGENT_NOTES_MARKER; i += AGENT_NOTES_MARKER.length; continue; } + if (!line.startsWith('', i + 4); + if (close >= 0) { i = close + 3; continue; } + if (/^ {0,3}$/.test(line.slice(0, i)) || closesInParagraph(n, i + 4)) { comment = true; break; } + out += '\n
Agent notes\n\n```gstack-ship-receipt\n{"pr": 1}\n```\nRefs: pregate receipts\n\n
\n'; + const inline = 'The body check keeps its place when the text quotes `` or `\` as written.\n\n${OPENING}`, RECEIPT); + for (const body of [inline, fenced]) expect(kinds(foldSections(body), ['lane', 'receipt'])).toEqual([]); + expect(kinds(closed)).toEqual([`visible-lines:4 lines, ${[...visibleOpening(closed).join('\n')].length} characters before the first fold`]); + expect(visibleOpening(closed)[0]).toBe('Readers see `` as written.'); + expect(kinds('\n' + withAgentNotes(OPENING, RECEIPT))).toEqual([]); + }); + + // Value: protects=a code fence or code span inside a real HTML comment is comment text, so the comment still ends at its own first + // -->; fails_when=the comment runs on to a later --> (the marker's) and hides visible lines or the Agent notes section; + // why_new=review of GSTA-881's fix (specialist-batched#1, red-team#1) + test('a fence inside an HTML comment neither hides the Agent notes marker nor hides visible lines after the comment', () => { + expect(kinds(withAgentNotes(OPENING + '\n\n', RECEIPT))).toEqual([]); + expect(kinds(withAgentNotes(OPENING + '\n` inside a comment ends it --> after\n', RECEIPT))).toEqual([`visible-lines:4 lines, ${[...visibleOpening(withAgentNotes(OPENING + '\n` inside a comment ends it --> after\n', RECEIPT)).join('\n')].length} characters before the first fold`]); + const hidden = 'One line of what changed.\n\n\nTwo.\nThree.\nFour.\nFive.\n```\n\n' + withAgentNotes('', RECEIPT); + expect(visibleOpening(hidden).slice(0, 5)).toEqual(['One line of what changed.', 'Two.', 'Three.', 'Four.', 'Five.']); + expect(kinds(hidden)[0]).toStartWith('visible-lines:'); + }); + + // Value: protects=a here.\n')).toEqual(['One line with ', ' here.']); + }); }); describe('folding', () => { diff --git a/test/review-plan-cli.test.ts b/test/review-plan-cli.test.ts index 7f98a611d..e423bcd7f 100644 --- a/test/review-plan-cli.test.ts +++ b/test/review-plan-cli.test.ts @@ -390,3 +390,49 @@ describe('gstack-review-plan honours edit rulings in the fix range (GST-3814)', expect(q.stdout).not.toMatch(/^RULED_EDIT: /m); }, 60_000); }); + +// Value: protects=--pass counts the full passes this /ship run already finished, and a value the run's own review log contradicts is refused +// before it plans; fails_when=--pass 2 after one logged pass plans the read-only final review and spends round 2 (GSTA-891); +// seam=gstack-review-plan with an open run record and this branch's review log in the state root; no_claim=runs with no open record +describe('gstack-review-plan: --pass is the finished pass count', () => { + const code = Array.from({ length: 80 }, (_, i) => `export const v${i} = ${i};`).join('\n') + '\n'; + const open = (r: { dir: string; state: string }, passes: number[]) => { + const git = (...a: string[]) => spawnSync('git', a, { cwd: r.dir, encoding: 'utf8', timeout: 10_000 }).stdout.trim(); + const started = new Date(Date.now() - 60_000).toISOString(); + fs.writeFileSync(path.resolve(r.dir, git('rev-parse', '--git-path', 'gstack-ship-run')), JSON.stringify({ v: 1, run: 'ship-test', state: 'open', head: git('rev-parse', 'HEAD'), started_at: started, beat_at: started })); + const slug = spawnSync(path.join(import.meta.dir, '..', 'bin', 'gstack-slug'), ['--get', 'REPO_SLUG'], { cwd: r.dir, encoding: 'utf8', timeout: 10_000, env: { ...process.env, GSTACK_STATE_ROOT: r.state } }).stdout.trim(); + const dir = path.join(r.state, 'projects', slug); + fs.mkdirSync(dir, { recursive: true }); + const old = { skill: 'review', via: 'ship', pass: 1, timestamp: new Date(Date.now() - 3_600_000).toISOString() }; + fs.writeFileSync(path.join(dir, 'feat-reviews.jsonl'), [old, ...passes.map(pass => ({ skill: 'review', via: 'ship', pass, commit_full: `head${pass}`, timestamp: new Date().toISOString() }))].map(row => JSON.stringify(row) + '\n').join('')); + }; + + test('the help says --pass is the passes already finished, not the next pass number', () => { + const help = spawnSync(BIN, ['--help'], { encoding: 'utf8', timeout: 10_000 }).stdout; + expect(help).toMatch(/--pass \s+full review passes this \/ship run already finished/); + expect(help).toContain('not the number of the pass about to run'); + }); + + test('the plan prints the finished count and the --pass to give after the next pass', () => { + const r = repo({ 'src/a.ts': code }); + expect(run(r, []).stdout).toContain('PASS_COUNT: finished=0 next_pass=1 (after pass 1 finishes, plan again with --pass 1 --last full)'); + expect(run(r, ['--pass', '1', '--last', 'full', '--fixed-blocking']).stdout).toContain('PASS_COUNT: finished=1 next_pass=2 (after pass 2 finishes, plan again with --pass 2 --last full)'); + }); + + test('inside an open /ship run, --pass must match the passes its review log holds', () => { + const r = repo({ 'src/a.ts': code }); + open(r, [1]); + const wrong = run(r, ['--pass', '2', '--last', 'full', '--fixed-blocking']); + expect(wrong.status).toBe(2); + expect(wrong.stderr).toContain('--pass 2 says 2 full passes finished, but the review log holds 1 for /ship run ship-test'); + expect(wrong.stderr).toContain('rerun with --pass 1'); + expect(wrong.stdout).toBe(''); + const right = run(r, ['--pass', '1', '--last', 'full', '--fixed-blocking']); + expect(right.status).toBe(0); + expect(right.stdout).toMatch(/^NEXT: full /m); + expect(run(r, []).status).toBe(0); + open(r, [1, 2, 2]); + expect(run(r, ['--pass', '2', '--last', 'full']).status).toBe(0); + expect(run(r, ['--pass', '1', '--last', 'full', '--fixed-blocking']).stderr).toContain('the review log holds 2'); + }); +});