Repository navigation
Description
Problem
LTM has a typed causal-access IR on the original model's Expr2 AST, but it still represents generated equations as source-facing datamodel::Equation strings and transforms them by re-parsing Expr0.
The seam is visible in three places:
LtmSyntheticVar::equationis adatamodel::Equation, even though these equations are internal compiler artifacts rather than persisted/user-authored equations.ltm_augment.rshas tenExpr0::newcall sites for ceteris-paribus wrapping, reducer substitution, row pinning, and related transforms. The resulting AST is generally printed back to text;db/ltm/parse.rs::parse_ltm_equationlater wraps that text in a transient datamodel aux and parses it again for compilation. ltm: C-LEARN LTM compile-time dominated by per-fragment fixed overhead (DimensionsContext clone, O(D^2) intern_name, repeated parse) #655 separately measures the repeated parse cost.- Access shape is decided twice.
db/ltm_ir.rs::{resolve_literal_index,classify_subscript_shape,classify_iterated_dim_shape}classify the model'sExpr2reference sites for causal edges/routing.ltm_augment.rs::{resolve_literal_element_index,classify_expr0_subscript_shape,...}re-derive the same facts from re-parsedExpr0so the partial builder can identify the occurrence to keep live.
The code documents the invariant directly: the Expr0 and Expr2 siblings "must stay in sync"; disagreement can cause the edge emitter and partial builder to choose different shapes, wrap the intended live reference in PREVIOUS(), and silently zero or corrupt a link score.
Why simplify this
#520 made the element graph, edge-shape query, and link-score routing consume one ClassifiedSite IR. That solved duplication among Expr2 consumers, but the actual equation transformer remains outside that IR and has no stable identity for the classified reference occurrence. It therefore parses printed text and reconstructs shape semantics.
This boundary has repeatedly amplified bugs:
- parser/printer asymmetries can drop LTM attribution (engine: three more print_eqn/parser asymmetries (unary
!,!=, and right-nested^re-parsing to a different AST) #913); - Expr0/Expr2 classification changes require paired implementations and comments/tests to prevent drift (for example ltm: iterated+literal mixed subscript keeps the cross-product element expansion -- phantom cross-element loops carry silent nonzero scores (rescoped: hard compile failure fixed) #525 and the indexed-literal synchronization comments in both classifiers);
- subscript-index context has been re-discovered inside the text transform after causal classification already knew the access (ltm-augment: partial builders PREVIOUS-wrap dimension-name subscript indices (Wildcard and Bare shapes), silently stubbing scores #759);
- generated equations are parsed 2–3 times during assembly (ltm: C-LEARN LTM compile-time dominated by per-fragment fixed overhead (DimensionsContext clone, O(D^2) intern_name, repeated parse) #655).
Each local fix can be correct, but the architecture keeps creating the same synchronization burden.
Proposed direction
Introduce one typed LTM transformation/causal-access representation that connects reference classification to equation generation.
A possible shape is:
struct LtmEquation {
dimensions: LtmDimensions,
expr: LtmExpr,
}
struct ClassifiedSite {
id: SiteId, // stable occurrence/path in the expression
access: AccessPattern, // per-axis iterated/pinned/reduced/dynamic
routing: SiteRouting, // direct or through a specific agg
// existing target-element/reducer metadata as needed
}The important property is not these exact types. It is that the ceteris-paribus/reducer transforms select occurrences by the same site identity and access metadata that causal-edge emission used, instead of reclassifying a printed Expr0.
Then:
- Build the reference-site/access IR once from the parsed/lowered model equation.
- Express
PREVIOUSwrapping, reducer substitution, pinning, guard forms, and generated link/loop equations as typed AST transforms/builders. - Carry typed synthetic equations through LTM compilation. Serialize only for diagnostics/debugging or at an unavoidable source-format boundary; do not round-trip through
datamodel::Variable::Auxmerely to compile an internal equation. - Remove the parallel Expr0 literal/subscript classifiers and their manual synchronization contract.
This can be staged: first retain Expr0 as the generated-equation AST but parse the source equation once and attach stable site paths; then teach the fragment compiler to accept typed synthetic equations; finally collapse or remove the Expr0/Expr2 bridge. A dedicated LtmExpr may be cleaner if neither public AST level safely represents generated-only constructs.
Acceptance criteria
- One representation is authoritative for both causal access/routing and selecting reference occurrences during LTM equation transformation.
resolve_literal_element_index/classify_expr0_subscript_shapeno longer duplicate the Expr2 classifier, and no correctness comment requires the two implementations to stay synchronized.- Internal
LtmSyntheticVarequations are stored/compiled as typed expressions; normal operation does not print and re-parse them throughdatamodel::Equation. - Existing scalar, arrayed, mapped-dimension, fixed-index, dynamic-index, and aggregate-node LTM golden/numeric behavior remains unchanged.
- Tests exercise the shared access/transform semantics directly; parser/printer round-trip tests remain useful defense for genuine text boundaries, not an LTM correctness dependency.
Relationship to existing work
- Follow-up to ltm: unify the element-graph and link-score reference-site walkers behind a single classification IR #520: that issue unified Expr2-side reference-site consumers; this issue removes the remaining Expr2-to-Expr0 duplication at the transformation/compiler boundary.
- Overlaps ltm: C-LEARN LTM compile-time dominated by per-fragment fixed overhead (DimensionsContext clone, O(D^2) intern_name, repeated parse) #655 finding 3 on parse cost, but the primary goal here is semantic simplification and a single source of truth. ltm: C-LEARN LTM compile-time dominated by per-fragment fixed overhead (DimensionsContext clone, O(D^2) intern_name, repeated parse) #655 still owns the broader fragment-compilation performance work.
- No protobuf or persisted-format change is required; this is an internal engine refactor.
Part of the LTM epic #488.
Activity
engine: delete the LTM occurrence fields nothing reads
engine: delete the LTM input-side round trip and the last Expr0 class…