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

The drill's proof of one hosted forge has a test: a fixture forge restores and reports ok, a corrupted one names the step that failed #14190

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/14183-the-drill-s-proof-of-one-hosted-forge-ha → main 1 commit fork

  • Merged. scout/14183-the-drill-s-proof-of-one-hosted-forge-ha was merged into main as 6cd1ce7.

Review

Sign in to review this change.
2 files changed +174 −5
src/snapshot/drill_hosted.ts +13 −5
@@ -67,13 +67,21 @@ export async function proveHosted(
6767 }
6868 if (kept.name !== forge.name) throw new Error(`hosted forge ${forge.name}: the ledger in its copy is not its own`);
6969
70 const db = new Database(join(dir, "forge.db"), { readonly: true });
70 // A page too spoiled to walk makes SQLite throw rather than answer: that is the check failing
71 // too, and the drill says so by the forge's name, not in SQLite's bare words.
72 let passed: boolean;
7173 try {
72 const checked = db.query<{ integrity_check: string }, []>("PRAGMA integrity_check").all();
73 if (checked.length !== 1 || checked[0]?.integrity_check !== "ok") throw new Error(`hosted forge ${forge.name}: its database does not pass the integrity check`);
74 } finally {
75 db.close();
74 const db = new Database(join(dir, "forge.db"), { readonly: true });
75 try {
76 const checked = db.query<{ integrity_check: string }, []>("PRAGMA integrity_check").all();
77 passed = checked.length === 1 && checked[0]?.integrity_check === "ok";
78 } finally {
79 db.close();
80 }
81 } catch {
82 passed = false;
7683 }
84 if (!passed) throw new Error(`hosted forge ${forge.name}: its database does not pass the integrity check`);
7785
7886 let repos = 0;
7987 const root = join(dir, "repos");
test/drill-hosted-proof.test.ts added +161
@@ -0,0 +1,161 @@
1 /**
2 * The restore drill's proof of one hosted forge (`proveHosted`, #13923, #14183), on a fixture small
3 * enough to stay under a second: a snapshot written by hand into a store in memory, holding one
4 * hosted forge of a database and one bare repository. A whole one restores and reports its name,
5 * repositories and bytes; a copy whose database, repository or count does not hold fails, naming
6 * the forge and the step.
7 */
8 import { Database } from "bun:sqlite";
9 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
10 import { mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
11 import { tmpdir } from "node:os";
12 import { join } from "node:path";
13 import { runGit } from "../src/git/env.ts";
14 import { chunkId, chunkKey, packChunk } from "../src/snapshot/chunks.ts";
15 import { proveHosted } from "../src/snapshot/drill_hosted.ts";
16 import type { HostedEntry } from "../src/snapshot/hosted.ts";
17 import { SNAPSHOT_PREFIX, type Manifest, type SnapshotFile } from "../src/snapshot/snapshot.ts";
18 import type { Store } from "../src/snapshot/store.ts";
19
20 const SNAP = "2026-11-16T08-00-00Z";
21
22 function memStore(): Store & { objects: Map<string, Uint8Array> } {
23 const objects = new Map<string, Uint8Array>();
24 return {
25 where: "memory",
26 objects,
27 put: async (key, body) => void objects.set(key, typeof body === "string" ? new TextEncoder().encode(body) : new Uint8Array(body)),
28 get: async (key) => objects.get(key),
29 list: async (prefix) => [...objects.keys()].filter((k) => k.startsWith(prefix)),
30 names: async () => [],
31 delete: async (key) => void objects.delete(key),
32 };
33 }
34
35 const git = async (args: string[], cwd?: string): Promise<string> => {
36 const r = await runGit(args, cwd === undefined ? {} : { cwd });
37 if (!r.ok) throw new Error(r.error.stderr);
38 return r.value;
39 };
40
41 let root: string;
42 let dbBytes: Uint8Array;
43 let bundleBytes: Uint8Array;
44 let made = 0;
45 const fresh = (): string => join(root, `back-${++made}`);
46
47 /**
48 * A snapshot holding forge `name` of these files, each one chunk; `repos` is what its entry says.
49 */
50 async function snapshotOf(name: string, files: Record<string, Uint8Array>, repos = 1): Promise<{ store: Store; entry: HostedEntry }> {
51 const store = memStore();
52 const listed: SnapshotFile[] = [];
53 let bytes = 0;
54 for (const [path, body] of Object.entries(files)) {
55 const id = chunkId(body);
56 await store.put(chunkKey(id), await packChunk(body));
57 listed.push({ path: `children/${name}/${path}`, bytes: body.byteLength, sha256: id, chunks: [id] });
58 bytes += body.byteLength;
59 }
60 const entry: HostedEntry = { name, at: "2026-11-16T08:00:00Z", repos, bytes, key: "none" };
61 const manifest: Manifest = {
62 format: 2,
63 name: SNAP,
64 startedAt: entry.at,
65 finishedAt: entry.at,
66 version: "t",
67 repos: 0,
68 apps: 0,
69 files: listed,
70 bytes,
71 hosted: [entry],
72 };
73 await store.put(`${SNAPSHOT_PREFIX}${SNAP}/manifest.json`, JSON.stringify(manifest));
74 return { store, entry };
75 }
76
77 const ledger = (name: string): Uint8Array => new TextEncoder().encode(JSON.stringify({ name, bodyId: `body-${name}`, done: true }));
78 const whole = (name: string): Record<string, Uint8Array> => ({ "ledger.json": ledger(name), "forge.db": dbBytes, [`repos/${name}/notes.bundle`]: bundleBytes });
79
80 beforeAll(async () => {
81 root = mkdtempSync(join(tmpdir(), "forge-drill-hosted-proof-"));
82 // A database of a few pages: a table and its index, so a page can be spoiled without its header.
83 const dbFile = join(root, "fixture.db");
84 const db = new Database(dbFile);
85 db.run("PRAGMA journal_mode = OFF");
86 db.run("PRAGMA synchronous = OFF");
87 db.run("CREATE TABLE note (id INTEGER PRIMARY KEY, body TEXT)");
88 db.run("CREATE INDEX note_body ON note (body)");
89 const add = db.prepare("INSERT INTO note (body) VALUES (?)");
90 // One transaction: each insert alone would wait on its own sync.
91 db.transaction(() => {
92 for (let i = 0; i < 200; i++) add.run(`note ${i} ${"x".repeat(40)}`);
93 })();
94 db.close();
95 dbBytes = new Uint8Array(readFileSync(dbFile));
96 // One repository of one commit, as a bundle.
97 const work = join(root, "work");
98 mkdirSync(work);
99 await git(["init", "--quiet", "-b", "main", work]);
100 writeFileSync(join(work, "README.md"), "hello\n");
101 await git(["add", "README.md"], work);
102 await git(["-c", "user.name=t", "-c", "user.email=t@t", "commit", "--quiet", "-m", "first"], work);
103 await git(["bundle", "create", "--quiet", join(root, "notes.bundle"), "--all"], work);
104 bundleBytes = new Uint8Array(readFileSync(join(root, "notes.bundle")));
105 });
106 afterAll(() => rmSync(root, { recursive: true, force: true }));
107
108 describe("proveHosted", () => {
109 test("a whole hosted forge restores and reports its name, repositories and bytes", async () => {
110 const { store, entry } = await snapshotOf("alpha", whole("alpha"));
111 const dir = fresh();
112 const proof = await proveHosted(store, SNAP, entry, dir, { of: 3 });
113 expect(proof).toEqual({ name: "alpha", of: 3, repos: 1, bytes: entry.bytes });
114 expect(readdirSync(join(dir, "repos", "alpha"))).toEqual(["notes.git"]);
115 });
116
117 test("a copy whose database is corrupted fails the integrity check, naming the forge", async () => {
118 // One letter of one row's text changed in place: the file opens and reads, and only the
119 // integrity check finds that the index no longer matches its table.
120 const reworded = new Uint8Array(dbBytes);
121 const at = Buffer.from(reworded).lastIndexOf("note 150 ");
122 expect(at).toBeGreaterThan(0);
123 reworded[at] = "m".charCodeAt(0);
124 const quiet = await snapshotOf("bravo", { ...whole("bravo"), "forge.db": reworded });
125 await expect(proveHosted(quiet.store, SNAP, quiet.entry, fresh(), { of: 1 })).rejects.toThrow("hosted forge bravo: its database does not pass the integrity check");
126
127 // A page past the schema overwritten whole: SQLite cannot walk it and throws, which is the
128 // check failing too, and said the same way.
129 const page = new DataView(dbBytes.buffer, dbBytes.byteOffset).getUint16(16);
130 const pages = dbBytes.byteLength / page;
131 expect(pages).toBeGreaterThan(3);
132 const spoiled = new Uint8Array(dbBytes);
133 spoiled.fill(0x41, (pages - 1) * page + 100, pages * page);
134 const loud = await snapshotOf("bravo", { ...whole("bravo"), "forge.db": spoiled });
135 await expect(proveHosted(loud.store, SNAP, loud.entry, fresh(), { of: 1 })).rejects.toThrow("hosted forge bravo: its database does not pass the integrity check");
136 });
137
138 test("a copy whose repository is corrupted fails git fsck, naming the repository", async () => {
139 const { store, entry } = await snapshotOf("charlie", whole("charlie"));
140 // The bundle is whole, so the clone succeeds; the restored repository then loses its objects'
141 // bytes on disk, as a repository rotted after its restore would, before its fsck.
142 const spoil: typeof runGit = async (args, options) => {
143 if (args.includes("fsck") && options?.cwd !== undefined) {
144 const packs = join(options.cwd, "objects", "pack");
145 // Git writes its packs read-only: the spoiled one takes the place of the old.
146 for (const f of readdirSync(packs))
147 if (f.endsWith(".pack")) {
148 rmSync(join(packs, f));
149 writeFileSync(join(packs, f), "PACK spoiled");
150 }
151 }
152 return runGit(args, options);
153 };
154 await expect(proveHosted(store, SNAP, entry, fresh(), { of: 1, git: spoil })).rejects.toThrow("hosted forge charlie: repository charlie/notes does not pass git fsck");
155 });
156
157 test("a copy whose repository count differs names both counts", async () => {
158 const { store, entry } = await snapshotOf("delta", whole("delta"), 2);
159 await expect(proveHosted(store, SNAP, entry, fresh(), { of: 1 })).rejects.toThrow("hosted forge delta: 1 repositories came back, its copy holds 2");
160 });
161 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs