Skip to content

ltm: LOOKUP index is not held fixed if it shares an element's name #984

Open

Description

@bpowers

A link-score equation must hold a LOOKUP table's index at its previous value, like every other input: LOOKUP(g[idx], x) becomes LOOKUP(g[PREVIOUS(idx)], x).
That works, except when some dimension anywhere in the project has an element with the same name as the index variable.
Then the index is taken for a static element selector and left varying, with no diagnostic.
The index should be resolved against the table's own dimension at that position, the way the compiler does (an element of that axis first, else the variable).

Example

D: A1, A2
B: idx, spare                 <-- declared, used by no equation
g[A1]( (0,0),(1,10),(2,20) )
g[A2]( (0,0),(1,100),(2,200) )
idx    = 1 + MODULO(Time, 2)
inflow = 1
src    = INTEG(inflow, 1)
y      = g[idx](src) + 0*idx
src -> y link-score equation score
B not declared lookup(g[PREVIOUS(idx, idx)], src) 0, 0.053, 0, 0
B declared lookup(g[idx], src) 0, 1.0, -1.0, 1.0

On the per-element path it is worse: the index is rewritten to bucket·ctr, an element of the unrelated dimension, and reads one fixed slot.

Why it matters

A silent wrong number, but only when a variable used as a table index shares its name with an element of some dimension.
Constructed; not seen in a real model.

Fix

Pass the table's declared dimension at each index position into the index pass, from db::variable_dimensions for the table variable.
The table is recorded in referenced_tables, beside the dependencies rather than in them (#606), which is why the existing axis lookup never finds it; freeze_lookup_table_indices passes a literal None and falls back to project-wide "is this an element of any dimension" checks.
generate_per_element_link_equation passes no axis table either.
Small.
Alternative: when the axis is unknown and the name is both a variable and an element, decline the link score with a warning.

Pointers

  • src/simlin-engine/src/ltm_augment.rs: freeze_lookup_table_indices, generate_per_element_link_equation
  • src/simlin-engine/src/ltm_augment_index.rs: wrap_index_non_matching_in_previous, index_axis_verdict
  • src/simlin-engine/src/dimensions.rs: resolve_axis_index_name, is_element_of_any_dimension
  • src/simlin-engine/src/db/ltm_char_tests.rs: arrayed_gf_lookup_mdl, a fixture to extend
  • Related: ltm: index of a held-fixed array read lags two steps instead of one #1060 (an index inside a held-fixed read is lagged twice)

Activity

  1. added
    ltm
    on Jul 20, 2026
  2. bpowers commented on Jul 28, 2026

    @bpowers
    OwnerAuthor

    Still open after the table-index freeze: the index resolution underneath it is project-wide

    The wrap now freezes a LOOKUP table argument's subscript index (freeze_lookup_table_indices,
    landed on the round-trips burndown branch), which fixes the shape this issue's title describes. It
    does not close the issue
    , because the freeze only fires for an index the wrap classifies as a
    runtime read, and that classification is still made by two project-wide predicates. This comment
    records the reproduction and the structural reason, so the next attempt starts here.

    Two corrections to the record while I am here:

    • The claim that this is "not reachable today" because the target's own fragment fails with
      DoesNotExist first is wrong, not merely conservative. It is true only for an index that
      appears nowhere else in the equation. Adding any second reference — + 0*idx is enough — buys
      the index into classify_dependencies' scope and the whole thing compiles.
    • The fix therefore needs a real LOOKUP fixture to be tested at all. An ordinary subscripted
      arrayed variable (q[idx]) takes a different code path and does not exercise this one.

    Reproduction: compiles with zero diagnostics, score moves by 1.0

    {UTF-8}
    D: A1, A2 ~~|
    B: idx, spare ~~|            <-- declared, referenced by NO equation
    g[A1]( (0,0),(1,10),(2,20) ) ~~|
    g[A2]( (0,0),(1,100),(2,200) ) ~~|
    idx = 1 + MODULO(Time, 2) ~~|
    inflow = 1 ~~|  src = INTEG(inflow, 1) ~~|
    y = g[idx](src) + 0*idx ~~|
    
    diagnostics emitted src→y partial simulated link score
    B not declared 0 lookup(g[PREVIOUS(idx, idx)], src) [0, 0.05263, 0, 0]
    B declared 0 lookup(g[idx], src) [0, 1.0, -1.0, 1.0]

    idx is a genuine runtime value. Declaring a dimension that no equation references leaves it live
    inside a partial that is supposed to be ceteris-paribus
    , with no diagnostic in either model. The
    same shape reproduces on the PerElement emitter, where the failure is one step worse — the index is
    rewritten to bucket·ctr, an element of a dimension the indexed variable is not declared over, which
    compiles and statically reads slot 1 forever:

    Region=[nyc,boston]  Age=[young,old]  Slot=[s1,s2]  [Bucket=[ctr,spare]]
    ctr = 1 + (INT(counter) MOD 2)
    q[Slot]: s1 = 1*drive, s2 = 10*drive
    growth[Region] = pop[Region, young] * 0.1 + q[ctr] * 0.002 + 0 * ctr
    
    pop[nyc,young]→growth[nyc] partial simulated score
    Bucket not declared PREVIOUS(q[PREVIOUS(ctr, ctr)]) [0, .7246, .98795, .94652, .99394, .96721]
    Bucket declared PREVIOUS(q[bucket·ctr]) [0, .7246, .98795, **.80214**, .99394, **.81967**]

    Same diagnostic count both ways.

    Why the GH #986 unification does not reach here

    dimensions::resolve_axis_index_name is the engine's single element-vs-dimension precedence rule and
    is what compiler::subscript::normalize_subscripts3 implements. GH #986 unified
    ltm_agg::classify_axis_access and ltm_augment_post_transform::pin_dimension_name_indices onto it;
    the wrap was the third consumer and has now been partially unified too
    (ltm_augment::index_axis_verdict). Partially, because resolving an index needs the axis — the
    declared Dimension at that index's position of the subscripted variable — and the wrap gets it from
    IteratedDimCtx::dep_dims.

    A graphical-function table holder can never be in dep_dims. That table is built from
    variable::classify_dependencies(..).all, and the BuiltinContents::LookupTable arm records the
    holder in referenced_tables and deliberately keeps it off the dependency graph (GH #606 — a
    lookup-only holder is a static table, not a value-bearing variable, and a bare Var reference to one
    is a compile error, so there is no other route into all either). So axis_dim_at always returns
    None on this path, axis_unknown is always true, and the two project-wide predicates
    (dimension_uniquely_containing_element, is_element_of_any_dimension) still decide every table
    index. Nothing in the test suite constrains it: deleting the call site outright leaves 5,435 tests
    green, which is why it is now a literal None with a comment rather than a call that reads as
    coverage.

    generate_per_element_link_equation reaches the same fallbacks for a different reason — it threads
    dep_dims: None, on a comment that was true when other_dep_verdict was that table's only consumer.

    What closing this needs

    The wrap needs the subscripted variable's declared dimensions from the source of truth the compiler
    uses
    (db::variable_dimensions, or the project's DimensionsContext), not from a table of the
    target's array dependencies. Those are different questions and the mismatch is exactly what bounds
    the current fix: "absent from dep_dims" is being read as "no axis exists", when it also means "this
    emitter did not thread the table" and "this kind of reference is not a dependency by construction".

    That is a decision about where this function family gets its dimension truth — it deliberately
    carries none today, which qualify_element_index's rustdoc has said honestly for a long time — so it
    is its own change rather than a rider. Both fixtures above are ready to become its tests; the second
    one is four lines on top of the existing arrayed_gf_lookup_mdl fixture in
    src/simlin-engine/src/db/ltm_char_tests.rs.

    Related: the partial above is not fully ceteris-paribus even once every name resolves, because an
    index frozen inside an already-frozen head is double-lagged (reads the head at t-1 indexed at
    t-2, where the PREVIOUS(target) anchor indexed at t-1). That is a separate semantics question
    and is disclosed on the burndown PR.

  3. bpowers commented on Jul 30, 2026

    @bpowers
    OwnerAuthor

    Partial update from #994, and a correction to one premise in this issue's surrounding analysis. Not claiming this closes it — recording precisely what moved so someone can decide.

    What #994 fixed: a LOOKUP table argument's dimension-name index is now element-pinned in a per-element partial. It previously survived into the scalar fragment and lowering rejected it with DimensionInScalarContext — 413 occurrences on C-LEARN, the largest single bucket of LTM fragment-compile failures.

    The premise that was wrong. The accepted explanation (repeated in src/simlin-engine/CLAUDE.md) was that a graphical-function holder is "by construction absent from dep_dims, so axis_dim_at can never resolve its axis." That conflates two things. The holder is deliberately kept out of the dependency graph — classify_dependencies' LookupTable arm records it in referenced_tables and keeps it off all (variable.rs:1150-1156, GH #606) so a table reference creates no runlist entry and no causal edge. Its dimensions were never unavailable: referenced_tables rides the same struct returned by the same single AST pass as all, one field away.

    So the fix did not need any new plumbing. It widens the per-element pin candidate list with referenced_tables, and deliberately leaves the wrap's dependency set alone — the separation is load-bearing for an attribution reason (a table cannot vary, so it earns no score and no edge, only an index).

    What this issue still covers, as far as I can tell: the runtime index case in the opening example — LOOKUP(g[idx], x) where idx is a genuine value read that should be frozen. That is a different thing from a dimension-name index needing to be pinned, and #994 did not touch it. freeze_lookup_table_indices exists for the freezing half; whether it now covers the runtime case completely is not something I verified.

    Also worth knowing before further work here: a broader design alternative was measured and rejected. Putting table references into all for generality breaks 14 lib tests, and the first thing that breaks is compilation itself — a lookup-only holder has no value slot and is excluded from every runlist, so the fragment compiler cannot treat it as a dependency. Five consumer families see them; four must filter, one wants them. The safe default is the current one (absent, opt in), because the failure directions are asymmetric: a consumer that needs tables and does not get them fails loudly, while a forgotten opt-out mints a causal edge for a variable that cannot vary — a wrong number rather than a crash.

    variable.rs's referenced_tables doc now records all of this, including that the third consumer (dimension resolution) should read that field rather than expect it in all.

  4. added
    bug
    on Oct 12, 2026
  5. changed the title [-]ltm: the ceteris-paribus wrap leaves a LOOKUP table argument's runtime index live in every link-score partial[/-] [+]ltm: LOOKUP index is not held fixed if it shares an element's name[/+] on Oct 12, 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

    bugltm

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions