Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
01357a3
Add tests that fail before household runs record their variables and …
anth-volk Dec 3, 2026
a0467e3
Correct two tests that fail before the change
anth-volk Dec 3, 2026
93a3bdf
Correct the reference error a test raises and format the new tests
anth-volk Dec 3, 2026
c0358f6
Expect the 400 answer of a household run's extra calculations
anth-volk Dec 3, 2026
223449d
Add tests that fail before the change for failed runs and NUL characters
anth-volk Dec 3, 2026
a43f123
Record household runs' variables and run their extra calculations
anth-volk Dec 3, 2026
4ff3de8
Add tests that fail before review round 1's fixes
anth-volk Dec 3, 2026
570a235
Refuse variables without a run and answer unconfirmed extra calculations
anth-volk Dec 3, 2026
183525e
Cover an unforced re-run's lists and the client's new fields, and lis…
anth-volk Dec 3, 2026
e92f900
Describe extra calculations in the sweep and Stage 12 documents
anth-volk Dec 3, 2026
90f4b74
Merge main (household function containers keep their runtime) into th…
anth-volk Dec 3, 2026
ca7d391
Keep the Modal app and its tests under the file-length limits after m…
anth-volk Dec 3, 2026
93ec16e
List the household run tables in the persistence tests that name ever…
anth-volk Dec 4, 2026
47e69d1
Name only the left-out grants in the missing-privileges rule's proof …
anth-volk Dec 4, 2026
9bc1e53
Name the primary-key test after what it now checks
anth-volk Dec 4, 2026
8cbf65b
Merge main (reports.type required, API v1 pinned on D1 · 8) into the …
anth-volk Dec 4, 2026
75b651f
Move the service tables' column limits and run states into table_values
anth-volk Dec 4, 2026
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/household-run-calculations.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A household report's create and re-run may list the `variables` its runs calculate; each new simulation run records the list, sorted, in `household_run_variables`, and the household function calculates each side with its run's list. `POST /reports/{report_id}/runs/{report_run_id}/household-calculations` finds or starts an extra calculation of other variables, or across axes, of each side's run on the current versions, in `household_run_calculations`; identical requests share one, the stuck-run sweep covers them, and a failed run's is refused with 409 until the report is re-run. A `variables` list that is empty, repeats a name or has a name with a NUL character is refused with 422, as is a `variables` list on `POST /reports/without-run`, which creates no run to record it. `policyengine-api-simulation-client` gains the route and the `variables` fields.
20 changes: 12 additions & 8 deletions docs/engineering/skills/database-access.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,10 @@ and this repository has no migration chain of its own
- the Stage 12 comparison tables, whose live schema deployment validation
compares with the Stage 12 library's SQLAlchemy mappings.
- the tables the simulation service writes (`simulations`, `reports`,
`simulation_runs`, `report_runs`, `budget_window_runs`, `household_jobs`
and `cliff_impacts`), modelled in `libs/policyengine-simulation-persistence`.
`simulation_runs`, `report_runs`, `budget_window_runs`, `household_jobs`,
`cliff_impacts`, `household_run_variables` and
`household_run_calculations`), modelled in
`libs/policyengine-simulation-persistence`.
The service connects as its own role, which policyengine-api's privilege
manifest grants.
- the policyengine-api tables a calculation is read from (`households`,
Expand All @@ -185,12 +187,14 @@ and this repository has no migration chain of its own
- the run tables the v2 Modal coordinator, workers, household functions and
stuck-run sweep write as the Modal worker role: `report_runs`,
`simulation_runs`, `budget_window_runs`, the result tables,
`household_jobs` and `cliff_impacts`. The role also deletes finished
`household_jobs` rows of households given in the request whose versions a
deployment replaced. They use the same models, in
`libs/policyengine-simulation-persistence` (`tables.py` and
`result_tables.py`, which `sim.persistence.models-match-public-api`
compares with the Public API's tables), through the stores in
`household_jobs`, `cliff_impacts` and `household_run_calculations`; a
household function also reads `household_run_variables`. The role also
deletes finished `household_jobs` rows of households given in the request
whose versions a deployment replaced. They use the same models, in
`libs/policyengine-simulation-persistence` (`tables.py`,
`household_run_tables.py` and `result_tables.py`, which
`sim.persistence.models-match-public-api` compares with API v1's tables),
through the stores in
`libs/policyengine-stage12-persistence`. The v1 `run_simulation` code
imports neither library.

Expand Down
2 changes: 1 addition & 1 deletion docs/engineering/skills/observability.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Use each identifier for its defined scope:
| `evaluation_id` | One Stage 12 report | Stage 12 report construction | Yes, as functional report state |
| `simulation_execution_id` | One Stage 12 baseline or reform simulation | Stage 12 coordinator | Yes, as functional simulation state |
| `submission_claim_id` | One API v1 attempt to acquire ownership of a simulation submission | API v1 | Only where the simulation request is stored |
| `database_table` | The API v1 table holding the row a v2 Modal function call, or a Simulation Entrypoint log record, is about: `household_jobs`, `report_runs` (a report run or a household report run), `simulation_runs`, `cliff_impacts`, `reports` or `simulations` | The code writing the record, as a fixed name | No: telemetry only |
| `database_table` | The API v1 table holding the row a v2 Modal function call, or a Simulation Entrypoint log record, is about: `household_jobs`, `report_runs` (a report run or a household report run), `simulation_runs`, `cliff_impacts`, `household_run_calculations` (a household run's extra calculation), `reports` or `simulations` | The code writing the record, as a fixed name | No: telemetry only |
| `database_row_id` | That row's UUID | The simulation service, which creates the row | Yes, as the row's ID |
| `modal_call_id` | One Modal function call of a v2 Modal function | Modal | Only where the row records the call that claimed it; a segment worker's call is recorded nowhere, and its `database_row_id` names the simulation run its simulation worker's call claimed |

Expand Down
39 changes: 33 additions & 6 deletions docs/migration/stage-12-modal-worker-foundation.md
Original file line number Diff line number Diff line change
Expand Up @@ -977,15 +977,17 @@ A call can instead name a household report run, with the
`HouseholdReportRunCalculation`
(`policyengine_simulation_contract.household_report_execution`): the
baseline's `HouseholdExecutionRequest` and, optionally, the reform's, each
request's calculation ID naming its simulation run. A call names a job or a
report run, never both. The function then:
request's calculation ID naming its simulation run. A call names at most
one of a job, a report run and an extra calculation. The function then:

1. claims the report run under its own Modal call ID, as a report run's
coordinator does, and reads which baseline and reform simulation runs it
names;
2. validates the payload and checks that its runs are those;
3. for each side, claims the side's simulation run, calculates it, and
writes its `HouseholdExecutionResult`, or its `HouseholdExecutionFailure`
3. for each side, claims the side's simulation run, calculates it with the
variables its run records in `household_run_variables` (every variable
without a row, whatever the payload lists, since identical requests
share runs), and writes its `HouseholdExecutionResult`, or its `HouseholdExecutionFailure`
and one-line summary, to the run's `household_result` and
`error_message`;
4. marks the report run succeeded only if every side's run did, or fails it
Expand Down Expand Up @@ -1025,6 +1027,31 @@ year. The service starts one call for a run it created, as for a new
household report, and the function claims and calculates it as it would a
new report's run.

A call can instead name an extra calculation of a household report run's
simulation run, with the `household_run_calculation_id` keyword argument,
and then takes that calculation's `HouseholdExecutionRequest`, whose
calculation ID is the calculation's. The service inserts the
`household_run_calculations` row pending, starts the call, and records its
ID (`POST /reports/{report_id}/runs/{report_run_id}/household-calculations`).
The function claims the row under its own Modal call ID, calculates the
request, and records the `HouseholdExecutionResult` as its `result`, or the
failure's one-line summary as its `error_message`, as for a household job: a
row another call holds, or one already finished, returns `deduplicated`, the
calculation's ID and its status, without calculating; any other error or
Modal's cancellation fails it with a summary naming only the error's type;
a pre-emption records nothing.

API v1's revision that adds `household_run_variables` and
`household_run_calculations` must be applied in an environment before this
release deploys there: the service's `/ready` privilege check names both,
every household report side reads `household_run_variables`, and the
stuck-run sweep lists `household_run_calculations` on every pass. Between the
promotion of the Simulation Entrypoint and the move of the v2 default, the
entry may start the previous default's household function with the new
keyword, which that function does not take: the call fails before any claim
and the stuck-run sweep fails the row once it is ten minutes old. Nothing calls the
route until API v1 does.

The stuck-run sweep (`src/modal/run_sweep_app.py`) fails household jobs
nothing will finish by the rule it applies to report runs and simulation
runs (a household report run and its simulation runs are judged as any
Expand All @@ -1033,8 +1060,8 @@ creation: a pending job with no call
recorded ten minutes after its creation, a pending or running job once
Modal reports its recorded call ended, and any job still unfinished 24 hours
after its creation, whatever Modal reports. It judges simulation runs' cliff
results last, by the same rule, counting from a result's first claim or,
while it is pending, its creation.
results, then household runs' extra calculations, last, by the same rule,
counting from a row's first claim or, while it is pending, its creation.

- Image: the CA store, the frozen `modal-simulation-image` dependency group
and the source packages, without the bundle's datasets or the Stage 12
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
DatabaseTable = Literal[
"cliff_impacts",
"household_jobs",
"household_run_calculations",
"report_runs",
"reports",
"simulation_runs",
Expand Down
41 changes: 27 additions & 14 deletions libs/policyengine-simulation-persistence/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,14 @@ referenced row before adding the row that names it.
service creates rows in: `Simulation`, `Report`, `SimulationRun`, `ReportRun`,
`BudgetWindowRun`, `HouseholdJob` and `CliffImpact`, for `simulations`,
`reports`, `simulation_runs`, `report_runs`, `budget_window_runs`,
`household_jobs` and `cliff_impacts`.
`household_jobs` and `cliff_impacts`. `household_run_tables.py` models the
two tables of household runs, `HouseholdRunVariables` and
`HouseholdRunCalculation`, for `household_run_variables` (the variables a
household report's simulation run calculates) and
`household_run_calculations` (a run's extra calculations of other variables,
or across axes). `table_values.py` holds the column limits and run states the
models use, such as `MODAL_CALL_ID_LENGTH` and `SimulationStatus`;
`tables.py` re-exports them.
They derive from `ServiceBase` in `orm_base.py`, and their tables are in
`service_metadata`, which names the `public` schema so writes reach those
tables whatever the role's `search_path`.
Expand Down Expand Up @@ -435,7 +442,9 @@ the read-only input models' tables in another disposable database and
requires every column they name to exist in the Public API's table with the
same type and nullability; their other columns, defaults, constraints and
indexes are the Public API's alone. `test_service_writes` creates a row in every table
the service writes, as the service's role; `test_modal_worker_writes`, as the
the service writes but the two household run tables, as the service's role,
and `test_household_run_records` writes those two as that role;
`test_modal_worker_writes`, as the
Modal worker role, checks the role's privileges, records a run's state in
each run table, inserts a row in every result table and reads back that a
`None` JSON result is SQL `NULL`. Another test, outside the rule, requires the modelled
Expand Down Expand Up @@ -489,11 +498,12 @@ uv run pytest tests/ -m public_api_schema
`SERVICE_TABLE_PRIVILEGES` lists the table privileges the service's role
needs: `SELECT` and `INSERT` on the tables it creates rows in
(`simulations`, `reports`, `simulation_runs`, `report_runs`,
`budget_window_runs`, `household_jobs` and `cliff_impacts`, a simulation
run's cliff calculations), and `SELECT` on the policy, dynamic, household and
catalog tables a run reads. `SERVICE_UPDATABLE_COLUMNS` lists the columns it
may `UPDATE` on `simulation_runs`, `report_runs`, `household_jobs` and
`cliff_impacts`: `status`, `modal_call_id`, `error_message`, `completed_at`
`budget_window_runs`, `household_jobs`, `cliff_impacts`, a simulation
run's cliff calculations, `household_run_variables` and
`household_run_calculations`), and `SELECT` on the policy, dynamic, household
and catalog tables a run reads. `SERVICE_UPDATABLE_COLUMNS` lists the columns
it may `UPDATE` on `simulation_runs`, `report_runs`, `household_jobs`,
`cliff_impacts` and `household_run_calculations`: `status`, `modal_call_id`, `error_message`, `completed_at`
and `updated_at`, to record a Modal call's ID once Modal accepts the call, or
to fail the run when Modal refuses it. On `budget_window_runs` it lists
`status`, `error_message`, `completed_at` and `updated_at`, to fail a window
Expand All @@ -503,15 +513,16 @@ refuses any other change to a run. The Public API's privilege manifest grants
them all.

`MODAL_WORKER_TABLE_PRIVILEGES` and `MODAL_WORKER_UPDATABLE_COLUMNS` list what
the Modal worker role needs: `SELECT` on the four run tables and
`cliff_impacts`, `SELECT` and `INSERT` on every result table, because a
the Modal worker role needs: `SELECT` on the four run tables,
`cliff_impacts`, `household_run_variables` and `household_run_calculations`,
`SELECT` and `INSERT` on every result table, because a
session's insert reads the row's server defaults back, and `UPDATE` of each
run's and cliff calculation's state (`status`, `modal_call_id`,
run's, cliff calculation's and extra calculation's state (`status`, `modal_call_id`,
`error_message`, `started_at`, `completed_at` and `updated_at`; a budget-window
run has no call of its own) and of the results its own row holds: a report
run's `spm_provenance` and `data_version`, a simulation run's output file and
`household_result`, a household job's `result`, and a cliff calculation's
`cliff_gap` and `cliff_share`. A report run's cliff impact is its two
`household_result`, a household job's `result`, a cliff calculation's
`cliff_gap` and `cliff_share`, and an extra calculation's `result`. A report run's cliff impact is its two
simulation runs' cliff results. API v1's manifest grants these privileges;
`test_modal_worker_writes` checks that in CI.

Expand Down Expand Up @@ -545,8 +556,10 @@ rule's proof among them needs no database, and the others need one the
Public API's own v2 migration chain built, with the privileges its manifest
declares granted:
set `PUBLIC_API_SCHEMA_DATABASE_URL` to that database's superuser URL.
`test_service_writes` creates a row in every table the service writes as the
service's role, and `test_modal_worker_writes` writes the Modal worker role's
`test_service_writes` creates a row in every table the service writes but the
two household run tables as the service's role, `test_household_run_records`
writes those two as that role, and `test_modal_worker_writes` writes the Modal
worker role's
runs and results as that role, each rolling its transaction back; neither
role needs a login there, since the superuser takes each role for the
transaction. The comparison in `test_public_api_schema` also needs
Expand Down
2 changes: 1 addition & 1 deletion libs/policyengine-simulation-persistence/public-api-commit
Original file line number Diff line number Diff line change
@@ -1 +1 @@
57202c04991ec4239ef135dc80d1e223a9e6b076
cb8200d2e8279a5116957c3532d6392071a0e723
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,15 @@
EconomyRecordReferenceError,
RunVersions,
SimulationChoice,
fail_unstarted_household_run_calculation,
fail_unstarted_report_run,
fail_unstarted_simulation_run,
read_dataset,
read_dataset_by_id,
read_local_area_dataset,
read_model_id,
read_region,
record_household_run_calculation_call,
record_report_run_call,
record_simulation_run_call,
)
Expand Down Expand Up @@ -119,6 +121,26 @@
HouseholdReportInputs,
HouseholdReportStore,
)
from policyengine_simulation_persistence.household_run_calculation_store import (
HouseholdRunCalculationStore,
ReportWithRun,
)
from policyengine_simulation_persistence.household_run_records import (
HOUSEHOLD_CALCULATION_KEY_VERSION,
FoundHouseholdRunCalculation,
NewHouseholdRunCalculation,
SideRun,
StoredReportRun,
find_or_insert_household_run_calculations,
household_calculation_key,
read_report_run,
record_household_run_variables,
recorded_variables,
)
from policyengine_simulation_persistence.household_run_tables import (
HouseholdRunCalculation,
HouseholdRunVariables,
)
from policyengine_simulation_persistence.input_tables import (
Dataset,
Dynamic,
Expand Down Expand Up @@ -237,6 +259,7 @@
)

__all__ = [
"HOUSEHOLD_CALCULATION_KEY_VERSION",
"HOUSEHOLD_REPORT_TYPE",
"KEY_VERSION",
"LATEST_YEAR",
Expand Down Expand Up @@ -279,6 +302,7 @@
"FoundEconomyReport",
"FoundEconomySimulation",
"FoundHouseholdReport",
"FoundHouseholdRunCalculation",
"FoundReportRerun",
"FoundReportRun",
"FoundReportWithoutRun",
Expand All @@ -298,6 +322,9 @@
"HouseholdReportInputs",
"HouseholdReportStore",
"HouseholdRerunSide",
"HouseholdRunCalculation",
"HouseholdRunCalculationStore",
"HouseholdRunVariables",
"HouseholdSimulationDefinition",
"Inequality",
"InputBase",
Expand All @@ -317,6 +344,7 @@
"MissingPolicyParametersError",
"NewHouseholdJob",
"NewHouseholdReportRerun",
"NewHouseholdRunCalculation",
"NewReportRerun",
"NewSimulationRerun",
"NewWindowRerun",
Expand All @@ -339,6 +367,7 @@
"ReportRunStatus",
"ReportSide",
"ReportSideError",
"ReportWithRun",
"RerunRecordDeletedError",
"RerunSide",
"ResultBase",
Expand All @@ -347,6 +376,7 @@
"SharedRowNotSettledError",
"SideDefinition",
"SideRefusal",
"SideRun",
"Simulation",
"SimulationChoice",
"SimulationDefinition",
Expand All @@ -357,6 +387,7 @@
"SimulationStatus",
"SimulationType",
"StoredHousehold",
"StoredReportRun",
"TaxBenefitModel",
"TaxBenefitModelVersion",
"UKAreaType",
Expand All @@ -381,6 +412,7 @@
"definition_key",
"dispose_session_factory",
"fail_unstarted_household_job",
"fail_unstarted_household_run_calculation",
"fail_unstarted_report_run",
"fail_unstarted_simulation_run",
"find_joinable_report_run",
Expand All @@ -392,12 +424,14 @@
"find_or_create_household_report",
"find_or_create_household_report_without_run",
"find_or_insert_cliff_calculations",
"find_or_insert_household_run_calculations",
"find_or_insert_report",
"find_or_insert_report_run",
"find_or_insert_report_run_cliffs",
"find_or_insert_simulation",
"find_or_insert_simulation_run",
"find_or_insert_simulation_runs",
"household_calculation_key",
"input_metadata",
"insert_household_job",
"insert_household_report_rerun",
Expand All @@ -421,12 +455,16 @@
"read_policy_reform",
"read_region",
"read_report_definition",
"read_report_run",
"read_simulation_definition",
"read_simulation_reform",
"read_stored_household",
"record_household_call",
"record_household_run_calculation_call",
"record_household_run_variables",
"record_report_run_call",
"record_simulation_run_call",
"recorded_variables",
"referenced_metadata",
"result_metadata",
"service_metadata",
Expand Down
Loading
Loading