ADR 0004: Demand is ingested from order-management's published events into a local read model; assigned_demand becomes optional
Status
Amended by ADR 0016: the consumer is always on and
DEMAND_CONSUMER_GROUP/DEMAND_SITE_IDare required (the off switch below no longer exists).Amended by ADR 0021: the allocation events now carry an optional
site_id(order-management ADR 0050); §4's exit is taken, so an order is attributed to that site andDEMAND_SITE_IDis the fallback.
Accepted (2026-10-04). Closes the "Phase 4 simplification" recorded on
CreateCapacityPlanCommand and in ADR 0001 (demand arrives in the request
body, final ingestion "a later decision"). Additive: nothing that worked
before changes.
Context
POST /capacity-plans evaluates assigned_demand (orders) against a path's
capacity. Nothing supplied that number: the operator typed it.
order-management is the fleet's order front door and PUBLISHES CloudEvents
with order facts. This context's cross-context rule is unchanged: no live
REST/MCP call to a sibling, published events only, local read models.
What the events actually carry (read from order-management's own
apis/asyncapi.yaml on develop, not guessed)
Its integration topic warehouse.order-management.events carries exactly
three types:
| type | meaning | payload (data) |
|---|---|---|
...order.OrderAllocated | every line allocated; eligible lines released in the same pass | order_id, promise_date (the promise cutoff instant; for a multi-group order the LATEST group's cutoff), lines[] (exactly the lines released in this pass, may be empty; each line_no, sku, path_id, gift_wrap, fulfillment_class, optional per-line promise_cutoff_at), optional promise_basis / promise_cpt_id |
...order.OrderPartiallyAllocated | some lines allocated and released, some backordered (partial-shipment orders only) | same shape |
...order.OrderRepromised | the promise of a shipment group moved | order_id, reason, optional cpt_id_old / cpt_id_new -- no new instant |
CloudEvents subject = the order id, time = occurred-at, key = order id.
Its analytics topic (warehouse.order-management.analytics) additionally
carries OrderReceived (line_count), OrderCancelled, OrderReleased, ...
but that topic is the analytics data product's stream (its own ADR 0006: the
projector is its sole consumer), not an integration contract.
What is therefore not available on the integration contract:
- No fulfillment site. Neither
Order/OrderLinenor any event carries one. order-management's ADR 0031 says so explicitly and matches its planned-capacity read model against ONE configured site (PLANNED_CAPACITY_SITE_ID, defaulting to itsDEFAULT_SITE_ID). - No quantities. A line has no unit count; there is no
unitsfigure to sum.lines[]counts lines, not units. - No cancellations or de-allocations.
OrderCancelledis analytics-only. (Its BR6 -- order-management ADR 0004 -- also makes cancellation legal only while no line has reachedReleased, andOrderAllocatedreleases every eligible line in the same pass, so a cancelled order that was already announced on the integration topic is the narrow case of an allocation whose lines were all ineligible.) - No new cutoff on a re-promise.
OrderRepromisednames CPT ids, not an instant.
Decision
1. A local read model fed ONLY by Kafka, in orders
internal/domain/demand holds the rules; order_demand (migration 0006,
purely additive) holds one row per order id: order_id, location,
promise_at, released_lines, as_of. There is no call to order-management
anywhere; a plan it never published an order for has no demand here.
2. What "expected demand in window W at site L" means
The number of distinct orders attributed to site
Lwhose promise cutoff (promise_date) lies in the half-open window[W.start, W.end)-- a cutoff exactly atW.startcounts, exactly atW.enddoes not (so adjacent windows never count an order twice) -- counted once per order id, at the cutoff of its latest event.
- Last writer wins per order id on the CloudEvents
time(demand.Order.Supersedes): a later or equaltimereplaces the stored row (so a re-allocation that moves the promise moves the order between windows, and a same-time redelivery under a new id converges instead of depending on arrival order); an older one is a no-op. released_linesis returned besideordersasreleased_linesand is explicitly not units. Units are not supported: no event carries them, and inventing one (e.g.lines x something) would be a made-up number.- The figure
POST /capacity-plansuses isorders-- the plan's unit is already ORDER (assigned_demandis in orders,path_capacityin ORDER/hour). - No data is not zero. A window with no order (or a site with none) yields
orders = 0; for the plan that is no data, never a silent zero (decision 6). as_of= the newesttimeamong the events the site's model reflects (whole site, not only the window), so a reader can judge staleness.
3. Events consumed and events ignored
Consumed (dispatch on the full type, topic warehouse.order-management.events):
com.warehouse.wes.order-management.order.OrderAllocatedcom.warehouse.wes.order-management.order.OrderPartiallyAllocated
Both carry promise_date and the released lines; both upsert the order. An
order with zero released lines is still an order (the promise is what places
it).
Ignored, and why:
OrderRepromised: carries no new cutoff instant, so it cannot move an order between windows. Consequence (accepted, documented): after a re-promise the model keeps the order at its original cutoff until a laterOrderAllocated/OrderPartiallyAllocatedfor the same order (e.g. a retried allocation) replaces it.- Cancellations / de-allocations: not supported, so the model is NOT netted.
They are not on the integration topic. Consuming
OrderCancelledwould mean subscribing to the analytics data product's stream, a second topic and consumer for an event that BR6 makes unreachable for a released order. The model therefore can overstate demand by the orders announced and then cancelled before release. A test that "a cancelled order lowers demand" is deliberately absent: the capability does not exist. OrderReceived/OrderReleased/OrderLine*/OrderCancelled(analytics topic only), per-linepromise_cutoff_at(the order-levelpromise_dateis used: one cutoff per order, so one order is one count), and any unknown type.
Validation (anything failing is skipped with a WARN, offset committed,
never retried, never blocking the partition): not a CloudEvents 1.0 event
(including the retired flat envelope), malformed payload, blank order_id,
subject != data.order_id, missing/unparsable promise_date, missing event
time. Validation runs before the transaction opens.
4. The site assumption (stated, not hidden)
order-management orders carry no fulfillment site. We do not invent a
per-order site (no SKU prefix, no reservation-id parsing). Exactly like
order-management's own ADR 0031, all consumed demand is attributed to ONE
configured site: env DEMAND_SITE_ID (e.g. SIM1, the location this
service already uses for the labor and facility data). It is stored on each row
as location at write time. If the events later carry a usable site, the
consumer should read it and DEMAND_SITE_ID becomes a fallback. If
DEMAND_SITE_ID differs from the location a plan uses, the plan finds no
demand and the 422 below applies -- it never defaults silently. Changing
DEMAND_SITE_ID leaves previously written rows under the old site (they stop
being read for the new one; replaying the topic under a new group re-attributes
the history).
5. The consumer
internal/adapters/inbound/kafka/order_demand_consumer.go, the labor and
storage consumers' shape exactly:
- Atomic:
usecases.RecordOrderDemandruns the processed-event claim (processed_events, consumerorder-demand-consumer, keyed on the CloudEventsid) and the upsert in ONEports.UnitOfWork.Do; a failure after the claim rolls the claim back so the redelivery is processed, not skipped. FetchMessage/CommitMessages: the offset is committed only after success; a transient (infrastructure) error returns non-nil and the same message is retried with capped exponential backoff (200ms to 5s); deterministic problems return nil.- Consumer group from env
DEMAND_CONSUMER_GROUP, no default, and that env var is the feature's off switch: unset means no consumer, no Kafka reader, no extra query -- start-up and behaviour are byte-for-byte what they were. A group withoutDEMAND_SITE_IDrefuses to boot. Stable shared group; a group without a committed offset starts from the earliest retained message, so a fresh deployment rebuilds the model from history. - Started in
cmd/apionly;cmd/mcpnever dials Kafka and reads the same Postgres table.
6. Reads and the plan default (all additive)
GET /demand?location=&window_start=&window_end=and MCP toolget_expected_demandreturnorders,released_lines,source(order-management) andas_of(null when nothing is known), RFC 7807 errors, no auth. The tool budget is raised 10 to 11 deliberately.POST /capacity-plans/create_capacity_plan:assigned_demandis optional. Present (including0) it is used exactly as stated -- it always wins -- and the response saysdemand_source: "request". Omitted: the plan uses the number of orders the read model expects at(location, window)and saysdemand_source: "orders";assigned_demandin the response is that figure. Omitted and the model has no order for that location and window (or is not wired): today's 422missing-assigned-demand, byte-identical, with the same precedence as before. Never a silent zero.demand_sourceis persisted (capacity_plans.demand_source, defaultrequest, which is true for every earlier plan) and is not part of any published event: event payloads are untouched.
Why a local read model (and what was rejected)
- Live REST/MCP call to order-management at plan time -- rejected: a hard fleet rule, and a capacity decision must stay available if the upstream is degraded.
- Per-process in-memory cache with full replay -- rejected: the demand must
be queryable from every replica and from
cmd/mcp(a separate deployable) without a warm-up, and the claim must be atomic with the write. - Consume the analytics topic (gets cancellations,
line_count) -- rejected: it is the analytics data product's stream, not an integration contract; it would add a second topic/consumer to approximate a quantity order-management does not publish, for an event BR6 makes nearly unreachable. - Per-order site inferred from SKU / reservation / order id -- rejected as invention; see decision 4.
- Units = lines x a factor -- rejected as invention; the WorkloadProfile factors already convert ORDERS to units on the capacity side.
- Netting by treating
OrderRepromisedas a removal -- rejected: the order is still demand, only later. assigned_demandas*float64in the use case command -- rejected: it would change the public command type and every existing test; an additiveDemandFromOrdersflag set by the adapters when the field is absent keeps the explicit path byte-identical.
The feedback loop (read before extending)
order-management consumes THIS service's capacity events (its ADR 0031: a
published CapacityShortageDetected annotates the orders whose promise window
overlaps the shortage, at ITS configured site) and this service consumes
order-management's order events (this ADR). The two data flows are
independent and neither writes into the other's aggregates: order-management
only annotates and never moves a promise because of a plan, and this service
only reads orders into its own read model and never changes an order.
Consequently a shortage cannot reduce the demand that produced it (the
annotation does not change promise_date, so the order stays counted); there is
no cycle to oscillate. The loop is a feedback of information, closed by a
human who re-plans. If order-management ever moves promises because of a
planned shortage (its ADR 0031 lists that as a separate future ADR), THIS
model would shift demand between windows in response and a new ADR must reason
about convergence before that ships.
Both services use the same single-site simplification, so with
DEMAND_SITE_ID equal to order-management's PLANNED_CAPACITY_SITE_ID (the
fleet uses SIM1) the two views describe the same site.
Consequences
- Planners stop typing demand when order data exists, and see where a number
came from (
demand_source). - Overstatement: cancellations are not netted; re-promised orders keep their
last announced cutoff;
released_linesof a partially allocated order covers the latest pass only. UseGET /demand'sas_ofandordersas an input, not as a guarantee. - Postgres: migration
0006_order_demand(new table +capacity_plans.demand_source, down migration drops both). It runs on boot ofcmd/apiandcmd/mcp(advisory lock; idempotent). - Deployment:
DEMAND_CONSUMER_GROUPandDEMAND_SITE_ID(chart valuesdemand.consumerGroup/demand.siteId) turn the consumer on; a non-emptydemand.consumerGrouprequiresdemand.siteIdandkafka.enabled.
Verification
- Domain (
gremlins, 13/13 killed): the half-open predicate at start, end and one second either side; adjacent windows never double-count;Supersedesolder/equal/newer; validation at the boundary values. - Handler tests: valid event updates the model; replay of the same id with a different payload is a no-op; unknown types (incl. a right suffix under the wrong owner) ignored; legacy-flat/garbage skipped without error and without opening a unit of work; invalid payloads skipped before the transaction; a transient failure after the claim returns an error, leaves nothing written and the claim un-recorded, and the retry succeeds; last-writer-wins on event time; the run loop commits only after success.
- Use-case tests: request wins (explicit 0 included); orders used; no data / other site / cutoff exactly at the window end / no model wired -> 422; read failures are not masked.
- godog scenarios through the real HTTP surface with events fed through the
consumer's
HandleMessage(features/expected_demand.feature). -tags=integration: testcontainers Kafka and Postgres -- a real event lands in the model, the replay and junk write nothing, and a plan created withoutassigned_demandpicks it up; a failure after the claim is retried, not lost; SQL boundaries equal the in-memory adapter's; migration0006down/up.