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

opened this in answer to

Closed 18d 3h ago

scout/forge:scout/12594-checks-cut-by-restarts-are-recorded-as-r → main 2 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
8 files changed +163 −24
src/apps/runtime.ts +5 −4
@@ -769,10 +769,11 @@ export class AppsRuntime {
769769 // Deploys are failed; a check the last forge left is queued again while
770770 // its commit still heads an open pull; an agent run is the crew's,
771771 // 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 });
776777 // An awake interval the last forge left open ends at its last heartbeat.
777778 const orphans = this.metered(() => closeOrphans(this.app.db));
778779 if (orphans) log.info("closed open meter intervals", { count: orphans });
src/checks/recheck.ts +29 −1
@@ -7,7 +7,7 @@
77 import type { App } from "../app.ts";
88 import { listOpenPulls, type Pull } from "../core/pulls.ts";
99 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";
1111 import type { User } from "../core/users.ts";
1212 import { askCheck } from "./again.ts";
1313
@@ -53,3 +53,31 @@ export async function recheckNamed(
5353 }
5454 return { queued, left: Math.max(0, red.length - max) };
5555 }
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 {
962962 write(cancelledLine(row) ?? `superseded: ${row.summary}`);
963963 return;
964964 }
965 if (!outcome.ok && stopped && signal.aborted) {
965 if (!outcome.ok && (stopped || signal.aborted)) {
966966 // Cut off by this forge stopping, not by the check: the same check
967967 // goes back on the queue, and the forge that carries on runs it.
968968 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";
33 export const route: Route = {
44 method: "post",
55 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",
77 auth: true,
88 role: "member",
99 status: 200,
1010 reply: { queued: "array" },
1111 refusals: {
12 invalid_window: "since and until are instants, since not after until (400)",
1213 invalid_test: "test is a name of 1 to 300 characters (400)",
1314 },
1415 };
src/core/runs.ts +35 −4
@@ -633,6 +633,27 @@ export const DISK_USED_SUMMARY = "waiting: the forge's disk is 80% used; queued
633633 */
634634 export const CHECK_RESTART_LIMIT = 3;
635635
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
636657 /**
637658 * Cancel a check the forge's own going-away cut off and queue the same
638659 * check again, `attempt + 1` — the one way a check is retried, from a
@@ -668,7 +689,10 @@ export function requeueCheck(
668689 env: cancelled.env,
669690 commitOid: at ?? cancelled.commitOid,
670691 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),
672696 ...(cancelled.issueId === null ? {} : { issueId: cancelled.issueId }),
673697 ...(cancelled.actorId === null ? {} : { actorId: cancelled.actorId }),
674698 });
@@ -676,6 +700,12 @@ export function requeueCheck(
676700 })();
677701 }
678702
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
679709 /**
680710 * Whether the run before this one in its chain (the attempt just below, same
681711 * kind, repository, reason and issue) was killed for its budget: the second
@@ -794,8 +824,8 @@ export function sweepAbandonedChecks(
794824 } else if (row.attempt >= CHECK_RESTART_LIMIT) {
795825 const done = settle(
796826 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`,
799829 );
800830 if (done) out.failed.push(done);
801831 } else {
@@ -1127,8 +1157,9 @@ export function checkSummary(
11271157 >("SELECT * FROM run WHERE kind IN ('deploy','check') AND issue_id = ?1 AND commit_oid = ?2 ORDER BY id DESC LIMIT 1")
11281158 .get(issueId, headOid);
11291159 const latest = row ? toRun(row) : undefined;
1160 // A check the forge cut short proved nothing: it is never red (#12594).
11301161 return latest
1131 ? { state: latest.state, run: latest }
1162 ? { state: latest.state === "failed" && isCut(latest) ? "cancelled" : latest.state, run: latest }
11321163 : { state: "none", run: null };
11331164 }
11341165
src/http/checks_recheck.ts +17 −2
@@ -2,7 +2,7 @@
22 * The admin's one call to re-check every open pull red on one named test.
33 */
44
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";
66 import { canAdmin, getRepo } from "../core/repos.ts";
77 import type { Handler } from "./context.ts";
88 import { apiError, body, json, notFound, requireScope, str } from "./respond.ts";
@@ -14,7 +14,22 @@ export function checksRecheckRoutes(router: Router<Handler>): void {
1414 if (denied) return denied;
1515 const repo = getRepo(ctx.app.db, ctx.params.owner!, ctx.params.name!);
1616 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);
1833 if (test.length < 1 || test.length > TEST_NAME_MAX)
1934 return apiError("invalid_test", 400, `test is a name of 1 to ${TEST_NAME_MAX} characters`);
2035 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";
99 import { createApp, type App } from "../src/app.ts";
1010 import { openPull } from "../src/actions/pulls.ts";
1111 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";
1313 import { provideChecksForTest, type ChecksHandle } from "../src/checks/service.ts";
1414 import type { Pull } from "../src/core/pulls.ts";
1515 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 () =>
114114 const next = await recheckNamed(app, alice, repo, "capped", 1);
115115 expect(next).toEqual({ queued: [pulls[1]!.number], left: 0 });
116116 });
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
1717 import {
1818 cancelRun,
1919 CHECK_RESTART_LIMIT,
20 checkSummary,
2021 claimRun,
2122 createRun,
2223 getRun,
24 isCut,
2325 listRuns,
2426 REQUEUED_SUMMARY,
2527 type Run,
@@ -54,7 +56,8 @@ describe("checks", () => {
5456 expect(interrupted?.state).toBe("cancelled");
5557 expect(interrupted?.summary).toContain("queued again");
5658 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);
5861
5962 // The forge that carries on finds it queued and runs it to the end.
6063 f.checks = startChecks(f.app, { forgeUrl: f.servers.url, concurrency: 1 });
@@ -217,8 +220,12 @@ describe("a check its forge died under", () => {
217220 });
218221 claimRun(app.db, run.id);
219222 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 );
222229 return getRun(app.db, run.id)!;
223230 };
224231
@@ -272,16 +279,50 @@ describe("a check its forge died under", () => {
272279 cancelRun(app.db, run.id);
273280 });
274281
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 () => {
276283 const run = abandoned(pull.headOid, CHECK_RESTART_LIMIT);
277284 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);
283289 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);
285326 });
286327
287328 test("a check for a commit that no longer heads an open pull is cancelled, not queued again", () => {

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs