Repository navigation
Description
Problem
Both the libsimlin C FFI and the engine's layout metadata path silently read only slot 0 of a loop's $⁚ltm⁚loop_score⁚{id} series for A2A (apply-to-all) arrayed loops, returning a single f64 series per loop_id instead of one series per element. For arrayed-LTM users (pysimlin, TypeScript WASM bindings, and layout feedback-loop importance metadata) this silently discards per-element attribution and reports a misleading scalar instead.
Evidence
src/libsimlin/src/analysis.rs:453(simlin_analyze_get_relative_loop_score): callsengine::ltm_post::compute_rel_loop_scores(results, <m.loop_partitions)and returns the resultingVec<f64>for the requestedloop_id. For A2A loops,compute_rel_loop_scoresreadsrow[off](no element offset added), so only slot 0 is exposed.src/simlin-engine/src/layout/mod.rs:3866(feedback-loop importance series): same pattern -- callsltm_post::compute_rel_loop_scores(&results, &loop_partitions)and maps the single-slot series intoFeedbackLoop::importance_seriesfor eachDetectedLoop.src/simlin-engine/src/ltm_post.rs::compute_rel_loop_scores(introduced/consolidated in PR engine: enable LTM always-on via auto-flip + post-sim rel_loop_score #461): normalizesrow[off]againstrow[off].abs()sums within a partition. The signature takes only&HashMap<String, Option<usize>>(partition map) -- no slot-count input -- so the function has no way to iterate per-element.
This is pre-existing behavior, not a regression from PR #461. git show origin/main:src/libsimlin/src/analysis.rs (prior to #461) shows the same pattern reading row[offset] for the synthetic rel_loop_score variable; PR #461 merely relocated the computation from compile-time synthetic emission to post-sim evaluation and preserved the scalar-collapse.
Test evidence
The test suite already acknowledges this gap via a per-element helper compute_rel_loop_scores_per_element in src/simlin-engine/tests/simulate_ltm.rs:3024 (~line 3014 depending on revision). It takes an additional n_slots_by_loop: &HashMap<String, usize> argument and returns a flat per-step per-slot series keyed by loop_id.
The helper is used only by two integration tests:
test_a2a_two_loop_relative_scores_sum_to_100(line ~3253)test_arrayed_population_ltm_exhaustive(line ~3824)
Production callers (libsimlin::analysis, layout::mod) have no equivalent. A comment at line ~3253 explicitly notes the production helper "collapses to element 0".
Why it matters
Arrayed LTM models are one of the main users of LTM analysis: a modeler running a multi-region population model wants to know which loop dominates in NYC vs LA at t=10, not a single scalar that happens to reflect slot 0 (NYC) only. The current scalar FFI / layout metadata silently misattributes dominance, producing results that look plausible but are wrong for any A2A loop with heterogeneous per-element dynamics.
Severity: medium. Pre-existing, so it does not block PR #461. But affects all arrayed-LTM consumers and the fix path is clear.
Proposed fix
- Extend
compute_rel_loop_scoresto accept a per-loop slot count (available viaLtmVariablesResult::vars[i].dimensions->Vec<String>insrc/simlin-engine/src/db.rs:1960, which can be cross-referenced againstdatamodel::Dimensionelement counts). Alternative: piggy-back onloop_partitionsby upgrading the value type to(Option<usize>, usize /* n_slots */). - Return a 2-D series per loop:
HashMap<String, Vec<Vec<f64>>>(outer indexed by step, inner by element), or a flatVec<f64>of lengthstep_count * n_slotswith the layout documented. - Update the FFI signature in
src/libsimlin/src/analysis.rs:479(simlin_analyze_get_relative_loop_score/simlin_analyze_get_rel_loop_score) to expose a per-element result -- e.g. add anelement_indexparameter, or exposen_slotsalongside the series so callers can reshape. - Update
src/simlin-engine/src/layout/mod.rs:3866to buildFeedbackLoop::importance_seriesper-element (or aggregate if the layout consumer only wants scalar importance -- but that aggregation should be explicit, not a silent slot-0 read).
The existing compute_rel_loop_scores_per_element test helper in src/simlin-engine/tests/simulate_ltm.rs:3024 is the reference shape for per-element normalization logic, including:
- Max-slot broadcasting within a partition (scalar loops broadcast slot 0 when mixed with A2A loops).
- Partition denominator computed per-element:
sum_j |loop_score_j[k]|.
Discovery context
Identified during adversarial review of PR #461 (branch ltm-perf-enable-always), which moved rel_loop_score from compile-time synthetic variables to post-sim computation in ltm_post::compute_rel_loop_scores. The move preserved scalar-FFI collapse from the prior implementation; reviewer flagged that both the new and old paths silently truncate A2A arrayed loops to slot 0 despite the engine already computing per-element loop scores internally.
Files
src/simlin-engine/src/ltm_post.rs(compute_rel_loop_scores)src/libsimlin/src/analysis.rs:453,479(scalar FFI path)src/simlin-engine/src/layout/mod.rs:3866(layout feedback-loop importance)src/simlin-engine/tests/simulate_ltm.rs:3024(reference per-element helper)src/simlin-engine/src/db.rs:1960(LtmSyntheticVar.dimensions-- source of per-loop slot count)
Activity
engine: close LTM auto-flip gaps flagged in review
engine: close LTM auto-flip gaps flagged in review