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 src/api/intents/

24

11

13

craig-placement src/api/intents/

19

11

8

craig-financial src/api/intents/

10

3

7

craig-security src/api/intents/

10

8

2

craig-exchange src/api/intents/

6

6

0

craig-rules src/api/intents/

5

5

0

craig-reporting src/api/intents/

2

1

1

crates/craig-ledger/src/intents.rs

3

—

out of scope

Facts that shape the design, each read from the tree:

  • serde_json serializes struct fields in declaration order, and the workspace enables float_roundtrip. No request DTO file holds a HashMap or HashSet, so serialization is deterministic.

  • rust_decimal uses serde-str: 10 and 10.00 serialize 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-identifier RecordIntentV1 and EnrollmentIntentV1 put a keyed digest there instead.

  • Eight request DTOs do not derive Serialize (for example CreatePlacementRequest, CreateFosterHomeRequest, SendExchangeRequest).

  • craig_ledger::intents::spec_of already hashes any Serialize value into a ClaimSpec. 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.rs pins 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:

  1. Secret: a field is stored encrypted or is a credential, and enters the hash only as a keyed digest (#2370, #2375).

  2. 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).

  3. 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_spec hashes intent_hash_v1(scope.claim, jurisdiction, &actor, path, &Revised { revision, request: body }). The envelope is untouched, so spec_of, the ledger pins and the golden do not move.

  • The request’s own client_request_id is inside the hashed bytes. That is harmless: a claim is looked up by that id, so two requests compared always carry the same one.

  • revision starts 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-credentials lint (a credential-shaped field on a Serialize type fails it) and the kept Secret structs' plaintext tests. No new gate is added.

  • The (ClaimScope, EntityKind) tuple constants of a collapsed scope become RequestScope constants. 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 craig_ledger::intents::spec_of, statement_spec, retract_spec or close_spec

Out of scope (a ledger act): untouched, name included. This covers custody.rs (three structs, six pins), LinkIntentV1 in ledgers/person_siblings.rs, and child_facts.rs, parents.rs, permanency.rs, demographics.rs

Giving the request type Serialize would trip serialize-credentials, or a field is credential material

Keep (Secret); the request type stays without Serialize. Expected: PartnerIntentV1 (auth_config)

Every request field but client_request_id is copied as it is, or through as_str, as_deref, as_ref, a schema literal

Delete struct and builder; add Serialize to the request type if missing; the handler calls request_spec(SCOPE, body.client_request_id, …, body)

A field enters as a digest

Keep (Secret). Expected: PersonIntentV5, ReportIntentV4, MedicaidApplicationIntentV1, person-identifier RecordIntentV1, EnrollmentIntentV1

A value is rescaled, sorted or canonicalised inside the builder

Keep (Normalised). Expected: AdjustmentIntentV1, RateIntentV1, ClaimingRecordIntentV1, PlacementIntentV7, HistoricalPlacementIntentV4, ProposalIntentV1

The request has a field the struct leaves out, or the builder takes no request

Keep (Not the client’s). Expected: IveDeterminationIntentV2, RecordSsaOutcomeIntentV1, StageScreeningRunIntentV1, SweepExecuteIntentV1, SubsidyGenerateIntentV1, AfcarsIntentV1, PlacementNoteIntentV1, RotateKeyIntentV1, SweepPreviewIntentV1, TraffickingIntentV1, the job-enqueue structs built from a computed id

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-conflict instead 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:

  1. ADR and substrate. ADR-062 amendment #2310; this plan page and nav entry; RequestScope, request_spec, Revised in craig-api with their tests; request_claims module doc rewritten. No service changes.

  2. craig-placement. Apply the table to its 19 structs; rewrite intents/mod.rs doc; add the service’s pin table test.

  3. craig-financial. Its 10 structs.

  4. craig-cases. Its 24 structs. ledger_pins.rs must pass unchanged.

  5. craig-security, craig-rules, craig-reporting, craig-exchange. Their 23 structs.

  6. 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 request_spec_pins_the_revised_body_layout: one literal v1: hash over a fixed request, so the wrapper’s bytes cannot drift. Existing golden_vector_pins_the_envelope_byte_layout passes unchanged.

Contract

Per service, one table test every_request_scope_hashes_to_its_pinned_value in src/api/intents/mod.rs: one fixed request per collapsed scope, its literal hash. A field added, removed or reordered fails it; the fix is the new literal, in the same commit as the DTO change.

Property

craig-api a_revision_or_a_body_change_always_moves_the_hash (proptest): two specs equal iff revision and serialized body are equal.

Unit

craig-api request_spec_carries_the_scope_kind_actor_and_request_id.

Regression (unchanged, must stay green)

ledger_pins.rs (45 literals), the six custody.rs pins (custody is out of scope, so they hold), the ledger-lift golden, the_person_intent_carries_no_ssn_or_dob_plaintext, the_report_intent_carries_no_opaque_plaintext, intent_digest_domains_are_disjoint_from_every_blind_index_domain, request_claims_battery.rs.

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 request_claims dump

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; serialize-credentials catches a credential-shaped name on any type newly given Serialize. Each MR lists every collapsed request type with its field list for review.

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.

Serialize added to a request type exposes a field in a log or response

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.adoc only if it names IntentV1.

  • CHANGELOG entry: CRAIG’s release notes are changelog.d/ fragments, folded into CHANGELOG.adoc at 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 409 idempotency-conflict; send it again with a new client_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 #2310 on step 6; plan archived; note on #2310 with merge SHAs' evidence, the kept list and the measured line change.

Edit this page · latest