Skip to content

Take a dynamic on household calculations and household reports - #4343

Merged
anth-volk merged 6 commits into
masterfrom
v2plan/c1-45-household-dynamics
Nov 26, 2026
Merged

anth-volk merged 6 commits into
masterfrom
v2plan/c1-45-household-dynamics

Conversation

@anth-volk

@anth-volk anth-volk commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #4342

Summary

/v2/household-calculations and household reports take a dynamic (tracker task C1 · 45).

  • Requests. /v2/household-calculations (HouseholdCalculationInput) and each household report side (HouseholdPolicyChoice) accept an optional dynamic_id.

  • Checks. A named dynamic is checked with the economy path's own code, simulation_service.services.require_runnable_dynamics, through the same dynamic service as /v2/dynamics:

    • 404 when the request's country doesn't store it;
    • 422 missing_parameters when it sets parameters the running catalog version lacks.

    Policies are checked first, then dynamics, baseline first. A failed dynamic read answers 503, as for policies.

  • Both sides. Each household report side keeps its own dynamic, and nothing requires the two to match, as for economy reports. The front end sends one dynamic on both sides.

  • Requests to the simulation service. A named dynamic is sent; a null one is left out of the body. The simulation service applies the dynamic (Household calculations and reports apply a dynamic, and every simulation refuses another country's dynamic policyengine-sim-api#1113).

  • Its refusals.

    • Household reports already go through the report client, where 400 unknown_dynamic answers 422.
    • The household calculation client now answers a 400 whose code is unknown_dynamic the same way (422, with the service's code and reason). It used to answer every 400 as 503.
    • The 422s located at dynamic_id (missing parameters, or a parameter both the policy and the dynamic set) pass through as 422.
  • Re-runs. A household report re-run still sends only the report, so the simulation service reads and applies each side's stored dynamic. Dynamics aren't checked again on a re-run, as for economy reports.

  • Responses. Household calculation items (submitted and read) gain dynamic_id, read from household_jobs.dynamic_id. Household report items already give each side's dynamic_id.

  • Structure. V2HouseholdCalculationService takes its countries, households, policies and dynamics sources as one HouseholdCalculationSources value; six keyword arguments would fail ruff's PLR0913.

  • No revision, grant, workflow or environment variable changes. household_jobs.dynamic_id and simulations.dynamic_id already exist.

Migration notes:

  • Route groups: household (/v2/household-calculations) and report (/v2/reports, household kind).
  • User-visible contract changes: the new optional dynamic_id request fields, and the required dynamic_id in household calculation responses. The migration contract documents are regenerated.
  • Unchanged: nothing moves off the Flask/API v1 path.

Deploy order

This PR merges after PolicyEngine/policyengine-sim-api#1113, once that sim release is deployed to production (the tracker order also puts C1 · 44 first). The simulation service's request models refuse unknown fields:

  • This release before sim's Alter CPU deployment #1113:
    • Requests without a dynamic are unchanged, because a null dynamic_id is left out, so they work.
    • A request naming a dynamic passes API v1's checks, then the simulation service refuses the unknown field with 422. API v1 answers that 422 with the service's reason, and nothing is created.
  • Sim's Alter CPU deployment #1113 before this release: API v1's current request models refuse dynamic_id themselves, so the simulation service never receives one. API v1 ignores the new dynamic_id in sim's job answers.

No caller sends a dynamic yet: the front end's household pages will send one only from a later task.

Overlap with other API v1 pull requests

#4329, #4333, #4335 and #4337, which touched some of the same files, have all merged. This branch merged master (888ae6118) at e063231fd with no conflict. The migration contract documents regenerate unchanged, and this PR's 145 unit and 18 PostgreSQL cases still pass by node ID.

Testing

  • Tests first: the failing tests were committed before each change.
  • Run by node ID, each passing: the 145 unit cases and 18 PostgreSQL 17 integration cases this PR adds or changes. The integration cases run as API v1's runtime role, which shows its grants cover reading each named dynamic and household_jobs.dynamic_id.
  • Checks: make quality (ruff, ruff format, Import Linter, mypy, quality guards), python -m scripts.rules validate, alembic -c alembic-v2.ini check (no new operations) and python scripts/export_migration_contracts.py.
  • Review: one adversarial review round found no bugs. Its documentation and coverage findings are fixed, test-first.

🤖 Generated with Claude Code

anth-volk and others added 5 commits November 25, 2026 00:23
…s (failing)

/v2/household-calculations and household reports are to accept a
dynamic_id, check it in the request's country through the economy path's
dynamic check, send it to the simulation service only when named, and map
the simulation service's unknown_dynamic refusal as the economy path does.
These tests fail until that change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
/v2/household-calculations and each side of a household report on
/v2/reports accept an optional dynamic_id. It is checked as an economy
simulation's dynamic is, through require_runnable_dynamics and the same
DynamicSource as /v2/dynamics: 404 when the request's country does not
store it, and 422 missing_parameters when it sets parameters the running
catalog version lacks, after the policy checks. Each household report side
keeps its own dynamic, as economy reports do.

API v1 sends a named dynamic to the simulation service and leaves a null
one out of the request body, so the simulation service's release before it
took a household dynamic still accepts every request without one. The
household calculation client answers the simulation service's 400
unknown_dynamic as the caller's error (422), as the report client already
does. Household calculations answer the dynamic_id they apply, read from
household_jobs.dynamic_id. Household report re-runs still send only the
report, so the simulation service keeps each side's stored dynamic.

The household calculation service now takes its countries, households,
policies and dynamics sources as one HouseholdCalculationSources value,
and the test helpers that build it change with it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…country's dynamic (failing)

From review round 1: the report create route's 422 description should
name the simulation service's 400 for a dynamic, and the household
calculation routes' 404 description should not say a calculation is read
in a country. The database-backed household calculation route also
refuses a dynamic stored for another country, and the household report
re-run test says what it checks.

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

The report create route's 422 description names the simulation service's
400 for a region, dynamic or dataset. The household calculation routes'
404 description no longer says a calculation is read in a country. The
household calculation route module and the resource service protocol name
the dynamic and its refusals.

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

codecov Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.29730% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 93.83%. Comparing base (888ae61) to head (e063231).
⚠️ Report is 7 commits behind head on master.

Files with missing lines Patch % Lines
...ine_api/services/v2/simulation_service/services.py 66.66% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4343      +/-   ##
==========================================
- Coverage   93.84%   93.83%   -0.01%     
==========================================
  Files         426      426              
  Lines       23839    23866      +27     
  Branches     3696     3703       +7     
==========================================
+ Hits        22372    22395      +23     
- Misses        848      852       +4     
  Partials      619      619              
Flag Coverage Δ
integration 59.03% <51.35%> (+<0.01%) ⬆️
unit 90.99% <97.29%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

…ehold dynamics branch

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@anth-volk
anth-volk marked this pull request as ready for review November 26, 2026 12:55
@anth-volk
anth-volk merged commit 7bc622d into master Nov 26, 2026
24 checks passed
@anth-volk
anth-volk deleted the v2plan/c1-45-household-dynamics branch November 26, 2026 12:55
anth-volk added a commit that referenced this pull request Nov 26, 2026
Brings C1 · 45 (#4343, dynamics on household calculations) and the
release version bump (9ed45bc).

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

This branch was previously deployed

1 inactive deployment
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.

/v2/household-calculations and household reports take a dynamic

1 participant