Skip to main content

0050. Every problem type has a replay class; a retryable refusal is not stored

Date: 2026-11-20

Status​

Accepted (2026-11-20). Amends ADR 0022 (the transactional Idempotency-Key middleware) and the guideline contract of ADR 0047. Implements the fleet decision of 2026-11-18 (warehouse-harness-template .claude/rules/fleet/idempotency-and-outbox.md, standard: docs/idempotency-replay-classes.md). Wire slugs, titles and statuses are unchanged; only which refusals are stored changes.

Context​

ADR 0022 stores and replays every answer below 500 — its deliberate v1 simplification — so a 4xx refusal is replayed verbatim for the 24-hour window of the key. That is wrong for refusals whose cause is state that can change: a 409 concurrent-modification (the optimistic-lock loser after retryOnPoolConflict's budget is spent), a 409 wip-limit-reached (the WIP drains as completions land), a 409 work-pool-empty (work arrives with the next enqueue or integration event), a 404 not-found for an id the CALLER chose (the Kafka consumer creates work units from order-management events, internal/adapters/inbound/kafka/consumer.go), a 422 no-active-case-level (the SKU's CASE pack level is a product-master fact that arrives by event, ADR 0045). A client that did the right thing — waited for the state to change and retried the same key — was answered with the stale refusal pinned by the key row for the window.

The fleet decision (owner, 2026-11-18): a retried request is answered from the store only when the first answer is FINAL for that request. Each problem type gets exactly one class, declared on its catalogue entry:

  • FINAL — the answer is a function of the request alone. Stored and replayed.
  • OUTCOME — the command COMMITTED state and then answered an error that must be remembered. Stored, and its writes commit with the stored answer.
  • RETRYABLE — nothing committed and the answer depends on state that can change. Not stored: the request transaction rolls back, key row included, and a retry executes again.

2xx is stored; 5xx and panics are never stored; a replay carries Idempotency-Replayed: true. The middleware reads the class and holds no list of slugs and no status ranges beyond 2xx and 5xx. A problem type without a class fails a unit test (TestEveryProblemTypeDeclaresAReplayClass); at runtime a refusal that declares no class is released like RETRYABLE — the safe fallback: a released refusal costs one more execution, a wrongly stored one pins a stale refusal for the window.

Releasing a RETRYABLE key means only that the same key may be tried again by whoever sent it: a different body under the same key afterwards is a NEW request, not idempotency-key-reused. No consumer-side or client-side retry behaviour is added by this ADR.

The store design this rides on (ADR 0022, unchanged)​

Design A, the savepoint design: the middleware opens the request's own transaction, inserts the idempotency_keys row with ON CONFLICT (key) DO NOTHING, runs the handler behind SAVEPOINT handler_run with the transaction bound into the request context (internal/pgtx; the repositories join it), then updates the row with the captured answer and commits. Releasing a RETRYABLE key is exactly the rollback the 5xx branch already did: tx.Rollback discards the key row AND the handler's writes. An OUTCOME refusal needs no new mechanism either — in this store a 4xx whose savepoint released cleanly already commits its writes with the key row — but the failed-release path (a statement died inside the handler and the savepoint had to be rolled back) refuses to store an OUTCOME whose writes are gone: the request fails with a 500 so the same key retries the real work.

What a concurrent duplicate sees when the first request ends RETRYABLE: the second request's INSERT ... ON CONFLICT blocks on the first transaction's key row until it resolves; the rollback removes the row, so the blocked insert SUCCEEDS and the duplicate executes the work fresh (no replay header). It never observes the released refusal.

Decision​

Every problem type of the service — the problemCatalog entries of internal/adapters/inbound/http/errors.go plus the standalone problems written through writeProblem directly (the readiness 503, the reports reader's own answers) — declares exactly one class, on its catalogue entry. GET and PUT routes take no Idempotency-Key; their entries are classified anyway so no problem type of the package is unclassified, and their classes are nominal.

Problem typeStatusClassWhy
malformed-request-body400FINALthe body alone (decodeJSON, rejectNUL runs before the middleware)
reference-required400FINALa missing query parameter (a GET: never behind the middleware)
idempotency-key-required400FINALanswered by the middleware before the store is touched
idempotency-key-invalid400FINALthe header's own format, before the store
unknown-query-parameter400FINALthe query alone (a GET: never stored)
invalid-limit400FINALthe paging query alone (a GET: never stored)
invalid-cursor400FINALthe paging query alone (a GET: never stored)
idempotency-key-reused409FINALthe store's own answer: no handler ran, no new row
not-found404RETRYABLEcaller/event-supplied ids (the K1 consumer creates units from order-management events); the slug is shared with the router's own NUL-path 404 — mixed causes
concurrent-modification409RETRYABLEthe optimistic-lock race after retryOnPoolConflict's budget; nothing of this request was written
wip-limit-reached409RETRYABLEthe WIP drains as completions land; raised before any write of this request
work-pool-entry-already-released409RETRYABLEthe pool entry's state, not the request alone
work-unit-already-enqueued409RETRYABLEthe pool's contents are state written by other requests and events; when unsure, RETRYABLE
work-pool-empty409RETRYABLEwork arrives: a later enqueue or an integration event
work-unit-already-released409RETRYABLEthe unit's state machine; state-dependent, FINAL arguable (judgement call 1)
work-unit-already-completed409RETRYABLEterminal state, FINAL arguable (judgement call 1)
work-unit-not-released409RETRYABLEthe release happens later (an operator or a release request): state not reached yet
work-unit-cancelled409RETRYABLEterminal state, FINAL arguable (judgement call 1)
invalid-quantity400FINALa request field
invalid-path-id400FINALa request field's format
unknown-path-id400RETRYABLEthe process-path catalogue may be Kafka-fed (ADR 0030): a path declared later is a referenced fact not known yet
work-unit-id-required400FINALa request field
work-unit-reference-required400FINALa request field
invalid-line-no400FINALa request field's range
unknown-pick-unit400FINALa request field's enum
invalid-pick-plan400FINALthe pick plan's own fields (and the pick/case-break exclusion)
invalid-case-break400FINALthe case break's own fields
invalid-pick-quantity400FINALa request field
no-active-case-level422RETRYABLEthe SKU's pack levels are fed by product-master events (ADR 0045): a referenced fact not known yet
quantity-not-case-multiple422RETRYABLEcomputed against the event-fed active CASE pack level
work-pool-entry-not-found400RETRYABLEthe entry may arrive later (an enqueue or an integration event)
invalid-wip-limit400FINALthe ConfigurePool body (a PUT: never behind the middleware)
unknown-feed-mode400FINALthe ConfigurePool body's enum (a PUT: never behind the middleware)
invalid-report-query400FINALthe reports query alone (a read-only binary, never behind the middleware)
dependency-unavailable503RETRYABLEa 5xx is never stored
report-store-error500RETRYABLEa 5xx is never stored
internal-error500RETRYABLEa 5xx is never stored

TestTheADRClassTableMatchesTheCatalogue holds this table equal to the catalogue.

Judgement calls (most consequential first)​

  1. Terminal-state refusals are RETRYABLE, not FINAL. work-unit-already-released, work-unit-already-completed, work-unit-cancelled, work-pool-entry-already-released and work-unit-already-enqueued can look FINAL: a completed unit never un-completes, a cancelled one never un-cancels, a pool entry never leaves the pool, so no retry could ever pass. They are classified RETRYABLE because the answer is a function of the aggregate's state — written by other requests and events — not of the request alone, and the fleet's unsure-rule says the cost of a released refusal (one more execution) is preferred over the cost of a wrongly pinned one (a stale 409 for 24 hours). Splitting the truly-irreversible ones out as FINAL is a recorded follow-up, not part of this PR.
  2. not-found and work-pool-entry-not-found are RETRYABLE. This service does not mint work unit ids (workUnitId is caller-supplied on the POST and event-supplied on the K1 choreography), the facts are delivered by other requests and consumers, and the not-found slug is shared with the router's own NUL-path 404 — mixed causes. A 404 on the command's own target is FINAL only when the service mints the ids, nothing is ever deleted AND the slug is unshared; none of the three holds.
  3. unknown-path-id is RETRYABLE. With PATH_CATALOGUE_SOURCE=file the catalogue is fixed at boot and the answer is request-stable; with =kafka (ADR 0030) a path declared later arrives by the replayed topic. The slug mixes both causes, so it is RETRYABLE.
  4. No OUTCOME entries. Every keyed use case validates before its first write; nothing commits state and then refuses. The savepoint design supports OUTCOME (a 4xx's handler writes commit — the store does NOT discard them, so OUTCOME can exist here), and the middleware branch is probe-tested (TestOutcomeProbeRefusalCommitsItsWritesWithTheStoredAnswer), but no problem type declares it today.
  5. The middleware's own answers (idempotency-key-required/invalid/reused) are classified FINAL even though they are answered outside the store: the catalogue is the one place every problem type of the package is classified, and the class documents the intent (the request alone / the store's own answer).
  6. The 410-moved routes were FINAL (charge-forecast-moved, shift-plan-moved): the answer was the same for every body of the gone route (ADR 0049). ADR 0049 stage E removed both routes (a POST answers 404), so both problem types left the catalogue and the table above.

Consequences​

  • A retry under a key whose first answer was RETRYABLE executes the work again; the same key with a DIFFERENT body is a new request, not idempotency-key-reused.
  • A stored FINAL refusal is unchanged: byte-identical replay for the window, same slugs, titles and statuses on the wire.
  • apis/openapi.yaml's Idempotency-Key description now says which refusals are replayed and which release the key (descriptions only; oasdiff breaking gate green).
  • The reference test of warehouse-harness-template (templates/idempotency/replay_classes_test.go.tmpl, rendered as idempotency_replay_classes_integration_test.go with the wiring in idempotency_replay_classes_wiring_integration_test.go) proves every entry's class against the real middleware over a real Postgres, per class; two real-router proofs (wip-limit-reached, concurrent-modification) drive the same rule through the shipped chi router.
  • Retention is unchanged and enforced: the housekeeper (ADR 0022, 24h TTL) purges expired idempotency_keys rows in batches.