From b47297a743ab9370c4c39b1d3f0069e885b70127 Mon Sep 17 00:00:00 2001 From: Anthony Volk <14987227+anth-volk@users.noreply.github.com> Date: Wed, 25 Nov 2026 23:29:50 +0400 Subject: [PATCH 1/6] Test that the section 911 legacy carry records its index reads The legacy fallback of foreign_earned_income_exclusion reads the per-capita AGI index from the system's own tree, so no tracer records either read. The test expects both reads on the formula's trace node, in the calculating simulation's tracer and under its branch name. It fails until the formula records them. Co-Authored-By: Claude Opus 5.5 --- ..._foreign_earned_income_exclusion_leaves.py | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/policyengine_us/tests/test_foreign_earned_income_exclusion_leaves.py b/policyengine_us/tests/test_foreign_earned_income_exclusion_leaves.py index 5b9f10b9a28..227f616c7b6 100644 --- a/policyengine_us/tests/test_foreign_earned_income_exclusion_leaves.py +++ b/policyengine_us/tests/test_foreign_earned_income_exclusion_leaves.py @@ -228,6 +228,66 @@ def test_legacy_aggregate_keeps_its_existing_carry_over(legacy): ) +PER_CAPITA_AGI_INDEX = ( + "calibration.gov.cbo.income_by_source.adjusted_gross_income_per_capita" +) + + +def recorded_reads_by_node(simulation, parameter_name): + """Each trace node's recorded reads of ``parameter_name``. + + Keys are (variable, period, branch name) of the node; values list each + read's (instant, branch name, value). + """ + reads_by_node = {} + pending_nodes = list(simulation.tracer.trees) + while pending_nodes: + node = pending_nodes.pop() + pending_nodes.extend(node.children) + reads = [ + (read.period, read.branch_name, read.value) + for read in node.parameters + if read.name == parameter_name + ] + if reads: + node_key = (node.name, str(node.period), node.branch_name) + reads_by_node.setdefault(node_key, []).extend(reads) + return reads_by_node + + +@pytest.mark.parametrize("branch_name", [None, "uprating_probe"]) +def test_traced_legacy_carry_records_both_index_reads(branch_name): + # The legacy carry reads the per-capita AGI index from the system's own + # tree, not through the formula's ``parameters`` argument, the only tree + # core traces for a formula. Both reads, at the latest known period and at + # the requested one, must be recorded on the formula's trace node, in the + # calculating simulation's tracer and under its branch name, as core + # records the index reads of its own uprating. + simulation = simulation_for_tax_units( + [{"foreign_earned_income_exclusion": {YEAR: 10_000}}] + ) + expected = expected_legacy_carry(simulation, 10_000) + parameters = simulation.tax_benefit_system.parameters + index_before = parameters.get_child(PER_CAPITA_AGI_INDEX)(f"{YEAR}-01-01") + index_after = parameters.get_child(PER_CAPITA_AGI_INDEX)(f"{YEAR + 1}-01-01") + simulation.trace = True + calculating = ( + simulation if branch_name is None else simulation.get_branch(branch_name) + ) + carried = calculating.calculate("foreign_earned_income_exclusion", YEAR + 1) + np.testing.assert_array_equal(carried, expected) + assert recorded_reads_by_node(calculating, PER_CAPITA_AGI_INDEX) == { + ( + "foreign_earned_income_exclusion", + str(YEAR + 1), + calculating.branch_name, + ): [ + (f"{YEAR}-01-01", calculating.branch_name, index_before), + (f"{YEAR + 1}-01-01", calculating.branch_name, index_after), + ] + } + + def test_carried_leaf_inputs_keep_gross_and_federal_net_distinct(): simulation = leaf_simulation([(8_000, 2_000, 1_000, 0, 0)]) federal = simulation.calculate("foreign_earned_income_exclusion", YEAR + 1) From c5253f2ab1cb4ab49cdcfb5ee7208345cdc7d37a Mon Sep 17 00:00:00 2001 From: Anthony Volk <14987227+anth-volk@users.noreply.github.com> Date: Wed, 25 Nov 2026 23:31:51 +0400 Subject: [PATCH 2/6] Record the section 911 legacy carry's index reads in the tracer The fallback reads the per-capita AGI index from the system's tree, which no tracer sees. When the simulation is traced, record both reads in its tracer under its branch name, with the instants and values it used, as core records the index reads of its own uprating. Co-Authored-By: Claude Opus 5.5 --- .../above_the_line_deductions/foreign_earned_income.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/policyengine_us/variables/gov/irs/income/taxable_income/adjusted_gross_income/above_the_line_deductions/foreign_earned_income.py b/policyengine_us/variables/gov/irs/income/taxable_income/adjusted_gross_income/above_the_line_deductions/foreign_earned_income.py index 6769e38f1c0..34a3d288216 100644 --- a/policyengine_us/variables/gov/irs/income/taxable_income/adjusted_gross_income/above_the_line_deductions/foreign_earned_income.py +++ b/policyengine_us/variables/gov/irs/income/taxable_income/adjusted_gross_income/above_the_line_deductions/foreign_earned_income.py @@ -39,6 +39,14 @@ def formula(tax_unit, period, parameters): ) before = _uprating_index_value(index, latest.start) after = _uprating_index_value(index, period.start) + if simulation.trace: + # The index is read from the system's tree, not through the + # formula's ``parameters`` argument, so no tracer sees these + # reads. Record them as core records its own uprating's. + for instant, value in ((latest.start, before), (period.start, after)): + simulation.tracer.record_parameter_access( + index.name, str(instant), simulation.branch_name, value + ) factor = ( 1 if before is None or after is None or before == 0 else after / before ) From 5097ed31e8e0ebbc0cbc2d94b72f31c12c5c770c Mon Sep 17 00:00:00 2001 From: Anthony Volk <14987227+anth-volk@users.noreply.github.com> Date: Thu, 26 Nov 2026 00:07:14 +0400 Subject: [PATCH 3/6] Test that the NY pre-TCJA credit records its pinned-tree read itself ny_ctc_pre_2024 reads the federal CTC phase-out threshold from its branch's pinned tree. Only the tracer the pin copies from its source root records that read today: in the 2020 recomputed-AGI case it carries the other branch's name, and a pin without the copied tracer records nothing. The tests expect the read on the formula's trace node under the calculating simulation's branch name in both cases, with untraced results unchanged. Co-Authored-By: Claude Opus 5.5 --- .../tests/test_pinned_tbs_sharing.py | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/policyengine_us/tests/test_pinned_tbs_sharing.py b/policyengine_us/tests/test_pinned_tbs_sharing.py index fc193c6948f..ae178e22104 100644 --- a/policyengine_us/tests/test_pinned_tbs_sharing.py +++ b/policyengine_us/tests/test_pinned_tbs_sharing.py @@ -738,3 +738,79 @@ def test_a_traced_simulation_leaves_an_ordinary_one_untraced(): assert pin.parameters.trace is False assert pinned_tbs.get_pre_tcja_ctc_tbs(traced.tax_benefit_system).parameters.trace + + +# New York's pre-2024 credit reads this threshold from its branch's pinned +# tree, not through the formula's ``parameters`` argument. The 2017 value for a +# head of household is $75,000; current law has $200,000. +PINNED_PHASE_OUT_THRESHOLD = "gov.irs.credits.ctc.phase_out.threshold" +PRE_TCJA_HEAD_OF_HOUSEHOLD_THRESHOLD = 75_000 + + +def ny_credit_threshold_reads(simulation): + """The threshold reads on each ``ny_ctc_pre_2024`` trace node. + + Keys are (period, branch name) of the node; values list each read's + (instant, branch name, value). + """ + reads_by_node = {} + nodes = list(simulation.tracer.trees) + while nodes: + node = nodes.pop() + nodes.extend(node.children) + if node.name != "ny_ctc_pre_2024": + continue + reads_by_node.setdefault((str(node.period), node.branch_name), []).extend( + (read.period, read.branch_name, read.value.tolist()) + for read in node.parameters + if read.name == PINNED_PHASE_OUT_THRESHOLD + ) + return reads_by_node + + +def traced_and_untraced_ny_credit(situation, year): + traced = Simulation(situation=situation, trace=True) + traced_credit = traced.calculate("ny_ctc_pre_2024", year) + untraced_credit = Simulation(situation=situation).calculate("ny_ctc_pre_2024", year) + assert traced_credit.tolist() == untraced_credit.tolist() + return traced + + +def test_the_ny_credit_records_its_pinned_read_under_its_own_branch(): + # In 2020 a non-itemizer's charitable deduction makes New York's + # recomputed federal AGI differ from federal AGI, so the formula pins the + # system of the branch that holds the recomputed AGI. The read must still + # carry the name of the branch whose formula made it, as core's view of a + # formula's own parameters does, not the name stored on the tree it read. + year = 2020 + situation = household(year=year) + situation["people"]["head"]["charitable_cash_donations"] = {year: 300} + traced = traced_and_untraced_ny_credit(situation, year) + assert ny_credit_threshold_reads(traced) == { + (str(year), "default"): [ + (f"{year}-01-01", "default", [PRE_TCJA_HEAD_OF_HOUSEHOLD_THRESHOLD]) + ] + } + + +def test_the_ny_credit_records_its_pinned_read_without_a_tracer_on_the_pin( + monkeypatch, +): + # A pin copies the root's fields, including the tracer a traced request + # wrote there, and only that copied tracer records the formula's read of + # the pinned tree. The reform systems in ``system.py`` clear those fields on + # their copy; a pin that did the same must not lose the read. + def pin_without_copied_tracer(base_tbs): + pinned = pin_spm_policy(base_tbs) + pinned.parameters.trace = False + pinned.parameters.tracer = None + pinned.parameters.branch_name = None + return pinned + + monkeypatch.setattr(pinned_tbs, "pin_spm_policy", pin_without_copied_tracer) + traced = traced_and_untraced_ny_credit(household(), YEAR) + assert ny_credit_threshold_reads(traced) == { + (str(YEAR), "default"): [ + (f"{YEAR}-01-01", "default", [PRE_TCJA_HEAD_OF_HOUSEHOLD_THRESHOLD]) + ] + } From 81b53c1070a533f352d19f18a390885c091fe8d9 Mon Sep 17 00:00:00 2001 From: Anthony Volk <14987227+anth-volk@users.noreply.github.com> Date: Thu, 26 Nov 2026 00:12:05 +0400 Subject: [PATCH 4/6] Record the NY pre-TCJA credit's pinned-tree reads in its own tracer When the simulation is traced, wrap the branch's pinned tree in core's per-call tracing view with the calculating simulation's tracer and branch name, as core wraps a formula's parameters argument. The read no longer depends on the tracer a pin copies from its source root, and in the 2020 recomputed-AGI case it now carries the calculating simulation's branch name. Co-Authored-By: Claude Opus 5.5 --- .../ny/tax/income/credits/ctc/ny_ctc_pre_2024.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/policyengine_us/variables/gov/states/ny/tax/income/credits/ctc/ny_ctc_pre_2024.py b/policyengine_us/variables/gov/states/ny/tax/income/credits/ctc/ny_ctc_pre_2024.py index ed95653cc32..1ae5fa5414e 100644 --- a/policyengine_us/variables/gov/states/ny/tax/income/credits/ctc/ny_ctc_pre_2024.py +++ b/policyengine_us/variables/gov/states/ny/tax/income/credits/ctc/ny_ctc_pre_2024.py @@ -1,4 +1,5 @@ from policyengine_us.model_api import * +from policyengine_core.tracers import TracingParameterNode from policyengine_us.tools.period_branch import drop_recomputed_values from policyengine_us.tools.pinned_tbs import ( CREDIT_DEPENDENT_VARIABLES, @@ -54,6 +55,16 @@ def formula(tax_unit, period, parameters): simulation.tax_benefit_system ) branch_parameters = pre_tcja_ctc.tax_benefit_system.parameters + if tax_unit.simulation.trace: + # This formula reads the pinned tree itself, not through its + # ``parameters`` argument. Record those reads as core records + # the argument's: in this simulation's tracer, under its + # branch name. + branch_parameters = TracingParameterNode( + branch_parameters, + tax_unit.simulation.tracer, + tax_unit.simulation.branch_name, + ) # Recalculate every CTC formula variable under the pinned rules, a # supplied current-law value included. drop_recomputed_values(pre_tcja_ctc, ["ctc", *CREDIT_DEPENDENT_VARIABLES]) From e7c8fe475885a6450a1b5b6ace141f1962bd94a4 Mon Sep 17 00:00:00 2001 From: Anthony Volk <14987227+anth-volk@users.noreply.github.com> Date: Thu, 26 Nov 2026 00:12:18 +0400 Subject: [PATCH 5/6] Add changelog fragment for the recorded parameter reads Co-Authored-By: Claude Opus 5.5 --- changelog.d/record-unrecorded-parameter-reads.fixed.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/record-unrecorded-parameter-reads.fixed.md diff --git a/changelog.d/record-unrecorded-parameter-reads.fixed.md b/changelog.d/record-unrecorded-parameter-reads.fixed.md new file mode 100644 index 00000000000..9f2ec4b7a3f --- /dev/null +++ b/changelog.d/record-unrecorded-parameter-reads.fixed.md @@ -0,0 +1 @@ +A traced simulation now records two parameter reads that bypass a formula's `parameters` argument, on the trace node of the formula that makes them, in the calculating simulation's tracer and under its branch name: the per-capita AGI index reads of `foreign_earned_income_exclusion`'s carry-over of an earlier year's amount, which no tracer recorded before, and `ny_ctc_pre_2024`'s read of the federal CTC phase-out threshold from its pinned pre-TCJA tree, which was recorded only through the tracer a pin copies from its source and, in 2020 cases with recomputed federal AGI, under another branch's name. From a5d2134b89820c810b2b8564a27896aee1e0ab6e Mon Sep 17 00:00:00 2001 From: Anthony Volk <14987227+anth-volk@users.noreply.github.com> Date: Thu, 26 Nov 2026 00:47:50 +0400 Subject: [PATCH 6/6] Say that the pin-without-tracer test guards against a later change No current route builds a pin without the copied tracer; the test clears the fields itself. Co-Authored-By: Claude Opus 5.5 --- policyengine_us/tests/test_pinned_tbs_sharing.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/policyengine_us/tests/test_pinned_tbs_sharing.py b/policyengine_us/tests/test_pinned_tbs_sharing.py index ae178e22104..f727d73221c 100644 --- a/policyengine_us/tests/test_pinned_tbs_sharing.py +++ b/policyengine_us/tests/test_pinned_tbs_sharing.py @@ -797,9 +797,11 @@ def test_the_ny_credit_records_its_pinned_read_without_a_tracer_on_the_pin( monkeypatch, ): # A pin copies the root's fields, including the tracer a traced request - # wrote there, and only that copied tracer records the formula's read of - # the pinned tree. The reform systems in ``system.py`` clear those fields on - # their copy; a pin that did the same must not lose the read. + # wrote there, and without the fix only that copied tracer records the + # formula's read of the pinned tree. No current route builds a pin without + # it, so this test clears the fields itself. It guards against a later + # change that clears them, as the reform systems in ``system.py`` do on + # their copy: the read must still be recorded. def pin_without_copied_tracer(base_tbs): pinned = pin_spm_policy(base_tbs) pinned.parameters.trace = False