Skip to content

engine: MDL writer ignores compat on stocks and arrayed vars, silently dropping ACTIVE INITIAL / GET DIRECT #857

Closed

Description

@bpowers

Problem

The MDL writer (src/simlin-engine/src/mdl/writer.rs) drops compat initialization metadata in two places, silently changing simulation initialization semantics on MDL round-trip.

Gap 1: write_stock_variable never reads stock.compat

write_stock_variable (writer.rs:974-1031) does not consult stock.compat at all. datamodel::Stock carries a compat: Compat field, but none of the three equation arms (Scalar, ApplyToAll, Arrayed) pass it through:

  • A stock imported with an ACTIVE INITIAL(...) initial value loses the ACTIVE INITIAL wrapper on export (its compat.active_initial is never applied).
  • A stock with a GET DIRECT ... data-source initial loses the reconstructed GET DIRECT call on export (compat.data_source is never consulted).

By contrast, the aux/flow path (write_variable, writer.rs:887-916) handles both cases: compat_get_direct_equation(compat) reconstructs the GET DIRECT expression, and wrap_active_initial(eqn, compat) re-wraps ACTIVE INITIAL. Stocks get neither.

Gap 2: wrap_active_initial / GET DIRECT not applied to Equation::Arrayed

Even in the aux/flow path, wrap_active_initial and compat_get_direct_equation are only applied to Equation::Scalar (writer.rs:895) and Equation::ApplyToAll (writer.rs:902). The Equation::Arrayed arm (writer.rs:913-915) calls write_arrayed_entries with neither compat nor the data-source equation, so an arrayed aux/flow with compat.active_initial silently loses its ACTIVE INITIAL wrapper. (The arrayed stock arm, write_arrayed_stock_entries at writer.rs:1066, has the same omission as part of Gap 1.)

Why it matters

ACTIVE INITIAL and GET DIRECT change how a variable is initialized at t=start. Dropping them on round-trip produces an MDL file that initializes differently from the source model -- a silent semantic loss, not a cosmetic diff. Severity: medium-high, because it corrupts real imported models on export with no error.

Components affected

  • src/simlin-engine/src/mdl/writer.rs (write_stock_variable, write_arrayed_stock_entries, the Equation::Arrayed arm of write_variable / write_arrayed_entries)

Possible approaches

  • Thread stock.compat into write_stock_variable and apply compat_get_direct_equation / wrap_active_initial in each arm, mirroring the aux/flow path.
  • Extend the arrayed writers (write_arrayed_entries and write_arrayed_stock_entries) to accept compat and apply the ACTIVE INITIAL wrapper (and, where in scope, the GET DIRECT reconstruction) to arrayed equations.
  • Add round-trip fixtures: a stock with an ACTIVE INITIAL initial, a stock with a GET DIRECT initial, and an arrayed aux/flow with ACTIVE INITIAL, asserting the wrapper/call survives export. The existing mdl_to_mdl_roundtrip datamodel-fixpoint harness may not catch this if import collapses the forms; a golden-text assertion on the emitted MDL is more reliable.

Related but distinct

Context

Identified during review of the MDL writer's compat/initialization handling.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions