Skip to content

Serve dynamic parameters as their own v2 metadata category, US simulation.* included - #4349

Merged
anth-volk merged 15 commits into
masterfrom
v2plan/c1-51-dynamic-parameter-routes
Nov 27, 2026
Merged

anth-volk merged 15 commits into
masterfrom
v2plan/c1-51-dynamic-parameter-routes

Conversation

@anth-volk

Copy link
Copy Markdown
Collaborator

Fixes #4348

Summary

API v1's v2 metadata routes serve dynamic parameters (the model assumptions under gov.simulation., US simulation. and UK gov.dynamic.) and their current-law values apart from policy parameters, US simulation.* included. Tracker task C1 · 51 of the detailed-save API plan.

New routes, all read-only GETs with the metadata routes' caching headers (ETag, Cache-Control, Vary, 304):

Route Query Policy counterpart
/v2/dynamic-parameters ParameterCollectionQuery: page, search, repeated name /v2/parameters
/v2/dynamic-parameters/{parameter_id} CatalogQuery /v2/parameters/{parameter_id}
/v2/dynamic-parameters/children new DynamicParameterChildrenQuery: page, parent_path, settable /v2/parameters/children
/v2/dynamic-parameter-values ParameterValueCollectionQuery: page, parameter_id, repeated parameter_name, current /v2/parameter-values
/v2/dynamic-parameter-values/{value_id} CatalogQuery /v2/parameter-values/{value_id}
/v2/model-catalog/dynamic-parameters CatalogQuery, no names /v2/model-catalog/parameters
  • What they read:
    • Dynamic parameters and their values come only from dynamic_parameters and the current-law rows of dynamic_parameter_values. So a policy parameter's or policy value's ID, a value a dynamic sets, and another version's ID all answer 404.
    • The children route and the catalog route also read the stored labels of the shared parameter_nodes, and the children route reads the version's variables.enum_values.
    • Every read goes through ORM mapped classes and the request's Session.
  • Items are the policy routes' items, MetadataParameterSummary and MetadataCanonicalParameterValue. A value's parameter_id is the dynamic parameter's catalog ID, which POST /v2/dynamics takes.
  • /v2/model-catalog/dynamic-parameters returns the version's whole category in one response for v3 (D1 · 4):
    • every dynamic parameter in name order, with its flags and its current-law values by date;
    • node_labels, the stored label of each ancestor node.
  • /v2/dynamic-parameters/children builds the tree on each request:
    • from the version's dynamic parameters, their ancestor nodes' stored labels and the version's enum labels;
    • with parameter_tree.build_parameter_tree, so the policy tree's label and order rules aren't copied; there is no stored tree table and no search file;
    • its top level is gov, plus simulation for the US.
  • The settable rule is is_dynamic_settable, beside is_reform_changeable:
    • a dynamic parameter whose economy or household flag is true, with no gov. requirement;
    • at policyengine-us 2.2.2 and policyengine-uk 2.174.1 that is all 29 US and 78 UK dynamic parameters;
    • versions stored before catalog format 2 have null flags, so none qualifies.
  • Request logs: the new path segments join the metadata route group and the v2 metadata read segments. model-catalog joins those segments too, so every /v2/model-catalog* route now logs Supabase as its read source. Before, they logged whatever DB_READ_METADATA and DB_WRITE_METADATA select. This has its own fixed changelog fragment.
  • Contracts:
    • the routes are added to metadata_resources_v2_preview in tests/contract/registry.py, with the regenerated docs/generated/migration_contracts.json and docs/engineering/migration-contracts.md;
    • the OpenAPI path tests and docs/migration/stage-9-v2-metadata.md ("Dynamic parameter routes", "Catalog pruning", rollback) list them.
  • CI: v2-integration-check.yml runs the new Postgres module in its metadata step, and the module joins the scope of api.v2.runtime-privileges-tested-as-runtime-role. The step adds no environment variable: it uses the job's env: block, whose literal values point at the job's own service containers, and pr.yml passes no secrets.

Decisions this follows

The user's decisions in C1 · 46, plus three follow-ups made while building:

  • Q3: the routes that serve current-law data call require_unpruned as their policy counterparts do, so a pruned version answers 404 with Cache-Control: no-store, or 503 for the running version. The children route checks when it selects the version. Its tree's rows are never pruned, so a process holding a selection from before the pruning still answers 200, as documented.
  • Q4: separate routes, not a category query parameter. The policy routes are unchanged.
  • Q5: the settable rule above. POST /v2/dynamics still accepts any dynamic parameter.
  • Follow-ups (2026-11-26):
    • no settable field on any item, only the children filter settable=true;
    • /v2/model-catalog/dynamic-parameters is read on each request, with no in-process body cache;
    • the children route reads the version's enum labels on each request, about 458 KB of JSON for the US and 41 KB for the UK.

Stack and deploy order

Files #4347 also changes

  • docs/migration/stage-9-v2-metadata.md;
  • policyengine_api/data/v2/catalog/publication_parameter_tree.py, whose enum_value_labels now calls the shared parameter_tree.enum_labels_by_value.

Testing

  • Red-green: failing tests first (9fcde69f5), then the implementation (683ad7666). Then three adversarial review rounds, each fixed test-first:
    1. Round 1: ruff import order on a fresh cache.
    2. Round 2: /v2/model-catalog* request logs.
    3. Round 3: CI didn't run the Postgres module.
  • 91 tests added or changed, each run by node ID: unit tests on SQLite and fakes, and Postgres tests that include reads as policyengine_v2_runtime.
  • ruff check --no-cache, ruff format --check, mypy (including the changed data/v2/catalog modules), lint-imports, python -m scripts.rules validate, python scripts/run_quality_guards.py, the api.data.orm-only scan and the changelog fragment check all pass.

🤖 Generated with Claude Code

anth-volk and others added 7 commits November 26, 2026 01:49
Tests for the routes that serve dynamic parameters and their current-law
values apart from policy parameters: /v2/dynamic-parameters (with
/{parameter_id} and /children?settable=true), /v2/dynamic-parameter-values
(with /{value_id}) and /v2/model-catalog/dynamic-parameters. They cover the
settable rule (economy or household flag, no gov. requirement), the tree
built on each request with the version's enum labels, 404s for IDs outside
the category, pruned versions, request logs, route groups and OpenAPI.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
API v1's metadata routes list, read and browse dynamic parameters and their
current-law values apart from policy parameters, reading only
dynamic_parameters and dynamic_parameter_values (C1 · 50):

- GET /v2/dynamic-parameters and /{parameter_id}, mirroring /v2/parameters;
- GET /v2/dynamic-parameters/children, built on each request with
  parameter_tree.py's builder, the stored node labels and the version's
  enum labels; settable=true keeps what a dynamic may set;
- GET /v2/dynamic-parameter-values and /{value_id}, current law only;
- GET /v2/model-catalog/dynamic-parameters, the version's whole category
  with values and ancestor node labels, for v3.

A dynamic may set a dynamic parameter whose economy or household flag is
true (is_dynamic_settable, beside is_reform_changeable), so US simulation.*
qualifies. Pruned versions answer as the policy routes do. The routes join
the metadata route group and its request logs, the contract registry and
the generated contract documents, and stage-9-v2-metadata.md describes them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Sort four test modules' imports as ruff does on a fresh cache: the
  failing-tests commit's cache had classed the then-missing
  dynamic_parameters package as third-party, so CI's ruff --fix would have
  changed them.
- Test the children route answering from a selection kept from before a
  pruning, a pruned version's value detail, and another version's IDs of
  the same country, and detach the kept selection's rows in the
  kept-selection test, as a closed request session leaves them.
- List the dynamic routes in the Stage 9 runbook's rollback section.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- The /v2/model-catalog routes, /v2/model-catalog/dynamic-parameters
  included, log Supabase as their read source, as the other v2 metadata
  reads do. They log Cloud SQL today, because model-catalog isn't a v2
  metadata segment.
- Dynamic values are paged, and a value's detail answers a pruned version
  from a selection kept from before the pruning.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- model-catalog joins the v2 metadata segments, so the /v2/model-catalog
  routes, /v2/model-catalog/dynamic-parameters included, log Supabase as
  their read source, with a changelog fragment for the fix.
- The Stage 9 runbook gives the measured size of the children route's enum
  read: about 458 KB of JSON for the US and 41 KB for the UK.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- The cross-database integration workflow runs the dynamic parameter
  routes' Postgres tests, which also read as the runtime role. It doesn't
  yet, so CI never runs them.
- Every dynamic read of a version without dynamic parameters answers with
  empty results.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- The cross-database integration workflow runs the dynamic parameter
  routes' Postgres tests in its metadata step, and the module joins the
  scope of api.v2.runtime-privileges-tested-as-runtime-role, which now names
  the dynamic parameter reads (rules documentation regenerated). The step
  adds no environment variable: it uses the job's env block.
- The routes' docstrings and the Stage 9 runbook say which tables each
  dynamic read uses, and the runbook's pruning list names the children
  route's exception for a selection kept from before a pruning.
- The model-catalog changelog fragment says what the logs recorded before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
anth-volk and others added 2 commits November 26, 2026 18:05
The six dynamic parameter routes join metadata_resources_v2_preview, so the
migration contract payload holds 119 requests, not 113.

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 93.95604% with 11 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.88%. Comparing base (806b1ec) to head (ed1da9c).
⚠️ Report is 17 commits behind head on master.

Files with missing lines Patch % Lines
.../v2/metadata/dynamic_parameters/transformations.py 76.19% 0 Missing and 5 partials ⚠️
...ervices/v2/metadata/dynamic_parameters/services.py 93.87% 0 Missing and 3 partials ⚠️
policyengine_api/data/v2/catalog/parameter_tree.py 90.90% 0 Missing and 1 partial ⚠️
...2/metadata/database_connectors/reads_parameters.py 96.77% 0 Missing and 1 partial ⚠️
...v2/metadata/database_connectors/reads_variables.py 66.66% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4349      +/-   ##
==========================================
- Coverage   93.88%   93.88%   -0.01%     
==========================================
  Files         428      432       +4     
  Lines       24023    24195     +172     
  Branches     3719     3752      +33     
==========================================
+ Hits        22555    22715     +160     
- Misses        847      852       +5     
- Partials      621      628       +7     
Flag Coverage Δ
integration 59.29% <87.36%> (+0.22%) ⬆️
unit 91.08% <93.95%> (+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.

Base automatically changed from v2plan/c1-50-dynamic-parameter-tables to master November 26, 2026 20:45
@anth-volk
anth-volk marked this pull request as ready for review November 27, 2026 13:32
@anth-volk
anth-volk requested a review from a team as a code owner November 27, 2026 13:32
@anth-volk
anth-volk merged commit 935080b into master Nov 27, 2026
23 checks passed
@anth-volk
anth-volk deleted the v2plan/c1-51-dynamic-parameter-routes branch November 27, 2026 13:32
anth-volk added a commit that referenced this pull request Nov 27, 2026
…ues dynamic_id drop branch

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.

Serve dynamic parameters and their current-law values through their own v2 metadata routes

1 participant