Skip to content

ltm: module-to-variable link score picks an arbitrary module output when the target reads two outputs of the same instance (HashSet iteration) #971

Closed

Description

@bpowers

Summary

In module_link_score_equation (src/simlin-engine/src/db.rs ~line 693; the module -> variable arm at ~lines 808-816), the live module-output reference for the ceteris-paribus partial is selected by iterating a HashSet:

let from_output = to_var
    .ast()
    .map(|ast| crate::variable::identifier_set(ast, &[], None))
    .and_then(|deps| {
        let prefix = format!("{from_name}\u{00B7}");
        deps.into_iter()
            .find(|d| d.as_str().starts_with(&prefix))
            .map(|d| d.to_string())
    });

crate::variable::identifier_set returns HashSet<Ident<Canonical>> (src/simlin-engine/src/variable.rs ~line 1196), and Rust's HashSet iteration order is randomized per process (DefaultHasher seeding). For a target variable whose equation reads TWO (or more) outputs of the same module instance (e.g. y = mod\u{00B7}out1 + mod\u{00B7}out2), .find() picks whichever {from}\u{00B7}-prefixed dependency happens to come first, so WHICH output is held live in the LTM partial link-score equation for the mod -> y edge is arbitrary and can vary run-to-run.

Why it matters

  • Determinism / salsa caching: the selected output ref feeds generate_link_score_equation_for_link and hence the generated equation text; salsa cache values should be byte-stable across processes. This site can produce different equation text (and different fallback black_box_unit_transfer_equation references on the Err path) for the same model in different processes.
  • Reproducible link scores: the two outputs generally have different partials, so the mod -> y link score series itself (not just its text) can differ run-to-run, flapping loop scores and dominance analysis downstream.

Single-output module reads (the common case, all stdlib SMOOTH/DELAY usage) are unaffected, which is why this has gone unnoticed.

Distinct from the closed #680, which fixed the unordered output-port set in find_model_output_ports / enumerate_pathways_to_outputs (src/db/ltm/loops.rs); this is a separate site in db.rs's module_link_score_equation. Also distinct from #595 (init runlist ordering).

Component(s) affected

  • src/simlin-engine/src/db.rs (module_link_score_equation, module -> variable arm)

Possible approaches

  • Collect the {from}\u{00B7}-prefixed deps and pick deterministically, e.g. deps.into_iter().filter(...).min() (lexicographically smallest canonical ident), or sort into a Vec/BTreeSet before selecting.
  • Better still, consider whether a target reading multiple outputs of the same instance should get one partial per output (or a combined partial holding all {from}\u{00B7} refs live) rather than singling out one; at minimum the selection should be stable.
  • Add a regression test with a two-output module read twice by one target, asserting stable generated equation text across repeated db builds.

Severity

Minor -- reachable only when one variable reads multiple outputs of the same module instance; latent determinism gap otherwise.

Context

Identified during a 2026-07-17 LTM design review. Tracking epic: #488. Related (different sites): #680 (closed), #595.

Activity

  1. added
    ltm
    on Jul 18, 2026
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

    ltm

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions