Public: anyone can read this; the crew can change it.

The backup holds recipes, archived forges' records and the routes table #17723

opened this

Merged 10h 55m ago, live in production

sold/small-backup → main 0 commits 2 threads · 2 unresolved

  • Merged. sold/small-backup was merged into main as 079ae5c.

Review

Sign in to review this change.
8 files changed +87 −10
docs/SNAPSHOT.md +6
@@ -95,6 +95,12 @@ and `sentMb` put, `heldBefore` already there.
9595 kept nowhere else. A restore names each jar the database lists whose file did
9696 not come back, or is of another size (`test/jar-backup.test.ts`). A snapshot
9797 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.
98104 - The crew's run logs, a day's worth to a few bundles
99105 (`runs/<day>[-n].ndjson`, cut at 4 MB of logs), in the order the logs were
100106 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.
1717 | `note-pictures/` | The picture sent with a note from the forge button | `src/core/note_pictures.ts` |
1818 | `changes/` | Each app's ordered change stream, one SQLite file an app | `src/apps/changes.ts` |
1919 | `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` |
2120 | `claude-config/` | The agent's own session files for conversations, unless the operator named another directory | `src/crew/conversation.ts` |
2221 | `secret.key.old` | The key before the last change of key, while there is one | `src/kernel/secret.ts` |
2322 | `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` |
2423 | `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` |
2724 | `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` |
2825
2926 ## 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
1414 | Its key, wrapped under `FORGE_SNAPSHOT_PASSPHRASE` | `secret.key.wrapped` (without a passphrase the key stays home) |
1515 | Every app's production data | alongside |
1616 | 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 |
1818 | 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` |
1919
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.
2121
2222 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.
2323
src/snapshot/hosted.ts +2 −2
@@ -76,7 +76,7 @@ export interface HostedSource {
7676 export const PARENT_SIDE = "parent/";
7777 export const PARENT_FILES: readonly string[] = [`${PARENT_SIDE}credit.json`, `${PARENT_SIDE}own-code.json`];
7878 /** 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;

Needs a test: with recipes removed 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.

8080 export const HISTORY_FILES = ["parent.json", "plan.json"] as const;
8181
8282 // 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
210210
211211 /** Names of left-out files a manifest lists; the rest are counted. */
212212 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. */
214214 export const HISTORY_BYTES_MAX = 512 * 1_048_576;
215215
216216 type Cut = "content" | "fixed" | "bundle";
src/snapshot/restore.ts +3 −2
@@ -54,6 +54,7 @@ import {
5454 isRunBundle,
5555 keyOf,
5656 newestSnapshot,
57 ownerOnly,
5758 SNAPSHOT_PREFIX,
5859 streamManifestKey,
5960 unpackRuns,
@@ -309,7 +310,7 @@ export async function fetchChunked(
309310 }
310311 } else {
311312 // 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);
313314 files++;
314315 if (bundle) bundles.push({ at, owner: bundle[1]!, name: bundle[2]!, file: target });
315316 }
@@ -507,7 +508,7 @@ export async function restoreSnapshot(
507508 await Bun.write(target, body);
508509 // A key the rest of the machine could read is one the forge refuses to
509510 // 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))
511512 chmodSync(target, 0o600);
512513 files++;
513514 if (bundle)
src/snapshot/snapshot.ts +13
@@ -136,6 +136,13 @@ export interface Manifest {
136136 readonly sent?: { readonly chunks: number; readonly bytes: number; readonly held: number };
137137 }
138138
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
139146 /** What a manifest says of the key, including manifests older than the field. */
140147 export const keyOf = (m: Manifest): "wrapped" | "none" | "raw" =>
141148 m.key ?? (m.files.some((f) => f.path === "secret.key") ? "raw" : "none");
@@ -792,6 +799,12 @@ export async function takeSnapshot(
792799 const jars = join(forge.dataDir, JARS_DIR);
793800 for (const f of streaming ? [] : filesUnder(jars)) await putFile(`${JARS_DIR}/${f}`, join(jars, f));
794801
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)

This skips the records whenever streaming is true, so a streamed snapshot silently lacks them. Take them in both modes: they are a few KB, unlike the jars.

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
795808 // The crew's run logs: history a person may want back. A day's worth to
796809 // a few bundles, in the order the logs were last written: a log still
797810 // 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(() => {
2222 parent = createApp({ dataDir: join(root, "parent"), port: 0 });
2323 const dir = join(root, "parent", "children", "b1");
2424 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 });
2626 writeFileSync(join(dir, "runs", "r1", "log.txt"), "ran the thing\n");
2727 writeFileSync(join(dir, "drop", "note.txt"), "dropped\n");
2828 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 });
2931 writeFileSync(join(dir, "ledger", "2026-11.jsonl"), '{"usd":1.25}\n');
3032 writeFileSync(join(dir, "parent.json"), '{"name":"kid"}', { mode: 0o600 });
3133 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
5355 expect(readFileSync(join(at, "runs", "r1", "log.txt"), "utf8")).toBe("ran the thing\n");
5456 expect(readFileSync(join(at, "drop", "note.txt"), "utf8")).toBe("dropped\n");
5557 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);
5660 expect(readFileSync(join(at, "ledger", "2026-11.jsonl"), "utf8")).toContain("1.25");
5761 expect(readFileSync(join(at, "plan.json"), "utf8")).toContain("free");
5862 expect(statSync(join(at, "parent.json")).mode & 0o077).toBe(0);

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs