Skip to content

Find or create a report and its simulations by key without a run - #1167

Merged
anth-volk merged 9 commits into
mainfrom
v2plan/d4-0-report-without-run
Dec 1, 2026
Merged

anth-volk merged 9 commits into
mainfrom
v2plan/d4-0-report-without-run

Conversation

@anth-volk

Copy link
Copy Markdown
Contributor

Fixes #1166

What

API v1's one-time migration of saved reports to API v2 (PolicyEngine/policyengine-api#4359) creates each saved report's report in the simulation service with no run. App v3 starts a migrated report's first run when someone opens it, through POST /reports/{report_id}/runs. Until now, POST /reports always found or created a run and started its calculation, so the migration had no way to create a report alone.

  • POST /reports/without-run (operation create_report_without_run, permission simulation:submit) takes POST /reports' body unchanged: economy, budget window or household. It reads the v2 version manifest and the catalog for the default worker's .py version, as POST /reports does, through the same code. It also runs the checks POST /reports runs before writing: a budget window's first-year calculation input, and a household report's household contract requests. It then finds or creates the report and its simulations (for a budget window, every year's) by the same keys, in one transaction. It creates no run, no cliff calculation and no Modal call. A report it finds keeps whatever runs it has. It answers 201 with ReportWithoutRunCreated: report_id, report_type, baseline_simulation_id, reform_simulation_id (null only for a household report of the baseline alone) and, for a budget window, years. include_cliffs: true is refused with 422 cliffs_not_available.
  • Persistence: find_or_create_economy_report, find_or_create_budget_window and find_or_create_household_report are each split into a report part and a run part. The report part is also a create of its own (find_or_create_*_without_run), so both creates write the report and its simulations the same way. A later POST /reports of the same body finds them and creates the run.
  • Re-run of a budget window with no window run: POST /reports/{report_id}/runs answered 409 for one, because a window's years came only from its newest window run. The re-run's definition read now checks first whether the window has any window run. If it has none, each year's simulations are found by their keys, provided the report's stored key still matches the key its stored fields give. The read checks for a window run before looking for years, so a window run another request creates meanwhile is joined rather than answered with 409.
  • Kept apart for removal: the services that create reports without a run are built by their own factories and registered by one call in create_app. The services that create runs don't depend on them. The route is temporary: a later task removes it once the migration's last run is done. The re-run change stays, since migrated windows have no window run until someone opens them.
  • Client: the generated policyengine-api-simulation-client gains create_report_without_run and ReportWithoutRunCreated. Hermetic tests in policyengine-apis-integ check its wire shapes.

Design decisions (the user, 2026-12-01)

  • A separate route rather than a request field on POST /reports, so POST /reports' answer union stays as it is and the client gets a function with one answer type.
  • The route reads the version manifest, so a report created without a run is stored against the catalog version that its first run, and any later POST /reports of the same body, uses.
  • The re-run of a keyed budget window with no window run is fixed here, so a migrated budget window can get its first run.

Tests

Red-green: failing tests were committed before each change (2bcd63e4 and 519f3bb2 before 3046dc6a; 2767ada5 before 5b66cef7; 5ae7dafb before 4a1a59ea). Every added or changed test was run by node ID:

  • 17 persistence cases against a database built by API v1's v2 migration chain at the pinned commit;
  • 25 entrypoint cases (the new file, the changed OpenAPI cases and the authorization cases);
  • 9 generated-client cases against a client generated from this branch.

Ruff, the rules render, the quality guards (file length included), the uv lock checks and Pyright on the persistence library, the entrypoint and policyengine-apis-integ pass. Two review rounds: the first found one concurrency bug, now fixed; the second found none. Both rounds' quality findings are fixed.

Deployment

No workflow, image, Terraform or environment variable changes. The route needs what POST /reports already needs: the simulation database and the v2 Modal environment, whose version manifest it reads.

🤖 Generated with Claude Code

anth-volk and others added 9 commits December 1, 2026 05:01
API v1's one-time migration (D4 · 1) creates each saved report's report
with no run, and app v3 starts its first run when it is opened. These
tests describe sim's new POST /reports/without-run, the persistence
functions behind it, and the re-run of a keyed budget window that has no
window run, which today has no years and is refused.

The test that a window whose report no longer matches its own key gets
no years passes already; it guards the key lookup the fix adds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
API v1's migration sends POST /reports/without-run through the generated
policyengine-api-simulation-client. These hermetic tests check the client
sends POST /reports' bodies to the new route and reads its answer and
errors. A client generated before the route has no
create_report_without_run, so they fail on its import.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
POST /reports/without-run takes POST /reports' body and reads the version
manifest and the catalog as POST /reports does, then finds or creates the
report and its simulations (a budget window's: every year's) by the same
keys, creating no run and starting no Modal call. API v1's one-time
migration (D4 · 1) creates its reports this way; app v3 starts a report's
first run when someone opens it.

- The persistence library's find-or-create of each report kind is split
  into the report part, now a create of its own, and the run part.
- The submission services share a step that reads and checks a request
  before anything is written.
- A keyed budget window with no window run has each year's simulations
  found by key when it is re-run, so POST /reports/{report_id}/runs gives
  it its first window run instead of answering 409.

Fixes #1166

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review round 1's bug 1: a re-run's definition read looked for a runless
window's years, then for any window run, then for its years by key, each
in its own statement. A window run another request commits between the
first two reads leaves the window with no years, and the re-run answers
409.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A re-run's definition read now decides first whether the window has any
window run: with one, its years come from its newest; with none, by key.
A window run another request commits after that read is joined by the
re-run's write, which finds it on the same years, so the re-run no longer
answers 409 (review round 1's bug 1).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- The services that create reports without a run are built apart from
  the services that create runs, by their own factories, and the route
  uses those (removing the route later then touches only its own
  modules).
- A budget window without a run is refused before writing when its first
  year's calculation input cannot be built, as POST /reports refuses it;
  the economy service gets the call's request ID for that check.
- A window's proposed and found years use the re-run read's
  WindowYearDefinition.
- The by-key window read is tested on a window created without a run,
  with and without an SPM selection; the route's permission is in the
  authorization table (those two cases pass already).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- The services that create reports without a run are built by their own
  factories (economy_reports_without_run_for and
  household_reports_without_run_for) and registered by one call in
  create_app; the re-run services, their protocols and the economy
  routes' wiring are as they were on main, so removing the route later
  touches only its own modules.
- A budget window's shared preparation builds its first year's
  calculation input on the proposed run IDs, so both POST /reports and
  POST /reports/without-run refuse a window that cannot be calculated
  before writing; the route passes the call's request ID for it.
- WindowYearDefinition is the one window-year type for proposed, found
  and re-run years, and REPORT_SIDES the one side-name tuple.
- The entrypoint README documents the route and the by-key re-run, and
  the re-run docstrings name the by-key years.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- WindowYearDefinition is defined with the window creates
  (keyed_window_records) and imported by the re-run read, so re-runs
  depend on creates and not the other way round.
- Every by-key re-run test builds its window with
  find_or_create_budget_window_without_run, and a new test shows a
  re-run whose definition was read before another request's window run
  joins that window run.
- The authorization table's comment is back above the table, and the
  economy_requests docstring names the prepared request types.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@anth-volk
anth-volk marked this pull request as ready for review December 1, 2026 15:16
@anth-volk
anth-volk merged commit 533928b into main Dec 1, 2026
28 checks passed
@anth-volk
anth-volk deleted the v2plan/d4-0-report-without-run branch December 1, 2026 15:26
anth-volk added a commit to PolicyEngine/policyengine-api that referenced this pull request Dec 1, 2026
The pinned policyengine-api-simulation-client lacks
create_report_without_run until the release published after
PolicyEngine/policyengine-sim-api#1167 deploys, and API v1 fails to load
its simulation modules without it, which fails every test shard.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
anth-volk added a commit to PolicyEngine/policyengine-api that referenced this pull request Dec 1, 2026
policyengine-api-simulation-client 0.20261201.454 is the first release
published after PolicyEngine/policyengine-sim-api#1167 deployed; it has
create_report_without_run, which the migration command's report
submitter calls.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

Find or create a report and its simulations by key without a run, for API v1's one-time migration

1 participant