Repository navigation
Conversation
The v2 US workers' snapshot hook is to calculate one reform segment of the first region group the v2 cache precomputes, for the first precompute year, only in a container Modal takes a snapshot from. The tests check that it reads no Secret and writes no credentials file (through the real v2 calculation up to its dataset load), configures no observability, leaves the first call's number at 1, leaves nothing in .py's cache or the prebuilt folder, skips without the prebuilt year file or the preloaded US models, and only logs a warning when it fails. Two tests cover a switch that makes the Google Cloud credential setup do nothing. The environment-read recorder moves from the run_simulation warm-up's tests into a support module both files use. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
SimulationSegmentWorkerUS and SimulationWorkerUS now calculate one segment before Modal takes their memory snapshot: the reform side of the first region group the v2 cache precomputes (state/hi, state/ia, state/wi), for the first precompute year (2026), under the CTC base amount raised to $2,500, through stage12_runtime.calculate_segment as the fan-out sends it. A reform segment always calculates and never reads or computes a baseline, so the warm-up does the same work with either cache outcome. It runs only when Modal snapshots the container (MODAL_ENABLE_SNAP_RESTORE is 1), the US models were preloaded and the image's prebuilt folder holds the group's year file in a form .py reuses. It calculates with no observability runtime and inside the new skip_gcp_credential_setup, so the v2 calculation's credential setup reads no Modal Secret and writes no credentials file before the snapshot. It removes the simulation and output file it leaves, with the run_simulation warm-up's cleanup, now public. Any failure only logs a warning. The UK worker's hook calls it and it does nothing there, since the UK image holds no prebuilt year file yet. v2_app.py would pass its 800-line limit, so the workers' CPU, memory, time and container limits move into src/modal/v2_worker_resources.py, with the registered options unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review round 1 found that the v2 warm-up imports the run_simulation app's warm-up module, so removing or renaming that module would stop the v2 app from importing. The new test blocks the run_simulation app's modules and expects the v2 app to import and warm up; it fails today. The real-calculation test now also checks that the warm-up's year-file check asks the region, dataset and prebuilt-folder lookups exactly what the calculation then asks, and that nothing opens a socket connection. A new test checks the v2 warm-up's own reform against the installed US model. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The v2 warm-up now defines its own snapshot flag name, reform and cleanup instead of importing them from src/modal/snapshot_warm_up.py, which goes away with the run_simulation app while the v2 app stays. The run_simulation app's warm-up module is back as it was. The v2 cleanup removes only what a reform segment leaves: new entries in .py's simulation cache and new output files in the prebuilt folder. Also adds the changelog fragment for #1146. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review round 2 found that the warm-up collects garbage while a failed calculation's exception still holds its frames, so the calculation's reference cycles survive into the snapshot. It also found that .py's cache remembers a memory warning the warm-up's calculation triggered, so restored containers would not warn when their own calls pass that threshold. Both new tests fail today. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The warm-up now collects garbage after its try statement, once a failed calculation's exception and traceback are gone, so the calculation's reference cycles are freed before Modal takes the snapshot. Its cleanup also removes the memory-threshold warnings .py's cache marked as given during the warm-up, so restored containers warn when their own calls pass 8, 16 or 32 GB. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review round 3 found no bug. Two of its points: the Secret-read tests now also refuse a read of the v2 runtime Secret's variables and the Modal worker role's database URL, and the warm-up's docstring now says that a reform segment never loads or computes the precomputed current-law baseline, while its own calculation still reads current-law values through its baseline branch. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
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.
Fixes #1146
Stacked on #1140 (base branch
v2-restore-telemetry), which edits the same snapshot hook and bringsv2_app.pyto its 800-line limit. Merge #1140 first.What changes
SimulationSegmentWorkerUSandSimulationWorkerUSin the v2 app now run one warm-up calculation in their@modal.enter(snap=True)hook, after the model preload, before Modal takes the memory snapshot. A process's first calculation for a year builds the year's parameter values and SPM threshold amounts, which later calculations reuse; containers restored from the snapshot start with that done.state/hi,state/ia,state/wi), for the first precompute year (2026), with the Child Tax Credit base amount (gov.irs.credits.ctc.amount.base[0].amount) raised to $2,500. Its input is the precompute's own planned reform side (stage12_cache.precompute_planning.scoped_planned_simulation), given that policy and planned again withresolve_simulation_output_plan. It goes throughstage12_runtime.calculate_segment, as the fan-out sends a segment to its worker.src/modal/v2_snapshot_warm_up.py. It shares no code with the run_simulation app's warm-up (src/modal/snapshot_warm_up.py), since that app is to be removed while this one stays.SimulationWorkerUK's hook calls the warm-up, which returns at once. The v2 UK image holds no prebuilt UK year file, so a UK warm-up would build its dataset (about 97 s in production) before the snapshot. It waits for the UK files in the v2 cache.Before the snapshot
MODAL_ENABLE_SNAP_RESTOREis1; Modal runs the hook in every container that isn't restored.calculate_simulation_frames) sets up Google Cloud credentials from thestage12-evaluation-gcp-credentialsSecret before every calculation. Newgcp_credentials.skip_gcp_credential_setup()sets a context variable that makessetup_gcp_credentialsread and write nothing; the warm-up calculates inside it. Calls after the warm-up set up credentials as before.ContainerCalls, so the first call is still number 1.gc.collect(), after the exception is handled when the calculation failed, so the calculation's reference cycles are freed before the snapshot.Snapshot-taking time (estimated, not measured)
Containers Modal takes a snapshot from start later by the warm-up: three output plannings with the preloaded US model, a 2.6 s year-file load and one reform segment's calculation on 8 CPUs. Production v2 segments on fresh containers (25–27 Nov, before snapshots) calculated in a median 31.9 s for baselines (all computed, since the cache missed) and 109.7 s for reforms, whose policies were users' own. So the estimate is about 35–110 s. The run_simulation app's warm-up added 72–97 s. #1138 doesn't change it: the warm-up never loads or computes the precomputed baseline.
The gain isn't measured for v2. The run_simulation app's warm-up made a restored container's first calculation 11–16 s faster on Modal
testing, with identical results.v2_app.pylength#1140 leaves
v2_app.pyat 800 lines, and this change adds two (an import and the call). The workers' CPU, memory, time and container limits move tosrc/modal/v2_worker_resources.py, sov2_app.pyis 795 lines. All six registered option sets (three classes, three plain functions) are unchanged, checked with a script comparing them with the old literal values.Environment variables
MODAL_ENABLE_SNAP_RESTORE_container_entrypoint.pyand_runtime/user_code_imports.py)1inside the hook yet. #1140'scontainer_startattribute will show it.POLICYENGINE_DATA_FOLDERbuild_v2_imageinsrc/modal/v2_images.pysets it toSTAGE12_DATA_DIRPOLICYENGINE_BUNDLE_RECEIPTsrc/modal/app.pysets it), so the receipt is read from the data folderGOOGLE_APPLICATION_CREDENTIALS_JSON,GCP_CREDENTIALS_JSON,GOOGLE_CREDENTIALS,SERVICE_ACCOUNT_JSON,GOOGLE_APPLICATION_CREDENTIALSstage12-evaluation-gcp-credentials, attached throughworker_secretsinv2_app.pyThe preload before the warm-up already reads
HUGGING_FACE_TOKEN(Modal Secretpe-uk-private-hf-read-token) for .py's data release manifest fetch, as since #1142.Tests
Red-green: failing tests first, then the change that makes them pass:
0518dc4fthen744768e7; after review round 1,33b4bdeethend2219d22; after round 2,18fba767then036dbc47.tests/test_v2_snapshot_warm_up.py(new, 18 tests): the hook order in all three classes; only the US workers calculate; nothing without the snapshot flag; the segment's group, year, role, policy and plan; skips without the year file or the preloaded models; the realcalculate_segmentandcalculate_simulation_frames, run up to the dataset load, read no Secret, write no credentials file, open no socket connection and look up the same year file as the warm-up's check; no observability and call number 1; failures only warn and clean up; a failed calculation's reference cycles are freed; no memory warning stays marked; the v2 app imports and warms up with the run_simulation app's modules blocked; a later call finds nothing the warm-up left; the reform changes a parameter of the installed US model.tests/test_gcp_credentials.py: two tests for the switch.tests/test_snapshot_warm_up.py: its environment-read recorder moved totests/environment_reads.py, shared by both warm-up test files.tests/test_v2_container_start.py(Report container start, call number, app and .py version on each v2 worker call #1140's): its fixture replaces the warm-up, astests/test_modal_container_start.pydoes for the run_simulation app.Run by node ID: every new test, the changed test in
test_snapshot_warm_up.py, and four oftest_v2_container_start.py's tests that use the changed fixture. Ruff 0.15.22 format and check, Pyright for the executor, the quality guards and the changelog fragment check pass.Review
Three adversarial review rounds by separate reviewers.
Not covered
testingrun would measure them.ensure(), SPM provenance, Parquet output) is checked by reading the code; round 3 also ran the real planning offline with connections refused.warningsmodule shows once per code location, if the calculation raises any, aren't shown again in restored containers. None was found.🤖 Generated with Claude Code