Skip to content

ltm: model_element_causal_edges' both-scalar fast path bypasses ThroughAgg routing (scalar feeder of a hoisted reducer with a scalar target loses the agg hop) #533

Closed

Description

@bpowers

Summary

model_element_causal_edges in src/simlin-engine/src/db_analysis.rs (#[salsa::tracked], ~line 1417) projects each variable-level (from, to) causal edge into element-level edges by reading the db_ltm_ir classified sites for that pair and dispatching on each site's routing (Direct → emit_edges_for_reference; ThroughAgg { agg } → emit_agg_routed_edges). But before that dispatch there is a both-scalar fast path (~lines 1422-1429):

// Fast path: both scalar -> direct edge.
if from_dims.is_empty() && to_dims.is_empty() {
    element_edges
        .entry(from_name.clone())
        .or_default()
        .insert(to_name.clone());
    continue;
}

When the edge source and target are both scalar, this emits a direct from → to element edge and continues -- it never consults ir.sites, so the ThroughAgg routing introduced by the #520 reference-site IR is bypassed for the scalar→scalar case.

Consequence

Consider a scalar variable that feeds a hoisted reducer subexpression where the reducer's target is also scalar -- e.g. total = base + SUM(pop[*] * scale) where total and scale are scalar and pop[*] is arrayed, so SUM(pop[*] * scale) is hoisted into a synthetic scalar agg $⁚ltm⁚agg⁚0. The (scale, total) causal edge is classified ThroughAgg { agg } in the IR, but model_element_causal_edges takes the both-scalar fast path and emits a direct scale → total element edge instead of scale → $⁚ltm⁚agg⁚0 + $⁚ltm⁚agg⁚0 → total. (The agg node itself is still wired correctly via the arrayed source side: pop[d] → $⁚ltm⁚agg⁚0 and $⁚ltm⁚agg⁚0 → total edges exist, since those edges aren't both-scalar. It's only the scalar-feeder→agg hop that is wrong/missing.)

So a feedback loop that passes through a scalar feeder of a hoisted reducer -- when the reducer's target is scalar -- could be invisible at the element-graph level: the path ... → scale → $⁚ltm⁚agg⁚0 → total → ... is broken at scale → agg (replaced by scale → total, which skips the agg). The loop-score chain (db_ltm.rs) that walks ... → from → $⁚ltm⁚agg⁚{n} → to → ... would not find the agg hop on that segment either.

Why it matters

Low-to-medium. The agg node is wired (via its arrayed source), so most loops through a hoisted reducer are still found; only a loop that specifically routes through a scalar-feeder→agg hop -- which is unusual -- is affected, and only when the reducer's target is also scalar (an arrayed target avoids the fast path entirely, because to_dims is non-empty). Never a crash; a silent loss of one element-graph edge (an agg hop replaced by a direct edge that skips it). The companion scalar-feeder fix in commit 7f837928 (emit_agg_routed_edges's empty-from_dims arm) handles the scalar-feeder→arrayed-agg-with-arrayed-target case correctly; this is the remaining scalar-feeder→scalar-agg-with-scalar-target case, which never reaches emit_agg_routed_edges because of the fast path.

This was surfaced when an IMPORTANT #2 fix test in the Phase 4 review had to use an arrayed target instead of a scalar one to avoid this fast path.

Components affected

  • src/simlin-engine/src/db_analysis.rs -- model_element_causal_edges (the both-scalar fast path near the top of the per-edge loop, ~lines 1422-1429), emit_agg_routed_edges (the ThroughAgg emitter the fast path skips; its empty-from_dims arm at ~line 571 already handles the scalar-feeder case once it's reached -- it just isn't reached for a scalar→scalar edge)
  • src/simlin-engine/src/db_ltm_ir.rs -- model_ltm_reference_sites (where the ThroughAgg routing for the (scale, total) site is decided)

Possible approaches

  • Don't take the both-scalar direct-edge fast path when the IR's classified sites for that (from, to) pair say the site routes ThroughAgg: when ir.sites.get(&edge_key) has a ThroughAgg site, fall through to the normal dispatch (which calls emit_agg_routed_edges, whose empty-from_dims arm already emits scale → agg / scale → agg[<slot>] correctly) instead of emitting a direct edge. Keep the fast path only when there is no ThroughAgg site for the pair (the common case -- two scalars with a plain Direct reference).
  • This naturally folds into ltm: unify the element-graph and link-score reference-site walkers behind a single classification IR #520 (unify the element-graph and link-score reference-site walkers behind one classification IR): once model_element_causal_edges consults the IR uniformly for every edge -- including scalar→scalar ones -- the fast path either disappears or becomes "the IR said Direct Bare, so emit the one obvious edge".

Discovery context

Identified during the Phase 4 code review of the LTM arrays hardening epic (#488; commit 7f837928 on branch ltm-arrays-hardening; design plan and per-phase tasks under docs/implementation-plans/2026-05-11-ltm-arrays-hardening/, see phase_04.md -- the sliced-reducer-hoisting / emit_agg_routed_edges work). The companion scalar-feeder fix in that commit handles the arrayed-target case; this is the scalar-target case the fast path still short-circuits. Not a Phase-4 regression -- the fast path predates the agg-routing machinery; #514's ThroughAgg routing just made it visible.

Tracking

Part of LTM tracking epic #488. Related to: #514 (sliced-reducer hoisting -- the Phase-4 work that introduced ThroughAgg routing through synthetic aggs; this is a gap in how model_element_causal_edges consumes that routing for the scalar→scalar edge case), #520 (unify the reference-site walkers -- the refactor that would dissolve the fast-path special case), #528 (agg→target link-score over-subscripts an arrayed synthetic agg -- the same "the element-graph and the rest must agree about agg routing" theme, a different site), and the LTM array-support umbrella #273.

Activity

  1. added
    ltm
    on May 13, 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