fix(editor): honest stopped state when the draft is unsaved; no dead retry

0279953 chose the stopped message from CoreRuntime.stopped alone: it
promised the report was kept even when the tab's storage refused the
draft (export, the banner's advice, needs the core), four surfaces still
offered a retry that could not work, and a failed recovery below the cap
was a dead end since nothing called retry().

The runtime restarts a failed recovery on the next call (compiled module
reused, cap still counts traps) and notifies status listeners. One
CoreStoppedNotice on the editor, Home, New report and the full-page
notice: reload when saved, else core.stopped_unsaved with a core-free
copy download (also on the storage-full banner) and a beforeunload
guard. Export, import and new report say the stopped message; a core
failure reading an unread draft is FAILED, not held. ESLint census of
the failure keys; core.stopped says "plusieurs erreurs".
This commit is contained in:
bchanot committed 2026-10-16 18:26:10 +02:00
1 parent 0279953e9e
commit 6371523f8c
24 files changed
+839 -83

No files matched your search

+20 -12
View File
@@ -141,6 +141,10 @@ const OK: ActionResult = { ok: true }
const FAILED: ActionResult = { ok: false }
const HELD: ActionResult = { ok: false, reason: 'held' }
/** The tab's storage refused the last write: the in-memory report is the only copy. */
export const isUnsaved = (s: Pick<ReportStore, 'saveFailed' | 'report'>): boolean =>
s.saveFailed && s.report !== null
function onReport(set: Setter, update: (r: Report, s: ReportStore) => Patch) {
set((s) => (s.report ? update(s.report, s) : s))
}
@@ -243,23 +247,24 @@ function restore(
/**
* Before a new session replaces texts this page never read: an unreadable draft (and its
* archive) or an unreadable archive is set aside first; a readable draft is replaced as
* before. False when something could not be set aside, or could not even be read.
* before. HELD when something could not be set aside; FAILED when the core could not even
* read the draft (not a held text: nothing is set aside, nothing is written).
*/
function releaseUnreadSession(
index: SheetIndex,
persistence: ReportPersistence,
archives: ArchivePersistence,
): boolean {
): ActionResult {
const draft = persistence.loadText()
const archiveText = archives.loadText()
if (draft === null) return restoreOrphanArchive(archiveText) === null
if (draft === null) return restoreOrphanArchive(archiveText) === null ? OK : HELD
const outcome = index.ops.restoreDraft(draft, archiveText)
if (!outcome.ok) {
if (!UNREADABLE_DRAFT_KINDS.has(outcome.kind)) return false
return quarantineSession(draft, archiveText)
if (!UNREADABLE_DRAFT_KINDS.has(outcome.kind)) return FAILED
return quarantineSession(draft, archiveText) ? OK : HELD
}
if (!outcome.value.archiveRejected || archiveText === null) return true
return quarantine('archive', archiveText, ARCHIVE_KEY)
if (!outcome.value.archiveRejected || archiveText === null) return OK
return quarantine('archive', archiveText, ARCHIVE_KEY) ? OK : HELD
}
export function createReportStore(
@@ -278,9 +283,10 @@ export function createReportStore(
}
/** A new session may start: either one is open (its texts are this store's), or none is left unread. */
const mayStartSession = (index: SheetIndex) =>
store.getState().report !== null ||
releaseUnreadSession(index, persistence, archives)
const mayStartSession = (index: SheetIndex): ActionResult =>
store.getState().report !== null
? OK
: releaseUnreadSession(index, persistence, archives)
const store = create<ReportStore>((set, get) => ({
report: null,
@@ -289,7 +295,8 @@ export function createReportStore(
saveFailed: false,
initReport: (draft, sheet, index) => {
if (!mayStartSession(index)) return HELD
const may = mayStartSession(index)
if (!may.ok) return may
const outcome = reportOps.newReport({
draft,
sheet,
@@ -303,7 +310,8 @@ export function createReportStore(
},
hydrateImport: ({ report, archive }, index) => {
if (!mayStartSession(index)) return HELD
const may = mayStartSession(index)
if (!may.ok) return may
set(withArchive({ report, activeObservationIndex: 0 }, archive))
return OK
},