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

A weekly restore drill: the newest snapshot restored into an empty directory, opened and its sealed values read, the verdict on the Backups page #1189

Closed opened this
Closed without merging

The forge now runs a restore drill every Sunday at 09:00 UTC, an hour after the nightly snapshot, and an admin can run one from the Backups page. bun x tsc --noEmit is clean and bun test test/snapshot-drill.test.ts passes (7 tests). I did not run bun test across the whole suite.

Notes

stopped closed without shipping deploy 840a45a

opened this

The forge now runs a restore drill every Sunday at 09:00 UTC, an hour after the nightly snapshot, and an admin can run one from the Backups page. bun x tsc --noEmit is clean and bun test test/snapshot-drill.test.ts passes (7 tests). I did not run bun test across the whole suite. This sandbox forbids opening the unix sockets the page tests need, so two page tests fail here: snapshot-destination.test.ts and the backups-page test in snapshot.test.ts. The snapshot-destination.test.ts failure also occurs without my change; I did not re-run the other one without it.

What changed

  • The drill: src/snapshot/drill.ts restores the newest snapshot into a fresh directory under $FORGE_APP_DATA/drill/. It opens the database, counts repositories and issues, and opens one sealed value with the restored key. It then deletes the directory, including when it fails.
  • Timeout: the drill gives up after 15 minutes. restoreSnapshot takes an optional abort signal and checks it between files, so a hung store ends as a red verdict instead of a stuck job.
  • Disk check: it refuses to start when free disk is under twice the snapshot's size, and says so as the verdict.
  • Records: each drill stores its time, snapshot, duration, repository, issue and sealed-value counts, verdict, and for a red one the failing step and reason. They are kept in snapshot-drills.json under the data directory, 20 at most.
  • Backups page: one line shows the last drill, green with its counts or red with the failing step. A "Run restore drill" button posts to POST /settings/backups/drill, which is admin-only and goes through the same form check as the snapshot-now button.
  • Docs and tests: docs/SNAPSHOT.md has a new section. test/snapshot-drill.test.ts covers green, wrong passphrase, no passphrase, no snapshot, hang, low disk and the weekly clock. The red cases name the step: restore, unseal, fetch, timeout and disk.

Choices

  • A wrong passphrase shows as a restore failure, because that is where the passphrase is checked.
  • A forge with no sealed values has nothing to unseal and still gets a green verdict.
  • Records are a JSON file rather than a database table, so there is no migration.

Not done

  • Nothing warns anyone about a red drill beyond the Backups page and the log.
  • A restore that hangs inside a single store read is not interrupted until that read returns. The verdict and the cleanup do not wait for it.

Try it: open Settings → Backups as an admin and press "Run restore drill". Or run bun test test/snapshot-drill.test.ts.

Closes #1182

1 earlier comment · 1 event
Changes requested — before the latest push

The drill itself is good work. It restores into a fresh directory under FORGE_APP_DATA/drill, every red verdict names its step, and the directory is removed on every path. The route is admin-only and goes through readForm's same-origin check. Output goes through the escaping html template. No SQL or shell is built from user text. bun test test/snapshot-drill.test.ts passes at 5ba80f38 (7 tests).

It does not yet meet #1182's done list. Fix first: that issue says "The last drill is on the Backups page and through the MCP (health carries drill: {at, verdict, step}); an admin can start one from the page and the MCP." Neither half is in this pull, and src/http/mcp.ts is not changed. Do two things:

  • Add drill: {at, ok, step} to the admin part of readHealth. Read it from snapshotsOf(app)?.lastDrill().
  • Add an admin-gated MCP tool with writes: true that starts a drill. Gate it the way the page does (requireAdmin).

Test both: a guest or non-admin gets no drill field and is refused by the tool, and an admin gets both.

Also needed in this round:

  1. The POST waits for the whole drill (up to 15 minutes). A real restore of a full forge will outlast the proxy's request timeout, and the admin sees an error page while the drill keeps running. Start the drill and redirect at once with a "drill started" flash. Show "running since …" on the page while it is in flight. The MCP tool should behave the same way.
  2. No test reaches the page or the route. Nothing checks that drillLine shows the green and red lines, or that POST /settings/backups/drill refuses a non-admin and a cross-origin post. test/snapshot.test.ts already tests the Backups page this way, so add the cases there.
  3. stop() can hold shutdown for 15 minutes. It awaits an in-flight drill, so a deploy during a drill waits for it. Abort the drill on stop instead (pass the controller's signal in through runDrill). A scratch restore is not worth holding a rollout for.
  4. Small race on timeout. On timeout, rmSync(dir) runs while the restore may still be writing a file it just fetched. Bun.write creates missing parent directories, so a partial tree can come back under drill/. Clear old drill/* entries when a drill starts.

Leave out of this pull, as the direction says: a terminal (src/tui) door for Backups. The terminal has no Backups screen at all today, so that gap is older than this pull.

I could not use the preview (lab--1189--… is blocked from my sandbox) or read the forge's screenshots (outside my working directory). The "by eye" item and the screenshot #1182 asks for are still to be shown in the pull.

commented

The restore drill now runs in the background, shows up in health, and can be started from MCP. I couldn't run the new page, route, health and MCP test here, so those assertions are unverified.

That test is in test/snapshot.test.ts. The sandbox refuses to create the server's unix socket (EPERM), and the existing Backups page test fails the same way here. test/snapshot-drill.test.ts (8 tests, including the new abort and sweep case) and the type check pass.

What changed

  • Background drill: POST /settings/backups/drill starts the drill and redirects at once with "Restore drill started". If one is already running, it says so. With no store, it flashes an error. While a drill is in flight the page shows "Restore drill running since …".
  • Health: an admin's health carries drill: {at, ok, step}. It is read from snapshotsOf(app)?.lastDrill() and is null before any drill has run. Guests and non-admins get no drill field.
  • MCP: a new restore_drill tool (writes: true) starts a drill the same non-blocking way. It refuses non-admins and a forge with no store.
  • Shutdown: runDrill takes an AbortSignal, and stop() aborts the drill instead of waiting for it. The drill ends as a red verdict at timeout ("the forge is shutting down").
  • Timeout race: every drill sweeps old drill/* entries before it starts, so a tree recreated by a late write can't pile up.
  • Tests:
    • Page and route: the page covers the no-drill line, the red "failed at" line, the green line and the "running since" state. The route test covers a non-admin getting 404 and a cross-origin post being refused.
    • Health and MCP: health shows drill to an admin only. MCP refuses a non-admin and starts a drill for an admin.
    • Abort and sweep: a drill test covers an abort ending the drill red and the sweep clearing a leftover tree.

Not done

  • No terminal (src/tui) door for Backups, as you directed.
  • I haven't looked at the preview, so the "by eye" item and the screenshot from #1182 are still to be shown.
  • The page has no auto-refresh; you reload to see the verdict.

To try it, set a snapshot store, open /settings/backups as an admin and press "Run restore drill". Then reload the page, or call health or restore_drill over MCP.

commented

Parked by the crew: the check failed ("a site admin with a read-only token gets the shape; others get the short one") and the decider saw no point in another try

Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs