ADR 0002: the ReplenishmentPolicy and ReplenishmentRequest aggregates, their invariants, and the detection policy ports
Status
Accepted (2026-10-17).
Context
ADR 0001 gives this context two jobs: hold the min/max rule of each pick face, and track each decision to move stock until the ledger records it. Stock levels, slot assignments and locations are owned by other contexts and arrive as events (ADR 0003); they are read models here, not aggregate state.
Decision
Domain code lives under internal/domain/ and imports nothing outside
internal/domain/. Four packages: policy, replenishment, detection,
shared (the ECMA-262 blank-string rule that matches the contract's
pattern: '\S').
ReplenishmentPolicy (package policy)
A versioned value object, one per (site, SKU): {SiteID, SKU, MinQty, MaxQty, Multiple, Version}.
- Invariants: site and SKU non-blank;
0 <= MinQty < MaxQty;Multiple >= 1;Version >= 1.Newdeclares version 1,Restorere-validates a persisted row,WithBoundsreturns a new value atVersion + 1(the receiver is left untouched), which backs the RESTIf-Matchoptimistic concurrency. RoundDown(n)returns the largest multiple ofMultiplenot aboven, and 0 whennis below one multiple. A request quantity is rounded DOWN so it never overfills the face; withMultiple == 1the floor is 1 unit. A need smaller than one multiple raises no request.Declaration(at)is the fact behind the analytics-onlyReplenishmentPolicyDeclaredevent.- v1 policy kind is explicit MIN_MAX only. A demand-cover policy is a planned version behind the same shape.
ReplenishmentRequest (package replenishment, the richest aggregate)
One decision to move quantity units of one SKU from one reserve bin to one
forward bin. States: Requested -> Completed | Cancelled; Completed and
Cancelled are terminal. Version is 1 when planned and +1 per transition.
Plan, Complete, Cancel, Snapshot and Restore are the only ways in;
Restore validates everything a snapshot can show, so a corrupt row never
becomes a live aggregate.
Invariants:
- At most ONE open request per (site, forward bin, SKU). The rule spans requests, so the use case enforces it and a partial unique index backs it (wave 2).
quantity >= 1andprojected + quantity <= policy max, whereprojected = on_hand - reserved + open_inboundat detection (checked byPlan).source != forward bin, same site, the source is a reserve-zone bin, andquantity <= source usableat planning (checked byPlan, from theSourcethe application layer builds out of its copies).- Completion only by a
StockRelocatedwhosesource_refequals the request id, whosereasonisREPLENISHMENT, and whose SKU, from bin and to bin match the request; the relocated quantity (at least 1, possibly less than requested) is recorded as moved (checked byComplete). - A request is cancelled with reason
ASSIGNMENT_CHANGEDwhen a newer approved slot plan no longer assigns the SKU to that forward bin; the use case callsCancel. The other reasons areOPERATORandPOLICY_REMOVED.Cancelrejects an unknown reason.
Priority is URGENT when the face had nothing available (on_hand - reserved <= 0) at detection, else NORMAL.
The aggregate raises plain domain events (ReplenishmentRequested,
ReplenishmentCompleted, ReplenishmentCancelled) that carry the request id,
site, the version after the change and the caller's time. They carry no
CloudEvents or Kafka type; the outbound adapter maps them to the wire (ADR 0004).
Levels are not aggregate state
bin_stock_levels, forward_assignments and locations are event-fed read
models (ADR 0003). The application layer reads them, builds a detection.Input
and, when the verdict is Raise, a replenishment.PlanInput.
Detection (package detection)
A pure function Detect(Input, SourceBinPolicy) Outcome: no I/O, no clock, no
randomness; the same input in any slice order gives the same outcome. The input
is the pick face (forward bin, SKU, on hand, reserved), its policy (or none),
the open request for the face (or none) and the candidate reserve bins. The
caller filters reserve bins by site and reserve zone; the detector only ranks.
Rules, in order:
Available = OnHand - Reserved;Projected = Available + open request quantity(0 without one).- No policy: verdict
NoPolicy. - A request is already open:
AlreadyOpen. Projected > MinQty:Healthy.- Otherwise
need = MaxQty - Projected, rounded down to a multiple; the source bin is chosen by theSourceBinPolicy; the quantity is capped at the source's usable stock and rounded down again. No source, or a quantity below 1:NoSource. - Else
Raisewith the source bin, quantity, priority and the policy min and max the request was sized with.
GET /pick-faces derives its status from the same outcome: NO_POLICY for
NoPolicy, NO_SOURCE for NoSource, and otherwise BELOW_MIN when
Projected <= MinQty (a Raise, or an AlreadyOpen face still at or below
min) and OK when Projected > MinQty.
Policy ports (versioned, replaceable)
SourceBinPolicy(Name(),Choose(forwardBin, reserve) (ReserveBin, bool)). v1 isfullest-reserve-bin-v1: among reserve bins other than the forward bin with usable stock (on_hand - reserved) of at least 1, choose the largest usable stock; ties go to the lexically smallest bin code (then the largest on hand, so even a repeated bin code is order-independent). It allows a partial fill (min(need, source usable)), gives the fewest trips, and is deterministic and explainable.- The MIN_MAX rule is the
Policyvalue itself; a demand-cover version would be a second policy kind with its own ADR.
Consequences
- Every decision a planner questions ("why this bin, why this quantity, why URGENT") is answered by the request's own fields and the detector rules above.
- Mutation testing covers every domain package (Makefile
mutationtarget and themutation-fastCI step loop overpolicy,replenishment,detectionandshared); thresholds are in.gremlins.yaml. - The one-open-request-per-face rule is not in the aggregate: wave 2 enforces it in the use case and with a partial unique index.
- A partial fill leaves the face below max; the next detection sweep raises a new request once the first one closes.