Skip to content

Commit b9a89e3

Browse files
committed
fix(pm): the queue guard's pull_request leg attributes to the payload PR and reads the net diff
On the pull_request leg, enumerateRows now returns one row: the net diff from the merge-base to the head, attributed to the payload's own pull request number. A commit subject naming an issue no longer sends the review read to that issue, and a branch that merged origin/main no longer forecasts every path main already landed. The merge_group leg keeps its per-first-parent-commit decomposition and subject attribution unchanged. Self-test: seven cases in the decomposition battery (6 -> 13) and one in the pull_request early-warning battery (5 -> 6), driven through a new runGit seam on enumerateRows. Claude-Session: https://claude.ai/code/session_01JQwxba3WdR4pYYA7CdMgAJ Co-authored-by: Claude <noreply@anthropic.com>
1 parent d2ca707 commit b9a89e3

1 file changed

Lines changed: 143 additions & 12 deletions

File tree

‎scripts/pm/check-governed-queue-guard.mjs‎

Lines changed: 143 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,13 @@
234234
* `head_ref`'s PR number is used as a fallback only when the group holds
235235
* exactly one commit, where it is unambiguous.
236236
*
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+
*
237244
* ## The generated-artifact exception is honoured, and that is not optional
238245
*
239246
* A generator-owned file sitting inside a governed surface makes routine
@@ -606,9 +613,9 @@ const SELF_TEST_BATTERIES = Object.freeze({
606613
'the approval predicate': 3,
607614
'⭐ The fail-open direction a naive `.some(r => r.state === \'APPROVED\')`': 5,
608615
'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,
610617
'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,
612619
'the replay fixtures: the three incidents this guard descends from': 11,
613620
'⭐ the ordering guarantee, measured with a spy that THROWS': 7,
614621
'the words a reader acts on (requirement (e))': 26,
@@ -2376,32 +2383,72 @@ function hasRev(root, rev) {
23762383
}
23772384

23782385
/**
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`.
23832405
*/
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}`]);
23862409
const commits = log
23872410
.split('\n')
23882411
.filter((l) => l !== '')
23892412
.map((l) => {
23902413
const [sha, ...rest] = l.split('\t');
23912414
return { sha, subject: rest.join('\t') };
23922415
});
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+
}
23932430
return commits.map((commit) => ({
23942431
...commit,
23952432
// The fallback is only unambiguous when the range holds exactly one
23962433
// commit; in a multi-PR group `head_ref` names the LAST PR, and applying
23972434
// it to an earlier commit attributes a governed diff to the wrong PR.
23982435
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])
24002437
.split('\n')
24012438
.filter((p) => p !== ''),
24022439
}));
24032440
}
24042441

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+
24052452
/**
24062453
* Does any row touch a register path the lift would RECOMPUTE? Pure, and THE
24072454
* one predicate for it: `liftGeneratedExceptions` consults the recompute iff
@@ -2744,7 +2791,7 @@ function readEventDiff(env) {
27442791

27452792
try {
27462793
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 });
27482795
return { ok: true, context, where, mergeBase, rows };
27492796
} catch (error) {
27502797
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 }) {
27752822
const hits = [...new Set(read.rows.flatMap((r) => r.paths).filter((p) => generatedExceptionFor(p) !== null))];
27762823
return { needed: true, why: `the diff touches ${hits.length} register path(s) the lift recomputes: ${hits.join(', ')}` };
27772824
}
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` };
27792826
}
27802827

27812828
async function main() {
@@ -2840,7 +2887,7 @@ async function main() {
28402887
});
28412888
const sizeBlock = renderSizeVerdict(size);
28422889
const report = [
2843-
`${context.label} — ${rows.length} commit(s) in range`,
2890+
`${context.label} — ${commitsInRange(read.rows)} commit(s) in range`,
28442891
...notes,
28452892
renderGuardVerdict(verdict),
28462893
...(sizeBlock === '' ? [] : ['', sizeBlock]),
@@ -3176,6 +3223,77 @@ export async function selfTest() {
31763223
const unattributed = decomposeGovernedWork([{ sha: 'c'.repeat(40), subject: 'chore: direct work', pr: null, paths: ['CLAUDE.md'] }], REPO);
31773224
assert('a-governed-commit-naming-no-pr-is-UNATTRIBUTED-never-dropped', unattributed.unattributed.length === 1 && unattributed.governed.length === 0);
31783225
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+
}
31793297

31803298
// ── the verdict table, both events ────────────────────────────────────────
31813299
battery('the verdict table, both events');
@@ -3230,6 +3348,19 @@ export async function selfTest() {
32303348
);
32313349
const warnedHText = renderGuardVerdict(warnedV);
32323350
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+
}
32333364

32343365
// ── the replay fixtures: the three incidents this guard descends from ─────
32353366
battery('the replay fixtures: the three incidents this guard descends from');

0 commit comments

Comments
 (0)