fix(app): roll the preview back on a throwing accept or a non-trap failure

This commit is contained in:
bchanot committed 2026-11-04 11:50:59 +01:00
1 parent 173be5949a
commit bc43a1b86b
3 files changed
+83 -32

No files matched your search

+24 -14
View File
@@ -32,12 +32,6 @@ export type ReplaceOutcome<R> =
| { ok: false; reason: 'core'; failure: CoreFailure } | { ok: false; reason: 'core'; failure: CoreFailure }
| { ok: false; reason: 'refused'; refusal: R } | { 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<string> = new Set([
'internal',
'core_unavailable',
])
/** /**
* A stored version loaded into the core (`PreviewSheet`, RUST.md §12): the * 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 * core checks each drawing's sha256 against its row, the fingerprint, the caps
@@ -87,14 +81,10 @@ export class CataloguePreview {
parseOutcome<null>(handle.value.replace_document(text)), parseOutcome<null>(handle.value.replace_document(text)),
) )
if (!replaced.ok) return { ok: false, reason: 'core', failure: replaced } 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 } if (outcome.ok) this.input = { ...this.input, document: text }
else if ( else if (!this.trappedSince(generation)) this.rollback(previous)
outcome.reason === 'refused' ||
!TRAPPED_KINDS.has(outcome.failure.kind)
) {
this.rollback(previous)
}
return outcome return outcome
} }
@@ -132,6 +122,26 @@ export class CataloguePreview {
this.discardHandle() 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<R>(
previous: string,
accept: ((state: PreviewState) => Acceptance<R>) | undefined,
): ReplaceOutcome<R> {
try {
return this.judge(accept)
} catch (error) {
this.rollback(previous)
throw error
}
}
private judge<R>( private judge<R>(
accept: ((state: PreviewState) => Acceptance<R>) | undefined, accept: ((state: PreviewState) => Acceptance<R>) | undefined,
): ReplaceOutcome<R> { ): ReplaceOutcome<R> {
@@ -143,7 +153,7 @@ export class CataloguePreview {
: { ok: false, reason: 'refused', refusal: verdict.refusal } : { 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 { private rollback(previous: string): void {
const handle = this.current() const handle = this.current()
const restored = handle.ok const restored = handle.ok
+24 -18
View File
@@ -28,22 +28,17 @@ export interface CleaningPreview {
const NAME = { fr: 'Aperçu', en: 'Preview' } const NAME = { fr: 'Aperçu', en: 'Preview' }
/** /** The throwaway document: the empty sheet plus one detail view naming the drawing. */
* A throwaway draft holding only the cleaned drawing, so the dialog renders the core's function cleaningDocument(
* composed tree and never the original file. The caller frees `preview` on close. viewId: string,
*/ assetId: string,
export function composeCleaningPreview( ): CoreOutcome<string> {
drawing: StoredDrawing, const empty = coreRuntime().call<unknown>(
): CoreOutcome<CleaningPreview> {
const runtime = coreRuntime()
const empty = runtime.call<unknown>(
'sheet.empty', 'sheet.empty',
JSON.stringify({ species_id: 'preview', name: NAME }), JSON.stringify({ species_id: 'preview', name: NAME }),
) )
if (!empty.ok) return empty if (!empty.ok) return empty
const assetId = generateUuid() return applyEdit(JSON.stringify(empty.value), {
const viewId = newElementId('view')
const edited = applyEdit(JSON.stringify(empty.value), {
op: 'insert', op: 'insert',
element: { element: {
kind: 'view', 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<CleaningPreview> {
const assetId = generateUuid()
const viewId = newElementId('view')
const document = cleaningDocument(viewId, assetId)
if (!document.ok) return document
const preview = new CataloguePreview( const preview = new CataloguePreview(
runtime, coreRuntime(),
{ status: 'draft', document: edited.value }, { status: 'draft', document: document.value },
[], [],
) )
const bytes = new TextEncoder().encode(drawing.stored) const bytes = new TextEncoder().encode(drawing.stored)
const added = preview.addDrawing({ assetId, sha256: drawing.sha256, bytes }) const added = preview.addDrawing({ assetId, sha256: drawing.sha256, bytes })
if (!added.ok) { if (added.ok)
return { ok: true, value: { preview, viewId, scope: `fv.${viewId}.` } }
preview.free() preview.free()
return added return added
} }
return { ok: true, value: { preview, viewId, scope: `fv.${viewId}.` } }
}
@@ -202,4 +202,39 @@ describe('replaceDocument accept hook', () => {
expect(stateOf(preview)).toEqual(before) expect(stateOf(preview)).toEqual(before)
preview.free() 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()
})
}) })