From 852415ffc1a5c5ea4d0777e8a5bed9910726cc5c Mon Sep 17 00:00:00 2001 From: Danyang Chen Date: Fri, 27 Nov 2026 13:23:53 +0800 Subject: [PATCH 1/6] ci: add a merge-decision script that advises on one head (Related #3393) --- .github/scripts/merge-decision-core.mjs | 89 +++++++++++++++++++++++++ .github/scripts/merge-decision.mjs | 64 ++++++++++++++++++ 2 files changed, 153 insertions(+) create mode 100644 .github/scripts/merge-decision-core.mjs create mode 100644 .github/scripts/merge-decision.mjs diff --git a/.github/scripts/merge-decision-core.mjs b/.github/scripts/merge-decision-core.mjs new file mode 100644 index 000000000..3a988e18d --- /dev/null +++ b/.github/scripts/merge-decision-core.mjs @@ -0,0 +1,89 @@ +// The decision itself (issue #3407): read the four merge-gate results plus the run history of this +// head, and say whether to merge, with the reasons and a paste-ready merge record. Pure: no network, +// no process.argv, no writes. +// +// The four gates come from merge-gate.mjs: (1) head and state, (2) required checks, (3) QA evidence +// and Lead review on this head, (4) exactly one risk:T* label. Gates 1-3 are the evidence the merge +// rests on; gate 4 is the record, not the tier. The tier is read into an advisory line only: since +// 2026-11-26 the merge is the FlyBot Lead's call and no tier blocks or authorises it. +// +// Two facts about the run history are treated as first class, because "green" alone hides them: +// +// flake a failed attempt followed by a success on the identical head -> merge, recorded +// as a flake, never as clean green +// contradiction gate 2 says success but the newest attempt is not a success -> hold +// +// Required checks whose history was not supplied are not called clean: the verdict states the +// coverage instead. +import { EXIT, REQUIRED, VERDICT, attemptText, byStart, detailLines, tierOf, validate } from './merge-decision-input.mjs' +import { mergeRecord } from './merge-decision-record.mjs' +import { render } from './merge-decision-render.mjs' + +// The module other files import: the decision, its input checks, and its two renderers. +export { EXIT, REQUIRED, VERDICT, attemptText, byStart, detailLines, tierOf, validate, render } + +export function decide(input) { + const { ok, errors } = validate(input) + if (!ok) { + return { verdict: VERDICT.invalid, code: EXIT.invalid, head: input?.head, pr: input?.gate?.number ?? null, blockers: errors, caveats: [], flakes: [], coverage: null, tier: tierOf(input || {}), record: undefined } + } + + const gates = input.gate.gates + const tier = tierOf(input) + const blockers = [] + const caveats = [] + const flakes = [] + + // Gates 1-3 are the evidence the merge rests on: any failure is a hold. + for (const gate of gates.slice(0, 3)) { + if (!gate.pass) blockers.push(`gate ${gate.n} (${gate.name}) failed: ${detailLines(gate.detail).join('; ') || 'no detail'}`) + } + // Gate 4 is the record, not the tier: a missing or doubled label means the record is incomplete. + if (!gates[3].pass) blockers.push(`gate 4 (${gates[3].name}) failed: ${detailLines(gates[3].detail).join('; ') || 'no detail'} - the record is incomplete, not the tier`) + if (gates[3].pass && !tier) blockers.push('gate 4 passes but the tier is not stated (input.tier or a risk:T* line in gate 4 detail)') + + const history = new Map() + for (const check of input.checks) { + const attempts = [...check.attempts].sort(byStart) + history.set(check.name, [...(history.get(check.name) || []), ...attempts].sort(byStart)) + } + + // A gate-2 pass and a newest attempt that is not a success are two statements about one fact. + if (gates[1].pass) { + for (const name of REQUIRED) { + const attempts = history.get(name) + if (!attempts) continue + const newest = attempts[attempts.length - 1] + if (newest.conclusion !== 'success') blockers.push(`gate 2 says this check is success, but the newest attempt of "${name}" is ${attemptText(newest)}`) + } + } + + // A failed attempt followed by a success on the identical head is a flake. It does not block the + // merge, and it must never be reported as green. + for (const [name, attempts] of history) { + if (attempts.length < 2) continue + const bad = attempts.filter((a) => a.conclusion !== 'success') + const good = attempts.filter((a) => a.conclusion === 'success') + if (bad.length === 0 || good.length === 0) continue + if (attempts[attempts.length - 1].conclusion !== 'success') continue + flakes.push({ name, attempts }) + } + for (const flake of flakes) { + const first = flake.attempts.find((a) => a.conclusion !== 'success') + const last = flake.attempts[flake.attempts.length - 1] + caveats.push(`flake: "${flake.name}" ${attemptText(first)} then ${attemptText(last)} on the identical head - one red and one green run on the same bytes. Recorded as a flake, not clean green; it does not prove the first failure was a false alarm.`) + } + + // Coverage: which required names the history actually holds. Uncovered names are not called clean. + const supplied = REQUIRED.filter((name) => history.has(name)) + const missing = REQUIRED.filter((name) => !history.has(name)) + const coverage = { required: REQUIRED.length, supplied: supplied.length, missing } + if (gates[1].pass && missing.length > 0) { + caveats.push(`run history covers ${supplied.length} of ${REQUIRED.length} required checks; ${missing.length} not read here, so this verdict does not call them clean (no flake evidence either way)`) + } + + const verdict = blockers.length === 0 ? VERDICT.merge : VERDICT.hold + const result = { verdict, code: EXIT[verdict], head: input.head, pr: input.gate.number ?? null, gates, tier, blockers, caveats, flakes, coverage } + result.record = mergeRecord(result, input.note) + return result +} diff --git a/.github/scripts/merge-decision.mjs b/.github/scripts/merge-decision.mjs new file mode 100644 index 000000000..c332b6ce8 --- /dev/null +++ b/.github/scripts/merge-decision.mjs @@ -0,0 +1,64 @@ +// Should this head be merged? (issue #3407) +// +// node .github/scripts/merge-decision.mjs --in decision.json # read one input file, print the decision +// cat decision.json | node .github/scripts/merge-decision.mjs # the same, from stdin +// node .github/scripts/merge-decision.mjs --in decision.json --json # the same result, as data +// node .github/scripts/merge-decision.mjs --self-test # no network: the ten cases below +// +// Input: one head plus the four merge-gate results, and the run history of the checks on that head +// (see merge-decision-core.mjs for the shape; `gate` is merge-gate.mjs --json). Output: +// +// exit 0 RECOMMEND MERGE - the four gates pass; caveats (a flake, partial coverage) are listed +// exit 1 DO NOT MERGE - a gate failed, or the run history contradicts a gate-2 success +// exit 2 INVALID INPUT - the input does not describe one head and its four gate results +// +// The tier is printed as an advisory line and never decides anything: since 2026-11-26 the merge is +// the FlyBot Lead's call. The script never writes: it does not merge, label, comment or push. It also +// does not call the API - the caller supplies the gate result and the run history, so the decision is +// reproducible from the record alone. +// +// This file is the CLI only; the decision itself is pure and lives in merge-decision-core.mjs, the +// fixtures in merge-decision-self-test.mjs. +import { readFileSync } from 'node:fs' +import { decide, render, VERDICT } from './merge-decision-core.mjs' +import { selfTest } from './merge-decision-self-test.mjs' + +export * from './merge-decision-core.mjs' +export { SELF_TEST_CASES, SNAPSHOT_3406 } from './merge-decision-self-test.mjs' + +function arg(name) { + const i = process.argv.indexOf(name) + return i > 0 ? process.argv[i + 1] : undefined +} + +function readInput() { + const path = arg('--in') + if (path) return readFileSync(path, 'utf8') + if (process.argv.includes('--in')) throw new Error('--in needs a file path') + if (process.stdin.isTTY) throw new Error('pass --in , or pipe the input on stdin') + return readFileSync(0, 'utf8') +} + +function main() { + if (process.argv.includes('--self-test')) { + process.exitCode = selfTest() ? 0 : 1 + return + } + let input + try { + input = JSON.parse(readInput()) + } catch (error) { + console.error(`merge-decision: cannot read the input: ${error.message}`) + console.error('Expected a JSON object: { head, gate, checks, tier? } - see merge-decision-core.mjs.') + process.exitCode = 2 + return + } + const result = decide(input) + console.log(process.argv.includes('--json') ? JSON.stringify(result, null, 2) : render(result)) + if (result.verdict === VERDICT.invalid) { + console.error('merge-decision: invalid input, no verdict was produced') + } + process.exitCode = result.code +} + +main() From ad8be9ff9c19a3dd22d4bd0578d62d7ce6bf7830 Mon Sep 17 00:00:00 2001 From: Danyang Chen Date: Fri, 27 Nov 2026 13:24:13 +0800 Subject: [PATCH 2/6] ci: add the merge-decision input validation and renderer (Related #3393) --- .github/scripts/merge-decision-input.mjs | 96 +++++++++++++++++++++++ .github/scripts/merge-decision-render.mjs | 55 +++++++++++++ 2 files changed, 151 insertions(+) create mode 100644 .github/scripts/merge-decision-input.mjs create mode 100644 .github/scripts/merge-decision-render.mjs diff --git a/.github/scripts/merge-decision-input.mjs b/.github/scripts/merge-decision-input.mjs new file mode 100644 index 000000000..7cf6d60af --- /dev/null +++ b/.github/scripts/merge-decision-input.mjs @@ -0,0 +1,96 @@ +// The input side of the merge decision (issue #3407): the shape, the validation, the tier lookup. +// Pure: no network, no process.argv, no files. +// +// { +// "head": "<40-hex>", +// "gate": { "number": 3406, "head": "", "pass": true, +// "gates": [ { "n": 1, "name": "...", "pass": true, "detail": ["..."] }, ... x4 ] }, +// "checks": [ { "name": "FlyBot required checks", +// "attempts": [ { "run": 45939643553, "conclusion": "failure", +// "started_at": "2026-11-27T04:28:04Z" }, ... ] } ], +// "tier": "T3", +// "note": "optional, at most 200 characters, copied into the merge record" +// } +// +// `gate` is the result of merge-gate.mjs (--json) for this head. `checks` is the per-check run +// history of the same head (the check-runs endpoint with filter=all, not filter=latest): without it +// a verdict cannot tell a clean run from a flake, so an input with no history at all is refused +// rather than assumed clean. History that covers only some required names is accepted, and the +// verdict then states the coverage - it does not call the uncovered names clean. +import { REQUIRED_CHECKS, ROLLUP_CHECK } from './merge-gate-core.mjs' + +export const REQUIRED = [...REQUIRED_CHECKS, ROLLUP_CHECK] +export const VERDICT = { merge: 'merge', hold: 'hold', invalid: 'invalid' } +export const EXIT = { merge: 0, hold: 1, invalid: 2 } + +const SHA = /^[0-9a-f]{40}$/ +const TIER = /^T[0-3]$/ +const LABEL = /risk:(T[0-3])/ + +export const detailLines = (detail) => (Array.isArray(detail) ? detail : [detail]).filter((d) => typeof d === 'string' && d.length > 0) + +// Attempts in the order they started; a missing started_at keeps the order as given. +export const byStart = (a, b) => (Date.parse(a.started_at || 0) || 0) - (Date.parse(b.started_at || 0) || 0) +export const attemptText = (a) => `${a.run ? `run ${a.run}` : 'an attempt'} ${a.conclusion}${a.started_at ? ` at ${a.started_at}` : ''}` + +export function tierOf(input) { + if (input && TIER.test(String(input.tier ?? ''))) return String(input.tier) + const gate4 = input?.gate?.gates?.[3] + if (!gate4) return null + for (const line of detailLines(gate4.detail)) { + const m = line.match(LABEL) + if (m) return m[1] + } + return null +} + +export function validate(input) { + const errors = [] + if (!input || typeof input !== 'object' || Array.isArray(input)) return { ok: false, errors: ['input is not an object'] } + const head = input.head + if (!SHA.test(String(head ?? ''))) errors.push(`head is ${JSON.stringify(head)}, not a 40-character lowercase sha`) + + const gate = input.gate + if (!gate || typeof gate !== 'object' || Array.isArray(gate)) { + errors.push('gate is missing: pass the merge-gate result for this head (merge-gate.mjs --pr N --json)') + } else { + if (!SHA.test(String(gate.head ?? ''))) errors.push(`gate.head is ${JSON.stringify(gate.head)}, not a 40-character sha`) + else if (SHA.test(String(head ?? '')) && gate.head !== head) { + errors.push(`gate result was produced for ${gate.head}, not for head ${head}: a stale gate result proves nothing about this head`) + } + if (gate.number !== undefined && (!Number.isInteger(gate.number) || gate.number <= 0)) errors.push(`gate.number is ${JSON.stringify(gate.number)}, not a pull request number`) + const gates = gate.gates + if (!Array.isArray(gates) || gates.length !== 4) { + errors.push(`gate.gates must hold exactly 4 entries, found ${Array.isArray(gates) ? gates.length : typeof gates}`) + } else { + gates.forEach((g, i) => { + if (!g || typeof g !== 'object') { errors.push(`gate entry ${i + 1} is not an object`); return } + if (g.n !== i + 1) errors.push(`gate entry ${i + 1} is numbered ${JSON.stringify(g.n)}, expected ${i + 1}`) + if (typeof g.pass !== 'boolean') errors.push(`gate ${i + 1} has no boolean pass`) + if (typeof g.name !== 'string' || !g.name) errors.push(`gate entry ${i + 1} has no name`) + }) + if (typeof gate.pass === 'boolean' && gates.length === 4 && gates.every((g) => g && typeof g.pass === 'boolean')) { + const all = gates.every((g) => g.pass === true) + if (gate.pass !== all) errors.push(`gate.pass is ${gate.pass} but the four entries say ${all ? 'all pass' : 'not all pass'}: inconsistent result`) + } + } + } + + if (!Array.isArray(input.checks)) { + errors.push('checks is missing: pass the per-check run history for this head (filter=all); a verdict without it cannot tell a clean run from a flake') + } else { + input.checks.forEach((c, i) => { + if (!c || typeof c !== 'object' || typeof c.name !== 'string' || !c.name) { errors.push(`checks[${i}] has no name`); return } + if (!Array.isArray(c.attempts) || c.attempts.length === 0) { errors.push(`checks[${i}] ("${c.name}") has no attempts`); return } + c.attempts.forEach((a, j) => { + if (!a || typeof a !== 'object') { errors.push(`checks[${i}].attempts[${j}] is not an object`); return } + if (typeof a.conclusion !== 'string' || !a.conclusion) errors.push(`checks[${i}].attempts[${j}] has no conclusion`) + if (a.started_at !== undefined && Number.isNaN(Date.parse(a.started_at))) errors.push(`checks[${i}].attempts[${j}] has an unreadable started_at ${JSON.stringify(a.started_at)}`) + }) + }) + } + + if (input.tier !== undefined && !TIER.test(String(input.tier))) errors.push(`tier is ${JSON.stringify(input.tier)}, not T0..T3`) + if (input.note !== undefined && (typeof input.note !== 'string' || input.note.length > 200)) errors.push('note must be a string of at most 200 characters') + return { ok: errors.length === 0, errors } +} diff --git a/.github/scripts/merge-decision-render.mjs b/.github/scripts/merge-decision-render.mjs new file mode 100644 index 000000000..2413a8975 --- /dev/null +++ b/.github/scripts/merge-decision-render.mjs @@ -0,0 +1,55 @@ +// The human-readable half of the merge decision (issue #3407): one screen for a person, ending with +// the paste-ready merge record. Pure text: no network, no files, no writes. +// +// The layout is fixed on purpose - verdict first, then the four gates, then the advisory tier, then +// what blocks or qualifies the verdict. The tier line always says it decides nothing, so a reader +// cannot mistake it for the reason. +import { VERDICT } from './merge-decision-input.mjs' + +const bullet = (text) => ` - ${text}` + +export function render(result) { + const pr = result.pr ? ` (PR #${result.pr})` : '' + const head = result.head ? String(result.head) : '(no head)' + const lines = [] + + if (result.verdict === VERDICT.invalid) { + lines.push(`INVALID INPUT${pr}: no verdict was produced, nothing is being merged (exit ${result.code}).`) + lines.push('', 'This input does not describe one head with its four gate results:') + for (const blocker of result.blockers) lines.push(bullet(blocker)) + return lines.join('\n') + } + + if (result.verdict === VERDICT.merge) { + lines.push(`RECOMMEND MERGE ${head}${pr} - exit ${result.code}. The FlyBot Lead decides; this is advice.`) + } else { + lines.push(`DO NOT MERGE ${head}${pr} - exit ${result.code}.`) + } + + lines.push('', 'Gates (1-3 are the evidence, 4 is the record):') + for (const gate of result.gates) lines.push(` ${gate.pass ? 'pass' : 'FAIL'} ${gate.n}. ${gate.name}`) + + if (result.tier) { + lines.push(`Tier risk:${result.tier} - advisory only: printed here, decides nothing.`) + } else { + lines.push('Tier - not stated; advisory anyway, it decides nothing.') + } + + if (result.blockers.length > 0) { + lines.push('', 'Blockers:') + for (const blocker of result.blockers) lines.push(bullet(blocker)) + } + if (result.caveats.length > 0) { + lines.push('', 'Caveats (they qualify the verdict, they do not reverse it):') + for (const caveat of result.caveats) lines.push(bullet(caveat)) + } + + if (result.verdict === VERDICT.hold) { + lines.push('', `Not claimed: that ${head} is mergeable; it stays open until the blockers are answered.`) + return lines.join('\n') + } + + lines.push('', 'Merge record (paste into the PR or the merge commit):') + lines.push(result.record.replace(/^/gm, ' ')) + return lines.join('\n') +} From 75c6c7197d974e64e93a75ae867cbc951f5686a5 Mon Sep 17 00:00:00 2001 From: Danyang Chen Date: Fri, 27 Nov 2026 13:26:21 +0800 Subject: [PATCH 3/6] Add the paste-ready merge record (Related #3393) --- .github/scripts/merge-decision-record.mjs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 .github/scripts/merge-decision-record.mjs diff --git a/.github/scripts/merge-decision-record.mjs b/.github/scripts/merge-decision-record.mjs new file mode 100644 index 000000000..c51ad8c73 --- /dev/null +++ b/.github/scripts/merge-decision-record.mjs @@ -0,0 +1,23 @@ +// The paste-ready merge record (issue #3407): the head, the reason, the caveats, and what is not +// claimed. It is the text a Lead can paste into the PR or the merge commit, so it carries the head +// that was decided and the tier as advisory only. Pure: no network, no files, no writes. +import { VERDICT } from './merge-decision-input.mjs' + +export function mergeRecord(result, note) { + const pr = result.pr ? ` (PR #${result.pr})` : '' + const tier = result.tier ? `risk:${result.tier}` : 'not stated' + if (result.verdict === VERDICT.merge) { + const why = 'the four merge-gate gates pass on this head: open and mergeable on main, every required check and the rollup completed success, QA evidence and the Lead review both name this head, and exactly one script-applied risk label' + const out = [`Merging ${result.head}${pr}.`, `Reason: ${why}. Tier ${tier} is advisory and did not decide this.`] + if (result.flakes.length === 0 && result.coverage && result.coverage.missing.length === 0) { + out.push(`Required checks: ${result.coverage.required}/${result.coverage.required} read, one success each on this head (clean green).`) + } + for (const caveat of result.caveats) out.push(`Caveat: ${caveat}`) + if (note) out.push(`Note: ${note}`) + out.push('Not claimed: parity or product acceptance; local green and CI green are reported separately.') + return out.join('\n') + } + const out = [`Not merging ${result.head}${pr}.`, `Reason: ${result.blockers.join(' | ')}`] + if (result.tier) out.push(`Tier ${tier} is advisory: it is not why this is on hold.`) + return out.join('\n') +} From 462fe346cc3031353b0c136141ff7d7ebf0b510d Mon Sep 17 00:00:00 2001 From: Danyang Chen Date: Fri, 27 Nov 2026 13:26:41 +0800 Subject: [PATCH 4/6] Add the #3406 replay fixture for the merge-decision script (Related #3393) --- .../scripts/fixtures/merge-decision-3406.json | 286 ++++++++++++++++++ 1 file changed, 286 insertions(+) create mode 100644 .github/scripts/fixtures/merge-decision-3406.json diff --git a/.github/scripts/fixtures/merge-decision-3406.json b/.github/scripts/fixtures/merge-decision-3406.json new file mode 100644 index 000000000..ff0807bf9 --- /dev/null +++ b/.github/scripts/fixtures/merge-decision-3406.json @@ -0,0 +1,286 @@ +{ + "head": "5072d22dbda12a31b15c4a50071fbcf1ee3fdad7", + "gate": { + "number": 3406, + "head": "5072d22dbda12a31b15c4a50071fbcf1ee3fdad7", + "pass": true, + "gates": [ + { + "n": 1, + "name": "head and state", + "pass": true, + "detail": [ + "open, not a draft, mergeable, base main, head 5072d22dbda12a31b15c4a50071fbcf1ee3fdad7" + ] + }, + { + "n": 2, + "name": "required checks", + "pass": true, + "detail": [ + "15 required checks and FlyBot required checks completed success on 5072d22d" + ] + }, + { + "n": 3, + "name": "evidence on this head", + "pass": true, + "detail": [ + "QA #7210114758; review #7210116921 - both name 5072d22d" + ] + }, + { + "n": 4, + "name": "risk (advisory)", + "pass": true, + "detail": [ + "risk:T3 - advisory only, merge is Lead's call" + ] + } + ] + }, + "checks": [ + { + "name": "Web and compose planner", + "attempts": [ + { + "run": 140310751362, + "conclusion": "success", + "started_at": "2026-11-27T04:23:24Z" + }, + { + "run": 140314992386, + "conclusion": "success", + "started_at": "2026-11-27T04:37:05Z" + } + ] + }, + { + "name": "Management API · Python 3.11", + "attempts": [ + { + "run": 140310751281, + "conclusion": "success", + "started_at": "2026-11-27T04:23:25Z" + }, + { + "run": 140314992356, + "conclusion": "success", + "started_at": "2026-11-27T04:37:06Z" + } + ] + }, + { + "name": "Management API · Python 3.13", + "attempts": [ + { + "run": 140310751136, + "conclusion": "success", + "started_at": "2026-11-27T04:23:24Z" + }, + { + "run": 140314992099, + "conclusion": "success", + "started_at": "2026-11-27T04:37:06Z" + } + ] + }, + { + "name": "upstream / Pin and vendor tree", + "attempts": [ + { + "run": 140310751490, + "conclusion": "success", + "started_at": "2026-11-27T04:23:25Z" + }, + { + "run": 140314992492, + "conclusion": "success", + "started_at": "2026-11-27T04:37:06Z" + } + ] + }, + { + "name": "upstream / Upstream suite · Node 22 · DSH from pin", + "attempts": [ + { + "run": 140310856686, + "conclusion": "success", + "started_at": "2026-11-27T04:23:44Z" + }, + { + "run": 140315115128, + "conclusion": "success", + "started_at": "2026-11-27T04:37:29Z" + } + ] + }, + { + "name": "upstream / Upstream suite · Node 24 · DSH from pin", + "attempts": [ + { + "run": 140310856705, + "conclusion": "success", + "started_at": "2026-11-27T04:23:44Z" + }, + { + "run": 140315115142, + "conclusion": "success", + "started_at": "2026-11-27T04:37:29Z" + } + ] + }, + { + "name": "upstream / Container startup smoke", + "attempts": [ + { + "run": 140310856661, + "conclusion": "success", + "started_at": "2026-11-27T04:23:45Z" + }, + { + "run": 140315115169, + "conclusion": "success", + "started_at": "2026-11-27T04:37:29Z" + } + ] + }, + { + "name": "runtime-image / Image recipe contract tests", + "attempts": [ + { + "run": 140310751359, + "conclusion": "success", + "started_at": "2026-11-27T04:23:25Z" + }, + { + "run": 140314992438, + "conclusion": "success", + "started_at": "2026-11-27T04:37:06Z" + } + ] + }, + { + "name": "runtime-image / Build and smoke under the manager contract", + "attempts": [ + { + "run": 140310751457, + "conclusion": "failure", + "started_at": "2026-11-27T04:23:24Z" + }, + { + "run": 140314992429, + "conclusion": "success", + "started_at": "2026-11-27T04:37:06Z" + } + ] + }, + { + "name": "social-host / Social plugin on the pinned DS-Bot host", + "attempts": [ + { + "run": 140310751294, + "conclusion": "success", + "started_at": "2026-11-27T04:23:25Z" + }, + { + "run": 140314992324, + "conclusion": "success", + "started_at": "2026-11-27T04:37:06Z" + } + ] + }, + { + "name": "social-host / Façade and ledger unit tests", + "attempts": [ + { + "run": 140310751398, + "conclusion": "success", + "started_at": "2026-11-27T04:23:25Z" + }, + { + "run": 140314992479, + "conclusion": "success", + "started_at": "2026-11-27T04:37:07Z" + } + ] + }, + { + "name": "browser / screenshots", + "attempts": [ + { + "run": 140310751237, + "conclusion": "success", + "started_at": "2026-11-27T04:23:24Z" + }, + { + "run": 140314992347, + "conclusion": "success", + "started_at": "2026-11-27T04:37:06Z" + } + ] + }, + { + "name": "provenance / provenance", + "attempts": [ + { + "run": 140310751451, + "conclusion": "success", + "started_at": "2026-11-27T04:23:25Z" + }, + { + "run": 140314992483, + "conclusion": "success", + "started_at": "2026-11-27T04:37:06Z" + } + ] + }, + { + "name": "provenance / flybot-social-plugin-tests", + "attempts": [ + { + "run": 140310751303, + "conclusion": "success", + "started_at": "2026-11-27T04:23:24Z" + }, + { + "run": 140314992316, + "conclusion": "success", + "started_at": "2026-11-27T04:37:05Z" + } + ] + }, + { + "name": "provenance / bot-social-source-tests", + "attempts": [ + { + "run": 140310751437, + "conclusion": "success", + "started_at": "2026-11-27T04:23:25Z" + }, + { + "run": 140314992526, + "conclusion": "success", + "started_at": "2026-11-27T04:37:06Z" + } + ] + }, + { + "name": "FlyBot required checks", + "attempts": [ + { + "run": 140312199514, + "conclusion": "failure", + "started_at": "2026-11-27T04:28:04Z" + }, + { + "run": 140316751442, + "conclusion": "success", + "started_at": "2026-11-27T04:42:58Z" + } + ] + } + ], + "tier": "T3", + "note": "Replay snapshot of PR #3406 (merged 2026-11-27T04:49:03Z): the gate block is reconstructed from the record, the check-run attempts were re-read with filter=all at 05:06Z." +} From 606a431ee9a08fb8c1871f53668dc111d4a2fb9e Mon Sep 17 00:00:00 2001 From: Danyang Chen Date: Fri, 27 Nov 2026 13:27:02 +0800 Subject: [PATCH 5/6] Add the merge-decision self-test fixtures (Related #3393) --- .github/scripts/merge-decision-self-test.mjs | 148 +++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 .github/scripts/merge-decision-self-test.mjs diff --git a/.github/scripts/merge-decision-self-test.mjs b/.github/scripts/merge-decision-self-test.mjs new file mode 100644 index 000000000..4a896cb44 --- /dev/null +++ b/.github/scripts/merge-decision-self-test.mjs @@ -0,0 +1,148 @@ +// The fixtures behind `--self-test`: no network, no files. Issue #3407. +// +// The first fixture is PR #3406 (head 5072d22d, merged as 60e80980): the failed-then-green attempts +// are the real ones, read from the check-runs endpoint with filter=all on 2026-11-27. The gate block +// is the merge-gate result as it stood at merge time - the PR is closed now, so gate 1 cannot be +// re-read. +import { REQUIRED, decide, render } from './merge-decision-core.mjs' +import { REQUIRED_CHECKS, ROLLUP_CHECK } from './merge-gate-core.mjs' + +const HEAD = 'a1b2c3d4e5f60718293a4b5c6d7e8f9012345678' +const HEAD_3406 = '5072d22dbda12a31b15c4a50071fbcf1ee3fdad7' +const OLD = '0000000000000000000000000000000000000000' + +const greenHistory = () => REQUIRED.map((name) => ({ name, attempts: [{ conclusion: 'success', started_at: '2026-11-27T04:40:00Z' }] })) + +export function gatesFor(head, over = {}) { + const detail = over.detail ?? {} + const flag = (n) => detail[n] === undefined || detail[n] === true + const gates = [ + { n: 1, name: 'head and state', pass: flag(1), detail: [over.g1 ?? `open, not a draft, mergeable, base main, head ${head}`] }, + { n: 2, name: 'required checks', pass: flag(2), detail: [over.g2 ?? `${REQUIRED_CHECKS.length} required checks and FlyBot required checks completed success on ${head.slice(0, 10)}`] }, + { n: 3, name: 'evidence on this head', pass: flag(3), detail: [over.g3 ?? `QA #11; review #12 - both name ${head.slice(0, 10)}`] }, + { n: 4, name: 'risk (advisory)', pass: flag(4), detail: [over.g4 ?? 'risk:T1 - advisory only, merge is Lead\'s call'] }, + ] + return { number: over.number ?? 1, head, pass: over.pass ?? gates.every((g) => g.pass), gates } +} + +const input = (over = {}) => ({ head: HEAD, gate: gatesFor(HEAD), checks: greenHistory(), ...over }) + +// PR #3406 as it stood at merge time: same head, TWO failed attempts that were green on the re-run - +// the rollup gate and one runtime-image job. Their runs and timestamps are the real ones from the +// check-runs endpoint (filter=all, read 2026-11-27). The other fourteen checks are represented by one +// success each: they also ran twice and were green twice, which changes nothing for the verdict. The +// full sixteen-check history is in fixtures/merge-decision-3406.json, and merge-decision.test.mjs +// fails if that file and this snapshot disagree. +const RUNTIME_IMAGE = 'runtime-image / Build and smoke under the manager contract' + +export const SNAPSHOT_3406 = { + head: HEAD_3406, + gate: gatesFor(HEAD_3406, { + number: 3406, + g3: 'QA #7210114758; review #7210116921 - both name 5072d22d', + g4: 'risk:T3 - advisory only, merge is Lead\'s call', + }), + checks: [ + ...greenHistory().filter((c) => c.name !== ROLLUP_CHECK && c.name !== RUNTIME_IMAGE), + { + name: RUNTIME_IMAGE, + attempts: [ + { run: 140310751457, conclusion: 'failure', started_at: '2026-11-27T04:23:24Z', completed_at: '2026-11-27T04:23:34Z' }, + { run: 140314992429, conclusion: 'success', started_at: '2026-11-27T04:37:06Z', completed_at: '2026-11-27T04:37:16Z' }, + ], + }, + { + name: ROLLUP_CHECK, + attempts: [ + { run: 140312199514, conclusion: 'failure', started_at: '2026-11-27T04:28:04Z', completed_at: '2026-11-27T04:28:08Z' }, + { run: 140316751442, conclusion: 'success', started_at: '2026-11-27T04:42:58Z', completed_at: '2026-11-27T04:43:03Z' }, + ], + }, + ], + tier: 'T3', +} + +export const SELF_TEST_CASES = [ + { + name: 'PR #3406 replay: two failed-then-green checks on one head -> merge, recorded as flakes', + fixture: SNAPSHOT_3406, + expect: 'merge', + expectText: ['flake', '140312199514', '140316751442', '140310751457', '140314992429', 'not clean green'], + notText: ['clean green on this head'], + }, + { + name: 'all sixteen checks one success on a fresh head -> merge, no caveat', + fixture: input(), + expect: 'merge', + expectText: ['Merging', 'Tier risk:T1 is advisory'], + notText: ['flake'], + }, + { + name: 'T0 and T3 decide the same -> the tier never changes the verdict', + fixture: input({ tier: 'T0' }), + expect: 'merge', + expectText: ['risk:T0', 'advisory'], + }, + { + name: 'gate 2 fails -> hold, and the tier is not the reason', + fixture: input({ gate: gatesFor(HEAD, { number: 7, detail: { 2: false }, g2: 'not green: Web and compose planner completed|failure' }) }), + expect: 'hold', + expectText: ['gate 2 (required checks) failed', 'not green: Web and compose planner'], + }, + { + name: 'gate 3 fails (evidence binds an older head) -> hold', + fixture: input({ gate: gatesFor(HEAD, { number: 8, detail: { 3: false }, g3: `no comment carries ${HEAD.slice(0, 10)} and the QA marker` }) }), + expect: 'hold', + expectText: ['gate 3 (evidence on this head) failed'], + }, + { + name: 'two risk labels -> hold: the record is incomplete, not the tier', + fixture: input({ gate: gatesFor(HEAD, { number: 9, detail: { 4: false }, g4: 'more than one risk label: risk:T1 risk:T2' }) }), + expect: 'hold', + expectText: ['the record is incomplete, not the tier'], + }, + { + name: 'gate result belongs to an older head -> invalid, no verdict', + fixture: input({ head: HEAD, gate: gatesFor(OLD) }), + expect: 'invalid', + expectText: ['stale gate result proves nothing'], + }, + { + name: 'no run history at all -> invalid, refused instead of assumed clean', + fixture: (() => { const f = input(); delete f.checks; return f })(), + expect: 'invalid', + expectText: ['cannot tell a clean run from a flake'], + }, + { + name: 'gate 2 passes but the newest attempt of a check failed -> hold', + fixture: (() => { + const f = input() + f.checks = f.checks.map((c) => (c.name === ROLLUP_CHECK ? { name: c.name, attempts: [{ run: 5, conclusion: 'failure', started_at: '2026-11-27T04:30:00Z' }] } : c)) + return f + })(), + expect: 'hold', + expectText: ['gate 2 says this check is success, but the newest attempt'], + }, + { + name: 'history covers 15 of 16 required checks -> merge, coverage stated, uncovered not called clean', + fixture: input({ checks: greenHistory().filter((c) => c.name !== ROLLUP_CHECK) }), + expect: 'merge', + expectText: ['run history covers 15 of 16 required checks'], + }, +] + +export function selfTest() { + let bad = 0 + for (const c of SELF_TEST_CASES) { + const result = decide(c.fixture) + const text = render(result) + const ok = result.verdict === c.expect + && (c.expectText || []).every((t) => text.includes(t)) + && (c.notText || []).every((t) => !text.includes(t)) + if (!ok) bad++ + console.log(`${ok ? 'ok ' : 'FAIL'} ${c.name}: verdict ${result.verdict}, exit ${result.code}${c.expect !== result.verdict ? `, expected ${c.expect}` : ''}`) + if (!ok || process.env.VERBOSE) console.log(text.replace(/^/gm, ' ')) + } + console.log(`self-test: ${SELF_TEST_CASES.length - bad}/${SELF_TEST_CASES.length} cases as expected`) + return bad === 0 +} From 2eab81e5a31d90e5f318651691bf0a979150948e Mon Sep 17 00:00:00 2001 From: Danyang Chen Date: Fri, 27 Nov 2026 13:27:22 +0800 Subject: [PATCH 6/6] Test the merge decision against the replay fixture and the self tests (Related #3393) --- .github/scripts/merge-decision.test.mjs | 150 ++++++++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 .github/scripts/merge-decision.test.mjs diff --git a/.github/scripts/merge-decision.test.mjs b/.github/scripts/merge-decision.test.mjs new file mode 100644 index 000000000..f7b2fc10f --- /dev/null +++ b/.github/scripts/merge-decision.test.mjs @@ -0,0 +1,150 @@ +// Unit tests for the merge decision (issue #3407). No network. +// node --test .github/scripts/merge-decision.test.mjs +import assert from 'node:assert/strict' +import test from 'node:test' +import { readFileSync } from 'node:fs' +import { decide, REQUIRED, render, tierOf, validate, VERDICT } from './merge-decision-core.mjs' +import { gatesFor, SELF_TEST_CASES, SNAPSHOT_3406 } from './merge-decision-self-test.mjs' +import { ROLLUP_CHECK } from './merge-gate-core.mjs' + +const HEAD = 'a1b2c3d4e5f60718293a4b5c6d7e8f9012345678' +const green = () => REQUIRED.map((name) => ({ name, attempts: [{ conclusion: 'success', started_at: '2026-11-27T04:40:00Z' }] })) +const input = (over = {}) => ({ head: HEAD, gate: gatesFor(HEAD), checks: green(), ...over }) + +test('PR #3406 replay: merge, both real failed-then-green pairs named', () => { + const result = decide(SNAPSHOT_3406) + assert.equal(result.verdict, VERDICT.merge) + assert.equal(result.code, 0) + assert.equal(result.flakes.length, 2) + for (const run of [140312199514, 140316751442, 140310751457, 140314992429]) { + assert.match(result.record, new RegExp(String(run))) + } + assert.match(result.record, /flake/) + assert.match(result.record, /not clean green/) + assert.doesNotMatch(result.record, /clean green on this head/) + assert.equal(result.tier, 'T3') +}) + +test('the fixture file matches the snapshot: same head, same two flakes', () => { + const fixture = JSON.parse(readFileSync(new URL('./fixtures/merge-decision-3406.json', import.meta.url), 'utf8')) + const result = decide(fixture) + assert.equal(fixture.head, SNAPSHOT_3406.head) + assert.equal(result.verdict, VERDICT.merge) + assert.deepEqual(result.flakes.map((f) => f.name).sort(), ['FlyBot required checks', 'runtime-image / Build and smoke under the manager contract']) + const ids = (r) => r.flakes.flatMap((f) => f.attempts.filter((a) => a.conclusion !== 'success').map((a) => a.run)).sort((a, b) => a - b) + assert.deepEqual(ids(result), ids(decide(SNAPSHOT_3406))) +}) + +test('the tier never decides: T0 and T3 produce the same verdict and code', () => { + const verdicts = ['T0', 'T1', 'T2', 'T3'].map((tier) => decide(input({ tier }))) + assert.deepEqual(verdicts.map((r) => r.verdict), ['merge', 'merge', 'merge', 'merge']) + assert.deepEqual(verdicts.map((r) => r.code), [0, 0, 0, 0]) + for (const r of verdicts) assert.match(r.record, /advisory/) +}) + +test('tier is read from input.tier, else from the gate 4 detail', () => { + assert.equal(tierOf({ tier: 'T2' }), 'T2') + assert.equal(tierOf({ gate: gatesFor(HEAD, { g4: 'risk:T3 - advisory only, merge is Lead\'s call' }) }), 'T3') + assert.equal(tierOf({ gate: gatesFor(HEAD) }), 'T1') + assert.equal(tierOf({}), null) +}) + +test('a stale gate result is invalid input, exit 2, and no merge record', () => { + const result = decide(input({ gate: gatesFor('0000000000000000000000000000000000000000') })) + assert.equal(result.verdict, VERDICT.invalid) + assert.equal(result.code, 2) + assert.equal(result.record, undefined) + assert.match(result.blockers.join('\n'), /stale gate result/) + assert.match(render(result), /INVALID INPUT/) +}) + +test('a head that is not a 40-character sha is invalid input', () => { + assert.equal(validate(input({ head: '5072d22d' })).ok, false) + assert.equal(decide(input({ head: '5072d22d' })).code, 2) +}) + +test('gate.gates must be exactly four numbered entries', () => { + const three = input() + three.gate.gates = three.gate.gates.slice(0, 3) + three.gate.pass = true + assert.match(validate(three).errors.join('\n'), /exactly 4 entries/) + + const renumbered = input() + renumbered.gate.gates[2] = { ...renumbered.gate.gates[2], n: 9 } + assert.match(validate(renumbered).errors.join('\n'), /numbered 9/) +}) + +test('gate.pass disagreeing with its four entries is invalid input', () => { + const lying = input() + lying.gate.pass = false + assert.match(validate(lying).errors.join('\n'), /inconsistent result/) +}) + +test('each of gates 1-3 failing is a hold that names that gate', () => { + for (const n of [1, 2, 3]) { + const gate = gatesFor(HEAD, { number: 10 + n, detail: { [n]: false } }) + const result = decide(input({ gate })) + assert.equal(result.verdict, VERDICT.hold, `gate ${n}`) + assert.equal(result.code, 1) + assert.match(result.blockers.join('\n'), new RegExp(`gate ${n} \\(`)) + assert.match(result.record, /^Not merging/) + } +}) + +test('missing or doubled risk label holds, and the tier is not the reason', () => { + const gate = gatesFor(HEAD, { number: 20, detail: { 4: false }, g4: 'no risk:T0..T3 label' }) + const result = decide(input({ gate, tier: 'T3' })) + assert.equal(result.verdict, VERDICT.hold) + assert.match(result.record, /record is incomplete, not the tier/) + assert.match(result.record, /Tier risk:T3 is advisory: it is not why this is on hold/) +}) + +test('gate 2 passing while the newest run failed is a contradiction', () => { + const checks = green().map((c) => (c.name === ROLLUP_CHECK + ? { name: c.name, attempts: [{ run: 9, conclusion: 'cancelled', started_at: '2026-11-27T04:50:00Z' }] } + : c)) + const result = decide(input({ checks })) + assert.equal(result.verdict, VERDICT.hold) + assert.match(result.blockers.join('\n'), /newest attempt of "FlyBot required checks" is run 9 cancelled/) +}) + +test('partial run history still decides, states coverage, and never calls the missing names clean', () => { + const checks = green().filter((c) => c.name !== ROLLUP_CHECK) + const result = decide(input({ checks })) + assert.equal(result.verdict, VERDICT.merge) + assert.equal(result.coverage.supplied, REQUIRED.length - 1) + assert.deepEqual(result.coverage.missing, [ROLLUP_CHECK]) + assert.match(result.record, /run history covers 15 of 16 required checks/) + assert.doesNotMatch(result.record, /clean green on this head/) +}) + +test('clean history is the only case that says clean green', () => { + const result = decide(input()) + assert.match(result.record, /four merge-gate gates pass/) + assert.doesNotMatch(result.record, /flake|not clean green/) + assert.deepEqual(result.coverage.missing, []) +}) + +test('an all-success re-run is not a flake', () => { + const checks = green().map((c) => (c.name === ROLLUP_CHECK + ? { name: c.name, attempts: [ + { run: 1, conclusion: 'success', started_at: '2026-11-27T04:10:00Z' }, + { run: 2, conclusion: 'success', started_at: '2026-11-27T04:40:00Z' }, + ] } + : c)) + const result = decide(input({ checks })) + assert.equal(result.verdict, VERDICT.merge) + assert.equal(result.flakes.length, 0) +}) + +test('attempts are read in start order, not in the order they are listed', () => { + const checks = green().map((c) => (c.name === ROLLUP_CHECK + ? { name: c.name, attempts: [ + { run: 2, conclusion: 'success', started_at: '2026-11-27T04:40:00Z' }, + { run: 1, conclusion: 'failure', started_at: '2026-11-27T04:10:00Z' }, + ] } + : c)) + const result = decide(input({ checks })) + assert.equal(result.flakes.length, 1) + assert.match(result.record, /run 1 failure at 2026-11-27T04:10:00Z then run 2 success/) +})