ADR 0002: the CustomerOrder and Shopper aggregates, their invariants and the assumptions the model left open
Status
Accepted (2026-10-23).
Context
retail-network-docs model/model.yaml (context customer-ordering) names two
aggregates, their states, commands, invariants with Err names and two events.
The Gap Register items G7, G10, G12 and G25 and contracts-delta.md (E7, E9, E11,
E16) close what the model left open for this context; where they are silent the
assumptions below apply. Assumptions are numbered A1.. and stay final unless the
orchestrator overrules them.
Decision
Aggregates (Go packages internal/domain/customerorder, internal/domain/shopper)
Pure Go: no I/O, the clock and the ids are passed in, every aggregate has
Snapshot() / Restore() and an optimistic version (the repository raises
ports.ErrConcurrentUpdate). internal/domain/shared holds the event interface,
Header and the shared errors (ErrValidation, ErrIllegalTransition,
ErrInvalidSnapshot).
CustomerOrder (root orderId)
- Order states:
PLACED,IN_FULFILLMENT,PARTIALLY_SHIPPED,SHIPPED,PARTIALLY_DELIVERED(G25),DELIVERED,LOST,RETURNED(A19, A23),CANCELLED. - Group states (per
GroupPromise, G25):PROMISED(no node locked: the group is "unlocked"),LOCKED,SHIPPED,DELIVERED,LOST,RETURNED(terminal, A19),CANCELLATION_REQUESTED,CANCELLED. - Value objects:
ShipTo{name, line1, line2, city, region, postalCode, countryCode, phone}(synthetic PII),OrderLine{lineNo, catalogItemId, quantity},GroupPromise{groupNo, promiseId, tier, witnessNodeId, shipBy, deliverByEarliest, deliverByLatest, lineNos[], status, revisionNo, assignmentId, nodeId, lockedAt, shippedAt, trackingNumbers[], deliveredTrackingNumbers[], deliveredAt, outcomeTrackingNumber, lostAt, returnedAt, cancelRequestedAt, cancelledAt, cancelRefusedReason}. - Commands:
PlaceOrder,MarkInFulfillment(FulfillmentLocked),MarkGroupShipped(FulfillmentShipped),MarkGroupDelivered(PackageDelivered),MarkGroupLost/MarkGroupReturned(PackageLost,PackageReturned, A19),CancelOrder(REST, andCancelUnkeepableGroupfor the policyCancelOnUnfulfillable),ConfirmGroupCancelledandRefuseGroupCancellation(FulfillmentCancelled,FulfillmentCancelRefused),UpdateGroupPromise(PromiseRevised). - Events:
CustomerOrderPlaced(PlaceOrder),CustomerOrderCancelled(CancelOrder and CancelUnkeepableGroup). The other commands record facts their owners already published and raise no integration event.
Order status is DERIVED from the group states after every command, over the
groups that are not CANCELLED:
| remaining groups | status |
|---|---|
none (all CANCELLED) | CANCELLED |
every group closed (DELIVERED, LOST or RETURNED): all DELIVERED | DELIVERED |
every group closed, some DELIVERED | PARTIALLY_DELIVERED |
every group closed, none DELIVERED, some LOST | LOST |
every group closed, none DELIVERED, none LOST | RETURNED |
(some group still open) some DELIVERED | PARTIALLY_DELIVERED |
every group SHIPPED, LOST or RETURNED | SHIPPED |
some group SHIPPED, LOST or RETURNED | PARTIALLY_SHIPPED |
some LOCKED | IN_FULFILLMENT |
| otherwise | PLACED |
Rows are read top to bottom, first match wins. A LOST or RETURNED group did ship,
so beside open groups it counts as shipped progress (A23).
A derived status that ranks below the current one is refused with
ErrStatusRegression. Ranks: PLACED 0, IN_FULFILLMENT 1, PARTIALLY_SHIPPED 2,
SHIPPED 3, PARTIALLY_DELIVERED 4, DELIVERED 5; LOST and RETURNED are terminal
like DELIVERED (nothing follows them). CANCELLED is terminal and is reached
when every group is CANCELLED. A group can only be cancelled while unlocked, so an
order with a locked, shipped or delivered group is never CANCELLED. PLACED -> CANCELLED
is therefore the only reachable cancellation; the model transition
IN_FULFILLMENT -> CANCELLED stays legal in the table but cannot occur (G25 notes it
was unreachable; the per-group cancellation above replaces it).
Group states move forward only: PROMISED 0, LOCKED 1, SHIPPED 2, DELIVERED 3;
LOST and RETURNED are entered from SHIPPED only (A19).
CANCELLATION_REQUESTED is entered from PROMISED only and leaves to CANCELLED
(confirmed) or back to LOCKED (refused); CANCELLED, DELIVERED, LOST and
RETURNED are terminal.
Invariants (all in the domain, table-tested):
| Rule | Err name | Problem slug |
|---|---|---|
| every line is in exactly one group, every group has a promise | ErrLineWithoutPromise | line-without-promise (422) |
| the promise's destination prefix matches the ship-to | ErrPromiseDestinationMismatch | promise-destination-mismatch (422) |
| cancel only while a group is unlocked (advisory: order-sourcing decides) | ErrCancelAfterLock | cancel-after-lock (409) |
| status moves forward only | ErrStatusRegression | status-regression (409) |
| the command is not allowed from the current state | ErrIllegalTransition | illegal-transition (409) |
| a value breaks a value-object rule | ErrValidation | validation-failed (422) |
Shopper (root shopperId): state ACTIVE; RegisterShopper; invariant "email
unique" = ErrShopperExists (shopper-exists, 409). It holds the normalised e-mail
and up to 20 saved addresses (ShipTo). No events (the model lists none).
Assumptions (where the model or the register is silent)
- A1 (G25) Order status
PARTIALLY_DELIVEREDexists; order and group status are derived as in the table above. A group is DELIVERED when every tracking number announced byFulfillmentShippedhas aPackageDelivered. - A2 (G25)
GroupPromisealso storestier,shipBy,witnessNodeId(the fieldsCustomerOrderPlacedpublishes), the assignment/lock/ship/delivery facts of its group and a per-group status. - A3 (G12, G25)
CancelOrderis a REQUEST. It moves everyPROMISEDgroup toCANCELLATION_REQUESTEDand raisesCustomerOrderCancelled{groupNos}with exactly those groups; locked, shipped and delivered groups are untouched ("cancel while any group is unlocked applies to those groups only").FulfillmentCancelledmakes the groupCANCELLED; the order becomesCANCELLEDwhen no group remains outsideCANCELLED.FulfillmentCancelRefusedreturns the group toLOCKEDwithcancelRefusedReason(the vendor ships; the shopper path is a return, outside this context). - A4
CancelOrderon an order that has no group left to cancel returns it unchanged and raises nothing when every group is alreadyCANCELLATION_REQUESTEDorCANCELLED(repeat);ErrStatusRegressionwhen the order isDELIVERED;ErrCancelAfterLockotherwise (every group locked, shipped or delivered). - A5 (G10)
PromiseUnkeepable{promiseId, orderId, groupNo, reason}applies the policyCancelOnUnfulfillable: aPROMISEDgroup goes straight toCANCELLEDandCustomerOrderCancelled{groupNos:[groupNo], reason:"PROMISE_UNKEEPABLE: <reason>"}is published (order-sourcing moves its UNSOURCEABLE assignment to CANCELLED); the order becomesCANCELLEDwhen nothing remains. In any other group state the event is recorded and skipped. - A6
FulfillmentUnsourceable,PromiseAtRisk,PromiseKept,PromiseBroken,LabelIssued,LabelVoided,PackageScannedandPackageExceptionRaisedchange no aggregate (PackageLostandPackageReturneddo: A19): they feed the read modelOrderTrackingViewonly (ADR 0003). - A7 Consumers: an unknown order,
ErrStatusRegression,ErrIllegalTransitionand an unknown group are recorded in the inbox and skipped ("a status-regressing event is recorded and skipped");ErrConcurrentUpdateis retried in-process up to five times and then fails the message (dead-letter); other errors fail the message. - A8
UpdateGroupPromiseapplies to a group inPROMISED,LOCKEDorSHIPPEDwhenrevisionNois greater than the stored one; an older or equal revision is a no-op, a group in another state is skipped. It never changes the order status. - A9 A checkout names a shopper that must exist; otherwise 422
validation-failed. - A10
destinationPostalPrefixof an order is the first 3 characters of the normalised postal code (upper-case letters and digits only; a code with fewer than 3 such characters is invalid). The promise destination matches when it is a non-empty prefix of the normalised postal code. - A11 The promise destination is read from the field
destinationPostalPrefixof delivery-promise'sPromiseresponse to MakePromise. The published OpenAPI of delivery-promise does not list it yet (it lists it onDeliveryOfferonly); it is requested as an additive field (model gap). When it is absent the check fails closed withErrPromiseDestinationMismatch. - A12
ConfirmPromiseisPOST /promises/{promiseId}/confirmationswith body{orderId, groupNo},Idempotency-Key<orderId>:<groupNo>:confirm, answering the promise (200/201). 404, 409 and 422 from MakePromise or ConfirmPromise mean the offer or hold is no longer valid: checkout re-quotes (all four tiers) and answers 409offer-not-quotable;promise-capacity-exhaustedandbook-closedare passed through as 409;group-spans-nodesas 422; a timeout, a transport error or a 5xx is 503delivery-promise-unavailable. The exact body is requested from delivery-promise (model gap). - A13 The order id is a UUID v5 of the
Idempotency-Key(namespace fixed in the code), so a retried checkout re-uses the same promises (MakePromise is idempotent per<orderId>:<groupNo>) and the same order. - A14 (final) Accepted.
CancelPromiseisPOST /promises/{promiseId}/cancellations(delivery-promise contract, final): headerIdempotency-Key(here<orderId>:<groupNo>:cancel), no body, 200 returns the promise as CANCELLED and gives its confirmed hold back to the book, a repeat on a CANCELLED promise is 200, 404not-found, 409illegal-transitionfor a promise that is not CONFIRMED or AT_RISK (delivery-promise's published route refuses PROVISIONAL, KEPT, BROKEN and RELEASED; a provisional hold lapses by itself). When a LATER confirm of a checkout fails,PlaceOrdercancels the promises it already confirmed (in order, on a context detached from the request so a client that hung up does not leave holds behind) and then fails with the confirm's own error (the problem type already used for it:offer-not-quotable,promise-capacity-exhausted,book-closedordelivery-promise-unavailable). If the failed confirm's outcome is unknown (timeout, transport error, 5xx) its own group is cancelled too, because it may have been applied; if it was not, the promise is still PROVISIONAL and the refusal is expected. A refusal (ports.ErrPromiseNotCancellable) is logged at warn level, a failure (ErrPromiseUnavailable, anything else) at error level (orderId,groupNo,promiseId); both are skipped and the checkout answer does not change. Residual risk, final decision: no compensation record is kept. A cancel that fails (delivery-promise unreachable) leaves that promise CONFIRMED for an order that never appears, and a confirmed hold has no expiry, so it stays until the shopper retries the checkout with the sameIdempotency-Key: the retry re-runs MakePromise (idempotent per<orderId>:<groupNo>), meets the same promise and re-confirms it (confirm is idempotent for the same order and group), so the order is placed on it; a shopper who never retries leaves the hold. The same holds if the order cannot be stored after every confirm succeeded. A cancelled promise answers the retry's confirm withpromise-closed(409), so the shopper sees fresh offers. Model sentence: when checkout fails after some groups were confirmed, the confirmed promises are cancelled and the failure is reported as a failed confirm. - A15 Limits (plain literal constants): up to 100 lines, 50 groups, quantity and numbers <= 2147483647, names/lines/cities <= 100 characters, postal code <= 20, phone <= 40, e-mail <= 254, cancel reason <= 200, 20 saved addresses.
- A16 The e-mail is trimmed and lower-cased; uniqueness is on the lower-cased value.
- A17 Instants are UTC; the domain keeps them with microsecond precision truncated
to what Postgres stores (
timestamptz, microseconds). - A18 Ship-to is never returned by
GET /orders/{orderId}, never logged and never put in an event; it is stored in thecustomer_ordersrow. - A19 (final) Accepted.
PackageLostandPackageReturned(shipping-and-trackingjourney, same topic and consumer group as the other package events;datais{trackingNumber, orderId, declaredAt | returnedAt}) are consumed. They name a package, not a group: the group is the one whoseFulfillmentShippedannounced the tracking number.SHIPPED->LOST/RETURNED(MarkGroupLost,MarkGroupReturned), terminal, withoutcomeTrackingNumberandlostAt/returnedAt. One lost or returned package closes the whole group (the promise is per group; delivery-promise judges it BROKEN on the same event); the packages already delivered stay recorded. A repeat of the same outcome is a no-op (and the inbox makes the same event id a no-op anyway); a different outcome after the first, a delivery, a promise revision, a lock, a shipment or a cancel confirmation for aLOST/RETURNEDgroup, and a loss for aDELIVERED,CANCELLEDor not yet shipped group are recorded and skipped (A7). A package already reported delivered cannot be lost. If the tracking number is not announced yet (cross-topic reordering, like A21) the outcome is stored on the tracking package row (outcome,outcomeAt; stateLOST/RETURNED, a later scan never overwrites it) and replayed, oldest first and after the early deliveries, whenFulfillmentShippedis applied. Decoding is tolerant (unknown fields ignored, a missing instant falls back to the event time); a missingtrackingNumberororderIdis a poison message (DLQ). The tracking view shows the group status,lostAt/returnedAt,outcomePackageand the package state. Returns and refunds stay outside this context. Model sentence: a package declared lost or returned closes its shipment group as LOST or RETURNED, and the order status is derived from its groups. - A20 Group numbers are positive integers chosen by the storefront; line numbers likewise; both unique within the order.
- A21 A
PackageDeliveredfor a group that is not yetSHIPPED(cross-topic reordering) is remembered on the tracking row and reconciled whenFulfillmentShippedis applied. - A22 Wire types: ids strings, instants RFC 3339 UTC, enums UPPER_SNAKE, optional
fields omitted (
apis/asyncapi.yaml). - A23 (final) Order status with LOST / RETURNED groups (derivation table above).
The order enum gains
LOSTandRETURNED(RESTOrderStatus, groupGroupStatus, database checks: migration0002).CANCELLEDgroups do not count. While any counted group is open (PROMISED,LOCKED,SHIPPED,CANCELLATION_REQUESTED) the order is neverLOST/RETURNED; a lost or returned group is shipped progress there (SHIPPED/PARTIALLY_SHIPPED) and a delivered group still givesPARTIALLY_DELIVERED. Once every counted group is closed: all deliveredDELIVERED; some deliveredPARTIALLY_DELIVERED(terminal, the groups say which were lost or returned); none deliveredLOSTif any group is lost, elseRETURNED(a loss outranks a return). Examples, tested inTestDeriveStatus:[LOST]->LOST;[RETURNED, RETURNED]->RETURNED;[RETURNED, LOST]->LOST;[DELIVERED, LOST]->PARTIALLY_DELIVERED;[LOST, SHIPPED]->SHIPPED;[LOST, LOCKED]->PARTIALLY_SHIPPED;[CANCELLED, RETURNED]->RETURNED.CancelOrderon aLOSTorRETURNEDorder isErrStatusRegression, like on aDELIVEREDone. The enum change is additive for REST clients and is a model gap: retail-network-docsOrderStatus/GroupStatusneed the two values. - A24 Saved addresses are rows, each under a stable
addressId(NG1). An address added throughPOST /shoppers/{shopperId}/addressestakes the UUID v5 of (shopper id,Idempotency-Key) in a fixed namespace (like the order id A13), so a replay of the same key — even after the 24 h idempotency record expired — derives the same id and is a no-op returning the stored address; addresses saved at registration get a random UUID v4. The cap of 20 (A15) isErrTooManyAddresses(422too-many-addresses), and the ids are unique per shopper (shopper_addresses, migration0003; addresses stored before the ids existed keep a deterministiclegacy-<md5(shopper, position)>id). No new events: an address is not a fact the network needs. - A25 Checkout by saved address (NG1):
POST /checkouttakes exactly one ofshipTo/shipToAddressId(both or neither isErrValidationFailed). The id resolves against the checkout's shopper: an unknown id, or an id the shopper does not hold, isErrValidationFailed(422validation-failed), checked before delivery-promise is called. The address is COPIED into the order at placement — a later edit or removal of the saved address never changes a placed order — so the stored order and the restricted ship-to route keep their shape. - A26 Order history (NG2):
GET /orders?shopperId=&limit=&cursor=returns the shopper's orders newest first (placed_at DESC,orderId ASCas the tiebreak; indexcustomer_orders_history_idx, migration0003), keyset paginated as{items, nextCursor}:cursoris the opaquenextCursorof the previous page (the fleetpagepackage: base64url JSON of the RFC 3339NanoplacedAtand theorderId; amended by ADR 0010, which retiredafter); an undecodable one is 400invalid-cursor.limitdefaults to 100 and is capped at 500 (400invalid-limit). An unknown shopper is 404; a shopper without orders is an empty page, never an error. The items never carry the address. - A27 Shopper reads (NG1, NG3):
GET /shoppers/{shopperId}returns the profile withsavedAddresses[](possibly empty, oldest first); the addresses are synthetic PII that leave the service only on the shopper routes.GET /shoppers?email=matches on the A16 normalisation (trimmed, lower-cased); an absent or emptyemailmatches nothing and answers 404not-found— the query parameter is required, and "no match" reads more honestly from it than a 400 (least-surprise reading of a silent contract; model gap).
Consequences
- The status derivation is one pure function; every consumed event is a command with a table of legal and illegal transitions.
model_gapsfor the orchestrator: A11 (destination on the promise), A12 (confirm body), A14 (resolved by the cancel route; the residual risk above), A19 and A23 (LOST / RETURNED are new group and order states, absent from the model'sOrderStatus), the model'sGroupPromise/OrderStatusback-fill (A1, A2, A3), and the storefront extension NG1-NG3 (A24-A27): the network model's customer-ordering REST section needs the four routes (POST/GET on shoppers, GET /orders) withSavedAddress,Shopper.savedAddresses,CustomerOrderListand thetoo-many-addressesproblem.
Addendum 2026-11-13: current state
A14's final decision, "no compensation record is kept", no longer describes the code. Since the
"durable promise compensation (A14)" change (PR #44, merged 2026-11-02, commit 9c4fd5d), a
compensating cancel that fails is enqueued, not only logged: PlaceOrder.compensate collects
the failed cancels and enqueueFailedCancels writes them to the compensation_cancels table
(migration 0005_compensation_cancels; internal/application/usecases/checkout.go). The
always-on worker in internal/adapters/outbound/compensation/worker.go (started by cmd/api)
retries each row every COMPENSATION_RETRY_INTERVAL (default 5s) until delivery-promise
releases the hold or answers that the promise is no longer cancellable, and stops claiming a
row after COMPENSATION_MAX_ATTEMPTS failures (default 20, 0 = unlimited). Two residual
cases remain: a cancel is only logged when the enqueue itself fails, and nothing is queued when
the order cannot be stored after every confirm succeeded (only a failed confirm calls
compensate). The rest of A14 is unchanged. Operations: the Runbook's "Compensation queue".