ADR 0014: a versioned sourcing policy
Status
Accepted (2026-11-09). Extends ADR 0011 (the comparator, clause 4) and ADR 0004 (catalogue). Refines gap G26. Additive and default-off: until a policy is published the ordering, the events and the REST views are unchanged. The split parts of the original contract are NOT implemented (see "What is not decided here").
Context
Money-denominated cost (ADR 0011) made the cross-region penalty only a
tie-break behind cents, so a cheaper cross-region lane always wins. The
operator needs a lever to trade a bounded cost deviation for in-region
fulfilment. The lever must be versioned and auditable (which policy was in
force when a group was released), it must not add a synchronous call to the
search, and it must be inert until somebody publishes it. delivery-promise
already solved the same shape with its PromisePolicy aggregate (one current
version, every publish creates version n+1, versions are kept); this context
copies that model.
Decision
- The aggregate.
SourcingPolicyis a single versioned document: one current version, every publish creates version n+1, versions are kept, version 0 is the built-in default (nothing published). It is modelled exactly like delivery-promise'sPromisePolicy: a Snapshot struct,New-equivalentPublish, a validatingRestoreand aSnapshot()builder, a repository port (Current,Save) with an in-memory double and a Postgres adapter that pass one repocontract suite, and a migration. Fields, all integers:crossRegionSurchargeCents(0..100000, default 0): added to the effective cost of a candidate whose node region differs from the delivery area region (both known).maxCostDeviationPercent(0..200, default 0): the regional budget of clause 3.
- Effective cost.
effectiveCostCents = estimatedCostCents + surcharges. Surcharges apply even when the estimate is incomplete (incomplete stays reported, not acted on, ADR 0011 #5). - The comparator extends ADR 0011 #4; nothing else about it changes. Let
cheapestbe the minimumeffectiveCostCentsamong the candidates. Budget =cheapest + floor(cheapest * maxCostDeviationPercent / 100). Among candidates within the budget an IN-REGION candidate beats a cross-region one; then effective cents, then the legacy ordinal tie-break and the id tie-breaks exactly as today. Candidates over the budget follow, by effective cents. The budget rule is active only whenmaxCostDeviationPercent > 0: withmaxCostDeviationPercent = 0and the surcharge 0 the ordering is byte-identical to today (a table test over the search fixtures proves it). "In-region" needs both regions known, as for the legacy cross-region penalty; a candidate with an unknown region counts as in-region. - The policy is an Input to the pure search. The sourcing use case reads
the current policy once per search through the repository and passes it in
sourcing.Request; the domain makes no call. The version read is stamped on the candidate and so on the release. - REST.
GET /sourcing-policyreturns the current version, or the defaults withversion: 0when nothing was published.PUT /sourcing-policyvalidates the ranges (422validation-failed, problem+json), publishes version n+1 and returns it. The body is the whole document (an omitted field is 0, an empty body is 400). Because every call publishes a new version, the route sits behind theIdempotency-Keymiddleware like a POST, so a retried publish replays instead of publishing twice; a publish that loses a race for the same version number is retried once, then 409concurrent-update. No MCP change. - The analytics-only event
SourcingPolicyPublished. Typecom.retailnetwork.commerce.order-sourcing.policy.SourcingPolicyPublished, topicretailnetwork.order-sourcing.analytics, dataschemaurn:retailnetwork:order-sourcing:analytics:SourcingPolicyPublished:v1, subject (and Kafka key) =policy, data = the policy fields +version+publishedAt. It is raised in the same transaction as the save (outbox). FulfillmentReleasedToNodegains optionalpolicyVersion(integer, present only when a policy version >= 1 was in force, omitted otherwise) and optionaleffectiveCostCents(present only when a surcharge changed the cost, that is when it differs from the estimate's cents; omitted otherwise). Additive and optional; consumers decode tolerantly, so no consumer change is expected. The REST candidate view carries both under the same omission rules, and the analytics-onlyFulfillmentSourcingDecidedcarries both too (it already carriesestimatedCostCents).- Metrics. An observable gauge
ordersourcing_sourcing_policy_version(the version last read) and a counterordersourcing_budget_preferred_total{kind="region"}incremented, once per release, when the budget rule changed the winner relative to the pure cheapest ordering.
What is not decided here (split surcharge, single-node preference)
The contract also asked for splitSurchargeCents (added once per additional
shipment when an order's lines are sourced from more than one node) and a
single-node-beats-split preference in the budget. Order-sourcing has no notion
of splitting an order across nodes: customer-ordering decides the shipment
groups, this context assigns each group on its own (one FulfillmentAssignment
per orderId + groupNo), and ResolveLines already requires ONE node to ship
every line of the group. A search never compares a single-node plan against a
split plan, so there is nothing for such a surcharge or preference to act on
without inventing an order-level planner. Both parts are therefore NOT built,
splitSurchargeCents is NOT a field of the policy, and the budget metric has
only the region kind. Revisit when sourcing plans across the groups of one
order.
Consequences
- Behaviour is unchanged until an operator publishes a policy with
PUT /sourcing-policy. A policy with both fields 0 is the default. - With
maxCostDeviationPercent = 20a cross-region lane that is cheapest loses to an in-region one inside 120 % of its effective cost and wins outside it; the surcharge makes the cross-region lane dearer in the cents themselves. - The persisted candidate (jsonb column) now also carries the estimate, its
completeness, the effective cost and the policy version, all optional; rows
written before this ADR decode with zeros, so no data migration touches the
assignments table. Migration 0005 only adds
sourcing_policies. - Every publish raises one analytics event; the projector ignores it (acknowledged silently) until a report needs it.
- Deploy: no environment variable, topic or chart change; the analytics topic already exists.
Addendum 2026-11-20: idempotency replay classes
Point 5 says a retried publish replays. That holds for a 2xx; a refusal of PUT /sourcing-policy (validation-failed, concurrent-update) is RETRYABLE and releases the key: the retry under the same key (or any body under it) runs the publish again, and the 409 text "retry with a new Idempotency-Key" no longer applies. The Decision text above is unchanged; ADR 0019 holds the rule and the class of every problem type.