Skip to content

Household calculations accept a list of variables to calculate - #4384

Open
anth-volk wants to merge 11 commits into
masterfrom
v2plan/d2-2b-household-calculation-variables
Open

anth-volk wants to merge 11 commits into
masterfrom
v2plan/d2-2b-household-calculation-variables

Conversation

@anth-volk

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

Copy link
Copy Markdown
Collaborator

Fixes #4382

Summary

POST /v2/household-calculations accepts variables, the variables to calculate, so app v3's Household tab (PolicyEngine/policyengine-app-v2#1611) can ask for the roughly 330 variables it reads instead of every variable add_missing_variables: true adds. Tracker task D2 · 2b of "5. Detailed-save API".

  • Request: variables is optional on both request forms, a stored household and a household given in the request. It holds at most 10,000 distinct names of 1 to 255 characters, the limits of the simulation service's request models in Calculate only the variables a household calculation lists policyengine-sim-api#1177. An empty list is accepted; null or an omitted field lists none. A repeated name, a name holding a NUL character, or a list given with add_missing_variables: true is refused with a 422 at variables.
  • Check: after every existing check, each listed name is checked against API v1's running catalog version, the catalog of the .py version API v1 runs, which policy and dynamic parameters are checked against. Names it lacks are refused with a 422, code unknown_variables, naming them sorted in unknown_variables (new V2UnknownVariablesErrorResponse), and nothing is sent. When the running .py version has no published catalog for the country, no names are checked. A catalog that can't be queried answers 503.
  • Sent: the simulation service is sent variables only when the caller gave it, so a request without it is sent exactly as on master. Responses don't return the list; the simulation service keeps it in the job row's request_data.
  • Code: the field and its checks in household_calculations/types.py; require_known_variables and HouseholdCalculationVariablesUnknownError in its validators; read_variable_names, a new read in the metadata variables connector module (the runtime role already reads variables); the check in the household calculation service, which now takes running_policyengine_version as the policy service does; the route's 422; the body sent; docstrings; docs/migration/stage-11-v2-households.md; changelog.d/household-calculation-variables.added.md.

Route groups covered: /v2/household-calculations (FastAPI). Nothing changes on the Flask API v1 routes. User-visible contract change: the new optional request field and the new 422 refusal. No migration, no deployment change, no inline suppressions. It builds on master, not on an earlier task's branch.

It raises the policyengine-api-simulation-client pin from 0.20261201.454 to 0.20261202.458, the release PolicyEngine/policyengine-sim-api#1177's merge published, whose HouseholdCalculationRequest and InlineHouseholdCalculationRequest have variables. On the old pin, send_request_with_body refused before sending any body the generated model would drop, so a request listing variables could not be sent. uv.lock changes only that package.

Tests

New tests/unit/v2/test_household_calculation_variables.py (request limits, the body sent, the check through the route on SQLite-backed services, OpenAPI) and two changed OpenAPI tests in tests/unit/v2/test_household_calculation_routes.py. Each was run individually by node ID:

  • 0564ac15 (tests before the change): 28 of 32 failed on assertions. The other 4 pass before the change as checks that existing behaviour holds: no variables key is sent when none is given, and the route refuses a list with add_missing_variables: true (on master as an unknown field).
  • 77a05735 (the change): all 32 pass.
  • b2552329 then 69443265: a 256-character name refused, failing on its assertion before the limit was set to 255, passing after.
  • cca563ad then 2a2cd274 (review round 1): a name holding a NUL character refused, both forms, failing on assertions before the fix, passing after; the variable check's 503 when the catalog can't be queried, both forms, passing (covers existing behaviour).
  • 1ad1b3cb (after review round 2): an unknown household answered 404 before the variable check, and an empty list sent as given, both forms; passing (cover existing behaviour).
  • ca98ba2f then 3f3c326d (the pin raise): a list of variables and an empty list sent through the generated client, for a stored and an inline household (cases in tests/unit/libs/test_simulation_generated_client.py). On 0.20261201.454 the 4 cases failed with send_request_with_body's ValueError naming variables (raised before the test's assertions); on 0.20261202.458 they pass.

Also run: the commit hook's make quality (ruff, ruff format, the import contracts, mypy on 363 files, scripts/run_quality_guards.py including file length) on every commit; python -m scripts.rules validate; scripts/export_migration_contracts.py (no file changed); alembic -c alembic-v2.ini check on a disposable PostgreSQL 17 container (no new upgrade operations).

Review

Two rounds of adversarial review (policyengine-api-v2plan-d2-2b-household-calculation-variables-review-round<N>-2026-12-02.md in the project's notes folder):

  • Round 1, head 69443265: 1 bug, low: a listed name holding a NUL character reached the catalog query, which PostgreSQL refuses, so the caller got a 503 instead of a refusal. Fixed in 2a2cd274, test first. Also fixed: a test name that overstated what it asserts, the missing 503 test, the migration doc's list of checks (the dynamic check was missing), and the changelog wording.
  • Round 2, head 2a2cd274: no bugs. Its minor findings (tests of the check order and an empty list; docstrings and doc giving every limit) are addressed in 1ad1b3cb, with no change in behaviour.

Bugs left: none in this change. A NUL character in an inline policy parameter name still answers 503, as it does on master; that is #4383.

What the merge waits for

🤖 Generated with Claude Code

anth-volk and others added 7 commits December 2, 2026 00:58
…list

POST /v2/household-calculations is to accept `variables`, a list of the
variables to calculate, for a stored household and for one given in the
request. The tests cover its limits (at most 10,000 distinct names, not
with add_missing_variables true, no empty name), the body sent to the
simulation service (the list only when given), the check of each name
against the running catalog version (a 422 with code unknown_variables
naming the names it lacks, nothing checked when that version has no
published catalog), and the OpenAPI document.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
POST /v2/household-calculations takes `variables`, the variables to
calculate instead of every variable add_missing_variables adds, for a
stored household and for one given in the request. The list holds at most
10,000 distinct names, as the simulation service's household requests do,
and is refused when given with add_missing_variables true. After the
existing checks, each listed name is checked against the running catalog
version, the catalog of the .py version API v1 runs, which policy
parameters are checked against: names it lacks are refused with a 422
whose code is unknown_variables and which names them, sorted. When that
version has no published catalog for the country, no names are checked.
The simulation service is sent the list only when the caller gave one, so
a calculation without it is accepted by every release.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The simulation service's household contract takes variable names of at
most 255 characters, so API v1 should refuse a longer listed name before
sending it, not let the simulation service refuse it.

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

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A listed variable name holding a NUL character is to be refused as the
caller's data, since PostgreSQL text can't hold NUL and the catalog query
would otherwise fail as a storage outage (503). Also cover the variable
check's 503 when the catalog can't be queried, and name the test of a
list given with add_missing_variables true for what it asserts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
PostgreSQL text can't hold NUL, so the catalog query failed on such a
name and the calculation was answered 503, as for a storage outage. The
request model now refuses it with a 422. Also: the migration doc lists the
dynamic check and the variable check's 503 and says API v1, the household
calculation session's docstring says API v1, and the changelog fragment
says what the list does next to add_missing_variables and that names go
unchecked when the running .py version has no published catalog.

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

Tests: an unknown household is answered 404 before its listed variables
are checked, and an empty list is accepted and sent as given, for both
request forms. The request and options docstrings (which the OpenAPI
document shows) and the migration doc now give the per-name limit, the
NUL refusal, the unpublished-catalog exception and what an empty list
does, and the doc names the optional dynamic_id.

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

codecov Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.82540% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.93%. Comparing base (0c58e1c) to head (3f3c326).

Files with missing lines Patch % Lines
...v2/metadata/database_connectors/reads_variables.py 50.00% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master    #4384   +/-   ##
=======================================
  Coverage   93.93%   93.93%           
=======================================
  Files         459      459           
  Lines       26219    26281   +62     
  Branches     4054     4069   +15     
=======================================
+ Hits        24628    24688   +60     
- Misses        905      908    +3     
+ Partials      686      685    -1     
Flag Coverage Δ
integration 60.85% <34.92%> (-0.07%) ⬇️
unit 90.39% <96.82%> (+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.

…bles branch

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
anth-volk and others added 3 commits December 2, 2026 18:10
…rent pin

A stored and an inline household calculation listing variables, and each
with an empty list, are sent through the simulation service's generated
client. On policyengine-api-simulation-client 0.20261201.454, whose
request models have no variables field, send_request_with_body refuses
before sending (ValueError naming variables); they pass once the pin is
raised to the release with the field.

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

policyengine-api-simulation-client 0.20261202.458, published by
PolicyEngine/policyengine-sim-api#1177's release, has `variables` on its
HouseholdCalculationRequest and InlineHouseholdCalculationRequest, so a
household calculation that lists variables is sent through the generated
client instead of being refused before sending.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@anth-volk
anth-volk marked this pull request as ready for review December 2, 2026 16:26

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.

Household calculations accept a list of variables to calculate

1 participant