Public: anyone can read this; the crew can change it.
The backup holds uploaded plugin jars, and a restore names each one that did not come back (#17284) #17716
Merged 19h 14m ago, live in production
sold/jars-backup → main 0 commits 2 threads · 2 unresolved
14 files
changed +203
−13
- docs/SNAPSHOT.md
- docs/features/backup-left-out.md
- docs/features/hosted-backup.md
- scripts/restore-drill.ts
- scripts/standby-restore.ts
- src/games/jars.ts
- src/games/store.ts
- src/snapshot/hosted.ts
- src/snapshot/restore.ts
- src/snapshot/snapshot.ts
- test/drill-hosted-red.test.ts
- test/jar-backup.test.ts
- test/snapshot-hosted-history.test.ts
- test/worker-database-opens.test.ts
docs/SNAPSHOT.md +7 −2
| @@ -91,6 +91,10 @@ and `sentMb` put, `heldBefore` already there. | ||
| 91 | 91 | A restore returns each file byte for byte (`test/drop-backup.test.ts`). A |
| 92 | 92 | large Drop makes the night longer by the time it takes to read and hash it; |
| 93 | 93 | only chunks the store lacks are sent. |
| 94 | - The plugin jars people uploaded for their game servers (`games/jars/<id>.jar`), | |
| 95 | kept nowhere else. A restore names each jar the database lists whose file did | |
| 96 | not come back, or is of another size (`test/jar-backup.test.ts`). A snapshot | |
| 97 | from before 2026-12-02 holds none of them. | |
| 94 | 98 | - The crew's run logs, a day's worth to a few bundles |
| 95 | 99 | (`runs/<day>[-n].ndjson`, cut at 4 MB of logs), in the order the logs were |
| 96 | 100 | last written: a bundle once full is the same bytes in every later |
| @@ -99,8 +103,9 @@ and `sentMb` put, `heldBefore` already there. | ||
| 99 | 103 | with its size, hash and chunks, `sent` (what this snapshot itself put), |
| 100 | 104 | and `key`: `wrapped` or `none` (older snapshots: `raw`). |
| 101 | 105 | |
| 102 | What stays out is what a boot or a deploy rebuilds: clones, checks, | |
| 103 | pictures, previews. | |
| 106 | Everything else under the data directory stays out. Most of it is what a boot | |
| 107 | or a deploy rebuilds: clones, checks, pictures of pages, previews. Some is not: | |
| 108 | [](features/backup-left-out.md) lists it. | |
| 104 | 109 | |
| 105 | 110 | ## Is a snapshot encrypted before it leaves? |
| 106 | 111 |
docs/features/backup-left-out.md added +52
| @@ -0,0 +1,52 @@ | ||
| 1 | --- | |
| 2 | topic: Backups and moving | |
| 3 | row: What is in no backup | |
| 4 | --- | |
| 5 | # What is in no backup | |
| 6 | ||
| 7 | The nightly snapshot takes a fixed list from the data directory (`src/snapshot/snapshot.ts`, | |
| 8 | [](../SNAPSHOT.md), "What a snapshot holds"). A folder that is not on the list is in no | |
| 9 | snapshot. This page lists those that hold something a boot or a deploy does not make again, as the | |
| 10 | code writes them. | |
| 11 | ||
| 12 | ## Kept nowhere else | |
| 13 | ||
| 14 | | Under the data directory | What it is | Written by | | |
| 15 | | --- | --- | --- | | |
| 16 | | `conversation-files/` | Files and images people attached to a Talk message | `src/core/conversation_files.ts` | | |
| 17 | | `note-pictures/` | The picture sent with a note from the forge button | `src/core/note_pictures.ts` | | |
| 18 | | `changes/` | Each app's ordered change stream, one SQLite file an app | `src/apps/changes.ts` | | |
| 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 | | `claude-config/` | The agent's own session files for conversations, unless the operator named another directory | `src/crew/conversation.ts` | | |
| 22 | | `secret.key.old` | The key before the last change of key, while there is one | `src/kernel/secret.ts` | | |
| 23 | | `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 | | `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 | | `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 | ||
| 29 | ## In the bucket, or only here | |
| 30 | ||
| 31 | With `S3_*` set these are written to the bucket, beside the snapshots and not inside them. Without | |
| 32 | it they are folders under the data directory, and in no backup. | |
| 33 | ||
| 34 | | Without a bucket | What it is | Written by | | |
| 35 | | --- | --- | --- | | |
| 36 | | `look-blobs/` | Pictures a person attached as looks | `src/looks/archive.ts` | | |
| 37 | | `run-archive/` | Finished run logs. The snapshot takes `runs/`, and a log the store holds leaves `runs/` about a day after it was last written | `src/runs/archive.ts` | | |
| 38 | | `large-blobs/` | Copies of big git blobs; git keeps its own, and the bundles hold it | `src/blob/large.ts` | | |
| 39 | ||
| 40 | ## Covered another way | |
| 41 | ||
| 42 | - `ledger/`, this forge's cost ledger: its lines are committed to the `ledger` branch of the forge's | |
| 43 | own repository, at most once an hour, and that repository is in the snapshot. With no such | |
| 44 | repository (`FORGE_LEDGER_REPO`, or the forge's own), the ledger is in no backup. A hosted forge's | |
| 45 | `ledger/` is in the snapshot itself. | |
| 46 | - `carts/`: a page saved from a build at a fixed commit. Capture makes it again while the build | |
| 47 | still runs. | |
| 48 | - `app-backups/`: sealed copies of an app's production data. The data is in the snapshot; the sealed | |
| 49 | copies are not. | |
| 50 | ||
| 51 | This list was read from the code on 2026-12-02 and is not checked by a test. It names what was found | |
| 52 | to matter; folders that hold clones, caches, checks and last-seen states are not listed. |
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, `parent.json`, `plan.json` | `runs/`, `drop/`, `ledger/`, alongside | | |
| 17 | | Its history: run logs, drop, cost ledger, uploaded plugin jars, `parent.json`, `plan.json` | `runs/`, `drop/`, `ledger/`, `games/jars/`, 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/` and `ledger/` 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/` 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. | |
| 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 |
scripts/restore-drill.ts +2 −1
| @@ -22,7 +22,7 @@ import { bootRestored } from "../src/snapshot/boot.ts"; | ||
| 22 | 22 | import { resolveSnapshots } from "../src/snapshot/destination.ts"; |
| 23 | 23 | import { offsiteFromEnv } from "../src/snapshot/mirror.ts"; |
| 24 | 24 | import { publicCounts, type PublicCounts } from "../src/snapshot/public_counts.ts"; |
| 25 | import { restoreSnapshot } from "../src/snapshot/restore.ts"; | |
| 25 | import { jarLines, restoreSnapshot } from "../src/snapshot/restore.ts"; | |
| 26 | 26 | import { listSnapshots } from "../src/snapshot/snapshot.ts"; |
| 27 | 27 | import { PREVIEW_ADMIN } from "../src/spawn/preview-looks.ts"; |
| 28 | 28 | |
| @@ -98,6 +98,7 @@ async function main(): Promise<number> { | ||
| 98 | 98 | }); |
| 99 | 99 | console.log(`restored ${done.files} files and ${done.repos} repositories, key ${done.key}, ${seconds()}s`); |
| 100 | 100 | if (done.sealedMissing.length > 0) console.log(`sealed values to set again: ${done.sealedMissing.length}`); |
| 101 | for (const line of jarLines(done.jarsMissing)) console.log(line); | |
| 101 | 102 | |
| 102 | 103 | let restored: PublicCounts | undefined; |
| 103 | 104 | const booted = await bootRestored(args.dataDir, BOOT_MS, undefined, async (base) => { |
scripts/standby-restore.ts +2
| @@ -16,6 +16,7 @@ | ||
| 16 | 16 | */ |
| 17 | 17 | import { resolve } from "node:path"; |
| 18 | 18 | import { resolveSnapshots } from "../src/snapshot/destination.ts"; |
| 19 | import { jarLines } from "../src/snapshot/restore.ts"; | |
| 19 | 20 | import { restoreStandby } from "../src/snapshot/standby.ts"; |
| 20 | 21 | |
| 21 | 22 | async function main(args: readonly string[]): Promise<number> { |
| @@ -38,6 +39,7 @@ async function main(args: readonly string[]): Promise<number> { | ||
| 38 | 39 | try { |
| 39 | 40 | const done = await restoreStandby(store, snapshot, dataDir, process.env["FORGE_SNAPSHOT_PASSPHRASE"] || undefined); |
| 40 | 41 | console.log(`restored ${done.name}: ${done.restored.files} files, ${done.repos} repositories, key ${done.restored.key}`); |
| 42 | for (const line of jarLines(done.restored.jarsMissing)) console.log(line); | |
| 41 | 43 | if (done.problems.length > 0) { |
| 42 | 44 | for (const p of done.problems) console.error(`standby-restore: ${p}`); |
| 43 | 45 | return 1; |
src/games/jars.ts added +37
| @@ -0,0 +1,37 @@ | ||
| 1 | /** | |
| 2 | * Where plugin jars people uploaded are kept, and which of them a data directory lacks. A jar is | |
| 3 | * a row in `game_jar` and a file under `<data>/games/jars`; the snapshot carries both, and a | |
| 4 | * restore says which rows came back without their file. | |
| 5 | */ | |
| 6 | import { Database } from "bun:sqlite"; | |
| 7 | import { existsSync, statSync } from "node:fs"; | |
| 8 | import { join } from "node:path"; | |
| 9 | ||
| 10 | /** Under the data directory, and under the same name in a snapshot. */ | |
| 11 | export const JARS_DIR = "games/jars"; | |
| 12 | ||
| 13 | /** The jars the database at `dataDir` names whose file is missing or of another size, in words. */ | |
| 14 | export function jarsMissing(dataDir: string): string[] { | |
| 15 | let db: Database | undefined; | |
| 16 | try { | |
| 17 | db = new Database(join(dataDir, "forge.db"), { readonly: true }); | |
| 18 | // A database from before jars names none. | |
| 19 | if (db.query("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'game_jar'").get() === null) return []; | |
| 20 | return db | |
| 21 | .query<{ id: string; name: string; bytes: number; box: string | null }, []>( | |
| 22 | "SELECT j.id AS id, j.name AS name, j.bytes AS bytes, b.name AS box FROM game_jar j LEFT JOIN game_box b ON b.id = j.box_id ORDER BY b.name, j.name, j.id", | |
| 23 | ) | |
| 24 | .all() | |
| 25 | .flatMap((j) => { | |
| 26 | const file = join(dataDir, JARS_DIR, `${j.id}.jar`); | |
| 27 | const of = `${j.name} (${j.box === null ? "a box that is gone" : `box ${j.box}`})`; | |
| 28 | if (!existsSync(file)) return [`${of}: no file`]; | |
| 29 | const size = statSync(file).size; | |
| 30 | return size === j.bytes ? [] : [`${of}: ${size} bytes, not ${j.bytes}`]; | |
| 31 | }); | |
| 32 | } catch (error) { | |
| 33 | return [`the jars could not be checked: ${error instanceof Error ? error.message : String(error)}`]; | |
| 34 | } finally { | |
| 35 | db?.close(); | |
| 36 | } | |
| 37 | } |
src/games/store.ts +2 −1
| @@ -8,6 +8,7 @@ | ||
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | 10 | import type { Database } from "bun:sqlite"; |
| 11 | import { JARS_DIR } from "./jars.ts"; | |
| 11 | 12 | import { COMPOSE_AT, sameFile, type ComposeFile } from "./compose_file.ts"; |
| 12 | 13 | import { keptDiag, readDiag, type Diag } from "./diag.ts"; |
| 13 | 14 | import { outcomeOf } from "./outcome.ts"; |
| @@ -475,7 +476,7 @@ export function envFiles(app: App, box: GameBox, compose: string, services: read | ||
| 475 | 476 | export const JAR_URL_MS = 6 * 3_600_000; |
| 476 | 477 | |
| 477 | 478 | export function jarPath(app: App, jarId: string): string { |
| 478 | return `${app.config.dataDir}/games/jars/${jarId}.jar`; | |
| 479 | return `${app.config.dataDir}/${JARS_DIR}/${jarId}.jar`; | |
| 479 | 480 | } |
| 480 | 481 | |
| 481 | 482 | /** What one box keeps: jars, and their bytes together; one jar is at most `JAR_MAX_BYTES`. */ |
src/snapshot/hosted.ts +10 −3
| @@ -21,6 +21,7 @@ | ||
| 21 | 21 | import { closeSync, constants, fstatSync, lstatSync, openSync, readdirSync, readFileSync, readlinkSync, readSync, realpathSync, rmSync } from "node:fs"; |
| 22 | 22 | import { lstat, readdir } from "node:fs/promises"; |
| 23 | 23 | import { dirname, join, resolve, sep } from "node:path"; |
| 24 | import { JARS_DIR } from "../games/jars.ts"; | |
| 24 | 25 | import { runGit } from "../git/env.ts"; |
| 25 | 26 | import { findingWords, scanRepos } from "../spawn/move_scan.ts"; |
| 26 | 27 | import { CHILD_NAME } from "../spawn/placement.ts"; |
| @@ -75,7 +76,7 @@ export interface HostedSource { | ||
| 75 | 76 | export const PARENT_SIDE = "parent/"; |
| 76 | 77 | export const PARENT_FILES: readonly string[] = [`${PARENT_SIDE}credit.json`, `${PARENT_SIDE}own-code.json`]; |
| 77 | 78 | /** What a forge keeps in its own directory beside its database: history and records, whole. */ |
| 78 | export const HISTORY_DIRS = ["runs", "drop", "ledger"] as const; | |
| 79 | export const HISTORY_DIRS = ["runs", "drop", "ledger", JARS_DIR] as const; | |
| 79 | 80 | export const HISTORY_FILES = ["parent.json", "plan.json"] as const; |
| 80 | 81 | |
| 81 | 82 | // A letter or digit first, as `spawn/archive.ts` holds: an id never reads as an option (#14783). |
| @@ -209,7 +210,7 @@ export type HostedCopy = Pick<HostedEntry, "repos" | "key" | "absent" | "omitted | ||
| 209 | 210 | |
| 210 | 211 | /** Names of left-out files a manifest lists; the rest are counted. */ |
| 211 | 212 | export const OMITTED_LISTED = 100; |
| 212 | /** Bytes of runs/, drop/ and ledger/ together a snapshot of one forge carries. */ | |
| 213 | /** Bytes of runs/, drop/, ledger/ and games/jars/ together a snapshot of one forge carries. */ | |
| 213 | 214 | export const HISTORY_BYTES_MAX = 512 * 1_048_576; |
| 214 | 215 | |
| 215 | 216 | type Cut = "content" | "fixed" | "bundle"; |
| @@ -232,7 +233,7 @@ export interface HostedTools { | ||
| 232 | 233 | /** The most files read from the forge's directory; `HOSTED_FILES_MAX` unless a test lowers it. */ |
| 233 | 234 | readonly filesMax?: number | undefined; |
| 234 | 235 | /** |
| 235 | * The most bytes of history (runs, drop, ledger); `HISTORY_BYTES_MAX` unless a test lowers it. | |
| 236 | * The most bytes of history (`HISTORY_DIRS`); `HISTORY_BYTES_MAX` unless a test lowers it. | |
| 236 | 237 | */ |
| 237 | 238 | readonly historyMax?: number | undefined; |
| 238 | 239 | } |
| @@ -574,6 +575,12 @@ export async function copyHosted(child: HostedSource, tools: HostedTools): Promi | ||
| 574 | 575 | }; |
| 575 | 576 | let room = tools.historyMax ?? HISTORY_BYTES_MAX; |
| 576 | 577 | for (const d of HISTORY_DIRS) { |
| 578 | // A directory inside another is listed only when the outer one is the forge's own, not a link. | |
| 579 | const outer = d.split("/").slice(0, -1); | |
| 580 | if (outer.length > 0 && notOwnDir(root, outer) !== undefined) { | |
| 581 | if (lstatSync(join(root, ...outer), { throwIfNoEntry: false }) !== undefined) omitted.push({ path: d, why: "it is not in a directory of the forge's own" }); | |
| 582 | continue; | |
| 583 | } | |
| 577 | 584 | const found = await historyIn(join(root, d), d, left); |
| 578 | 585 | omitted.push(...found.omitted); |
| 579 | 586 | for (const f of found.files) { |
src/snapshot/restore.ts +13 −2
| @@ -45,6 +45,7 @@ import { chmodSync, existsSync, mkdirSync, readdirSync, rmSync } from "node:fs"; | ||
| 45 | 45 | import { open as openFile, type FileHandle } from "node:fs/promises"; |
| 46 | 46 | import { availableParallelism } from "node:os"; |
| 47 | 47 | import { dirname, join } from "node:path"; |
| 48 | import { jarsMissing } from "../games/jars.ts"; | |
| 48 | 49 | import { runGit } from "../git/env.ts"; |
| 49 | 50 | import { installHooks } from "../git/hooks.ts"; |
| 50 | 51 | import { repoDir } from "../git/repos.ts"; |
| @@ -76,8 +77,14 @@ export interface Restored { | ||
| 76 | 77 | readonly key: "restored" | "raw" | "missing"; |
| 77 | 78 | /** Sealed values that will not open without the key, by name. */ |
| 78 | 79 | readonly sealedMissing: readonly string[]; |
| 80 | /** Uploaded plugin jars the database names that did not come back, in words. */ | |
| 81 | readonly jarsMissing: readonly string[]; | |
| 79 | 82 | } |
| 80 | 83 | |
| 84 | /** What a restore says of jars that did not come back; nothing when all did. */ | |
| 85 | export const jarLines = (missing: readonly string[]): string[] => | |
| 86 | missing.length === 0 ? [] : [`${missing.length} uploaded plugin jar(s) did not come back and must be uploaded again:`, ...missing.map((m) => ` ${m}`)]; | |
| 87 | ||
| 81 | 88 | /** Where a restore is, as plain data: `done` of `total` in `phase`, `bytes` so far in it. */ |
| 82 | 89 | export interface Progress { |
| 83 | 90 | readonly phase: "manifest" | "files" | "unpack" | "bundles" | "key"; |
| @@ -596,7 +603,7 @@ export async function restoreSnapshot( | ||
| 596 | 603 | key !== undefined ? "restored" : kind === "raw" ? "raw" : "missing"; |
| 597 | 604 | const sealedMissing = |
| 598 | 605 | have === "missing" ? await sealedNames(join(dataDir, "forge.db")) : []; |
| 599 | return { manifest, files, repos, key: have, sealedMissing }; | |
| 606 | return { manifest, files, repos, key: have, sealedMissing, jarsMissing: jarsMissing(dataDir) }; | |
| 600 | 607 | } |
| 601 | 608 | |
| 602 | 609 | /** What one hosted forge's restore brought back. */ |
| @@ -613,6 +620,8 @@ export interface RestoredHosted { | ||
| 613 | 620 | readonly files: number; |
| 614 | 621 | readonly repos: number; |
| 615 | 622 | readonly key: "restored" | "missing"; |
| 623 | /** As `Restored.jarsMissing`. */ | |
| 624 | readonly jarsMissing: readonly string[]; | |
| 616 | 625 | } |
| 617 | 626 | |
| 618 | 627 | /** |
| @@ -676,7 +685,7 @@ export async function restoreHosted( | ||
| 676 | 685 | } |
| 677 | 686 | // What its parent wrote of it was private to it, and is again. |
| 678 | 687 | if (existsSync(join(dir, "parent.json"))) chmodSync(join(dir, "parent.json"), 0o600); |
| 679 | return { name: forge, dir, ledger: ledgerText, parentFiles, files: got.files, repos: got.bundles.length, key: key === undefined ? "missing" : "restored" }; | |
| 688 | return { name: forge, dir, ledger: ledgerText, parentFiles, files: got.files, repos: got.bundles.length, key: key === undefined ? "missing" : "restored", jarsMissing: jarsMissing(dir) }; | |
| 680 | 689 | } |
| 681 | 690 | |
| 682 | 691 | /** |
| @@ -791,6 +800,7 @@ if (import.meta.main) | ||
| 791 | 800 | try { |
| 792 | 801 | const one = await restoreHostedInto(store, snap, forgeName, dataDir, { passphrase: process.env["FORGE_SNAPSHOT_PASSPHRASE"] || undefined }); |
| 793 | 802 | console.log(`restored ${forgeName} from ${snap}: ${one.files} files, ${one.repos} repositories into ${one.dir}${one.key === "missing" ? "; its key was not restored" : ""}`); |
| 803 | for (const line of jarLines(one.jarsMissing)) console.log(line); | |
| 794 | 804 | process.exit(0); |
| 795 | 805 | } catch (e) { |
| 796 | 806 | // CLI message on stderr for the person at the terminal. |
| @@ -819,4 +829,5 @@ if (import.meta.main) | ||
| 819 | 829 | ); |
| 820 | 830 | for (const n of done.sealedMissing) console.log(` ${n}`); |
| 821 | 831 | } |
| 832 | for (const line of jarLines(done.jarsMissing)) console.log(line); | |
| 822 | 833 | })(); |
src/snapshot/snapshot.ts +5
| @@ -40,6 +40,7 @@ import { vacuumFile, vacuumInto } from "./vacuum.ts"; | ||
| 40 | 40 | import { closeSync, createReadStream, fstatSync, lstatSync, mkdtempSync, openSync, readdirSync, readSync, rmSync, statSync } from "node:fs"; |
| 41 | 41 | import { tmpdir } from "node:os"; |
| 42 | 42 | import { join, relative } from "node:path"; |
| 43 | import { JARS_DIR } from "../games/jars.ts"; | |
| 43 | 44 | import { envDataDir } from "../apps/data.ts"; |
| 44 | 45 | import { listApps } from "../core/apps.ts"; |
| 45 | 46 | import { listEveryRepo } from "../core/repos.ts"; |
| @@ -787,6 +788,10 @@ export async function takeSnapshot( | ||
| 787 | 788 | for (const f of streaming ? [] : filesUnder(dropped)) |
| 788 | 789 | if (!f.split("/").some((part) => part.startsWith("."))) await putFile(`drop/${f}`, join(dropped, f)); |
| 789 | 790 | |
| 791 | // Plugin jars people uploaded for their game servers (`games/store.ts`): kept nowhere else. | |
| 792 | const jars = join(forge.dataDir, JARS_DIR); | |
| 793 | for (const f of streaming ? [] : filesUnder(jars)) await putFile(`${JARS_DIR}/${f}`, join(jars, f)); | |
| 794 | ||
| 790 | 795 | // The crew's run logs: history a person may want back. A day's worth to |
| 791 | 796 | // a few bundles, in the order the logs were last written: a log still |
| 792 | 797 | // growing is in the day's last bundle, and the bundles before it are the |
test/drill-hosted-red.test.ts +1 −1
| @@ -57,7 +57,7 @@ const restore: NonNullable<DrillDeps["restore"]> = async (_store, _name, target) | ||
| 57 | 57 | db.run("CREATE TABLE secret (name TEXT, scope TEXT, value_enc TEXT)"); |
| 58 | 58 | db.run("CREATE TABLE app_env (app_id TEXT, name TEXT, value_enc TEXT)"); |
| 59 | 59 | db.close(); |
| 60 | return { manifest, files: 1, repos: 0, key: "missing", sealedMissing: [] }; | |
| 60 | return { manifest, files: 1, repos: 0, key: "missing", sealedMissing: [], jarsMissing: [] }; | |
| 61 | 61 | }; |
| 62 | 62 | |
| 63 | 63 | let root: string; |
test/jar-backup.test.ts added +66
| @@ -0,0 +1,66 @@ | ||
| 1 | /** | |
| 2 | * The backup holds the plugin jars people uploaded for their game servers (`games/jars/<id>.jar`), | |
| 3 | * and a restore says, by name, each one whose row came back without its file. | |
| 4 | */ | |
| 5 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 6 | import { mkdtempSync, readFileSync, rmSync } 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 { createGameBox, jarPath, storeJar } from "../src/games/store.ts"; | |
| 12 | import { sha256 } from "../src/kernel/hash.ts"; | |
| 13 | import { restoreSnapshot } from "../src/snapshot/restore.ts"; | |
| 14 | import { takeSnapshot } from "../src/snapshot/snapshot.ts"; | |
| 15 | ||
| 16 | let root: string; | |
| 17 | let app: App; | |
| 18 | let jar: string; | |
| 19 | const stored = (): string => join(root, "store"); | |
| 20 | const snap = (at: string) => | |
| 21 | takeSnapshot({ db: app.db, dataDir: app.config.dataDir, reposRoot: app.reposRoot, version: "t" }, dirStore(stored()), new Date(at)); | |
| 22 | ||
| 23 | /** The restore command as a person runs it: what it printed. */ | |
| 24 | async function restoreCommand(name: string, into: string): Promise<string> { | |
| 25 | const p = Bun.spawn([process.execPath, "run", join(import.meta.dir, "../src/snapshot/restore.ts"), name, into], { | |
| 26 | env: { PATH: process.env["PATH"] ?? "", HOME: root, FORGE_SNAPSHOT_DIR: stored() }, | |
| 27 | stdout: "pipe", | |
| 28 | stderr: "pipe", | |
| 29 | }); | |
| 30 | const [out, err, code] = await Promise.all([new Response(p.stdout).text(), new Response(p.stderr).text(), p.exited]); | |
| 31 | expect(`${code} ${err}`.trim()).toBe("0"); | |
| 32 | return out; | |
| 33 | } | |
| 34 | ||
| 35 | beforeAll(async () => { | |
| 36 | root = mkdtempSync(join(tmpdir(), "forge-jar-backup-")); | |
| 37 | app = createApp({ dataDir: join(root, "live"), port: 0 }); | |
| 38 | const box = createGameBox(app.db, { name: "mc" }); | |
| 39 | // A zip's first bytes, then every byte value, past one chunk's smallest size. | |
| 40 | const body = Uint8Array.from({ length: 70_001 }, (_, i) => [0x50, 0x4b, 0x03, 0x04][i] ?? (i * 7) % 256); | |
| 41 | const made = await storeJar(app, box.id, new Blob([body]), "Hello.jar"); | |
| 42 | if (!made.ok) throw new Error(made.error); | |
| 43 | jar = made.id; | |
| 44 | }); | |
| 45 | afterAll(() => { | |
| 46 | app.close(); | |
| 47 | rmSync(root, { recursive: true, force: true }); | |
| 48 | }); | |
| 49 | ||
| 50 | test("an uploaded jar comes back from a restore with its row's sha256", async () => { | |
| 51 | const m = await snap("2026-12-02T08:00:00Z"); | |
| 52 | const back = join(root, "back"); | |
| 53 | const done = await restoreSnapshot(dirStore(stored()), m.name, back); | |
| 54 | const row = app.db.query<{ sha256: string }, [string]>("SELECT sha256 FROM game_jar WHERE id = ?1").get(jar)!; | |
| 55 | expect(sha256(readFileSync(join(back, "games", "jars", `${jar}.jar`)))).toBe(row.sha256); | |
| 56 | expect(done.jarsMissing).toEqual([]); | |
| 57 | expect(await restoreCommand(m.name, join(root, "said-nothing"))).not.toContain("did not come back"); | |
| 58 | }, 30_000); | |
| 59 | ||
| 60 | test("a jar whose file is in no backup is named by the restore, never passed over", async () => { | |
| 61 | rmSync(jarPath(app, jar)); | |
| 62 | const m = await snap("2026-12-02T09:00:00Z"); | |
| 63 | const said = await restoreCommand(m.name, join(root, "without")); | |
| 64 | expect(said).toContain("1 uploaded plugin jar(s) did not come back and must be uploaded again:"); | |
| 65 | expect(said).toContain("Hello.jar (box mc): no file"); | |
| 66 | }, 30_000); |
test/snapshot-hosted-history.test.ts +3 −1
| @@ -22,9 +22,10 @@ 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"]) mkdirSync(join(dir, d), { recursive: true }); | |
| 25 | for (const d of ["runs/r1", "drop", "ledger", "games/jars"]) 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 | writeFileSync(join(dir, "games", "jars", "j1.jar"), "a jar\n"); | |
| 28 | 29 | writeFileSync(join(dir, "ledger", "2026-11.jsonl"), '{"usd":1.25}\n'); |
| 29 | 30 | writeFileSync(join(dir, "parent.json"), '{"name":"kid"}', { mode: 0o600 }); |
| 30 | 31 | writeFileSync(join(dir, "plan.json"), '{"plan":"free"}'); |
| @@ -51,6 +52,7 @@ test("a restored hosted forge has its runs, drop, ledger, records, spend and swi | ||
| 51 | 52 | const at = join(back, "children", "b1"); |
| 52 | 53 | expect(readFileSync(join(at, "runs", "r1", "log.txt"), "utf8")).toBe("ran the thing\n"); |
| 53 | 54 | expect(readFileSync(join(at, "drop", "note.txt"), "utf8")).toBe("dropped\n"); |
| 55 | expect(readFileSync(join(at, "games", "jars", "j1.jar"), "utf8")).toBe("a jar\n"); | |
| 54 | 56 | expect(readFileSync(join(at, "ledger", "2026-11.jsonl"), "utf8")).toContain("1.25"); |
| 55 | 57 | expect(readFileSync(join(at, "plan.json"), "utf8")).toContain("free"); |
| 56 | 58 | expect(statSync(join(at, "parent.json")).mode & 0o077).toBe(0); |
test/worker-database-opens.test.ts +1
| @@ -59,6 +59,7 @@ const REACHED: Record<string, { opens: number; why: string }> = { | ||
| 59 | 59 | "src/apps/data.ts": { opens: 1, why: "quiesceSqlite folds each app database's log before a copy: called by the serving thread's clone and backup paths, in reach of Workers only through the page layer's imports" }, |
| 60 | 60 | "src/apps/dump_import.ts": { opens: 3, why: "a person's import of a dump, on the serving thread behind a request; no Worker calls it" }, |
| 61 | 61 | "src/crew/sentinel.ts": { opens: 1, why: "one scratch database in memory, made once for the process and remembered; the sentinel is a duty of the serving thread" }, |
| 62 | "src/games/jars.ts": { opens: 1, why: "a restore reads the jar rows of the restored copy once, in the restore tool or a drill, beside the sealed names in snapshot/restore.ts; no Worker calls it" }, | |
| 62 | 63 | "src/kernel/db.ts": { opens: 2, why: "the forge's own database, opened once by createApp on the thread that boots; the Workers that read it open their own handle and keep it (KEEPS)" }, |
| 63 | 64 | "src/release/rollback.ts": { opens: 1, why: "liveSchema reads the schema count once for a rollback or a swap, on the serving thread or in the release tool" }, |
| 64 | 65 | "src/snapshot/drill.ts": { opens: 1, why: "a restore drill opens the restored copy once, as a duty of the serving thread" }, |
new above
offline
The size-mismatch message is never exercised by a test. Add a case where the file is truncated after the upload.