Skip to content

Protect verified v1 household results - #4029

Draft
MaxGhenis wants to merge 9 commits into
masterfrom
max/legacy-user-route-authz-20261023
Draft

MaxGhenis wants to merge 9 commits into
masterfrom
max/legacy-user-route-authz-20261023

Conversation

@MaxGhenis

@MaxGhenis MaxGhenis commented Oct 23, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #4028

A completed v1 household simulation can become final when its output matches a stored-household calculation recorded for the current or a newer runtime. A household report can become final when every linked simulation result matches server evidence for that report's cache version. The v1 migrations persist verification and reserve one row when requests race to create the same current report.

Stale-SPM simulations, results from older runtimes, reports with an active server-started rerun, geography simulations, economy reports, and other outputs without matching server evidence remain correctable. Reading a report preserves its active rerun. A valid US economy completion can replace malformed historical output.

An ordinary PATCH cannot correct a final result. A conflicting update returns 409 and a repeat leaves the saved result and run history unchanged, including when a retry uses the stored JSON number representation. The PATCH routes also validate IDs and reject completions with null output.

For this fix round, the Front door chose server-side verification with the existing PATCH payloads. Calculation keys retain the producer's runtime and cache hits renew its evidence. A rollout between calculation and save cannot give an old result the saving revision's verification. A server-issued receipt returned by callers remains an option for maintainers.

Tests cover rollout in both directions, overlapping MySQL completions through old and current report IDs, MySQL JSON round trips, active reruns, stale SPM, malformed historical outputs, and existing legacy user-route behavior. Local focused tests, Redis and MySQL integration tests, migration checks, and repository quality checks passed.

This update was written by a Codex cloud task for Max and builds on draft PR #4029. No session owns the draft. The merge decision stays with the maintainers.

Updated by a Codex cloud task (queue file 3207-policyengine-api-pr4029-draft-remove-the-v1-user-routes-an).

MaxGhenis and others added 2 commits October 23, 2026 06:48
POST, GET and PUT /{country_id}/user-profile and POST, GET and PUT
/{country_id}/user-policy read and changed user profiles and saved
policies by a user_id, auth0_id or record id taken from the request,
and checked no credential. App v2 does not call them, and #4017 reports
no production requests to them in the 30 days to 2026-10-19. Signed-in
users reach their saved work through the v2 routes, which take the
owner from the verified credential. Found by the 2026-10-22 security
review.

The deletions match #4017's: the user-profile blueprint and v1 user
service go, the saved-policy routes and their helpers leave
policy_routes.py, the app-v2 contract registry drops the saved-policy
workflow and the generated contract documents are refreshed. As there,
the v1 saved-policy service and copying modules stay until dual-write
is deleted. GET /{country_id}/policies stays; #4017 removes it with the
legacy site.

New tests state the invariant that every legacy user record operation
is owner-scoped: the deployed application serves neither path, no
module an HTTP route can import touches the v1 user-record tables, and
a Hypothesis property over generated requests to the removed paths
(any method, country, identifier, ownership fields and credentials)
gets 404 or 405 and leaves every stored user record unchanged. The
Cloud Run candidate check asserts the removed paths answer 404.
hypothesis joins the dev extra.

Tests (failed first on origin/master: the routes were registered and
reachable from the route modules, and requests to them reached the
handlers instead of answering 404): the new invariant tests, then the
route boundary, contract and service-boundary tests.

Fixes #4028

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
PATCH /{country_id}/simulation and PATCH /{country_id}/report take no
credential, and their records are shared: identical requests reuse one
record. Any caller could replace a complete result with another output
or an error, and every viewer of that record saw it. Found by the
2026-10-22 security review.

There is no compute worker to restrict these updates to: the browser
that ran a calculation stores its result. So the rule is on the record.
Once it holds a complete result, an update that repeats it (a retried
write, or a second browser finishing the same calculation, in either
JSON encoding) answers 200 and writes nothing, and one that would
change its status, output or error message answers 409 and leaves the
record and its runs as they were. A completed US economy output is
compared in its canonical SPM form. The services check this after
locking the record and before any write, so the no-op path does not
restamp the version or touch the runs.

Three results stay open, because the server decided they run again:
stale SPM simulation output, a report under an outdated cache version
(reads resolve it to its current equivalent) and either record while a
server-started rerun is pending or running. Marking a complete report
running now answers 409 rather than 400; the old check still applies
to a failed report.

Tests (failed first on origin/master: a forged output and a status
change replaced complete results with 200): regression tests for each
anonymous change, the canonical economy comparison, the calculation
path and the three open cases, and Hypothesis properties over update
sequences for both records, checked step by step against a reference
model. The OpenAPI document gains both 409 responses.

Fixes #4028

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

codecov Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 89.09091% with 42 lines in your changes missing coverage. Please review.
✅ Project coverage is 94.10%. Comparing base (5ed914e) to head (323df37).
⚠️ Report is 62 commits behind head on master.

Files with missing lines Patch % Lines
policyengine_api/services/report_output_service.py 86.66% 9 Missing and 7 partials ⚠️
...icyengine_api/runtime_cache/result_verification.py 83.75% 5 Missing and 8 partials ⚠️
policyengine_api/services/report_output_runs.py 83.33% 2 Missing and 2 partials ⚠️
policyengine_api/routes/report_output_routes.py 87.50% 2 Missing and 1 partial ⚠️
policyengine_api/routes/simulation_routes.py 84.21% 2 Missing and 1 partial ⚠️
policyengine_api/services/simulation_service.py 95.38% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4029      +/-   ##
==========================================
+ Coverage   93.90%   94.10%   +0.20%     
==========================================
  Files         433      451      +18     
  Lines       24216    25530    +1314     
  Branches     3754     3976     +222     
==========================================
+ Hits        22739    24025    +1286     
- Misses        847      855       +8     
- Partials      630      650      +20     
Flag Coverage Δ
integration 62.45% <62.59%> (+3.22%) ⬆️
unit 91.48% <87.01%> (+0.32%) ⬆️

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.

…lity

Two independent reviews of the first head found that the finality rule
broke one app-v2 flow and left three gaps.

App v2 recalculates every simulation of a new household report,
reused complete ones included, and stores the installed model's result.
After a model release that result differs, so it got 409 and the
shared simulation stayed on the older model. A simulation result is now
final only when it came from the installed country model or a newer
one (PEP 440 comparison of api_version; an unreadable version counts as
older when it differs), so recalculation replaces older results as
before, and an older revision cannot replace a newer one. The
speculative simulation-rerun exception goes: nothing starts one.

An update to an outdated report ID now goes to its current equivalent,
the record GET already serves for that ID, and keeps the requested ID
in the response. Before, PATCH wrote the outdated row and the current
record stayed pending; and the outdated-cache-version exception let a
revision with another bundle reopen current reports. That exception is
gone.

A completion whose output decodes to null, such as "null", stored a
complete record with no result, which then refused the real one: it is
rejected, and a complete record without output is not final. A body id
that is not an integer is rejected. Stored outputs are compared as
stored, so a JSON-string output repeats as itself.

The Cloud Run candidate check reads IDs no record can have and never
prints the body, since CI logs are public. uv.lock is relocked with uv
0.13.4, CI's pin, and now only adds hypothesis 6.168.5. The OpenAPI
document and docs/canonical-spm.md describe the rules; the report 409
response has its schema.

Tests: regressions for newer models, outdated report IDs, null
completions, non-integer IDs and JSON-string outputs, which failed first
on origin/master, and for older models, which must pass on both; the properties now compare
every column of the record and its runs after completion, over a wider
output domain; the import walk handles package-relative imports and
models used through v1_models.

Fixes #4028

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@MaxGhenis MaxGhenis changed the title Remove the v1 user routes and keep complete simulation and report results final Protect final v1 simulation and report results Dec 1, 2026
@MaxGhenis MaxGhenis changed the title Protect final v1 simulation and report results Protect verified v1 household results Dec 1, 2026

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.

Remove the v1 user routes and keep complete simulation and report results final

1 participant