Public: anyone can read this; the crew can change it.
The backup holds recipes, archived forges' records and the routes table #17723
Merged 10h 55m ago, live in production
sold/small-backup → main 0 commits 2 threads · 2 unresolved
8 files
changed +87
−10
docs/SNAPSHOT.md +6
| @@ -95,6 +95,12 @@ and `sentMb` put, `heldBefore` already there. | ||
| 95 | 95 | kept nowhere else. A restore names each jar the database lists whose file did |
| 96 | 96 | not come back, or is of another size (`test/jar-backup.test.ts`). A snapshot |
| 97 | 97 | from before 2026-12-02 holds none of them. |
| 98 | - Small records kept in files of their own: the recipes people saved | |
| 99 | (`recipes/*.json`), the record of each archived hosted forge and when its | |
| 100 | owner was told (`archive/*.json`, not the copies), and `routes.json`, which | |
| 101 | machine holds a forge that is not on this one. A restore gives them back | |
| 102 | readable by the owner only. `routes.json` says where a forge was when the | |
| 103 | snapshot was taken; move or recover a forge that has moved since. | |
| 98 | 104 | - The crew's run logs, a day's worth to a few bundles |
| 99 | 105 | (`runs/<day>[-n].ndjson`, cut at 4 MB of logs), in the order the logs were |
| 100 | 106 | last written: a bundle once full is the same bytes in every later |
docs/features/backup-left-out.md −3
| @@ -17,13 +17,10 @@ code writes them. | ||
| 17 | 17 | | `note-pictures/` | The picture sent with a note from the forge button | `src/core/note_pictures.ts` | |
| 18 | 18 | | `changes/` | Each app's ordered change stream, one SQLite file an app | `src/apps/changes.ts` | |
| 19 | 19 | | `machines/` | Each person's own directory on a repository's machines | `src/machines/shell.ts` | |
| 20 | | `recipes/` | Forge configurations a person saved by name | `src/spawn/recipes.ts` | | |
| 21 | 20 | | `claude-config/` | The agent's own session files for conversations, unless the operator named another directory | `src/crew/conversation.ts` | |
| 22 | 21 | | `secret.key.old` | The key before the last change of key, while there is one | `src/kernel/secret.ts` | |
| 23 | 22 | | `edge/signing.pem` | The key that signs the files the edge serves. A boot without it makes a new one, with a new public key | `src/cloud/edge.ts` | |
| 24 | 23 | | `dataset/salt` | The secret that names people in the dataset. An export without it makes a new one, so the names change | `src/actions/dataset.ts` | |
| 25 | | `archive/` | The record of each archived hosted forge: which store holds its copy, and the copy's hash | `src/spawn/archive.ts` | | |
| 26 | | `routes.json` | Which machine holds a hosted forge that is not on this one | `src/spawn/routes.ts` | | |
| 27 | 24 | | `access/`, `perf/`, `nightly/` | The access log (14 days), the performance log, each nightly job's last output | `src/http/access_log.ts`, `src/perf/kept_log.ts`, `src/nightly/output.ts` | |
| 28 | 25 | |
| 29 | 26 | ## In the bucket, or only here |
docs/features/hosted-backup.md +2 −2
| @@ -14,10 +14,10 @@ Each forge this one hosts goes into the nightly snapshot (#13819), under `childr | ||
| 14 | 14 | | Its key, wrapped under `FORGE_SNAPSHOT_PASSPHRASE` | `secret.key.wrapped` (without a passphrase the key stays home) | |
| 15 | 15 | | Every app's production data | alongside | |
| 16 | 16 | | This forge's own record of it | `ledger.json` | |
| 17 | | Its history: run logs, drop, cost ledger, uploaded plugin jars, `parent.json`, `plan.json` | `runs/`, `drop/`, `ledger/`, `games/jars/`, alongside | | |
| 17 | | Its history: run logs, drop, cost ledger, uploaded plugin jars, saved recipes, `parent.json`, `plan.json` | `runs/`, `drop/`, `ledger/`, `games/jars/`, `recipes/`, alongside | | |
| 18 | 18 | | What the host holds of it: credit and spend, the own-code switch | `parent/credit.json`, `parent/own-code.json`; `--forge` puts them back under `<data>/credit` and `<data>/own-code` | |
| 19 | 19 | |
| 20 | `parent/credit.json` holds the forge's `tokenHash`; it is backed up like everything else and is covered by the same encryption: a snapshot kept on a sealed lane is sealed chunk by chunk, this file with the rest. Files are opened without following a link and only if regular; a link, a pipe, a file that changed while it was copied, or history past the 512 MiB cap on `runs/`, `drop/`, `ledger/` and `games/jars/` together is left out and named in the manifest's `omitted` list (first 100, `omittedTotal` counts all). A restore refuses a `parent/` entry it has no place for. | |
| 20 | `parent/credit.json` holds the forge's `tokenHash`; it is backed up like everything else and is covered by the same encryption: a snapshot kept on a sealed lane is sealed chunk by chunk, this file with the rest. Files are opened without following a link and only if regular; a link, a pipe, a file that changed while it was copied, or history past the 512 MiB cap on `runs/`, `drop/`, `ledger/`, `games/jars/` and `recipes/` together is left out and named in the manifest's `omitted` list (first 100, `omittedTotal` counts all). A restore refuses a `parent/` entry it has no place for. | |
| 21 | 21 | |
| 22 | 22 | Chunks are shared with the rest of the snapshot, so a forge unchanged since last night sends nothing. A forge that cannot be read is named in the manifest's `hosted` list and the log; the snapshot goes on without it. |
| 23 | 23 |
src/snapshot/hosted.ts +2 −2
| @@ -76,7 +76,7 @@ export interface HostedSource { | ||
| 76 | 76 | export const PARENT_SIDE = "parent/"; |
| 77 | 77 | export const PARENT_FILES: readonly string[] = [`${PARENT_SIDE}credit.json`, `${PARENT_SIDE}own-code.json`]; |
| 78 | 78 | /** What a forge keeps in its own directory beside its database: history and records, whole. */ |
| 79 | export const HISTORY_DIRS = ["runs", "drop", "ledger", JARS_DIR] as const; | |
| 79 | export const HISTORY_DIRS = ["runs", "drop", "ledger", JARS_DIR, "recipes"] as const; | |
| 80 | 80 | export const HISTORY_FILES = ["parent.json", "plan.json"] as const; |
| 81 | 81 | |
| 82 | 82 | // A letter or digit first, as `spawn/archive.ts` holds: an id never reads as an option (#14783). |
| @@ -210,7 +210,7 @@ export type HostedCopy = Pick<HostedEntry, "repos" | "key" | "absent" | "omitted | ||
| 210 | 210 | |
| 211 | 211 | /** Names of left-out files a manifest lists; the rest are counted. */ |
| 212 | 212 | export const OMITTED_LISTED = 100; |
| 213 | /** Bytes of runs/, drop/, ledger/ and games/jars/ together a snapshot of one forge carries. */ | |
| 213 | /** Bytes of the `HISTORY_DIRS` together a snapshot of one forge carries. */ | |
| 214 | 214 | export const HISTORY_BYTES_MAX = 512 * 1_048_576; |
| 215 | 215 | |
| 216 | 216 | type Cut = "content" | "fixed" | "bundle"; |
src/snapshot/restore.ts +3 −2
| @@ -54,6 +54,7 @@ import { | ||
| 54 | 54 | isRunBundle, |
| 55 | 55 | keyOf, |
| 56 | 56 | newestSnapshot, |
| 57 | ownerOnly, | |
| 57 | 58 | SNAPSHOT_PREFIX, |
| 58 | 59 | streamManifestKey, |
| 59 | 60 | unpackRuns, |
| @@ -309,7 +310,7 @@ export async function fetchChunked( | ||
| 309 | 310 | } |
| 310 | 311 | } else { |
| 311 | 312 | // The snapshot carries bytes, not modes; a key others could read is one the forge refuses. |
| 312 | if (entry.path === "secret.key" || entry.path.startsWith("ssh/")) chmodSync(target, 0o600); | |
| 313 | if (ownerOnly(entry.path)) chmodSync(target, 0o600); | |
| 313 | 314 | files++; |
| 314 | 315 | if (bundle) bundles.push({ at, owner: bundle[1]!, name: bundle[2]!, file: target }); |
| 315 | 316 | } |
| @@ -507,7 +508,7 @@ export async function restoreSnapshot( | ||
| 507 | 508 | await Bun.write(target, body); |
| 508 | 509 | // A key the rest of the machine could read is one the forge refuses to |
| 509 | 510 | // use; the snapshot carries bytes, not modes, so the modes are set here. |
| 510 | if (entry.path === "secret.key" || entry.path.startsWith("ssh/")) | |
| 511 | if (ownerOnly(entry.path)) | |
| 511 | 512 | chmodSync(target, 0o600); |
| 512 | 513 | files++; |
| 513 | 514 | if (bundle) |
src/snapshot/snapshot.ts +13
| @@ -136,6 +136,13 @@ export interface Manifest { | ||
| 136 | 136 | readonly sent?: { readonly chunks: number; readonly bytes: number; readonly held: number }; |
| 137 | 137 | } |
| 138 | 138 | |
| 139 | /** Folders whose own `.json` files a snapshot takes: not what lies deeper in them. */ | |
| 140 | const RECORD_DIRS = ["recipes", "archive"] as const; | |
| 141 | const ROUTES_FILE = "routes.json"; | |
| 142 | /** Files a restore gives back to their owner only: a snapshot carries bytes, not modes. */ | |
| 143 | export const ownerOnly = (path: string): boolean => | |
| 144 | path === "secret.key" || path === ROUTES_FILE || ["ssh/", ...RECORD_DIRS.map((d) => `${d}/`)].some((d) => path.startsWith(d)); | |
| 145 | ||
| 139 | 146 | /** What a manifest says of the key, including manifests older than the field. */ |
| 140 | 147 | export const keyOf = (m: Manifest): "wrapped" | "none" | "raw" => |
| 141 | 148 | m.key ?? (m.files.some((f) => f.path === "secret.key") ? "raw" : "none"); |
| @@ -792,6 +799,12 @@ export async function takeSnapshot( | ||
| 792 | 799 | const jars = join(forge.dataDir, JARS_DIR); |
| 793 | 800 | for (const f of streaming ? [] : filesUnder(jars)) await putFile(`${JARS_DIR}/${f}`, join(jars, f)); |
| 794 | 801 | |
| 802 | // Small records kept in files of their own and nowhere else: the recipes people saved, where | |
| 803 | // each archived forge's copy is, and which machine holds a forge that is not on this one. | |
| 804 | for (const d of streaming ? [] : RECORD_DIRS) | |
| 805 | for (const f of filesUnder(join(forge.dataDir, d))) if (/^[^/.][^/]*\.json$/.test(f)) await putFile(`${d}/${f}`, join(forge.dataDir, d, f)); | |
| 806 | if (!streaming) await putFile(ROUTES_FILE, join(forge.dataDir, ROUTES_FILE)); | |
| 807 | ||
| 795 | 808 | // The crew's run logs: history a person may want back. A day's worth to |
| 796 | 809 | // a few bundles, in the order the logs were last written: a log still |
| 797 | 810 | // growing is in the day's last bundle, and the bundles before it are the |
test/small-records-backup.test.ts added +56
| @@ -0,0 +1,56 @@ | ||
| 1 | /** | |
| 2 | * The backup holds the small records the forge keeps in files of their own: recipes, the records | |
| 3 | * of archived forges, and the router's table. A restore reads them back, for their owner only. | |
| 4 | */ | |
| 5 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 6 | import { existsSync, mkdirSync, mkdtempSync, rmSync, statSync, writeFileSync } from "node:fs"; | |
| 7 | import { tmpdir } from "node:os"; | |
| 8 | import { join } from "node:path"; | |
| 9 | import { createApp, type App } from "../src/app.ts"; | |
| 10 | import { dirStore } from "../src/blob/store.ts"; | |
| 11 | import { restoreSnapshot } from "../src/snapshot/restore.ts"; | |
| 12 | import { takeSnapshot } from "../src/snapshot/snapshot.ts"; | |
| 13 | import { archivedForges } from "../src/spawn/archive.ts"; | |
| 14 | import { listRecipes } from "../src/spawn/recipes.ts"; | |
| 15 | import { routeOf, setRoute } from "../src/spawn/routes.ts"; | |
| 16 | ||
| 17 | let root: string; | |
| 18 | let app: App; | |
| 19 | const record = { name: "old", bodyId: "b9", archivedAt: "2026-11-01T00:00:00Z", where: "host/bucket", key: "forge-archives/old", bytes: 9, sha256: "0".repeat(64), for: null, apps: [] }; | |
| 20 | ||
| 21 | beforeAll(() => { | |
| 22 | root = mkdtempSync(join(tmpdir(), "forge-small-records-")); | |
| 23 | app = createApp({ dataDir: join(root, "live"), port: 0 }); | |
| 24 | const live = app.config.dataDir; | |
| 25 | mkdirSync(join(live, "recipes")); | |
| 26 | writeFileSync(join(live, "recipes", "p1.json"), JSON.stringify([{ name: "Shop", config: { apps: [] } }])); | |
| 27 | mkdirSync(join(live, "archive", "files"), { recursive: true }); | |
| 28 | writeFileSync(join(live, "archive", "old.json"), JSON.stringify(record)); | |
| 29 | writeFileSync(join(live, "archive", "notices.json"), '{"old":"2026-11-01T00:00:00Z"}'); | |
| 30 | writeFileSync(join(live, "archive", "files", "old.tar.gz"), "a copy not yet verified"); | |
| 31 | setRoute(app, "kid", { machine: "two", port: 4101, movedAt: "2026-11-02T00:00:00Z" }); | |
| 32 | }); | |
| 33 | afterAll(() => { | |
| 34 | app.close(); | |
| 35 | rmSync(root, { recursive: true, force: true }); | |
| 36 | }); | |
| 37 | ||
| 38 | test("recipes, archive records and the routes come back from a restore, for their owner only", async () => { | |
| 39 | const live = app.config.dataDir; | |
| 40 | expect(listRecipes(live, "p1")).toHaveLength(1); | |
| 41 | const store = dirStore(join(root, "store")); | |
| 42 | const m = await takeSnapshot({ db: app.db, dataDir: live, reposRoot: app.reposRoot, version: "t" }, store, new Date("2026-12-02T08:00:00Z")); | |
| 43 | const back = join(root, "back"); | |
| 44 | await restoreSnapshot(store, m.name, back); | |
| 45 | const restored = createApp({ dataDir: back, port: 0 }); | |
| 46 | try { | |
| 47 | expect(listRecipes(back, "p1")).toEqual(listRecipes(live, "p1")); | |
| 48 | expect(archivedForges(restored)).toEqual([record]); | |
| 49 | expect(routeOf(restored, "kid")).toEqual({ machine: "two", port: 4101, movedAt: "2026-11-02T00:00:00Z" }); | |
| 50 | } finally { | |
| 51 | restored.close(); | |
| 52 | } | |
| 53 | for (const f of ["recipes/p1.json", "archive/old.json", "archive/notices.json", "routes.json"]) expect([f, statSync(join(back, f)).mode & 0o777]).toEqual([f, 0o600]); | |
| 54 | // The copy of an archived forge is the bucket's; one still here is not a record. | |
| 55 | expect(existsSync(join(back, "archive", "files"))).toBe(false); | |
| 56 | }); |
test/snapshot-hosted-history.test.ts +5 −1
| @@ -22,10 +22,12 @@ beforeAll(() => { | ||
| 22 | 22 | parent = createApp({ dataDir: join(root, "parent"), port: 0 }); |
| 23 | 23 | const dir = join(root, "parent", "children", "b1"); |
| 24 | 24 | child = createApp({ dataDir: dir, port: 0 }); |
| 25 | for (const d of ["runs/r1", "drop", "ledger", "games/jars"]) mkdirSync(join(dir, d), { recursive: true }); | |
| 25 | for (const d of ["runs/r1", "drop", "ledger", "games/jars", "recipes"]) mkdirSync(join(dir, d), { recursive: true }); | |
| 26 | 26 | writeFileSync(join(dir, "runs", "r1", "log.txt"), "ran the thing\n"); |
| 27 | 27 | writeFileSync(join(dir, "drop", "note.txt"), "dropped\n"); |
| 28 | 28 | writeFileSync(join(dir, "games", "jars", "j1.jar"), "a jar\n"); |
| 29 | // Wider than it may come back, so the mode below is the restore's own. | |
| 30 | writeFileSync(join(dir, "recipes", "p1.json"), '{"recipe":"p1"}', { mode: 0o644 }); | |
| 29 | 31 | writeFileSync(join(dir, "ledger", "2026-11.jsonl"), '{"usd":1.25}\n'); |
| 30 | 32 | writeFileSync(join(dir, "parent.json"), '{"name":"kid"}', { mode: 0o600 }); |
| 31 | 33 | writeFileSync(join(dir, "plan.json"), '{"plan":"free"}'); |
| @@ -53,6 +55,8 @@ test("a restored hosted forge has its runs, drop, ledger, records, spend and swi | ||
| 53 | 55 | expect(readFileSync(join(at, "runs", "r1", "log.txt"), "utf8")).toBe("ran the thing\n"); |
| 54 | 56 | expect(readFileSync(join(at, "drop", "note.txt"), "utf8")).toBe("dropped\n"); |
| 55 | 57 | expect(readFileSync(join(at, "games", "jars", "j1.jar"), "utf8")).toBe("a jar\n"); |
| 58 | expect(readFileSync(join(at, "recipes", "p1.json"), "utf8")).toBe('{"recipe":"p1"}'); | |
| 59 | expect(statSync(join(at, "recipes", "p1.json")).mode & 0o777).toBe(0o600); | |
| 56 | 60 | expect(readFileSync(join(at, "ledger", "2026-11.jsonl"), "utf8")).toContain("1.25"); |
| 57 | 61 | expect(readFileSync(join(at, "plan.json"), "utf8")).toContain("free"); |
| 58 | 62 | expect(statSync(join(at, "parent.json")).mode & 0o077).toBe(0); |
new above
offline
Needs a test: with
recipesremoved from this list no test fails. Add a hosted snapshot and restore test that carries a recipe file for a hosted forge and checks it comes back at 0600.