Public: anyone can read this; the crew can change it.
A wrong Bun says so in one line, and the repository says which Bun it needs #16515
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/16326-a-wrong-bun-says-so-in-one-line-and-the → main 0 commits fork 2 threads · 2 unresolved
19 files
changed +153
−49
- .bun-version
- CLAUDE.md
- Dockerfile
- package.json
- scripts/cut-release.ts
- src/cloud/machine.ts
- src/keeper/by_hand.ts
- src/keeper/wire.ts
- src/kernel/bun_guard.ts
- src/kernel/bun_pin.ts
- src/kernel/versions.ts
- src/main.ts
- src/perf/baseline.ts
- src/release/shipped.ts
- test/bun-pin.test.ts
- test/estimates_done.test.ts
- test/helpers/preload.ts
- test/release-shipped.test.ts
- test/versions.test.ts
.bun-version added +1
| @@ -0,0 +1,1 @@ | ||
| 1 | 1.4.4 |
CLAUDE.md +2
| @@ -63,6 +63,8 @@ canAdmin`), token scope (`readForm` floor + `minScope`), and branch | ||
| 63 | 63 | |
| 64 | 64 | ## How to work |
| 65 | 65 | |
| 66 | **Bun is pinned in `.bun-version` (1.4.4); an older one stops `bun test` and `src/main.ts` with one sentence naming the install command.** | |
| 67 | ||
| 66 | 68 | sh |
| 67 | 69 | bun test # full suite — must stay green |
| 68 | 70 | bun x tsc --noEmit # zero type errors is the standard (strict, exact-optional; an unused import or local fails it) |
Dockerfile +1 −1
| @@ -15,7 +15,7 @@ RUN apt-get update \ | ||
| 15 | 15 | && npm install -g @anthropic-ai/claude-code \ |
| 16 | 16 | && npm cache clean --force |
| 17 | 17 | WORKDIR /app |
| 18 | COPY package.json tsconfig.json ./ | |
| 18 | COPY package.json tsconfig.json .bun-version ./ | |
| 19 | 19 | COPY src ./src |
| 20 | 20 | # The forge's documentation: /docs and the terminal list docs/*.md and docs/features/*.md from the |
| 21 | 21 | # release's checkout (src/view/docs.ts), and the crew is briefed from docs/PRINCIPLES.md and |
package.json +3
scripts/cut-release.ts +3 −2
| @@ -11,7 +11,8 @@ | ||
| 11 | 11 | |
| 12 | 12 | import { migrationFiles, NUMBERED } from "../src/core/schema.ts"; |
| 13 | 13 | import { loadCheckHint } from "../src/perf/baseline.ts"; |
| 14 | import { compareVersions, mergeShipped, readTagged, recordFor, shippedReleases } from "../src/release/shipped.ts"; | |
| 14 | import { compareVersions } from "../src/kernel/versions.ts"; | |
| 15 | import { mergeShipped, readTagged, recordFor, shippedReleases } from "../src/release/shipped.ts"; | |
| 15 | 16 | |
| 16 | 17 | const VERSION = /^\d+\.\d+\.\d+$/; |
| 17 | 18 | const [version, flag] = process.argv.slice(2); |
| @@ -21,7 +22,7 @@ if (version === undefined || !VERSION.test(version) || (flag !== undefined && fl | ||
| 21 | 22 | } |
| 22 | 23 | const root = `${import.meta.dir}/..`; |
| 23 | 24 | const newest = mergeShipped(shippedReleases(), await readTagged(root))[0]; |
| 24 | if (newest !== undefined && compareVersions(version, newest.version) >= 0) { | |
| 25 | if (newest !== undefined && compareVersions(version, newest.version) <= 0) { | |
| 25 | 26 | console.error(`cut-release: ${version} is not newer than ${newest.version}, the newest release`); |
| 26 | 27 | process.exit(1); |
| 27 | 28 | } |
src/cloud/machine.ts +5 −3
| @@ -18,6 +18,7 @@ | ||
| 18 | 18 | * a failure half-way leaves a row a retry can finish, never an orphan. |
| 19 | 19 | */ |
| 20 | 20 | |
| 21 | import { BUN_PIN } from "../kernel/bun_pin.ts"; | |
| 21 | 22 | import type { Fetch } from "./providers.ts"; |
| 22 | 23 | import { firstAdminSeed, readSeed } from "../core/first_admin.ts"; |
| 23 | 24 | import { BOOT_NAMES, portOf, validHost, validSource, type Pinned } from "../core/pinned.ts"; |
| @@ -25,10 +26,11 @@ import { OWN_FORGE_REPO } from "../spawn/own_forge.ts"; | ||
| 25 | 26 | import { BUN_HOME, CHECKOUT, hardenEarly, hardenFiles, hardenHead, hardenLate, mount, proxyStore, serviceUnit } from "./harden.ts"; |
| 26 | 27 | |
| 27 | 28 | /** |
| 28 | * The Bun the machine installs: it moves only when this line does, in a pull, so the runtime is | |
| 29 | * pinned as the forge is. | |
| 29 | * The Bun the machine installs: it moves only when `.bun-version` does, in a pull, so the runtime | |
| 30 | * is pinned as the forge is. Where `.bun-version` does not ship (a tree of `src/` alone) it is the | |
| 31 | * Bun running this process, not a pin. | |
| 30 | 32 | */ |
| 31 | export const BUN_VERSION = "1.4.4"; | |
| 33 | export const BUN_VERSION = BUN_PIN ?? Bun.version; | |
| 32 | 34 | |
| 33 | 35 | /** The name of the volume holding a forge's data, the same on every cloud. */ |
| 34 | 36 | export const volumeName = (name: string) => `forge-${name}-data`; |
src/keeper/by_hand.ts +6 −17
| @@ -10,22 +10,11 @@ | ||
| 10 | 10 | */ |
| 11 | 11 | import type { Database } from "bun:sqlite"; |
| 12 | 12 | import { running } from "../kernel/version.ts"; |
| 13 | import { compareVersions, DOTTED } from "../kernel/versions.ts"; | |
| 13 | 14 | |
| 14 | const NUMERIC = /^\d+(?:\.\d+){0,3}$/; | |
| 15 | ||
| 16 | /** | |
| 17 | * Dotted numbers compared part by part: negative when `a` is older; undefined when either is not | |
| 18 | * dotted numbers. | |
| 19 | */ | |
| 20 | export function compareVersions(a: string, b: string): number | undefined { | |
| 21 | if (!NUMERIC.test(a) || !NUMERIC.test(b)) return undefined; | |
| 22 | const x = a.split(".").map(Number); | |
| 23 | const y = b.split(".").map(Number); | |
| 24 | for (let i = 0; i < Math.max(x.length, y.length); i++) { | |
| 25 | const d = (x[i] ?? 0) - (y[i] ?? 0); | |
| 26 | if (d !== 0) return d; | |
| 27 | } | |
| 28 | return 0; | |
| 15 | /** True when `a` is older than `b`; false when either is not dotted numbers. */ | |
| 16 | function sooner(a: string, b: string): boolean { | |
| 17 | return DOTTED.test(a) && DOTTED.test(b) && compareVersions(a, b) < 0; | |
| 29 | 18 | } |
| 30 | 19 | |
| 31 | 20 | /** The version recorded, or undefined when this boot records nothing. */ |
| @@ -35,12 +24,12 @@ export function recordByHand( | ||
| 35 | 24 | now: Date = new Date(), |
| 36 | 25 | ): string | undefined { |
| 37 | 26 | const stamp = running({ FORGE_VERSION: env["FORGE_VERSION"], FORGE_COMMIT: env["FORGE_COMMIT"] }); |
| 38 | if (stamp.commit === null || !NUMERIC.test(stamp.version)) return undefined; | |
| 27 | if (stamp.commit === null || !DOTTED.test(stamp.version)) return undefined; | |
| 39 | 28 | const served = db |
| 40 | 29 | .query<{ version: string }, []>(`SELECT version FROM keeper_release WHERE live_at IS NOT NULL AND stage != 'rolled_back'`) |
| 41 | 30 | .all(); |
| 42 | 31 | if (served.some((r) => r.version.replace(/^v(?=\d)/, "") === stamp.version)) return undefined; |
| 43 | if (served.some((r) => (compareVersions(stamp.version, r.version.replace(/^v(?=\d)/, "")) ?? 0) < 0)) return undefined; | |
| 32 | if (served.some((r) => sooner(stamp.version, r.version.replace(/^v(?=\d)/, "")))) return undefined; | |
| 44 | 33 | const at = now.toISOString(); |
| 45 | 34 | const made = db |
| 46 | 35 | .query( |
src/keeper/wire.ts +2 −1
| @@ -29,7 +29,8 @@ import { ownProbeHeaders } from "../kernel/probe.ts"; | ||
| 29 | 29 | import { bootStall } from "../perf/live.ts"; |
| 30 | 30 | import { writePause } from "./pause.ts"; |
| 31 | 31 | import { running } from "../kernel/version.ts"; |
| 32 | import { compareVersions, readTags, recordFor } from "../release/shipped.ts"; | |
| 32 | import { compareVersions } from "../kernel/versions.ts"; | |
| 33 | import { readTags, recordFor } from "../release/shipped.ts"; | |
| 33 | 34 | import { checkoutRelease, preflightRelease } from "../release/checkout.ts"; |
| 34 | 35 | import { handover } from "../release/swap.ts"; |
| 35 | 36 | import { runningChildren } from "../boards/sampler.ts"; |
src/kernel/bun_guard.ts added +12
src/kernel/bun_pin.ts added +42
| @@ -0,0 +1,42 @@ | ||
| 1 | /** | |
| 2 | * The Bun this repository needs: one line in `.bun-version`, read by the boot guard, the test | |
| 3 | * preload, the cloud machine's installer, and checked against the Dockerfile's image tag. | |
| 4 | */ | |
| 5 | ||
| 6 | import { readFileSync } from "node:fs"; | |
| 7 | import { join } from "node:path"; | |
| 8 | import { compareVersions, DOTTED } from "./versions.ts"; | |
| 9 | ||
| 10 | /** | |
| 11 | * The pinned version from `.bun-version` at the repository root; null where the file does not ship | |
| 12 | * (the tree's `src/` alone), where the running Bun is the only one there is. | |
| 13 | */ | |
| 14 | export const BUN_PIN: string | null = (() => { | |
| 15 | try { | |
| 16 | return readFileSync(join(import.meta.dir, "..", "..", ".bun-version"), "utf8").trim() || null; | |
| 17 | } catch { | |
| 18 | return null; | |
| 19 | } | |
| 20 | })(); | |
| 21 | ||
| 22 | /** True when `found` is older than `need` (dotted numbers; one that is not is let by). */ | |
| 23 | export function olderThan(found: string, need: string): boolean { | |
| 24 | return DOTTED.test(found) && DOTTED.test(need) && compareVersions(found, need) < 0; | |
| 25 | } | |
| 26 | ||
| 27 | /** The one sentence a wrong Bun gets, or null when `found` is new enough. */ | |
| 28 | export function wrongBun(found: string, need: string | null = BUN_PIN): string | null { | |
| 29 | if (need === null) return null; | |
| 30 | if (!olderThan(found, need)) return null; | |
| 31 | return ( | |
| 32 | `This runtime is Bun ${found} but the repository needs Bun ${need}: ` + | |
| 33 | `install it with \`curl -fsSL https://bun.sh/install | bash -s "bun-v${need}"\`.` | |
| 34 | ); | |
| 35 | } | |
| 36 | ||
| 37 | /** Stops the process with the sentence when the running Bun is too old. */ | |
| 38 | export function requireBun(found: string = Bun.version): void { | |
| 39 | const s = wrongBun(found); | |
| 40 | if (s === null) return; | |
| 41 | throw new Error(s); | |
| 42 | } |
src/kernel/versions.ts added +17
| @@ -0,0 +1,17 @@ | ||
| 1 | /** The one way versions are compared: dotted parts as numbers, anything else as text. */ | |
| 2 | ||
| 3 | /** Dotted numbers, up to four parts: what a release or a runtime is called. */ | |
| 4 | export const DOTTED = /^\d+(?:\.\d+){0,3}$/; | |
| 5 | ||
| 6 | /** Negative when `a` is older than `b`, positive when newer, 0 when equal. */ | |
| 7 | export function compareVersions(a: string, b: string): number { | |
| 8 | const x = a.split("."), y = b.split("."); | |
| 9 | for (let i = 0; i < Math.max(x.length, y.length); i++) { | |
| 10 | const p = x[i] ?? "", q = y[i] ?? ""; | |
| 11 | if (p === q) continue; | |
| 12 | const m = Number(p), n = Number(q); | |
| 13 | if (p !== "" && q !== "" && Number.isInteger(m) && Number.isInteger(n)) return m - n; | |
| 14 | return p < q ? -1 : 1; | |
| 15 | } | |
| 16 | return 0; | |
| 17 | } |
src/main.ts +1
src/perf/baseline.ts +3 −3
| @@ -3,7 +3,7 @@ | ||
| 3 | 3 | * File names in, the newest older one out; `bench/load.ts` does the judging. |
| 4 | 4 | */ |
| 5 | 5 | |
| 6 | import { compareVersions } from "../release/shipped.ts"; | |
| 6 | import { compareVersions } from "../kernel/versions.ts"; | |
| 7 | 7 | |
| 8 | 8 | /** |
| 9 | 9 | * Each release keeps two results (#9514): `<version>.json` read through the |
| @@ -25,8 +25,8 @@ export function lastBaseline(files: readonly string[], version: string, cache: C | ||
| 25 | 25 | .map((f) => KEPT.exec(f)) |
| 26 | 26 | .filter((m): m is RegExpExecArray => m !== null && (m[2] === undefined) === (cache === "edge")) |
| 27 | 27 | .map((m) => m[1]!) |
| 28 | .filter((v) => compareVersions(v, version) > 0) | |
| 29 | .sort(compareVersions); | |
| 28 | .filter((v) => compareVersions(version, v) > 0) | |
| 29 | .sort((a, b) => compareVersions(b, a)); | |
| 30 | 30 | const newest = older[0]; |
| 31 | 31 | return newest === undefined ? undefined : resultFile(newest, cache); |
| 32 | 32 | } |
src/release/shipped.ts +2 −14
| @@ -17,6 +17,7 @@ | ||
| 17 | 17 | import before from "../core/releases-before-tags.json" with { type: "json" }; |
| 18 | 18 | import { runGit } from "../git/env.ts"; |
| 19 | 19 | import { err, ok, type Result } from "../kernel/result.ts"; |
| 20 | import { compareVersions } from "../kernel/versions.ts"; | |
| 20 | 21 | |
| 21 | 22 | export interface Shipped { |
| 22 | 23 | readonly version: string; |
| @@ -55,25 +56,12 @@ export function parseRecord(message: string): Omit<Shipped, "at"> | undefined { | ||
| 55 | 56 | return { version, migrations: migrations as number, ids: ids as string[] }; |
| 56 | 57 | } |
| 57 | 58 | |
| 58 | /** Newer first: dotted numbers compare as numbers, anything else as text. */ | |
| 59 | export function compareVersions(a: string, b: string): number { | |
| 60 | const x = a.split("."), y = b.split("."); | |
| 61 | for (let i = 0; i < Math.max(x.length, y.length); i++) { | |
| 62 | const p = x[i] ?? "", q = y[i] ?? ""; | |
| 63 | if (p === q) continue; | |
| 64 | const m = Number(p), n = Number(q); | |
| 65 | if (p !== "" && q !== "" && Number.isInteger(m) && Number.isInteger(n)) return n - m; | |
| 66 | return p < q ? 1 : -1; | |
| 67 | } | |
| 68 | return 0; | |
| 69 | } | |
| 70 | ||
| 71 | 59 | /** The frozen releases and the tagged ones as one list, newest first; a tag wins over a frozen entry of its version. */ |
| 72 | 60 | export function mergeShipped(frozen: readonly Shipped[], tagged: readonly Shipped[]): Shipped[] { |
| 73 | 61 | const byVersion = new Map<string, Shipped>(); |
| 74 | 62 | for (const r of frozen) byVersion.set(r.version, r); |
| 75 | 63 | for (const r of tagged) byVersion.set(r.version, r); |
| 76 | return [...byVersion.values()].sort((a, b) => compareVersions(a.version, b.version)); | |
| 64 | return [...byVersion.values()].sort((a, b) => compareVersions(b.version, a.version)); | |
| 77 | 65 | } |
| 78 | 66 | |
| 79 | 67 | /** The release records on the annotated `v*` tags of the repository at `dir`, newest first; why not when git cannot say. */ |
test/bun-pin.test.ts added +30
| @@ -0,0 +1,30 @@ | ||
| 1 | import { expect, test } from "bun:test"; | |
| 2 | import { readFileSync } from "node:fs"; | |
| 3 | import { BUN_PIN, requireBun, wrongBun } from "../src/kernel/bun_pin.ts"; | |
| 4 | import { BUN_VERSION } from "../src/cloud/machine.ts"; | |
| 5 | ||
| 6 | test("a Bun below the pin is refused with one sentence", () => { | |
| 7 | const s = wrongBun("1.3.13", "1.4.4"); | |
| 8 | expect(s).toContain("Bun 1.3.13"); | |
| 9 | expect(s).toContain("needs Bun 1.4.4"); | |
| 10 | expect(s).toContain('bun-v1.4.4'); | |
| 11 | expect(s).not.toContain("\n"); | |
| 12 | expect(() => requireBun("1.3.13")).toThrow(s as string); | |
| 13 | }); | |
| 14 | ||
| 15 | test("the pinned Bun and newer pass", () => { | |
| 16 | expect(wrongBun("1.4.4", "1.4.4")).toBeNull(); | |
| 17 | expect(wrongBun("1.10.0", "1.4.4")).toBeNull(); | |
| 18 | expect(() => requireBun(BUN_PIN as string)).not.toThrow(); | |
| 19 | }); | |
| 20 | ||
| 21 | test("the machine and the image read the one pin", () => { | |
| 22 | expect(BUN_PIN).not.toBeNull(); | |
| 23 | expect(BUN_VERSION).toBe(BUN_PIN as string); | |
| 24 | expect(readFileSync("Dockerfile", "utf8")).toContain(`FROM oven/bun:${BUN_PIN}-debian`); | |
| 25 | expect(JSON.parse(readFileSync("package.json", "utf8")).engines.bun).toBe(`>=${BUN_PIN}`); | |
| 26 | }); | |
| 27 | ||
| 28 | test("the running Bun meets the pin", () => { | |
| 29 | expect(wrongBun(Bun.version)).toBeNull(); | |
| 30 | }); |
test/estimates_done.test.ts +1 −3
| @@ -15,7 +15,7 @@ import { createIssue } from "../src/core/issues.ts"; | ||
| 15 | 15 | import type { Repo } from "../src/core/repos.ts"; |
| 16 | 16 | import { createUser, type User } from "../src/core/users.ts"; |
| 17 | 17 | import { baseRate, historySamples, type HistorySample } from "../src/flow/estimate_base.ts"; |
| 18 | import { compareVersions, recordByHand } from "../src/keeper/by_hand.ts"; | |
| 18 | import { recordByHand } from "../src/keeper/by_hand.ts"; | |
| 19 | 19 | import { listKeeperReleases } from "../src/keeper/record.ts"; |
| 20 | 20 | |
| 21 | 21 | const H = 3_600_000; |
| @@ -150,7 +150,5 @@ describe("estimates: done, reopens, by-hand releases", () => { | ||
| 150 | 150 | expect(recordByHand(app.db, { FORGE_VERSION: "dev", FORGE_COMMIT: "1234567" })).toBeUndefined(); |
| 151 | 151 | expect(recordByHand(app.db, { FORGE_VERSION: "0.7.561" })).toBeUndefined(); |
| 152 | 152 | expect(recordByHand(app.db, { FORGE_VERSION: "0.7.561", FORGE_COMMIT: "1234567" })).toBe("0.7.561"); |
| 153 | expect(compareVersions("0.7.560", "0.7.99")).toBeGreaterThan(0); | |
| 154 | expect(compareVersions("0.7.560", "x")).toBeUndefined(); | |
| 155 | 153 | }); |
| 156 | 154 | }); |
test/helpers/preload.ts +1
| @@ -3,6 +3,7 @@ | ||
| 3 | 3 | * the forge. Only what the whole suite must agree on belongs here. |
| 4 | 4 | */ |
| 5 | 5 | |
| 6 | import "../../src/kernel/bun_guard.ts"; | |
| 6 | 7 | import { provideCpusForTest } from "../../src/core/capacity.ts"; |
| 7 | 8 | import { provideMirrorOutboundForTest } from "../../src/origins/git-mirror.ts"; |
| 8 | 9 | import { installCheapHash } from "./cheap-hash.ts"; |
test/release-shipped.test.ts −5
| @@ -10,7 +10,6 @@ import { join } from "node:path"; | ||
| 10 | 10 | import { migrationFiles, NUMBERED } from "../src/core/schema.ts"; |
| 11 | 11 | import { releasesBehind } from "../src/spawn/heartbeat.ts"; |
| 12 | 12 | import { |
| 13 | compareVersions, | |
| 14 | 13 | mergeShipped, |
| 15 | 14 | parseRecord, |
| 16 | 15 | readTagged, |
| @@ -75,10 +74,6 @@ describe("a release record", () => { | ||
| 75 | 74 | expect(parseRecord(JSON.stringify(record))).toEqual(record); |
| 76 | 75 | }); |
| 77 | 76 | |
| 78 | test("versions sort newest first, by number", () => { | |
| 79 | expect(["0.7.9", "0.7.271", "0.10.0", "0.7.10"].sort(compareVersions)).toEqual(["0.10.0", "0.7.271", "0.7.10", "0.7.9"]); | |
| 80 | expect(compareVersions("1.0.0", "1.0.0")).toBe(0); | |
| 81 | }); | |
| 82 | 77 | }); |
| 83 | 78 | |
| 84 | 79 | describe("the shipped releases", () => { |
test/versions.test.ts added +21
| @@ -0,0 +1,21 @@ | ||
| 1 | import { describe, expect, test } from "bun:test"; | |
| 2 | import { compareVersions, DOTTED } from "../src/kernel/versions.ts"; | |
| 3 | ||
| 4 | describe("compareVersions", () => { | |
| 5 | test("dotted parts compare as numbers; negative is older", () => { | |
| 6 | expect(compareVersions("0.7.560", "0.7.99")).toBeGreaterThan(0); | |
| 7 | expect(compareVersions("0.7.9", "0.10.0")).toBeLessThan(0); | |
| 8 | expect(compareVersions("1.0.0", "1.0.0")).toBe(0); | |
| 9 | expect(compareVersions("1.0", "1.0.1")).toBeLessThan(0); | |
| 10 | }); | |
| 11 | test("sorted ascending, and newest first by swapping", () => { | |
| 12 | const v = ["0.7.9", "0.7.271", "0.10.0", "0.7.10"]; | |
| 13 | expect([...v].sort(compareVersions)).toEqual(["0.7.9", "0.7.10", "0.7.271", "0.10.0"]); | |
| 14 | expect([...v].sort((a, b) => compareVersions(b, a))).toEqual(["0.10.0", "0.7.271", "0.7.10", "0.7.9"]); | |
| 15 | }); | |
| 16 | test("anything else compares as text; DOTTED names what is numeric", () => { | |
| 17 | expect(compareVersions("1.0.0-b", "1.0.0-a")).toBeGreaterThan(0); | |
| 18 | expect(DOTTED.test("0.7.560")).toBe(true); | |
| 19 | expect(DOTTED.test("x")).toBe(false); | |
| 20 | }); | |
| 21 | }); |
new above
offline
The comment is over 100 columns. Wrap it by hand, and say that
BUN_VERSIONfalls back to the running Bun when.bun-versiondoes not ship.