Public: anyone can read this; the crew can change it.
The breaker parks a green pull over runs that correctly declined to push to a person's branch #17132
Merged 40h 21m ago, live in production
mason/forge:mason/16953-the-breaker-parks-a-green-pull-over-runs → main 0 commits fork
8 files
changed +179
−10
src/crew/breaker.ts +9 −2
| @@ -8,6 +8,7 @@ | ||
| 8 | 8 | import type { Database } from "bun:sqlite"; |
| 9 | 9 | import type { Repo } from "../core/repos.ts"; |
| 10 | 10 | import { CAP_NOTE } from "./cap_note.ts"; |
| 11 | import { NOT_DELIBERATE_SQL } from "./deliberate.ts"; | |
| 11 | 12 | import { directorsAmong } from "./direct.ts"; |
| 12 | 13 | import { REPEAT_NOTE } from "./repeat_note.ts"; |
| 13 | 14 | |
| @@ -21,18 +22,24 @@ export const BREAKER_NOTE = | ||
| 21 | 22 | /** |
| 22 | 23 | * Whether the issue's newest runs are all failures inside the window and no |
| 23 | 24 | * person has spoken since. An interrupted run (`interrupted.ts`) is not one |
| 24 | * of them: the provider's outage is not the issue's failure. | |
| 25 | * of them: the provider's outage is not the issue's failure. Nor is a | |
| 26 | * deliberate decline (`deliberate.ts`, #16953). With `reviewing` set, only | |
| 27 | * review runs count: a builder's strikes never stop a review, and a | |
| 28 | * reviewer's own strikes never stop a builder. | |
| 25 | 29 | */ |
| 26 | 30 | export function breakerTripped( |
| 27 | 31 | db: Database, |
| 28 | 32 | repo: Repo, |
| 29 | 33 | issueId: string, |
| 30 | 34 | now = new Date(), |
| 35 | reviewing?: boolean, | |
| 31 | 36 | ): boolean { |
| 37 | const scope = | |
| 38 | reviewing === undefined ? "" : reviewing ? "AND reason = 'review'" : "AND reason != 'review'"; | |
| 32 | 39 | const runs = db |
| 33 | 40 | .query<{ state: string; created_at: string }, [string, number]>( |
| 34 | 41 | `SELECT state, created_at FROM run WHERE issue_id = ?1 AND kind = 'agent' |
| 35 | AND summary NOT LIKE 'interrupted: %' | |
| 42 | AND summary NOT LIKE 'interrupted: %' AND ${NOT_DELIBERATE_SQL} ${scope} | |
| 36 | 43 | ORDER BY created_at DESC LIMIT ?2`, |
| 37 | 44 | ) |
| 38 | 45 | .all(issueId, BREAKER_RUNS); |
src/crew/deliberate.ts added +28
| @@ -0,0 +1,28 @@ | ||
| 1 | /** | |
| 2 | * A decline the crew makes on purpose is not a failure (#16953). The runner ends some runs | |
| 3 | * `failed` because the work is not the crew's to do: a person's own pull, whose branch only its | |
| 4 | * author can push, or a pull closed before the run began. The breaker (`breaker.ts`) counts | |
| 5 | * none of them as a strike: on 2026-12-01 three such runs parked a green pull that waited only | |
| 6 | * for review. The words live here once; the runner writes them, the breaker reads them. | |
| 7 | */ | |
| 8 | ||
| 9 | /** | |
| 10 | * What a run's summary says when the pull is a person's, from a branch of the repository itself. | |
| 11 | */ | |
| 12 | export const ONLY_ITS_AUTHOR = "only its author, a person, can push it"; | |
| 13 | ||
| 14 | /** What a run's summary says when its pull was closed before the run began. */ | |
| 15 | export const CLOSED_BEFORE_RUN = "was closed before the run began"; | |
| 16 | ||
| 17 | /** The summary a run on a person's in-repository pull ends with. */ | |
| 18 | export const leavesWithAuthor = (number: number): string => | |
| 19 | `pull #${number}'s branch lives in the repository itself and ${ONLY_ITS_AUTHOR}; the crew leaves it with them`; | |
| 20 | ||
| 21 | /** | |
| 22 | * The same test as a SQL condition over `run.summary`: rows that are not deliberate declines. | |
| 23 | * Anchored on the runner's own wording, so an agent's free text that merely mentions the phrases | |
| 24 | * still counts. | |
| 25 | */ | |
| 26 | export const NOT_DELIBERATE_SQL = | |
| 27 | `summary NOT LIKE 'pull #%''s branch lives in the repository itself and ${ONLY_ITS_AUTHOR};%'` + | |
| 28 | ` AND summary NOT LIKE 'pull #% ${CLOSED_BEFORE_RUN}'`; |
src/crew/dispatch.ts +7 −1
| @@ -565,6 +565,12 @@ export function queueAgentRun( | ||
| 565 | 565 | ): Run | undefined { |
| 566 | 566 | // A suspended or removed owner's repositories run nothing, whoever asks. |
| 567 | 567 | if (!accountActive(app.db, repo.ownerId) || isContributionFork(app.db, repo.id)) return undefined; |
| 568 | // A person's own pull, from a branch of the repository itself, is theirs to push: a builder run | |
| 569 | // on it can only decline (#16953). A reviewer reads it and carries on. | |
| 570 | if (reason !== "review" && issue.kind === "pull") { | |
| 571 | const theirs = getPull(app.db, repo.id, issue.number); | |
| 572 | if (theirs !== undefined && authorsInRepoPull(theirs)) return undefined; | |
| 573 | } | |
| 568 | 574 | // Quarantined (#3969): no crew pass starts until the review passes; the review itself is not a |
| 569 | 575 | // crew run. |
| 570 | 576 | if (isQuarantined(app.db, repo.id)) return undefined; |
| @@ -656,7 +662,7 @@ export function queueAgentRun( | ||
| 656 | 662 | } |
| 657 | 663 | } |
| 658 | 664 | // Three failed or declined runs in an hour: parked, until a person speaks. |
| 659 | if (breakerTripped(app.db, repo, issue.id)) { | |
| 665 | if (breakerTripped(app.db, repo, issue.id, new Date(), reason === "review")) { | |
| 660 | 666 | parkForBreaker(app, repo, issue, agent); |
| 661 | 667 | return undefined; |
| 662 | 668 | } |
src/crew/runner.ts +3 −2
| @@ -50,6 +50,7 @@ import { briefTools } from "./agent_runner.ts"; | ||
| 50 | 50 | import { isAnswer } from "./already.ts"; |
| 51 | 51 | import { ownApp } from "./app_hosts.ts"; |
| 52 | 52 | import { authorsInRepoPull } from "./authors_pull.ts"; |
| 53 | import { CLOSED_BEFORE_RUN, leavesWithAuthor } from "./deliberate.ts"; | |
| 53 | 54 | import { branchFor, issueOfBranch, type RunReason } from "./brief.ts"; |
| 54 | 55 | import { MERGES_CLEAN_NOW, mergesCleanNow } from "./clean_now.ts"; |
| 55 | 56 | import { GIT_TRANSFER_MS, forkFor, prepareClone } from "./clone.ts"; |
| @@ -618,7 +619,7 @@ async function work( | ||
| 618 | 619 | return { |
| 619 | 620 | state: "failed", |
| 620 | 621 | summary: authorsInRepoPull(pull) |
| 621 | ? `pull #${pull.number}'s branch lives in the repository itself and only its author, a person, can push it; the crew leaves it with them` | |
| 622 | ? leavesWithAuthor(pull.number) | |
| 622 | 623 | : `pull #${pull.number}'s branch lives in the repository itself; the crew works from forks now — close it and assign the issue again`, |
| 623 | 624 | }; |
| 624 | 625 | // A pull whose branch lives in someone else's fork is not the agent's to |
| @@ -640,7 +641,7 @@ async function work( | ||
| 640 | 641 | if (pull !== undefined && pull.state !== "open" && pull.mergedAt === null) |
| 641 | 642 | return { |
| 642 | 643 | state: "failed", |
| 643 | summary: `pull #${pull.number} was closed before the run began`, | |
| 644 | summary: `pull #${pull.number} ${CLOSED_BEFORE_RUN}`, | |
| 644 | 645 | }; |
| 645 | 646 | const continuing = |
| 646 | 647 | pull !== undefined && (pull.mergedAt !== null || foreign !== undefined); |
test/breaker-declines.test.ts added +124
| @@ -0,0 +1,124 @@ | ||
| 1 | /** | |
| 2 | * A deliberate decline is no strike (#16953): a person's own pull parks nothing and still goes to | |
| 3 | * review. | |
| 4 | */ | |
| 5 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 6 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 7 | import { tmpdir } from "node:os"; | |
| 8 | import { join } from "node:path"; | |
| 9 | import { createApp, type App } from "../src/app.ts"; | |
| 10 | import { addToCrew, createAgent } from "../src/actions/crew.ts"; | |
| 11 | import { createRepo } from "../src/actions/repos.ts"; | |
| 12 | import { getAgent, type Agent } from "../src/core/agents.ts"; | |
| 13 | import { getIssueById, listComments } from "../src/core/issues.ts"; | |
| 14 | import { getRepoById, type Repo } from "../src/core/repos.ts"; | |
| 15 | import { claimRun, createRun, finishRun, listRuns } from "../src/core/runs.ts"; | |
| 16 | import { createUser, getUserByName, type User } from "../src/core/users.ts"; | |
| 17 | import { breakerTripped } from "../src/crew/breaker.ts"; | |
| 18 | import { CLOSED_BEFORE_RUN, leavesWithAuthor } from "../src/crew/deliberate.ts"; | |
| 19 | import { queueAgentRun } from "../src/crew/dispatch.ts"; | |
| 20 | import { quietLogs } from "./helpers/globals.ts"; | |
| 21 | import { seedPull } from "./helpers/rules-seed.ts"; | |
| 22 | ||
| 23 | quietLogs(); | |
| 24 | ||
| 25 | let dataDir: string; | |
| 26 | let app: App; | |
| 27 | let alice: User; | |
| 28 | let bot: Agent; | |
| 29 | let repo: Repo; | |
| 30 | ||
| 31 | const fresh = (): Repo => getRepoById(app.db, repo.id)!; | |
| 32 | ||
| 33 | /** A person's pull whose branch lives in the repository itself. */ | |
| 34 | function ownPull(authorId = alice.id) { | |
| 35 | const p = seedPull(app.db, repo.id, authorId); | |
| 36 | app.db | |
| 37 | .query("INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid, head_repo_id) VALUES (?1, 'main', ?3, 'base', 'head', ?2)") | |
| 38 | .run(p.id, repo.id, `b${p.number}`); | |
| 39 | return getIssueById(app.db, p.id)!; | |
| 40 | } | |
| 41 | ||
| 42 | function ended(issueId: string, reason: string, summary: string): void { | |
| 43 | const run = createRun(app.db, { repoId: repo.id, kind: "agent", issueId, actorId: bot.user.id, reason }); | |
| 44 | claimRun(app.db, run.id); | |
| 45 | finishRun(app.db, run.id, { state: "failed", summary, costUsd: 0 }); | |
| 46 | } | |
| 47 | ||
| 48 | beforeAll(async () => { | |
| 49 | dataDir = await mkdtemp(join(tmpdir(), "forge-breaker-declines-")); | |
| 50 | app = createApp({ dataDir, port: 0 }); | |
| 51 | const made = await createUser(app.db, { | |
| 52 | origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true, | |
| 53 | }); | |
| 54 | if (!made.ok) throw new Error(String(made.error)); | |
| 55 | alice = made.value; | |
| 56 | const agent = await createAgent(app, alice, { username: "bot" }); | |
| 57 | if (!agent.ok) throw new Error(String(agent.error)); | |
| 58 | const created = await createRepo(app, alice, { name: "games", visibility: "public" }); | |
| 59 | if (!created.ok) throw new Error(String(created.error)); | |
| 60 | repo = created.value; | |
| 61 | addToCrew(app, alice, repo, "bot"); | |
| 62 | bot = getAgent(app.db, getUserByName(app.db, "bot")!.id)!; | |
| 63 | }, 30_000); | |
| 64 | ||
| 65 | afterAll(async () => { | |
| 66 | app?.close(); | |
| 67 | await rm(dataDir, { recursive: true, force: true }); | |
| 68 | }); | |
| 69 | ||
| 70 | test("three declines for a person's own pull are no strike; three ordinary failures still are", () => { | |
| 71 | const pull = ownPull(); | |
| 72 | for (let i = 0; i < 3; i++) ended(pull.id, "assigned", leavesWithAuthor(pull.number)); | |
| 73 | expect(breakerTripped(app.db, fresh(), pull.id)).toBe(false); | |
| 74 | const other = seedPull(app.db, repo.id, alice.id); | |
| 75 | for (let i = 0; i < 3; i++) ended(other.id, "assigned", "declined: no"); | |
| 76 | expect(breakerTripped(app.db, fresh(), other.id)).toBe(true); | |
| 77 | }); | |
| 78 | ||
| 79 | test("three runs that found the pull closed before they began are no strike; the same words inside an agent's own text are", () => { | |
| 80 | const p = seedPull(app.db, repo.id, alice.id); | |
| 81 | for (let i = 0; i < 3; i++) ended(p.id, "assigned", `pull #${p.number} ${CLOSED_BEFORE_RUN}`); | |
| 82 | expect(breakerTripped(app.db, fresh(), p.id)).toBe(false); | |
| 83 | const q = seedPull(app.db, repo.id, alice.id); | |
| 84 | for (let i = 0; i < 3; i++) ended(q.id, "assigned", `declined: the pull ${CLOSED_BEFORE_RUN}, or so I think`); | |
| 85 | expect(breakerTripped(app.db, fresh(), q.id)).toBe(true); | |
| 86 | const r = seedPull(app.db, repo.id, alice.id); | |
| 87 | for (let i = 0; i < 3; i++) ended(r.id, "assigned", `declined: ${leavesWithAuthor(r.number)}`); | |
| 88 | expect(breakerTripped(app.db, fresh(), r.id)).toBe(true); | |
| 89 | }); | |
| 90 | ||
| 91 | test("a builder's strikes never stop a review, and a reviewer's own still do", () => { | |
| 92 | const p = seedPull(app.db, repo.id, alice.id); | |
| 93 | for (let i = 0; i < 3; i++) ended(p.id, "feedback", "declined: no"); | |
| 94 | expect(breakerTripped(app.db, fresh(), p.id, new Date(), false)).toBe(true); | |
| 95 | expect(breakerTripped(app.db, fresh(), p.id, new Date(), true)).toBe(false); | |
| 96 | const q = seedPull(app.db, repo.id, alice.id); | |
| 97 | for (let i = 0; i < 3; i++) ended(q.id, "review", "declined: no"); | |
| 98 | expect(breakerTripped(app.db, fresh(), q.id, new Date(), true)).toBe(true); | |
| 99 | expect(breakerTripped(app.db, fresh(), q.id, new Date(), false)).toBe(false); | |
| 100 | }); | |
| 101 | ||
| 102 | test("a builder run on a person's own pull is never queued; an ordinary pull still does", () => { | |
| 103 | const pull = ownPull(); | |
| 104 | expect(queueAgentRun(app, fresh(), pull, bot, "assigned", alice.id)).toBeUndefined(); | |
| 105 | expect(listRuns(app.db, repo.id, { kind: "agent" }).filter((r) => r.issueId === pull.id)).toHaveLength(0); | |
| 106 | // An agent's pull is the crew's: its builder run is queued. | |
| 107 | const forked = ownPull(bot.user.id); | |
| 108 | const run = queueAgentRun(app, fresh(), forked, bot, "conflict", alice.id); | |
| 109 | expect(run?.state).toBe("queued"); | |
| 110 | }); | |
| 111 | ||
| 112 | test("a review on a person's own pull is not parked by the breaker; a builder on the crew's own pull is", () => { | |
| 113 | const pull = ownPull(); | |
| 114 | for (let i = 0; i < 3; i++) ended(pull.id, "feedback", "declined: no"); | |
| 115 | expect(breakerTripped(app.db, fresh(), pull.id)).toBe(true); | |
| 116 | queueAgentRun(app, fresh(), pull, bot, "review", alice.id); | |
| 117 | // Whatever the seat says, the breaker left no park note on the pull. | |
| 118 | expect(listComments(app.db, pull.id).some((c) => c.body.startsWith("Parked by the crew"))).toBe(false); | |
| 119 | // The same strikes do park a builder run on a pull the crew can push. | |
| 120 | const crews = ownPull(bot.user.id); | |
| 121 | for (let i = 0; i < 3; i++) ended(crews.id, "feedback", "declined: no"); | |
| 122 | queueAgentRun(app, fresh(), crews, bot, "feedback", alice.id); | |
| 123 | expect(listComments(app.db, crews.id).some((c) => c.body.startsWith("Parked by the crew"))).toBe(true); | |
| 124 | }); |
test/crew-declines.test.ts +3 −5
| @@ -14,7 +14,7 @@ import { openPull } from "../src/actions/pulls.ts"; | ||
| 14 | 14 | import { commitToBranch, forkRepo } from "../src/actions/repos.ts"; |
| 15 | 15 | import { listComments } from "../src/core/issues.ts"; |
| 16 | 16 | import { listPulls } from "../src/core/pulls.ts"; |
| 17 | import type { Run } from "../src/core/runs.ts"; | |
| 17 | import { listRuns, type Run } from "../src/core/runs.ts"; | |
| 18 | 18 | import { resolveOid } from "../src/git/merge.ts"; |
| 19 | 19 | import { repoDir } from "../src/git/repos.ts"; |
| 20 | 20 | import { |
| @@ -80,10 +80,8 @@ describe("the crew, with a stand-in Claude Code: declines", () => { | ||
| 80 | 80 | }); |
| 81 | 81 | if (!opened.ok) throw new Error(String(opened.error)); |
| 82 | 82 | expect(assignIssue(app, alice, repo, opened.value, bot).ok).toBe(true); |
| 83 | const run = await settled((r) => r.issueId === opened.value.id); | |
| 84 | expect(run.state).toBe("failed"); | |
| 85 | expect(run.summary).toContain("lives in the repository itself"); | |
| 86 | expect(run.costUsd).toBe(0); | |
| 83 | // A person's own pull is theirs to push: no builder run is queued at all (#16953). | |
| 84 | expect(listRuns(app.db, repo.id, { kind: "agent" }).filter((r) => r.issueId === opened.value.id)).toHaveLength(0); | |
| 87 | 85 | }, 30_000); |
| 88 | 86 | |
| 89 | 87 | test("a pull whose branch lives in another person's fork is continued on a fresh branch in the agent's own", async () => { |
test/crew-red-main.test.ts +2
| @@ -50,6 +50,8 @@ describe("the crew, with a red main it shares", () => { | ||
| 50 | 50 | const o = await openPull(app, alice, repo, { title: branch, baseBranch: "main", headBranch: branch }); |
| 51 | 51 | if (!o.ok) throw new Error(String(o.error)); |
| 52 | 52 | app.db.run("UPDATE issue SET assignee_id = ?1 WHERE id = ?2", [bot.id, o.value.id]); |
| 53 | // The agent's own pull: a person's in-repository pull gets no builder run (#16953). | |
| 54 | app.db.run("UPDATE issue SET author_id = ?1 WHERE id = ?2", [bot.id, o.value.id]); | |
| 53 | 55 | return getPull(app.db, repo.id, o.value.number)!; |
| 54 | 56 | }; |
| 55 | 57 | const finish = (env: string, state: "ok" | "failed", names: string[], pull?: Pull) => { |
test/studio-update-pull.test.ts +3
| @@ -68,6 +68,9 @@ async function agentPull() { | ||
| 68 | 68 | const agent = await createAgent(app, alice, { username: `bot${n}` }); |
| 69 | 69 | if (!agent.ok) throw new Error(JSON.stringify(agent.error)); |
| 70 | 70 | app.db.query("UPDATE issue SET assignee_id = ?1 WHERE id = ?2").run(agent.value.user.id, opened.value.id); |
| 71 | // The agent's own pull: a person's in-repository pull is theirs, no builder run is queued | |
| 72 | // (#16953). | |
| 73 | app.db.query("UPDATE issue SET author_id = ?1 WHERE id = ?2").run(agent.value.user.id, opened.value.id); | |
| 71 | 74 | return opened.value; |
| 72 | 75 | } |
| 73 | 76 |
new above
offline