Karyo
Documentation/ Decision records

ADR 0045: The fulfilment flow and webhooks work in a Facility

Karyo 2.1.1rendered at build time
On this page

Status: Accepted. Converts the fulfilment-flow consumers that ADR 0044 left on the bridge: fulfilment, waves, work, streaming, cross-dock and replenishment, with their schedulers, the outbox and the webhook relay. ADR 0046 converts the readers and documents (monitors, reporting, forecasting, simulation, slotting and the document archive) and removes the two bridges this record kept.

Context

The pick, wave, shipment, cross-dock, streaming and replenishment code found its rows by the owning Business Unit. It read the request scope, or took a clientId, and the schedulers enumerated owners. Where a caller held only an owner, it called the order, stock and layout ports through OrganizationLookup.requireDefaultFacilityId, the bridge that ADR 0044 kept for this work. The database already stamped facility_id on the rows, but nothing in this flow named it, so a wave, pick route, cross-dock order or shipment could in principle read or write across Facilities.

The webhook relay matched a subscription to an event by client_id, and its envelope named only the owner. A receiver could not tell which Facility an operational event came from.

The end state is a Karyo that works with Company, Business Unit and Facility and not with the legacy stock owner. Karyo has no users, so a change here may be disruptive, and compatibility code has no reason to exist.

Decision

  • A flow port names its Facility. The fulfilment, wave, work, streaming, cross-dock and replenishment ports take a typed FacilityId, or carry one in the request (WavePickRequest, OpenGroupShipmentRequest, WaveRef, FlushContext). Each ambient and explicit-clientId method pair is one method, and every caller is converted. WorkProvider and WorkEligibilityResolver take the Facility, so a work inbox lists only the Facility it is opened in.
  • The flow rows are Facility-owned. PickOrder, Pick, Shipment, ShipmentOrder, ShippingUnit, ShippingUnitLine, Wave, ConsolidationGroup, ConsolidationLine, SortScan, WaveSelectionRule, CrossDockOrder, StreamBatch, WorkGroup and WorkGroupMember extend FacilityEntity. Services, repositories and resources filter on facility_id. A row in a Facility is unknown to a request in another.
  • No wave, pick route, cross-dock order or shipment crosses Facilities. A wave selects only the orders of its Facility. The picks, consolidation groups and shipments it produces carry that Facility, and the progress, expiry and release observers act in the Facility of the event they observe.
  • A selection rule works only in its own Facility. A rule names the zones and locations of one Facility, and an order strategy is shared by every Facility of the unit. A strategy that names a rule of another Facility works only in that rule's Facility: in any other, POST /waves and the scheduler fail with a 422 problem (wave-rule-other-facility) that says the rule belongs to a different Facility. WaveScheduler reads the order pool first and returns early for a Facility with nothing to wave, so it looks up no rule there.
  • The owner is the Business Unit of the Facility. A row written in a Facility names that Facility's Business Unit as client_id, as ADRs 0043 and 0044 decided. OrganizationLookup.requireBusinessUnitOf answers it. client_id stays on the row as data.
  • Every scheduled job in the flow fans out per Facility. WaveScheduler, StreamingReleaseScheduler, StockPurgeScheduler and CrossDockExpirySweep enumerate the Facilities that have work; ReplenishmentScheduler enumerates every active Facility in the organization registry (OrganizationLookup.activeFacilityIds), because its scan covers fix assignments and item-data areas and a list derived from one of them skipped the other. Each job runs each Facility in its own failure boundary: one Facility's failure is logged and the others still run. The enumerations (OrderReleasePort.facilitiesWithWaveEligibleOrders, StreamingReleasePort.facilitiesWithStreamingWork) cross scopes by design and are listed as such. The registry scope of these jobs is PER_FACILITY. Settings still resolve through the Business Unit of the Facility.
  • Facility events and master-data events are two scopes of outbox row. OutboxService has no unscoped publish. publishInFacility writes an operational event with its Facility and its Business Unit, and publishForBusinessUnit writes a master-data event (a product, a client, a template) that no single Facility owns. outbox_events.facility_id is null for the second kind. State events of the flow (WaveStateChangedEvent, the cross-dock events, PickClaimWarningAccepted, WavePickActivityEvent) carry facilityId and no longer carry clientId.
  • The webhook envelope is version 2, with no version 1. The body carries schemaVersion, scopeType (FACILITY or BUSINESS_UNIT), facilityId (null for a master-data event), businessUnitId, companyCode, the aggregate fields and data. The relay emits only this shape. The v1 tenantId field and a drain window for old receivers do not exist: Karyo is unreleased and nobody receives v1.
  • A webhook subscription's audience is one Facility. It receives the operational events of that Facility and the master-data events of the Facility's Business Unit, and nothing else. webhook_subscription.facility_id and webhook_delivery.facility_id are required, the subscription routes read and write only the Facility in the request, and the synthetic ping is a Facility event.
  • Two bridges stay for the follow-up, and no others. BinBelowReorderDetector (monitors) and the demo and layout-setup callers still resolved a default Facility through OrganizationLookup.requireDefaultFacilityId and findDefaultFacility. The follow-up that converted the readers (ADR 0046) removed requireDefaultFacilityId with the last caller. The monitors and the document archive kept their Business Unit key until then, and MonitorEvaluator stayed PER_OWNER.

Consequences

  • Two Facilities of one Business Unit run their waves, pick orders, shipments, cross-dock orders and stream batches side by side, and one Facility's scheduler failure does not stop the other.
  • A receiver tells the Facility of an event from the envelope, and a subscription never sees another Facility's operational events. A receiver that wants a whole Business Unit subscribes once per Facility.
  • A test that depends on a Facility must provision one first (TestOrganizations.facilityOf), and each test takes its own client_id, as AGENTS.md records.
  • config/scope-consumers.json shrinks: the converted ports leave the list of ports without a scope, the raw-scope entries of the converted files become operationalFacilityId or go, and the client_id predicate entries of the converted repositories are removed. The registry only shrinks.
  • The monitors, reporting, forecasting, simulation, slotting readers and the document archive read by Business Unit when this record was written, until ADR 0046 converted them.

Alternatives considered

  • Emit envelope v1 and v2 for a drain window. Rejected: there are no receivers to drain, and two shapes would be compatibility code with no reader.
  • One subscription for a whole Business Unit. Rejected: a Facility is the unit of operation, and a subscription that spans Facilities would reopen the cross-Facility reads this decision closes.
  • Keep the owner-enumerating schedulers and resolve each default Facility. Rejected for the reason in ADR 0044: the bridge hides which Facility a job works in and fails for a Business Unit with two.
  • Convert the readers and documents in the same change. Not done: the change was too large to review as one. The follow-up carries them.

Evidence

  • services/fulfillment-service/, services/wave-service/, services/work-service/, services/streaming-service/, services/crossdock-service/ and services/replenishment-service/ - the ports under -api and the implementations under -core, with each module's FacilityRows.kt
  • libs/karyo-events/src/main/kotlin/com/karyo/events/outbox/OutboxService.kt - the two publishers
  • services/integration-hub-service/karyo-webhooks-api/src/main/kotlin/com/karyo/webhooks/dto/WebhookEnvelope.kt and .../karyo-webhooks-core/.../relay/WebhookFanoutScheduler.kt - the envelope and the audience rule
  • services/karyo-app/src/main/resources/db/migration/common/V4__outbox_facility.sql and .../webhooks/V902__facility_audience.sql
  • config/scope-consumers.json and services/karyo-app/src/test/kotlin/com/karyo/security/guards/ - the registry and the guards that hold it