Public: anyone can read this; the crew can change it.
Self-deploy, step 1: `bun src/main.ts --preflight` proves a tree can boot: config parses, migrations dry-run on a copy of the database, then exits 0 or 1 with one line #1670
scout/forge:scout/1661-self-deploy-step-1-bun-src-main-ts-prefl → main 1 commit fork
4 files
changed +191
−0
scripts/gate.ts +1
| @@ -54,6 +54,7 @@ export const GATE: readonly string[] = [ | ||
| 54 | 54 | "test/refresh-meta.test.ts", |
| 55 | 55 | "test/restore-card.test.ts", |
| 56 | 56 | "test/recovery-doc.test.ts", |
| 57 | "test/release-preflight.test.ts", | |
| 57 | 58 | "test/returns.test.ts", |
| 58 | 59 | "test/review-route.test.ts", |
| 59 | 60 | "test/review-lines.test.ts", |
src/kernel/preflight.ts added +92
| @@ -0,0 +1,92 @@ | ||
| 1 | /** | |
| 2 | * Can this tree boot? `bun src/main.ts --preflight` asks without starting | |
| 3 | * anything: the configuration parses, and every migration runs on a copy of | |
| 4 | * the database. One line out, exit 0 or 1. No socket is opened and the live | |
| 5 | * database is never opened — only read as bytes. | |
| 6 | * | |
| 7 | * The copy lives in a directory of its own under the data directory (the | |
| 8 | * same disk, so a database that fits fits twice) and is removed in a | |
| 9 | * `finally`, whatever happened. | |
| 10 | */ | |
| 11 | ||
| 12 | import { Database } from "bun:sqlite"; | |
| 13 | import { copyFileSync, existsSync, mkdirSync, mkdtempSync, rmSync } from "node:fs"; | |
| 14 | import { join } from "node:path"; | |
| 15 | import { parseCidrs } from "./cidr.ts"; | |
| 16 | import { dataHomeRefusal } from "./data_home.ts"; | |
| 17 | import { applyPragmas } from "./db.ts"; | |
| 18 | import { migrate } from "./migrate.ts"; | |
| 19 | ||
| 20 | export type Preflight = { ok: boolean; line: string }; | |
| 21 | ||
| 22 | /** Where this forge keeps its state: the same answer boot gives. */ | |
| 23 | export function dataDirOf(env: Record<string, string | undefined>): string { | |
| 24 | const hosted = (env.FORGE_APP_URL ?? "").trim() !== ""; | |
| 25 | return env.FORGE_DATA ?? (hosted ? env.FORGE_APP_DATA : undefined) ?? "data"; | |
| 26 | } | |
| 27 | ||
| 28 | /** Why the configuration will not boot, in one line; `null` when it will. */ | |
| 29 | export function configProblem( | |
| 30 | env: Record<string, string | undefined>, | |
| 31 | treeDir: string, | |
| 32 | ): string | null { | |
| 33 | const refusal = dataHomeRefusal(dataDirOf(env), treeDir); | |
| 34 | if (refusal !== null) return refusal; | |
| 35 | const proxies = (env.FORGE_TRUSTED_PROXIES ?? "") | |
| 36 | .split(",") | |
| 37 | .map((s) => s.trim()) | |
| 38 | .filter((s) => s !== ""); | |
| 39 | const parsed = parseCidrs(proxies.join(",")); | |
| 40 | if (!parsed.ok) | |
| 41 | return `FORGE_TRUSTED_PROXIES: "${parsed.error}" is not an address or range`; | |
| 42 | const hosted = (env.FORGE_APP_URL ?? "").trim(); | |
| 43 | const url = hosted !== "" ? hosted : (env.FORGE_URL ?? "").trim(); | |
| 44 | if (url !== "") { | |
| 45 | try { | |
| 46 | new URL(url); | |
| 47 | } catch { | |
| 48 | return `${hosted !== "" ? "FORGE_APP_URL" : "FORGE_URL"}: "${url}" is not a URL`; | |
| 49 | } | |
| 50 | } | |
| 51 | return null; | |
| 52 | } | |
| 53 | ||
| 54 | export function preflight( | |
| 55 | env: Record<string, string | undefined>, | |
| 56 | treeDir: string, | |
| 57 | migrations: readonly string[], | |
| 58 | ): Preflight { | |
| 59 | const bad = configProblem(env, treeDir); | |
| 60 | if (bad !== null) return { ok: false, line: `preflight failed: ${bad}` }; | |
| 61 | ||
| 62 | const dataDir = dataDirOf(env); | |
| 63 | let scratch: string | undefined; | |
| 64 | try { | |
| 65 | mkdirSync(dataDir, { recursive: true }); | |
| 66 | scratch = mkdtempSync(join(dataDir, "preflight-")); | |
| 67 | const live = join(dataDir, "forge.db"); | |
| 68 | const copy = join(scratch, "forge.db"); | |
| 69 | // The write-ahead log holds commits the main file does not yet; the copy | |
| 70 | // needs both to be the database the forge would open. | |
| 71 | for (const suffix of ["", "-wal"]) | |
| 72 | if (existsSync(live + suffix)) copyFileSync(live + suffix, copy + suffix); | |
| 73 | const db = new Database(copy, { create: true, strict: true }); | |
| 74 | try { | |
| 75 | applyPragmas(db); | |
| 76 | migrate(db, migrations); | |
| 77 | } finally { | |
| 78 | db.close(); | |
| 79 | } | |
| 80 | return { | |
| 81 | ok: true, | |
| 82 | line: `preflight ok: ${migrations.length} migrations apply to a copy of the database`, | |
| 83 | }; | |
| 84 | } catch (error) { | |
| 85 | const message = (error instanceof Error ? error.message : String(error)) | |
| 86 | .replace(/\s+/g, " ") | |
| 87 | .trim(); | |
| 88 | return { ok: false, line: `preflight failed: ${message}` }; | |
| 89 | } finally { | |
| 90 | if (scratch !== undefined) rmSync(scratch, { recursive: true, force: true }); | |
| 91 | } | |
| 92 | } |
src/main.ts +12
| @@ -52,6 +52,18 @@ if (process.argv[2] === "hook") { | ||
| 52 | 52 | await runHookClient(name); |
| 53 | 53 | } |
| 54 | 54 | |
| 55 | // `--preflight` asks whether this tree can boot and answers in one line: | |
| 56 | // config parsed, migrations run on a copy of the database. No socket, no | |
| 57 | // timers, and the live database is never opened. | |
| 58 | if (process.argv.includes("--preflight")) { | |
| 59 | const { migrations } = await import("./core/schema.ts"); | |
| 60 | const { preflight } = await import("./kernel/preflight.ts"); | |
| 61 | const result = preflight(process.env, import.meta.dir, migrations); | |
| 62 | // CLI answer on the stream a person or a script reads. | |
| 63 | (result.ok ? console.log : console.error)(result.line); | |
| 64 | process.exit(result.ok ? 0 : 1); | |
| 65 | } | |
| 66 | ||
| 55 | 67 | // A forge can be an app of a forge (docs/APPS.md, "The forge as an app of |
| 56 | 68 | // itself"). The host's `FORGE_APP_*` say where this one lives and what it |
| 57 | 69 | // answers to; `FORGE_URL` there names the host, not this forge. Hosted, the |
test/release-preflight.test.ts added +86
| @@ -0,0 +1,86 @@ | ||
| 1 | /** | |
| 2 | * `bun src/main.ts --preflight` (#1661): a tree that can boot says so in one | |
| 3 | * line and exits 0; one whose migration breaks names the migration and exits | |
| 4 | * 1; either way the live database is not touched and no copy is left behind. | |
| 5 | */ | |
| 6 | ||
| 7 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 8 | import { mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync } from "node:fs"; | |
| 9 | import { tmpdir } from "node:os"; | |
| 10 | import { join } from "node:path"; | |
| 11 | import { migrations } from "../src/core/schema.ts"; | |
| 12 | import { openDb } from "../src/kernel/db.ts"; | |
| 13 | import { preflight } from "../src/kernel/preflight.ts"; | |
| 14 | ||
| 15 | const tree = join(import.meta.dir, "..", "src"); | |
| 16 | let dir: string; | |
| 17 | ||
| 18 | beforeAll(() => { | |
| 19 | dir = mkdtempSync(join(tmpdir(), "preflight-")); | |
| 20 | }); | |
| 21 | afterAll(() => rmSync(dir, { recursive: true, force: true })); | |
| 22 | ||
| 23 | function liveDb(name: string, upTo: readonly string[]): string { | |
| 24 | const data = join(dir, name); | |
| 25 | mkdirSync(data); | |
| 26 | openDb(join(data, "forge.db"), upTo).close(); | |
| 27 | return data; | |
| 28 | } | |
| 29 | ||
| 30 | function run(data: string) { | |
| 31 | const p = Bun.spawnSync(["bun", join(tree, "main.ts"), "--preflight"], { | |
| 32 | env: { ...process.env, FORGE_DATA: data, FORGE_APP_URL: "" }, | |
| 33 | stdout: "pipe", | |
| 34 | stderr: "pipe", | |
| 35 | }); | |
| 36 | return { | |
| 37 | code: p.exitCode, | |
| 38 | out: p.stdout.toString() + p.stderr.toString(), | |
| 39 | }; | |
| 40 | } | |
| 41 | ||
| 42 | describe("--preflight", () => { | |
| 43 | test("a good tree passes with one line, over a database one migration behind", () => { | |
| 44 | const data = liveDb("behind", migrations.slice(0, -1)); | |
| 45 | const { code, out } = run(data); | |
| 46 | expect(code).toBe(0); | |
| 47 | expect(out.trim().split("\n")).toHaveLength(1); | |
| 48 | expect(out).toContain("preflight ok"); | |
| 49 | }); | |
| 50 | ||
| 51 | test("a good tree passes with no database at all", () => { | |
| 52 | const { code } = run(join(dir, "fresh")); | |
| 53 | expect(code).toBe(0); | |
| 54 | }); | |
| 55 | ||
| 56 | test("a broken migration fails and is named", () => { | |
| 57 | const data = liveDb("broken", migrations); | |
| 58 | const bad = [...migrations, "CREATE TABLE ok_one (id INTEGER);", "ALTER TABLE nowhere ADD COLUMN x TEXT;"]; | |
| 59 | const result = preflight({ FORGE_DATA: data }, tree, bad); | |
| 60 | expect(result.ok).toBe(false); | |
| 61 | expect(result.line).toContain(`migration ${bad.length} failed`); | |
| 62 | expect(result.line.split("\n")).toHaveLength(1); | |
| 63 | }); | |
| 64 | ||
| 65 | test("the live database is byte-identical afterwards and no copy remains", () => { | |
| 66 | const data = liveDb("bytes", migrations.slice(0, -1)); | |
| 67 | const before = readFileSync(join(data, "forge.db")); | |
| 68 | const files = readdirSync(data).sort(); | |
| 69 | expect(run(data).code).toBe(0); | |
| 70 | const bad = [...migrations, "SELECT * FROM nowhere;"]; | |
| 71 | expect(preflight({ FORGE_DATA: data }, tree, bad).ok).toBe(false); | |
| 72 | expect(Buffer.compare(readFileSync(join(data, "forge.db")), before)).toBe(0); | |
| 73 | expect(readdirSync(data).sort()).toEqual(files); | |
| 74 | }); | |
| 75 | ||
| 76 | test("a configuration that will not parse fails before any database work", () => { | |
| 77 | const result = preflight( | |
| 78 | { FORGE_DATA: join(dir, "cfg"), FORGE_TRUSTED_PROXIES: "not-an-address" }, | |
| 79 | tree, | |
| 80 | migrations, | |
| 81 | ); | |
| 82 | expect(result.ok).toBe(false); | |
| 83 | expect(result.line).toContain("FORGE_TRUSTED_PROXIES"); | |
| 84 | expect(readdirSync(dir)).not.toContain("cfg"); | |
| 85 | }); | |
| 86 | }); |
new above
offline