Public: anyone can read this; the crew can change it.
Review is owed from the check's green, not the preview's: a slow check queue lapsed every pull to a person tonight #1218
scout/forge:scout/1216-review-is-owed-from-the-check-s-green-no → main 1 commit fork
3 files
changed +234
−15
src/crew/review.ts +91 −14
| @@ -84,8 +84,16 @@ const REVIEW_TRIES = 2; | ||
| 84 | 84 | export interface ReviewAttempt { |
| 85 | 85 | readonly createdAt: string; |
| 86 | 86 | readonly active: boolean; |
| 87 | /** The reviewer it went to, when known. */ | |
| 88 | readonly actorId?: string | null; | |
| 87 | 89 | } |
| 88 | 90 | |
| 91 | /** | |
| 92 | * A head that went green this long after the lapse, with no review yet, | |
| 93 | * is still sent once, late; past it, nothing was ever going to send. | |
| 94 | */ | |
| 95 | const LATE_SEND_MS = REVIEW_LAPSE_MS; | |
| 96 | ||
| 89 | 97 | /** |
| 90 | 98 | * Where review stands on a green head that has no verdict: `send` a |
| 91 | 99 | * review run (the first, or the one retry), `wait` for the one that is |
| @@ -119,27 +127,71 @@ export function reviewStage( | ||
| 119 | 127 | export function reviewAttempts( |
| 120 | 128 | db: Database, |
| 121 | 129 | pull: Pull, |
| 122 | ): { greenAt: string; attempts: ReviewAttempt[] } | undefined { | |
| 123 | const green = db | |
| 124 | .query<{ at: string | null }, [string, string]>( | |
| 125 | `SELECT MIN(finished_at) AS at FROM run WHERE issue_id = ?1 AND commit_oid = ?2 | |
| 126 | AND kind IN ('check','deploy') AND state = 'ok'`, | |
| 130 | ): ReviewHistory | undefined { | |
| 131 | // Review is owed from the check's green: a preview deploy finishes in | |
| 132 | // seconds, a check may queue for an hour. A deploy counts only where the | |
| 133 | // head has no check at all. | |
| 134 | const hasCheck = | |
| 135 | db | |
| 136 | .query<{ n: number }, [string, string]>( | |
| 137 | `SELECT COUNT(*) AS n FROM run WHERE issue_id = ?1 AND commit_oid = ?2 AND kind = 'check'`, | |
| 138 | ) | |
| 139 | .get(pull.id, pull.headOid)!.n > 0; | |
| 140 | const greenRun = db | |
| 141 | .query<{ at: string | null; created: string | null }, [string, string, string]>( | |
| 142 | `SELECT MIN(finished_at) AS at, created_at AS created FROM run | |
| 143 | WHERE issue_id = ?1 AND commit_oid = ?2 AND kind = ?3 AND state = 'ok'`, | |
| 127 | 144 | ) |
| 128 | .get(pull.id, pull.headOid)?.at; | |
| 145 | .get(pull.id, pull.headOid, hasCheck ? "check" : "deploy"); | |
| 146 | const green = greenRun?.at; | |
| 129 | 147 | if (green === null || green === undefined) return undefined; |
| 148 | const checkMs = hasCheck | |
| 149 | ? Date.parse(green) - Date.parse(greenRun?.created ?? green) | |
| 150 | : 0; | |
| 130 | 151 | const released = lastRelease(db, pull.id); |
| 131 | 152 | const since = released !== undefined && released > green ? released : green; |
| 132 | 153 | const attempts = db |
| 133 | .query<{ created_at: string; state: string }, [string, string]>( | |
| 134 | `SELECT created_at, state FROM run WHERE issue_id = ?1 AND kind = 'agent' | |
| 154 | .query<{ created_at: string; state: string; actor_id: string | null }, [string, string]>( | |
| 155 | `SELECT created_at, state, actor_id FROM run WHERE issue_id = ?1 AND kind = 'agent' | |
| 135 | 156 | AND reason = 'review' AND created_at >= ?2 ORDER BY created_at, id`, |
| 136 | 157 | ) |
| 137 | 158 | .all(pull.id, since) |
| 138 | 159 | .map((r) => ({ |
| 139 | 160 | createdAt: r.created_at, |
| 140 | 161 | active: r.state === "queued" || r.state === "running", |
| 162 | actorId: r.actor_id, | |
| 141 | 163 | })); |
| 142 | return { greenAt: since, attempts }; | |
| 164 | const first = attempts[0]; | |
| 165 | const lateAt = | |
| 166 | first !== undefined && Date.parse(first.createdAt) - Date.parse(since) >= REVIEW_LAPSE_MS | |
| 167 | ? first.createdAt | |
| 168 | : undefined; | |
| 169 | return { greenAt: since, attempts, lateAt, checkMinutes: Math.round(checkMs / 60_000) }; | |
| 170 | } | |
| 171 | ||
| 172 | /** What the run table says about review on a head. */ | |
| 173 | export interface ReviewHistory { | |
| 174 | readonly greenAt: string; | |
| 175 | readonly attempts: ReviewAttempt[]; | |
| 176 | /** When the first review was sent, if it was sent past the lapse: the lapse clock restarts there. */ | |
| 177 | readonly lateAt: string | undefined; | |
| 178 | /** How long the check took, from queued to green. */ | |
| 179 | readonly checkMinutes: number; | |
| 180 | } | |
| 181 | ||
| 182 | /** | |
| 183 | * `reviewStage` for a history: a head that went green and was never sent | |
| 184 | * is sent once, late, before it lapses to a person; a review sent late | |
| 185 | * has the lapse counted from its sending. | |
| 186 | */ | |
| 187 | export function stageOf( | |
| 188 | history: ReviewHistory, | |
| 189 | now: Date, | |
| 190 | ): "send" | "wait" | "lapsed" | "late" { | |
| 191 | const age = now.getTime() - Date.parse(history.greenAt); | |
| 192 | if (history.attempts.length === 0 && age >= REVIEW_LAPSE_MS) | |
| 193 | return age < REVIEW_LAPSE_MS + LATE_SEND_MS ? "late" : "lapsed"; | |
| 194 | return reviewStage(history.lateAt ?? history.greenAt, history.attempts, now); | |
| 143 | 195 | } |
| 144 | 196 | |
| 145 | 197 | /** Why an agent's green pull waits for a person, and since when. */ |
| @@ -147,6 +199,8 @@ export interface ReviewWait { | ||
| 147 | 199 | readonly why: "no reviewer" | "lapsed"; |
| 148 | 200 | /** When review became owed: the head went green, or the pull was let go. */ |
| 149 | 201 | readonly since: string; |
| 202 | /** Minutes the check took, when the review was sent late because of it. */ | |
| 203 | readonly lateCheckMinutes?: number; | |
| 150 | 204 | } |
| 151 | 205 | |
| 152 | 206 | /** |
| @@ -170,8 +224,14 @@ export function waitsForPerson( | ||
| 170 | 224 | if (history === undefined) return undefined; |
| 171 | 225 | if (reviewersOf(db, repoId).length === 0) |
| 172 | 226 | return { why: "no reviewer", since: history.greenAt }; |
| 173 | return reviewStage(history.greenAt, history.attempts, now) === "lapsed" | |
| 174 | ? { why: "lapsed", since: history.greenAt } | |
| 227 | return stageOf(history, now) === "lapsed" | |
| 228 | ? { | |
| 229 | why: "lapsed", | |
| 230 | since: history.greenAt, | |
| 231 | ...(history.lateAt === undefined | |
| 232 | ? {} | |
| 233 | : { lateCheckMinutes: history.checkMinutes }), | |
| 234 | } | |
| 175 | 235 | : undefined; |
| 176 | 236 | } |
| 177 | 237 | |
| @@ -196,8 +256,9 @@ export async function sendToReview( | ||
| 196 | 256 | if (seen.approvals > 0 || seen.changesRequested > 0) return "done"; |
| 197 | 257 | const history = reviewAttempts(app.db, pull); |
| 198 | 258 | if (history === undefined) return "wait"; |
| 199 | const stage = reviewStage(history.greenAt, history.attempts, now); | |
| 200 | if (stage !== "send") return stage; | |
| 259 | const stage = stageOf(history, now); | |
| 260 | if (stage === "wait" || stage === "lapsed") return stage; | |
| 261 | const late = stage === "late"; | |
| 201 | 262 | const issue = getIssueById(app.db, pull.id); |
| 202 | 263 | if (!issue) return "wait"; |
| 203 | 264 | const { diff } = await pullDiff( |
| @@ -216,6 +277,7 @@ export async function sendToReview( | ||
| 216 | 277 | pull: pull.number, |
| 217 | 278 | reviewer: reviewer.user.username, |
| 218 | 279 | attempt: history.attempts.length + 1, |
| 280 | ...(late ? { late: true, checkMinutes: history.checkMinutes } : {}), | |
| 219 | 281 | }); |
| 220 | 282 | return "sent"; |
| 221 | 283 | } |
| @@ -241,8 +303,23 @@ export async function sweepReviews(app: App): Promise<number> { | ||
| 241 | 303 | limit: 50, |
| 242 | 304 | })) { |
| 243 | 305 | if (!pull.authorIsAgent || !greenHead(app.db, pull)) continue; |
| 244 | if ((await sendToReview(app, repo, pull)) === "sent") queued++; | |
| 306 | const result = await sendToReview(app, repo, pull); | |
| 307 | if (result === "sent") queued++; | |
| 308 | else if (result === "lapsed" || result === "wait") skipped(app, repo, pull, result); | |
| 245 | 309 | } |
| 246 | 310 | } |
| 247 | 311 | return queued; |
| 248 | 312 | } |
| 313 | ||
| 314 | /** One line for a pull the sweep leaves be, and why, so a stall reads in the log. */ | |
| 315 | function skipped(app: App, repo: Repo, pull: Pull, why: "wait" | "lapsed"): void { | |
| 316 | const history = reviewAttempts(app.db, pull); | |
| 317 | if (history === undefined) return; | |
| 318 | let line = `review: lapsed since ${history.greenAt.slice(11, 16)}`; | |
| 319 | if (why === "wait") { | |
| 320 | const out = history.attempts.at(-1)?.actorId; | |
| 321 | const who = reviewersOf(app.db, repo.id).find((r) => r.user.id === out); | |
| 322 | line = `review: waiting on ${who?.user.username ?? "the reviewer"}`; | |
| 323 | } | |
| 324 | log.info(line, { repo: `${repo.ownerName}/${repo.name}`, pull: pull.number }); | |
| 325 | } |
src/web/studio/asks.ts +3 −1
| @@ -647,7 +647,9 @@ function lineOf(c: AskCandidate): string { | ||
| 647 | 647 | |
| 648 | 648 | /** Why an agent's change waits for a person's review, in a clause. */ |
| 649 | 649 | const reviewWhy = (wait: ReviewWait | undefined): string => |
| 650 | wait?.why === "lapsed" | |
| 650 | wait?.why === "lapsed" && wait.lateCheckMinutes !== undefined | |
| 651 | ? `review ran late: the check took ${wait.lateCheckMinutes} min, and the reviewer did not answer, since ${wait.since.slice(0, 10)}` | |
| 652 | : wait?.why === "lapsed" | |
| 651 | 653 | ? `the reviewer did not answer, since ${wait.since.slice(0, 10)}` |
| 652 | 654 | : `no reviewer on the crew, since ${wait?.since.slice(0, 10) ?? "it went green"}`; |
| 653 | 655 |
test/review-from-check.test.ts added +140
| @@ -0,0 +1,140 @@ | ||
| 1 | /** | |
| 2 | * Review is owed from the check's green, not the preview deploy's (#1216): | |
| 3 | * a slow check queue must not lapse a pull before review was ever sent. | |
| 4 | */ | |
| 5 | ||
| 6 | import { afterAll, 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 { addToCrew, createAgent } from "../src/actions/crew.ts"; | |
| 11 | import { openPull } from "../src/actions/pulls.ts"; | |
| 12 | import { commitToBranch, createRepo } from "../src/actions/repos.ts"; | |
| 13 | import { createApp, type App } from "../src/app.ts"; | |
| 14 | import { setCrewRole } from "../src/core/agents.ts"; | |
| 15 | import type { Pull } from "../src/core/pulls.ts"; | |
| 16 | import { getRepoById, type Repo } from "../src/core/repos.ts"; | |
| 17 | import { claimRun, createRun, finishRun, listRuns } from "../src/core/runs.ts"; | |
| 18 | import { createUser, getUserByName, type User } from "../src/core/users.ts"; | |
| 19 | import { sendToReview, waitsForPerson } from "../src/crew/review.ts"; | |
| 20 | import { quietLogs } from "./helpers/globals.ts"; | |
| 21 | ||
| 22 | quietLogs(); | |
| 23 | ||
| 24 | const T0 = Date.parse("2026-10-13T03:00:00.000Z"); | |
| 25 | const at = (min: number) => new Date(T0 + min * 60_000); | |
| 26 | ||
| 27 | describe("review is owed from the check's green", () => { | |
| 28 | let dataDir = ""; | |
| 29 | let app: App; | |
| 30 | let alice: User; | |
| 31 | let repo: Repo; | |
| 32 | let n = 0; | |
| 33 | ||
| 34 | const fresh = () => getRepoById(app.db, repo.id)!; | |
| 35 | const reviewRuns = (pull: Pull) => | |
| 36 | listRuns(app.db, repo.id, { issueId: pull.id }).filter( | |
| 37 | (r) => r.reason === "review", | |
| 38 | ); | |
| 39 | const pullOn = async (): Promise<Pull> => { | |
| 40 | const branch = `b${++n}`; | |
| 41 | const c = await commitToBranch(app, alice, repo, { | |
| 42 | branch, | |
| 43 | base: "main", | |
| 44 | message: branch, | |
| 45 | files: [{ path: `${branch}.md`, content: "x\n" }], | |
| 46 | }); | |
| 47 | if (!c.ok) throw new Error(JSON.stringify(c.error)); | |
| 48 | const o = await openPull(app, alice, repo, { | |
| 49 | title: branch, | |
| 50 | body: "", | |
| 51 | baseBranch: "main", | |
| 52 | headBranch: branch, | |
| 53 | }); | |
| 54 | if (!o.ok) throw new Error(String(o.error)); | |
| 55 | return o.value; | |
| 56 | }; | |
| 57 | const green = (pull: Pull, kind: "check" | "deploy", queued: number, done: number) => { | |
| 58 | const run = createRun(app.db, { | |
| 59 | repoId: repo.id, | |
| 60 | kind, | |
| 61 | issueId: pull.id, | |
| 62 | env: `pull-${pull.number}`, | |
| 63 | commitOid: pull.headOid, | |
| 64 | reason: "pull", | |
| 65 | }); | |
| 66 | claimRun(app.db, run.id); | |
| 67 | finishRun(app.db, run.id, { state: "ok", summary: "ok" }); | |
| 68 | app.db | |
| 69 | .query("UPDATE run SET created_at = ?2, finished_at = ?3 WHERE id = ?1") | |
| 70 | .run(run.id, at(queued).toISOString(), at(done).toISOString()); | |
| 71 | }; | |
| 72 | const agentPull = async () => { | |
| 73 | const pull = await pullOn(); | |
| 74 | return { ...pull, authorIsAgent: true }; | |
| 75 | }; | |
| 76 | ||
| 77 | beforeAll(async () => { | |
| 78 | dataDir = await mkdtemp(join(tmpdir(), "forge-review-check-")); | |
| 79 | app = createApp({ dataDir, port: 0 }); | |
| 80 | const made = await createUser(app.db, { | |
| 81 | trusted: true, | |
| 82 | username: "alice", | |
| 83 | email: "[email protected]", | |
| 84 | password: "a-good-passphrase", | |
| 85 | adminIfFirst: true, | |
| 86 | }); | |
| 87 | if (!made.ok) throw new Error(made.error); | |
| 88 | alice = made.value; | |
| 89 | const r = await createRepo(app, alice, { name: "garden", visibility: "private" }); | |
| 90 | if (!r.ok) throw new Error(String(r.error)); | |
| 91 | repo = r.value; | |
| 92 | const seeded = await commitToBranch(app, alice, repo, { | |
| 93 | message: "seed", | |
| 94 | files: [{ path: "README.md", content: "# garden\n" }], | |
| 95 | }); | |
| 96 | if (!seeded.ok) throw new Error(JSON.stringify(seeded.error)); | |
| 97 | const agent = await createAgent(app, alice, { username: "quill" }); | |
| 98 | if (!agent.ok) throw new Error(String(agent.error)); | |
| 99 | addToCrew(app, alice, repo, "quill"); | |
| 100 | setCrewRole(app.db, repo.id, getUserByName(app.db, "quill")!.id, "reviewer"); | |
| 101 | }, 30_000); | |
| 102 | ||
| 103 | afterAll(async () => { | |
| 104 | app.close(); | |
| 105 | await rm(dataDir, { recursive: true, force: true }); | |
| 106 | }); | |
| 107 | ||
| 108 | test("a deploy green at t=0 and a check green at t=70 min: sent at t=70, not lapsed", async () => { | |
| 109 | const pull = await agentPull(); | |
| 110 | green(pull, "deploy", 0, 0); | |
| 111 | green(pull, "check", 0, 70); | |
| 112 | expect(waitsForPerson(app.db, repo.id, pull, at(70))).toBeUndefined(); | |
| 113 | expect(await sendToReview(app, fresh(), pull, at(70))).toBe("sent"); | |
| 114 | expect(reviewRuns(pull)).toHaveLength(1); | |
| 115 | }); | |
| 116 | ||
| 117 | test("a head with no check is reviewed from its deploy, as before", async () => { | |
| 118 | const pull = await agentPull(); | |
| 119 | green(pull, "deploy", 0, 0); | |
| 120 | expect(await sendToReview(app, fresh(), pull, at(1))).toBe("sent"); | |
| 121 | expect(await sendToReview(app, fresh(), pull, at(1))).toBe("wait"); | |
| 122 | }); | |
| 123 | ||
| 124 | test("a head that went green long ago and was never sent is sent once, late, and says so if it lapses", async () => { | |
| 125 | const pull = await agentPull(); | |
| 126 | green(pull, "check", 0, 72); | |
| 127 | // Green at 72; the first look comes at 140, past the lapse. | |
| 128 | expect(await sendToReview(app, fresh(), pull, at(72 + 70))).toBe("sent"); | |
| 129 | const run = reviewRuns(pull)[0]!; | |
| 130 | app.db | |
| 131 | .query("UPDATE run SET created_at = ?2 WHERE id = ?1") | |
| 132 | .run(run.id, at(72 + 70).toISOString()); | |
| 133 | // The late review is out: the lapse counts from its sending. | |
| 134 | expect(waitsForPerson(app.db, repo.id, pull, at(72 + 80))).toBeUndefined(); | |
| 135 | app.db.query("UPDATE run SET state = 'failed' WHERE id = ?1").run(run.id); | |
| 136 | const wait = waitsForPerson(app.db, repo.id, pull, at(72 + 70 + 61)); | |
| 137 | expect(wait?.why).toBe("lapsed"); | |
| 138 | expect(wait?.lateCheckMinutes).toBe(72); | |
| 139 | }); | |
| 140 | }); |
new above
offline