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 on list_claims, get_claim and get_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> and Extension<PgPool>, never State, and checks its own role guard (require_service_or_caseworker_or_above / …_writer_or_above, crates/canopy-auth/src/claims.rs ~505-531). post_claim records EffectiveUser::resolve(&claims).actor().

    • The module also holds ListOverpaymentsQuery, LIST_CLAIMS_DEFAULT_LIMIT (200), LIST_CLAIMS_MAX_LIMIT (500), the const-assert against canopy_api::pagination::MAX_LIMIT (ADR-001 Amendment 1, §B2) and to_api_error.

  • Registration. Each service’s services/canopy-{snap,tanf,medicaid}/src/api/mod.rs declares mod overpayments_handler;, registers the five paths as literal strings inside routes(), and lists the six fns in its ApiDoc paths(…​). Its main.rs layers canopy_auth::receiver::exchanged_gate over api::routes() and layers Extension<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_as only (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 base 20260510000001_create_overpayments.sql copies 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’s main.rs.

  • Precedent for a shared handler. canopy_api::admin::admin_replay_handler is a pub #[utoipa::path] handler in a crate, named in each service’s paths(…​) (crates/canopy-api/src/admin.rs ~144-150 records why it must be pub). A generic Router<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 covers services/*/{src,migrations}; action-coverage keys on each snapshot’s path, verb and operationId; no compliance test_ref names 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/, where canopy_common::fti_audit and canopy-mq’s outbox already run service SQL);

  • the base canonical.sql has no parity test against the stamped 20260510000001_create_overpayments.sql copies, 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:

  • canopy_overpayments::store (crates/canopy-overpayments/src/store.rs): the store moved as-is, canopy_overpayments:: paths becoming crate::;

  • canopy_overpayments::api (crates/canopy-overpayments/src/api.rs): the six handlers, ListOverpaymentsQuery, the two limits, the const-assert, to_api_error, and a documented pub fn routes<S: Clone + Send + Sync + 'static>() → axum::Router<S> registering the five paths with the same literal strings and methods.

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 axum, canopy-auth, canopy-api, anyhow and tracing as direct workspace dependencies; none is new to the build.

D2

Mounting.

  • Each service’s api::routes() replaces its five literal registrations with .merge(canopy_overpayments::api::routes()) at the same point in the chain, so the routes stay inside exchanged_gate and under /v1.

  • Its ApiDoc paths(…​) names the six handlers by full path, one entry each (canopy_overpayments::api::list_claims, canopy_overpayments::api::post_claim, …); components(schemas(…​)) is unchanged.

  • Extension<PgPool> stays layered by each main.rs, so each service’s handlers see only its own database.

D3

One 403 description. A shared fn carries one description.

  • The three read endpoints document their 403 as Bearer is not service-class or caseworker-or-above, matching the writes' style and the read guard. snap’s "Caller lacks admin / quality_control role" is wrong about the guard, and tanf’s "Insufficient role" says nothing.

  • The committed snapshots docs/modules/ROOT/openapi/{snap,tanf,medicaid}.json change in those three strings each and nowhere else. Path keys, operationIds, tags, schemas and the path-count pins (29, 29, 25) are unchanged.

D4

Callers and surface.

  • Every store call moves to canopy_overpayments::store::….

  • The per-service services/canopy-{snap,tanf,medicaid}/src/store/overpayments.rs and services/canopy-{snap,tanf,medicaid}/src/api/overpayments_handler.rs are deleted, with their pub mod overpayments; and mod overpayments_handler; lines.

  • tanf’s and medicaid’s pub use store::overpayments::append_adjustment; and its comment are deleted (the fn is pub in a library now).

  • snap’s services/canopy-snap/src/store/recomputes.rs intra-doc link super::overpayments::create_claim points at canopy_overpayments::store::create_claim (the pre-push cargo doc denies broken links).

  • The two store info! events change log target from canopy_<svc>::store::overpayments to canopy_overpayments::store; no config or dashboard names the old target (grep).

D5

Tests follow the code.

  • store_module_is_triplicated_byte_identically is deleted: there is one copy. The commit body says so.

  • The six store-only tests in snap’s services/canopy-snap/src/main.rs infra_tests move to crates/canopy-overpayments/src/store/infra_tests.rs (#[cfg(test)] mod infra_tests; in store.rs, the crates/canopy-common/src/chain_verify.rs + crates/canopy-common/src/chain_verify/ layout):

    • assessment_void_and_reack_lookup_converge, claim_provenance_uniques_reject_duplicates, adjustments_recompute_close_reopen_and_flag_over_recovery, void_zeroes_balance_and_is_sticky_and_idempotent, claims_page_drains_with_totals_and_tripwire, claims_page_drains_a_10k_universe_within_budget.

    • They run on EphemeralSchema::new_for_snap(&service_database_url("snap")) (precedent crates/canopy-db/tests/db_test.rs ~405-411): any consumer’s schema carries the stamped canonical tables.

  • snap’s handler test post_claim_records_the_bearers_actor moves to api.rs's #[cfg(test)] mod infra_tests, unchanged in substance; its crate::infra_tests::snap_pg_url() becomes service_database_url("snap").

  • snap’s two subscriber tests (appeal_claim_write_failure_errs_for_nack_retry, subscriber_minted_claims_record_canopy_snap_as_discoverer) stay in snap and call canopy_overpayments::store. The helpers test_claim_req and test_discoverer serve both groups, so they are copied into the moved module, not moved.

  • New unit test in api.rs: routes() matches each of the six (method, path) pairs (the response is neither 404 nor 405) and answers 405 for an unregistered method on /overpayments/{id}.

  • The crate gains dev-dependencies canopy-test-lib, tokio and tower (features = ["util"], for oneshot, as in crates/canopy-api/Cargo.toml), all workspace; canopy-test-lib does not depend on canopy-overpayments, so there is no cycle.

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); new crates/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’s services/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/overpayments handlers … each const-asserting");

    • the guard: citation strings in crates/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 from canopy_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 store void_claim row, and the route rows naming overpayments_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/overpayments handlers … each const-asserting" is now one handler with one const-assert;

  • CHANGELOG.adoc == Unreleased › Changed.

Verification

  1. 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.env sourced (ephemeral schemas only); cargo xtask quality-budgets, cargo machete --with-metadata crates services tools xtask, cargo doc --workspace --no-deps with broken intra-doc links denied.

  2. CANOPY_CARGO_FEATURES=canopy-api/test-clock cargo xtask dev reload, then cargo xtask api-docs (only D3’s strings differ from main’s snapshots, already committed), then git push. The battery runs qc_read_only_test in 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 crates/canopy-api/src/pagination.rs comment, repo-relative cites, the #1012 criterion, copied test helpers, full paths(…​) entries, the api module doc, the parity wording, citation precision).

Edit this page · default