Repository navigation
Refuse population aggregates in the US input-stage batch passes and release their engines (1.6 · 2) - #1776
Merged
Conversation
…d release engines Add failing tests for tracker task 1.6 · 2. The release's five input-stage loops (ACA source inputs, Medicaid eligibility, December SSI amounts, SNAP eligibility and the other-health-insurance premiums) should: - write exactly what main's loops write: a frozen copy of each loop from origin/main c585965 is the reference, compared byte for byte; - refuse a batch engine, or a branch it dropped, that computed a population-aggregate variable, as target compilation does, with a refuse_population_aggregates override; - refuse a frame whose groups or Medicaid claims cross households; - release every batch engine before the next, also when a batch fails. Also test the new household_batches module with stand-in engines, the US adapter's caller-owned engine, and that the local-area build switches the refusal on for its donor and ACS views. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…se their engines The release's five input-stage passes over household batches (ACA source inputs, Medicaid eligibility, December SSI amounts, SNAP eligibility and the other-health-insurance premiums) now run each batch engine in checked_batch_engine: with more than one batch, or refuse_population_aggregates=True, an engine or a branch it dropped that computed a population-aggregate variable is refused before the engine is released, and every engine is released in a finally. household_batch_plan refuses a frame whose groups span households, or whose positive medicaid_claiming_tax_unit_id points outside the household, before any engine is built, as target compilation does. The household partition, the selection, both frame checks, the branch recorder, the refusal and the three garbage-collection helpers move from the release tool into microcosm.build.us_runtime.household_batches under public names, and every reference moves to them (_collect_family_garbage is now collect_pass_garbage). Target compilation's refusals name their pass and keep their message. The PolicyEngine-US adapter gains build_microsimulation and calculate_variables, which materialize calls. The other-health premium loop calls them itself, so it checks and releases its engines, and it uses the tool's garbage-collection pattern instead of a full collection per batch. The local-area build passes refuse_population_aggregates=True to the SSI and Medicaid passes, because its donor and ACS views are each part of the population, and drops its duplicate frame checks. policyengine-us 2.2.1 reaches no population-aggregate variable in these passes; every loop's output is byte-identical to main's on the test fixtures. Also fix two assertions in the new tests: the probe's refusal lists the state cost index beside the denominator, and the stage test's stand-in for main's loop takes the new keyword. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…on changes Review round 1 found that the byte-identity tests could pass while the behavior they name was broken: - assert_same_bytes compared NumPy dtypes after to_numpy(), which drops a pandas extension dtype, and accepted a Series in place of an array. It now compares container types and pandas dtypes too. - The fixture's rows were in household order, so a loop that placed batch results at a running row offset instead of by id wrote the same bytes. The identity tests now use input_loop_identity_frame: group ids run against household order, person rows are reversed, weights differ, and a New York family, reported premiums and ACA target tables make every compared array hold more than one value (medicaid_premium is zero everywhere in policyengine-us 2.2.1). - Nothing tested the other-health loop's garbage-collection pattern; an engine-free test now does. The adapter test's docstring now says that it checks the split's contract, and that the comparison with main's materialize is the other-health byte-identity test. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…s conditions Review round 2: no test passed refuse_population_aggregates=True through with_us_other_health_insurance_inputs, so dropping the forwarding would have passed. An engine-free test now runs the stage in one batch with an engine that holds a population aggregate: kept by default, refused with the switch on. The changelog fragment now says when the refusal and the frame checks run, and that the claiming-ID check refuses an ID that names no tax unit in the household. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…usal Resolve _reform_household_income_tax onto main's reform-system-only batch engines and its baseline-branch refusal, reading the moved household_batches helpers. Keep main's two new refusals and drop its copies of the functions this branch moved. Point the new JCT batch tests at the moved names. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
CI failures on #1776: - test_us_runtime_module_classification_is_complete requires every us_runtime module to be classified; household_batches.py is now listed with the reviewed non-registry modules (it applies no population treatment and reads no spine column). - The spec-engine inventory pins the seed protocol, whose digest covers the source of us_runtime.other_health_insurance. That source changed (the premium loop's batching and release), not its random draws, so the seed protocol and seed map digests and the US bundle's spec_sha256 are re-pinned with tools/repin_us_spec_engine_contracts.py and the coverage evidence regenerated with tools/spec_engine_coverage.py (41/41 inventory checks). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
anth-volk
marked this pull request as ready for review
November 27, 2026 14:14
anth-volk
requested review from
MaxGhenis and
juaristi22
and removed request for
MaxGhenis
November 27, 2026 14:14
test_the_batterys_binding_identity_adds_only_the_leaf_and_its_imports counts the modules the release-check bindings reach. us_runtime.other_health_insurance now imports us_runtime.household_batches, so the bindings reach one more module; list it in BINDING_MODULES_ADDED_AFTER_BASE. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
juaristi22
reviewed
Nov 27, 2026
juaristi22
left a comment
Collaborator
There was a problem hiding this comment.
Reviewed at 5b54c912f: on a 24-household frame of my own, all five passes write the same bytes as on main (c4f421d1f) in one batch and in batches of 1 and 5 households, none reaches a population aggregate with the refusal on at one household per batch, and, merged into current main, the 51 input-loop tests pass alongside #1777's two pinned-rules tests. Without a full collection after each batch, the other-health pass takes 32 CPU-s instead of 64 over 24 batches here, which over a release build's 177 batches comes to roughly 4 CPU-minutes. Nothing to raise.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Before target compilation, the US release runs policyengine-us over its households in batches in five input-stage passes:
tools/build_us_fiscal_refresh_release.py: ACA source inputs (_aca_source_tax_unit_table_batched), Medicaid eligibility (_medicaid_person_eligibility), December SSI amounts (_ssi_person_uncapped_amount) and SNAP eligibility (_snap_spm_unit_eligibility);us_runtime/other_health_insurance.py: the other-health-insurance modeled premiums (_materialize_modeled_premiums).A population-aggregate variable is a policyengine-us formula that sums or ranks over everyone in its engine: the Medicaid SLCSP state sums and the household and SPM-unit income deciles. A batch engine would compute it over its own batch instead of the whole population. Target compilation and its reform passes already refuse an engine that computed one; these five passes did not. Other health also never released its batch engines.
Now all five passes:
medicaid_claiming_tax_unit_idnames no tax unit in the person's household, as target compilation does;Outputs are unchanged. In policyengine-us 2.2.1 none of the five passes reaches a population-aggregate variable. Every pass writes the same bytes as main's on the test fixtures.
Tracker task 1.6 · 2 in project "15. US build memory, CPU and run time". Task 2.3 · 4 moves these passes into the worker pool from #1726 and can build its worker units from the shared module this PR adds.
What changes
New module
microcosm.build.us_runtime.household_batches. These functions move from the release tool, under public names, and every reference moves with them (no aliases are kept):household_position_batches) and the household selection (select_households_by_position);assert_group_entities_nest_in_households, withgroup_to_household_positions) and the Medicaid claiming-ID check (assert_medicaid_claiming_tax_units_local);record_engine_branches) and the refusal (refuse_batch_population_aggregates);automatic_gc_suspended,collect_batch_garbage, andcollect_pass_garbage, which was_collect_family_garbage).Two functions are new:
household_batch_planmakes the batches and runs the frame checks before any engine is built.checked_batch_engineruns one batch on its engine. With the refusal on, it records the branches the batch creates and refuses an aggregate before release. It releases the engine in afinally.When the refusal is on. As in target compilation, it is on when a pass has more than one batch. A new
refuse_population_aggregatesparameter on each pass overrides that. The nesting check runs with more than one batch or with the refusal on; the claiming-ID check runs with the refusal on.Refusal messages. They name their pass ("The SNAP eligibility pass is not batch-invariant: ..."). Target compilation's messages keep their exact text (
TARGET_MATERIALIZATION_PASS,TARGET_POPULATION_AGGREGATE_REMEDY).PolicyEngine-US adapter.
PolicyEngineUSEnginegainsbuild_microsimulationandcalculate_variables;materializeis now those two calls. The other-health loop calls them itself, so it can check and release its engine. The dataset it builds is the same as before. microcosm-frame still imports nothing from microcosm-build.Other-health garbage collection. The pass now uses the release tool's pattern: no automatic collection inside a batch, a youngest-generation collection after each batch, and one full collection at the end. It used to run a full
gc.collect()after each of its 177 batches.Local-area build (
tools/build_us_acs_local_release.py)._acs_engine_valuesruns the SSI and Medicaid passes on the donor households, then on the ACS households. It now passesrefuse_population_aggregates=True, because neither view is the whole population, so even a view that fits in one chunk would compute a view-local aggregate. A comment explains this. Its own calls of the two frame checks are removed, because the passes now run them.Other references renamed:
tools/score_us_release_head_to_head.py,tools/check_us_acs_county_agi_support.py, a docstring intools/probe_us_post_export.py,test_supportand the tests.tools/us_household_measures.pykeeps its own, separate_assert_medicaid_claiming_tax_units_local.Results are unchanged
test_support/microcosm_build/us_input_batch_loops_main_baseline.pyis a frozen copy of the five passes and ofmaterializefrom origin/main c585965. It reads the tool's unchanged helpers, and holds its own copies of the partition, selection and garbage-collection helpers, so it does not follow the code under test.test_input_loop_writes_mains_bytes_and_releases_each_batch_enginecompares each pass with that copy, in one batch and in batches of two households, oninput_loop_identity_frame. The comparison covers values, NumPy and pandas dtypes, container types, index and column order. The fixture:medicaid_premium, which is zero everywhere in policyengine-us 2.2.1.test_other_health_insurance_stage_writes_mains_tablesruns the stage in two batches and compares every table it writes with the same stage running main's premium loop.How the input passes were checked for aggregates
Before changing any code, two checks on policyengine-us 2.2.1:
A reviewer's broader static scan, which also follows
adds/subtractslists anddefined_for, found no path either. policyengine-core skips adefined_forformula when nobody in the engine qualifies, so this depends partly on the data; the refusal now guards production batches.This was not checked under policyengine-us 2.145.3 (draft #1676). The new tests will show whether any pass reaches an aggregate there.
Tests
Red-green: the failing tests are in 65aec3d and the implementation in 158401c. Run against the old code, 44 of the 52 new real-engine tests failed; the 8 that passed covered behavior the four tool passes already had. 19a70fd and 7c61a3e hold the review fixes, aeee9c1 merges main, and 33cf432 fixes the two CI failures (module classification and the re-pinned digests).
Tests run (only the ones this PR adds or changes, by node ID):
engine_workflow/us/test_us_input_batch_loops.py, covering byte identity, the refusal on a batch engine, on a dropped branch and in one batch with the refusal switched on, the refusal switched off, release when a batch fails, and frames refused before any engine;TestMaterialize::test_a_caller_owned_engine_gives_materializes_arrays;test_real_engine_refuses_a_batched_medicaid_cost_targetandtest_batches_read_from_their_files_equal_the_written_h5s_batches.test_us_household_batches.py(new);test_us_other_health_insurance.py, including a new one for the garbage-collection pattern and one for passing the refusal switch through the stage;Review
Two rounds of adversarial review by subagents:
Fingerprints that change
The seed protocol digest covers the source of
us_runtime.other_health_insurance, and that source changed: the premium loop's batching and engine release. Its random draws did not change. Sotools/repin_us_spec_engine_contracts.pyre-pinned:spec_engine/inventory_coverage.py;spec_sha256in two tests.tools/spec_engine_coverage.pyregenerateddocs/evidence/spec-engine/us-f0-coverage.json, with 41/41 inventory checks.household_batches.pyis listed with the reviewed non-registryus_runtimemodules intest_us_spine_blindness.py.Other work on the same code
_reform_household_income_taxkeeps JCT reform batches: give each batch engine the reform system alone #1773's reform-system-only engines and baseline-branch refusal, and reads the moved helpers. Its new JCT batch tests use the moved names._ssi_person_uncapped_amountand_medicaid_person_eligibility. This PR adds an optional parameter to both functions and leaves those calls unchanged.builder._record_engine_branches. After this PR they becomebuilder.record_engine_branches, or an import fromhousehold_batches.Whichever merges later rebases onto the other.
🤖 Generated with Claude Code