From 98403580f4a3093309e2d96c781845ecf35e4262 Mon Sep 17 00:00:00 2001 From: Day Dreamer <87640074+HugoXK@users.noreply.github.com> Date: Fri, 20 Nov 2026 13:45:50 +0800 Subject: [PATCH] ci: start the team's own fork when head and author both match (#3248) The trigger refused every fork head, and every pull request this team opens has its head in `HugoXK/FlyBot`, so the trigger could not reach its own work. The owner's decision is a conjunction: a forked head is started only when `head repo == HugoXK/FlyBot` AND `author == HugoXK`; every other fork, and the same repository name under another author, is refused with a sentence naming both halves. A branch of this repository keeps the plain answer it always had. The three directions are pinned as tests, and the rule is written where a reader looks for it: the workflow's header comment and the pull request body. `actions: write` is documented as the one deliberate escalation, scoped to the single `Test` dispatch after an enqueue lands. Docs-not-needed: `.github/**` and `docs/testing-layers.*` are not owned paths of any subsystem page; the new workflow's row is in `docs/testing-layers.json` and `docs/testing-layers.md` is the table `--write` generated from it. --- .github/scripts/auto-queue-t1.mjs | 330 +++++++++++++++++++++++++ .github/scripts/auto-queue-t1.test.mjs | 297 ++++++++++++++++++++++ .github/workflows/auto-queue-t1.yml | 71 ++++++ docs/testing-layers.json | 9 + docs/testing-layers.md | 1 + 5 files changed, 708 insertions(+) create mode 100644 .github/scripts/auto-queue-t1.mjs create mode 100644 .github/scripts/auto-queue-t1.test.mjs create mode 100644 .github/workflows/auto-queue-t1.yml diff --git a/.github/scripts/auto-queue-t1.mjs b/.github/scripts/auto-queue-t1.mjs new file mode 100644 index 000000000..75590315e --- /dev/null +++ b/.github/scripts/auto-queue-t1.mjs @@ -0,0 +1,330 @@ +// #3248: a T0/T1 pull request needs no verdict, so `pass-label.yml` never labels it and nothing starts +// its queue entry — every one of them waits for a person to press "Add to merge queue". This is the +// trigger. It reads the open pull requests, decides one pull request at a time from the gates that +// already exist, and — only when it is switched on — enqueues with the runbook command. +// +// Report mode is the default and the shipped state: the decision reaches the job summary and nothing is +// pressed. The switch is a `workflow_dispatch` input or the repository variable +// `AUTO_QUEUE_T1_MODE=execute`, never a code edit. +// +// Which heads it may start: a branch of this repository, or the team's fork when `head repo == +// HugoXK/FlyBot` AND `author == HugoXK`. Every other fork is refused (owner decision 2026-11-20). +// +// It never comments on a pull request, never touches a label and never edits a body: a reason lives in +// the job summary, where a reader looks and where nothing can be mistaken for a review. There is no +// `--admin` and no force flag anywhere: the queue is the only way in, so the worst a bug here can do is +// put a T0/T1 pull request where a person would have put it a few minutes later. +import { execFileSync } from 'node:child_process' +import { appendFileSync, readFileSync } from 'node:fs' +import { fileURLToPath } from 'node:url' +import { createGate, loadRules } from './risk-gate.mjs' + +// A change to either of these is never queued by this trigger: the code that decides must not be the +// code that decides whether it may decide (#3248, owner constraint 3). +export const SELF_PATHS = Object.freeze([ + '.github/scripts/auto-queue-t1.mjs', + '.github/workflows/auto-queue-t1.yml', +]) + +const MODES = ['report', 'execute'] +const LOW_TIERS = ['T0', 'T1'] +const MAX_FILES = 3000 // the pull-request files API stops here, and risk-gate treats it as truncated + +const short = (sha) => String(sha ?? '').slice(0, 8) +const labelsOf = (pr) => (pr?.labels ?? []).map((label) => (typeof label === 'string' ? label : label?.name)).filter(Boolean) +const covered = (file, entries = []) => entries.some((entry) => (entry.endsWith('/') ? file.startsWith(entry) : file === entry)) + +// The switch: a dispatch input wins over the repository variable, an unreadable value fails closed. +// Report mode is what ships, so forgetting to set anything presses nothing. +export function resolveMode({ input = '', variable = '' } = {}) { + const read = (value) => { + const text = String(value ?? '').trim().toLowerCase() + return MODES.includes(text) ? text : '' + } + return read(input) || read(variable) || 'report' +} + +// The one fork this trigger starts, and only when both halves hold (owner decision 2026-11-20): +// `head repo == HugoXK/FlyBot` AND `author == HugoXK`. The conjunction is the point — the repository +// name alone would start a stranger's pull request opened from a same-named fork, and the author alone +// would start any branch that login happens to push. A head in this repository needs no fork rule: only +// a member can push there, and it keeps the plain "is it this repository" answer it always had. +export const TRUSTED_FORK = Object.freeze({ repo: 'HugoXK/FlyBot', author: 'HugoXK' }) + +// The refusals that need no API call beyond the pull request itself: a head this trigger does not +// start, a human's hold, a draft, and a base that is not this repository's `main`. They are read first +// both for the reason they carry and because they are what keeps a five-minute round inside a +// GITHUB_TOKEN's hourly budget: only a survivor of this function is worth a files or a checks request. +export function headRefusal({ repo, pr } = {}) { + const headRepo = String(pr?.head?.repo?.full_name ?? '') + const author = String(pr?.user?.login ?? '') + if (headRepo === repo) return '' + if (headRepo === TRUSTED_FORK.repo && author === TRUSTED_FORK.author) return '' + if (headRepo === '') return `the head repository is gone, so the head is not ${TRUSTED_FORK.repo} by ${TRUSTED_FORK.author}; a forked head is started only when both match` + if (headRepo === TRUSTED_FORK.repo) return `the head is ${headRepo} but its author is ${author || 'unknown'}, not ${TRUSTED_FORK.author}; a forked head is started only when both match` + return `the head is a fork this trigger does not start (${headRepo} by ${author || 'unknown author'})` +} + +function cheapRefusal({ repo, pr }) { + const head = headRefusal({ repo, pr }) + if (head) return head + if (labelsOf(pr).includes('hold')) return 'the hold label is set' + if (pr?.draft) return 'the pull request is a draft' + const base = String(pr?.base?.ref ?? '') + if (base !== 'main') return `the base is not main (${base || 'unknown'})` + return '' +} + +// A head can carry several runs of one check: a re-run is a second entry, and GitHub reads the newest +// one as the context's state. Reading the first entry instead would let a stale failure outvote the +// re-run that fixed it (and a stale success outvote a later failure), so the newest wins. +const rankOf = (check) => [String(check?.started_at ?? check?.completed_at ?? ''), Number(check?.id ?? 0)] +function checkAt(checks = [], name, sha) { + const at = checks.filter((check) => check.name === name && check.head_sha === sha) + if (at.length === 0) return undefined + return at.reduce((best, check) => { + const [aTime, aId] = rankOf(check) + const [bTime, bId] = rankOf(best) + return aTime > bTime || (aTime === bTime && aId > bId) ? check : best + }) +} + +// A check that has not finished has no conclusion. `in progress` rather than `null` so the summary +// sentence reads as a reason a person can act on. +function conclusionOf(check) { + if (check.conclusion) return check.conclusion + return String(check.status ?? 'pending').replaceAll('_', ' ') +} + +// The decision, pure: fixtures in, one answer out. `tier` comes from `risk-gate.mjs`'s own judge(), so +// these rules are never a second copy of the tier rules; the caller only passes the result. +export function decidePull({ repo = '', pr = {}, files = [], tier = '', checks = [], queued = false, gateFiles = [] }) { + const head = String(pr?.head?.sha ?? '') + const where = short(head) + const answer = (action, reason, command) => ({ number: pr?.number, tier, sha: head, action, reason, command }) + + const cheap = cheapRefusal({ repo, pr }) + if (cheap) return answer('refuse', cheap) + if (!LOW_TIERS.includes(tier)) return answer('refuse', `tier ${tier || 'unknown'} is judged by a reviewer, not by this trigger`) + + for (const file of files.map((entry) => entry?.filename).filter(Boolean)) { + if (covered(file, gateFiles)) return answer('refuse', `changed file is a gate file: ${file}`) + if (covered(file, SELF_PATHS)) return answer('refuse', `changed file is this trigger: ${file}`) + } + + for (const name of ['tests-ok', 'merge-gate']) { + const check = checkAt(checks, name, head) + if (check === undefined) return answer('refuse', `${name} has not reported at ${where}`) + if (check.conclusion !== 'success') return answer('refuse', `${name} is ${conclusionOf(check)} at ${where}`) + } + const guard = checkAt(checks, 'enqueue-guard', head) + if (guard !== undefined && guard.conclusion !== 'success') return answer('refuse', `enqueue-guard is ${conclusionOf(guard)} at ${where}`) + + if (queued) return answer('refuse', 'already in the merge queue') + return answer( + 'enqueue', + `every condition holds: tier ${tier}, tests-ok and merge-gate green at ${where}`, + `gh pr merge ${pr?.number} --auto --squash --match-head-commit ${head}`, + ) +} + +// The job summary: the only place a reason is written. One line per pull request so a reader can grep +// a number, and one machine-readable line at the end so a later round can be compared with this one. +export function renderSummary({ mode = 'report', rows = [], note = '' } = {}) { + const counts = { 'would-enqueue': 0, enqueued: 0, refused: 0, moved: 0, failed: 0 } + const verbOf = (row) => { + if (row.action === 'refuse') return 'refused' + if (row.action === 'moved') return 'moved' + if (row.action === 'failed') return 'failed' + if (row.action === 'enqueued') return 'enqueued' + return mode === 'execute' ? 'enqueued' : 'would enqueue' + } + const lines = [ + `### auto-queue-t1 — ${mode} mode`, + '', + mode === 'execute' + ? 'Execute mode: every "enqueued" line below was sent to the merge queue with its tip pinned.' + : 'Report mode: nothing was pressed. Switch: repository variable `AUTO_QUEUE_T1_MODE=execute`, or dispatch this workflow with `mode: execute`.', + '', + ] + for (const row of rows) { + const verb = verbOf(row) + const bucket = verb === 'would enqueue' ? 'would-enqueue' : verb + counts[bucket] += 1 + const detail = row.command ? `\`${row.command}\`` : String(row.reason ?? '') + lines.push(`- #${row.number} (${row.tier || 'tier unknown'}, ${short(row.sha)}): ${verb} — ${detail}`) + } + if (rows.length === 0) lines.push('- no open pull request this round') + const parts = [`mode=${mode}`] + parts.push(mode === 'execute' ? `enqueued=${counts.enqueued}` : `would-enqueue=${counts['would-enqueue']}`) + parts.push(`refused=${counts.refused}`) + for (const extra of ['moved', 'failed']) if (counts[extra] > 0) parts.push(`${extra}=${counts[extra]}`) + lines.push('', `auto-queue-t1: ${parts.join(' ')}`) + if (note) lines.push('', note) + return lines.join('\n') + '\n' +} + +// A snapshot is a read-only artefact: it carries the head shas of a past read, so acting on it would +// enqueue or chase a tip that has since moved. Report only, whatever mode was asked for. +export function guardMode({ mode = 'report', snapshot = false } = {}) { + if (snapshot && mode === 'execute') throw new Error('--snapshot is report-only; refusing to enqueue from a snapshot') + return mode +} + +const arg = (name) => { + const index = process.argv.indexOf(name) + return index > 0 ? process.argv[index + 1] : undefined +} + +const gh = (args) => execFileSync('gh', args, { encoding: 'utf8', maxBuffer: 256 << 20 }) +const ghJson = (args) => JSON.parse(gh(args)) +// One JSON value per line, the way `risk-gate.mjs` reads a paginated list: `gh api --paginate` repeats +// the page array, so the array itself is not one JSON document. +const ghList = (path) => gh(['api', '--paginate', '--jq', '.[]', path]).split('\n').filter(Boolean).map((line) => JSON.parse(line)) +const firstLine = (error) => String(error?.stderr || error?.stdout || error?.message || error).split('\n').map((line) => line.trim()).filter(Boolean)[0] ?? 'unknown error' + +const prFiles = (repo, number) => ghList(`repos/${repo}/pulls/${number}/files?per_page=100`) +const prChecks = (repo, sha) => ghList(`repos/${repo}/commits/${sha}/check-runs?per_page=100`) + .map((check) => ({ id: check.id, name: check.name, conclusion: check.conclusion, status: check.status, started_at: check.started_at, completed_at: check.completed_at, head_sha: check.head_sha })) +const prHead = (repo, number) => gh(['api', `repos/${repo}/pulls/${number}`, '--jq', '.head.sha']).trim() + +const QUEUE_QUERY = 'query($owner:String!,$name:String!,$number:Int!){repository(owner:$owner,name:$name){pullRequest(number:$number){mergeQueueEntry{state}}}}' +function inQueue(repo, number) { + const [owner, name] = String(repo).split('/') + const out = ghJson(['api', 'graphql', '-f', `query=${QUEUE_QUERY}`, '-f', `owner=${owner}`, '-f', `name=${name}`, '-F', `number=${number}`]) + return out?.data?.repository?.pullRequest?.mergeQueueEntry != null +} + +function policyGateFiles() { + const path = arg('--policy') || fileURLToPath(new URL('../../policy/gates.json', import.meta.url)) + const policy = JSON.parse(readFileSync(path, 'utf8')) + return policy.gateFiles ?? [] +} + +// One round over the live open pull requests. The cheap refusals are decided from the list response +// alone; files, checks and the queue state are read only for a pull request that survived them. +function liveRound({ repo, gate, gateFiles }) { + const pulls = ghList(`repos/${repo}/pulls?state=open&per_page=100`).sort((a, b) => a.number - b.number) + const rows = [] + for (const pr of pulls) { + const cheap = cheapRefusal({ repo, pr }) + if (cheap !== '') { + rows.push({ number: pr.number, tier: '', sha: pr?.head?.sha, action: 'refuse', reason: cheap }) + continue + } + const files = prFiles(repo, pr.number) + const tier = gate.judge(files, { truncated: files.length >= MAX_FILES }).tier + const checks = prChecks(repo, pr.head.sha) + let decision = decidePull({ repo, pr, files, tier, checks, queued: false, gateFiles }) + if (decision.action === 'enqueue') { + let queued = false + try { + queued = inQueue(repo, pr.number) + } catch (error) { + decision = { ...decision, action: 'refuse', reason: `could not read the merge-queue state (${firstLine(error)})`, command: undefined } + } + if (decision.action === 'enqueue' && queued) { + decision = decidePull({ repo, pr, files, tier, checks, queued: true, gateFiles }) + } + } + rows.push(decision) + } + return rows +} + +// A snapshot of the same inputs, for a report a reader can reproduce without the API: the shape the +// workflow reads, written to one JSON file. Nothing here presses anything. +function snapshotRound({ path, repo, gate, gateFiles }) { + const snapshot = JSON.parse(readFileSync(path, 'utf8')) + const rows = [] + for (const entry of snapshot.pulls ?? []) { + const files = entry.files ?? [] + const tier = gate.judge(files, { truncated: files.length >= MAX_FILES }).tier + rows.push(decidePull({ + repo: snapshot.repo ?? repo, + pr: entry.pr, + files, + tier, + checks: entry.checks ?? [], + queued: entry.queued === true, + gateFiles, + })) + } + return rows +} + +// Execute mode only. The head is re-read *after* the decision and pinned with `--match-head-commit`: +// a tip that moved while this round was reading other pull requests is dropped, not chased, and the +// next round decides again (constraint 2). `already queued to merge` is success — the round wanted it +// queued and it is. +function act(rows, { repo, mode }) { + let enqueued = 0 + for (const row of rows) { + if (row.action !== 'enqueue') continue + if (mode !== 'execute') continue + const fresh = prHead(repo, row.number) + if (fresh !== row.sha) { + Object.assign(row, { action: 'moved', reason: `the head moved to ${short(fresh)} after the decision; the next round decides again`, command: undefined }) + continue + } + try { + gh(['pr', 'merge', String(row.number), '--auto', '--squash', '--match-head-commit', row.sha]) + Object.assign(row, { action: 'enqueued' }) + enqueued += 1 + } catch (error) { + const text = `${error?.stderr ?? ''}${error?.stdout ?? ''}` + if (/already queued/i.test(text)) { + Object.assign(row, { action: 'enqueued' }) + enqueued += 1 + } else { + Object.assign(row, { action: 'failed', reason: firstLine(error) }) + } + } + } + return enqueued +} + +// A merge made with a GITHUB_TOKEN starts no push pipeline by itself, so the `Test` run on main that +// the merge would have started is dispatched once, here, after an enqueue landed (#3248). +function dispatchAfterEnqueue(enqueued) { + if (enqueued === 0) return '' + try { + gh(['workflow', 'run', 'test.yml', '--ref', 'main']) + return `Dispatched \`Test\` on main once after ${enqueued} enqueue(s).` + } catch (error) { + return `Could not dispatch \`Test\` on main (${firstLine(error)}); the merge itself landed.` + } +} + +function main() { + const repo = arg('--repo') || process.env.GH_REPO || process.env.GITHUB_REPOSITORY || '' + if (!repo) throw new Error('set GH_REPO or pass --repo owner/name') + const mode = guardMode({ + mode: resolveMode({ input: arg('--mode') ?? process.env.INPUT_MODE, variable: process.env.AUTO_QUEUE_T1_MODE }), + snapshot: Boolean(arg('--snapshot')), + }) + const gateFiles = policyGateFiles() + const rulesPath = arg('--rules') || process.env.RISK_RULES || fileURLToPath(new URL('../agent/risk-rules.json', import.meta.url)) + const gate = createGate(loadRules(rulesPath)) + + const snapshot = arg('--snapshot') + const rows = snapshot + ? snapshotRound({ path: snapshot, repo, gate, gateFiles }) + : liveRound({ repo, gate, gateFiles }) + const enqueued = act(rows, { repo, mode }) + const summary = renderSummary({ mode, rows, note: mode === 'execute' ? dispatchAfterEnqueue(enqueued) : '' }) + + const out = arg('--summary') + if (out) appendFileSync(out, summary) + if (process.env.GITHUB_STEP_SUMMARY) appendFileSync(process.env.GITHUB_STEP_SUMMARY, summary) + if (!out && !process.env.GITHUB_STEP_SUMMARY) process.stdout.write(summary) +} + +if (process.argv[1] === fileURLToPath(import.meta.url)) { + try { + main() + } catch (error) { + console.error(`auto-queue-t1: ${error?.message ?? error}`) + process.exit(1) + } +} diff --git a/.github/scripts/auto-queue-t1.test.mjs b/.github/scripts/auto-queue-t1.test.mjs new file mode 100644 index 000000000..e13fe72ba --- /dev/null +++ b/.github/scripts/auto-queue-t1.test.mjs @@ -0,0 +1,297 @@ +// The T0/T1 queue trigger (#3248): a T0/T1 pull request needs no verdict, so no `pass` label ever +// appears and nothing starts its queue entry — every one of them waits for a person to press the +// button. These tests pin the decision function only: fixtures in, one decision out, no network and +// no `gh`. One test per refusal, because each refusal is a promise that this trigger will not press +// the button in that shape. +import { test } from 'node:test' +import assert from 'node:assert/strict' +import { SELF_PATHS, decidePull, guardMode, renderSummary, resolveMode } from './auto-queue-t1.mjs' + +const REPO = 'ThinkFlowLab/FlyBot' +const HEAD = '4a1c2d3e4f5061728394a5b6c7d8e9f001122334' + +const pr = (over = {}) => ({ + number: 4001, + draft: false, + labels: ['agent-pr'], + base: { ref: 'main' }, + head: { sha: HEAD, repo: { full_name: REPO } }, + user: { login: 'HugoXK' }, + ...over, +}) + +const checks = (over = {}) => [ + { name: 'tests-ok', conclusion: 'success', head_sha: HEAD }, + { name: 'merge-gate', conclusion: 'success', head_sha: HEAD }, + { name: 'enqueue-guard', conclusion: 'success', head_sha: HEAD }, + ...over.extra ?? [], +] + +const files = (...names) => names.map((filename) => ({ filename, additions: 3, deletions: 0, patch: '+ a\n' })) + +// The command this trigger is allowed to run, and the only one: no `--admin`, no force, pinned tip. +const COMMAND = `gh pr merge 4001 --auto --squash --match-head-commit ${HEAD}` + +const decide = (over = {}) => decidePull({ + repo: REPO, + pr: pr(), + files: files('docs/subsystems/team.md'), + tier: 'T0', + checks: checks(), + queued: false, + gateFiles: ['.github/scripts/merge-gate.mjs', 'policy/'], + ...over, +}) + +test('#3248: a T0 pull request with green checks is enqueued, with the runbook command', () => { + const r = decide() + assert.equal(r.action, 'enqueue') + assert.equal(r.command, COMMAND, 'the command is the runbook one: --auto --squash, pinned to the tip') + assert.equal(r.sha, HEAD) + assert.equal(r.tier, 'T0') +}) + +test('#3248: a T1 pull request (changelog fragment) is enqueued the same way', () => { + const r = decide({ pr: pr({ number: 4002 }), files: files('changelog.d/3248.added.md'), tier: 'T1' }) + assert.equal(r.action, 'enqueue') + assert.equal(r.command, COMMAND.replace('4001', '4002')) +}) + +test('#3248: a T2 change is refused — a reviewer judges it, not this trigger', () => { + const r = decide({ tier: 'T2', files: files('plugins/timeline/client.js') }) + assert.equal(r.action, 'refuse') + assert.match(r.reason, /T2/) + assert.match(r.reason, /reviewer/) + assert.equal(r.command, undefined, 'a refusal carries no command') +}) + +test('#3248: a T3 change is refused for the same reason', () => { + const r = decide({ tier: 'T3', files: files('.github/scripts/enqueue-rules.mjs') }) + assert.equal(r.action, 'refuse') + assert.match(r.reason, /T3/) + assert.equal(r.command, undefined) +}) + +test('#3248: a draft is refused', () => { + const r = decide({ pr: pr({ draft: true }) }) + assert.equal(r.action, 'refuse') + assert.equal(r.reason, 'the pull request is a draft') +}) + +test('#3248: the hold label is refused, and it is refused before any other reason is read', () => { + const r = decide({ pr: pr({ labels: ['agent-pr', 'hold'] }), tier: 'T3' }) + assert.equal(r.action, 'refuse') + assert.equal(r.reason, 'the hold label is set') +}) + +// The trusted head is the conjunction the owner asked for: `head repo == HugoXK/FlyBot` AND +// `author == HugoXK`. Three directions, because each single half alone is a head this trigger must not +// start: our own fork is started, a stranger's fork is refused, and our fork's name under another +// author is refused. +test("#3248: the team's own fork is started when the head repository and the author both match", () => { + const r = decide({ + pr: pr({ head: { sha: HEAD, repo: { full_name: 'HugoXK/FlyBot' } }, user: { login: 'HugoXK' } }), + }) + assert.equal(r.action, 'enqueue') + assert.equal(r.command, COMMAND) +}) + +test("#3248: a stranger's fork is refused, naming the repository and its author", () => { + const r = decide({ pr: pr({ head: { sha: HEAD, repo: { full_name: 'someone/FlyBot' } }, user: { login: 'someone' } }) }) + assert.equal(r.action, 'refuse') + assert.match(r.reason, /fork/) + assert.match(r.reason, /someone\/FlyBot/) + assert.equal(r.command, undefined) +}) + +test('#3248: the trusted fork repository name under another author is refused, and the reason names both halves', () => { + const r = decide({ + pr: pr({ head: { sha: HEAD, repo: { full_name: 'HugoXK/FlyBot' } }, user: { login: 'elsewhere' } }), + }) + assert.equal(r.action, 'refuse') + assert.match(r.reason, /not HugoXK/) + assert.match(r.reason, /both match/) + assert.equal(r.command, undefined) +}) + +test('#3248: a pull request whose head repository is gone is refused, not assumed local', () => { + const r = decide({ pr: pr({ head: { sha: HEAD, repo: null } }) }) + assert.equal(r.action, 'refuse') + assert.match(r.reason, /fork/) +}) + +test('#3248: a base other than main is refused', () => { + const r = decide({ pr: pr({ base: { ref: 'dev/integration' } }) }) + assert.equal(r.action, 'refuse') + assert.equal(r.reason, 'the base is not main (dev/integration)') +}) + +test('#3248: tests-ok missing at the head is refused, naming the sha', () => { + const r = decide({ checks: checks().filter((c) => c.name !== 'tests-ok') }) + assert.equal(r.action, 'refuse') + assert.match(r.reason, /tests-ok has not reported at 4a1c2d3e/) +}) + +test('#3248: tests-ok green on another head does not count', () => { + const r = decide({ + checks: [ + { name: 'tests-ok', conclusion: 'success', head_sha: 'ffffffffffffffffffffffffffffffffffffffff' }, + { name: 'merge-gate', conclusion: 'success', head_sha: HEAD }, + ], + }) + assert.equal(r.action, 'refuse') + assert.match(r.reason, /tests-ok has not reported at 4a1c2d3e/) +}) + +test('#3248: a red tests-ok is refused, naming the conclusion', () => { + const r = decide({ + checks: [ + { name: 'tests-ok', conclusion: 'failure', head_sha: HEAD }, + { name: 'merge-gate', conclusion: 'success', head_sha: HEAD }, + ], + }) + assert.equal(r.action, 'refuse') + assert.equal(r.reason, 'tests-ok is failure at 4a1c2d3e') +}) + +test('#3248: a pending tests-ok is refused', () => { + const r = decide({ + checks: [ + { name: 'tests-ok', conclusion: null, status: 'in_progress', head_sha: HEAD }, + { name: 'merge-gate', conclusion: 'success', head_sha: HEAD }, + ], + }) + assert.equal(r.action, 'refuse') + assert.equal(r.reason, 'tests-ok is in progress at 4a1c2d3e') +}) + +test('#3248: merge-gate has to be green at the head too', () => { + const r = decide({ checks: checks().filter((c) => c.name !== 'merge-gate') }) + assert.equal(r.action, 'refuse') + assert.match(r.reason, /merge-gate has not reported/) + + const red = decide({ + checks: [ + { name: 'tests-ok', conclusion: 'success', head_sha: HEAD }, + { name: 'merge-gate', conclusion: 'failure', head_sha: HEAD }, + ], + }) + assert.equal(red.reason, 'merge-gate is failure at 4a1c2d3e') +}) + +test('#3248: enqueue-guard, when it exists at the head, must be green', () => { + const r = decide({ + checks: [...checks().filter((c) => c.name !== 'enqueue-guard'), { name: 'enqueue-guard', conclusion: 'failure', head_sha: HEAD }], + }) + assert.equal(r.action, 'refuse') + assert.equal(r.reason, 'enqueue-guard is failure at 4a1c2d3e') +}) + +test('#3248: a newer re-run decides — a stale failure does not outvote the run that fixed it', () => { + const r = decide({ + checks: [ + { name: 'tests-ok', conclusion: 'failure', head_sha: HEAD, started_at: '2026-11-20T01:00:00Z', id: 11 }, + { name: 'tests-ok', conclusion: 'success', head_sha: HEAD, started_at: '2026-11-20T02:00:00Z', id: 12 }, + { name: 'merge-gate', conclusion: 'success', head_sha: HEAD }, + ], + }) + assert.equal(r.action, 'enqueue') +}) + +test('#3248: and a newer failure outvotes an older success at the same head', () => { + const r = decide({ + checks: [ + { name: 'tests-ok', conclusion: 'success', head_sha: HEAD, started_at: '2026-11-20T01:00:00Z', id: 12 }, + { name: 'tests-ok', conclusion: 'failure', head_sha: HEAD, started_at: '2026-11-20T02:00:00Z', id: 11 }, + { name: 'merge-gate', conclusion: 'success', head_sha: HEAD }, + ], + }) + assert.equal(r.action, 'refuse') + assert.equal(r.reason, 'tests-ok is failure at 4a1c2d3e') +}) + +test('#3248: enqueue-guard absent is not a refusal — the body is judged at the queue entry', () => { + const r = decide({ checks: checks().filter((c) => c.name !== 'enqueue-guard') }) + assert.equal(r.action, 'enqueue') +}) + +test('#3248: a changed gate file is refused, even when the tier says T0', () => { + const r = decide({ files: files('policy/gates.json'), tier: 'T0' }) + assert.equal(r.action, 'refuse') + assert.equal(r.reason, 'changed file is a gate file: policy/gates.json') +}) + +test('#3248: a change to this trigger script is refused', () => { + const r = decide({ files: files('.github/scripts/auto-queue-t1.mjs'), tier: 'T0' }) + assert.equal(r.action, 'refuse') + assert.equal(r.reason, 'changed file is this trigger: .github/scripts/auto-queue-t1.mjs') +}) + +test('#3248: a change to this workflow is refused', () => { + const r = decide({ files: files('.github/workflows/auto-queue-t1.yml'), tier: 'T0' }) + assert.equal(r.action, 'refuse') + assert.equal(r.reason, 'changed file is this trigger: .github/workflows/auto-queue-t1.yml') +}) + +test('#3248: a pull request already in the merge queue is left alone', () => { + const r = decide({ queued: true }) + assert.equal(r.action, 'refuse') + assert.equal(r.reason, 'already in the merge queue') + assert.equal(r.command, undefined) +}) + +test('#3248: no decision can carry an admin or force flag', () => { + const all = [ + decide(), + decide({ tier: 'T2' }), + decide({ queued: true }), + decide({ files: files('core/client.js'), tier: 'T1' }), + ] + for (const r of all) { + assert.equal(r.command === undefined ? 'none' : r.command.replace(/\s+/g, ' '), r.command === undefined ? 'none' : r.command) + if (r.command !== undefined) { + assert.doesNotMatch(r.command, /admin/, 'no admin flag, ever') + assert.doesNotMatch(r.command, /force|--no-verify/, 'no force flag, ever') + assert.match(r.command, /^gh pr merge \d+ --auto --squash --match-head-commit [0-9a-f]{40}$/) + } + } + assert.deepEqual(SELF_PATHS, ['.github/scripts/auto-queue-t1.mjs', '.github/workflows/auto-queue-t1.yml']) +}) + +test('#3248: mode is report unless the switch says execute', () => { + assert.equal(resolveMode({}), 'report', 'a checkout with no switch presses nothing') + assert.equal(resolveMode({ variable: 'execute' }), 'execute') + assert.equal(resolveMode({ variable: ' EXECUTE ' }), 'execute') + assert.equal(resolveMode({ variable: 'yes' }), 'report', 'an unknown value fails closed') + assert.equal(resolveMode({ input: 'execute' }), 'execute', 'a dispatch may ask for one executing run') + assert.equal(resolveMode({ input: 'report', variable: 'execute' }), 'report', 'the dispatch input wins') + assert.equal(resolveMode({ input: '', variable: 'execute' }), 'execute') +}) + +test('#3248: the summary says would enqueue in report mode and enqueued in execute mode', () => { + const allow = decide({ pr: pr({ number: 4001 }) }) + const refuse = decide({ pr: pr({ number: 4002 }), tier: 'T2', files: files('plugins/timeline/client.js') }) + const rows = [allow, refuse] + + const report = renderSummary({ mode: 'report', rows }) + assert.match(report, /auto-queue-t1 — report mode/) + assert.match(report, /- #4001 \(T0, 4a1c2d3e\): would enqueue — `gh pr merge 4001 --auto --squash --match-head-commit 4a1c2d3e4f5061728394a5b6c7d8e9f001122334`/) + assert.match(report, /- #4002 \(T2, 4a1c2d3e\): refused — tier T2 is judged by a reviewer/) + assert.match(report, /auto-queue-t1: mode=report would-enqueue=1 refused=1/) + + const execute = renderSummary({ mode: 'execute', rows }) + assert.match(execute, /- #4001 \(T0, 4a1c2d3e\): enqueued — `gh pr merge 4001/) + assert.match(execute, /auto-queue-t1: mode=execute enqueued=1 refused=1/) +}) + +test('#3248: a round that decided nothing still writes one line a reader can grep', () => { + const text = renderSummary({ mode: 'report', rows: [] }) + assert.match(text, /auto-queue-t1: mode=report would-enqueue=0 refused=0/) +}) + +test('#3248: a snapshot can be reported on but never acted from', () => { + assert.equal(guardMode({}), 'report') + assert.equal(guardMode({ mode: 'report', snapshot: true }), 'report') + assert.equal(guardMode({ mode: 'execute', snapshot: false }), 'execute') + assert.throws(() => guardMode({ mode: 'execute', snapshot: true }), /report-only/) +}) diff --git a/.github/workflows/auto-queue-t1.yml b/.github/workflows/auto-queue-t1.yml new file mode 100644 index 000000000..b137ab8b7 --- /dev/null +++ b/.github/workflows/auto-queue-t1.yml @@ -0,0 +1,71 @@ +name: Auto-queue T0/T1 + +# #3248. A T2/T3 pull request enters the merge queue when `pass-label.yml` writes its `pass` label from a +# counting verdict. A T0/T1 pull request needs no verdict, so no label ever appears and nothing starts its +# queue entry: every one of them waits for a person to press "Add to merge queue" (5 of the 12 T1 merges +# in the week to 2026-11-20 did, 17–37 minutes after they were ready). This workflow is that trigger. +# +# Report mode is the default and the shipped state. The decision — "would enqueue" or "refused, and why" — +# goes to the job summary and nothing else is written: no comment, no label, no body edit. The switch to +# pressing the button is the repository variable `AUTO_QUEUE_T1_MODE=execute`, set by FlyBot Lead after +# the owner reads a day of summaries, or a single `workflow_dispatch` with `mode: execute`. +# +# Three events, so a ready pull request is seen twice over: the `Test` run it just finished (fast path), a +# five-minute sweep (the safety net for a Run that ended before its checks were all reported), and a +# manual dispatch. The job checks out `main` and never the pull request's code, so a fork's tree is never +# executed here. +# +# Which heads this trigger may start: a branch of this repository, or the team's fork when BOTH hold — +# `head repo == HugoXK/FlyBot` AND `author == HugoXK`. Every other fork is refused, including a head whose +# repository carries that name but whose author does not (owner decision 2026-11-20; `headRefusal()` in +# `auto-queue-t1.mjs` is the one implementation, and the three tests pin the three directions). +# +# `actions: write` is a deliberate, single-purpose escalation beyond the design's list in the issue: it +# exists only for the one `Test` dispatch on `main` after an enqueue lands, because a merge made with a +# `GITHUB_TOKEN` starts no push pipeline by itself. No other write happens in this job — no other +# workflow dispatch, no secret read, no repository write — and the permission belongs to this job alone: +# do not copy it into another job or widen it to `actions: write` elsewhere in this repository. +on: + workflow_run: + workflows: [Test] + types: [completed] + schedule: + - cron: '*/5 * * * *' + workflow_dispatch: + inputs: + mode: + description: 'report (presses nothing) or execute (enqueues)' + type: choice + options: [report, execute] + default: report + +concurrency: + group: auto-queue-t1 + cancel-in-progress: false + +permissions: + contents: read + pull-requests: write + checks: read + actions: write + +jobs: + decide: + name: auto-queue-t1 + runs-on: ubuntu-latest + timeout-minutes: 10 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + ref: main + persist-credentials: false + - uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 + with: + node-version: 24 + - name: Decide, and enqueue only in execute mode + env: + GH_TOKEN: ${{ github.token }} + GH_REPO: ${{ github.repository }} + AUTO_QUEUE_T1_MODE: ${{ vars.AUTO_QUEUE_T1_MODE }} + INPUT_MODE: ${{ inputs.mode }} + run: node .github/scripts/auto-queue-t1.mjs diff --git a/docs/testing-layers.json b/docs/testing-layers.json index 29e2406d9..2da62da3e 100644 --- a/docs/testing-layers.json +++ b/docs/testing-layers.json @@ -274,6 +274,15 @@ "gate": false, "runs": "daily 02:30 Asia/Shanghai, and workflow_dispatch", "reason": "The release-beat watchdog (#1209, second half): reads the newest `v*` tag, and files at most one `Release beat missing: ` issue per day when no tag carries today. It runs no tests of its own, does not touch release.yml, and gates nothing." + }, + { + "id": "auto-queue-t1", + "workflow": ".github/workflows/auto-queue-t1.yml", + "kind": "workflow-only", + "layer": "none", + "gate": false, + "runs": "`Test` completed, every 5 minutes, and workflow_dispatch", + "reason": "Starts the merge-queue entry of a T0/T1 pull request, which no `pass` label ever triggers (#3248). Report mode writes \"would enqueue / refused + reason\" to the job summary and presses nothing; the decision function is tested by `.github/scripts/auto-queue-t1.test.mjs` in the L1 `scripts` suite, so this workflow has no test file of its own and produces no required check." } ] } diff --git a/docs/testing-layers.md b/docs/testing-layers.md index e16de1b44..405943563 100644 --- a/docs/testing-layers.md +++ b/docs/testing-layers.md @@ -118,5 +118,6 @@ The tables below are generated: edit `docs/testing-layers.json`, then run | `review-verdict.yml` | workflow-only | none | no | pull_request_review (submitted) | Says, in one warning and the run summary, that a PASS posted as a review body whose Written-by names someone outside the policy's writers does not count (#801). Read-only by construction (permissions: contents: read), so a pull request that edits it can only change what its own review job prints; a workflow-only layer with no gate. | | `agent-audience.yml` | workflow-only | none | no | pull_request (opened, edited, synchronize, reopened, ready_for_review), issue_comment (created) and workflow_dispatch | Reads the body's Owner:/Reviewer: lines and the calls written after the current tip, and prints where they disagree (#911). Report only: continue-on-error, read-only permissions, no comment and no label, so it gates nothing. | | `release-beat.yml` | workflow-only | none | no | daily 02:30 Asia/Shanghai, and workflow_dispatch | The release-beat watchdog (#1209, second half): reads the newest `v*` tag, and files at most one `Release beat missing: ` issue per day when no tag carries today. It runs no tests of its own, does not touch release.yml, and gates nothing. | +| `auto-queue-t1.yml` | workflow-only | none | no | `Test` completed, every 5 minutes, and workflow_dispatch | Starts the merge-queue entry of a T0/T1 pull request, which no `pass` label ever triggers (#3248). Report mode writes "would enqueue / refused + reason" to the job summary and presses nothing; the decision function is tested by `.github/scripts/auto-queue-t1.test.mjs` in the L1 `scripts` suite, so this workflow has no test file of its own and produces no required check. |