Skip to main content

Sourcing policy

The versioned SourcingPolicy (ADR 0014): a cross-region surcharge and a regional cost-deviation budget the candidate search reads once per search. Everything defaults to 0 (off) until an operator publishes a version.

📄️Introduction

REST contract of the order-sourcing bounded context of the retail network, derived from retail-network-docs model/model.yaml (contexts[order-sourcing].rest) and adjusted by the cross-context decisions G5, G10, G12, G13, G14, G20 and G26. Order Sourcing decides which vendor node and path fulfil each shipment group: it keeps a FulfillmentAssignment per group in a virtual queue, re-optimises it until its release point, releases it to one node (a purchase order through vendor-connectivity), reassigns on rejection or expiry, and locks it when the node accepts. This surface is the network-ops planner's: list and inspect assignments, ask for an immediate re-optimisation of a QUEUED one, and read and publish the versioned sourcing policy (ADR 0014). Every other change arrives as a Kafka event (see apis/asyncapi.yaml). No PII is held or returned by this context. No authentication, consistent with the rest of the network. Errors are RFC 9457 problem details whose type is the URI https://errors.order-sourcing.retail-network.example/ followed by the slug (for example .../assignment-locked); detail and instance are always set. The shared problem types are not-found (404), resource-not-found (404, a path that names no resource), method-not-allowed (405), malformed-request (400), illegal-transition (409), concurrent-update (409), validation-failed (422) and internal-error (500); the idempotency problems are idempotency-key-required and idempotency-key-invalid (400) and idempotency-key-reused (409); the model's Err names add assignment-locked, release-in-flight, node-excluded, stale-assignment-version (409) and candidate-breaks-promise (422), and policy-version-not-next (409, PUT /sourcing-policy only: the stored policy version cannot be incremented). The read-only analytics reports (tag reports, ADR 0013) are served by cmd/os-reports on its own port (per-path server) from the separate analytical database; their range problem is invalid-report-range (400). A repeat with the same Idempotency-Key and the same request replays the first response for 24 hours with the Idempotency-Replayed header when that response is final: a 2xx, or a refusal the same request cannot pass later (malformed-request, assignment-locked). A refusal that depends on state that can change is not stored: the key is released and the same request retried under the same key runs again (a different body under a released key is a new request, not idempotency-key-reused). The problem types that release the key (RETRYABLE): release-in-flight, node-excluded, stale-assignment-version, candidate-breaks-promise, not-found, concurrent-update, illegal-transition, validation-failed. A 5xx is never stored (ADR 0019).

📄️Cost to serve per unit, per day

Over the LAST sourcing decision of each assignment (highest releaseNo), per UTC day of its decidedAt: costCompleteShare = decisions with a complete estimate / decisions; costPerUnitCents = the sum of estimatedCostCents of the complete decisions / the sum of their units (null when undefined); p50CostCentsPerGroup and p90CostCentsPerGroup = the continuous percentiles of estimatedCostCents per group over the complete decisions (null without one). Estimates are planner-declared cents per package-unit (ADR 0011). Days without a decision are omitted.

📄️Sourcing mix (in-region share, legs, reassignments), per day

Per UTC day of release (decidedAt of FulfillmentSourcingDecided): groupsReleased = the releases of that day; inRegionShare = releases whose node is in the destination's region / releases with both regions known, with unknownRegionCount = releases with an unknown region; meanLegs = the departures after the pickup per release; reassignmentRate = releases with releaseNo > 1 / groupsReleased; unsourceableCount = the FulfillmentUnsourceable events of that day; splitShipment = the split-shipment rate of the day (ADR 0018): the sourced orders whose first sourcing decision is on that day, those released as 2 or more shipment groups (a follower merged into its carrier shares the carrier's shipment) and the distribution of shipment groups per order over the buckets 1, 2, 3 and 4+. A day appears when it has a release or an unsourceable declaration.

📄️Publish the next sourcing policy version (PublishSourcingPolicy)

Replaces the whole policy document: validates every range, stores version n+1 (n is the highest stored version, 0 when none) and raises the analytics-only SourcingPolicyPublished event in the same transaction. An omitted field takes its default, 0 (the lever is off); an empty body is 400. Every call publishes a NEW version, so it takes an Idempotency-Key like a POST: the same key with the same body replays the first answer for 24 hours instead of publishing twice; a retryable refusal (validation-failed, concurrent-update) is not stored, so the retry under the same key publishes; policy-version-not-next (the policy version is at its ceiling) is stored and replayed instead. The same key with another request that was stored is 409 idempotency-key-reused. Searches that started before the publish keep the version they read.

📄️Re-optimise a queued assignment now (ReoptimiseAssignment)

Re-runs the candidate search (AssignmentCost = legs + crossRegionPenalty + scorecardPenalty + bookPressurePenalty, ties to the witness node) over the current read models, skipping excluded nodes and any candidate whose eta is after deliverByLatest. Allowed only while QUEUED. If the best node changes the assignment moves (FulfillmentReassigned is published) and releaseAt follows the new candidate (VirtualQueue); if no candidate is left the assignment becomes UNSOURCEABLE at once (FulfillmentUnsourceable, gap G26). A repeat on unchanged read models changes nothing. A retry with the same Idempotency-Key replays the stored response for 24 hours when it is final (a 200, malformed-request or assignment-locked); a refusal that state can still change is not stored and the retry runs again. The same key with another request is 409 idempotency-key-reused.