ADR 0002: the VendorAccount, PurchaseOrder and Submission aggregates, their invariants and the assumptions the model left open
Status
Accepted (2026-10-23). Design source: retail-network-docs model/model.yaml, context
vendor-connectivity (aggregates, policies, rest, publishes) and the pack pages
aggregate-design-canvas, use-cases, domain-events; docs/integration/vendor-api.md;
contracts-delta.md (E2, E4, E17) and Gap Register items G2, G4, G12, G30, G31, G32.
Context
The model gives three aggregate roots, each its own transaction boundary: VendorAccount
(vendorId), PurchaseOrder (poNumber) and Submission (transactionId). The
purchase-order protocol is a conversation: the vendor writes, the write becomes a
Submission that is processed later, and the outcome is applied back onto the order.
The model is silent on a number of details; every one the implementation had to decide is an
assumption below, flagged A<n>, and is reported to the orchestrator as a model gap.
The model is not edited here.
Decision
Layout and conventions
internal/domain/{shared,vendor,purchaseorder,submission,network}. Pure Go: no I/O,time.Nowand id generation are never called;nowand ids are passed in. Dependency direction: every package may useshared; no aggregate imports another.shared:ErrValidation(problemvalidation-failed, 422),ErrIllegalTransition(illegal-transition, 409),ErrInvalidSnapshot(an internal error), theEventinterface and id/text checks.ErrNotFoundandErrConcurrentUpdateare NOT domain errors: they are the port sentinels ofinternal/application/ports(not-found404,concurrent-update409).- Each aggregate has a constructor or command method per model command,
Snapshot(), a validatingRestore(Snapshot)and an optimisticVersion(). A command returns the raised events; a save expectsstored version == Version()-1(0for a create). Events are typed structs whose fields are the model'sdatalist (ADR 0004). networkholds the two event-fed read models the decisions need:Node(the model'sVendorNodeView) andListing(ListingView). They are domain values (they carry the ownership rule) rebuilt from consumed events; see ADR 0003.- Bounded integers that reach Postgres are plain literal constants (
const MaxInt32 = 2147483647).
VendorAccount
States ACTIVE, SUSPENDED; SuspendVendor ACTIVE -> SUSPENDED, ReinstateVendor
SUSPENDED -> ACTIVE. OnboardVendor creates an ACTIVE account and raises VendorOnboarded;
suspend and reinstate raise VendorSuspended and VendorReinstated (Gap G30).
| Invariant | Err | Problem type | Enforced by |
|---|---|---|---|
| ackWindow between 1h and 24h (60..1440 minutes) | ErrAckWindowOutOfRange | ack-window-out-of-range 422 | vendor.Onboard |
| a vendor acts only on its own nodes | ErrNodeNotOwnedByVendor | node-not-owned-by-vendor 404 | network.Node.EnsureOwnedBy (the owner is on the node, the rule is the vendor's) |
| a suspended vendor's writes are refused (G30) | ErrVendorSuspended | vendor-suspended 409 | vendor.Account.EnsureActive |
PurchaseOrder
States ISSUED, ACK_PROCESSING, ACCEPTED, REJECTED, EXPIRED, SHIPMENT_PROCESSING,
SHIPPED, CANCELLED. Transitions (command): ISSUED -> ACK_PROCESSING (SubmitAcknowledgement);
ACK_PROCESSING -> ACCEPTED | REJECTED | ISSUED (ProcessSubmission: the decision succeeded as
ACCEPT or REJECT, or the transaction failed); ISSUED -> EXPIRED (ExpireUnacknowledgedOrders);
ISSUED -> CANCELLED (CancelPurchaseOrder); ACCEPTED -> SHIPMENT_PROCESSING
(SubmitShipmentConfirmation); SHIPMENT_PROCESSING -> SHIPPED | ACCEPTED (ProcessSubmission).
RequestLabel changes no state; it adds labels to an ACCEPTED order. Every other
command/state pair is an illegal transition (A7).
| Invariant | Err | Problem type | Enforced by |
|---|---|---|---|
| an acknowledgement covers every line with one decision (fill-or-kill) | ErrPartialAcknowledgement | partial-acknowledgement 422 | PurchaseOrder.SubmitAcknowledgement |
| acknowledged at most once | ErrAlreadyAcknowledged | already-acknowledged 409 | same |
no acknowledgement after ackBy | ErrAckWindowClosed | ack-window-closed 409 | same |
| no label before ACCEPTED | ErrLabelBeforeAcceptance | label-before-acceptance 409 | PurchaseOrder.AddLabels |
| no shipment confirmation without a label for every package | ErrConfirmWithoutLabel | confirm-without-label 409 | PurchaseOrder.SubmitShipmentConfirmation |
| cancel only while ISSUED | ErrCancelAfterAcknowledgement | cancel-after-acknowledgement 409 (never reaches HTTP) | PurchaseOrder.Cancel |
Submission
States PROCESSING, SUCCESS, FAILURE; ProcessSubmission PROCESSING -> SUCCESS | FAILURE.
SubmissionKind: ACKNOWLEDGEMENT, INVENTORY, NODE_REGISTRATION, NODE_PROFILE,
SHIPMENT_CONFIRMATION.
| Invariant | Err | Problem type | Enforced by |
|---|---|---|---|
| outcome set once | ErrSubmissionAlreadyProcessed | submission-already-processed 409 (internal) | Submission.Complete |
processing never starts before notBefore = receivedAt + delay | ErrSubmissionNotDue | submission-not-due 409 (internal) | Submission.Complete |
Assumptions where the model is silent
- A1
poNumberisPO-+ the first 16 hex digits ofsha256(assignmentId + ":" + releaseNo): deterministic, so a redeliveredFulfillmentReleasedToNodefinds its order; a unique index on(assignment_id, release_no)is the second guard. No consumer parses apoNumber. - A2 Rules this context can check at receipt are refused synchronously and create no
transaction (ownership, suspension, state, window, fill-or-kill, body shape). Refusals by a
neighbour during processing end the transaction in
FAILUREwith the neighbour'sErr...asSubmissionError.code(the neighbour's problem slug in PascalCase with anErrprefix). - A3 A node with no declared operational profile has handling time 0, so
ackBy = min(issuedAt + ackWindow, requiredShipBy). - A4 A release for a node that
VendorNodeViewdoes not know yet is a retryable error, not a dropped event (the topics are separate, so node-topology's event can lag). The consumer retries with backoff and dead-letters when the budget is exhausted. A release for a node the view knows to beNETWORK-operated is ignored (inbox row written). - A5 An
ackByalready in the past at issue time is accepted as computed; the sweep expires the order on its next pass. - A6
SubmitAcknowledgementchecks in this order: state, window, completeness (every line once, ordered quantity), decision (REJECT needs areasonCode; ACCEPT must not carry one). The first failure wins. The window is judged on the receipt instant andnow == ackByis still inside it. - A7 States without a dedicated
Err...: an acknowledgement of aCANCELLEDorder, a label for aREJECTED/EXPIRED/CANCELLEDorder, a shipment confirmation for aSHIPMENT_PROCESSING,SHIPPED,REJECTED,EXPIREDorCANCELLEDorder areErrIllegalTransition(illegal-transition). A label before acceptance (ISSUED,ACK_PROCESSING) isErrLabelBeforeAcceptance; a shipment confirmation of anISSUEDorACK_PROCESSINGorder isErrConfirmWithoutLabel(no label can exist yet). A repeated label request for packages that already hold labels returns them in any ofACCEPTED,SHIPMENT_PROCESSING,SHIPPED. - A8 Repeating a command that is already in effect is a no-op, not an error: onboarding an
existing
vendorIdreturns the account unchanged (200), suspending a suspended vendor and reinstating an active one return the account unchanged (200); no event is raised. - A9 A suspended vendor's writes (acknowledgement, inventory, node registration, profile,
label, shipment confirmation) are refused with
vendor-suspended. Reads, polling, the issuing of new purchase orders, the expiry sweep and the processing of submissions received before the suspension continue. - A10 Neighbour outcomes during
ProcessSubmission: a2xxisSUCCESS; a4xxisFAILUREwith the neighbour'sErr...; a5xx, a timeout or a refused connection leaves the submissionPROCESSINGfor the next pass (one attempt is counted per pass); afterSUBMISSION_MAX_ATTEMPTS(default 5) it endsFAILUREwithErrUpstreamUnavailable. The simulated failure (TX_FAILURE_RATE) endsFAILUREwithErrSimulatedFailure. Two neighbour answers are success: node-topology replaying an idempotent registration, and offer-availabilityfeed-already-applied(the feed id is the transaction id, so the feed was applied by an earlier attempt). - A11 A successful
NODE_REGISTRATIONalso upserts the node intoVendorNodeViewfrom the node-topology response, in the same transaction that completes the submission, so the vendor can declare a profile the moment it readsSUCCESS. The node's own events later merge into the same row (they never lower a profile revision or a status). - A12 An inventory feed line for a listing
ListingViewknows to belong to a different node is refused at receipt (validation-failed). A listing the view does not know yet is passed through: offer-availability decides (ErrListingNotOnNode). - A13
VendorNodeViewkeeps every node node-topology announces, including network-operated ones, so a release for a parcel node is recognised and ignored instead of retried (A4). - A14
VendorSuspendedandVendorReinstatedare integration events onretailnetwork.vendor-connectivity.events; no context consumes them yet. Suspension takes an optionalreason. The routes arePOST /vendors/{vendorId}/suspensionsandPOST /vendors/{vendorId}/reinstatements, plusGET /vendors/{vendorId}, for partner managers (the model names no route for either command). - A15
ErrCancelAfterAcknowledgementis logged at INFO and the inbox row is written; the event is not retried and nothing is published. order-sourcing decides the cancel (E11, Gap G12) and learns the order's fate fromPurchaseOrderAccepted. - A16
CancelOnSourcingCanceladdresses the order with the highestreleaseNoof theassignmentId(the open release; order-sourcing allows one pending release). No order for the assignment is a no-op with an inbox row. - A17 Polling pages are keyset pages ordered oldest first by
(issuedAt, poNumber);createdAfteris strict onissuedAt; the opaque base64urlcursor(returned asnextCursor, ADR 0010) carries the last(issuedAt, poNumber); the page size is 100 by default (limit1..500). The poll returns orders in every state so the vendor can reconcile. - A18
PO_REDELIVERY_RATE(0..1): each non-empty page repeats its first order with that probability.TX_PROCESSING_DELAY(duration),TX_FAILURE_RATE(0..1) andRATE_LIMIT_RPS(0 disables) complete the model's four simulation knobs; all default to well-behaved. - A19
RequestLabelcalls shipping-and-trackingPOST /labelsonce per requested package with the Idempotency-Keyvc-label-<poNumber>-<packageNo>, and the body{poNumber, packageNo, assignmentId, orderId, tier}(Gap G2;orderIdandtierstay inside this service). Labels already issued are kept when a later package fails; the answer is then503 upstream-unavailableand a retry returns the stored labels and issues only the missing ones. - A20 A shipment confirmation names every tracking number of the order's labels, and only
those (set equality); anything else is
ErrConfirmWithoutLabel.shippedAtis required. - A21 The rate limit is one token bucket per process over
/vendor/*(burst = max(1, RPS)). Over the limit the answer is429rate-limitedwithRetry-Afterin whole seconds. - A22 Submissions are retained 30 days (the model's size estimate) and then purged by a daily sweep.
- A23 Reads take
vendorIdas a required query parameter (Gap G32). A resource of another vendor isnode-not-owned-by-vendor(404); a transaction of another vendor isnot-found. - A24 The in-organization base URLs of the four sync calls (
NODE_TOPOLOGY_URL,OFFER_AVAILABILITY_URL,SHIPPING_AND_TRACKING_URL,NODE_PERFORMANCE_URL) are mandatory settings like the database and broker: a missing one is a boot error naming it. - A25 While an acknowledgement is
ACK_PROCESSINGthe order keeps the pending decision (decision,reasonCode,transactionId); the vendor-facingacknowledgementappears only when the transaction succeeded (the OpenAPI text). A failed transaction clears it and returns the order toISSUED. - A26 The expiry sweep expires an
ISSUEDorder withackBy < now(strict); an order inACK_PROCESSINGis never expired. A transaction that fails afterackByreturns the order toISSUEDand the next pass expires it. - A27 The model's "one aggregate per transaction" is relaxed in exactly one place, as the pack
proposes:
ProcessSubmissionsaves theSubmission, thePurchaseOrderit concerns and their outbox rows in one transaction, so a vendor that readsSUCCESSalways finds the order in its new state. Receipt does the same for the acknowledgement and shipment confirmation.
Consequences
- Every invariant above has a table-driven domain test; the mutation gate runs over each
internal/domain/*package. - The REST error mapping is a single table (
internal/adapters/inbound/http/errors.go). - The assumptions are reported as
model_gapsfor the orchestrator to fold into the model.
Addendum 2026-11-19: released refusals (A10)
A10 above is the decision as it was taken. Since ADR 0016 a 4xx that the callee RELEASES under the
Idempotency-Key (replay class RETRYABLE, per-callee table in httpclient/refusal_classes.go) no longer
ends the submission: it stays PROCESSING, is retried with the same key on the next pass, counts as an
attempt, and ends FAILURE with that refusal's own Err... code at the same attempt cap. Every other 4xx,
and every slug the table does not know, is FAILURE at once as A10 says.