Repository navigation
Conversation
…run extra calculations
These tests describe D2 · 3b and fail before the change:
- sim's models of API v1's household_run_variables and
household_run_calculations tables, and the role privileges on them;
- a household report's create or re-run recording its listed variables,
sorted, for each simulation run it inserts, and each side's household
function request listing those variables;
- the household function calculating each side's run with the variables
that run records;
- finding or inserting a household run's extra calculation by a key of its
variables and axes, recording its Modal call, and failing an unstarted one;
- the household function claiming, calculating and recording an extra
calculation, and the stuck-run sweep failing one no call finished;
- POST /reports/{report_id}/runs/{report_run_id}/household-calculations and
its OpenAPI answers.
The table definitions are pinned to the head of API v1's household run
tables branch until it merges.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The extra-calculation store test inserted JSON null for absent axes, which the table's check refuses; it now leaves the column out. The variable-list refusal test now checks the list itself is refused, not an unknown field. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The extra-calculation test that deletes a run after its read built its EconomyRecordReferenceError with one argument; the error takes the table and the column. The new tests are formatted, and their long lines wrapped, as the repository's lint requires. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The route reads the stored report's household, policies and dynamics as a household report's re-run does, so it answers 400 when one of them, or the default worker's household function or catalog version, is missing. The OpenAPI test now expects that answer with the error schema. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Anthony's answers in D2 · 3c: an extra calculation of a report run that has failed is refused with 409 (report_run_failed), while pending, running and succeeded runs get one; and a household report's or extra calculation's variable name containing a NUL character is refused with 422, as /v2/household-calculations refuses it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A household report's create and re-run take an optional `variables` list,
recorded, sorted, in `household_run_variables` for each side's simulation
run the request inserted; the household function calculates each side
with its run's recorded list, or every variable without one.
`POST /reports/{report_id}/runs/{report_run_id}/household-calculations`
finds, for each distinct simulation run of the report run, the
`household_run_calculations` row of the same variables and axes that
hasn't failed, or inserts one pending and starts one household function
call for it. The function claims the row under its call and records the
calculated household or the failure's summary; the stuck-run sweep fails
a row no call finishes. A run that failed, or one on other versions than
the default worker's, is refused with 409 until the report is re-run.
An empty list, a repeated name or a name with a NUL character is refused
with 422. The sim ORM models match the tables API v1 creates (pinned to
D2 · 3a's branch until it merges).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A household report created without a run that lists variables is refused with 422 variables_not_available, as one asking for cliffs is, since no run records the list. An extra calculation whose commit can't be confirmed is answered as other creates answer it: a 500 saying the rows may have been written, logged as its own event. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A household report created without a run that lists variables is refused with 422 variables_not_available, as one asking for cliffs is: no run records the list, and the report's first run lists its own. An extra calculation whose commit can't be confirmed answers the 500 saying the calculations may have been written, logged as household_run_calculations_outcome_unknown, as other creates answer it. The household report request's description says a run it joins keeps its own list. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…t the tables A database test follows an unforced household re-run that joins the report's run, one that inserts both sides' runs, and one that joins one side's run and inserts the other's: each records the list only on the runs it inserted. Generated-client tests send an extra calculation and a household report's variables, and read the answers. The persistence README and the database-access and observability skills name the two household run tables. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The stuck-run sweep's application, its summary and the executor README name household runs' extra calculations among what the sweep fails. The Stage 12 migration document describes the household function's `household_run_calculation_id` call, that each side calculates its run's recorded variables, and that API v1's revision adding the two household run tables must be applied before this release deploys. The persistence README names the test that writes those two tables as the service. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…e extra calculations branch The household function now uses its container's observability runtime and leaves it open; the conflict in v2_app keeps that and adds the extra calculation's keyword, check and record attributes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…erging main The merge took the Modal app to 810 lines and its tests to 1,576. The household function now picks the row its call names in one branch, and the tests of calls naming an extra calculation move to their own module, expecting the container's runtime to stay open as main's household functions now leave it. The refusal tests import the household calculation module first, so its imports' own exit handlers are not recorded as the runtime's when a test runs alone. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Dec 3, 2026
…y service table `test_each_base_maps_exactly_the_tables_of_its_metadata[service]` and `test_each_refused_key_is_known_by_its_table_and_column` list the service's mapped classes by hand, so they failed once this branch mapped `household_run_variables` and `household_run_calculations`. Both lists now name the two classes. Listing them adds both to the per-class tests. API v1 keys `household_run_variables` by `simulation_run_id`, one row per run, so the primary-key test now reads each class's expected key from `PRIMARY_KEYS`, defaulting to `id`. 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 #1200
Do not merge: raise
libs/policyengine-simulation-persistence/public-api-committo the merge commit of PolicyEngine/policyengine-api#4396 (D2 · 3a). It is pinned to that PR's head until it merges, so the pinned-commit check fails until then.What this changes
The simulation service now records which variables each household report run calculates, and runs extra calculations of a run. It uses the two tables API v1 adds in PolicyEngine/policyengine-api#4396.
Variables on household reports
POST /reports,report_type: household) and re-run (POST /reports/{report_id}/runs) take an optionalvariableslist. Absent or null means every variable, as before.household_run_variables. This covers create, forced re-run and unforced re-run. A request that joins an existing run records nothing, and that run keeps its own list.variableswith 422variables_not_availablein two places: an economy or budget-window report's re-run, andPOST /reports/without-run, which creates no run to record the list.variables.Extra calculations:
POST /reports/{report_id}/runs/{report_run_id}/household-calculations{"variables": [...], "axes": ... | null}. The route calculates those variables of each side's simulation run, or of its household across the axes, under the run's household, policy, dynamic, year and versions.unknown_report), or a run that isn't the report's (404unknown_report_run);household_calculations_not_available);report_run_failed);report_run_not_current);household_run_calculationsrow of the same variables (in any order) and axes that hasn't failed, or inserts one pending.household_run_calculation_id) and records the call's ID. When Modal refuses the call or doesn't confirm it in time, the row is failed at once and answeredfailed, so the next identical request starts another.report_id,report_run_id, and forbaselineandreform(null for a baseline-only report) the calculation's ID, its simulation run and its status. A database write that can't be confirmed answers a 500 saying the calculations may have been written.HouseholdExecutionResultasresult, or the failure's one-line summary aserror_message. A row another call holds, or one already finished, isn't calculated again.Persistence and documentation
result.policyengine-api-simulation-clientgains the route and thevariablesfields when this merges.Decisions, quoting Anthony
variableskeeps the bounds above.Deployment order. API v1's revision from PolicyEngine/policyengine-api#4396 must be applied in an environment before this release deploys there, for three reasons:
/readyprivilege check names both tables;household_run_variables;household_run_calculationson every pass.Between the entry's promotion and the move of the v2 default, the entry could start the previous default's household function with the new keyword, which that function doesn't take. The call would fail before claiming the row, and the sweep would fail the row once it is ten minutes old. Nothing calls the route until API v1 does (D2 · 3c).
Tests
Tests that fail before the change were committed first, each failing on its assertion:
01357a30,a0467e39,93a3bdfaandc0358f61: the change itself;223449da: failed runs and NUL characters;4ff3de8d: review round 1's two fixes.The change then made them pass. Run locally by node ID, one file at a time, against a disposable PostgreSQL 17 with API v1's v2 schema at D2 · 3a's head:
libs/policyengine-simulation-persistencelibs/policyengine-stage12-persistenceprojects/policyengine-simulation-executorprojects/policyengine-simulation-entryprojects/policyengine-apis-integ(generated client)Five of these cover code already written, so they passed on their first run:
The repository checks passed on the changed packages: format, lint, rules registry, quality guards (file length included), lock files, and Pyright on persistence, Stage 12 persistence, observability, executor, entry and apis-integ.
Review
Two rounds of review, each by a fresh reviewer against a fresh clone.
Round 1 (
a43f1232): all eight claims met. It found one low-priority bug:POST /reports/without-runaccepted and dropped a household report'svariables. That is fixed, test first. Of its six code-quality findings:Round 2 (
183525e4): no bugs. It found documentation of the stuck-run sweep and the Stage 12 migration document still out of date, and the deployment order unstated. Both are fixed ine92f9005.Left as they are (no bugs):
0and0.0, an omitted and an explicitindex,2026and"2026".axes: []is keyed apart from no axes.What the merge waits for
This PR is built on
main, not on another task's branch.Merged
mainonce (90f4b744), because the PR conflicted with it.main's household functions now keep their container's observability runtime open. The conflict inv2_appkeeps that, and adds the extra calculation's keyword, check and record attributes.The merge took
v2_appto 810 lines andtest_stage12_modal_appto 1,576, over the file-length limits.ca7d3913fixes both:test_stage12_modal_household_run_calculations, expecting the runtime to stay open;Those three tests pass, each run alone.
Environment variables
No variable is added. The changed code reads these existing ones, wired as before:
SIMULATION_SERVICE_DATABASE_URLSIMULATION_SERVICE_DATABASE_URL_SECRET_NAMEsimulation-deploy.reusable.ymlpasses the name todeploy-cloud-run-simulation-entry.sh, which adds it as a Cloud Run runtime secretSIMULATION_V2_MODAL_ENVIRONMENT,SIMULATION_V2_MANIFEST_NAMEdeploy-cloud-run-simulation-entry.shas Cloud Run environment variablesMODAL_WORKER_DATABASE_URLv2_app.pyand to the sweep inrun_sweep_app.py🤖 Generated with Claude Code