Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/prebuilt-level-handlers.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The run_simulation app's precomputed year files and current-law baselines are now planned, built and looked up through handlers that take a country and a dataset level (national, group of states, state, district, UK nation, UK local area). US results and the set of US files built are unchanged, but the files have new keys (`yf1` year files, `bl3` baselines, including the policyengine-core version), sit under `prebuilt/<country>/<dataset>/<subset>/` in the image, and are listed in one `mf2` manifest per country, whose recorded SHA-256 and size the image build checks for each file. The precompute takes `--country` (default `us`). The first release with this change recomputes every US file once, as a .py version update does.
66 changes: 38 additions & 28 deletions projects/policyengine-simulation-executor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,14 +276,17 @@ an operator repairs them by hand.

## Baseline artifact pipeline (precompute → store → image fetch)

The precompute app fills a content-addressed GCS store with single-year US
datasets (2026, 2027, 2025), the 20 per-cohort national baseline
simulations per year, and per-state datasets: for each state region whose
requests load a dataset other than the bundle default (the ACS-local
dataset since policyengine 6.7), that dataset's year file filtered to the
state's households, one file per state and year. The deploy bakes them into
the image, and the runtime loads baselines instead of computing them. Library logic lives in
`src/policyengine_simulation_executor/precompute.py` (keys:
The precompute app fills a content-addressed GCS store for one country
(`--country`, default `us`), planned by that country's level handlers
(`src/policyengine_simulation_executor/stage12_cache/levels*.py`). For the
US it builds the default dataset's year file for 2026, 2027 and 2025, the
current-law baseline of each of the 20 groups of states per year, and state
year files: for each state region whose requests load a dataset other than
the bundle default (the ACS-local dataset since policyengine 6.7), that
dataset's year file filtered to the state's households, one file per state
and year. The UK builds nothing yet. The deploy copies the files into the
image, and the runtime loads them instead of computing them. Library logic
lives in `src/policyengine_simulation_executor/precompute.py` (keys:
`artifact_keys.py`, store client: `artifact_store.py`); the Modal app
(`src/modal/precompute_app.py`) is plumbing only.

Expand All @@ -300,17 +303,20 @@ Manual runs remain valid for ad-hoc warming:
The deploy job consumes the precompute job's manifest digest:
`src/modal/app.py` resolves the manifest from GCS on the deploying
machine and passes its content as the `fetch_artifacts` image layer's
args (`src/modal/_image_setup.py`). The layer downloads the full
artifact set into `POLICYENGINE_DATA_FOLDER`: about 430 MB of national
datasets and baselines, plus several GB of per-state datasets, each placed
in `state-datasets/<dataset>/<state>/` under the year file's own name. It
sits between
the version env and the source mount — do not reorder. Because the
manifest is content-addressed and rides in the layer args, Modal's layer
cache busts exactly when the artifact set changes and never otherwise;
there is no force-rebuild ritual. A freshness gate inside the layer
refuses to bake artifacts computed for versions other than the image's
own, and any missing store object fails the build loudly.
args (`src/modal/_image_setup.py`); each country with a manifest gets one
such layer, US only today. The layer downloads the country's full file set
under `POLICYENGINE_DATA_FOLDER`, each file in
`prebuilt/<country>/<dataset>/<state code or all>/` under its own name: for
the US, about 430 MB of national year files and group-of-states baselines in
`prebuilt/us/<default dataset>/all/`, plus several GB of state year files in
`prebuilt/us/<state-level dataset>/<state>/`. It sits between the version
env and the source mount — do not reorder. Because the manifest is
content-addressed and rides in the layer args, Modal's layer cache rebuilds
the layer exactly when the file set changes and never otherwise; there is no
forced rebuild to run. The layer refuses a manifest of another schema or one
computed for package versions other than the image's own, checks each
downloaded file's SHA-256 and size against the manifest, refuses a file
already in the image, and fails the build on any missing store object.

After the health check, each deploy leg writes a
`deployed/<environment>.json` marker (`deployed/beta.json`,
Expand Down Expand Up @@ -343,20 +349,24 @@ Operational notes:
precompute — deletion turns the key back into an ordinary miss, and the
next deploy's precompute job recomputes it before the deploy leg builds
the image.
- A determinism gate runs whenever baselines were computed: one cohort's
uploaded artifact is compared frame-by-frame against an independent
fresh run. The run fails if they differ.
- A determinism check runs whenever baselines were computed: for each
level that computed any, one uploaded baseline is compared column by
column against an independent fresh run. The run fails if they differ.
- The final stdout line `MANIFEST_DIGEST=<digest>` names the published
deploy manifest; the deploy pipeline consumes exactly that line.
- Only pure default requests read the baked folder: any request naming an
explicit `data` dataset or pinning a `data_version` bypasses it (see
`_load_dataset` in `simulation_runtime.py`).
- A US state or congressional-district request whose region loads a
non-default dataset reads its state's baked file when the image has one for
the requested year and .py's year-file reuse checks accept it (see
`state_datasets.baked_state_data_folder`). Otherwise it prepares the full
regional dataset during the request, as before. The run records which in
its `dataset_source` attribute: `baked_state` or `on_demand`.
- Every run whose region has a dataset level (US national, group of states,
state or congressional district) reads its year file from its prebuilt
folder when the image has one for the requested year and .py's year-file
reuse checks accept it (see `prebuilt_dataset_folder` in
`stage12_cache/levels.py`). A district reads its state's file. Otherwise
the run uses the folder it used before the prebuilt files: the data
folder's root for the default dataset, or a temporary folder where it
prepares the full state-level dataset. A run on a non-default dataset
records which in its `dataset_source` attribute: `baked_state` or
`on_demand`.
- Known residual, inherited rather than introduced: a data re-release
under the same revision labels leaves the cached bundle-install layer —
and therefore the receipt every artifact key derives from — unchanged.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
"""Shared stubs for ``collect_dataset_identity``'s input seams.
"""Shared stubs for the input seams of ``artifact_keys.year_file_identity``.

The key-discipline tests (test_artifact_keys) and the writer==reader
contract tests (test_precompute) must stub the SAME seam list —
``release_bundle.get_country_release_bundle``,
``release_bundle._receipt_dataset``,
The key tests (test_prebuilt_identities, test_artifact_keys) and the
writer==reader contract tests (test_prebuilt_plan) must stub the SAME seam
list — ``release_bundle.get_country_release_bundle``,
``manifest.resolve_dataset_reference``, ``manifest.dataset_logical_name``,
``manifest.get_release_manifest``, ``spm.runtime_spm_capability`` — or one
suite silently tests a stale list when identity collection grows a seam.
This is that list's one home.
``manifest.get_release_manifest``, ``spm.runtime_spm_capability``,
``artifact_keys._installed_package_versions`` — or one suite silently tests
a stale list when identity collection grows a seam. This is that list's one
home. ``release_bundle._receipt_dataset`` is stubbed too, for the bundle
helpers that read the install receipt; no identity reads it.

The SPM capability is a seam because ``collect_dataset_identity`` resolves
the installed bundle's SPM selection into the dataset identity. The default
The SPM capability is a seam because ``year_file_identity`` resolves the
installed bundle's SPM selection into the year-file identity. The default
stub has no capability so fixed identifier tests remain tied to their
synthetic inputs instead of whichever PolicyEngine release is installed.
"""
Expand All @@ -31,7 +32,8 @@ def install_identity_stubs(monkeypatch: pytest.MonkeyPatch) -> SimpleNamespace:
SPM-capability state, plus a handle to the installed capability resolver
for tests that intentionally exercise the installed release. The bundle
has one non-default dataset, ``populace_cps_local``, for the per-state
dataset identities.
dataset identities. ``core_version`` is the policyengine-core version the
installed-package stub reports.
"""
# This module imports ``get_release_manifest`` by value while constructing
# country models. Import it before replacing the manifest module's function
Expand Down Expand Up @@ -62,6 +64,7 @@ def install_identity_stubs(monkeypatch: pytest.MonkeyPatch) -> SimpleNamespace:
},
)
release_datasets = {
"populace_cps": SimpleNamespace(sha256="d00dfeed" * 8),
"populace_cps_local": SimpleNamespace(sha256="c0ffee00" * 8),
}
receipt_entry = {
Expand All @@ -74,6 +77,7 @@ def install_identity_stubs(monkeypatch: pytest.MonkeyPatch) -> SimpleNamespace:
receipt_entry=receipt_entry,
release_datasets=release_datasets,
spm_capability=None,
core_version="3.33.3",
)

monkeypatch.setattr(
Expand Down Expand Up @@ -101,6 +105,27 @@ def install_identity_stubs(monkeypatch: pytest.MonkeyPatch) -> SimpleNamespace:
),
)

from policyengine_simulation_executor import ( # noqa: PLC0415 # patch target, imported when the stubs are installed
artifact_keys,
)

def installed_package_versions(country: str) -> dict[str, str]:
country_bundle = release_bundle.get_country_release_bundle(country)
return {
"policyengine": country_bundle.policyengine_version,
"policyengine-core": state.core_version,
f"policyengine-{country.lower()}": country_bundle.model_version,
}

# Year-file keys name the installed packages; the stub pins them to the
# bundle's versions and a fixed policyengine-core version.
monkeypatch.setattr(
artifact_keys,
"_installed_package_versions",
installed_package_versions,
raising=False,
)

from policyengine_simulation_executor import (
spm as executor_spm,
)
Expand Down
Loading