Repository navigation
Free policyengine-us 2.2.1's pinned copy of the rules when an engine is released - #1777
Merged
Merged
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_eligibleandny_ctc_pre_2024, every year from 2018 except 2025–2027) recompute the federal CTC under 2017 rules throughpolicyengine_us/tools/pinned_tbs.py. The 2021 New York EITC (ny_eitc) does the same under 2020 rules.base_tbs.clone(). That rebuilds the whole parameter tree (102,530 parameters) and deep-copies every variable, which takes 2–3 s.(weakref to the base system, weakref to its tree, clone)in the module-level dict_PINNED_TBS_CACHE, holding the clone strongly.TaxBenefitSystem.clonecopies the base system'ssimulationattribute 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_simulationdoesn't reach this. The clone is the system of thepre_tcja_ctcbranch, and itssimulationattribute 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_taxfor 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_simulationcollects the simulations it walks (the root,branchesvalues andbaseline) and their systems. After the walk,_drop_pinned_rule_copiesremoves a_PINNED_TBS_CACHEentry when either of these holds:branches.The second case is found through the clone's
simulationattribute and theparent_branchchain (_made_in_released_simulation, at most 64 links). policyengine-us's marginal tax rate formula, for example, makes a branch for each of its firstmarginal_tax_rate_adultsadults, 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()inSPMSimulationMixin.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:
The module is read from
sys.modulesand 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_tbshas that weak cache. On that engine_drop_pinned_rule_copiesonly 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:
cd5c07ec0, thend1762e424;b72124f8a, then68f5a096f;7004cbd98, thene90f9455f;f000a0117, thenbfdc818b5.The branch then merged
origin/mainatc58596526(#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 aparent_branchloop 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: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 computesmarginal_tax_ratefor 2024. The newest pinned copy must have been made in a deletedmtr_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): asalt_refund_incomeneutralization, scored onstate_income_taxfor 2024 on the guard fixture, reaches theny_pre_tcja_ctclookup. No parameter it added is still alive once the scorer has finished the reform. Onmainit 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 verifypasses.Review
Three rounds of adversarial review by subagents, each followed by fixes. Review set no PR review state.
simulationandparent_branchchain, and by counting lookups instead of the second run.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.🤖 Generated with Claude Code