Public: anyone can read this; the crew can change it.
The forge's backup includes every app's Drop, and a restore test brings one dropped file back byte for byte #13440
Check · Review · Land · Live done — Landed and live in production.
mason/forge:mason/13424-the-forge-s-backup-includes-every-app-s → main 1 commit fork
2 files
changed +79
−0
docs/SNAPSHOT.md +7
| @@ -81,6 +81,13 @@ and `sentMb` put, `heldBefore` already there. | ||
| 81 | 81 | inside are copied the consistent way; their `-wal`/`-shm` side-files are |
| 82 | 82 | the live process's and stay out. Previews are rebuilt from production and |
| 83 | 83 | stay out. |
| 84 | - Every app's Drop (`drop/<repo id>/<name>`): the reference files its owner | |
| 85 | dropped for the crew, kept nowhere else. Uploads still in flight (dot-files) | |
| 86 | stay out. The Drop lives beside `apps/`, never inside an environment's | |
| 87 | `FORGE_APP_DATA`, so a preview's copy of production's data never carries it. | |
| 88 | A restore returns each file byte for byte (`test/drop-backup.test.ts`). A | |
| 89 | large Drop makes the night longer by the time it takes to read and hash it; | |
| 90 | only chunks the store lacks are sent. | |
| 84 | 91 | - The crew's run logs, a day's worth to a few bundles |
| 85 | 92 | (`runs/<day>[-n].ndjson`, cut at 4 MB of logs), in the order the logs were |
| 86 | 93 | last written: a bundle once full is the same bytes in every later |
test/drop-backup.test.ts added +72
| @@ -0,0 +1,72 @@ | ||
| 1 | /** | |
| 2 | * The backup holds every app's drop (`drop/<repo id>/…`), and a restore brings a dropped file back | |
| 3 | * byte for byte. The drop stays outside every environment's FORGE_APP_DATA, so the data a preview | |
| 4 | * copies from production never carries it. | |
| 5 | */ | |
| 6 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 7 | import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; | |
| 8 | import { tmpdir } from "node:os"; | |
| 9 | import { join, relative } from "node:path"; | |
| 10 | import { createRepo } from "../src/actions/repos.ts"; | |
| 11 | import { createApp, type App } from "../src/app.ts"; | |
| 12 | import { envDataDir } from "../src/apps/data.ts"; | |
| 13 | import { getRepo } from "../src/core/repos.ts"; | |
| 14 | import { createUser } from "../src/core/users.ts"; | |
| 15 | import { dropDir } from "../src/drop/store.ts"; | |
| 16 | import { restoreSnapshot } from "../src/snapshot/restore.ts"; | |
| 17 | import { takeSnapshot } from "../src/snapshot/snapshot.ts"; | |
| 18 | import { dirStore } from "../src/snapshot/store.ts"; | |
| 19 | ||
| 20 | let root: string; | |
| 21 | let app: App; | |
| 22 | const ids: string[] = []; | |
| 23 | /** Every byte value, twice over, and a size that fits no chunk boundary. */ | |
| 24 | const bytes = (seed: number): Uint8Array => Uint8Array.from({ length: 70_001 }, (_, i) => (i * 7 + seed) % 256); | |
| 25 | ||
| 26 | beforeAll(async () => { | |
| 27 | root = mkdtempSync(join(tmpdir(), "forge-drop-backup-")); | |
| 28 | app = createApp({ dataDir: join(root, "live"), port: 0 }); | |
| 29 | const made = await createUser(app.db, { origin: "seed", trusted: true, username: "alice", | |
| 30 | email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true }); | |
| 31 | if (!made.ok) throw new Error(made.error); | |
| 32 | for (const name of ["one", "two"]) { | |
| 33 | const repo = await createRepo(app, made.value, { name, visibility: "private" }); | |
| 34 | if (!repo.ok) throw new Error(String(repo.error)); | |
| 35 | ids.push(getRepo(app.db, "alice", name)!.id); | |
| 36 | } | |
| 37 | }); | |
| 38 | afterAll(() => rmSync(root, { recursive: true, force: true })); | |
| 39 | ||
| 40 | test("the backup holds every app's drop, and a restore returns a dropped file byte for byte", async () => { | |
| 41 | const live = app.config.dataDir; | |
| 42 | ids.forEach((id, n) => { | |
| 43 | mkdirSync(dropDir(live, id), { recursive: true }); | |
| 44 | writeFileSync(join(dropDir(live, id), "part.bin"), bytes(n + 1)); | |
| 45 | }); | |
| 46 | const store = dirStore(join(root, "store")); | |
| 47 | const manifest = await takeSnapshot( | |
| 48 | { db: app.db, dataDir: live, reposRoot: app.reposRoot, version: "t" }, | |
| 49 | store, | |
| 50 | new Date("2026-09-22T08:00:00Z"), | |
| 51 | ); | |
| 52 | const paths = manifest.files.map((f) => f.path); | |
| 53 | for (const id of ids) expect(paths).toContain(`drop/${id}/part.bin`); | |
| 54 | ||
| 55 | // Wipe one app's drop, then restore into an empty directory. | |
| 56 | rmSync(dropDir(live, ids[0]!), { recursive: true }); | |
| 57 | const back = join(root, "back"); | |
| 58 | await restoreSnapshot(store, manifest.name, back); | |
| 59 | ids.forEach((id, n) => { | |
| 60 | const got = readFileSync(join(dropDir(back, id), "part.bin")); | |
| 61 | expect(Buffer.compare(got, Buffer.from(bytes(n + 1)))).toBe(0); | |
| 62 | }); | |
| 63 | }); | |
| 64 | ||
| 65 | test("the drop is outside every environment's FORGE_APP_DATA", () => { | |
| 66 | const live = app.config.dataDir; | |
| 67 | for (const env of ["production", "pull-7", "build-1a2b3c4", "variant-print"] as const) { | |
| 68 | const data = envDataDir(live, ids[0]!, env); | |
| 69 | expect(relative(data, dropDir(live, ids[0]!)).startsWith("..")).toBe(true); | |
| 70 | expect(existsSync(join(data, "part.bin"))).toBe(false); | |
| 71 | } | |
| 72 | }); |
new above
offline