Skip to content

engine: split ltm.rs into module directory - #498

Merged
bpowers merged 1 commit into
mainfrom
ltm/refactor-module-layout
May 8, 2026
Merged

bpowers merged 1 commit into
mainfrom
ltm/refactor-module-layout

Conversation

@bpowers

@bpowers bpowers commented May 7, 2026

Copy link
Copy Markdown
Owner

Summary

Move the 5837-line src/simlin-engine/src/ltm.rs into a module directory at src/simlin-engine/src/ltm/ with submodules organised by logical area.

Module layout

File Lines Purpose
mod.rs 98 Re-exports the public API at crate::ltm::*, declares submodules, defines MAX_LTM_SCC_NODES and the top-level detect_loops helper.
types.rs 216 Public LTM vocabulary: LinkPolarity, Link, Loop, LoopPolarity, TruncatedByBudget, ModuleLtmRole, plus classify_module_for_ltm / normalize_module_ref helpers.
partitions.rs 139 CyclePartitions plus the generic Tarjan SCC used by compute_cycle_partitions.
polarity.rs 472 Static polarity analysis on Expr2 ASTs: analyze_link_polarity and the small expression predicates it leans on (flip_polarity, expr_references_var, is_positive_constant, etc.).
indexed.rs 719 Compact integer-indexed graph plus the Johnson 1975 elementary-circuit enumerator and iterative Tarjan SCC. Tiernan 1970 enumerator retained under cfg(test) as the equivalence oracle.
graph.rs 939 Public CausalGraph type owning model adjacency, stock identity, variable AST references for polarity, and recursively-built sub-graphs for dynamic modules.
tests.rs 3391 Integration-style tests spanning all submodules; preserved as a single file to avoid splitting shared test fixtures.

Why now

ltm.rs was at 5837 lines, well into the project's 6000-line lint warning zone. Six in-flight LTM PRs (#489, #490, #491, #493, #494, #496 -- branches ltm/312-module-pathway-depth, ltm/485-polarity-confidence, ltm/480-array-reducer-polarity, ltm/460-johnson-per-scc-alloc, ltm/308-canonical-cycle-dedup, ltm/482-variable-level-loops-first) all add to this file. Splitting first lets the follow-on PRs land in narrower, more focused files.

API unchanged

mod.rs re-exports every public item; every existing crate::ltm::Foo import in the workspace continues to resolve. Pure code movement, no behaviour change.

The only visibility widening is pub(super) for items that are now accessed across submodule boundaries (IndexedGraph and its internals, polarity helpers, get_variable_dependencies, analyze_graphical_function_polarity). Nothing is exposed beyond the ltm module tree.

Stacking plan

After this lands, the six in-flight LTM PRs rebase onto the new layout. Each PR's diff against ltm.rs becomes a diff against the specific submodule it actually touches, dramatically reducing conflict surface.

Prep commit

The first commit (serve: clear inherited GIT_* env in git probe and tests) is the same fix already on ltm/312-module-pathway-depth, applied here so the project's pre-commit hook can pass on this branch. Without it, workspace tests inherit GIT_DIR / GIT_WORK_TREE from the outer git commit and the simlin-serve::git::tests::invalidate_repo_cache_removes_cached_entries_for_that_repo test recursively invokes the parent pre-commit hook, blocking every subsequent commit on this and the other LTM branches that don't yet carry the fix.

Test plan

  • cargo test -p simlin-engine
  • cargo test -p simlin-engine --features file_io
  • cargo clippy --workspace --all-targets -- -D warnings
  • cargo fmt --check
  • Pre-commit hook passes

@claude

claude Bot commented May 7, 2026

Copy link
Copy Markdown

Reviewed the split of src/simlin-engine/src/ltm.rs (5837 lines) into the seven-file ltm/ module directory and the prep fix for GIT_* env inheritance in simlin-serve.

No findings.

Verified:

  • Every public item from the old ltm.rs is re-exported from ltm/mod.rs at the same crate::ltm::* path (CausalGraph, CyclePartitions, Link, LinkPolarity, Loop, LoopPolarity, TruncatedByBudget, MAX_LTM_SCC_NODES, detect_loops) plus the crate-internal assign_loop_ids / normalize_module_ref.
  • All in-crate consumers (db.rs, db_analysis.rs, db_ltm.rs, ltm_finding.rs, ltm_augment.rs, json_sdai.rs, analysis.rs) and the ltm_mem_bench example still resolve, including direct-field construction of CyclePartitions { partitions, stock_partition } and CausalGraph { edges, stocks, variables, module_graphs } from db_analysis.rs / db_ltm.rs (fields remain pub / pub(crate)).
  • Visibility widenings (pub(super) on IndexedGraph and its fields, get_variable_dependencies, polarity helpers, analyze_graphical_function_polarity) are confined to the ltm module tree.
  • cfg(test) Tiernan oracle, comments, doc strings, and the lone TODO (subscripted lookup tables in polarity.rs:106) are preserved verbatim.
  • The GIT_* stripping in simlin-serve/src/git.rs and the two test files covers exactly the call sites that operate on a cwd; the remaining Command::new(\"git\") sites are git --version probes that are not affected by GIT_DIR / GIT_WORK_TREE / GIT_INDEX_FILE. The vars_os + byte-prefix approach correctly tolerates non-UTF-8 env names.

Overall correctness: correct. Pure code movement plus an orthogonal env-sanitization fix; no behavior change observed.

@codecov

codecov Bot commented May 7, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.23649% with 73 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.38%. Comparing base (d136a07) to head (24c51b9).

Files with missing lines Patch % Lines
src/simlin-engine/src/ltm/polarity.rs 85.57% 30 Missing ⚠️
src/simlin-engine/src/ltm/graph.rs 94.02% 20 Missing ⚠️
src/simlin-engine/src/ltm/types.rs 77.14% 8 Missing ⚠️
src/simlin-engine/src/ltm/indexed.rs 96.82% 6 Missing ⚠️
src/simlin-engine/src/ltm/mod.rs 0.00% 5 Missing ⚠️
src/simlin-engine/src/ltm/partitions.rs 97.61% 1 Missing ⚠️
src/simlin-serve/src/git.rs 83.33% 1 Missing ⚠️
src/simlin-serve/tests/git_integration.rs 85.71% 1 Missing ⚠️
src/simlin-serve/tests/watcher_git.rs 83.33% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #498      +/-   ##
==========================================
- Coverage   82.39%   82.38%   -0.01%     
==========================================
  Files         238      243       +5     
  Lines       61519    61531      +12     
==========================================
+ Hits        50689    50693       +4     
- Misses      10830    10838       +8     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@bpowers
bpowers marked this pull request as ready for review May 8, 2026 13:42
Move the 5837-line src/simlin-engine/src/ltm.rs into a module directory
at src/simlin-engine/src/ltm/ with submodules organised by logical area:
  - types.rs: LinkPolarity, Link, Loop, LoopPolarity, TruncatedByBudget,
    ModuleLtmRole and classify_module_for_ltm / normalize_module_ref
    helpers (216 lines)
  - partitions.rs: CyclePartitions plus the generic Tarjan SCC used by
    compute_cycle_partitions (139 lines)
  - polarity.rs: static polarity analysis on Expr2 ASTs --
    analyze_link_polarity and the small expression predicates it leans
    on (472 lines)
  - indexed.rs: compact integer-indexed graph plus the Johnson 1975
    elementary-circuit enumerator and iterative Tarjan SCC; Tiernan
    1970 enumerator retained under cfg(test) as the equivalence oracle
    (719 lines)
  - graph.rs: public CausalGraph type owning model adjacency, stock
    identity, variable AST references for polarity, and recursively
    built sub-graphs for dynamic modules (939 lines)
  - tests.rs: integration-style tests spanning all submodules
    (3391 lines)

mod.rs re-exports every public item so crate::ltm::* paths are
unchanged for external callers. Pure code movement; no behaviour
change.

Motivation: ltm.rs was approaching the project's 6000-line lint
threshold with multiple in-flight LTM PRs adding to it. Splitting now
lets follow-on PRs land in narrower files with smaller, more focused
diffs against the specific submodule each touches.

Visibility changes: items previously private to ltm.rs that are now
accessed across submodule boundaries (IndexedGraph and its internals,
polarity helpers, get_variable_dependencies,
analyze_graphical_function_polarity) are widened to pub(super) so the
tests submodule and graph.rs can reach them; nothing is exposed beyond
the ltm module tree. The public API surface at crate::ltm::* is
unchanged.
@bpowers
bpowers force-pushed the ltm/refactor-module-layout branch from 24c51b9 to 7dc8620 Compare May 8, 2026 13:54

@bpowers bpowers left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rebased onto post-#489 main

  • Dropped the redundant prep commit (older vars()-based GIT_* strip).
    Main now carries the vars_os + as_encoded_bytes version from #489
    which is strictly better.
  • Re-applied main's enumerate_module_pathways node-count bound
    in the new src/simlin-engine/src/ltm/graph.rs location during the
    rebase. Behavior on the refactor branch is unchanged from
    pre-rebase: every existing test continues to pass, no expected
    values modified.

mergeable: MERGEABLE / CLEAN after force-push.

@claude

claude Bot commented May 8, 2026

Copy link
Copy Markdown

Reviewed the refactor. Verified that this is pure code movement: re-exports in mod.rs cover every external import (CausalGraph, CyclePartitions, Link, LinkPolarity, Loop, LoopPolarity, TruncatedByBudget, MAX_LTM_SCC_NODES, assign_loop_ids, normalize_module_ref), test counts match (96), function counts match (161), and spot-checked function bodies (assign_loop_ids, tarjan_scc, analyze_link_polarity, get_link_polarity, from_model, find_loops_with_limit, enumerate_circuits_in_scc) are identical. Visibility widening to pub(super) is scoped within the ltm module tree. No findings.

Overall correctness: correct.

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.

1 participant