Repository navigation
Description
Summary
No compile path in simlin-engine rejects two variables whose names canonicalize to the same identifier. Canonicalization collapses case, whitespace, and underscores, so <flow name="Attrition"> vs <flow name="attrition">, or net flow vs net_flow, are the same canonical ident. A model containing such a pair compiles and simulates with one of the twins silently dropped (last-in-document-order wins on the salsa path) -- no diagnostic anywhere.
ErrorCode::DuplicateVariable exists but is raised only by edit operations in patch.rs; model sync/compile never emits it, so any reader or importer that tolerates duplicates (XMILE, JSON, protobuf, external tools writing datamodel directly) produces a silently-wrong simulation.
Evidence
- XMILE reader stable-sorts variables by canonical ident but neither dedups nor rejects duplicates:
src/simlin-engine/src/xmile/model.rs:539-564(the sort at 561-564 makes duplicate pairs adjacent, then hands them downstream unchanged). - Salsa sync inserts variables into
HashMaps keyed by canonical name, so a later duplicate silently replaces the earlier one:src/simlin-engine/src/db/sync.rs:284-293(source_var_map.insert/variables.insert), and again in the incremental sync path (~lines 611-641). - The legacy/monolithic path does the same via
HashMap<Ident<Canonical>, _> = variable_list.into_iter().map(|v| (Ident::new(v.ident()), v)).collect():src/simlin-engine/src/model.rs:995and:1099. ErrorCode::DuplicateVariable(src/simlin-engine/src/common.rs:371) is raised only atsrc/simlin-engine/src/patch.rs:293and:502-- edit-time upsert/rename checks, never during compilation.
Why it matters
- Correctness: one of the twin variables' equations is silently discarded; simulation results are wrong with no error or warning. Downstream references resolve to whichever twin survived the HashMap insert.
- Latent panics: pre-HashMap consumers that iterate the raw datamodel variable list see both twins and can violate assumed invariants. Issue conveyor-engine: expand_conveyors panics (unwrap) on duplicate canonical flow idents where a later flow carries the leak #870 (expand_conveyors unwrap panic on a duplicate leak/non-leak flow pair) is exactly this class -- the conveyor expansion runs on the datamodel before any dedup. conveyor-engine: expand_conveyors panics (unwrap) on duplicate canonical flow idents where a later flow carries the leak #870's body asserted duplicate-ident validation "lives in the bypassed salsa path", but no such validation exists on any path; the conveyor-engine: expand_conveyors panics (unwrap) on duplicate canonical flow idents where a later flow carries the leak #870 fix removes that panic locally and deliberately does not add general duplicate validation (wrong layer).
- Developer/modeler experience: an accidental
net flow/net_flowpair (easy to create via import or hand-edited XMILE) is undiagnosable today.
Components affected
src/simlin-engine/src/db/sync.rs(salsa sync -- the production compile entry)src/simlin-engine/src/model.rs(legacy/monolithic path)src/simlin-engine/src/xmile/model.rsand other readers/importers that tolerate duplicates
Suggested direction
Add duplicate-canonical-ident validation at model sync time (and/or as a model-level diagnostic in the salsa pipeline): when two datamodel variables in the same model canonicalize to the same ident, emit a loud ErrorCode::DuplicateVariable diagnostic naming both original spellings, instead of silently collapsing. Sync is the right chokepoint because every reader/importer funnels through it, and it sees the pre-collapse variable list. The legacy model.rs path and any pre-sync datamodel consumers (e.g. conveyor expansion) then inherit the guarantee or can keep local defensive handling.
Note the model-name analogue already has precedent: SourceProject.macro_declarations exists precisely because the canonical-name-keyed models map collapses duplicate model names needed for macro-registry validation (src/simlin-engine/src/db/macro_registry.rs) -- the same collapse happens per-variable with no corresponding validation.
Context
Identified while fixing #870 on the conveyor-engine branch (PR #869 review follow-ups): the panic there is reachable only because duplicate canonical flow idents survive to conveyor expansion, and investigating the assumed upstream validation showed it does not exist on any compile path. Related but distinct: #399 (systems-format translator can generate colliding idents -- a producer-side bug; this issue is the missing engine-side gate).
Activity
engine: fix panic on duplicate canonical leak flow idents
engine: reject duplicate idents on the monolithic compile path too
engine: fix panic on duplicate canonical leak flow idents
engine: reject duplicate idents on the monolithic compile path too