One Engine, Four Front-Ends, and Why Every Solve Runs in Its Own Process
How nyx split apart its original monolith into 15 strictly layered crates, why Monty beat Starlark and Rhai for scripts, and why small solves pay a 70 ms worker-process tax.
Early versions of nyx did not look like a general multiphysics workbench. The project started around stimulated Brillouin scattering in photonic waveguides, with chip recipes baked straight into femcore alongside meshing, sparse linear algebra, HDF5 export, and an experimental AI MCP workspace sitting in a separate Cargo graph (ROUGH_SPOTS.md).
When the project pivoted to photonics, phononics, piezoelectrics, thermal conduction, and superconducting quantum circuits, keeping specialised ring-chip templates inside the FEM core stopped making sense. ADR 0024 removed the Brillouin kernels and hardcoded chips altogether — freezing the last state at git tag sbs-last — and the restructure broke the codebase into 15 workspace crates behind strict compiler-enforced layering (crates/nyx-engine/tests/layering.rs).
Splitting the Monolith and Isolating geom
Before the restructure, nyx-store pulled in the entire FEM crate just to inspect two fields of a JSON result (R15), Project state lived inside the GUI crate where headless scripts and MCP tools could not open a project file (R16), and the MCP server maintained its own Cargo.lock (R17). Separating geometry (geom), FEM core (femcore), typed physics (nyx-physics plus nyx-dfs/nyx-sms/nyx-ems), post-processing (nyx-analysis), pure-Rust storage (nyx-storage), and orchestration (nyx-engine) left one engine boundary with 118 versioned commands.
One architectural boundary is guarded with unusual strictness: geom must never depend on femcore, verified on every CI run (geom_depends_on_serde_and_optional_rayon_only). Because parametric expressions must evaluate identically in geometry construction and in post-processing field formulas, nyx intentionally carries two parallel expression evaluators — geom::expr and femcore::expr (R06) — and locks them together with crates/femcore/tests/expr_grammar.rs, which feeds one shared corpus through both engines and checks identical acceptance, refusal, and bit-for-bit values.
Every tracked file in the repository must also match a row in docs/architecture/CODE_TO_ADR.md, checked mechanically by check_code_to_adr.sh so new modules cannot drift into the tree without an architectural owner.
Why Monty Beat Starlark and Rhai for *.nys Scripts
With a single command catalogue driving the desktop window, headless CLI, and MCP server (ADR 0057), scripted parameter studies still needed loops and conditionals that flat JSON command lists could not express (ADR 0089). Re-introducing CPython would have violated ADR 0013 (zero Python runtime required to build or use nyx). The scripting spike (ADR 0093) wired all 118 commands into three pure-Rust embeddable candidates:
- Starlark: Cleanly sandboxed, yet adding
starlarkturned onserde_json’sarbitrary_precisionCargo feature across the entire workspace. That single global feature flag immediately broke nine tests involving untagged enums in saved project formats. Starlark also lackedwhileloops,try/except, and standard Python string formatting. - Rhai: Fast and bounded in-process, with zero Cargo feature fallout — yet every physicist and coding agent would have had to learn a bespoke Rust-adjacent scripting syntax.
- Monty: Executes standard Python syntax in pure Rust (
nyx-script) with CPython-style tracebacks and built-in static stub checking (nyx.pyigenerated directly fromEngine::catalog()). Its catch was memory safety: an unbounded loop likewhile Trueor[0] * 10**12aborts the process hosting the interpreter.
Running Monty inside an isolated child binary (nyx-script-worker) resolved that catch, while pinning determinism rules strictly: no open, os, pathlib, or network modules, wall-clock calls refused, random locked to seed 0, and every path-like argument checked inside nyx_engine::path_guard against the workspace root (explicitly tested across all catalogue arguments by tests/path_guard.rs). For AI hosts (ADR 0094), nyx-mcp was simultaneously collapsed from over a hundred per-command tools into four script-session tools (script_run, script_wait, script_check, api).
Why Small Solves Pay a 70 ms Child-Process Tax
Executing a 3D finite-element cavity can demand 10.7 GB to 22 GB of RAM (ADR 0086), and sparse direct factorisations inside faer cannot be cleanly interrupted mid-call. Running solves directly inside the GUI window or MCP daemon created two unacceptable failures: an out-of-memory panic crashed the whole interface along with unsaved edits, and clicking Stop waited up to several minutes for a multi-gigabyte factorisation to finish before checking cancellation.
nyx therefore isolates every solve_run job in a child worker process by default (controlled via isolate), trading ~0.07 s startup latency on trivial 0.01 s 2D runs (or ~7% overhead at 39,000 unknowns) for three concrete properties:
- Sub-1.5 s cancellation: Polling cancel every 40 ms and killing the child process stops even a mid-flight 3D LU factorisation cleanly, and worker aborts fail only that job (
tests/worker_crash.rs). - Real process-level memory telemetry: Measuring
/procwatermarks on Linux or process peak working set on Windows belongs strictly to one isolated solve rather than a long-lived GUI session. - Power-law admission from local history (
estimate.rs): Completed worker runs append cell counts, peak memory, and wall time tosolve_history.jsonl. Once two sizes exist for a physics chain and dimension, new jobs fit memory against cell count as a power law (3D cavities grow roughly as DOFs to the power 1.5, making naive linear bytes-per-unknown guesses dangerously optimistic). Jobs projected above 90% of physical RAM are rejected unlessforce = trueis set, and jobs exceeding currently free RAM pause in the queue until running work clears.1.One subtle Windows hazard found during profiling: piped background processes without a console window can be demoted to efficiency cores by OS power throttling unless the worker opts out explicitly (nyx_engine::process::opt_out_of_power_throttling).
One honest boundary remains open in worker isolation (ADR 0086): two-grid and three-grid convergence_check runs still execute sequentially inside a single worker or process rather than spawning per-mesh admission gates, and distributed workers across separate cluster machines are not implemented.