Public: anyone can read this; the crew can change it.
Main's own check starts in a slot of its own: no main check had started since 21:08Z #17287
Merged 39h 38m ago, live in production
main-first → main 0 commits 3 threads · 3 unresolved
8 files
changed +320
−30
src/checks/lane_state.ts +16 −3
| @@ -207,6 +207,11 @@ export interface LaneState { | ||
| 207 | 207 | readonly grantWaiting: Set<string>; |
| 208 | 208 | /** The verification-lane run in flight, which holds no check slot. */ |
| 209 | 209 | verifying: string | undefined; |
| 210 | /** | |
| 211 | * The default-branch check in its own slot, by repository id (at most one, forge-wide): it holds | |
| 212 | * no check slot (`scheduler.ts` `startMain`), so runner jobs filling those never starve main. | |
| 213 | */ | |
| 214 | readonly mains: Map<string, string>; | |
| 210 | 215 | /** How long main's check after a land waits before it may start. */ |
| 211 | 216 | readonly settle: number; |
| 212 | 217 | stopped: boolean; |
| @@ -229,9 +234,17 @@ export interface LaneState { | ||
| 229 | 234 | ): Promise<Run | undefined>; |
| 230 | 235 | } |
| 231 | 236 | |
| 232 | /** Check slots in use: what runs, less the verification lane's, which holds none. */ | |
| 233 | export const slotted = (lane: LaneState): number => | |
| 234 | lane.inFlight.size - (lane.verifying === undefined ? 0 : 1); | |
| 237 | /** Every check in flight but the verification lane's: what the memory hold weighs. */ | |
| 238 | export const running = (lane: LaneState): number => lane.inFlight.size - (lane.verifying === undefined ? 0 : 1); | |
| 239 | ||
| 240 | /** | |
| 241 | * Check slots in use: what runs, less the verification lane's and the main check in its own slot, | |
| 242 | * which hold slots of their own. | |
| 243 | */ | |
| 244 | export const slotted = (lane: LaneState): number => { | |
| 245 | const inMainSlot = [...lane.mains.values()].filter((id) => lane.inFlight.has(id)).length; | |
| 246 | return running(lane) - inMainSlot; | |
| 247 | }; | |
| 235 | 248 | |
| 236 | 249 | /** The most checks at once however many runners are live: the setting's own ceiling. */ |
| 237 | 250 | export const CHECKS_CEILING = ((kind) => (kind.type === "count" ? kind.max : 16))(CAPACITY["checks.concurrency"].kind); |
src/checks/scheduler.ts +62 −2
| @@ -16,12 +16,65 @@ import { usedRefusal } from "../apps/disk_guard.ts"; | ||
| 16 | 16 | import { rankOnce } from "./ranked_first.ts"; |
| 17 | 17 | import { gateQueued, gatesInFlight } from "./queue.ts"; |
| 18 | 18 | import type { LaneState } from "./lane_state.ts"; |
| 19 | import { slotted, capacity } from "./lane_state.ts"; | |
| 19 | import { running, slotted, capacity } from "./lane_state.ts"; | |
| 20 | 20 | import { pickNext, holdsLastSlot } from "./pick.ts"; |
| 21 | 21 | import { start } from "./start.ts"; |
| 22 | 22 | import { startVerify } from "./verify_lane.ts"; |
| 23 | 23 | import { grantHold, holdLine, type SlotView } from "./check_slots.ts"; |
| 24 | 24 | |
| 25 | /** | |
| 26 | * Whether the run is the check of its repository's default branch (no pull, on that branch), asked | |
| 27 | * once per repository. | |
| 28 | */ | |
| 29 | function mainOnce(db: LaneState["app"]["db"]): (run: Pick<Run, "issueId" | "repoId" | "env">) => boolean { | |
| 30 | const branches = new Map<string, string | undefined>(); | |
| 31 | return (run) => { | |
| 32 | if (run.issueId !== null) return false; | |
| 33 | if (!branches.has(run.repoId)) branches.set(run.repoId, getRepoById(db, run.repoId)?.defaultBranch); | |
| 34 | return run.env === branches.get(run.repoId); | |
| 35 | }; | |
| 36 | } | |
| 37 | ||
| 38 | /** | |
| 39 | * One default-branch check at a time starts in a slot of its own, beside the check slots as the | |
| 40 | * verification lane's run is (#7786), so pull checks holding every slot never keep main waiting. On | |
| 41 | * 2026-12-01 none of trevato/forge's started from 21:08Z on: runner jobs held all the slots, and | |
| 42 | * when one freed, the newest approved or own pull, or the oldest pull past the starvation rule, | |
| 43 | * took it, since every landing queues main's check afresh; about 35 were superseded unstarted. | |
| 44 | * Without it nothing says main is red, and nothing keeps the impact map or the build cache every | |
| 45 | * pull's check is seeded from (`checks/cache.ts`). | |
| 46 | * | |
| 47 | * One for the whole forge, not one per repository: a forge of many repositories landing at once | |
| 48 | * would otherwise run as many beside `checks.concurrency`. The longest-waiting repository's newest | |
| 49 | * main check takes it, once its land settle has passed. That repository's next main check waits | |
| 50 | * behind it, where a newer commit supersedes it; another repository's queues for a check slot as | |
| 51 | * before. The disk and memory holds and the account limit still hold it (`startable`); red | |
| 52 | * main and the grants never do. | |
| 53 | */ | |
| 54 | function startMain(lane: LaneState, isMain: (run: Run) => boolean): void { | |
| 55 | const { app } = lane; | |
| 56 | for (const [repoId, runId] of lane.mains) if (!lane.inFlight.has(runId)) lane.mains.delete(repoId); | |
| 57 | if (lane.mains.size > 0) return; | |
| 58 | // Each repository's newest main check, and when its oldest one queued. | |
| 59 | const newest = new Map<string, { run: Run; since: string }>(); | |
| 60 | for (const run of listActive(app.db, "check")) { | |
| 61 | if (run.state !== "queued" || lane.inFlight.has(run.id) || inVerifyLane(run.env) || !isMain(run)) continue; | |
| 62 | if (run.notBefore !== null && Date.parse(run.notBefore) > Date.now()) continue; | |
| 63 | const held = newest.get(run.repoId); | |
| 64 | const later = held === undefined || run.createdAt > held.run.createdAt || (run.createdAt === held.run.createdAt && run.id > held.run.id); | |
| 65 | newest.set(run.repoId, { | |
| 66 | run: later ? run : held!.run, | |
| 67 | since: held === undefined || run.createdAt < held.since ? run.createdAt : held.since, | |
| 68 | }); | |
| 69 | } | |
| 70 | const next = [...newest.values()].sort((a, b) => (a.since < b.since ? -1 : a.since > b.since ? 1 : 0))[0]?.run; | |
| 71 | if (next === undefined || startable(lane, [next]) === undefined) return; | |
| 72 | const repo = getRepoById(app.db, next.repoId); | |
| 73 | if (repo === undefined) return; | |
| 74 | lane.mains.set(next.repoId, next.id); | |
| 75 | start(lane, next, repo); | |
| 76 | } | |
| 77 | ||
| 25 | 78 | /** |
| 26 | 79 | * An account at its concurrent-checks limit (`settings/quotas.ts`) keeps |
| 27 | 80 | * the check queued, the count in its log once, until one of its own ends. |
| @@ -102,7 +155,8 @@ function startable(lane: LaneState, queued: readonly Run[], grants = false): Run | ||
| 102 | 155 | return undefined; |
| 103 | 156 | } |
| 104 | 157 | // A child near its memory limit runs one check at a time (`runs/memory_hold.ts`). |
| 105 | const tight = memoryHold(slotted(lane), options.pressure); | |
| 158 | // Every check in flight counts, main's own slot too: the machine holds them all. | |
| 159 | const tight = memoryHold(running(lane), options.pressure); | |
| 106 | 160 | if (tight !== undefined) { |
| 107 | 161 | for (const run of queued) |
| 108 | 162 | if (!lane.memoryWaiting.has(run.id)) { |
| @@ -158,6 +212,9 @@ export function schedule(lane: LaneState): void { | ||
| 158 | 212 | if (!crewHolds) lane.pullHold.reset(); |
| 159 | 213 | else lane.pullHold.keep(new Set(listActive(app.db, "check").filter((run) => run.state === "queued").map((run) => run.id))); |
| 160 | 214 | startVerify(lane); |
| 215 | // Main's own check, in its own slot: never one the pulls compete for. | |
| 216 | const isMain = mainOnce(app.db); | |
| 217 | startMain(lane, isMain); | |
| 161 | 218 | // Every merge-queue gate in flight holds a slot, a line's first too (#6376). |
| 162 | 219 | while (slotted(lane) < limit - gatesInFlight()) { |
| 163 | 220 | // A pull's check waits while its main is red (`checks/red_main.ts`). |
| @@ -167,6 +224,9 @@ export function schedule(lane: LaneState): void { | ||
| 167 | 224 | run.state === "queued" && |
| 168 | 225 | !lane.inFlight.has(run.id) && |
| 169 | 226 | !inVerifyLane(run.env) && |
| 227 | // A repository's next main check waits behind the one in main's slot, where a newer | |
| 228 | // commit supersedes it; another repository's queues for a check slot. | |
| 229 | !(isMain(run) && lane.mains.has(run.repoId)) && | |
| 170 | 230 | (run.notBefore === null || Date.parse(run.notBefore) <= Date.now()), |
| 171 | 231 | ), |
| 172 | 232 | (repoId) => redMain(app.db, repoId), |
src/checks/service.ts +1
src/checks/start.ts +4 −1
| @@ -28,7 +28,10 @@ export function start(lane: LaneState, run: Run, repo: Repo, adopted = false): v | ||
| 28 | 28 | // A person's cancel (`cancelRepoRun`) reaches it through this. |
| 29 | 29 | trackRun(run.id, abort); |
| 30 | 30 | workBegins(app.db, run.id); |
| 31 | const promise = execute(lane, run, repo, abort.signal, !low, adopted) | |
| 31 | // The verification lane's run and a main check in its own slot (`startMain`) take no slot of | |
| 32 | // the gate. | |
| 33 | const ownSlot = low || lane.mains.get(run.repoId) === run.id; | |
| 34 | const promise = execute(lane, run, repo, abort.signal, !ownSlot, adopted) | |
| 32 | 35 | .catch((error) => |
| 33 | 36 | log.error("check failed to execute", { |
| 34 | 37 | run: run.id, |
test/capacity.test.ts +25 −18
| @@ -17,6 +17,7 @@ import { setSsh } from "../src/settings/ssh.ts"; | ||
| 17 | 17 | import { changeCapacity, listCapacity } from "../src/actions/capacity.ts"; |
| 18 | 18 | import { addToCrew, createAgent } from "../src/actions/crew.ts"; |
| 19 | 19 | import { assignIssue, openIssue } from "../src/actions/issues.ts"; |
| 20 | import { openPull } from "../src/actions/pulls.ts"; | |
| 20 | 21 | import { commitToBranch, createRepo } from "../src/actions/repos.ts"; |
| 21 | 22 | import { enableApp } from "../src/actions/apps.ts"; |
| 22 | 23 | import { meterEgress, NPM_REGISTRY, recordEgress, resetEgressTotals, startEgress } from "../src/apps/egress.ts"; |
| @@ -650,40 +651,44 @@ describe("the next check honours a concurrency the admin lowered, and gets the s | ||
| 650 | 651 | await rm(dataDir, { recursive: true, force: true }); |
| 651 | 652 | }); |
| 652 | 653 | |
| 653 | test("lowered to one, three repositories' checks run one at a time; each sees FORGE_CHECK_SHARDS", async () => { | |
| 654 | test("lowered to one, pull checks run one at a time beside main's own slot, the peak exactly two; each sees FORGE_CHECK_SHARDS", async () => { | |
| 654 | 655 | // Invariant: a checks.concurrency lowered below the boot's three admits one check at a time, |
| 655 | // and checks.shards reaches each check's env. | |
| 656 | // and checks.shards reaches each check's env. One default-branch check runs in a slot of its | |
| 657 | // own beside it (`scheduler.ts` startMain, #17287): one extra, never more. | |
| 656 | 658 | expect(changeCapacity(app, alice, "checks.concurrency", "1").ok).toBe(true); |
| 657 | 659 | expect(changeCapacity(app, alice, "checks.shards", "2").ok).toBe(true); |
| 658 | const repos: Repo[] = []; | |
| 659 | for (const name of ["one", "two", "three"]) { | |
| 660 | const made = await createRepo(app, alice, { name, visibility: "public" }); | |
| 661 | if (!made.ok) throw new Error(String(made.error)); | |
| 662 | repos.push(made.value); | |
| 663 | } | |
| 660 | const made = await createRepo(app, alice, { name: "one", visibility: "public" }); | |
| 661 | if (!made.ok) throw new Error(String(made.error)); | |
| 662 | const repo = made.value; | |
| 664 | 663 | // Each check holds until the test writes `release` into its home, the one |
| 665 | 664 | // place a contained check and the test both see: a check ends when the |
| 666 | 665 | // test says, so a second one let in beside it is still running when sampled. |
| 666 | // A pull's check runs its base's command, so main's forge.json holds all three. | |
| 667 | 667 | const hold = 'echo "shards=$FORGE_CHECK_SHARDS"; while [ ! -e "${FORGE_CHECK_SCRATCH:-$PWD.home}/release" ]; do sleep 0.01; done'; |
| 668 | await Promise.all( | |
| 669 | repos.map((repo) => | |
| 670 | commitToBranch(app, alice, repo, { | |
| 671 | message: "A held check", | |
| 672 | files: [{ path: "forge.json", content: JSON.stringify({ check: { run: hold } }) }], | |
| 673 | }), | |
| 674 | ), | |
| 675 | ); | |
| 668 | const base = await commitToBranch(app, alice, repo, { | |
| 669 | message: "A held check", | |
| 670 | files: [{ path: "forge.json", content: JSON.stringify({ check: { run: hold } }) }], | |
| 671 | }); | |
| 672 | if (!base.ok) throw new Error(JSON.stringify(base.error)); | |
| 673 | for (const name of ["p1", "p2"]) { | |
| 674 | const branch = await commitToBranch(app, alice, repo, { branch: name, message: name, files: [{ path: `${name}.txt`, content: `${name}\n` }] }); | |
| 675 | if (!branch.ok) throw new Error(JSON.stringify(branch.error)); | |
| 676 | const pull = await openPull(app, alice, repo, { title: name, body: "", baseBranch: "main", headBranch: name }); | |
| 677 | if (!pull.ok) throw new Error(JSON.stringify(pull.error)); | |
| 678 | } | |
| 676 | 679 | // A running check is released once it has been seen on three samples: |
| 677 | 680 | // time for the gate to start a second beside it, were the lowered limit |
| 678 | 681 | // not honoured. |
| 679 | 682 | let most = 0; |
| 683 | let mostPulls = 0; | |
| 680 | 684 | const seenRunning = new Map<string, number>(); |
| 681 | 685 | const deadline = Date.now() + 60_000; |
| 682 | 686 | let done: Run[] = []; |
| 683 | 687 | while (Date.now() < deadline) { |
| 684 | const runs = repos.flatMap((r) => listRuns(app.db, r.id, { kind: "check", limit: 10 })); | |
| 688 | const runs = listRuns(app.db, repo.id, { kind: "check", limit: 10 }); | |
| 685 | 689 | const running = runs.filter((r) => r.state === "running"); |
| 686 | 690 | most = Math.max(most, running.length); |
| 691 | mostPulls = Math.max(mostPulls, running.filter((r) => r.issueId !== null).length); | |
| 687 | 692 | done = runs.filter((r) => r.state === "ok" || r.state === "failed"); |
| 688 | 693 | if (done.length === 3) break; |
| 689 | 694 | for (const run of running) { |
| @@ -698,7 +703,9 @@ describe("the next check honours a concurrency the admin lowered, and gets the s | ||
| 698 | 703 | await Bun.sleep(20); |
| 699 | 704 | } |
| 700 | 705 | expect(done.map((r) => r.state)).toEqual(["ok", "ok", "ok"]); |
| 701 | expect(most).toBe(1); | |
| 706 | // The lowered slot holds one pull's check at a time; main's own slot holds main's beside it. | |
| 707 | expect(mostPulls).toBe(1); | |
| 708 | expect(most).toBe(2); | |
| 702 | 709 | // The scheduler resized the gate itself (not only the admission reads): booted at three, it now |
| 703 | 710 | // holds one. |
| 704 | 711 | expect(checks.lane.limit).toBe(1); |
test/combined-check.test.ts +7 −1
| @@ -76,11 +76,17 @@ describe("a kept fork's pull is checked on its combined commit (#9932)", () => { | ||
| 76 | 76 | // proven by it, so the log read is of every check run at this head. |
| 77 | 77 | const pull = getPull(app.db, getRepo(app.db, "alice", "app")!.id, number)!; |
| 78 | 78 | const all = checksForHead(app.db, pull.id, now.commitOid); |
| 79 | const logs = all.map((r) => readRunLog(dataDir, r.id).lines.join("\n")); | |
| 80 | // A check proven green by an earlier run of the same tree (a combined commit rebuilt with | |
| 81 | // the same content, at another oid) did not run its gate: that run's log is the one that | |
| 82 | // did. | |
| 83 | const provers = [...new Set(logs.flatMap((l) => [...l.matchAll(/already proven green by (\w+)/g)].map((m) => m[1]!)))]; | |
| 84 | for (const id of provers) logs.push(readRunLog(dataDir, id).lines.join("\n")); | |
| 79 | 85 | return { |
| 80 | 86 | id: run.id, |
| 81 | 87 | state: now.state, |
| 82 | 88 | baseOid: all.findLast((r) => r.baseOid !== "")?.baseOid ?? "", |
| 83 | log: all.map((r) => readRunLog(dataDir, r.id).lines.join("\n")).join("\n"), | |
| 89 | log: logs.join("\n"), | |
| 84 | 90 | }; |
| 85 | 91 | } |
| 86 | 92 | await Bun.sleep(50); |
test/main-slot.test.ts added +192
| @@ -0,0 +1,192 @@ | ||
| 1 | /** | |
| 2 | * Main's own check starts in a slot of its own (`scheduler.ts` `startMain`): pull checks held by | |
| 3 | * runners filling every check slot never keep it waiting. On 2026-12-01 none of trevato/forge's | |
| 4 | * main checks started from 21:08Z on; each was superseded by the next landing, about 35 in a row, | |
| 5 | * so nothing said main was red and nothing kept the build cache. | |
| 6 | */ | |
| 7 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 8 | import { writeFileSync } from "node:fs"; | |
| 9 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 10 | import { tmpdir } from "node:os"; | |
| 11 | import { join } from "node:path"; | |
| 12 | import { openPull } from "../src/actions/pulls.ts"; | |
| 13 | import { commitToBranch, createRepo } from "../src/actions/repos.ts"; | |
| 14 | import { createApp, type App } from "../src/app.ts"; | |
| 15 | import type { runRunnerStep } from "../src/checks/runner_step.ts"; | |
| 16 | import { startChecks } from "../src/checks/service.ts"; | |
| 17 | import { listRuns } from "../src/core/runs.ts"; | |
| 18 | import { createUser, type User } from "../src/core/users.ts"; | |
| 19 | import { ok } from "../src/kernel/result.ts"; | |
| 20 | import { quietLogs } from "./helpers/globals.ts"; | |
| 21 | import { waitFor } from "./helpers/poll.ts"; | |
| 22 | ||
| 23 | quietLogs(); | |
| 24 | ||
| 25 | const SLOTS = 8; | |
| 26 | const priorCells = process.env["FORGE_CELLS"]; // env-restored: afterAll | |
| 27 | let dir = ""; | |
| 28 | let app: App; | |
| 29 | let alice: User; | |
| 30 | beforeAll(async () => { | |
| 31 | process.env["FORGE_CELLS"] = "off"; // env-restored: afterAll | |
| 32 | dir = await mkdtemp(join(tmpdir(), "main-slot-")); | |
| 33 | app = createApp({ dataDir: dir, port: 0, host: "forge.test" }); | |
| 34 | const u = await createUser(app.db, { | |
| 35 | origin: "seed", | |
| 36 | trusted: true, | |
| 37 | username: "alice", | |
| 38 | email: "[email protected]", | |
| 39 | password: "a-good-passphrase", | |
| 40 | adminIfFirst: true, | |
| 41 | }); | |
| 42 | if (!u.ok) throw new Error(u.error); | |
| 43 | alice = u.value; | |
| 44 | }); | |
| 45 | afterAll(async () => { | |
| 46 | if (priorCells === undefined) delete process.env["FORGE_CELLS"]; | |
| 47 | else process.env["FORGE_CELLS"] = priorCells; | |
| 48 | app.close(); | |
| 49 | await rm(dir, { recursive: true, force: true }); | |
| 50 | }); | |
| 51 | ||
| 52 | test(`with ${SLOTS} runner jobs holding every slot and a pull's check queued behind them, main's check still starts and passes`, async () => { | |
| 53 | const made = await createRepo(app, alice, { name: "busy", visibility: "public" }); | |
| 54 | if (!made.ok) throw new Error("repo"); | |
| 55 | const repo = made.value; | |
| 56 | const base = await commitToBranch(app, alice, repo, { | |
| 57 | message: "base", | |
| 58 | files: [{ path: "forge.json", content: JSON.stringify({ check: { run: "true", install: "" } }) }], | |
| 59 | }); | |
| 60 | if (!base.ok) throw new Error(JSON.stringify(base.error)); | |
| 61 | for (let i = 1; i <= SLOTS + 1; i++) { | |
| 62 | const topic = await commitToBranch(app, alice, repo, { | |
| 63 | branch: `topic-${i}`, | |
| 64 | message: `topic ${i}`, | |
| 65 | files: [{ path: `t${i}.txt`, content: `${i}\n` }], | |
| 66 | }); | |
| 67 | if (!topic.ok) throw new Error(JSON.stringify(topic.error)); | |
| 68 | } | |
| 69 | // A runner (stood in) takes each pull's check and holds it until released. | |
| 70 | const held: (() => void)[] = []; | |
| 71 | let released = false; | |
| 72 | const runnerStep: typeof runRunnerStep = async (_app, input) => { | |
| 73 | if (!released) await new Promise<void>((resolve) => held.push(resolve)); | |
| 74 | input.output.push("passed"); | |
| 75 | return { taken: true, ran: ok(undefined), runner: "local-1", contained: true, note: "" }; | |
| 76 | }; | |
| 77 | const checks = startChecks(app, { | |
| 78 | forgeUrl: "http://forge.test", | |
| 79 | concurrency: SLOTS, | |
| 80 | placement: { mode: "runner", waitMs: 300 }, | |
| 81 | runnerStep, | |
| 82 | }); | |
| 83 | const mainChecks = () => | |
| 84 | listRuns(app.db, repo.id, { kind: "check", limit: 100 }).filter((r) => r.issueId === null && r.env === repo.defaultBranch); | |
| 85 | const pullChecks = () => listRuns(app.db, repo.id, { kind: "check", limit: 100 }).filter((r) => r.issueId !== null); | |
| 86 | try { | |
| 87 | for (let i = 1; i <= SLOTS; i++) { | |
| 88 | const pull = await openPull(app, alice, repo, { title: `t${i}`, body: "", baseBranch: "main", headBranch: `topic-${i}` }); | |
| 89 | if (!pull.ok) throw new Error(JSON.stringify(pull.error)); | |
| 90 | } | |
| 91 | expect(await waitFor(() => held.length === SLOTS, { within: 40_000 })).toBe(true); | |
| 92 | // Every slot is a runner's: a ninth pull's check waits. | |
| 93 | const ninth = await openPull(app, alice, repo, { title: "t9", body: "", baseBranch: "main", headBranch: `topic-${SLOTS + 1}` }); | |
| 94 | if (!ninth.ok) throw new Error(JSON.stringify(ninth.error)); | |
| 95 | expect(await waitFor(() => pullChecks().some((r) => r.issueId === ninth.value.id), { within: 10_000 })).toBe(true); | |
| 96 | ||
| 97 | // A landing on main: its check starts beside the full slots and passes, the runners still busy. | |
| 98 | const landed = await commitToBranch(app, alice, repo, { message: "land", files: [{ path: "landed.txt", content: "yes\n" }] }); | |
| 99 | if (!landed.ok) throw new Error(JSON.stringify(landed.error)); | |
| 100 | const sha = landed.value.commitOid; | |
| 101 | expect(await waitFor(() => mainChecks().some((r) => r.commitOid === sha && r.state === "ok"), { within: 40_000 })).toBe(true); | |
| 102 | expect(held.length).toBe(SLOTS); | |
| 103 | expect(pullChecks().find((r) => r.issueId === ninth.value.id)?.state).toBe("queued"); | |
| 104 | } finally { | |
| 105 | released = true; | |
| 106 | for (const release of held.splice(0)) release(); | |
| 107 | await checks.stop(); | |
| 108 | } | |
| 109 | }, 120_000); | |
| 110 | ||
| 111 | /** A repository whose main check holds until the test writes `go` into the data directory as `name`. */ | |
| 112 | async function heldRepo(name: string): Promise<{ repo: Awaited<ReturnType<typeof makeRepo>>; release: () => void }> { | |
| 113 | const repo = await makeRepo(name); | |
| 114 | const gate = join(dir, `go-${name}`); | |
| 115 | const made = await commitToBranch(app, alice, repo, { | |
| 116 | message: "A held check", | |
| 117 | files: [{ path: "forge.json", content: JSON.stringify({ check: { run: `while [ ! -f '${gate}' ]; do sleep 0.01; done`, install: "" } }) }], | |
| 118 | }); | |
| 119 | if (!made.ok) throw new Error(JSON.stringify(made.error)); | |
| 120 | return { repo, release: () => writeFileSync(gate, "") }; | |
| 121 | } | |
| 122 | ||
| 123 | async function makeRepo(name: string) { | |
| 124 | const made = await createRepo(app, alice, { name, visibility: "public" }); | |
| 125 | if (!made.ok) throw new Error(String(made.error)); | |
| 126 | return made.value; | |
| 127 | } | |
| 128 | ||
| 129 | const mainChecksOf = (repoId: string, defaultBranch: string) => | |
| 130 | listRuns(app.db, repoId, { kind: "check", limit: 50 }).filter((r) => r.issueId === null && r.env === defaultBranch); | |
| 131 | ||
| 132 | test("one main slot for the whole forge: with the check slot full, a second repository's main check waits until the first one's ends", async () => { | |
| 133 | // The one check slot, held by a pull's check on a stood-in runner. | |
| 134 | const pullRepo = await makeRepo("pulls"); | |
| 135 | const pullBase = await commitToBranch(app, alice, pullRepo, { message: "base", files: [{ path: "forge.json", content: JSON.stringify({ check: { run: "true", install: "" } }) }] }); | |
| 136 | if (!pullBase.ok) throw new Error(JSON.stringify(pullBase.error)); | |
| 137 | const topic = await commitToBranch(app, alice, pullRepo, { branch: "topic", message: "topic", files: [{ path: "t.txt", content: "t\n" }] }); | |
| 138 | if (!topic.ok) throw new Error(JSON.stringify(topic.error)); | |
| 139 | const held: (() => void)[] = []; | |
| 140 | let released = false; | |
| 141 | const runnerStep: typeof runRunnerStep = async () => { | |
| 142 | if (!released) await new Promise<void>((resolve) => held.push(resolve)); | |
| 143 | return { taken: true, ran: ok(undefined), runner: "local-1", contained: true, note: "" }; | |
| 144 | }; | |
| 145 | const checks = startChecks(app, { forgeUrl: "http://forge.test", concurrency: 1, placement: { mode: "runner", waitMs: 300 }, runnerStep }); | |
| 146 | try { | |
| 147 | expect(await waitFor(() => mainChecksOf(pullRepo.id, pullRepo.defaultBranch).every((r) => r.state === "ok"), { within: 20_000 })).toBe(true); | |
| 148 | const pull = await openPull(app, alice, pullRepo, { title: "t", body: "", baseBranch: "main", headBranch: "topic" }); | |
| 149 | if (!pull.ok) throw new Error(JSON.stringify(pull.error)); | |
| 150 | expect(await waitFor(() => held.length === 1, { within: 20_000 })).toBe(true); | |
| 151 | ||
| 152 | const a = await heldRepo("aaa"); | |
| 153 | expect(await waitFor(() => mainChecksOf(a.repo.id, a.repo.defaultBranch)[0]?.state === "running", { within: 20_000 })).toBe(true); | |
| 154 | const b = await heldRepo("bbb"); | |
| 155 | const bCheck = () => mainChecksOf(b.repo.id, b.repo.defaultBranch)[0]; | |
| 156 | // Main's slot is the first repository's, the check slot the pull's: the second waits. | |
| 157 | expect(await waitFor(() => bCheck()?.state === "running", { within: 1_500 })).toBe(false); | |
| 158 | expect(bCheck()?.state).toBe("queued"); | |
| 159 | a.release(); | |
| 160 | expect(await waitFor(() => bCheck()?.state === "running" || bCheck()?.state === "ok", { within: 20_000 })).toBe(true); | |
| 161 | b.release(); | |
| 162 | expect(await waitFor(() => bCheck()?.state === "ok", { within: 20_000 })).toBe(true); | |
| 163 | } finally { | |
| 164 | released = true; | |
| 165 | for (const release of held.splice(0)) release(); | |
| 166 | await checks.stop(); | |
| 167 | } | |
| 168 | }, 120_000); | |
| 169 | ||
| 170 | test("a repository's next main check waits behind the one in main's slot, not in a free check slot, and a newer commit supersedes it", async () => { | |
| 171 | const checks = startChecks(app, { forgeUrl: "http://forge.test", concurrency: 8 }); | |
| 172 | try { | |
| 173 | const d = await heldRepo("ddd"); | |
| 174 | const mains = () => mainChecksOf(d.repo.id, d.repo.defaultBranch); | |
| 175 | expect(await waitFor(() => mains()[0]?.state === "running", { within: 20_000 })).toBe(true); | |
| 176 | const two = await commitToBranch(app, alice, d.repo, { message: "two", files: [{ path: "TWO.md", content: "2\n" }] }); | |
| 177 | if (!two.ok) throw new Error(JSON.stringify(two.error)); | |
| 178 | const second = () => mains().find((r) => r.commitOid === two.value.commitOid); | |
| 179 | // Seven check slots are free; the second still waits behind the first. | |
| 180 | expect(await waitFor(() => second()?.state === "running", { within: 1_500 })).toBe(false); | |
| 181 | expect(second()?.state).toBe("queued"); | |
| 182 | const three = await commitToBranch(app, alice, d.repo, { message: "three", files: [{ path: "THREE.md", content: "3\n" }] }); | |
| 183 | if (!three.ok) throw new Error(JSON.stringify(three.error)); | |
| 184 | expect(await waitFor(() => second()?.state === "cancelled", { within: 20_000 })).toBe(true); | |
| 185 | expect(second()?.summary).toContain("superseded by"); | |
| 186 | d.release(); | |
| 187 | expect(await waitFor(() => mains().find((r) => r.commitOid === three.value.commitOid)?.state === "ok", { within: 20_000 })).toBe(true); | |
| 188 | } finally { | |
| 189 | await checks.stop(); | |
| 190 | } | |
| 191 | }, 120_000); | |
| 192 |
test/memory-hold.test.ts +13 −5
| @@ -4,9 +4,10 @@ import { mkdtemp, rm } from "node:fs/promises"; | ||
| 4 | 4 | import { tmpdir } from "node:os"; |
| 5 | 5 | import { join } from "node:path"; |
| 6 | 6 | import { createApp, type App } from "../src/app.ts"; |
| 7 | import { openPull } from "../src/actions/pulls.ts"; | |
| 7 | 8 | import { commitToBranch } from "../src/actions/repos.ts"; |
| 8 | 9 | import { startChecks } from "../src/checks/service.ts"; |
| 9 | import { createRun, getRun } from "../src/core/runs.ts"; | |
| 10 | import { createRun, getRun, listRuns } from "../src/core/runs.ts"; | |
| 10 | 11 | import { createUser } from "../src/core/users.ts"; |
| 11 | 12 | import { pump } from "../src/crew/dispatch.ts"; |
| 12 | 13 | import type { CrewOptions } from "../src/crew/runner.ts"; |
| @@ -125,7 +126,7 @@ describe("the check queue holds a second check under pressure", () => { | ||
| 125 | 126 | const f = checksForge("forge-memory-checks-"); |
| 126 | 127 | let share = 0.9; |
| 127 | 128 | |
| 128 | test("a second check stays queued with the log line while one is in flight, and starts once the pressure drops", async () => { | |
| 129 | test("a pull's check stays queued with the log line while main's is in flight, and starts once the pressure drops", async () => { | |
| 129 | 130 | await f.checks.stop(); |
| 130 | 131 | f.checks = startChecks(f.app, { |
| 131 | 132 | forgeUrl: f.servers.url, |
| @@ -139,19 +140,26 @@ describe("the check queue holds a second check under pressure", () => { | ||
| 139 | 140 | }); |
| 140 | 141 | if (!first.ok) throw new Error(JSON.stringify(first.error)); |
| 141 | 142 | await f.until(() => f.checksOf(first.value.commitOid)[0]?.state === "running", "first running"); |
| 143 | // Main's check runs in a slot of its own (`scheduler.ts` startMain), and still counts towards | |
| 144 | // the memory hold: a pull's check waits for it. | |
| 142 | 145 | const second = await commitToBranch(f.app, f.alice, f.repo, { |
| 146 | branch: "mem-pull", | |
| 143 | 147 | message: "Waits for memory", |
| 144 | 148 | files: [{ path: "forge.json", content: checkJson("test -f OK.md") }], |
| 145 | 149 | }); |
| 146 | 150 | if (!second.ok) throw new Error(JSON.stringify(second.error)); |
| 147 | await f.until(() => f.checksOf(second.value.commitOid).length > 0, "second queued"); | |
| 148 | const id = f.checksOf(second.value.commitOid)[0]!.id; | |
| 151 | const pull = await openPull(f.app, f.alice, f.repo, { title: "Waits for memory", body: "", baseBranch: "main", headBranch: "mem-pull" }); | |
| 152 | if (!pull.ok) throw new Error(JSON.stringify(pull.error)); | |
| 153 | const pullCheck = () => listRuns(f.app.db, f.repo.id, { kind: "check", limit: 50 }).find((r) => r.issueId === pull.value.id); | |
| 154 | await f.until(() => pullCheck() !== undefined, "second queued"); | |
| 155 | const id = pullCheck()!.id; | |
| 149 | 156 | await f.until(() => readRunLog(f.dataDir, id).lines.join("\n").includes("this forge holds 90%"), "the hold said"); |
| 150 | 157 | await Bun.sleep(100); // real-timer: a hold is a start that never comes; let several ticks pass |
| 151 | 158 | expect(getRun(f.app.db, id)?.state).toBe("queued"); |
| 152 | 159 | share = 0.1; |
| 160 | // A pull's check runs its base's check command, the held one: let both end. | |
| 161 | f.release("mem-first"); | |
| 153 | 162 | const done = await f.settled((r) => r.id === id); |
| 154 | 163 | expect(done.state).toBe("ok"); |
| 155 | f.release("mem-first"); | |
| 156 | 164 | }); |
| 157 | 165 | }); |
new above
offline
The
slotteddoc block above is now attached torunning, andslottedhas none. Give each function its own comment.