ADR-0015: Rebalance Policy and Transfer Lane catalogues
Status: Accepted
Context
ADR 0007 added the scheduled rebalance pass (NIP_REBALANCE_SCHEDULE) and
left one thing for later: "no lane/policy catalogue projected yet (a later
phase wires the lane catalogue)". The pass called the planner with an empty
[]Policy and []Lane, so every run was a COMPLETED run with zero
proposals. The ticker existed but could never propose anything.
The planner itself (transfer.Planner, ADR 0001) already reasons with a
Policy (site + SKU: safety stock, target stock, unit priority, version) and
a Lane (origin, destination, lead time, unit handling cost, enabled).
POST /v1/transfer-proposal-runs accepts both on every call. No sibling
context owns either concept: NIP is the only bounded context that reasons
about inter-site transfer economics. So no fact exists to project from, and
the catalogue has to be NIP's own data, managed by an operator.
NIP also holds no per-site stock position, because no sibling publishes
one. The scheduled pass therefore cannot run the position-based
Planner.Generate that the generate endpoint runs over a caller-supplied
snapshot.
Decision
-
Two NIP-owned catalogues. These are persisted by additive migration
0005_rebalance_catalogue:Table Key Holds rebalance_policies(site_id, sku)Policytransfer_lanesdirected (origin_site_id, destination_site_id)LaneEach row carries a
revision, which starts at 1 and gains 1 per accepted change, and anupdated_at. The domain types aretransfer.RebalancePolicyandtransfer.TransferLane.Validaterefuses an entry on its face withErrInvalidCatalogueEntryin these cases:- a missing key;
- a negative quantity or cost;
TargetStock < SafetyStock;- a lane to itself;
- a revision that changes the key.
-
REST, with the existing shapes. The routes are:
GET /v1/rebalance-policiesandPUT /v1/rebalance-policiesGET /v1/transfer-lanesandPUT /v1/transfer-lanes
A PUT body is exactly the existing
PolicyorLaneschema, the same JSON the generate endpoint accepts, and is keyed by its own Site + SKU or Origin → Destination. Responses embed that shape as{policy|lane, revision, updatedAt[, outcome]}. -
PUT is idempotent and uses optimistic concurrency.
Situation Result The PUT carries exactly the stored entry Nothing is written; 200 UNCHANGEDA new entry 201 CREATED, revision 1A changed entry 200 UPDATED, revision + 1If-Match(theETag, i.e. the revision) is sent but the entry moved on or does not exist412 version-mismatchIf-Matchis omittedThe PUT applies to the current revision Two writers race on the same revision The guarded write ( INSERT … ON CONFLICT DO NOTHING/UPDATE … WHERE revision = $n) lets exactly one win; the other gets 409concurrent-modificationThe entry is invalid 422 invalid-catalogue-entryThe instance has no database 503 catalogue-unavailableThere is no DELETE. A lane is retired with
Enabled: false. A site stops receiving a SKU when its policy hasTargetStock: 0. -
The scheduled pass plans from the catalogues. The new
Planner.PlanFromSnapshot(snapshot, policies, lanes)reuses the site-level approximation that approval validation (ADR 0003) enforces, so every scheduled proposal is approvable as generated:- Lane usability. A lane is usable only when it is
Enabledandsite_capabilitylets its origin send and its destination receive. The fail-closed snapshot (ADR 0002) already excludes sites without complete facts, and it fails the whole run when a participating site is disabled for a direction. That makes it a FAILED run with the reason, the same as the simulation and the approval. - Need. The destination's need for a SKU is its in-window ACTIVE
demand, capped by its policy's
TargetStock. - Donation. The origin donates from its capacity headroom (capacity
over the window minus its own in-window demand) and keeps its policy's
SafetyStockback. An origin without a policy for the SKU never donates. - Allocation. Candidates are served greedily, highest score first, against shared need and headroom budgets, so one pass never promises the same units twice. The order is deterministic: score, then SKU, origin, destination.
- Rejections. A candidate that fails
Proposal.Valid, for example one whose handling and lead-time cost outweigh its benefit, is counted in the run'srejectedCount. - Unreadable catalogue. An unreadable catalogue is a FAILED run
(
load rebalance catalogue: …), never a silent zero-proposal run.
The pass stays observe-only (ADR 0007): it records the run and publishes
RebalanceRunCompletedwith the counts. It never approves, never allocates and never releases work. - Lane usability. A lane is usable only when it is
-
The generate endpoint is unchanged: it still plans over a caller-supplied snapshot of positions, policies and lanes.
Consequences
-
The scheduled run proposes once an operator fills the catalogues. With empty catalogues it behaves exactly as before: COMPLETED with zero proposals.
-
Operators get an auditable, concurrency-safe catalogue API. A stale dashboard cannot overwrite a newer edit when it sends
If-Match. -
The scheduled planner is position-blind by construction. If a sibling ever publishes stock positions, a later ADR can project them and switch the pass to the position-based planner without changing the catalogue API.
-
New tests:
Test Covers domain catalogue_test.govalidation and revision rules domain scheduled_planner_test.gocapability gate, budgets, greedy sharing, determinism use-case tests idempotent PUT, If-Match, lost race, scheduled run proposing, catalogue outage as a FAILED run HTTP handler and OpenAPI contract tests the REST surface against the spec postgres_integration/catalogue_integration_test.goa real two-writer race against Postgres 16 features/rebalance_catalogue.featureBDD for the scheduled run producing proposals
Follow-ups
- Proposed:
InterWarehouseTransfer.Cancel(D7). The aggregate'sCanceltransition stays reserved. The domain allows it from DRAFT, PROPOSED, APPROVED and ALLOCATING, but it has no route, no use case and no published event. No ADR yet specifies who may cancel a transfer, or how compensation works. Compensation means releasing an origin reservation that inventory-storage may already hold for an ALLOCATING transfer whose reply is in flight, and withdrawing released work demand. A future ADR must define the cancel command, its authorization, the compensation messages to inventory-storage and wes-work-planning, and the analytics event. Until then no transfer reaches CANCELLED; a stuck transfer surfaces through the saga health check (ADR 0007). - Not planned. A catalogue change event (for example
RebalancePolicyChanged) has no consumer today and is not published.
Addendum 2026-11-13: current state
The cancel follow-up above was built by ADR 0022 (PR #103): the use case
CancelTransfer (internal/application/usecases/cancel_transfer.go), the route
POST /v1/transfers/{transferId}/cancellation and the command
TransferAllocationReleaseRequested to inventory-storage, so transfers do reach
CANCELLED. The cancel publishes only that command and the
TransferStateAdvanced occurrence (cancel_transfer.go:160-168); no event
withdraws an already released pick WorkDemandReleased.