M1.7: crash-recovery journal and restore dialog

Dirty buffers are written atomically to <data_dir>/recovery/<project-id>/
about 1 s after the last edit (at most 2 s while typing), from pump_vfs,
never per keystroke. Entries are removed on save, close (also force),
forced project close/open and recovery.discard. project.open emits
recoveryAvailable; recovery.list/restore/discard; restored buffers are
dirty and keep line ending and BOM. Corrupt entries are moved aside and
reported as warning notifications.

UI: restore dialog in ui/src/components/recovery (Restore all, Restore
selected, Discard, time, changed-on-disk hint), catalog keys and runtime
allow-list. Tests: crash/restore with CRLF + BOM, corrupt entries, removal
paths, property test of random edits, headless control-channel test,
Vitest for the dialog. Docs: architecture, parity, gaps, ROADMAP.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
danny 2026-10-16 15:54:52 +02:00
parent 6d976ed03e
commit 0b40eb90f8
27 changed files with 2222 additions and 27 deletions

View file

@ -0,0 +1,316 @@
<script setup lang="ts">
/**
* The restore dialog: lists the unsaved texts the recovery journal holds
* after a project opened, with the time each was written and a hint when
* the file changed on disk since. Restore all, Restore selected (the ticked
* files; the rest stay listed) or Discard (every listed file). Escape closes
* the dialog and keeps the journal for the next open. When a restore leaves
* failures, they are listed until the dialog is closed.
*/
import { nextTick, onMounted, onUnmounted, ref, watch } from "vue";
import { t } from "../../i18n";
import { useRecoveryStore } from "./recovery";
const recovery = useRecoveryStore();
/** The Restore all button, focused when the dialog appears. */
const restoreAllButton = ref<HTMLButtonElement | null>(null);
/** The Close button shown when only failures are left. */
const closeButton = ref<HTMLButtonElement | null>(null);
onMounted(() => {
recovery.start();
});
onUnmounted(() => {
recovery.stop();
});
/**
* What the dialog shows: the list, only failures, or nothing. Focus moves
* to the first button whenever this changes to something visible.
*/
function mode(): "list" | "failures" | "hidden" {
if (!recovery.open) {
return "hidden";
}
return recovery.entries.length > 0 ? "list" : "failures";
}
watch(
mode,
async (shown) => {
if (shown !== "hidden") {
await nextTick();
(restoreAllButton.value ?? closeButton.value)?.focus();
}
},
{ immediate: true },
);
/**
* Formats a journal time for the list.
*
* @param savedAt - ms since the Unix epoch.
*/
function formatTime(savedAt: number): string {
return new Date(savedAt).toLocaleString();
}
/**
* Closes on Escape.
*
* @param event - the key event inside the dialog.
*/
function onKeydown(event: KeyboardEvent): void {
if (event.key === "Escape") {
event.preventDefault();
recovery.close();
}
}
/**
* Ticks or unticks an entry from its checkbox.
*
* @param path - the entry's path.
* @param event - the change event of the checkbox.
*/
function onToggle(path: string, event: Event): void {
recovery.toggle(path, (event.target as HTMLInputElement).checked);
}
</script>
<template>
<div
v-if="recovery.open"
class="recovery"
data-test="recovery-dialog"
>
<div
class="recovery__dialog"
role="dialog"
aria-modal="true"
aria-labelledby="lt-recovery-title"
aria-describedby="lt-recovery-intro"
@keydown="onKeydown"
>
<h2
id="lt-recovery-title"
class="recovery__title"
>
{{ t("recovery.dialog.title") }}
</h2>
<p
id="lt-recovery-intro"
class="recovery__intro"
>
{{ t("recovery.dialog.intro") }}
</p>
<ul
v-if="recovery.entries.length > 0"
class="recovery__list"
>
<li
v-for="entry in recovery.entries"
:key="entry.path"
class="recovery__entry"
data-test="recovery-entry"
>
<label class="recovery__label">
<input
type="checkbox"
:checked="recovery.selected.has(entry.path)"
:data-test="`recovery-select-${entry.path}`"
@change="onToggle(entry.path, $event)"
>
<span class="recovery__path">{{ entry.path }}</span>
</label>
<span class="recovery__meta">
{{ t("recovery.dialog.savedAt", { time: formatTime(entry.savedAt) }) }}
</span>
<span
v-if="entry.diskChanged"
class="recovery__changed"
data-test="recovery-changed"
>
{{ t("recovery.dialog.changedOnDisk") }}
</span>
</li>
</ul>
<ul
v-if="recovery.failures.length > 0"
class="recovery__failures"
role="alert"
data-test="recovery-failures"
>
<li
v-for="failure in recovery.failures"
:key="failure.path"
>
{{ t("recovery.dialog.failed", { path: failure.path, message: failure.message }) }}
</li>
</ul>
<p
v-if="recovery.error"
class="recovery__error"
role="alert"
data-test="recovery-error"
>
{{ recovery.error }}
</p>
<div class="recovery__buttons">
<template v-if="recovery.entries.length > 0">
<button
ref="restoreAllButton"
type="button"
class="recovery__button recovery__button--primary"
data-test="recovery-restore-all"
:disabled="recovery.busy"
@click="recovery.restoreAll()"
>
{{ t("recovery.dialog.restoreAll") }}
</button>
<button
type="button"
class="recovery__button"
data-test="recovery-restore-selected"
:disabled="recovery.busy || recovery.selected.size === 0"
@click="recovery.restoreSelected()"
>
{{ t("recovery.dialog.restoreSelected") }}
</button>
<button
type="button"
class="recovery__button"
data-test="recovery-discard"
:disabled="recovery.busy"
@click="recovery.discard()"
>
{{ t("recovery.dialog.discard") }}
</button>
</template>
<button
v-else
ref="closeButton"
type="button"
class="recovery__button recovery__button--primary"
data-test="recovery-close"
@click="recovery.close()"
>
{{ t("common.close") }}
</button>
</div>
</div>
</div>
</template>
<style scoped>
.recovery {
position: fixed;
inset: 0;
display: flex;
align-items: flex-start;
justify-content: center;
padding-top: 10%;
background: color-mix(in srgb, var(--lt-bg) 50%, transparent);
z-index: 20;
}
.recovery__dialog {
min-width: 360px;
max-width: min(640px, 90vw);
max-height: 70vh;
display: flex;
flex-direction: column;
padding: 16px;
background: var(--lt-bg);
color: var(--lt-fg);
border: 1px solid var(--lt-border);
border-radius: 6px;
}
.recovery__title {
margin: 0 0 8px;
font-size: var(--lt-font-size-ui);
font-weight: 600;
}
.recovery__intro {
margin: 0 0 12px;
}
.recovery__list,
.recovery__failures {
margin: 0 0 12px;
padding: 0;
list-style: none;
overflow-y: auto;
}
.recovery__entry {
display: flex;
flex-wrap: wrap;
align-items: baseline;
gap: 4px 12px;
padding: 4px 0;
border-bottom: 1px solid var(--lt-border);
}
.recovery__label {
display: flex;
align-items: baseline;
gap: 6px;
}
.recovery__path {
overflow-wrap: anywhere;
}
.recovery__meta {
color: var(--lt-fg-muted);
font-size: var(--lt-font-size-small);
}
.recovery__changed {
color: var(--lt-accent);
font-size: var(--lt-font-size-small);
}
.recovery__failures,
.recovery__error {
color: var(--lt-error);
}
.recovery__error {
margin: 0 0 12px;
}
.recovery__buttons {
display: flex;
justify-content: flex-end;
gap: 8px;
}
.recovery__button {
padding: 4px 12px;
font: inherit;
color: var(--lt-fg);
background: var(--lt-editor-bg);
border: 1px solid var(--lt-border);
border-radius: 4px;
}
.recovery__button--primary {
border-color: var(--lt-accent);
}
.recovery__button:disabled {
color: var(--lt-fg-muted);
}
.recovery__button:focus-visible,
.recovery__label input:focus-visible {
outline: 1px solid var(--lt-accent);
}
</style>