Public: anyone can read this; the crew can change it.
Recovery, part 1: a one-page notecard whose commands the drill runs, and a Backups line that says whether the forge is recoverable now #1990
scout/forge:scout/1986-recovery-part-1-a-one-page-notecard-whos → main 3 commits fork 2 threads · 2 unresolved
16 files
changed +387
−35
docs/RESTORE.md +20 −9
| @@ -18,18 +18,29 @@ Print this. Keep it somewhere that is not the forge. | ||
| 18 | 18 | Replace `<store-dir>`, `<data-dir>` and `<passphrase>`. A line ending |
| 19 | 19 | `# network` needs the network and `# boot` starts the forge and does not |
| 20 | 20 | return; `test/restore-card.test.ts` runs every other line against a test |
| 21 | snapshot, so this card cannot drift from the command. | |
| 21 | snapshot, and holds line 3 to the one `src/snapshot/drill.ts` runs every | |
| 22 | week, so this card cannot drift from the command. | |
| 22 | 23 | |
| 23 | 1. `git clone https://forge.open-platform.sh/trevato/forge.git forge && cd forge # network` | |
| 24 | 2. `bun install # network` | |
| 25 | 3. `export FORGE_SNAPSHOT_DIR=<store-dir>` | |
| 26 | 4. `export FORGE_SNAPSHOT_PASSPHRASE=<passphrase>` | |
| 27 | 5. `bun run src/snapshot/restore.ts latest <data-dir>` | |
| 28 | 6. `test -s <data-dir>/forge.db` | |
| 29 | 7. `FORGE_DATA=<data-dir> bun run src/main.ts # boot` | |
| 24 | 1. `command -v git bun >/dev/null || curl -fsSL https://bun.sh/install | bash; git clone https://forge.open-platform.sh/trevato/forge.git forge && cd forge && bun install # network` | |
| 25 | 2. `export FORGE_SNAPSHOT_DIR=<store-dir> FORGE_SNAPSHOT_PASSPHRASE=<passphrase>` | |
| 26 | 3. `bun run src/snapshot/restore.ts latest <data-dir> && test -s <data-dir>/forge.db` | |
| 27 | 4. `FORGE_DATA=<data-dir> bun run src/main.ts # boot` | |
| 28 | 5. `echo "point forge.open-platform.sh (A/AAAA) at this machine" # network` | |
| 29 | ||
| 30 | Line 1 installs bun and git first if they are missing (Debian: `apt-get install -y git`). | |
| 31 | Line 5 is the last step by hand: at your DNS provider, point the forge's name | |
| 32 | at this machine. | |
| 33 | ||
| 34 | ## Where the secrets live | |
| 35 | ||
| 36 | - **Passphrase**: in the owner's password manager and on a paper copy in the | |
| 37 | safe, never in the bucket and never in the repository. | |
| 38 | - **Bucket credentials** (`S3_ACCESS_KEY`, `S3_SECRET_KEY`): in the same | |
| 39 | place. For a bucket, export `S3_ENDPOINT`, `S3_BUCKET`, `S3_ACCESS_KEY`, | |
| 40 | `S3_SECRET_KEY` and `S3_REGION` in place of `FORGE_SNAPSHOT_DIR` in line 2. | |
| 30 | 41 | |
| 31 | 42 | The restore prints what it brought back. A wrong passphrase refuses before |
| 32 | 43 | writing anything; a missing one restores without the key and lists the |
| 33 | 44 | sealed values to set again. To pick an older snapshot, put its name |
| 34 | (`2026-09-22T08-00-00Z`) where line 5 says `latest`. Details: | |
| 45 | (`2026-09-22T08-00-00Z`) where line 3 says `latest`. Details: | |
| 35 | 46 | [](SNAPSHOT.md). |
src/core/migrations/20261016000000-drill-counts.ts added +9
| @@ -0,0 +1,9 @@ | ||
| 1 | // The drill counts what a restore brought back (#1986): users, pulls and | |
| 2 | // apps beside repositories and issues, and whether a sealed value opened. | |
| 3 | // Rows from before have zeros and no word on the seal. | |
| 4 | export default ` | |
| 5 | ALTER TABLE drill ADD COLUMN users INTEGER NOT NULL DEFAULT 0; | |
| 6 | ALTER TABLE drill ADD COLUMN pulls INTEGER NOT NULL DEFAULT 0; | |
| 7 | ALTER TABLE drill ADD COLUMN apps INTEGER NOT NULL DEFAULT 0; | |
| 8 | ALTER TABLE drill ADD COLUMN sealed_opens INTEGER; | |
| 9 | `; |
src/snapshot/drill.ts +22 −6
| @@ -14,12 +14,20 @@ import { mkdirSync, mkdtempSync, rmSync } from "node:fs"; | ||
| 14 | 14 | import { join } from "node:path"; |
| 15 | 15 | import { bootRestored } from "./boot.ts"; |
| 16 | 16 | import { forgetKey, open } from "../kernel/secret.ts"; |
| 17 | import { restoreSnapshot } from "./restore.ts"; | |
| 17 | import { restoreLatest } from "./restore.ts"; | |
| 18 | 18 | import { listSnapshots } from "./snapshot.ts"; |
| 19 | 19 | import type { Store } from "./store.ts"; |
| 20 | 20 | |
| 21 | 21 | export const DRILL_TIMEOUT_MS = 10 * 60_000; |
| 22 | 22 | |
| 23 | export interface DrillCounts { | |
| 24 | readonly repositories: number; | |
| 25 | readonly users: number; | |
| 26 | readonly issues: number; | |
| 27 | readonly pulls: number; | |
| 28 | readonly apps: number; | |
| 29 | } | |
| 30 | ||
| 23 | 31 | export type DrillStep = "fetch" | "restore" | "open" | "count" | "unseal" | "health"; |
| 24 | 32 | |
| 25 | 33 | export interface DrillDeps { |
| @@ -30,7 +38,7 @@ export interface DrillDeps { | ||
| 30 | 38 | readonly timeoutMs?: number | undefined; |
| 31 | 39 | readonly now?: (() => Date) | undefined; |
| 32 | 40 | /** Test seam: the restore itself. */ |
| 33 | readonly restore?: typeof restoreSnapshot | undefined; | |
| 41 | readonly restore?: typeof restoreLatest | undefined; | |
| 34 | 42 | /** Test seam: boot a data directory on a side port and say whether it is healthy. */ |
| 35 | 43 | readonly boot?: typeof bootRestored | undefined; |
| 36 | 44 | } |
| @@ -39,7 +47,9 @@ export interface DrillResult { | ||
| 39 | 47 | readonly at: string; |
| 40 | 48 | readonly snapshotId: string | null; |
| 41 | 49 | readonly ms: number; |
| 42 | readonly counts: { readonly repositories: number; readonly issues: number }; | |
| 50 | readonly counts: DrillCounts; | |
| 51 | /** Whether one sealed value opened with the restored key; null when nothing is sealed. */ | |
| 52 | readonly sealedOpens: boolean | null; | |
| 43 | 53 | /** The side-booted forge's word; "unreached" when an earlier step failed. */ |
| 44 | 54 | readonly health: "ok" | "failed" | "unreached"; |
| 45 | 55 | readonly verdict: "green" | "red"; |
| @@ -63,8 +73,9 @@ export async function runDrill(deps: DrillDeps): Promise<DrillResult> { | ||
| 63 | 73 | const started = performance.now(); |
| 64 | 74 | const at = clock().toISOString(); |
| 65 | 75 | const timeoutMs = deps.timeoutMs ?? DRILL_TIMEOUT_MS; |
| 66 | const restore = deps.restore ?? restoreSnapshot; | |
| 67 | const counts = { repositories: 0, issues: 0 }; | |
| 76 | const restore = deps.restore ?? restoreLatest; | |
| 77 | const counts = { repositories: 0, users: 0, issues: 0, pulls: 0, apps: 0 }; | |
| 78 | let sealedOpens: boolean | null = null; | |
| 68 | 79 | let snapshotId: string | null = null; |
| 69 | 80 | let step: DrillStep = "fetch"; |
| 70 | 81 | let scratch: string | undefined; |
| @@ -84,7 +95,7 @@ export async function runDrill(deps: DrillDeps): Promise<DrillResult> { | ||
| 84 | 95 | scratch = mkdtempSync(join(parent, "restore-")); |
| 85 | 96 | const target = join(scratch, "data"); |
| 86 | 97 | await within( |
| 87 | restore(deps.store, newest.name, target, { passphrase: deps.passphrase }), | |
| 98 | restore(deps.store, target, { passphrase: deps.passphrase }), | |
| 88 | 99 | left(), |
| 89 | 100 | ); |
| 90 | 101 | |
| @@ -98,7 +109,10 @@ export async function runDrill(deps: DrillDeps): Promise<DrillResult> { | ||
| 98 | 109 | const n = (sql: string): number => |
| 99 | 110 | db.query<{ n: number }, []>(sql).get()?.n ?? 0; |
| 100 | 111 | counts.repositories = n("SELECT count(*) AS n FROM repo"); |
| 112 | counts.users = n("SELECT count(*) AS n FROM user"); | |
| 101 | 113 | counts.issues = n("SELECT count(*) AS n FROM issue"); |
| 114 | counts.pulls = n("SELECT count(*) AS n FROM pull"); | |
| 115 | counts.apps = n("SELECT count(*) AS n FROM app"); | |
| 102 | 116 | |
| 103 | 117 | step = "unseal"; |
| 104 | 118 | const secret = db |
| @@ -121,6 +135,7 @@ export async function runDrill(deps: DrillDeps): Promise<DrillResult> { | ||
| 121 | 135 | : undefined; |
| 122 | 136 | if (sealed !== undefined) { |
| 123 | 137 | const opened = open(target, sealed.value, sealed.aad); |
| 138 | sealedOpens = opened.ok; | |
| 124 | 139 | if (!opened.ok) |
| 125 | 140 | throw new Error( |
| 126 | 141 | "a sealed value does not open with the restored key", |
| @@ -159,6 +174,7 @@ export async function runDrill(deps: DrillDeps): Promise<DrillResult> { | ||
| 159 | 174 | snapshotId, |
| 160 | 175 | ms: Math.round(performance.now() - started), |
| 161 | 176 | counts, |
| 177 | sealedOpens, | |
| 162 | 178 | health, |
| 163 | 179 | verdict: failedStep === null ? "green" : "red", |
| 164 | 180 | failedStep, |
src/snapshot/drills.ts +18 −3
| @@ -13,6 +13,10 @@ interface Row { | ||
| 13 | 13 | ms: number; |
| 14 | 14 | repositories: number; |
| 15 | 15 | issues: number; |
| 16 | users: number; | |
| 17 | pulls: number; | |
| 18 | apps: number; | |
| 19 | sealed_opens: number | null; | |
| 16 | 20 | verdict: "green" | "red"; |
| 17 | 21 | failed_step: DrillStep | null; |
| 18 | 22 | reason: string | null; |
| @@ -20,14 +24,18 @@ interface Row { | ||
| 20 | 24 | |
| 21 | 25 | export function recordDrill(db: Database, result: DrillResult): void { |
| 22 | 26 | db.query( |
| 23 | `INSERT INTO drill (at, snapshot_id, ms, repositories, issues, verdict, failed_step, reason) | |
| 24 | VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, | |
| 27 | `INSERT INTO drill (at, snapshot_id, ms, repositories, issues, users, pulls, apps, sealed_opens, verdict, failed_step, reason) | |
| 28 | VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, | |
| 25 | 29 | ).run( |
| 26 | 30 | result.at, |
| 27 | 31 | result.snapshotId, |
| 28 | 32 | result.ms, |
| 29 | 33 | result.counts.repositories, |
| 30 | 34 | result.counts.issues, |
| 35 | result.counts.users, | |
| 36 | result.counts.pulls, | |
| 37 | result.counts.apps, | |
| 38 | result.sealedOpens === null ? null : result.sealedOpens ? 1 : 0, | |
| 31 | 39 | result.verdict, |
| 32 | 40 | result.failedStep, |
| 33 | 41 | result.reason, |
| @@ -45,7 +53,14 @@ export function lastDrill(db: Database): DrillResult | undefined { | ||
| 45 | 53 | at: row.at, |
| 46 | 54 | snapshotId: row.snapshot_id, |
| 47 | 55 | ms: row.ms, |
| 48 | counts: { repositories: row.repositories, issues: row.issues }, | |
| 56 | counts: { | |
| 57 | repositories: row.repositories, | |
| 58 | users: row.users, | |
| 59 | issues: row.issues, | |
| 60 | pulls: row.pulls, | |
| 61 | apps: row.apps, | |
| 62 | }, | |
| 63 | sealedOpens: row.sealed_opens === null ? null : row.sealed_opens === 1, | |
| 49 | 64 | // Not stored: green means health read ok; a red one failed at it or never got there. |
| 50 | 65 | health: |
| 51 | 66 | row.verdict === "green" ? "ok" : row.failed_step === "health" ? "failed" : "unreached", |
src/snapshot/recoverable.ts added +71
| @@ -0,0 +1,71 @@ | ||
| 1 | /** | |
| 2 | * Whether the forge is recoverable now, as plain data for both doors (the | |
| 3 | * Backups page and the terminal): the store answers, the newest snapshot, | |
| 4 | * whether the key travels in it, the last drill. Any part that fails makes | |
| 5 | * the whole not ok. | |
| 6 | */ | |
| 7 | ||
| 8 | import type { Database } from "bun:sqlite"; | |
| 9 | import { lastDrill } from "./drills.ts"; | |
| 10 | import { keyOf, type Manifest } from "./snapshot.ts"; | |
| 11 | ||
| 12 | /** One clause; `at` is an ISO time the surface formats in its own way. */ | |
| 13 | export interface Clause { | |
| 14 | readonly text: string; | |
| 15 | readonly at?: string; | |
| 16 | } | |
| 17 | ||
| 18 | export interface Recoverable { | |
| 19 | readonly ok: boolean; | |
| 20 | readonly clauses: readonly Clause[]; | |
| 21 | } | |
| 22 | ||
| 23 | export function recoverable( | |
| 24 | db: Database, | |
| 25 | snapshots: readonly Manifest[], | |
| 26 | listingError: string | undefined, | |
| 27 | ): Recoverable { | |
| 28 | const d = lastDrill(db); | |
| 29 | const newest = snapshots[0]; | |
| 30 | const carried = newest === undefined ? undefined : keyOf(newest); | |
| 31 | const clauses: Clause[] = [ | |
| 32 | { text: listingError === undefined ? "store reachable" : "store not reachable" }, | |
| 33 | newest === undefined | |
| 34 | ? { text: "no snapshot yet" } | |
| 35 | : { text: "newest snapshot", at: newest.finishedAt }, | |
| 36 | { text: carried === "wrapped" ? "key carried" : "key not carried" }, | |
| 37 | d === undefined | |
| 38 | ? { text: "no drill yet" } | |
| 39 | : { text: `last drill ${d.verdict}`, at: d.at }, | |
| 40 | ]; | |
| 41 | const ok = | |
| 42 | listingError === undefined && | |
| 43 | newest !== undefined && | |
| 44 | carried === "wrapped" && | |
| 45 | d?.verdict === "green"; | |
| 46 | return { ok, clauses }; | |
| 47 | } | |
| 48 | ||
| 49 | /** What the last drill counted, in a clause: "3 repositories, 2 users, …". */ | |
| 50 | export function countsSay(c: { | |
| 51 | readonly repositories: number; | |
| 52 | readonly users: number; | |
| 53 | readonly issues: number; | |
| 54 | readonly pulls: number; | |
| 55 | readonly apps: number; | |
| 56 | }, sealedOpens: boolean | null): string { | |
| 57 | const n = (k: number, one: string, many: string): string => | |
| 58 | `${k} ${k === 1 ? one : many}`; | |
| 59 | return [ | |
| 60 | n(c.repositories, "repository", "repositories"), | |
| 61 | n(c.users, "user", "users"), | |
| 62 | n(c.issues, "issue", "issues"), | |
| 63 | n(c.pulls, "pull", "pulls"), | |
| 64 | n(c.apps, "app", "apps"), | |
| 65 | ].join(", ") + | |
| 66 | (sealedOpens === null | |
| 67 | ? "; nothing sealed to open" | |
| 68 | : sealedOpens | |
| 69 | ? "; a sealed value opens" | |
| 70 | : "; a sealed value does not open"); | |
| 71 | } |
src/snapshot/restore.ts +28 −6
| @@ -174,6 +174,24 @@ export async function restoreSnapshot( | ||
| 174 | 174 | return { manifest, files, repos, key: have, sealedMissing }; |
| 175 | 175 | } |
| 176 | 176 | |
| 177 | /** | |
| 178 | * The line `docs/RESTORE.md` prints for the restore. The CLI below is that | |
| 179 | * line, and the drill restores through `restoreLatest` too, so the card, the | |
| 180 | * command and the drill share one entry point. | |
| 181 | */ | |
| 182 | export const RESTORE_LINE = "bun run src/snapshot/restore.ts latest <data-dir>"; | |
| 183 | ||
| 184 | /** Restore the newest snapshot in the store; throws when it holds none. */ | |
| 185 | export async function restoreLatest( | |
| 186 | store: Store, | |
| 187 | dataDir: string, | |
| 188 | opts: { passphrase?: string | undefined } = {}, | |
| 189 | ): Promise<Restored> { | |
| 190 | const newest = (await listSnapshots(store))[0]; | |
| 191 | if (newest === undefined) throw new Error(`no snapshot in ${store.where}`); | |
| 192 | return restoreSnapshot(store, newest.name, dataDir, opts); | |
| 193 | } | |
| 194 | ||
| 177 | 195 | if (import.meta.main) { |
| 178 | 196 | const [name, dataDir] = process.argv.slice(2); |
| 179 | 197 | if (!name || !dataDir) { |
| @@ -193,15 +211,19 @@ if (import.meta.main) { | ||
| 193 | 211 | ); |
| 194 | 212 | process.exit(2); |
| 195 | 213 | } |
| 196 | const newest = name === "latest" ? (await listSnapshots(store))[0] : undefined; | |
| 197 | if (name === "latest" && newest === undefined) { | |
| 214 | const passphrase = process.env["FORGE_SNAPSHOT_PASSPHRASE"] || undefined; | |
| 215 | let done: Restored; | |
| 216 | try { | |
| 217 | done = | |
| 218 | name === "latest" | |
| 219 | ? await restoreLatest(store, dataDir, { passphrase }) | |
| 220 | : await restoreSnapshot(store, name, dataDir, { passphrase }); | |
| 221 | } catch (error) { | |
| 222 | if (!String(error).includes("no snapshot in")) throw error; | |
| 198 | 223 | // CLI message on stderr for the person at the terminal. |
| 199 | console.error(`no snapshot in ${store.where}`); | |
| 224 | console.error(String(error instanceof Error ? error.message : error)); | |
| 200 | 225 | process.exit(1); |
| 201 | 226 | } |
| 202 | const done = await restoreSnapshot(store, newest?.name ?? name, dataDir, { | |
| 203 | passphrase: process.env["FORGE_SNAPSHOT_PASSPHRASE"] || undefined, | |
| 204 | }); | |
| 205 | 227 | console.log( |
| 206 | 228 | `restored ${done.manifest.name} (forge ${done.manifest.version}): ${done.files} files, ${done.repos} repositories into ${dataDir}`, |
| 207 | 229 | ); |
src/tui/forge.ts +12
| @@ -38,6 +38,7 @@ import { pullScreen } from "./screens/pull.ts"; | ||
| 38 | 38 | import { blobScreen } from "./screens/repo.ts"; |
| 39 | 39 | import { dashboardScreen } from "./screens/dashboard.ts"; |
| 40 | 40 | import { accessScreen } from "./screens/access.ts"; |
| 41 | import { backupLines, backupsScreen } from "./screens/backups.ts"; | |
| 41 | 42 | import { resilienceLines, resilienceScreen } from "./screens/resilience.ts"; |
| 42 | 43 | import { flowLines, flowScreen } from "./screens/flow.ts"; |
| 43 | 44 | import { stalledScreen } from "./screens/stalled.ts"; |
| @@ -248,6 +249,17 @@ export function forgeCommands(session: Session): (ctx: Ctx) => Command[] { | ||
| 248 | 249 | hint: "what outside agents tried", |
| 249 | 250 | run: () => ctx.push(accessScreen(session, ctx)), |
| 250 | 251 | }); |
| 252 | // Whether the forge is recoverable now, as Settings → Backups says it: a site admin's. | |
| 253 | if (session.user?.isAdmin === true && session.user.kind !== "agent") | |
| 254 | commands.push({ | |
| 255 | id: "backups", | |
| 256 | title: "Backups", | |
| 257 | hint: "whether the forge is recoverable now", | |
| 258 | run: () => | |
| 259 | runAsync(ctx, async () => | |
| 260 | ctx.push(backupsScreen(ctx, await backupLines(session))), | |
| 261 | ), | |
| 262 | }); | |
| 251 | 263 | commands.push({ |
| 252 | 264 | id: "family", |
| 253 | 265 | title: "Family", |
src/tui/screens/backups.ts added +64
| @@ -0,0 +1,64 @@ | ||
| 1 | /** | |
| 2 | * The Backups command's dialog, a forge admin's: Settings → Backups' one | |
| 3 | * line ("is the forge recoverable now?") and the last drill's counts, from | |
| 4 | * `snapshot/recoverable.ts`, the same the page reads. Read-only; Enter, Esc | |
| 5 | * or q closes it. | |
| 6 | */ | |
| 7 | ||
| 8 | import { countsSay, recoverable } from "../../snapshot/recoverable.ts"; | |
| 9 | import { lastDrill } from "../../snapshot/drills.ts"; | |
| 10 | import { snapshotsOf } from "../../snapshot/service.ts"; | |
| 11 | import type { Manifest } from "../../snapshot/snapshot.ts"; | |
| 12 | import type { Ctx, Screen } from "../app/app.ts"; | |
| 13 | import { binding } from "../app/keys.ts"; | |
| 14 | import type { Session } from "../session.ts"; | |
| 15 | import { wrap } from "../term/width.ts"; | |
| 16 | import { dialog } from "../ui/widgets.ts"; | |
| 17 | import { ago } from "./common.ts"; | |
| 18 | ||
| 19 | const WIDTH = 64; | |
| 20 | ||
| 21 | export async function backupLines(f: Session): Promise<string[]> { | |
| 22 | const svc = snapshotsOf(f.app); | |
| 23 | if (svc === undefined || svc.store === undefined) | |
| 24 | return wrap("No snapshots are taken: this forge was given nowhere to put them.", WIDTH - 6); | |
| 25 | let snapshots: Manifest[] = []; | |
| 26 | let listing: string | undefined; | |
| 27 | try { | |
| 28 | snapshots = await svc.list(); | |
| 29 | } catch (error) { | |
| 30 | listing = String(error); | |
| 31 | } | |
| 32 | const r = recoverable(f.db, snapshots, listing); | |
| 33 | const d = lastDrill(f.db); | |
| 34 | const said = r.clauses | |
| 35 | .map((c) => (c.at === undefined ? c.text : `${c.text} ${ago(c.at)}`)) | |
| 36 | .join(" · "); | |
| 37 | return [ | |
| 38 | ...wrap(`${r.ok ? "Recoverable" : "Not proven recoverable"}: ${said}`, WIDTH - 6), | |
| 39 | ...(d === undefined || d.verdict !== "green" | |
| 40 | ? [] | |
| 41 | : ["", ...wrap(`Drill restored ${countsSay(d.counts, d.sealedOpens)}.`, WIDTH - 6)]), | |
| 42 | ]; | |
| 43 | } | |
| 44 | ||
| 45 | export function backupsScreen(ctx: Ctx, lines: readonly string[]): Screen { | |
| 46 | return { | |
| 47 | title: "Backups", | |
| 48 | keys: [binding(["enter", "esc", "q"], "close")], | |
| 49 | render(area, s) { | |
| 50 | const buttons = dialog(s, area, ctx.theme.c, { | |
| 51 | title: "Backups", | |
| 52 | body: lines, | |
| 53 | actions: [{ label: "Close" }], | |
| 54 | selected: 0, | |
| 55 | width: WIDTH, | |
| 56 | }); | |
| 57 | buttons.forEach((rect) => ctx.hit(rect, () => ctx.pop())); | |
| 58 | }, | |
| 59 | key(e) { | |
| 60 | if (e.key === "enter" || e.key === "esc" || e.key === "q") ctx.pop(); | |
| 61 | return true; | |
| 62 | }, | |
| 63 | }; | |
| 64 | } |
src/web/pages/backups.ts +20 −2
| @@ -17,7 +17,8 @@ import { | ||
| 17 | 17 | import { forgetMirror, saveMirror } from "../../actions/mirror.ts"; |
| 18 | 18 | import { mirrorSealedSet, mirrorNeed, MIRROR_SEALED } from "../../snapshot/mirror.ts"; |
| 19 | 19 | import { resolveDestination, sealedSet } from "../../snapshot/destination.ts"; |
| 20 | import { keyOf } from "../../snapshot/snapshot.ts"; | |
| 20 | import { keyOf, type Manifest } from "../../snapshot/snapshot.ts"; | |
| 21 | import { countsSay, recoverable } from "../../snapshot/recoverable.ts"; | |
| 21 | 22 | import { lastDrill } from "../../snapshot/drills.ts"; |
| 22 | 23 | import { snapshotsOf } from "../../snapshot/service.ts"; |
| 23 | 24 | import type { Ctx } from "../../http/context.ts"; |
| @@ -98,7 +99,7 @@ function drillLine(ctx: Ctx): Html { | ||
| 98 | 99 | ? html`<p class="backups-drill"> |
| 99 | 100 | ${icon("check")} Last drill ${time(d.at)}: snapshot |
| 100 | 101 | <code>${d.snapshotId ?? ""}</code> restored green in ${d.ms} ms — |
| 101 | ${d.counts.repositories} repositories, ${d.counts.issues} issues. | |
| 102 | ${countsSay(d.counts, d.sealedOpens)}. | |
| 102 | 103 | </p>` |
| 103 | 104 | : html`<p class="backups-error backups-drill"> |
| 104 | 105 | ${icon("alert")} Last drill ${time(d.at)}: snapshot |
| @@ -113,6 +114,22 @@ function drillLine(ctx: Ctx): Html { | ||
| 113 | 114 | </form>`; |
| 114 | 115 | } |
| 115 | 116 | |
| 117 | /** Whether the forge is recoverable now, in one line; any failing part makes it an alert. */ | |
| 118 | function recoverableLine( | |
| 119 | ctx: Ctx, | |
| 120 | snapshots: readonly Manifest[], | |
| 121 | listing: string | undefined, | |
| 122 | ): Html { | |
| 123 | const r = recoverable(ctx.app.db, snapshots, listing); | |
| 124 | return html`<p class="backups-recoverable${r.ok ? "" : " backups-error"}"> | |
| 125 | ${icon(r.ok ? "check" : "alert")} | |
| 126 | ${r.clauses.map( | |
| 127 | (c, i) => | |
| 128 | html`${i ? " · " : ""}${c.text}${c.at === undefined ? "" : html` ${time(c.at)}`}`, | |
| 129 | )} | |
| 130 | </p>`; | |
| 131 | } | |
| 132 | ||
| 116 | 133 | export async function backupsPage(ctx: Ctx): Promise<Response> { |
| 117 | 134 | const denied = requireAdmin(ctx); |
| 118 | 135 | if (denied) return denied; |
| @@ -150,6 +167,7 @@ bun run src/snapshot/restore.ts ${snapshots[0].name} /path/to/empty/data</pre>` | ||
| 150 | 167 | crew's logs — to <code>${svc.store.where}</code>, and keeps the newest |
| 151 | 168 | fourteen. |
| 152 | 169 | </p> |
| 170 | ${recoverableLine(ctx, snapshots, listing)} | |
| 153 | 171 | ${keyNote(svc.wraps, snapshots[0])} |
| 154 | 172 | ${when( |
| 155 | 173 | svc.progress(), |
test/domains.test.ts +1 −1
| @@ -62,7 +62,7 @@ test("a nearly full disk says the state's words", () => { | ||
| 62 | 62 | |
| 63 | 63 | test("a red restore drill says the snapshot store's words", () => { |
| 64 | 64 | recordVerdict(dataDir, { |
| 65 | at: new Date().toISOString(), snapshotId: null, ms: 1, counts: { repositories: 0, issues: 0 }, | |
| 65 | at: new Date().toISOString(), snapshotId: null, ms: 1, counts: { repositories: 0, users: 0, issues: 0, pulls: 0, apps: 0 }, sealedOpens: null, | |
| 66 | 66 | health: "unreached", verdict: "red", failedStep: "fetch", reason: "no bucket", |
| 67 | 67 | }); |
| 68 | 68 | for (const s of said("snapshots")) expect(read()).toContain(s); |
test/drill-board-line.test.ts +1 −1
| @@ -23,7 +23,7 @@ const drill = (at: number, red: boolean): DrillResult => ({ | ||
| 23 | 23 | at: new Date(at).toISOString(), |
| 24 | 24 | snapshotId: "snap-1", |
| 25 | 25 | ms: 5, |
| 26 | counts: { repositories: 1, issues: 1 }, | |
| 26 | counts: { repositories: 1, users: 0, issues: 1, pulls: 0, apps: 0 }, sealedOpens: null, | |
| 27 | 27 | health: red ? "failed" : "ok", |
| 28 | 28 | verdict: red ? "red" : "green", |
| 29 | 29 | failedStep: red ? "unseal" : null, |
test/drill-verdict.test.ts +2 −2
| @@ -134,7 +134,7 @@ describe("the drill's verdict", () => { | ||
| 134 | 134 | at: "2026-09-22T09:00:00Z", |
| 135 | 135 | snapshotId: null, |
| 136 | 136 | ms: 5, |
| 137 | counts: { repositories: 0, issues: 0 }, | |
| 137 | counts: { repositories: 0, users: 0, issues: 0, pulls: 0, apps: 0 }, sealedOpens: null, | |
| 138 | 138 | health: "unreached", |
| 139 | 139 | verdict: "red", |
| 140 | 140 | failedStep: "fetch", |
| @@ -221,7 +221,7 @@ describe("telling the recovery issue", () => { | ||
| 221 | 221 | at: "2026-10-18T09:00:00Z", |
| 222 | 222 | snapshotId: "s", |
| 223 | 223 | ms: 1, |
| 224 | counts: { repositories: 0, issues: 0 }, | |
| 224 | counts: { repositories: 0, users: 0, issues: 0, pulls: 0, apps: 0 }, sealedOpens: null, | |
| 225 | 225 | health: "ok", |
| 226 | 226 | verdict: "green", |
| 227 | 227 | failedStep: null, |
test/restore-card.test.ts +9 −2
| @@ -15,6 +15,7 @@ import { createApp, type App } from "../src/app.ts"; | ||
| 15 | 15 | import { commitToBranch, createRepo } from "../src/actions/repos.ts"; |
| 16 | 16 | import { createUser } from "../src/core/users.ts"; |
| 17 | 17 | import { dirStore } from "../src/snapshot/store.ts"; |
| 18 | import { RESTORE_LINE } from "../src/snapshot/restore.ts"; | |
| 18 | 19 | import { takeSnapshot } from "../src/snapshot/snapshot.ts"; |
| 19 | 20 | |
| 20 | 21 | const PASS = "correct horse battery staple"; |
| @@ -73,11 +74,17 @@ test("only network and boot lines are skipped, and they are the known ones", () | ||
| 73 | 74 | const skipped = lines.filter((l) => /# (network|boot)$/.test(l.text)); |
| 74 | 75 | expect(skipped.map((l) => l.text.replace(/ # (network|boot)$/, "")).sort()).toEqual([ |
| 75 | 76 | "FORGE_DATA=<data-dir> bun run src/main.ts", |
| 76 | "bun install", | |
| 77 | "git clone https://forge.open-platform.sh/trevato/forge.git forge && cd forge", | |
| 77 | 'command -v git bun >/dev/null || curl -fsSL https://bun.sh/install | bash; git clone https://forge.open-platform.sh/trevato/forge.git forge && cd forge && bun install', | |
| 78 | 'echo "point forge.open-platform.sh (A/AAAA) at this machine"', | |
| 78 | 79 | ]); |
| 79 | 80 | }); |
| 80 | 81 | |
| 82 | test("the card's restore line is the one the drill runs", () => { | |
| 83 | expect(lines.some((l) => l.text.startsWith(`${RESTORE_LINE} `))).toBe(true); | |
| 84 | expect(lines.filter((l) => l.text.includes("restore.ts"))).toHaveLength(1); | |
| 85 | expect(RESTORE_LINE).toContain("src/snapshot/restore.ts latest"); | |
| 86 | }); | |
| 87 | ||
| 81 | 88 | test("the card's own lines restore the snapshot into an empty directory", async () => { |
| 82 | 89 | const store = join(root, "store"); |
| 83 | 90 | const data = join(root, "restored"); |
test/snapshot-drill-page.test.ts +8 −2
| @@ -62,7 +62,7 @@ describe("the drill on the Backups page", () => { | ||
| 62 | 62 | at: new Date().toISOString(), |
| 63 | 63 | snapshotId: "snap-stub-1", |
| 64 | 64 | ms: 1234, |
| 65 | counts: { repositories: 7, issues: 42 }, | |
| 65 | counts: { repositories: 7, users: 3, issues: 42, pulls: 5, apps: 2 }, sealedOpens: true, | |
| 66 | 66 | health: verdict === "green" ? "ok" : "unreached", |
| 67 | 67 | verdict, |
| 68 | 68 | failedStep: verdict === "green" ? null : "unseal", |
| @@ -93,12 +93,18 @@ describe("the drill on the Backups page", () => { | ||
| 93 | 93 | const before = await page(cookie); |
| 94 | 94 | expect(before).toContain("No restore drill has run yet"); |
| 95 | 95 | expect(before).toContain("Run a drill"); |
| 96 | // The one line: store, newest snapshot, key, last drill. | |
| 97 | expect(before).toContain("store reachable"); | |
| 98 | expect(before).toContain("no snapshot yet"); | |
| 99 | expect(before).toContain("key not carried"); | |
| 100 | expect(before).toContain("no drill yet"); | |
| 96 | 101 | expect((await post(cookie)).status).toBe(303); |
| 97 | 102 | expect(ran).toBe(1); |
| 98 | 103 | const html = await page(cookie); |
| 99 | 104 | expect(html).toContain("snap-stub-1"); |
| 100 | expect(html).toContain("7 repositories, 42 issues"); | |
| 105 | expect(html).toContain("7 repositories, 3 users, 42 issues, 5 pulls, 2 apps; a sealed value opens"); | |
| 101 | 106 | expect(html).toContain("1234 ms"); |
| 107 | expect(html).toContain("last drill green"); | |
| 102 | 108 | expect(app.db.query("SELECT count(*) AS n FROM drill").get()).toEqual({ n: 1 }); |
| 103 | 109 | }); |
| 104 | 110 |
test/snapshot-drill.test.ts +18 −1
| @@ -14,6 +14,7 @@ import { createApp, type App } from "../src/app.ts"; | ||
| 14 | 14 | import { createIssue } from "../src/core/issues.ts"; |
| 15 | 15 | import { createUser } from "../src/core/users.ts"; |
| 16 | 16 | import { runDrill } from "../src/snapshot/drill.ts"; |
| 17 | import { RESTORE_LINE, restoreLatest } from "../src/snapshot/restore.ts"; | |
| 17 | 18 | import { takeSnapshot } from "../src/snapshot/snapshot.ts"; |
| 18 | 19 | import { dirStore } from "../src/snapshot/store.ts"; |
| 19 | 20 | |
| @@ -98,11 +99,27 @@ describe("the restore drill", () => { | ||
| 98 | 99 | expect(r.verdict).toBe("green"); |
| 99 | 100 | expect(r.failedStep).toBeNull(); |
| 100 | 101 | expect(r.snapshotId).toBe("2026-09-22T08-00-00Z"); |
| 101 | expect(r.counts).toEqual({ repositories: 1, issues: 2 }); | |
| 102 | expect(r.counts).toEqual({ repositories: 1, users: 1, issues: 2, pulls: 0, apps: 1 }); | |
| 103 | expect(r.sealedOpens).toBe(true); | |
| 102 | 104 | expect(r.ms).toBeGreaterThanOrEqual(0); |
| 103 | 105 | expect(scratchLeft()).toEqual([]); |
| 104 | 106 | }); |
| 105 | 107 | |
| 108 | test("the drill restores through restoreLatest, the card's entry point", async () => { | |
| 109 | const seen: string[] = []; | |
| 110 | const r = await runDrill( | |
| 111 | deps({ | |
| 112 | restore: (st: unknown, target: string, opts: unknown) => { | |
| 113 | seen.push(target); | |
| 114 | return restoreLatest(st as never, target, opts as never); | |
| 115 | }, | |
| 116 | }), | |
| 117 | ); | |
| 118 | expect(r.verdict).toBe("green"); | |
| 119 | expect(seen).toHaveLength(1); | |
| 120 | expect(RESTORE_LINE).toContain("restore.ts latest"); | |
| 121 | }); | |
| 122 | ||
| 106 | 123 | test("a sealed value that cannot open (no passphrase, so no key) is red at unseal", async () => { |
| 107 | 124 | const r = await runDrill(deps({ passphrase: undefined })); |
| 108 | 125 | expect(r.verdict).toBe("red"); |
test/tui-backups.test.ts added +84
| @@ -0,0 +1,84 @@ | ||
| 1 | /** | |
| 2 | * The terminal answers "is the forge recoverable now?" as the Backups page | |
| 3 | * does, from the same module, and shows what the last drill counted. Only a | |
| 4 | * site admin is offered the command. | |
| 5 | */ | |
| 6 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 7 | import { mkdtempSync, rmSync } from "node:fs"; | |
| 8 | import { tmpdir } from "node:os"; | |
| 9 | import { join } from "node:path"; | |
| 10 | import { createApp, type App } from "../src/app.ts"; | |
| 11 | import { createUser, type User } from "../src/core/users.ts"; | |
| 12 | import { recordDrill } from "../src/snapshot/drills.ts"; | |
| 13 | import { startSnapshots, type SnapshotsHandle } from "../src/snapshot/service.ts"; | |
| 14 | import { dirStore } from "../src/snapshot/store.ts"; | |
| 15 | import { forgeCommands, Session } from "../src/tui/forge.ts"; | |
| 16 | import { backupLines } from "../src/tui/screens/backups.ts"; | |
| 17 | import { quietLogs } from "./helpers/globals.ts"; | |
| 18 | ||
| 19 | quietLogs(); | |
| 20 | ||
| 21 | let root = ""; | |
| 22 | let app: App; | |
| 23 | let svc: SnapshotsHandle; | |
| 24 | let admin: User; | |
| 25 | let member: User; | |
| 26 | ||
| 27 | const identity = (user?: User) => ({ | |
| 28 | sshUsername: "x", | |
| 29 | remoteAddress: "127.0.0.1", | |
| 30 | ...(user ? { user } : {}), | |
| 31 | }); | |
| 32 | ||
| 33 | beforeAll(async () => { | |
| 34 | root = mkdtempSync(join(tmpdir(), "forge-tuibackups-")); | |
| 35 | app = createApp({ dataDir: root, port: 0 }); | |
| 36 | const make = async (username: string, adminIfFirst: boolean) => { | |
| 37 | const u = await createUser(app.db, { | |
| 38 | trusted: true, | |
| 39 | username, | |
| 40 | email: `${username}@forge.local`, | |
| 41 | password: "a-good-passphrase", | |
| 42 | adminIfFirst, | |
| 43 | }); | |
| 44 | if (!u.ok) throw new Error(u.error); | |
| 45 | return u.value; | |
| 46 | }; | |
| 47 | admin = await make("alice", true); | |
| 48 | member = await make("bob", false); | |
| 49 | svc = startSnapshots(app, dirStore(join(root, "store")), { version: "t", nightly: false }); | |
| 50 | }); | |
| 51 | ||
| 52 | afterAll(async () => { | |
| 53 | await svc.stop(); | |
| 54 | app.close(); | |
| 55 | rmSync(root, { recursive: true, force: true }); | |
| 56 | }); | |
| 57 | ||
| 58 | test("the terminal says the forge is not proven recoverable, then what the drill counted", async () => { | |
| 59 | const session = new Session(app, identity(admin)); | |
| 60 | const before = (await backupLines(session)).join(" "); | |
| 61 | expect(before).toContain("Not proven recoverable"); | |
| 62 | expect(before).toContain("no snapshot yet"); | |
| 63 | expect(before).toContain("no drill yet"); | |
| 64 | recordDrill(app.db, { | |
| 65 | at: new Date().toISOString(), | |
| 66 | snapshotId: "s1", | |
| 67 | ms: 5, | |
| 68 | counts: { repositories: 7, users: 3, issues: 42, pulls: 5, apps: 2 }, | |
| 69 | sealedOpens: true, | |
| 70 | health: "ok", | |
| 71 | verdict: "green", | |
| 72 | failedStep: null, | |
| 73 | reason: null, | |
| 74 | }); | |
| 75 | const after = (await backupLines(session)).join(" ").replace(/\s+/g, " "); | |
| 76 | expect(after).toContain("last drill green"); | |
| 77 | expect(after).toContain("7 repositories, 3 users, 42 issues, 5 pulls, 2 apps; a sealed value opens"); | |
| 78 | }); | |
| 79 | ||
| 80 | test("only a site admin is offered the command", () => { | |
| 81 | const ids = (u: User) => forgeCommands(new Session(app, identity(u)))({} as never).map((c) => c.id); | |
| 82 | expect(ids(admin)).toContain("backups"); | |
| 83 | expect(ids(member)).not.toContain("backups"); | |
| 84 | }); |
new above
offline