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

+129
View File
@@ -47,6 +47,38 @@ function countingSource(offlineAfter = Infinity) {
return source
}
/**
* Counts compiles and imports; the numbered glue imports in `failing` are refused like a
* lost network (import 1 is the first instance, 2 its spare, 3 the recovery's fallback).
*/
function flakySource(failing: number[]) {
const inner = nodeCoreSource('pkg-proofs')
const source = {
compiles: 0,
imports: 0,
compile: () => {
source.compiles += 1
return inner.compile()
},
importGlue: async (n: number): Promise<GlueModule> => {
source.imports += 1
if (failing.includes(source.imports)) throw new TypeError('offline')
return inner.importGlue(n)
},
}
return source
}
/**
* A handle of a trapped instance is freed by the glue's finalizer inside the dead module,
* which throws out of the garbage collector at a random later time. Tests that trap with a
* live handle keep it reachable until the process ends.
*/
const retainedHandles: unknown[] = []
function retainHandle(sheet: object): void {
retainedHandles.push((sheet as { handle: unknown }).handle)
}
/** Traps the live instance through the proofs build (stack exhaustion). */
function trap(runtime: CoreRuntime) {
return runtime.invoke((glue) => {
@@ -91,6 +123,7 @@ describe('core runtime', () => {
ok: true,
value: { ref: { sheetId: 'sheet', versionId: null } },
})
retainHandle(sheet)
trap(runtime)
expect(sheet.call('index_data', '')).toMatchObject({
kind: 'core_unavailable',
@@ -180,4 +213,100 @@ describe('core runtime', () => {
expect(runtime.sheet(BUNDLE).open()).toMatchObject({ ok: false, kind: 'internal' })
expect(runtime.status).not.toBe('ready')
})
it('allows three recoveries by default, then stops on the fourth trap', async () => {
const runtime = new CoreRuntime(countingSource())
await runtime.ready()
for (let i = 0; i < 3; i += 1) {
trap(runtime)
expect(await runtime.ready()).toBe(true)
expect(runtime.stopped).toBe(false)
}
trap(runtime)
expect(runtime.stopped).toBe(true)
expect(await runtime.retry()).toBe(false)
})
it('restarts on the next call after a failed recovery below the cap', async () => {
const source = flakySource([2, 3])
const runtime = new CoreRuntime(source)
await runtime.ready()
trap(runtime)
expect(await runtime.ready()).toBe(false)
expect(runtime.stopped).toBe(false)
expect(runtime.ready()).toBeInstanceOf(Promise)
expect(await runtime.ready()).toBe(true)
expect(runtime.call('core_version', '')).toMatchObject({ ok: true })
// A restart is not a recovery, and it reuses the compiled module.
expect(source.compiles).toBe(1)
})
it('kicks the restart from a call and answers the next one', async () => {
const runtime = new CoreRuntime(flakySource([2, 3]))
await runtime.ready()
const sheet = runtime.sheet(BUNDLE)
trap(runtime)
expect(await runtime.ready()).toBe(false)
expect(runtime.call('core_version', '')).toMatchObject({ kind: 'core_unavailable' })
expect(runtime.status).toBe('loading')
expect(await runtime.ready()).toBe(true)
expect(sheet.call('index_data', '')).toMatchObject({ ok: true })
})
it('restarts from a sheet call as well', async () => {
const runtime = new CoreRuntime(flakySource([2, 3]))
await runtime.ready()
const sheet = runtime.sheet(BUNDLE)
expect(sheet.open()).toEqual({ ok: true, value: null })
retainHandle(sheet)
trap(runtime)
await runtime.ready()
expect(sheet.call('index_data', '')).toMatchObject({ kind: 'core_unavailable' })
expect(runtime.status).toBe('loading')
await runtime.ready()
expect(sheet.call('index_data', '')).toMatchObject({ ok: true })
})
it('does not restart once the cap is spent', async () => {
const source = flakySource([2, 3])
const runtime = new CoreRuntime(source, { maxRecoveries: 1 })
await runtime.ready()
trap(runtime)
expect(await runtime.ready()).toBe(false)
expect(runtime.stopped).toBe(true)
const imports = source.imports
expect(runtime.call('core_version', '')).toMatchObject({ kind: 'core_unavailable' })
expect(await runtime.ready()).toBe(false)
expect(runtime.status).toBe('unavailable')
expect(source.imports).toBe(imports)
})
it('does not restart a failed first load from a call', async () => {
const source = flakySource([1])
const runtime = new CoreRuntime(source)
expect(await runtime.ready()).toBe(false)
expect(runtime.call('core_version', '')).toMatchObject({ kind: 'core_unavailable' })
expect(await runtime.ready()).toBe(false)
expect(source.imports).toBe(1)
expect(await runtime.retry()).toBe(true)
})
it('notifies listeners after a microtask and survives a throwing one', async () => {
const runtime = new CoreRuntime(countingSource(), { maxRecoveries: 0 })
await runtime.ready()
const calls: boolean[] = []
runtime.subscribe(() => {
throw new Error('listener bug')
})
const unsubscribe = runtime.subscribe(() => calls.push(runtime.stopped))
trap(runtime)
expect(calls).toEqual([])
await Promise.resolve()
expect(calls).toEqual([true])
expect(runtime.stopped).toBe(true)
unsubscribe()
await runtime.retry()
await Promise.resolve()
expect(calls).toEqual([true])
})
})