Skip to content

Keep dynamic parameters and their values in their own tables - #4347

Merged
anth-volk merged 13 commits into
masterfrom
v2plan/c1-50-dynamic-parameter-tables
Nov 26, 2026
Merged

anth-volk merged 13 commits into
masterfrom
v2plan/c1-50-dynamic-parameter-tables

Conversation

@anth-volk

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

Copy link
Copy Markdown
Collaborator

Fixes #4346

Builds on #4345 (the UK gov.dynamic. prefix and the one-time split of stored policy copies), which merged into master as 1cf1d1ac7; this branch contains it and targets master.

Summary

Two tables.

  • dynamic_parameters holds each catalog version's model assumptions (gov.simulation., US simulation., UK gov.dynamic.), with parameters' columns, catalog IDs and unique name per version.

  • dynamic_parameter_values holds:

    • their current-law values (no dynamic_id);
    • every dynamic's values (a nullable dynamic_id, CASCADE);

    with no policy_id. Like parameter_values, it has the period check, the period index, and two partial unique indexes: one current-law value per parameter and start date, and one value per dynamic, parameter and start date.

  • parameter_nodes stays shared.

Revision 7f7335f0eb31 (parent d417fe8b1b17, master's head) is autogenerated for the tables. Its one hand-written data step is the explicit human migration decision the skill's "Mandatory generation rule" requires: the user's decision C1 · 46, Q2 (2026-11-25). In the one transaction that migrations/v2/env.py runs every pending revision in, the step:

  1. locks every stored parameter it moves, and raises, changing nothing, while a parameter_values row with a policy_id names one (deleting the parameter would delete the value and silently change the policy), or while a dynamic's value names a parameter it doesn't move (the new tables can't hold it);
  2. copies every version's model assumptions, their current-law values and every dynamic's values into the new tables, with their IDs and creation times, so stored dynamics' content hashes stay valid;
  3. lowers the policy tree's parent counts and deletes the tree rows of gov.simulation, US simulation and UK gov.dynamic and everything below them;
  4. deletes the moved values and parameters, and checks that it deleted what it copied.

More on the revision:

  • Prefixes: it keeps its own frozen copy of the prefixes, with a unit test that it equals DYNAMIC_PARAMETER_PREFIXES.
  • Downgrade: copies both tables back and drops them. It leaves the trees as the upgrade left them (decision 1, the user, 2026-11-25).
  • Read-only check: the new step "Qualify the pending dynamic parameter move" in seed-v2-database.yml (scripts/qualify_v2_dynamic_parameter_move.py) counts the same policy values before the upgrade, so a deploy stops with the count before Alembic starts.

Publication:

  • writes, compares both ways, restores and prunes both tables;
  • analyzes nine catalog tables instead of seven;
  • counts the two kinds apart in its evidence (entity_counts, and dynamic_parameter_values in restored_catalog_versions and in PrunedCatalogVersion);
  • builds the policy tree without model assumptions or their folders;
  • writes the parameter search file in format 2, which lists none of them. A stored file of the current format can't change, so the format moved too. Versions stored before keep their format 1 files, which the previous release serves.

Readers and writers:

  • Dynamics resolve, store, read and compare their values in the new tables.
  • /v2/policies names in its 422 dynamic_parameters the IDs it finds in dynamic_parameters.
  • /v2/dynamics names in its 422 policy_parameters the IDs it finds in parameters.
  • The v1-to-v2 copy looks the assumptions up among dynamic parameters.
  • HTTP shapes are unchanged, apart from the search file's 503 message.

Grants:

  • Runtime: reads both tables and inserts dynamics' values.
  • Catalog writer: inserts both, deletes current-law values (pruning) and analyzes both, with no UPDATE.
  • Simulation service and Modal worker: read both, for C1 · 52.
  • Comparison runs: no access.

C1 · 48's split command is removed (decision 3, the user, 2026-11-25). Operator step C1 · 49 runs it before this revision, and afterwards no stored policy can hold a model assumption. The command, its package, its temporary role's grants, V2_POLICY_COPY_SPLIT_DATABASE_URL and its tests go; the Stage 10 runbook keeps a short account.

docs/engineering/skills/alembic-migrations.md gains "API v2 dynamic parameters in their own tables", and the Stage 9 and Stage 10 runbooks follow.

Deploy order

A merge to master releases and runs the revision in staging and production, so this must not merge until both of these hold in both environments:

  1. Operator step C1 · 49 has run: no parameter_values row with a policy_id names a model assumption. Otherwise the seed job stops at the new check step.
  2. The simulation service's Refuse a dynamic with no parameter values instead of running it as current law policyengine-sim-api#1122 (C1 · 47) is in production. The current release reads a dynamic's values from parameter_values, so after the move it would read none and run the dynamic as current law, storing that result. With Update PolicyEngine CANADA to 0.87.1 #1122 it refuses a dynamic without values (400 dynamic_without_values, which API v1 answers as 503) until C1 · 52 reads the new tables.

After it: C1 · 52 (the simulation service reads the new tables), then C1 · 53 (drops parameter_values.dynamic_id).

The deploy window is an accepted exception (decision 2, the user, 2026-11-25). It is the second exception to "the serving revision must work with the database changed on its own", after catalog pruning's first release, and this PR's review is the maintainers' approval. From the seed job's upgrade until the new release is promoted (or a later release succeeds), the previous API v1 release serves on the moved tables:

  • GET /v2/dynamics/{id} answers 200 with no values.
  • POST /v2/dynamics answers the generic 422.
  • POST /v2/policies answers a model assumption with the generic 422 instead of the named one.
  • The dual-write v1-to-v2 copy fails, with the logged reason missing_parameters and the v1 response unchanged, for v1 policies that set model assumptions; such a policy gets no v2 copy until it is written again.

Nothing in production calls these v2 routes yet.

Environment variables of the new step:

  • V2_MIGRATION_DATABASE_URL: a GitHub Actions environment secret in staging and production, passed by push.yml's secrets: inherit and the step's env:.
  • V2_SUPABASE_PROJECT_REF and V2_SUPABASE_ENVIRONMENT: environment variables, set in the job's env:.

These are the same as the three existing check steps.

Testing

  • Every added or changed test is run by node ID, including the Postgres tests for:
    • the revision: the move, both refusals, a policy value committed while the upgrade waits for its lock (refused; without the lock it would be deleted), other countries' look-alike prefixes, the downgrade, the re-upgrade, the constraints and the check step's count;
    • publication, pruning and restore of both tables as the catalog writer, a version stored before catalog format 2, and a downgrade round trip republished with the exact comparison;
    • dynamics and the copy as the runtime role.
  • alembic -c alembic-v2.ini check finds no drift on a fresh database. reconcile_v2_privileges.py apply --create-missing-roles and verify pass.
  • lint-imports, python -m scripts.rules validate, scripts/run_quality_guards.py, ruff, ruff format and mypy pass. mypy on the changed data/v2 files reports only the 7 errors models/metadata.py already had.
  • scripts/export_migration_contracts.py changes nothing.
  • Two read-only adversarial review rounds: round 1's bug (the check step failed on a database without tables) and its quality findings are fixed test-first; round 2 found no bug, and its quality findings are fixed.

Inline suppression added: # noqa: BLE001 on main()'s except Exception in dynamic_parameter_move_qualification.py. The command must print safe evidence for any failure, as the three existing check commands do.

🤖 Generated with Claude Code

anth-volk and others added 7 commits November 26, 2026 01:12
…r C1 · 50

C1 · 48's scripts/split_v2_policy_copies.py split each stored v2 policy that
held model assumptions into a policy and a dynamic, and C1 · 49 runs it in
staging and production before C1 · 50's revision. That revision moves dynamic
parameters into their own tables and refuses to run while any policy value
names one, so afterwards no stored policy can hold a model assumption, and
the command's dynamics could no longer be stored either. The user decided on
2026-11-25 that C1 · 50 removes it.

Removed: the policy_copy_split package, the script, its temporary role's
privileges, V2_POLICY_COPY_SPLIT_DATABASE_URL and its loader, its import
rules, its integration workflow entry and its tests. The stage-10 runbook
keeps a short account of what the command did. The UK gov.dynamic. prefix
and C1 · 48's other changes stay.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Failing tests for tracker task C1 · 50 (API v1):

- Revision 7f7335f0eb31 creates dynamic_parameters and
  dynamic_parameter_values, then moves every stored version's dynamic
  parameters (US gov.simulation. and simulation., UK gov.simulation. and
  gov.dynamic.), their current-law values and every dynamic's values there
  with their IDs, deletes the policy tree's rows under those folders and
  lowers their parents' child counts, and deletes the originals. It refuses
  while a policy value names a parameter it would move, keeps its own copy of
  the prefixes, and its downgrade moves the rows back and leaves the trees
  (the user's decisions: C1 · 46, Q2, and C1 · 50's decision 1).
- A read-only step in seed-v2-database.yml counts the same policy values and
  stops the deploy first.
- Publication writes, compares, restores and prunes both tables, and leaves
  model assumptions and their folders out of the policy tree and the search
  file, whose format becomes 2.
- Dynamics, the v1-to-v2 copy and the 422s on /v2/policies and /v2/dynamics
  read the new tables, and the grants cover them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
API v1 now stores model assumptions (gov.simulation., US simulation. and UK
gov.dynamic.) in dynamic_parameters and their current-law values and every
dynamic's values in dynamic_parameter_values (tracker task C1 · 50):

- Revision 7f7335f0eb31 creates the two tables (generated) and, in its
  hand-written data step (the user's migration decision, C1 · 46, Q2), locks
  and moves every stored version's rows with their IDs, refuses while a
  policy value names a moved parameter, deletes the policy tree's rows under
  the dynamic folders after lowering their parents' child counts, and checks
  it deleted what it copied. Its downgrade moves the rows back and leaves the
  trees (decision 1). It keeps its own copy of the prefixes.
- seed-v2-database.yml's new read-only step counts the same policy values
  before the upgrade (scripts/qualify_v2_dynamic_parameter_move.py).
- Publication stages each parameter's kind, inserts, compares, restores and
  prunes both tables, analyzes them, counts them apart in its evidence, and
  builds the policy tree and the search file (now format 2) without model
  assumptions or their folders.
- Dynamics read and write the new tables; /v2/policies and /v2/dynamics name
  the IDs they find in the other table in their 422s; the v1-to-v2 copy looks
  the assumptions up among dynamic parameters.
- Every role has an entry for both tables.
- alembic-migrations.md documents the revision, the decisions, the deploy
  order and the deploy window as the second accepted exception (decision 2);
  the stage 9 and stage 10 runbooks follow.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- The read-only check before revision 7f7335f0eb31 counts no value on a
  target without the catalog tables, as the other check steps do, so a new
  or reset database can still be upgraded and seeded.
- The catalog writer holds no UPDATE on dynamic_parameters: publication only
  inserts it.
- The revision tests' fixture deletes its rows before upgrading back to the
  head, so a failed refusal test leaves neither rows nor a downgraded
  database; the tree test that could not fail is removed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- The read-only check before revision 7f7335f0eb31 counts nothing on a
  database lacking any of the catalog tables it reads, as the other check
  steps do, so a new or reset database can still be upgraded and seeded.
- The catalog writer has no UPDATE on dynamic_parameters, which publication
  only inserts.
- The search file's 503 message and its route description say that only
  versions published since the current format have a file; the migration
  guide and the stage 9 runbook say that older kept versions get one only
  when a release installs them again; mirror_legacy_policy_in_session's
  docstring no longer says a stored copy keeps its assumptions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- The revision refuses while a dynamic's value names a parameter it doesn't
  move, which the new tables couldn't hold (failing).
- A policy value written while the upgrade waits for its lock is counted and
  refused: without the lock the upgrade would delete it with its parameter.
- The seed workflow's check counts no policy value on another country's
  look-alike prefix.
- A version stored before catalog format 2 compares its dynamic parameters
  without format 2's fields; clear_format_2_fields clears their flags too.
- The copy test named for parameters of the same name says what it checks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Revision 7f7335f0eb31 also refuses, changing nothing, while a dynamic's
  value names a parameter it doesn't move: dynamic_parameter_values can't
  hold it, and after the move no reader would find it.
- ParameterTreeChild's docstring says model assumptions and their folders
  have no tree rows; the migration guide names the publication modules that
  write both tables and says the check step classifies with the package's
  prefixes, which a test keeps equal to the revision's.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
anth-volk and others added 2 commits November 26, 2026 17:26
…' into v2plan/c1-50-dynamic-parameter-tables
…ables

The merge of C1 · 48's branch brought C1 · 45's household calculation route
tests, whose catalog stored model assumptions in parameters and a dynamic's
values in parameter_values. Since C1 · 50 they are dynamic_parameters and
dynamic_parameter_values rows, as catalog publication and the dynamics
service store them, so the missing-assumption check reads them there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…' into v2plan/c1-50-dynamic-parameter-tables

# Conflicts:
#	policyengine_api/services/v2/policy_copy_split/database_connectors/reads.py
#	policyengine_api/services/v2/policy_copy_split/services.py
#	tests/integration/test_v2_policy_copy_splits.py
#	tests/unit/v2/test_policy_copy_split_rules.py
Base automatically changed from v2plan/c1-48-split-policy-copies to master November 26, 2026 14:01
…bles

CI found seven existing tests still expecting publication's shapes from
before the dynamic parameter tables: the evidence's entity counts (now with
dynamic_parameters and dynamic_parameter_values), the restored versions'
tuples (now with a second count), the nine analyzed tables, the comparison
pairs (now with dynamic parameters and their values), and the staged
parameter rows (now ending with is_dynamic).

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

codecov Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.84211% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.86%. Comparing base (1cf1d1a) to head (a1c89ad).
⚠️ Report is 15 commits behind head on master.

Files with missing lines Patch % Lines
...pi/data/v2/dynamic_parameter_move_qualification.py 96.82% 1 Missing and 1 partial ⚠️
policyengine_api/data/v2/catalog/publication.py 90.90% 0 Missing and 1 partial ⚠️
..._api/data/v2/catalog/publication_reconciliation.py 90.00% 0 Missing and 1 partial ⚠️
policyengine_api/services/v2/dynamics/services.py 83.33% 0 Missing and 1 partial ⚠️
...engine_api/services/v2/dynamics/transformations.py 66.66% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4347      +/-   ##
==========================================
- Coverage   93.94%   93.86%   -0.08%     
==========================================
  Files         434      428       -6     
  Lines       24291    24023     -268     
  Branches     3767     3719      -48     
==========================================
- Hits        22820    22550     -270     
- Misses        845      853       +8     
+ Partials      626      620       -6     
Flag Coverage Δ
integration 59.06% <61.05%> (-0.62%) ⬇️
unit 91.03% <88.94%> (+0.48%) ⬆️

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.

…nsupported country

Codecov's first report on #4347 listed these as uncovered changed lines: the
dynamic parameter move check printing its evidence, asking Alembic about
revision 7f7335f0eb31, and publication refusing to classify a country with
no model assumption prefixes.

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 20:44
@anth-volk
anth-volk merged commit 806b1ec into master Nov 26, 2026
23 checks passed
@anth-volk
anth-volk deleted the v2plan/c1-50-dynamic-parameter-tables branch November 26, 2026 20:45
anth-volk added a commit that referenced this pull request Nov 26, 2026
anth-volk added a commit that referenced this pull request Nov 26, 2026
…arameter_values dynamic_id drop branch

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
anth-volk added a commit to PolicyEngine/policyengine-sim-api that referenced this pull request Nov 26, 2026
`public-api-commit` moves from `825a9b3c9` (C1 · 41's merge) to `806b1ec9a`,
PolicyEngine/policyengine-api#4347's merge into `master`, whose revision
`7f7335f0eb31` creates `dynamic_parameters` and `dynamic_parameter_values` and
grants the service's role SELECT on both. The `Public API schema` job builds
the tables at this commit, so the tests of the new tables run against them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
anth-volk added a commit that referenced this pull request Nov 27, 2026
C1 · 50 (#4347) merged without leaving dynamic_id out of ParameterValue's
mapper, so the skill section's deploy order names C1 · 51a, which the user
added to do it in its own release before this revision runs.

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.

Keep dynamic parameters and their values in their own tables

1 participant