Repository navigation
Description
Summary
Every cross-element loop recovered through a synthetic aggregate node ($⁚ltm⁚agg⁚{n}) is classified LoopPolarity::Undetermined, because the aggregate-hop links are always assigned LinkPolarity::Unknown.
recover_cross_agg_loops (in src/simlin-engine/src/db_ltm.rs:2712) builds its element-subscripted links via var_graph.circuit_to_links(&var_level_nodes) (db_ltm.rs:2797), where var_graph is the variable-level causal graph -- which has no aggregate node (aggs live only in the element-level graph). So get_link_polarity(X, agg) and get_link_polarity(agg, Y) both miss (the agg ident appears in no model variable's AST) and return LinkPolarity::Unknown. Then the loop-polarity reduction at db_ltm.rs:2804:
let polarity = if links.iter().any(|l| l.polarity == LinkPolarity::Unknown) {
LoopPolarity::Undetermined
} else { ... };forces Undetermined for every recovered cross-agg loop, since every such loop has at least two agg-hops.
Why this matters
For a monotone reducer (SUM, MEAN) the polarity of the agg hops is actually derivable:
polarity(X → agg) = Positive-- SUM/MEAN is monotone increasing in every element it reads, so increasing the elementXfeeds increases the aggregate. (This is exactly the property ltm: polarity analysis returns Unknown for all array reducers #480 / tech-debt item fix new user creation flow #21 want to exploit for static polarity analysis ofSUM(x[*])user-variable expressions; here it's the same fact, applied to the synthetic agg's incoming edge.)polarity(agg → target)= the target equation's polarity with respect to the aggregate (whatever signaggenters the target's RHS with).
So cross-element loops through SUM/MEAN aggregates could get a real Reinforcing/Balancing classification instead of always Undetermined. (For non-monotone reducers -- MIN/MAX/STDDEV/RANK -- Unknown is correct and should stay.)
This is silent loss of information, not a correctness bug: the loop scores are still right, but the polarity column for these loops is uninformative even when it didn't have to be.
Relationship to existing tracking
Overlaps the same root fact as tech-debt item #21 ("LTM Polarity Analysis Has Reducer Blind Spots", docs/tech-debt.md) and #480 -- both about SUM/MEAN being trivially monotone -- but the site is different: #480 is about analyze_expr_polarity_with_context in src/simlin-engine/src/ltm/polarity.rs returning Unknown for a user variable whose RHS is SUM(x[*]); this issue is about recover_cross_agg_loops in db_ltm.rs building the synthetic agg's incoming/outgoing link polarity off a graph that doesn't contain the agg. Tracking separately so the agg-hop site isn't lost; the fix may share a helper with #480 (a "polarity of a monotone reducer's incoming edge" predicate).
Possible approaches
- In
recover_cross_agg_loops, after building the links, post-process the agg hops: look up the agg's reducer kind (theAggNodeinsrc/simlin-engine/src/ltm_agg.rsknows whether it's SUM/MEAN vs MIN/MAX/STDDEV/RANK). For a monotone reducer setpolarity(source → agg) = Positive, and derivepolarity(agg → target)from the target equation's static polarity w.r.t. the substituted aggregate subexpression (reuse theltm::polarityanalyzer on the target'sExpr2, treating the reducer subexpression as the "source"). For a non-monotone reducer leaveUnknown. - Cheaper interim: at least flip the source → agg hop to
Positivefor SUM/MEAN (it's unconditionally true and needs no target analysis), so a loop through a SUM/MEAN agg isUndeterminedonly because of theagg → targethop, not both.
Locations
src/simlin-engine/src/db_ltm.rs:2712--recover_cross_agg_loops; thecircuit_to_linkscall atdb_ltm.rs:2797(uses the agg-less variable graph) and theUnknown → Undeterminedreduction atdb_ltm.rs:2804src/simlin-engine/src/ltm_agg.rs--AggNode(carries the reducer kind needed to decide monotone vs not)src/simlin-engine/src/ltm/polarity.rs--analyze_link_polarity/ the static polarity analyzer that could derive theagg → targetsign (shares the SUM/MEAN-monotone fact with ltm: polarity analysis returns Unknown for all array reducers #480)
Discovery context
Identified during code review of Phase 5 ("reducer hoist / aggregate nodes") of the "LTM Cross-Element Aggregate Scoring" implementation plan (branch ltm-503-cross-element-agg; design plan under docs/implementation-plans/2026-05-09-ltm-503-cross-element-agg/). Acknowledged trade-off, not a Phase 5 blocker.
Tracking
Part of LTM tracking epic: #488. Cross-references tech-debt item #21 and GH #480 (LTM polarity reducer blind spots) -- same underlying SUM/MEAN-is-monotone fact, different code site (agg-hop link polarity in loop recovery vs static polarity of SUM(x[*]) user variables).
Activity
engine: fix LTM agg-hop polarity and inlined-reducer PREVIOUS wrapping