feat: restore history, undo and redo in the Activity log for everyone who may refresh
- Activity lists restores from refresh runs, so the Refresh permission is enough to see them; an administrator's restore jobs add the database, the person and the snapshot - an undo is listed under the restore it reverses, and a redo under that undo; opening a restore or an undo opens its undo or redo dialog - the dialog has an explicit undo mode: it describes what is put back instead of the snapshot that was open, and an interrupted restore offers Roll back - a restore whose safety backup retention has forgotten shows "Safety backup expired" instead of a link, and the dialog says how long a safety copy is kept Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,8 +1,12 @@
|
||||
import type { CaptureJob, Orbit, RestoreJob, Snapshot } from "../api";
|
||||
import type { CaptureJob, Orbit, RefreshRun, RestoreJob, Snapshot } from "../api";
|
||||
import { formatBytes, formatWhen } from "./format.ts";
|
||||
import { undoDepth } from "./restoreRun.ts";
|
||||
|
||||
/** Where an entry leads. Restores and exports start from a snapshot, so that is where most entries go. */
|
||||
export type ActivityTarget = { kind: "snapshot" | "orbit"; id: string };
|
||||
export type ActivityTarget =
|
||||
| { kind: "snapshot" | "orbit"; id: string }
|
||||
/** A restore (or undo) that can still be put back: opens the undo (or redo) dialog for this run. */
|
||||
| { kind: "undo" | "redo"; run: RefreshRun };
|
||||
|
||||
export type ActivityEntry = {
|
||||
id: string;
|
||||
@@ -17,8 +21,19 @@ export type ActivityEntry = {
|
||||
opens: ActivityTarget | null;
|
||||
/** Position along the five phases of a job in flight, 0 to 4. */
|
||||
progress?: number;
|
||||
/** Said in place of the link, when the entry leads nowhere on purpose (for instance, its safety backup expired). */
|
||||
note?: string;
|
||||
/** An undo (a restore put back) or a redo (an undo put back), shown under the restore it reverses. */
|
||||
variant?: "undo" | "redo";
|
||||
/** The undo or redo of this restore, oldest first. They are listed under it, not on their own. */
|
||||
children?: ActivityEntry[];
|
||||
/** Where the entry sits in the log: its own group and time, or the most urgent and newest of its subtree. */
|
||||
slotGroup: "needs" | "running" | "history";
|
||||
slotAt: number | null;
|
||||
};
|
||||
|
||||
type Draft = Omit<ActivityEntry, "slotGroup" | "slotAt" | "children"> & { children?: Draft[] };
|
||||
|
||||
const captureLabels: Record<string, string> = {
|
||||
queued: "Waiting for outpost",
|
||||
claimed: "Assigned to agent",
|
||||
@@ -41,6 +56,7 @@ const restoreLabels: Record<string, string> = {
|
||||
wrong_target: "Target identity check failed",
|
||||
invalid_payload: "Archive check failed",
|
||||
safety_failed: "Safety copy could not be verified",
|
||||
rolled_back: "Rolled back",
|
||||
apply_failed: "Apply failed",
|
||||
lease_lost: "Agent disconnected during apply",
|
||||
reconciled: "Recovery verified",
|
||||
@@ -48,23 +64,36 @@ const restoreLabels: Record<string, string> = {
|
||||
};
|
||||
const restoreOrder: string[][] = [["queued", "claimed"], ["checking"], ["safety"], ["ready", "applying"], ["completed", "reconciled"]];
|
||||
|
||||
const EXPIRED = "Safety backup expired";
|
||||
|
||||
const shortId = (id: string) => id.slice(0, 8);
|
||||
const inFlight = (status: string) => status === "queued" || status === "running" || status === "applying";
|
||||
|
||||
function restoreEntry(job: RestoreJob, snapshots: Map<string, Snapshot>): ActivityEntry {
|
||||
function restoreEntry(job: RestoreJob, snapshots: Map<string, Snapshot>, run: RefreshRun | undefined, runs: Map<string, RefreshRun>): Draft {
|
||||
const source = snapshots.get(job.snapshot_id);
|
||||
const from = source ? `${source.orbit_name} · ${formatWhen(source.taken_at)}` : `Snapshot ${shortId(job.snapshot_id)}`;
|
||||
const phase = restoreLabels[job.phase] ?? job.status;
|
||||
const depth = run ? undoDepth(run, runs) : 0;
|
||||
// What this row did (an undo, then a redo of it, then an undo of that) and what opening it offers are opposites.
|
||||
const variant = depth === 0 ? undefined : depth % 2 === 1 ? "undo" as const : "redo" as const;
|
||||
const where = `${job.target.name} / ${job.target.identity.database}`;
|
||||
// An undo restores the safety copy of an earlier restore, which has no snapshot to open: it opens its own dialog.
|
||||
// A restore whose safety backup retention has forgotten says so, instead of leading anywhere that suggests it is there.
|
||||
const expired = run?.undo_state === "expired";
|
||||
const opens: ActivityTarget | null = expired ? null : run?.undo_available ? { kind: variant === "undo" ? "redo" : "undo", run }
|
||||
: source ? { kind: "snapshot", id: source.id } : null;
|
||||
const base = {
|
||||
id: `restore-${job.id}`,
|
||||
kind: "restore" as const,
|
||||
variant,
|
||||
at: job.created_at * 1000,
|
||||
title: `${from} → ${job.target.name} / ${job.target.identity.database}`,
|
||||
opens: source ? { kind: "snapshot" as const, id: source.id } : null,
|
||||
title: variant ? `${variant === "redo" ? "Redo" : "Undo"} → ${where}` : `${from} → ${where}`,
|
||||
opens,
|
||||
note: expired ? EXPIRED : undefined,
|
||||
};
|
||||
if (job.status === "recovery_required") {
|
||||
return { ...base, group: "needs", tone: "red", state: "Recovery required",
|
||||
meta: `Restore · ${phase.toLowerCase()} · target locked until verified · open the snapshot` };
|
||||
meta: `Restore · ${phase.toLowerCase()} · ${run?.undo_available ? "roll back available" : "target locked until verified · open the snapshot"}` };
|
||||
}
|
||||
if (inFlight(job.status)) {
|
||||
const progress = Math.max(0, restoreOrder.findIndex((phases) => phases.includes(job.phase)));
|
||||
@@ -78,11 +107,54 @@ function restoreEntry(job: RestoreJob, snapshots: Map<string, Snapshot>): Activi
|
||||
return { ...base, group: "history", tone: "grey", state: "Cancelled", meta: `Restore · by ${job.actor}` };
|
||||
}
|
||||
const recovered = job.status === "reconciled";
|
||||
return { ...base, group: "history", tone: "green", state: recovered ? "Recovered" : "Complete",
|
||||
meta: `Restore · ${job.safety_snapshot_id ? "safety copy kept · " : ""}by ${job.actor}` };
|
||||
const rolledBack = job.phase === "rolled_back";
|
||||
const noun = variant === "redo" ? "Redo" : variant === "undo" ? "Undo" : "Restore";
|
||||
const undoable = run?.undo_available ? ` · ${variant === "undo" ? "redo" : "undo"} available` : "";
|
||||
return { ...base, group: "history", tone: "green", state: rolledBack ? "Rolled back" : recovered ? "Recovered" : "Complete",
|
||||
meta: `${noun} · ${job.safety_snapshot_id ? "safety copy kept · " : ""}by ${job.actor}${undoable}` };
|
||||
}
|
||||
|
||||
function captureEntry(job: CaptureJob, orbits: Map<string, Orbit>): ActivityEntry | undefined {
|
||||
/** The id a restore has in the log: its job's when it has one, so a run and its job are one entry. */
|
||||
const restoreId = (run: RefreshRun) => (run.restore_job_id ? `restore-${run.restore_job_id}` : `restore-run-${run.id}`);
|
||||
|
||||
const runStopped: Record<string, string> = { restore_failed: "Failed", blocked: "Stopped", expired: "Expired", capture_failed: "Could not start" };
|
||||
|
||||
/**
|
||||
* A restore as the run shows it, for people who may not read restore jobs (those are administrator-only): what was
|
||||
* restored where, how far it got, and whether it can be put back. It carries no database names or who started it.
|
||||
*/
|
||||
function runEntry(run: RefreshRun, runs: Map<string, RefreshRun>): Draft {
|
||||
const depth = undoDepth(run, runs);
|
||||
const variant = depth === 0 ? undefined : depth % 2 === 1 ? "undo" as const : "redo" as const;
|
||||
const taken = run.snapshot_taken_at ? formatWhen(new Date(run.snapshot_taken_at * 1000).toISOString()) : undefined;
|
||||
const noun = variant === "redo" ? "Redo" : variant === "undo" ? "Undo" : "Restore";
|
||||
const base = {
|
||||
id: restoreId(run),
|
||||
kind: "restore" as const,
|
||||
variant,
|
||||
at: run.created_at * 1000,
|
||||
title: variant ? `${noun} → ${run.target_name}` : `${run.source_name}${taken ? ` · ${taken}` : ""} → ${run.target_name}`,
|
||||
opens: run.undo_available ? { kind: variant === "undo" ? "redo" as const : "undo" as const, run } : null,
|
||||
// A restore known only by its run has nothing to open unless it can be put back; that is not "removed".
|
||||
note: run.undo_state === "expired" ? EXPIRED : run.undo_available ? undefined : "",
|
||||
};
|
||||
const phase = (run.restore_phase && restoreLabels[run.restore_phase]) || undefined;
|
||||
if (run.status === "recovery_required") {
|
||||
return { ...base, group: "needs", tone: "red", state: "Recovery required",
|
||||
meta: `${noun} · ${(phase ?? "interrupted").toLowerCase()} · ${run.undo_available ? "roll back available" : "target locked until an administrator verifies it"}` };
|
||||
}
|
||||
if (run.status === "capturing" || run.status === "restoring") {
|
||||
const progress = Math.max(0, restoreOrder.findIndex((phases) => phases.includes(run.restore_phase ?? "queued")));
|
||||
return { ...base, group: "running", tone: "amber", state: phase ?? "Running", progress, meta: `${noun} · step ${progress + 1} of 5` };
|
||||
}
|
||||
if (run.status === "cancelled") return { ...base, group: "history", tone: "grey", state: "Cancelled", meta: noun };
|
||||
const stopped = runStopped[run.status];
|
||||
if (stopped) return { ...base, group: "history", tone: "red", state: stopped, meta: `${noun}${phase && run.status === "restore_failed" ? ` · ${phase.toLowerCase()}` : ""}` };
|
||||
return { ...base, group: "history", tone: "green", state: run.restore_phase === "rolled_back" ? "Rolled back" : run.status === "reconciled" ? "Recovered" : "Complete",
|
||||
meta: `${noun}${run.undo_available ? ` · ${variant === "undo" ? "redo" : "undo"} available` : ""}` };
|
||||
}
|
||||
|
||||
function captureEntry(job: CaptureJob, orbits: Map<string, Orbit>): Draft | undefined {
|
||||
const orbit = orbits.get(job.profile.orbit_id);
|
||||
const name = orbit?.name ?? job.profile.application_id;
|
||||
const base = {
|
||||
@@ -104,17 +176,35 @@ function captureEntry(job: CaptureJob, orbits: Map<string, Orbit>): ActivityEntr
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export function buildActivity({ snapshots, orbits, captureJobs, restoreJobs, now }: {
|
||||
const urgency = { needs: 0, running: 1, history: 2 } as const;
|
||||
|
||||
/** Entries sit in the log by their own group and time, except a restore, which sits by its most urgent and newest undo. */
|
||||
function place(entry: Draft): ActivityEntry {
|
||||
const children = entry.children?.map(place);
|
||||
let slotGroup = entry.group;
|
||||
let slotAt = entry.at;
|
||||
for (const child of children ?? []) {
|
||||
if (urgency[child.slotGroup] < urgency[slotGroup]) slotGroup = child.slotGroup;
|
||||
if ((child.slotAt ?? -Infinity) > (slotAt ?? -Infinity)) slotAt = child.slotAt;
|
||||
}
|
||||
return { ...entry, children, slotGroup, slotAt };
|
||||
}
|
||||
|
||||
export function buildActivity({ snapshots, orbits, captureJobs, restoreJobs, refreshRuns = [], now }: {
|
||||
snapshots: readonly Snapshot[];
|
||||
orbits: readonly Orbit[];
|
||||
captureJobs: readonly CaptureJob[];
|
||||
restoreJobs: readonly RestoreJob[];
|
||||
/** The hub's restore runs: they say which restore an undo reverses. */
|
||||
refreshRuns?: readonly RefreshRun[];
|
||||
now: number;
|
||||
}): ActivityEntry[] {
|
||||
const snapshotById = new Map(snapshots.map((snapshot) => [snapshot.id, snapshot]));
|
||||
const orbitById = new Map(orbits.map((orbit) => [orbit.id, orbit]));
|
||||
const jobForSnapshot = new Map(captureJobs.filter((job) => job.snapshot_id).map((job) => [job.snapshot_id!, job]));
|
||||
const entries: ActivityEntry[] = [];
|
||||
const runByJob = new Map(refreshRuns.filter((run) => run.restore_job_id).map((run) => [run.restore_job_id!, run]));
|
||||
const runById = new Map(refreshRuns.map((run) => [run.id, run]));
|
||||
const entries: Draft[] = [];
|
||||
|
||||
for (const snapshot of snapshots) {
|
||||
const job = jobForSnapshot.get(snapshot.id);
|
||||
@@ -146,13 +236,40 @@ export function buildActivity({ snapshots, orbits, captureJobs, restoreJobs, now
|
||||
opens: { kind: "orbit", id: orbit.id } });
|
||||
}
|
||||
}
|
||||
for (const job of restoreJobs) entries.push(restoreEntry(job, snapshotById));
|
||||
// Every restore comes from its run, which anyone with the refresh permission may read; an administrator's restore
|
||||
// job adds the database, the person and the snapshot, and restores started without a run (another registered
|
||||
// target) are only known by their job.
|
||||
const jobById = new Map(restoreJobs.map((job) => [job.id, job]));
|
||||
const parentOf = new Map<string, string>();
|
||||
for (const run of refreshRuns) {
|
||||
const job = run.restore_job_id ? jobById.get(run.restore_job_id) : undefined;
|
||||
entries.push(job ? restoreEntry(job, snapshotById, run, runById) : runEntry(run, runById));
|
||||
const parent = run.undo_of ? runById.get(run.undo_of) : undefined;
|
||||
if (parent) parentOf.set(restoreId(run), restoreId(parent));
|
||||
}
|
||||
for (const job of restoreJobs) if (!runByJob.has(job.id)) entries.push(restoreEntry(job, snapshotById, undefined, runById));
|
||||
|
||||
const rank = { needs: 0, running: 1, history: 2 } as const;
|
||||
return entries.sort((a, b) => rank[a.group] - rank[b.group] || (b.at ?? -Infinity) - (a.at ?? -Infinity) || a.id.localeCompare(b.id));
|
||||
// An undo hangs under the restore it reverses, and a redo under that undo, when that restore is in the log.
|
||||
const byId = new Map(entries.map((entry) => [entry.id, entry]));
|
||||
const top: Draft[] = [];
|
||||
for (const entry of entries) {
|
||||
const parent = byId.get(parentOf.get(entry.id) ?? "");
|
||||
if (parent && parent !== entry) (parent.children ??= []).push(entry);
|
||||
else top.push(entry);
|
||||
}
|
||||
const oldestFirst = (entry: Draft) => { entry.children?.sort((x, y) => (x.at ?? 0) - (y.at ?? 0)); entry.children?.forEach(oldestFirst); };
|
||||
top.forEach(oldestFirst);
|
||||
return top.map(place)
|
||||
.sort((a, b) => urgency[a.slotGroup] - urgency[b.slotGroup] || (b.slotAt ?? -Infinity) - (a.slotAt ?? -Infinity) || a.id.localeCompare(b.id));
|
||||
}
|
||||
|
||||
export function activityCounts(entries: readonly ActivityEntry[]) {
|
||||
/** Every entry in the log, undos included. */
|
||||
export function flatten(entries: readonly ActivityEntry[]): ActivityEntry[] {
|
||||
return entries.flatMap((entry) => [entry, ...flatten(entry.children ?? [])]);
|
||||
}
|
||||
|
||||
export function activityCounts(all: readonly ActivityEntry[]) {
|
||||
const entries = flatten(all);
|
||||
return {
|
||||
running: entries.filter(({ group }) => group === "running").length,
|
||||
needs: entries.filter(({ group }) => group === "needs").length,
|
||||
|
||||
Reference in New Issue
Block a user