Repository navigation
Keep a household container per country running in production - #1197
Merged
Merged
Conversation
…change Production's v2 household functions are to keep one container running per country, with extra containers stopped after ten idle minutes; staging keeps none. The tests cover reading those settings at deploy time, the deploy workflow passing each environment's values, and a script, run once the v2 default has moved, that sets the minimum to 0 on every other deployed v2 app's household functions. Placeholder modules let the tests fail on their assertions; the next commit implements them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Each caller of the reusable deploy now sets the API v2 household functions' minimum container count and idle window for its environment: one container and 600 seconds in production, none and Modal's 60 seconds in staging. The v2 app reads them from the environment when it is deployed (src/modal/v2_household_scaling.py), and an invalid value stops the deploy. Every .py version's v2 app stays deployed, and the simulation service calls only the default worker's household functions. So after the Stage 12 v2 default moves, the new job lower_replaced_household_containers runs src/modal/utils/lower_replaced_household_containers.py, which sets the minimum to 0 on the household functions of every other deployed v2 app in the Modal environment. It is its own job, so a failure there never puts the previous default back. Registers the tests under the new rule sim.deploy.household-containers-follow-the-default, documents the job in the executor README and the entry's DEPLOYMENT.md, and adds a changelog fragment. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The household container script must set the v2 default's household functions to the environment's minimum, so a default that an earlier run set to 0 gets its containers back; use Function.autoscaler.update, since the locked Modal client deprecates update_autoscaler; fail a function whose reported minimum isn't the one requested; and refuse to change anything when modal app list doesn't list the default's app as deployed. The workflow passes the deploy's minimum as --default-min-containers. The script takes the new argument as a placeholder, unused, so the tests fail on what it does rather than on its signature. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review round 1 found that nothing gave a new default back its household containers once an earlier run had set them to 0, as "Re-run failed jobs" on an older deployment can leave it. The job now passes the deploy's minimum as --default-min-containers, and the script sets the default's household functions to it before lowering every other v2 app's. The script also uses Function.autoscaler.update, which the locked Modal client recommends over the deprecated update_autoscaler; fails a function whose reported minimum isn't the one requested; and changes nothing when modal app list doesn't list the default's app as deployed, so a change of the client's output can't make it lower nothing while reporting success. The job's name and comment, the rule's statement, scope and tests, the docs and the changelog fragment follow. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review round 2 found the executor README naming two cases that leave the household minimums wrong until the next deployment, missing a run cancelled after the default moved and a failed run of the job, and the settings module's docstring describing the script from before it set the default's minimum. Wording only. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The new job that keeps household containers only in the v2 default's app declares the release environment, as every job but stage12_v2_ready does, so the reusable workflow has 19 such jobs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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 #1196
Same-Repository Draft PR Check
PolicyEngine/policyengine-sim-api, not a personal fork.Changelog
changelog.d/<name>.<type>.md, whose type is breaking, added, changed, fixed or removed:changelog.d/v2-household-containers.changed.md.Summary
Tracker task D3 · 5c of "5. Detailed-save API": production keeps one API v2 household container running for each country.
Settings for each environment. The reusable deploy workflow takes two new required number inputs,
household_function_min_containersandhousehold_function_scaledown_window_seconds, and passes them tomodal deploy src/modal/v2_app.pyasHOUSEHOLD_FUNCTION_MIN_CONTAINERSandHOUSEHOLD_FUNCTION_SCALEDOWN_WINDOW_SECONDS. Production (prodinsimulation-deploy.yml,deploy-stage12-productioninstage12-deploy.yml) sets 1 and 600; staging (beta,deploy-stage12-staging) sets 0 and 60.The v2 app.
src/modal/v2_household_scaling.pyreads the two variables (ASCII digits only; the minimum from 0 to the functions' limit of 20, the idle window from 2 to 3600 seconds) andv2_app.pyadds them to both household functions' declarations,calculate_household_usandcalculate_household_uk. Without the variables, the functions keep no container and use Modal's idle window; an invalid value stops the deploy. No other function or class changes.Replaced apps and the default. Every .py version's v2 app stays deployed, and the simulation service calls only the default worker's household functions. After
publish_stage12_v2_defaultsucceeds, the new joblower_replaced_household_containers(shown as "Keep household containers running only in the v2 default's app") runssrc/modal/utils/lower_replaced_household_containers.py, which changes only minimums, throughFunction.autoscaler.update:modal app list --json. Opening or reading either fails before anything changes, and so does a list that doesn't show the default's app as deployed (another output format, or the wrong environment).--default-min-containers, the same input the v2 deploy step uses), since an earlier run may have set them to 0 and "Re-run failed jobs" on an older deployment can make that app the default again without deploying it.A failed update, a default without household functions, or a minimum Modal reports other than the one requested is reported, and fails the job after every other function has been tried.
A failure doesn't put the previous default back. The lowering is its own job, after the default job, so the default job's restore step is still its last step. A failure fails the workflow call: in the beta deployment, production and the client publishing are then skipped, as with a failed runtime estimates deploy; in the production deployment the run fails, which also stops the client publishing.
deployment_readydoesn't require the job.Rule and docs. New rule
sim.deploy.household-containers-follow-the-defaultnames the new tests (the registry's guard globs make the workflow tests guard tests); this changesrules/registry.toml, which rulesim.process.rule-machinery-code-ownedasks a core developer other than the author to approve. The executor README has a new "Household containers" section, including the three cases that leave the minimums wrong until the next successful deployment or a run by hand (a deployment that fails after deploying its v2 app; a run cancelled after the default moved, or a failed run of this job; a default moved by hand); the entry'sDEPLOYMENT.mdsummarises it.Cost. About $0.126 an hour for a 2-core, 4 GiB container at list price: about $92 a month each, $184 for the two.
Anthony's decisions: one container for the US and one for the UK, all day; a 600-second idle window in production; the settings as deploy inputs that
modal deployreads, with a step that lowers every replaced app, tested thoroughly (since review round 1 it also sets the default's minimum); preloading the country model left to D2 · 8.Environment variables
HOUSEHOLD_FUNCTION_MIN_CONTAINERSv2_household_scaling.py, whenmodal deployimportsv2_app.pyhousehold_function_min_containers, a literal in each of the four callerswith:→ reusableinputs→env:of the step "Deploy the additional versioned v2 Modal application" (jobdeploy_stage12_v2) only. Not given to Modal containers, where it is unset; Modal applies the declaration it received at deploy.HOUSEHOLD_FUNCTION_SCALEDOWN_WINDOW_SECONDShousehold_function_scaledown_window_seconds, a literal in each callerMODAL_TOKEN_ID,MODAL_TOKEN_SECRETlower_replaced_household_containers, and itsmodal app listsubprocesssecrets: inherit; the job's last step sets both fromsecrets.*, as the other Modal steps of the reusable workflow do. The job runs in the release environment (betaorprod).The job's step also passes the
household_function_min_containersinput to the script as--default-min-containers, on its command, not as a variable. The script runspython -I -m modal app listas a subprocess, which inherits the step's environment minusFORCE_COLORandTTY_COMPATIBLE, withNO_COLOR=1.Testing
New tests:
tests/test_v2_household_scaling.pyandtests/test_lower_replaced_household_containers.pyin the executor,tests/test_deployment_assets_household_containers.pyin the entry project. The script's tests run it against a manifest store held in memory,modal app list --jsonoutput and stand-in Modal functions, covering: the default kept and set to the environment's minimum (listed once, twice, or in other letter case); only deployed v2 worker apps lowered (eleven other app names, seven other states); the manifest's default even when older; an app the manifest doesn't name; each Modal environment; functions missing at lookup or update; failed updates and minimums Modal reports differently, with every other function still tried; an unreadable, unpublished or invalid manifest; seven unreadable app lists; a list without the default deployed; the command it runs and its environment; the realpython -I -m modal app list --help; and the command-line arguments, including twelve refused ones.Only the added or changed tests were run, each by node ID:
ec546749, tests firstDID NOT RAISE; 3 passed, which check what was already true (the variable names, no settings on the other functions,modal app list --helpruns)6e5932bd, the change64f1ec5a, round 1's tests41ff818a, round 1's fixesd56120b0changes wording only.63edbad1updates an existing test,test_deployment_uses_gcloud_workflow_without_terraform, which counts the jobs that declare the release environment: the new job makes 19 (it passed by node ID). Ruff format and lint on the changed files,python -m scripts.rules renderandvalidate,scripts/run_quality_guards.py, the lock checks and Pyright on the executor and entry packages pass. Nothing was deployed and no Modal call was made.Review
Two rounds of adversarial review, in fresh subagents:
policyengine-sim-api-v2plan-d3-5c-household-containers-review-round1-2026-12-03.md): one low-priority bug. Nothing gave a new default back its household containers once an earlier run had set them to 0, as "Re-run failed jobs" on an older deployment can leave it. Fixed with tests first (64f1ec5a,41ff818a). From its code-quality points, also done:Function.autoscaler.updateinstead ofupdate_autoscaler, which the locked Modal 1.6.4 deprecates; each reported minimum checked; nothing changed unless the default's app is listed as deployed; the job's comment and the rule's scope corrected. Not done: running the job after a failed deployment as well, which would lower a failed release's app sooner but changes the decision that it runs after the default moves; the README documents the case.policyengine-sim-api-v2plan-d3-5c-household-containers-review-round2-2026-12-03.md): no bugs. Its wording points are fixed (d56120b0). Left: no test checks the stand-in Modal objects against the real client (a changed client fails the job through the checks above, rather than passing); a failure in staging, where the job changes nothing, still skips production, as a failed runtime estimates deploy does; the script and the settings module repeat a small whole-number check.Bugs left: none.
What the merge waits for
Every earlier task is already complete, and none needs its deployment: D3 · 1 and D3 · 5a (action tasks) are done; D3 · 2 to D3 · 5 are merged and deployed; D3 · 5b is cancelled. This PR doesn't build on an earlier task's branch.
🤖 Generated with Claude Code