Skip to content

ltm: composite-reference link score into a stdlib-macro module fails to compile in exhaustive mode, so loops through SMOOTH/DELAY stub to 0 #548

Closed

Description

@bpowers

Summary

In non-discovery (exhaustive) LTM mode, an LTM link score whose target is a stdlib-macro module instance (SMTH1/SMTH3/DELAY1/DELAY3/TREND/NPV/...) is generated as a composite-reference equation -- "{module}·$⁚ltm⁚composite⁚{port}" -- but that fragment fails to compile in compile_ltm_equation_fragment, so assemble_module silently drops it. The synthetic link-score variable keeps its layout slot, no bytecode writes it, and it reads a constant 0 for the entire simulation. Because the loop score for any loop through that macro is the product of its link scores, the whole loop score collapses to 0.

Net effect: in exhaustive mode, any feedback loop that passes through a stdlib macro (SMOOTH/DELAY/TREND/NPV) gets a silently-zero loop score.

This is the same hazard class as #546 (empty tables map -> link scores into graphical-function targets stub to 0) and the arrayed flow-to-stock link score that originally motivated model_ltm_fragment_diagnostics -- "the fragment compiler can't handle a particular target shape, the var stubs to 0, the model still simulates, so the degraded score masquerades as correct" -- but the root cause here is distinct from #546 and is currently untracked.

Root cause (traced)

The link level → $⁚smoothed_level⁚0⁚smth1 (level a stock, target the implicit SMTH1 module instance) takes the !from_is_module && to_is_module branch in link_score_equation_text_shaped (src/simlin-engine/src/db_ltm.rs ~lines 342-393). level is found in the module's inputs, and since we are not in discovery mode it emits the composite-reference form (db_ltm.rs ~lines 358-364):

format!(
    "\"{module}\u{00B7}$\u{205A}ltm\u{205A}composite\u{205A}{port}\"",
    module = to_ident.as_str(),   // "$⁚smoothed_level⁚0⁚smth1"
    port = input.dst.as_str(),    // "input"
)

-> equation text "$⁚smoothed_level⁚0⁚smth1·$⁚ltm⁚composite⁚input".

This references a synthetic composite-score variable that lives inside the LTM-augmented submodel. The full assembly path (assemble_module, src/simlin-engine/src/db.rs ~lines 2098-2126 generate it, ~3712 resolves module·var deps) does arrange for the submodel to be LTM-augmented, so the composite var $⁚ltm⁚composite⁚input genuinely exists in the final layout (verified: the slot is allocated). But the standalone fragment compiler compile_ltm_equation_fragment (db_ltm.rs ~line 557) cannot resolve a module·$⁚ltm⁚composite⁚port dependency: when it builds the Variable::Module for the dependency and registers implicit_submodels, the submodel it reconstructs is the un-LTM-augmented stdlib⁚smth1, which has no $⁚ltm⁚composite⁚input variable. The dep is unresolvable, the fragment fails to compile, assemble_module drops it, and the var is a constant 0.

compile_ltm_synthetic_fragment dispatches this case -- a scalar link score with no element subscript, no synthetic-agg endpoint, and compile_directly: false -- to the salsa-cached (from, to)-keyed compile_ltm_var_fragment path (not compile_direct()); that path re-derives the equation via the legacy link_score_equation_text (whose module-target branch emits the same composite-reference form) and then calls compile_ltm_equation_fragment. Either way the failure surfaces inside compile_ltm_equation_fragment.

Verified behavior (probe on build_smooth_loop_with_failing_fragment)

For the fixture build_smooth_loop_with_failing_fragment in src/simlin-engine/src/db_ltm_unified_tests.rs (level -> smoothed_level (SMTH1) -> gap -> adjustment -> level), with ltm_enabled = true, exhaustive mode:

  • $⁚ltm⁚link_score⁚level→$⁚smoothed_level⁚0⁚smth1: equation "$⁚smoothed_level⁚0⁚smth1·$⁚ltm⁚composite⁚input", compile_ltm_synthetic_fragment -> fails to compile (flow_bytecodes: None).
  • Full project compile succeeds and the variable gets an offset slot.
  • In the actual simulation the link score is [0.0, 0.0, 0.0, ...] (constant 0) -- assemble_module silently dropped the fragment.
  • Consequently $⁚ltm⁚loop_score⁚b1 (the product of all five link scores in the loop) is also constant 0 -- even though the other four link scores compute fine.
  • The submodel stdlib⁚smth1, when LTM-augmented, does carry $⁚ltm⁚composite⁚input and $⁚ltm⁚composite⁚delay_time -- i.e. the referenced variable is real; only the standalone fragment compile can't see it.

Discovery mode is unaffected

In discovery mode the same link routes through black_box_delta_ratio_equation instead, which references $⁚smoothed_level⁚0⁚smth1·output -- the module's real output port -- and that compiles fine (verified: flow_bytecodes present). The bug is specific to non-discovery / exhaustive mode, which is the default exhaustive LTM path.

Component(s) affected

  • src/simlin-engine/src/db_ltm.rs -- link_score_equation_text_shaped (composite-reference emission, ~lines 358-364), compile_ltm_equation_fragment (~line 557; the module·var dependency-resolution path, ~lines 762-990, does not LTM-augment the reconstructed submodel), compile_ltm_synthetic_fragment (~line 1328).
  • src/simlin-engine/src/db.rs -- assemble_module composite-reference dep handling (~line 3712) for the contrast: the full path can resolve it.
  • Downstream: every LTM link/loop/relative-loop score for an exhaustive-mode loop that passes through a SMOOTH/DELAY/TREND/NPV stdlib macro.

Why it matters

Suggested fix direction

The composite-reference equation is correct; what is missing is making the standalone fragment compiler resolve a module·$⁚ltm⁚composite⁚port dependency the same way assemble_module does. Options:

  1. LTM-augment the reconstructed submodel in the fragment-compile path. When compile_ltm_equation_fragment reconstructs an implicit/explicit module dependency and registers it in implicit_submodels, the submodel's compiled form needs to include its LTM synthetic variables ($⁚ltm⁚composite⁚*, etc.) so the module·composite_var reference resolves -- mirroring whatever assemble_module does to make the full path work.
  2. Treat a module·$⁚ltm⁚composite⁚port reference as a known layout-dependent resolution (analogous to the documented sub-model dedup case that model_ltm_fragment_diagnostics deliberately leaves silent) -- but only if the full assembly path genuinely produces a correct non-zero score, which the probe shows it does not here (the score is 0 in the real sim). So option 2 alone would just hide a real bug; the score must actually be computed.

The fix should be paired with a regression test that asserts the loop score for a SMOOTH-in-loop model is non-zero in exhaustive mode (the current build_smooth_loop_with_failing_fragment fixture would need to be repurposed or replaced -- see #547, which already proposes a test-injection hook so the fragment-diagnostics positive tests stop depending on this bug's lifetime).

How it was discovered

Identified during an adversarial review of the LTM-hardening effort, while tracing why build_smooth_loop_with_failing_fragment produces a fragment-compile failure. #547 (test coupling) and #546 (GF-tables, "names confirming the GF pattern ... alongside some SMOOTH-macro-internal ones") both gesture at this SMOOTH-internal compile failure but neither tracks it: #547 is about the fixture's coupling and #546's fix (threading the tables map) would not resolve the composite-reference resolution failure.

Linkage

Activity

  1. added
    ltm
    on May 14, 2026
  2. added a commit that references this issue on Jun 1, 2026
  3. bpowers commented on Jun 2, 2026

    @bpowers
    OwnerAuthor

    Fixed by e012ebc (PR #648; the commit message notes the fix was not auto-closed). build_submodel_metadata (src/simlin-engine/src/db/assemble.rs) now registers a sub-model's LTM composite vars at their layout offsets, so the standalone fragment compiler resolves the {module}·$⁚ltm⁚composite⁚{port} reference instead of dropping it.

    Verified empirically on main (521fc37): smooth_macro_composite_flows_into_parent_loop_score_gh548 and macro_in_loop_score_varies_over_run_gh548 pass (SMOOTH and DELAY in-loop link scores are non-zero and vary over the run). The coupled #547 fixture was retargeted at the still-open #528 broadcast case, decoupling the diagnostic tests from this bug's lifetime.

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