Skip to content

LTM scalar FFI returns element-0 only for arrayed A2A loops #463

Closed

Description

@bpowers

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): calls engine::ltm_post::compute_rel_loop_scores(results, &ltm.loop_partitions) and returns the resulting Vec<f64> for the requested loop_id. For A2A loops, compute_rel_loop_scores reads row[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 -- calls ltm_post::compute_rel_loop_scores(&results, &loop_partitions) and maps the single-slot series into FeedbackLoop::importance_series for each DetectedLoop.
  • 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): normalizes row[off] against row[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

  1. Extend compute_rel_loop_scores to accept a per-loop slot count (available via LtmVariablesResult::vars[i].dimensions -> Vec<String> in src/simlin-engine/src/db.rs:1960, which can be cross-referenced against datamodel::Dimension element counts). Alternative: piggy-back on loop_partitions by upgrading the value type to (Option<usize>, usize /* n_slots */).
  2. Return a 2-D series per loop: HashMap<String, Vec<Vec<f64>>> (outer indexed by step, inner by element), or a flat Vec<f64> of length step_count * n_slots with the layout documented.
  3. 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 an element_index parameter, or expose n_slots alongside the series so callers can reshape.
  4. Update src/simlin-engine/src/layout/mod.rs:3866 to build FeedbackLoop::importance_series per-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)
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