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
+85 -34

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: '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
* 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)),
)
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<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>(
accept: ((state: PreviewState) => Acceptance<R>) | undefined,
): ReplaceOutcome<R> {
@@ -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