Public: anyone can read this; the crew can change it.
Share an idle forge's machine with friends: their CI, their apps and previews… #16525
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/16479-share-an-idle-forge-s-machine-with-frien → main 0 commits fork 5 threads · 5 unresolved
9 files
changed +161
−6
docs/features/runners.md +6
| @@ -143,3 +143,9 @@ Code: `src/checks/runner_pool.ts` (the forge's own), `src/checks/runner_jobs.ts` | ||
| 143 | 143 | (the desk), `src/checks/runner_step.ts` (the forge's side), `src/runner/main.ts` |
| 144 | 144 | (the runner), `src/http/api/runners.ts`. |
| 145 | 145 | Tests: `test/checks-runner-placement.test.ts`, `test/checks-runner-pool.test.ts`. Epic: #13652. |
| 146 | ||
| 147 | ## Lent runners yield to the owner (#16479) | |
| 148 | ||
| 149 | A runner enrolled with a name starting `lent-` is machine time the owner shares. When this machine's one-minute load per core is above `FORGE_LEND_YIELD_LOAD` (default 0.75, `0` never yields), its claim answers 204: it finishes the job it holds and takes no new one. Other runners are never held. `src/checks/lent_yield.ts`. | |
| 150 | ||
| 151 | The owner sees it too: while lent runners wait, the runners screen in the terminal and Settings → Runners say so, with the load and the line. Nothing lends a runner yet (part 1 of #16479 is separate), so this rule is dormant until a `lent-` runner is enrolled. |
scripts/unused-exports-allow.json +1
| @@ -17,6 +17,7 @@ | ||
| 17 | 17 | "src/checks/deepsec_plan.ts sweepArgvs", |
| 18 | 18 | "src/checks/doctor.ts isDoctorHold", |
| 19 | 19 | "src/checks/order_dependent.ts orderDependent", |
| 20 | "src/checks/lent_yield.ts setLoadSource", | |
| 20 | 21 | "src/checks/queue.ts gateDeadlineForTest", |
| 21 | 22 | "src/checks/queue.ts gateRuns", |
| 22 | 23 | "src/checks/scans.ts SCAN_BUDGET_SECONDS", |
src/checks/lent_yield.ts added +56
| @@ -0,0 +1,56 @@ | ||
| 1 | /** | |
| 2 | * Yielding to the owner (#16479, part 2). A runner named `lent-…` is machine time the owner | |
| 3 | * shares; when the owner's own work needs the cores it finishes the job it holds and takes no | |
| 4 | * new one. The runner is told "nothing now" (204), exactly as for an empty queue, and asks again. | |
| 5 | * | |
| 6 | * FORGE_LEND_YIELD_LOAD one-minute load per core above which lent runners wait. @default 0.75 | |
| 7 | * `0` never yields. | |
| 8 | */ | |
| 9 | ||
| 10 | import { cpus, loadavg } from "node:os"; | |
| 11 | ||
| 12 | /** The name prefix of runners the owner has lent out. */ | |
| 13 | export const LENT_PREFIX = "lent-"; | |
| 14 | const DEFAULT_LOAD_PER_CORE = 0.75; | |
| 15 | ||
| 16 | export const isLent = (runnerName: string): boolean => runnerName.startsWith(LENT_PREFIX); | |
| 17 | ||
| 18 | /** `FORGE_LEND_YIELD_LOAD`: a non-negative number, else the default. */ | |
| 19 | export function yieldThreshold(raw: string | undefined): number { | |
| 20 | const text = (raw ?? "").trim(); | |
| 21 | if (text === "") return DEFAULT_LOAD_PER_CORE; | |
| 22 | const value = Number(text); | |
| 23 | return value >= 0 && value < Infinity ? value : DEFAULT_LOAD_PER_CORE; | |
| 24 | } | |
| 25 | ||
| 26 | /** Whether a runner may take a new job: only a lent one ever waits, and only above the line. */ | |
| 27 | export function mayTakeJob( | |
| 28 | runnerName: string, | |
| 29 | loadPerCore: number, | |
| 30 | threshold: number, | |
| 31 | ): boolean { | |
| 32 | if (!isLent(runnerName) || threshold === 0) return true; | |
| 33 | return loadPerCore <= threshold; | |
| 34 | } | |
| 35 | ||
| 36 | const realLoad = (): number => loadavg()[0]! / Math.max(1, cpus().length); | |
| 37 | let loadSource = realLoad; | |
| 38 | ||
| 39 | /** This machine's one-minute load per core. Tests swap the source; pass nothing to restore it. */ | |
| 40 | export const machineLoad = (): number => loadSource(); | |
| 41 | export function setLoadSource(source?: () => number): void { | |
| 42 | loadSource = source ?? realLoad; | |
| 43 | } | |
| 44 | ||
| 45 | /** What the owner sees: the line, the load now, and whether lent runners wait because of it. */ | |
| 46 | export function yieldState(env: string | undefined, load: number = machineLoad()) { | |
| 47 | const threshold = yieldThreshold(env); | |
| 48 | return { threshold, load, waiting: threshold !== 0 && load > threshold }; | |
| 49 | } | |
| 50 | ||
| 51 | /** One line for the runners screen and page; empty when nothing waits. */ | |
| 52 | export function yieldNote(state: { threshold: number; load: number; waiting: boolean }): string { | |
| 53 | return state.waiting | |
| 54 | ? `Lent runners are waiting: load ${state.load.toFixed(2)} per core is above ${state.threshold} (FORGE_LEND_YIELD_LOAD).` | |
| 55 | : ""; | |
| 56 | } |
src/http/api/runners.ts +9 −5
| @@ -10,6 +10,7 @@ | ||
| 10 | 10 | */ |
| 11 | 11 | |
| 12 | 12 | import { enrol, list, remove } from "../../actions/runners.ts"; |
| 13 | import { machineLoad, mayTakeJob, yieldThreshold } from "../../checks/lent_yield.ts"; | |
| 13 | 14 | import { |
| 14 | 15 | LEASE_MS, |
| 15 | 16 | claimJob, |
| @@ -123,14 +124,17 @@ export function runnerRoutes(router: Router<Handler>): void { | ||
| 123 | 124 | Math.min(Math.max(Number(input.wait) || 0, 0), CLAIM_WAIT_MAX_S) * 1000; |
| 124 | 125 | const until = Date.now() + wait; |
| 125 | 126 | const stop = AbortSignal.any([ctx.request.signal, ctx.app.closing.signal]); |
| 127 | const threshold = yieldThreshold(process.env["FORGE_LEND_YIELD_LOAD"]); | |
| 126 | 128 | for (;;) { |
| 127 | 129 | // Removed while it waited: its token is no longer one. |
| 128 | 130 | if (getRunner(ctx.app.db, runner.id) === undefined) return unauthorized(); |
| 129 | const claim = claimJob( | |
| 130 | ctx.app.db, | |
| 131 | { id: runner.id, cells: input.cells === true }, | |
| 132 | Date.now(), | |
| 133 | ); | |
| 131 | const claim = !mayTakeJob(runner.name, machineLoad(), threshold) | |
| 132 | ? undefined | |
| 133 | : claimJob( | |
| 134 | ctx.app.db, | |
| 135 | { id: runner.id, cells: input.cells === true }, | |
| 136 | Date.now(), | |
| 137 | ); | |
| 134 | 138 | if (claim !== undefined) { |
| 135 | 139 | deskOf(ctx.app).ears.get(claim.job.id)?.wake(); |
| 136 | 140 | return json({ job: claim.job, token: claim.token, lease }); |
src/tui/screens/runners.ts +3 −1
| @@ -7,6 +7,7 @@ | ||
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | 9 | import { isLocal, list, removeHandMade, runnerState } from "../../actions/runners.ts"; |
| 10 | import { yieldNote, yieldState } from "../../checks/lent_yield.ts"; | |
| 10 | 11 | import { heldByRunner, workByRunner } from "../../checks/runner_jobs.ts"; |
| 11 | 12 | import type { Ctx, Screen } from "../app/app.ts"; |
| 12 | 13 | import { binding } from "../app/keys.ts"; |
| @@ -54,7 +55,8 @@ export function runnersScreen(f: Session, ctx: Ctx): Screen { | ||
| 54 | 55 | : entries().length === 0 |
| 55 | 56 | ? "No runner is enrolled; checks run on the forge's own machine." |
| 56 | 57 | : "The machines that take this forge's checks; x removes a hand-made one."; |
| 57 | s.text(head!.x, head!.y, truncate(text, head!.w), style(c.mutedFg, c.bg), head!.x + head!.w); | |
| 58 | const waiting = mayRunners(f) ? yieldNote(yieldState(process.env["FORGE_LEND_YIELD_LOAD"])) : ""; | |
| 59 | s.text(head!.x, head!.y, truncate(waiting === "" ? text : waiting, head!.w), style(c.mutedFg, c.bg), head!.x + head!.w); | |
| 58 | 60 | drawList(s, body!, c, state, rows()); |
| 59 | 61 | }, |
| 60 | 62 | key(e) { |
src/web/pages/runners.ts +3
| @@ -9,6 +9,7 @@ | ||
| 9 | 9 | * check answers rather than the admin gate's 403. |
| 10 | 10 | */ |
| 11 | 11 | import { isLocal, list, removeHandMade, runnerState } from "../../actions/runners.ts"; |
| 12 | import { yieldNote, yieldState } from "../../checks/lent_yield.ts"; | |
| 12 | 13 | import { heldByRunner, workByRunner, type Enrolled, type RunnerWork } from "../../checks/runner_jobs.ts"; |
| 13 | 14 | import { notePhase, takeSessionTime } from "../../core/route_timing.ts"; |
| 14 | 15 | import type { Ctx } from "../../http/context.ts"; |
| @@ -60,7 +61,9 @@ export function runnersPage(ctx: Ctx): Response { | ||
| 60 | 61 | const now = Date.now(); |
| 61 | 62 | const held = heldByRunner(ctx.app.db); |
| 62 | 63 | const work = workByRunner(ctx.app.db); |
| 64 | const waiting = yieldNote(yieldState(process.env["FORGE_LEND_YIELD_LOAD"])); | |
| 63 | 65 | const body = html`<section class="settings-block"> |
| 66 | ${waiting === "" ? "" : html`<p class="muted small" id="lent-waiting">${waiting}</p>`} | |
| 64 | 67 | <p class="muted small">Enrol one with <code>POST /api/v1/admin/runners</code>.</p> |
| 65 | 68 | ${seen.value.length === 0 |
| 66 | 69 | ? emptyList("runners") |
test/lent-yield.test.ts added +54
| @@ -0,0 +1,54 @@ | ||
| 1 | /** #16479: a lent runner takes no new job while the owner's machine is busy. */ | |
| 2 | import { expect, test } from "bun:test"; | |
| 3 | import { mayTakeJob, setLoadSource, yieldNote, yieldState, yieldThreshold } from "../src/checks/lent_yield.ts"; | |
| 4 | import { enrolRunner, offerJob, type RunnerJob } from "../src/checks/runner_jobs.ts"; | |
| 5 | import { checksForge } from "./helpers/checks-forge.ts"; | |
| 6 | import { quietLogs } from "./helpers/globals.ts"; | |
| 7 | ||
| 8 | quietLogs(); | |
| 9 | const f = checksForge("forge-lent-yield-"); | |
| 10 | ||
| 11 | test("only a lent runner waits, and only above the line; 0 never yields", () => { | |
| 12 | expect(mayTakeJob("lent-bob", 0.9, 0.75)).toBe(false); | |
| 13 | expect(mayTakeJob("lent-bob", 0.5, 0.75)).toBe(true); | |
| 14 | expect(mayTakeJob("local-1", 5, 0.75)).toBe(true); | |
| 15 | expect(mayTakeJob("lent-bob", 5, 0)).toBe(true); | |
| 16 | expect(yieldThreshold(undefined)).toBe(0.75); | |
| 17 | expect(yieldThreshold("abc")).toBe(0.75); | |
| 18 | expect(yieldThreshold("-1")).toBe(0.75); | |
| 19 | expect(yieldThreshold("0")).toBe(0); | |
| 20 | }); | |
| 21 | ||
| 22 | test("the claim route gives a lent runner 204 when busy, an own runner its job", async () => { | |
| 23 | const job: Omit<RunnerJob, "id"> = { | |
| 24 | run: "r1", repo: "alice/lib", commit: "a".repeat(40), depth: 1, | |
| 25 | steps: [{ name: "check", command: "true" }], env: {}, limits: {}, timeoutS: 60, | |
| 26 | egress: [], egressMode: "enforce", cell: "preferred", cache: { repo: "r", tree: "", base: "" }, | |
| 27 | }; | |
| 28 | offerJob(f.app.db, { runId: "r1", repoId: "r", dir: "/nowhere", job }); | |
| 29 | const claim = (token: string) => | |
| 30 | fetch(`${f.servers.url}/api/v1/runner/claim`, { | |
| 31 | method: "POST", | |
| 32 | headers: { authorization: `Bearer ${token}`, "content-type": "application/json" }, | |
| 33 | body: JSON.stringify({ wait: 0 }), | |
| 34 | }); | |
| 35 | const was = process.env["FORGE_LEND_YIELD_LOAD"]; | |
| 36 | process.env["FORGE_LEND_YIELD_LOAD"] = "0.75"; | |
| 37 | setLoadSource(() => 0.9); | |
| 38 | try { | |
| 39 | const lent = enrolRunner(f.app.db, "lent-bob", f.alice.id).token; | |
| 40 | const own = enrolRunner(f.app.db, "own", f.alice.id).token; | |
| 41 | expect((await claim(lent)).status).toBe(204); | |
| 42 | expect((await claim(own)).status).toBe(200); | |
| 43 | } finally { | |
| 44 | setLoadSource(); | |
| 45 | if (was === undefined) delete process.env["FORGE_LEND_YIELD_LOAD"]; | |
| 46 | else process.env["FORGE_LEND_YIELD_LOAD"] = was; | |
| 47 | } | |
| 48 | }); | |
| 49 | ||
| 50 | test("the owner is told why lent runners wait, and only when they do", () => { | |
| 51 | expect(yieldNote(yieldState("0.75", 0.9))).toContain("Lent runners are waiting: load 0.90"); | |
| 52 | expect(yieldNote(yieldState("0.75", 0.5))).toBe(""); | |
| 53 | expect(yieldNote(yieldState("0", 9))).toBe(""); | |
| 54 | }); |
test/settings-runners.test.ts +19
| @@ -5,6 +5,7 @@ import { tmpdir } from "node:os"; | ||
| 5 | 5 | import { join } from "node:path"; |
| 6 | 6 | import { list } from "../src/actions/runners.ts"; |
| 7 | 7 | import { createApp, type App } from "../src/app.ts"; |
| 8 | import { setLoadSource } from "../src/checks/lent_yield.ts"; | |
| 8 | 9 | import { claimJob, enrolRunner, noteRunnerJobs, offerJob, settleJob, type RunnerJob } from "../src/checks/runner_jobs.ts"; |
| 9 | 10 | import { createSession } from "../src/core/sessions.ts"; |
| 10 | 11 | import { createUser, type User } from "../src/core/users.ts"; |
| @@ -103,6 +104,24 @@ describe("the runners page", () => { | ||
| 103 | 104 | expect(main.slice(at, at + 1200)).toContain("runs 4 at once · holding 0 now · 2 jobs run"); |
| 104 | 105 | }); |
| 105 | 106 | |
| 107 | test("the owner is told when lent runners wait for a busy machine, and not when it is idle", async () => { | |
| 108 | const saved = process.env["FORGE_LEND_YIELD_LOAD"]; | |
| 109 | delete process.env["FORGE_LEND_YIELD_LOAD"]; | |
| 110 | try { | |
| 111 | setLoadSource(() => 0.9); | |
| 112 | const busy = await (await get(alice)).text(); | |
| 113 | expect(busy).toContain('id="lent-waiting"'); | |
| 114 | expect(busy).toContain("Lent runners are waiting: load 0.90 per core is above 0.75"); | |
| 115 | setLoadSource(() => 0.1); | |
| 116 | const idle = await (await get(alice)).text(); | |
| 117 | expect(idle).not.toContain('id="lent-waiting"'); | |
| 118 | expect(idle).not.toContain("Lent runners are waiting"); | |
| 119 | } finally { | |
| 120 | setLoadSource(); | |
| 121 | if (saved !== undefined) process.env["FORGE_LEND_YIELD_LOAD"] = saved; | |
| 122 | } | |
| 123 | }); | |
| 124 | ||
| 106 | 125 | test("a signed-in non-admin gets the plain 404, and cannot remove", async () => { |
| 107 | 126 | const r = enrolRunner(app.db, "keep", alice.id).runner; |
| 108 | 127 | const res = await get(bob); |
test/tui-runners.test.ts +10
| @@ -9,6 +9,7 @@ import { mkdtemp, rm } from "node:fs/promises"; | ||
| 9 | 9 | import { tmpdir } from "node:os"; |
| 10 | 10 | import { join } from "node:path"; |
| 11 | 11 | import { createApp, type App } from "../src/app.ts"; |
| 12 | import { setLoadSource } from "../src/checks/lent_yield.ts"; | |
| 12 | 13 | import { enrolRunner, listRunners } from "../src/checks/runner_jobs.ts"; |
| 13 | 14 | import { createUser, type User } from "../src/core/users.ts"; |
| 14 | 15 | import { createForgeApp } from "../src/tui/forge.ts"; |
| @@ -95,6 +96,15 @@ describe("runners in the terminal", () => { | ||
| 95 | 96 | expect(events("runner.removed")).toBe(0); |
| 96 | 97 | }); |
| 97 | 98 | |
| 99 | test("the owner is told when lent runners wait for a busy machine", async () => { | |
| 100 | setLoadSource(() => 0.9); | |
| 101 | try { | |
| 102 | expect((await drive(ada)).text).toContain("Lent runners are waiting: load 0.90"); | |
| 103 | } finally { | |
| 104 | setLoadSource(); | |
| 105 | } | |
| 106 | }); | |
| 107 | ||
| 98 | 108 | test("x removes a hand-made runner after a confirm", async () => { |
| 99 | 109 | await drive(ada, ["j", "x", ENTER]); |
| 100 | 110 | expect(names()).toEqual(["local-1", "mac"]); |
new above
offline
machineLoad reads the real machine and is called straight from the route, so nothing can control it in tests. Pass it in. Also surface this value and the yield state on the runners screen in src/tui/ and on /system, so the owner sees why lent runners wait.