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. 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) diff --git a/policyengine_us/tests/test_pinned_tbs_sharing.py b/policyengine_us/tests/test_pinned_tbs_sharing.py index fc193c6948f..f727d73221c 100644 --- a/policyengine_us/tests/test_pinned_tbs_sharing.py +++ b/policyengine_us/tests/test_pinned_tbs_sharing.py @@ -738,3 +738,81 @@ 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 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 + 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]) + ] + } 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 ) 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])