Skip to content

Free policyengine-us 2.2.1's pinned copy of the rules when an engine is released - #1777

Merged
anth-volk merged 9 commits into
mainfrom
claude/salt-refund-rules-copy-c2815f
Nov 27, 2026
Merged

anth-volk merged 9 commits into
mainfrom
claude/salt-refund-rules-copy-c2815f

Conversation

@anth-volk

Copy link
Copy Markdown
Contributor

Tracker task 1.4 · 5a (project "15. US build memory, CPU and run time"), found by 1.4 · 5.

What keeps the copy alive

In policyengine-us 2.2.1, New York's pre-2024 child tax credit formulas (ny_ctc_pre_2024_eligible and ny_ctc_pre_2024, every year from 2018 except 2025–2027) recompute the federal CTC under 2017 rules through policyengine_us/tools/pinned_tbs.py. The 2021 New York EITC (ny_eitc) does the same under 2020 rules.

  • A lookup that misses calls base_tbs.clone(). That rebuilds the whole parameter tree (102,530 parameters) and deep-copies every variable, which takes 2–3 s.
  • It stores (weakref to the base system, weakref to its tree, clone) in the module-level dict _PINNED_TBS_CACHE, holding the clone strongly.
  • Core's TaxBenefitSystem.clone copies the base system's simulation attribute into the clone. So the entry keeps alive the engine the clone was made in, and through that engine's system, its parameter tree, variables and rule modules.
  • release_engine_simulation doesn't reach this. The clone is the system of the pre_tcja_ctc branch, and its simulation attribute points at the parent engine, so the existing check (tax_benefit_system.simulation is current) doesn't match.

On the eight-household guard fixture, the SALT refund reform pass (state_income_tax, 2024) leaves 214,858 parameters alive after the scorer has finished the reform. That is two whole parameter trees, the clone's and the reformed system's, plus 9,798 others: +1.2–1.3 GiB of footprint. After a baseline pass the clone alone stays, about 0.5 GiB.

Every batch engine that computes state_income_tax for 2024 with a New York household clones again: each engine has its own system object, and the cache only matches that object. Only the newest entry is kept, so the memory doesn't build up across passes. The full investigation is in the 1.4 · 5 notes (salt-refund-rules-copy-2026-11-26/README.md).

What changes

release_engine_simulation collects the simulations it walks (the root, branches values and baseline) and their systems. After the walk, _drop_pinned_rule_copies removes a _PINNED_TBS_CACHE entry when either of these holds:

  • its first item is a weak reference to one of those systems;
  • its clone was made in one of those simulations, or in a branch of one that a formula had already removed from branches.

The second case is found through the clone's simulation attribute and the parent_branch chain (_made_in_released_simulation, at most 64 links). policyengine-us's marginal tax rate formula, for example, makes a branch for each of its first marginal_tax_rate_adults adults, computes household income in it and deletes it, so the release never walks the branch where that lookup ran.

  • No scored value changes. A lookup reuses an entry only for the same system object, and each policyengine-us simulation and branch gets one of its own (share_spm_policy, copy() in SPMSimulationMixin.clone), so no later engine could use a removed entry. A removed branch whose chain leads to a released engine is a finished part of that engine. Within an engine's life nothing changes: both New York formulas still share one clone.

  • What it leaves alone:

    • entries made in engines still running, dead or not;
    • a missing module, a missing cache, or a cache that isn't a dict;
    • entries without a weak reference as their first item;
    • entries whose clone raises when its attributes are read.

    The module is read from sys.modules and never imported, so the helper keeps its rule of never raising.

  • Not fixed here: the 2–3 s clone per engine, about 1.4–2.1 CPU hours per national run.

Engine version

policyengine-us 2.88.6 fixes this upstream (PolicyEngine/policyengine-us#10550): cache entries are held weakly and a pin copies tree nodes only (0.13 s). Draft #1676 moves the US engine to 2.145.3, whose _get_pinned_tbs has that weak cache. On that engine _drop_pinned_rule_copies only removes weak entries that could no longer be used. Its docstring says to remove it once the US engine pin reaches 2.88.6.

The engine tests don't depend on the shipped probe, which #1676 rescores on federal income_tax, a key that never reaches the New York lookup. They assert only what holds on both engines, and say so.

Tests

Each review round's tests were committed and seen failing before its fix:

  • the change: cd5c07ec0, then d1762e424;
  • round 1: b72124f8a, then 68f5a096f;
  • round 2: 7004cbd98, then e90f9455f;
  • round 3: f000a0117, then bfdc818b5.

The branch then merged origin/main at c58596526 (#1751, which added a test at the end of the same workflow test file).

  • engine_free/us/test_us_release_slice_memory.py:
    • test_release_drops_rules_pinned_from_the_released_simulation_and_its_branches: entries from the simulation, a branch and the baseline branch are removed, including a weak-only 2.88.6-shaped entry. A live engine's entry stays, and the clone is freed.
    • test_release_drops_rules_pinned_in_branches_a_formula_already_removed: removed are entries made in a deleted branch, in a deleted branch of a walked branch, and in a deleted branch whose base is already collected. An unrelated entry with a parent_branch loop stays.
    • test_release_drops_rules_pinned_in_a_simulation_without_a_system.
    • test_release_leaves_pinned_rules_caches_it_does_not_recognise_alone, four cases:
      • no cache attribute;
      • a cache that isn't a dict;
      • unrecognised entries, and a dead entry made in a live engine;
      • clones that raise (a dead weak proxy, and a raising property).
    • test_release_without_the_engines_pinned_rules_module: nothing is imported.
  • engine_scenario/us/test_us_engine_pinned_rules_release.py::test_release_frees_rules_pinned_in_a_branch_the_formula_deleted (policyengine-us): one New York household computes marginal_tax_rate for 2024. The newest pinned copy must have been made in a deleted mtr_for_adult_* branch. After the release the engine is freed. It fails if only the base system is matched. About 90 s.
  • engine_workflow/us/test_us_post_export_scoring.py::test_reform_pass_leaves_no_pinned_copy_of_the_rules_behind (policyengine-us): a salt_refund_income neutralization, scored on state_income_tax for 2024 on the guard fixture, reaches the ny_pre_tcja_ctc lookup. No parameter it added is still alive once the scorer has finished the reform. On main it fails with 214,858. About two minutes. It was re-run after the merge with Keep the US post-export scorer's household batches in files, not memory #1751.

Only these tests were run, each by node ID. tools/ci_test_plan.py verify passes.

Review

Three rounds of adversarial review by subagents, each followed by fixes. Review set no PR review state.

  • Round 1. Confirmed: entries made in branches that a formula deleted were missed, keeping the deleted branch and the released engine. Also, the engine test's second run, with the removal turned off, left its own 1.3 GiB entry after the test, and its byte comparison could not fail. Fixed by matching through the clone's simulation and parent_branch chain, and by counting lookups instead of the second run.
  • Round 2. Confirmed: the engine test read the shipped probe, which Move the US engine to policyengine-us 2.145.3 and prepare the Route A national build #1676 rescores on income_tax, so it would fail once both merge. Minor issues: the dead-entry case tested nothing, a clone whose attribute raises made the helper raise, and the scenario test's precondition was weak. All fixed.
  • Round 3. No bug that blocks merging. Minor issues: an early return when no system was walked, and wording. Both fixed.
    • Not changed: no test compares scored values with and without the removal. Such a comparison can't fail, because no lookup can reuse a removed entry. Value neutrality rests on the cache's key, an object identity check, which the reviewers checked against the engine source.

🤖 Generated with Claude Code

anth-volk and others added 9 commits November 27, 2026 00:15
…the rules

In policyengine-us 2.2.1, New York's pre-2024 CTC formulas clone the whole
rules system into policyengine_us.tools.pinned_tbs._PINNED_TBS_CACHE and hold
the clone strongly. The clone's copied `simulation` attribute keeps the batch
engine and its reformed parameter tree alive after the reform is finished.

- Engine-free: release_engine_simulation removes the cache entries pinned
  from the released simulation, its branches and its baseline branch,
  including 2.88.6's weak-only entries, and keeps a live engine's entry. It
  leaves a missing cache, a cache that is not a dict, unrecognised entries
  and dead entries alone, and never imports the module.
- Engine workflow: after the SALT refund reform pass on the guard fixture
  (state_income_tax, 2024), no Parameter the pass added is still alive, and
  the scored arrays equal those scored with the entry kept.

Both new behaviour tests fail on main: the engine test finds 214,858 added
Parameters still alive (two whole parameter trees and 9,798 more).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…eased

policyengine-us 2.2.1's New York pre-2024 CTC formulas (and its 2021 EITC)
look up rules pinned to earlier federal law through
policyengine_us.tools.pinned_tbs. A miss clones the simulation's whole
tax-benefit system (2-3 s) and stores (weak reference to that system, weak
reference to its tree, clone) in the module-level _PINNED_TBS_CACHE. Core's
TaxBenefitSystem.clone copies the `simulation` attribute into the clone, so
the entry keeps the finished batch engine and, through its system, the
reformed parameter tree, variables and rule modules alive until another
engine's miss replaces it: about 1.3 GiB after a reform pass.

release_engine_simulation now collects the systems of the simulation and of
every branch it walks, and removes the cache entries whose weak base
reference points at one of them. A lookup reuses an entry only for the same
system object, and every policyengine-us simulation has its own, so a
released simulation's entries can never be used again: no scored value
changes. The module is read from sys.modules, not imported, and caches or
entries of any other shape are left alone. policyengine-us 2.88.6 holds these
entries weakly (PolicyEngine/policyengine-us#10550); the docstring says to
remove this when the US engine pin reaches it.

This frees the memory but not the clone time per engine.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…a deleted

Review round 1 findings:

- policyengine-us's marginal tax rate formula makes a branch per adult,
  computes household income in it and deletes the branch. A New York pinned
  lookup inside that branch is pinned from the branch's system, which the
  release never walks, so the entry kept the deleted branch and, through its
  parent_branch, the released engine. New tests: an engine-free one (deleted
  branch of the simulation, deleted branch of a walked branch, an unrelated
  entry with a parent_branch loop that stays) and an engine scenario test on
  one New York household computing marginal_tax_rate for 2024. Both fail on
  the current helper; the scenario test finds the released engine alive.
- The engine workflow test's second run, with the removal turned off, left
  its 1.3 GiB entry alive after the test, and its byte comparison could not
  detect a value change; its retention check would also fail on
  policyengine-us 2.88.6 or later, which keeps nothing (draft #1676 moves to
  2.145.3). It now counts the pinned lookups instead, so it fails if the pass
  stops reaching them, and asserts what holds on both engines.
- The stand-in clones for branch entries now name their engine, as real
  clones do.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review round 1: an entry pinned inside a branch that a formula removed from
`branches` (policyengine-us's marginal tax rates) has a base system the
release never walks, so it kept the deleted branch and the released engine.

_drop_pinned_rule_copies now also removes an entry whose clone was made in a
released simulation: the clone's copied `simulation` attribute, or a
simulation on its `parent_branch` chain (at most 64 links, so a loop ends),
is one the release walked. The walked simulations are held for the duration
of the call, so identity checks cannot match a reused id. Such a branch is
a finished descendant of the released engine, so no later lookup could use
the entry.

The docstring now says what is matched (a weak base reference first, or the
clone's simulation chain), that 2.88.6's all-weak entries are matched the
same way and removing them frees nothing, and what is left alone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…robe

Review round 2 findings:

- Draft #1676 rescores the shipped salt_refund_income_neutralization probe
  on federal income_tax, which never reaches New York's pinned lookup, so the
  engine workflow test would fail once both merge. It now builds its own
  salt_refund_income neutralization, scored on state_income_tax for 2024,
  and its docstring says that on policyengine-us 2.88.6 or later the cache
  holds nothing strongly.
- The dead-entry case tested nothing. Engine-free cases now cover an entry
  whose base is already collected but whose clone was made in a deleted
  branch of the released simulation (to be removed), and a dead entry made
  in an engine that is still running (to stay). The first fails on the
  current helper.
- A clone whose `simulation` attribute raises (a dead weakref.proxy, or a
  property that raises) made the helper raise. New case; it fails on the
  current helper with ReferenceError.
- The engine scenario test now records where the newest pinned system was
  made and requires it to be a deleted mtr_for_adult_* branch, so a helper
  that matches only the base system fails it whatever order the formula's
  lookups run in.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…lone

Review round 2:

- An entry whose base system is already collected is now removed when its
  clone was made in a released simulation (its `simulation` or `parent_branch`
  chain); dead entries made elsewhere still stay.
- _made_in_released_simulation answers False when reading `simulation` or
  `parent_branch` raises (a dead weak proxy, or a property of another engine
  version), so the helper keeps its rule of never raising and leaves that
  entry alone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…entries

Review round 3 (minor): _drop_pinned_rule_copies returned early when the
release walked no system, so an entry whose clone was made in a walked
simulation without a tax_benefit_system stayed, against its docstring. New
engine-free case; it fails on the current helper. Also corrects the
engine-free docstring on how the marginal tax rate formula branches (one
branch for each of its first `marginal_tax_rate_adults` adults) and notes in
the scenario test that it passes without the removal on policyengine-us
2.88.6 or later.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…stem

Review round 3 (minor): _drop_pinned_rule_copies now returns early only when
the release walked neither a system nor a simulation, as its docstring says.
Also names the function that follows `parent_branch` links in the depth
bound's comment, and states how the marginal tax rate formula branches.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts:
#	packages/microcosm-build/tests/engine_workflow/us/test_us_post_export_scoring.py
@anth-volk
anth-volk marked this pull request as ready for review November 27, 2026 14:11
@anth-volk
anth-volk merged commit c4f421d into main Nov 27, 2026
11 checks passed
@anth-volk
anth-volk deleted the claude/salt-refund-rules-copy-c2815f branch November 27, 2026 14:12
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