Skip to content

Derive reform tax-benefit systems from the built default system - #11129

Merged
anth-volk merged 4 commits into
mainfrom
derive-reform-systems-from-baseline
Nov 25, 2026
Merged

anth-volk merged 4 commits into
mainfrom
derive-reform-systems-from-baseline

Conversation

@anth-volk

@anth-volk anth-volk commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Dependencies

Both required dependencies are met.

Dependency State
PolicyEngine/policyengine-core#660, merged and released Released in policyengine-core 3.37.1 (2026-11-19). This PR requires >=3.37.2 (reason below), with pyproject.toml and uv.lock updated.
PolicyEngine/policyengine-us#11126, merged Merged 2026-11-19. This branch is rebased onto main after 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_child clear 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 main now: PolicyEngine/policyengine-us#11121 changed add_per_capita_uprating to point only input variables at their per-capita series. A full build therefore now accepts a reform that gives an uprated input such as rental_income a 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, with rental_income keeping the national total it inherits.

What and why

Creating a Simulation or Microsimulation with reform= and no tax_benefit_system= builds a new CountryTaxBenefitSystem, which reloads about 12,000 parameter files and 12,300 variable files. Almost none of that work depends on the reform.

_prepare_spm_system now asks the built default system to derive the reform's system:

  1. Clone the default parameter tree and undo the per-capita uprating step that the default build ran last.
  2. Restore the variable registry from before the reform-dependent steps.
  3. Run those steps (_apply_reforms) on the copy, including structural-reform detection at start_instant.
  4. Apply 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 pass tax_benefit_system=.

Known limit: changes to the shared default system that set no modified flag are not detected, such as add_child, merge, assignments to values_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. The can_derive_reform_systems docstring 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 231 gov.contrib switches.

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.contrib switches. 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.

Outcome Reforms
Same system and simulation results 825
Full build rejects the reform; derivation raises the same error type and message 20
Same VariableNameConflictError in the simulation steps 1

The default system was unchanged throughout. The corpus contains no stored production policies.

Build time per reform system (local macOS, earlier run):

Measurement Full build Derived
Median over the comparison, 2 workers 9.61 s 3.54 s
Separate clean processes 6.8 s 2.75 to 3.1 s

These are not Modal or Linux CI measurements.

Test plan

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 core group now runs as 4 processes: in main run 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 the core-2 process: its 20 tests took 230 s, and the process peaked at 4.68 GiB over 19 minutes. The other core processes 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

anth-volk and others added 4 commits November 24, 2026 23:19
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>
@anth-volk
anth-volk force-pushed the derive-reform-systems-from-baseline branch from 36e6eba to e12c969 Compare November 24, 2026 20:44
@anth-volk
anth-volk marked this pull request as ready for review November 24, 2026 22:22
@anth-volk
anth-volk requested a review from DTrim99 November 24, 2026 22:22

@DTrim99 DTrim99 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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_reforms is the old reform code, unchanged, split out of __init__. The default build's only tree edit after the recorded point is add_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, modified flags, 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.2 ParameterNode.clone gives 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) set modified, 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. Simulation and Microsimulation both pass start_instant (default DEFAULT_START_DATE) through to derivation, and structural detection runs on a tree that matches the full build's at that point. spm=config gets 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_system docstring 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.

@anth-volk
anth-volk merged commit 37804f4 into main Nov 25, 2026
38 checks passed
@anth-volk
anth-volk deleted the derive-reform-systems-from-baseline branch November 25, 2026 14:59
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.

2 participants