Skip to content

Household calculations and reports apply a dynamic, and every simulation refuses another country's dynamic - #1113

Merged
anth-volk merged 8 commits into
mainfrom
v2plan/c1-44-household-dynamics
Nov 25, 2026
Merged

anth-volk merged 8 commits into
mainfrom
v2plan/c1-44-household-dynamics

Conversation

@anth-volk

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

Copy link
Copy Markdown
Contributor

Fixes #1112

Household calculations and household reports now apply a dynamic, as economy simulations do. Every simulation now refuses a dynamic stored for another country, using dynamics.country_id from API v1's revision 9d1d12a7ac58 (PolicyEngine/policyengine-api#4325). The pin move and the insert_dynamic change this PR first made are now on main, from #1096.

What changes

Persistence library

Simulation Entrypoint

  • POST /household-calculations takes dynamic_id, and GET /household-calculations/{id} returns it.
  • Each household report side (HouseholdPolicyChoice) takes dynamic_id. A stored household report whose side names a dynamic is now re-run with it, where it used to be refused with 409.
  • New refusals:
    • 400 unknown_dynamic, also for a dynamic of another country;
    • 422 missing_parameters at dynamic_id, for a dynamic setting parameters the catalog version lacks;
    • 422 overlapping_parameters at dynamic_id, for a dynamic setting a parameter the policy also sets.
  • A value the household contract refuses is located at dynamic_id when the dynamic sets its parameter.
  • Economy requests and household reports build these refusals with one function, simulation_values_refusal. The unknown_dynamic detail now names the country.

The v1 run_simulation code imports nothing this adds. API v1 sends no household dynamic_id until C1 · 45.

Tests

  • Red first: 1717adda committed the failing tests. Then 29 of 50 persistence cases and 40 of 54 entry cases failed, for the expected reasons.
  • On the final head, every added or changed test passes by node ID: 57 persistence cases, 95 entry cases and 4 generated-client cases.
  • The persistence cases include the comparisons with API v1's tables. They ran against disposable PostgreSQL 17 databases that API v1's chain built, with its privilege manifest applied: at 003ee4996 before the merges of main, then at each pin main brought in: d2b5dc31 and now 825a9b3c (revision 4e30076d4b9f). At 825a9b3c, 61 persistence cases (the six comparisons included), 95 entry cases and 4 generated-client cases pass.
  • The generated-client cases ran against a client built from this branch by scripts/generate-clients.sh.
  • Ruff, Pyright (persistence, entry, apis-integ) and the quality guards pass.
  • On unchanged main, all five comparisons also pass at 003ee4996. Between 63cce8273 and 003ee4996, API v1's tables changed only in dynamics and in tables sim doesn't model.

Deploy order

Production must have API v1's revision 9d1d12a7ac58 before this releases. As of 2026-11-24 it does (below).

  • With this change, sim reads dynamics.country_id whenever a request, or a stored record being re-run, names a dynamic.
  • Without that column, such a request fails with a database error, answered 503 database_unavailable, where it is answered 400 today. Requests without a dynamic don't read dynamics, and no readiness check reads the column.
  • A merge to main releases and deploys by itself, so this PR should merge only after API v1's production release containing #4325.
  • Order: API v1's release with #4325 in production, then this PR, then C1 · 45 (API v1 sends household dynamics).

Read from GitHub only (no database read):

  • 2026-11-20: API v1's release commit 80a42ddeb had staged the revision, but its Push run then failed before any production job.
  • 2026-11-25: API v1's release commit 5d9b1eee8, which contains 003ee4996 and d2b5dc31, finished its Push run on 2026-11-24, including Seed production v2 database / Upgrade schema and seed v2 database and Deploy production Cloud Run candidate. So production's v2 database has been upgraded past 9d1d12a7ac58.

Grants

From API v1's privileges.py at 003ee4996, no new grant is needed:

  • dynamics: table-level SELECT for the simulation service role and the Modal worker role.
  • household_jobs: SELECT, INSERT for the service role, so it can insert dynamic_id, plus UPDATE on the call-start columns.
  • simulations, parameter_values, parameters, policies, households and the catalog: unchanged.
  • Unchanged since, through 825a9b3c and C1 · 43's merge 9bcda036 (Let users save named dynamics through /v2/user-dynamics policyengine-api#4337), which adds only the user_dynamics table and its grants.

The persistence tests ran as policyengine_v2_simulation_service on a database with those grants applied. Sim's readiness list (SERVICE_TABLE_PRIVILEGES) already requires SELECT on dynamics.

Pin order and overlap

Review

  • Round 1 found two existing tests that the change broke, and both are fixed:
    • a route case that still expected a side's dynamic_id to be refused;
    • a generated-client test that read a job state without the now-required dynamic_id.
  • Round 2 found no bugs. Its quality findings are fixed: the shared refusal function, SimulationReform passed through, and documentation.
  • After the merge of main at d2328526, the changelog fragment dropped its sentence naming the pin at 003ee4996, since main now moves the pin (f7161f1f). A review of that edit found no bugs; its one quality finding, that no fragment then said sim reads dynamics.country_id, is fixed in aee0634c.

🤖 Generated with Claude Code

anth-volk and others added 4 commits November 20, 2026 01:15
… country (failing)

Household jobs take and store a dynamic_id, household reports and their
re-runs take one per side, and each merges the dynamic's values into the
household reform, as economy simulations do. Every simulation refuses a
dynamic stored for another country. The insert_dynamic test helper writes
the columns API v1's revision 9d1d12a7ac58 requires.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ther country's

Household jobs take a dynamic_id, store it in household_jobs.dynamic_id and
return it in the job's state; household reports and their re-runs take one
per side. Each reads the policy's and the dynamic's values together with the
new read_simulation_reform, as economy simulations now do too, so a
parameter both set is refused. The household report key already names each
side's dynamic, and a stored household report naming one is now re-run with
it instead of refused.

The Dynamic model gains country_id, and read_dynamic_reform refuses a
dynamic stored for another country as UnknownDynamicError, which every
route answers as unknown_dynamic. A value the household contract refuses is
located at dynamic_id when the dynamic sets its parameter.

public-api-commit moves to 003ee4996, API v1's merge of PolicyEngine/policyengine-api#4325, whose
revision 9d1d12a7ac58 gives dynamics their country.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…, and require dynamic_parameters

Review round 1 found two existing tests that the change broke: a route case
still expected a household report side's dynamic_id to be refused, and the
generated-client test read a household job state without the now-required
dynamic_id. Both now expect the new shapes, and new route and generated-client
tests send each side's dynamic and read it back.

The inputs that carry which parameters a dynamic sets now require them, so
no caller can leave them out and have a refused dynamic value located at
policy_id. The README paragraphs this change edited are rewrapped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…h its dynamic's parameters

Review round 2 found no bugs. Its quality findings:
- Economy requests and household reports now build the refusal of a
  simulation's policy or dynamic with one function,
  simulation_values_refusal, so each says the same thing and locates it the
  same way. The household calculation route keeps its own refusals, whose
  details and error types differ.
- The household inputs carry each side's SimulationReform, the combined
  values with the parameters its dynamic sets, instead of two parallel
  fields.
- The entry README's re-run refusals and the CallerRowRefusalCode comment
  name the dynamic refusals, and two README paragraphs are rewrapped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
#1096 moved public-api-commit to d2b5dc31, API v1's merge of
PolicyEngine/policyengine-api#4327, which contains 003ee4996, and rewrote
insert_dynamic to write the same columns. The conflicts keep main's pin and
helper, and this branch's Dynamic.country_id, with the model's docstring
saying that sim reads the country.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@anth-volk anth-volk changed the title Household calculations and reports apply a dynamic; every simulation refuses another country's; pin API v1 at 003ee4996 Household calculations and reports apply a dynamic, and every simulation refuses another country's dynamic Nov 24, 2026
anth-volk and others added 3 commits November 25, 2026 23:39
main now pins API v1's merge of the report column drops (825a9b3c), and
its table models drop the columns C1 · 41 drops. Nothing conflicted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
#1096 and #1111 moved public-api-commit on main, so this PR no longer
compares the models at API v1's 003ee4996.

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 November 25, 2026 20:16
@anth-volk
anth-volk merged commit 2a3bd7d into main Nov 25, 2026
30 checks passed
@anth-volk
anth-volk deleted the v2plan/c1-44-household-dynamics branch November 25, 2026 20:16
anth-volk added a commit that referenced this pull request Nov 26, 2026
…namic parameter tables branch

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.

Household calculations and household reports apply a dynamic, and every simulation refuses another country's dynamic

1 participant