Skip to content

Keep a household container per country running in production - #1197

Merged
anth-volk merged 6 commits into
mainfrom
v2plan/d3-5c-household-containers
Dec 3, 2026
Merged

anth-volk merged 6 commits into
mainfrom
v2plan/d3-5c-household-containers

Conversation

@anth-volk

@anth-volk anth-volk commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1196

Same-Repository Draft PR Check

  • This PR is a draft.
  • This PR is from a branch in PolicyEngine/policyengine-sim-api, not a personal fork.

Changelog

  • This PR adds a changelog fragment, 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_containers and household_function_scaledown_window_seconds, and passes them to modal deploy src/modal/v2_app.py as HOUSEHOLD_FUNCTION_MIN_CONTAINERS and HOUSEHOLD_FUNCTION_SCALEDOWN_WINDOW_SECONDS. Production (prod in simulation-deploy.yml, deploy-stage12-production in stage12-deploy.yml) sets 1 and 600; staging (beta, deploy-stage12-staging) sets 0 and 60.

  • The v2 app. src/modal/v2_household_scaling.py reads the two variables (ASCII digits only; the minimum from 0 to the functions' limit of 20, the idle window from 2 to 3600 seconds) and v2_app.py adds them to both household functions' declarations, calculate_household_us and calculate_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_default succeeds, the new job lower_replaced_household_containers (shown as "Keep household containers running only in the v2 default's app") runs src/modal/utils/lower_replaced_household_containers.py, which changes only minimums, through Function.autoscaler.update:

    1. It reads the default from the v2 version manifest and lists the Modal environment's apps with 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).
    2. It sets the default's household functions to the environment's minimum (--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.
    3. It sets the minimum to 0 on the household functions of every other deployed v2 worker app, including one whose deploy failed before the manifest named it. It never lowers the default's app (compared ignoring letter case), touches a non-v2 app, or changes an idle window or container limit. A replaced app without household functions is skipped.

    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_ready doesn't require the job.

  • Rule and docs. New rule sim.deploy.household-containers-follow-the-default names the new tests (the registry's guard globs make the workflow tests guard tests); this changes rules/registry.toml, which rule sim.process.rule-machinery-code-owned asks 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's DEPLOYMENT.md summarises 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 deploy reads, 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

Variable Read by Provider Wiring
HOUSEHOLD_FUNCTION_MIN_CONTAINERS v2_household_scaling.py, when modal deploy imports v2_app.py Workflow input household_function_min_containers, a literal in each of the four callers Caller with: → reusable inputs → env: of the step "Deploy the additional versioned v2 Modal application" (job deploy_stage12_v2) only. Not given to Modal containers, where it is unset; Modal applies the declaration it received at deploy.
HOUSEHOLD_FUNCTION_SCALEDOWN_WINDOW_SECONDS as above Workflow input household_function_scaledown_window_seconds, a literal in each caller as above
MODAL_TOKEN_ID, MODAL_TOKEN_SECRET the Modal client in the job lower_replaced_household_containers, and its modal app list subprocess GitHub Actions repository secrets (confirmed by name) Every caller passes secrets: inherit; the job's last step sets both from secrets.*, as the other Modal steps of the reusable workflow do. The job runs in the release environment (beta or prod).

The job's step also passes the household_function_min_containers input to the script as --default-min-containers, on its command, not as a variable. The script runs python -I -m modal app list as a subprocess, which inherits the step's environment minus FORCE_COLOR and TTY_COMPATIBLE, with NO_COLOR=1.

Testing

New tests: tests/test_v2_household_scaling.py and tests/test_lower_replaced_household_containers.py in the executor, tests/test_deployment_assets_household_containers.py in the entry project. The script's tests run it against a manifest store held in memory, modal app list --json output 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 real python -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:

Commit Executor Entry
ec546749, tests first 86 failed on assertions or DID NOT RAISE; 3 passed, which check what was already true (the variable names, no settings on the other functions, modal app list --help runs) 17 failed on assertions; 3 passed, which check what other jobs and steps must not have
6e5932bd, the change 89 passed 20 passed
64f1ec5a, round 1's tests 50 of the 59 changed tests failed; the other 9 are refusals the placeholder option already made 3 failed on assertions
41ff818a, round 1's fixes 59 passed 3 passed

d56120b0 changes wording only. 63edbad1 updates 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 render and validate, 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:

  • Round 1 (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.update instead of update_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.
  • Round 2 (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

anth-volk and others added 6 commits December 3, 2026 23:00
…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>
@anth-volk
anth-volk marked this pull request as ready for review December 3, 2026 20:40
@anth-volk
anth-volk requested a review from a team as a code owner December 3, 2026 20:40
@anth-volk
anth-volk merged commit 1190fcf into main Dec 3, 2026
28 of 30 checks passed
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.

Keep a household container running for each country in API v2's production

1 participant