Skip to content

Record scale, child-list and undated parameter reads in the tracer - #664

Merged
anth-volk merged 16 commits into
masterfrom
record-parameter-reads-in-tracer
Nov 27, 2026
Merged

anth-volk merged 16 commits into
masterfrom
record-parameter-reads-in-tracer

Conversation

@anth-volk

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

Copy link
Copy Markdown
Collaborator

Fixes #541. Fixes #542.

Replaces #543 by Pavel Makarchuk, rebuilt on the per-formula parameter view from #574, following the review of 2026-10-27.

Merge order

PolicyEngine/policyengine-us#11248 must merge before the first core release that contains this PR. test_traced_simulations_record_their_own_parameter_accesses in policyengine-us expects gov.abolitions.spm_unit_fpg and gov.abolitions.spm_unit_size in each traced simulation's recorded parameters. Those names come from core's abolition check, which this PR no longer records, so the test fails with this branch. PolicyEngine/policyengine-us#11248 changes the test to leave out each variable's check of its own abolition switch; it passes with core master and with this branch.

What changes

A traced simulation now records these reads on the node of the variable being calculated:

  • tax-scale reads, with a JSON-safe description of the schedule;
  • which children a node has (_children, iterating, in), at the node's own path;
  • a look-up by name that finds no child, such as getattr(parameters(period).taxes, "future_rate", 0), hasattr(...), or parameters(period).taxes["future_rate"] caught by the formula, at the missing child's path with no value. It builds none of the node's other children;
  • parameters read before an instant is chosen, such as parameters.gov.irs.credits.eitc(period). master records none of these, and US uses them in the federal EITC eligibility formulas among others;
  • attributes of an undated parameter or node that hold values (values_list, children, clone, brackets), at that node's path with period eternity;
  • parameters core reads for adds, subtracts and uprating. Core makes these calls only when the simulation is traced.

Each trace node also records whether its formula ran (formula_ran).

Core's abolition check now reads the parameter tree without a tracing view. When a country package traces its own parameter root, as policyengine_us.Simulation does, the check used to add one read per calculated variable, which made up most of the reads in a US trace. It now adds none.

policyengine-core dependency-map -c <package> writes which variables read each parameter and which variables feed which. An output now counts as covered in a file only after the formula it uses at that period has run and the output calculated without an error. Every recorded read becomes an edge, a formula's read of an abolition switch included. The command keeps the tracer that a country package hands to its own parameter root when tracing is switched on, so reads through that root reach the map.

Relationship to #543

Kept: describe_tax_scale, TracingChildren, _record_parameter_read, and the dependency-map command. The command is a separate commit with Pavel as author. Rebuilt commits carry a Co-authored-by trailer.

Dropped, because #574 already does this: ParameterNode.set_tracing, the tracer write in the Simulation.trace setter, the save and restore of tracing state in _run_formula, the tree updates in get_branch, and the at-instant cache change. parameter_node.py is unchanged.

Changed from #543:

  • test selection (above), now inside trace_yaml_tests, with skipped and excluded counts;
  • the fingerprint covers the whole package and the traced test files;
  • YAML files holding one mapping, .yml files and nested period mappings are read as the runner reads them;
  • the tests now check what gets recorded.

test_tracing_parameter_node_reads_through_to_the_node from master changes, because view.taxes is now a tracing view of parameters.taxes.

Verification

Check Result
The 99 added or changed tests, selected by node ID, with current master (0d66b304, which includes #570) merged into this branch Pass. The merge had no text conflict
household_net_income, one three-person US household: (variable, parameter) reads recorded, leaving out gov.abolitions, on 1936f11b 1,039 on master, 1,201 here, none lost
(Variable, parameter) pairs recorded for marginal_tax_rate, household_net_income and ssi, one three-person CA household, against 1936f11b Only each variable's own abolition check is gone (1,470, 1,462 and 84 pairs). No other pair lost or added

With #570 in both (master 0d66b304 against 8f7ee588): one CA household, household_net_income then marginal_tax_rate for 2026, 6 fresh processes per version in rotating order on a shared machine (medians):

master Here
Untraced time 2.31 s 2.26 s
Traced time 3.17 s 2.64 s
Full garbage collections in a traced run 2 1
Parameter reads recorded 66,725 18,083

All 11,495 (variable, parameter, period, branch) pairs master records outside core's own abolition checks are still recorded, including 462 formula reads of other variables' switches. 1,510 pairs are new. Results were identical in every run.

Before #570 was merged, traced US calculations, 5 runs per version, run in rotating order on a shared machine (medians):

master Here
household_net_income, CA family of four: time 2.21 s 2.21 s
Same: parameter reads recorded; JSON size of the recorded parameters 21,456; 1.0 MB 6,079; 0.7 MB
marginal_tax_rate, one TX adult: time 3.80 s 3.61 s
Same: parameter reads recorded; JSON size of the recorded parameters 57,211; 2.6 MB 14,948; 1.8 MB
Peak memory after serializing the trace, both calculations 1,471 to 1,498 MB 1,465 to 1,471 MB
Traced against untraced outputs, on 866c25ca 1,019 US and 1,700 UK outputs, no differences. Later commits change only what is recorded, not what is calculated
The three parameters the 2026-10-27 review named With default selection, surtax_rate is read by md_capital_gains_surtax, credits.ctc.phase_out.rate by md_ctc and credits.cdcc.rate by co_cdcc

Two adversarial review rounds ran. Round 2 found no missing read and no changed value, but found that a round 1 fix made US traces very large. A third review, of that fix (1936f11b), confirmed it and found two more bugs, both fixed here with tests:

  • a key look-up that found no child was not recorded, so a formula that falls back on a default had no reader edge;
  • dependency-map left out every gov.abolitions read, including another variable's switch. US SSI income helpers read such switches, and turning one on changes the reading variable.

A fourth round reviewed those fixes. It found that recording core's abolition check as a miss for every variable slowed traced US household calculations by about 24%, through an extra full garbage collection, and that dependency-map replaced the tracer a country package had handed to its parameter root, so reads through that root never reached the map. Both are fixed here, by taking core's check out of traces and by keeping that tracer, together with the path of a key that is a str-based enum member. The fixes from the fourth round have had no further adversarial review.

Pavel's review of c968e968 found that, with #570, a traced attribute look-up that finds no child listed the node's children, which builds every child, so a child that fails to build raised in the traced run only. That look-up is now recorded at the missing child's path, as a key look-up is, and builds no other child.

A fifth adversarial review covered that change and the merge with #570. It found no wrong value, no read recorded against the wrong simulation, and no tracing view left in a node that other simulations read; with a child that fails to build, no traced read path builds more children than the untraced one. It found that a look-up by a dotted name was recorded at the dotted path, which can be a real parameter's, and replaced that parameter's value in the flat trace. Such a look-up now records nothing, with a test. Its other findings are minor and listed under the known limits.

Pavel's review of 88dd8e41 found that adds, subtracts and uprating called the read recorder, and read the parameter's name, on every run, traced or not. policyengine-us's test_social_security_input_state_machine.py builds a stand-in simulation that copies only some core methods and has an uprating index without a name, so it failed with this core. Core now makes those calls only when the simulation is traced (c277a33c, with the failing test first in e2535982). With policyengine-us main (b3edc044), TestSocialSecurityInputStateMachine::runTest fails in 2 of 2 runs on 88dd8e41 and passes in 2 of 2 runs on c277a33c.

Not run: the full test suite, which is left to CI; dependency-map over the whole US suite; trace_microdata on US microdata.

Default dependency-map selection compared with --every-test

Each state's baseline test directory only, on current policyengine-us:

State Tests traced / skipped Parameters of that state missing All parameter paths missing
Maryland 156 / 580 2 195
Colorado 202 / 787 1 158
New York 196 / 882 0 17

The default can still miss a formula below an output whose own formula has run. Other test directories should supply many of the paths in the last column, but that was not measured. The command says when it skipped tests and points to --every-test.

Reads this PR does not record

  • Core's abolition check of each variable's own switch (gov.abolitions.<variable>). A formula's read of a switch is recorded.
  • Reads through simulation.tax_benefit_system.parameters rather than the formula's parameters argument. In US: foreign_earned_income.py, tools/marital_units.py, ny_ctc_pre_2024.py and behavioral_response_measurements.py.
  • Reads by simulations a formula creates.
  • Non-leaf vectorial results read only through NumPy methods.
  • Look-ups that find nothing in these forms: a dotted name, which never names a child; a name on a vectorial node; an array that names a missing child; and, on the undated tree, get_child and a bracket position past the end of a scale.

Known limits left open

These came from review. None is reached by current policyengine-us code:

  • a test period such as 2017.5, or a test file that is not UTF-8, stops dependency-map;
  • a package with no tests directory gets a traceback;
  • test files in hidden directories are traced but not fingerprinted;
  • unknown top-level output names are not counted;
  • vars(), dir() and repr() on a tracing view differ from the plain node or record nothing;
  • a parameter directly under the root is recorded as .name when read at an instant.

From the fifth review. The first is not reached by the policyengine-us formulas that read _children; the other two were not checked against policyengine-us:

  • the _children view keeps the child names from its first use, so after add_child on that node at an instant, len and in miss the new child, and the view has no copy() and does not support reversed();
  • names that other libraries try on a node, such as _typ from pandas.isna(node), are recorded as missing children (taxes._typ). Before, the same look-ups were recorded at the node with its child names;
  • a child that exists but raises AttributeError while it is built is recorded as a missing child with no value. Traced and untraced results agree.

🤖 Generated with Claude Code

anth-volk and others added 9 commits November 19, 2026 02:26
Build on the per-formula TracingParameterNode view from #574. The trace now
records tax-scale reads with a description of the schedule, reads of which
children a node has, parameters a formula reads before choosing an instant,
and the parameters core reads for adds, subtracts and uprating. Each trace
node also records whether its formula ran.

Rebuilt from #543. The tree-level tracing state that PR added is dropped,
because #574 replaced it.

Co-authored-by: PavelMakarchuk <pavel.ma99@gmail.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Taken from #543 at f140bc2. The only change is removing the
ParameterNode.set_tracing call, which #574 made unnecessary.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
An output now counts as covered in a file only after the formula it uses at
that period has run and the output calculated without an error. A value masked
by defined_for, given as an input, or from an output that raised no longer
hides later tests. The selection moves into trace_yaml_tests, which reports how
many tests it skipped.

The fingerprint now covers the whole package and the traced tests. YAML files
holding one mapping and .yml files are read as the runner reads them. The
filter on gov.abolitions paths is gone, because core's abolition check is not
traced. The command, trace_microdata and the selection's effect on edges now
have tests.

Co-authored-by: PavelMakarchuk <pavel.ma99@gmail.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Attributes of an undated parameter such as values_list, children and clone are
now recorded at that node's path for all instants, and looking for a child that
is absent records the node. dependency-map counts excluded tests and unreadable
files, follows nested period mappings, ignores hidden files in the fingerprint,
rejects a missing tests root and says when it skipped tests.

Co-authored-by: PavelMakarchuk <pavel.ma99@gmail.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A country simulation that traces its own parameter root sends core's abolition
check through the tracing node, once per variable. Recording absent children
for key look-ups stored every abolition name each time, so only attribute
look-ups are recorded now. dependency-map leaves gov.abolitions reads out again,
because they reach the trace the same way.

Co-authored-by: PavelMakarchuk <pavel.ma99@gmail.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A key look-up that finds no child should be recorded at the missing path,
without the names of the node's other children, so core's abolition check
records only the variable's own switch. dependency-map should leave out only
that switch, and keep a formula's read of another variable's switch.

These tests fail until the next commit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A key look-up that finds no child is recorded again, now at the missing child's
path with no value, so a formula that falls back on a default is a reader of
the parameter that would change it. Core's abolition check therefore records
only the variable's own switch, without the names of the other switches.

dependency-map leaves out only that switch, gov.abolitions.<variable>, and
keeps a formula's read of another variable's switch: policyengine-us's SSI
income helpers make such reads, and turning those switches on changes the
reading variable.

The tracing node now documents how absent children are recorded, and the
docstrings no longer say that key look-ups are not recorded.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A country package can trace its own parameter root. Core's abolition check
then reached the trace, once per variable, and dependency-map had to leave
those reads out. These tests expect the check to record nothing, whether the
switch exists, is missing, or there is no gov node.

They also expect trace_microdata to record reads made through a root the
package traces with the tracer it hands over, and a miss looked up with a
str-based enum member to be recorded at the member's string value.

These tests fail until the next commit.

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

Core's abolition check now reads the parameter tree without a tracing view, so
a country package that traces its own parameter root no longer records one
read, or one miss, per calculated variable. In policyengine-us that removed
the extra objects that caused a second full garbage collection in traced
household calculations. dependency-map no longer needs to leave abolition
switches out: every recorded switch read is a formula's.

dependency-map's tracing setup kept replacing the simulation's tracer after a
country package had handed it to its own parameter root, so reads through
that root went to a tracer nobody read. The same tracer now drops values
instead.

A key that is a str-based enum member is recorded at its string value, the
path the look-up uses. The docstrings and changelog say which look-ups that
find nothing are recorded.

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

@PavelMakarchuk PavelMakarchuk 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.

Verdict: REQUEST_CHANGES (head c968e968b7)

Summary

The tracer changes are correct for the cases that I checked. A traced US household records every (variable, parameter) pair that master core records, except each variable's own abolition switch. The PR intends that removal. The trace also records 474 new pairs. Untraced runs are not slower. Traced runs are faster. The full core test suite passes at head, on head merged with current master, and on head merged with master and #570.

One item blocks approval. The PR removes core's own abolition check from traces. A policyengine-us test asserts that check, so it fails on policyengine-us main with this core. Core CI does not run that test, so CI is green.

#664 and #570 merge cleanly in either order, and the two orders give the same tree. The combined tests pass. One edge case appears only when both PRs are in: a traced formula can raise where the untraced formula does not (see the first suggestion below).

Findings

CRITICAL

None.

SHOULD ADDRESS

1. A policyengine-us test fails with this core. policyengine_core/simulations/simulation.py:1721 now reads the abolition switch through _untraced(...). A country package that traces its own parameter root (policyengine-us does) therefore no longer records gov.abolitions.<variable> for each calculated variable. policyengine-us main (eae2e2775f) asserts these reads in policyengine_us/tests/core/test_spm_simulation_isolation.py:253-261:

expected = {
    "gov.abolitions.spm_unit_fpg",
    "gov.abolitions.spm_unit_size",
    ...
}
assert set(first_accesses) == expected

Failure case: test_traced_simulations_record_their_own_parameter_accesses[2033] and [2034] fail with "Extra items in the right set: 'gov.abolitions.spm_unit_size', 'gov.abolitions.spm_unit_fpg'". They pass with master core. policyengine-us requires policyengine-core>=3.36.0, so the first core release that contains this PR breaks policyengine-us CI. The core "Test Core and country packages" job runs only the smoke tests, so it did not catch this. The PR body does not mention the failure.

Fix: open a policyengine-us PR that changes the test, and link it from this PR. To pass with old and new core, the test can compare only the reads that are not gov.abolitions.*. Merge it before the core release, as #570 did with policyengine-uk#2926.

SUGGESTION

1. With #570, a traced attribute miss resolves every sibling. policyengine_core/tracers/tracing_parameter_node_at_instant.py:261 calls _record_absent_child_read, which calls _record_children_read (line 316). Line 323 runs list(node_at_instant._children). Under #570, _children resolves every child of the node. A child that fails to build then raises inside the traced run only. #570 says that such a child "fails when it is read, not when the node is built, so the node's other children can be read".

Failure case (country template, a node with a child x and a child bad whose _get_at_instant raises; the formula returns p.x + getattr(p, "missing", 10)):

Core Untraced Traced
master error error
master + #664 error error
master + #570 11.0 11.0
master + #570 + #664 11.0 RuntimeError

No current policyengine-us code reaches this case. The US trace with #570 and #664 is the same as with #664 alone (4,465 pairs, no difference), and the 623 partner tests pass traced. The same line also makes each traced attribute miss resolve the whole node, which takes away part of the laziness that #570 adds. A possible fix: on an attribute miss, record the node path without the child-name list, or catch errors from the child listing on that path.

2. Remove the downstream workarounds after the release. policyengine-app-v2 app/scripts/build-parameter-dependency-map.py and us-cliffs-2017 scripts/audit_parameters.py monkeypatch get_traced_child, _get_at_instant or the undated view to record scale reads and undated calls. With this PR they record those reads twice (once with None, once with the scale description). Both scripts put names into sets, so the result does not change. An issue in each repository can say that the workarounds are no longer necessary.

3. Scope. The PR adds the dependency-map command (about 1,340 lines with its tests) together with the tracer fix. A separate PR for the command would make each review smaller. This is optional, because the PR keeps the command in a separate commit and has a separate changelog fragment.

What I checked

  1. PR, issues and history. I read the PR body, the linked issues #541 and #542, and the history of #570 and #660. The PR has no comments and no reviews. The head commit was pushed on 2026-11-19 at 3:45pm EST. CI ran from 4:02pm EST on 2026-11-19, before #660 merged at 4:13pm EST. The PR body tested #570 at c9bbd7e8. The current #570 head is f2451292d5 (pushed 4:33pm EST on 2026-11-19). I tested with the current heads.
  2. Diff, line by line. I read all changes in simulation.py, full_tracer.py, simple_tracer.py, trace_node.py, tracing_parameter_node_at_instant.py, __init__.py, the command parser and tools/dependency_map.py, and the changed test in test_tracing_parameter_isolation.py.
    • Scale reads record {type, thresholds, rates|amounts} with plain Python numbers. Undated reads (values_list, children, clone) record the node path with period eternity and no value. Child-list reads record the node path with the child names. Key misses record the missing path with no value.
    • formula_ran is a new dataclass field with a default, at the end. Positional construction does not change. The flat trace and the computation log do not show it.
    • _record_parameter_read for adds, subtracts and uprating runs only when self.trace is true.
  3. Trace format. For one CA family of four (household_net_income, ssi, eitc, ctc, snap for 2026), the computation log has 23,403 lines with master core and with #664 core. The flat trace has 9,298 keys with both. The performance graph builds with both. Traced and untraced values are equal. Parameter reads go from 22,307 to 6,087. Serialized flat-trace JSON goes from 3.57 MB to 3.23 MB. Duplicate reads in one node go from 1,014 to 873 (repeated reads, as on master). Strict JSON (allow_nan=False) fails with both cores because parameter values already hold inf. Scale descriptions add 10 more parameters with infinite thresholds, so this is not new.
  4. Lost and new edges. Pairs (variable, period, parameter): 10,494 with master core, 4,465 with #664 core. All 6,503 lost pairs are the variable's own gov.abolitions.<variable>. No other pair is lost. 474 pairs are new (scale reads, undated calls such as gov.ssa.uprating(...), values_list reads in the Social Security COLA helpers).
  5. Tracer consumers. I read the consumers that gh search code found: policyengine.py derivations/trace.py, policyengine-server computation_trees.py, policyengine-uk utils/dependencies.py, policyengine-app-v2 build-parameter-dependency-map.py, ccdf-calculator scripts/policy_index.py, microcosm validation_input_coverage.py, computation-tree-explainer app.py and us-cliffs-2017 audit_parameters.py. None reads TraceNode fields by position. None needs a numeric parameter value. Those that read parameter names use only the part before <.
  6. Overhead. Country template, 150 simulations by 3 monthly and 3 yearly variables, 3 rotating rounds with fresh processes (best of 5 in each): untraced 0.237 to 0.243 s on master and 0.238 to 0.244 s with #664 (no difference). US household (household_net_income and marginal_tax_rate, best of 3 warm runs, 2 rounds): untraced 1.43 to 1.49 s on master and 1.42 s with #664. Traced 2.50 to 2.52 s on master and 1.65 to 1.67 s with #664.
  7. Core test suite. Master e556c9e7: 3,980 passed. Head: 3,962 passed. Head merged with master: 4,066 passed. Master with #570: 4,219 passed. Master with #570 and #664: 4,305 passed. Each run also had the 4 known test_release_tagging.py failures (no tomllib in the local setup) and nothing else. The country-template YAML tests pass (39) in all five. The HDF5 lock flake did not occur.
  8. Merge with #570. No file is changed by both PRs. #570 then #664 and #664 then #570 both merge without conflict and give the same tree. The merge order does not matter.
  9. policyengine-us main (eae2e2775f). 803 tests from test_pinned_tbs_sharing.py, the parameter and reform tests, and the tracer, SPM-isolation and Social Security tests: 803 passed with master core; 801 passed and 2 failed with #664 core, and the same with #664 and #570 (the failures are in SHOULD ADDRESS 1). test_pinned_tbs_sharing.py passes. The 623 partner tests pass with master core, #664 core, and #664 with #570 core, both untraced and traced (-v). I did not edit partner test files.
  10. Changelog, mergeable state and CI. Two fragments at the top level of changelog.d/: dependency-map.added.md and tracer-parameter-reads.fixed.md. GitHub shows MERGEABLE and CLEAN. All 19 checks pass. ruff format --check passes.

Not verified

  • dependency-map over the whole US test suite, and --population microdata.
  • policyengine-uk tests with this core.
  • policyengine-api (the search found no direct tracer consumer there; the code search hit the GitHub rate limit once, so the consumer list may be incomplete).
  • A traced US microsimulation.
  • Windows.

anth-volk and others added 5 commits November 25, 2026 18:18
Brings in #570 (parameter-node children built on first read) and #660.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…builds no sibling

Fails now: the read is recorded at the node with every child's name, and
listing those names builds every child, so a child that fails to build
raises in the traced run only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
It was recorded at the node with the names of all its children. Since
nodes at an instant build each child on first read (#570), listing those
names built every child, so a child that fails to build raised in a
traced formula that never read it, and each traced miss built the whole
node. The read is now recorded as a key look-up that finds nothing is:
at the missing child's path, with no value.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Fails now: the miss is recorded at the dotted path, which can be a real
parameter's path, and replaces that parameter's value in the flat trace.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The look-up does not descend into the name, so it finds nothing whatever
the parameters hold. Recorded at the dotted path, the read could match a
real parameter and replace its value in the flat trace.

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

Copy link
Copy Markdown
Collaborator Author

Changes since the review of c968e968. The head is now 88dd8e41.

SHOULD ADDRESS 1: the policyengine-us test. PolicyEngine/policyengine-us#11248 changes test_traced_simulations_record_their_own_parameter_accesses so that it leaves out each trace node's check of its own variable's abolition switch. A formula's read of another variable's switch still counts. It also adds a test variable that reads a parameter through the simulation's own parameter root, so the test still checks that such reads reach the right tracer. It passes with core master and with this branch, and it merged on 2026-11-25 (e93d9b4b). The description now has a "Merge order" section.

SUGGESTION 1: an attribute miss with #570. Current master (0d66b304, which includes #570) is merged into this branch (68c45a60). A traced attribute look-up that finds no child (node.x, getattr with a default, hasattr) is now recorded at the missing child's path with no value, the same way a key look-up is. It no longer lists the node's children, so it builds none of them (8f7ee588, with the failing tests first in 9394ce9d). Your failure case now returns the same value traced and untraced, and the child that fails to build is never built.

SUGGESTION 2: the downstream workarounds. Issues are open in each repository: PolicyEngine/policyengine-app-v2#1598 and PolicyEngine/us-cliffs-2017#9.

SUGGESTION 3: scope. This stays one PR. dependency-map remains a separate commit with its own changelog fragment.

Also changed. An adversarial review covered the #570 merge and the attribute-miss change. It found no wrong value, no read recorded against the wrong simulation, and no tracing view left in a node that other simulations read. It did find one problem: a look-up by a dotted name (getattr(node, "a.b", 0) or node["a.b"]) was recorded at the dotted path. That path can be a real parameter's, and the recorded miss then replaced that parameter's value in the flat trace. Such a look-up now records nothing (88dd8e41, with the failing test first in f8da9e5a). The review's other findings are minor and are listed under the known limits in the description.

Checks at 88dd8e41.

  • The 94 added or changed tests pass, selected by node ID, and CI passes.
  • Timing with Resolve parameter nodes at instant on first read #570 on both sides: one CA household, household_net_income then marginal_tax_rate for 2026, 6 runs, medians.
    • Traced: 2.64 s here against 3.17 s on master.
    • Untraced: 2.26 s here against 2.31 s on master.
  • Every (variable, parameter, period, branch) pair that master records outside core's own abolition checks is still recorded.

@PavelMakarchuk PavelMakarchuk 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.

Review at 88dd8e4

The blocker from my last review is resolved. policyengine-us#11248 merged on 2026-11-25 at 11:58am EST, and the isolation test passes with this head. The new commits fix the attribute-miss case with #570 and the dotted-name case correctly. But a second policyengine-us test fails with this core. I missed it in my last review: the same code was already at c968e968. A one-line guard in core fixes it.

Verdict: REQUEST_CHANGES

Earlier items

Item (review at c968e968b7) Status at 88dd8e41d5
SHOULD ADDRESS 1: test_spm_simulation_isolation.py:253-261 in policyengine-us fails with this core Resolved. policyengine-us#11248 changes the test and merged (e93d9b4b). With this head, the file passes on policyengine-us main (28 passed). The file as it was before #11248 still fails (2 failed, [2033] and [2034]). So #11248 had to merge first, and it did. The PR body now has a "Merge order" section.
SUGGESTION 1: with #570, a traced attribute miss builds every sibling Resolved. tracing_parameter_node_at_instant.py:285-296 now records the miss at the missing child's path and does not list the children. The new test test_attribute_look_up_that_finds_no_child_builds_no_other_child uses my failure case. Traced and untraced both return 1, and the broken child is never built.
SUGGESTION 2: downstream workarounds Done. policyengine-app-v2#1598 and us-cliffs-2017#9 are open.
SUGGESTION 3: scope Kept as one PR, with dependency-map in its own commit and changelog fragment. Acceptable.
Interaction with #570 #570 is in master. master (0d66b304) is merged into the branch (68c45a60) with no manual conflict resolution.

Should address

1. A second policyengine-us test fails with this core. policyengine_core/simulations/simulation.py:1956-1963 (in _calculate, the uprating branch) calls self._record_parameter_read(uprating_parameter.name, ...) on every run. The trace check is inside _record_parameter_read (line 2412), so the arguments are evaluated also when the simulation is not traced.

policyengine-us policyengine_us/tests/core/_social_security_state_backend.py builds a stand-in Simulation. It copies a fixed list of core methods by name (lines 625-660), sets self.trace = False (line 365), and gives a stand-in uprating index that has no .name. With this head, _calculate reaches the new call and fails.

Failure case: policyengine_us/tests/core/test_social_security_input_state_machine.py::TestSocialSecurityInputStateMachine::runTest. Hypothesis finds this sequence: configure_cache(('blacklist', {'social_security_retirement'})), then set_input(component='social_security_retirement', period='2023', route='simulation', amount=0, ...), then a later calculation that uprates.

  • Expected (core master 0d66b304): 1 passed (2 of 2 runs).
  • Actual (this head): 1 failed (3 of 3 runs), with AttributeError: 'DelegatingSimulation' object has no attribute '_record_parameter_read'.

policyengine-us CI runs this file in its Rest Python groups (make test-other-python-rest-*, which includes tests/core). So the first core release with this PR breaks policyengine-us CI. Core CI does not run it.

Fix: in core, wrap the two uprating calls in if self.trace:. I tried this on this head. The state-machine test then passes (2 of 2 runs) with no policyengine-us change. It also skips the .name look-ups in untraced runs. The adds and subtracts calls (lines 2441-2500) can take the same guard for consistency. A fix only in policyengine-us is harder: adding _record_parameter_read to the copied-method list is not enough, because the test then fails with AttributeError: 'function' object has no attribute 'name'.

What I checked

  1. New diff. 68c45a60 is a clean merge of master. After it, the code change is only in policyengine_core/tracers/tracing_parameter_node_at_instant.py, plus tests in tests/core/test_tracer_parameter_reads.py.
    • An attribute miss (getattr with a default, hasattr) now goes through _record_missing_key_read, the same path as a key miss. Dunder names stay unrecorded.
    • A dotted name (getattr(node, "a.b", 0), node["a.b"]) records nothing. Neither ParameterNodeAtInstant.__getattr__ nor __getitem__ descends into a dotted name, so this look-up never finds a child. The new test confirms that the real value at the dotted path stays in the flat trace.
    • A non-dotted miss path cannot match a real child of the same node, because the look-up failed. So it cannot replace a recorded value.
    • The dependency_map.py docstring and the changelog fragment match the new behavior.
  2. Core tests at head (Python 3.12). Full suite: 4,316 passed, 4 skipped, 3 xfailed, 0 failed. Country-template YAML tests: 39 passed.
  3. policyengine-us main (d8b1deb520, includes #11248) with this head.
    • tests/core/test_spm_simulation_isolation.py: 28 passed. The pre-#11248 version of the file: 2 failed, 26 passed, with "Extra items in the right set" for gov.abolitions.spm_unit_fpg and gov.abolitions.spm_unit_size.
    • All of policyengine_us/tests/core: 3,949 passed, 4 failed. One failure is Should address 1. The other 3 (test_capital_gains_response_forms.py float32 round trip, 2 tests, and test_medicare_cost_part_d_premium.py::test_medicare_cost_bounds_and_irmaa_slope) also fail with core master. They come from a newer local pytest (9.2.0, the lock file has 8.4.2), which rejects a float32 tolerance in pytest.approx. They are not from this PR.
  4. Abolition reads. This PR changes only the trace of core's own check (simulation.py:1733-1741, now read through _untraced). The check itself and its result do not change. Core creates gov.abolitions.<variable> for each variable (tax_benefit_system.py:239-268). policyengine-us calls this in system.py:177, and policyengine-us code reads the switches directly in tools/social_security_periods.py:51,71,119 and variables/gov/ssa/ssi/eligibility/income/_annual_input.py:22.
  5. Rebase and CI. master is an ancestor of the head, so no commit on master is missing. GitHub shows MERGEABLE and CLEAN. All 19 checks pass on 88dd8e41. The run started on 2026-11-25 at 10:43am EST.

anth-volk and others added 2 commits November 26, 2026 01:08
Fails now: adds, subtracts and uprating call _record_parameter_read on
every run, and only the method checks whether the simulation is traced.
A stand-in simulation without that method, as in policyengine-us's
Social Security state tests, then fails; inside adds and subtracts a
bare except turns the error into a missing-parameter error.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The trace check was inside _record_parameter_read, so every run called
it and read the parameter's name. policyengine-us's Social Security
state tests build a stand-in simulation that copies only some core
methods and has an uprating index without a name, and failed. The
callers now check self.trace first.

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

Copy link
Copy Markdown
Collaborator Author

Changes since the review of 88dd8e41. The head is now c277a33c.

Should address 1: the policyengine-us Social Security state test. adds, subtracts and uprating now call _record_parameter_read only when self.trace is true. Untraced runs make none of these calls and never read the parameter's .name. All five call sites are changed: the two uprating calls, and the list and parameter-held forms of adds and subtracts. The method's docstring says why (c277a33c, with the failing test first in e2535982).

The new test removes _record_parameter_read from Simulation and runs an untraced calculation for each of the five cases. Before the fix, all five failed. In the adds and subtracts list cases, the bare except: around the call turned the error into "does not match any variable or parameter".

Checks at c277a33c.

  • The 99 added or changed tests pass, selected by node ID.
  • With policyengine-us main (b3edc044), test_social_security_input_state_machine.py::TestSocialSecurityInputStateMachine::runTest behaves as follows:
    • on 88dd8e41: fails in 2 of 2 runs, with AttributeError: 'Simulation' object has no attribute '_record_parameter_read';
    • on c277a33c: passes in 2 of 2 runs, with no policyengine-us change.

The description has the same update.

@PavelMakarchuk PavelMakarchuk 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.

Review at c277a33

The Should address item from my last review is resolved. All five core read sites (two uprating, and the list and parameter-held forms of adds and subtracts) now call _record_parameter_read only when the simulation is traced. Traced runs still record these reads, and the policyengine-us Social Security state test now passes with no policyengine-us change.

Verdict: APPROVE

Earlier items

Item (review at 88dd8e41d5) Status at c277a33c6f
SHOULD ADDRESS 1: test_social_security_input_state_machine.py in policyengine-us fails with AttributeError on _record_parameter_read Resolved. simulation.py:1956, 2447, 2466, 2487 and 2511 gate each call on if self.trace:. The state-machine test passes in 5 of 5 runs with this head. With the 88dd8e41 code it fails in 2 of 2 runs, so the test still reaches this path.
Suggestion in that item: also guard adds and subtracts Done, in the same commit.
Earlier review items (isolation test, attribute miss with #570, downstream issues, scope) Still resolved. test_spm_simulation_isolation.py passes (28 passed). No code for those items changed in the new commits.

What I checked

  1. New diff (88dd8e41..c277a33c). Only policyengine_core/simulations/simulation.py and tests/core/test_tracer_parameter_reads.py change.
    • Each of the five calls is now inside if self.trace:. The value read and the uprating, adds and subtracts arithmetic do not change. The parameter's .name is now read only when traced.
    • _record_parameter_read was the only new self.<method>( call in simulation.py against master (0d66b304). So an untraced run makes no core method call that master does not make.
    • The docstring at simulation.py:2410-2417 says why the callers gate the call.
    • ruff format --check passes on both files. No changelog change is needed: the fix stays inside the existing tracer-parameter-reads.fixed.md change.
  2. The tracer feature is not weakened. The traced tests for these reads are unchanged and pass: test_parameter_in_an_adds_list_is_recorded, test_parameter_in_a_subtracts_list_is_recorded, test_parameter_holding_the_adds_list_is_recorded, test_parameter_holding_the_subtracts_list_is_recorded, test_uprating_records_the_index_at_both_instants, test_uprating_records_the_value_held_before_the_index_starts and test_core_reads_in_a_branch_carry_the_branch_name. When I turned the five guards into if False:, all 7 of these tests failed. So they still catch a lost read.
  3. New test. test_untraced_simulation_does_not_call_the_read_recorder (tests/core/test_tracer_parameter_reads.py:885) covers all five cases. With the 88dd8e41 version of simulation.py, all 5 cases fail. With this head, they pass.
  4. Core tests at head (Python 3.12). tests/core/test_tracer_parameter_reads.py: 64 passed. Full suite: 4,317 passed, 4 skipped, 3 xfailed, 4 failed. The 4 failures are in test_release_tagging.py (the git tag script). They fail the same way with master on my machine, and they pass in CI, so they are not from this PR. Country-template YAML tests: 39 passed.
  5. policyengine-us main (d8b1deb520) with the locked pytest (8.4.2), this head against core master.
    • test_social_security_input_state_machine.py: 5 of 5 passed with this head, 5 of 5 passed with master.
    • test_spm_simulation_isolation.py: 28 passed with each.
    • All of policyengine_us/tests/core: 3,954 passed, 2 failed with each. The same 2 tests fail with both: the Hypothesis tests test_dependent_above_the_line_deductions_invariants.py::test_dependent_deductions_stay_off_the_filers_return_2020 and _2025 (a mn_homestead_credit_refund_household_income mismatch). They are not from this PR.
    • The 3 float32 pytest.approx failures from my last review do not occur with pytest 8.4.2. This confirms that they came from pytest 9.2.0, not from this PR.
  6. CI and merge state. All 19 checks pass on c277a33c. The run started on 2026-11-25 at 4:12pm EST. GitHub shows MERGEABLE and CLEAN. master is an ancestor of the head.

@anth-volk
anth-volk merged commit ba5d367 into master Nov 27, 2026
19 checks passed
@anth-volk
anth-volk deleted the record-parameter-reads-in-tracer branch November 27, 2026 14:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants