Public: anyone can read this; the crew can change it.
Checks cut by restarts are recorded as red on the pull and sent back to the builder #12597
Closed 18d 3h ago
scout/forge:scout/12594-checks-cut-by-restarts-are-recorded-as-r → main 2 commits fork
8 files
changed +163
−24
src/apps/runtime.ts +5 −4
| @@ -769,10 +769,11 @@ export class AppsRuntime { | ||
| 769 | 769 | // Deploys are failed; a check the last forge left is queued again while |
| 770 | 770 | // its commit still heads an open pull; an agent run is the crew's, |
| 771 | 771 | // which queues it again rather than failing it. |
| 772 | const abandoned = | |
| 773 | failAbandonedRuns(this.app.db, STALE_AFTER_MS, ["deploy"]) + | |
| 774 | requeueAbandonedChecks(this.app, STALE_AFTER_MS); | |
| 775 | if (abandoned > 0) log.info("settled abandoned runs", { count: abandoned }); | |
| 772 | const deploys = failAbandonedRuns(this.app.db, STALE_AFTER_MS, ["deploy"]); | |
| 773 | const cut = requeueAbandonedChecks(this.app, STALE_AFTER_MS); | |
| 774 | if (deploys + cut > 0) log.info("settled abandoned runs", { count: deploys + cut }); | |
| 775 | // After an unclean stop: one line says how many checks were cut and queued again (#12594). | |
| 776 | if (cut > 0) log.info(`boot after an unclean stop: ${cut} check${cut === 1 ? "" : "s"} cut by it, queued again`, { cut }); | |
| 776 | 777 | // An awake interval the last forge left open ends at its last heartbeat. |
| 777 | 778 | const orphans = this.metered(() => closeOrphans(this.app.db)); |
| 778 | 779 | if (orphans) log.info("closed open meter intervals", { count: orphans }); |
src/checks/recheck.ts +29 −1
| @@ -7,7 +7,7 @@ | ||
| 7 | 7 | import type { App } from "../app.ts"; |
| 8 | 8 | import { listOpenPulls, type Pull } from "../core/pulls.ts"; |
| 9 | 9 | import type { Repo } from "../core/repos.ts"; |
| 10 | import { getRun, type Run } from "../core/runs.ts"; | |
| 10 | import { getRun, isCut, type Run } from "../core/runs.ts"; | |
| 11 | 11 | import type { User } from "../core/users.ts"; |
| 12 | 12 | import { askCheck } from "./again.ts"; |
| 13 | 13 | |
| @@ -53,3 +53,31 @@ export async function recheckNamed( | ||
| 53 | 53 | } |
| 54 | 54 | return { queued, left: Math.max(0, red.length - max) }; |
| 55 | 55 | } |
| 56 | ||
| 57 | /** | |
| 58 | * Every open pull whose latest check on its head was cut by the forge or | |
| 59 | * failed, and ended inside `[since, until]`, asked for again in one call | |
| 60 | * (#12594): after a bad release, one request instead of one per pull. | |
| 61 | */ | |
| 62 | export async function recheckWindow( | |
| 63 | app: App, | |
| 64 | user: User, | |
| 65 | repo: Repo, | |
| 66 | since: string, | |
| 67 | until: string, | |
| 68 | max = RECHECK_MAX, | |
| 69 | ): Promise<Rechecked> { | |
| 70 | const hit = listOpenPulls(app.db, repo.id).filter((pull) => { | |
| 71 | if (pull.headOid === "") return false; | |
| 72 | const run = latestCheck(app, pull); | |
| 73 | if (run === undefined || run.finishedAt === null) return false; | |
| 74 | if (run.state !== "failed" && !isCut(run)) return false; | |
| 75 | return run.finishedAt >= since && run.finishedAt <= until; | |
| 76 | }); | |
| 77 | const queued: number[] = []; | |
| 78 | for (const pull of hit.slice(0, max)) { | |
| 79 | const asked = await askCheck(app, user, repo, pull); | |
| 80 | if (asked.ok) queued.push(pull.number); | |
| 81 | } | |
| 82 | return { queued, left: Math.max(0, hit.length - max) }; | |
| 83 | } |
src/checks/service.ts +1 −1
| @@ -962,7 +962,7 @@ export function startChecks(app: App, options: ChecksOptions): ChecksHandle { | ||
| 962 | 962 | write(cancelledLine(row) ?? `superseded: ${row.summary}`); |
| 963 | 963 | return; |
| 964 | 964 | } |
| 965 | if (!outcome.ok && stopped && signal.aborted) { | |
| 965 | if (!outcome.ok && (stopped || signal.aborted)) { | |
| 966 | 966 | // Cut off by this forge stopping, not by the check: the same check |
| 967 | 967 | // goes back on the queue, and the forge that carries on runs it. |
| 968 | 968 | const moved = requeueCheck(app.db, run.id, undefined, REQUEUED_SUMMARY, againAt()); |
src/contract/openapi/routes/postApiV1ReposOwnerNameChecksRecheck.ts +2 −1
| @@ -3,12 +3,13 @@ import { REPO, type Route } from "../shared.ts"; | ||
| 3 | 3 | export const route: Route = { |
| 4 | 4 | method: "post", |
| 5 | 5 | path: `${REPO}/checks/recheck`, |
| 6 | summary: "Ask again for every open pull whose latest check on its head failed on the test named in the body { test } (admin scope, a repository admin's; at most 50 per call); answers { queued: [numbers] }, with more when some were left", | |
| 6 | summary: "Ask again for every open pull whose latest check on its head failed on the test named in the body { test } (admin scope, a repository admin's; at most 50 per call); answers { queued: [numbers] }, with more when some were left; with { since, until? } instead, every open pull whose latest check was cut by a forge stop or failed and ended inside that window", | |
| 7 | 7 | auth: true, |
| 8 | 8 | role: "member", |
| 9 | 9 | status: 200, |
| 10 | 10 | reply: { queued: "array" }, |
| 11 | 11 | refusals: { |
| 12 | invalid_window: "since and until are instants, since not after until (400)", | |
| 12 | 13 | invalid_test: "test is a name of 1 to 300 characters (400)", |
| 13 | 14 | }, |
| 14 | 15 | }; |
src/core/runs.ts +35 −4
| @@ -633,6 +633,27 @@ export const DISK_USED_SUMMARY = "waiting: the forge's disk is 80% used; queued | ||
| 633 | 633 | */ |
| 634 | 634 | export const CHECK_RESTART_LIMIT = 3; |
| 635 | 635 | |
| 636 | /** | |
| 637 | * A check cut off after running less than this did not use up an attempt: a | |
| 638 | * crash loop kills a check seconds after it starts, again and again, and | |
| 639 | * that must never exhaust the count (#12594). | |
| 640 | */ | |
| 641 | export const CRASH_LOOP_MS = 60_000; | |
| 642 | ||
| 643 | /** What a check says when the forge kept going away under it for long stretches: cut, never failed. */ | |
| 644 | export const CUT_LIMIT_SUMMARY = "cut: the forge kept going away under this check"; | |
| 645 | ||
| 646 | /** Summaries that mean the forge, not the change, ended a check: such a run is cut, never red. */ | |
| 647 | const CUT_PREFIXES = ["the forge ", "cut:", REQUEUED_SUMMARY]; | |
| 648 | ||
| 649 | /** Whether a run ended because the forge stopped (a restart, a crash), not because the change was judged. */ | |
| 650 | export function isCut(run: Pick<Run, "state" | "summary">): boolean { | |
| 651 | return ( | |
| 652 | (run.state === "failed" || run.state === "cancelled") && | |
| 653 | CUT_PREFIXES.some((p) => run.summary.startsWith(p)) | |
| 654 | ); | |
| 655 | } | |
| 656 | ||
| 636 | 657 | /** |
| 637 | 658 | * Cancel a check the forge's own going-away cut off and queue the same |
| 638 | 659 | * check again, `attempt + 1` — the one way a check is retried, from a |
| @@ -668,7 +689,10 @@ export function requeueCheck( | ||
| 668 | 689 | env: cancelled.env, |
| 669 | 690 | commitOid: at ?? cancelled.commitOid, |
| 670 | 691 | reason: cancelled.reason, |
| 671 | attempt: cancelled.attempt + 1, | |
| 692 | // A forge's own cut after a few seconds is a crash loop's: not an attempt. | |
| 693 | attempt: | |
| 694 | cancelled.attempt + | |
| 695 | (summary === REQUEUED_SUMMARY && livedMs(cancelled) < CRASH_LOOP_MS ? 0 : 1), | |
| 672 | 696 | ...(cancelled.issueId === null ? {} : { issueId: cancelled.issueId }), |
| 673 | 697 | ...(cancelled.actorId === null ? {} : { actorId: cancelled.actorId }), |
| 674 | 698 | }); |
| @@ -676,6 +700,12 @@ export function requeueCheck( | ||
| 676 | 700 | })(); |
| 677 | 701 | } |
| 678 | 702 | |
| 703 | /** How long a run had been running when it was last read; a run that never started lived 0. */ | |
| 704 | function livedMs(run: Run): number { | |
| 705 | if (run.startedAt === null) return 0; | |
| 706 | return Math.max(0, Date.now() - Date.parse(run.startedAt)); | |
| 707 | } | |
| 708 | ||
| 679 | 709 | /** |
| 680 | 710 | * Whether the run before this one in its chain (the attempt just below, same |
| 681 | 711 | * kind, repository, reason and issue) was killed for its budget: the second |
| @@ -794,8 +824,8 @@ export function sweepAbandonedChecks( | ||
| 794 | 824 | } else if (row.attempt >= CHECK_RESTART_LIMIT) { |
| 795 | 825 | const done = settle( |
| 796 | 826 | row.id, |
| 797 | "failed", | |
| 798 | `the forge kept going away under this check (${row.attempt + 1} times); not trying again`, | |
| 827 | "cancelled", | |
| 828 | `${CUT_LIMIT_SUMMARY} (${row.attempt + 1} long runs); not queued again, ask for a re-check`, | |
| 799 | 829 | ); |
| 800 | 830 | if (done) out.failed.push(done); |
| 801 | 831 | } else { |
| @@ -1127,8 +1157,9 @@ export function checkSummary( | ||
| 1127 | 1157 | >("SELECT * FROM run WHERE kind IN ('deploy','check') AND issue_id = ?1 AND commit_oid = ?2 ORDER BY id DESC LIMIT 1") |
| 1128 | 1158 | .get(issueId, headOid); |
| 1129 | 1159 | const latest = row ? toRun(row) : undefined; |
| 1160 | // A check the forge cut short proved nothing: it is never red (#12594). | |
| 1130 | 1161 | return latest |
| 1131 | ? { state: latest.state, run: latest } | |
| 1162 | ? { state: latest.state === "failed" && isCut(latest) ? "cancelled" : latest.state, run: latest } | |
| 1132 | 1163 | : { state: "none", run: null }; |
| 1133 | 1164 | } |
| 1134 | 1165 |
src/http/checks_recheck.ts +17 −2
| @@ -2,7 +2,7 @@ | ||
| 2 | 2 | * The admin's one call to re-check every open pull red on one named test. |
| 3 | 3 | */ |
| 4 | 4 | |
| 5 | import { RECHECK_MAX, recheckNamed, TEST_NAME_MAX } from "../checks/recheck.ts"; | |
| 5 | import { RECHECK_MAX, recheckNamed, recheckWindow, TEST_NAME_MAX } from "../checks/recheck.ts"; | |
| 6 | 6 | import { canAdmin, getRepo } from "../core/repos.ts"; |
| 7 | 7 | import type { Handler } from "./context.ts"; |
| 8 | 8 | import { apiError, body, json, notFound, requireScope, str } from "./respond.ts"; |
| @@ -14,7 +14,22 @@ export function checksRecheckRoutes(router: Router<Handler>): void { | ||
| 14 | 14 | if (denied) return denied; |
| 15 | 15 | const repo = getRepo(ctx.app.db, ctx.params.owner!, ctx.params.name!); |
| 16 | 16 | if (!repo || !canAdmin(ctx.app.db, repo, ctx.auth.user)) return notFound(); |
| 17 | const test = str((await body(ctx.request)).test); | |
| 17 | const input = await body(ctx.request); | |
| 18 | // `{ since, until? }`: every pull whose last check was cut or failed inside the window. | |
| 19 | if (input.since !== undefined) { | |
| 20 | const since = Date.parse(str(input.since)); | |
| 21 | const until = input.until === undefined ? Date.now() : Date.parse(str(input.until)); | |
| 22 | if (Number.isNaN(since) || Number.isNaN(until) || since > until) | |
| 23 | return apiError("invalid_window", 400, "since (and until) are instants, since not after until"); | |
| 24 | const done = await recheckWindow(ctx.app, ctx.auth.user!, repo, new Date(since).toISOString(), new Date(until).toISOString()); | |
| 25 | return json( | |
| 26 | done.left > 0 | |
| 27 | ? { queued: done.queued, more: done.left, message: `at most ${RECHECK_MAX} per call; ${done.left} left` } | |
| 28 | : { queued: done.queued }, | |
| 29 | 200, | |
| 30 | ); | |
| 31 | } | |
| 32 | const test = str(input.test); | |
| 18 | 33 | if (test.length < 1 || test.length > TEST_NAME_MAX) |
| 19 | 34 | return apiError("invalid_test", 400, `test is a name of 1 to ${TEST_NAME_MAX} characters`); |
| 20 | 35 | const done = await recheckNamed(ctx.app, ctx.auth.user!, repo, test); |
test/checks-recheck.test.ts +23 −1
| @@ -9,7 +9,7 @@ import { join } from "node:path"; | ||
| 9 | 9 | import { createApp, type App } from "../src/app.ts"; |
| 10 | 10 | import { openPull } from "../src/actions/pulls.ts"; |
| 11 | 11 | import { commitToBranch, createRepo } from "../src/actions/repos.ts"; |
| 12 | import { recheckNamed, RECHECK_MAX } from "../src/checks/recheck.ts"; | |
| 12 | import { recheckNamed, recheckWindow, RECHECK_MAX } from "../src/checks/recheck.ts"; | |
| 13 | 13 | import { provideChecksForTest, type ChecksHandle } from "../src/checks/service.ts"; |
| 14 | 14 | import type { Pull } from "../src/core/pulls.ts"; |
| 15 | 15 | import type { Repo } from "../src/core/repos.ts"; |
| @@ -114,3 +114,25 @@ test("past the cap, the rest are left and the answer says how many", async () => | ||
| 114 | 114 | const next = await recheckNamed(app, alice, repo, "capped", 1); |
| 115 | 115 | expect(next).toEqual({ queued: [pulls[1]!.number], left: 0 }); |
| 116 | 116 | }); |
| 117 | ||
| 118 | test("one call re-checks every pull whose last check was cut or failed inside a window", async () => { | |
| 119 | const alice = getUserByName(app.db, "alice")!; | |
| 120 | const cutOn = (pull: Pull): void => { | |
| 121 | const run = createRun(app.db, { repoId: repo.id, kind: "check", env: `pull-${pull.number}`, commitOid: pull.headOid, issueId: pull.id }); | |
| 122 | claimRun(app.db, run.id); | |
| 123 | finishRun(app.db, run.id, { state: "failed", summary: "the forge that was running this went away" }); | |
| 124 | }; | |
| 125 | cutOn(pulls[0]!); | |
| 126 | redOn(pulls[1]!, "windowed"); | |
| 127 | cutOn(pulls[2]!); | |
| 128 | const hour = 3_600_000; | |
| 129 | const before = asked().length; | |
| 130 | const none = await recheckWindow(app, alice, repo, new Date(Date.now() - 2 * hour).toISOString(), new Date(Date.now() - hour).toISOString()); | |
| 131 | expect(none.queued).toEqual([]); | |
| 132 | const res = await call({ since: new Date(Date.now() - hour).toISOString() }); | |
| 133 | expect(res.status).toBe(200); | |
| 134 | expect(((await res.json()) as { queued: number[] }).queued.sort()).toEqual(pulls.map((p) => p.number).sort()); | |
| 135 | expect(asked().length).toBe(before + 3); | |
| 136 | expect((await call({ since: "nonsense" })).status).toBe(400); | |
| 137 | expect((await call({ since: new Date().toISOString() }, {}, bobCookie)).status).toBe(404); | |
| 138 | }); |
test/checks-restart.test.ts +51 −10
| @@ -17,9 +17,11 @@ import { requeueAbandonedChecks, startChecks, sweepSoon } from "../src/checks/se | ||
| 17 | 17 | import { |
| 18 | 18 | cancelRun, |
| 19 | 19 | CHECK_RESTART_LIMIT, |
| 20 | checkSummary, | |
| 20 | 21 | claimRun, |
| 21 | 22 | createRun, |
| 22 | 23 | getRun, |
| 24 | isCut, | |
| 23 | 25 | listRuns, |
| 24 | 26 | REQUEUED_SUMMARY, |
| 25 | 27 | type Run, |
| @@ -54,7 +56,8 @@ describe("checks", () => { | ||
| 54 | 56 | expect(interrupted?.state).toBe("cancelled"); |
| 55 | 57 | expect(interrupted?.summary).toContain("queued again"); |
| 56 | 58 | const again = runs.find((r) => r.commitOid === oid && r.state === "queued"); |
| 57 | expect(again?.attempt).toBe(1); | |
| 59 | // Cut within a minute of starting: a restart, not an attempt used up (#12594). | |
| 60 | expect(again?.attempt).toBe(0); | |
| 58 | 61 | |
| 59 | 62 | // The forge that carries on finds it queued and runs it to the end. |
| 60 | 63 | f.checks = startChecks(f.app, { forgeUrl: f.servers.url, concurrency: 1 }); |
| @@ -217,8 +220,12 @@ describe("a check its forge died under", () => { | ||
| 217 | 220 | }); |
| 218 | 221 | claimRun(app.db, run.id); |
| 219 | 222 | app.db |
| 220 | .query("UPDATE run SET heartbeat_at = ?2 WHERE id = ?1") | |
| 221 | .run(run.id, new Date(Date.now() - STALE_AFTER_MS - 1_000).toISOString()); | |
| 223 | .query("UPDATE run SET heartbeat_at = ?2, started_at = ?3 WHERE id = ?1") | |
| 224 | .run( | |
| 225 | run.id, | |
| 226 | new Date(Date.now() - STALE_AFTER_MS - 1_000).toISOString(), | |
| 227 | new Date(Date.now() - 600_000).toISOString(), | |
| 228 | ); | |
| 222 | 229 | return getRun(app.db, run.id)!; |
| 223 | 230 | }; |
| 224 | 231 | |
| @@ -272,16 +279,50 @@ describe("a check its forge died under", () => { | ||
| 272 | 279 | cancelRun(app.db, run.id); |
| 273 | 280 | }); |
| 274 | 281 | |
| 275 | test("at the ceiling the check fails once, saying the forge kept going away", async () => { | |
| 282 | test("at the ceiling the check is cut, never failed, and the pull is not red", async () => { | |
| 276 | 283 | const run = abandoned(pull.headOid, CHECK_RESTART_LIMIT); |
| 277 | 284 | expect(requeueAbandonedChecks(app, STALE_AFTER_MS)).toBe(1); |
| 278 | const failed = getRun(app.db, run.id)!; | |
| 279 | expect(failed.state).toBe("failed"); | |
| 280 | expect(failed.summary).toContain( | |
| 281 | "the forge kept going away under this check", | |
| 282 | ); | |
| 285 | const cut = getRun(app.db, run.id)!; | |
| 286 | expect(cut.state).toBe("cancelled"); | |
| 287 | expect(cut.summary).toContain("the forge kept going away under this check"); | |
| 288 | expect(isCut(cut)).toBe(true); | |
| 283 | 289 | expect(queuedFor(pull.headOid)).toHaveLength(0); |
| 284 | expect((await word()).word).toBe("stopped"); | |
| 290 | expect(checkSummary(app.db, pull.id, pull.headOid).state).not.toBe("failed"); | |
| 291 | }); | |
| 292 | ||
| 293 | test("a failed run the forge's death wrote is read as cut, not red, and sends no builder", () => { | |
| 294 | // Invariant: a pull is red for its builder only on a check that ran to a verdict. | |
| 295 | const run = abandoned(pull.headOid); | |
| 296 | app.db.query("UPDATE run SET state = 'failed', summary = 'the forge that was running this went away' WHERE id = ?1").run(run.id); | |
| 297 | expect(checkSummary(app.db, pull.id, pull.headOid).state).toBe("cancelled"); | |
| 298 | const real = abandoned(pull.headOid); | |
| 299 | app.db.query("UPDATE run SET state = 'failed', summary = 'check failed: 1 test' WHERE id = ?1").run(real.id); | |
| 300 | expect(checkSummary(app.db, pull.id, pull.headOid).state).toBe("failed"); | |
| 301 | app.db.query("DELETE FROM run WHERE id IN (?1, ?2)").run(run.id, real.id); | |
| 302 | }); | |
| 303 | ||
| 304 | test("three kills in a row leave the check queued, the pull not red, and no builder run", async () => { | |
| 305 | // Invariant: a crash loop's restarts inside a minute never exhaust the retry count. | |
| 306 | const first = abandoned(pull.headOid); | |
| 307 | app.db.query("UPDATE run SET started_at = ?2 WHERE id = ?1").run(first.id, new Date().toISOString()); | |
| 308 | for (let kill = 0; kill < 3; kill++) { | |
| 309 | expect(requeueAbandonedChecks(app, STALE_AFTER_MS)).toBe(1); | |
| 310 | const next = queuedFor(pull.headOid); | |
| 311 | expect(next).toHaveLength(1); | |
| 312 | expect(next[0]!.attempt).toBe(0); | |
| 313 | // The next boot claims it, and dies again at once. | |
| 314 | claimRun(app.db, next[0]!.id); | |
| 315 | app.db | |
| 316 | .query("UPDATE run SET heartbeat_at = ?2 WHERE id = ?1") | |
| 317 | .run(next[0]!.id, new Date(Date.now() - STALE_AFTER_MS - 1_000).toISOString()); | |
| 318 | } | |
| 319 | expect(requeueAbandonedChecks(app, STALE_AFTER_MS)).toBe(1); | |
| 320 | const left = queuedFor(pull.headOid); | |
| 321 | expect(left).toHaveLength(1); | |
| 322 | expect(checkSummary(app.db, pull.id, pull.headOid).state).toBe("queued"); | |
| 323 | expect((await word()).word).toBe("working"); | |
| 324 | expect(listRuns(app.db, repo.id, { kind: "agent", limit: 50 })).toHaveLength(0); | |
| 325 | cancelRun(app.db, left[0]!.id); | |
| 285 | 326 | }); |
| 286 | 327 | |
| 287 | 328 | test("a check for a commit that no longer heads an open pull is cancelled, not queued again", () => { |
new above
offline