diff --git a/scripts/pm/check-governed-merges.mjs b/scripts/pm/check-governed-merges.mjs index 4dead9bca27..0b9e78e2f4e 100644 --- a/scripts/pm/check-governed-merges.mjs +++ b/scripts/pm/check-governed-merges.mjs @@ -1267,14 +1267,17 @@ const SELF_TEST_BATTERIES = Object.freeze({ // 100 → 103 (#35612): the record must stand on the queue's record leg, its control (the queue leg defers), and `--pr`. '⚖️ the low-risk skills arm: skills wording on Clause-② no lands Tier S, everything else stays H (#33501)': 103, // 51 → 66 with the record stage as the sweep reads it at the landing: ten pins, and five on which landings it binds; - // 66 → 71 with the other cards as they stood at the landing (four pins) and the stage's two NOT MEASURED reads. - '⚖️ #33603: the sweep lists a low-risk arm landing APART and judges it on a same-head at-tier PASS record': 71, + // 66 → 71 with the other cards as they stood at the landing (four pins) and the stage's two NOT MEASURED reads; + // 71 → 77 (#38863) with the queue's own removal up to `MERGE_QUEUE_REMOVAL_WINDOW_MS` before `merged_at`: the + // one-second shape, its controls, outside the window, another actor, the re-add after an ejection, the pure reader. + '⚖️ #33603: the sweep lists a low-risk arm landing APART and judges it on a same-head at-tier PASS record': 77, '⭐ the weekly audit card: every reading the job files on, both ways (#33814)': 54, // #35612: the split leg — every state with its control, the report, the audit card and the INCOMPLETE banner; // 24 → 30 with the review of record 7238665187: S1 (each BREACH row names its leg, both headings name both), N1 // (`sweepComplete` and the loop's count), N2 (an unread workflow diff beside the split is NOT MEASURED, and its control); - // 30 → 35 with S2 of record 7239017248 (an approval or enqueue clears it first: two cases, two controls, one landing half that does not clear). - '⚖️ #35612: the sweep lists a `.claude/**` declaration-split landing APART and judges it on the record that verifies it': 35, + // 30 → 35 with S2 of record 7239017248 (an approval or enqueue clears it first: two cases, two controls, one landing half that does not clear); + // 35 → 36 (#38863): the queue's own removal one second before the merge, on the split leg's landing half. + '⚖️ #35612: the sweep lists a `.claude/**` declaration-split landing APART and judges it on the record that verifies it': 36, '⚖️ #33539: the ADR leg of the low-risk arm — an add-only note lands Tier S, any other ADR change stays H': 52, // 40 → 44 (#35612, N1 of #35610): a record the queue's record leg refuses — a FAIL, below its tier, or a leg not // read — keeps the split at Tier H however its `Rule-change-verified:` line reads; one case pins the words. @@ -5396,8 +5399,10 @@ export async function loadArmReaders(load = (specifier) => import(specifier)) { * readers); `attribution` is the row's pull read (`mergedAt`, `mergeCommitSha`), or null when it failed. * ⭐ The legs run in the guard's own order (`entrySatisfied`): an authorized approval, then an authorized enqueue, * then the record — so an entry the maintainer cleared is the maintainer's merge, never a seat's BREACH (#33711 S1). - * Each leg reads only what existed before the landing: reviews and timeline events strictly before `merged_at` - * (the queue's `removed_from_merge_queue` shares the merge's instant, measured on #33555, #33632 and #33616), and + * Each leg reads only what existed before the landing: reviews and timeline events strictly before `merged_at`, less + * the queue's own terminal `removed_from_merge_queue`, which `github-merge-queue[bot]` stamps at the merge's instant + * or up to `MERGE_QUEUE_REMOVAL_WINDOW_MS` (1 s) before it (`timelineAsQueued`: 0 s on #33555, #33632 and #33616, + * 1 s on all twelve BREACH rows of #34178), and * comments by an author the guard trusts (`recordAuthorTrusted`, #33711 S3), created AND last edited at or before * it (#33711 N4). The record's head is the landed commit's only * when GitHub's `merge_commit_sha` is that commit (#33711 S2). @@ -5518,6 +5523,39 @@ export async function readArmLanding(entry, { paths, repo, readers, fetchers, ru return landingBreach({ guard, recognisers, record, unedited, editedAfter, cardNote, headSha, found, ignored }); } +/** The account that stamps a queue merge's own terminal `removed_from_merge_queue` (measured on the rows below). */ +export const MERGE_QUEUE_BOT = 'github-merge-queue[bot]'; + +/** + * How long before `merged_at` the queue's OWN terminal removal may be stamped and still be read as the merge's, not + * as a removal the queue made first. Measured on this repository's queue merges: 0 s on #33555, #33632 and #33616 + * (removal == `merged_at`, which the strict `< merged_at` cut already drops); 1 s on every one of the twelve BREACH + * rows of #34178 — #37072 (added 2026-11-24T19:16:14Z by os-zhuang, removed 19:34:41Z, merged 19:34:42Z), #36775, + * #36067, #35673, #35709, #33355, #33577, #33589, #33394, #32841, #32839 and #32313. GitHub stamps whole seconds, so + * 1 s is the narrowest window the readings support; no gap over 1 s was measured. ⛔ Widen it only on a measured + * queue merge whose removal sits further out — an ejection the queue makes earlier is a removal of its own. + */ +export const MERGE_QUEUE_REMOVAL_WINDOW_MS = 1000; + +/** + * The timeline as the queue read it before the merge, for the enqueue leg: every event strictly before `landed` + * (`merged_at` in ms), less the merge's own terminal removal. That removal is dropped only when all three hold — it + * is the LAST queue event (`enqueueEvent` / `dequeueEvent`, the guard's names) the cut keeps, its actor is + * `MERGE_QUEUE_BOT`, and it sits at most `MERGE_QUEUE_REMOVAL_WINDOW_MS` before `landed` — so the guard's + * `enqueuerFromTimeline` reads the actor of the `added_to_merge_queue` before it. Every other shape is the plain cut, + * read as it was: a removal by any other actor, one further out (an ejection the queue made first), or an earlier + * removal a re-add follows (the newest queue event is then the add). Names that are not strings drop nothing, so a + * guard rename leaves the leg undetermined, never cleared. Pure. + */ +export function timelineAsQueued(events, landed, { enqueueEvent, dequeueEvent } = {}) { + const kept = (Array.isArray(events) ? events : []).filter((e) => Date.parse(e?.created_at ?? '') < landed); + if (typeof enqueueEvent !== 'string' || typeof dequeueEvent !== 'string') return kept; + const last = kept.findLastIndex((e) => e?.event === enqueueEvent || e?.event === dequeueEvent); + const removal = kept[last]; + if (last < 0 || removal.event !== dequeueEvent || removal.actor?.login !== MERGE_QUEUE_BOT) return kept; + return landed - Date.parse(removal.created_at) <= MERGE_QUEUE_REMOVAL_WINDOW_MS ? kept.filter((_, i) => i !== last) : kept; +} + /** * The first half of `readLandingThreads`, which the split leg's unread-input answer also reads (S2 of record * 7239017248): the merge that ties the pull to THIS commit (S2 of #33603), then the approval leg and the enqueue leg @@ -5548,7 +5586,10 @@ async function readLandingClearance({ entry, headSha, events, guard, fetchers, a } const approval = guard.authorizedApprovalVerdict(reviewsAsLanded(reviews.filter((r) => before(r?.submitted_at)), events, landed), headSha); if (approval.state === 'approved') return { settled: { state: 'cleared', by: 'review', who: approval.approvers, headSha } }; - const enqueue = guard.authorizedEnqueueVerdict(guard.enqueuerFromTimeline(events.filter((e) => before(e?.created_at)))); + // The merge's own terminal removal, up to `MERGE_QUEUE_REMOVAL_WINDOW_MS` before `merged_at`, is not the newest + // queue event the queue read (`timelineAsQueued`). + const queued = timelineAsQueued(events, landed, { enqueueEvent: guard.ENQUEUE_EVENT, dequeueEvent: guard.DEQUEUE_EVENT }); + const enqueue = guard.authorizedEnqueueVerdict(guard.enqueuerFromTimeline(queued)); if (enqueue.state === 'authorized') return { settled: { state: 'cleared', by: 'enqueue', who: [enqueue.login], headSha } }; return { landed }; } @@ -11533,7 +11574,8 @@ async function selfTest() { const APPROVED = (login, at = '2026-11-15T04:10:00Z') => ({ id: 1, state: 'APPROVED', user: { login }, commit_id: HEAD, submitted_at: at }); const cleared = async (over, by) => { const v = await run(over); return v?.state === 'cleared' && v.by === by ? true : JSON.stringify(v); }; assert('⭐ an-authorized-APPROVAL-before-the-landing-clears-it-the-maintainers-merge-not-a-BREACH', await cleared({ prRows: [], reviews: [APPROVED('os-zhuang')] }, 'review') === true); - // The timeline a queue merge leaves, measured on #33555, #33632 and #33616: the removal shares the merge's instant. + // The timeline a queue merge leaves: the removal shares the merge's instant (measured on #33555, #33632 and + // #33616) or sits `MERGE_QUEUE_REMOVAL_WINDOW_MS` (1 s) before it (all twelve BREACH rows of #34178; `QUEUED_EARLY`). const QUEUED = (login) => [ { event: 'added_to_merge_queue', actor: { login }, created_at: '2026-11-15T04:40:00Z' }, { event: 'merged', actor: { login }, created_at: MERGED_AT }, @@ -11542,6 +11584,52 @@ async function selfTest() { assert('⭐ an-authorized-ENQUEUE-clears-it-read-on-the-timeline-as-the-queue-saw-it-before-the-merge', await cleared({ prRows: [], events: QUEUED('os-zhuang') }, 'enqueue') === true); assert('control-a-seat-account-enqueue-clears-nothing-the-record-decides', (await run({ events: QUEUED('objectstack-fleet[bot]') }))?.state === 'pass' && await breach({ prRows: [], events: QUEUED('objectstack-fleet[bot]') }, /no `## Contract review` record/) === true); + // The #37072 shape: removed by the queue one second BEFORE `merged_at`, the merge event at it. `gap` is seconds + // before `merged_at`, `by` the removal's actor; `before` puts queue events ahead of the add (an earlier entry). + const QUEUED_EARLY = (login, { gap = 1, by = MERGE_QUEUE_BOT, before = [] } = {}) => [ + ...before, + { event: 'added_to_merge_queue', actor: { login }, created_at: '2026-11-15T04:40:00Z' }, + { event: 'removed_from_merge_queue', actor: { login: by }, created_at: new Date(Date.parse(MERGED_AT) - gap * 1000).toISOString().replace('.000Z', 'Z') }, + { event: 'merged', actor: { login }, created_at: MERGED_AT }, + ]; + assert('⭐ WINDOW the-queues-own-removal-ONE-SECOND-before-merged_at-is-not-the-newest-queue-event-an-authorized-ENQUEUE-clears-it', + MERGE_QUEUE_REMOVAL_WINDOW_MS === 1000 && await cleared({ prRows: [], events: QUEUED_EARLY('os-zhuang') }, 'enqueue') === true); + assert('control-WINDOW the-equal-instant-removal-still-clears-and-a-seat-enqueue-on-the-one-second-shape-clears-nothing', + await cleared({ prRows: [], events: QUEUED('os-zhuang') }, 'enqueue') === true && + await breach({ prRows: [], events: QUEUED_EARLY('objectstack-fleet[bot]') }, /no `## Contract review` record/) === true && + (await run({ events: QUEUED_EARLY('objectstack-fleet[bot]') }))?.state === 'pass'); + assert('⛔ WINDOW a-queue-removal-OUTSIDE-the-window-stays-undetermined-the-record-decides', + await breach({ prRows: [], events: QUEUED_EARLY('os-zhuang', { gap: 2 }) }, /no `## Contract review` record/) === true && + await breach({ prRows: [], events: QUEUED_EARLY('os-zhuang', { gap: 60 }) }, /no `## Contract review` record/) === true); + assert('⛔ WINDOW a-removal-inside-the-window-by-ANOTHER-actor-stays-undetermined-the-record-decides', + await breach({ prRows: [], events: QUEUED_EARLY('os-zhuang', { by: 'os-zhuang' }) }, /no `## Contract review` record/) === true && + await breach({ prRows: [], events: QUEUED_EARLY('os-zhuang', { by: 'objectstack-fleet[bot]' }) }, /no `## Contract review` record/) === true); + // The #33355 and #33394 shape: an earlier entry the queue ejected, then a re-add, then the merge's own removal. + const EJECTED = (login) => [ + { event: 'added_to_merge_queue', actor: { login }, created_at: '2026-11-15T03:00:00Z' }, + { event: 'removed_from_merge_queue', actor: { login: MERGE_QUEUE_BOT }, created_at: '2026-11-15T03:30:00Z' }, + ]; + assert('⭐ WINDOW after-an-earlier-ejection-the-RE-ADD-is-read-its-enqueuer-decides-never-the-first-add', + await cleared({ prRows: [], events: QUEUED_EARLY('os-zhuang', { before: EJECTED('objectstack-fleet[bot]') }) }, 'enqueue') === true && + await breach({ prRows: [], events: QUEUED_EARLY('objectstack-fleet[bot]', { before: EJECTED('os-zhuang') }) }, /no `## Contract review` record/) === true); + assert('⛔ WINDOW timelineAsQueued-drops-only-the-terminal-queue-bot-removal-in-the-window-and-names-that-are-not-strings-drop-nothing', (() => { + const names = { enqueueEvent: readers.guard.ENQUEUE_EVENT, dequeueEvent: readers.guard.DEQUEUE_EVENT }; + const landed = Date.parse(MERGED_AT); + const kinds = (evs) => evs.map((e) => e.event).join(','); + const early = timelineAsQueued(QUEUED_EARLY('os-zhuang'), landed, names); + const reAddLast = [...EJECTED('os-zhuang'), { event: 'added_to_merge_queue', actor: { login: 'os-zhuang' }, created_at: '2026-11-15T04:59:59Z' }]; + // A queue-bot removal inside the window that an add FOLLOWS is not the terminal queue event: nothing is dropped. + const addAfter = [...QUEUED_EARLY('os-zhuang').slice(0, 2), { event: 'added_to_merge_queue', actor: { login: 'objectstack-fleet[bot]' }, created_at: '2026-11-15T04:59:59Z' }]; + const unnamed = timelineAsQueued(QUEUED_EARLY('os-zhuang'), landed, {}); + return typeof names.enqueueEvent === 'string' && typeof names.dequeueEvent === 'string' && + kinds(early) === 'added_to_merge_queue' && + kinds(timelineAsQueued(reAddLast, landed, names)) === kinds(reAddLast) && + kinds(timelineAsQueued(addAfter, landed, names)) === kinds(addAfter) && + kinds(timelineAsQueued(QUEUED_EARLY('os-zhuang', { gap: 2 }), landed, names)) === 'added_to_merge_queue,removed_from_merge_queue' && + kinds(unnamed) === 'added_to_merge_queue,removed_from_merge_queue' && + timelineAsQueued(null, landed, names).length === 0 + ? true : JSON.stringify({ names, early: kinds(early), unnamed: kinds(unnamed) }); + })() === true); assert('⛔ an-approval-AFTER-the-landing-or-by-an-unauthorized-account-clears-nothing', await breach({ prRows: [], reviews: [APPROVED('os-zhuang', '2026-11-15T06:00:00Z')] }, /no `## Contract review` record/) === true && await breach({ prRows: [], reviews: [APPROVED('os-someone')] }, /no `## Contract review` record/) === true); @@ -11918,6 +12006,12 @@ async function selfTest() { assert('control-a-seat-enqueue-or-a-late-approval-clears-nothing-the-record-decides', await state({ events: QUEUED('objectstack-fleet[bot]') }, 'pass') === true && await state({ prRows: [], reviews: [APPROVED('os-zhuang', '2026-11-25T06:00:00Z')] }, 'breach', /no `## Contract review` record/) === true); + // The split leg reads the same landing half: the queue's own removal one second before `merged_at` (#34178). + const QUEUED_EARLY = (login, by = MERGE_QUEUE_BOT) => [QUEUED(login)[0], { event: 'removed_from_merge_queue', actor: { login: by }, created_at: '2026-11-25T04:59:59Z' }, QUEUED(login)[1]]; + assert('⭐ WINDOW the-queues-own-removal-one-second-before-the-merge-leaves-an-authorized-enqueue-CLEARING-the-split-and-another-actors-does-not', + (await run({ prRows: [], events: QUEUED_EARLY('os-zhuang') }))?.by === 'enqueue' && + await state({ prRows: [], events: QUEUED_EARLY('os-zhuang', 'os-zhuang') }, 'breach', /no `## Contract review` record/) === true && + await state({ prRows: [], events: QUEUED_EARLY('objectstack-fleet[bot]') }, 'breach', /no `## Contract review` record/) === true); // Tier H on what the pull shows: the maintainer's merge, with the split's own reasons. assert('⭐ a-body-declaring-yes-no-line-or-a-doubled-line-stays-on-the-maintainers-list-Tier-H', await state({ body: 'Fixes #69\n\nRule-change: yes\n' }, 'tier-h', /declares `Rule-change: yes`/, 'reasons') === true &&