Skip to main content

ADR 0019: Every problem type has a replay class; a retryable refusal is not stored under the Idempotency-Key

Status​

Accepted (2026-11-18). Refines 0008 (the Idempotency-Key middleware: a stored 4xx is no longer one rule for every refusal), 0007 (the MCP write wrapper applies the same classes) and 0016 (the addendum of 2026-11-18 made carrier-suspended the first OUTCOME; this ADR makes it one of three classes). Decided by the owner on 2026-11-18 as a fleet rule; this repository is the second pilot (the first is shipping-and-tracking, which uses a list; here the class is a field of the catalogue entry and the list is gone). Code: internal/adapters/inbound/http/errors.go (the class of each entry), replay_class.go, capture.go, idempotency.go; internal/adapters/inbound/mcp/errors.go, replay_class.go, idempotency.go.

Context​

A request retried under the same Idempotency-Key was answered from the store whatever the first answer was, a 4xx included (ADR 0008). That is wrong for a refusal the same request could pass later: a 409 concurrent-modification is gone on the next attempt, a 404 no-claimable-move stops being true when a supervisor requests a move, a 422 unknown-location stops being true when the facility event arrives. A client that reuses its key (the floor client reuses the key of an identical pending action; a deterministic key is normal) was handed the stale refusal for 24 hours. It also cannot simply be "never store a 4xx": 409 carrier-suspended COMMITS a REJECTED visit before it answers, and a second execution would record a second turn-away (ADR 0016, addendum).

Decision​

Every problem type belongs to exactly one replay class, declared on its entry of the problem catalogue (problemCatalogue in errors.go). The middleware reads the class; it holds no list of slugs and no status range except 2xx and 5xx.

ClassMeaningMiddleware
FINALthe answer cannot change for this requestthe handler's writes roll back; the answer is stored on its own and replayed with Idempotency-Replayed: true
OUTCOMEthe command COMMITTED state and then answered an error that must be rememberedthe writes commit together with the stored answer, in one transaction; a replay records nothing
RETRYABLEnothing was committed and the answer depends on state that can changeeverything rolls back, nothing is stored, the key is free: the same request under the same key runs again

A 2xx is stored and a 5xx or a panic is never stored, as before. The classes are chosen by what the use case does and by whether the same request can pass once state changes, not by the status code (409 is FINAL, OUTCOME and RETRYABLE in the table below). There is no runtime default: an entry without a class fails TestEveryCatalogueEntryDeclaresAReplayClass, and a problem the adapter writes that is neither a catalogue entry nor listed as outside the store fails TestEveryProblemTheAdapterWritesHasAClassOrIsOutsideTheStore. This table and the catalogue are held equal by TestTheADRDeclaresTheClassOfEveryProblem (it reads the rows below that start with a backticked slug, a class and |).

How the class was chosen. FINAL only when the answer cannot change: a field of the request, or a fact that never changes after it is set. OUTCOME only for a refusal that commits state, proven by the use case. Everything else that depends on state that can change is RETRYABLE, and when unsure RETRYABLE: a refusal that rolled everything back costs nothing to run again, while a wrongly FINAL one pins a refusal that is no longer true for 24 hours. The one real risk of RETRYABLE is a refusal that did commit; the use cases were read for it (section "OUTCOME", below).

Classification​

Problem typeClassStatusReason (from the code)
malformed-request-bodyFINAL400the body is not valid JSON for the operation: a function of the request (decodeJSON)
unknown-query-parameterFINAL400a query parameter the operation does not take
invalid-if-matchFINAL400the If-Match header is not an entity tag of a version
invalid-report-rangeFINAL400from/to are not RFC 3339 instants, or to is not after from within 92 days: both are in the request
invalid-threshold-minutesFINAL400thresholdMinutes is not an integer from 1 to 10080
invalid-stateFINAL400state is not a value of the operation
invalid-limitFINAL400the paging limit is invalid
invalid-cursorFINAL400the paging cursor is invalid
visit-not-foundFINAL404the visit id is minted by this service (IDs.New("visit")) and handed out after its commit; a visit is never deleted, so an id not found now is never found later. Judgement call J1
trailer-not-foundFINAL404same as visit-not-found (IDs.New("trl"), loadTrailer). J1
move-not-foundFINAL404same (IDs.New("mv"), moves.go 173 and 231). J1
yard-check-not-foundFINAL404same (checks_policy.go 35). J1
inspection-not-foundFINAL404same (inspections.go 135 and 153). J1
site-not-foundRETRYABLE404GET /sites/{siteId}/yard-board (queries.go 122): a site is named by the caller and known from facility events (ADR 0003), which may arrive later
template-not-foundRETRYABLE404a template or version is published by a later PUT (inspection_templates.go 46)
no-claimable-moveRETRYABLE404ClaimNextMove finds no REQUESTED hostler move now (moves.go 115-117); a supervisor requests one later, and a polling hostler must reach it
visit-already-openRETRYABLE409the tractor plate has an open visit (gate_checkin.go 75-76, unique index uq_yard_visits_one_open_per_plate); passes when that visit checks out
appointment-requiredRETRYABLE409the site needs a BOOKED appointment in the local copy (visit.go 164); the copy is event-fed and the policy is changeable. J4
trailer-not-releasedRETRYABLE409a trailer taken out is not on site or not released (visit.go 352, ReleasedForDeparture): door work ends, a move ends
seal-requiredRETRYABLE409a loaded outbound trailer has no seal (visit.go 355); the seal is recorded later by POST .../seals
carrier-suspendedOUTCOME409checkInVisit turns the refusal into a nil error inside its unit of work (gate_checkin.go 56-66), so rejectSuspendedCarrier saves the REJECTED visit and publishes VisitRejected before the refusal is returned (carriers.go 77-92, called at gate_checkin.go 101-103). A second execution would record a second turn-away (ADR 0016, addendum)
location-occupiedRETRYABLE409another trailer holds the spot or door (placement.go 75); passes when it leaves
door-closedRETRYABLE409the door is closed in the closure copy fed by facility events (placement.go 130)
door-flow-mismatchRETRYABLE409the door's flow, from the facility copy, does not suit the trailer's direction (placement.go 133)
trailer-on-holdRETRYABLE409the trailer is held (work.go 31 and 90, placement.go 136); passes after the release
trailer-not-on-holdRETRYABLE409ReleaseHold on a trailer that is not held (work.go 101); passes once a hold exists
door-work-openRETRYABLE409door work is not done (movement.go 99, securing.go 93); passes when it is
trailer-not-securedRETRYABLE409the trailer is not secured (work.go 34, securing.go 90); passes after POST .../securings
trailer-still-securedRETRYABLE409the trailer is still secured (securing.go 107); passes after the release
trailer-already-on-siteRETRYABLE409the unit is on site (gate_checkin.go 141); passes after it departs
placement-rule-violatedRETRYABLE409the spot's kind, from the facility copy, does not allow what the trailer carries (classification.go 113 and 160); the copy and the trailer's classification can change. J4
seal-class-too-lowRETRYABLE409the seal is below the site's minimum class (sealclass.go 69-71); the policy is changeable. J4
inspection-requiredRETRYABLE409no PASSED inspection yet (gate.go 34); passes after one
inspection-failedRETRYABLE409the latest inspection FAILED (gate.go 36); passes after a passing re-inspection
inspection-closedFINAL409the inspection is PASSED or FAILED (inspection.go 158 and 214) and a completed inspection never reopens
required-item-pendingRETRYABLE409a required item has no result (inspection.go 223); passes after it is recorded
inspection-already-openRETRYABLE409an inspection is open for the trailer, template and gate (inspections.go 56); passes after it completes
template-version-immutableFINAL409the version was published with other content (inspection_templates.go 109) and a published version never changes (ADR 0015)
template-is-built-inFINAL409the template id is a built-in (inspection_templates.go 95), a constant of the code
trailer-has-open-moveRETRYABLE409the trailer has an open move (moves.go 76, doors.go 149); passes when it ends
destination-reservedRETRYABLE409another open move reserves the destination (targeting.go 134, moves.go 208)
hostler-busyRETRYABLE409the hostler holds a claimed move (moves.go 111); passes after its completion
not-lease-holderRETRYABLE409the completing hostler is not the holder; passes if the move is claimed again by that hostler. J6
lease-not-expiredRETRYABLE409mapped but never answered (mappedButNeverAnswered, only the lease sweep raises it); by its meaning it passes when the lease expires
yard-check-openRETRYABLE409the site has an open yard check (unique index); passes after its closure
location-already-observedFINAL409the observations of an OPEN check only grow (yardcheck.go 104-110); a closed check answers illegal-transition
no-free-spotRETRYABLE409every spot is taken (targeting.go 160); a spot frees
illegal-transitionRETRYABLE409the state machine has no such transition from the aggregate's state now. Mixed: most callers can pass after the state moves on (StartDoorWork on a PARKED trailer), a few cannot (CheckOut of a CHECKED_OUT visit). J5
concurrent-modificationRETRYABLE409the optimistic version guard lost a race (UPDATE ... WHERE version = $n, ADR 0005); the retry reads the new version and passes
precondition-failedFINAL412the If-Match names a policy version and versions only grow (checks_policy.go 116-118); a stale one never matches again. J2
invalid-carrierFINAL422the carrier name or SCAC is malformed (shared.go 64-68, visit.go 210)
unknown-locationRETRYABLE422the slot is not in the local copy fed by facility events (placement.go 121, yardpolicy.go 141); it can arrive later
invalid-unit-numberFINAL422the unit number fails its scheme
invalid-seal-classFINAL422the seal class is not I, S or H
invalid-hazmat-classFINAL422the hazmat class is not a US DOT class or division
invalid-un-numberFINAL422the UN number is malformed
unknown-itemFINAL422the item code is not in the inspection, whose items are frozen at opening (inspection.go 162, ADR 0015)
not-applicable-not-allowedFINAL422the template does not allow NOT_APPLICABLE for the item (inspection.go 168)
unknown-templateRETRYABLE422the template or version is not published yet (inspections.go 45, inspection_gates.go 56); a later PUT publishes it
unknown-appointmentRETRYABLE422the appointment is not in the local copy fed by dock-scheduling (visit.go 161); it can arrive later. The same slug also covers "not exactly one trailer", the request's own part. J3
unexplained-destinationFINAL422a completion elsewhere than the destination without a note: both are in the request
validationFINAL422a field of the request is invalid (shared.Invalid, about 100 sites). One site is state-dependent (moves.go 79). J3
idempotency-key-requiredOUTSIDE400written by the middleware before a key exists; never stored
idempotency-key-invalidOUTSIDE400written by the middleware before a key is read; never stored
idempotency-key-reusedOUTSIDE409written by the middleware on a replay with another fingerprint; it is never the stored answer
method-not-allowedOUTSIDE405written by the router, outside the keyed routes
resource-not-foundOUTSIDE404written by the router and the NUL guard, before the middleware
dependency-unavailableOUTSIDE503the 503 of /readyz, not a keyed route (and a 5xx is never stored)
internal-errorOUTSIDE500a 5xx is never stored

Counts: 27 FINAL, 1 OUTCOME, 34 RETRYABLE, 7 outside the store: 69 problem types, the 69 slugs of PR #48's parity test.

OUTCOME: why only carrier-suspended​

The use cases that run behind a keyed route were read for a refusal returned after a commit. checkInVisit is the only one that turns an error into a nil result inside atomically and returns it afterwards (gate_checkin.go 56-66). rejectOnSeal and RejectAtGate record a REJECTED visit and answer success (201), not a refusal. Every other use case returns its refusal from inside atomically, which rolls back. The two sweeps keep one unit of work per item and may commit items before a later item fails, but what they return then is an unmapped error (a 5xx, not stored) or, for the mapped ones (concurrent-modification, illegal-transition, lease-not-expired, not-found), they skip the item inside the loop. A new OUTCOME needs an ADR and a use case that commits before it refuses (TestTheSuspendedCarrierIsTheOnlyOutcome).

Mechanism​

  • The catalogue entry is {err, problem, replayClass}. writeError tells the middleware's capturing writer the class of the problem it writes (MarkReplay); the two lists of PR #51 (outcomeRefusals, KeepWrites) are replaced by it.
  • serveInUnitOfWork asks verdict for the answer: a success or an OUTCOME refusal saves the key row inside the handler's unit of work and commits both; a FINAL refusal rolls the writes back and storeRefusal stores it on its own (as before); a RETRYABLE refusal, a 5xx and a 4xx no entry classified end the unit of work with errNotStored: it rolls back, the key row was never written, and the answer is sent. 2xx, 5xx and FINAL behave as before.
  • The sweeps (NewOptionalIdempotency) have no unit of work around the handler and store the answer when it is a success, FINAL or OUTCOME; a RETRYABLE refusal is not stored.
  • A concurrent duplicate while the first ends RETRYABLE. The duplicate waits on the advisory lock idempotency-key/<key> (pg_advisory_xact_lock, released at commit or rollback). The first request rolls back and releases it; the duplicate takes the lock, finds no row for the key, and runs the handler itself: it does not replay the released refusal and it does not run alongside the first. If it succeeds, its answer is stored and later copies replay that success. TestACopyWaitingOnARetryableFirstRunsItself shows 12 copies: one refusal, one fresh success, ten replays of the success, the handler run twice and never concurrently.
  • Consequence for idempotency-key-reused: after a RETRYABLE refusal the key is free, so the same key with a different body is accepted as a new request.
  • MCP: the write wrapper (keyed) has its own copy of the store shape (the key row inside the tool's unit of work, a refusal stored on its own after the rollback). A RETRYABLE release is trivial there: the tool's error is returned from inside the unit of work like an infrastructure failure, so it rolls back and nothing is stored. The MCP catalogue carries the same class per slug (TestEveryMCPProblemDeclaresTheClassOfItsRESTTwin reads the REST catalogue's source). It has no OUTCOME: no write tool answers carrier-suspended.

Judgement calls for the owner​

  • J1. 404 on the command's own target (visit, trailer, move, yard-check, inspection): FINAL. The plan's default was RETRYABLE ("state-dependent by the definition"). Here every id is minted by this service and returned after its commit, and nothing is deleted, so the same request cannot pass later. If the owner wants the plan's default, five entries change to RETRYABLE; nothing else moves.
  • J2. precondition-failed (stale If-Match): FINAL. It is an optimistic-lock answer, which the plan lists as RETRYABLE, but the request itself carries the stale version and versions only grow, so a retry of the same request always fails. Flip to RETRYABLE costs one more execution per retry and changes no outcome.
  • J3. validation and unknown-appointment are mixed slugs. validation is FINAL although moves.go 79 ("the trailer is already at X") depends on where the trailer is; a stored 422 there can outlive the move of the trailer. The clean fix is its own problem type (not done here). unknown-appointment is RETRYABLE although the "not exactly one trailer" part is the request's own.
  • J4. Policy- and copy-dependent refusals are RETRYABLE (appointment-required, seal-class-too-low, placement-rule-violated, door-flow-mismatch): the site policy or the facility copy can change. The cost of the doubt is one more execution of a refusal that rolled back.
  • J5. illegal-transition is RETRYABLE although some of its callers can never pass (checking out a CHECKED_OUT visit). One class per slug; RETRYABLE is the side that cannot pin a stale refusal.
  • J6. not-lease-holder is RETRYABLE (the same hostler can become the holder again); a different caller never passes, which a re-execution tells again.

Consequences​

  • A client that reuses its key after a RETRYABLE refusal now runs the command again; the OpenAPI text that said a stored 4xx is replayed was made true (descriptions only). A client that wants to replay-protect a refusal gets that for a FINAL or OUTCOME one only.
  • The migration comment of 0002_idempotency_keys ("only 2xx and 4xx answers are stored") is history and is not edited; the status < 500 check still holds.
  • A new problem type needs a class on its catalogue entry (and in this table), and the same slug in the MCP catalogue the same class.
  • INCIDENTS.md and the fleet standard are updated by the wave, not by this repository.