Repository navigation
Let users save named dynamics through /v2/user-dynamics - #4337
Merged
Merged
Conversation
Add tests for `user_dynamics`, which holds users' named saved dynamics, built like `user_policies` without its v1 reference: - the service: the owner is the signed-in user or the browser, and nobody else can read, change or delete it; the dynamic must be stored in the saved dynamic's own country; renaming, repointing, paging and filtering by dynamic; the dynamic can't be deleted while saved; the saved dynamic is deleted with its user, moves to the account at sign-in, and appears in the account export; and the function D4 · 1's migration calls; - the /v2/user-dynamics routes, their error mapping and OpenAPI, and the routes' refusal without a credential; - the export's new `dynamics` list in /v2/me/export and the admin saved-items read; - the runtime role's grant, the `policy` route group, the contract registry, and the revision chain with revision d417fe8b1b17; - against Postgres: a revision that adds only `user_dynamics`, its keys, deletion with the user and the downgrade, and the runtime role saving, merging and deleting saved dynamics. They fail because `user_dynamics`, its services and routes, and the revision don't exist yet. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add `user_dynamics`, users' named saved dynamics, built like `user_policies` without its v1 reference, `baseline` and `provenance`: the owner (a signed-in user or a signed-out browser), the country, a dynamic stored in that country, and a name and description. Revision d417fe8b1b17, on 9d1d12a7ac58, creates the table, autogenerated with its ID chosen through --rev-id. The owner's key is ON DELETE CASCADE, and the dynamic's key, with the country, is RESTRICT, so a saved dynamic names a dynamic of its own country and that dynamic can't be deleted while saved. The runtime role reads and writes it, as it does `user_policies`. - services/v2/user_dynamics creates, reads, lists, renames, repoints and deletes the caller's saved dynamics. Another user's answers 404, an unknown dynamic 404 and another country's 400. create_user_dynamic_in_session is the function D4 · 1's migration calls to give a v1 saved policy a saved dynamic. - POST, GET, PATCH and DELETE /v2/user-dynamics, with the routes' service protocol, dependency and error mapping. The saved-item protocols move to fastapi_routes/saved_item_service_protocols.py, which resource_service_protocols re-exports, to keep that file under 800 lines. - The sign-in merge moves a browser's saved dynamics to the account, and /v2/me/export and the admin saved-items read list them in `dynamics`. - `user-dynamics` joins the policy route group. The contract registry gains user_dynamic_associations_v2 and the export's `result.dynamics`, and the contract documents are regenerated. - The revision's integration test runs in the v2 lifecycle CI script. v1's saved-policy copy is unchanged: v1 no longer writes saved policies, so D4 · 1's migration creates saved dynamics for existing ones. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review round 1 found that the request log records /v2/user-dynamics with the policy route group's Cloud SQL database settings, because the logging module's lists of Supabase and saved-work route segments omit `user-dynamics`. Add its GET, POST, PATCH and DELETE to the cases that must log Supabase reads or writes and no simulation flow, and set the policy group's database settings to invalid values there, as the other groups' are, so a case that reads them fails. They fail with migration_flag_error from DB_WRITE_POLICY. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add `user-dynamics` to the logging module's Supabase resources and saved-work routes, as `user-policies` is, so its requests record Supabase reads or writes and no simulation flow instead of the policy route group's configured v1 database settings. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
From review round 1's code-quality findings: - The contract test that requires every saved-item route to accept a signed-out browser's header now checks /v2/user-dynamics too. - The Stage 12A document lists /v2/user-dynamics among the saved-item routes and saved dynamics in the admin saved-items read. - The typing-only imports skill names user_dynamics among the route modules that don't use `from __future__ import annotations`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review round 2 found three existing tests that saved dynamics broke, each seen failing by node ID before this change: - test_openapi_references_explicit_resource_response_schemas lists every native v2 path; it now includes /v2/user-dynamics. - test_migration_contract_payload_summarizes_route_contracts pins the contract counts and names; it now has 20 workflows and 113 requests, with user_dynamic_associations_v2. - The dynamics revision's downgrade-refusal test upgraded to its own revision, which does nothing from a newer head, so the refused downgrade also ran this revision's step, rolled both back and left the newer version recorded. It now steps down to its revision first. The saved-dynamics downgrade test does the same, so a later revision doesn't break it. Round 2's code-quality findings: - The write-size test injects a saved-dynamic service, so its check that no service ran covers /v2/user-dynamics. - The model tests check saved dynamics' country key, module and owner key with the other saved items. - The countries model's docstring and the Stage 12A document count dynamics and saved dynamics. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review round 3 found that the import side-effect test pins the number of v2 tables at 50; `user_dynamics` makes it 51. It failed at that assertion when run by node ID before this change. Round 3's code-quality findings: - The check that user services build no SQL now covers the saved-dynamic service, as rule api.v2.user-services-no-sql says. - The revision CI check requires the saved-dynamics revision test in the Postgres revision test script. - The country-key model test covers `dynamics.country_id`, which the countries model's docstring names. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #4337 +/- ##
==========================================
+ Coverage 93.79% 93.82% +0.02%
==========================================
Files 413 426 +13
Lines 23516 23839 +323
Branches 3661 3696 +35
==========================================
+ Hits 22057 22367 +310
- Misses 842 854 +12
- Partials 617 618 +1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
test_user_contracts_require_a_signed_in_or_signed_out_credential pins how many user-route contract requests take each credential. The five /v2/user-dynamics operations add one of each per header, so there are now 31 with a bearer token and 28 with the unauthenticated-user header. CI's test shard 3 reported it failing, and it failed the same way when run by node ID. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Revision d417fe8b1b17 now sits on 4e30076d4b9f, master's head, instead of 9d1d12a7ac58. Autogenerating it again from 4e30076d4b9f gives the same operations, which its docstring and the migrations skill now say. - The head constant, the revision chain test, this revision's own test, the lifecycle script and its workflow test, and the skill's downgrade-chain paragraph name the new order: d417fe8b1b17, 4e30076d4b9f, bc334f9877b7, 9d1d12a7ac58. - Master now runs every database statement through the ORM (rule api.data.orm-only). test_alembic_v2_user_dynamics.py and the saved-dynamic fixture in test_v2_user_identity_persistence.py move from Core connections and SQL text to the owner's Sessions, mapped classes and add_row; the rule's test listed their 20 findings before the change and none after. - Both sides raised the import test's table count from 50 to 51; it is 52 with cliff_impacts and user_dynamics. - The report column drops test of the deploy's privilege steps upgraded only to 4e30076d4b9f, so apply refused while user_dynamics was declared but absent. It now upgrades to the head before applying, as the deploy does. Each of those tests was seen failing by node ID before the change and passes after it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A downgrade from the head now starts with d417fe8b1b17's, which drops user_dynamics with every saved dynamic. The Stage 9 metadata runbook and the Stage 12A user-login runbook listed the later revisions' downgrades from 4e30076d4b9f's, so they understated what such a downgrade deletes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
anth-volk
marked this pull request as ready for review
November 25, 2026 19:37
This was referenced Nov 25, 2026
This branch was previously deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #4336
Summary
Users save named dynamics, as they save named policies.
user_dynamicsis built likeuser_policieswithout its v1 reference,baselineandprovenance: the owner (a signed-in user or a signed-out browser), the country, a dynamic stored in that country, and a name and description.d417fe8b1b17(parent4e30076d4b9f, Drop the per-user and cliff columns from v2 report rows #4335's revision) createsuser_dynamicsand nothing else. It is autogenerated, with its ID chosen through--rev-idso the tests written first could name it, and has no corrections apart fromruff format. It was generated on Store dynamics like policies, create them through /v2/dynamics, and keep gov.simulation parameters out of policies #4325's9d1d12a7ac58and re-pointed when Store one cliff result per simulation run, and run every database statement through the ORM #4327 and Drop the per-user and cliff columns from v2 report rows #4335 merged first; autogenerating it again from4e30076d4b9fgives the same operations.ON DELETE CASCADE, and the dynamic's key, with the country, isRESTRICT. So a saved dynamic names a dynamic of its own country, and that dynamic can't be deleted while it's saved.(id, country_id)is unique. There's no unique rule on the owner and dynamic, as for saved policies.POST /v2/user-dynamics(201; 404 for an unknown dynamic, 400 for another country's),GET /v2/user-dynamics(filtered bydynamic_id, paged), andGET,PATCH(rename, or repoint to another dynamic of the country) andDELETEon/v2/user-dynamics/{association_id}. They have the same credential and ownership rules as/v2/user-policies, and another user's saved dynamic answers 404.user_dynamics, as it doesuser_policies; no other role has access.GET /v2/me/exportandGET /v2/admin/users/{user_id}/saved-itemslist them in a newdynamicsfield;services.v2.user_dynamics.services.create_user_dynamic_in_sessionto give a v1 saved policy a saved dynamic.fastapi_routes/saved_item_service_protocols.py, whichresource_service_protocolsre-exports, keeping that file under 800 lines.Migration contract:
/v2/user-dynamicsroutes are in thepolicyroute group, with contractuser_dynamic_associations_v2.result.dynamics; nothing else changes.Base and conflicts:
masteras003ee4996, and it now targetsmaster.masteris merged in at019acda77, with Store one cliff result per simulation run, and run every database statement through the ORM #4327, Read report cliffs from simulation runs' cliff results, and take include_cliffs on re-runs #4329, Stop mapping the report columns the next v2 revision drops #4333 and Drop the per-user and cliff columns from v2 report rows #4335, and the revisiond417fe8b1b17sits on Drop the per-user and cliff columns from v2 report rows #4335's4e30076d4b9f.masternow runs every database statement through the ORM (ruleapi.data.orm-only), so this PR's revision test and its saved-dynamic fixture in the identity persistence test use the owner's Sessions and mapped classes. The merge also corrects the import test's table count to 52, and has the report column drops test of the deploy's privilege steps upgrade to the head before applying privileges, as the deploy does.masternow contains.Testing
Tests were written first and committed failing, then the code, then three review rounds whose bugs were each fixed test-first. Each test was run by node ID:
RESTRICTkey, deletion with the user, the sign-in merge and the export;user_dynamics, with its keys, deletion with the user, and the downgrade;lint-imports, mypy,scripts.rules validate,run_quality_guards.py,alembic -c alembic-v2.ini check, andreconcile_v2_privileges.py apply --create-missing-rolesandverify..github/scripts/test_alembic_v2_lifecycle.shnow runs the new revision test.alembic-v2-check.yml'spostgres-lifecyclejob sets the two variables it reads,V2_MIGRATION_DATABASE_URLandV2_ALEMBIC_DISPOSABLE_TEST, as literal values for its Postgres service container.🤖 Generated with Claude Code