Skip to content

Plan, build and look up prebuilt year files and baselines through country and dataset-level handlers - #1132

Merged
anth-volk merged 12 commits into
mainfrom
prebuilt-handlers-us
Nov 26, 2026
Merged

anth-volk merged 12 commits into
mainfrom
prebuilt-handlers-us

Conversation

@anth-volk

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

Copy link
Copy Markdown
Contributor

Fixes #1127

What changes

The run_simulation app's prebuilt year files and current-law baselines are planned, built, listed and looked up through handlers that take a country and a dataset level, as the approved design for tracker task 1.6 · 1 sets out. The US builds the same files as before and its results don't change, but the files get new keys and a new place in the image.

  • Level handlers (stage12_cache/levels.py, levels_us.py, levels_uk.py). Six dataset levels: US national, group of states, state and district; UK national, nation and local area. Each handler is key-free. It says which year files and baselines a year builds and writes the year files. For a run, it gives the year file and prebuilt folder the run reads, and whether the run's current-law baseline gets a fixed ID. The US settings build today's files: 3 national year files, 153 state year files and 60 group-of-states baselines, for 2026, 2027 and 2025. The UK settings build nothing yet and keep the UK-wide fixed ID. The handlers sit in stage12_cache/ so the v2 report app can use them next (task 1.6 · 3). They import none of the run_simulation modules that the Stage 12 import test forbids.
  • Identities (artifact_keys.py). One yf1 year-file key and one bl3 baseline key for every level, plus sf1 for source files copied whole (no handler returns one yet). Every key includes policyengine-core's version, and the default dataset's hash now comes from the release manifest, like a state file's. Store paths are year-files/… and baselines/…. Simulation IDs change from bl1-… to bl3-….
  • Plan and builds (precompute_planning.py, precompute_datasets.py, precompute_baselines.py, precompute.py, src/modal/precompute_app.py). plan_artifacts_impl(bucket, country) asks each level for its files. One build function serves every level's year files. The baseline builder downloads its year file into the prebuilt folder and loads it through that level's lookup, so each stored baseline is computed on the file the image holds. run_precompute starts each baseline once the year file it reads has been built, and runs one determinism check (a fresh recompute of one stored baseline, compared column by column) per level that computed baselines. The precompute takes --country, default us, so modal-precompute.sh and the workflow run unchanged.
  • Manifest and image (precompute_models.py, artifact_store.py, src/modal/_image_setup.py, src/modal/app.py). One mf2 manifest per country. Each entry records the stored file's SHA-256 and size, which uploads now record as object metadata. The download step checks both on every file before moving it into place. It places files only under prebuilt/<country>/, and refuses a file already in the image, a manifest of another schema, and a manifest built for another policyengine-core version. app.py resolves each country's manifest from its own variable; only the US has one, POLICYENGINE_MANIFEST_DIGEST, as before.
  • Runtime (baseline_artifacts.py, simulation_runtime.py). Every run with a level reads its year file through the handlers, a national run included: it now reads prebuilt/us/<default dataset>/all/. A run falls back to today's folders when its file is missing or fails .py's reuse checks. qualifying_baseline_identity asks the level handler for the baseline scope. A lookup failure never fails a request.
  • Removed. state_datasets.py, the ds1/sds1/bl1 identities, the per-state plan and manifest models, and the hard-coded PRECOMPUTE_COUNTRY and PRECOMPUTE_YEARS.
  • One list of the 20 groups. The groups move to a new stage12_cache/partition.py, which Stage 12's partition, the US handlers and national_partition all import. The design named stage12_runtime.partition as their home, but that package's __init__ imports the v2 database code, which the run_simulation images don't install, so importing it there would stop those images from starting and planning. tests/test_prebuilt_imports.py imports the handlers with those packages blocked. The groups are the same, group for group and in order.

Where the code differs from the design

  • The groups' module, above.
  • UK groups of nations keep their fixed baseline ID. On main, a UK current-law run over a group of nations (for example England plus Wales) on the default dataset gets a fixed ID. The design's UK levels leave that case out, so the UK national handler also covers it, reading the UK national year file, and the set of runs with fixed IDs is the same as on main. Results don't change either way, since nothing UK is prebuilt.
  • The policyengine-core version check reads two sources. The manifest records the installed policyengine-core, as the design says for every key, and the image compares it with POLICYENGINE_CORE_VERSION, which comes from the version .py's bundle declares. Both are 3.33.3. If a relock ever made them differ, the image build would stop at the download step with a stale-manifest error.

What stays the same, and how it's shown

The first commit pins what main (544ff628) did, for fixed inputs: the plan's files, which requests get a fixed baseline ID, the hit, incomplete and miss outcomes, the state-folder lookup rule, write-once publishing, the MANIFEST_DIGEST= output line and the deployed marker. Those assertions are unchanged. The design changes the keys, paths, IDs, image folders and manifest bytes, so their pinned values change in their own commit, with the design's reason. The new yf1, bl3 and mf2 golden digests were computed from the design's payloads with hashlib, independently of the code, and the code produces them.

Rollout

  • The new keys rotate every stored US key once. Merge this in the same release as a .py version update, which rotates them anyway. Otherwise the release's precompute is a full cold run: 3 national builds, 3 state builds of up to 2 hours each, 60 baseline containers and one determinism check.
  • src/modal/app.py is also edited by draft Report in worker telemetry whether each call's container was restored from a memory snapshot #1124, in other functions. Whichever merges second resolves any text conflict.
  • Tracker task 1.6 · 4 checks the provider and wiring of every environment variable against this diff. This PR adds no variable and changes no workflow or script. The download step now also reads POLICYENGINE_CORE_VERSION, which the deploy job passes from the bundle's versions into VERSION_ENV in app.py, so it is set in the image. Unverified without GitHub or Modal settings access, and unchanged here: the POLICYENGINE_ARTIFACT_BUCKET variable and GCP_CREDENTIALS_JSON secret in each GitHub environment, and the key names in the Modal secret gcp-credentials.

Checks run

  • Every added or changed test, about 310, passes, each run by node ID. The full suite was not run locally; CI runs it.
  • Three rounds of adversarial review (a self-review). Round 1 found four bugs, each fixed with a failing test first: the database-code import above, a ported SPM key test, the UK groups' fixed ID, and a prebuilt-file lookup that could fail a request instead of falling back. Round 2 found none, only stale fixture docstrings, which are fixed. Round 3, after main was merged in, found no bug either, and four small items, all fixed: a source file's manifest entry now records its sf1 key; the precompute stops before any build when a plan needs a source file it can't copy yet; the executor README describes the prebuilt/ layout; and the database-free import check also covers the runtime and precompute modules.
  • main (8ef36e5d) is merged in. The one conflict, the precompute app's module docstring, keeps main's statement of the baseline containers' size.
  • Ruff format and lint pass on src; Pyright passes for the executor package.
  • No Modal, GCS or precompute run. Comparing a US national and a US state report with the previous release's is tracker task 1.6 · 5, and needs approval.

🤖 Generated with Claude Code

anth-volk and others added 12 commits November 25, 2026 21:19
Record what main (544ff62) produces for fixed inputs, so moving the US
precompute, manifest and runtime lookup onto handlers that take a country
and a dataset level can show the US results are unchanged:

- the plan from plan_artifacts_impl: three default-dataset year files,
  one baseline per year and each of the 20 region groups, and one
  state-level year file per year and state, with every key, store path,
  simulation ID and folder, with and without an SPM capability;
- the entries select_work picks with and without force;
- the key schema labels and the store paths, manifest_path and
  deployed_marker_path;
- the manifest's entry order, its exact published bytes and its digest
  for the full US plan (with and without SPM) and for a small plan;
- which requests get a fixed baseline ID, the hit, incomplete and miss
  outcomes ArtifactBaselineSimulation records, and the folder
  baked_state_data_folder returns.

The inputs are stand-ins with no GCS, Hugging Face or Modal access:
identity_stubs' version-set, the 51 US state regions with their real
codes and FIPS numbers, and a fake GCS client. The national partition is
the real one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Tests for the approved design of prebuilt files by country and dataset
level, which fail until the handlers exist:

- the handlers: six dataset levels, the US and UK settings, how a run
  gets its level, each level's year-file and baseline targets, the year
  file a run reads, its fixed-ID scope and its prebuilt/ folder, and that
  the handler modules import no run_simulation feature module;
- one yf1 year-file identity and one bl3 baseline identity for every
  level, golden digests computed from the design's payloads;
- plan_artifacts_impl(bucket, country): the same US files as before,
  each with its new identity, and an empty UK plan;
- the mf2 manifest, with each file's recorded SHA-256 and size, and
  run_precompute for one country;
- the download step placing files under prebuilt/, refusing a hash or
  size mismatch, a file already in the image, another schema and another
  policyengine-core version;
- a national run reading its prebuilt folder.

The identity stubs gain the default dataset's certified hash and fixed
installed-package versions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The run_simulation app's prebuilt year files and current-law baselines go
through handlers that take a country and a dataset level, as the approved
1.6 · 1 design sets out:

- stage12_cache/levels.py, levels_us.py and levels_uk.py: six dataset
  levels, per-country settings, and key-free handlers that say which
  files a year builds, write the year files, and tell a run which year
  file and prebuilt folder it reads and whether its baseline gets a fixed
  ID. The US settings build today's files: 3 national year files, 153
  state year files and 60 group baselines. UK settings build nothing yet
  and keep the UK-wide fixed ID.
- artifact_keys: one yf1 year-file identity and one bl3 baseline
  identity for every level, with policyengine-core's version in the key,
  and store paths under year-files/ and baselines/.
- plan_artifacts_impl(bucket, country) plans through the handlers; one
  build function serves every level's year files; the baseline builder
  loads its year file through the level's prebuilt lookup; the precompute
  app takes --country, default us.
- One mf2 manifest per country, recording each stored file's SHA-256 and
  size; the image's download step checks both, places files only under
  prebuilt/<country>/, refuses a file already in the image, another
  schema and another policyengine-core version.
- The runtime reads every run's year file through the handlers, a
  national run's included; qualifying_baseline_identity asks the level
  handler for the baseline scope.
- state_datasets.py is deleted; national_region_groups returns Stage
  12's groups.

The old tests of the replaced code are ported to the handlers. The US
pins' digests, paths and IDs change in the next commit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The approved 1.6 · 1 design (section 2, "the cleaner version") replaces
the ds1, sds1 and bl1 keys with one yf1 year-file identity and one bl3
baseline identity, adds policyengine-core's version to every key, moves
the image's prebuilt files under prebuilt/<country>/<dataset>/<subset>/,
and (section 4) publishes one mf2 manifest per country that records each
file's SHA-256 and size. Section 8 lists which US pins that changes:
every digest, store path, simulation ID and image folder, the key and
manifest schema labels, and the manifest's entry order, bytes and
digest. Those values are re-recorded from the new code for the same
fixed inputs.

What section 8 says must not change stays pinned as recorded on main
(544ff62): the plan's targets (3 national year files, 60 group
baselines in the 20 groups' order, 153 state year files sorted by
code), select_work with and without force, which requests get a fixed
baseline ID and which a random one, the refusal of an SPM selection
without a capability, the hit, incomplete and miss outcomes, write-once
republishing, the MANIFEST_DIGEST= output line, and the deployed
marker's path and overwrite. The tests now build the fixed-ID requests
with a dataset selection, which replaced dataset_is_default.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The run_simulation app's images install neither persistence package, but
levels_us and national_partition import stage12_runtime.partition, whose
package __init__ imports policyengine_stage12_persistence. Each check runs
in a fresh interpreter with both packages blocked.

Found by the 1.6 · 4 environment-variable check.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
stage12_runtime/__init__.py imports the v2 database packages, which the
run_simulation app's images don't install, so importing
stage12_runtime.partition from levels_us and national_partition would
stop those images from planning a precompute, looking up a run's prebuilt
file, and importing the segmented national runner.

The groups move to stage12_cache/partition.py, which imports nothing;
stage12_runtime.partition, levels_us and national_partition all import
them from there, so there is still one list. The user chose this over
adding the database packages to the images (2026-11-26).

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

From review round 1:

- test_each_setting_rotates_artifact_identity still called the deleted
  ds1 and bl1 key functions. It now checks that each SPM setting
  rotates the yf1 year-file key and the bl3 baseline key and storage
  ID; it passes.
- On main, a UK current-law macro run over a group of nations on the
  default dataset gets a fixed baseline ID; the handlers gave it none.
  The new tests expect main's behaviour (failing).
- prebuilt_dataset_folder must return no folder, not raise, when a
  country's settings fail to load (failing).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
From review round 1:

- A UK current-law macro run over a group of nations on the default
  dataset is a national-level run again, whose scope is the group's
  cache key, so it keeps the fixed baseline ID it has on main.
- prebuilt_dataset_folder loads a country's settings inside its error
  handling, so a failure there falls back to today's folders instead of
  failing the request.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
From review round 2: the identity stubs, the wrapper storage-ID fixture
and a baseline-builder test still named the removed identity classes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
One conflict, in the precompute app's module docstring: main states the
baseline containers' own size (8 CPUs and 32 GiB), since the segment
workers now request less, and this branch rewrote the docstring for the
level handlers. The docstring keeps the handler wording with main's size.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
From review round 3: a source file's manifest entry must carry its sf1
identity digest, as the other kinds carry theirs, and run_precompute,
which has nothing to copy source files with yet, must stop before
starting any build when a plan needs one, instead of failing to publish
after every other build.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
From review round 3:

- A source file's plan entry carries its sf1 identity digest, and its
  manifest entry records that digest, as the other kinds do.
- run_precompute stops before starting any build when the plan needs a
  source file, since nothing copies one into the store yet (2.2 · 4 adds
  it), instead of failing to publish after every other build.
- The executor README describes the prebuilt/ image folders, the per-
  country download step and its checks, and prebuilt_dataset_folder,
  in place of the state-datasets/ folders and the deleted
  baked_state_data_folder.
- The import check without the v2 database packages also covers
  simulation_runtime, baseline_artifacts and precompute.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@anth-volk
anth-volk marked this pull request as ready for review November 26, 2026 17:58
@anth-volk
anth-volk merged commit d05ad3c into main Nov 26, 2026
30 checks passed
@anth-volk
anth-volk deleted the prebuilt-handlers-us branch November 26, 2026 17:58
anth-volk added a commit that referenced this pull request Nov 26, 2026
Since the precompute moved onto the level handlers (#1132), the image keeps
year files and baselines under prebuilt/<country>/<dataset>/<subset>/, and a
run finds its year file through simulation_runtime._prebuilt_folder, which
returns no folder for a year file the image lacks or .py wouldn't reuse. The
tests now expect the warm-up to:

- calculate group 1's precomputed current-law request from the US
  group-of-states handler, for the first prebuilt year, with a reform added;
- look up its baked inputs as a run of that request does: the region and
  dataset resolution, the prebuilt-folder lookup and the precompute's
  baseline identity, with the baseline beside the year file;
- clean up in the prebuilt folder, where outputs are now saved;
- read none of the workers' Secrets, under any name the code reads them by.

A new test fails the old module outright: with a module the warm-up uses
made unimportable, the app must still import and the hook only log a
warning. With main merged in, the old module's import-time imports made the
whole app fail to import.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
anth-volk added a commit that referenced this pull request Nov 26, 2026
#1132 moved the precompute onto the level handlers: it deleted
state_datasets.py, replaced the precompute planning names the warm-up
imported, and moved baked files under prebuilt/<country>/. With that in main,
the warm-up module failed to import, and app.py, which imports it, failed
with it.

- The group and year come from the US group-of-states handler: group 1's
  current-law request for the first prebuilt year, with the reform added.
  Group 1 is still state/hi+state/ia+state/wi, the group both measurements
  used.
- The baked inputs are found as a run of that request finds them: region and
  dataset resolution, then simulation_runtime._prebuilt_folder, which returns
  no folder for a year file the image lacks or .py wouldn't reuse; the
  baseline is the precompute's own identity for the group, beside the year
  file. Either missing skips the warm-up with a warning.
- The cleanup looks in the prebuilt folder, where outputs are now saved.
- Every executor module the warm-up uses is imported when it runs, so a
  later move or rename in them only costs the warm-up, with a warning,
  instead of stopping the app from importing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
anth-volk added a commit that referenced this pull request Nov 26, 2026
…anch

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
anth-volk added a commit that referenced this pull request Nov 27, 2026
1.6 · 2's branch, which this branch was built on, merged into main as
#1132 at its head e92f6c6, so this
branch now targets main.

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.

Plan, build and look up prebuilt year files and baselines through country and dataset-level handlers

1 participant