Skip to content

Stop deep-copying the parameter tree in Reform.modify_parameters, and read in-place edits fresh - #645

Draft
MaxGhenis wants to merge 5 commits into
masterfrom
fix-reform-modify-parameters-copy
Draft

MaxGhenis wants to merge 5 commits into
masterfrom
fix-reform-modify-parameters-copy

Conversation

@MaxGhenis

@MaxGhenis MaxGhenis commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #618

Reform.modify_parameters deep-copied baseline.parameters although Reform.__init__ had already cloned them. That copy threw away what apply() had changed before it, and it carried the baseline's cached nodes with it, so an edit that bypassed Parameter.update could read the baseline's values. In-place reforms had the same stale read.

What changes

  • Reform no longer overrides modify_parameters. It inherits TaxBenefitSystem.modify_parameters, so a modifier gets the reform's own tree on both paths a reform runs.
  • TaxBenefitSystem.modify_parameters drops every node cached at an instant once the modifier finishes, even if it raises: in the tree it handed over, in any tree the modifier returned, and in the system's own cache. It rebinds that system cache rather than clearing it, as it did before, because policyengine-us shares the dict between SPM policies.
  • ParameterNode.clear_descendant_caches does the walk, brackets included. A loaded bracket has no parent, so Parameter.update clears the bracket and nothing above it.

The cache design stays as it is, for #621. This PR changes when modify_parameters clears caches, and nothing else about them. That includes one change on the in-place path: a modifier that returns something other than a tree now resets the system cache too, where master returned before resetting it.

Behaviour changes for reforms built as instances

  • Changes made earlier in apply() stay, and successive modify_parameters calls build on each other, as apply_reform_set already did.
  • A modifier that edits the tree in place and returns something else keeps its edits, again as apply_reform_set did. modify_parameters still returns a ValueError here rather than raising it.
  • A reform instance can pass the dict form.
  • modify_parameters on a reform instance returns the reform, not None, as TaxBenefitSystem.modify_parameters does. The annotation now says so.
  • Edits a modifier made before raising stay in the reform's tree, which apply() sees if it catches the exception. With one update and a raise, master's instance read 600 and the branch reads 1, as apply_reform_set does on both.
  • A modifier sees parent set on brackets, because clones set it and a deep copy of a loaded tree left it None. An update inside a bracket now clears caches up to the root on this path.
  • Narrower: a modifier used to get a private deep copy, so it could edit nested metadata or a list value in place. It now gets a clone, which shares those with the baseline (Share nested metadata values between parameter clones #631), so such an edit would reach the baseline. A modifier has to replace them. The docstring says so, a test pins replacing them, and a search of policyengine-us, policyengine-uk and the API found no reform that edits them in place. ct_hb5114 replaces its list through update.

Direct edits with no later modify_parameters, such as Reform.from_dict applied in place to a loaded tree's bracket, can still read stale values. That stays with #621.

Invariants

For any baseline read at any instants, and any reform whose apply() edits the tree directly, reads it, calls modifiers ending in any way and uses the dict form, on either path:

  • Fresh reads: once modify_parameters has run last, the system, its tree and each bracket read as a tree never read at all, given the same edits. That holds for a modifier that raises as well.
  • The two paths agree: an instance and apply_reform_set leave the same tree.
  • The baseline stays put: an instance changes nothing in its baseline, its values, its metadata or the nodes it has cached.
  • Composition: two modifiers do what one modifier making both sets of edits does.
  • Differential: for one modifier on a baseline never read, the tree matches what the old deep copy left. Only this case can match, because the deep copy dropped earlier steps and read stale values.

Two exclusions are deliberate: the direct edit above, and in-place edits of nested metadata or list values.

Tests

  • tests/core/test_reform_modify_parameters.py: 49 examples, including the template reform on both paths, warm and cold edits, brackets through the dict form, returned and dropped trees, a raising modifier, the return value, the rebound system cache and reforms of reforms.
  • tests/core/test_reform_modify_parameters_property.py: 6 Hypothesis properties, 200 derandomized examples each.
  • 40 of these 55 tests fail on master.
  • All 21 mutants of the fix fail the suite: each cache left uncleared, partial walks, a walk that reaches siblings, the walk or the reset moved before the modifier, the system cache cleared rather than rebound, the deep copy restored, None returned and edits rolled back on a bad return. Reviewers predicted the 4 that first survived, and the tests that catch them are in this PR. A 22nd mutant cloned the tree after the edits, which keeps them, so it matched the fix and I dropped it.
  • The fresh-read property reaches the stale state. With the walk removed, 16 of 116 instance examples and 25 of 84 in-place examples read differently from a tree never read. With the system cache kept, 11 and 45 do.
  • test_reforms.py, test_vectorial_parameter_node_copy.py and test_tracing_parameter_isolation.py no longer describe the deep copy or clear caches by hand.

Differential against master

Same model commits on both sides: policyengine-us 2.90.2 at 469ad12cb9, policyengine-uk 2.170.0 at 2da212abc, master at 3c89556f2.

policyengine-us, 85 cases: the Reform objects in the 25 modules that use modifiers, as an instance and in place, the contrib switches, and hand-written bracket and rate reforms. Each case compares the tree digest and household outputs.

  • 84 match. 78 of those changed parameters, and 2 fail the same way on both sides, with VariableNameConflictError from ut_hb210_s2.
  • 1 differs, and master was wrong: set_parameter on a bracket as an instance. The trees match, but master's household still read baseline tax (income tax 1,712). The branch reads -688, which is what the in-place path reads on both Cores.
  • The baseline tree stays unchanged after every instance reform.

policyengine-uk, 25 cases:

  • 23 match, and 21 of those had parameter effects.
  • 2 differ, both intended:
    • A modifier that assigns values as an instance kept master at baseline household income while its tree changed. The branch reads the change.
    • Two dict modifiers as an instance raised TypeError on master and now work, matching the in-place result.
  • The baseline tree stays unchanged.

Test suites on this branch:

  • policyengine-us YAML, the 27 files that name a reform that reaches this code: all pass, 276 cases.
  • policyengine-us Python tests in the 37 files that touch reforms: 674 pass and 1 fails, in 35 files that pass, one that fails on both Cores (below) and one fixture module with no tests.
  • policyengine-uk reform test files, 44 files: all 1,421 tests pass.

Timing

Building a Reform on policyengine-us (156,949 nodes, Python 3.14.7). Median of 5 rounds, wall / CPU seconds, master and branch running side by side on a loaded host.

Build Master Branch
Modifier, tree never read 27.6 / 15.1 3.9 / 2.3
Modifier, read at 32 instants 50.1 / 30.1 5.1 / 2.8
Modifier, never read, GC paused 10.7 / 8.3 0.52 / 0.47
Modifier, read at 32 instants, GC paused 22.3 / 17.0 0.79 / 0.59
set_parameter, read at 32 instants 54.3 / 30.6 4.6 / 2.8
No modifier, tree never read 4.1 / 2.2 4.1 / 2.3

The YAML runner pauses GC while it builds a reform (#643), which is what the two paused rows show. The no-modifier row is the clone both Cores share. Peak memory for the run falls from 5.0 GB to 2.6 GB.

The in-place path did no walk on master, so it gets slower:

  • policyengine-us: a modify_parameters call on a tree read at 16 instants costs 0.13 s of CPU, up from nothing measurable.
  • policyengine-uk: building a household Simulation makes 6 calls and costs 2.01 s of CPU, against 2.00 s on master. A walk of the tree takes a median 1.71 ms.

Not run

The full policyengine-us and policyengine-uk suites: I ran the affected files only, at ea99d5d5f. The commit after it changes only tests, a docstring and a return annotation. The one US failure, TestSocialSecurityInputStateMachine, fails the same way on both Cores here, because it looks for Core's files in site-packages and my install is editable. CI's Test Core and country packages job passes.

axiom: n/a: engine change in Core, no country policy.

🤖 Generated with Claude Code

MaxGhenis and others added 2 commits November 1, 2026 16:25
Reform.__init__ already clones the baseline's tree, so the deep copy in
Reform.modify_parameters paid for a second full copy, threw away what
apply() had changed, and carried the baseline's cached nodes into the
reform. Reform now inherits TaxBenefitSystem.modify_parameters.

That method now drops every node cached at an instant once the modifier
finishes, in the tree it handed over and any tree it got back, so edits
made without Parameter.update read fresh on both paths a reform runs.

Fixes #618

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add tests for the system cache being rebound after the modifier, a read
through the system inside a modifier, the return value, a walk that stops
at the nodes below, and edits made before a modifier raises. Warm the tree
inside apply() so the instance path exercises the walk as well.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
MaxGhenis and others added 3 commits November 30, 2026 18:10
…ild names

Copy mutable values and metadata during reform construction. Document modifier inputs and cache timing, and report invalid callback returns for callable objects.

Co-Authored-By: GPT-6.2 Sol <noreply@openai.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: GPT-6.2 Sol <noreply@openai.com>
@MaxGhenis

Copy link
Copy Markdown
Contributor Author

Agent review for Max, written by Codex and posted by Antigravity for Max Ghenis (exact reviewed head d7c669b8e465887e514050f223f8a74b074c2a93).

VERDICT: REQUEST_CHANGES
HEAD: d7c669b

The cache fix works for the covered reform paths. Current master conflicts in modify_parameters, and the reviewed head introduces two reproducible payload-isolation regressions.

Merge base baf74aa5030f88c766654e4c3bde00db7ddc49e2. Current master a310bdf64169d8610a548562690a8de58d46b4ef. The fetched head matched GitHub. I reviewed the full diff and the changes since round 1 at 92a07ff772. Model GPT-6 Codex, with reasoning effort unavailable.

Findings

  • P1 — policyengine_core/taxbenefitsystems/tax_benefit_system.py:859. Preserve master’s change tracking when resolving the conflict. In a read-only simulation, replace values_list inside a modifier and then calculate again. Master expects ReadOnlySimulationError. A scratch merge taking the PR side instead failed with DID NOT RAISE ReadOnlySimulationError. Keep master’s change_counting_nodes increment around the callback and the PR’s cache cleanup on exit. With both retained, all 76 selected read-only tests passed. Also keep master’s automatically merged clone.parent = None. On the exact PR head, updating a reform cloned from a mounted subtree cleared the baseline ancestor’s dated cache and set its modified flag. The scratch merge preserved both. PR callback, master regression test
  • P1 — policyengine_core/reforms/reform.py:88 and :174. A variable-only reform can now fail before apply(). Put OrderedDict(view=MappingProxyType({"sources": ["salary"]})) in baseline metadata and construct an empty reform. The merge base constructs it successfully. The head and scratch merge raise TypeError: cannot pickle 'mappingproxy' object. The proxy-aware copier handles exact built-in containers, so this nested proxy falls through to deepcopy. Preserve the supported container type and handle nested proxies along that copy path. Payload copier
  • P1 — policyengine_core/taxbenefitsystems/tax_benefit_system.py:871. A replacement retained after failure can leak later edits into the baseline. In an instance modifier, install self.baseline.parameters.clone() through self.parameters, then return None or raise. In a second modifier, append "pension" to a list initially containing "salary". On the head and scratch merge, both the baseline leaf and its already-cached read became ["salary", "pension"]. Both stayed ["salary"] on the merge base. Apply the replacement ownership check and payload detachment to a directly installed tree that survives the call, while preserving the original return or exception. Replacement handling, existing plain-system test
  • P2 — policyengine_core/reforms/reform.py:112. The copy changes alias behavior for ordinary dicts and lists. Set a leaf’s list value to [leaf.metadata] and change leaf.metadata["code"] from 1 to 2 in one cold modifier. The old deep copy preserves the alias and reads [{"code": 2}]. The head reads [{"code": 1}] while the reform’s metadata says 2. Copy from the original payloads into cloned wrappers with a shared memo so references to the same source dict remain references to the same copied dict. Payload traversal
  • P2 — policyengine_core/parameters/parameter_node.py:396. Cleanup can loop on a cyclic child link. root.add_child("cycle", root) succeeds, and a bounded probe yielded the same root three times. A modifier that makes this link and raises would therefore hang in cleanup instead of propagating the exception. The hang is inferred from the traversal. Track visited node identities or reject the cycle when attaching the child. Descendant walk
  • P2 — tests/fixtures/reform_modifiers.py:5 and tests/core/test_reform_modify_parameters_property.py:186. Update the description and fixture rationale for this head. Loaded brackets already have parents on the merge base. A direct bracket update refreshed the bracket and root reads in my probe, while the separate system cache stayed stale. The PR now copies nested payloads, including for reforms without a modifier. Its own read-dependent test also gets 40 from separate calls and from the instance, and 280 from the warm combined in-place call. Scope path agreement and composition to edits independent of intermediate reads and parent links, and label the country results and timings with their measured revision. Bracket loading, read-dependent test

Scope and description checks

  • The diff changes engine behavior and synthetic test inputs. It changes no country parameter values, effective dates, thresholds, or formulas, and cites no government source. I traced the template’s person-month social_security_contribution, income_tax, and basic_income reads. In a synthetic household covering negative and zero salaries and bracket boundaries, both warm reform paths produced [0, 0, 0, 1024, 4336, 4336.40] for salaries [-1, 0, 6000, 12400, 40000, 40001]. Both merge-base paths read the baseline series [0, 0, 120, 504, 3816, 3816.12]. The template probe has no filing-status input. Template contribution formula
  • The code and tests support inheriting modify_parameters, preserving earlier and successive edits, accepting dicts, returning the system on success and a ValueError object for invalid returns, retaining edits on exceptions, and rebinding the system cache. The separate descendant walk refreshes future reads of the handed-over and installed trees. In my update-then-raise probe the head read 1 through both the tree and system on either path. A warm merge-base in-place call read 1 through the tree and 600 through the system, so the description’s old in-place result depends on which cache was read.
  • I checked US SPM detachment and cache rebinding, derived per-capita updates, the CT list replacement, UK replacement installation, and the API’s update callback. Source inspection found no confirmed production caller regression in those paths. The historical differential, country totals, mutant counts, memory figures, and timings remain unverified at this head. The description’s claim that subsequent commits only touched tests, a docstring and an annotation omits runtime changes. Its US state-machine failure explanation is consistent with the backend locating Core through installed-distribution metadata. The YAML runner’s GC pause is present. US SPM, UK replacement, YAML runner
  • The official repository links resolved, as did the Claude Code attribution on Anthropic’s site. GitHub showed no human review or comment and 19 successful checks at this head. The .breaking.md fragment matches the configured towncrier format and passed the changelog check. CONTRIBUTING.md has no axiom: requirement. PR #645, #618, #621, #631, #643, Claude Code

Tests

The existing macOS Python 3.13.9 environment imported Core from the review checkout. Each changed file ran separately under the specified cap.

File Passed Failed Exit
tests/core/parameters/test_vectorial_parameter_node_copy.py 64 0 0
tests/core/test_reform_modify_parameters.py 63 0 0
tests/core/test_reform_modify_parameters_property.py 7 0 0
tests/core/test_reforms.py 30 0 0
tests/core/test_tracing_parameter_isolation.py 49 0 0
tests/fixtures/reform_modifiers.py 0 0 5, no standalone tests

The head passed 213 tests. The property file contains six properties configured for 200 derandomized examples each and one deterministic test. git diff --check passed. The initial throwaway merge exited 1 with one content conflict. Taking the PR side gave one pass and one failure in the two selected read-only cases, exit 1. Preserving change tracking gave 76 read-only passes and 63 reform-example passes, both exit 0. The scratch probes ran on the head, merge base, and resolved merge. The one excluded merge test was tests/core/test_read_only_simulation.py::test_a_read_only_microsimulation_gives_the_same_weighted_results, under the household-only scope.

Proposed regression cases, exercised as scratch probes except for the bounded cycle check:

- case: empty reform with an OrderedDict containing a mapping proxy
  expected: construction succeeds and nested edits stay private
- case: install a baseline clone, then fail before the next modifier
  endings: [return_none, raise]
  expected_baseline_and_cached_list: [salary]
  expected_reform_list: [salary, pension]
- case: update a reform built from a mounted subtree
  expected_baseline_modified: false
  expected_baseline_dated_node: same_object
- case: list value references its leaf metadata dict
  edit: set metadata code to 2
  expected_dated_list: [{code: 2}]
- case: callback attaches the root as its own child and raises
  expected: cleanup terminates and the original exception propagates

The clone and scratch files were removed.

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.

modify_parameters reads stale cached values after in-place edits, and Reform.modify_parameters deep-copies a tree the reform already owns

1 participant