/** * RESTORE 2, THE OWNER'S OWN RESTORE (RESTORE PHONE 2, 28 Nov 2026; assets/design/shared/RESTORE-2-CONTRACT.md 8). * The owner, from their own signed-in phone, puts the account back to a day the snapshots kept (src/snapshots.ts: * a weekly full and the dailies after it). The phone only asks and shows: the server freezes, checks every part, * lays the rows back, replays the never-again ledger, raises the version and writes a new generation, exactly as the * admin's restore from snapshots does (one code path, `snapshotRestoreBegin` / `snapshotRestoreStep`), then unfreezes * by itself. Each phone then sees the new generation and makes its copy again (contract 4 and 6). * * GET /v1/restore the days that can be restored, newest first, and where a restore stands * POST /v1/restore/begin { point, anyway? }: freeze, check and begin; refused once the account's delete started * POST /v1/restore/step a few parts more (the phone's progress; the alarm also steps while no phone asks) * GET /v1/restore/state where it stands, read only * POST /v1/restore/cancel while it is still checking (no row touched yet): taken back, the account unfrozen * * THE OFF SWITCH. Answered only where the restore routes are (`restoreRoutesOn`: the dev and test Workers); anywhere * else every one is 404 `not_found`, so live answers nothing new. Snapshots are off by default per account (the admin * switches them), so an account with none lists no day. * * WHO. Only the owner's own phone, signed in with its device credential (a session `whoIs` names, role owner). An * assistant's key (`ctn_`), the admin token, another person's phone (a member) and no credential are refused: 403 * `owner_device_only` (401 for a credential that does not sign in). Nothing here serves a snapshot's bytes: the parts * never leave the server, the rows are laid back inside the object, and sealed rows stay sealed (the phone's keys are * never asked for and never sent). */ import type * as snapshots from "./snapshots.ts"; /** One day the snapshots can put the account back to: the weekly full and the daily that ends at it. */ export type Point = { id: string; to: string; full: string; through: string }; /** The id a point travels under: opaque to the phone. */ export const pointId = (full: string, through: string): string => `${full}~${through}`; /** * The days that can be restored, newest first: each daily that follows a weekly full in an unbroken chain (each * daily beginning at the version the copy before it ended) is one; a full alone is none (L2: a full is a mix of * moments). `restorable` bounds them by the never-again ledger's reach (40 days). */ export function pointsOf(fulls: snapshots.Manifest[], dailies: snapshots.Manifest[], restorable: (at: string) => boolean): Point[] { const out: Point[] = []; const ordered = [...dailies].sort((a, b) => a.stamp.localeCompare(b.stamp)); for (const full of fulls) { if (!restorable(full.started_at)) continue; let upto = full.upto; for (const daily of ordered) { if (daily.stamp <= full.stamp) continue; if (daily.base !== upto) break; upto = daily.upto; out.push({ id: pointId(full.stamp, daily.stamp), to: daily.started_at, full: full.stamp, through: daily.stamp }); } } return out.sort((a, b) => b.to.localeCompare(a.to) || b.id.localeCompare(a.id)); } /** Where an owner's restore stands, in its order: none, checking every part, laying the rows back. `held`: the account is frozen by the operator. */ export type Stage = "none" | "checking" | "laying" | "held"; /** The job a restore from snapshots keeps in meta (src/business.ts `SnapshotRestore`), as far as the owner sees it. */ export type Job = { phase: "verify" | "lay"; plan: { parts: unknown[] }[]; i: number; j: number; restored_to: string; rows: number; checked: number }; /** Parts done of the whole, over both stages: checking counts half, laying the other half. */ export function progressOf(job: Job): { parts: number; checked: number; laid: number; done: number; total: number } { const parts = job.plan.reduce((n, p) => n + p.parts.length, 0); const before = job.plan.slice(0, job.i).reduce((n, p) => n + p.parts.length, 0) + job.j; const checked = job.phase === "verify" ? Math.min(job.checked, parts) : parts; const laid = job.phase === "lay" ? Math.min(before, parts) : 0; return { parts, checked, laid, done: checked + laid, total: parts * 2 }; } /** Who began an owner's restore, kept in the restore marker (outside the object) while it is under way. */ export type Owner = { device: string; point: string; to: string; started_at: string }; /** How long the alarm waits before it steps an owner's restore no phone is stepping. */ export const ALARM_STEP_MS = 30_000; /** How long one step works: a phone's call, or the alarm's. */ export const STEP_BUDGET_MS = 5_000;