Public: anyone can read this; the crew can change it.

A self-swap release waits for the pull checks running, at most 20 minutes #16988

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

release-pull-idle → main 0 commits

  • Merged. release-pull-idle was merged into main as 429ec86.

Review

Sign in to review this change.
3 files changed +190 −7
src/checks/landing.ts +15 −4
@@ -23,7 +23,12 @@ import {
2323 type Verdict,
2424 pruneReleases,
2525 } from "../release/checkout.ts";
26 import { mainCheckRunning, untilMainIdle } from "../release/main_check_idle.ts";
26 import {
27 type IdleClock,
28 mainCheckRunning,
29 untilMainIdle,
30 untilPullChecksDone,
31 } from "../release/main_check_idle.ts";
2732 import { beginSwap, endSwap, handover, startRelease } from "../release/swap.ts";
2833 import { running } from "../kernel/version.ts";
2934 import { noteReleased } from "../crew/note_closed.ts";
@@ -261,9 +266,15 @@ export interface DeployDeps {
261266 */
262267 readonly idle?: (app: App, repo: Repo) => Promise<unknown>;
263268 }
264 const mainIdle: NonNullable<DeployDeps["idle"]> = (app, repo) =>
265 untilMainIdle({ running: () => mainCheckRunning(app.db, repo.id, repo.defaultBranch) });
266 const realDeps: DeployDeps = { wait: waitForCheck, swap: (...a) => swapIn(...a), idle: mainIdle };
269 /**
270 * The default `idle`: main's full check ends, then the pull checks running now (at most 20 min),
271 * so a swap does not throw their work away. `clock` stands in for the timers in a test.
272 */
273 export async function mainIdle(app: App, repo: Repo, clock: IdleClock = {}): Promise<void> {
274 await untilMainIdle({ ...clock, running: () => mainCheckRunning(app.db, repo.id, repo.defaultBranch) });
275 app.log.info("release ready to swap", { pulls: await untilPullChecksDone(app.db, repo.id, clock) });
276 }
277 const realDeps: DeployDeps = { wait: waitForCheck, swap: (...a) => swapIn(...a), idle: (app, repo) => mainIdle(app, repo) };
267278
268279 /**
269280 * Swap in only on a green check of the commit; red, superseded or late is a log line and, when
src/release/main_check_idle.ts +62 −3
@@ -5,12 +5,20 @@
55 * moment between main verdicts: no full check of the default branch is running.
66 *
77 * Bounded: past `maxMs` the release goes ahead, so a check that hangs cannot hold every release.
8 *
9 * The pull checks running at that moment are waited for too, for at most `PULL_CHECK_WAIT_MS`
10 * (`untilPullChecksDone`): a swap cut every one of them (9 at each of three releases on
11 * 2026-12-01), each queued again behind about half an hour of others, so a pushed pull's check did
12 * not finish between releases and the pull was never green to review. Only the checks running
13 * when the wait begins are waited for: a check that starts during it does not extend it.
814 */
915
1016 import type { Database } from "bun:sqlite";
1117 import { during } from "../perf/jobs.ts";
1218
1319 export const MAIN_CHECK_WAIT_MS = 2 * 60 * 60_000;
20 /** The longest a release waits for the pull checks running when it is ready to swap. */
21 export const PULL_CHECK_WAIT_MS = 20 * 60_000;
1422 const POLL_MS = 15_000;
1523
1624 /** Whether the default branch's full check (never a pull's fast tier) is running now. */
@@ -26,14 +34,18 @@ export function mainCheckRunning(db: Database, repoId: string, branch: string):
2634 );
2735 }
2836
29 export interface IdleInput {
30 readonly running: () => boolean;
37 /** The timers a wait reads, so a test can step them. */
38 export interface IdleClock {
3139 readonly sleep?: (ms: number) => Promise<void>;
3240 readonly now?: () => number;
33 readonly maxMs?: number;
3441 readonly pollMs?: number;
3542 }
3643
44 export interface IdleInput extends IdleClock {
45 readonly running: () => boolean;
46 readonly maxMs?: number;
47 }
48
3749 /** Resolves true once no main check runs, false when `maxMs` passed first. */
3850 export async function untilMainIdle(input: IdleInput): Promise<boolean> {
3951 const sleep = input.sleep ?? ((ms: number) => new Promise<void>((done) => { setTimeout(() => during("release-main-idle", done), ms).unref(); }));
@@ -45,3 +57,50 @@ export async function untilMainIdle(input: IdleInput): Promise<boolean> {
4557 }
4658 return true;
4759 }
60
61 /** The pull checks of `repoId` running now, by run id. */
62 export function runningPullChecks(db: Database, repoId: string): string[] {
63 return db
64 .query<{ id: string }, [string]>(
65 `SELECT id FROM run
66 WHERE repo_id = ?1 AND kind = 'check' AND issue_id IS NOT NULL AND state = 'running'`,
67 )
68 .all(repoId)
69 .map((r) => r.id);
70 }
71
72 /** How many of `ids` are still running. */
73 function countRunning(db: Database, ids: readonly string[]): number {
74 if (ids.length === 0) return 0;
75 const marks = ids.map((_, i) => `?${i + 1}`).join(",");
76 return db
77 .query<{ n: number }, string[]>(
78 `SELECT COUNT(*) AS n FROM run WHERE state = 'running' AND id IN (${marks})`,
79 )
80 .get(...ids)!.n;
81 }
82
83 /**
84 * Wait for the pull checks running now to end, at most `maxMs` (`PULL_CHECK_WAIT_MS`). Answers the
85 * release log's line: what it waited for, how long, and what it cut, if anything.
86 */
87 export async function untilPullChecksDone(
88 db: Database,
89 repoId: string,
90 input: IdleClock & { readonly maxMs?: number } = {},
91 ): Promise<string> {
92 const ids = runningPullChecks(db, repoId);
93 if (ids.length === 0) return "no pull check running";
94 const now = input.now ?? Date.now;
95 const from = now();
96 const done = await untilMainIdle({
97 ...input,
98 maxMs: input.maxMs ?? PULL_CHECK_WAIT_MS,
99 running: () => countRunning(db, ids) > 0,
100 });
101 const minutes = Math.round((now() - from) / 60_000);
102 const left = countRunning(db, ids);
103 return done
104 ? `waited ${minutes} min for ${ids.length} running pull check${ids.length === 1 ? "" : "s"} to finish`
105 : `waited ${minutes} min for ${ids.length} running pull checks; ${left} still running are cut and queued again`;
106 }
test/release-waits-for-pull-checks.test.ts added +113
@@ -0,0 +1,113 @@
1 /**
2 * A release waits for the pull checks running when it is ready to swap, at most 20 minutes
3 * (`untilPullChecksDone`, release/main_check_idle.ts). A swap cut every running check: on
4 * 2026-12-01 three releases cut 9 each, so pushed pulls never got a green check between releases
5 * and were never sent to review. A check that starts during the wait does not extend it.
6 */
7 import { expect, test } from "bun:test";
8 import { mkdtempSync } from "node:fs";
9 import { tmpdir } from "node:os";
10 import { join } from "node:path";
11 import { createApp } from "../src/app.ts";
12 import { createRepo } from "../src/actions/repos.ts";
13 import { createIssue } from "../src/core/issues.ts";
14 import { claimRun, createRun, finishRun } from "../src/core/runs.ts";
15 import { PULL_CHECK_WAIT_MS, runningPullChecks, untilPullChecksDone } from "../src/release/main_check_idle.ts";
16 import { seedAdmin } from "./helpers/admin.ts";
17 import { deployWhenChecked, mainIdle } from "../src/checks/landing.ts";
18 import type { Pull } from "../src/core/pulls.ts";
19 import { getRun } from "../src/core/runs.ts";
20
21 async function fixture() {
22 const app = createApp({ dataDir: mkdtempSync(join(tmpdir(), "forge-pull-idle-")), port: 0, selfSwap: true });
23 const u = await seedAdmin(app, { username: "alice", email: "[email protected]", password: "a-good-passphrase" });
24 if (!u.ok) throw new Error("user");
25 const r = await createRepo(app, u.value, { name: "forge-code", visibility: "public" });
26 if (!r.ok) throw new Error("repo");
27 const pull = createIssue(app.db, { repoId: r.value.id, authorId: u.value.id, title: "p", kind: "pull" });
28 if (!pull.ok) throw new Error("pull");
29 const check = (issue = true) => {
30 const run = createRun(app.db, {
31 repoId: r.value.id,
32 kind: "check",
33 env: "pull-1",
34 commitOid: "d".repeat(40),
35 ...(issue ? { issueId: pull.value.id } : {}),
36 });
37 claimRun(app.db, run.id);
38 return run;
39 };
40 return { app, user: u.value, repo: r.value, pull: pull.value, repoId: r.value.id, check };
41 }
42
43 test("nothing running: no wait, and the line says so", async () => {
44 const f = await fixture();
45 // main's own check (no pull) is untilMainIdle's to wait for, not this one's.
46 f.check(false);
47 expect(runningPullChecks(f.app.db, f.repoId)).toEqual([]);
48 expect(
49 await untilPullChecksDone(f.app.db, f.repoId, {
50 sleep: async () => {
51 throw new Error("slept");
52 },
53 }),
54 ).toBe("no pull check running");
55 });
56
57 test("the running pull checks finish first; one that starts during the wait does not extend it", async () => {
58 const f = await fixture();
59 const a = f.check();
60 const b = f.check();
61 let t = 0;
62 let looks = 0;
63 const line = await untilPullChecksDone(f.app.db, f.repoId, {
64 now: () => t,
65 pollMs: 60_000,
66 sleep: async (ms) => {
67 t += ms;
68 looks++;
69 if (looks === 1) finishRun(f.app.db, a.id, { state: "ok", summary: "" });
70 if (looks === 2) f.check(); // started during the wait
71 if (looks === 3) finishRun(f.app.db, b.id, { state: "failed", summary: "red" });
72 },
73 });
74 expect(line).toBe("waited 3 min for 2 running pull checks to finish");
75 expect(runningPullChecks(f.app.db, f.repoId)).toHaveLength(1);
76 });
77
78 test("bounded: after 20 minutes it swaps all the same, and the line says how many it cuts", async () => {
79 const f = await fixture();
80 f.check();
81 f.check();
82 let t = 0;
83 const line = await untilPullChecksDone(f.app.db, f.repoId, { now: () => t, pollMs: 60_000, sleep: async (ms) => void (t += ms) });
84 expect(t).toBe(PULL_CHECK_WAIT_MS);
85 expect(line).toBe("waited 20 min for 2 running pull checks; 2 still running are cut and queued again");
86 });
87
88 test("the release swaps only once the pull check running has ended (the deploy's own idle, mainIdle)", async () => {
89 const f = await fixture();
90 const running = f.check();
91 let t = 0;
92 let looks = 0;
93 const swappedWhile: (string | undefined)[] = [];
94 // The swap never reads the pull here: the check is green, so nothing is said on it.
95 const pull = { id: f.pull.id, number: f.pull.number } as unknown as Pull;
96 await deployWhenChecked(f.app, f.user, f.repo, pull, "e".repeat(40), "/x", {
97 wait: async () => ({ ok: true }),
98 idle: (app, repo) =>
99 mainIdle(app, repo, {
100 now: () => t,
101 pollMs: 60_000,
102 sleep: async (ms) => {
103 t += ms;
104 looks++;
105 if (looks === 2) finishRun(f.app.db, running.id, { state: "ok", summary: "" });
106 },
107 }),
108 swap: async () => void swappedWhile.push(getRun(f.app.db, running.id)?.state),
109 });
110 // Two looks of a minute each, and the swap saw the check ended, never running.
111 expect(looks).toBe(2);
112 expect(swappedWhile).toEqual(["ok"]);
113 });

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

Keyboard shortcuts

Docs