/** * The core-free half of the analysis engine (W2e): the AnalysisEngine * interface, plan and report types, classic's engine, the run-on-plan helpers * and the grid stepper. engine.ts adds the core engine and the planner that * can choose it, and re-exports everything here. * * WHY a separate file: the runners that cannot use the core yet (SPICE * `.tran`, forced classic) import only this file, so the `./spice` entry does * not pull the core compiler, the wasm loader and CoreEngine into its module * graph. The planner stays synchronous (the runners are), which rules out a * lazy dynamic import of CoreEngine; a static split is the way to keep the * graph small. See engine.ts for the design (D14, D15). */ import { SimEngine } from "../sim/engine/sim-engine.js"; import type { SimCircuit, SimFailure } from "../sim/engine/sim-engine.js"; import type { Net } from "../sim/engine/graph.js"; import { AnalysisConvergenceError } from "./checked-step.js"; /** What a spec or a caller asks for. `auto` is the default: the core when the whole run supports it, else classic. */ export type AnalysisEngineChoice = "auto" | "classic" | "core"; /** The engine a run actually used. */ export type AnalysisEngineKind = "classic" | "core"; /** `choice` plus the knob the dual-engine tests turn. */ export interface AnalysisEngineOptions { /** Overrides `spec.engine`. Unset: the spec's, else `auto`. */ engine?: AnalysisEngineChoice; /** * Which kinds `auto` and `core` may route to the core. Defaults to * CORE_SUPPORTED_KINDS, the kinds that cleared the accuracy gates. Passing * CORE_IMPLEMENTED_KINDS also routes the ones that have not; for tests only. */ coreSupportedKinds?: ReadonlySet; } /** * The restart hook the runners' hooks types share. A run that was on the core * and was declined by the Rust load part-way through, or that the core failed * or stopped converging in, is replayed whole on classic; `onPoint` would then count the same points twice, so a progress UI * is told first. */ export interface AnalysisRestartHook { /** * Called exactly once, before the replay on classic starts and before any * `onPoint` of the replay, with the reasons the core declined. After it, the * point counter starts again from 0. Not called when the run never restarts * (the common core rejection is caught by the planner's first-point load, * before any point runs). */ onRestart?: (reasons: readonly string[]) => void; } /** What every analysis result carries about its engine. */ export interface AnalysisEngineReport { engine: AnalysisEngineKind; /** * Why the run is on classic when it was not asked to be: the vocabulary of * HeadlessRunner.engineFallbackReasons (`unsupported-kind:`, * `malformed::`, `topology:`, `core-not-initialized`, * `core-poisoned`) plus the analysis-specific ones a runner forces * (`ac-sweep-needs-signal-gen-on-core`, `tran-needs-carry-seed`). Sorted * and deduplicated; empty when the run is on the core or classic was asked for. */ fallbackReasons: string[]; } export interface AdvanceOptions { /** * Classic's step for this advance. Classic takes exactly one step of this * size (so the float sequence of the runners' old loops is reproduced bit * for bit); the core ignores it and advances to `t`. Unset: `t - simTime`. */ stepS?: number; /** Names the phase in a rejected step's message ("runSweep point 3 (x=5)"). */ context?: string; } /** The slice of an engine the transient analyses read. */ export interface AnalysisEngine { readonly kind: AnalysisEngineKind; /** Simulation time (s); 0 after coldLoad. */ readonly simTime: number; /** The loaded topology's nets (deterministic ids, identical on both engines). */ readonly nets: Net[]; /** Load `circuit` with every element state zeroed and the clock at 0. */ coldLoad(circuit: SimCircuit): void; /** * Advance to `t`. Throws AnalysisConvergenceError if the solve did not * converge; the readouts are then the last trusted instant, never a failed * iterate. Throws a plain Error if the engine itself threw. */ advanceTo(t: number, options?: AdvanceOptions): void; /** Net id -> volts. A live record on both engines: copy it to keep it. */ getNetV(): Record; getFailures(): Record; /** Release engine resources (the core's wasm handle). Safe to call twice. */ dispose(): void; } /** * The core could not run a circuit after every point had compiled: its Rust * load declined it (a topology it does not take, a load-time livelock or solve * failure), or the core itself failed (a trap, a poisoned instance, a load code * for a malformed program). `runOnPlan` turns it into a whole-run rerun on * classic under `auto`. A non-converged advance is NOT this: it throws * AnalysisCoreStalledError, below. */ export declare class AnalysisCoreRejectedError extends Error { readonly reasons: readonly string[]; constructor(reasons: readonly string[]); } /** * A core advance that did not converge (a failed solve or a livelock part-way * through a point). It IS an AnalysisConvergenceError, so an explicit `core` * run reports it exactly as it always did. Under `auto`, `runOnPlan` replays * the whole run on classic and records `reasons` (ruling R116): the core * giving up on a circuit does not show that classic would, and if classic * cannot solve it either, classic's own AnalysisConvergenceError is the * answer. */ export declare class AnalysisCoreStalledError extends AnalysisConvergenceError { readonly reasons: readonly string[]; constructor(message: string, reasons: readonly string[]); } /** * A classic engine over a SimEngine the caller has already prepared (SPICE * `.tran` seeds its operating point and element state before stepping, so it * cannot coldLoad). Steps are unchecked, as `.tran` always was. */ export declare function adoptClassicEngine(sim: SimEngine): AnalysisEngine; /** A decided engine for one run. */ export interface AnalysisEnginePlan extends AnalysisEngineReport { /** Construct the run's engine: one for the whole run, coldLoaded per point. */ open(): AnalysisEngine; /** * The same run on classic after the core declined it in its Rust load, or * null when the caller asked for the core explicitly (no silent fallback). */ downgrade(reasons: readonly string[]): AnalysisEnginePlan | null; } export interface PlanOptions extends AnalysisEngineOptions { /** * Reasons a runner knows the core cannot take its analysis yet, so it is * classic regardless of the circuit. Deleting a runner's reason is the whole * switch-over. Ignored for an explicit `classic` (nothing fell back). */ forcedClassicReasons?: readonly string[]; } export declare function sortedUnique(reasons: Iterable): string[]; export declare function classicPlan(fallbackReasons: string[]): AnalysisEnginePlan; /** * The plan for a runner whose analysis cannot use the core yet: classic with * the runner's reasons, decided without touching the core at all. Same rules * as planAnalysisEngine on a forced run: `classic` records nothing, `auto` * records the reasons, `core` is an Error naming them. */ export declare function planForcedClassic(choice: AnalysisEngineChoice | undefined, reasons: readonly string[]): AnalysisEnginePlan; export declare function planOptionsFor(specEngine: AnalysisEngineChoice | undefined, hooks: AnalysisEngineOptions | undefined, forcedClassicReasons?: readonly string[]): PlanOptions; /** * Run `body` on the plan's engine, then dispose it. A core rejection from the * Rust load (the one thing compiling cannot see), a core failure (a trap, a * poisoned instance) or a core advance that does not converge discards the * partial run and reruns the WHOLE body on classic, so a result never mixes * engines; `body` receives the report current at each attempt and must put it * on its result. Under an explicit `core` request nothing is rerun: a * rejection is an Error naming the reasons, and a failed advance is the * AnalysisConvergenceError it throws. * `onRestart(reasons)` is called once before the replay (see AnalysisRestartHook). */ export declare function runOnPlan(plan: AnalysisEnginePlan, body: (engine: AnalysisEngine, report: AnalysisEngineReport) => R, onRestart?: (reasons: readonly string[]) => void): R; /** `runOnPlan` for the async runners (they yield a macrotask between points). */ export declare function runOnPlanAsync(plan: AnalysisEnginePlan, body: (engine: AnalysisEngine, report: AnalysisEngineReport) => Promise, onRestart?: (reasons: readonly string[]) => void): Promise; /** * `engine.coldLoad(circuit)` with a failure wrapped in `what` (the runners' * "runSweep: coldLoad failed at point 3 (x=5)" prefix), except the core's * rejection, which must reach `runOnPlan` untouched to trigger the rerun. */ export declare function coldLoadPoint(engine: AnalysisEngine, circuit: SimCircuit, what: string): void; /** * Step `engine` to `endTime` on a fixed grid of `h`, the last step clamped to * land on `endTime`; `onStep(t)` runs after each step with the engine's time. * This is the loop every runner used to write by hand, once: the float * sequence (`h = min(step, remaining)`, time read back from the engine) is * unchanged, so classic reproduces the old runs bit for bit, and the core is * advanced to `t + h`, never coarser than classic's step (D15). */ export declare function stepToGrid(engine: AnalysisEngine, endTime: number, h: number, context: string, onStep?: (t: number) => void): void;