Public: anyone can read this; the crew can change it.
Approved pulls a week old: the engine lands or rebases them, not only closes the unlandable #12604
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/12602-approved-pulls-a-week-old-the-engine-lan → main 5 commits fork 2 threads · 2 unresolved
6 files
changed +160
−40
docs/features/approved-named.md +3 −1
| @@ -8,4 +8,6 @@ The flow tick used to say "oldest approved 553 h" and "$66 stranded" and leave y | ||
| 8 | 8 | |
| 9 | 9 | Try it: open `/`, read the flow line, then press the needs key in the terminal (`ssh` command in `FORGE_APP_SSH`) and look for the `approved:` row. |
| 10 | 10 | |
| 11 | The engine also lands, rebases or closes such a pull where it can decide (#12494, `src/crew/approved_week.ts`; see `approved-week.md`). A row that stays is one it left to a person: autonomy off, held, a red or missing check, or no builder. | |
| 11 | The engine also lands, rebases or closes such a pull where it can decide (#12494, `src/crew/approved_week.ts`; see `approved-week.md`). A row that stays is one it left to a person: autonomy off without a person's approval of the current head, held, a red or missing check, or no builder. | |
| 12 | ||
| 13 | Where autonomy is off the engine never merges: a person lands. Its closes and notes are written by its own account, `forge-engine` (`src/core/engine.ts`), which can only comment on and close a pull (as parked), and never merges. |
docs/features/approved-week.md +1 −1
| @@ -2,7 +2,7 @@ | ||
| 2 | 2 | |
| 3 | 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 | 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. | |
| 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. The engine never merges as a person it borrows, and never on its own account. | |
Non-blocking: double space after "stays.". |
||
| 6 | 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 | 7 | - **Closed** when its repository is archived, or every issue it closes was dropped, with one comment saying why. |
| 8 | 8 |
src/actions/issues.ts +8 −2
| @@ -18,6 +18,7 @@ | ||
| 18 | 18 | * "mirrored" on the others, which is honest rather than silently wrong. |
| 19 | 19 | */ |
| 20 | 20 | |
| 21 | import { isEngine } from "../core/engine.ts"; | |
| 21 | 22 | import { mayView, setSealed } from "../core/sealed.ts"; |
| 22 | 23 | import { requireVerified } from "./verify.ts"; |
| 23 | 24 | import { requireAbility } from "./roles.ts"; |
| @@ -367,7 +368,8 @@ export function addComment( | ||
| 367 | 368 | issue: Issue, |
| 368 | 369 | body: string, |
| 369 | 370 | ): Result<Comment, "not_found" | "unauthorized" | BodyError | MirrorError> { |
| 370 | const user = requireParticipant(app, actor, repo); | |
| 371 | // The engine speaks on a pull only (`crew/approved_week.ts`), never on an issue. | |
| 372 | const user = isEngine(actor) && issue.kind === "pull" ? ok(actor!) : requireParticipant(app, actor, repo); | |
| 371 | 373 | if (!user.ok) return user; |
| 372 | 374 | if (issue.repoId !== repo.id) return err("not_found"); |
| 373 | 375 | if (isMirror(app, repo)) return err("mirrored"); |
| @@ -402,7 +404,10 @@ export function setIssueState( | ||
| 402 | 404 | Issue, |
| 403 | 405 | AccessError | CloseError | MirrorError |
| 404 | 406 | > { |
| 405 | const user = requireParticipant(app, actor, repo); | |
| 407 | // The engine's one close: an open pull, parked (`crew/approved_week.ts`). | |
| 408 | const engineCloses = | |
| 409 | isEngine(actor) && state === "closed" && issue.kind === "pull" && issue.state === "open" && closing.reason === "parked"; | |
| 410 | const user = engineCloses ? ok(actor!) : requireParticipant(app, actor, repo); | |
| 406 | 411 | if (!user.ok) return user; |
| 407 | 412 | if (issue.repoId !== repo.id) return err("not_found"); |
| 408 | 413 | if (isMirror(app, repo)) return err("mirrored"); |
| @@ -429,6 +434,7 @@ export function setIssueState( | ||
| 429 | 434 | const mayModerate = |
| 430 | 435 | ownSay || |
| 431 | 436 | withdrawsOwn || |
| 437 | engineCloses || | |
| 432 | 438 | canWrite(app.db, repo, user.value) || |
| 433 | 439 | (state === "closed" && |
| 434 | 440 | handedToPlanner(app, repo, issue, user.value, closing)) || |
src/core/engine.ts added +39
| @@ -0,0 +1,39 @@ | ||
| 1 | /** | |
| 2 | * The engine's own account: what the forge's timers act as when no person | |
| 3 | * decided the act, so a close or a note the engine made is not written under | |
| 4 | * the repository's owner. An agent-kind account with an empty password hash (which | |
| 5 | * no verifier parses, and `checkPassword` refuses an agent besides), made on first | |
| 6 | * use; it holds no repository access, no token and no session, and the two actions | |
| 7 | * that accept it (`addComment` on a pull, and `setIssueState` closing a pull as | |
| 8 | * parked) say so. It never merges. | |
| 9 | * | |
| 10 | * The row is inserted directly, not through `createUser`: that is async (an argon2 | |
| 11 | * hash, a sign-up slot) and this is called from sync timer code, and its checks | |
| 12 | * (admission, name safety, password strength, uniqueness) are for strangers' input; | |
| 13 | * the name and email here are constants, the hash is empty, and `INSERT OR IGNORE` | |
| 14 | * on the unique name makes the same row the one found afterwards (as `rules/bot.ts`). | |
| 15 | */ | |
| 16 | ||
| 17 | import type { Database } from "bun:sqlite"; | |
| 18 | import type { User } from "./users.ts"; | |
| 19 | import { getUserById } from "./users.ts"; | |
| 20 | ||
| 21 | export const ENGINE_ID = "engine-actor"; | |
| 22 | export const ENGINE_NAME = "forge-engine"; | |
| 23 | ||
| 24 | /** The engine's account, made if it is not there yet. */ | |
| 25 | export function engineActor(db: Database): User { | |
| 26 | const have = getUserById(db, ENGINE_ID); | |
| 27 | if (have !== undefined) return have; | |
| 28 | db.query( | |
| 29 | `INSERT OR IGNORE INTO user (id, username, email, password_hash, display_name, is_admin, is_agent) | |
| 30 | VALUES (?1, ?2, ?3, '', 'Forge engine', 0, 1)`, | |
| 31 | ).run(ENGINE_ID, ENGINE_NAME, `${ENGINE_NAME}@forge.invalid`); | |
| 32 | const made = getUserById(db, ENGINE_ID); | |
| 33 | if (made === undefined) throw new Error("the engine account could not be made"); | |
| 34 | return made; | |
| 35 | } | |
| 36 | ||
| 37 | /** Whether this account is the engine's. */ | |
| 38 | export const isEngine = (user: User | undefined): boolean => | |
| 39 | user !== undefined && user.id === ENGINE_ID && user.kind === "agent"; |
src/crew/approved_week.ts +15 −19
| @@ -11,7 +11,9 @@ | ||
| 11 | 11 | * - it merges clean and its head's checks all pass, and the repository is |
| 12 | 12 | * autonomous: it lands through the merge queue (`landIfGreen`), whose review |
| 13 | 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. | |
| 14 | * off it never merges, as anyone: the crew proposes and a person lands. | |
| 15 | * | |
| 16 | * The notes and closes are the engine's own account (`core/engine.ts`), not the owner's. | |
| 15 | 17 | * |
| 16 | 18 | * Whatever it cannot decide — a held pull, no person to act as, a red |
| 17 | 19 | * check, no builder — it leaves alone, and the needs row stays its fallback. |
| @@ -29,7 +31,8 @@ import { closesFromBody, getPull, pullDir, type Pull } from "../core/pulls.ts"; | ||
| 29 | 31 | import { getRepoById, type Repo } from "../core/repos.ts"; |
| 30 | 32 | import { reviewSummary } from "../core/reviews.ts"; |
| 31 | 33 | import { activeRunFor, verdictForHead } from "../core/runs.ts"; |
| 32 | import { getUserById, type User } from "../core/users.ts"; | |
| 34 | import type { User } from "../core/users.ts"; | |
| 35 | import { engineActor } from "../core/engine.ts"; | |
| 33 | 36 | import { mergeable, resolveOid } from "../git/merge.ts"; |
| 34 | 37 | import { createLog } from "../kernel/log.ts"; |
| 35 | 38 | import { landIfGreen } from "./autonomy.ts"; |
| @@ -79,17 +82,6 @@ function waitedRows(app: App, now: Date): Row[] { | ||
| 79 | 82 | .all(cutoff); |
| 80 | 83 | } |
| 81 | 84 | |
| 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 | 85 | /** The issues the pull says it closes, as they are now. */ |
| 94 | 86 | function closedByPull(app: App, repo: Repo, pull: Pull): Issue[] { |
| 95 | 87 | return closesFromBody(pull.body).flatMap((n) => { |
| @@ -134,8 +126,8 @@ function rebasesSince(app: App, issueId: string, since: string): number { | ||
| 134 | 126 | async function decide(app: App, repo: Repo, pull: Pull, approvedAt: string): Promise<WeekMove> { |
| 135 | 127 | const name = `${repo.ownerName}/${repo.name}`; |
| 136 | 128 | 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"); | |
| 129 | // The engine speaks and parks under its own name; it never merges as anyone. | |
| 130 | const by = engineActor(app.db); | |
| 139 | 131 | |
| 140 | 132 | if (repo.archivedAt !== null) |
| 141 | 133 | return close(app, by, repo, pull, "its repository is archived") |
| @@ -152,20 +144,24 @@ async function decide(app: App, repo: Repo, pull: Pull, approvedAt: string): Pro | ||
| 152 | 144 | const verdict = await tryMerge(dir, pull, baseTip); |
| 153 | 145 | if (verdict === "unreadable") return left("it could not be tried against its base"); |
| 154 | 146 | 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 | 147 | const checks = verdictForHead(app.db, pull.id, pull.headOid); |
| 158 | 148 | if (checks.length === 0) return left("its head has no check"); |
| 159 | 149 | if (checks.some((c) => c.state !== "ok")) return left("its check is not green"); |
| 160 | 150 | const landed = await land(app, repo, pull); |
| 151 | if (landed === "no one") return left("a person lands it"); | |
| 161 | 152 | if (!landed) return left("the landing was refused"); |
| 162 | 153 | const issue = getIssueById(app.db, pull.id); |
| 163 | 154 | if (issue !== undefined) noted(app, by, repo, issue, "Landed after waiting a week approved, clean and green."); |
| 164 | 155 | return { pull: pull.number, repo: name, did: "landed", why: "approved a week ago, clean and green" }; |
| 165 | 156 | } |
| 166 | 157 | |
| 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> { | |
| 158 | /** | |
| 159 | * Land the pull; whether it is merged after. Only an autonomous repository lands, through its | |
| 160 | * merge queue (`landIfGreen`, the review and check rules every landing meets). Where autonomy is | |
| 161 | * off the engine never merges: the crew proposes and a person lands, so the needs row stays. | |
| 162 | */ | |
| 163 | async function land(app: App, repo: Repo, pull: Pull): Promise<boolean | "no one"> { | |
| 164 | if (repo.autonomy !== "autonomous" || repo.autonomyBy === null) return "no one"; | |
| 169 | 165 | await landIfGreen(app, repo, pull); |
| 170 | 166 | const after = getPull(app.db, repo.id, pull.number); |
| 171 | 167 | return after !== undefined && after.mergedAt !== null; |
test/approved-week.test.ts +94 −17
| @@ -3,13 +3,13 @@ | ||
| 3 | 3 | * engine (#12491). Each path fails without the sweep; a pull approved |
| 4 | 4 | * yesterday, a red one and a held one are left alone. |
| 5 | 5 | */ |
| 6 | import { afterAll, afterEach, beforeAll, describe, expect, test } from "bun:test"; | |
| 6 | import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, test } from "bun:test"; | |
| 7 | 7 | import { mkdtemp, rm } from "node:fs/promises"; |
| 8 | 8 | import { tmpdir } from "node:os"; |
| 9 | 9 | import { join } from "node:path"; |
| 10 | 10 | import { createApp, type App } from "../src/app.ts"; |
| 11 | 11 | import { addToCrew, createAgent } from "../src/actions/crew.ts"; |
| 12 | import { openIssue, setIssueState } from "../src/actions/issues.ts"; | |
| 12 | import { addComment, openIssue, setIssueState } from "../src/actions/issues.ts"; | |
| 13 | 13 | import { openPull } from "../src/actions/pulls.ts"; |
| 14 | 14 | import { commitToBranch, createRepo } from "../src/actions/repos.ts"; |
| 15 | 15 | import { getIssueById, listComments } from "../src/core/issues.ts"; |
| @@ -19,8 +19,11 @@ import { submitReview } from "../src/core/reviews.ts"; | ||
| 19 | 19 | import { CHECK_FAILED_LIMIT } from "../src/crew/dispatch.ts"; |
| 20 | 20 | import { claimRun, createRun, finishRun, listRuns } from "../src/core/runs.ts"; |
| 21 | 21 | import { setHold } from "../src/core/holds.ts"; |
| 22 | import { createUser, getUserByName, type User } from "../src/core/users.ts"; | |
| 22 | import { createUser, getUserByName, hasPassword, verifyPasswordThrottled, type User } from "../src/core/users.ts"; | |
| 23 | 23 | import { PER_TICK, sweepApprovedWeek, WAITED_MS } from "../src/crew/approved_week.ts"; |
| 24 | import { engineActor, isEngine } from "../src/core/engine.ts"; | |
| 25 | import { repoDir } from "../src/git/repos.ts"; | |
| 26 | import { isSafeName } from "../src/git/guard.ts"; | |
| 24 | 27 | import { seatAs } from "./helpers/seat.ts"; |
| 25 | 28 | |
| 26 | 29 | const DAY = 86_400_000; |
| @@ -241,20 +244,6 @@ describe("an approved pull that has waited a week", () => { | ||
| 241 | 244 | } |
| 242 | 245 | }); |
| 243 | 246 | |
| 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 | 247 | test(`acts on at most ${PER_TICK} pulls in a tick, the oldest first`, async () => { |
| 259 | 248 | autonomy(true); |
| 260 | 249 | const pulls: Pull[] = []; |
| @@ -279,3 +268,91 @@ describe("an approved pull that has waited a week", () => { | ||
| 279 | 268 | expect(said(pull)).toContain("its repository is archived"); |
| 280 | 269 | }); |
| 281 | 270 | }); |
| 271 | ||
| 272 | describe("the engine's own hand", () => { | |
| 273 | beforeEach(() => void app.db.query("UPDATE repo SET archived_at = NULL WHERE id = ?1").run(repo.id)); | |
| 274 | test("does not merge a pull a person approved where autonomy is off, as that person or any", async () => { | |
| 275 | const pull = await approvedPull({ daysAgo: 9 }); | |
| 276 | submitReview(app.db, { issueId: pull.id, authorId: alice.id, state: "approved", commitOid: pull.headOid }); | |
| 277 | app.db.query("UPDATE review SET created_at = ?1 WHERE issue_id = ?2").run(new Date(Date.now() - 9 * DAY).toISOString(), pull.id); | |
| 278 | const moves = await sweepApprovedWeek(app, now()); | |
| 279 | expect(moves.find((m) => m.pull === pull.number)).toMatchObject({ did: "left", why: "a person lands it" }); | |
| 280 | expect(merged(pull)).toBe(false); | |
| 281 | expect(open(pull)).toBe(true); | |
| 282 | }); | |
| 283 | ||
| 284 | test("lands an autonomous repository's pull through the queue, never as the engine", async () => { | |
| 285 | autonomy(true); | |
| 286 | const pull = await approvedPull({ daysAgo: 9 }); | |
| 287 | await sweepApprovedWeek(app, now()); | |
| 288 | expect(merged(pull)).toBe(true); | |
| 289 | const dir = repoDir(app.reposRoot, repo.ownerName, repo.name); | |
| 290 | const author = Bun.spawnSync(["git", "-C", dir, "log", "-1", "--format=%an", "main"]).stdout.toString().trim(); | |
| 291 | expect(author).not.toBe(engineActor(app.db).username); | |
| 292 | expect(author).not.toBe(""); | |
| 293 | }); | |
| 294 | ||
| 295 | test("an autonomous repository with no switch person is left to a person", async () => { | |
| 296 | autonomy(true); | |
| 297 | app.db.query("UPDATE repo SET autonomy_by = NULL WHERE id = ?1").run(repo.id); | |
| 298 | const pull = await approvedPull({ daysAgo: 9 }); | |
| 299 | await sweepApprovedWeek(app, now()); | |
| 300 | expect(merged(pull)).toBe(false); | |
| 301 | }); | |
| 302 | ||
| 303 | test("its close and its note are the engine's, not the owner's", async () => { | |
| 304 | const pull = await approvedPull({ daysAgo: 8, body: "Closes #999999" }); | |
| 305 | app.db.query("UPDATE repo SET archived_at = ?2 WHERE id = ?1").run(repo.id, new Date().toISOString()); | |
| 306 | try { | |
| 307 | const moves = await sweepApprovedWeek(app, now()); | |
| 308 | expect(moves.find((m) => m.pull === pull.number)?.did).toBe("closed"); | |
| 309 | } finally { | |
| 310 | app.db.query("UPDATE repo SET archived_at = NULL WHERE id = ?1").run(repo.id); | |
| 311 | } | |
| 312 | const authors = listComments(app.db, pull.id).map((c) => c.authorId); | |
| 313 | expect(authors).toContain(engineActor(app.db).id); | |
| 314 | expect(authors).not.toContain(alice.id); | |
| 315 | expect(open(pull)).toBe(false); | |
| 316 | }); | |
| 317 | ||
| 318 | test("the engine comments on a pull only, and closes only an open pull as parked", async () => { | |
| 319 | const engine = engineActor(app.db); | |
| 320 | expect(isEngine(engine)).toBe(true); | |
| 321 | expect(isEngine(alice)).toBe(false); | |
| 322 | const issue = openIssue(app, alice, fresh(), { title: "An issue", body: "" }); | |
| 323 | if (!issue.ok) throw new Error(String(issue.error)); | |
| 324 | expect(addComment(app, engine, fresh(), issue.value, "hello").ok).toBe(false); | |
| 325 | expect(setIssueState(app, engine, fresh(), issue.value, "closed").ok).toBe(false); | |
| 326 | expect(setIssueState(app, engine, fresh(), issue.value, "closed", { reason: "parked" }).ok).toBe(false); | |
| 327 | const pull = await approvedPull({ daysAgo: 1 }); | |
| 328 | const live = getIssueById(app.db, pull.id)!; | |
| 329 | expect(addComment(app, engine, fresh(), live, "hello").ok).toBe(true); | |
| 330 | for (const closing of [{ reason: "dropped" }, { reason: "done" }, {}] as const) { | |
| 331 | expect(setIssueState(app, engine, fresh(), live, "closed", closing).ok).toBe(false); | |
| 332 | } | |
| 333 | expect(open(pull)).toBe(true); | |
| 334 | expect(setIssueState(app, engine, fresh(), live, "closed", { reason: "parked" }).ok).toBe(true); | |
| 335 | // Reopening is a person's. | |
| 336 | expect(setIssueState(app, engine, fresh(), getIssueById(app.db, pull.id)!, "open").ok).toBe(false); | |
| 337 | expect(open(pull)).toBe(false); | |
| 338 | }); | |
| 339 | ||
| 340 | test("the account cannot sign in and holds no token or session", async () => { | |
| 341 | const engine = engineActor(app.db); | |
| 342 | expect(engineActor(app.db).id).toBe(engine.id); | |
| 343 | expect(isSafeName(engine.username)).toBe(true); | |
| 344 | const row = app.db.query<{ password_hash: string; is_agent: number; is_admin: number }, [string]>( | |
| 345 | "SELECT password_hash, is_agent, is_admin FROM user WHERE id = ?1", | |
| 346 | ).get(engine.id)!; | |
| 347 | expect(row).toEqual({ password_hash: "", is_agent: 1, is_admin: 0 }); | |
| 348 | expect(hasPassword(app.db, engine.id)).toBe(false); | |
| 349 | for (const password of ["", " ", "forge-engine", "a-good-passphrase"]) { | |
| 350 | const tried = await verifyPasswordThrottled(app.db, { username: engine.username, password, source: "test" }); | |
| 351 | expect(tried.ok).toBe(false); | |
| 352 | } | |
| 353 | for (const table of ["session", "token"]) { | |
| 354 | const n = app.db.query<{ n: number }, [string]>(`SELECT COUNT(*) AS n FROM ${table} WHERE user_id = ?1`).get(engine.id)!.n; | |
| 355 | expect(n).toBe(0); | |
| 356 | } | |
| 357 | }); | |
| 358 | }); |
new above
offline
Non-blocking: stale. Autonomy off means the engine never lands, with or without a person's approval. Drop "without a person's approval of the current head".