pcbjam/tests/kicad/utils/ngspice-service.ts

718 lines
33 KiB
TypeScript
Raw Normal View History

import * as fs from 'fs';
import * as path from 'path';
import type { Page } from '@playwright/test';
/**
* Install a REAL `globalThis.ngspiceService` provider into a harness page —
* the same worker-backed ngspice_service boot the standalone app does
* (web/standalone/src/wasm/ngspice-service.ts), minus the CDN manifest
* resolution: the harness serves ngspice_service.{js,wasm} same-origin next
* to the tool page (tests/scripts/setup-kicad-wasm.sh copies them from
* output/).
*
* The worker-side wrapper is the SHARED source of truth
* (web/standalone/src/wasm/ngspice-worker.js — the standalone imports it via
* vite `?raw`; the harness reads it off disk and injects it verbatim), so the
* boot logic cannot drift between app and tests.
*
* Additions for assertability:
* - every `{ evt }` frame is appended to window.__ngspiceEvents
* ({ kind, lines?, finished?, status?, t: ms-since-install }) BEFORE being
* forwarded to globalThis.__ngspiceOnEvent (the editor client stub's
* dispatcher, when integrated) — specs assert live streaming by comparing
* event timestamps against run boundaries;
findings(E-1,E-2,E-3,E-6): ngspice service generations + credit-bounded event transport Adapted from codex/asyncify-execution-owner-core 3753320. Service side mirrors the occ-service shape (E-1 watchdogs, E-2 fail-all + boot-death fix — onerror now rejects the in-flight boot waiter instead of stranding it, E-3 onmessageerror terminal, Blob URL revoked, per-generation evtQueue cleared on retirement). E-6 transport bounds (worker hunks re-applied inside the emscripten-6 em-pthread else-branch — the codex file predates that split, so this is a re-application, not a cherry-pick): - batch cut at 512 lines / 1 MiB exact JSON-UTF-8 bytes, measured before a line is retained; a single line > 1 MiB flushes the accepted prefix then stops the event stream terminally (never retained); - posting gated by a 64-frame / 8 MiB unacked credit window; each frame carries { eventSequence, eventBytes } and is released only by an exact { sequence, bytes } ack; any mismatched ack is terminal; - the service mirrors the same 64-frame / 8 MiB bound on its pre-handler queue, acks after handing a frame to __ngspiceOnEvent, and retires the generation on invalid credit; { fatal } frames retire the worker. Tests: ngspice-service.test.ts (11, ported) — watchdogs, crash/bootError/ decode-fault settlement + recovery, out-of-order ids, sync postMessage throw, stale-generation event drops, fatal-frame retirement. tests/tools/ ngspice-worker-batch-unit.ts (node:vm over the production worker source; `npm run ngspice:worker-batch`) — bounded ordered chunks, byte-pressure flush, 100k-chunk credit storm, over-limit line, exact ack lease. e2e harness twin updated to speak the ack protocol (adds __ngspiceServiceTestHooks). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-19 15:48:41 +02:00
* - request/response summaries are appended to window.__ngspiceLog. Each
* carries the sequence assigned when the request was issued. The test hook
* offers a scan-then-subscribe receipt so a response cannot land in the
* gap between an array scan and listener installation;
* - Worker generations own their Blob URL, boot deadline, response deadlines,
* pending calls, and queued events. Retirement settles and cleans only that
* exact generation;
* - the native simulator publishes its run generation only after the final
* plot, operating-point, and canvas refresh calls. The harness stores an
* atomic scan-then-subscribe receipt for that applied generation.
*
* The worker fetches ngspice_service.js lazily on the FIRST request — specs
* assert the lazy-load boundary by watching network requests.
*/
const NGSPICE_WORKER_SRC = fs.readFileSync(
path.resolve(__dirname, '..', '..', '..',
'web', 'standalone', 'src', 'wasm', 'ngspice-worker.js'),
'utf8');
findings(E-1,E-2,E-3,E-6): ngspice service generations + credit-bounded event transport Adapted from codex/asyncify-execution-owner-core 3753320. Service side mirrors the occ-service shape (E-1 watchdogs, E-2 fail-all + boot-death fix — onerror now rejects the in-flight boot waiter instead of stranding it, E-3 onmessageerror terminal, Blob URL revoked, per-generation evtQueue cleared on retirement). E-6 transport bounds (worker hunks re-applied inside the emscripten-6 em-pthread else-branch — the codex file predates that split, so this is a re-application, not a cherry-pick): - batch cut at 512 lines / 1 MiB exact JSON-UTF-8 bytes, measured before a line is retained; a single line > 1 MiB flushes the accepted prefix then stops the event stream terminally (never retained); - posting gated by a 64-frame / 8 MiB unacked credit window; each frame carries { eventSequence, eventBytes } and is released only by an exact { sequence, bytes } ack; any mismatched ack is terminal; - the service mirrors the same 64-frame / 8 MiB bound on its pre-handler queue, acks after handing a frame to __ngspiceOnEvent, and retires the generation on invalid credit; { fatal } frames retire the worker. Tests: ngspice-service.test.ts (11, ported) — watchdogs, crash/bootError/ decode-fault settlement + recovery, out-of-order ids, sync postMessage throw, stale-generation event drops, fatal-frame retirement. tests/tools/ ngspice-worker-batch-unit.ts (node:vm over the production worker source; `npm run ngspice:worker-batch`) — bounded ordered chunks, byte-pressure flush, 100k-chunk credit storm, over-limit line, exact ack lease. e2e harness twin updated to speak the ack protocol (adds __ngspiceServiceTestHooks). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-19 15:48:41 +02:00
export interface NgspiceHarnessWatchdogs {
bootTimeoutMs?: number;
responseTimeoutMs?: number;
}
export async function installNgspiceServiceStub(
page: Page,
watchdogs: NgspiceHarnessWatchdogs = {},
): Promise<void> {
const validDeadline = (value: number | undefined, fallback: number, name: string): number => {
const deadline = value ?? fallback;
if (!Number.isSafeInteger(deadline) || deadline < 1)
throw new Error(`${name} must be a positive safe integer`);
return deadline;
};
const bootTimeoutMs = validDeadline(watchdogs.bootTimeoutMs, 2 * 60_000, 'bootTimeoutMs');
const responseTimeoutMs = validDeadline(
watchdogs.responseTimeoutMs,
30 * 60_000,
'responseTimeoutMs',
);
await page.addInitScript((options: {
workerSrc: string;
bootTimeoutMs: number;
responseTimeoutMs: number;
}) => {
if ((globalThis as any).ngspiceService) return;
findings(E-1,E-2,E-3,E-6): ngspice service generations + credit-bounded event transport Adapted from codex/asyncify-execution-owner-core 3753320. Service side mirrors the occ-service shape (E-1 watchdogs, E-2 fail-all + boot-death fix — onerror now rejects the in-flight boot waiter instead of stranding it, E-3 onmessageerror terminal, Blob URL revoked, per-generation evtQueue cleared on retirement). E-6 transport bounds (worker hunks re-applied inside the emscripten-6 em-pthread else-branch — the codex file predates that split, so this is a re-application, not a cherry-pick): - batch cut at 512 lines / 1 MiB exact JSON-UTF-8 bytes, measured before a line is retained; a single line > 1 MiB flushes the accepted prefix then stops the event stream terminally (never retained); - posting gated by a 64-frame / 8 MiB unacked credit window; each frame carries { eventSequence, eventBytes } and is released only by an exact { sequence, bytes } ack; any mismatched ack is terminal; - the service mirrors the same 64-frame / 8 MiB bound on its pre-handler queue, acks after handing a frame to __ngspiceOnEvent, and retires the generation on invalid credit; { fatal } frames retire the worker. Tests: ngspice-service.test.ts (11, ported) — watchdogs, crash/bootError/ decode-fault settlement + recovery, out-of-order ids, sync postMessage throw, stale-generation event drops, fatal-frame retirement. tests/tools/ ngspice-worker-batch-unit.ts (node:vm over the production worker source; `npm run ngspice:worker-batch`) — bounded ordered chunks, byte-pressure flush, 100k-chunk credit storm, over-limit line, exact ack lease. e2e harness twin updated to speak the ack protocol (adds __ngspiceServiceTestHooks). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-19 15:48:41 +02:00
const { workerSrc, bootTimeoutMs, responseTimeoutMs } = options;
const t0 = Date.now();
(window as any).__ngspiceEvents = [];
(window as any).__ngspiceLog = [];
findings(E-1,E-2,E-3,E-6): ngspice service generations + credit-bounded event transport Adapted from codex/asyncify-execution-owner-core 3753320. Service side mirrors the occ-service shape (E-1 watchdogs, E-2 fail-all + boot-death fix — onerror now rejects the in-flight boot waiter instead of stranding it, E-3 onmessageerror terminal, Blob URL revoked, per-generation evtQueue cleared on retirement). E-6 transport bounds (worker hunks re-applied inside the emscripten-6 em-pthread else-branch — the codex file predates that split, so this is a re-application, not a cherry-pick): - batch cut at 512 lines / 1 MiB exact JSON-UTF-8 bytes, measured before a line is retained; a single line > 1 MiB flushes the accepted prefix then stops the event stream terminally (never retained); - posting gated by a 64-frame / 8 MiB unacked credit window; each frame carries { eventSequence, eventBytes } and is released only by an exact { sequence, bytes } ack; any mismatched ack is terminal; - the service mirrors the same 64-frame / 8 MiB bound on its pre-handler queue, acks after handing a frame to __ngspiceOnEvent, and retires the generation on invalid credit; { fatal } frames retire the worker. Tests: ngspice-service.test.ts (11, ported) — watchdogs, crash/bootError/ decode-fault settlement + recovery, out-of-order ids, sync postMessage throw, stale-generation event drops, fatal-frame retirement. tests/tools/ ngspice-worker-batch-unit.ts (node:vm over the production worker source; `npm run ngspice:worker-batch`) — bounded ordered chunks, byte-pressure flush, 100k-chunk credit storm, over-limit line, exact ack lease. e2e harness twin updated to speak the ack protocol (adds __ngspiceServiceTestHooks). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-19 15:48:41 +02:00
interface WorkerSlot {
generation: number;
worker?: Worker;
workerUrl?: string;
failed: boolean;
ready: Promise<WorkerSlot>;
bootTimer?: ReturnType<typeof setTimeout>;
rejectBoot?: (reason?: unknown) => void;
removeBootListener?: () => void;
/** The exact lifecycle transition used by Worker.onmessageerror. */
failDecode: () => void;
}
interface PendingRequest {
generation: number;
resolve: (res: any) => void;
timer: ReturnType<typeof setTimeout>;
}
interface RequestSummary {
sequence: number;
kind: string;
cmd?: string;
name?: string;
ret?: number;
error?: string;
length?: number;
t: number;
}
interface RequestCriteria {
kind?: string;
name?: string;
minimumLength?: number;
}
interface AppliedGenerationReceipt {
generation: number;
t: number;
}
const RECEIPT_TIMEOUT_MS = 2 * 60_000;
const MAX_RECEIPT_TIMEOUT_MS = 5 * 60_000;
const MAX_RECEIPT_WAITERS = 128;
let workerSlot: WorkerSlot | null = null;
let nextGeneration = 1;
const pending = new Map<number, PendingRequest>();
let nextId = 1;
findings(E-1,E-2,E-3,E-6): ngspice service generations + credit-bounded event transport Adapted from codex/asyncify-execution-owner-core 3753320. Service side mirrors the occ-service shape (E-1 watchdogs, E-2 fail-all + boot-death fix — onerror now rejects the in-flight boot waiter instead of stranding it, E-3 onmessageerror terminal, Blob URL revoked, per-generation evtQueue cleared on retirement). E-6 transport bounds (worker hunks re-applied inside the emscripten-6 em-pthread else-branch — the codex file predates that split, so this is a re-application, not a cherry-pick): - batch cut at 512 lines / 1 MiB exact JSON-UTF-8 bytes, measured before a line is retained; a single line > 1 MiB flushes the accepted prefix then stops the event stream terminally (never retained); - posting gated by a 64-frame / 8 MiB unacked credit window; each frame carries { eventSequence, eventBytes } and is released only by an exact { sequence, bytes } ack; any mismatched ack is terminal; - the service mirrors the same 64-frame / 8 MiB bound on its pre-handler queue, acks after handing a frame to __ngspiceOnEvent, and retires the generation on invalid credit; { fatal } frames retire the worker. Tests: ngspice-service.test.ts (11, ported) — watchdogs, crash/bootError/ decode-fault settlement + recovery, out-of-order ids, sync postMessage throw, stale-generation event drops, fatal-frame retirement. tests/tools/ ngspice-worker-batch-unit.ts (node:vm over the production worker source; `npm run ngspice:worker-batch`) — bounded ordered chunks, byte-pressure flush, 100k-chunk credit storm, over-limit line, exact ack lease. e2e harness twin updated to speak the ack protocol (adds __ngspiceServiceTestHooks). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-19 15:48:41 +02:00
let maxPending = 0;
let bootMessageErrorArmed = false;
let runtimeMessageErrorThreshold: number | null = null;
findings(E-10..E-22): fix the defects a code review found in the E-1..E-9 work A review of the group-E fixes found 13 further defects; ten were introduced by those fixes, two pre-existed and were merely relocated, one is deferred. Services / transport E-10 retireWorker synthesized no bg/exit frame, so sharedspice's s_bgRunning mirror stayed latched true after a mid-run worker death: Run stayed disabled and the promised fresh-worker restart was unreachable for the whole session. Retirement now dispatches a synthetic controlled-exit straight to the installed handler (never through dispatchEvt — a fabricated frame must not touch the credit ledger). Driving the repro exposed two further defects, both fixed here: a replacement worker trapped on pre-init engine reads, and the rerun's cm_input_path/circ hit that uninitialized engine before KiCad's validate() re-init (the native flow assumes a crashed engine survives in-process — true for the dll, false for a dead worker). Reads now answer their empty shapes pre-init, writes lazy-init, and init is idempotent per worker engine. E-19 dispatchEvt acked only AFTER handler(evt) returned, and the sharedspice client deliberately rethrows non-trap errors — so each throw leaked one unit of the 64-frame credit window until the stream died with a misattributed "transport exceeded". The ack moves to a finally in both service copies; the throw still propagates (the trap machinery needs it). E-20 the oversize-line path promises to transfer the accepted prefix, but with the window full that flush only DEFERS, and stopEventStream wiped the deferred queue — losing the diagnostics that explain the failure. The terminal notice now carries them as pendingEvents; both hosts deliver them in order, unacked (the fatal frame is outside the credit protocol). E-21 the 30s prefetch deadline discarded every model already collected and reported nothing. A caller-owned progress sink ships the partials and the omission reaches the export report. (Awaiting the aborted collection was rejected: an in-flight source fetch is not abortable — E-4's original disease.) Plus a serving-candidate memo, so a .wrl ref served by its .step fallback stops re-probing the miss on every export. Scheduler E-14 _terminalizeNativeTrap classified by message substring, so any plain JS error QUOTING 'Aborted(' or 'out of bounds' permanently bricked a healthy instance. Now structural only: instanceof RuntimeError plus a duck-typed name check (verified in this build's glue that abort() throws a genuine RuntimeError both pre- and post-runtime-init). Module.onAbort now latches the gate — the authoritative notification, previously ignored. E-15 the shim half: _pumpResume gates on terminal (catching wakes already queued at latch time) and resolveWait refuses on terminal WITHOUT consuming the entry, so a frame stays visibly parked rather than resuming inside a trapped module. E-16 the E-5 handler read the realm-global scheduler at dispatch instead of its installing module's; also frees the per-line buffer on the non-trap rethrow path. E-11 get_vec trusted the worker's res.length over the transferred arrays. Observed death shape: a 4 GiB std::vector threw an unhandled std::length_error that exited the editor's main loop. Now clamped, with the buffers freed on every failure path. Guardrails (replacing two deferred refactors: e2e→production-code injection and collapsing the four copies of the worker-lifecycle machinery) E-18 the source contract asserted comment-string counts — rewording failed CI while moving a guard outside its #ifdef passed. It now parses the #ifdef regions and asserts on code. service-stub-parity.ts pins what the four lifecycle copies must share: credit-window equality parsed from source, the finally-ack, boot deadlines, terminal-notice consumption. The transport numbers are now single-sourced from the worker. CI actually runs the gates: the web/standalone vitest suites (which had NEVER run in CI), the reducer, the source contract and the parity tool — with a NON_PLAYWRIGHT_GATES check so deleting a step re-fails the lint. E-22 the e2e occ stub's 60s boot watchdog, deleted in a66e109, is restored in the ngspice-stub shape with a wedgeNextBoot() repro hook. Every behavioral fix has red-then-green evidence (the reds were captured first). E-17 (a stale RUNNING cross-stamping the next run's generation under E-6's transport deferral) is DEFERRED with its analysis recorded — a real fix needs run identity on the bg frames. Test hygiene: the dwell lint now requires the mandated ": <why>" and all 47 bare markers carry their reason; three export-report dwells became modal-lease polls; exact-ledger assertions became relative deltas; the dead data-wx-dom-id branch, an unused fault hook and unused receipt plumbing are gone; abort scans, wx dialog drivers, the sim harness and the vitest FakeWorker are each one copy now. Bumps kicad and wxwidgets to their findings-group-e tips. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-27 12:27:12 +02:00
let dieOnNextBgRunArmed = false;
let corruptNextGetVecArmed = false;
findings(E-1,E-2,E-3,E-6): ngspice service generations + credit-bounded event transport Adapted from codex/asyncify-execution-owner-core 3753320. Service side mirrors the occ-service shape (E-1 watchdogs, E-2 fail-all + boot-death fix — onerror now rejects the in-flight boot waiter instead of stranding it, E-3 onmessageerror terminal, Blob URL revoked, per-generation evtQueue cleared on retirement). E-6 transport bounds (worker hunks re-applied inside the emscripten-6 em-pthread else-branch — the codex file predates that split, so this is a re-application, not a cherry-pick): - batch cut at 512 lines / 1 MiB exact JSON-UTF-8 bytes, measured before a line is retained; a single line > 1 MiB flushes the accepted prefix then stops the event stream terminally (never retained); - posting gated by a 64-frame / 8 MiB unacked credit window; each frame carries { eventSequence, eventBytes } and is released only by an exact { sequence, bytes } ack; any mismatched ack is terminal; - the service mirrors the same 64-frame / 8 MiB bound on its pre-handler queue, acks after handing a frame to __ngspiceOnEvent, and retires the generation on invalid credit; { fatal } frames retire the worker. Tests: ngspice-service.test.ts (11, ported) — watchdogs, crash/bootError/ decode-fault settlement + recovery, out-of-order ids, sync postMessage throw, stale-generation event drops, fatal-frame retirement. tests/tools/ ngspice-worker-batch-unit.ts (node:vm over the production worker source; `npm run ngspice:worker-batch`) — bounded ordered chunks, byte-pressure flush, 100k-chunk credit storm, over-limit line, exact ack lease. e2e harness twin updated to speak the ack protocol (adds __ngspiceServiceTestHooks). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-19 15:48:41 +02:00
const retiredGenerations: number[] = [];
let nextRequestSequence = 1;
const appliedGenerations: AppliedGenerationReceipt[] = [];
let disposed = false;
const validateReceiptTimeout = (timeoutMs: number): string | undefined => {
if (!Number.isSafeInteger(timeoutMs) || timeoutMs < 1
|| timeoutMs > MAX_RECEIPT_TIMEOUT_MS) {
return `receipt timeout must be an integer from 1 to ${MAX_RECEIPT_TIMEOUT_MS} ms`;
}
return undefined;
};
const requestMatches = (
summary: RequestSummary,
after: number,
criteria: RequestCriteria,
): boolean => summary.sequence > after
&& summary.error === undefined
&& (criteria.kind === undefined || summary.kind === criteria.kind)
&& (criteria.name === undefined || summary.name === criteria.name)
&& (criteria.minimumLength === undefined
|| (summary.length ?? -1) >= criteria.minimumLength);
findings(E-10..E-22): fix the defects a code review found in the E-1..E-9 work A review of the group-E fixes found 13 further defects; ten were introduced by those fixes, two pre-existed and were merely relocated, one is deferred. Services / transport E-10 retireWorker synthesized no bg/exit frame, so sharedspice's s_bgRunning mirror stayed latched true after a mid-run worker death: Run stayed disabled and the promised fresh-worker restart was unreachable for the whole session. Retirement now dispatches a synthetic controlled-exit straight to the installed handler (never through dispatchEvt — a fabricated frame must not touch the credit ledger). Driving the repro exposed two further defects, both fixed here: a replacement worker trapped on pre-init engine reads, and the rerun's cm_input_path/circ hit that uninitialized engine before KiCad's validate() re-init (the native flow assumes a crashed engine survives in-process — true for the dll, false for a dead worker). Reads now answer their empty shapes pre-init, writes lazy-init, and init is idempotent per worker engine. E-19 dispatchEvt acked only AFTER handler(evt) returned, and the sharedspice client deliberately rethrows non-trap errors — so each throw leaked one unit of the 64-frame credit window until the stream died with a misattributed "transport exceeded". The ack moves to a finally in both service copies; the throw still propagates (the trap machinery needs it). E-20 the oversize-line path promises to transfer the accepted prefix, but with the window full that flush only DEFERS, and stopEventStream wiped the deferred queue — losing the diagnostics that explain the failure. The terminal notice now carries them as pendingEvents; both hosts deliver them in order, unacked (the fatal frame is outside the credit protocol). E-21 the 30s prefetch deadline discarded every model already collected and reported nothing. A caller-owned progress sink ships the partials and the omission reaches the export report. (Awaiting the aborted collection was rejected: an in-flight source fetch is not abortable — E-4's original disease.) Plus a serving-candidate memo, so a .wrl ref served by its .step fallback stops re-probing the miss on every export. Scheduler E-14 _terminalizeNativeTrap classified by message substring, so any plain JS error QUOTING 'Aborted(' or 'out of bounds' permanently bricked a healthy instance. Now structural only: instanceof RuntimeError plus a duck-typed name check (verified in this build's glue that abort() throws a genuine RuntimeError both pre- and post-runtime-init). Module.onAbort now latches the gate — the authoritative notification, previously ignored. E-15 the shim half: _pumpResume gates on terminal (catching wakes already queued at latch time) and resolveWait refuses on terminal WITHOUT consuming the entry, so a frame stays visibly parked rather than resuming inside a trapped module. E-16 the E-5 handler read the realm-global scheduler at dispatch instead of its installing module's; also frees the per-line buffer on the non-trap rethrow path. E-11 get_vec trusted the worker's res.length over the transferred arrays. Observed death shape: a 4 GiB std::vector threw an unhandled std::length_error that exited the editor's main loop. Now clamped, with the buffers freed on every failure path. Guardrails (replacing two deferred refactors: e2e→production-code injection and collapsing the four copies of the worker-lifecycle machinery) E-18 the source contract asserted comment-string counts — rewording failed CI while moving a guard outside its #ifdef passed. It now parses the #ifdef regions and asserts on code. service-stub-parity.ts pins what the four lifecycle copies must share: credit-window equality parsed from source, the finally-ack, boot deadlines, terminal-notice consumption. The transport numbers are now single-sourced from the worker. CI actually runs the gates: the web/standalone vitest suites (which had NEVER run in CI), the reducer, the source contract and the parity tool — with a NON_PLAYWRIGHT_GATES check so deleting a step re-fails the lint. E-22 the e2e occ stub's 60s boot watchdog, deleted in a66e109, is restored in the ngspice-stub shape with a wedgeNextBoot() repro hook. Every behavioral fix has red-then-green evidence (the reds were captured first). E-17 (a stale RUNNING cross-stamping the next run's generation under E-6's transport deferral) is DEFERRED with its analysis recorded — a real fix needs run identity on the bg frames. Test hygiene: the dwell lint now requires the mandated ": <why>" and all 47 bare markers carry their reason; three export-report dwells became modal-lease polls; exact-ledger assertions became relative deltas; the dead data-wx-dom-id branch, an unused fault hook and unused receipt plumbing are gone; abort scans, wx dialog drivers, the sim harness and the vitest FakeWorker are each one copy now. Bumps kicad and wxwidgets to their findings-group-e tips. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-27 12:27:12 +02:00
interface ReceiptWaiter<TReceipt, TCriteria> {
after: number;
criteria: TCriteria;
resolve: (receipt: TReceipt) => void;
reject: (reason?: unknown) => void;
timer: ReturnType<typeof setTimeout>;
}
/**
* One scan-then-subscribe receipt channel (shared by the request and
* applied-generation receipts, which differ only in their match
* predicate, existing-receipt scan, and error strings): scan the
* already-published receipts first, otherwise subscribe a bounded,
* timed waiter — so a receipt cannot land in the gap between an array
* scan and listener installation.
*/
const makeReceiptChannel = <TReceipt, TCriteria>(channel: {
validate: (after: number, criteria: TCriteria) => string | undefined;
/** Defensive copy of the criteria, taken only after validation. */
snapshotCriteria?: (criteria: TCriteria) => TCriteria;
scanExisting: (after: number, criteria: TCriteria) => TReceipt | undefined;
matches: (receipt: TReceipt, after: number, criteria: TCriteria) => boolean;
capacityError: string;
timeoutError: (timeoutMs: number) => string;
}) => {
const waiters = new Set<ReceiptWaiter<TReceipt, TCriteria>>();
const rejectWaiter = (
waiter: ReceiptWaiter<TReceipt, TCriteria>,
reason: Error,
): void => {
if (!waiters.delete(waiter)) return;
clearTimeout(waiter.timer);
waiter.reject(reason);
};
return {
wait(after: number, criteria: TCriteria, timeoutMs: number): Promise<TReceipt> {
if (disposed)
return Promise.reject(new Error('ngspice receipt service was disposed'));
const invalid = channel.validate(after, criteria)
?? validateReceiptTimeout(timeoutMs);
if (invalid) return Promise.reject(new Error(invalid));
const existing = channel.scanExisting(after, criteria);
if (existing) return Promise.resolve(existing);
if (waiters.size >= MAX_RECEIPT_WAITERS)
return Promise.reject(new Error(channel.capacityError));
const held = channel.snapshotCriteria
? channel.snapshotCriteria(criteria) : criteria;
return new Promise<TReceipt>((resolve, reject) => {
const waiter: ReceiptWaiter<TReceipt, TCriteria> = {
after,
criteria: held,
resolve,
reject,
timer: setTimeout(() => rejectWaiter(
waiter,
new Error(channel.timeoutError(timeoutMs)),
), timeoutMs),
};
waiters.add(waiter);
});
},
publish(receipt: TReceipt): void {
for (const waiter of [...waiters]) {
if (!channel.matches(receipt, waiter.after, waiter.criteria)) continue;
waiters.delete(waiter);
clearTimeout(waiter.timer);
waiter.resolve(receipt);
}
},
drain(reason: string): void {
for (const waiter of [...waiters]) rejectWaiter(waiter, new Error(reason));
},
size: () => waiters.size,
};
findings(E-1,E-2,E-3,E-6): ngspice service generations + credit-bounded event transport Adapted from codex/asyncify-execution-owner-core 3753320. Service side mirrors the occ-service shape (E-1 watchdogs, E-2 fail-all + boot-death fix — onerror now rejects the in-flight boot waiter instead of stranding it, E-3 onmessageerror terminal, Blob URL revoked, per-generation evtQueue cleared on retirement). E-6 transport bounds (worker hunks re-applied inside the emscripten-6 em-pthread else-branch — the codex file predates that split, so this is a re-application, not a cherry-pick): - batch cut at 512 lines / 1 MiB exact JSON-UTF-8 bytes, measured before a line is retained; a single line > 1 MiB flushes the accepted prefix then stops the event stream terminally (never retained); - posting gated by a 64-frame / 8 MiB unacked credit window; each frame carries { eventSequence, eventBytes } and is released only by an exact { sequence, bytes } ack; any mismatched ack is terminal; - the service mirrors the same 64-frame / 8 MiB bound on its pre-handler queue, acks after handing a frame to __ngspiceOnEvent, and retires the generation on invalid credit; { fatal } frames retire the worker. Tests: ngspice-service.test.ts (11, ported) — watchdogs, crash/bootError/ decode-fault settlement + recovery, out-of-order ids, sync postMessage throw, stale-generation event drops, fatal-frame retirement. tests/tools/ ngspice-worker-batch-unit.ts (node:vm over the production worker source; `npm run ngspice:worker-batch`) — bounded ordered chunks, byte-pressure flush, 100k-chunk credit storm, over-limit line, exact ack lease. e2e harness twin updated to speak the ack protocol (adds __ngspiceServiceTestHooks). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-19 15:48:41 +02:00
};
findings(E-10..E-22): fix the defects a code review found in the E-1..E-9 work A review of the group-E fixes found 13 further defects; ten were introduced by those fixes, two pre-existed and were merely relocated, one is deferred. Services / transport E-10 retireWorker synthesized no bg/exit frame, so sharedspice's s_bgRunning mirror stayed latched true after a mid-run worker death: Run stayed disabled and the promised fresh-worker restart was unreachable for the whole session. Retirement now dispatches a synthetic controlled-exit straight to the installed handler (never through dispatchEvt — a fabricated frame must not touch the credit ledger). Driving the repro exposed two further defects, both fixed here: a replacement worker trapped on pre-init engine reads, and the rerun's cm_input_path/circ hit that uninitialized engine before KiCad's validate() re-init (the native flow assumes a crashed engine survives in-process — true for the dll, false for a dead worker). Reads now answer their empty shapes pre-init, writes lazy-init, and init is idempotent per worker engine. E-19 dispatchEvt acked only AFTER handler(evt) returned, and the sharedspice client deliberately rethrows non-trap errors — so each throw leaked one unit of the 64-frame credit window until the stream died with a misattributed "transport exceeded". The ack moves to a finally in both service copies; the throw still propagates (the trap machinery needs it). E-20 the oversize-line path promises to transfer the accepted prefix, but with the window full that flush only DEFERS, and stopEventStream wiped the deferred queue — losing the diagnostics that explain the failure. The terminal notice now carries them as pendingEvents; both hosts deliver them in order, unacked (the fatal frame is outside the credit protocol). E-21 the 30s prefetch deadline discarded every model already collected and reported nothing. A caller-owned progress sink ships the partials and the omission reaches the export report. (Awaiting the aborted collection was rejected: an in-flight source fetch is not abortable — E-4's original disease.) Plus a serving-candidate memo, so a .wrl ref served by its .step fallback stops re-probing the miss on every export. Scheduler E-14 _terminalizeNativeTrap classified by message substring, so any plain JS error QUOTING 'Aborted(' or 'out of bounds' permanently bricked a healthy instance. Now structural only: instanceof RuntimeError plus a duck-typed name check (verified in this build's glue that abort() throws a genuine RuntimeError both pre- and post-runtime-init). Module.onAbort now latches the gate — the authoritative notification, previously ignored. E-15 the shim half: _pumpResume gates on terminal (catching wakes already queued at latch time) and resolveWait refuses on terminal WITHOUT consuming the entry, so a frame stays visibly parked rather than resuming inside a trapped module. E-16 the E-5 handler read the realm-global scheduler at dispatch instead of its installing module's; also frees the per-line buffer on the non-trap rethrow path. E-11 get_vec trusted the worker's res.length over the transferred arrays. Observed death shape: a 4 GiB std::vector threw an unhandled std::length_error that exited the editor's main loop. Now clamped, with the buffers freed on every failure path. Guardrails (replacing two deferred refactors: e2e→production-code injection and collapsing the four copies of the worker-lifecycle machinery) E-18 the source contract asserted comment-string counts — rewording failed CI while moving a guard outside its #ifdef passed. It now parses the #ifdef regions and asserts on code. service-stub-parity.ts pins what the four lifecycle copies must share: credit-window equality parsed from source, the finally-ack, boot deadlines, terminal-notice consumption. The transport numbers are now single-sourced from the worker. CI actually runs the gates: the web/standalone vitest suites (which had NEVER run in CI), the reducer, the source contract and the parity tool — with a NON_PLAYWRIGHT_GATES check so deleting a step re-fails the lint. E-22 the e2e occ stub's 60s boot watchdog, deleted in a66e109, is restored in the ngspice-stub shape with a wedgeNextBoot() repro hook. Every behavioral fix has red-then-green evidence (the reds were captured first). E-17 (a stale RUNNING cross-stamping the next run's generation under E-6's transport deferral) is DEFERRED with its analysis recorded — a real fix needs run identity on the bg frames. Test hygiene: the dwell lint now requires the mandated ": <why>" and all 47 bare markers carry their reason; three export-report dwells became modal-lease polls; exact-ledger assertions became relative deltas; the dead data-wx-dom-id branch, an unused fault hook and unused receipt plumbing are gone; abort scans, wx dialog drivers, the sim harness and the vitest FakeWorker are each one copy now. Bumps kicad and wxwidgets to their findings-group-e tips. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-27 12:27:12 +02:00
const requestReceipts = makeReceiptChannel<RequestSummary, RequestCriteria>({
validate: (after, criteria) => {
if (!Number.isSafeInteger(after) || after < 0)
return 'request checkpoint must be a non-negative integer';
if (!criteria || typeof criteria !== 'object')
return 'request receipt criteria must be an object';
if (criteria.minimumLength !== undefined
&& (!Number.isSafeInteger(criteria.minimumLength)
|| criteria.minimumLength < 0)) {
return 'minimumLength must be a non-negative integer';
}
return undefined;
},
snapshotCriteria: (criteria) => ({ ...criteria }),
scanExisting: (after, criteria) => ((window as any).__ngspiceLog as RequestSummary[])
.find((entry) => requestMatches(entry, after, criteria)),
matches: requestMatches,
capacityError: 'ngspice request receipt waiter capacity exceeded',
timeoutError: (timeoutMs) =>
`ngspice request receipt timed out after ${timeoutMs} ms`,
});
const appliedReceipts = makeReceiptChannel<AppliedGenerationReceipt, undefined>({
validate: (after) => (!Number.isSafeInteger(after) || after < 0)
? 'applied generation checkpoint must be a non-negative integer'
: undefined,
scanExisting: (after) => appliedGenerations.find((entry) => entry.generation > after),
matches: (receipt, after) => receipt.generation > after,
capacityError: 'ngspice applied-generation waiter capacity exceeded',
timeoutError: (timeoutMs) =>
`ngspice applied generation timed out after ${timeoutMs} ms`,
});
findings(E-1,E-2,E-3,E-6): ngspice service generations + credit-bounded event transport Adapted from codex/asyncify-execution-owner-core 3753320. Service side mirrors the occ-service shape (E-1 watchdogs, E-2 fail-all + boot-death fix — onerror now rejects the in-flight boot waiter instead of stranding it, E-3 onmessageerror terminal, Blob URL revoked, per-generation evtQueue cleared on retirement). E-6 transport bounds (worker hunks re-applied inside the emscripten-6 em-pthread else-branch — the codex file predates that split, so this is a re-application, not a cherry-pick): - batch cut at 512 lines / 1 MiB exact JSON-UTF-8 bytes, measured before a line is retained; a single line > 1 MiB flushes the accepted prefix then stops the event stream terminally (never retained); - posting gated by a 64-frame / 8 MiB unacked credit window; each frame carries { eventSequence, eventBytes } and is released only by an exact { sequence, bytes } ack; any mismatched ack is terminal; - the service mirrors the same 64-frame / 8 MiB bound on its pre-handler queue, acks after handing a frame to __ngspiceOnEvent, and retires the generation on invalid credit; { fatal } frames retire the worker. Tests: ngspice-service.test.ts (11, ported) — watchdogs, crash/bootError/ decode-fault settlement + recovery, out-of-order ids, sync postMessage throw, stale-generation event drops, fatal-frame retirement. tests/tools/ ngspice-worker-batch-unit.ts (node:vm over the production worker source; `npm run ngspice:worker-batch`) — bounded ordered chunks, byte-pressure flush, 100k-chunk credit storm, over-limit line, exact ack lease. e2e harness twin updated to speak the ack protocol (adds __ngspiceServiceTestHooks). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-19 15:48:41 +02:00
const publishRequestReceipt = (summary: RequestSummary) => {
(window as any).__ngspiceLog.push(summary);
findings(E-10..E-22): fix the defects a code review found in the E-1..E-9 work A review of the group-E fixes found 13 further defects; ten were introduced by those fixes, two pre-existed and were merely relocated, one is deferred. Services / transport E-10 retireWorker synthesized no bg/exit frame, so sharedspice's s_bgRunning mirror stayed latched true after a mid-run worker death: Run stayed disabled and the promised fresh-worker restart was unreachable for the whole session. Retirement now dispatches a synthetic controlled-exit straight to the installed handler (never through dispatchEvt — a fabricated frame must not touch the credit ledger). Driving the repro exposed two further defects, both fixed here: a replacement worker trapped on pre-init engine reads, and the rerun's cm_input_path/circ hit that uninitialized engine before KiCad's validate() re-init (the native flow assumes a crashed engine survives in-process — true for the dll, false for a dead worker). Reads now answer their empty shapes pre-init, writes lazy-init, and init is idempotent per worker engine. E-19 dispatchEvt acked only AFTER handler(evt) returned, and the sharedspice client deliberately rethrows non-trap errors — so each throw leaked one unit of the 64-frame credit window until the stream died with a misattributed "transport exceeded". The ack moves to a finally in both service copies; the throw still propagates (the trap machinery needs it). E-20 the oversize-line path promises to transfer the accepted prefix, but with the window full that flush only DEFERS, and stopEventStream wiped the deferred queue — losing the diagnostics that explain the failure. The terminal notice now carries them as pendingEvents; both hosts deliver them in order, unacked (the fatal frame is outside the credit protocol). E-21 the 30s prefetch deadline discarded every model already collected and reported nothing. A caller-owned progress sink ships the partials and the omission reaches the export report. (Awaiting the aborted collection was rejected: an in-flight source fetch is not abortable — E-4's original disease.) Plus a serving-candidate memo, so a .wrl ref served by its .step fallback stops re-probing the miss on every export. Scheduler E-14 _terminalizeNativeTrap classified by message substring, so any plain JS error QUOTING 'Aborted(' or 'out of bounds' permanently bricked a healthy instance. Now structural only: instanceof RuntimeError plus a duck-typed name check (verified in this build's glue that abort() throws a genuine RuntimeError both pre- and post-runtime-init). Module.onAbort now latches the gate — the authoritative notification, previously ignored. E-15 the shim half: _pumpResume gates on terminal (catching wakes already queued at latch time) and resolveWait refuses on terminal WITHOUT consuming the entry, so a frame stays visibly parked rather than resuming inside a trapped module. E-16 the E-5 handler read the realm-global scheduler at dispatch instead of its installing module's; also frees the per-line buffer on the non-trap rethrow path. E-11 get_vec trusted the worker's res.length over the transferred arrays. Observed death shape: a 4 GiB std::vector threw an unhandled std::length_error that exited the editor's main loop. Now clamped, with the buffers freed on every failure path. Guardrails (replacing two deferred refactors: e2e→production-code injection and collapsing the four copies of the worker-lifecycle machinery) E-18 the source contract asserted comment-string counts — rewording failed CI while moving a guard outside its #ifdef passed. It now parses the #ifdef regions and asserts on code. service-stub-parity.ts pins what the four lifecycle copies must share: credit-window equality parsed from source, the finally-ack, boot deadlines, terminal-notice consumption. The transport numbers are now single-sourced from the worker. CI actually runs the gates: the web/standalone vitest suites (which had NEVER run in CI), the reducer, the source contract and the parity tool — with a NON_PLAYWRIGHT_GATES check so deleting a step re-fails the lint. E-22 the e2e occ stub's 60s boot watchdog, deleted in a66e109, is restored in the ngspice-stub shape with a wedgeNextBoot() repro hook. Every behavioral fix has red-then-green evidence (the reds were captured first). E-17 (a stale RUNNING cross-stamping the next run's generation under E-6's transport deferral) is DEFERRED with its analysis recorded — a real fix needs run identity on the bg frames. Test hygiene: the dwell lint now requires the mandated ": <why>" and all 47 bare markers carry their reason; three export-report dwells became modal-lease polls; exact-ledger assertions became relative deltas; the dead data-wx-dom-id branch, an unused fault hook and unused receipt plumbing are gone; abort scans, wx dialog drivers, the sim harness and the vitest FakeWorker are each one copy now. Bumps kicad and wxwidgets to their findings-group-e tips. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-27 12:27:12 +02:00
requestReceipts.publish(summary);
findings(E-1,E-2,E-3,E-6): ngspice service generations + credit-bounded event transport Adapted from codex/asyncify-execution-owner-core 3753320. Service side mirrors the occ-service shape (E-1 watchdogs, E-2 fail-all + boot-death fix — onerror now rejects the in-flight boot waiter instead of stranding it, E-3 onmessageerror terminal, Blob URL revoked, per-generation evtQueue cleared on retirement). E-6 transport bounds (worker hunks re-applied inside the emscripten-6 em-pthread else-branch — the codex file predates that split, so this is a re-application, not a cherry-pick): - batch cut at 512 lines / 1 MiB exact JSON-UTF-8 bytes, measured before a line is retained; a single line > 1 MiB flushes the accepted prefix then stops the event stream terminally (never retained); - posting gated by a 64-frame / 8 MiB unacked credit window; each frame carries { eventSequence, eventBytes } and is released only by an exact { sequence, bytes } ack; any mismatched ack is terminal; - the service mirrors the same 64-frame / 8 MiB bound on its pre-handler queue, acks after handing a frame to __ngspiceOnEvent, and retires the generation on invalid credit; { fatal } frames retire the worker. Tests: ngspice-service.test.ts (11, ported) — watchdogs, crash/bootError/ decode-fault settlement + recovery, out-of-order ids, sync postMessage throw, stale-generation event drops, fatal-frame retirement. tests/tools/ ngspice-worker-batch-unit.ts (node:vm over the production worker source; `npm run ngspice:worker-batch`) — bounded ordered chunks, byte-pressure flush, 100k-chunk credit storm, over-limit line, exact ack lease. e2e harness twin updated to speak the ack protocol (adds __ngspiceServiceTestHooks). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-19 15:48:41 +02:00
};
const waitForRequestAfter = (
after: number,
criteria: RequestCriteria,
timeoutMs = RECEIPT_TIMEOUT_MS,
findings(E-10..E-22): fix the defects a code review found in the E-1..E-9 work A review of the group-E fixes found 13 further defects; ten were introduced by those fixes, two pre-existed and were merely relocated, one is deferred. Services / transport E-10 retireWorker synthesized no bg/exit frame, so sharedspice's s_bgRunning mirror stayed latched true after a mid-run worker death: Run stayed disabled and the promised fresh-worker restart was unreachable for the whole session. Retirement now dispatches a synthetic controlled-exit straight to the installed handler (never through dispatchEvt — a fabricated frame must not touch the credit ledger). Driving the repro exposed two further defects, both fixed here: a replacement worker trapped on pre-init engine reads, and the rerun's cm_input_path/circ hit that uninitialized engine before KiCad's validate() re-init (the native flow assumes a crashed engine survives in-process — true for the dll, false for a dead worker). Reads now answer their empty shapes pre-init, writes lazy-init, and init is idempotent per worker engine. E-19 dispatchEvt acked only AFTER handler(evt) returned, and the sharedspice client deliberately rethrows non-trap errors — so each throw leaked one unit of the 64-frame credit window until the stream died with a misattributed "transport exceeded". The ack moves to a finally in both service copies; the throw still propagates (the trap machinery needs it). E-20 the oversize-line path promises to transfer the accepted prefix, but with the window full that flush only DEFERS, and stopEventStream wiped the deferred queue — losing the diagnostics that explain the failure. The terminal notice now carries them as pendingEvents; both hosts deliver them in order, unacked (the fatal frame is outside the credit protocol). E-21 the 30s prefetch deadline discarded every model already collected and reported nothing. A caller-owned progress sink ships the partials and the omission reaches the export report. (Awaiting the aborted collection was rejected: an in-flight source fetch is not abortable — E-4's original disease.) Plus a serving-candidate memo, so a .wrl ref served by its .step fallback stops re-probing the miss on every export. Scheduler E-14 _terminalizeNativeTrap classified by message substring, so any plain JS error QUOTING 'Aborted(' or 'out of bounds' permanently bricked a healthy instance. Now structural only: instanceof RuntimeError plus a duck-typed name check (verified in this build's glue that abort() throws a genuine RuntimeError both pre- and post-runtime-init). Module.onAbort now latches the gate — the authoritative notification, previously ignored. E-15 the shim half: _pumpResume gates on terminal (catching wakes already queued at latch time) and resolveWait refuses on terminal WITHOUT consuming the entry, so a frame stays visibly parked rather than resuming inside a trapped module. E-16 the E-5 handler read the realm-global scheduler at dispatch instead of its installing module's; also frees the per-line buffer on the non-trap rethrow path. E-11 get_vec trusted the worker's res.length over the transferred arrays. Observed death shape: a 4 GiB std::vector threw an unhandled std::length_error that exited the editor's main loop. Now clamped, with the buffers freed on every failure path. Guardrails (replacing two deferred refactors: e2e→production-code injection and collapsing the four copies of the worker-lifecycle machinery) E-18 the source contract asserted comment-string counts — rewording failed CI while moving a guard outside its #ifdef passed. It now parses the #ifdef regions and asserts on code. service-stub-parity.ts pins what the four lifecycle copies must share: credit-window equality parsed from source, the finally-ack, boot deadlines, terminal-notice consumption. The transport numbers are now single-sourced from the worker. CI actually runs the gates: the web/standalone vitest suites (which had NEVER run in CI), the reducer, the source contract and the parity tool — with a NON_PLAYWRIGHT_GATES check so deleting a step re-fails the lint. E-22 the e2e occ stub's 60s boot watchdog, deleted in a66e109, is restored in the ngspice-stub shape with a wedgeNextBoot() repro hook. Every behavioral fix has red-then-green evidence (the reds were captured first). E-17 (a stale RUNNING cross-stamping the next run's generation under E-6's transport deferral) is DEFERRED with its analysis recorded — a real fix needs run identity on the bg frames. Test hygiene: the dwell lint now requires the mandated ": <why>" and all 47 bare markers carry their reason; three export-report dwells became modal-lease polls; exact-ledger assertions became relative deltas; the dead data-wx-dom-id branch, an unused fault hook and unused receipt plumbing are gone; abort scans, wx dialog drivers, the sim harness and the vitest FakeWorker are each one copy now. Bumps kicad and wxwidgets to their findings-group-e tips. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-27 12:27:12 +02:00
): Promise<RequestSummary> => requestReceipts.wait(after, criteria, timeoutMs);
findings(E-1,E-2,E-3,E-6): ngspice service generations + credit-bounded event transport Adapted from codex/asyncify-execution-owner-core 3753320. Service side mirrors the occ-service shape (E-1 watchdogs, E-2 fail-all + boot-death fix — onerror now rejects the in-flight boot waiter instead of stranding it, E-3 onmessageerror terminal, Blob URL revoked, per-generation evtQueue cleared on retirement). E-6 transport bounds (worker hunks re-applied inside the emscripten-6 em-pthread else-branch — the codex file predates that split, so this is a re-application, not a cherry-pick): - batch cut at 512 lines / 1 MiB exact JSON-UTF-8 bytes, measured before a line is retained; a single line > 1 MiB flushes the accepted prefix then stops the event stream terminally (never retained); - posting gated by a 64-frame / 8 MiB unacked credit window; each frame carries { eventSequence, eventBytes } and is released only by an exact { sequence, bytes } ack; any mismatched ack is terminal; - the service mirrors the same 64-frame / 8 MiB bound on its pre-handler queue, acks after handing a frame to __ngspiceOnEvent, and retires the generation on invalid credit; { fatal } frames retire the worker. Tests: ngspice-service.test.ts (11, ported) — watchdogs, crash/bootError/ decode-fault settlement + recovery, out-of-order ids, sync postMessage throw, stale-generation event drops, fatal-frame retirement. tests/tools/ ngspice-worker-batch-unit.ts (node:vm over the production worker source; `npm run ngspice:worker-batch`) — bounded ordered chunks, byte-pressure flush, 100k-chunk credit storm, over-limit line, exact ack lease. e2e harness twin updated to speak the ack protocol (adds __ngspiceServiceTestHooks). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-19 15:48:41 +02:00
const waitForAppliedGenerationAfter = (
after: number,
timeoutMs = RECEIPT_TIMEOUT_MS,
findings(E-10..E-22): fix the defects a code review found in the E-1..E-9 work A review of the group-E fixes found 13 further defects; ten were introduced by those fixes, two pre-existed and were merely relocated, one is deferred. Services / transport E-10 retireWorker synthesized no bg/exit frame, so sharedspice's s_bgRunning mirror stayed latched true after a mid-run worker death: Run stayed disabled and the promised fresh-worker restart was unreachable for the whole session. Retirement now dispatches a synthetic controlled-exit straight to the installed handler (never through dispatchEvt — a fabricated frame must not touch the credit ledger). Driving the repro exposed two further defects, both fixed here: a replacement worker trapped on pre-init engine reads, and the rerun's cm_input_path/circ hit that uninitialized engine before KiCad's validate() re-init (the native flow assumes a crashed engine survives in-process — true for the dll, false for a dead worker). Reads now answer their empty shapes pre-init, writes lazy-init, and init is idempotent per worker engine. E-19 dispatchEvt acked only AFTER handler(evt) returned, and the sharedspice client deliberately rethrows non-trap errors — so each throw leaked one unit of the 64-frame credit window until the stream died with a misattributed "transport exceeded". The ack moves to a finally in both service copies; the throw still propagates (the trap machinery needs it). E-20 the oversize-line path promises to transfer the accepted prefix, but with the window full that flush only DEFERS, and stopEventStream wiped the deferred queue — losing the diagnostics that explain the failure. The terminal notice now carries them as pendingEvents; both hosts deliver them in order, unacked (the fatal frame is outside the credit protocol). E-21 the 30s prefetch deadline discarded every model already collected and reported nothing. A caller-owned progress sink ships the partials and the omission reaches the export report. (Awaiting the aborted collection was rejected: an in-flight source fetch is not abortable — E-4's original disease.) Plus a serving-candidate memo, so a .wrl ref served by its .step fallback stops re-probing the miss on every export. Scheduler E-14 _terminalizeNativeTrap classified by message substring, so any plain JS error QUOTING 'Aborted(' or 'out of bounds' permanently bricked a healthy instance. Now structural only: instanceof RuntimeError plus a duck-typed name check (verified in this build's glue that abort() throws a genuine RuntimeError both pre- and post-runtime-init). Module.onAbort now latches the gate — the authoritative notification, previously ignored. E-15 the shim half: _pumpResume gates on terminal (catching wakes already queued at latch time) and resolveWait refuses on terminal WITHOUT consuming the entry, so a frame stays visibly parked rather than resuming inside a trapped module. E-16 the E-5 handler read the realm-global scheduler at dispatch instead of its installing module's; also frees the per-line buffer on the non-trap rethrow path. E-11 get_vec trusted the worker's res.length over the transferred arrays. Observed death shape: a 4 GiB std::vector threw an unhandled std::length_error that exited the editor's main loop. Now clamped, with the buffers freed on every failure path. Guardrails (replacing two deferred refactors: e2e→production-code injection and collapsing the four copies of the worker-lifecycle machinery) E-18 the source contract asserted comment-string counts — rewording failed CI while moving a guard outside its #ifdef passed. It now parses the #ifdef regions and asserts on code. service-stub-parity.ts pins what the four lifecycle copies must share: credit-window equality parsed from source, the finally-ack, boot deadlines, terminal-notice consumption. The transport numbers are now single-sourced from the worker. CI actually runs the gates: the web/standalone vitest suites (which had NEVER run in CI), the reducer, the source contract and the parity tool — with a NON_PLAYWRIGHT_GATES check so deleting a step re-fails the lint. E-22 the e2e occ stub's 60s boot watchdog, deleted in a66e109, is restored in the ngspice-stub shape with a wedgeNextBoot() repro hook. Every behavioral fix has red-then-green evidence (the reds were captured first). E-17 (a stale RUNNING cross-stamping the next run's generation under E-6's transport deferral) is DEFERRED with its analysis recorded — a real fix needs run identity on the bg frames. Test hygiene: the dwell lint now requires the mandated ": <why>" and all 47 bare markers carry their reason; three export-report dwells became modal-lease polls; exact-ledger assertions became relative deltas; the dead data-wx-dom-id branch, an unused fault hook and unused receipt plumbing are gone; abort scans, wx dialog drivers, the sim harness and the vitest FakeWorker are each one copy now. Bumps kicad and wxwidgets to their findings-group-e tips. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-27 12:27:12 +02:00
): Promise<AppliedGenerationReceipt> => appliedReceipts.wait(after, undefined, timeoutMs);
findings(E-1,E-2,E-3,E-6): ngspice service generations + credit-bounded event transport Adapted from codex/asyncify-execution-owner-core 3753320. Service side mirrors the occ-service shape (E-1 watchdogs, E-2 fail-all + boot-death fix — onerror now rejects the in-flight boot waiter instead of stranding it, E-3 onmessageerror terminal, Blob URL revoked, per-generation evtQueue cleared on retirement). E-6 transport bounds (worker hunks re-applied inside the emscripten-6 em-pthread else-branch — the codex file predates that split, so this is a re-application, not a cherry-pick): - batch cut at 512 lines / 1 MiB exact JSON-UTF-8 bytes, measured before a line is retained; a single line > 1 MiB flushes the accepted prefix then stops the event stream terminally (never retained); - posting gated by a 64-frame / 8 MiB unacked credit window; each frame carries { eventSequence, eventBytes } and is released only by an exact { sequence, bytes } ack; any mismatched ack is terminal; - the service mirrors the same 64-frame / 8 MiB bound on its pre-handler queue, acks after handing a frame to __ngspiceOnEvent, and retires the generation on invalid credit; { fatal } frames retire the worker. Tests: ngspice-service.test.ts (11, ported) — watchdogs, crash/bootError/ decode-fault settlement + recovery, out-of-order ids, sync postMessage throw, stale-generation event drops, fatal-frame retirement. tests/tools/ ngspice-worker-batch-unit.ts (node:vm over the production worker source; `npm run ngspice:worker-batch`) — bounded ordered chunks, byte-pressure flush, 100k-chunk credit storm, over-limit line, exact ack lease. e2e harness twin updated to speak the ack protocol (adds __ngspiceServiceTestHooks). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-19 15:48:41 +02:00
const previousAppliedHook = (globalThis as any).__pcbjamNgspiceFinalRefreshApplied;
const publishAppliedGeneration = (generation: number): void => {
if (disposed) return;
if (!Number.isSafeInteger(generation) || generation < 1) {
console.error(`[TEST-NGSPICE] ignored invalid applied generation ${generation}`);
return;
}
const previousGeneration = appliedGenerations.length
? appliedGenerations[appliedGenerations.length - 1]!.generation
: 0;
if (generation <= previousGeneration) {
console.error(`[TEST-NGSPICE] ignored stale applied generation ${generation}`);
return;
}
const receipt = { generation, t: Date.now() - t0 };
appliedGenerations.push(receipt);
findings(E-10..E-22): fix the defects a code review found in the E-1..E-9 work A review of the group-E fixes found 13 further defects; ten were introduced by those fixes, two pre-existed and were merely relocated, one is deferred. Services / transport E-10 retireWorker synthesized no bg/exit frame, so sharedspice's s_bgRunning mirror stayed latched true after a mid-run worker death: Run stayed disabled and the promised fresh-worker restart was unreachable for the whole session. Retirement now dispatches a synthetic controlled-exit straight to the installed handler (never through dispatchEvt — a fabricated frame must not touch the credit ledger). Driving the repro exposed two further defects, both fixed here: a replacement worker trapped on pre-init engine reads, and the rerun's cm_input_path/circ hit that uninitialized engine before KiCad's validate() re-init (the native flow assumes a crashed engine survives in-process — true for the dll, false for a dead worker). Reads now answer their empty shapes pre-init, writes lazy-init, and init is idempotent per worker engine. E-19 dispatchEvt acked only AFTER handler(evt) returned, and the sharedspice client deliberately rethrows non-trap errors — so each throw leaked one unit of the 64-frame credit window until the stream died with a misattributed "transport exceeded". The ack moves to a finally in both service copies; the throw still propagates (the trap machinery needs it). E-20 the oversize-line path promises to transfer the accepted prefix, but with the window full that flush only DEFERS, and stopEventStream wiped the deferred queue — losing the diagnostics that explain the failure. The terminal notice now carries them as pendingEvents; both hosts deliver them in order, unacked (the fatal frame is outside the credit protocol). E-21 the 30s prefetch deadline discarded every model already collected and reported nothing. A caller-owned progress sink ships the partials and the omission reaches the export report. (Awaiting the aborted collection was rejected: an in-flight source fetch is not abortable — E-4's original disease.) Plus a serving-candidate memo, so a .wrl ref served by its .step fallback stops re-probing the miss on every export. Scheduler E-14 _terminalizeNativeTrap classified by message substring, so any plain JS error QUOTING 'Aborted(' or 'out of bounds' permanently bricked a healthy instance. Now structural only: instanceof RuntimeError plus a duck-typed name check (verified in this build's glue that abort() throws a genuine RuntimeError both pre- and post-runtime-init). Module.onAbort now latches the gate — the authoritative notification, previously ignored. E-15 the shim half: _pumpResume gates on terminal (catching wakes already queued at latch time) and resolveWait refuses on terminal WITHOUT consuming the entry, so a frame stays visibly parked rather than resuming inside a trapped module. E-16 the E-5 handler read the realm-global scheduler at dispatch instead of its installing module's; also frees the per-line buffer on the non-trap rethrow path. E-11 get_vec trusted the worker's res.length over the transferred arrays. Observed death shape: a 4 GiB std::vector threw an unhandled std::length_error that exited the editor's main loop. Now clamped, with the buffers freed on every failure path. Guardrails (replacing two deferred refactors: e2e→production-code injection and collapsing the four copies of the worker-lifecycle machinery) E-18 the source contract asserted comment-string counts — rewording failed CI while moving a guard outside its #ifdef passed. It now parses the #ifdef regions and asserts on code. service-stub-parity.ts pins what the four lifecycle copies must share: credit-window equality parsed from source, the finally-ack, boot deadlines, terminal-notice consumption. The transport numbers are now single-sourced from the worker. CI actually runs the gates: the web/standalone vitest suites (which had NEVER run in CI), the reducer, the source contract and the parity tool — with a NON_PLAYWRIGHT_GATES check so deleting a step re-fails the lint. E-22 the e2e occ stub's 60s boot watchdog, deleted in a66e109, is restored in the ngspice-stub shape with a wedgeNextBoot() repro hook. Every behavioral fix has red-then-green evidence (the reds were captured first). E-17 (a stale RUNNING cross-stamping the next run's generation under E-6's transport deferral) is DEFERRED with its analysis recorded — a real fix needs run identity on the bg frames. Test hygiene: the dwell lint now requires the mandated ": <why>" and all 47 bare markers carry their reason; three export-report dwells became modal-lease polls; exact-ledger assertions became relative deltas; the dead data-wx-dom-id branch, an unused fault hook and unused receipt plumbing are gone; abort scans, wx dialog drivers, the sim harness and the vitest FakeWorker are each one copy now. Bumps kicad and wxwidgets to their findings-group-e tips. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-27 12:27:12 +02:00
appliedReceipts.publish(receipt);
findings(E-1,E-2,E-3,E-6): ngspice service generations + credit-bounded event transport Adapted from codex/asyncify-execution-owner-core 3753320. Service side mirrors the occ-service shape (E-1 watchdogs, E-2 fail-all + boot-death fix — onerror now rejects the in-flight boot waiter instead of stranding it, E-3 onmessageerror terminal, Blob URL revoked, per-generation evtQueue cleared on retirement). E-6 transport bounds (worker hunks re-applied inside the emscripten-6 em-pthread else-branch — the codex file predates that split, so this is a re-application, not a cherry-pick): - batch cut at 512 lines / 1 MiB exact JSON-UTF-8 bytes, measured before a line is retained; a single line > 1 MiB flushes the accepted prefix then stops the event stream terminally (never retained); - posting gated by a 64-frame / 8 MiB unacked credit window; each frame carries { eventSequence, eventBytes } and is released only by an exact { sequence, bytes } ack; any mismatched ack is terminal; - the service mirrors the same 64-frame / 8 MiB bound on its pre-handler queue, acks after handing a frame to __ngspiceOnEvent, and retires the generation on invalid credit; { fatal } frames retire the worker. Tests: ngspice-service.test.ts (11, ported) — watchdogs, crash/bootError/ decode-fault settlement + recovery, out-of-order ids, sync postMessage throw, stale-generation event drops, fatal-frame retirement. tests/tools/ ngspice-worker-batch-unit.ts (node:vm over the production worker source; `npm run ngspice:worker-batch`) — bounded ordered chunks, byte-pressure flush, 100k-chunk credit storm, over-limit line, exact ack lease. e2e harness twin updated to speak the ack protocol (adds __ngspiceServiceTestHooks). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-19 15:48:41 +02:00
if (typeof previousAppliedHook === 'function') previousAppliedHook(generation);
};
(globalThis as any).__pcbjamNgspiceFinalRefreshApplied = publishAppliedGeneration;
findings(E-1,E-2,E-3,E-6): ngspice service generations + credit-bounded event transport Adapted from codex/asyncify-execution-owner-core 3753320. Service side mirrors the occ-service shape (E-1 watchdogs, E-2 fail-all + boot-death fix — onerror now rejects the in-flight boot waiter instead of stranding it, E-3 onmessageerror terminal, Blob URL revoked, per-generation evtQueue cleared on retirement). E-6 transport bounds (worker hunks re-applied inside the emscripten-6 em-pthread else-branch — the codex file predates that split, so this is a re-application, not a cherry-pick): - batch cut at 512 lines / 1 MiB exact JSON-UTF-8 bytes, measured before a line is retained; a single line > 1 MiB flushes the accepted prefix then stops the event stream terminally (never retained); - posting gated by a 64-frame / 8 MiB unacked credit window; each frame carries { eventSequence, eventBytes } and is released only by an exact { sequence, bytes } ack; any mismatched ack is terminal; - the service mirrors the same 64-frame / 8 MiB bound on its pre-handler queue, acks after handing a frame to __ngspiceOnEvent, and retires the generation on invalid credit; { fatal } frames retire the worker. Tests: ngspice-service.test.ts (11, ported) — watchdogs, crash/bootError/ decode-fault settlement + recovery, out-of-order ids, sync postMessage throw, stale-generation event drops, fatal-frame retirement. tests/tools/ ngspice-worker-batch-unit.ts (node:vm over the production worker source; `npm run ngspice:worker-batch`) — bounded ordered chunks, byte-pressure flush, 100k-chunk credit storm, over-limit line, exact ack lease. e2e harness twin updated to speak the ack protocol (adds __ngspiceServiceTestHooks). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-19 15:48:41 +02:00
interface QueuedEventFrame {
generation: number;
evt: any;
sequence: number;
bytes: number;
}
const MAX_QUEUED_EVENT_FRAMES = 64;
const MAX_QUEUED_EVENT_BYTES = 8 * 1024 * 1024;
const evtQueue: QueuedEventFrame[] = [];
let evtQueueBytes = 0;
const ackEvent = (slot: WorkerSlot, frame: QueuedEventFrame): boolean => {
if (slot.failed || workerSlot !== slot || !slot.worker) return false;
try {
slot.worker.postMessage({
eventAck: { sequence: frame.sequence, bytes: frame.bytes },
});
return true;
} catch (error) {
retireWorker(
slot,
`ngspice_service event acknowledgment failed: ${String(error)}`,
);
return false;
}
};
const dispatchEvt = (
slot: WorkerSlot,
evt: any,
sequence: number,
bytes: number,
) => {
if (slot.failed || workerSlot !== slot) return;
if (!Number.isSafeInteger(sequence) || sequence < 1
|| !Number.isSafeInteger(bytes) || bytes < 1
|| bytes > MAX_QUEUED_EVENT_BYTES) {
retireWorker(slot, 'ngspice_service sent invalid event-frame credit');
return;
}
const frame = { generation: slot.generation, evt, sequence, bytes };
(window as any).__ngspiceEvents.push({ ...evt, t: Date.now() - t0 });
const handler = (globalThis as any).__ngspiceOnEvent;
if (handler) {
findings(E-10..E-22): fix the defects a code review found in the E-1..E-9 work A review of the group-E fixes found 13 further defects; ten were introduced by those fixes, two pre-existed and were merely relocated, one is deferred. Services / transport E-10 retireWorker synthesized no bg/exit frame, so sharedspice's s_bgRunning mirror stayed latched true after a mid-run worker death: Run stayed disabled and the promised fresh-worker restart was unreachable for the whole session. Retirement now dispatches a synthetic controlled-exit straight to the installed handler (never through dispatchEvt — a fabricated frame must not touch the credit ledger). Driving the repro exposed two further defects, both fixed here: a replacement worker trapped on pre-init engine reads, and the rerun's cm_input_path/circ hit that uninitialized engine before KiCad's validate() re-init (the native flow assumes a crashed engine survives in-process — true for the dll, false for a dead worker). Reads now answer their empty shapes pre-init, writes lazy-init, and init is idempotent per worker engine. E-19 dispatchEvt acked only AFTER handler(evt) returned, and the sharedspice client deliberately rethrows non-trap errors — so each throw leaked one unit of the 64-frame credit window until the stream died with a misattributed "transport exceeded". The ack moves to a finally in both service copies; the throw still propagates (the trap machinery needs it). E-20 the oversize-line path promises to transfer the accepted prefix, but with the window full that flush only DEFERS, and stopEventStream wiped the deferred queue — losing the diagnostics that explain the failure. The terminal notice now carries them as pendingEvents; both hosts deliver them in order, unacked (the fatal frame is outside the credit protocol). E-21 the 30s prefetch deadline discarded every model already collected and reported nothing. A caller-owned progress sink ships the partials and the omission reaches the export report. (Awaiting the aborted collection was rejected: an in-flight source fetch is not abortable — E-4's original disease.) Plus a serving-candidate memo, so a .wrl ref served by its .step fallback stops re-probing the miss on every export. Scheduler E-14 _terminalizeNativeTrap classified by message substring, so any plain JS error QUOTING 'Aborted(' or 'out of bounds' permanently bricked a healthy instance. Now structural only: instanceof RuntimeError plus a duck-typed name check (verified in this build's glue that abort() throws a genuine RuntimeError both pre- and post-runtime-init). Module.onAbort now latches the gate — the authoritative notification, previously ignored. E-15 the shim half: _pumpResume gates on terminal (catching wakes already queued at latch time) and resolveWait refuses on terminal WITHOUT consuming the entry, so a frame stays visibly parked rather than resuming inside a trapped module. E-16 the E-5 handler read the realm-global scheduler at dispatch instead of its installing module's; also frees the per-line buffer on the non-trap rethrow path. E-11 get_vec trusted the worker's res.length over the transferred arrays. Observed death shape: a 4 GiB std::vector threw an unhandled std::length_error that exited the editor's main loop. Now clamped, with the buffers freed on every failure path. Guardrails (replacing two deferred refactors: e2e→production-code injection and collapsing the four copies of the worker-lifecycle machinery) E-18 the source contract asserted comment-string counts — rewording failed CI while moving a guard outside its #ifdef passed. It now parses the #ifdef regions and asserts on code. service-stub-parity.ts pins what the four lifecycle copies must share: credit-window equality parsed from source, the finally-ack, boot deadlines, terminal-notice consumption. The transport numbers are now single-sourced from the worker. CI actually runs the gates: the web/standalone vitest suites (which had NEVER run in CI), the reducer, the source contract and the parity tool — with a NON_PLAYWRIGHT_GATES check so deleting a step re-fails the lint. E-22 the e2e occ stub's 60s boot watchdog, deleted in a66e109, is restored in the ngspice-stub shape with a wedgeNextBoot() repro hook. Every behavioral fix has red-then-green evidence (the reds were captured first). E-17 (a stale RUNNING cross-stamping the next run's generation under E-6's transport deferral) is DEFERRED with its analysis recorded — a real fix needs run identity on the bg frames. Test hygiene: the dwell lint now requires the mandated ": <why>" and all 47 bare markers carry their reason; three export-report dwells became modal-lease polls; exact-ledger assertions became relative deltas; the dead data-wx-dom-id branch, an unused fault hook and unused receipt plumbing are gone; abort scans, wx dialog drivers, the sim harness and the vitest FakeWorker are each one copy now. Bumps kicad and wxwidgets to their findings-group-e tips. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-27 12:27:12 +02:00
// Mirrors the production service: the host owns the frame's
// credit from onmessage on, so the ack survives a throwing
// handler (which keeps propagating).
try {
while (evtQueue.length) {
const queued = evtQueue.shift()!;
evtQueueBytes -= queued.bytes;
if (queued.generation !== slot.generation) continue;
// Queued frames were acked at enqueue (ownership taken then).
handler(queued.evt);
}
handler(evt);
} finally {
ackEvent(slot, frame);
findings(E-1,E-2,E-3,E-6): ngspice service generations + credit-bounded event transport Adapted from codex/asyncify-execution-owner-core 3753320. Service side mirrors the occ-service shape (E-1 watchdogs, E-2 fail-all + boot-death fix — onerror now rejects the in-flight boot waiter instead of stranding it, E-3 onmessageerror terminal, Blob URL revoked, per-generation evtQueue cleared on retirement). E-6 transport bounds (worker hunks re-applied inside the emscripten-6 em-pthread else-branch — the codex file predates that split, so this is a re-application, not a cherry-pick): - batch cut at 512 lines / 1 MiB exact JSON-UTF-8 bytes, measured before a line is retained; a single line > 1 MiB flushes the accepted prefix then stops the event stream terminally (never retained); - posting gated by a 64-frame / 8 MiB unacked credit window; each frame carries { eventSequence, eventBytes } and is released only by an exact { sequence, bytes } ack; any mismatched ack is terminal; - the service mirrors the same 64-frame / 8 MiB bound on its pre-handler queue, acks after handing a frame to __ngspiceOnEvent, and retires the generation on invalid credit; { fatal } frames retire the worker. Tests: ngspice-service.test.ts (11, ported) — watchdogs, crash/bootError/ decode-fault settlement + recovery, out-of-order ids, sync postMessage throw, stale-generation event drops, fatal-frame retirement. tests/tools/ ngspice-worker-batch-unit.ts (node:vm over the production worker source; `npm run ngspice:worker-batch`) — bounded ordered chunks, byte-pressure flush, 100k-chunk credit storm, over-limit line, exact ack lease. e2e harness twin updated to speak the ack protocol (adds __ngspiceServiceTestHooks). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-19 15:48:41 +02:00
}
} else {
findings(E-1,E-2,E-3,E-6): ngspice service generations + credit-bounded event transport Adapted from codex/asyncify-execution-owner-core 3753320. Service side mirrors the occ-service shape (E-1 watchdogs, E-2 fail-all + boot-death fix — onerror now rejects the in-flight boot waiter instead of stranding it, E-3 onmessageerror terminal, Blob URL revoked, per-generation evtQueue cleared on retirement). E-6 transport bounds (worker hunks re-applied inside the emscripten-6 em-pthread else-branch — the codex file predates that split, so this is a re-application, not a cherry-pick): - batch cut at 512 lines / 1 MiB exact JSON-UTF-8 bytes, measured before a line is retained; a single line > 1 MiB flushes the accepted prefix then stops the event stream terminally (never retained); - posting gated by a 64-frame / 8 MiB unacked credit window; each frame carries { eventSequence, eventBytes } and is released only by an exact { sequence, bytes } ack; any mismatched ack is terminal; - the service mirrors the same 64-frame / 8 MiB bound on its pre-handler queue, acks after handing a frame to __ngspiceOnEvent, and retires the generation on invalid credit; { fatal } frames retire the worker. Tests: ngspice-service.test.ts (11, ported) — watchdogs, crash/bootError/ decode-fault settlement + recovery, out-of-order ids, sync postMessage throw, stale-generation event drops, fatal-frame retirement. tests/tools/ ngspice-worker-batch-unit.ts (node:vm over the production worker source; `npm run ngspice:worker-batch`) — bounded ordered chunks, byte-pressure flush, 100k-chunk credit storm, over-limit line, exact ack lease. e2e harness twin updated to speak the ack protocol (adds __ngspiceServiceTestHooks). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-19 15:48:41 +02:00
if (evtQueue.length >= MAX_QUEUED_EVENT_FRAMES
|| evtQueueBytes > MAX_QUEUED_EVENT_BYTES - bytes) {
retireWorker(slot, 'ngspice_service event-frame queue exceeded credit');
return;
}
evtQueue.push(frame);
evtQueueBytes += bytes;
findings(E-5,E-6): validation-round fixes — live e2e falsified two ported shapes E-5: the module-identity bridge called installingModule._malloc, but this build exposes _malloc only as a bare glue-closure export (Module._malloc is absent) — every char/stat event entry threw TypeError, which also starved the E-6 credit window (thrown dispatches never acked) and wedged the queued bg-finished frame behind them. The bridge now uses the bare closure exports (identity is still exact: the EM_JS body IS the installing module's closure; the __ngspiceOnEvent self-disarm covers supersession). E-6 (codex reference design corrected — its validation matrix never ran): a FULL credit window was terminal (stopEventStream at 64 in-flight frames). Under live e2e that killed a real simulation: bg-thread emissions proxy one per task, so each line ships as its own frame and a normal transient outruns a busy main thread. A full window now DEFERS into a bounded FIFO (512 events / 4 MiB) drained in order as acks free credit; only true overload or an invalid ack is terminal. Retention stays bounded (8 MiB in flight + 4 MiB deferred + 1 MiB open batch). And the service/harness mirror queue now acks at ENQUEUE — placing a frame in the bounded pre-handler queue is taking ownership; without that, a stream starting before the C++ handler installs (the ngspice-probe page) starves the worker window forever. Test updates: worker-batch reducer — new "a full credit window defers and drains in order, never terminal" case pinning the regression; the storm case now proves the deferred caps are the terminal edge. board-ready.ts gains the owner-free openBoardProgrammatically (codex helper the ported occ-export spec needs; the barrier-based waitForUiBoardReady was NOT taken). occ-export.spec: domId is optional on this line's registry (coordinate fallback is the supported path). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-19 17:53:11 +02:00
// Enqueueing IS taking ownership (mirrors the production
// service): release the transport credit so a pre-handler
// stream cannot starve the worker's window.
ackEvent(slot, frame);
findings(E-1,E-2,E-3,E-6): ngspice service generations + credit-bounded event transport Adapted from codex/asyncify-execution-owner-core 3753320. Service side mirrors the occ-service shape (E-1 watchdogs, E-2 fail-all + boot-death fix — onerror now rejects the in-flight boot waiter instead of stranding it, E-3 onmessageerror terminal, Blob URL revoked, per-generation evtQueue cleared on retirement). E-6 transport bounds (worker hunks re-applied inside the emscripten-6 em-pthread else-branch — the codex file predates that split, so this is a re-application, not a cherry-pick): - batch cut at 512 lines / 1 MiB exact JSON-UTF-8 bytes, measured before a line is retained; a single line > 1 MiB flushes the accepted prefix then stops the event stream terminally (never retained); - posting gated by a 64-frame / 8 MiB unacked credit window; each frame carries { eventSequence, eventBytes } and is released only by an exact { sequence, bytes } ack; any mismatched ack is terminal; - the service mirrors the same 64-frame / 8 MiB bound on its pre-handler queue, acks after handing a frame to __ngspiceOnEvent, and retires the generation on invalid credit; { fatal } frames retire the worker. Tests: ngspice-service.test.ts (11, ported) — watchdogs, crash/bootError/ decode-fault settlement + recovery, out-of-order ids, sync postMessage throw, stale-generation event drops, fatal-frame retirement. tests/tools/ ngspice-worker-batch-unit.ts (node:vm over the production worker source; `npm run ngspice:worker-batch`) — bounded ordered chunks, byte-pressure flush, 100k-chunk credit storm, over-limit line, exact ack lease. e2e harness twin updated to speak the ack protocol (adds __ngspiceServiceTestHooks). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-19 15:48:41 +02:00
}
};
findings(E-10..E-22): fix the defects a code review found in the E-1..E-9 work A review of the group-E fixes found 13 further defects; ten were introduced by those fixes, two pre-existed and were merely relocated, one is deferred. Services / transport E-10 retireWorker synthesized no bg/exit frame, so sharedspice's s_bgRunning mirror stayed latched true after a mid-run worker death: Run stayed disabled and the promised fresh-worker restart was unreachable for the whole session. Retirement now dispatches a synthetic controlled-exit straight to the installed handler (never through dispatchEvt — a fabricated frame must not touch the credit ledger). Driving the repro exposed two further defects, both fixed here: a replacement worker trapped on pre-init engine reads, and the rerun's cm_input_path/circ hit that uninitialized engine before KiCad's validate() re-init (the native flow assumes a crashed engine survives in-process — true for the dll, false for a dead worker). Reads now answer their empty shapes pre-init, writes lazy-init, and init is idempotent per worker engine. E-19 dispatchEvt acked only AFTER handler(evt) returned, and the sharedspice client deliberately rethrows non-trap errors — so each throw leaked one unit of the 64-frame credit window until the stream died with a misattributed "transport exceeded". The ack moves to a finally in both service copies; the throw still propagates (the trap machinery needs it). E-20 the oversize-line path promises to transfer the accepted prefix, but with the window full that flush only DEFERS, and stopEventStream wiped the deferred queue — losing the diagnostics that explain the failure. The terminal notice now carries them as pendingEvents; both hosts deliver them in order, unacked (the fatal frame is outside the credit protocol). E-21 the 30s prefetch deadline discarded every model already collected and reported nothing. A caller-owned progress sink ships the partials and the omission reaches the export report. (Awaiting the aborted collection was rejected: an in-flight source fetch is not abortable — E-4's original disease.) Plus a serving-candidate memo, so a .wrl ref served by its .step fallback stops re-probing the miss on every export. Scheduler E-14 _terminalizeNativeTrap classified by message substring, so any plain JS error QUOTING 'Aborted(' or 'out of bounds' permanently bricked a healthy instance. Now structural only: instanceof RuntimeError plus a duck-typed name check (verified in this build's glue that abort() throws a genuine RuntimeError both pre- and post-runtime-init). Module.onAbort now latches the gate — the authoritative notification, previously ignored. E-15 the shim half: _pumpResume gates on terminal (catching wakes already queued at latch time) and resolveWait refuses on terminal WITHOUT consuming the entry, so a frame stays visibly parked rather than resuming inside a trapped module. E-16 the E-5 handler read the realm-global scheduler at dispatch instead of its installing module's; also frees the per-line buffer on the non-trap rethrow path. E-11 get_vec trusted the worker's res.length over the transferred arrays. Observed death shape: a 4 GiB std::vector threw an unhandled std::length_error that exited the editor's main loop. Now clamped, with the buffers freed on every failure path. Guardrails (replacing two deferred refactors: e2e→production-code injection and collapsing the four copies of the worker-lifecycle machinery) E-18 the source contract asserted comment-string counts — rewording failed CI while moving a guard outside its #ifdef passed. It now parses the #ifdef regions and asserts on code. service-stub-parity.ts pins what the four lifecycle copies must share: credit-window equality parsed from source, the finally-ack, boot deadlines, terminal-notice consumption. The transport numbers are now single-sourced from the worker. CI actually runs the gates: the web/standalone vitest suites (which had NEVER run in CI), the reducer, the source contract and the parity tool — with a NON_PLAYWRIGHT_GATES check so deleting a step re-fails the lint. E-22 the e2e occ stub's 60s boot watchdog, deleted in a66e109, is restored in the ngspice-stub shape with a wedgeNextBoot() repro hook. Every behavioral fix has red-then-green evidence (the reds were captured first). E-17 (a stale RUNNING cross-stamping the next run's generation under E-6's transport deferral) is DEFERRED with its analysis recorded — a real fix needs run identity on the bg frames. Test hygiene: the dwell lint now requires the mandated ": <why>" and all 47 bare markers carry their reason; three export-report dwells became modal-lease polls; exact-ledger assertions became relative deltas; the dead data-wx-dom-id branch, an unused fault hook and unused receipt plumbing are gone; abort scans, wx dialog drivers, the sim harness and the vitest FakeWorker are each one copy now. Bumps kicad and wxwidgets to their findings-group-e tips. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-27 12:27:12 +02:00
// Mirrors the production service: the worker's terminal notice carries
// the deferred frames it had already accepted — deliver best-effort,
// in order, WITHOUT acking (the fatal frame is outside the credit
// protocol), and record them for the specs like any live frame.
const deliverTerminalEvents = (entries: unknown): void => {
if (!Array.isArray(entries) || entries.length === 0) return;
const handler = (globalThis as any).__ngspiceOnEvent;
for (const entry of entries) {
const evt = (entry as { evt?: any } | null)?.evt;
if (!evt) continue;
(window as any).__ngspiceEvents.push({ ...evt, t: Date.now() - t0 });
if (!handler) continue;
try {
handler(evt);
} catch (error) {
console.log(`[TEST-NGSPICE] terminal event delivery failed: ${String(error)}`);
}
}
};
findings(E-1,E-2,E-3,E-6): ngspice service generations + credit-bounded event transport Adapted from codex/asyncify-execution-owner-core 3753320. Service side mirrors the occ-service shape (E-1 watchdogs, E-2 fail-all + boot-death fix — onerror now rejects the in-flight boot waiter instead of stranding it, E-3 onmessageerror terminal, Blob URL revoked, per-generation evtQueue cleared on retirement). E-6 transport bounds (worker hunks re-applied inside the emscripten-6 em-pthread else-branch — the codex file predates that split, so this is a re-application, not a cherry-pick): - batch cut at 512 lines / 1 MiB exact JSON-UTF-8 bytes, measured before a line is retained; a single line > 1 MiB flushes the accepted prefix then stops the event stream terminally (never retained); - posting gated by a 64-frame / 8 MiB unacked credit window; each frame carries { eventSequence, eventBytes } and is released only by an exact { sequence, bytes } ack; any mismatched ack is terminal; - the service mirrors the same 64-frame / 8 MiB bound on its pre-handler queue, acks after handing a frame to __ngspiceOnEvent, and retires the generation on invalid credit; { fatal } frames retire the worker. Tests: ngspice-service.test.ts (11, ported) — watchdogs, crash/bootError/ decode-fault settlement + recovery, out-of-order ids, sync postMessage throw, stale-generation event drops, fatal-frame retirement. tests/tools/ ngspice-worker-batch-unit.ts (node:vm over the production worker source; `npm run ngspice:worker-batch`) — bounded ordered chunks, byte-pressure flush, 100k-chunk credit storm, over-limit line, exact ack lease. e2e harness twin updated to speak the ack protocol (adds __ngspiceServiceTestHooks). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-19 15:48:41 +02:00
const failPending = (generation: number, why: string) => {
for (const [id, request] of pending) {
if (request.generation !== generation) continue;
pending.delete(id);
clearTimeout(request.timer);
request.resolve({ error: why });
}
};
findings(E-1,E-2,E-3,E-6): ngspice service generations + credit-bounded event transport Adapted from codex/asyncify-execution-owner-core 3753320. Service side mirrors the occ-service shape (E-1 watchdogs, E-2 fail-all + boot-death fix — onerror now rejects the in-flight boot waiter instead of stranding it, E-3 onmessageerror terminal, Blob URL revoked, per-generation evtQueue cleared on retirement). E-6 transport bounds (worker hunks re-applied inside the emscripten-6 em-pthread else-branch — the codex file predates that split, so this is a re-application, not a cherry-pick): - batch cut at 512 lines / 1 MiB exact JSON-UTF-8 bytes, measured before a line is retained; a single line > 1 MiB flushes the accepted prefix then stops the event stream terminally (never retained); - posting gated by a 64-frame / 8 MiB unacked credit window; each frame carries { eventSequence, eventBytes } and is released only by an exact { sequence, bytes } ack; any mismatched ack is terminal; - the service mirrors the same 64-frame / 8 MiB bound on its pre-handler queue, acks after handing a frame to __ngspiceOnEvent, and retires the generation on invalid credit; { fatal } frames retire the worker. Tests: ngspice-service.test.ts (11, ported) — watchdogs, crash/bootError/ decode-fault settlement + recovery, out-of-order ids, sync postMessage throw, stale-generation event drops, fatal-frame retirement. tests/tools/ ngspice-worker-batch-unit.ts (node:vm over the production worker source; `npm run ngspice:worker-batch`) — bounded ordered chunks, byte-pressure flush, 100k-chunk credit storm, over-limit line, exact ack lease. e2e harness twin updated to speak the ack protocol (adds __ngspiceServiceTestHooks). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-19 15:48:41 +02:00
const retireWorker = (slot: WorkerSlot, why: string) => {
if (slot.failed) return;
slot.failed = true;
findings(E-10..E-22): fix the defects a code review found in the E-1..E-9 work A review of the group-E fixes found 13 further defects; ten were introduced by those fixes, two pre-existed and were merely relocated, one is deferred. Services / transport E-10 retireWorker synthesized no bg/exit frame, so sharedspice's s_bgRunning mirror stayed latched true after a mid-run worker death: Run stayed disabled and the promised fresh-worker restart was unreachable for the whole session. Retirement now dispatches a synthetic controlled-exit straight to the installed handler (never through dispatchEvt — a fabricated frame must not touch the credit ledger). Driving the repro exposed two further defects, both fixed here: a replacement worker trapped on pre-init engine reads, and the rerun's cm_input_path/circ hit that uninitialized engine before KiCad's validate() re-init (the native flow assumes a crashed engine survives in-process — true for the dll, false for a dead worker). Reads now answer their empty shapes pre-init, writes lazy-init, and init is idempotent per worker engine. E-19 dispatchEvt acked only AFTER handler(evt) returned, and the sharedspice client deliberately rethrows non-trap errors — so each throw leaked one unit of the 64-frame credit window until the stream died with a misattributed "transport exceeded". The ack moves to a finally in both service copies; the throw still propagates (the trap machinery needs it). E-20 the oversize-line path promises to transfer the accepted prefix, but with the window full that flush only DEFERS, and stopEventStream wiped the deferred queue — losing the diagnostics that explain the failure. The terminal notice now carries them as pendingEvents; both hosts deliver them in order, unacked (the fatal frame is outside the credit protocol). E-21 the 30s prefetch deadline discarded every model already collected and reported nothing. A caller-owned progress sink ships the partials and the omission reaches the export report. (Awaiting the aborted collection was rejected: an in-flight source fetch is not abortable — E-4's original disease.) Plus a serving-candidate memo, so a .wrl ref served by its .step fallback stops re-probing the miss on every export. Scheduler E-14 _terminalizeNativeTrap classified by message substring, so any plain JS error QUOTING 'Aborted(' or 'out of bounds' permanently bricked a healthy instance. Now structural only: instanceof RuntimeError plus a duck-typed name check (verified in this build's glue that abort() throws a genuine RuntimeError both pre- and post-runtime-init). Module.onAbort now latches the gate — the authoritative notification, previously ignored. E-15 the shim half: _pumpResume gates on terminal (catching wakes already queued at latch time) and resolveWait refuses on terminal WITHOUT consuming the entry, so a frame stays visibly parked rather than resuming inside a trapped module. E-16 the E-5 handler read the realm-global scheduler at dispatch instead of its installing module's; also frees the per-line buffer on the non-trap rethrow path. E-11 get_vec trusted the worker's res.length over the transferred arrays. Observed death shape: a 4 GiB std::vector threw an unhandled std::length_error that exited the editor's main loop. Now clamped, with the buffers freed on every failure path. Guardrails (replacing two deferred refactors: e2e→production-code injection and collapsing the four copies of the worker-lifecycle machinery) E-18 the source contract asserted comment-string counts — rewording failed CI while moving a guard outside its #ifdef passed. It now parses the #ifdef regions and asserts on code. service-stub-parity.ts pins what the four lifecycle copies must share: credit-window equality parsed from source, the finally-ack, boot deadlines, terminal-notice consumption. The transport numbers are now single-sourced from the worker. CI actually runs the gates: the web/standalone vitest suites (which had NEVER run in CI), the reducer, the source contract and the parity tool — with a NON_PLAYWRIGHT_GATES check so deleting a step re-fails the lint. E-22 the e2e occ stub's 60s boot watchdog, deleted in a66e109, is restored in the ngspice-stub shape with a wedgeNextBoot() repro hook. Every behavioral fix has red-then-green evidence (the reds were captured first). E-17 (a stale RUNNING cross-stamping the next run's generation under E-6's transport deferral) is DEFERRED with its analysis recorded — a real fix needs run identity on the bg frames. Test hygiene: the dwell lint now requires the mandated ": <why>" and all 47 bare markers carry their reason; three export-report dwells became modal-lease polls; exact-ledger assertions became relative deltas; the dead data-wx-dom-id branch, an unused fault hook and unused receipt plumbing are gone; abort scans, wx dialog drivers, the sim harness and the vitest FakeWorker are each one copy now. Bumps kicad and wxwidgets to their findings-group-e tips. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-27 12:27:12 +02:00
console.log(`[TEST-NGSPICE] retiring generation ${slot.generation}: ${why}`);
findings(E-1,E-2,E-3,E-6): ngspice service generations + credit-bounded event transport Adapted from codex/asyncify-execution-owner-core 3753320. Service side mirrors the occ-service shape (E-1 watchdogs, E-2 fail-all + boot-death fix — onerror now rejects the in-flight boot waiter instead of stranding it, E-3 onmessageerror terminal, Blob URL revoked, per-generation evtQueue cleared on retirement). E-6 transport bounds (worker hunks re-applied inside the emscripten-6 em-pthread else-branch — the codex file predates that split, so this is a re-application, not a cherry-pick): - batch cut at 512 lines / 1 MiB exact JSON-UTF-8 bytes, measured before a line is retained; a single line > 1 MiB flushes the accepted prefix then stops the event stream terminally (never retained); - posting gated by a 64-frame / 8 MiB unacked credit window; each frame carries { eventSequence, eventBytes } and is released only by an exact { sequence, bytes } ack; any mismatched ack is terminal; - the service mirrors the same 64-frame / 8 MiB bound on its pre-handler queue, acks after handing a frame to __ngspiceOnEvent, and retires the generation on invalid credit; { fatal } frames retire the worker. Tests: ngspice-service.test.ts (11, ported) — watchdogs, crash/bootError/ decode-fault settlement + recovery, out-of-order ids, sync postMessage throw, stale-generation event drops, fatal-frame retirement. tests/tools/ ngspice-worker-batch-unit.ts (node:vm over the production worker source; `npm run ngspice:worker-batch`) — bounded ordered chunks, byte-pressure flush, 100k-chunk credit storm, over-limit line, exact ack lease. e2e harness twin updated to speak the ack protocol (adds __ngspiceServiceTestHooks). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-19 15:48:41 +02:00
retiredGenerations.push(slot.generation);
if (slot.bootTimer !== undefined) {
clearTimeout(slot.bootTimer);
slot.bootTimer = undefined;
}
slot.removeBootListener?.();
slot.removeBootListener = undefined;
failPending(slot.generation, why);
for (let i = evtQueue.length - 1; i >= 0; --i) {
if (evtQueue[i]!.generation === slot.generation) {
evtQueueBytes -= evtQueue[i]!.bytes;
evtQueue.splice(i, 1);
}
}
if (workerSlot === slot) workerSlot = null;
try { slot.worker?.terminate(); } catch { /* already gone */ }
if (slot.workerUrl) {
try { URL.revokeObjectURL(slot.workerUrl); } catch { /* cleanup only */ }
slot.workerUrl = undefined;
}
const reject = slot.rejectBoot;
slot.rejectBoot = undefined;
reject?.(new Error(why));
findings(E-10..E-22): fix the defects a code review found in the E-1..E-9 work A review of the group-E fixes found 13 further defects; ten were introduced by those fixes, two pre-existed and were merely relocated, one is deferred. Services / transport E-10 retireWorker synthesized no bg/exit frame, so sharedspice's s_bgRunning mirror stayed latched true after a mid-run worker death: Run stayed disabled and the promised fresh-worker restart was unreachable for the whole session. Retirement now dispatches a synthetic controlled-exit straight to the installed handler (never through dispatchEvt — a fabricated frame must not touch the credit ledger). Driving the repro exposed two further defects, both fixed here: a replacement worker trapped on pre-init engine reads, and the rerun's cm_input_path/circ hit that uninitialized engine before KiCad's validate() re-init (the native flow assumes a crashed engine survives in-process — true for the dll, false for a dead worker). Reads now answer their empty shapes pre-init, writes lazy-init, and init is idempotent per worker engine. E-19 dispatchEvt acked only AFTER handler(evt) returned, and the sharedspice client deliberately rethrows non-trap errors — so each throw leaked one unit of the 64-frame credit window until the stream died with a misattributed "transport exceeded". The ack moves to a finally in both service copies; the throw still propagates (the trap machinery needs it). E-20 the oversize-line path promises to transfer the accepted prefix, but with the window full that flush only DEFERS, and stopEventStream wiped the deferred queue — losing the diagnostics that explain the failure. The terminal notice now carries them as pendingEvents; both hosts deliver them in order, unacked (the fatal frame is outside the credit protocol). E-21 the 30s prefetch deadline discarded every model already collected and reported nothing. A caller-owned progress sink ships the partials and the omission reaches the export report. (Awaiting the aborted collection was rejected: an in-flight source fetch is not abortable — E-4's original disease.) Plus a serving-candidate memo, so a .wrl ref served by its .step fallback stops re-probing the miss on every export. Scheduler E-14 _terminalizeNativeTrap classified by message substring, so any plain JS error QUOTING 'Aborted(' or 'out of bounds' permanently bricked a healthy instance. Now structural only: instanceof RuntimeError plus a duck-typed name check (verified in this build's glue that abort() throws a genuine RuntimeError both pre- and post-runtime-init). Module.onAbort now latches the gate — the authoritative notification, previously ignored. E-15 the shim half: _pumpResume gates on terminal (catching wakes already queued at latch time) and resolveWait refuses on terminal WITHOUT consuming the entry, so a frame stays visibly parked rather than resuming inside a trapped module. E-16 the E-5 handler read the realm-global scheduler at dispatch instead of its installing module's; also frees the per-line buffer on the non-trap rethrow path. E-11 get_vec trusted the worker's res.length over the transferred arrays. Observed death shape: a 4 GiB std::vector threw an unhandled std::length_error that exited the editor's main loop. Now clamped, with the buffers freed on every failure path. Guardrails (replacing two deferred refactors: e2e→production-code injection and collapsing the four copies of the worker-lifecycle machinery) E-18 the source contract asserted comment-string counts — rewording failed CI while moving a guard outside its #ifdef passed. It now parses the #ifdef regions and asserts on code. service-stub-parity.ts pins what the four lifecycle copies must share: credit-window equality parsed from source, the finally-ack, boot deadlines, terminal-notice consumption. The transport numbers are now single-sourced from the worker. CI actually runs the gates: the web/standalone vitest suites (which had NEVER run in CI), the reducer, the source contract and the parity tool — with a NON_PLAYWRIGHT_GATES check so deleting a step re-fails the lint. E-22 the e2e occ stub's 60s boot watchdog, deleted in a66e109, is restored in the ngspice-stub shape with a wedgeNextBoot() repro hook. Every behavioral fix has red-then-green evidence (the reds were captured first). E-17 (a stale RUNNING cross-stamping the next run's generation under E-6's transport deferral) is DEFERRED with its analysis recorded — a real fix needs run identity on the bg frames. Test hygiene: the dwell lint now requires the mandated ": <why>" and all 47 bare markers carry their reason; three export-report dwells became modal-lease polls; exact-ledger assertions became relative deltas; the dead data-wx-dom-id branch, an unused fault hook and unused receipt plumbing are gone; abort scans, wx dialog drivers, the sim harness and the vitest FakeWorker are each one copy now. Bumps kicad and wxwidgets to their findings-group-e tips. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-27 12:27:12 +02:00
// Mirrors the production service (E-10): a retired worker emits
// no bg/exit frame of its own, so synthesize the controlled-exit
// the crashed engine could not send — straight to the installed
// handler, never through dispatchEvt (no fabricated credit).
const handler = (globalThis as any).__ngspiceOnEvent;
if (handler) {
(window as any).__ngspiceEvents.push({
kind: 'exit', status: 1, immediate: true, quit: false,
t: Date.now() - t0,
});
try {
handler({ kind: 'exit', status: 1, immediate: true, quit: false });
} catch (error) {
console.log(`[TEST-NGSPICE] synthetic exit dispatch failed: ${String(error)}`);
}
}
};
findings(E-1,E-2,E-3,E-6): ngspice service generations + credit-bounded event transport Adapted from codex/asyncify-execution-owner-core 3753320. Service side mirrors the occ-service shape (E-1 watchdogs, E-2 fail-all + boot-death fix — onerror now rejects the in-flight boot waiter instead of stranding it, E-3 onmessageerror terminal, Blob URL revoked, per-generation evtQueue cleared on retirement). E-6 transport bounds (worker hunks re-applied inside the emscripten-6 em-pthread else-branch — the codex file predates that split, so this is a re-application, not a cherry-pick): - batch cut at 512 lines / 1 MiB exact JSON-UTF-8 bytes, measured before a line is retained; a single line > 1 MiB flushes the accepted prefix then stops the event stream terminally (never retained); - posting gated by a 64-frame / 8 MiB unacked credit window; each frame carries { eventSequence, eventBytes } and is released only by an exact { sequence, bytes } ack; any mismatched ack is terminal; - the service mirrors the same 64-frame / 8 MiB bound on its pre-handler queue, acks after handing a frame to __ngspiceOnEvent, and retires the generation on invalid credit; { fatal } frames retire the worker. Tests: ngspice-service.test.ts (11, ported) — watchdogs, crash/bootError/ decode-fault settlement + recovery, out-of-order ids, sync postMessage throw, stale-generation event drops, fatal-frame retirement. tests/tools/ ngspice-worker-batch-unit.ts (node:vm over the production worker source; `npm run ngspice:worker-batch`) — bounded ordered chunks, byte-pressure flush, 100k-chunk credit storm, over-limit line, exact ack lease. e2e harness twin updated to speak the ack protocol (adds __ngspiceServiceTestHooks). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-19 15:48:41 +02:00
const ensureWorker = (): Promise<WorkerSlot> => {
if (disposed) return Promise.reject(new Error('ngspice service was disposed'));
if (!workerSlot) {
const slot = {
generation: nextGeneration++,
failed: false,
} as WorkerSlot;
workerSlot = slot;
const bootDeadline = new Promise<never>((_resolve, reject) => {
slot.rejectBoot = reject;
slot.bootTimer = setTimeout(() => {
if (slot.failed || workerSlot !== slot) return;
const why = `ngspice_service boot timed out after ${bootTimeoutMs} ms`;
console.log(`[TEST-NGSPICE] ${why} — resetting service`);
retireWorker(slot, why);
}, bootTimeoutMs);
});
const boot = (async () => {
const glue = new URL('ngspice_service.js', window.location.href).href;
console.log(`[TEST-NGSPICE] booting ngspice_service from ${glue}`);
findings(E-1,E-2,E-3,E-6): ngspice service generations + credit-bounded event transport Adapted from codex/asyncify-execution-owner-core 3753320. Service side mirrors the occ-service shape (E-1 watchdogs, E-2 fail-all + boot-death fix — onerror now rejects the in-flight boot waiter instead of stranding it, E-3 onmessageerror terminal, Blob URL revoked, per-generation evtQueue cleared on retirement). E-6 transport bounds (worker hunks re-applied inside the emscripten-6 em-pthread else-branch — the codex file predates that split, so this is a re-application, not a cherry-pick): - batch cut at 512 lines / 1 MiB exact JSON-UTF-8 bytes, measured before a line is retained; a single line > 1 MiB flushes the accepted prefix then stops the event stream terminally (never retained); - posting gated by a 64-frame / 8 MiB unacked credit window; each frame carries { eventSequence, eventBytes } and is released only by an exact { sequence, bytes } ack; any mismatched ack is terminal; - the service mirrors the same 64-frame / 8 MiB bound on its pre-handler queue, acks after handing a frame to __ngspiceOnEvent, and retires the generation on invalid credit; { fatal } frames retire the worker. Tests: ngspice-service.test.ts (11, ported) — watchdogs, crash/bootError/ decode-fault settlement + recovery, out-of-order ids, sync postMessage throw, stale-generation event drops, fatal-frame retirement. tests/tools/ ngspice-worker-batch-unit.ts (node:vm over the production worker source; `npm run ngspice:worker-batch`) — bounded ordered chunks, byte-pressure flush, 100k-chunk credit storm, over-limit line, exact ack lease. e2e harness twin updated to speak the ack protocol (adds __ngspiceServiceTestHooks). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-19 15:48:41 +02:00
slot.workerUrl = URL.createObjectURL(new Blob(
[`self.NGSPICE_GLUE_URL = ${JSON.stringify(glue)};\n`, workerSrc],
findings(E-1,E-2,E-3,E-6): ngspice service generations + credit-bounded event transport Adapted from codex/asyncify-execution-owner-core 3753320. Service side mirrors the occ-service shape (E-1 watchdogs, E-2 fail-all + boot-death fix — onerror now rejects the in-flight boot waiter instead of stranding it, E-3 onmessageerror terminal, Blob URL revoked, per-generation evtQueue cleared on retirement). E-6 transport bounds (worker hunks re-applied inside the emscripten-6 em-pthread else-branch — the codex file predates that split, so this is a re-application, not a cherry-pick): - batch cut at 512 lines / 1 MiB exact JSON-UTF-8 bytes, measured before a line is retained; a single line > 1 MiB flushes the accepted prefix then stops the event stream terminally (never retained); - posting gated by a 64-frame / 8 MiB unacked credit window; each frame carries { eventSequence, eventBytes } and is released only by an exact { sequence, bytes } ack; any mismatched ack is terminal; - the service mirrors the same 64-frame / 8 MiB bound on its pre-handler queue, acks after handing a frame to __ngspiceOnEvent, and retires the generation on invalid credit; { fatal } frames retire the worker. Tests: ngspice-service.test.ts (11, ported) — watchdogs, crash/bootError/ decode-fault settlement + recovery, out-of-order ids, sync postMessage throw, stale-generation event drops, fatal-frame retirement. tests/tools/ ngspice-worker-batch-unit.ts (node:vm over the production worker source; `npm run ngspice:worker-batch`) — bounded ordered chunks, byte-pressure flush, 100k-chunk credit storm, over-limit line, exact ack lease. e2e harness twin updated to speak the ack protocol (adds __ngspiceServiceTestHooks). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-19 15:48:41 +02:00
{ type: 'text/javascript' }));
const worker = new Worker(slot.workerUrl);
slot.worker = worker;
worker.onmessage = (e) => {
findings(E-1,E-2,E-3,E-6): ngspice service generations + credit-bounded event transport Adapted from codex/asyncify-execution-owner-core 3753320. Service side mirrors the occ-service shape (E-1 watchdogs, E-2 fail-all + boot-death fix — onerror now rejects the in-flight boot waiter instead of stranding it, E-3 onmessageerror terminal, Blob URL revoked, per-generation evtQueue cleared on retirement). E-6 transport bounds (worker hunks re-applied inside the emscripten-6 em-pthread else-branch — the codex file predates that split, so this is a re-application, not a cherry-pick): - batch cut at 512 lines / 1 MiB exact JSON-UTF-8 bytes, measured before a line is retained; a single line > 1 MiB flushes the accepted prefix then stops the event stream terminally (never retained); - posting gated by a 64-frame / 8 MiB unacked credit window; each frame carries { eventSequence, eventBytes } and is released only by an exact { sequence, bytes } ack; any mismatched ack is terminal; - the service mirrors the same 64-frame / 8 MiB bound on its pre-handler queue, acks after handing a frame to __ngspiceOnEvent, and retires the generation on invalid credit; { fatal } frames retire the worker. Tests: ngspice-service.test.ts (11, ported) — watchdogs, crash/bootError/ decode-fault settlement + recovery, out-of-order ids, sync postMessage throw, stale-generation event drops, fatal-frame retirement. tests/tools/ ngspice-worker-batch-unit.ts (node:vm over the production worker source; `npm run ngspice:worker-batch`) — bounded ordered chunks, byte-pressure flush, 100k-chunk credit storm, over-limit line, exact ack lease. e2e harness twin updated to speak the ack protocol (adds __ngspiceServiceTestHooks). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-19 15:48:41 +02:00
if (slot.failed || workerSlot !== slot) return;
const data = e.data ?? {};
findings(E-1,E-2,E-3,E-6): ngspice service generations + credit-bounded event transport Adapted from codex/asyncify-execution-owner-core 3753320. Service side mirrors the occ-service shape (E-1 watchdogs, E-2 fail-all + boot-death fix — onerror now rejects the in-flight boot waiter instead of stranding it, E-3 onmessageerror terminal, Blob URL revoked, per-generation evtQueue cleared on retirement). E-6 transport bounds (worker hunks re-applied inside the emscripten-6 em-pthread else-branch — the codex file predates that split, so this is a re-application, not a cherry-pick): - batch cut at 512 lines / 1 MiB exact JSON-UTF-8 bytes, measured before a line is retained; a single line > 1 MiB flushes the accepted prefix then stops the event stream terminally (never retained); - posting gated by a 64-frame / 8 MiB unacked credit window; each frame carries { eventSequence, eventBytes } and is released only by an exact { sequence, bytes } ack; any mismatched ack is terminal; - the service mirrors the same 64-frame / 8 MiB bound on its pre-handler queue, acks after handing a frame to __ngspiceOnEvent, and retires the generation on invalid credit; { fatal } frames retire the worker. Tests: ngspice-service.test.ts (11, ported) — watchdogs, crash/bootError/ decode-fault settlement + recovery, out-of-order ids, sync postMessage throw, stale-generation event drops, fatal-frame retirement. tests/tools/ ngspice-worker-batch-unit.ts (node:vm over the production worker source; `npm run ngspice:worker-batch`) — bounded ordered chunks, byte-pressure flush, 100k-chunk credit storm, over-limit line, exact ack lease. e2e harness twin updated to speak the ack protocol (adds __ngspiceServiceTestHooks). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-19 15:48:41 +02:00
if (data.fatal) {
findings(E-10..E-22): fix the defects a code review found in the E-1..E-9 work A review of the group-E fixes found 13 further defects; ten were introduced by those fixes, two pre-existed and were merely relocated, one is deferred. Services / transport E-10 retireWorker synthesized no bg/exit frame, so sharedspice's s_bgRunning mirror stayed latched true after a mid-run worker death: Run stayed disabled and the promised fresh-worker restart was unreachable for the whole session. Retirement now dispatches a synthetic controlled-exit straight to the installed handler (never through dispatchEvt — a fabricated frame must not touch the credit ledger). Driving the repro exposed two further defects, both fixed here: a replacement worker trapped on pre-init engine reads, and the rerun's cm_input_path/circ hit that uninitialized engine before KiCad's validate() re-init (the native flow assumes a crashed engine survives in-process — true for the dll, false for a dead worker). Reads now answer their empty shapes pre-init, writes lazy-init, and init is idempotent per worker engine. E-19 dispatchEvt acked only AFTER handler(evt) returned, and the sharedspice client deliberately rethrows non-trap errors — so each throw leaked one unit of the 64-frame credit window until the stream died with a misattributed "transport exceeded". The ack moves to a finally in both service copies; the throw still propagates (the trap machinery needs it). E-20 the oversize-line path promises to transfer the accepted prefix, but with the window full that flush only DEFERS, and stopEventStream wiped the deferred queue — losing the diagnostics that explain the failure. The terminal notice now carries them as pendingEvents; both hosts deliver them in order, unacked (the fatal frame is outside the credit protocol). E-21 the 30s prefetch deadline discarded every model already collected and reported nothing. A caller-owned progress sink ships the partials and the omission reaches the export report. (Awaiting the aborted collection was rejected: an in-flight source fetch is not abortable — E-4's original disease.) Plus a serving-candidate memo, so a .wrl ref served by its .step fallback stops re-probing the miss on every export. Scheduler E-14 _terminalizeNativeTrap classified by message substring, so any plain JS error QUOTING 'Aborted(' or 'out of bounds' permanently bricked a healthy instance. Now structural only: instanceof RuntimeError plus a duck-typed name check (verified in this build's glue that abort() throws a genuine RuntimeError both pre- and post-runtime-init). Module.onAbort now latches the gate — the authoritative notification, previously ignored. E-15 the shim half: _pumpResume gates on terminal (catching wakes already queued at latch time) and resolveWait refuses on terminal WITHOUT consuming the entry, so a frame stays visibly parked rather than resuming inside a trapped module. E-16 the E-5 handler read the realm-global scheduler at dispatch instead of its installing module's; also frees the per-line buffer on the non-trap rethrow path. E-11 get_vec trusted the worker's res.length over the transferred arrays. Observed death shape: a 4 GiB std::vector threw an unhandled std::length_error that exited the editor's main loop. Now clamped, with the buffers freed on every failure path. Guardrails (replacing two deferred refactors: e2e→production-code injection and collapsing the four copies of the worker-lifecycle machinery) E-18 the source contract asserted comment-string counts — rewording failed CI while moving a guard outside its #ifdef passed. It now parses the #ifdef regions and asserts on code. service-stub-parity.ts pins what the four lifecycle copies must share: credit-window equality parsed from source, the finally-ack, boot deadlines, terminal-notice consumption. The transport numbers are now single-sourced from the worker. CI actually runs the gates: the web/standalone vitest suites (which had NEVER run in CI), the reducer, the source contract and the parity tool — with a NON_PLAYWRIGHT_GATES check so deleting a step re-fails the lint. E-22 the e2e occ stub's 60s boot watchdog, deleted in a66e109, is restored in the ngspice-stub shape with a wedgeNextBoot() repro hook. Every behavioral fix has red-then-green evidence (the reds were captured first). E-17 (a stale RUNNING cross-stamping the next run's generation under E-6's transport deferral) is DEFERRED with its analysis recorded — a real fix needs run identity on the bg frames. Test hygiene: the dwell lint now requires the mandated ": <why>" and all 47 bare markers carry their reason; three export-report dwells became modal-lease polls; exact-ledger assertions became relative deltas; the dead data-wx-dom-id branch, an unused fault hook and unused receipt plumbing are gone; abort scans, wx dialog drivers, the sim harness and the vitest FakeWorker are each one copy now. Bumps kicad and wxwidgets to their findings-group-e tips. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-27 12:27:12 +02:00
deliverTerminalEvents(data.pendingEvents);
findings(E-1,E-2,E-3,E-6): ngspice service generations + credit-bounded event transport Adapted from codex/asyncify-execution-owner-core 3753320. Service side mirrors the occ-service shape (E-1 watchdogs, E-2 fail-all + boot-death fix — onerror now rejects the in-flight boot waiter instead of stranding it, E-3 onmessageerror terminal, Blob URL revoked, per-generation evtQueue cleared on retirement). E-6 transport bounds (worker hunks re-applied inside the emscripten-6 em-pthread else-branch — the codex file predates that split, so this is a re-application, not a cherry-pick): - batch cut at 512 lines / 1 MiB exact JSON-UTF-8 bytes, measured before a line is retained; a single line > 1 MiB flushes the accepted prefix then stops the event stream terminally (never retained); - posting gated by a 64-frame / 8 MiB unacked credit window; each frame carries { eventSequence, eventBytes } and is released only by an exact { sequence, bytes } ack; any mismatched ack is terminal; - the service mirrors the same 64-frame / 8 MiB bound on its pre-handler queue, acks after handing a frame to __ngspiceOnEvent, and retires the generation on invalid credit; { fatal } frames retire the worker. Tests: ngspice-service.test.ts (11, ported) — watchdogs, crash/bootError/ decode-fault settlement + recovery, out-of-order ids, sync postMessage throw, stale-generation event drops, fatal-frame retirement. tests/tools/ ngspice-worker-batch-unit.ts (node:vm over the production worker source; `npm run ngspice:worker-batch`) — bounded ordered chunks, byte-pressure flush, 100k-chunk credit storm, over-limit line, exact ack lease. e2e harness twin updated to speak the ack protocol (adds __ngspiceServiceTestHooks). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-19 15:48:41 +02:00
failWorker(`event stream failure: ${String(data.fatal)}`);
return;
}
if (data.evt) {
dispatchEvt(
slot,
data.evt,
data.eventSequence,
data.eventBytes,
);
return;
}
if (typeof data.id !== 'number') return;
findings(E-1,E-2,E-3,E-6): ngspice service generations + credit-bounded event transport Adapted from codex/asyncify-execution-owner-core 3753320. Service side mirrors the occ-service shape (E-1 watchdogs, E-2 fail-all + boot-death fix — onerror now rejects the in-flight boot waiter instead of stranding it, E-3 onmessageerror terminal, Blob URL revoked, per-generation evtQueue cleared on retirement). E-6 transport bounds (worker hunks re-applied inside the emscripten-6 em-pthread else-branch — the codex file predates that split, so this is a re-application, not a cherry-pick): - batch cut at 512 lines / 1 MiB exact JSON-UTF-8 bytes, measured before a line is retained; a single line > 1 MiB flushes the accepted prefix then stops the event stream terminally (never retained); - posting gated by a 64-frame / 8 MiB unacked credit window; each frame carries { eventSequence, eventBytes } and is released only by an exact { sequence, bytes } ack; any mismatched ack is terminal; - the service mirrors the same 64-frame / 8 MiB bound on its pre-handler queue, acks after handing a frame to __ngspiceOnEvent, and retires the generation on invalid credit; { fatal } frames retire the worker. Tests: ngspice-service.test.ts (11, ported) — watchdogs, crash/bootError/ decode-fault settlement + recovery, out-of-order ids, sync postMessage throw, stale-generation event drops, fatal-frame retirement. tests/tools/ ngspice-worker-batch-unit.ts (node:vm over the production worker source; `npm run ngspice:worker-batch`) — bounded ordered chunks, byte-pressure flush, 100k-chunk credit storm, over-limit line, exact ack lease. e2e harness twin updated to speak the ack protocol (adds __ngspiceServiceTestHooks). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-19 15:48:41 +02:00
const request = pending.get(data.id);
if (request?.generation === slot.generation) {
pending.delete(data.id);
clearTimeout(request.timer);
request.resolve(data.res);
}
};
findings(E-1,E-2,E-3,E-6): ngspice service generations + credit-bounded event transport Adapted from codex/asyncify-execution-owner-core 3753320. Service side mirrors the occ-service shape (E-1 watchdogs, E-2 fail-all + boot-death fix — onerror now rejects the in-flight boot waiter instead of stranding it, E-3 onmessageerror terminal, Blob URL revoked, per-generation evtQueue cleared on retirement). E-6 transport bounds (worker hunks re-applied inside the emscripten-6 em-pthread else-branch — the codex file predates that split, so this is a re-application, not a cherry-pick): - batch cut at 512 lines / 1 MiB exact JSON-UTF-8 bytes, measured before a line is retained; a single line > 1 MiB flushes the accepted prefix then stops the event stream terminally (never retained); - posting gated by a 64-frame / 8 MiB unacked credit window; each frame carries { eventSequence, eventBytes } and is released only by an exact { sequence, bytes } ack; any mismatched ack is terminal; - the service mirrors the same 64-frame / 8 MiB bound on its pre-handler queue, acks after handing a frame to __ngspiceOnEvent, and retires the generation on invalid credit; { fatal } frames retire the worker. Tests: ngspice-service.test.ts (11, ported) — watchdogs, crash/bootError/ decode-fault settlement + recovery, out-of-order ids, sync postMessage throw, stale-generation event drops, fatal-frame retirement. tests/tools/ ngspice-worker-batch-unit.ts (node:vm over the production worker source; `npm run ngspice:worker-batch`) — bounded ordered chunks, byte-pressure flush, 100k-chunk credit storm, over-limit line, exact ack lease. e2e harness twin updated to speak the ack protocol (adds __ngspiceServiceTestHooks). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-19 15:48:41 +02:00
const failWorker = (detail: string) => {
const why = `ngspice_service crashed: ${detail}`;
console.log(`[TEST-NGSPICE] worker error: ${detail} — resetting service`);
retireWorker(slot, why);
};
findings(E-1,E-2,E-3,E-6): ngspice service generations + credit-bounded event transport Adapted from codex/asyncify-execution-owner-core 3753320. Service side mirrors the occ-service shape (E-1 watchdogs, E-2 fail-all + boot-death fix — onerror now rejects the in-flight boot waiter instead of stranding it, E-3 onmessageerror terminal, Blob URL revoked, per-generation evtQueue cleared on retirement). E-6 transport bounds (worker hunks re-applied inside the emscripten-6 em-pthread else-branch — the codex file predates that split, so this is a re-application, not a cherry-pick): - batch cut at 512 lines / 1 MiB exact JSON-UTF-8 bytes, measured before a line is retained; a single line > 1 MiB flushes the accepted prefix then stops the event stream terminally (never retained); - posting gated by a 64-frame / 8 MiB unacked credit window; each frame carries { eventSequence, eventBytes } and is released only by an exact { sequence, bytes } ack; any mismatched ack is terminal; - the service mirrors the same 64-frame / 8 MiB bound on its pre-handler queue, acks after handing a frame to __ngspiceOnEvent, and retires the generation on invalid credit; { fatal } frames retire the worker. Tests: ngspice-service.test.ts (11, ported) — watchdogs, crash/bootError/ decode-fault settlement + recovery, out-of-order ids, sync postMessage throw, stale-generation event drops, fatal-frame retirement. tests/tools/ ngspice-worker-batch-unit.ts (node:vm over the production worker source; `npm run ngspice:worker-batch`) — bounded ordered chunks, byte-pressure flush, 100k-chunk credit storm, over-limit line, exact ack lease. e2e harness twin updated to speak the ack protocol (adds __ngspiceServiceTestHooks). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-19 15:48:41 +02:00
worker.onerror = (e) => failWorker(e.message || 'worker error');
slot.failDecode = () => failWorker('message decode failed');
worker.onmessageerror = slot.failDecode;
if (bootMessageErrorArmed) {
bootMessageErrorArmed = false;
// Synthetic dispatch on Worker is engine-dependent.
// Call the same transition as the real event handler
// after rejectBoot is installed below.
queueMicrotask(() => slot.failDecode());
}
await new Promise<void>((resolve) => {
const onFirst = (e: MessageEvent) => {
jspi: fix the dead-tools ownership bug, emscripten-6 fallout, and green the full suite on Playwright 1.62 Live-app fix (Place Footprints / routing dead in Chrome): submodule bumps carry the coroutine ownership fix (kicad 012d95ecb4) and the handler-exception survival fix (wxwidgets 1b5f0e31f4). Emscripten-6 fallout: - occ/ngspice worker wrappers: mainScriptUrlOrBlob was removed upstream; pthread children re-run the wrapper blob, so an em-pthread realm now importScripts the glue and gets out of the way (before: recursive service boots, pool never fills, silent 180s boot hangs — every occ spec and ngspice bg_run). - Makefile.wasm: -sASYNCIFY frankenlinks on the no-wx coroutine repro targets ported to -sJSPI (the JSPI-only libcontext crashed at first yield under them); mainloop/gl repro pages drive their tick through a promising export (emscripten_set_main_loop callbacks cannot suspend); retired inject-dyncall-shims lines removed (targets were unbuildable since Phase 8); $stringToNewUTF8 force-included (the EM_ASM value bridge aborted the runtime on the first decoded exception). - fiber-park levers: neither embind shape can drive suspending levers (plain throws on strict-JSPI Firefox; emscripten::async() re-executes its invoker on settle) — kept sync for manual Chromium probing, spec coverage moved to the jspi-coroutine harness (18 cases). Suite work: - Playwright 1.61.1 -> 1.62.1 (Firefox 153: JSPI on by default). - fiber-resume-park.spec retired -> coroutine-lifecycle.spec: census gate over boot / board load / chooser open / cancel (deterministically red on the pre-fix build). - Blind asyncify-era pins re-keyed: quasimodal-strand + wait-beacons beacon regexes, footprint-chooser-close liveness -> wx parking-timer heartbeat (scheduler counters idle flat on Firefox). - occ/ngspice test providers: 60s boot timeout + worker error surfacing (a worker death used to be a silent 180s timeout). - Harness pages: stale 9.99 config dir -> 10.0 (library_manager wxCHECK noise, chooser had no libraries). - gal-webgl harness: missing artifacts rebuilt (boost/glm extracted to the host sysroot), PgmOrNull stub added for the rebased GAL. - jspi-scheduler: clean-shutdown console line restored (app-quit contract), quarantine never yanks SP from a live window. Gates: test:e2e 699 passed / 0 failed (wx-chromium, kicad-firefox, kicad-chromium, jspi-firefox, coroutine-firefox); web ff/cr/mobile 71 passed; lint:ci-coverage 166, lint:determinism 163, screenshots manifest 492 current, corpus 7/7, tools:contract green. Offline screenshot baselines show expected mass drift from the engine bump — re-baseline (screenshots:noise -> promote) is a follow-up. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016X9eh1s5sTx1o9Em9KBuwR
2026-08-13 17:41:28 +02:00
if (e.data?.ready) {
findings(E-1,E-2,E-3,E-6): ngspice service generations + credit-bounded event transport Adapted from codex/asyncify-execution-owner-core 3753320. Service side mirrors the occ-service shape (E-1 watchdogs, E-2 fail-all + boot-death fix — onerror now rejects the in-flight boot waiter instead of stranding it, E-3 onmessageerror terminal, Blob URL revoked, per-generation evtQueue cleared on retirement). E-6 transport bounds (worker hunks re-applied inside the emscripten-6 em-pthread else-branch — the codex file predates that split, so this is a re-application, not a cherry-pick): - batch cut at 512 lines / 1 MiB exact JSON-UTF-8 bytes, measured before a line is retained; a single line > 1 MiB flushes the accepted prefix then stops the event stream terminally (never retained); - posting gated by a 64-frame / 8 MiB unacked credit window; each frame carries { eventSequence, eventBytes } and is released only by an exact { sequence, bytes } ack; any mismatched ack is terminal; - the service mirrors the same 64-frame / 8 MiB bound on its pre-handler queue, acks after handing a frame to __ngspiceOnEvent, and retires the generation on invalid credit; { fatal } frames retire the worker. Tests: ngspice-service.test.ts (11, ported) — watchdogs, crash/bootError/ decode-fault settlement + recovery, out-of-order ids, sync postMessage throw, stale-generation event drops, fatal-frame retirement. tests/tools/ ngspice-worker-batch-unit.ts (node:vm over the production worker source; `npm run ngspice:worker-batch`) — bounded ordered chunks, byte-pressure flush, 100k-chunk credit storm, over-limit line, exact ack lease. e2e harness twin updated to speak the ack protocol (adds __ngspiceServiceTestHooks). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-19 15:48:41 +02:00
if (slot.bootTimer !== undefined) {
clearTimeout(slot.bootTimer);
slot.bootTimer = undefined;
}
slot.removeBootListener?.();
slot.removeBootListener = undefined;
slot.rejectBoot = undefined;
jspi: fix the dead-tools ownership bug, emscripten-6 fallout, and green the full suite on Playwright 1.62 Live-app fix (Place Footprints / routing dead in Chrome): submodule bumps carry the coroutine ownership fix (kicad 012d95ecb4) and the handler-exception survival fix (wxwidgets 1b5f0e31f4). Emscripten-6 fallout: - occ/ngspice worker wrappers: mainScriptUrlOrBlob was removed upstream; pthread children re-run the wrapper blob, so an em-pthread realm now importScripts the glue and gets out of the way (before: recursive service boots, pool never fills, silent 180s boot hangs — every occ spec and ngspice bg_run). - Makefile.wasm: -sASYNCIFY frankenlinks on the no-wx coroutine repro targets ported to -sJSPI (the JSPI-only libcontext crashed at first yield under them); mainloop/gl repro pages drive their tick through a promising export (emscripten_set_main_loop callbacks cannot suspend); retired inject-dyncall-shims lines removed (targets were unbuildable since Phase 8); $stringToNewUTF8 force-included (the EM_ASM value bridge aborted the runtime on the first decoded exception). - fiber-park levers: neither embind shape can drive suspending levers (plain throws on strict-JSPI Firefox; emscripten::async() re-executes its invoker on settle) — kept sync for manual Chromium probing, spec coverage moved to the jspi-coroutine harness (18 cases). Suite work: - Playwright 1.61.1 -> 1.62.1 (Firefox 153: JSPI on by default). - fiber-resume-park.spec retired -> coroutine-lifecycle.spec: census gate over boot / board load / chooser open / cancel (deterministically red on the pre-fix build). - Blind asyncify-era pins re-keyed: quasimodal-strand + wait-beacons beacon regexes, footprint-chooser-close liveness -> wx parking-timer heartbeat (scheduler counters idle flat on Firefox). - occ/ngspice test providers: 60s boot timeout + worker error surfacing (a worker death used to be a silent 180s timeout). - Harness pages: stale 9.99 config dir -> 10.0 (library_manager wxCHECK noise, chooser had no libraries). - gal-webgl harness: missing artifacts rebuilt (boost/glm extracted to the host sysroot), PgmOrNull stub added for the rebased GAL. - jspi-scheduler: clean-shutdown console line restored (app-quit contract), quarantine never yanks SP from a live window. Gates: test:e2e 699 passed / 0 failed (wx-chromium, kicad-firefox, kicad-chromium, jspi-firefox, coroutine-firefox); web ff/cr/mobile 71 passed; lint:ci-coverage 166, lint:determinism 163, screenshots manifest 492 current, corpus 7/7, tools:contract green. Offline screenshot baselines show expected mass drift from the engine bump — re-baseline (screenshots:noise -> promote) is a follow-up. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016X9eh1s5sTx1o9Em9KBuwR
2026-08-13 17:41:28 +02:00
resolve();
} else if (e.data?.bootError) {
findings(E-1,E-2,E-3,E-6): ngspice service generations + credit-bounded event transport Adapted from codex/asyncify-execution-owner-core 3753320. Service side mirrors the occ-service shape (E-1 watchdogs, E-2 fail-all + boot-death fix — onerror now rejects the in-flight boot waiter instead of stranding it, E-3 onmessageerror terminal, Blob URL revoked, per-generation evtQueue cleared on retirement). E-6 transport bounds (worker hunks re-applied inside the emscripten-6 em-pthread else-branch — the codex file predates that split, so this is a re-application, not a cherry-pick): - batch cut at 512 lines / 1 MiB exact JSON-UTF-8 bytes, measured before a line is retained; a single line > 1 MiB flushes the accepted prefix then stops the event stream terminally (never retained); - posting gated by a 64-frame / 8 MiB unacked credit window; each frame carries { eventSequence, eventBytes } and is released only by an exact { sequence, bytes } ack; any mismatched ack is terminal; - the service mirrors the same 64-frame / 8 MiB bound on its pre-handler queue, acks after handing a frame to __ngspiceOnEvent, and retires the generation on invalid credit; { fatal } frames retire the worker. Tests: ngspice-service.test.ts (11, ported) — watchdogs, crash/bootError/ decode-fault settlement + recovery, out-of-order ids, sync postMessage throw, stale-generation event drops, fatal-frame retirement. tests/tools/ ngspice-worker-batch-unit.ts (node:vm over the production worker source; `npm run ngspice:worker-batch`) — bounded ordered chunks, byte-pressure flush, 100k-chunk credit storm, over-limit line, exact ack lease. e2e harness twin updated to speak the ack protocol (adds __ngspiceServiceTestHooks). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-19 15:48:41 +02:00
const why = `ngspice_service boot failed: ${String(e.data.bootError)}`;
retireWorker(slot, why);
jspi: fix the dead-tools ownership bug, emscripten-6 fallout, and green the full suite on Playwright 1.62 Live-app fix (Place Footprints / routing dead in Chrome): submodule bumps carry the coroutine ownership fix (kicad 012d95ecb4) and the handler-exception survival fix (wxwidgets 1b5f0e31f4). Emscripten-6 fallout: - occ/ngspice worker wrappers: mainScriptUrlOrBlob was removed upstream; pthread children re-run the wrapper blob, so an em-pthread realm now importScripts the glue and gets out of the way (before: recursive service boots, pool never fills, silent 180s boot hangs — every occ spec and ngspice bg_run). - Makefile.wasm: -sASYNCIFY frankenlinks on the no-wx coroutine repro targets ported to -sJSPI (the JSPI-only libcontext crashed at first yield under them); mainloop/gl repro pages drive their tick through a promising export (emscripten_set_main_loop callbacks cannot suspend); retired inject-dyncall-shims lines removed (targets were unbuildable since Phase 8); $stringToNewUTF8 force-included (the EM_ASM value bridge aborted the runtime on the first decoded exception). - fiber-park levers: neither embind shape can drive suspending levers (plain throws on strict-JSPI Firefox; emscripten::async() re-executes its invoker on settle) — kept sync for manual Chromium probing, spec coverage moved to the jspi-coroutine harness (18 cases). Suite work: - Playwright 1.61.1 -> 1.62.1 (Firefox 153: JSPI on by default). - fiber-resume-park.spec retired -> coroutine-lifecycle.spec: census gate over boot / board load / chooser open / cancel (deterministically red on the pre-fix build). - Blind asyncify-era pins re-keyed: quasimodal-strand + wait-beacons beacon regexes, footprint-chooser-close liveness -> wx parking-timer heartbeat (scheduler counters idle flat on Firefox). - occ/ngspice test providers: 60s boot timeout + worker error surfacing (a worker death used to be a silent 180s timeout). - Harness pages: stale 9.99 config dir -> 10.0 (library_manager wxCHECK noise, chooser had no libraries). - gal-webgl harness: missing artifacts rebuilt (boost/glm extracted to the host sysroot), PgmOrNull stub added for the rebased GAL. - jspi-scheduler: clean-shutdown console line restored (app-quit contract), quarantine never yanks SP from a live window. Gates: test:e2e 699 passed / 0 failed (wx-chromium, kicad-firefox, kicad-chromium, jspi-firefox, coroutine-firefox); web ff/cr/mobile 71 passed; lint:ci-coverage 166, lint:determinism 163, screenshots manifest 492 current, corpus 7/7, tools:contract green. Offline screenshot baselines show expected mass drift from the engine bump — re-baseline (screenshots:noise -> promote) is a follow-up. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016X9eh1s5sTx1o9Em9KBuwR
2026-08-13 17:41:28 +02:00
}
};
worker.addEventListener('message', onFirst);
findings(E-1,E-2,E-3,E-6): ngspice service generations + credit-bounded event transport Adapted from codex/asyncify-execution-owner-core 3753320. Service side mirrors the occ-service shape (E-1 watchdogs, E-2 fail-all + boot-death fix — onerror now rejects the in-flight boot waiter instead of stranding it, E-3 onmessageerror terminal, Blob URL revoked, per-generation evtQueue cleared on retirement). E-6 transport bounds (worker hunks re-applied inside the emscripten-6 em-pthread else-branch — the codex file predates that split, so this is a re-application, not a cherry-pick): - batch cut at 512 lines / 1 MiB exact JSON-UTF-8 bytes, measured before a line is retained; a single line > 1 MiB flushes the accepted prefix then stops the event stream terminally (never retained); - posting gated by a 64-frame / 8 MiB unacked credit window; each frame carries { eventSequence, eventBytes } and is released only by an exact { sequence, bytes } ack; any mismatched ack is terminal; - the service mirrors the same 64-frame / 8 MiB bound on its pre-handler queue, acks after handing a frame to __ngspiceOnEvent, and retires the generation on invalid credit; { fatal } frames retire the worker. Tests: ngspice-service.test.ts (11, ported) — watchdogs, crash/bootError/ decode-fault settlement + recovery, out-of-order ids, sync postMessage throw, stale-generation event drops, fatal-frame retirement. tests/tools/ ngspice-worker-batch-unit.ts (node:vm over the production worker source; `npm run ngspice:worker-batch`) — bounded ordered chunks, byte-pressure flush, 100k-chunk credit storm, over-limit line, exact ack lease. e2e harness twin updated to speak the ack protocol (adds __ngspiceServiceTestHooks). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-19 15:48:41 +02:00
slot.removeBootListener = () => worker.removeEventListener('message', onFirst);
});
findings(E-1,E-2,E-3,E-6): ngspice service generations + credit-bounded event transport Adapted from codex/asyncify-execution-owner-core 3753320. Service side mirrors the occ-service shape (E-1 watchdogs, E-2 fail-all + boot-death fix — onerror now rejects the in-flight boot waiter instead of stranding it, E-3 onmessageerror terminal, Blob URL revoked, per-generation evtQueue cleared on retirement). E-6 transport bounds (worker hunks re-applied inside the emscripten-6 em-pthread else-branch — the codex file predates that split, so this is a re-application, not a cherry-pick): - batch cut at 512 lines / 1 MiB exact JSON-UTF-8 bytes, measured before a line is retained; a single line > 1 MiB flushes the accepted prefix then stops the event stream terminally (never retained); - posting gated by a 64-frame / 8 MiB unacked credit window; each frame carries { eventSequence, eventBytes } and is released only by an exact { sequence, bytes } ack; any mismatched ack is terminal; - the service mirrors the same 64-frame / 8 MiB bound on its pre-handler queue, acks after handing a frame to __ngspiceOnEvent, and retires the generation on invalid credit; { fatal } frames retire the worker. Tests: ngspice-service.test.ts (11, ported) — watchdogs, crash/bootError/ decode-fault settlement + recovery, out-of-order ids, sync postMessage throw, stale-generation event drops, fatal-frame retirement. tests/tools/ ngspice-worker-batch-unit.ts (node:vm over the production worker source; `npm run ngspice:worker-batch`) — bounded ordered chunks, byte-pressure flush, 100k-chunk credit storm, over-limit line, exact ack lease. e2e harness twin updated to speak the ack protocol (adds __ngspiceServiceTestHooks). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-19 15:48:41 +02:00
if (slot.failed || workerSlot !== slot) {
throw new Error('ngspice_service worker retired during boot');
}
console.log('[TEST-NGSPICE] ngspice_service ready');
findings(E-1,E-2,E-3,E-6): ngspice service generations + credit-bounded event transport Adapted from codex/asyncify-execution-owner-core 3753320. Service side mirrors the occ-service shape (E-1 watchdogs, E-2 fail-all + boot-death fix — onerror now rejects the in-flight boot waiter instead of stranding it, E-3 onmessageerror terminal, Blob URL revoked, per-generation evtQueue cleared on retirement). E-6 transport bounds (worker hunks re-applied inside the emscripten-6 em-pthread else-branch — the codex file predates that split, so this is a re-application, not a cherry-pick): - batch cut at 512 lines / 1 MiB exact JSON-UTF-8 bytes, measured before a line is retained; a single line > 1 MiB flushes the accepted prefix then stops the event stream terminally (never retained); - posting gated by a 64-frame / 8 MiB unacked credit window; each frame carries { eventSequence, eventBytes } and is released only by an exact { sequence, bytes } ack; any mismatched ack is terminal; - the service mirrors the same 64-frame / 8 MiB bound on its pre-handler queue, acks after handing a frame to __ngspiceOnEvent, and retires the generation on invalid credit; { fatal } frames retire the worker. Tests: ngspice-service.test.ts (11, ported) — watchdogs, crash/bootError/ decode-fault settlement + recovery, out-of-order ids, sync postMessage throw, stale-generation event drops, fatal-frame retirement. tests/tools/ ngspice-worker-batch-unit.ts (node:vm over the production worker source; `npm run ngspice:worker-batch`) — bounded ordered chunks, byte-pressure flush, 100k-chunk credit storm, over-limit line, exact ack lease. e2e harness twin updated to speak the ack protocol (adds __ngspiceServiceTestHooks). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-19 15:48:41 +02:00
return slot;
})();
slot.ready = Promise.race([boot, bootDeadline]).catch((e) => {
retireWorker(slot, `ngspice_service unavailable: ${String(e)}`);
throw e;
});
}
return workerSlot.ready;
};
const post = (slot: WorkerSlot, req: any): Promise<any> => {
const worker = slot.worker;
if (!worker || slot.failed || workerSlot !== slot) {
return Promise.resolve({ error: 'ngspice_service worker is unavailable' });
}
findings(E-1,E-2,E-3,E-6): ngspice service generations + credit-bounded event transport Adapted from codex/asyncify-execution-owner-core 3753320. Service side mirrors the occ-service shape (E-1 watchdogs, E-2 fail-all + boot-death fix — onerror now rejects the in-flight boot waiter instead of stranding it, E-3 onmessageerror terminal, Blob URL revoked, per-generation evtQueue cleared on retirement). E-6 transport bounds (worker hunks re-applied inside the emscripten-6 em-pthread else-branch — the codex file predates that split, so this is a re-application, not a cherry-pick): - batch cut at 512 lines / 1 MiB exact JSON-UTF-8 bytes, measured before a line is retained; a single line > 1 MiB flushes the accepted prefix then stops the event stream terminally (never retained); - posting gated by a 64-frame / 8 MiB unacked credit window; each frame carries { eventSequence, eventBytes } and is released only by an exact { sequence, bytes } ack; any mismatched ack is terminal; - the service mirrors the same 64-frame / 8 MiB bound on its pre-handler queue, acks after handing a frame to __ngspiceOnEvent, and retires the generation on invalid credit; { fatal } frames retire the worker. Tests: ngspice-service.test.ts (11, ported) — watchdogs, crash/bootError/ decode-fault settlement + recovery, out-of-order ids, sync postMessage throw, stale-generation event drops, fatal-frame retirement. tests/tools/ ngspice-worker-batch-unit.ts (node:vm over the production worker source; `npm run ngspice:worker-batch`) — bounded ordered chunks, byte-pressure flush, 100k-chunk credit storm, over-limit line, exact ack lease. e2e harness twin updated to speak the ack protocol (adds __ngspiceServiceTestHooks). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-19 15:48:41 +02:00
const id = nextId++;
return new Promise((resolve) => {
const timer = setTimeout(() => {
if (pending.get(id)?.generation !== slot.generation) return;
const why = `ngspice_service response timed out after ${responseTimeoutMs} ms`;
console.log(`[TEST-NGSPICE] ${why} — resetting service`);
retireWorker(slot, why);
}, responseTimeoutMs);
pending.set(id, { generation: slot.generation, resolve, timer });
let generationPending = 0;
for (const request of pending.values()) {
if (request.generation === slot.generation) generationPending++;
}
maxPending = Math.max(maxPending, generationPending);
try {
worker.postMessage({ id, req });
if (runtimeMessageErrorThreshold !== null
&& generationPending >= runtimeMessageErrorThreshold) {
runtimeMessageErrorThreshold = null;
slot.failDecode();
}
} catch (error) {
pending.delete(id);
clearTimeout(timer);
resolve({ error: `ngspice_service request failed: ${String(error)}` });
}
});
};
const request = async (req: any) => {
findings(E-1,E-2,E-3,E-6): ngspice service generations + credit-bounded event transport Adapted from codex/asyncify-execution-owner-core 3753320. Service side mirrors the occ-service shape (E-1 watchdogs, E-2 fail-all + boot-death fix — onerror now rejects the in-flight boot waiter instead of stranding it, E-3 onmessageerror terminal, Blob URL revoked, per-generation evtQueue cleared on retirement). E-6 transport bounds (worker hunks re-applied inside the emscripten-6 em-pthread else-branch — the codex file predates that split, so this is a re-application, not a cherry-pick): - batch cut at 512 lines / 1 MiB exact JSON-UTF-8 bytes, measured before a line is retained; a single line > 1 MiB flushes the accepted prefix then stops the event stream terminally (never retained); - posting gated by a 64-frame / 8 MiB unacked credit window; each frame carries { eventSequence, eventBytes } and is released only by an exact { sequence, bytes } ack; any mismatched ack is terminal; - the service mirrors the same 64-frame / 8 MiB bound on its pre-handler queue, acks after handing a frame to __ngspiceOnEvent, and retires the generation on invalid credit; { fatal } frames retire the worker. Tests: ngspice-service.test.ts (11, ported) — watchdogs, crash/bootError/ decode-fault settlement + recovery, out-of-order ids, sync postMessage throw, stale-generation event drops, fatal-frame retirement. tests/tools/ ngspice-worker-batch-unit.ts (node:vm over the production worker source; `npm run ngspice:worker-batch`) — bounded ordered chunks, byte-pressure flush, 100k-chunk credit storm, over-limit line, exact ack lease. e2e harness twin updated to speak the ack protocol (adds __ngspiceServiceTestHooks). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-19 15:48:41 +02:00
// Assign at issue time, before worker boot or posting. A late
// response from a request issued before a new simulation's
// checkpoint can therefore never satisfy that simulation.
if (disposed) return { error: 'ngspice service was disposed' };
const requestSequence = nextRequestSequence++;
findings(E-10..E-22): fix the defects a code review found in the E-1..E-9 work A review of the group-E fixes found 13 further defects; ten were introduced by those fixes, two pre-existed and were merely relocated, one is deferred. Services / transport E-10 retireWorker synthesized no bg/exit frame, so sharedspice's s_bgRunning mirror stayed latched true after a mid-run worker death: Run stayed disabled and the promised fresh-worker restart was unreachable for the whole session. Retirement now dispatches a synthetic controlled-exit straight to the installed handler (never through dispatchEvt — a fabricated frame must not touch the credit ledger). Driving the repro exposed two further defects, both fixed here: a replacement worker trapped on pre-init engine reads, and the rerun's cm_input_path/circ hit that uninitialized engine before KiCad's validate() re-init (the native flow assumes a crashed engine survives in-process — true for the dll, false for a dead worker). Reads now answer their empty shapes pre-init, writes lazy-init, and init is idempotent per worker engine. E-19 dispatchEvt acked only AFTER handler(evt) returned, and the sharedspice client deliberately rethrows non-trap errors — so each throw leaked one unit of the 64-frame credit window until the stream died with a misattributed "transport exceeded". The ack moves to a finally in both service copies; the throw still propagates (the trap machinery needs it). E-20 the oversize-line path promises to transfer the accepted prefix, but with the window full that flush only DEFERS, and stopEventStream wiped the deferred queue — losing the diagnostics that explain the failure. The terminal notice now carries them as pendingEvents; both hosts deliver them in order, unacked (the fatal frame is outside the credit protocol). E-21 the 30s prefetch deadline discarded every model already collected and reported nothing. A caller-owned progress sink ships the partials and the omission reaches the export report. (Awaiting the aborted collection was rejected: an in-flight source fetch is not abortable — E-4's original disease.) Plus a serving-candidate memo, so a .wrl ref served by its .step fallback stops re-probing the miss on every export. Scheduler E-14 _terminalizeNativeTrap classified by message substring, so any plain JS error QUOTING 'Aborted(' or 'out of bounds' permanently bricked a healthy instance. Now structural only: instanceof RuntimeError plus a duck-typed name check (verified in this build's glue that abort() throws a genuine RuntimeError both pre- and post-runtime-init). Module.onAbort now latches the gate — the authoritative notification, previously ignored. E-15 the shim half: _pumpResume gates on terminal (catching wakes already queued at latch time) and resolveWait refuses on terminal WITHOUT consuming the entry, so a frame stays visibly parked rather than resuming inside a trapped module. E-16 the E-5 handler read the realm-global scheduler at dispatch instead of its installing module's; also frees the per-line buffer on the non-trap rethrow path. E-11 get_vec trusted the worker's res.length over the transferred arrays. Observed death shape: a 4 GiB std::vector threw an unhandled std::length_error that exited the editor's main loop. Now clamped, with the buffers freed on every failure path. Guardrails (replacing two deferred refactors: e2e→production-code injection and collapsing the four copies of the worker-lifecycle machinery) E-18 the source contract asserted comment-string counts — rewording failed CI while moving a guard outside its #ifdef passed. It now parses the #ifdef regions and asserts on code. service-stub-parity.ts pins what the four lifecycle copies must share: credit-window equality parsed from source, the finally-ack, boot deadlines, terminal-notice consumption. The transport numbers are now single-sourced from the worker. CI actually runs the gates: the web/standalone vitest suites (which had NEVER run in CI), the reducer, the source contract and the parity tool — with a NON_PLAYWRIGHT_GATES check so deleting a step re-fails the lint. E-22 the e2e occ stub's 60s boot watchdog, deleted in a66e109, is restored in the ngspice-stub shape with a wedgeNextBoot() repro hook. Every behavioral fix has red-then-green evidence (the reds were captured first). E-17 (a stale RUNNING cross-stamping the next run's generation under E-6's transport deferral) is DEFERRED with its analysis recorded — a real fix needs run identity on the bg frames. Test hygiene: the dwell lint now requires the mandated ": <why>" and all 47 bare markers carry their reason; three export-report dwells became modal-lease polls; exact-ledger assertions became relative deltas; the dead data-wx-dom-id branch, an unused fault hook and unused receipt plumbing are gone; abort scans, wx dialog drivers, the sim harness and the vitest FakeWorker are each one copy now. Bumps kicad and wxwidgets to their findings-group-e tips. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-27 12:27:12 +02:00
// Armed fault: the transport dies on the bg_run launch itself —
// AFTER the native side published its run generation, BEFORE any
// RUNNING transition could fire (the E-12 window). Retirement
// runs the production funnel (and its synthetic exit).
if (dieOnNextBgRunArmed && req.kind === 'command'
&& typeof req.cmd === 'string' && req.cmd.startsWith('bg_run')) {
dieOnNextBgRunArmed = false;
console.log('[TEST-NGSPICE] armed fault: transport death on bg_run');
const res = {
error: 'ngspice_service crashed: transport died on bg_run (armed fault)',
};
if (workerSlot) retireWorker(workerSlot, res.error);
publishRequestReceipt({
sequence: requestSequence,
kind: req.kind,
cmd: req.cmd,
error: res.error,
t: Date.now() - t0,
});
return res;
}
findings(E-1,E-2,E-3,E-6): ngspice service generations + credit-bounded event transport Adapted from codex/asyncify-execution-owner-core 3753320. Service side mirrors the occ-service shape (E-1 watchdogs, E-2 fail-all + boot-death fix — onerror now rejects the in-flight boot waiter instead of stranding it, E-3 onmessageerror terminal, Blob URL revoked, per-generation evtQueue cleared on retirement). E-6 transport bounds (worker hunks re-applied inside the emscripten-6 em-pthread else-branch — the codex file predates that split, so this is a re-application, not a cherry-pick): - batch cut at 512 lines / 1 MiB exact JSON-UTF-8 bytes, measured before a line is retained; a single line > 1 MiB flushes the accepted prefix then stops the event stream terminally (never retained); - posting gated by a 64-frame / 8 MiB unacked credit window; each frame carries { eventSequence, eventBytes } and is released only by an exact { sequence, bytes } ack; any mismatched ack is terminal; - the service mirrors the same 64-frame / 8 MiB bound on its pre-handler queue, acks after handing a frame to __ngspiceOnEvent, and retires the generation on invalid credit; { fatal } frames retire the worker. Tests: ngspice-service.test.ts (11, ported) — watchdogs, crash/bootError/ decode-fault settlement + recovery, out-of-order ids, sync postMessage throw, stale-generation event drops, fatal-frame retirement. tests/tools/ ngspice-worker-batch-unit.ts (node:vm over the production worker source; `npm run ngspice:worker-batch`) — bounded ordered chunks, byte-pressure flush, 100k-chunk credit storm, over-limit line, exact ack lease. e2e harness twin updated to speak the ack protocol (adds __ngspiceServiceTestHooks). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-19 15:48:41 +02:00
let slot: WorkerSlot;
try {
findings(E-1,E-2,E-3,E-6): ngspice service generations + credit-bounded event transport Adapted from codex/asyncify-execution-owner-core 3753320. Service side mirrors the occ-service shape (E-1 watchdogs, E-2 fail-all + boot-death fix — onerror now rejects the in-flight boot waiter instead of stranding it, E-3 onmessageerror terminal, Blob URL revoked, per-generation evtQueue cleared on retirement). E-6 transport bounds (worker hunks re-applied inside the emscripten-6 em-pthread else-branch — the codex file predates that split, so this is a re-application, not a cherry-pick): - batch cut at 512 lines / 1 MiB exact JSON-UTF-8 bytes, measured before a line is retained; a single line > 1 MiB flushes the accepted prefix then stops the event stream terminally (never retained); - posting gated by a 64-frame / 8 MiB unacked credit window; each frame carries { eventSequence, eventBytes } and is released only by an exact { sequence, bytes } ack; any mismatched ack is terminal; - the service mirrors the same 64-frame / 8 MiB bound on its pre-handler queue, acks after handing a frame to __ngspiceOnEvent, and retires the generation on invalid credit; { fatal } frames retire the worker. Tests: ngspice-service.test.ts (11, ported) — watchdogs, crash/bootError/ decode-fault settlement + recovery, out-of-order ids, sync postMessage throw, stale-generation event drops, fatal-frame retirement. tests/tools/ ngspice-worker-batch-unit.ts (node:vm over the production worker source; `npm run ngspice:worker-batch`) — bounded ordered chunks, byte-pressure flush, 100k-chunk credit storm, over-limit line, exact ack lease. e2e harness twin updated to speak the ack protocol (adds __ngspiceServiceTestHooks). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-19 15:48:41 +02:00
slot = await ensureWorker();
} catch (e) {
findings(E-1,E-2,E-3,E-6): ngspice service generations + credit-bounded event transport Adapted from codex/asyncify-execution-owner-core 3753320. Service side mirrors the occ-service shape (E-1 watchdogs, E-2 fail-all + boot-death fix — onerror now rejects the in-flight boot waiter instead of stranding it, E-3 onmessageerror terminal, Blob URL revoked, per-generation evtQueue cleared on retirement). E-6 transport bounds (worker hunks re-applied inside the emscripten-6 em-pthread else-branch — the codex file predates that split, so this is a re-application, not a cherry-pick): - batch cut at 512 lines / 1 MiB exact JSON-UTF-8 bytes, measured before a line is retained; a single line > 1 MiB flushes the accepted prefix then stops the event stream terminally (never retained); - posting gated by a 64-frame / 8 MiB unacked credit window; each frame carries { eventSequence, eventBytes } and is released only by an exact { sequence, bytes } ack; any mismatched ack is terminal; - the service mirrors the same 64-frame / 8 MiB bound on its pre-handler queue, acks after handing a frame to __ngspiceOnEvent, and retires the generation on invalid credit; { fatal } frames retire the worker. Tests: ngspice-service.test.ts (11, ported) — watchdogs, crash/bootError/ decode-fault settlement + recovery, out-of-order ids, sync postMessage throw, stale-generation event drops, fatal-frame retirement. tests/tools/ ngspice-worker-batch-unit.ts (node:vm over the production worker source; `npm run ngspice:worker-batch`) — bounded ordered chunks, byte-pressure flush, 100k-chunk credit storm, over-limit line, exact ack lease. e2e harness twin updated to speak the ack protocol (adds __ngspiceServiceTestHooks). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-19 15:48:41 +02:00
const res = { error: `ngspice_service unavailable: ${e}` };
publishRequestReceipt({
sequence: requestSequence,
kind: String(req.kind),
cmd: req.cmd,
name: req.name,
error: res.error,
t: Date.now() - t0,
});
return res;
}
findings(E-1,E-2,E-3,E-6): ngspice service generations + credit-bounded event transport Adapted from codex/asyncify-execution-owner-core 3753320. Service side mirrors the occ-service shape (E-1 watchdogs, E-2 fail-all + boot-death fix — onerror now rejects the in-flight boot waiter instead of stranding it, E-3 onmessageerror terminal, Blob URL revoked, per-generation evtQueue cleared on retirement). E-6 transport bounds (worker hunks re-applied inside the emscripten-6 em-pthread else-branch — the codex file predates that split, so this is a re-application, not a cherry-pick): - batch cut at 512 lines / 1 MiB exact JSON-UTF-8 bytes, measured before a line is retained; a single line > 1 MiB flushes the accepted prefix then stops the event stream terminally (never retained); - posting gated by a 64-frame / 8 MiB unacked credit window; each frame carries { eventSequence, eventBytes } and is released only by an exact { sequence, bytes } ack; any mismatched ack is terminal; - the service mirrors the same 64-frame / 8 MiB bound on its pre-handler queue, acks after handing a frame to __ngspiceOnEvent, and retires the generation on invalid credit; { fatal } frames retire the worker. Tests: ngspice-service.test.ts (11, ported) — watchdogs, crash/bootError/ decode-fault settlement + recovery, out-of-order ids, sync postMessage throw, stale-generation event drops, fatal-frame retirement. tests/tools/ ngspice-worker-batch-unit.ts (node:vm over the production worker source; `npm run ngspice:worker-batch`) — bounded ordered chunks, byte-pressure flush, 100k-chunk credit storm, over-limit line, exact ack lease. e2e harness twin updated to speak the ack protocol (adds __ngspiceServiceTestHooks). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-19 15:48:41 +02:00
const res: any = await post(slot, req);
findings(E-10..E-22): fix the defects a code review found in the E-1..E-9 work A review of the group-E fixes found 13 further defects; ten were introduced by those fixes, two pre-existed and were merely relocated, one is deferred. Services / transport E-10 retireWorker synthesized no bg/exit frame, so sharedspice's s_bgRunning mirror stayed latched true after a mid-run worker death: Run stayed disabled and the promised fresh-worker restart was unreachable for the whole session. Retirement now dispatches a synthetic controlled-exit straight to the installed handler (never through dispatchEvt — a fabricated frame must not touch the credit ledger). Driving the repro exposed two further defects, both fixed here: a replacement worker trapped on pre-init engine reads, and the rerun's cm_input_path/circ hit that uninitialized engine before KiCad's validate() re-init (the native flow assumes a crashed engine survives in-process — true for the dll, false for a dead worker). Reads now answer their empty shapes pre-init, writes lazy-init, and init is idempotent per worker engine. E-19 dispatchEvt acked only AFTER handler(evt) returned, and the sharedspice client deliberately rethrows non-trap errors — so each throw leaked one unit of the 64-frame credit window until the stream died with a misattributed "transport exceeded". The ack moves to a finally in both service copies; the throw still propagates (the trap machinery needs it). E-20 the oversize-line path promises to transfer the accepted prefix, but with the window full that flush only DEFERS, and stopEventStream wiped the deferred queue — losing the diagnostics that explain the failure. The terminal notice now carries them as pendingEvents; both hosts deliver them in order, unacked (the fatal frame is outside the credit protocol). E-21 the 30s prefetch deadline discarded every model already collected and reported nothing. A caller-owned progress sink ships the partials and the omission reaches the export report. (Awaiting the aborted collection was rejected: an in-flight source fetch is not abortable — E-4's original disease.) Plus a serving-candidate memo, so a .wrl ref served by its .step fallback stops re-probing the miss on every export. Scheduler E-14 _terminalizeNativeTrap classified by message substring, so any plain JS error QUOTING 'Aborted(' or 'out of bounds' permanently bricked a healthy instance. Now structural only: instanceof RuntimeError plus a duck-typed name check (verified in this build's glue that abort() throws a genuine RuntimeError both pre- and post-runtime-init). Module.onAbort now latches the gate — the authoritative notification, previously ignored. E-15 the shim half: _pumpResume gates on terminal (catching wakes already queued at latch time) and resolveWait refuses on terminal WITHOUT consuming the entry, so a frame stays visibly parked rather than resuming inside a trapped module. E-16 the E-5 handler read the realm-global scheduler at dispatch instead of its installing module's; also frees the per-line buffer on the non-trap rethrow path. E-11 get_vec trusted the worker's res.length over the transferred arrays. Observed death shape: a 4 GiB std::vector threw an unhandled std::length_error that exited the editor's main loop. Now clamped, with the buffers freed on every failure path. Guardrails (replacing two deferred refactors: e2e→production-code injection and collapsing the four copies of the worker-lifecycle machinery) E-18 the source contract asserted comment-string counts — rewording failed CI while moving a guard outside its #ifdef passed. It now parses the #ifdef regions and asserts on code. service-stub-parity.ts pins what the four lifecycle copies must share: credit-window equality parsed from source, the finally-ack, boot deadlines, terminal-notice consumption. The transport numbers are now single-sourced from the worker. CI actually runs the gates: the web/standalone vitest suites (which had NEVER run in CI), the reducer, the source contract and the parity tool — with a NON_PLAYWRIGHT_GATES check so deleting a step re-fails the lint. E-22 the e2e occ stub's 60s boot watchdog, deleted in a66e109, is restored in the ngspice-stub shape with a wedgeNextBoot() repro hook. Every behavioral fix has red-then-green evidence (the reds were captured first). E-17 (a stale RUNNING cross-stamping the next run's generation under E-6's transport deferral) is DEFERRED with its analysis recorded — a real fix needs run identity on the bg frames. Test hygiene: the dwell lint now requires the mandated ": <why>" and all 47 bare markers carry their reason; three export-report dwells became modal-lease polls; exact-ledger assertions became relative deltas; the dead data-wx-dom-id branch, an unused fault hook and unused receipt plumbing are gone; abort scans, wx dialog drivers, the sim harness and the vitest FakeWorker are each one copy now. Bumps kicad and wxwidgets to their findings-group-e tips. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-27 12:27:12 +02:00
// Armed fault: corrupt the next get_vec_info answer's LENGTH field
// only (the arrays stay small) — the corrupted-worker shape the
// sharedspice client must clamp against.
if (corruptNextGetVecArmed && req.kind === 'get_vec_info'
&& res && !res.error) {
corruptNextGetVecArmed = false;
console.log('[TEST-NGSPICE] armed fault: inflating get_vec_info length');
res.length = 1 << 29;
}
findings(E-1,E-2,E-3,E-6): ngspice service generations + credit-bounded event transport Adapted from codex/asyncify-execution-owner-core 3753320. Service side mirrors the occ-service shape (E-1 watchdogs, E-2 fail-all + boot-death fix — onerror now rejects the in-flight boot waiter instead of stranding it, E-3 onmessageerror terminal, Blob URL revoked, per-generation evtQueue cleared on retirement). E-6 transport bounds (worker hunks re-applied inside the emscripten-6 em-pthread else-branch — the codex file predates that split, so this is a re-application, not a cherry-pick): - batch cut at 512 lines / 1 MiB exact JSON-UTF-8 bytes, measured before a line is retained; a single line > 1 MiB flushes the accepted prefix then stops the event stream terminally (never retained); - posting gated by a 64-frame / 8 MiB unacked credit window; each frame carries { eventSequence, eventBytes } and is released only by an exact { sequence, bytes } ack; any mismatched ack is terminal; - the service mirrors the same 64-frame / 8 MiB bound on its pre-handler queue, acks after handing a frame to __ngspiceOnEvent, and retires the generation on invalid credit; { fatal } frames retire the worker. Tests: ngspice-service.test.ts (11, ported) — watchdogs, crash/bootError/ decode-fault settlement + recovery, out-of-order ids, sync postMessage throw, stale-generation event drops, fatal-frame retirement. tests/tools/ ngspice-worker-batch-unit.ts (node:vm over the production worker source; `npm run ngspice:worker-batch`) — bounded ordered chunks, byte-pressure flush, 100k-chunk credit storm, over-limit line, exact ack lease. e2e harness twin updated to speak the ack protocol (adds __ngspiceServiceTestHooks). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-19 15:48:41 +02:00
publishRequestReceipt({
sequence: requestSequence,
kind: req.kind,
cmd: req.cmd,
name: req.name,
ret: res?.ret,
error: res?.error,
length: res?.length,
t: Date.now() - t0,
});
return res;
};
findings(E-1,E-2,E-3,E-6): ngspice service generations + credit-bounded event transport Adapted from codex/asyncify-execution-owner-core 3753320. Service side mirrors the occ-service shape (E-1 watchdogs, E-2 fail-all + boot-death fix — onerror now rejects the in-flight boot waiter instead of stranding it, E-3 onmessageerror terminal, Blob URL revoked, per-generation evtQueue cleared on retirement). E-6 transport bounds (worker hunks re-applied inside the emscripten-6 em-pthread else-branch — the codex file predates that split, so this is a re-application, not a cherry-pick): - batch cut at 512 lines / 1 MiB exact JSON-UTF-8 bytes, measured before a line is retained; a single line > 1 MiB flushes the accepted prefix then stops the event stream terminally (never retained); - posting gated by a 64-frame / 8 MiB unacked credit window; each frame carries { eventSequence, eventBytes } and is released only by an exact { sequence, bytes } ack; any mismatched ack is terminal; - the service mirrors the same 64-frame / 8 MiB bound on its pre-handler queue, acks after handing a frame to __ngspiceOnEvent, and retires the generation on invalid credit; { fatal } frames retire the worker. Tests: ngspice-service.test.ts (11, ported) — watchdogs, crash/bootError/ decode-fault settlement + recovery, out-of-order ids, sync postMessage throw, stale-generation event drops, fatal-frame retirement. tests/tools/ ngspice-worker-batch-unit.ts (node:vm over the production worker source; `npm run ngspice:worker-batch`) — bounded ordered chunks, byte-pressure flush, 100k-chunk credit storm, over-limit line, exact ack lease. e2e harness twin updated to speak the ack protocol (adds __ngspiceServiceTestHooks). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-19 15:48:41 +02:00
const dispose = (): void => {
if (disposed) return;
disposed = true;
if (workerSlot) retireWorker(workerSlot, 'ngspice service was disposed');
findings(E-10..E-22): fix the defects a code review found in the E-1..E-9 work A review of the group-E fixes found 13 further defects; ten were introduced by those fixes, two pre-existed and were merely relocated, one is deferred. Services / transport E-10 retireWorker synthesized no bg/exit frame, so sharedspice's s_bgRunning mirror stayed latched true after a mid-run worker death: Run stayed disabled and the promised fresh-worker restart was unreachable for the whole session. Retirement now dispatches a synthetic controlled-exit straight to the installed handler (never through dispatchEvt — a fabricated frame must not touch the credit ledger). Driving the repro exposed two further defects, both fixed here: a replacement worker trapped on pre-init engine reads, and the rerun's cm_input_path/circ hit that uninitialized engine before KiCad's validate() re-init (the native flow assumes a crashed engine survives in-process — true for the dll, false for a dead worker). Reads now answer their empty shapes pre-init, writes lazy-init, and init is idempotent per worker engine. E-19 dispatchEvt acked only AFTER handler(evt) returned, and the sharedspice client deliberately rethrows non-trap errors — so each throw leaked one unit of the 64-frame credit window until the stream died with a misattributed "transport exceeded". The ack moves to a finally in both service copies; the throw still propagates (the trap machinery needs it). E-20 the oversize-line path promises to transfer the accepted prefix, but with the window full that flush only DEFERS, and stopEventStream wiped the deferred queue — losing the diagnostics that explain the failure. The terminal notice now carries them as pendingEvents; both hosts deliver them in order, unacked (the fatal frame is outside the credit protocol). E-21 the 30s prefetch deadline discarded every model already collected and reported nothing. A caller-owned progress sink ships the partials and the omission reaches the export report. (Awaiting the aborted collection was rejected: an in-flight source fetch is not abortable — E-4's original disease.) Plus a serving-candidate memo, so a .wrl ref served by its .step fallback stops re-probing the miss on every export. Scheduler E-14 _terminalizeNativeTrap classified by message substring, so any plain JS error QUOTING 'Aborted(' or 'out of bounds' permanently bricked a healthy instance. Now structural only: instanceof RuntimeError plus a duck-typed name check (verified in this build's glue that abort() throws a genuine RuntimeError both pre- and post-runtime-init). Module.onAbort now latches the gate — the authoritative notification, previously ignored. E-15 the shim half: _pumpResume gates on terminal (catching wakes already queued at latch time) and resolveWait refuses on terminal WITHOUT consuming the entry, so a frame stays visibly parked rather than resuming inside a trapped module. E-16 the E-5 handler read the realm-global scheduler at dispatch instead of its installing module's; also frees the per-line buffer on the non-trap rethrow path. E-11 get_vec trusted the worker's res.length over the transferred arrays. Observed death shape: a 4 GiB std::vector threw an unhandled std::length_error that exited the editor's main loop. Now clamped, with the buffers freed on every failure path. Guardrails (replacing two deferred refactors: e2e→production-code injection and collapsing the four copies of the worker-lifecycle machinery) E-18 the source contract asserted comment-string counts — rewording failed CI while moving a guard outside its #ifdef passed. It now parses the #ifdef regions and asserts on code. service-stub-parity.ts pins what the four lifecycle copies must share: credit-window equality parsed from source, the finally-ack, boot deadlines, terminal-notice consumption. The transport numbers are now single-sourced from the worker. CI actually runs the gates: the web/standalone vitest suites (which had NEVER run in CI), the reducer, the source contract and the parity tool — with a NON_PLAYWRIGHT_GATES check so deleting a step re-fails the lint. E-22 the e2e occ stub's 60s boot watchdog, deleted in a66e109, is restored in the ngspice-stub shape with a wedgeNextBoot() repro hook. Every behavioral fix has red-then-green evidence (the reds were captured first). E-17 (a stale RUNNING cross-stamping the next run's generation under E-6's transport deferral) is DEFERRED with its analysis recorded — a real fix needs run identity on the bg frames. Test hygiene: the dwell lint now requires the mandated ": <why>" and all 47 bare markers carry their reason; three export-report dwells became modal-lease polls; exact-ledger assertions became relative deltas; the dead data-wx-dom-id branch, an unused fault hook and unused receipt plumbing are gone; abort scans, wx dialog drivers, the sim harness and the vitest FakeWorker are each one copy now. Bumps kicad and wxwidgets to their findings-group-e tips. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-27 12:27:12 +02:00
requestReceipts.drain('ngspice request receipt canceled by teardown');
appliedReceipts.drain('ngspice applied-generation receipt canceled by teardown');
findings(E-1,E-2,E-3,E-6): ngspice service generations + credit-bounded event transport Adapted from codex/asyncify-execution-owner-core 3753320. Service side mirrors the occ-service shape (E-1 watchdogs, E-2 fail-all + boot-death fix — onerror now rejects the in-flight boot waiter instead of stranding it, E-3 onmessageerror terminal, Blob URL revoked, per-generation evtQueue cleared on retirement). E-6 transport bounds (worker hunks re-applied inside the emscripten-6 em-pthread else-branch — the codex file predates that split, so this is a re-application, not a cherry-pick): - batch cut at 512 lines / 1 MiB exact JSON-UTF-8 bytes, measured before a line is retained; a single line > 1 MiB flushes the accepted prefix then stops the event stream terminally (never retained); - posting gated by a 64-frame / 8 MiB unacked credit window; each frame carries { eventSequence, eventBytes } and is released only by an exact { sequence, bytes } ack; any mismatched ack is terminal; - the service mirrors the same 64-frame / 8 MiB bound on its pre-handler queue, acks after handing a frame to __ngspiceOnEvent, and retires the generation on invalid credit; { fatal } frames retire the worker. Tests: ngspice-service.test.ts (11, ported) — watchdogs, crash/bootError/ decode-fault settlement + recovery, out-of-order ids, sync postMessage throw, stale-generation event drops, fatal-frame retirement. tests/tools/ ngspice-worker-batch-unit.ts (node:vm over the production worker source; `npm run ngspice:worker-batch`) — bounded ordered chunks, byte-pressure flush, 100k-chunk credit storm, over-limit line, exact ack lease. e2e harness twin updated to speak the ack protocol (adds __ngspiceServiceTestHooks). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-19 15:48:41 +02:00
if ((globalThis as any).__pcbjamNgspiceFinalRefreshApplied
=== publishAppliedGeneration) {
(globalThis as any).__pcbjamNgspiceFinalRefreshApplied = previousAppliedHook;
}
};
(globalThis as any).__ngspiceServiceTestHooks = {
requestCheckpoint() {
return nextRequestSequence - 1;
},
waitForRequestAfter,
appliedGenerationCheckpoint() {
return appliedGenerations.length
? appliedGenerations[appliedGenerations.length - 1]!.generation
: 0;
},
waitForAppliedGenerationAfter,
dispose,
messageErrorDuringNextBoot() {
bootMessageErrorArmed = true;
},
messageErrorWhenPendingAtLeast(count: number) {
if (!Number.isSafeInteger(count) || count < 1)
throw new Error('pending threshold must be a positive safe integer');
runtimeMessageErrorThreshold = count;
},
findings(E-10..E-22): fix the defects a code review found in the E-1..E-9 work A review of the group-E fixes found 13 further defects; ten were introduced by those fixes, two pre-existed and were merely relocated, one is deferred. Services / transport E-10 retireWorker synthesized no bg/exit frame, so sharedspice's s_bgRunning mirror stayed latched true after a mid-run worker death: Run stayed disabled and the promised fresh-worker restart was unreachable for the whole session. Retirement now dispatches a synthetic controlled-exit straight to the installed handler (never through dispatchEvt — a fabricated frame must not touch the credit ledger). Driving the repro exposed two further defects, both fixed here: a replacement worker trapped on pre-init engine reads, and the rerun's cm_input_path/circ hit that uninitialized engine before KiCad's validate() re-init (the native flow assumes a crashed engine survives in-process — true for the dll, false for a dead worker). Reads now answer their empty shapes pre-init, writes lazy-init, and init is idempotent per worker engine. E-19 dispatchEvt acked only AFTER handler(evt) returned, and the sharedspice client deliberately rethrows non-trap errors — so each throw leaked one unit of the 64-frame credit window until the stream died with a misattributed "transport exceeded". The ack moves to a finally in both service copies; the throw still propagates (the trap machinery needs it). E-20 the oversize-line path promises to transfer the accepted prefix, but with the window full that flush only DEFERS, and stopEventStream wiped the deferred queue — losing the diagnostics that explain the failure. The terminal notice now carries them as pendingEvents; both hosts deliver them in order, unacked (the fatal frame is outside the credit protocol). E-21 the 30s prefetch deadline discarded every model already collected and reported nothing. A caller-owned progress sink ships the partials and the omission reaches the export report. (Awaiting the aborted collection was rejected: an in-flight source fetch is not abortable — E-4's original disease.) Plus a serving-candidate memo, so a .wrl ref served by its .step fallback stops re-probing the miss on every export. Scheduler E-14 _terminalizeNativeTrap classified by message substring, so any plain JS error QUOTING 'Aborted(' or 'out of bounds' permanently bricked a healthy instance. Now structural only: instanceof RuntimeError plus a duck-typed name check (verified in this build's glue that abort() throws a genuine RuntimeError both pre- and post-runtime-init). Module.onAbort now latches the gate — the authoritative notification, previously ignored. E-15 the shim half: _pumpResume gates on terminal (catching wakes already queued at latch time) and resolveWait refuses on terminal WITHOUT consuming the entry, so a frame stays visibly parked rather than resuming inside a trapped module. E-16 the E-5 handler read the realm-global scheduler at dispatch instead of its installing module's; also frees the per-line buffer on the non-trap rethrow path. E-11 get_vec trusted the worker's res.length over the transferred arrays. Observed death shape: a 4 GiB std::vector threw an unhandled std::length_error that exited the editor's main loop. Now clamped, with the buffers freed on every failure path. Guardrails (replacing two deferred refactors: e2e→production-code injection and collapsing the four copies of the worker-lifecycle machinery) E-18 the source contract asserted comment-string counts — rewording failed CI while moving a guard outside its #ifdef passed. It now parses the #ifdef regions and asserts on code. service-stub-parity.ts pins what the four lifecycle copies must share: credit-window equality parsed from source, the finally-ack, boot deadlines, terminal-notice consumption. The transport numbers are now single-sourced from the worker. CI actually runs the gates: the web/standalone vitest suites (which had NEVER run in CI), the reducer, the source contract and the parity tool — with a NON_PLAYWRIGHT_GATES check so deleting a step re-fails the lint. E-22 the e2e occ stub's 60s boot watchdog, deleted in a66e109, is restored in the ngspice-stub shape with a wedgeNextBoot() repro hook. Every behavioral fix has red-then-green evidence (the reds were captured first). E-17 (a stale RUNNING cross-stamping the next run's generation under E-6's transport deferral) is DEFERRED with its analysis recorded — a real fix needs run identity on the bg frames. Test hygiene: the dwell lint now requires the mandated ": <why>" and all 47 bare markers carry their reason; three export-report dwells became modal-lease polls; exact-ledger assertions became relative deltas; the dead data-wx-dom-id branch, an unused fault hook and unused receipt plumbing are gone; abort scans, wx dialog drivers, the sim harness and the vitest FakeWorker are each one copy now. Bumps kicad and wxwidgets to their findings-group-e tips. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-27 12:27:12 +02:00
/** Retire the active generation through the production funnel
* (the same retireWorker every watchdog/onerror path uses). */
forceRetire(reason: string): boolean {
const slot = workerSlot;
if (!slot) return false;
retireWorker(slot, String(reason || 'forced retirement'));
return true;
},
/** One-shot: the transport dies on the next bg_run launch (the
* generation retires through the production funnel before any
* RUNNING transition can fire). */
dieOnNextBgRun() {
dieOnNextBgRunArmed = true;
},
/** One-shot: the next successful get_vec_info answer reports a
* huge vector length while its arrays stay small. */
corruptNextGetVec() {
corruptNextGetVecArmed = true;
},
findings(E-1,E-2,E-3,E-6): ngspice service generations + credit-bounded event transport Adapted from codex/asyncify-execution-owner-core 3753320. Service side mirrors the occ-service shape (E-1 watchdogs, E-2 fail-all + boot-death fix — onerror now rejects the in-flight boot waiter instead of stranding it, E-3 onmessageerror terminal, Blob URL revoked, per-generation evtQueue cleared on retirement). E-6 transport bounds (worker hunks re-applied inside the emscripten-6 em-pthread else-branch — the codex file predates that split, so this is a re-application, not a cherry-pick): - batch cut at 512 lines / 1 MiB exact JSON-UTF-8 bytes, measured before a line is retained; a single line > 1 MiB flushes the accepted prefix then stops the event stream terminally (never retained); - posting gated by a 64-frame / 8 MiB unacked credit window; each frame carries { eventSequence, eventBytes } and is released only by an exact { sequence, bytes } ack; any mismatched ack is terminal; - the service mirrors the same 64-frame / 8 MiB bound on its pre-handler queue, acks after handing a frame to __ngspiceOnEvent, and retires the generation on invalid credit; { fatal } frames retire the worker. Tests: ngspice-service.test.ts (11, ported) — watchdogs, crash/bootError/ decode-fault settlement + recovery, out-of-order ids, sync postMessage throw, stale-generation event drops, fatal-frame retirement. tests/tools/ ngspice-worker-batch-unit.ts (node:vm over the production worker source; `npm run ngspice:worker-batch`) — bounded ordered chunks, byte-pressure flush, 100k-chunk credit storm, over-limit line, exact ack lease. e2e harness twin updated to speak the ack protocol (adds __ngspiceServiceTestHooks). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-19 15:48:41 +02:00
snapshot() {
return {
activeGeneration: workerSlot?.generation ?? null,
pending: pending.size,
maxPending,
retiredGenerations: [...retiredGenerations],
bootFaultArmed: bootMessageErrorArmed,
runtimeFaultArmed: runtimeMessageErrorThreshold !== null,
lastRequestSequence: nextRequestSequence - 1,
findings(E-10..E-22): fix the defects a code review found in the E-1..E-9 work A review of the group-E fixes found 13 further defects; ten were introduced by those fixes, two pre-existed and were merely relocated, one is deferred. Services / transport E-10 retireWorker synthesized no bg/exit frame, so sharedspice's s_bgRunning mirror stayed latched true after a mid-run worker death: Run stayed disabled and the promised fresh-worker restart was unreachable for the whole session. Retirement now dispatches a synthetic controlled-exit straight to the installed handler (never through dispatchEvt — a fabricated frame must not touch the credit ledger). Driving the repro exposed two further defects, both fixed here: a replacement worker trapped on pre-init engine reads, and the rerun's cm_input_path/circ hit that uninitialized engine before KiCad's validate() re-init (the native flow assumes a crashed engine survives in-process — true for the dll, false for a dead worker). Reads now answer their empty shapes pre-init, writes lazy-init, and init is idempotent per worker engine. E-19 dispatchEvt acked only AFTER handler(evt) returned, and the sharedspice client deliberately rethrows non-trap errors — so each throw leaked one unit of the 64-frame credit window until the stream died with a misattributed "transport exceeded". The ack moves to a finally in both service copies; the throw still propagates (the trap machinery needs it). E-20 the oversize-line path promises to transfer the accepted prefix, but with the window full that flush only DEFERS, and stopEventStream wiped the deferred queue — losing the diagnostics that explain the failure. The terminal notice now carries them as pendingEvents; both hosts deliver them in order, unacked (the fatal frame is outside the credit protocol). E-21 the 30s prefetch deadline discarded every model already collected and reported nothing. A caller-owned progress sink ships the partials and the omission reaches the export report. (Awaiting the aborted collection was rejected: an in-flight source fetch is not abortable — E-4's original disease.) Plus a serving-candidate memo, so a .wrl ref served by its .step fallback stops re-probing the miss on every export. Scheduler E-14 _terminalizeNativeTrap classified by message substring, so any plain JS error QUOTING 'Aborted(' or 'out of bounds' permanently bricked a healthy instance. Now structural only: instanceof RuntimeError plus a duck-typed name check (verified in this build's glue that abort() throws a genuine RuntimeError both pre- and post-runtime-init). Module.onAbort now latches the gate — the authoritative notification, previously ignored. E-15 the shim half: _pumpResume gates on terminal (catching wakes already queued at latch time) and resolveWait refuses on terminal WITHOUT consuming the entry, so a frame stays visibly parked rather than resuming inside a trapped module. E-16 the E-5 handler read the realm-global scheduler at dispatch instead of its installing module's; also frees the per-line buffer on the non-trap rethrow path. E-11 get_vec trusted the worker's res.length over the transferred arrays. Observed death shape: a 4 GiB std::vector threw an unhandled std::length_error that exited the editor's main loop. Now clamped, with the buffers freed on every failure path. Guardrails (replacing two deferred refactors: e2e→production-code injection and collapsing the four copies of the worker-lifecycle machinery) E-18 the source contract asserted comment-string counts — rewording failed CI while moving a guard outside its #ifdef passed. It now parses the #ifdef regions and asserts on code. service-stub-parity.ts pins what the four lifecycle copies must share: credit-window equality parsed from source, the finally-ack, boot deadlines, terminal-notice consumption. The transport numbers are now single-sourced from the worker. CI actually runs the gates: the web/standalone vitest suites (which had NEVER run in CI), the reducer, the source contract and the parity tool — with a NON_PLAYWRIGHT_GATES check so deleting a step re-fails the lint. E-22 the e2e occ stub's 60s boot watchdog, deleted in a66e109, is restored in the ngspice-stub shape with a wedgeNextBoot() repro hook. Every behavioral fix has red-then-green evidence (the reds were captured first). E-17 (a stale RUNNING cross-stamping the next run's generation under E-6's transport deferral) is DEFERRED with its analysis recorded — a real fix needs run identity on the bg frames. Test hygiene: the dwell lint now requires the mandated ": <why>" and all 47 bare markers carry their reason; three export-report dwells became modal-lease polls; exact-ledger assertions became relative deltas; the dead data-wx-dom-id branch, an unused fault hook and unused receipt plumbing are gone; abort scans, wx dialog drivers, the sim harness and the vitest FakeWorker are each one copy now. Bumps kicad and wxwidgets to their findings-group-e tips. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-27 12:27:12 +02:00
requestReceiptWaiters: requestReceipts.size(),
findings(E-1,E-2,E-3,E-6): ngspice service generations + credit-bounded event transport Adapted from codex/asyncify-execution-owner-core 3753320. Service side mirrors the occ-service shape (E-1 watchdogs, E-2 fail-all + boot-death fix — onerror now rejects the in-flight boot waiter instead of stranding it, E-3 onmessageerror terminal, Blob URL revoked, per-generation evtQueue cleared on retirement). E-6 transport bounds (worker hunks re-applied inside the emscripten-6 em-pthread else-branch — the codex file predates that split, so this is a re-application, not a cherry-pick): - batch cut at 512 lines / 1 MiB exact JSON-UTF-8 bytes, measured before a line is retained; a single line > 1 MiB flushes the accepted prefix then stops the event stream terminally (never retained); - posting gated by a 64-frame / 8 MiB unacked credit window; each frame carries { eventSequence, eventBytes } and is released only by an exact { sequence, bytes } ack; any mismatched ack is terminal; - the service mirrors the same 64-frame / 8 MiB bound on its pre-handler queue, acks after handing a frame to __ngspiceOnEvent, and retires the generation on invalid credit; { fatal } frames retire the worker. Tests: ngspice-service.test.ts (11, ported) — watchdogs, crash/bootError/ decode-fault settlement + recovery, out-of-order ids, sync postMessage throw, stale-generation event drops, fatal-frame retirement. tests/tools/ ngspice-worker-batch-unit.ts (node:vm over the production worker source; `npm run ngspice:worker-batch`) — bounded ordered chunks, byte-pressure flush, 100k-chunk credit storm, over-limit line, exact ack lease. e2e harness twin updated to speak the ack protocol (adds __ngspiceServiceTestHooks). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-19 15:48:41 +02:00
appliedGenerations: appliedGenerations.map((entry) => entry.generation),
findings(E-10..E-22): fix the defects a code review found in the E-1..E-9 work A review of the group-E fixes found 13 further defects; ten were introduced by those fixes, two pre-existed and were merely relocated, one is deferred. Services / transport E-10 retireWorker synthesized no bg/exit frame, so sharedspice's s_bgRunning mirror stayed latched true after a mid-run worker death: Run stayed disabled and the promised fresh-worker restart was unreachable for the whole session. Retirement now dispatches a synthetic controlled-exit straight to the installed handler (never through dispatchEvt — a fabricated frame must not touch the credit ledger). Driving the repro exposed two further defects, both fixed here: a replacement worker trapped on pre-init engine reads, and the rerun's cm_input_path/circ hit that uninitialized engine before KiCad's validate() re-init (the native flow assumes a crashed engine survives in-process — true for the dll, false for a dead worker). Reads now answer their empty shapes pre-init, writes lazy-init, and init is idempotent per worker engine. E-19 dispatchEvt acked only AFTER handler(evt) returned, and the sharedspice client deliberately rethrows non-trap errors — so each throw leaked one unit of the 64-frame credit window until the stream died with a misattributed "transport exceeded". The ack moves to a finally in both service copies; the throw still propagates (the trap machinery needs it). E-20 the oversize-line path promises to transfer the accepted prefix, but with the window full that flush only DEFERS, and stopEventStream wiped the deferred queue — losing the diagnostics that explain the failure. The terminal notice now carries them as pendingEvents; both hosts deliver them in order, unacked (the fatal frame is outside the credit protocol). E-21 the 30s prefetch deadline discarded every model already collected and reported nothing. A caller-owned progress sink ships the partials and the omission reaches the export report. (Awaiting the aborted collection was rejected: an in-flight source fetch is not abortable — E-4's original disease.) Plus a serving-candidate memo, so a .wrl ref served by its .step fallback stops re-probing the miss on every export. Scheduler E-14 _terminalizeNativeTrap classified by message substring, so any plain JS error QUOTING 'Aborted(' or 'out of bounds' permanently bricked a healthy instance. Now structural only: instanceof RuntimeError plus a duck-typed name check (verified in this build's glue that abort() throws a genuine RuntimeError both pre- and post-runtime-init). Module.onAbort now latches the gate — the authoritative notification, previously ignored. E-15 the shim half: _pumpResume gates on terminal (catching wakes already queued at latch time) and resolveWait refuses on terminal WITHOUT consuming the entry, so a frame stays visibly parked rather than resuming inside a trapped module. E-16 the E-5 handler read the realm-global scheduler at dispatch instead of its installing module's; also frees the per-line buffer on the non-trap rethrow path. E-11 get_vec trusted the worker's res.length over the transferred arrays. Observed death shape: a 4 GiB std::vector threw an unhandled std::length_error that exited the editor's main loop. Now clamped, with the buffers freed on every failure path. Guardrails (replacing two deferred refactors: e2e→production-code injection and collapsing the four copies of the worker-lifecycle machinery) E-18 the source contract asserted comment-string counts — rewording failed CI while moving a guard outside its #ifdef passed. It now parses the #ifdef regions and asserts on code. service-stub-parity.ts pins what the four lifecycle copies must share: credit-window equality parsed from source, the finally-ack, boot deadlines, terminal-notice consumption. The transport numbers are now single-sourced from the worker. CI actually runs the gates: the web/standalone vitest suites (which had NEVER run in CI), the reducer, the source contract and the parity tool — with a NON_PLAYWRIGHT_GATES check so deleting a step re-fails the lint. E-22 the e2e occ stub's 60s boot watchdog, deleted in a66e109, is restored in the ngspice-stub shape with a wedgeNextBoot() repro hook. Every behavioral fix has red-then-green evidence (the reds were captured first). E-17 (a stale RUNNING cross-stamping the next run's generation under E-6's transport deferral) is DEFERRED with its analysis recorded — a real fix needs run identity on the bg frames. Test hygiene: the dwell lint now requires the mandated ": <why>" and all 47 bare markers carry their reason; three export-report dwells became modal-lease polls; exact-ledger assertions became relative deltas; the dead data-wx-dom-id branch, an unused fault hook and unused receipt plumbing are gone; abort scans, wx dialog drivers, the sim harness and the vitest FakeWorker are each one copy now. Bumps kicad and wxwidgets to their findings-group-e tips. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-27 12:27:12 +02:00
appliedGenerationWaiters: appliedReceipts.size(),
findings(E-1,E-2,E-3,E-6): ngspice service generations + credit-bounded event transport Adapted from codex/asyncify-execution-owner-core 3753320. Service side mirrors the occ-service shape (E-1 watchdogs, E-2 fail-all + boot-death fix — onerror now rejects the in-flight boot waiter instead of stranding it, E-3 onmessageerror terminal, Blob URL revoked, per-generation evtQueue cleared on retirement). E-6 transport bounds (worker hunks re-applied inside the emscripten-6 em-pthread else-branch — the codex file predates that split, so this is a re-application, not a cherry-pick): - batch cut at 512 lines / 1 MiB exact JSON-UTF-8 bytes, measured before a line is retained; a single line > 1 MiB flushes the accepted prefix then stops the event stream terminally (never retained); - posting gated by a 64-frame / 8 MiB unacked credit window; each frame carries { eventSequence, eventBytes } and is released only by an exact { sequence, bytes } ack; any mismatched ack is terminal; - the service mirrors the same 64-frame / 8 MiB bound on its pre-handler queue, acks after handing a frame to __ngspiceOnEvent, and retires the generation on invalid credit; { fatal } frames retire the worker. Tests: ngspice-service.test.ts (11, ported) — watchdogs, crash/bootError/ decode-fault settlement + recovery, out-of-order ids, sync postMessage throw, stale-generation event drops, fatal-frame retirement. tests/tools/ ngspice-worker-batch-unit.ts (node:vm over the production worker source; `npm run ngspice:worker-batch`) — bounded ordered chunks, byte-pressure flush, 100k-chunk credit storm, over-limit line, exact ack lease. e2e harness twin updated to speak the ack protocol (adds __ngspiceServiceTestHooks). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-19 15:48:41 +02:00
disposed,
};
},
};
(globalThis as any).ngspiceService = { request };
findings(E-1,E-2,E-3,E-6): ngspice service generations + credit-bounded event transport Adapted from codex/asyncify-execution-owner-core 3753320. Service side mirrors the occ-service shape (E-1 watchdogs, E-2 fail-all + boot-death fix — onerror now rejects the in-flight boot waiter instead of stranding it, E-3 onmessageerror terminal, Blob URL revoked, per-generation evtQueue cleared on retirement). E-6 transport bounds (worker hunks re-applied inside the emscripten-6 em-pthread else-branch — the codex file predates that split, so this is a re-application, not a cherry-pick): - batch cut at 512 lines / 1 MiB exact JSON-UTF-8 bytes, measured before a line is retained; a single line > 1 MiB flushes the accepted prefix then stops the event stream terminally (never retained); - posting gated by a 64-frame / 8 MiB unacked credit window; each frame carries { eventSequence, eventBytes } and is released only by an exact { sequence, bytes } ack; any mismatched ack is terminal; - the service mirrors the same 64-frame / 8 MiB bound on its pre-handler queue, acks after handing a frame to __ngspiceOnEvent, and retires the generation on invalid credit; { fatal } frames retire the worker. Tests: ngspice-service.test.ts (11, ported) — watchdogs, crash/bootError/ decode-fault settlement + recovery, out-of-order ids, sync postMessage throw, stale-generation event drops, fatal-frame retirement. tests/tools/ ngspice-worker-batch-unit.ts (node:vm over the production worker source; `npm run ngspice:worker-batch`) — bounded ordered chunks, byte-pressure flush, 100k-chunk credit storm, over-limit line, exact ack lease. e2e harness twin updated to speak the ack protocol (adds __ngspiceServiceTestHooks). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-19 15:48:41 +02:00
window.addEventListener('pagehide', dispose, { once: true });
}, { workerSrc: NGSPICE_WORKER_SRC, bootTimeoutMs, responseTimeoutMs });
}