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

+61 -12
View File
@@ -13,6 +13,8 @@ import {
* a fresh one is created from the compiled module kept in memory and a glue module record
* imported in advance (a trap needs no network). Calls made while it is re-created answer
* `core_unavailable`; after `maxRecoveries` traps the page stays unavailable until reload.
* A recovery or restart that fails below the cap is not final: the next call or `ready()`
* restarts the runtime (a restart is not a recovery and does not touch the counter).
*/
/** What the wasm-bindgen glue (`--target web`) exports. */
@@ -60,6 +62,7 @@ export class CoreRuntime {
private imports = 0
private recoveries = 0
private readonly maxRecoveries: number
private readonly listeners = new Set<() => void>()
/** Increments with every instance; sheet handles of an older generation are dropped. */
generation = 0
@@ -82,9 +85,30 @@ export class CoreRuntime {
return this.state === 'unavailable' && this.recoveries >= this.maxRecoveries
}
/**
* Unavailable after an instance already ran (a failed recovery or restart) with
* recoveries left: the next call may bring it back. A failed FIRST load is not restarted
* implicitly, only by `retry()`.
*/
private get restartable(): boolean {
return this.state === 'unavailable' && !this.stopped && this.generation > 0
}
/**
* Calls `listener` (after a microtask, never inside a load or recovery) each time the
* status changes; returns the unsubscribe function.
*/
subscribe(listener: () => void): () => void {
this.listeners.add(listener)
return () => {
this.listeners.delete(listener)
}
}
/** Loads the module on first use; true when calls can be made. */
ready(): Promise<boolean> {
if (this.state === 'ready') return Promise.resolve(true)
if (this.restartable) return this.retry()
if (this.state === 'unavailable') return Promise.resolve(false)
this.pending ??= this.load()
return this.pending
@@ -94,12 +118,18 @@ export class CoreRuntime {
retry(): Promise<boolean> {
if (this.state !== 'unavailable') return this.ready()
if (this.recoveries >= this.maxRecoveries) return Promise.resolve(false)
this.state = 'idle'
this.compiled = null
this.setState('idle')
// Only a failed first load clears the compiled module: once an instance ran, it is sound.
if (this.generation === 0) this.compiled = null
this.pending = null
return this.ready()
}
/** Starts a restart when the next call may succeed (the current call still answers). */
kickRestart(): void {
if (this.restartable) void this.retry()
}
/** Runs a free operation. */
call<T>(op: string, input: string): CoreOutcome<T> {
return this.invoke((glue) => parseOutcome<T>(glue.call(op, input)))
@@ -116,7 +146,10 @@ export class CoreRuntime {
*/
invoke<T>(fn: (glue: GlueModule) => CoreOutcome<T>): CoreOutcome<T> {
const glue = this.glue
if (this.state !== 'ready' || !glue) return CORE_UNAVAILABLE
if (this.state !== 'ready' || !glue) {
this.kickRestart()
return CORE_UNAVAILABLE
}
try {
return fn(glue)
} catch (error) {
@@ -126,12 +159,12 @@ export class CoreRuntime {
}
private async load(): Promise<boolean> {
this.state = 'loading'
this.setState('loading')
try {
await this.instantiate(await this.source.importGlue(this.nextImport()))
return true
} catch {
this.state = 'unavailable'
this.setState('unavailable')
this.pending = null
return false
}
@@ -142,12 +175,28 @@ export class CoreRuntime {
await glue.default({ module_or_path: await this.compiled })
this.glue = glue
this.generation += 1
this.state = 'ready'
this.setState('ready')
this.spare = this.source
.importGlue(this.nextImport())
.catch(() => null)
}
private setState(next: CoreStatus): void {
if (next === this.state) return
this.state = next
queueMicrotask(() => this.notify())
}
private notify(): void {
for (const listener of [...this.listeners]) {
try {
listener()
} catch {
// A listener never changes the state machine.
}
}
}
private nextImport(): number {
this.imports += 1
return this.imports
@@ -156,11 +205,11 @@ export class CoreRuntime {
private poison(): void {
this.glue = null
if (this.recoveries >= this.maxRecoveries) {
this.state = 'unavailable'
this.setState('unavailable')
return
}
this.recoveries += 1
this.state = 'recovering'
this.setState('recovering')
this.pending = this.recover()
}
@@ -172,7 +221,7 @@ export class CoreRuntime {
)
return true
} catch {
this.state = 'unavailable'
this.setState('unavailable')
return false
} finally {
this.pending = null
@@ -220,9 +269,9 @@ export class SheetSession {
/** The handle of the live instance; a handle of a dropped instance is never called. */
private current(): CoreOutcome<GlueSheetHandle> {
if (this.handle && this.handleGeneration === this.runtime.generation) {
return this.runtime.status === 'ready'
? { ok: true, value: this.handle }
: CORE_UNAVAILABLE
if (this.runtime.status === 'ready') return { ok: true, value: this.handle }
this.runtime.kickRestart()
return CORE_UNAVAILABLE
}
this.handle = null
return this.runtime.invoke((glue) => {