0033. Destination transfer receipt custody — stage, quarantine, stow
Status
Accepted
Context
ADR 0030 closed the ORIGIN half of the network transfer saga: a
planning command reserves origin-site stock, ledgered in
transfer_allocations (DB-unique on transfer_line_id), and the reply
rides the transactional outbox. The DESTINATION half did not exist.
When the truck arrives, this service had no way to answer the only
question that matters: did the goods we expect arrive, where did they
go, and whose usable stock just rose?
The network-transfer design names the failure modes that a naive "receive it like any inbound PO" implementation hits:
- Silent absorption. Receiving whatever count arrives against whatever the ledger promised, without recording the difference, makes over/short invisible — the shrinkage or windfall becomes ordinary usable stock and no one is ever accountable for it.
- Unrecognized arrivals. A scan whose
transfer_line_idthis service never allocated (a mis-keyed scan, a transfer from another network leg, an unsanctioned shipment) must not raise availability. The design rule is explicit: a destination scan without a recognized transfer is quarantined as an inventory exception rather than silently raising availability. - Double-apply. Receiving is scan/REST-driven from the dock; a lost HTTP response and a retry, or a double scan, must never create stock twice. The end-to-end invariant the fleet verifies is "destination usable rises ONCE after stow".
One design input deserves its own note: fulfillment-execution publishes
com.warehouse.wes.fulfillment-execution.transfer.TransferArrived on
warehouse.fulfillment.events (subject/key = task id). This service
deliberately does NOT consume it. TransferArrived is a work-execution
fact about a fulfillment task completing; it is not a custody fact.
Custody at the destination is taken by a human scan of what physically
arrived — the count that establishes over/short — recorded through this
service's own REST surface. Consuming TransferArrived here would let a
task-completion event raise usable stock with no physical count behind
it, the exact silent-absorption failure above. (NIP may later consume
TransferArrived to drive its in-transit → received saga transitions;
that is a planning-state concern, not a stock concern.)
Decision
-
StageTransferReceipt(POST /transfers/{transferLineId}/receipt,Idempotency-Keyrequired) records ONE destination scan:{transfer_id, transfer_line_id, destination_site_id, sku, received_quantity}. The lookup key is the ORIGIN ledger — the sametransfer_allocationsrow ADR 0030 wrote:- no row, a REJECTED row, a
transfer_idmismatch, or a destination equal to the transfer's own origin site → quarantine: aninventory_exceptionsrow (kindUNKNOWN_TRANSFERorUNRECOGNIZED_TRANSFER, migration 0032) is written in the same transaction, NOTHING that could raise usable stock is published, and the caller gets an explicit 422 problem whoseexceptionextension carries the quarantine row's coordinates; - a recognized ALLOCATED row → a
transfer_receiptsrow is created (stateSTAGED,expected_quantity= the ledger'srequested_quantity,received_quantityas counted,variance= received − expected signed), andcom.warehouse.wms.inventory-storage.stock.TransferReceiptStagedis published through the same transactional outbox. No usable stock moves at stage.
- no row, a REJECTED row, a
-
Variance is explicit, never absorbed. Over-receipt (positive) and short-receipt (negative) are recorded on the receipt row (CHECK constraint
variance = received_quantity - expected_quantity), ride theTransferReceiptStagedevent, and appear in the REST response. The stow places exactly the RECEIVED quantity — never the expected one — so stock always matches the physical count and the difference is always accountable somewhere. -
StowTransferStock(POST /transfers/{transferLineId}/stowages,Idempotency-Keyrequired) places a STAGED receipt's goods:{bins: [{bin_id, quantity}]}. Every bin must be registered and belong to the receipt'sdestination_site_id— the site-custody fact from ADR 0030 fails closed (another site's bin, or a legacy site-less bin, is a 409) — and the bin quantities must sum EXACTLY to the receipt's received quantity. OneStockUnitper leg is created AT the destination site (NewStockUnitAtSite), the receipt movesSTAGED → STOWEDwith its stow legs recorded, andcom.warehouse.wms.inventory-storage.stock.TransferStockStowed(subject/key =transfer_line_id) is published. This is the only path that raises the destination site's usable stock for a transfer. -
Idempotency is database-level, like the origin side. The
transfer_receiptsrow is DB-UNIQUE ontransfer_line_id(migration 0032); the STOWED transition is a guarded UPDATE (WHERE state = 'STAGED', zero rows = already stowed). A replayed identical stage returns the ORIGINAL receipt (replay: true, nothing republished); a different payload on the same line is a 409 (transfer-receipt-conflict) with the original immutable. A replayed stow for a STOWED receipt returns the original allocations, creates NO second StockUnit, and publishes nothing. A repeated identical QUARANTINED scan is also a benign replay: theinventory_exceptionsunique key on the exact scan tuple(transfer_line_id, destination_site_id, sku, received_quantity)keeps one row. The RESTIdempotency-Keymiddleware (ADR 0018) in front of both endpoints is defense in depth that also replays the original HTTP response byte-for-byte. -
One UnitOfWork per decision. Exception row OR receipt row OR stock units + bins + receipt transition, plus the outbox row(s) for the published event — all commit or none does. A forced outbox failure mid-stow rolls back the stock rows and leaves the receipt STAGED (proven against real Postgres by the integration test).
-
A quarantine is a committed outcome, not an error path. The exception row is durable state (auditable, resolvable by a human); the caller receives BOTH the exception (in the response's
exceptionmember) and the explicit problem (as the 422).
Consequences
- The custody invariant closes: origin usable fell at allocation (ADR
0030), destination usable rises at stow, once, only through
TransferStockStowed;wes-work-planning's observed-usable projection can consume the new events by their full type strings. - Over/short is a first-class fact. Downstream, NIP can reconcile
varianceagainst the saga state and decide corrective transfers; this service does not auto-correct — a physical custody break is never silently repaired. TransferArrivedstays unconsumed here (documented above); if the fleet later wants a dock-pre-alert, it belongs in planning's saga, not in stock custody.- The
bins.site_idcolumn from migration 0030 becomes load-bearing on the stow path: bins reach it via site-scoped registration; the REST bin-registration surface does not yet expose site (a v2 concern, and a backfill story for legacy bins). - Two new published messages are catalogued in
apis/asyncapi.yamland ADR-0024's type table; REST docs regenerate fromapis/openapi.yaml(npm run gen-api-docs inventory).