Skip to content

engine: a stock (or stdlib instance) initialized from a stockless sub-model's output reads 0 at t=0 (initials runlist is closed per model) #1028

Closed

Description

@bpowers

Summary

A parent-side initial value that reads an output of a sub-model with no stocks sees 0 at t=0 instead of the output's true value. A model's INITIALS runlist is seeded from that model's own stocks, modules, INIT()-referenced variables and INITIAL()-shaped variables, then transitively closed within the model. A stockless sub-model therefore has an empty initials runlist: during the parent's initials phase the module instance runs, evaluates nothing, and the parent reads an unwritten sub·output slot (0 in a clean buffer). The value is snapshotted into initial_values and the stock starts from the wrong number for the whole run. No diagnostic is produced; the model compiles and runs to FINAL TIME.

This is the general form of the boundary that #584 fixed for one structural special case (an INITIAL()-backed module/macro primary output). The runlist_initials comment in db/dep_graph.rs already states the wider invariant -- "the sub-model's dep graph is computed in isolation and cannot see that cross-model read" -- but the realization is bounded to the empty-dt_deps / non-empty-initial_deps shape, so a plain aux output (output = input * 10) read by a parent stock's initial is still missed.

Repro

Parent main (sim 0..2, dt 1):

src   = 3
sub   : module of model `producer`, wiring src -> sub.input
level = INTEG(0, sub.output)          # explicit stock, initial = sub.output
sm    = SMTH1(sub.output * 2, 2)      # stdlib instance, stock initial = its input port

Sub-model producer (no stocks):

input  (module input port, default 0)
output = input * 10

Observed (incremental compile path, compile_project_incremental + Vm):

variable t=0 t=1 t=2 expected t=0
sub·output 30 30 30 30
level 0 0 0 30
$⁚sm⁚0⁚arg0 (hoisted sub·output * 2) 60 60 60 60
$⁚sm⁚0⁚smth1·input 60 60 60 60
sm 0 30 45 60, 60, 60

Both wrong numbers have the same cause: the SMTH1 instance's internal stock is initialized from its input port, which is computed from sub·output during the parent's initials phase, when producer has not evaluated anything. The flows-phase values are all correct, which is what makes the defect silent.

Root cause

src/simlin-engine/src/db/dep_graph.rs, the runlist_initials block in the model dep-graph query. The needed set is

!i.is_table_only
    && (i.is_stock
        || i.is_module
        || (i.dt_deps.is_empty() && !i.initial_deps.is_empty()))   // GH #584 clause
|| all_init_referenced.contains(n.as_str())

and is then transitively closed over initial_dependencies of that model only. For producer: output has dt_deps = {input} (non-empty), is not a stock, not a module, not INIT-referenced; input has no deps at all. Nothing is seeded, so the sub-model's initials runlist is []. In the parent, sub is a module (seeded) and level's init dep is sub, so the parent's initials runlist is [src, sub, level] -- correct on its own; it is the sub-model's side that cannot see the read.

Cross-model dependencies are deliberately absorbed at the orchestrator level (the is_module early-returns and the "cross-model deps handled upstream" notes in the same file), which is why the per-model query has no information about which of its variables a parent reads during initials.

Why it matters

  • Correctness, silent: a stock starts from 0 (or from whatever the buffer held) instead of its true initial, with no diagnostic. The error is permanent for the run because run_initials snapshots the slot into initial_values.
  • Shape is common: any "converter" sub-model (unit conversion, lookup wrapper, parameter block) with no stocks, whose output feeds a stock initial or a stdlib SMTH*/DELAY*/TREND instance, hits this. Stdlib instances make it easy to hit without the user ever writing an explicit INTEG.
  • Independent of the fragment compiler: the explicit-stock twin reproduces on the tree before the compiler-unification Phase 3 change; Phase 3 only made the SMTH1 shape compile at all (it used to fail with does_not_exist), which exposed the 0 start.

Components affected

  • src/simlin-engine/src/db/dep_graph.rs -- runlist_initials seeding and its per-model transitive closure; the module-as-sink treatment in the init/dt walkers.
  • The orchestrator / assembly layer that wires module instances (db/assemble.rs, db/fragment_compile.rs) -- the only place that knows which sub-model variables a parent reads during initials.
  • src/simlin-engine/src/vm.rs -- run_initials snapshot and LoadInitial are where the unwritten slot is frozen and served (symptom site, not fix site).

Possible approaches

  1. Cross-model seeding of the initials runlist. For each module instance, compute the set of sub-model outputs the parent reads during its initials phase (stock initials, INIT()-referenced reads, INITIAL()-shaped vars, and the input ports of stdlib instances), and seed those -- plus their in-model transitive init closure and the input ports they depend on -- into the sub-model's initials runlist. Because the per-model dep graph is a salsa query keyed on the model, this needs either a per-(model, init-read-set) key or a union over all instances of that model in the project. This is the fix the pinning test names.
  2. Evaluate a module's non-stock aux chain in the initials phase. Treat every aux in a sub-model that is computable from its input ports and constants as an initials-phase variable (which is exactly what a flattened, single-model version of the same equations does). Simpler and instance-independent, at the cost of some extra init-time work; ordering between those auxes and any stocks inside the module would need the same care engine: init-time feedback through SMTH1 gives wrong t=0 value, no error #595 describes.
  3. At minimum, stop being silent. If a parent initials-phase read targets a sub-model variable that is not in that sub-model's initials runlist, emit a compile diagnostic instead of reading 0.

Whatever the fix, it must be structural (keyed on "read across a module boundary during initials"), not specific to SMTH1 or to stockless modules; a sub-model WITH stocks whose parent reads a non-stock output during initials has the same gap unless that output happens to be in the stock's init closure.

Relationship to existing issues (distinct)

Pinned by

Both tests live in src/simlin-engine/src/db/fragment_input_tests.rs on branch compiler-unification (not yet on main):

  • stock_initialized_from_a_stockless_modules_output_is_a_pre_existing_residual -- asserts sub·output = [30, 30, 30] and level = [0, 0, 0] as a disclosed residual; flip level to [30, 30, 30] when fixing.
  • smooth_argument_reading_a_module_output_compiles -- asserts sm = [0, 30, 45]; becomes [60, 60, 60] when fixed.

Also recorded in docs/design-plans/2026-08-25-compiler-unification.md, "Phase 3 semantic divergences".

Discovery context

Found while unifying the fragment compiler (compiler-unification Phase 3, branch compiler-unification). Filed rather than fixed in that branch because the fix is a dependency-graph / orchestration change (cross-model seeding), not a lowering change, and would swamp that branch's review surface.

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

    bugengine

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions