Public: anyone can read this; the crew can change it.
A pull's review starts only once its check is green, and a run waiting on the… #16115
Check · Review · Land · Live done — Landed and live in production.
torque/forge:torque/16094-a-pull-s-review-starts-only-once-its-che → main 0 commits fork
4 files
changed +273
−8
src/crew/review.ts +39 −4
| @@ -163,6 +163,18 @@ export function greenHead(db: Database, pull: Pull): boolean { | ||
| 163 | 163 | return checks.length > 0 && checks.every((c) => c.state === "ok"); |
| 164 | 164 | } |
| 165 | 165 | |
| 166 | /** | |
| 167 | * Whether the pull's head still has a check or deploy queued or running (#16094). A review | |
| 168 | * run that starts then only waits behind the check queue, which can outlast its time limit: | |
| 169 | * it is not started, and its time does not count. | |
| 170 | */ | |
| 171 | export function waitingOnCheck(db: Database, pull: Pull): boolean { | |
| 172 | return verdictForHead(db, pull.id, pull.headOid).some((c) => c.state === "queued" || c.state === "running"); | |
| 173 | } | |
| 174 | ||
| 175 | /** What a review run that stood down for a pending check says: no attempt, not queued again. */ | |
| 176 | export const CHECK_PENDING_SUMMARY = "stood down: the head's check is still queued; review starts when it is green"; | |
| 177 | ||
| 166 | 178 | /** How long a review may take from being queued before it is sent again. */ |
| 167 | 179 | export const REVIEW_WAIT_MS = 20 * 60_000; |
| 168 | 180 | /** How long a green head waits for a verdict at most, however the attempts went. */ |
| @@ -256,17 +268,27 @@ export function reviewAttemptsFor( | ||
| 256 | 268 | ) |
| 257 | 269 | .all(...batch.flatMap((p) => [p.id, p.headOid])); |
| 258 | 270 | const released = lastReleasesFor(db, ids); |
| 259 | const reviews = new Map<string, { createdAt: string; active: boolean; reviewerId: string | null }[]>(); | |
| 271 | const reviews = new Map< | |
| 272 | string, | |
| 273 | { createdAt: string; finishedAt: string | null; cancelled: boolean; active: boolean; reviewerId: string | null }[] | |
| 274 | >(); | |
| 260 | 275 | for (const r of db |
| 261 | .query<{ issue_id: string; created_at: string; state: string; actor_id: string | null; summary: string | null }, string[]>( | |
| 262 | `SELECT issue_id, created_at, state, actor_id, summary FROM run INDEXED BY run_review_attempts | |
| 276 | .query< | |
| 277 | { issue_id: string; created_at: string; finished_at: string | null; state: string; actor_id: string | null; summary: string | null }, | |
| 278 | string[] | |
| 279 | >( | |
| 280 | `SELECT issue_id, created_at, finished_at, state, actor_id, summary FROM run INDEXED BY run_review_attempts | |
| 263 | 281 | WHERE kind = 'agent' AND reason = 'review' |
| 264 | 282 | AND issue_id IN (${ids.map((_, n) => `?${n + 1}`).join(",")}) |
| 265 | 283 | ORDER BY created_at, id`, |
| 266 | 284 | ) |
| 267 | 285 | .all(...ids)) { |
| 268 | 286 | if (diedBeforeAgent(r.state, r.summary)) continue; |
| 287 | // A run that stood down for the check queue was never an attempt (#16094). | |
| 288 | if (r.state === "cancelled" && r.summary === CHECK_PENDING_SUMMARY) continue; | |
| 269 | 289 | const attempt = { |
| 290 | finishedAt: r.finished_at, | |
| 291 | cancelled: r.state === "cancelled", | |
| 270 | 292 | createdAt: r.created_at, |
| 271 | 293 | active: r.state === "queued" || r.state === "running", |
| 272 | 294 | reviewerId: r.actor_id, |
| @@ -291,7 +313,20 @@ export function reviewAttemptsFor( | ||
| 291 | 313 | const green = greenRun.finished_at!; |
| 292 | 314 | const checkMs = hasCheck ? Date.parse(green) - Date.parse(greenRun.created_at) : 0; |
| 293 | 315 | const release = released.get(pull.id); |
| 294 | const since = release !== undefined && release > green ? release : green; | |
| 316 | let since = release !== undefined && release > green ? release : green; | |
| 317 | // A review that ended cancelled while a check or deploy of this head was queued or | |
| 318 | // running starved behind the queue: no attempt, and the lapse clock restarts when that | |
| 319 | // check finished (#16094). One that overran after the check was green still counts. | |
| 320 | const starved = (reviews.get(pull.id) ?? []).flatMap((a) => { | |
| 321 | if (!a.cancelled || a.finishedAt === null) return []; | |
| 322 | return runs | |
| 323 | .filter((c) => c.finished_at !== null && c.created_at < a.finishedAt! && c.finished_at > a.createdAt) | |
| 324 | .map((c) => c.finished_at!); | |
| 325 | }); | |
| 326 | if (starved.length > 0) { | |
| 327 | const restart = starved.reduce((m, f) => (f > m ? f : m)); | |
| 328 | if (restart > since) since = restart; | |
| 329 | } | |
| 295 | 330 | const attempts = (reviews.get(pull.id) ?? []).filter((a) => a.createdAt >= since); |
| 296 | 331 | found.push({ pull, since, attempts, checkMinutes: Math.round(checkMs / 60_000) }); |
| 297 | 332 | } |
src/crew/runner.ts +17 −1
| @@ -121,7 +121,7 @@ import { | ||
| 121 | 121 | namedCommits, |
| 122 | 122 | onBase, |
| 123 | 123 | } from "./already.ts"; |
| 124 | import { touchesDesign } from "./review.ts"; | |
| 124 | import { CHECK_PENDING_SUMMARY, touchesDesign, waitingOnCheck } from "./review.ts"; | |
| 125 | 125 | import { fetchSealedHead, SEALED_REMOTE, sealedHeadRepo } from "./sealed_head.ts"; |
| 126 | 126 | import { BUDGETS, budgetSummary } from "../kernel/budgets.ts"; |
| 127 | 127 | import { createLog, holdValues, redactValues, releaseValues } from "../kernel/log.ts"; |
| @@ -457,6 +457,14 @@ export async function executeAgentRun( | ||
| 457 | 457 | } |
| 458 | 458 | } |
| 459 | 459 | |
| 460 | /** Whether this review run is for an open agent pull whose head's check or deploy is pending. */ | |
| 461 | function reviewWaitsOnCheck(app: App, run: Run): boolean { | |
| 462 | const issue = run.issueId === null ? undefined : getIssueById(app.db, run.issueId); | |
| 463 | if (issue === undefined || issue.kind !== "pull") return false; | |
| 464 | const pull = getPull(app.db, run.repoId, issue.number); | |
| 465 | return pull !== undefined && pull.state === "open" && pull.authorIsAgent && waitingOnCheck(app.db, pull); | |
| 466 | } | |
| 467 | ||
| 460 | 468 | async function runClaimed( |
| 461 | 469 | app: App, |
| 462 | 470 | run: Run, |
| @@ -477,6 +485,14 @@ async function runClaimed( | ||
| 477 | 485 | if (skipped) announceRun(app, skipped); |
| 478 | 486 | return undefined; |
| 479 | 487 | } |
| 488 | // A review of a head whose check is still queued would only wait behind the check queue, | |
| 489 | // past its time limit: it stands down, is no attempt and is not queued again. The check's | |
| 490 | // green and the sweep send it (#16094). | |
| 491 | if (run.reason === "review" && run.startedAt === null && reviewWaitsOnCheck(app, run)) { | |
| 492 | const stood = cancelRun(app.db, run.id, CHECK_PENDING_SUMMARY); | |
| 493 | if (stood) announceRun(app, stood); | |
| 494 | return undefined; | |
| 495 | } | |
| 480 | 496 | const claimed = claimRun(app.db, run.id); |
| 481 | 497 | if (!claimed) return undefined; |
| 482 | 498 | began.claimed = true; |
test/crew-review-budget.test.ts +42 −3
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | /** |
| 2 | * The crew with a stand-in Claude Code: a review run past its budget. | |
| 3 | * The stand-in forge is `helpers/crew-standin.ts`; split from | |
| 2 | * The crew with a stand-in Claude Code: a review run past its budget, and one for a head whose | |
| 3 | * check is queued (#16094). The stand-in forge is `helpers/crew-standin.ts`; split from | |
| 4 | 4 | * `crew.test.ts` so each part runs apart (#1798). |
| 5 | 5 | */ |
| 6 | 6 | |
| @@ -15,7 +15,8 @@ import { commitToBranch } from "../src/actions/repos.ts"; | ||
| 15 | 15 | import { queueAgentRun } from "../src/crew/dispatch.ts"; |
| 16 | 16 | import {} from "../src/core/agents.ts"; |
| 17 | 17 | import { getIssue } from "../src/core/issues.ts"; |
| 18 | import { listRuns, type Run } from "../src/core/runs.ts"; | |
| 18 | import { CHECK_PENDING_SUMMARY } from "../src/crew/review.ts"; | |
| 19 | import { createRun, listRuns, type Run } from "../src/core/runs.ts"; | |
| 19 | 20 | import { |
| 20 | 21 | agentRunSettled, |
| 21 | 22 | startStandInCrew, |
| @@ -78,4 +79,42 @@ describe("the crew, with a stand-in Claude Code: review budget", () => { | ||
| 78 | 79 | /^budget: a review run/, |
| 79 | 80 | ); |
| 80 | 81 | }, 10_000); |
| 82 | ||
| 83 | test("a review run for a head whose check is queued stands down: no attempt, not queued again", async () => { | |
| 84 | const made = await createAgent(app, alice, { username: "waitrev" }); | |
| 85 | if (!made.ok) throw new Error(String(made.error)); | |
| 86 | expect(addToCrew(app, alice, repo, "waitrev").ok).toBe(true); | |
| 87 | seatAs(app.db, repo.id, made.value.user.id, "reviewer"); | |
| 88 | const branch = await commitToBranch(app, alice, repo, { | |
| 89 | branch: "waits-on-check", | |
| 90 | base: "main", | |
| 91 | message: "Waits on its check", | |
| 92 | files: [{ path: "WAIT.md", content: "wait\n" }], | |
| 93 | }); | |
| 94 | if (!branch.ok) throw new Error(JSON.stringify(branch.error)); | |
| 95 | const opened = await openPull(app, alice, repo, { | |
| 96 | title: "SLOW-REVIEW waits", | |
| 97 | baseBranch: "main", | |
| 98 | headBranch: "waits-on-check", | |
| 99 | }); | |
| 100 | if (!opened.ok) throw new Error(String(opened.error)); | |
| 101 | const issue = getIssue(app.db, repo.id, opened.value.number)!; | |
| 102 | app.db.query("UPDATE issue SET author_id = ?2 WHERE id = ?1").run(issue.id, made.value.user.id); | |
| 103 | createRun(app.db, { | |
| 104 | repoId: repo.id, | |
| 105 | kind: "check", | |
| 106 | issueId: issue.id, | |
| 107 | env: `pull-${issue.number}`, | |
| 108 | commitOid: opened.value.headOid, | |
| 109 | reason: "pull", | |
| 110 | }); | |
| 111 | expect(queueAgentRun(app, repo, issue, made.value, "review")).toBeDefined(); | |
| 112 | const stood = await settled((r) => r.reason === "review" && r.issueId === issue.id && r.state === "cancelled"); | |
| 113 | expect(stood.summary).toBe(CHECK_PENDING_SUMMARY); | |
| 114 | expect(stood.startedAt).toBeNull(); | |
| 115 | const reviews = listRuns(app.db, repo.id, { kind: "agent" }).filter( | |
| 116 | (r) => r.reason === "review" && r.issueId === issue.id, | |
| 117 | ); | |
| 118 | expect(reviews).toHaveLength(1); | |
| 119 | }, 10_000); | |
| 81 | 120 | }); |
test/review-check-queue.test.ts added +175
| @@ -0,0 +1,175 @@ | ||
| 1 | import { seatAs } from "./helpers/seat.ts"; | |
| 2 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 3 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 4 | import { tmpdir } from "node:os"; | |
| 5 | import { join } from "node:path"; | |
| 6 | import { createApp, type App } from "../src/app.ts"; | |
| 7 | import { addToCrew, createAgent } from "../src/actions/crew.ts"; | |
| 8 | import { openPull } from "../src/actions/pulls.ts"; | |
| 9 | import { commitToBranch, createRepo } from "../src/actions/repos.ts"; | |
| 10 | import { type Pull } from "../src/core/pulls.ts"; | |
| 11 | import { type Repo } from "../src/core/repos.ts"; | |
| 12 | import { markVerified } from "../src/core/verify.ts"; | |
| 13 | import { setLimit } from "../src/settings/limits.ts"; | |
| 14 | import { createRun, listRuns } from "../src/core/runs.ts"; | |
| 15 | import { createUser, getUserByName, type User } from "../src/core/users.ts"; | |
| 16 | import { | |
| 17 | CHECK_PENDING_SUMMARY, | |
| 18 | waitingOnCheck, | |
| 19 | reviewAttempts, | |
| 20 | sweepReviews, | |
| 21 | waitsForPerson, | |
| 22 | } from "../src/crew/review.ts"; | |
| 23 | ||
| 24 | /** | |
| 25 | * A review waits for the pull's check, however long the check queue is (#16094): a pull whose | |
| 26 | * check sits queued past the review window is neither sent nor lapsed; a review that starved | |
| 27 | * behind a re-queued check is no attempt; one that overran after green still is. | |
| 28 | */ | |
| 29 | const MIN = 60_000; | |
| 30 | ||
| 31 | describe("review and the check queue", () => { | |
| 32 | let dataDir: string; | |
| 33 | let app: App; | |
| 34 | let alice: User; | |
| 35 | let repo: Repo; | |
| 36 | const T0 = Date.now() - 6 * 60 * MIN; | |
| 37 | const iso = (m: number) => new Date(T0 + m * MIN).toISOString(); | |
| 38 | ||
| 39 | beforeAll(async () => { | |
| 40 | dataDir = await mkdtemp(join(tmpdir(), "forge-review-queue-")); | |
| 41 | app = createApp({ dataDir, port: 0 }); | |
| 42 | const a = await createUser(app.db, { | |
| 43 | origin: "seed", | |
| 44 | trusted: true, | |
| 45 | username: "alice", | |
| 46 | email: "[email protected]", | |
| 47 | password: "a-good-passphrase", | |
| 48 | adminIfFirst: true, | |
| 49 | }); | |
| 50 | if (!a.ok) throw new Error(a.error); | |
| 51 | alice = a.value; | |
| 52 | markVerified(app.db, alice.id, new Date().toISOString()); | |
| 53 | for (const [key, value] of [ | |
| 54 | ["signup.per_source", 1000], | |
| 55 | ["guest.pulls_per_day", 1000], | |
| 56 | ] as const) | |
| 57 | setLimit(app.db, key, value, "setup"); | |
| 58 | const made = await createRepo(app, alice, { name: "engine", visibility: "private" }); | |
| 59 | if (!made.ok) throw new Error(String(made.error)); | |
| 60 | repo = made.value; | |
| 61 | await commitToBranch(app, alice, repo, { | |
| 62 | message: "Start", | |
| 63 | files: [{ path: "README.md", content: "# engine\n" }], | |
| 64 | }); | |
| 65 | for (const name of ["scout", "juniper"]) { | |
| 66 | const agent = await createAgent(app, alice, { username: name, brief: `I am ${name}.` }); | |
| 67 | if (!agent.ok) throw new Error(String(agent.error)); | |
| 68 | expect(addToCrew(app, alice, repo, name).ok).toBe(true); | |
| 69 | } | |
| 70 | seatAs(app.db, repo.id, getUserByName(app.db, "juniper")!.id, "reviewer"); | |
| 71 | }, 60_000); | |
| 72 | ||
| 73 | afterAll(async () => { | |
| 74 | await app.close?.(); | |
| 75 | await rm(dataDir, { recursive: true, force: true }); | |
| 76 | }); | |
| 77 | ||
| 78 | function settle(id: string, state: string, summary: string, createdMin: number, finishedMin: number | null): void { | |
| 79 | app.db | |
| 80 | .query("UPDATE run SET state = ?2, summary = ?3, created_at = ?4, started_at = ?4, finished_at = ?5 WHERE id = ?1") | |
| 81 | .run(id, state, summary, iso(createdMin), finishedMin === null ? null : iso(finishedMin)); | |
| 82 | } | |
| 83 | ||
| 84 | async function agentPull(name: string): Promise<Pull> { | |
| 85 | await commitToBranch(app, alice, repo, { | |
| 86 | branch: `feature/${name}`, | |
| 87 | base: "main", | |
| 88 | message: name, | |
| 89 | files: [{ path: `docs/${name}.md`, content: `${name}\n` }], | |
| 90 | }); | |
| 91 | const opened = await openPull(app, alice, repo, { | |
| 92 | title: name, | |
| 93 | body: "", | |
| 94 | baseBranch: "main", | |
| 95 | headBranch: `feature/${name}`, | |
| 96 | }); | |
| 97 | if (!opened.ok) throw new Error(String(opened.error)); | |
| 98 | const pull = opened.value; | |
| 99 | app.db.query("UPDATE issue SET author_id = ?2 WHERE id = ?1").run(pull.id, getUserByName(app.db, "scout")!.id); | |
| 100 | return { ...pull, authorIsAgent: true }; | |
| 101 | } | |
| 102 | ||
| 103 | function check(pull: Pull, state: string, createdMin: number, finishedMin: number | null): void { | |
| 104 | const run = createRun(app.db, { | |
| 105 | repoId: repo.id, | |
| 106 | kind: "check", | |
| 107 | issueId: pull.id, | |
| 108 | env: `pull-${pull.number}`, | |
| 109 | commitOid: pull.headOid, | |
| 110 | reason: "pull", | |
| 111 | }); | |
| 112 | settle(run.id, state, state === "ok" ? "check passed" : "", createdMin, finishedMin); | |
| 113 | } | |
| 114 | ||
| 115 | function review(pull: Pull, state: string, summary: string, createdMin: number, finishedMin: number): void { | |
| 116 | const run = createRun(app.db, { | |
| 117 | repoId: repo.id, | |
| 118 | kind: "agent", | |
| 119 | issueId: pull.id, | |
| 120 | reason: "review", | |
| 121 | actorId: getUserByName(app.db, "juniper")!.id, | |
| 122 | }); | |
| 123 | settle(run.id, state, summary, createdMin, finishedMin); | |
| 124 | } | |
| 125 | ||
| 126 | const reviews = (pull: Pull) => listRuns(app.db, pull.repoId, { issueId: pull.id }).filter((r) => r.reason === "review"); | |
| 127 | const overran = "a review run took 30 min, past its 30 min budget; queued again"; | |
| 128 | ||
| 129 | test("a check queued for hours: no review is sent, the pull shows waiting, it never lapses", async () => { | |
| 130 | const pull = await agentPull("queued"); | |
| 131 | check(pull, "queued", 0, null); | |
| 132 | expect(waitingOnCheck(app.db, pull)).toBe(true); | |
| 133 | const now = new Date(T0 + 300 * MIN); | |
| 134 | expect(await sweepReviews(app, now)).toBe(0); | |
| 135 | expect(reviews(pull)).toHaveLength(0); | |
| 136 | expect(reviewAttempts(app.db, pull, now)).toBeUndefined(); | |
| 137 | expect(waitsForPerson(app.db, repo.id, pull, now)).toBeUndefined(); | |
| 138 | }); | |
| 139 | ||
| 140 | test("reviews that starved behind a re-queued check are no attempt: not lapsed, one new review after green", async () => { | |
| 141 | const pull = await agentPull("starved"); | |
| 142 | check(pull, "ok", -10, 0); | |
| 143 | // The check ran again: queued at 10, green at 180. Two reviews overran behind it. | |
| 144 | check(pull, "ok", 10, 180); | |
| 145 | review(pull, "cancelled", overran, 12, 42); | |
| 146 | review(pull, "cancelled", overran, 50, 80); | |
| 147 | const now = new Date(T0 + 190 * MIN); | |
| 148 | expect(waitingOnCheck(app.db, pull)).toBe(false); | |
| 149 | expect(reviewAttempts(app.db, pull, now)?.attempts).toHaveLength(0); | |
| 150 | expect(waitsForPerson(app.db, repo.id, pull, now)).toBeUndefined(); | |
| 151 | expect(await sweepReviews(app, now)).toBe(1); | |
| 152 | expect(reviews(pull).filter((r) => r.state === "queued")).toHaveLength(1); | |
| 153 | // Asked again: it waits, it does not send a second. | |
| 154 | expect(await sweepReviews(app, now)).toBe(0); | |
| 155 | }, 60_000); | |
| 156 | ||
| 157 | test("a run that stood down for the queue is no attempt; overruns after green still count, so there is no loop", async () => { | |
| 158 | const stood = await agentPull("stood"); | |
| 159 | check(stood, "ok", -10, 0); | |
| 160 | review(stood, "cancelled", CHECK_PENDING_SUMMARY, 5, 5); | |
| 161 | review(stood, "cancelled", CHECK_PENDING_SUMMARY, 6, 6); | |
| 162 | review(stood, "cancelled", CHECK_PENDING_SUMMARY, 7, 7); | |
| 163 | expect(reviewAttempts(app.db, stood, new Date(T0 + 30 * MIN))?.attempts).toHaveLength(0); | |
| 164 | ||
| 165 | const slow = await agentPull("slow"); | |
| 166 | check(slow, "ok", -10, 0); | |
| 167 | review(slow, "cancelled", overran, 5, 35); | |
| 168 | review(slow, "cancelled", overran, 36, 66); | |
| 169 | const now = new Date(T0 + 70 * MIN); | |
| 170 | expect(reviewAttempts(app.db, slow, now)?.attempts).toHaveLength(2); | |
| 171 | expect(waitsForPerson(app.db, repo.id, slow, now)?.why).toBe("lapsed"); | |
| 172 | expect(await sweepReviews(app, now)).not.toBeGreaterThan(1); | |
| 173 | expect(reviews(slow)).toHaveLength(2); | |
| 174 | }, 60_000); | |
| 175 | }); |
new above
offline