|
234 | 234 | * `head_ref`'s PR number is used as a fallback only when the group holds |
235 | 235 | * exactly one commit, where it is unambiguous. |
236 | 236 | * |
| 237 | + * The `pull_request` leg is NOT decomposed: its payload names the one pull |
| 238 | + * request being judged, so every governed path is attributed to that number |
| 239 | + * (a subject naming an issue would read the issue's reviews), and the paths |
| 240 | + * are the net diff against the merge-base (a merge of `origin/main` on the |
| 241 | + * branch would otherwise forecast every path `main` already landed). See |
| 242 | + * `enumerateRows`; the `merge_group` reading above is unchanged by it. |
| 243 | + * |
237 | 244 | * ## The generated-artifact exception is honoured, and that is not optional |
238 | 245 | * |
239 | 246 | * A generator-owned file sitting inside a governed surface makes routine |
@@ -606,9 +613,9 @@ const SELF_TEST_BATTERIES = Object.freeze({ |
606 | 613 | 'the approval predicate': 3, |
607 | 614 | '⭐ The fail-open direction a naive `.some(r => r.state === \'APPROVED\')`': 5, |
608 | 615 | 'the 2026-09-04 unpinned predicate (the queue leg\'s)': 12, |
609 | | - 'decomposition, and the multi-PR group trap': 6, |
| 616 | + 'decomposition, and the multi-PR group trap': 13, |
610 | 617 | 'the verdict table, both events': 10, |
611 | | - 'the pull_request leg is an EARLY WARNING and never reddens': 5, |
| 618 | + 'the pull_request leg is an EARLY WARNING and never reddens': 6, |
612 | 619 | 'the replay fixtures: the three incidents this guard descends from': 11, |
613 | 620 | '⭐ the ordering guarantee, measured with a spy that THROWS': 7, |
614 | 621 | 'the words a reader acts on (requirement (e))': 26, |
@@ -2376,32 +2383,72 @@ function hasRev(root, rev) { |
2376 | 2383 | } |
2377 | 2384 |
|
2378 | 2385 | /** |
2379 | | - * The first-parent commits between `baseSha` and `headSha`, each with the |
2380 | | - * paths it changed and the pull request its subject names. One row per PR |
2381 | | - * landing in a merge group; see the header on why the group is decomposed |
2382 | | - * rather than keyed to `head_ref`. |
| 2386 | + * The rows a diff is judged on, read two ways — one per event, because the two |
| 2387 | + * events ask different questions of the same range. |
| 2388 | + * |
| 2389 | + * `merge_group` (the default): the first-parent commits between `baseSha` and |
| 2390 | + * `headSha`, each with the paths it changed and the pull request its subject |
| 2391 | + * names. One row per PR landing in a merge group; see the header on why the |
| 2392 | + * group is decomposed rather than keyed to `head_ref`. |
| 2393 | + * |
| 2394 | + * `pull_request`: ONE row — the net diff `baseSha..headSha` (the caller hands |
| 2395 | + * in the merge-base), attributed to the payload's own pull request number, |
| 2396 | + * which the event names outright. A subject is no evidence here: a commit |
| 2397 | + * subject naming an ISSUE (`fix: … (#<issue>)`) would read that issue's |
| 2398 | + * reviews, and a branch that merged `origin/main` would carry, on its merge |
| 2399 | + * commit's first-parent diff, every path `main` already landed. The net diff |
| 2400 | + * against the merge-base holds only what the pull request itself changes. |
| 2401 | + * `commits` keeps the range's first-parent commit count for the header line. |
| 2402 | + * |
| 2403 | + * `runGit(args)` is the seam the self-test drives; production passes none and |
| 2404 | + * reads the checkout at `root`. |
2383 | 2405 | */ |
2384 | | -export function enumerateRows(root, baseSha, headSha, fallbackPull = null) { |
2385 | | - const log = git(root, ['log', '--first-parent', '--format=%H%x09%s', `${baseSha}..${headSha}`]); |
| 2406 | +export function enumerateRows(root, baseSha, headSha, fallbackPull = null, { event = EVENT_MERGE_GROUP, runGit = null } = {}) { |
| 2407 | + const readGit = runGit ?? ((args) => git(root, args)); |
| 2408 | + const log = readGit(['log', '--first-parent', '--format=%H%x09%s', `${baseSha}..${headSha}`]); |
2386 | 2409 | const commits = log |
2387 | 2410 | .split('\n') |
2388 | 2411 | .filter((l) => l !== '') |
2389 | 2412 | .map((l) => { |
2390 | 2413 | const [sha, ...rest] = l.split('\t'); |
2391 | 2414 | return { sha, subject: rest.join('\t') }; |
2392 | 2415 | }); |
| 2416 | + if (event === EVENT_PULL_REQUEST) { |
| 2417 | + if (commits.length === 0) return []; |
| 2418 | + return [ |
| 2419 | + { |
| 2420 | + sha: headSha, |
| 2421 | + subject: commits[0].subject, |
| 2422 | + pr: fallbackPull, |
| 2423 | + commits: commits.length, |
| 2424 | + paths: readGit(['diff-tree', '-r', '--no-commit-id', '--no-renames', '--name-only', baseSha, headSha]) |
| 2425 | + .split('\n') |
| 2426 | + .filter((p) => p !== ''), |
| 2427 | + }, |
| 2428 | + ]; |
| 2429 | + } |
2393 | 2430 | return commits.map((commit) => ({ |
2394 | 2431 | ...commit, |
2395 | 2432 | // The fallback is only unambiguous when the range holds exactly one |
2396 | 2433 | // commit; in a multi-PR group `head_ref` names the LAST PR, and applying |
2397 | 2434 | // it to an earlier commit attributes a governed diff to the wrong PR. |
2398 | 2435 | pr: pullNumberFromSubject(commit.subject) ?? (commits.length === 1 ? fallbackPull : null), |
2399 | | - paths: git(root, ['diff-tree', '-r', '--no-commit-id', '--no-renames', '--name-only', '-m', '--first-parent', commit.sha]) |
| 2436 | + paths: readGit(['diff-tree', '-r', '--no-commit-id', '--no-renames', '--name-only', '-m', '--first-parent', commit.sha]) |
2400 | 2437 | .split('\n') |
2401 | 2438 | .filter((p) => p !== ''), |
2402 | 2439 | })); |
2403 | 2440 | } |
2404 | 2441 |
|
| 2442 | +/** |
| 2443 | + * How many commits the read range held — the header line's count. A |
| 2444 | + * `pull_request` net row carries its range's count; a `merge_group` reading |
| 2445 | + * is one row per commit, so the row count IS the commit count there. |
| 2446 | + */ |
| 2447 | +export function commitsInRange(rows) { |
| 2448 | + const list = Array.isArray(rows) ? rows : []; |
| 2449 | + return list.reduce((n, row) => n + (Number.isInteger(row?.commits) ? row.commits : 1), 0); |
| 2450 | +} |
| 2451 | + |
2405 | 2452 | /** |
2406 | 2453 | * Does any row touch a register path the lift would RECOMPUTE? Pure, and THE |
2407 | 2454 | * one predicate for it: `liftGeneratedExceptions` consults the recompute iff |
@@ -2744,7 +2791,7 @@ function readEventDiff(env) { |
2744 | 2791 |
|
2745 | 2792 | try { |
2746 | 2793 | const mergeBase = git(repoRoot, ['merge-base', context.baseSha, context.headSha]).trim(); |
2747 | | - const rows = enumerateRows(repoRoot, mergeBase, context.headSha, context.namedPull); |
| 2794 | + const rows = enumerateRows(repoRoot, mergeBase, context.headSha, context.namedPull, { event: context.event }); |
2748 | 2795 | return { ok: true, context, where, mergeBase, rows }; |
2749 | 2796 | } catch (error) { |
2750 | 2797 | return { ok: false, message: `⛔ ${CHECK_CONTEXT_NAME}: could not read the diff (${String(error?.message ?? error).split('\n')[0]}).` }; |
@@ -2775,7 +2822,7 @@ export function toolchainNeededAnswer({ eventName, read }) { |
2775 | 2822 | const hits = [...new Set(read.rows.flatMap((r) => r.paths).filter((p) => generatedExceptionFor(p) !== null))]; |
2776 | 2823 | return { needed: true, why: `the diff touches ${hits.length} register path(s) the lift recomputes: ${hits.join(', ')}` }; |
2777 | 2824 | } |
2778 | | - return { needed: false, why: `${read.rows.length} commit(s) in range touch no register path the lift recomputes` }; |
| 2825 | + return { needed: false, why: `${commitsInRange(read.rows)} commit(s) in range touch no register path the lift recomputes` }; |
2779 | 2826 | } |
2780 | 2827 |
|
2781 | 2828 | async function main() { |
@@ -2840,7 +2887,7 @@ async function main() { |
2840 | 2887 | }); |
2841 | 2888 | const sizeBlock = renderSizeVerdict(size); |
2842 | 2889 | const report = [ |
2843 | | - `${context.label} — ${rows.length} commit(s) in range`, |
| 2890 | + `${context.label} — ${commitsInRange(read.rows)} commit(s) in range`, |
2844 | 2891 | ...notes, |
2845 | 2892 | renderGuardVerdict(verdict), |
2846 | 2893 | ...(sizeBlock === '' ? [] : ['', sizeBlock]), |
@@ -3176,6 +3223,77 @@ export async function selfTest() { |
3176 | 3223 | const unattributed = decomposeGovernedWork([{ sha: 'c'.repeat(40), subject: 'chore: direct work', pr: null, paths: ['CLAUDE.md'] }], REPO); |
3177 | 3224 | assert('a-governed-commit-naming-no-pr-is-UNATTRIBUTED-never-dropped', unattributed.unattributed.length === 1 && unattributed.governed.length === 0); |
3178 | 3225 | assert('an-UNGOVERNED-commit-naming-no-pr-is-simply-not-our-business', decomposeGovernedWork([{ sha: 'd'.repeat(40), subject: 'x', pr: null, paths: ['README.md'] }], REPO).unattributed.length === 0); |
| 3226 | + // The enumeration itself, through its git seam. Two branch shapes: one |
| 3227 | + // commit whose subject names an ISSUE (the PR #25965 shape), and a branch |
| 3228 | + // that merged `origin/main` between its own two commits (the PR #26311 |
| 3229 | + // shape). The fake answers only the three reads `enumerateRows` makes, and |
| 3230 | + // records them, so the pull_request leg is measured to read the NET diff. |
| 3231 | + const EB = 'b'.repeat(40); |
| 3232 | + const enumGit = (log, perCommit, net) => { |
| 3233 | + const reads = []; |
| 3234 | + const fake = (args) => { |
| 3235 | + reads.push(args.join(' ')); |
| 3236 | + if (args[0] === 'log') return log.map(([sha, subject]) => `${sha}\t${subject}`).join('\n') + '\n'; |
| 3237 | + if (args[0] === 'diff-tree' && args.includes('--first-parent')) return (perCommit[args[args.length - 1]] ?? []).join('\n') + '\n'; |
| 3238 | + if (args[0] === 'diff-tree') return net.join('\n') + '\n'; |
| 3239 | + throw new Error(`unexpected git read: ${args.join(' ')}`); |
| 3240 | + }; |
| 3241 | + return { fake, reads }; |
| 3242 | + }; |
| 3243 | + const ISSUE_HEAD = '1'.repeat(40); |
| 3244 | + const issueBranch = () => enumGit([[ISSUE_HEAD, 'fix(pm): the main-line script reads the lock slot (#25770)']], { [ISSUE_HEAD]: ['AGENTS.md'] }, ['AGENTS.md']); |
| 3245 | + const MERGED_HEAD = '2'.repeat(40); |
| 3246 | + const MERGE_SHA = '3'.repeat(40); |
| 3247 | + const FIRST = '4'.repeat(40); |
| 3248 | + const mergedBranch = () => |
| 3249 | + enumGit( |
| 3250 | + [[MERGED_HEAD, 'ci(governed-surface-guard): the workflow commit'], [MERGE_SHA, "Merge branch 'main' into claude/issue-26201-x"], [FIRST, 'ci(governed-surface-guard): pin the self-test step']], |
| 3251 | + { [MERGED_HEAD]: ['.github/workflows/governed-surface-guard.yml'], [MERGE_SHA]: ['.github/workflows/lint.yml', 'AGENTS.md', 'packages/core/src/x.ts'], [FIRST]: ['.github/workflows/governed-surface-guard.yml'] }, |
| 3252 | + ['.github/workflows/governed-surface-guard.yml'], |
| 3253 | + ); |
| 3254 | + { |
| 3255 | + const g = issueBranch(); |
| 3256 | + const prRows = enumerateRows('/w', EB, ISSUE_HEAD, 25965, { event: EVENT_PULL_REQUEST, runGit: g.fake }); |
| 3257 | + const prWork = decomposeGovernedWork(prRows, REPO); |
| 3258 | + assert( |
| 3259 | + '⭐ pull_request-leg: a-subject-naming-an-ISSUE-is-attributed-to-the-PAYLOAD-pr-never-the-issue', |
| 3260 | + prRows.length === 1 && prRows[0].pr === 25965 && prWork.governed.map((e) => e.pr).join() === '25965' && prWork.unattributed.length === 0, |
| 3261 | + JSON.stringify(prRows), |
| 3262 | + ); |
| 3263 | + const qg = issueBranch(); |
| 3264 | + const queueRows = enumerateRows('/w', EB, ISSUE_HEAD, 26000, { event: EVENT_MERGE_GROUP, runGit: qg.fake }); |
| 3265 | + assert( |
| 3266 | + '⛔ CONTROL merge_group-leg: the-same-subject-still-attributes-by-its-trailing-number-unchanged', |
| 3267 | + queueRows.length === 1 && queueRows[0].pr === 25770 && queueRows[0].sha === ISSUE_HEAD, |
| 3268 | + JSON.stringify(queueRows), |
| 3269 | + ); |
| 3270 | + } |
| 3271 | + { |
| 3272 | + const g = mergedBranch(); |
| 3273 | + const prRows = enumerateRows('/w', EB, MERGED_HEAD, 26311, { event: EVENT_PULL_REQUEST, runGit: g.fake }); |
| 3274 | + const prWork = decomposeGovernedWork(prRows, REPO); |
| 3275 | + assert( |
| 3276 | + '⭐ pull_request-leg: a-branch-that-merged-origin-main-carries-ONLY-its-net-rows-against-the-merge-base', |
| 3277 | + prRows.length === 1 && prRows[0].paths.join() === '.github/workflows/governed-surface-guard.yml' && |
| 3278 | + prWork.governed.length === 1 && prWork.governed[0].pr === 26311 && prWork.governed[0].paths.join() === '.github/workflows/governed-surface-guard.yml' && |
| 3279 | + prWork.unattributed.length === 0, |
| 3280 | + JSON.stringify(prRows), |
| 3281 | + ); |
| 3282 | + assert( |
| 3283 | + 'and-it-reads-the-net-diff-base-to-head-never-a-per-commit-first-parent-diff', |
| 3284 | + g.reads.some((r) => r === `diff-tree -r --no-commit-id --no-renames --name-only ${EB} ${MERGED_HEAD}`) && !g.reads.some((r) => r.includes('--first-parent') && r.startsWith('diff-tree')), |
| 3285 | + g.reads.join(' | '), |
| 3286 | + ); |
| 3287 | + assert('and-the-header-still-counts-the-ranges-three-commits', commitsInRange(prRows) === 3, String(commitsInRange(prRows))); |
| 3288 | + const qg = mergedBranch(); |
| 3289 | + const queueRows = enumerateRows('/w', EB, MERGED_HEAD, 26311, { event: EVENT_MERGE_GROUP, runGit: qg.fake }); |
| 3290 | + assert( |
| 3291 | + '⛔ CONTROL merge_group-leg: the-same-range-is-still-one-row-per-first-parent-commit-unattributed-without-a-subject-number', |
| 3292 | + queueRows.length === 3 && queueRows.every((r) => r.pr === null) && queueRows[1].paths.includes('AGENTS.md') && commitsInRange(queueRows) === 3, |
| 3293 | + JSON.stringify(queueRows.map((r) => [r.sha.slice(0, 1), r.pr, r.paths.length])), |
| 3294 | + ); |
| 3295 | + assert('and-an-empty-pull_request-range-is-no-rows', enumerateRows('/w', EB, EB, 1, { event: EVENT_PULL_REQUEST, runGit: enumGit([], {}, []).fake }).length === 0); |
| 3296 | + } |
3179 | 3297 |
|
3180 | 3298 | // ── the verdict table, both events ──────────────────────────────────────── |
3181 | 3299 | battery('the verdict table, both events'); |
@@ -3230,6 +3348,19 @@ export async function selfTest() { |
3230 | 3348 | ); |
3231 | 3349 | const warnedHText = renderGuardVerdict(warnedV); |
3232 | 3350 | assert('and-names-Tier-H-and-the-approval-it-waits-for-with-no-Tier-S-block', /landing tier: H/.test(warnedHText) && /authorized APPROVED review/.test(warnedHText) && !/Tier S \(/.test(warnedHText), warnedHText); |
| 3351 | + // The PR #25965 shape end to end on this leg: the warning names the PAYLOAD's |
| 3352 | + // pull request, whose reviews it reads, and never the issue the subject names. |
| 3353 | + { |
| 3354 | + const ISSUE_ONLY = '5'.repeat(40); |
| 3355 | + const fake = (args) => { |
| 3356 | + if (args[0] === 'log') return `${ISSUE_ONLY}\tfix(pm): the main-line script reads the lock slot (#25770)\n`; |
| 3357 | + if (args[0] === 'diff-tree') return 'AGENTS.md\n'; |
| 3358 | + throw new Error(args.join(' ')); |
| 3359 | + }; |
| 3360 | + const issueRows = enumerateRows('/w', 'b'.repeat(40), ISSUE_ONLY, 25965, { event: EVENT_PULL_REQUEST, runGit: fake }); |
| 3361 | + const issueText = renderGuardVerdict(run('pull_request', issueRows, new Map([[25965, approvalVerdict([])]]))); |
| 3362 | + assert('⭐ the-early-warning-names-the-payloads-pull-request-and-never-the-issue-its-subject-names', /#25965 — governed/.test(issueText) && !/#25770/.test(issueText), issueText); |
| 3363 | + } |
3233 | 3364 |
|
3234 | 3365 | // ── the replay fixtures: the three incidents this guard descends from ───── |
3235 | 3366 | battery('the replay fixtures: the three incidents this guard descends from'); |
|
0 commit comments