Skip to content

Declare current-law reads and build the current-law copy on demand - #675

Draft
anth-volk wants to merge 12 commits into
masterfrom
declared-current-law-reads
Draft

anth-volk wants to merge 12 commits into
masterfrom
declared-current-law-reads

Conversation

@anth-volk

@anth-volk anth-volk commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #674

What this adds

Formulas read current law through declared calls, and the simulation that calculates current-law values is built only when something needs it.

  • Declared calls.

    • Population.current_law(variable, period, options) (also through projections and on group populations) returns the variable's value under current law, for a variable that declares the new Variable attribute current_law_read = True in the current-law tax-benefit system. Any other variable raises the new UndeclaredCurrentLawReadError. Without a reform it returns the simulation's own value.
    • Simulation.compares_with_current_law and Simulation.current_law_system (properties) never build a copy.
    • Simulation.current_law_simulation() returns the current-law copy, or the simulation itself without a reform.
  • Tracer. Each declared call is reported to the simulation's tracer through the new record_current_law_call(call, variable, period, branch_name). SimpleTracer ignores it, FullTracer keeps CurrentLawCall records in the new TraceNode.current_law_calls, and core skips a tracer that lacks the method. Reads of simulation.baseline are reported too, as call baseline. The value a current_law call reads stays an ordinary child calculation, so a run's dependency record shows which formulas read current law or asked whether the run differs from it.

  • The copy, built on demand. Simulation.__init__ no longer calls get_branch("baseline"). The overridable _build_current_law_simulation() builds the copy the first time something needs it:

    • a branch named baseline, registered in branches (also on a read-only simulation, unlike its other branches) and traced in the simulation;
    • under default_tax_benefit_system_instance, with holders rebound and its own list of input variables;
    • holding the inputs the simulation holds at that moment and none of the values it has calculated, with empty records of requested variables and pending cache deletions.

    The first declared call that reaches the copy makes it read-only with make_read_only() (Add read-only simulations #672).

  • simulation.baseline is deprecated. It is now a property:

    • reading it returns the copy, building it if needed and leaving it writable, or None without a reform;
    • assigning a simulation makes that simulation the copy, kept as it is given and never made read-only by core;
    • assigning None stops the comparison, and assigning on a read-only simulation raises ReadOnlySimulationError.

    It gives no DeprecationWarning yet: country packages read and assign it on every simulation they build.

  • Branches, clones and subsamples.

    • A branch reads its parent's copy, built once from the parent's inputs, as a branch's baseline was its parent's.
    • clone() keeps a copy only if one was built. A clone of a simulation with no copy builds its own later, from its own inputs.
    • subsample() builds a copy that was built again, through the builder.
    • get_branch("baseline") returns the copy, as it did when construction built it.
    • derivative() builds the copy before cloning, so the changed clone compares with the current law of the unchanged inputs, as before.

Behaviour changes for existing code

  • simulation.branches["baseline"] read right after construction raises KeyError; read simulation.baseline (or use the declared calls) instead. Two existing tests did this and are adjusted.
  • An input set on a reform simulation after construction, and before anything reads current law, now reaches the current-law copy. Before, the copy built during construction never saw it. An input set after the copy is built does not reach it, as before.

Compatibility

  • policyengine-us (main at 97cabe17): still gets its own current-law simulation. During construction, migrate_social_security_inputs reads simulation.baseline, which builds core's copy. _rebuild_baseline_branch then replaces it with policyengine-us's own branch and assigns that, so the copy stays writable and registered.
  • policyengine-uk: never calls core's __init__, and assigns baseline itself. Its subsample announces clones as branches to share arrays; those clones now get a current-law record of their own that does not refer to their source.
  • .py: sets inputs on microsim.baseline right after construction. That works because a copy reached through baseline stays writable.
  • Four policyengine-us formulas remove temporary branches with del simulation.branches[name], which would raise KeyError inside a read-only copy. Open the branching formulas' temporary branches with Simulation.temporary_branch policyengine-us#11254 changes them to branches.pop(name, None). No current package lets a declared call reach a copy that core built.

Checks

  • Tests. tests/core/test_current_law_reads.py, 54 tests. Each test commit precedes the change or fix it tests:

    • The change. 30 tests (001f201d, e1a2d7dd): 29 failed on assertions before the change. The one that passed (get_branch("baseline") returns the copy) also held before. One more test (05485906, the copy starts with nothing requested) failed before its fix. All 31 passed on the change commit 8f0a3781.
    • Review round 1. 12 tests (5d591dd6): 6 failed before the fixes in 977323b6. The other 6 cover behaviour that already held.
    • Review round 2. 7 tests (be5b67cf): 4 failed before the fixes in d0b42e78.
    • Review round 3. 4 tests (1a90495f, a6bc9250): 3 failed before the fixes in 13aad572.
    • On the final commit, all 54 pass.
  • Existing tests. Every test in tests/core/*baseline*, *clone* (simulation and variable clones) and *subsample*, plus the tests elsewhere with "baseline" in their name: 292 tests, each run by node ID, all passing on the final commit. Edited:

    • test_subsample_baseline_branch.py, which now reads simulation.baseline first;
    • test_reform_baseline_system.py, which now builds the branch by reading baseline;
    • the module docstring of test_clone_reform_baseline.py.

    The full suite was not run locally.

  • Values before and after (before: master 8eabab83; after: this branch):

    • policyengine-us household reform. A California household of two earners and a child in 2026, with an income elasticity of -0.05, a substitution elasticity of 0.25 and a 3,000 CTC base amount. Identical for the reform and its baseline: employment_income, labor_supply_behavioral_response, income_tax, household_net_income and medicaid.
    • Core's country template, with a reform income tax rate of 0.42 against 0.15:
      • Ordinary runs and derivative() are identical.
      • Changing salaries to 1,000 and 3,000 after construction, before any current-law read, changes current-law income tax from 600, 375 (the old salaries) to 150, 450. The reform-minus-current-law difference changes from -180, 885 to 270, 810.
  • Review. Three adversarial review rounds by separate review agents, each followed by fixes, each with a test that fails before it:

    • Round 1: 2 medium and 3 low bugs. Fixed: current_law through projections; a missing period built the copy first; derivative() depended on whether the copy was built; removing the registered copy then get_branch("baseline") returned it. One medium was not changed: the policyengine-us del above.
    • Round 2: 1 medium and 1 low bug, both fixed: a clone of a read-only simulation lost track of its copy; a simulation saved by an earlier core shared its baseline with its clones.
    • Round 3: 1 medium and 1 low bug, both fixed: a clone announced as a branch outside get_branch kept its source alive through the current-law record; assigning baseline on a saved simulation left the saved attribute.
    • The fixes after round 3 were not reviewed again.
  • Not run: make documentation, policyengine-uk, the full test suite.

Left as is

  • The policyengine-us del simulation.branches[name] formulas (above): fixed by Open the branching formulas' temporary branches with Simulation.temporary_branch policyengine-us#11254.
  • When the first declared call reaches the copy, make_read_only() copies every array the copy still shares with the reform simulation. For a microsimulation that is its input data, once per run. This is Add read-only simulations #672's documented behaviour, and it only happens in runs that make declared calls.
  • policyengine-us gets its own current-law simulation only because migrate_social_security_inputs reads baseline before _rebuild_baseline_branch runs. An explicit _build_current_law_simulation override in policyengine-us would make that independent of the read.

Merge order

Overlapping open PRs

🤖 Generated with Claude Code

anth-volk and others added 12 commits December 1, 2026 22:31
Tests that fail before the change: Population.current_law reads a variable
declared with current_law_read, an undeclared read raises, each declared call
is reported to the tracer, a reform simulation builds no current-law copy
while it is constructed, the copy is built from the inputs held when it is
first needed and made read-only by the first declared call, and assigning
baseline replaces the copy. Two existing tests read the baseline branch before
anything built it; they now build it through simulation.baseline, or drop it
only if it exists.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Core requires a label on every variable, and income tax is calculated in
float32, so the tests compare it with the expected amounts to one part in a
million.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A variable that must be calculated after another raises in the current-law
copy when the copy has not requested that other variable, as it did when
construction built the copy, even if the reform simulation requested it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Formulas read current law through declared calls, each reported to the
simulation's tracer (record_current_law_call; the full tracer keeps them on
TraceNode.current_law_calls):

- Population.current_law(variable, period) reads a variable declaring
  current_law_read = True, and raises UndeclaredCurrentLawReadError for any
  other; without a reform it returns the simulation's own value.
- Simulation.compares_with_current_law and Simulation.current_law_system,
  which never build a copy.
- Simulation.current_law_simulation(), the simulation that calculates
  current-law values.

A reform simulation no longer branches its current-law copy while it is
constructed. The overridable _build_current_law_simulation() builds it the
first time it is needed: a branch named baseline under the default system
instance, keeping the inputs held then and dropping calculated values. The
first declared call that reaches it makes it read-only. Branches read their
parent's copy, and clone() and subsample() keep a copy only if one was built.
Simulation.baseline is now a deprecated property: reading it returns the copy,
left writable, and is reported to the tracer; assigning a simulation makes it
the copy, kept as given, and assigning None stops the comparison.
get_branch("baseline") still returns the copy.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Tests that fail before the fixes: current_law is projected through entity
projections like a population call; current_law without a period raises
before building the copy and names the calling code; a derivative does not
depend on whether the copy was built before it; removing the registered copy
lets get_branch("baseline") make a new branch; a simulation saved with
baseline in its instance dictionary keeps it; an AttributeError while building
the copy is not read as "no baseline".

Tests for behaviour that already holds: options pass through current_law; a
copy built inside a calculation ignores that calculation's reform values; a
clone of a simulation with a read-only copy gets its own copy; a branch of a
branch reads the first simulation's copy; a deep copy keeps its own copy; the
declaration check reads an assigned simulation's system. One edited test now
builds the baseline by reading it, and two test modules no longer say
construction builds the branch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- current_law is projectable, so person.household.current_law(...) returns
  one value per person, as person.household(...) does.
- current_law checks the period before building the copy, so a missing
  period builds nothing and the error names the calling code.
- derivative builds the current-law copy before cloning, so its result does
  not depend on whether an earlier call built the copy: the changed clone
  compares with the current law of the simulation's own inputs, as before.
- get_branch("baseline") returns a built copy missing from branches only on a
  read-only simulation; elsewhere, code that removed the registered copy gets
  a new branch, as before.
- subsample builds a copy core built again with _build_current_law_simulation,
  so an override applies and the copy starts with nothing requested; other
  baseline branches are rebuilt as before.
- The builder also clears pending cache invalidations.
- A simulation that kept baseline in its instance dictionary (saved by an
  earlier core) keeps it as its current-law copy.
- An AttributeError while building the copy through the baseline property is
  raised as a RuntimeError, so getattr(simulation, "baseline", None) does not
  read it as "no baseline".
- Docstrings no longer say construction builds the copy.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Tests that fail before the fixes: a read-only simulation registers the
current-law copy it builds, so a clone of it keeps its copy registered and
get_branch("baseline") and subsample see it; a saved simulation's own
baseline branch, kept in its instance dictionary by an earlier core, is
copied by clone rather than shared; assigning baseline on a read-only
simulation raises.

Tests for behaviour that already holds: subsample builds a read-only copy
again as a writable one, and does so with an overridden builder; a copy built
in a calculation that runs again after an input change reads the new input.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- A read-only simulation registers the current-law copy it builds, so a
  clone of it keeps its copy registered and get_branch("baseline") and
  subsample see it. get_branch("baseline") now returns the record's copy
  only while it is not built.
- clone() and get_branch() turn a baseline kept in the instance dictionary by
  an earlier core into a record before deciding, so a clone copies a saved
  simulation's own baseline branch rather than sharing it.
- Assigning baseline on a read-only simulation raises
  ReadOnlySimulationError, as assigning tax_benefit_system does.
- The builder and subsample set up the baseline branch with one helper,
  _make_current_law_branch, and subsample gives the simulation a new record
  in both of its paths.
- Docstrings: the builder is also called by subsample; get_branch's
  "baseline" case applies to a reform simulation, not its branches; a
  current_law result from a read-only copy is non-writable.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Brings in the merged #672 (read-only
simulations, final head 7d60a06) and the 3.39.0 release commit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Tests that fail before the fixes: a clone announced as a branch outside
get_branch (as policyengine-uk's subsample makes one, to share arrays) builds
its own current-law copy from its own inputs and does not keep its source
alive through the current-law record; assigning baseline on a simulation
saved by an earlier core drops the saved attribute, so its clones do not
carry it.

Test for behaviour that already holds: a traced clone of a simulation whose
copy is built traces the clone's copy in the clone. The clone test module's
docstring now says when a test's clone builds a copy of its own.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The template's module-level system names the last simulation built from it,
which keeps that simulation alive whatever the clone holds, so the test now
checks that the clone's current-law record refers to the clone and not to its
source. It fails before the fix.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- A clone announced as a branch outside get_branch (policyengine-uk's
  subsample announces one to share arrays) gets a current-law record of its
  own that does not refer to its source, so it no longer keeps the source
  alive, and builds its own copy from its own inputs. get_branch gives the
  branch its parent's record once the clone is made.
- Assigning baseline drops a baseline kept in the instance dictionary by an
  earlier core, so clones and pickles no longer carry it.
- Docstrings: current_law_system names the default system of the simulation
  the copy is built from; current_law_simulation states that the copy is
  registered, also on a read-only simulation; current_law states the
  exception for an assigned simulation; TraceNode.current_law_calls says the
  values read are children only when the calculating simulation shares the
  tracer.

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

This branch has not been deployed

No deployments
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.

Declare current-law reads and build the current-law copy only when needed

1 participant