Public: anyone can read this; the crew can change it.
An approved pull that has waited a week is landed, rebased or closed by the engine, not only named #12494
Check · Review · Land · Live done — Landed and live in production.
mason/forge:mason/12491-an-approved-pull-that-has-waited-a-week → main 3 commits fork
4 files
changed +565
−0
docs/features/approved-week.md added +9
| @@ -0,0 +1,9 @@ | ||
| 1 | # An approved pull that has waited a week | |
| 2 | ||
| 3 | The tick names the oldest approved pull and the needs stack lists those approved a week ago. Past a week the engine also acts (`src/crew/approved_week.ts`, called from `autonomyTick`), in any repository; only landing needs autonomy: | |
| 4 | ||
| 5 | - **Landed** when it merges clean and every check on its head is green. Only an autonomous repository lands it, through its merge queue (`landIfGreen`); where autonomy is off a person lands it and the needs row stays. It acts as the person who turned autonomy on, else the repository's owner when that is a person; with neither it does nothing. | |
| 6 | - **Handed to its builder** to bring up to date when main moved under it: one conflict run, never past the failed-attempt limit since the approval. | |
| 7 | - **Closed** when its repository is archived, or every issue it closes was dropped, with one comment saying why. | |
| 8 | ||
| 9 | The git merge test runs once per pair of tips, so a pull that has not moved costs nothing on the next tick. A held pull, a red or missing check, a person holding it and a pull with no builder are left alone: the needs row stays the fallback. At most three pulls are acted on per tick. |
src/crew/approved_week.ts added +267
| @@ -0,0 +1,267 @@ | ||
| 1 | /** | |
| 2 | * An approved pull that has waited a week is acted on, not only named (#12491). | |
| 3 | * The tick names the oldest approved pull and the needs stack lists the ones | |
| 4 | * approved a week ago; this is the engine doing what it can decide, once a | |
| 5 | * pull has waited `WAITED_MS` since its latest approval, in any repository: | |
| 6 | * | |
| 7 | * - its repository is archived, or every issue it closes was dropped: the | |
| 8 | * pull is closed, with one line saying why; | |
| 9 | * - it conflicts with its base: its own builder is handed a bring-up-to-date | |
| 10 | * run (never past `CHECK_FAILED_LIMIT` tries since the approval); | |
| 11 | * - it merges clean and its head's checks all pass, and the repository is | |
| 12 | * autonomous: it lands through the merge queue (`landIfGreen`), whose review | |
| 13 | * and check rules are the ones every other landing meets. Where autonomy is | |
| 14 | * off a person lands, never the engine: the needs row stays. | |
| 15 | * | |
| 16 | * Whatever it cannot decide — a held pull, no person to act as, a red | |
| 17 | * check, no builder — it leaves alone, and the needs row stays its fallback. | |
| 18 | * It reads the database each tick, remembers only which two tips it already | |
| 19 | * tried a merge between (so an unmoved pull costs no git), and acts on at most | |
| 20 | * `PER_TICK` pulls so one tick is never a burst of landings. | |
| 21 | */ | |
| 22 | ||
| 23 | import type { App } from "../app.ts"; | |
| 24 | import { addComment, setIssueState } from "../actions/issues.ts"; | |
| 25 | import { getAgent, listCrewRoles } from "../core/agents.ts"; | |
| 26 | import { getHold } from "../core/holds.ts"; | |
| 27 | import { getIssue, getIssueById, type Issue } from "../core/issues.ts"; | |
| 28 | import { closesFromBody, getPull, pullDir, type Pull } from "../core/pulls.ts"; | |
| 29 | import { getRepoById, type Repo } from "../core/repos.ts"; | |
| 30 | import { reviewSummary } from "../core/reviews.ts"; | |
| 31 | import { activeRunFor, verdictForHead } from "../core/runs.ts"; | |
| 32 | import { getUserById, type User } from "../core/users.ts"; | |
| 33 | import { mergeable, resolveOid } from "../git/merge.ts"; | |
| 34 | import { createLog } from "../kernel/log.ts"; | |
| 35 | import { landIfGreen } from "./autonomy.ts"; | |
| 36 | import { readConflictRoom } from "./conflict_room.ts"; | |
| 37 | import { CHECK_FAILED_LIMIT, queueAgentRun } from "./dispatch.ts"; | |
| 38 | import { ownBuilder } from "./finishing.ts"; | |
| 39 | ||
| 40 | const log = createLog("approved-week"); | |
| 41 | ||
| 42 | /** How long an approved pull waits, since its latest approval, before the engine acts. */ | |
| 43 | export const WAITED_MS = 7 * 24 * 3_600_000; | |
| 44 | /** Pulls acted on in one tick. */ | |
| 45 | export const PER_TICK = 3; | |
| 46 | ||
| 47 | export type WeekMove = | |
| 48 | | { | |
| 49 | readonly pull: number; | |
| 50 | readonly repo: string; | |
| 51 | readonly did: "landed" | "closed" | "queued rebase"; | |
| 52 | readonly why: string; | |
| 53 | } | |
| 54 | | { readonly pull: number; readonly repo: string; readonly did: "left"; readonly why: string }; | |
| 55 | ||
| 56 | interface Row { | |
| 57 | id: string; | |
| 58 | repo_id: string; | |
| 59 | approved_at: string; | |
| 60 | } | |
| 61 | ||
| 62 | /** | |
| 63 | * Open pulls whose latest approval is at least a week old with no change asked after it, | |
| 64 | * oldest first. | |
| 65 | */ | |
| 66 | function waitedRows(app: App, now: Date): Row[] { | |
| 67 | const cutoff = new Date(now.getTime() - WAITED_MS).toISOString(); | |
| 68 | return app.db | |
| 69 | .query<Row, [string]>( | |
| 70 | `SELECT i.id AS id, i.repo_id AS repo_id, a.at AS approved_at | |
| 71 | FROM pull p | |
| 72 | JOIN issue i ON i.id = p.issue_id | |
| 73 | JOIN (SELECT issue_id, MAX(created_at) AS at FROM review WHERE state = 'approved' GROUP BY issue_id) a | |
| 74 | ON a.issue_id = i.id | |
| 75 | WHERE i.state = 'open' AND p.merged_at IS NULL AND a.at <= ?1 | |
| 76 | AND NOT EXISTS (SELECT 1 FROM review c WHERE c.issue_id = i.id AND c.state = 'changes_requested' AND c.created_at > a.at) | |
| 77 | ORDER BY a.at ASC, i.number ASC`, | |
| 78 | ) | |
| 79 | .all(cutoff); | |
| 80 | } | |
| 81 | ||
| 82 | /** | |
| 83 | * Who the engine acts as: the person who turned autonomy on, else the | |
| 84 | * repository's owner when that is a person. Never an agent (the crew | |
| 85 | * proposes; a person lands), and `undefined` when there is no one. | |
| 86 | */ | |
| 87 | function actorFor(app: App, repo: Repo): User | undefined { | |
| 88 | const named = repo.autonomyBy === null ? undefined : getUserById(app.db, repo.autonomyBy); | |
| 89 | const by = named ?? getUserById(app.db, repo.ownerId); | |
| 90 | return by !== undefined && by.kind === "user" ? by : undefined; | |
| 91 | } | |
| 92 | ||
| 93 | /** The issues the pull says it closes, as they are now. */ | |
| 94 | function closedByPull(app: App, repo: Repo, pull: Pull): Issue[] { | |
| 95 | return closesFromBody(pull.body).flatMap((n) => { | |
| 96 | const issue = getIssue(app.db, repo.id, n); | |
| 97 | return issue !== undefined && issue.kind === "issue" ? [issue] : []; | |
| 98 | }); | |
| 99 | } | |
| 100 | ||
| 101 | /** Whether the pull has issues it closes and every one of them was dropped. */ | |
| 102 | function issueDropped(app: App, repo: Repo, pull: Pull): boolean { | |
| 103 | const issues = closedByPull(app, repo, pull); | |
| 104 | return issues.length > 0 && issues.every((i) => i.state === "closed" && i.closeReason === "dropped"); | |
| 105 | } | |
| 106 | ||
| 107 | /** A comment after the act it reports; a refusal is logged, never lost. */ | |
| 108 | function noted(app: App, by: User, repo: Repo, issue: Issue, body: string): void { | |
| 109 | const said = addComment(app, by, repo, issue, body); | |
| 110 | if (!said.ok) log.error("approved-week comment refused", { pull: issue.number, why: String(said.error) }); | |
| 111 | } | |
| 112 | ||
| 113 | /** Close the pull with one line, the comment after the state change it reports. */ | |
| 114 | function close(app: App, by: User, repo: Repo, pull: Pull, why: string): boolean { | |
| 115 | const issue = getIssueById(app.db, pull.id); | |
| 116 | if (issue === undefined || issue.state !== "open") return false; | |
| 117 | if (!setIssueState(app, by, repo, issue, "closed", { reason: "parked" }).ok) return false; | |
| 118 | noted(app, by, repo, issue, `Closed after waiting a week approved: ${why}.`); | |
| 119 | return true; | |
| 120 | } | |
| 121 | ||
| 122 | /** Bring-up-to-date runs queued since `since`, finished or not. */ | |
| 123 | function rebasesSince(app: App, issueId: string, since: string): number { | |
| 124 | return ( | |
| 125 | app.db | |
| 126 | .query<{ n: number }, [string, string]>( | |
| 127 | "SELECT COUNT(*) AS n FROM run WHERE kind = 'agent' AND reason = 'conflict' AND issue_id = ?1 AND created_at > ?2", | |
| 128 | ) | |
| 129 | .get(issueId, since)?.n ?? 0 | |
| 130 | ); | |
| 131 | } | |
| 132 | ||
| 133 | /** Decide one pull, and act. */ | |
| 134 | async function decide(app: App, repo: Repo, pull: Pull, approvedAt: string): Promise<WeekMove> { | |
| 135 | const name = `${repo.ownerName}/${repo.name}`; | |
| 136 | const left = (why: string): WeekMove => ({ pull: pull.number, repo: name, did: "left", why }); | |
| 137 | const by = actorFor(app, repo); | |
| 138 | if (by === undefined) return left("no person to act as"); | |
| 139 | ||
| 140 | if (repo.archivedAt !== null) | |
| 141 | return close(app, by, repo, pull, "its repository is archived") | |
| 142 | ? { pull: pull.number, repo: name, did: "closed", why: "its repository is archived" } | |
| 143 | : left("could not close it"); | |
| 144 | if (issueDropped(app, repo, pull)) | |
| 145 | return close(app, by, repo, pull, "the issue it closes was dropped") | |
| 146 | ? { pull: pull.number, repo: name, did: "closed", why: "the issue it closes was dropped" } | |
| 147 | : left("could not close it"); | |
| 148 | ||
| 149 | const dir = await pullDir(app.db, app.reposRoot, repo, pull); | |
| 150 | const baseTip = await resolveOid(dir, `refs/heads/${pull.baseBranch}`); | |
| 151 | if (baseTip === undefined) return left("its base is unreadable"); | |
| 152 | const verdict = await tryMerge(dir, pull, baseTip); | |
| 153 | if (verdict === "unreadable") return left("it could not be tried against its base"); | |
| 154 | if (verdict !== "clean") return rebase(app, by, repo, pull, approvedAt, verdict.files, name); | |
| 155 | if (repo.autonomy !== "autonomous" || repo.autonomyBy === null) return left("a person lands it"); | |
| 156 | ||
| 157 | const checks = verdictForHead(app.db, pull.id, pull.headOid); | |
| 158 | if (checks.length === 0) return left("its head has no check"); | |
| 159 | if (checks.some((c) => c.state !== "ok")) return left("its check is not green"); | |
| 160 | const landed = await land(app, repo, pull); | |
| 161 | if (!landed) return left("the landing was refused"); | |
| 162 | const issue = getIssueById(app.db, pull.id); | |
| 163 | if (issue !== undefined) noted(app, by, repo, issue, "Landed after waiting a week approved, clean and green."); | |
| 164 | return { pull: pull.number, repo: name, did: "landed", why: "approved a week ago, clean and green" }; | |
| 165 | } | |
| 166 | ||
| 167 | /** Land through the autonomous repository's merge queue; whether the pull is merged after. */ | |
| 168 | async function land(app: App, repo: Repo, pull: Pull): Promise<boolean> { | |
| 169 | await landIfGreen(app, repo, pull); | |
| 170 | const after = getPull(app.db, repo.id, pull.number); | |
| 171 | return after !== undefined && after.mergedAt !== null; | |
| 172 | } | |
| 173 | ||
| 174 | type Tried = "clean" | "unreadable" | { readonly files: readonly string[] }; | |
| 175 | ||
| 176 | /** The last merge tried for each pull, by the two tips it was between. */ | |
| 177 | const judged = new Map<string, { readonly between: string; readonly result: Tried }>(); | |
| 178 | const REMEMBERED = 500; | |
| 179 | ||
| 180 | /** Whether the head merges into the base; asked of git once per pair of tips. */ | |
| 181 | async function tryMerge(dir: string, pull: Pull, baseTip: string): Promise<Tried> { | |
| 182 | const between = `${pull.headOid}:${baseTip}`; | |
| 183 | const before = judged.get(pull.id); | |
| 184 | if (before?.between === between) return before.result; | |
| 185 | const merged = await mergeable(dir, baseTip, pull.headOid); | |
| 186 | const result: Tried = merged.ok | |
| 187 | ? "clean" | |
| 188 | : merged.error.kind === "conflict" | |
| 189 | ? { files: merged.error.files } | |
| 190 | : "unreadable"; | |
| 191 | if (result !== "unreadable") { | |
| 192 | if (judged.size >= REMEMBERED) judged.clear(); | |
| 193 | judged.set(pull.id, { between, result }); | |
| 194 | } | |
| 195 | return result; | |
| 196 | } | |
| 197 | ||
| 198 | /** Hand a conflicted pull to its own builder, for a bring-up-to-date run. */ | |
| 199 | function rebase( | |
| 200 | app: App, | |
| 201 | by: User, | |
| 202 | repo: Repo, | |
| 203 | pull: Pull, | |
| 204 | approvedAt: string, | |
| 205 | files: readonly string[], | |
| 206 | name: string, | |
| 207 | ): WeekMove { | |
| 208 | const left = (why: string): WeekMove => ({ pull: pull.number, repo: name, did: "left", why }); | |
| 209 | const issue = getIssueById(app.db, pull.id); | |
| 210 | if (issue === undefined || issue.state !== "open") return left("it is not open"); | |
| 211 | if (activeRunFor(app.db, "agent", pull.id) !== undefined) return left("a run is on it"); | |
| 212 | // A person's name on it is a person holding it. | |
| 213 | if (issue.assigneeId !== null && getAgent(app.db, issue.assigneeId) === undefined) return left("a person has it"); | |
| 214 | if (rebasesSince(app, pull.id, approvedAt) >= CHECK_FAILED_LIMIT) return left("bring-up-to-date runs have not moved it"); | |
| 215 | const builders = listCrewRoles(app.db, repo.id) | |
| 216 | .filter((c) => c.role === "builder") | |
| 217 | .map((c) => c.agent); | |
| 218 | const forkOwner = getRepoById(app.db, pull.headRepoId)?.ownerId; | |
| 219 | const builder = ownBuilder(issue, builders, new Map(), forkOwner); | |
| 220 | if (builder === undefined) return left("no builder on the crew"); | |
| 221 | if (readConflictRoom(app.db) <= 0) return left("the check lane has no room"); | |
| 222 | const run = queueAgentRun(app, repo, issue, builder, "conflict", undefined, { | |
| 223 | notBefore: new Date().toISOString(), | |
| 224 | attempt: 0, | |
| 225 | summary: `main moved: ${files.join(", ")}`, | |
| 226 | }); | |
| 227 | if (run === undefined) return left("the run was not queued"); | |
| 228 | noted( | |
| 229 | app, | |
| 230 | by, | |
| 231 | repo, | |
| 232 | issue, | |
| 233 | `Approved a week ago and main has moved under it: @${builder.user.username} is asked to bring it up to date.`, | |
| 234 | ); | |
| 235 | return { pull: pull.number, repo: name, did: "queued rebase", why: `main moved: ${files.join(", ")}` }; | |
| 236 | } | |
| 237 | ||
| 238 | /** | |
| 239 | * One pass over every pull approved a week ago or more. Returns what it did | |
| 240 | * with each it acted on or could not decide (the latter carries `did: "left"`). | |
| 241 | */ | |
| 242 | export async function sweepApprovedWeek(app: App, now: Date = new Date()): Promise<WeekMove[]> { | |
| 243 | const moves: WeekMove[] = []; | |
| 244 | let acted = 0; | |
| 245 | for (const row of waitedRows(app, now)) { | |
| 246 | if (acted >= PER_TICK) break; | |
| 247 | const repo = getRepoById(app.db, row.repo_id); | |
| 248 | const issue = getIssueById(app.db, row.id); | |
| 249 | const pull = repo === undefined || issue === undefined ? undefined : getPull(app.db, repo.id, issue.number); | |
| 250 | if (repo === undefined || pull === undefined) continue; | |
| 251 | // The approval counts on the head it judged. | |
| 252 | const seen = reviewSummary(app.db, pull.id, pull.headOid); | |
| 253 | if (seen.approvals === 0 || seen.changesRequested > 0) continue; | |
| 254 | if (getHold(app.db, pull.id) !== undefined) continue; | |
| 255 | try { | |
| 256 | const move = await decide(app, repo, pull, row.approved_at); | |
| 257 | moves.push(move); | |
| 258 | if (move.did !== "left") { | |
| 259 | acted++; | |
| 260 | log.info("approved a week", { repo: move.repo, pull: move.pull, did: move.did, why: move.why }); | |
| 261 | } | |
| 262 | } catch (error) { | |
| 263 | log.error("approved-week pass failed on a pull", { repo: `${repo.ownerName}/${repo.name}`, pull: pull.number, error: String(error) }); | |
| 264 | } | |
| 265 | } | |
| 266 | return moves; | |
| 267 | } |
src/crew/autonomy.ts +8
| @@ -17,6 +17,7 @@ import { tickBets } from "../actions/bets.ts"; | ||
| 17 | 17 | import { betsInUse } from "../core/bets.ts"; |
| 18 | 18 | import { onlyListDocs, resolveListConflicts } from "../actions/list-conflicts.ts"; |
| 19 | 19 | import { closedToday, closeStale } from "./stale_pulls.ts"; |
| 20 | import { sweepApprovedWeek } from "./approved_week.ts"; | |
| 20 | 21 | import type { Database } from "bun:sqlite"; |
| 21 | 22 | import type { App } from "../app.ts"; |
| 22 | 23 | import { ceilingReached, forgeCeiling } from "../flow/ceiling.ts"; |
| @@ -1262,6 +1263,13 @@ export async function autonomyTick( | ||
| 1262 | 1263 | } catch (error) { |
| 1263 | 1264 | log.error("stranded sweep failed", { error: String(error) }); |
| 1264 | 1265 | } |
| 1266 | // An approved pull that has waited a week is landed, rebased or closed (#12491), | |
| 1267 | // in any repository. | |
| 1268 | try { | |
| 1269 | await sweepApprovedWeek(app, now); | |
| 1270 | } catch (error) { | |
| 1271 | log.error("approved-week sweep failed", { error: String(error) }); | |
| 1272 | } | |
| 1265 | 1273 | // Three days before a trusted source lapses, a note (#8937). |
| 1266 | 1274 | try { |
| 1267 | 1275 | warnExpiring(app, now); |
test/approved-week.test.ts added +281
| @@ -0,0 +1,281 @@ | ||
| 1 | /** | |
| 2 | * An approved pull that has waited a week is landed, rebased or closed by the | |
| 3 | * engine (#12491). Each path fails without the sweep; a pull approved | |
| 4 | * yesterday, a red one and a held one are left alone. | |
| 5 | */ | |
| 6 | import { afterAll, afterEach, beforeAll, describe, expect, test } from "bun:test"; | |
| 7 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 8 | import { tmpdir } from "node:os"; | |
| 9 | import { join } from "node:path"; | |
| 10 | import { createApp, type App } from "../src/app.ts"; | |
| 11 | import { addToCrew, createAgent } from "../src/actions/crew.ts"; | |
| 12 | import { openIssue, setIssueState } from "../src/actions/issues.ts"; | |
| 13 | import { openPull } from "../src/actions/pulls.ts"; | |
| 14 | import { commitToBranch, createRepo } from "../src/actions/repos.ts"; | |
| 15 | import { getIssueById, listComments } from "../src/core/issues.ts"; | |
| 16 | import { getPull, type Pull } from "../src/core/pulls.ts"; | |
| 17 | import { getRepoById, type Repo } from "../src/core/repos.ts"; | |
| 18 | import { submitReview } from "../src/core/reviews.ts"; | |
| 19 | import { CHECK_FAILED_LIMIT } from "../src/crew/dispatch.ts"; | |
| 20 | import { claimRun, createRun, finishRun, listRuns } from "../src/core/runs.ts"; | |
| 21 | import { setHold } from "../src/core/holds.ts"; | |
| 22 | import { createUser, getUserByName, type User } from "../src/core/users.ts"; | |
| 23 | import { PER_TICK, sweepApprovedWeek, WAITED_MS } from "../src/crew/approved_week.ts"; | |
| 24 | import { seatAs } from "./helpers/seat.ts"; | |
| 25 | ||
| 26 | const DAY = 86_400_000; | |
| 27 | let dataDir: string; | |
| 28 | let app: App; | |
| 29 | let alice: User; | |
| 30 | let mason: User; | |
| 31 | let quill: User; | |
| 32 | let repo: Repo; | |
| 33 | let n = 0; | |
| 34 | ||
| 35 | const fresh = () => getRepoById(app.db, repo.id)!; | |
| 36 | const now = () => new Date(); | |
| 37 | ||
| 38 | /** | |
| 39 | * An agent's pull on its own branch, approved `daysAgo` days ago, with a check of `state` | |
| 40 | * on its head. | |
| 41 | */ | |
| 42 | async function approvedPull(opts: { daysAgo: number; body?: string; file?: string; check?: "ok" | "failed" | "none" }): Promise<Pull> { | |
| 43 | const branch = `feature/${++n}`; | |
| 44 | const pushed = await commitToBranch(app, alice, repo, { | |
| 45 | branch, | |
| 46 | base: "main", | |
| 47 | message: `Change ${n}`, | |
| 48 | files: [{ path: opts.file ?? `file-${n}.txt`, content: `branch ${n}\n` }], | |
| 49 | }); | |
| 50 | if (!pushed.ok) throw new Error(JSON.stringify(pushed.error)); | |
| 51 | const opened = await openPull(app, mason, repo, { title: `Change ${n}`, body: opts.body ?? "", baseBranch: "main", headBranch: branch }); | |
| 52 | if (!opened.ok) throw new Error(String(opened.error)); | |
| 53 | const pull = opened.value; | |
| 54 | const approved = submitReview(app.db, { issueId: pull.id, authorId: quill.id, state: "approved", commitOid: pull.headOid }); | |
| 55 | if (!approved.ok) throw new Error(approved.error); | |
| 56 | app.db | |
| 57 | .query("UPDATE review SET created_at = ?1 WHERE issue_id = ?2") | |
| 58 | .run(new Date(Date.now() - opts.daysAgo * DAY).toISOString(), pull.id); | |
| 59 | if (opts.check !== "none") { | |
| 60 | const run = createRun(app.db, { repoId: repo.id, kind: "check", issueId: pull.id, commitOid: pull.headOid }); | |
| 61 | claimRun(app.db, run.id); | |
| 62 | finishRun(app.db, run.id, { state: opts.check ?? "ok", summary: "" }); | |
| 63 | } | |
| 64 | return pull; | |
| 65 | } | |
| 66 | ||
| 67 | const autonomy = (on: boolean) => | |
| 68 | app.db | |
| 69 | .query("UPDATE repo SET autonomy = ?2, autonomy_by = ?3 WHERE id = ?1") | |
| 70 | .run(repo.id, on ? "autonomous" : "off", on ? alice.id : null); | |
| 71 | ||
| 72 | /** End every pull still open, the way a person would: through the close action. */ | |
| 73 | function endPulls(): void { | |
| 74 | const ids = app.db | |
| 75 | .query<{ id: string }, [string]>("SELECT id FROM issue WHERE repo_id = ?1 AND kind = 'pull' AND state = 'open'") | |
| 76 | .all(repo.id); | |
| 77 | for (const { id } of ids) setIssueState(app, alice, fresh(), getIssueById(app.db, id)!, "closed", { reason: "parked" }); | |
| 78 | } | |
| 79 | ||
| 80 | /** A conflict between a pull and main, so the engine must hand it to a builder. */ | |
| 81 | async function conflicted(): Promise<Pull> { | |
| 82 | const pull = await approvedPull({ daysAgo: 8, file: "shared.ts" }); | |
| 83 | const moved = await commitToBranch(app, alice, repo, { | |
| 84 | branch: "main", | |
| 85 | message: `Shared, main's way ${++n}`, | |
| 86 | files: [{ path: "shared.ts", content: `export const shared = '${n}';\n` }], | |
| 87 | }); | |
| 88 | if (!moved.ok) throw new Error(JSON.stringify(moved.error)); | |
| 89 | return pull; | |
| 90 | } | |
| 91 | ||
| 92 | const conflictRuns = (pull: Pull) => | |
| 93 | listRuns(app.db, repo.id, { kind: "agent" }).filter((r) => r.issueId === pull.id && r.reason === "conflict"); | |
| 94 | ||
| 95 | const open = (pull: Pull) => getIssueById(app.db, pull.id)!.state === "open"; | |
| 96 | const merged = (pull: Pull) => getPull(app.db, repo.id, pull.number)!.mergedAt !== null; | |
| 97 | const said = (pull: Pull) => listComments(app.db, pull.id).map((c) => c.body).join("\n"); | |
| 98 | ||
| 99 | beforeAll(async () => { | |
| 100 | dataDir = await mkdtemp(join(tmpdir(), "forge-approved-week-")); | |
| 101 | app = createApp({ dataDir, port: 0 }); | |
| 102 | const a = await createUser(app.db, { | |
| 103 | origin: "seed", | |
| 104 | trusted: true, | |
| 105 | username: "alice", | |
| 106 | email: "[email protected]", | |
| 107 | password: "a-good-passphrase", | |
| 108 | adminIfFirst: true, | |
| 109 | }); | |
| 110 | if (!a.ok) throw new Error(a.error); | |
| 111 | alice = a.value; | |
| 112 | const made = await createRepo(app, alice, { name: "engine", visibility: "private" }); | |
| 113 | if (!made.ok) throw new Error(String(made.error)); | |
| 114 | repo = made.value; | |
| 115 | const seeded = await commitToBranch(app, alice, repo, { | |
| 116 | message: "Start", | |
| 117 | files: [{ path: "shared.ts", content: "export const shared = 0;\n" }], | |
| 118 | }); | |
| 119 | if (!seeded.ok) throw new Error(JSON.stringify(seeded.error)); | |
| 120 | for (const name of ["mason", "quill"]) { | |
| 121 | const agent = await createAgent(app, alice, { username: name }); | |
| 122 | if (!agent.ok) throw new Error(String(agent.error)); | |
| 123 | expect(addToCrew(app, alice, repo, name).ok).toBe(true); | |
| 124 | } | |
| 125 | mason = getUserByName(app.db, "mason")!; | |
| 126 | quill = getUserByName(app.db, "quill")!; | |
| 127 | seatAs(app.db, repo.id, mason.id, "builder"); | |
| 128 | seatAs(app.db, repo.id, quill.id, "reviewer"); | |
| 129 | }); | |
| 130 | ||
| 131 | afterEach(() => { | |
| 132 | endPulls(); | |
| 133 | autonomy(false); | |
| 134 | }); | |
| 135 | ||
| 136 | afterAll(async () => { | |
| 137 | app.close(); | |
| 138 | await rm(dataDir, { recursive: true, force: true }); | |
| 139 | }); | |
| 140 | ||
| 141 | describe("an approved pull that has waited a week", () => { | |
| 142 | test("lands through the merge queue when the repository is autonomous, and says so", async () => { | |
| 143 | autonomy(true); | |
| 144 | const pull = await approvedPull({ daysAgo: 8 }); | |
| 145 | const moves = await sweepApprovedWeek(app, now()); | |
| 146 | expect(moves.find((m) => m.pull === pull.number)?.did).toBe("landed"); | |
| 147 | expect(merged(pull)).toBe(true); | |
| 148 | expect(said(pull)).toContain("Landed after waiting a week"); | |
| 149 | }); | |
| 150 | ||
| 151 | test("is never landed by the engine where autonomy is off: a person lands it", async () => { | |
| 152 | const pull = await approvedPull({ daysAgo: 30 }); | |
| 153 | const moves = await sweepApprovedWeek(app, now()); | |
| 154 | expect(moves.find((m) => m.pull === pull.number)).toMatchObject({ did: "left", why: "a person lands it" }); | |
| 155 | expect(merged(pull)).toBe(false); | |
| 156 | expect(open(pull)).toBe(true); | |
| 157 | }); | |
| 158 | ||
| 159 | test("is left alone before the week is out", async () => { | |
| 160 | const pull = await approvedPull({ daysAgo: 6 }); | |
| 161 | expect(WAITED_MS).toBe(7 * DAY); | |
| 162 | await sweepApprovedWeek(app, now()); | |
| 163 | expect(open(pull)).toBe(true); | |
| 164 | expect(merged(pull)).toBe(false); | |
| 165 | }); | |
| 166 | ||
| 167 | test("is left alone when its check is red, has none, or a person holds it", async () => { | |
| 168 | const red = await approvedPull({ daysAgo: 9, check: "failed" }); | |
| 169 | const none = await approvedPull({ daysAgo: 9, check: "none" }); | |
| 170 | const held = await approvedPull({ daysAgo: 9 }); | |
| 171 | setHold(app.db, held.id, alice.id, "wait"); | |
| 172 | const moves = await sweepApprovedWeek(app, now()); | |
| 173 | for (const p of [red, none, held]) expect(merged(p)).toBe(false); | |
| 174 | expect(moves.find((m) => m.pull === red.number)?.did).toBe("left"); | |
| 175 | expect(moves.find((m) => m.pull === none.number)?.did).toBe("left"); | |
| 176 | }); | |
| 177 | ||
| 178 | test("is closed when the issue it closes was dropped", async () => { | |
| 179 | const issue = openIssue(app, alice, repo, { title: "A thing nobody wants now" }); | |
| 180 | if (!issue.ok) throw new Error(String(issue.error)); | |
| 181 | const pull = await approvedPull({ daysAgo: 8, body: `Closes #${issue.value.number}` }); | |
| 182 | const dropped = setIssueState(app, alice, repo, issue.value, "closed", { reason: "dropped" }); | |
| 183 | expect(dropped.ok).toBe(true); | |
| 184 | const moves = await sweepApprovedWeek(app, now()); | |
| 185 | expect(moves.find((m) => m.pull === pull.number)).toMatchObject({ did: "closed" }); | |
| 186 | expect(open(pull)).toBe(false); | |
| 187 | expect(merged(pull)).toBe(false); | |
| 188 | expect(said(pull)).toContain("the issue it closes was dropped"); | |
| 189 | }); | |
| 190 | ||
| 191 | test("is landed, not closed, when the issue it closes is still open", async () => { | |
| 192 | autonomy(true); | |
| 193 | const issue = openIssue(app, alice, repo, { title: "Still wanted" }); | |
| 194 | if (!issue.ok) throw new Error(String(issue.error)); | |
| 195 | const pull = await approvedPull({ daysAgo: 8, body: `Closes #${issue.value.number}` }); | |
| 196 | await sweepApprovedWeek(app, now()); | |
| 197 | expect(merged(pull)).toBe(true); | |
| 198 | }); | |
| 199 | ||
| 200 | test("goes to its builder to rebase when main moved under it", async () => { | |
| 201 | const pull = await conflicted(); | |
| 202 | const moves = await sweepApprovedWeek(app, now()); | |
| 203 | expect(moves.find((m) => m.pull === pull.number)?.did).toBe("queued rebase"); | |
| 204 | expect(conflictRuns(pull)).toHaveLength(1); | |
| 205 | expect(conflictRuns(pull)[0]!.actorId).toBe(mason.id); | |
| 206 | expect(open(pull)).toBe(true); | |
| 207 | // One run in flight is enough: the next tick queues no second. | |
| 208 | await sweepApprovedWeek(app, now()); | |
| 209 | expect(conflictRuns(pull)).toHaveLength(1); | |
| 210 | }); | |
| 211 | ||
| 212 | test("is not handed to its builder again past the failed-attempt limit", async () => { | |
| 213 | const pull = await conflicted(); | |
| 214 | for (let i = 0; i < CHECK_FAILED_LIMIT; i++) { | |
| 215 | const run = createRun(app.db, { repoId: repo.id, kind: "agent", issueId: pull.id, actorId: mason.id, reason: "conflict" }); | |
| 216 | claimRun(app.db, run.id); | |
| 217 | finishRun(app.db, run.id, { state: "failed", summary: "" }); | |
| 218 | } | |
| 219 | const moves = await sweepApprovedWeek(app, now()); | |
| 220 | expect(moves.find((m) => m.pull === pull.number)).toMatchObject({ did: "left" }); | |
| 221 | expect(conflictRuns(pull)).toHaveLength(CHECK_FAILED_LIMIT); | |
| 222 | }); | |
| 223 | ||
| 224 | test("is not handed to a builder when a person has it", async () => { | |
| 225 | const pull = await conflicted(); | |
| 226 | app.db.query("UPDATE issue SET assignee_id = ?2 WHERE id = ?1").run(pull.id, alice.id); | |
| 227 | const moves = await sweepApprovedWeek(app, now()); | |
| 228 | expect(moves.find((m) => m.pull === pull.number)).toMatchObject({ did: "left", why: "a person has it" }); | |
| 229 | expect(conflictRuns(pull)).toHaveLength(0); | |
| 230 | }); | |
| 231 | ||
| 232 | test("is not handed to anyone when the crew has no builder", async () => { | |
| 233 | const pull = await conflicted(); | |
| 234 | seatAs(app.db, repo.id, mason.id, "reviewer"); | |
| 235 | try { | |
| 236 | const moves = await sweepApprovedWeek(app, now()); | |
| 237 | expect(moves.find((m) => m.pull === pull.number)).toMatchObject({ did: "left", why: "no builder on the crew" }); | |
| 238 | expect(conflictRuns(pull)).toHaveLength(0); | |
| 239 | } finally { | |
| 240 | seatAs(app.db, repo.id, mason.id, "builder"); | |
| 241 | } | |
| 242 | }); | |
| 243 | ||
| 244 | test("does nothing when no person can be acted as", async () => { | |
| 245 | autonomy(false); | |
| 246 | const pull = await approvedPull({ daysAgo: 8 }); | |
| 247 | app.db.query("UPDATE repo SET owner_id = ?2 WHERE id = ?1").run(repo.id, mason.id); | |
| 248 | try { | |
| 249 | const moves = await sweepApprovedWeek(app, now()); | |
| 250 | expect(moves.find((m) => m.pull === pull.number)).toMatchObject({ did: "left", why: "no person to act as" }); | |
| 251 | expect(open(pull)).toBe(true); | |
| 252 | expect(merged(pull)).toBe(false); | |
| 253 | } finally { | |
| 254 | app.db.query("UPDATE repo SET owner_id = ?2 WHERE id = ?1").run(repo.id, alice.id); | |
| 255 | } | |
| 256 | }); | |
| 257 | ||
| 258 | test(`acts on at most ${PER_TICK} pulls in a tick, the oldest first`, async () => { | |
| 259 | autonomy(true); | |
| 260 | const pulls: Pull[] = []; | |
| 261 | for (let i = 0; i < PER_TICK + 1; i++) pulls.push(await approvedPull({ daysAgo: 20 - i })); | |
| 262 | const moves = await sweepApprovedWeek(app, now()); | |
| 263 | expect(moves.filter((m) => m.did === "landed")).toHaveLength(PER_TICK); | |
| 264 | expect(pulls.slice(0, PER_TICK).every(merged)).toBe(true); | |
| 265 | expect(merged(pulls[PER_TICK]!)).toBe(false); | |
| 266 | // The next tick takes the one left. | |
| 267 | await sweepApprovedWeek(app, now()); | |
| 268 | expect(merged(pulls[PER_TICK]!)).toBe(true); | |
| 269 | }); | |
| 270 | ||
| 271 | test("is closed when its repository is archived", async () => { | |
| 272 | const pull = await approvedPull({ daysAgo: 8 }); | |
| 273 | app.db.query("UPDATE repo SET archived_at = ?1 WHERE id = ?2").run(new Date().toISOString(), repo.id); | |
| 274 | expect(fresh().archivedAt).not.toBeNull(); | |
| 275 | const moves = await sweepApprovedWeek(app, now()); | |
| 276 | expect(moves.find((m) => m.pull === pull.number)).toMatchObject({ did: "closed" }); | |
| 277 | expect(open(pull)).toBe(false); | |
| 278 | expect(merged(pull)).toBe(false); | |
| 279 | expect(said(pull)).toContain("its repository is archived"); | |
| 280 | }); | |
| 281 | }); |
new above
offline