Skip to content

engine: split into crates and drop simlin-serve - #1098

Merged
bpowers merged 27 commits into
mainfrom
engine-crates
Nov 25, 2026
Merged

bpowers merged 27 commits into
mainfrom
engine-crates

Conversation

@bpowers

@bpowers bpowers commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Fixes #1094
Fixes #1097
Fixes #473
Fixes #478
Fixes #828
Fixes #916
Fixes #918
Fixes #945
Fixes #1046

A one-line engine edit cost about 13.5 s before a unit test could run: rustc rebuilds a whole crate, and the engine was one crate of 606k lines.
This splits it into crates so an edit rebuilds only what depends on it, removes simlin-serve, and fixes two editor bugs that lost people's work on app.simlin.com.

The crates

Crate Holds Built on
simlin-base data model, identifiers, AST, builtins, results
simlin-diagram SVG, PNG and scene drawing base
simlin-formats XMILE, MDL, VDF, JSON, protobuf base, diagram
simlin-edit patches, gestures, equation views base, diagram, formats
simlin-engine compiler, salsa database, LTM, units, VM base
simlin-wasmgen the per-model wasm backend base, engine
simlin-layout diagram layout base, diagram, edit, engine
simlin-save-check does a save keep what the model means base, formats, engine
simlin-tools the agent tool surface base, formats, edit, engine, layout, save-check
simlin-tests integration tests, benchmarks, instruments everything; nothing depends on it

There is no facade crate, and none of these re-exports another: callers name the crate an item is in.

One-line edit, unit tests built Before After
layout 13.7 s 0.8 s
MDL parser 13.4 s 2.2 s
compiler (db/analysis) 13.4 s 3.3 s
agent tools 13.4 s 1.6 s
wasm backend 13.4 s 0.6 s
data model, every crate 13.5 s 5.5 s
cold build, 32 cores / 4 cores 84 / 93 s 34 / 48 s

Decisions worth a look:

  • simlin-save-check is its own crate because the browser bundle has simlin_project_check_save and not the agent tools.
  • Wide visibility in places. simlin-wasmgen reads nearly all of CompiledSimulation, ByteCode and ByteCodeContext, so their fields are public. What only its tests call is behind test-support.
  • analyze_model no longer returns a copy of the model. It made one through the JSON format, which is now above it; simlin-mcp-core makes it.
  • The debug-derive feature is gone; every crate would have had to forward it.
  • The hook also lints the shipped configuration. Its other pass turns on every feature and dev-dependency, so it could not see what CI's Build job rejects.

simlin-serve is removed

It was never released and nobody uses it.
Cargo.lock goes from 487 packages to 339, and CI loses the embedded SPA build and two release smoke legs.
Loro's chain carried the crate whose license has the lint job red on main.

  • The Windows check stays, pointed at simlin-mcp: it was the only pull request job that compiled the engine on Windows, and @simlin/mcp ships a Windows binary.
  • docs/threat-model.md goes; it described simlin-serve alone. Its security contact moves to the README.
  • simlin-mcp-core keeps ProjectAccess and its version token, though nothing constructs AccessError::VersionMismatch now.

Editor fixes

Editing the equation of an arrayed variable replaced the whole array with one scalar equation, and saving a lookup edit dropped the table's own x points, both without a message.

  • The details panel now sends one operation per field (setEquation, setUnits, setNotes, setLookup) where it used to re-send the whole variable, so a field it does not know about can't be lost.
  • Apply-to-all variables show their plain equation with a line such as "Each element of Region" above it.
  • Per-element variables list each element's equation read-only, the EXCEPT default included; units and notes stay editable.
  • Changing the x scale of a table with its own x points leaves the points alone (XMILE 1.0 section 4.1.3 has a function give one or the other), and typing a point count no longer flattens the curve.

Evidence

  • The split moves code and does not change it. Comparing every Rust statement before and after, ignoring paths, visibility and line wrapping, 222 of the old ones and 323 of the new ones differ, in 600k lines; each was read. They are the items listed above, test helpers moving behind test-support, and thin non-generic entry points for flow routing.
  • No test is lost. 8,072 test functions before and after, one of them renamed and moved, and the same 49 ignored; 8,285 passed at every commit of the split and on the result. All 49 ignored tests pass under scripts/gates.sh.
  • Each crate stands alone: library, tests, --no-default-features and --all-features each check with warnings denied, as do both wasm builds.
  • The C API is the same. simlin.h differs from main's in comments only, and the wasm bundles export the same 119 (full) and 108 (browser) functions.
  • Editor fixes: editor-panel-edits-engine.test.ts runs the real editor and engine over 24 kinds of variable and 11 lookup edits; the arrayed rows fail without the fix. variable-details-arrayed.test.tsx covers what the panel shows. An edit through the notebook widget in Chromium kept its dimensions.

What it costs

  • Browser download: 2.68 to 2.74 MB with brotli (+2.2%). Each crate compiles its own copies of generic code.
  • Running an already-built cargo test --workspace: 15 to about 22 s on 32 cores, since cargo runs the crates' test binaries one after another; about 42 s either way on 4 cores.
  • Opening World3 in the browser wasm: 3.2 to 3.3 ms, 4.6 to 5.0 ms with LTM. C-LEARN opens within 2%.
  • Run time does not slow. Medians, pinned, against the branch before any crate existed; native parse and compile are within 1.1%:
Run time C-LEARN World3
native, LTO -0.5% -3.4%
native, no LTO -1.6% -0.2%
browser wasm, LTM off / on 1.00x / 0.97x 1.00x / 1.01x

The VM's dispatch loop is one function whose speed moves several percent with what else lands in its codegen unit: the same code with one more #[inline] measured +1.1% and +3.6% without LTO, and with one codegen unit the differences go.

Not covered

  • Editing per-element equations or the EXCEPT default in the panel.
  • The editor fixes in dark theme, at phone width, or in the app's own page; the browser check ran in the notebook editor.
  • The engine's LaTeX for an arrayed variable is the placeholder TODO(array), so these variables preview as text.
  • The JupyterLab journey runs only in CI.
  • The daily Dependency advisories workflow still fails for rmcp (RUSTSEC-2026-0462).

The local HTTP viewer and editor was never released and nobody uses it.
It accounted for 148 of the 487 packages in Cargo.lock, and every pull
request paid for it with an SPA build, two release smoke legs and a
Windows type-check, each compiling the engine again. Loro's dependency
chain (loro-internal -> pest -> psm -> ar_archive_writer) also carries
the Apache-2.0 WITH LLVM-exception license that cargo-deny rejects,
which is what has the lint job red. deny.toml keeps only the license
allowances and advisory exceptions the remaining dependency graph needs.

simlin-mcp-core keeps the ProjectAccess trait and its version token as
they are; only what serve alone reached is trimmed. The empty-project
helper becomes one function with one definition of the default
sim-specs, and helpers with no caller outside the crate lose their pub.
Nothing in libsimlin's C API changes. The threat model described serve
alone, so it goes, and the security contact it held moves to the README.
@simlin/mcp ships a win32-x64 binary, and the release workflow that
cross-compiles it runs only on a tag. No pull request job compiled the
engine or simlin-mcp on Windows, so a change that does not build there
was found at release time.
Every crate the engine is split into would have had to declare and forward the
feature that gated Debug, and it was already a trap: an unconditional derive on
a type holding a gated one passed a default build and failed the wasm and
pysimlin builds.

The gate bought no size. The browser bundle (wasm-release, no default features,
before wasm-opt) is 12,986,892 bytes with it and 12,985,372 without.
The MDL writer read the compiler's test for an equation that says nothing, which
read the MDL reader's sentinel for one; the MDL reader built its equations with
an edit operation's constructor; the compiler's sync asked the patch code for a
variable's uid; and the importers joined names for a sentence with the
diagnostics formatter's function. Each is a fact of the datamodel or a string
helper that needs nothing above it, so it now lives where both of its callers
can reach it: the sentinel and its test, `Equation::arrayed` and
`Variable::get_uid` in the datamodel, and `join_quoted_names` in `common`.

This is the first of the moves that leave the engine's modules in layers, so
that each layer can become a crate.
A row of results begins with the clock's slots, and everything that reads a row
by them (the VM, the wasm backend, the analyses, the agent tools) reads them
from the results, yet `results` took the first of them from the VM and the wasm
backend kept a second copy of the rest. They are one list in `results` now,
beside the names of those slots: what the systems reader and a rename refuse a
variable is the name of a results key, which is not a fact of the db.
The map from a lowered expression back to a parsed one is the ast's: it reads
and builds nothing else, and none of its callers is a patch. The shape of a name
an equation references is read by the ast's lowering scope and by the fragment
compiler, and is made only of dimensions and identifiers, so it moves down to
the ast too. The ast then uses nothing of the compiler, and LTM and the db use
nothing of the patch code.
The JSON a host sends a patch in is read into, and written from, the patch's own
types, and those were in the module that applies them: the format code depended
on every edit the engine can make. `project_patch` holds the operations as data,
made only of datamodel values, and `patch` is what they do.
`generate_relationships` joins the causal graph's polarities with the SD-AI
format, and it was in the format, which made reading and writing a file depend
on LTM. It is the analysis's: `model_links` beside it is the owner of a model's
causal links. The alternative was to move LTM's `LinkPolarity` below the
formats, for one match, beside the datamodel's own type of that name, the
polarity a diagram draws.

`SourceVariableKind` moves beside `VariableSource`, the parse input it is a
field of, so `variable` no longer reads the db that is built on it.
The engine's modules form layers, which are to become crates, and nothing held
them: one `crate::` path is all it takes to put a cycle back. The table says
which layer each crate-root module is in and which layers each layer may use,
and the check fails on a production reference to any other. It lists no
exceptions. Run over the tree before the moves it reports 54 references, the
ones the moves removed, and a copy of this tree with each layer in a crate of
its own, its visibility widened and nothing else changed, compiles.

It reads Rust by tokens, follows `mod` declarations to find the files, and
skips what no build without tests compiles. A new crate-root module fails the
check until the table places it.
The engine is about to become several crates, and its integration tests,
benchmarks and instruments use all of it at once. They are simlin-tests, which
nothing depends on and which depends on everything.

The engine's dev-dependency on itself goes with them. It was there to turn
`file_io` on for the corpus (GH #925); what the feature gates is now
`cfg(any(test, feature = "file_io"))`, and the corpus asks for the feature in
the one edge simlin-tests has onto the engine, which no flag turns off.

The examples need the `instruments` feature, so `cargo test` links none of them
and the hook's `--all-features` clippy still checks them all.

`vm.rs` imported a constant only a test-support build uses, which the deny-
warnings build CI does, of production code alone, refuses.
simlin-base is the engine's lowest layer as a crate: the data model and the
patches to it, identifiers, errors, dimensions, the equation lexer, parser and
AST, the builtins, the stdlib and the results of a run. Nothing re-exports it;
whoever uses one of its items names it there.

What is public is what another crate uses, and no more: an item that was pub in
a module nothing outside the crate could reach is pub(crate) unless it crosses
the boundary, so the dead-code lint still sees it.

Eleven tests moved. Nine of `ast::ArrayView` were in the compiler's dimension
tests and are beside it now. Two run a model on the VM to say something of a
base type, which base cannot, and are the engine's: that a canonical equation
computes what its original does, and the table a run prints.
simlin-diagram draws a model's diagram as SVG, PNG and a scene, on simlin-base
alone; the resvg rasterizer and its `png_render` feature are its own.

Nine tests draw a corpus model, which they have to open, and the readers are
built on this crate: they are in simlin-tests. The oracle they check a scene by
is in the crate behind `test-support`, so its own tests and theirs share one.
`analyze_model` returned a JSON snapshot of the model beside the loops, so it
could not be built below the file formats; libsimlin paid for the snapshot and
dropped it. The MCP tools are what report it, and they take it now
(`types::model_snapshot`), beside the SD-AI relationships, which they alone
write. What a variable reads (`model_reads`) is the agent tools' question and
walks the text a rename walks, so it is theirs.
simlin-formats reads and writes what a model is kept in: XMILE, Vensim MDL and
VDF, the systems format, JSON and the stored protobuf. It is built on the data
model and the diagram geometry, not on the compiler, so a compiler edit leaves
it alone and the reverse; its tests that run what they read use the engine as a
dev-dependency.

The tests that ask the save check, land a patch or lay out a diagram are in
simlin-tests, since those are built on this crate; the generator and comparisons
they share with the writer's own tests are behind `test-support`.
simlin-edit holds the patches a host or an agent lands, the plans of gestures on
a diagram and a variable's equations as a host draws them. An edit is made to a
project whether or not it compiles, so none of it uses the compiler.

Two tests of what the edits by field store ask the save check and are in
simlin-tests; the one of what a diagram sync shares is with the layout it tests.
simlin-wasmgen lowers the engine's compiled simulation to a wasm module. It
reads the bytecode tables and a few VM helpers, which are what the engine makes
public for it; everything else of both stays inside the crate, so the dead-code
lint still says when an opcode has no reader.

What its tests build bytecode with is behind the engine's `test-support`, and
the test that the backend refuses a stray overflow marker is here.
simlin-tools is what libsimlin's `agent_tools` feature now depends on, so the
engine carries no feature for it and a build without the feature compiles none
of it. `schema` is the tools' and the formats' alone.

It leaves before the layout, which it uses to draw what an edit adds: the engine
cannot hold the tools and depend on a layout crate built on the engine.
The check saves and reads back with the formats and runs both sides with the
engine, so it is above both. It is not part of the agent tools, which use it: the
browser bundle has `simlin_project_check_save` and leaves the tools out.
simlin-layout places what the engine's dependencies and loops say and draws it
with the editing core's routing, so it is above both. The engine is left with
the compiler, the VM and the analysis of runs, on the data model alone; its unit
tests read and edit models with simlin-formats and simlin-edit, which do not
depend on it.

Two tests that ask the layout and the compiler the same question are in
simlin-tests.
The wasm backend's tests hold the code it emits to the VM's own arithmetic, so
those functions were public and the compiler could no longer inline them into
the one call the VM makes of each. They are `vm_oracle` now, which only
`test-support` has. `to_runtime_view`, public on a type the backend reads, and
`dense_linear_start` are `#[inline]`: both were calls in the profile of a build
without LTO.

The dispatch loop is one function big enough that its speed moves several
percent with what else is in its codegen unit; with one codegen unit the crates
execute the instructions the single crate did, to 0.3%.
The routing is generic over its obstacles, so the layout's calls compiled it a
second time, 66 KB of the browser bundle. The layout asks it through functions
that take the stocks' centers, and the obstacles and their bounds are the
editing core's own again.
The layers it held inside one crate are crates, so cargo refuses a use that goes
up and `scripts/check-deps.py` holds the edges between them.
The hook's clippy pass turns on every feature and every dev-dependency,
so an item that only a test configuration uses looks used. CI's Build
job compiles the default features with warnings denied and fails on it,
which a local commit could not see. A second pass over the default
features costs a few seconds after an engine edit.
The details panel re-sent the whole variable for every change, with the text
of its one equation field as a scalar equation. An apply-to-all variable's
field also held an "{apply-to-all:}" line, and a per-element variable's a TODO,
so editing either saved that text as a scalar and discarded the dimensions and
every element's equation. The panel now says what the user changed -- the
engine's setEquation, setUnits and setNotes operations, one per field -- and
the engine keeps the rest, modules' units and notes included. Scalars end up
as they did before, except that the engine trims the units it is given.

The equation field holds the equation alone, so what it shows is what a save
sends and the engine's error offsets index it directly; a line beside it names
the dimensions. A per-element variable has no equation field: setEquation there
would set the default its other elements follow, so its equations are listed
read-only, the EXCEPT default with them, until there is an editor for them.
Arrayed variables preview as text, since the engine's LaTeX for them is a
placeholder.
Saving any edit to a lookup dropped the table's x points, so a table given as
(0,0), (1,5), (10,9) was saved as three points spread evenly over its x scale
even when only the y axis maximum had changed. The handler re-sent the variable
with a table it rebuilt by hand, without the x points. It now sends the engine's
setLookup operation with the table serialized as everything else serializes
one, which also leaves nothing about the variable to re-send.

The lookup editor saves the x points it shows, and no longer moves them on its
own. The x axis minimum and maximum of a table with x points of its own are the
range it is drawn over -- the engine does not read them then -- so changing one
leaves the points where they are; they were spread evenly over the new range.
Changing the point count does resample, over the x the table covers rather than
the range it is drawn over, and always from the table as it was before the
count was touched: typing 12 passes through 1, and resampling that one point
flattened every table, and putting the count back now puts the points back.
The C API's comments, the conveyor fixtures' README and the layout_eval
instrument named modules at the paths they had in one crate.
simlin.h changes in those comments alone.

With them, three things found while checking the crates one by one.
A full wasm build compiled the batch budget that only native builds use.
The dependency policy allowed simlin-cli a dependency on libsimlin it is
documented not to take.
dense_linear_start does not need its inline attribute: its crate's only
callers are beside it, and builds without it measure no slower.
cargo-deny reads a path dependency with no version as a wildcard, which the
policy denies, and it is not one of the hook's checks.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-24T22:29:43.923556Z 25125ed PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@codecov

codecov Bot commented Nov 24, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.51973% with 101 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.01%. Comparing base (15f8f60) to head (25125ed).

Files with missing lines Patch % Lines
src/libsimlin/src/lib.rs 41.50% 62 Missing ⚠️
src/libsimlin/src/simulation.rs 60.86% 9 Missing ⚠️
src/simlin-diagram/src/scene_test_support.rs 98.54% 6 Missing ⚠️
src/libsimlin/src/project.rs 88.63% 5 Missing ⚠️
src/simlin-cli/src/main.rs 42.85% 4 Missing ⚠️
src/libsimlin/src/analysis.rs 94.33% 3 Missing ⚠️
src/libsimlin/src/model.rs 88.88% 3 Missing ⚠️
src/simlin-cli/src/vdf_dump.rs 0.00% 3 Missing ⚠️
src/simlin-base/src/ast/to_expr0.rs 96.22% 2 Missing ⚠️
src/libsimlin/src/layout.rs 87.50% 1 Missing ⚠️
... and 3 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1098      +/-   ##
==========================================
+ Coverage   92.88%   93.01%   +0.13%     
==========================================
  Files         318      300      -18     
  Lines      198542   192638    -5904     
==========================================
- Hits       184408   179177    -5231     
+ Misses      14134    13461     -673     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@bpowers
bpowers merged commit a21e27d into main Nov 25, 2026
15 of 17 checks passed
@bpowers
bpowers deleted the engine-crates branch November 25, 2026 00:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment