Skip to content

Run each year of a US nationwide budget window in segments, at most 4 years at once - #1199

Merged
anth-volk merged 6 commits into
mainfrom
claude/budget-window-years-in-segments
Dec 3, 2026
Merged

anth-volk merged 6 commits into
mainfrom
claude/budget-window-years-in-segments

Conversation

@anth-volk

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

Copy link
Copy Markdown
Contributor

Fixes #1198

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.

Summary

In the 30 days to 2026-12-01, every US nationwide budget window API v1 sent failed: 8 of 9 batches stopped at the batch function's 3,600 s time limit. Each year ran in one container (1,284 to 2,341 s), because build_child_simulation_request set segmented: false on every year. A single-year US nationwide request splits into region-group segments instead (median 156 s).

  • Each year follows the single-year split rule. build_child_simulation_request no longer forces segmented: false; it sets segmented on every year to should_run_segmented_national of the year's request. A plain US nationwide year (no labour-supply responses, no segmented: false) now runs as region-group segments; state, UK, segmented: false and labour-supply-reform years run in one container, as before. Every year keeps report_type: budget_window_year from Name the report type in every run_simulation record #1183.
  • At most 4 split years run at once. New max_running_years in the contract library's budget_window_state returns min(max_parallel, 4) when the window's years split, and the requested number otherwise. create_initial_batch_state stores it as the record's max_parallel, so the gateway's submit-time record holds it; request_payload keeps the requested number (20 from API v1). The batch runner's load_or_create_batch_state also lowers a record that a gateway deployed before this change stored with the requested number.
  • Docs. The gateway README gets a "Budget-window batches" section; _job_progress_reporter's docstring counts the per-region-group progress writes of a split year.

The status response has no max_parallel field, so API v1 sees no difference apart from running_years listing at most 4 years for a US nationwide window.

Testing

Run by pytest node ID only (no full suite), Ruff 0.15.22 (CI's version) lint and format check on the changed files, and Pyright through python3 -m scripts.rules pyright --sync for the contract library and the executor.

  • Tests first (8f644a0d). New tests in the contract library's test_budget_window_state (a split window stores 4 and keeps 20 in request_payload; asking for 2 keeps 2; state, UK, segmented: false, labour-supply-reform and non-macro windows keep 20), the executor's test_budget_window_context (a plain US nationwide year says segmented: true; state, UK, segmented: false and labour-supply years say false; replaces the test that pinned every year to false) and test_budget_window_batch (a 6-year split window whose record was stored with 20 runs at most 4 years at once and stores 4; a state window runs 6 at once and stores 20). On 8f644a0d: 3 failed on assertions (20 == 4, False is True, 6 == 4), 14 passed (the cases that must not change, and 3 existing tests whose helper changed).
  • Change (6b62e5fd). The same 17 passed.
  • After merging main (0e7bbc15) and the review fixes (head 436d982b). The new tests, Name the report type in every run_simulation record #1183's report_type test (now through the shared helper) and the existing tests whose helpers changed: 13 executor and 7 contract cases passed. Ruff and Pyright pass. Each review round also re-ran the new tests before and after the change.

Review

Three rounds of adversarial review before opening this PR.

  • Round 1 (head 6b62e5fd): all claims verified. One medium bug: the gateway would store 4 for a window routed to an app of an older .py version that still runs each year in one container. Dropped: older apps are out of scope (API v1 routes only to the app of its own .py version). Code-quality fixes: the test helper's return type, the year-request docstring, the README section.
  • Round 2 (head 5fbd7260): all claims verified, including that the merge of main kept Name the report type in every run_simulation record #1183. One medium bug (50% confidence): a window of more than 4 years can still overrun the batch's 3,600 s limit, and its status then reads running until API v1 drops it after 24 hours. Not fixed here: the status route and the batch's time limit are unchanged by this PR, and the same happened before (November's failures ran 20 years at once). Windows long enough to overrun even at 4 years at once are assigned to a later tracker task. Code-quality fixes: docstrings, the changelog fragment, test helper types.
  • Round 3 (head df076f2c): no bugs. Code-quality fixes in 436d982b: the README now lists every kind of year the rule keeps in one container (places, non-macro scope, labour-supply responses in the baseline), and the contract test helper types its overrides. Left: the executor's two test payload helpers differ in whether they drop unset fields, which does not change what the tests check.

Bugs left, by priority

  • Medium: a batch stopped at its 3,600 s limit keeps reporting running, and its running years and their segments are not cancelled (exists before this PR; long windows belong to a later task).
  • Low: running years are not cancelled when one year fails, and a batch started again from its submit-time record runs every year again (exists before this PR; the cost now falls on the shared segment pool).
  • Low: the limit of 4 is per window, so several US nationwide windows at once can ask for more than the 300 segment-pool containers single-year reports share; capacity and time are not measured yet (the production check after deploy measures them).
  • Low: nothing records the number of years a batch allows at once; max_parallel has no field description of its two meanings; no gateway endpoint test of the stored number; no deployed check runs a split year (the integration window is state/ut).

What the merge waits for

No workflow, image, Terraform or environment-variable change.

🤖 Generated with Claude Code

anth-volk and others added 6 commits December 3, 2026 21:16
… 4 at once

Tests that fail before the change: a plain US nationwide year of a budget
window asks for the geographic split (segmented: true), and a window whose
years split runs at most 4 years at once, also when the gateway stored the
requested 20. State, UK, segmented: false and labour-supply-reform windows
keep one container per year and the requested number at once.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…4 at once

A budget-window year no longer forces segmented: false. It follows the rule
a single-year request does (should_run_segmented_national), and its request
states the outcome, as the gateway's does for a single-year request. A
window whose years split runs at most 4 years at once (max_running_years):
create_initial_batch_state stores that limit, so the gateway's submit-time
record and the batch runner's state hold it, and the runner lowers a seed
that a gateway deployed before this change stored with the requested
number. The stored request keeps the number the caller asked for. State,
UK, segmented: false and labour-supply-reform windows keep one container
per year and the requested number at once.

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

Keeps main's report_type on every budget-window year and drops the forced
segmented: false, so each year follows the split rule a single-year request
follows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The gateway README describes how budget-window years split and the limit of
4 split years running at once. The batch test helper returns the stored
state typed and asserts it exists.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The year-request docstring says which plain nationwide years stay whole,
the progress reporter's docstring counts the per-region-group writes of a
year that splits, and the changelog names segmented: false windows. The
test payload helpers declare their return types.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The section now names all the cases the split rule keeps in one container:
places, non-macro scope and labour-supply responses in the baseline as well
as the reform. The contract test helper types its overrides.

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 21:17
@anth-volk
anth-volk merged commit 3b2d63c into main Dec 3, 2026
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.

Run each year of a US nationwide budget window in segments, at most 4 years at once

1 participant