Repository navigation
Description
Importing the same .mdl file in two separate processes could give two different models, and different simulation results.
An import should be a function of the file.
Example
test/test-models/tests/subscript_transposition/test_subscript_transposition.mdl declares two dimensions with the same five elements, sector and sector1.
Over 40 runs of simlin simulate, output2 came out two ways: all NaN, or 480, 4800, 480, 640, -96.
Vensim's run beside the fixture gives 109, 1090, 109, 141, -13.
The same split showed in arrays_cname.mdl, arrays_varname.mdl (sdeverywhere) and subscript_switching.mdl: which dimension a variable was declared over, and whether a bang subscript was imported as * or Dim.*, changed from run to run.
With LTM on, the set of loop-score variables and the warnings changed too.
Why it matters
Silent wrong numbers that change between runs, on test models with Vensim ground truth.
In-process determinism tests could not see it, because a process seeds its hash maps once.
Cause
When two dimensions of the same size share elements, the importer gave the elements to whichever dimension a HashMap yielded first.
That one choice decided the declared dimension and the * vs Dim.* spelling.
A second instance: resolving a per-element equation's mapped dimensions iterated a HashMap.
Status
Both are now decided in declaration order (element_owners in convert/dimensions.rs; build_element_context in convert/variables.rs).
import_determinism_tests.rs imports every corpus file 13 times and requires one result (each map has its own seed, so this works in one process), and mdl_vensim_truth.rs holds the first three fixtures to Vensim's output (subscript_switching does not simulate, for an unrelated reason).
Pointers
src/simlin-engine/src/mdl/convert/dimensions.rs:element_ownerssrc/simlin-engine/src/mdl/import_determinism_tests.rssrc/simlin-engine/tests/integration/mdl_vensim_truth.rs
Activity
mdl: read apply-to-all intent off the source spelling, not slot coverage
bpowers commentedon Aug 26, 2026
This reaches simulation output: silent wrong numbers, not just round-trip non-idempotence
Re-confirmed independently during the compiler-unification Phase 6(a) differential sweep (2026-08-26), which ran a pre-change and a post-change CLI over all 509 models under
test/. 397 models were byte-identical and 110 refused identically; the only three movers were all explained by this nondeterminism rather than by the change under test.The issue as filed frames the impact as importer datamodel differences plus
EXPECTED_NON_IDEMPOTENTround-trip churn, and notes #847's writer change "masks the bug at the corpus re-parse ratchet". The masking is narrower than that reads: the same seed-dependent classification changes simulated values, and both outcomes exit 0. Measurements below use the prebuilt release CLI, N separate processes of the same binary, counting distinctmd5sums of the whole TSV.convert --to xmilesimulatetest/test-models/tests/subscript_transposition/test_subscript_transposition.mdltest/sdeverywhere/models/arrays_varname/arrays_varname.mdltest/sdeverywhere/models/arrays_cname/arrays_cname.mdlThe nondeterminism is entirely upstream of compile
Feeding each captured XMILE variant back through
simlin simulate40 times gives 1 distinct output per variant, for all 6 captured variants. So compile and simulation are deterministic; the whole split is introduced at import, which is exactly this issue's scope. (Worth stating explicitly because the natural first hypothesis — aHashMapreachingDimensionsContextor the compiled artifact — is disconfirmed here.)Manifestation 1 confirmed, and it produces wrong numbers
subscript_transpositionsplits precisely along theWildcard-vs-StarRangeaxis this issue describes:Vensim DSS 7.3.4 ground truth is checked in beside the fixture (
output.tab, contributed by Eneko Martin, 12/02/20). Its two dimensions have identical element lists (sector1andsector, bothagriculture, industry, transport, tourism, comercial). Vensim'soutput2is109, 1090, 109, 141, -13. The engine gives, per process:output2= allNaNoutput2=480, 4800, 480, 640, -96Neither matches.
output(the non-transposed spelling) is111, 251, 319, 495, 527in both and does match Vensim, so the ambiguity only bites the transposed read.A third manifestation: the variable's declared dimension list
Beyond the two manifestations already listed,
arrays_varnamevaries in which dimension a variable is declared over — the<dim name>entries, not a wildcard binding. Two independent axes compose into the 4 datamodels:Here
DimAisA1, A2, A3andDimXisSubA, A1whereSubA: A2, A3— the same element set in a different order. TheDimC/DimC'axis is semantically inert (same elements, same order) and does not move values; theDimA/DimXaxis does, which is why 4 datamodels collapse to 2 outputs.The wrong-number form here is a label/value mismatch, not a reordering. In one variant the element keys and values agree (
inputab[a1,b1]=11 … inputab[a3,b3]=19) but the reducerp[DimA] = SUM(inputAB[DimA, DimB!])reports:i.e.
p[a1]carries a2's row sum under a1's label. The other variant gives the correct36, 45, 54.Why no test catches it
arrays_varnamenorarrays_cnameis intests/integration/simulate.rs's corpus — they are commented out there (~line 2147, "xmutil drops subscript mappings in XMILE conversion").subscript_transpositionappears only intests/integration/mdl_roundtrip.rs'sEXPECTED_NON_IDEMPOTENTlist, which says nothing about simulated values. Its Vensimoutput.tabtherefore sits in the tree as unused ground truth.fragment_determinism_tests/diagnostic_determinism_testsrepeat 12 times inside one process, whereRandomStateis seeded once, so they are structurally blind to a per-process split.Suggested additions to the fix
Beyond the ordered-container fix already proposed here:
docs/reference/xmile-v1.0.htmlshould be checked before choosing rather than reasoned from.subscript_transpositiontotests/integration/simulate.rs's corpus so its Vensim ground truth is actually asserted.Note for whoever picks this up
Phase 6(a) separately fixes the transposed star-range/temp-name defect, which moves
subscript_transposition's variant A from all-NaNto exactly Vensim's109, 1090, 109, 141, -13. Variant B (480, 4800, …) is untouched and remains this issue.engine: LTM reads typed values; one module-instance owner
engine: LTM reads typed values; one module-instance owner
bpowers commentedon Sep 4, 2026
Root cause located (one site), plus three LTM-side faces of the same flip
Re-confirmed on
compiler-unification-v2(PR #1040), where the design doc namesarrays_cname,arrays_varnameandsubscript_transpositionas "GH #859 flippers" and excludes them from its artifact claims. Everything below is pre-existing onmain.The single site
The body above says the root cause "needs confirmation via instrumentation". It is now confirmed by reading and by reproduction, and it is one tie-break, not a set of unordered iterations:
src/simlin-engine/src/mdl/convert/dimensions.rs,build_dimensionsPhase 4 (lines 75-89):The sort is stable and keyed on cardinality only, so two dimensions of EQUAL size that share elements are ordered by
HashMap(RandomState) iteration order, and the first one claims the elements. Every fixture that flips has exactly that shape:arrays_cname.mdl,arrays_varname.mdlDimA(3: A1,A2,A3) vsDimX(3: SubA+A1);DimC(3) vsDimC'(3 --DimC': DimCis a subscript definition that expands recursively to the same three elements, not an<->equivalence)subscript_switching.mdlCats(5) vsdummy(5, identical list)subscript_transposition.mdlsector(5) vssector1(5, identical list)Two consumers of
element_ownersturn the winner into every manifestation listed in this issue:expand_subscript(dimensions.rs:349): a per-element slotx[A1]is declared over the element's OWNER, so the variable's<dim name>isDimAon one seed andDimXon another (manifestation 3 in the comment above).convert/mod.rs:238-255(and the same test inresolve_subrange_to_parent,dimensions.rs:415-425): the LOSER of the tie is classified as a subrange of the winner and put inXmileFormatter::subrange_dims, so its bang subscript formats asDim.*(xmile_compat.rs:184-190,678-684) while the winner's formats as*(manifestations 1 and 2).So
WildcardvsStarRange, "which dimension the wildcard binds to", and "which dimension the variable is declared over" are one bit, chosen per process. Other first-match loops over the sameHashMapinconvert/variables.rs(build_element_context~line 878,build_subrange_mappings~line 921, bothbreakon the first hit) are hash-ordered too; I have not shown them to flip anything, but a fix should sweep them.The field comment on
element_ownerssays "First/largest match wins like xmutil's owner assignment". The "first" it intends is presumably definition order, whichself.dimensions(aVec, in file order) already preserves; iterating that instead ofdimension_elementswould make the tie deterministic in the sense the comment describes. Which of two identical dimensions Vensim itself treats as the owner is a claim about an external tool and should be checked against the checked-in ground truth (subscript_switching/output.tab,subscript_transposition/output.tab, sdeverywhere'sarrays_*expected output) rather than reasoned from; the earlier comment's suggestions 1-3 still apply.Reproduction (release CLI,
convert --to xmile, 12 fresh processes per model)subscript_switching.mdl: 2 distinct outputs (6/6). The whole diff is the bang spelling, in both directions:arrays_cname.mdl: 4 distinct outputs (theDimA/DimXaxis times theDimC/DimC'axis), matching the comment above.Three new faces, all in Loops That Matter
The CLI shows only the already-known face:
arrays_cname/arrays_varnameundersimulate --ltmflip stdout 2 ways exactly as plainsimulatedoes (same 513-column set, different column ORDER and values --d[a2],d[a3],d[a1]vsd[a1],d[a2],d[a3],inputab[a2,b1]=14vsinputab[a1,b1]=11in the first slot), andsubscript_switching.mdl --ltmis refused identically for both import variants (failed to compile fragments for variables: categories). The faces below were seen through the branch's in-engine harnesses: the corpus LTM dump (model_ltm_variablesoutput) and the diagnostics corpus (collect_all_diagnosticswith LTM enabled), run 3x per binary on two binaries.(b) The discovery-mode LTM variable SET varies per process.
arrays_cname.mdlproduced 38 LTM variables on one run and 58 on another (V9b report: on theDimXside the shared-name reads are described, on theDimAside they are not);arrays_varname.mdlandsubscript_switching.mdl"flip within a side" over 3 runs (V9b reviews). The branch lead's harness runs recorded discovery counts of 27/31/38 forarrays_varnameand 54/83/84 forsubscript_switchingacross processes; those specific figures are as reported, not re-reproduced here.(c) LTM-discovery DIAGNOSTICS for
arrays_cname.mdlare 0 or 3 warnings depending on process state (Phase 9 review: base binary 0/3/0 over three runs, tree binary 3/3/3 in isolation and 0 inside the full corpus run). A diagnostics-are-a-function-of-the-model gate (the #999 / #1003 invariant) therefore flaps on this model even with every emission site sorted.(d) Engine-generated LTM aggregate equation TEXT flips. For
subscript_switching.mdlthe synthetic aggregate node's equation issum(transition_matrix[cats, *:dummy] * ...)on one seed andsum(transition_matrix[cats, *] * ...)on another (V9a review D8, 4 runs each on both binaries,probes/repeat-eqs.sh). This is thedummy.*spelling from the reproduction above parsed asIndexExpr0::StarRangeand printed as*:dummy; the LTMReducedaxis treats a StarRange over a proper subdimension differently from a bare wildcard (GH #766,ltm_agg.rs), which is why the variable set and the detected loops move with the spelling.Downstream-of-import check: each captured XMILE variant run 5x under
simulate --ltmgives exactly one stdout digest, one sorted-stderr digest and one raw-stderr digest per variant. The LTM faces are a deterministic function of the imported datamodel; there is no second nondeterminism on the LTM side of these models.Not to be conflated with #1036: that issue is warning ORDER with a stable set (an LTM emission loop over a
HashMap); this is CONTENT -- which LTM variables exist, what their equations say, and how many warnings are produced.Why the branch's harnesses caught it and the suites do not
Same reason as the comment above: the determinism suites repeat inside one process. The branch's sweeps run N separate processes per binary and count distinct digests, which is what separates a flipper (same digest SET on both binaries) from a mover. An LTM-enabled row in whatever cross-process guard closes this issue would pin faces (b)-(d); the existing plain-output guard would not.
engine: make an mdl save a fixed point
engine: resolve mapped dimensions in one order
[-]engine: MDL importer subscript/dimension classification is non-deterministic across process hash seeds[/-][+]mdl: one file imports differently from run to run[/+]bpowers commentedon Oct 12, 2026
Closing: already fixed on main.
PR #1076 (f629e96): element_owners (src/simlin-engine/src/mdl/convert/dimensions.rs:503) iterates dimensions in declaration order with a stable sort, and build_element_context (convert/variables.rs:962) resolves mapped dimensions in LHS order with sorted rounds. mdl/import_determinism_tests.rs imports every corpus .mdl 13 times and requires one result, and tests/integration/mdl_vensim_truth.rs requires subscript_transposition, arrays_cname and arrays_varname (unlisted in EXPECTED, so class Matches) to match Vensim's output.