Repository navigation
Description
Summary
rank_and_filter() in src/simlin-engine/src/ltm_finding.rs ranks discovered loops -- and truncates the set to MAX_LOOPS (200) -- using each loop's raw avg_abs_score, the mean absolute loop score over the run. Raw avg_abs_score is an absolute magnitude, not a relative contribution, so the "most important loops" selection is biased toward whichever cycle partition happens to have the largest-magnitude scores.
Where
src/simlin-engine/src/ltm_finding.rs:avg_abs_scorecomputed at lines ~822-826 (abs_score_sum / valid_count), before cycle partitions are known.rank_and_filterat lines ~890-968:- initial sort by
avg_abs_scoredescending (lines ~892-896) found_loops.truncate(MAX_LOOPS)(line ~899) -- keyed on that sort- final re-sort by
avg_abs_scoredescending for callers (lines ~963-967)
- initial sort by
- The only partition-aware step is the
MIN_CONTRIBUTIONretention filter (lines ~901-957), which dividesscore.abs()bypartition_totals-- but that is a keep/drop threshold, not a ranking key.
Why this is a problem
LTM importance is partition-scoped: a loop's significance is its contribution relative to the other loops in its own cycle partition (cf. relative loop scores). The principled ranking key is the loop's relative contribution within its cycle partition.
Because rank_and_filter instead sorts on raw absolute magnitude:
- A non-dominant loop in a high-activity partition can outrank a partition-dominant loop in a low-activity partition, even though the latter is more important relative to its own partition.
- Since
truncate(MAX_LOOPS)keys on the same sort, a partition-dominant but low-magnitude loop can be dropped entirely while non-dominant high-magnitude loops are retained. - The final caller-facing ranking is similarly skewed: consumers that read the ordered
Vec<FoundLoop>as "ranked by importance" get a magnitude ranking, not an importance ranking.
ltm_post.rs::compute_rel_loop_scores already computes the principled per-partition relative score, but it runs after discovery and is never fed back into ranking or truncation.
Component(s) affected
src/simlin-engine/src/ltm_finding.rs(rank_and_filter,FoundLoop::avg_abs_score)
Relationship to #310
This is related to but distinct from #310 ("ltm-finding: truncate-before-filter can drop partition-dominant loops").
- ltm-finding: truncate-before-filter can drop partition-dominant loops #310 is about the order of operations: it argues the partition-aware
MIN_CONTRIBUTIONfilter should run before theMAX_LOOPStruncation (or that per-partition quotas should be applied before the global cap). It assumes the existing keep/drop filter is the right partition-aware mechanism and only wants it applied earlier. - This issue is about the ranking key itself being wrong: even with ltm-finding: truncate-before-filter can drop partition-dominant loops #310 fixed (filter before truncate), the sort that drives truncation order and the final caller-facing ranking would still use raw
avg_abs_score, so a partition-dominant low-magnitude loop would still sort below rank 200 and a non-dominant high-magnitude loop would still sort above it.
A complete fix for both would touch the same function and should probably be designed together, but they are genuinely distinct defects (wrong key vs. wrong operation order).
Possible approaches
- Rank and truncate on a partition-relative key (e.g. peak or mean of
score.abs() / partition_totalover timesteps) rather than rawavg_abs_score-- reusing the per-partition, per-timestep totals already computed for theMIN_CONTRIBUTIONfilter. - Or apply per-partition quotas / interleave partitions before the global
MAX_LOOPScap so no partition's dominant loops are starved (this also addresses ltm-finding: truncate-before-filter can drop partition-dominant loops #310). - Keep deterministic tie-breaking and stable IDs / output ordering.
- Add a test with multiple partitions where a low-absolute-magnitude but partition-dominant loop is ranked above (and retained over) a higher-absolute-magnitude non-dominant loop in a different partition.
Context
Identified during an LTM-subsystem review, one of a batch of ~6 LTM-review findings being filed. Tracked under epic #488 (LTM area: discovery / post-sim). Related: #310.
Activity
LTM hardening: close out the top tracked gaps from epic #488 (#668)
bpowers commentedon Jun 2, 2026
Fixed in PR #668 (merged as f9f850d). rank_and_filter now ranks (and truncates) by mean magnitude of the partition-relative loop score over active steps — the literature's loop-inclusion measure — instead of raw avg_abs_score, with content-based tie-breaking. Partition totals are computed over all discovered loops; the peak-semantics MIN_CONTRIBUTION retention filter is unchanged but now runs before the cap (the #310 half). Pinned by test_rank_and_filter_543_partition_relative_ranking and test_rank_and_filter_543_truncation_keeps_partition_dominant (both red-verified against the raw-magnitude key).
engine: make discovery loop importance a relative score