Skip to content

engine: rank LTM discovery competitive-first, expose cycle partitions (#671) - #686

Merged
bpowers merged 1 commit into
mainfrom
ltm-partition-aware-discovery
Jun 3, 2026
Merged

bpowers merged 1 commit into
mainfrom
ltm-partition-aware-discovery

Conversation

@bpowers

@bpowers bpowers commented Jun 3, 2026

Copy link
Copy Markdown
Owner

Improves loop discovery so it prioritizes the most important loops: partition the feedback structure, then surface the loops competing within the giant component instead of the trivially-isolated singletons.

Problem

On C-LEARN, model.analyze() led with dozens of two-variable gas-uptake loops while the coupled carbon-climate core was buried below them. The defect is information content: a loop alone in its cycle partition has relative loop score exactly +/-1 at every active step by construction (its own |score| is the entire normalization denominator), so ranking by mean |relative score| lets these zero-information loops win over loops that genuinely compete for dominance.

Ranking change

Discovery now ranks competitive-first: loops sharing their cycle partition with at least one other discovered loop come first, ordered by the unchanged paper-aligned mean relative importance (ref 13.3); trivially-isolated loops sort after all of them, and the MAX_LOOPS cap drops the solo loops first. Never-active (NaN) loops still sort last.

A partition-major ordering (giant component first by stock count) was considered and rejected after an adversarial design review: it would rank a dynamically inert giant partition's loops above a genuinely dominant small-partition loop, and any score-based partition ordering is unit-dependent and crosses the papers' cross-partition-comparability boundary (ref section 8). The GH #543/#310 contracts are preserved for competing loops -- their test fixtures gain a second small-partition loop so the partition-dominant survivor is non-degenerate rather than a solo +/-1 loop.

Cycle partitions become first-class API

Each FoundLoop carries a result-scoped dense partition index (first-appearance order over the ranked list; deliberately documented as NOT stable across edits, since the SCC numbering renumbers on unrelated stock adds/renames -- durable identity keys on the partition's stock-name set), and DiscoveryResult/ModelAnalysis carry the partition list (element-level stock names, returned-loop count). Threaded through:

  • C FFI: SimlinDiscoveredLoop.partition, new SimlinDiscoveredPartition, SimlinDiscoveryResult.partitions (neither discovery struct is ABI-size-tracked; only pysimlin reads them, via the regenerated header)
  • pysimlin: Analysis.partitions, Loop.partition, new Partition dataclass

Exhaustive/pinned-surface parity (SimlinLoop, Model.loops/Run.loops, MCP) is deliberately out of scope and tracked as #685.

C-LEARN, end to end

analyze() now reports 153 loops across 15 partitions: three 15-stock, 47-loop carbon-climate giant components (one per scenario element) on top -- ocean carbon buffering, the CO2-forcing-temperature loops, the biomass revolving door -- and the 12 isolated gas-decay singletons at the bottom. The notebook's section 4 was rebuilt to open with this feedback anatomy (partition table first, then the ranking with a partition column); the expert-pin cross-validation and the decade-median dominance story are unchanged.

Testing

Full pre-commit gauntlet (Rust fmt/cbindgen/clippy/tests, TS lint/build/tsc/tests, pysimlin tests/ruff/mypy), plus the ignored C-LEARN suites run explicitly: clearn_ltm_discovery_compiles, clearn_with_ltm_simulates_model_vars_identically, clearn_pinned_climate_loop_is_scored, simulates_clearn, clearn_residual_exactness, and the wasm twins (simulates_clearn_wasm, simulates_wrld3_03_wasm). New/rewritten unit tests pin the solo-demotion ordering, truncation order under cap pressure, dense partition-metadata indexing (including the no-score-data path), and determinism under input permutation.

Closes #671.

On C-LEARN, model.analyze() led with dozens of trivial two-variable
gas-uptake loops while the coupled carbon-climate core was buried below
them. The defect is information content: a loop alone in its cycle
partition has relative loop score exactly +/-1 at every active step by
construction (its own |score| is the whole normalization denominator),
so ranking by mean |relative score| lets these zero-information loops
win over loops that genuinely compete for dominance.

Discovery now ranks competitive-first: loops sharing their partition
with at least one other discovered loop come first (ordered by the
unchanged, paper-aligned mean relative importance), and trivially
isolated loops sort after all of them; the MAX_LOOPS cap drops the solo
loops first. A partition-major ordering (giant component first by stock
count) was considered and rejected: it would rank a dynamically inert
giant partition's loops above a genuinely dominant small-partition loop,
and any score-based partition ordering is unit-dependent and crosses the
papers' partition-comparability boundary. The GH #543/#310 contracts are
preserved for competing loops (their fixtures gain a second small-
partition loop so the partition-dominant survivor is non-degenerate).

Cycle partitions become first-class discovery output: each FoundLoop
carries a result-scoped dense partition index (first-appearance order;
not stable across edits, since the SCC numbering renumbers on stock
adds/renames -- consumers needing durable identity key on the stock-name
set) and DiscoveryResult/ModelAnalysis carry the partition list (stocks,
returned-loop count), threaded through the C FFI (SimlinDiscoveredLoop.
partition, SimlinDiscoveredPartition; neither discovery struct is
ABI-size-tracked and only pysimlin reads them) and pysimlin
(Analysis.partitions, Loop.partition). Exhaustive-mode parity is
deliberately out of scope and tracked separately.

The C-LEARN notebook's section 4 now opens with the model's feedback
anatomy (3 giant 15-stock carbon-climate partitions of ~47 competing
loops each, plus 12 isolated gas-decay singletons) and the ranking
surfaces the core directly; verified end-to-end by rebuilding the
notebook and running the ignored C-LEARN suites (discovery compile,
LTM identity vs Ref.vdf, pinned climate loop, wasm twins).
@claude

claude Bot commented Jun 3, 2026

Copy link
Copy Markdown

Review summary

No blocking findings.

The competitive-first ranking (cmp_relative_importance + RelativeImportance.competing) and partition-metadata threading are well-structured. Key invariants that the code preserves:

  • The "competing" classification is computed over the full discovered set (pre-MIN_CONTRIBUTION filter), so a loop whose partition mate gets filtered still ranks as competing — correct, because the relative-score denominator (partition_totals) is likewise built pre-filter, so the surviving loop's relative score is not pinned to ±1.
  • cmp_relative_importance chains by_nan BEFORE by_competing so a never-active (NaN-mean) loop still sorts last regardless of competing/solo. Among NaN loops, by_competing further pushes solo NaN below competing NaN, which is harmless and documented.
  • attach_partition_metadata runs on both the scored path and the no-scores path, so DiscoveredPartition::loop_count always reflects the FINAL returned list (post-cap, post-filter).
  • FFI memory: simlin_free_discovery_result correctly drops each partition's stocks array first, then reclaims the Box<[SimlinDiscoveredPartition]> via slice_from_raw_parts_mut — same pattern as the loops/periods arrays. The error path in discovery_to_ffi cleans up the partial c_partitions vec before bailing.
  • partition_for_loop(loop_info, &[]) is safe here because discovered FoundLoops are always scalar (loop_info.dimensions is empty), so the empty dims slice is never consulted (as the slot0 closure comment notes).
  • All FoundLoop { ... } constructions in tests and production are updated with the new partition: None field.
  • pysimlin Loop.partition: int | None = None has a default, so existing Loop(...) call sites (model.py structural loops, run.py) still compile without modification.

Overall correctness verdict

Correct. No correctness bugs identified; tests cover the solo-demotion ordering, cap-pressure truncation order, partition metadata indexing (including the no-score-data path), and determinism under input permutation. The notebook + design doc + MCP instructions are aligned with the new ranking contract.

@bpowers
bpowers merged commit 86bfddb into main Jun 3, 2026
12 of 13 checks passed
@bpowers
bpowers deleted the ltm-partition-aware-discovery branch June 3, 2026 03:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ltm: expose cycle-partition id/size on discovered loops so singleton-partition loops can be grouped or filtered

1 participant