Skip to content

ltm: cross-element loops through aggregate nodes always classify as Undetermined (agg-hop link polarity is always Unknown) #516

Closed

Description

@bpowers

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 element X feeds 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 of SUM(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 sign agg enters 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 (the AggNode in src/simlin-engine/src/ltm_agg.rs knows whether it's SUM/MEAN vs MIN/MAX/STDDEV/RANK). For a monotone reducer set polarity(source → agg) = Positive, and derive polarity(agg → target) from the target equation's static polarity w.r.t. the substituted aggregate subexpression (reuse the ltm::polarity analyzer on the target's Expr2, treating the reducer subexpression as the "source"). For a non-monotone reducer leave Unknown.
  • Cheaper interim: at least flip the source → agg hop to Positive for SUM/MEAN (it's unconditionally true and needs no target analysis), so a loop through a SUM/MEAN agg is Undetermined only because of the agg → target hop, not both.

Locations

  • src/simlin-engine/src/db_ltm.rs:2712 -- recover_cross_agg_loops; the circuit_to_links call at db_ltm.rs:2797 (uses the agg-less variable graph) and the Unknown → Undetermined reduction at db_ltm.rs:2804
  • src/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 the agg → target sign (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

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