Skip to content

Return every US post-export request as a picklable descriptor - #1722

Merged
anth-volk merged 16 commits into
mainfrom
claude/post-export-request-descriptors-d699c1
Nov 26, 2026
Merged

anth-volk merged 16 commits into
mainfrom
claude/post-export-request-descriptors-d699c1

Conversation

@anth-volk

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

Copy link
Copy Markdown
Contributor

What this does

The US release's post-export scoring learns what each post-export stage will ask policyengine-us for by running the stage once against engine-free recording stand-ins before the export (the "dry run" in tools/build_us_fiscal_refresh_release.py). This PR adds _record_post_export_request_descriptors, which returns every request each stage makes in that dry run as a picklable PostExportRequest, so later work (tracker task 1.4 · 7) can send the requests to worker processes started with spawn and rebuild each stage's output from their results. The release itself still takes only each stage's baseline plan and reform-pass count from the dry run. Nothing in the release uses the descriptors yet, and nothing records them.

The descriptor

PostExportRequest lives in the new module microcosm.build.us_runtime.post_export_requests, so a spawned process can unpickle it however the release tool was started. The tests and the sweep tool load the tool by file path (outside sys.modules), where a type defined in it cannot be unpickled at all. Each descriptor holds:

  • consumer: the stage's name in the post-export plan (reform_coverage_smoke, reform_validation, demographics, ssi_social_security_holdout, medicare_irmaa_holdout);
  • position: the request's place in the order the serial scorer runs the stage's requests. The baseline plan comes first (its keys are scored together in one pass, in ascending period order and then request order), then one reform pass per reform request, in the order the stage first asks each reformed simulation for each key;
  • variable, period and map_to. map_to=None means the variable's own entity, which the engine resolves in the worker; the dry run has no engine, so it cannot name that entity itself;
  • reform: None for the baseline, otherwise the description the reform is rebuilt from. build_reform() rebuilds it with the stage's own builder: a ReformCoverageProbe through reform_coverage_smoke._build_reform, a ReformValidationSpec through its build_reform, or a new ParameterChangesReform (one cumulative OBBBA state's Reform.from_dict payload) through reform_validation._build_parameter_reform;
  • reformed_simulation: which of the stage's simulate(reform) calls made the simulation that asked, numbered from 0 in call order (a simulation never asked keeps its number but makes no request).

Descriptors cannot be hashed (most reform descriptions hold dictionaries), so results should be keyed by (consumer, position). A ParameterChangesReform keeps a deep copy of its payload, so a later change to reform validation's dictionaries, which its specs share, does not reach a recorded request.

Why reform validation changes

Reform.from_dict returns a class defined inside a function, which pickle cannot name (checked on policyengine-core 3.32.5). So the dry run cannot keep the reforms reform validation builds. Reform validation now does what the smoke check already did: when its simulate carries RECORDS_REQUESTS_ONLY, it hands it the spec (for a row simulated on its own) or a ParameterChangesReform (for each cumulative OBBBA state) instead of building the reform. The other path, which scores the H5, builds and passes the same reforms in the same order as before. As a result the dry run builds no reform and no longer needs policyengine-core. The dry run refuses any reform that is not exactly one of the three description types (a subclass's module might be missing in a worker), and any description that fails a trial pickle.dumps, with a TypeError, which the release records in the plan and refuses before the export, as it does for any other plan failure. RECORDS_REQUESTS_ONLY moves from reform_coverage_smoke to the new module.

One pre-export check goes away with this: on origin/main the plan's dry run built every reform-validation reform class (64 calls to Reform.from_dict or spec.build_reform() on the fixture) before the export, which would have caught a missing policyengine-core or a changed Reform.from_dict signature then rather than at the first reform-validation pass. The smoke check already worked this way.

A reformed simulation asked after the next opens

Since the shared worker pool (#1726), the scorer finishes a reformed simulation when its stage opens the next one, and refuses it any further key, even one it already scored. The dry run now refuses the same thing: each stand-in reformed simulation shares the dry run's list of opened reforms and raises once a later one is open. A stage that goes back to an earlier reformed simulation therefore fails the release's plan, which refuses the run before the export, instead of failing certification's scoring after it. No shipped stage does this; the pinned digests are unchanged.

What stays the same

  • _record_post_export_requests still returns the baseline plan and the reform pass count, now derived from the same dry run that produces the descriptors. _post_export_scoring_plan, _PostExportScoringPlan and POST_EXPORT_SCORING_METHOD are unchanged.
  • The dry run still assumes a stage asks for the same keys whatever values it gets back, and the served baseline still refuses any key outside the recorded plan.
  • The plan written to calibration_diagnostics.json and every stage's output keep their bytes, and each stage's planned reform-pass count is unchanged (43 for the smoke check, 64 for reform validation on the fixture). test_the_release_s_recorded_post_export_outputs_are_unchanged pins the SHA-256 of each output and the counts on the fixture frame and fake engine. The values were taken by running that test on origin/main at fcabe4e before the change, and a reviewer reproduced the digests there independently. The digests also cover the shipped probes, specs, baseline levels and holdout benchmarks, so a later change to any of them needs a re-pin. The manifests' post_export_scoring block is not pinned; a reviewer compared it on both trees and found it identical.

The guard sweep

tools/sweep_us_post_export_scoring.py had its own recorder that kept the built reform objects. It now records through _record_post_export_request_descriptors, and each worker rebuilds only the reforms it scores from their descriptors. Its counts, request order and comparison choices are unchanged (43 smoke passes, 66 validation passes, 109 in all). One behaviour moves: its baseline worker no longer builds every reform while recording, so a probe's list edit that no longer fits the installed engine now fails in the worker that scores that probe. Every reform is still built by some worker.

For 1.4 · 7, which will consume these (formerly "For 2.3 · 1, which will consume these")

  • Reform requests are not checked against the dry run the way baseline keys are: the served baseline refuses a key outside its plan, but a reformed simulation answers any key. A stage that chose a reform key from the values it got back would run a pass no descriptor lists. No shipped stage does, and today the count only feeds progress reporting. The step that rebuilds outputs from worker results is the place to refuse a reform key with no descriptor.
  • The smoke check's output records each list-edit probe's resolved lists from the reform it builds itself (resolved_list_edits), which is not part of any request's result. Rebuilding that output means running the check again over the results with its reforms built in the parent (cheap: no tax-benefit system is built), not assembling it from results alone.
  • The trial pickle checks only that a description pickles in the parent. A value nested inside a description whose class exists only in the parent (no shipped description has one) would pickle there and fail to unpickle in a worker, and a worker that fails to unpickle its call kills a ProcessPoolExecutor for every pending request. The pool may want to unpickle each request's arguments inside a try in the worker.
  • A release runs the tool as a script, so every worker started with spawn from a release run executes the tool's whole module body again as __mp_main__, as spawn does with any parent's main script. The pool should start workers in a way that avoids this, or accept the cost.

Tests

Red-green: the first commit adds the failing tests, the second the change. Three rounds of adversarial review followed; the fixes from rounds 1 and 2 are each a failing-tests commit followed by the fix. Round 3 found no bug; its two wording points are fixed in the last pair of commits. All three rounds reproduced the pinned output digests on origin/main independently, and round 2 rebuilt every one of the 107 shipped reformed simulations' reforms from descriptors on policyengine-us 2.2.1, in the parent and in a spawned process, and found no difference from what each stage builds.

New, in packages/microcosm-build/tests/engine_free/us/test_us_post_export_requests.py:

  • the dry run returns one descriptor per request of every shipped stage, with no engine package importable and every reform builder replaced by one that fails, and agrees with the plan and pass count the release records;
  • every shipped descriptor round-trips through a process started with spawn and comes back equal, its types coming from microcosm.build.us_runtime modules only;
  • each stage, scored through the batched scorer on the fake engine as _main scores it, runs its reform passes in descriptor order, on the reformed simulation each descriptor names, with the reform each descriptor rebuilds;
  • reform requests follow the order of first asks, each reformed simulation asked for all its keys before the next is opened (the scorer from Shared worker pool for household-batched policyengine-us work (1.4 · 1) #1726 refuses a finished reformed simulation further keys);
  • the dry run refuses a built reform, a subclass of a description type from a module only this process has, a description whose pickling fails (a read-only mapping, a value whose pickling raises ValueError), and the release records the refusal in its plan;
  • the dry run refuses a key asked of a reformed simulation after the next one opened, whether or not it asked that key before, and the scorer refuses the same stage;
  • descriptors cannot be hashed, and a ParameterChangesReform keeps its own copy of its payload;
  • descriptor field checks, ParameterChangesReform checks, the PostExportReform alias, and builder dispatch;
  • the recorded outputs' digests.

Changed: the guard-sweep test now checks descriptors, the smoke check's recording test imports RECORDS_REQUESTS_ONLY from its new module, and test_us_spine_blindness.py lists post_export_requests.py among the reviewed US runtime modules (test_us_runtime_module_classification_is_complete refuses an unlisted one), and test_transport_evidence_kernels.py lists us_runtime.post_export_requests among the modules the transport registry's binding hashes reach after its base (reform_coverage_smoke imports it), after CI's engine-free job failed on that test's module count. Not changed: a comment in test_main_writes_diagnostics_before_post_calibration_gate_failure (test_us_fiscal_refresh_builder.py) still says the plan's dry run builds reform objects; its stubs for the reform builders are now unused by the dry run but harmless, and I left that large parametrised test alone.

Run locally, each by node ID, in an engine-free environment and in the US engine environment (policyengine-us 2.2.1), on this branch after merging main at 8c51b6e, which includes the shared worker pool (#1726): the 40 cases of the 39 added or changed node IDs pass, including the two module-classification tests in test_us_spine_blindness.py and test_the_batterys_binding_identity_adds_only_the_leaf_and_its_imports. After that merge, test_reform_requests_follow_the_order_of_first_asks failed in CI and locally, because its consumer opened three reformed simulations before asking any; its consumer now asks each completely before opening the next. That test fix (2240525) and the refusal above (failing test c18f496, change 83080ce) came after the three review rounds and have not been reviewed adversarially. The earlier merges of main conflicted only where both sides added entries: test_us_post_export_scoring.py's imports and test_transport_evidence_kernels.py's list of modules the bindings reach; each keeps both sides'. Other tests that scan every source file for particular names were not run locally. On policyengine-us 2.2.1, three guard-sweep worker chunks (--worker --worker-start 0, 43 and 52, four reform passes each, batch size 3) on the eight-household fixture H5 from the engine-workflow tests rebuilt their reforms from descriptors and scored them in 3 batches: smoke passes 1–4, validation passes 44–47 and validation passes 53–56, which include the first two cumulative OBBBA states (ParameterChangesReform). The four selected passes (1, 2, 44 and 45) matched whole-file engines at rtol=1e-12, atol=1e-6. Each worker peaked at 2.3–3.3 GB resident memory. The other 97 passes of the full sweep were not run.

Overlap

Tracker task 1.4 · 3 (merging the five baseline sweeps into one) changes the same area of the release tool. This PR keeps its edits to the dry-run functions.

🤖 Generated with Claude Code

anth-volk and others added 12 commits November 24, 2026 21:37
Failing tests for the engine-free dry run returning every post-export
request as a PostExportRequest: one per request of each shipped
consumer, in the serial scorer's order, with no engine importable and no
reform built; a pickle round trip through a spawned process; the
refusal of a reform the dry run cannot describe; and the guard sweep
recording descriptors. The new module these tests import does not exist
yet, so every test in the three changed files fails at import here.

The test of the release's recorded outputs pins digests taken by running
it on origin/main (fcabe4e), before the descriptor imports were added
to its file.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The release tool's engine-free dry run now returns each request a
post-export consumer makes as a PostExportRequest, defined in the new
microcosm.build.us_runtime.post_export_requests module so that a
spawned process can unpickle it (the tool itself is loaded by file
path, outside sys.modules). A descriptor holds the consumer, its place
in the order the serial scorer runs the consumer's requests (the
baseline plan, then each reform pass in the order of first asks), the
variable, period and map_to, and for a reform request the description
its reform is rebuilt from and which reformed simulation asked for it.

Reform validation, like the smoke check, now hands a simulate that
carries RECORDS_REQUESTS_ONLY the spec or a ParameterChangesReform
instead of a built reform, because Reform.from_dict returns a class
pickle cannot name. The dry run therefore builds no reform, and it
refuses a reform it cannot describe. RECORDS_REQUESTS_ONLY moves to the
new module. The plan and pass count the release records come from the
same dry run, and nothing records the descriptors, so every recorded
output keeps its bytes. The guard sweep records through the descriptors
and rebuilds each reform in the worker that scores it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Failing tests for the gaps the first review found: the dry run accepts
a reform description pickle cannot name; most descriptors look hashable
but raise when hashed; a cumulative OBBBA state shares the consumer's
dictionaries; the PostExportReform alias cannot be evaluated; and bad
integer fields raise inconsistent error types. The recorded-outputs
test also pins each stage's planned reform-pass count, as on
origin/main.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- check_post_export_reform also pickles each reform description, so a
  description pickle cannot name fails the release's pre-export plan,
  not the hand-over to a worker.
- PostExportRequest and ParameterChangesReform are unhashable on
  purpose (most descriptions hold dictionaries); results are keyed by
  consumer and position.
- ParameterChangesReform keeps a deep copy of its payload as a dict,
  so no later change to the consumer's dictionaries, which its reform
  specs share, reaches a recorded request.
- reform_validation imports post_export_requests inside
  reform_validation_payload, so post_export_requests can import
  ReformValidationSpec and the PostExportReform alias evaluates.
- Integer fields raise TypeError for a non-integer and ValueError
  below their minimum.
- The descriptor function's docstring says that rebuilding an output
  means running the consumer again over the results: the smoke check
  also records each list-edit probe's resolved lists from the reform
  it builds itself.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Failing tests for the gaps the second review found: the dry run accepts
a subclass of a description type from a module only this process has,
which pickles here but which a spawned worker could not unpickle; a
pickling failure raised as ValueError escapes the refusal; parameter
changes that cannot be copied raise an unexplained error; and
wrong-typed string fields raise ValueError instead of TypeError.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- check_post_export_reform accepts exactly the three description
  types, not subclasses, whose module a spawned worker might not have,
  and refuses every exception a trial pickle raises.
- ParameterChangesReform reports changes it cannot copy as a TypeError
  that says so.
- PostExportRequest raises TypeError for a wrong-typed string field and
  ValueError for an empty one, as it does for its integer fields.
- The module docstring gives the right reason the types live in a
  package: a spawned process unpickles them however the release tool
  was started, while a type in the tool would unpickle only when the
  tool runs as a script, and then only by running the whole tool again
  as __mp_main__.
- The docs, the changelog and the sweep's docstring say that
  _record_post_export_request_descriptors returns the descriptors and
  the release itself still takes only the plans and pass counts, and
  the changelog says the plan no longer builds reform-validation reform
  classes before the export.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The third review found the refusal message ambiguous for a subclass or
a second copy of a description class, since it named the type without
its module and ended "not a built reform". It now names the module and
says the description must be one of the three types exactly. The module
docstring now says a type in the tool loaded by file path cannot even be
pickled, and the docs say _record_post_export_request_descriptors runs
its own call of the dry run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
test_us_runtime_module_classification_is_complete refuses a us_runtime
module that is neither a registered population operator nor a reviewed
other module. The new module reads no frame and treats no population.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
reform_coverage_smoke now imports RECORDS_REQUESTS_ONLY from
us_runtime.post_export_requests, so the hashes of the transport
registry's bindings cover one more module (247, not 246). The engine-free
CI job's test_the_batterys_binding_identity_adds_only_the_leaf_and_its_imports
failed on that count.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The only conflict was test_us_post_export_scoring.py's imports: this
branch imports ParameterChangesReform and PostExportRequest, and main
imports weakref, Iterator and DroppedConstraintMatrix. The merge keeps
all of them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The only conflict was test_transport_evidence_kernels.py's list of
modules the transport bindings reach after their base: this branch adds
us_runtime.post_export_requests and main adds
us_runtime.sipp_december_person_rows. The merge keeps both.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@anth-volk
anth-volk marked this pull request as ready for review November 25, 2026 19:12
anth-volk and others added 4 commits November 25, 2026 23:30
Brings in the shared worker pool (#1726), whose post-export scorer
finishes a reform when the consumer opens the next one and refuses
further keys from it. No conflicts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Since the shared worker pool (#1726), the post-export scorer finishes a
reformed simulation when the consumer opens the next one and refuses it
further keys. The test's consumer opened all three reformed simulations
before asking any, so the scored half of
test_reform_requests_follow_the_order_of_first_asks raised on main. Its
consumer now asks each reformed simulation for all its keys before
opening the next, and still covers a repeated key, a simulation asked
two keys, and an unasked simulation that keeps its number.

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

The post-export scorer finishes a reformed simulation when the consumer
opens the next one and refuses it any further key (#1726). A consumer
that goes back to an earlier reformed simulation passes today's dry run
and fails only in certification's scoring, after the export. The test
expects the dry run to refuse it, for a key the simulation already asked
and for a new one, and checks that the scorer refuses the same consumer.
It fails on this commit: the dry run does not raise.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The post-export scorer finishes a reformed simulation when the consumer
opens the next one and refuses it any further key (#1726). The dry run's
recorders now share its list of opened reforms, and a reformed
simulation's recorder raises once a later one is open, so a consumer
that goes back to an earlier reformed simulation fails the release's
plan before the export instead of certification's scoring after it. No
shipped consumer does; the recorded outputs' pinned digests are
unchanged. The docs and changelog say so.

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

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

Reviewed at 83080ce91: the post-export plan and outputs are unchanged on this head and when merged with current main, and I have nothing to raise.

@anth-volk
anth-volk merged commit 5df16cf into main Nov 26, 2026
12 checks passed
@anth-volk
anth-volk deleted the claude/post-export-request-descriptors-d699c1 branch November 26, 2026 13:07
anth-volk added a commit that referenced this pull request Nov 26, 2026
#1722's engine-free dry run records every request through
_RecordingPostExportSimulation and counted one reform pass per request.
This branch's prefetch scores several keys of one reformed simulation in
one pass, so the merged recorder takes prefetch too:
- it records the keys not asked before in ascending period order, as
  _BatchedPostExportReform.prefetch computes them, and counts the call as
  one pass (_dry_run_post_export returns that count);
- it refuses a prefetch of a reformed simulation after the next one opened,
  as it refuses a calculate.

Re-pins for the shared pre-OBBBA state, which reform validation now reads
for both OBBBA groups from one simulation in one pass: the planned
reform-validation count on #1722's fixture goes from 64 to 63 for the same
64 requests, and the descriptor test allows that one shared pass and
simulation. The recorded output digests are unchanged. A new test covers
the dry run's prefetch order, count and refusal.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
juaristi22 added a commit that referenced this pull request Nov 26, 2026
…C housing costs claim and the SPI copies' Child Benefit reports (#931)

Main took three pull requests since 1c82d18: #1722 (each US post-export
request as a picklable descriptor), #1744 (BE's releases publish into build
folders as BE builds) and #1718 (the SPI copies' Child Benefit reports,
#1508, and B1 and B3 of #1710: the UC housing costs element claim from DWP's
award statement, the national UC family weighted as three loss families, and
HMRC's one-child Child Benefit families held out of the solve). The UK lock
moves to policyengine-uk 2.180.0.

Twenty files conflicted.
- uk_runtime/child_benefit_take_up.py: main keys a family at its eldest
  eligible child under 16, else its eldest, and solves ages 15 down to 0,
  then 19 down to 16; this branch counts each age's child rows among
  geographic clone 0's. The merge keeps both sides' helpers and docstrings.
- uk_runtime/graph_kernels.py: main's new uc_housing_costs_claim stage,
  bound in this branch's form: the engine stage modules, frs_release,
  frs_spine, receipt_counts and, because frs_spine draws through them, the
  keyed-draw helpers (the derived binding rule). test_uk_graph lists the
  stage among those that read only frs_spine's constants, so it need not
  bind the copy-id table.
- uk_runtime/rowwise_posture.py: main's loss_family_equal rule for the
  national role; this branch's removal of expected_constituency_vintage
  stays.
- uk_runtime/stage_health.py and the Child Benefit gate's note: this
  branch's sampling-noise band and clone-0 row count, and main's SPI copy
  reports.
- uk/spec/sources.yaml: frs_spine's notes keep both new carriers (main's
  frs_uc_housing_element_status, this branch's imd_decile and
  council_tax_band_observed); main's one edit to frs_take_up, its engine
  version, goes to the block this branch moved after the area stages; and
  uc_capital_coherence's notes keep both edits.
- docs/uk-full-build-graph.md, tools/graph_uk_spine_fixture.py and the
  stage-count tests: 65 stages, this branch's 64 and main's
  uc_housing_costs_claim.
- test_uk_measure_simulation: 147 exclusions, the 144 before, this branch's
  two land rows and main's one-child row.
- test_uk_rowwise_candidate and test_uk_rowwise_posture: main's rule,
  without the vintage field.
- test_uk_stage_health: this branch's module-level Child Benefit helpers
  take main's copy-report receipt, parameter and checks. Beyond the
  conflicts, this branch's clone-count gate test in
  test_uk_child_benefit_take_up gives its invented receipt the SPI copy
  reports main's gate now requires: a frame without copies, one cell on
  its source families at every clone count, and no share to hold.
- The gate contract pins, the stage table, the coverage manifest and the H2
  fixture are recomputed on the merged tree, the fixture on policyengine-uk
  2.180.0.

The new stage sits before uc_take_up, so every later spine node's key
moves, and the next K = 15 spine must be built at this merge or later.

Checks: the UK graph tests with their binding rules (65); the
engine-free tests the merge touches, main's changed tests among them, with
the parity pins, the graph evidence, the kernel frame copies, the
transport binding, battery and cross-grain tests (3,670 pass); the US
coverage report and pins (--check); CI's test plan verify; and on the
engine, H2 parity, the UK graph, the new stage, Child Benefit take-up and
the release input coverage (69 pass).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

2 participants