diff --git a/apps/app/src/catalogue/preview.ts b/apps/app/src/catalogue/preview.ts index 3b3c5114..dad7d5fe 100644 --- a/apps/app/src/catalogue/preview.ts +++ b/apps/app/src/catalogue/preview.ts @@ -32,12 +32,6 @@ export type ReplaceOutcome = | { ok: false; reason: 'core'; failure: CoreFailure } | { ok: false; reason: 'refused'; refusal: R } -/** A trap or a stopped module: no handle to roll back, the rebuild uses the kept input. */ -const TRAPPED_KINDS: ReadonlySet = new Set([ - 'internal', - 'core_unavailable', -]) - /** * A stored version loaded into the core (`PreviewSheet`, RUST.md §12): the * core checks each drawing's sha256 against its row, the fingerprint, the caps @@ -87,14 +81,10 @@ export class CataloguePreview { parseOutcome(handle.value.replace_document(text)), ) if (!replaced.ok) return { ok: false, reason: 'core', failure: replaced } - const outcome = this.judge(accept) + const generation = this.runtime.generation + const outcome = this.judgeOrRollback(previous, accept) if (outcome.ok) this.input = { ...this.input, document: text } - else if ( - outcome.reason === 'refused' || - !TRAPPED_KINDS.has(outcome.failure.kind) - ) { - this.rollback(previous) - } + else if (!this.trappedSince(generation)) this.rollback(previous) return outcome } @@ -132,6 +122,26 @@ export class CataloguePreview { this.discardHandle() } + /** A trap poisons the runtime: it is no longer ready, or already a newer instance. */ + private trappedSince(generation: number): boolean { + return ( + this.runtime.status !== 'ready' || this.runtime.generation !== generation + ) + } + + /** `accept` throwing must not leave the handle on the candidate text. */ + private judgeOrRollback( + previous: string, + accept: ((state: PreviewState) => Acceptance) | undefined, + ): ReplaceOutcome { + try { + return this.judge(accept) + } catch (error) { + this.rollback(previous) + throw error + } + } + private judge( accept: ((state: PreviewState) => Acceptance) | undefined, ): ReplaceOutcome { @@ -143,7 +153,7 @@ export class CataloguePreview { : { ok: false, reason: 'refused', refusal: verdict.refusal } } - /** Puts the handle back on the kept text; if even that fails the handle is dropped and rebuilt. */ + /** Puts the handle back on the kept text; if that fails the handle is dropped and rebuilt. */ private rollback(previous: string): void { const handle = this.current() const restored = handle.ok diff --git a/apps/app/src/editor/core/cleaning.ts b/apps/app/src/editor/core/cleaning.ts index 5c518482..37687912 100644 --- a/apps/app/src/editor/core/cleaning.ts +++ b/apps/app/src/editor/core/cleaning.ts @@ -28,22 +28,17 @@ export interface CleaningPreview { const NAME = { fr: 'Aperçu', en: 'Preview' } -/** - * A throwaway draft holding only the cleaned drawing, so the dialog renders the core's - * composed tree and never the original file. The caller frees `preview` on close. - */ -export function composeCleaningPreview( - drawing: StoredDrawing, -): CoreOutcome { - const runtime = coreRuntime() - const empty = runtime.call( +/** The throwaway document: the empty sheet plus one detail view naming the drawing. */ +function cleaningDocument( + viewId: string, + assetId: string, +): CoreOutcome { + const empty = coreRuntime().call( 'sheet.empty', JSON.stringify({ species_id: 'preview', name: NAME }), ) if (!empty.ok) return empty - const assetId = generateUuid() - const viewId = newElementId('view') - const edited = applyEdit(JSON.stringify(empty.value), { + return applyEdit(JSON.stringify(empty.value), { op: 'insert', element: { kind: 'view', @@ -57,17 +52,28 @@ export function composeCleaningPreview( }, }, }) - if (!edited.ok) return edited +} + +/** + * A throwaway draft holding only the cleaned drawing, so the dialog renders the core's + * composed tree and never the original file. The caller frees `preview` on close. + */ +export function composeCleaningPreview( + drawing: StoredDrawing, +): CoreOutcome { + const assetId = generateUuid() + const viewId = newElementId('view') + const document = cleaningDocument(viewId, assetId) + if (!document.ok) return document const preview = new CataloguePreview( - runtime, - { status: 'draft', document: edited.value }, + coreRuntime(), + { status: 'draft', document: document.value }, [], ) const bytes = new TextEncoder().encode(drawing.stored) const added = preview.addDrawing({ assetId, sha256: drawing.sha256, bytes }) - if (!added.ok) { - preview.free() - return added - } - return { ok: true, value: { preview, viewId, scope: `fv.${viewId}.` } } + if (added.ok) + return { ok: true, value: { preview, viewId, scope: `fv.${viewId}.` } } + preview.free() + return added } diff --git a/apps/app/tests/unit/editor-preview.test.ts b/apps/app/tests/unit/editor-preview.test.ts index 146ac4e5..fb2fc833 100644 --- a/apps/app/tests/unit/editor-preview.test.ts +++ b/apps/app/tests/unit/editor-preview.test.ts @@ -202,4 +202,39 @@ describe('replaceDocument accept hook', () => { expect(stateOf(preview)).toEqual(before) preview.free() }) + + it('rolls back when accept throws, and rethrows', () => { + const preview = draft() + const before = stateOf(preview) + expect(() => + preview.replaceDocument(renamed(preview.document, 'Boom'), () => { + throw new Error('boom') + }), + ).toThrow('boom') + expect(stateOf(preview)).toEqual(before) + forceRebuild() + expect(stateOf(preview)).toEqual(before) + preview.free() + }) + + it('rolls back on a non-trap internal failure of state', () => { + const preview = draft() + const before = stateOf(preview) + const handle = ( + preview as unknown as { handle: { call(op: string, i: string): string } } + ).handle + const real = handle.call.bind(handle) + let failed = false + handle.call = (op, input) => { + if (op === 'preview.state' && !failed) { + failed = true + return '{"ok":false,"kind":"internal"}' + } + return real(op, input) + } + const outcome = preview.replaceDocument(renamed(preview.document, 'Perdu')) + expect(outcome).toMatchObject({ ok: false, failure: { kind: 'internal' } }) + expect(stateOf(preview)).toEqual(before) + preview.free() + }) })