Skip to content

Run at most four years of an API v2 budget window at once - #1185

Draft
anth-volk wants to merge 6 commits into
mainfrom
claude/budget-window-four-years
Draft

anth-volk wants to merge 6 commits into
mainfrom
claude/budget-window-four-years

Conversation

@anth-volk

Copy link
Copy Markdown
Contributor

Fixes #1184

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/budget-window-four-years.changed.md)

Summary

API v2 runs at most four years of a budget window at once instead of five. This is tracker task 1.9 · 2 of "14. Report runtimes", whose substage makes budget windows run each year in segments, at most four years at once, in API v1 and API v2.

  • BUDGET_WINDOW_MAX_RUNNING_YEARS (policyengine-stage12-persistence) drops from 5 to 4. The simulation service starts up to a new window's first four years, and each year's coordinator, when its year ends, starts the earliest waiting years until four are under way (running, or started with a Modal call recorded). No caller overrides the default.
  • The container limits of API v2's Modal functions are unchanged: coordinate_report 25, run_single_simulation_us and run_single_simulation_uk 50 each, run_single_simulation_segment_us 1,000. The capacity test now gives the coordinator's 25 as three windows of four years and 13 other reports, and the comment on the limit in v2_app.py says so.
  • Texts that said five now say four or name the constant: docstrings in budget_window_scheduling and budget_window_submissions, the create and re-run routes' descriptions (both appear in the simulation service's OpenAPI document), both READMEs, and the Stage 12 worker document.
  • US national years were already split into the 20 region groups; this PR adds tests that show it for a budget window's later years.

During the deployment

A merge to main deploys the v2 Modal application (deploy_stage12_v2) before the simulation service receives traffic (promote_entrypoint), and coordinator calls already running keep the old code. A window that spans the deployment can briefly have five years under way. Its next advance then finds no free place and starts nothing until fewer than four are under way; a new store test covers this. The container limits, sized for five years per window before this change, have room for it.

Environment variables

The changed code reads and declares no environment variables. The only change in deployment code is a comment in projects/policyengine-simulation-executor/src/modal/v2_app.py.

Testing

Commits: 60a239b5 adds tests that fail before the change; bc112a84 makes the change; 02ef2b2e, ced94ab0, 216329b0 and fe03cc9b are fixes from review. Every test below was run on its own by pytest node ID. The persistence tests ran against a local PostgreSQL 17, with the API's tables built at the pinned commit as pr.yml builds them.

Changed tests, which fail before the change and pass after it:

  • In test_budget_window_store, six tests now expect the first advance to start the four earliest years, at most four under way, and one free place when three are under way:
    • test_the_window_runs_at_most_four_years_at_once
    • test_the_first_advance_starts_the_four_earliest_years
    • test_an_advance_fills_only_the_free_places
    • test_a_start_that_fails_after_its_call_claimed_the_year_keeps_the_year
    • test_two_advances_at_once_start_each_year_once
    • test_a_reports_later_window_run_starts_only_its_own_years
  • In test_budget_window_sessions, two tests (fixed after review round 2) now expect 2026 to 2029 started:
    • test_an_advance_starts_the_earliest_waiting_years
    • test_concurrent_advances_start_each_year_once
  • test_budget_window_submissions::test_a_window_is_created_with_a_run_per_year_and_its_first_four_started.
  • test_stage12_budget_window_capacity::test_the_coordinator_holds_three_windows_at_their_cap_and_other_reports. Before the change it failed with 25 == 3 * 5 + 13.

New tests, which pass before and after the change because they record existing behaviour:

  • In test_stage12_budget_window_segments, each test starts a later year of a US national window with the real ModalYearStarter, whose input comes from year_calculation. It plans each side with resolve_simulation_output_plan, the plan a report run's coordinator gives each worker, using the installed US model, and applies should_segment_simulation. The three cases:
    • test_both_simulations_of_a_later_us_national_year_run_in_segments: a plain year, where both sides split.
    • test_a_labour_supply_response_reform_runs_whole_beside_a_split_baseline: the reform runs in one piece and the baseline still splits.
    • test_a_window_marked_unsegmented_keeps_the_mark_in_its_later_years: sides marked segmented: false in the first year keep the mark in the later year, and neither splits. No API v2 request can send this option.
  • test_budget_window_store::test_an_advance_with_more_years_under_way_than_the_limit_starts_none.

Results:

  • Before the change (60a239b5; the two session tests at 02ef2b2e), every changed test failed on its assertions and the three segment tests passed.
  • After the change, all of them pass.
  • Ruff 0.15.22 format and check report nothing on the changed Python files. Pyright 1.1.414 reports 0 errors on the changed source files.
  • I didn't run full suites, per the task's rules; CI runs them.

Review

There were three rounds of adversarial review before this PR opened:

  • Round 1: one low-priority bug. The rerun_report description still said "first five started at once". Fixed in 02ef2b2e.
  • Round 2: one high-priority bug. Two test_budget_window_sessions tests still expected five years started; they would have failed the "Public API schema" job. Fixed in ced94ab0. The over-limit store test was added after this round (216329b0).
  • Round 3: no bugs. Its two code-quality suggestions were applied in fe03cc9b: the over-limit test derives its years from the constant, and the re-run descriptions say "up to" four.

No bugs are left open.

What the merge waits for

This PR builds on main, not on another task's branch. In the tracker, the substage's PRs merge in order. Before this one merges, each of the following must be merged; neither needs to be deployed, because this PR uses nothing either one creates or deploys:

  • 1.9 · 0, "Mark each year of a multi-year report in its run record", which changes API v1's budget-window batch.
  • 1.9 · 1, "Run each year of an API v1 US national budget window in segments, at most 4 years at once", which changes API v1's batch.

Then it waits for this PR's own checks.

🤖 Generated with Claude Code

anth-volk and others added 6 commits December 3, 2026 00:56
The budget-window store's tests, and the simulation service's window
creation test, expect the earliest four years started and at most four
under way, so they fail while BUDGET_WINDOW_MAX_RUNNING_YEARS is 5. The
capacity test keeps the v2 functions' container limits and gives the
coordinator's 25 as three windows of four years and 13 other reports,
which fails while a window may run five.

New tests start a later year of a US national window as a coordinator
does, plan its two simulations with resolve_simulation_output_plan and
apply should_segment_simulation: both are split into region groups; a
labour-supply-response reform runs whole beside a split baseline; and a
window marked segmented: false keeps the mark and runs whole. These pass
before the change: they record what the code already does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
BUDGET_WINDOW_MAX_RUNNING_YEARS drops from 5 to 4. The simulation service
starts a new window's first four years, and each year's coordinator starts
the next waiting year only while fewer than four are under way.

The v2 functions' container limits stay at 25 coordinators, 50 calls of
each simulation worker and 1,000 US segment calls; the comment on the
coordinator's limit now gives its 25 as three windows of four years and
13 other reports. Docstrings, route descriptions, READMEs and the Stage 12
worker document that said five now say four or name the constant.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The rerun_report OpenAPI description still said a new budget-window run
has its first five years started at once. The segment tests' reforms now
end where the simulation service ends an open-ended value, in 2200, and
the changelog fragment says each coordinator starts waiting years until
four are under way.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
test_an_advance_starts_the_earliest_waiting_years and
test_concurrent_advances_start_each_year_once still expected a seven-year
window's first advance to start 2026 to 2030; with at most four years
under way it starts 2026 to 2029. The segment tests' docstring now says
the simulation service starts a window's first years and a coordinator
the later ones, and the changelog fragment names API v2's Modal functions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A window whose coordinator calls started a fifth year under the old limit
of five has more years under way than the new limit; its next advance
starts none until fewer than four are under way.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The over-limit store test puts one year more than
BUDGET_WINDOW_MAX_RUNNING_YEARS under way, so it keeps testing a window
over the limit if the limit changes. A re-run's window starts up to its
first four years, since a shorter window has fewer and a later year
Modal doesn't confirm in time is deferred.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

Run at most four years of an API v2 budget window at once

1 participant