Repository navigation
Derive reform tax-benefit systems from the built default system - #11129
Conversation
Creating a Simulation or Microsimulation with a reform and no supplied tax-benefit system rebuilt CountryTaxBenefitSystem from every parameter and variable file. Copy the built default system instead, return it to its state before the reform-dependent steps, and run those steps on the copy. Fall back to a full build when the default system has changed in a way that would make the copy differ. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Derivation relies on core loading a parameter tree with every scale bracket and homogenized node linked to its parent (3.37.1), and 3.37.2 changes how a node's values at an instant read its children, so the derived-system tests and the reform comparison run on 3.37.2. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Since #11121, the per-capita step re-points only input variables, so a full rebuild accepts a reform that gives rental_income a formula instead of raising "mixes computation modes". The test now requires the derived system to keep the inherited national total, as the rebuild does; the case that a full rebuild still rejects (a reform to a per-capita series) keeps its own test. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
36e6eba to
e12c969
Compare
DTrim99
left a comment
There was a problem hiding this comment.
Program review
Base repository: PolicyEngine/policyengine-us
PR number: 11129
Reviewed head SHA: e12c969
Merge base SHA: b3edc04
Mode: full
Scope: changed behavior and affected dependencies
Source manifest: /private/tmp/policyengine-command-runs/ea33c0bb2b64/pr-11129-review-sources.json
Review status: COMPLETE
Source Documents
No source documents registered; see scope and validation.
Critical
None.
Should Address
None.
Suggestions
None.
Evidence Gaps
No material gaps reported.
Notes
- This PR changes how a reform's tax-benefit system is built and no policy, so it had one code reviewer and no source review.
- Derivation matches a full build.
_apply_reformsis the old reform code, unchanged, split out of__init__. The default build's only tree edit after the recorded point isadd_per_capita_uprating, which derivation undoes (removes the added series, restores the replaced ones, puts back child order). The tests compare full parameter state (values, metadata,modifiedflags, child order) and variable state against a full rebuild, so a derivation that skipped the undo, shared the re-pointed variables, misordered children or detected structural reforms at the wrong instant would fail. - The shared default is never written, even temporarily. Every write (per-capita undo, cache reset,
_apply_reforms) goes to the copy, and on core 3.37.2ParameterNode.clonegives each clone a new at-instant cache. A diagnostic ran 6 derivations on parallel threads while another thread ran baseline simulations on the same default: every result matched the sequential one and the default was unchanged. After the default had served simulations, a reform simulation gave the same 12 values (income tax, net income, refundable CTC, SPM net income in 2024, 2026 and 2030) derived as fully built. - Fallback and start instant. Edits through
Parameter.update(reform dicts,modify_parameters) setmodified, so the system is rebuilt; registry changes are caught by identity; a custom__init__or a default of another class also rebuilds. No non-test code in this repository edits the shared default after construction.SimulationandMicrosimulationboth passstart_instant(defaultDEFAULT_START_DATE) through to derivation, and structural detection runs on a tree that matches the full build's at that point.spm=configgets its own provider in both paths (read from the code, not executed). - Optional: the test file has no concurrency case (the diagnostic above covers it), and callers outside this repository rest on the PR's text search, which this review did not repeat. Reform simulations now share variable objects with the default system, as baseline simulations already do; the
derive_reform_systemdocstring says so.
Validation Summary
Local tests at the reviewed head on policyengine-core 3.37.2 (imports from the snapshot): 144 passed, 0 failed across the new test file and six related core test files (per-capita uprating, SPM isolation, SPM policy family, reform backdating, structural reforms in simulation, ID/GA CTC reform activation). Plus the threaded and warm-default diagnostic above. Microsimulation was not run locally; CI's Microsimulation shard passed. CI at e12c969: 38/38 succeeded.
Timing
setup seconds: 60.00s; scope seconds: 60.00s; code role seconds: 703.00s; parallel review seconds: 703.00s; adjudication seconds: 0.00s; consolidation cleanup seconds: 6.00s; elapsed seconds: 789.00s
Review Severity
APPROVE. Open findings: 0 critical, 0 should address, 0 suggestions.
Dependencies
Both required dependencies are met.
>=3.37.2(reason below), withpyproject.tomlanduv.lockupdated.mainafter it.Why 3.37.2: 3.37.2 changes how a parameter node's values at an instant read its children (each child when first read, instead of all of them when the node is built), and makes
add_childclear the cached values above the node. Derivation clones the parameter tree and resets the system's cache of values at an instant, so the tests and the comparison run on 3.37.2.Also on
mainnow: PolicyEngine/policyengine-us#11121 changedadd_per_capita_upratingto point only input variables at their per-capita series. A full build therefore now accepts a reform that gives an uprated input such asrental_incomea formula, where it used to raise "mixes computation modes". The test that expected that error now checks that the derived system matches the full build for this reform: same variables and parameters, withrental_incomekeeping the national total it inherits.What and why
Creating a
SimulationorMicrosimulationwithreform=and notax_benefit_system=builds a newCountryTaxBenefitSystem, which reloads about 12,000 parameter files and 12,300 variable files. Almost none of that work depends on the reform._prepare_spm_systemnow asks the built default system to derive the reform's system:_apply_reforms) on the copy, including structural-reform detection atstart_instant.with_parameter_barrier, as before.It uses a full build when derivation cannot give the same result: the default system's variable registry or per-capita parameter nodes changed, a parameter is marked
modified, or the system class has its own constructor.Who is affected: .py passes
reform=without a system, so its household calculations and microsimulations use this path. API v1 society-wide simulations reach it through the simulation API and .py. Household calculations in API v1 and the household API do not: they clone their own system, apply the reform and passtax_benefit_system=.Known limit: changes to the shared default system that set no
modifiedflag are not detected, such asadd_child,merge, assignments tovalues_list[i].value, and metadata or variable attribute edits. A derived system includes them, as simulations sharing the default system already do; a full build would not. Thecan_derive_reform_systemsdocstring records this. A text search of the default branches of API v1, the household API, the simulation API, .py and app-v2 found no code that edits a policyengine-us system in place.Evidence
Comparison with a full build, on policyengine-core 3.37.2: 359 of the 846 reforms, on this branch's head (
e12c9696be) with the same comparison script as the earlier run. Every one gives the same system and the same simulation results, the full build raised for none of them, and the default system was unchanged throughout. The 359 are all 19 targeted cases, 334 of the 596 reforms from YAML tests and 6 of the 231gov.contribswitches.The 19 targeted cases were chosen for what derivation touches: changes to the CPI-U and national average wage indices, a national total and the population series behind the per-capita series, the payroll cap, reform periods before 2024, ending in 2020 or starting mid-year, ETERNITY, scalar and compound period keys, structural switches from 2026 at both start instants, Utah's CTC structural reform (which calls
modify_parameters) with a federal change, and a reform object followed by a parameter change. All 19 match.The 340 include all 21 reforms that #11121 made buildable, which the earlier run could only check for raising the same error both ways. They are the refundable state EITC reforms for Alabama, Kentucky, Mississippi, North Dakota and Ohio (
gov.contrib.states.*.child_poverty_impact_dashboard.eitc; the switches and the YAML tests' reforms), which replace an uprated input with a formula, and Utah HB210 Substitute 2's marriage credit. All 21 match.Not compared on 3.37.2: the other 262 YAML-test reforms and 225
gov.contribswitches. All of them matched in the earlier run below.Median build time in this run, on a loaded machine: full build 12.4 s, derived 4.23 s.
Earlier comparison (2026-11-17), before the rebase: 846 reforms, no differences, on this PR's first commit and on core commit
1ce7fd23, an earlier commit of policyengine-core#660.VariableNameConflictErrorin the simulation stepsThe default system was unchanged throughout. The corpus contains no stored production policies.
Build time per reform system (local macOS, earlier run):
These are not Modal or Linux CI measurements.
Test plan
policyengine_us/tests/core/test_derived_reform_system.py, run by node ID on policyengine-core 3.37.2 after the rebase. 19 passed; the one that expected the error Fix structural reform builds, move the default year to 2026, and widen reform checks #11121 removed was rewritten as described above. The rewritten tests pass, and the new one fails against a derivation patched to copy the default system's re-pointed variables.ruff formatchangelog.d/Test cost: the 20 tests took 98 s elapsed, 88.5 s user CPU and a peak of 4.2 GiB resident memory on macOS with Python 3.14, while other jobs were running. This is not a Linux CI measurement. In CI, the rest-python
coregroup now runs as 4 processes: inmainrun 45245929428 they peaked at 6.3 to 8.8 GiB each on the 16 GB runner, so the earlier figure of 13.3 GB for one process no longer applies. In this PR's CI run (45483983574), the file ran in thecore-2process: its 20 tests took 230 s, and the process peaked at 4.68 GiB over 19 minutes. The othercoreprocesses peaked at 2.45 to 4.75 GiB, so the file needs no group of its own.axiom: n/a: changes how the tax-benefit system is constructed, with no policy change
🤖 Generated with Claude Code