One shared overpayments handler and store (#1012)
Status: Done (2026-09-27) — !1520
Issues: #1012 (epic &67)
Branches: refactor/1012-shared-overpayments (carries this page)
This is a Design + Scope plan. GitLab owns tracking. The plan freezes when U-A starts. One MR, one battery.
Erratum (2026-09-27): the #1820/#1850 QC write-gate sweep (crates/canopy-test-lib/tests/qc_write_gate_sweep_test.rs) walked only services//src, so the three shared write routes would have left it; it now also walks crates//src, and MIN_WRITER_GATED moves from 60 to 58, the tree’s count (55 service routes plus the 3 shared; 64 with the nine per-service copies).
Context
-
Three copies.
-
services/canopy-{snap,tanf,medicaid}/src/store/overpayments.rs(582 lines) are byte-identical, and a test keeps them so (store_module_is_triplicated_byte_identically,crates/canopy-overpayments/src/lib.rs~1038-1054). -
services/canopy-{tanf,medicaid}/src/api/overpayments_handler.rs(245 lines) are byte-identical. snap’s differs in two ways only: the 403 description onlist_claims,get_claimandget_ledger(~81, ~148, ~230), and a#[cfg(test)] mod infra_tests(~247-318).
-
-
The handlers. Six fns on five paths:
list_claims,post_claim,get_claim,post_repayment_plan,post_recoupment,get_ledger.-
Each takes
Extension<Claims>andExtension<PgPool>, neverState, and checks its own role guard (require_service_or_caseworker_or_above/…_writer_or_above,crates/canopy-auth/src/claims.rs~505-531).post_claimrecordsEffectiveUser::resolve(&claims).actor(). -
The module also holds
ListOverpaymentsQuery,LIST_CLAIMS_DEFAULT_LIMIT(200),LIST_CLAIMS_MAX_LIMIT(500), the const-assert againstcanopy_api::pagination::MAX_LIMIT(ADR-001 Amendment 1, §B2) andto_api_error.
-
-
Registration. Each service’s
services/canopy-{snap,tanf,medicaid}/src/api/mod.rsdeclaresmod overpayments_handler;, registers the five paths as literal strings insideroutes(), and lists the six fns in itsApiDocpaths(…). Itsmain.rslayerscanopy_auth::receiver::exchanged_gateoverapi::routes()and layersExtension<PgPool>on the outer router (services/canopy-snap/src/main.rs~515,services/canopy-tanf/src/main.rs~228,services/canopy-medicaid/src/main.rs~1213). -
The store. Runtime
sqlx::query/query_asonly (no compile-time macros, no offline data), so it compiles against no schema. Its row types already live in the crate. The four tables come from the canonical migrations stamped byte-identically into each service; the crate’s parity tests pin four of the five stamped migrations, and the base20260510000001_create_overpayments.sqlcopies are identical today (cmp) but unpinned (see Scope).-
Callers outside the handler: snap’s
services/canopy-snap/src/main.rs(handle_overpayment_assessed,handle_assessment_voided),services/canopy-snap/src/ipv_claim.rs,services/canopy-snap/src/recompute_persist.rs; tanf’s and medicaid’smain.rs.
-
-
Precedent for a shared handler.
canopy_api::admin::admin_replay_handleris apub#[utoipa::path]handler in a crate, named in each service’spaths(…)(crates/canopy-api/src/admin.rs~144-150 records why it must bepub). A genericRouter<S>merged into a service router:test_clock_router(crates/canopy-api/src/lib.rs~241-243). -
Gates checked (grounding). route-authz scans canopy-web only; quality budget B1 counts
services//src/api/.rs; the data-tenancy scan coversservices/*/{src,migrations}; action-coverage keys on each snapshot’s path, verb andoperationId; no compliancetest_refnames a moved file or test. No gate keys on the moved files' paths.
Rulings in force: pre-1.0, no compatibility shims; every jurisdiction without code changes; ADR-001 isolates databases, not code (docs/modules/ROOT/pages/adrs/adr-001-program-service-isolation.adoc ~27-33).
Scope
In scope: D1–D5.
Out of scope (GitLab issues, filed at execution):
-
the data-tenancy scan attributing shared-crate code to its consuming services (it skips
crates/, wherecanopy_common::fti_auditand canopy-mq’s outbox already run service SQL); -
the base
canonical.sqlhas no parity test against the stamped20260510000001_create_overpayments.sqlcopies, and the crate doc’s example names a file that does not exist (crates/canopy-overpayments/src/lib.rs~57).
Design
| # | Decision |
|---|---|
D1 |
Home. The crate gains two public modules:
Both module docs stop saying "SNAP" and say they serve every consuming program service. No feature gate: the crate’s only dependents are four native services that already depend on axum, sqlx, canopy-auth and canopy-api, so a gate would be an axis no consumer turns off. The crate adds |
D2 |
Mounting.
|
D3 |
One 403 description. A shared fn carries one description.
|
D4 |
Callers and surface.
|
D5 |
Tests follow the code.
|
Invariants
-
Every overpayment route, method, path,
operationId, security requirement, guard and response body is unchanged in all three services; only the three read 403 descriptions change. -
Each service’s handlers reach only that service’s database.
-
The shared routes sit inside each service’s
exchanged_gate.
Units — U-A → U-B, one MR
Commit 1: docs(plans): plan the shared overpayments handler and store (#1012). Last: docs(plans): close the shared overpayments plan (#1012).
U-A (w3) — refactor: serve overpayments from one shared handler and store (#1012)
Files:
-
the crate:
crates/canopy-overpayments/Cargo.toml,Cargo.lock;crates/canopy-overpayments/src/lib.rs(pub mod api; pub mod store;, the crate doc’s Architecture section and the per-service store mentions ~21-22 and ~647 describe the shared modules, the pin test deleted); newcrates/canopy-overpayments/src/{api.rs,store.rs,store/infra_tests.rs}; -
the services:
services/canopy-{snap,tanf,medicaid}/src/{api/mod.rs,store/mod.rs,main.rs}, the deleted files in D4, and snap’sservices/canopy-snap/src/{ipv_claim.rs,recompute_persist.rs,store/recomputes.rs}; -
docs/modules/ROOT/openapi/{snap,tanf,medicaid}.json(D3’s strings, edited by hand); -
text naming the old location:
-
crates/canopy-api/src/pagination.rs~12-17 ("the three/v1/overpaymentshandlers … each const-asserting"); -
the
guard:citation strings incrates/canopy-test-lib/src/conformance.rs~343 and ~478 (→crates/canopy-overpayments/src/api.rs:<line>); -
the module docs of
crates/canopy-contracts-{snap,tanf,medicaid}/src/paths.rs; -
the tanf and medicaid router comments, and the "stamped-store orchestration" doc comments (
services/canopy-tanf/src/main.rs~45,services/canopy-medicaid/src/main.rs~985).The stamped migration comments stay: migrations are immutable and parity-pinned.
-
Tests: D5.
U-B (w1) — docs: document the shared overpayments handler and store (#1012)
Files (under docs/modules/ROOT/pages/):
-
api/canopy-snap.adoc,api/canopy-tanf.adoc,api/canopy-medicaid.adoc: the "Overpayment Recovery" intros (routes come fromcanopy_overpayments::api::routes()), and the tanf and medicaid "byte-identical" and "stamped-store" wording; -
api/index.adoc(~75, ~104); -
authorization-inventory.adoc: the per-fn rows citing the per-service handler files, the storevoid_claimrow, and the route rows namingoverpayments_handler::<fn>; -
shared-crates.adoc: the canopy-overpayments section (types, store and handlers shared; data per service); -
adrs/adr-001-program-service-isolation.adoc: one dated note after Amendment 1’s as-built paragraph, since "the three/v1/overpaymentshandlers … each const-asserting" is now one handler with one const-assert; -
CHANGELOG.adoc== Unreleased› Changed.
Verification
-
Per unit, in an isolated worktree: fmt, workspace clippy with the portal features, non-live nextest for canopy-overpayments, snap, tanf, medicaid and reporting; the moved infra tests run filtered by name with
.ports.envsourced (ephemeral schemas only);cargo xtask quality-budgets,cargo machete --with-metadata crates services tools xtask,cargo doc --workspace --no-depswith broken intra-doc links denied. -
CANOPY_CARGO_FEATURES=canopy-api/test-clock cargo xtask dev reload, thencargo xtask api-docs(only D3’s strings differ from main’s snapshots, already committed), thengit push. The battery runsqc_read_only_testin all three services and snap’s overpayment e2e journeys against the shared routes.
Documentation Updates
-
The pages in U-B.
-
GitLab: file the two out-of-scope issues, and amend #1012’s "OpenAPI snapshots stable" criterion to name D3’s three 403 strings.
Review log
| round | vendor | resolved model | verdict |
|---|---|---|---|
1 (2026-09-27) |
Anthropic |
claude-opus-5-5 |
ship — no blockers; eight observations folded in (a stale |