fix(app): rescue copy importable, trapping draft held, replace confirmed

The JSON rescue copy could not be restored in the app, a draft that traps
the engine left the tab on a reload screen and spent a recovery on every
read, and an unsaved or in-progress report could be lost by a reload
outside the editor or by New report / Import without warning.

Home import reads the copy (content-sniffed, 16 MiB cap, strict UTF-8)
through the core's draft.restore before any session change; a trapping
(draft, archive) pair is held in the resolution panel and only re-read
on Retry; ReplaceSessionDialog guards New report and Import; the
beforeunload guard is app-wide while unsaved. Supersedes BDR-055's
"never held" for `internal` only.
This commit is contained in:
bchanot committed 2026-10-17 12:58:57 +02:00
1 parent 9464651140
commit 9bd9acaf58
22 files changed
+1090 -68

No files matched your search

+72 -7
View File
@@ -20,6 +20,7 @@ import {
ARCHIVE_KEY,
quarantine,
quarantineSession,
readSessionText,
removeSessionText,
sessionStorageArchivePersistence,
sessionStoragePersistence,
@@ -57,17 +58,20 @@ export interface ActionResult {
ok: boolean
/** Refused because a session text is held (it could not be set aside and would be lost). */
reason?: 'held'
/** With `held`: the engine itself fails on the tab's draft (a trap), not a storage problem. */
engine?: boolean
}
/**
* A session text kept in place because its safety copy failed (storage full, or the
* quarantine slot already holds an earlier text): `unreadable` by this build, the
* archive `companion` of an unreadable draft, or a draft whose `pre_rebase` original
* could not be kept before the rebase replaces it.
* could not be kept before the rebase replaces it, or a draft the engine trapped on
* (`engine_failure`, kept as is).
*/
export interface HeldText {
text: SessionText
reason: 'unreadable' | 'companion' | 'pre_rebase'
reason: 'unreadable' | 'companion' | 'pre_rebase' | 'engine_failure'
/** UTF-8 size of the stored text, for the resolution panel. */
bytes: number
}
@@ -75,6 +79,8 @@ export interface HeldText {
export interface RestoreOptions {
/** The practitioner confirmed that the rebased draft may replace its original. */
replaceOriginal?: boolean
/** The practitioner asked to read a draft the engine trapped on again. */
retryTrapped?: boolean
}
/** What a draft restore did; `null` from the action when there was nothing to restore. */
@@ -116,6 +122,8 @@ export interface ReportStore {
) => RestoreResult | null
/** Removes a held session text, on the practitioner's confirmed request; no-op once a report is open. */
discardSessionText: (text: SessionText) => void
/** Forgets the draft the engine trapped on (test seam; every discard does it too). */
clearTrapped: () => void
addObservation: () => ActionResult
removeObservation: (index: number) => ActionResult
moveObservationUp: (index: number) => ActionResult
@@ -140,6 +148,7 @@ type Getter = () => ReportStore
const OK: ActionResult = { ok: true }
const FAILED: ActionResult = { ok: false }
const HELD: ActionResult = { ok: false, reason: 'held' }
const HELD_ENGINE: ActionResult = { ok: false, reason: 'held', engine: true }
/** 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 =>
@@ -172,7 +181,7 @@ function applyCore(
* (`lone_surrogate`: reading it would alter a string, L1). Any other failure is the core's
* or the sheet's and leaves the draft in place.
*/
const UNREADABLE_DRAFT_KINDS: ReadonlySet<string> = new Set([
export const UNREADABLE_DRAFT_KINDS: ReadonlySet<string> = new Set([
'invalid',
'unsupported_version',
'lone_surrogate',
@@ -186,6 +195,37 @@ function held(text: SessionText, reason: HeldText['reason'], raw: string): HeldT
return { text, reason, bytes: utf8Bytes(raw) }
}
/**
* The (draft, archive) pair a `draft.restore` call trapped on. The same pair is never read
* again until the practitioner retries: each read of it would spend an engine recovery.
*/
interface TrapMemory {
matches: (draft: string, archiveText: string | null) => boolean
remember: (draft: string, archiveText: string | null) => void
clear: () => void
}
function createTrapMemory(): TrapMemory {
let trapped: { draft: string; archive: string | null } | null = null
return {
matches: (draft, archiveText) =>
trapped !== null && trapped.draft === draft && trapped.archive === archiveText,
remember: (draft, archiveText) => {
trapped = { draft, archive: archiveText }
},
clear: () => {
trapped = null
},
}
}
/** The held rows of a trapped draft: the draft, and its archive when one is beside it. */
function engineFailureRows(draft: string, archiveText: string | null): HeldText[] {
const rows = [held('draft', 'engine_failure', draft)]
if (archiveText !== null) rows.push(held('archive', 'companion', archiveText))
return rows
}
type Restored = { result: RestoreResult; patch?: Patch } | null
/** An archive with no draft beside it can never be restored: set it aside, or hold it. */
@@ -212,12 +252,21 @@ function restore(
persistence: ReportPersistence,
archives: ArchivePersistence,
options: RestoreOptions,
trap: TrapMemory,
): Restored {
const draft = persistence.loadText()
const archiveText = archives.loadText()
if (draft === null) return restoreOrphanArchive(archiveText)
if (options.retryTrapped) trap.clear()
if (trap.matches(draft, archiveText)) {
return { result: { kind: 'held', held: engineFailureRows(draft, archiveText) } }
}
const outcome = index.ops.restoreDraft(draft, archiveText)
if (!outcome.ok) {
if (outcome.kind === 'internal') {
trap.remember(draft, archiveText)
return { result: { kind: 'held', held: engineFailureRows(draft, archiveText) } }
}
// Only an unreadable draft is set aside; a core or sheet failure leaves it in place.
if (!UNREADABLE_DRAFT_KINDS.has(outcome.kind)) {
return { result: { kind: 'core_unavailable' } }
@@ -247,19 +296,26 @@ 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. 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).
* before. HELD when something could not be set aside, or when the engine traps on the draft
* (`engine`: kept as is); 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,
trap: TrapMemory,
): ActionResult {
const draft = persistence.loadText()
const archiveText = archives.loadText()
if (draft === null) return restoreOrphanArchive(archiveText) === null ? OK : HELD
if (trap.matches(draft, archiveText)) return HELD_ENGINE
const outcome = index.ops.restoreDraft(draft, archiveText)
if (!outcome.ok) {
if (outcome.kind === 'internal') {
trap.remember(draft, archiveText)
return HELD_ENGINE
}
if (!UNREADABLE_DRAFT_KINDS.has(outcome.kind)) return FAILED
return quarantineSession(draft, archiveText) ? OK : HELD
}
@@ -271,6 +327,7 @@ export function createReportStore(
persistence: ReportPersistence,
archives: ArchivePersistence,
) {
const trap = createTrapMemory()
/** The archive mirror is explicit: these actions are the session's edges. */
// The archive of this session could not be written: the tab's copy lacks it (banner).
let archiveRefused = false
@@ -286,7 +343,7 @@ export function createReportStore(
const mayStartSession = (index: SheetIndex): ActionResult =>
store.getState().report !== null
? OK
: releaseUnreadSession(index, persistence, archives)
: releaseUnreadSession(index, persistence, archives, trap)
const store = create<ReportStore>((set, get) => ({
report: null,
@@ -318,7 +375,7 @@ export function createReportStore(
restoreDraft: (index, options = {}) => {
if (get().report) return null
const restored = restore(index, persistence, archives, options)
const restored = restore(index, persistence, archives, options, trap)
if (!restored) return null
if (restored.patch) set(restored.patch)
return restored.result
@@ -326,9 +383,12 @@ export function createReportStore(
discardSessionText: (text) => {
if (get().report) return
trap.clear()
removeSessionText(text)
},
clearTrapped: () => trap.clear(),
addObservation: () =>
applyCore(
set,
@@ -409,3 +469,8 @@ export const useReportStore = createReportStore(
sessionStoragePersistence,
sessionStorageArchivePersistence,
)
/** A bilan is in progress in this tab: in memory, or as a draft this page never opened. */
export function isSessionInProgress(): boolean {
return useReportStore.getState().report !== null || readSessionText('draft') !== null
}