Skip to content

Move the v2 report app's cache onto the prebuilt-file level handlers - #1136

Merged
anth-volk merged 8 commits into
mainfrom
v2-cache-onto-handlers
Nov 27, 2026
Merged

anth-volk merged 8 commits into
mainfrom
v2-cache-onto-handlers

Conversation

@anth-volk

Copy link
Copy Markdown
Contributor

Fixes #1135

Summary

Moves the v2 report app's cache (stage12_cache/) onto the prebuilt-file level handlers and deletes its copies of that code. The handlers were added by #1132, which merged into main; this branch was built on that PR's head and has main merged in. Its keys, store paths, the set of files it builds, which runs get a fixed cache ID, and its US results are unchanged. What changes is where the files sit in the worker image: every file now sits in its handler's prebuilt/<country>/<dataset>/<state or "all">/ folder.

What changes

Paths are under projects/policyengine-simulation-executor/src/.

  • The cache's own levels: stage12_cache/cache_levels.py (new) lists the year-file levels the cache bakes (US national and state) and the levels whose baselines it precomputes (US state groups). These settings are separate from the run_simulation app's year_file_levels and baseline_levels, so a change there adds no files or fixed IDs to the v2 cache.
  • Planning (stage12_cache/precompute_planning.py):
    • The years come from the handler settings, and the year files and baselines from the handlers' targets.
    • Each target is keyed by the existing stage12-ds1, stage12-sds1 and stage12-bl1 identities.
    • Planning refuses a target the cache has no key for, and a setting with no baseline level, before anything is built.
    • Each planned baseline key goes through qualifying_cache_identity, the function the worker uses.
    • expected_cache_contents gives the years and the year-file and baseline counts without loading the US model.
  • Builders:
    • The national and state year files are written by the handlers' write_year_files. The cache's copies of the state targets, the district checks and the state filtering are deleted.
    • The baseline build and the determinism check download the year file into the baseline's prebuilt folder. The worker's lookup then loads it there, and .py saves the baseline beside it.
  • Fixed cache IDs (stage12_runtime/cached_simulation.py):
    • qualifying_cache_identity now takes the run's level and scope key from the handlers.
    • A fixed ID still needs three things: a current-law baseline, a region group that lists only state/… codes, and a level the cache precomputes. Every other run keeps a random ID.
    • A failed lookup, including a failure to load the handler settings, gives a random ID instead of failing the request.
  • Runtime lookup (stage12_runtime/simulation.py): the worker gets each run's folder from levels.prebuilt_dataset_folder. National and group runs now read the national prebuilt folder when the image holds it.
  • Manifest and image:
    • Every manifest entry records its folder; the schema label stays stage12-mf1.
    • The image download step (src/modal/stage12_cache_image.py) places each file in that folder.
    • It refuses a folder outside prebuilt/<country>/.
    • It checks the years and counts that src/modal/v2_app.py computes on the deploying machine, instead of fixed constants.
    • The deploy-time worker check (stage12_worker_validation.py) reads the same values, and reads its probe baseline from its folder.
  • Deleted:
    • PRECOMPUTE_YEARS, StateDatasetTarget and state_dataset_targets.
    • The cache's baked_state_data_folder lookup; year_file_reuse_problem stays, because the handlers use it.
    • The scope half of qualifying_cache_identity.

What stays the same, and the evidence

New pin tests (tests/test_v2_cache_us_*_pins.py, inputs in fixtures/v2_cache_us_inputs.py) record what main produced for fixed inputs. The commits come in this order: the pins; failing tests of the new behaviour; the pins that change, updated alone; then the change. After the change, these pins pass unchanged:

  • the plan's years, the 3 + 153 + 60 entries in order, every key, store path, simulation ID and output-plan hash;
  • the manifest digest computed without the folder fields;
  • which runs get a fixed cache ID: every planned state group gets its planned ID. These get random IDs: US national without a group, single states, districts, groups that include a district, reform sides, runs with a policy and UK runs.

The pins that changed are the image-folder ones, the manifest's full digest, and the download step's pins, which now pass in the expected counts. Two pins of deleted names now read the same values through the handlers.

Deployment effects

  • The manifest digest changes once, through the folder fields alone.
  • The next deploy's precompute rebuilds no file, because the keys are unchanged; it publishes the new manifest.
  • The image downloads all 216 files once, as after any .py update.
  • New code refuses a manifest published before this change, and old code refuses new manifests. The deploy job always runs the precompute before modal deploy, so a normal deploy is unaffected.

Environment variables

The change adds and removes none, and changes no wiring. The download step gains one argument, the expected years and counts. The deploying machine computes it from the installed .py package's metadata and the handler settings, which read no environment variable.

Variable Where the changed code reads it Provider Wiring
POLICYENGINE_DATA_FOLDER download step, worker lookup, precompute baseline build, worker check STAGE12_DATA_DIR in src/modal/v2_app.py build_v2_image's .env({...}) step, before the download step
STAGE12_CACHE_MANIFEST_DIGEST v2_app._deploy_time_cache_inputs; download step output of the "Build the independent Stage 12 cache" step the deploy step's env: in simulation-deploy.reusable.yml, then the image .env({...}) step
STAGE12_CACHE_BUCKET v2_app._deploy_time_cache_inputs GitHub Actions variable vars.STAGE12_CACHE_BUCKET the precompute and deploy steps' env:
STAGE12_BUNDLE_MANIFEST_SHA256 download step the installed .py bundle's digest, on the deploying machine the image .env({...}) step
GOOGLE_APPLICATION_CREDENTIALS_JSON download step Modal secret stage12-evaluation-gcp-credentials attached with run_function(..., secrets=[gcp_secret]); the key names inside the secret are not visible in the code (unverified)

Known limitations

  • The cache accepts a setting whose baseline levels need a year-file level it doesn't bake, for example group baselines without the national year file. No current setting does this; such a setting would fail during the precompute.
  • National and group runs now find their year file only through the handlers' lookup, which reads private .py helpers. If a .py upgrade moved one, the lookup would find nothing, and those runs would compute their baselines during the request. The precompute's "produced no cache file" check and the deploy probe's cache-hit requirement would both fail first.
  • Not addressed here: the v2 US baseline segments' cache misses since 2026-11-16, which are being investigated separately. This change leaves every input of a v2 cache key unchanged.

Checks run

  • Tests: only the tests this PR adds or ports, plus the Stage 12 import rule test and the handler import test: 216 in all, each run by node ID, on the branch with main merged in. The full suite was not run locally.
  • Other checks: Pyright for the executor package, Ruff 0.15.22 (the CI version) format and lint on the changed files, scripts/run_quality_guards.py, and the changelog fragment check against main.
  • Review: two rounds of adversarial review by separate reviewers. Round 1's four low-severity findings were fixed, each with a failing test first. Of round 2's three low-severity findings, the two test findings were fixed, and the other is the second known limitation above.

🤖 Generated with Claude Code

anth-volk and others added 8 commits November 26, 2026 23:56
Record what main (6865dc2) produces for fixed inputs, so moving the v2
report app's cache (stage12_cache) onto the level handlers can show that
its US behaviour is unchanged:

- the plan from plan_artifacts_impl: the years 2026, 2027 and 2025 in
  that order, 3 default-dataset year files, 60 baselines (each year by
  the 20 region groups, in order) and 153 state files (each year by the
  51 states sorted by code), with every stage12-ds1, stage12-sds1 and
  stage12-bl1 digest, store path, simulation ID and output plan hash;
  the bundle identity, partition hash, determinism record path and the
  presence flags and select_work result;
- the key payloads of the three identities, which include
  policyengine-core's version and the output plan's SHA-256;
- the manifest: entry order, hashes and sizes, top-level fields, the
  exact payload of a one-entry-per-kind plan, write-once publishing of
  its canonical bytes under their digest, and the digest line the
  precompute prints;
- which runs build_stage12_simulation gives a fixed cache identity
  (every planned group baseline, an unplanned state group, a planned
  group in a year without files) and which a random one (reform side
  or role, a policy, US national without a group, one state, a
  district, a group with a district, UK-wide);
- the download step's checks: exactly 3 year files and 60 baselines,
  the years in submission order, each file's size; and the deployed
  worker's check of the downloaded cache and the release it reports.

The image folders (where the download step places files, where the
worker loads a state's or district's dataset from, where the deploy
check reads its probe baseline, and the manifest digest, which covers
each state file's folder) are pinned in classes of their own: the
handler design moves those folders to prebuilt/, so those pins change
in a commit of their own. The manifest is also pinned without its
folder fields, which must not change.

The inputs are stand-ins with no GCS, Hugging Face or Modal access: a
fixed Stage 12 bundle and release bundle, the 51 US state regions with
their real codes and FIPS numbers, two districts, a fixed SPM capability
and an in-memory cache bucket. The national partition is the real one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The v2 report app's cache should plan, place, check and look up its files
through the level handlers in stage12_cache:

- the plan takes its years from the US handler settings, its year files
  and baselines from the handlers' targets, and names each entry's
  country and prebuilt/ image folder (baselines sit beside their year
  file); which year-file and baseline levels it covers are the cache's
  own settings (stage12_cache.cache_levels), never the run_simulation
  app's, and a year file it has no key for is refused;
- expected_cache_contents gives the years and the year-file and baseline
  counts, without loading the US model, for the download step and the
  deploy-time check;
- every manifest entry records its folder (schema label unchanged); the
  download step places each file there, refuses a folder outside
  prebuilt/<country>/, and checks the years and counts it is passed;
- the worker takes each run's folder from levels.prebuilt_dataset_folder,
  and qualifying_cache_identity takes the level and scope key from the
  handlers, giving a random ID when the scope lookup fails, the level is
  not one the cache precomputes, or the group lists anything but states;
- the year-file builds go through the handlers' write_year_files, the
  baseline build downloads its year file into the prebuilt folder, and
  the copies the handlers replace are gone.

33 of the 34 tests fail on the current code; the one that passes guards
that the cache ignores the run_simulation app's levels.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The approved handler design (1.6 · 1, section 6.2) moves every prebuilt
file under prebuilt/<country>/<dataset>/<state or "all">/, so the v2 cache's
files move there too: state files from state-datasets/<dataset>/<state>,
and year files and baselines from the image data folder itself to the
national folder, where a baseline sits beside the year file it was
computed on. The user chose on 2026-11-26 that every manifest entry records
its folder, so the download step, which runs before the package code is in
the image and cannot call the handlers, places each file where its entry
says. These pins change with it, and only these:

- the plan entries' folders, and where the download step places each file
  and the deploy-time check reads its probe baseline;
- the folder a state, district, national or group run loads its dataset
  from (national and group runs now read the national prebuilt folder
  when the image holds it);
- the manifest's full digest and the exact payload of a small plan; the
  digest without the folder fields is unchanged;
- the download pins pass the expected years and counts, which the step
  now receives from the deploying machine instead of holding as
  constants; what it accepts and refuses is unchanged;
- two pins read the years and state targets through the handlers, since
  main's PRECOMPUTE_YEARS and state_dataset_targets are deleted; their
  values are unchanged.

The fixture now patches only the module attributes that exist, so it
works before and after the change. The plan, key, digest-without-folders
and fixed-ID pins pass unchanged; the 19 changed pins fail until the
change lands.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The v2 report app's cache (stage12_cache) now plans, builds and looks up its
files through the level handlers in stage12_cache/levels*.py, and its
copies of that code are gone (design 1.6 · 1, section 6.2):

- stage12_cache/cache_levels.py holds the cache's own levels: US national
  and state year files, and the state-group baselines. They are separate
  from the run_simulation app's year_file_levels and baseline_levels, so a
  task that adds files or fixed IDs there does not add them here.
- The plan takes its years from the handler settings and its targets from
  the handlers, keyed by the existing stage12-ds1, stage12-sds1 and
  stage12-bl1 identities; a target the cache has no key for is refused.
  Every plan entry names its country and the handler's prebuilt folder.
  The baseline keys go through qualifying_cache_identity, so the precompute
  and the worker apply one rule.
- qualifying_cache_identity takes the run's level and scope key from the
  handlers. A fixed ID still needs a current-law baseline whose group lists
  only states, and a level the cache precomputes baselines for (the user's
  decision of 2026-11-26: fixed IDs for the 20 state groups as today; other
  runs keep random IDs until a task adds their level, noted on 2.2 · 6 and
  2.1 · 1). A failing scope lookup gives a random ID.
- The year-file builds go through the handlers' write_year_files; the
  baseline build and the determinism check download the year file into the
  baseline's prebuilt folder, where .py saves the baseline beside it.
- Deleted: state_dataset_targets, StateDatasetTarget and PRECOMPUTE_YEARS;
  _checked_state_targets, _registered_district_geoids,
  _require_districts_within_their_states and the filtering body of the
  state build; the state_datasets lookup (year_file_reuse_problem stays,
  for the handlers); the scope half of qualifying_cache_identity.
- Every manifest entry records its folder (the user's decision of
  2026-11-26; schema label unchanged). The image download step, which runs
  before the package code is in the image, places each file there, refuses
  a folder outside prebuilt/<country>/, and checks the years and counts the
  deploying machine computes with expected_cache_contents instead of fixed
  constants. The deploy-time worker check reads the same values and its
  probe baseline from its folder.

Keys, store paths, the set of files built and the fixed-ID outcomes are
unchanged: the plan, key, folder-free manifest digest and fixed-ID pins
pass unchanged, and the image-folder pins moved in the previous commit. No
environment variable is added or removed; the download step gains an
argument passed from the deploying machine.

Existing tests are ported to the new fields, folders and interfaces; tests
of code that moved into the handlers are deleted where 1.6 · 2's handler
tests cover it (tests/test_prebuilt_levels.py, test_prebuilt_state_files.py).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Adversarial review round 1 confirmed three defects; these tests fail until
they are fixed:

- a failure while loading the handler settings fails a request instead of
  giving its baseline a random ID, because the settings are loaded outside
  the guarded scope lookup;
- a cache setting with no baseline levels plans fine, then crashes the
  precompute after the year files are built, and would also crash the
  manifest publication and the deploy check: planning and
  expected_cache_contents must refuse it first;
- the national year-file build leaves an empty temporary folder behind on
  every call.

Two tests that relied on an empty baseline setting now show the same
dependence on the cache's own levels another way.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- qualifying_cache_identity loads the handler settings inside its guarded
  lookup, so a failure there gives the baseline a random ID instead of
  failing the request, as the prebuilt-folder lookup already did.
- plan_artifacts_impl and expected_cache_contents refuse a cache setting
  with no baseline level (required_baseline_levels) before anything is
  built: the precompute's determinism check, the manifest's publication
  and the deployed worker's check each probe a precomputed baseline.
- The national year-file build passes the handler a temporary work folder
  that is removed afterwards.
- A new test runs the real prebuilt-folder lookup during the precompute's
  baseline build, with only the dataset loader replaced: it loads the
  downloaded year file from the baseline's prebuilt folder. The existing
  test stubbed the calculation, so it showed only where the year file is
  downloaded and where the upload is read from.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- The expected-contents test no longer treats as valid a setting that
  precomputes group baselines without the national year file they run on;
  it now varies only the handler years.
- The states-only test now uses a group that includes a district: its level
  is one the cache precomputes and the handler gives it a scope key, so only
  the states-only check keeps it from a fixed ID. The old version used a
  national run, which the handler gives no key, so it passed whether or not
  the check existed. With the check replaced by one that accepts any group,
  the new version fails.

Round 2's third finding is left as stated in the pull request: national
and group runs find their year file only through the handlers' lookup,
which reads private .py helpers; the precompute and the deploy check fail
if that lookup stops finding it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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>
@anth-volk
anth-volk marked this pull request as ready for review November 27, 2026 18:16
@anth-volk
anth-volk merged commit a5fa6c4 into main Nov 27, 2026
28 checks passed
@anth-volk
anth-volk deleted the v2-cache-onto-handlers branch November 27, 2026 18:17
anth-volk added a commit that referenced this pull request Nov 27, 2026
Since #1136, qualifying_cache_identity takes the run's RunScope (country,
year, region and dataset selection) in place of the dataset-default flag
and the scoping strategy. The test now builds it as the segment worker
does. Against main's coordinator the identity test still fails with
stage12-bl1-6bf58faeedaedc36 against stage12-bl1-928929ee9ffcc2a4.

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.

Move the v2 report app's cache onto the prebuilt-file level handlers

1 participant