Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/2026-12-02-bot-merge-guard.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improvement: the maintenance bot no longer asks to merge a pull request GitHub would refuse. A merge that conflicts with main, or that a review still requests changes on (the bot's own included), is neither proposed nor run, so the owner never approves a merge that then fails.
2 changes: 1 addition & 1 deletion docs/MAINTENANCE_BOT.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ The maintenance bot does the routine part of the [daily pass](MAINTENANCE.md) on

A **decision** is a question only the owner can answer, such as an OK for a port or a shared hook. The bot does not file it as a to-do: it proposes the reply it recommends (a comment or board reply with `intent: "decision"`), which is posted after 24 hours unless the owner stops it. An answer that grants what the rules do not already allow (a waiver, a scope exception) or touches a unit's safety is marked `exception` and waits for Approve; the policy also asks whenever the answer's own words name a waiver or an exception. Only a decision the owner stopped becomes a to-do.

Only an ask, a failure and an urgent to-do make the owner's phone buzz. A window arrives silently, and the morning report names the next eight that can still be stopped. Each pass also drops what waits but can no longer happen (the item closed, new commits arrived, someone replied), so every message in Telegram is live. A merge whose required checks have not all reported waits for the next pass instead of failing. The model also sees the open to-dos: it adds one to-do per cause and drops those it sees settled (`drop` in its proposals).
Only an ask, a failure and an urgent to-do make the owner's phone buzz. A window arrives silently, and the morning report names the next eight that can still be stopped. Each pass also drops what waits but can no longer happen (the item closed, new commits arrived, someone replied), so every message in Telegram is live. A merge whose required checks have not all reported waits for the next pass instead of failing, and one that conflicts with main or that a review still requests changes on is not proposed or run. The model also sees the open to-dos: it adds one to-do per cause and drops those it sees settled (`drop` in its proposals).

The bot never pushes, releases, deploys, touches secrets, or works on firmware, a unit or an emulator. Those become **to-dos** for a desktop session, each with the `/modwerk-*` command that does it, kept in the ledger (`maintenance_bot_todos`, migration 0092). They are the owner's private list. Work others could take is public already as its GitHub issue or pull request; the bot posts nothing to the agent board for it and opens no issues for to-dos. In any session at the owner's computer, `/modwerk-todo` lists them (`npm run todo`, signed in with the owner's `npm run board -- login`), runs the command, and ticks it off. A to-do whose item closes is dropped on the next daily run. Every action pins the head commit and comment count it was proposed against. If either changes before the action runs, the action is dropped and proposed again next pass. A merge is checked again for green checks at the moment it runs. The Worker then merges it in the owner's name (`POST /api/maintenance-bot/merge`) at exactly that commit, which starts the usual push workflows. A model-proposed release of someone else's module needs the owner's Approve. An explicit [reviewed merge](DEVELOPER_WORKFLOW.md#who-reviews-a-first-release) uses the worker's authenticated `/merge/reviewed` route and the same merge operation and ledger after the trusted GitHub job verifies the human review. A paused worker or an owner veto for that head stops it. Registered authors keep their separate self-merge workflow.

Expand Down
3 changes: 2 additions & 1 deletion scripts/maintenance-bot/act.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { tagDiscordLinks } from '../../server/site-link-tags.ts'
import { existsSync, readFileSync } from 'node:fs'
import { join } from 'node:path'
import { HARD_ONLY } from './lanes.mjs'
import { TODO_COMMAND, actionId, classify } from './policy.mjs'
import { TODO_COMMAND, actionId, classify, mergeBlock } from './policy.mjs'
import { MARKER, approveRun, boardGet, discord, github, ledger, pages, readBoardTarget, readDiscordTarget, readTarget } from './lib.mjs'
import { pilotReport } from './eval-pilot.mjs'
import { dailyReport, weeklyRetro } from './report.mjs'
Expand Down Expand Up @@ -206,6 +206,7 @@ async function carryOut(action) {
return ['done', '']
case 'merge': {
if (now.draft || !now.checksGreen) return ['expired', now.draft ? 'it became a draft' : 'its checks are no longer green']
if (mergeBlock(now)) return ['expired', mergeBlock(now)]
// The Worker merges with the owner's token, at exactly the commit that was checked, so the merge counts as
// the owner's for the site's release gate and starts the usual push workflows.
let result
Expand Down
3 changes: 2 additions & 1 deletion scripts/maintenance-bot/lib.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ export async function readTarget(api, number) {
const files = await pages(api, '/pulls/' + number + '/files')
const waitingRuns = ((await api('/actions/runs?status=action_required&head_sha=' + pull.head.sha + '&per_page=30')).workflow_runs ?? []).length
const checks = await checkSummary(api, pull.head.sha)
return { ...target, waitingRuns, draft: !!pull.draft, head: pull.head.sha, baseRef: pull.base.ref, headRef: pull.head.ref, files: files.flatMap(file => [file.filename, file.previous_filename].filter(Boolean)), firstTime: ['FIRST_TIMER', 'FIRST_TIME_CONTRIBUTOR', 'NONE'].includes(pull.author_association), checks, checksGreen: checks === 'green' }
const reviews = (await pages(api, '/pulls/' + number + '/reviews')).map(review => ({ user: review.user?.id, state: review.state }))
return { ...target, waitingRuns, mergeable: pull.mergeable, reviews, draft: !!pull.draft, head: pull.head.sha, baseRef: pull.base.ref, headRef: pull.head.ref, files: files.flatMap(file => [file.filename, file.previous_filename].filter(Boolean)), firstTime: ['FIRST_TIMER', 'FIRST_TIME_CONTRIBUTOR', 'NONE'].includes(pull.author_association), checks, checksGreen: checks === 'green' }
}

/** The agent board's public reads (threads and claims need no sign-in). */
Expand Down
13 changes: 12 additions & 1 deletion scripts/maintenance-bot/policy.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,19 @@ export function checkText(body, author) {

const waives = proposal => proposal.exception === true || WAIVES.test([proposal.summary, proposal.reason, proposal.body].join(' '))

/**
* Why GitHub would refuse a merge whose checks are green, or '': a conflict with main, or a reviewer whose latest
* verdict still requests changes, the bot's own included (#2110 and #2344 failed on these, November 2026).
*/
export function mergeBlock(target) {
if (target.mergeable === false) return 'it conflicts with main'
const latest = new Map((target.reviews ?? []).filter(review => ['APPROVED', 'CHANGES_REQUESTED', 'DISMISSED'].includes(review.state)).map(review => [review.user, review.state]))
return [...latest.values()].includes('CHANGES_REQUESTED') ? 'a review still requests changes' : ''
}

/**
* Decides one proposal against the target's live state. `target` is what the executor read from GitHub:
* { number, isPull, author, state, draft, head, comments, files, firstTime, checksGreen }.
* { number, isPull, author, state, draft, head, comments, files, firstTime, checksGreen, mergeable, reviews }.
* Returns { tier, windowHours, id, payload } or { reject }.
*/
export function classify(proposal, target, existingLabels = new Set()) {
Expand Down Expand Up @@ -100,6 +110,7 @@ export function classify(proposal, target, existingLabels = new Set()) {
if (!target.isPull) return { reject: 'only a pull request merges' }
if (target.draft) return { reject: 'it is a draft' }
if (!target.checksGreen) return { reject: 'its checks are not green' }
if (mergeBlock(target)) return { reject: mergeBlock(target) }
if (proposal.head !== target.head) return { reject: 'it was reviewed at another commit' }
// A merge runs in the owner's name (the site only publishes owner merges), so someone else's release always
// asks the owner. The owner marked their own pull request ready, so it merges after a short window.
Expand Down
9 changes: 9 additions & 0 deletions scripts/maintenance-bot/policy.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ describe('maintenance bot policy', () => {
expect(classify(merge(), pull({ state: 'closed' })).reject).toBeTruthy()
})

it('never proposes a merge GitHub would refuse: a conflict, or a change request its reviewer has not lifted', () => {
expect(classify(merge(), pull({ mergeable: false })).reject).toBe('it conflicts with main')
expect(classify(merge(), pull({ mergeable: null })).reject).toBeUndefined()
const asked = { user: 1, state: 'CHANGES_REQUESTED' }
expect(classify(merge(), pull({ reviews: [asked, { user: 1, state: 'COMMENTED' }] })).reject).toBe('a review still requests changes')
expect(classify(merge(), pull({ reviews: [{ user: 2, state: 'APPROVED' }, asked] })).reject).toBe('a review still requests changes')
for (const state of ['APPROVED', 'DISMISSED']) expect(classify(merge(), pull({ reviews: [asked, { user: 1, state }] })).reject).toBeUndefined()
})

it('gives every action it accepts a summary the ledger takes', () => {
// A merge once lost its summary to a shadowed name, and the Worker refused the whole pass (26 November 2026).
const thread = { kind: 'thread', id: 't1', number: 0, author: 'alice', to: 'repeat98', replies: 0, state: 'open' }
Expand Down
2 changes: 1 addition & 1 deletion scripts/maintenance-bot/prompt.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Your answer is the proposals, as structured output: `{"actions": [...], "todos":
- A **decision**: a question only the owner can answer (an OK for a port, whether a gesture or menu may be taken, which of two plans, a shared hook) is a `comment` (or `board_reply`) with `"intent": "decision"` and the reply you recommend, in the owner's voice. It is posted after 24 hours unless the owner stops it. Add `"exception": true` when your answer grants anything the rules do not already allow (a waiver, a scope exception, an OK the modding guidelines would refuse, a merge without its test record) or touches a unit's safety: that one waits for the owner's Approve and never runs without it. When you are unsure which it is, mark it an exception. The `summary` is the question with your answer ("OK Kits 0.1.1 in the shared Part Edit menu: yes"), the `reason` the case for and against in two sentences. Recommend what the guidelines, MAINTENANCE.md and the owner's earlier answers in the ledger point to. Only if the ledger shows the owner stopped your answer and the question still waits does it become a to-do.
- A first-time contributor's waiting checks are started for you; do not propose that.
- `review`: `body` and `event` (`COMMENT` or `REQUEST_CHANGES`), on a contributor's pull request, with the changes the rules require and how to make them (`npm run pr:ready`, the guide's anchor). Waits 15 minutes, so it posts at the next pass. A pull request's `botReviews` are your own earlier reviews of it, in full. A push or a draft toggle is no reason to review again: when nothing you asked for was settled and nothing new is wrong, propose no review, because your last one still stands. If the author marked it ready since, one sentence is enough, naming what still stands. Otherwise write a follow-up that names only what changed since your last review: what is now settled, and anything new. Point to the rest in one line ("The rest of my review from 29 November still stands") instead of repeating it. Never restate an ask that is already in an earlier review.
- `merge` (hard lane only): `head` (the exact head commit you reviewed). Only for a pull request that is not a draft, has green checks, passes the review rules in MAINTENANCE.md and the review skill, and is the right next step. The owner's own pull request merges after 1 hour, anyone else's after 12; the policy turns it into an owner question when the pull request touches CI, the bot, migrations, dependencies, sign-in, the base or SDK code outside module folders, publishes someone else's module, or comes from a first-time contributor. A base for any device never merges on a timer: a pull request from or into a `base/…` branch, or one that changes a base's sources or built files, always waits for the owner's Approve after its review. Propose its merge only when it is not a draft and its description records the builds that ran on a unit, and say in the reason which ran and who reviewed it; a machine's cumulative draft stays a draft until the owner marks it ready. A module release notifies its followers when it merges: say so in the reason.
- `merge` (hard lane only): `head` (the exact head commit you reviewed). Only for a pull request that is not a draft, has green checks, does not conflict with main, has no change request still standing (your own included: when the author met it, say so in a `review`; GitHub refuses the merge until the owner dismisses it), passes the review rules in MAINTENANCE.md and the review skill, and is the right next step. The owner's own pull request merges after 1 hour, anyone else's after 12; the policy turns it into an owner question when the pull request touches CI, the bot, migrations, dependencies, sign-in, the base or SDK code outside module folders, publishes someone else's module, or comes from a first-time contributor. A base for any device never merges on a timer: a pull request from or into a `base/…` branch, or one that changes a base's sources or built files, always waits for the owner's Approve after its review. Propose its merge only when it is not a draft and its description records the builds that ran on a unit, and say in the reason which ran and who reviewed it; a machine's cumulative draft stays a draft until the owner marks it ready. A module release notifies its followers when it merges: say so in the reason.
- `close`: `body` (the reason, to the author) and `reason_code` (`completed` or `not_planned`). For issues that are fixed and live, answered, duplicates (link the original), superseded, or a week past an unanswered question from us. Waits 12 hours. Closing someone else's pull request always needs the owner's OK; propose it only with a clear reason from MAINTENANCE.md.

Do not propose the same thing twice for an unchanged item, nor say again on GitHub what an earlier comment or review of yours already said, and never re-propose what the ledger shows the owner stopped, unless the item has changed since. At most 40 actions a run.
Expand Down