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

A check that replaces a waiting one keeps its place in line: main's own check… #17240

opened this in answer to

Merged 41h 49m ago, live in production

mason/forge:mason/17229-a-check-that-replaces-a-waiting-one-keep → main 0 commits fork 1 thread · 1 unresolved

  • Merged. mason/17229-a-check-that-replaces-a-waiting-one-keep was merged into main as db83789.

Review

Sign in to review this change.
3 files changed +119 −2
src/checks/request.ts +6
@@ -74,6 +74,9 @@ export async function request(
7474 // and written in one write transaction, so a second forge process over
7575 // this database (a roll) cannot queue the same check beside it.
7676 const superseded: Run[] = [];
77 // The newer commit takes the place of a waiting check it replaces, not a new one at the
78 // back of the line; a check that was running already had its turn (#17229).
79 let place: string | undefined;
7780 const { run, fresh } = transact(app.db, () => {
7881 for (const active of listActive(app.db, "check")) {
7982 if (active.repoId !== repo.id || active.env !== env) continue;
@@ -93,6 +96,8 @@ export async function request(
9396 `superseded by ${commitOid.slice(0, 7)}`,
9497 );
9598 if (gone && active.state === "running") superseded.push(gone);
99 if (gone && active.state === "queued" && (place === undefined || active.createdAt < place))

Correct: only a queued run hands on its place, and the earliest one wins. This line is over 100 columns, so run bun scripts/gate.ts --fix and commit the rewrap.

100 place = active.createdAt;
96101 }
97102 }
98103 const created = createRun(app.db, {
@@ -104,6 +109,7 @@ export async function request(
104109 ...(issueId === undefined ? {} : { issueId }),
105110 ...(actorId === undefined ? {} : { actorId }),
106111 ...(notBefore === undefined ? {} : { notBefore }),
112 createdAt: place,
107113 });
108114 return { run: created, fresh: true };
109115 });
src/core/runs.ts +6 −2
@@ -257,14 +257,17 @@ export interface CreateRunInput {
257257 variantGroup?: string | undefined;
258258 /** A person sent it: the week's cap does not hold it. */
259259 sent?: boolean | undefined;
260 /** Its place in line: a check that replaces a waiting one takes that one's (#17229). */
261 createdAt?: string | undefined;
260262 }
261263
262264 export function createRun(db: Database, input: CreateRunInput): Run {
263265 const runId = id();
264266 db.query(
265267 `INSERT INTO run (id, repo_id, kind, issue_id, env, commit_oid, branch, actor_id, reason,
266 not_before, attempt, summary, variant_group, interruptions, sent)
267 VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?14, ?15)`,
268 not_before, attempt, summary, variant_group, interruptions, sent, created_at)
269 VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?14, ?15,
270 COALESCE(?16, strftime('%Y-%m-%dT%H:%M:%fZ','now')))`,
268271 ).run(
269272 runId,
270273 input.repoId,
@@ -281,6 +284,7 @@ export function createRun(db: Database, input: CreateRunInput): Run {
281284 input.variantGroup ?? null,
282285 input.interruptions ?? 0,
283286 input.sent === true ? 1 : 0,
287 input.createdAt ?? null,
284288 );
285289 return getRun(db, runId)!;
286290 }
test/check-supersede-keeps-place.test.ts added +107
@@ -0,0 +1,107 @@
1 /**
2 * A check that replaces a waiting one keeps its place in line (#17229): a landing every few
3 * minutes must not send main's check to the back each time, where it is superseded unstarted.
4 */
5 import { afterAll, beforeAll, expect, test } from "bun:test";
6 import { mkdtemp, rm } from "node:fs/promises";
7 import { tmpdir } from "node:os";
8 import { join } from "node:path";
9 import { createApp, type App } from "../src/app.ts";
10 import { openPull } from "../src/actions/pulls.ts";
11 import { commitToBranch, createRepo } from "../src/actions/repos.ts";
12 import { request } from "../src/checks/request.ts";
13 import { pickOrder } from "../src/checks/pick.ts";
14 import { STARVATION_MS } from "../src/kernel/budgets.ts";
15 import type { LaneState } from "../src/checks/lane_state.ts";
16 import { claimRun, createRun, getRun } from "../src/core/runs.ts";
17 import type { Repo } from "../src/core/repos.ts";
18 import { createUser, type User } from "../src/core/users.ts";
19
20 let dataDir: string;
21 let app: App;
22 let alice: User;
23 let repo: Repo;
24 let lane: LaneState;
25
26 const forgeJson = `{"check": {"run": "true", "install": ""}}\n`;
27
28 async function commit(branch: string, content: string): Promise<string> {
29 const made = await commitToBranch(app, alice, repo, {
30 branch,
31 message: `Write ${content}`,
32 files: [{ path: "forge.json", content: forgeJson }, { path: "f.txt", content }],
33 });
34 if (!made.ok) throw new Error(JSON.stringify(made.error));
35 return made.value.commitOid;
36 }
37
38 const back = (id: string, ms: number): void => {
39 app.db
40 .query("UPDATE run SET created_at = ?2 WHERE id = ?1")
41 .run(id, new Date(Date.now() - ms).toISOString());
42 };
43
44 beforeAll(async () => {
45 dataDir = await mkdtemp(join(tmpdir(), "forge-keeps-place-"));
46 app = createApp({ dataDir, port: 0 });
47 const u = await createUser(app.db, {
48 origin: "seed", trusted: true, username: "alice", email: "[email protected]",
49 password: "a-good-passphrase", adminIfFirst: true,
50 });
51 if (!u.ok) throw new Error(u.error);
52 alice = u.value;
53 const made = await createRepo(app, alice, { name: "lib", visibility: "public" });
54 if (!made.ok) throw new Error(String(made.error));
55 repo = made.value;
56 lane = {
57 app,
58 stopped: false,
59 inFlight: new Map(),
60 schedule: () => {},
61 } as unknown as LaneState;
62 });
63
64 afterAll(async () => {
65 app.close();
66 await rm(dataDir, { recursive: true, force: true });
67 });
68
69 test("main's check replacing a waiting one takes its createdAt, and the old one is superseded", async () => {
70 const a = await commit("main", "a");
71 const r1 = (await request(lane, repo, "main", a, "push"))!;
72 back(r1.id, 10 * 60_000);
73 const old = getRun(app.db, r1.id)!;
74 const b = await commit("main", "b");
75 const r2 = (await request(lane, repo, "main", b, "push"))!;
76 expect(getRun(app.db, r1.id)).toMatchObject({ state: "cancelled", summary: `superseded by ${b.slice(0, 7)}` });
77 expect(r2.state).toBe("queued");
78 expect(r2.createdAt).toBe(old.createdAt);
79 expect(Date.now() - Date.parse(r2.createdAt)).toBeGreaterThan(9 * 60_000);
80 });
81
82 test("a running check's stop passes its place on to nobody", async () => {
83 const c1 = await commit("topic", "c1");
84 const opened = await openPull(app, alice, repo, { title: "topic", baseBranch: "main", headBranch: "topic" });
85 if (!opened.ok) throw new Error(String(opened.error));
86 const env = `pull-${opened.value.number}`;
87 const p1 = (await request(lane, repo, env, c1, "push", opened.value.id))!;
88 claimRun(app.db, p1.id);
89 back(p1.id, 10 * 60_000);
90 const c2 = await commit("topic", "c2");
91 const p2 = (await request(lane, repo, env, c2, "push", opened.value.id))!;
92 expect(getRun(app.db, p1.id)!.state).toBe("cancelled");
93 expect(p2.createdAt > getRun(app.db, p1.id)!.createdAt).toBe(true);
94 expect(Date.now() - Date.parse(p2.createdAt)).toBeLessThan(60_000);
95 });
96
97 test("pickOrder: the replacing check goes before a pull check queued after the one it replaced", async () => {
98 const base = Date.now() - 60 * 60_000;
99 const at = (n: number) => new Date(base + n * 60_000).toISOString();
100 const p1 = createRun(app.db, { repoId: repo.id, kind: "check", env: "pull-90", createdAt: at(0) });
101 const first = createRun(app.db, { repoId: repo.id, kind: "check", env: "main", createdAt: at(1) });
102 const p2 = createRun(app.db, { repoId: repo.id, kind: "check", env: "pull-91", createdAt: at(2) });
103 const r2 = createRun(app.db, { repoId: repo.id, kind: "check", env: "main", createdAt: first.createdAt });
104 const order = pickOrder([p1, r2, p2], Date.now(), undefined, undefined, undefined, undefined);
105 expect(Date.now() - Date.parse(p1.createdAt)).toBeGreaterThan(STARVATION_MS);
106 expect(order.map((r) => r.id)).toEqual([p1.id, r2.id, p2.id]);
107 });

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

Keyboard shortcuts

Docs