Public: anyone can read this; the crew can change it.
Revert "A "Revert …" pull left open past a day is landed or closed with its reason, by the forge" #15915
Check · Review · Land · Live done — Landed and live in production.
revert-15810 → main 0 commits
2 files
changed +0
−231
src/maintain/duties/revert_pulls.ts deleted −133
| @@ -1,133 +0,0 @@ | ||
| 1 | /** | |
| 2 | * Revert pulls left open (#15804): an open pull titled `Revert "…"` older than a day is noise on | |
| 3 | * the board once its fate is plain. The duty reads the base branch's log once per repository and | |
| 4 | * decides each one: | |
| 5 | * - the revert is already on the base (a commit titled the same): closed, "already on main <oid>"; | |
| 6 | * - the original is still on the base and the revert's check is red: closed, naming the original; | |
| 7 | * - the original is still on the base and the check is green: listed for a person to land. The | |
| 8 | * engine never merges (`core/engine.ts`): the crew proposes and a person lands. | |
| 9 | * Anything else (no check yet, original not on the base) is left alone. | |
| 10 | * Never closed: a pull a person opened, one a person commented on, one assigned to a person. | |
| 11 | * Every close is the engine's own (`setIssueState` reason `parked`), with one comment saying why, | |
| 12 | * and is a line in the run's actions, so `GET /api/v1/maintenance/runs` shows it. | |
| 13 | */ | |
| 14 | ||
| 15 | import type { Database } from "bun:sqlite"; | |
| 16 | import type { App } from "../../app.ts"; | |
| 17 | import { addComment, setIssueState } from "../../actions/issues.ts"; | |
| 18 | import { engineActor } from "../../core/engine.ts"; | |
| 19 | import { getIssueById } from "../../core/issues.ts"; | |
| 20 | import { getPull } from "../../core/pulls.ts"; | |
| 21 | import { getRepoById } from "../../core/repos.ts"; | |
| 22 | import { checkSummary } from "../../core/runs.ts"; | |
| 23 | import { commitLog, type Commit } from "../../git/read.ts"; | |
| 24 | import { repoDir } from "../../git/repos.ts"; | |
| 25 | import type { Duty, Finding } from "../duty.ts"; | |
| 26 | ||
| 27 | export const REVERT_AFTER_MS = 24 * 3_600_000; | |
| 28 | /** How far back the base branch's log is read for the original and an earlier revert. */ | |
| 29 | export const LOG_DEPTH = 2000; | |
| 30 | ||
| 31 | export const REVERT_SQL = `SELECT issue.id, issue.repo_id, issue.number, issue.title, | |
| 32 | u.is_agent AS agent, | |
| 33 | EXISTS (SELECT 1 FROM user a WHERE a.id = issue.assignee_id AND a.is_agent = 0) AS person_assigned, | |
| 34 | EXISTS (SELECT 1 FROM comment c JOIN user cu ON cu.id = c.author_id | |
| 35 | WHERE c.issue_id = issue.id AND cu.is_agent = 0) AS person_said | |
| 36 | FROM issue JOIN pull ON pull.issue_id = issue.id JOIN user u ON u.id = issue.author_id | |
| 37 | WHERE issue.state = 'open' AND issue.kind = 'pull' AND issue.title LIKE 'Revert "%"' | |
| 38 | AND issue.created_at < ?1 | |
| 39 | ORDER BY issue.repo_id, issue.number`; | |
| 40 | ||
| 41 | interface Row { id: string; repo_id: string; number: number; title: string; agent: number; person_assigned: number; person_said: number } | |
| 42 | ||
| 43 | export type Verdict = | |
| 44 | | { readonly kind: "already"; readonly oid: string } | |
| 45 | | { readonly kind: "red"; readonly original: string } | |
| 46 | | { readonly kind: "green"; readonly original: string } | |
| 47 | | { readonly kind: "wait" }; | |
| 48 | ||
| 49 | /** What a revert's title reverts: `Revert "X"` gives X, anything else undefined. */ | |
| 50 | export function revertedSubject(title: string): string | undefined { | |
| 51 | const m = /^Revert "(.+)"$/.exec(title.trim()); | |
| 52 | return m?.[1]; | |
| 53 | } | |
| 54 | ||
| 55 | /** A subject as it lands: itself, or squashed with ` (#N)` after it. */ | |
| 56 | const landedAs = (subject: string, wanted: string): boolean => subject === wanted || subject.startsWith(`${wanted} (#`); | |
| 57 | ||
| 58 | /** | |
| 59 | * The verdict on one revert pull, from the base's log and the check of its head. Plain data in and | |
| 60 | * out. | |
| 61 | */ | |
| 62 | export function judge(title: string, log: readonly Pick<Commit, "oid" | "subject">[], check: string): Verdict { | |
| 63 | const original = revertedSubject(title); | |
| 64 | if (original === undefined) return { kind: "wait" }; | |
| 65 | const already = log.find((c) => landedAs(c.subject, title.trim())); | |
| 66 | if (already) return { kind: "already", oid: already.oid }; | |
| 67 | const kept = log.find((c) => landedAs(c.subject, original)); | |
| 68 | if (!kept) return { kind: "wait" }; | |
| 69 | if (check === "failed") return { kind: "red", original: kept.oid }; | |
| 70 | if (check === "ok") return { kind: "green", original: kept.oid }; | |
| 71 | return { kind: "wait" }; | |
| 72 | } | |
| 73 | ||
| 74 | const short = (oid: string): string => oid.slice(0, 10); | |
| 75 | ||
| 76 | /** One pass over the open revert pulls older than a day: closes and findings. */ | |
| 77 | export async function settleReverts(app: App, now: Date): Promise<{ findings: Finding[]; actions: string[] }> { | |
| 78 | const db: Database = app.db; | |
| 79 | const cutoff = new Date(now.getTime() - REVERT_AFTER_MS).toISOString(); | |
| 80 | const rows = db.query<Row, [string]>(REVERT_SQL).all(cutoff); | |
| 81 | const actions: string[] = []; | |
| 82 | const green: string[] = []; | |
| 83 | const logs = new Map<string, Commit[]>(); | |
| 84 | for (const row of rows) { | |
| 85 | const repo = getRepoById(db, row.repo_id); | |
| 86 | const pull = repo && getPull(db, repo.id, row.number); | |
| 87 | if (!repo || !pull) continue; | |
| 88 | const key = `${repo.id}\0${pull.baseBranch}`; | |
| 89 | let log = logs.get(key); | |
| 90 | if (log === undefined) { | |
| 91 | const read = await commitLog(repoDir(app.reposRoot, repo.ownerName, repo.name), `refs/heads/${pull.baseBranch}`, { limit: LOG_DEPTH }); | |
| 92 | logs.set(key, (log = read.ok ? read.value : [])); | |
| 93 | } | |
| 94 | const verdict = judge(row.title, log, checkSummary(db, pull.id, pull.headOid).state); | |
| 95 | const name = `${repo.ownerName}/${repo.name}#${row.number}`; | |
| 96 | if (verdict.kind === "wait") continue; | |
| 97 | if (verdict.kind === "green") { | |
| 98 | green.push(`${name} (original ${short(verdict.original)})`); | |
| 99 | continue; | |
| 100 | } | |
| 101 | // A person's say stands: their pull, their comment or their assignment keeps it open. | |
| 102 | if (row.agent !== 1 || row.person_assigned === 1 || row.person_said === 1) continue; | |
| 103 | const why = | |
| 104 | verdict.kind === "already" | |
| 105 | ? `already on ${pull.baseBranch} as ${short(verdict.oid)}` | |
| 106 | : `its check is red and the original ${short(verdict.original)} is still on ${pull.baseBranch}`; | |
| 107 | const issue = getIssueById(db, pull.id); | |
| 108 | const by = engineActor(db); | |
| 109 | if (!issue || !setIssueState(app, by, repo, issue, "closed", { reason: "parked" }).ok) continue; | |
| 110 | const said = addComment(app, by, repo, issue, `Closed by the forge: this revert is ${why}.`); | |
| 111 | actions.push(`closed ${name} "${row.title}": ${why}${said.ok ? "" : " (the comment was refused)"}`); | |
| 112 | } | |
| 113 | const findings: Finding[] = | |
| 114 | green.length === 0 | |
| 115 | ? [] | |
| 116 | : [ | |
| 117 | { | |
| 118 | summary: `${green.length} revert pull${green.length === 1 ? "" : "s"} open over a day, green, waiting for a person to land: ${green.slice(0, 5).join(", ")}${green.length > 5 ? ", …" : ""}`, | |
| 119 | probe: { kind: "query", what: "open pulls titled Revert \"…\" over a day old whose original is on the base and whose check is green", number: green.length, at: now.toISOString() }, | |
| 120 | }, | |
| 121 | ]; | |
| 122 | return { findings, actions }; | |
| 123 | } | |
| 124 | ||
| 125 | export const duty: Duty = { | |
| 126 | name: "revert-pulls", | |
| 127 | intervalMs: 6 * 3_600_000, | |
| 128 | budget: { runsPerDay: 8, dollarsPerDay: 1, reason: "every six hours is four a day; the rest is room for a catch-up, and it spends no money" }, | |
| 129 | async run({ now, app }) { | |
| 130 | if (app === undefined) return { findings: [], actions: [] }; | |
| 131 | return settleReverts(app, now); | |
| 132 | }, | |
| 133 | }; |
test/revert-pulls-duty.test.ts deleted −98
| @@ -1,98 +0,0 @@ | ||
| 1 | /** | |
| 2 | * Revert pulls left open past a day (#15804): green is listed, red and already-on-main are closed. | |
| 3 | */ | |
| 4 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 5 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 6 | import { tmpdir } from "node:os"; | |
| 7 | import { join } from "node:path"; | |
| 8 | import { createApp, type App } from "../src/app.ts"; | |
| 9 | import { commitToBranch } from "../src/actions/repos.ts"; | |
| 10 | import { getIssueById, listComments } from "../src/core/issues.ts"; | |
| 11 | import { createPull } from "../src/core/pulls.ts"; | |
| 12 | import { createRepo, type Repo } from "../src/core/repos.ts"; | |
| 13 | import { createRun } from "../src/core/runs.ts"; | |
| 14 | import { createUser, type User } from "../src/core/users.ts"; | |
| 15 | import { repoDir } from "../src/git/repos.ts"; | |
| 16 | import { duty, judge, revertedSubject } from "../src/maintain/duties/revert_pulls.ts"; | |
| 17 | import { startMaintain } from "../src/maintain/runner.ts"; | |
| 18 | import { latest } from "../src/maintain/runs.ts"; | |
| 19 | import { quietLogs } from "./helpers/globals.ts"; | |
| 20 | ||
| 21 | quietLogs(); | |
| 22 | ||
| 23 | const ok = <T>(r: { ok: true; value: T } | { ok: false; error: unknown }): T => { | |
| 24 | if (!r.ok) throw new Error(JSON.stringify(r.error)); | |
| 25 | return r.value; | |
| 26 | }; | |
| 27 | const HOUR = 3_600_000; | |
| 28 | const NOW = new Date(); | |
| 29 | let root = "", app: App, ada: User, bot: User, repo: Repo, dir = ""; | |
| 30 | const main: Record<string, string> = {}; | |
| 31 | ||
| 32 | beforeAll(async () => { | |
| 33 | root = await mkdtemp(join(tmpdir(), "forge-revert-pulls-")); | |
| 34 | app = createApp({ dataDir: root, port: 0, forgeRepo: "ada/forge" }); | |
| 35 | ada = ok(await createUser(app.db, { origin: "seed", trusted: true, username: "ada", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true })); | |
| 36 | bot = ok(await createUser(app.db, { username: "builder", email: "[email protected]", agent: true, origin: "agent" })); | |
| 37 | repo = ok(await createRepo(app.db, app.reposRoot, { ownerId: ada.id, name: "forge", visibility: "public" })); | |
| 38 | dir = repoDir(app.reposRoot, ada.username, repo.name); | |
| 39 | for (const [i, message] of ["Add widget", "Add knob", "Add gadget", 'Revert "Add gadget"', "Add dial"].entries()) | |
| 40 | main[message] = ok(await commitToBranch(app, ada, repo, { message, files: [{ path: `f${i}.txt`, content: `${i}\n` }] })).commitOid; | |
| 41 | }); | |
| 42 | afterAll(async () => { | |
| 43 | app.close(); | |
| 44 | await rm(root, { recursive: true, force: true }); | |
| 45 | }); | |
| 46 | ||
| 47 | let seq = 0; | |
| 48 | async function revertPull(title: string, check: "ok" | "failed", hoursOld = 48, author: User = bot) { | |
| 49 | const branch = `revert-${++seq}`; | |
| 50 | ok(await commitToBranch(app, ada, repo, { message: title, branch, files: [{ path: `${branch}.txt`, content: "x\n" }] } as any)); | |
| 51 | const pull = ok(await createPull(app.db, dir, { repoId: repo.id, authorId: author.id, title, baseBranch: "main", headBranch: branch })); | |
| 52 | app.db.query("UPDATE issue SET created_at = ?1 WHERE id = ?2").run(new Date(NOW.getTime() - hoursOld * HOUR).toISOString(), pull.id); | |
| 53 | const run = createRun(app.db, { repoId: repo.id, kind: "check", issueId: pull.id, commitOid: pull.headOid }); | |
| 54 | app.db.query("UPDATE run SET state = ?1, summary = ?1, finished_at = ?2 WHERE id = ?3").run(check, NOW.toISOString(), run.id); | |
| 55 | return pull; | |
| 56 | } | |
| 57 | const state = (id: string) => getIssueById(app.db, id)!.state; | |
| 58 | const said = (id: string) => listComments(app.db, id).map((c) => c.body).join("\n"); | |
| 59 | ||
| 60 | test("the title names what it reverts, and only that shape", () => { | |
| 61 | expect(revertedSubject('Revert "Add widget"')).toBe("Add widget"); | |
| 62 | expect(revertedSubject("Add widget")).toBeUndefined(); | |
| 63 | expect(judge('Revert "Add widget"', [{ oid: "a1", subject: "Add widget (#12)" }], "ok")).toEqual({ kind: "green", original: "a1" }); | |
| 64 | expect(judge('Revert "Add widget"', [], "failed")).toEqual({ kind: "wait" }); | |
| 65 | expect(judge('Revert "Add widget"', [{ oid: "a1", subject: "Add widget" }], "pending")).toEqual({ kind: "wait" }); | |
| 66 | }); | |
| 67 | ||
| 68 | test("green, red and already reverted: listed, closed with the original, closed as already on main; each receipted", async () => { | |
| 69 | const green = await revertPull('Revert "Add widget"', "ok"); | |
| 70 | const red = await revertPull('Revert "Add knob"', "failed"); | |
| 71 | const already = await revertPull('Revert "Add gadget"', "ok"); | |
| 72 | const fresh = await revertPull('Revert "Add dial"', "failed", 2); | |
| 73 | const byPerson = await revertPull('Revert "Add dial"', "failed", 48, ada); | |
| 74 | const discussed = await revertPull('Revert "Add dial"', "failed"); | |
| 75 | app.db.query("INSERT INTO comment (id, issue_id, author_id, body) VALUES ('c1', ?1, ?2, 'keep this')").run(discussed.id, ada.id); | |
| 76 | ||
| 77 | await startMaintain(app, { duties: [duty] }).tick(NOW); | |
| 78 | ||
| 79 | expect(state(green.id)).toBe("open"); | |
| 80 | expect(state(red.id)).toBe("closed"); | |
| 81 | expect(said(red.id)).toContain(`the original ${main["Add knob"]!.slice(0, 10)} is still on main`); | |
| 82 | expect(state(already.id)).toBe("closed"); | |
| 83 | expect(said(already.id)).toContain(`already on main as ${main['Revert "Add gadget"']!.slice(0, 10)}`); | |
| 84 | for (const kept of [fresh, byPerson, discussed]) expect(state(kept.id)).toBe("open"); | |
| 85 | ||
| 86 | const [run] = latest(app.db, 1); | |
| 87 | expect(run!.duty).toBe("revert-pulls"); | |
| 88 | expect(run!.outcome).toBe("ok"); | |
| 89 | expect(run!.actions).toHaveLength(2); | |
| 90 | expect(run!.actions.join("\n")).toContain(`#${red.number} "Revert "Add knob""`); | |
| 91 | expect(run!.actions.join("\n")).toContain(`#${already.number} `); | |
| 92 | expect(String(run!.findings[0])).toContain(`ada/forge#${green.number}`); | |
| 93 | }); | |
| 94 | ||
| 95 | test("a second pass closes nothing more", async () => { | |
| 96 | await startMaintain(app, { duties: [duty] }).tick(new Date(NOW.getTime() + 7 * HOUR)); | |
| 97 | expect(latest(app.db, 1)[0]!.actions).toHaveLength(0); | |
| 98 | }); |
new above
offline