Skip to content

ltm: source->agg link score wrong-signed for hoisted multi-source reducer with sign-flipping coefficient (SUM linear shortcut ignores the body's coefficient) #744

Closed

Description

@bpowers

Summary

The per-row source[e] -> $⁚ltm⁚agg⁚{n} runtime link-score series is wrong-signed (and wrong-magnitude) when a hoisted multi-source reducer's body applies a non-unit coefficient to that source. The SUM/MEAN linear-shortcut partial scores each co-source's row as if the reducer body were the bare source element (implicit ∂agg/∂source[e] = 1), ignoring the body's coefficient on that source -- which can be negative.

Repro fixture

grow            = 1 + SUM(pop[*] * (1 - weight[*]))
weight[region]  = 0.001 * total + 0.01      ; 1 - weight stays positive
total           = INTEG(grow, ...)           ; stock closing the loop

The reducer SUM(pop[*] * (1 - weight[*])) is hoisted as a multi-source agg: combined_read_slice (src/simlin-engine/src/ltm_agg.rs) accepts pop and weight as co-sources (both axes [Reduced], slices agree), so each gets per-row source[e] -> agg link scores.

The true partial is ∂agg/∂weight[e] = -pop[e] < 0, yet the runtime weight[e] -> agg link-score series is sustained POSITIVE (~ +0.0027). Any loop through the weight -> agg hop gets a wrong-signed runtime score contribution, and runtime polarity reclassification then confirms (rather than corrects) a wrong static label.

Verified pre-existing: identical at ltm-core-batch HEAD a14a6bee and at the branch base (effectively main); NOT a regression of the ltm-core-batch work.

Root cause

  • emit_source_to_agg_link_scores (src/simlin-engine/src/db/ltm/link_scores.rs, ~line 921; the per-row calls at ~1038/~1054) classifies the agg's reducer via classify_reducer(&agg_var, from) and calls generate_element_to_scalar_equation / generate_element_to_reduced_equation with a hardcoded is_bare = true.
  • classify_reducer_in_expr -> classify_builtin_if_references_source (src/simlin-engine/src/ltm_augment.rs, ~2955-3015) classifies SUM/MEAN as ReducerKind::Linear whenever the reducer argument references the source anywhere -- it never checks the argument IS the bare source element.
  • generate_linear_partial (ltm_augment.rs, ~3259) then emits PREVIOUS(agg) + (source[e] - PREVIOUS(source[e])) (MEAN divides by N), i.e. it asserts ∂agg/∂source[e] = 1.
  • The existing is_bare guard in build_element_reducer_link_score (~3214) only covers arithmetic AROUND the reducer (2 * SUM(...)); arithmetic INSIDE the reducer argument is unguarded. For SUM(pop[*] * (1 - weight[*])) the shortcut is wrong for BOTH co-sources: for weight[e] the true coefficient is -pop[e] (sign flip -> wrong-signed series); for pop[e] it is (1 - weight[e]) (sign preserved while positive, magnitude wrong).

Why it matters

Medium severity: silent wrong-signed link scores for a legal, hoisted multi-source reducer shape. The wrong sign propagates into every loop score through that hop, and into runtime loop-polarity (re)classification -- the runtime check rubber-stamps a wrong static polarity instead of correcting it.

Possible approaches

  • In emit_source_to_agg_link_scores, detect that the reducer argument is not the bare source reference (an expression over the source / multiple co-sources) and fall back from the linear shortcut to a true ceteris-paribus partial: hold the OTHER co-sources and the other rows of from at PREVIOUS while from[e] varies (the same selective-PREVIOUS machinery generate_nonlinear_partial / build_partial_equation_shaped already use), instead of the bare-element delta.
  • Alternatively (cheaper, conservative): extend classify_reducer to return Linear only when the reducer argument is exactly the bare source subscript expression for THIS source; otherwise treat it like the existing !is_bare delta-ratio fallback. This fixes the sign at the cost of per-row attribution precision.
  • Either way, a regression test with the fixture above asserting the weight[e] -> agg series sign matches SIGN(-pop[e]).

Cross-references (related, NOT duplicates)

Discovery context

Identified during adversarial review of the ltm-core-batch branch (out of scope for that batch; filed for tracking).

Activity

  1. added
    ltm
    on Jun 10, 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