Plan: The idempotency hash reads the request, not a shadow struct
Status
Done (2026-11-18) — forty acts across seven services hash the request itself; twenty-two intent structs are kept and say why they differ; the ledger acts are untouched.
Erratum (2026-11-18): the measured outcome. Of the 79 structs the census found, 40 are deleted, 22 are kept and renamed without a version suffix, and 17 are untouched because their claim is built through craig_ledger::intents (14 in the services, 3 in craig-ledger). The services' source shrank by about 1,000 lines net (about 2,700 removed, about 1,650 added, the pin tests included), not the 4,000 the issue hoped for.
Erratum (2026-11-18): the red proofs were mutations against each service’s unit and pin tests. The plan’s "revert one handler to hash a constant body and name the API test that fails" was not run against a live handler, since the API tests need a devstack; those tests ran unchanged on each step’s car.
Erratum (2026-11-18): every pin fixture states every field of its request, nested ones included, from craig-cases on. A field left unset is not pinned when the type skips it from serialization, which the commit review of the craig-cases step caught.
Closes: #2310, on the last step.
Tree: written against main.
Ruling (maintainer, 2026-11-12, on #2310): collapse. The hash is a canonical serialization of the request DTO plus a per-scope version constant; a shadow struct is kept only where the hashed intent really differs from the DTO, and says what differs. Every create’s hash moves once (accepted pre-1.0).
Context
A convert-class endpoint claims its client_request_id in the domain transaction and stores an intent hash beside it (ADR-062). A retry with the same id and the same hash replays; the same id with another hash is a 409 idempotency-conflict. The hash is intent_hash_v1 in crates/craig-api/src/request_claims.rs: SHA-256 over an envelope of scope, jurisdiction, actor, path legs and a body.
The body is never the request. Each endpoint has a private *IntentV<n> struct, a *_spec() builder that copies the request into it, and a doc paragraph. ADR-062 and the module docs justify the struct by "DTO evolution adds an IntentV2 and dual-decode". No bump has done that: all 18 structs above V1 say "REPLACES … outright, there is no dual decode".
Census of main (a script over the struct and its one construction site; regex level, so each verdict is a candidate the implementing step confirms by reading):
| Where | Structs | Copy every request field | Differ or omit |
|---|---|---|---|
craig-cases |
24 |
11 |
13 |
craig-placement |
19 |
11 |
8 |
craig-financial |
10 |
3 |
7 |
craig-security |
10 |
8 |
2 |
craig-exchange |
6 |
6 |
0 |
craig-rules |
5 |
5 |
0 |
craig-reporting |
2 |
1 |
1 |
|
3 |
— |
out of scope |
Facts that shape the design, each read from the tree:
-
serde_jsonserializes struct fields in declaration order, and the workspace enablesfloat_roundtrip. No request DTO file holds aHashMaporHashSet, so serialization is deterministic. -
rust_decimalusesserde-str:10and10.00serialize differently. The money structs rescale to 2 first. -
The hash is unkeyed and stored in
request_claims. ADR-062’s #2370 and #2375 amendments forbid an encrypted column’s plaintext in it;PersonIntentV5,ReportIntentV4,MedicaidApplicationIntentV1, the person-identifierRecordIntentV1andEnrollmentIntentV1put a keyed digest there instead. -
Eight request DTOs do not derive
Serialize(for exampleCreatePlacementRequest,CreateFosterHomeRequest,SendExchangeRequest). -
craig_ledger::intents::spec_ofalready hashes anySerializevalue into aClaimSpec. Its 45 pinned hashes (services/craig-cases/src/api/intents/ledger_pins.rs) and the ledger-lift golden depend on today’s envelope bytes. -
Outside the ledger, only
custody.rspins a hash (6). The other scopes have no test that notices their hash moving.
Design
The rule (ADR-062 amendment)
The hashed body of a claim is the request as the handler holds it when it builds the claim, wrapped with the scope’s revision. A separate intent struct exists only where the intent differs from the request, for one of three reasons, and its doc comment names the reason:
-
Secret: a field is stored encrypted or is a credential, and enters the hash only as a keyed digest (#2370, #2375).
-
Normalised: two requests that mean the same thing must hash the same and the request type cannot say so (money rescaled to 2; a set hashed in dictionary order).
-
Not the client’s: the request carries a field the server ignores or derives, or the hashed value is not in the body at all (a job’s computed period).
The V<n> suffix and the dual-decode promise are deleted from the ADR and the module docs.
Substrate (crates/craig-api/src/request_claims.rs)
pub struct RequestScope { pub claim: ClaimScope, pub kind: EntityKind, pub revision: u16 }
/// The hashed body of a request claim: the scope's revision, then the request
/// nested under `request` (not flattened), in this field order.
#[derive(serde::Serialize)]
struct Revised<'a, B> { revision: u16, request: &'a B }
pub fn request_spec<B: serde::Serialize>(
scope: RequestScope, client_request_id: Uuid, jurisdiction: &str,
actor: Actor, path: &[(&str, String)], body: &B,
) -> Result<ClaimSpec, ApiError>
There is no marker trait: request types live in the *-contracts crates, which do not depend on craig-api, so the orphan rule forbids a craig-api trait impl for them from a service crate. The struct is named RequestScope because craig_ledger::intents::Scope already exists.
-
request_spechashesintent_hash_v1(scope.claim, jurisdiction, &actor, path, &Revised { revision, request: body }). The envelope is untouched, sospec_of, the ledger pins and the golden do not move. -
The request’s own
client_request_idis inside the hashed bytes. That is harmless: a claim is looked up by that id, so two requests compared always carry the same one. -
revisionstarts at 1 for every scope. It is bumped when the meaning of an unchanged body changes. A changed request type moves the hash by itself. -
What keeps a secret out of the hash is the per-struct rule below plus two existing guards: the blocking
serialize-credentialslint (a credential-shaped field on aSerializetype fails it) and the kept Secret structs' plaintext tests. No new gate is added. -
The
(ClaimScope, EntityKind)tuple constants of a collapsed scope becomeRequestScopeconstants. A kept struct keeps its tuple constant, its builder and its bytes: its hash does not move.
Erratum (2026-11-18): a service binds its request types to their scopes with a trait defined in its own crate (ClaimedRequest, with request_claim), so a handler passes the body once; the first form pushed create_caregiver past the 40-line limit. The trait is local to the service, so the orphan rule does not apply, and it calls request_spec. The craig-api macro claimed_requests! generates the trait, the impls and the function, so the services share one definition.
Per struct
For each struct the step reads the construction site against the request type and takes the first row that applies:
| Finding | Action |
|---|---|
The builder hashes through |
Out of scope (a ledger act): untouched, name included. This covers |
Giving the request type |
Keep (Secret); the request type stays without |
Every request field but |
Delete struct and builder; add |
A field enters as a digest |
Keep (Secret). Expected: |
A value is rescaled, sorted or canonicalised inside the builder |
Keep (Normalised). Expected: |
The request has a field the struct leaves out, or the builder takes no request |
Keep (Not the client’s). Expected: |
The step cannot tell |
Keep, and say so in the MR |
A kept struct is renamed without its V<n> (PersonIntent); a schema: "v5" field inside it stays, because removing it would move a hash for no gain. The three craig-ledger structs are out of scope: the issue keeps the ledger acts.
The census also found structs outside src/api/intents/ (services/craig-cases/src/api/ssa_outcomes.rs, referrals.rs, ledgers/person_siblings.rs); each step runs git grep -E 'IntentV[0-9]+' services/<service> and handles every hit.
A handler that canonicalises a field before building the claim today (the placement admin_unit) keeps doing so before request_spec; the rule hashes the request "as the handler holds it".
Expected result, unmeasured until built: about 40 structs deleted, about 27 kept and renamed, about 12 ledger-act structs untouched. The issue’s "shrinks by ≥ 4k lines" is not promised; each MR states the measured line change.
Changed behaviour, on record
-
Every collapsed scope’s hash moves once. A client that sent a request before the deploy and retries it after gets a 409
idempotency-conflictinstead of a replay, for that one request. No stored row is rewritten; old claims age out with the 30-day window. -
A collapsed scope now distinguishes values the struct merged only where the struct merged them by omission; the per-struct table keeps every such struct, so none is expected. Any found is listed in its MR.
-
A request DTO’s field order becomes hash input. Reordering fields moves that scope’s hash.
Scope
Steps, one MR each, stacked:
-
ADR and substrate. ADR-062 amendment
#2310; this plan page and nav entry;RequestScope,request_spec,Revisedin craig-api with their tests;request_claimsmodule doc rewritten. No service changes. -
craig-placement. Apply the table to its 19 structs; rewrite
intents/mod.rsdoc; add the service’s pin table test. -
craig-financial. Its 10 structs.
-
craig-cases. Its 24 structs.
ledger_pins.rsmust pass unchanged. -
craig-security, craig-rules, craig-reporting, craig-exchange. Their 23 structs.
-
Close. Plan Status Done, archive, issue note with the measured line change and the list of kept structs by reason.
Out of scope: the envelope layout; craig-ledger’s structs; `legacy_service_intent_hash_v1; the one-time-secret pair columns; any wire or OpenAPI change (a request type gaining Serialize changes neither).
Tests
| Category | Test |
|---|---|
Contract (wire: the hash bytes) |
craig-api |
Contract |
Per service, one table test |
Property |
craig-api |
Unit |
craig-api |
Regression (unchanged, must stay green) |
|
Devstack-gated (car only) |
Each service’s existing replay and conflict API tests (same id and body replays; same id and a changed field is 409) run unchanged; they are the end-to-end proof per scope. A collapsed scope with no such pair is listed in its MR, not given a new test here. |
Red proof |
For each service step: revert one handler to hash a constant body and name the API test that fails; remove one field from a pinned fixture and see the table test fail. |
Assertions changed or removed are listed per MR. Tests that named a deleted struct (for example a_trafficking_intent_is_deterministic_blind_to_the_request_id) stay if their struct is kept.
Risks
| Risk | Mode | Mitigation |
|---|---|---|
A request with an encrypted or credential field is hashed as submitted, so its plaintext is guessable from a |
fail-open |
Not mechanically closed for an encrypted field whose name is not credential-shaped: it rests on the per-struct read. The six Secret structs are kept and their plaintext tests stay; |
A retry that straddles the deploy is refused |
fail-closed |
One 409 per in-flight request; stated in the changelog Ops clause. Accepted by the ruling. |
A struct that merged two spellings (rescale, set order, an ignored field) is collapsed, so equal intents now conflict |
fail-closed |
The table keeps any struct whose builder transforms or omits; "cannot tell" keeps. A wrong collapse shows as a 409 on a legitimate retry, never as a wrong replay. |
A collapse drops a field that told two intents apart, so a different request replays the first one’s result |
fail-open |
A collapse only adds fields: the step confirms the request holds every field the struct held. The service’s replay and conflict API tests run unchanged, and the red proof names one per step. |
A later DTO edit reorders or adds a field and moves a hash unnoticed |
silent |
The per-service pin table fails on it. |
|
silent |
The eight types are request-only; the step greps for any place that serializes them and names the result in the MR. |
The ledger pins or golden move |
fail-closed |
The envelope is untouched; both run unchanged in every step’s car. |
Delivery
-
Doc page:
docs/modules/ROOT/pages/adrs/adr-062-transactional-idempotency-claims.adoc(the amendment, and the Decision bullet at lines 105-109 reworded);architecture.adoconly if it namesIntentV1. -
CHANGELOG entry: CRAIG’s release notes are
changelog.d/fragments, folded intoCHANGELOG.adocat a release. One fragment per service step,changelog.d/2310.<service>.changed.adoc, Ops clause: a request sent before the upgrade and retried after it is answered 409idempotency-conflict; send it again with a newclient_request_id. -
Gates before each push: fmt, clippy, machete, nextest for the touched crates,
cargo xtask validate --profile fast,cargo xtask test --doc. -
Close:
Closes #2310on step 6; plan archived; note on #2310 with merge SHAs' evidence, the kept list and the measured line change.