feat: undo a restore from the safety copy it took
Some checks failed
ci / rust (push) Has been cancelled
ci / supply-chain (push) Has been cancelled
ci / web (push) Has been cancelled

- POST /refresh-runs accepts undo_of: restores the safety copy of a completed restore back onto the same
  target through the normal run engine (checks, countdown, progress, and a new safety copy so the undo can be
  undone); refused when something was restored after it or the copy is gone
- a safety copy is accepted as a source only for the target it was taken from (hub and agent both check)
- run views carry undo and undo_available; audited as refresh.undo_started
- the modal offers Undo last restore and, after a restore, Undo this restore
- proven with a real agent and PostgreSQL: restore, undo back to the original contents, undo of the undo

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-16 16:47:02 +07:00
parent 854e3572e9
commit 648c8c6a2a
16 changed files with 457 additions and 41 deletions

View File

@@ -1,7 +1,7 @@
import { useEffect, useMemo, useRef, useState } from "react";
import { motion } from "motion/react";
import { useQuery } from "@tanstack/react-query";
import { cancelRefresh, fetchRefreshRun, refusalReason, startRefresh, type RefreshRelation, type RefreshRun, type Snapshot } from "../../api";
import { cancelRefresh, fetchRefreshRun, fetchRefreshRuns, refusalReason, startRefresh, type RefreshRelation, type RefreshRun, type Snapshot } from "../../api";
import { formatBytes, formatWhen } from "../../lib/format";
import { isFinal, outcomeOf, progressOf, relationLabels, STAGES } from "../../lib/restoreRun";
import { Button } from "../ui/Button";
@@ -17,6 +17,7 @@ const REFUSALS: Record<string, string> = {
relation_unusable: "This restore point cannot be used right now. Ask an administrator.",
too_many_runs: "Too many restores are running at once. Try again in a few minutes.",
snapshot_unusable: "This backup cannot be restored to that database.",
nothing_to_undo: "There is nothing to undo any more: something was restored after it, or its safety copy was removed.",
source_unavailable: "The source cannot be reached right now.",
};
@@ -42,6 +43,8 @@ export function RestoreWizard({ snapshot, relations, admin, onBusyChange }: {
const [runId, setRunId] = useState<string>();
const [refusal, setRefusal] = useState<string>();
const [other, setOther] = useState(false);
// Set while an undo is being started or followed: the run whose restore is being put back.
const [undoOf, setUndoOf] = useState<string>();
// One key per attempt: the same press can never start two restores, even if the request is retried.
const attempt = useRef(globalThis.crypto?.randomUUID?.() ?? String(Math.random()));
@@ -66,7 +69,7 @@ export function RestoreWizard({ snapshot, relations, admin, onBusyChange }: {
if (left <= 0) {
setPhase("running");
if (!point) return;
startRefresh(point, attempt.current, snapshot.id)
startRefresh(point, attempt.current, undoOf ? { undo_of: undoOf } : { snapshot_id: snapshot.id })
.then((started) => setRunId(started.id))
.catch((error) => {
setRefusal(REFUSALS[refusalReason(error) ?? ""] ?? "The restore could not be started.");
@@ -77,7 +80,7 @@ export function RestoreWizard({ snapshot, relations, admin, onBusyChange }: {
}
const timer = window.setTimeout(() => setLeft((value) => value - 1), 1000);
return () => clearTimeout(timer);
}, [phase, left, point, snapshot.id]);
}, [phase, left, point, snapshot.id, undoOf]);
const abortable = phase === "countdown" || (phase === "running" && !finished && progress.index < 2);
const abort = async () => {
@@ -89,7 +92,15 @@ export function RestoreWizard({ snapshot, relations, admin, onBusyChange }: {
}
};
const again = () => {
setPhase("idle"); setLeft(COUNTDOWN); setRunId(undefined); setRefusal(undefined);
setPhase("idle"); setLeft(COUNTDOWN); setRunId(undefined); setRefusal(undefined); setUndoOf(undefined);
attempt.current = globalThis.crypto?.randomUUID?.() ?? String(Math.random());
};
// The newest restore on the chosen restore point, if it can still be undone.
const recent = useQuery({ queryKey: ["restore-runs"], queryFn: fetchRefreshRuns, enabled: phase === "idle", retry: false, refetchInterval: 15_000 });
const undoable = Array.isArray(recent.data) ? recent.data.find((r) => r.relation_id === point?.id && r.undo_available) : undefined;
const beginUndo = (of: string) => {
setRefusal(undefined); setRunId(undefined); setUndoOf(of); setLeft(COUNTDOWN); setPhase("countdown");
attempt.current = globalThis.crypto?.randomUUID?.() ?? String(Math.random());
};
@@ -132,11 +143,17 @@ export function RestoreWizard({ snapshot, relations, admin, onBusyChange }: {
</div>
<div className="flex gap-2.5">
<Button variant="danger" className="flex-1" disabled={!abortable} onClick={() => void abort()}>Abort</Button>
<Button variant="primary" className="flex-1" disabled={!point || phase !== "idle"} onClick={() => { setRefusal(undefined); setLeft(COUNTDOWN); setPhase("countdown"); }}>
<Button variant="primary" className="flex-1" disabled={!point || phase !== "idle"} onClick={() => { setRefusal(undefined); setUndoOf(undefined); setLeft(COUNTDOWN); setPhase("countdown"); }}>
Begin restore
</Button>
</div>
{refusal && <p role="alert" className="text-sm text-orbit-red">{refusal}</p>}
{undoable && phase === "idle" && !runId && (
<div className="restore-undo">
<p>The last restore on {target} was {formatWhen(new Date(undoable.updated_at * 1000).toISOString())}. Its safety copy can put {target} back as it was before.</p>
<Button onClick={() => beginUndo(undoable.id)}>Undo last restore</Button>
</div>
)}
{admin && database && phase === "idle" && !runId && (
<button type="button" className="footer-link self-start" onClick={() => setOther(true)}>Other registered target…</button>
)}
@@ -155,7 +172,7 @@ export function RestoreWizard({ snapshot, relations, admin, onBusyChange }: {
</svg>
<span className="restore-ring-text"><b>{left}</b><i>seconds</i></span>
</div>
<p className="restore-count-note">Restoring to {target} in {left}s.<br />Abort restores nothing.</p>
<p className="restore-count-note">{undoOf ? `Undoing the last restore on ${target} in ${left}s.` : `Restoring to ${target} in ${left}s.`}<br />Abort restores nothing.</p>
</motion.div>
)}
{phase === "running" && (
@@ -182,7 +199,10 @@ export function RestoreWizard({ snapshot, relations, admin, onBusyChange }: {
<div role="status" className={`restore-outcome ${outcome.tone}`}>
<p>{outcome.title}</p>
<p>{outcome.detail}</p>
<Button onClick={again}>{outcome.tone === "good" ? "Restore again" : "Try again"}</Button>
<div className="flex flex-wrap gap-2">
{run.data?.undo_available && <Button onClick={() => beginUndo(run.data!.id)}>Undo this restore</Button>}
<Button onClick={again}>{outcome.tone === "good" ? "Restore again" : "Try again"}</Button>
</div>
</div>
)}
{run.isError && <p role="alert" className="text-sm text-orbit-amber">Reconnecting to the restore…</p>}