Karyo
Documentation/ Decision records

ADR 0043: Stock works in a Facility, and the stock owner is the Business Unit of that Facility

Karyo 2.1.1rendered at build time
On this page

Status: Accepted. Converts the stock consumers that ADR 0040 left keyed on the owning Business Unit, after ADR 0042 converted the product, tasks and layout consumers. The order, count, fulfilment and other consumers stay as those records describe them until each one converts.

Context

ADR 0042 left the stock ports as they were: each had an ambient method that read the request scope and, beside it, an explicit-clientId method for schedulers and for callers that already held an owner. The explicit method built a synthetic TenantContext with TenantContext.ownerScoped and matched rows on client_id equality. The database filled facility_id on stock rows behind the code, so no stock read or write named its Facility.

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 stock port names its Facility. StockPicker, StockReserver, StockMover, StockReceiver, StockUnitLookup, UnitLoadLookup, UnitLoadMover, StockCountingPort, and ReplenishmentSourceSelector take a typed FacilityId. A receive carries it in ReceiveStockRequest.facilityId, a selection in StockSelectionRequest.facilityId, and a replenishment source query in SourceQuery.facilityId. A stock unit, unit load or reservation outside the named Facility is unknown.
  • Each ambient and explicit-clientId method pair is one method. There is no ambient overload and no clientId overload left. UnitLoadLookup.findForMutation and StockUnitLookup.findOwnerClientIdsByIds are deleted with them.
  • StockUnit, UnitLoad and InventoryJournal are Facility-owned. The inventory services, repositories and resources filter on facility_id. A second row an operation touches (the unit load under a stock unit, the target of a transfer) must exist in the same Facility as the first. Unit-load create and location transfer resolve the destination through StorageLocationLookup.findById. A missing location and another Facility's location both refuse the operation with HTTP 422 and the standard https://karyowms.com/errors/invalid-reference problem, before any unit-load, stock, allocation or outbox write. An id-only reference does not waive existence validation; the former foreign-only guard accepted missing rows and is replaced (issue #159). The pre-2.0.0 error-contract correction replaces the former 404 for body references with 422; the path's own missing unit load still returns 404, through a distinct exception type. Three reads still take the owning Business Unit and are the remaining owner-keyed inventory reads: StockSummaryLookup.summaryInLocations, PurgeBlockerLookup and ReservationRefMover. They convert with their callers.
  • The stock owner is the Business Unit of the Facility. A Facility belongs to one Business Unit, so every row in it has the same owner. The guards that compared the owner of two rows inside one operation (a transfer across owners, a mixed-owner extinguish selection) cannot fire and are deleted. client_id stays on the row as data and as the owner the journal, the outbox and the reports name.
  • Changing the owner is removed. No movement crosses Facilities, and a Facility has one Business Unit, so handing a unit load to another owner would move it across Facilities. UnitLoadService.changeClient and POST /api/v1/unit-loads/{id}/change-client are deleted, and the route answers as any unknown route does. An ownership change across Business Units is a ship out of the first and a receive in at the second. The cascade, the encumbrance checks, the UnitLoadClientChanged event and the OpenPickGuard port that served them are deleted, with the web client's unused request type and hook.
  • TenantContext.ownerScoped is deleted. Nothing builds a synthetic owner context any more: a scheduler or a port passes the Facility it works in and reads no ambient scope. UnprimedTenantContextException stays for an ambient read on an unprimed thread.
  • A caller that holds only a Business Unit uses its default Facility. Orders, fulfilment, waves, cross-dock, the replenishment scan and counts call OrganizationLookup.requireDefaultFacilityId and pass the result. The tasks module does the same through the nullable findDefaultFacilityId. Each of those calls is a bridge that the conversion of its own module deletes, as in ADR 0042. ExtinguishService.extinguish takes the owning Business Unit from the REST resource instead of reading the ambient context.

Consequences

  • Two Facilities of one Business Unit can hold stock of the same item without the lookups merging them, and a pick, reservation, transfer or count in one Facility never reads the other's rows. A unit load label is still unique across the installation (unit_loads.label_id).
  • A cross-owner test inside one scope has no meaning. The tests that proved owner isolation now prove Facility isolation with two Business Units that each have their own Facility.
  • config/scope-consumers.json shrinks: the stock ports leave the list of ports without a scope, and the registered raw-scope and client_id predicate entries of the inventory files that converted are removed. The registry only shrinks.
  • The default-Facility bridge sits in more callers until the order, count, fulfilment and wave modules convert. A Business Unit with no default Facility fails there with an IllegalStateException. The database already guarantees one for every positive unit.
  • An operator who needs goods under another Business Unit ships them out of the first and receives them in at the second. An inter-Facility transfer workflow is outside this program.

Alternatives considered

  • Keep the explicit-clientId ports for schedulers. Rejected: they are the second half of every pair this decision removes, and the Facility is the value a second Facility makes a real choice.
  • Resolve the Facility inside inventory from the Business Unit. Rejected for the reason in ADR 0042: inventory would need the organization lookup and would hide the choice.
  • Move the unit load and its stock to the target Business Unit's default Facility on changeClient. Rejected: it is a movement across Facilities, and it rewrites the derived ownership of rows under a fixed Facility. Removing the operation is the smaller and safer rule.
  • Keep ownerScoped for the remaining schedulers. Rejected: nothing in the application builds one once the ports take a Facility, and an unused factory is a way back to owner-keyed reads.

Evidence

  • services/inventory-service/karyo-inventory-api/src/main/kotlin/com/karyo/inventory/api/spi/ - the ports above
  • services/inventory-service/karyo-inventory-core/src/main/kotlin/com/karyo/inventory/service/
    • the implementations and FacilityRows.kt
  • services/fulfillment-service/karyo-fulfillment-core/src/main/kotlin/com/karyo/fulfillment/service/ExtinguishService.kt
  • config/scope-consumers.json and services/karyo-app/src/test/kotlin/com/karyo/security/guards/ - the registry and the guards that hold it
  • services/karyo-app/src/test/kotlin/com/karyo/security/CrossOwnerWriteTest.kt and security/ReadIsolationTest.kt - the behaviour above