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

Merged opened this in answer to

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

Merged

scout/forge:scout/16326-a-wrong-bun-says-so-in-one-line-and-the → main 0 commits fork 2 threads · 2 unresolved

  • Merged. scout/16326-a-wrong-bun-says-so-in-one-line-and-the was merged into main as b0fffa4.

Review

Sign in to review this change.
19 files changed +153 −49
.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
6363
6464 ## How to work
6565
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
6668 ```sh
6769 bun test # full suite — must stay green
6870 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 \
1515 && npm install -g @anthropic-ai/claude-code \
1616 && npm cache clean --force
1717 WORKDIR /app
18 COPY package.json tsconfig.json ./
18 COPY package.json tsconfig.json .bun-version ./
1919 COPY src ./src
2020 # The forge's documentation: /docs and the terminal list docs/*.md and docs/features/*.md from the
2121 # release's checkout (src/view/docs.ts), and the crew is briefed from docs/PRINCIPLES.md and
package.json +3
@@ -3,6 +3,9 @@
33 "version": "0.0.0",
44 "private": true,
55 "type": "module",
6 "engines": {
7 "bun": ">=1.4.4"
8 },
69 "scripts": {
710 "dev": "bun run --watch src/main.ts",
811 "start": "bun run src/main.ts",
scripts/cut-release.ts +3 −2
@@ -11,7 +11,8 @@
1111
1212 import { migrationFiles, NUMBERED } from "../src/core/schema.ts";
1313 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";
1516
1617 const VERSION = /^\d+\.\d+\.\d+$/;
1718 const [version, flag] = process.argv.slice(2);
@@ -21,7 +22,7 @@ if (version === undefined || !VERSION.test(version) || (flag !== undefined && fl
2122 }
2223 const root = `${import.meta.dir}/..`;
2324 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) {
2526 console.error(`cut-release: ${version} is not newer than ${newest.version}, the newest release`);
2627 process.exit(1);
2728 }
src/cloud/machine.ts +5 −3
@@ -18,6 +18,7 @@
1818 * a failure half-way leaves a row a retry can finish, never an orphan.
1919 */
2020
21 import { BUN_PIN } from "../kernel/bun_pin.ts";
2122 import type { Fetch } from "./providers.ts";
2223 import { firstAdminSeed, readSeed } from "../core/first_admin.ts";
2324 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";
2526 import { BUN_HOME, CHECKOUT, hardenEarly, hardenFiles, hardenHead, hardenLate, mount, proxyStore, serviceUnit } from "./harden.ts";
2627
2728 /**
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

The comment is over 100 columns. Wrap it by hand, and say that BUN_VERSION falls back to the running Bun when .bun-version does not ship.

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.
3032 */
31 export const BUN_VERSION = "1.4.4";
33 export const BUN_VERSION = BUN_PIN ?? Bun.version;
3234
3335 /** The name of the volume holding a forge's data, the same on every cloud. */
3436 export const volumeName = (name: string) => `forge-${name}-data`;
src/keeper/by_hand.ts +6 −17
@@ -10,22 +10,11 @@
1010 */
1111 import type { Database } from "bun:sqlite";
1212 import { running } from "../kernel/version.ts";
13 import { compareVersions, DOTTED } from "../kernel/versions.ts";
1314
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;
2918 }
3019
3120 /** The version recorded, or undefined when this boot records nothing. */
@@ -35,12 +24,12 @@ export function recordByHand(
3524 now: Date = new Date(),
3625 ): string | undefined {
3726 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;
3928 const served = db
4029 .query<{ version: string }, []>(`SELECT version FROM keeper_release WHERE live_at IS NOT NULL AND stage != 'rolled_back'`)
4130 .all();
4231 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;
4433 const at = now.toISOString();
4534 const made = db
4635 .query(
src/keeper/wire.ts +2 −1
@@ -29,7 +29,8 @@ import { ownProbeHeaders } from "../kernel/probe.ts";
2929 import { bootStall } from "../perf/live.ts";
3030 import { writePause } from "./pause.ts";
3131 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";
3334 import { checkoutRelease, preflightRelease } from "../release/checkout.ts";
3435 import { handover } from "../release/swap.ts";
3536 import { runningChildren } from "../boards/sampler.ts";
src/kernel/bun_guard.ts added +12
@@ -0,0 +1,12 @@
1 /**
2 * Imported first by `src/main.ts` and the test preload: a wrong Bun stops here, in one sentence.
3 */
4
5 import { requireBun } from "./bun_pin.ts";
6
7 try {
8 requireBun();
9 } catch (e) {
10 console.error((e as Error).message);
11 process.exit(1);
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). */
outdated

Duplicates compareVersions in src/keeper/by_hand.ts and src/release/shipped.ts. Move the one comparison into src/kernel and have both of those import it, deleting their copies in this pull.

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
@@ -4,6 +4,7 @@
44 * environment-only — a handful of variables, all with sane defaults.
55 */
66
7 import "./kernel/bun_guard.ts";
78 import { join } from "node:path";
89 import { sweepEntitlements } from "./billing/sync.ts";
910 import { sweepDesk } from "./actions/desk.ts";
src/perf/baseline.ts +3 −3
@@ -3,7 +3,7 @@
33 * File names in, the newest older one out; `bench/load.ts` does the judging.
44 */
55
6 import { compareVersions } from "../release/shipped.ts";
6 import { compareVersions } from "../kernel/versions.ts";
77
88 /**
99 * 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
2525 .map((f) => KEPT.exec(f))
2626 .filter((m): m is RegExpExecArray => m !== null && (m[2] === undefined) === (cache === "edge"))
2727 .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));
3030 const newest = older[0];
3131 return newest === undefined ? undefined : resultFile(newest, cache);
3232 }
src/release/shipped.ts +2 −14
@@ -17,6 +17,7 @@
1717 import before from "../core/releases-before-tags.json" with { type: "json" };
1818 import { runGit } from "../git/env.ts";
1919 import { err, ok, type Result } from "../kernel/result.ts";
20 import { compareVersions } from "../kernel/versions.ts";
2021
2122 export interface Shipped {
2223 readonly version: string;
@@ -55,25 +56,12 @@ export function parseRecord(message: string): Omit<Shipped, "at"> | undefined {
5556 return { version, migrations: migrations as number, ids: ids as string[] };
5657 }
5758
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
7159 /** The frozen releases and the tagged ones as one list, newest first; a tag wins over a frozen entry of its version. */
7260 export function mergeShipped(frozen: readonly Shipped[], tagged: readonly Shipped[]): Shipped[] {
7361 const byVersion = new Map<string, Shipped>();
7462 for (const r of frozen) byVersion.set(r.version, r);
7563 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));
7765 }
7866
7967 /** 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";
1515 import type { Repo } from "../src/core/repos.ts";
1616 import { createUser, type User } from "../src/core/users.ts";
1717 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";
1919 import { listKeeperReleases } from "../src/keeper/record.ts";
2020
2121 const H = 3_600_000;
@@ -150,7 +150,5 @@ describe("estimates: done, reopens, by-hand releases", () => {
150150 expect(recordByHand(app.db, { FORGE_VERSION: "dev", FORGE_COMMIT: "1234567" })).toBeUndefined();
151151 expect(recordByHand(app.db, { FORGE_VERSION: "0.7.561" })).toBeUndefined();
152152 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();
155153 });
156154 });
test/helpers/preload.ts +1
@@ -3,6 +3,7 @@
33 * the forge. Only what the whole suite must agree on belongs here.
44 */
55
6 import "../../src/kernel/bun_guard.ts";
67 import { provideCpusForTest } from "../../src/core/capacity.ts";
78 import { provideMirrorOutboundForTest } from "../../src/origins/git-mirror.ts";
89 import { installCheapHash } from "./cheap-hash.ts";
test/release-shipped.test.ts −5
@@ -10,7 +10,6 @@ import { join } from "node:path";
1010 import { migrationFiles, NUMBERED } from "../src/core/schema.ts";
1111 import { releasesBehind } from "../src/spawn/heartbeat.ts";
1212 import {
13 compareVersions,
1413 mergeShipped,
1514 parseRecord,
1615 readTagged,
@@ -75,10 +74,6 @@ describe("a release record", () => {
7574 expect(parseRecord(JSON.stringify(record))).toEqual(record);
7675 });
7776
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 });
8277 });
8378
8479 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 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs