ADR 0043: Stock works in a Facility, and the stock owner is the Business Unit of that Facility
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, andReplenishmentSourceSelectortake a typedFacilityId. A receive carries it inReceiveStockRequest.facilityId, a selection inStockSelectionRequest.facilityId, and a replenishment source query inSourceQuery.facilityId. A stock unit, unit load or reservation outside the named Facility is unknown. - Each ambient and explicit-
clientIdmethod pair is one method. There is no ambient overload and noclientIdoverload left.UnitLoadLookup.findForMutationandStockUnitLookup.findOwnerClientIdsByIdsare deleted with them. StockUnit,UnitLoadandInventoryJournalare Facility-owned. The inventory services, repositories and resources filter onfacility_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 throughStorageLocationLookup.findById. A missing location and another Facility's location both refuse the operation with HTTP 422 and the standardhttps://karyowms.com/errors/invalid-referenceproblem, 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,PurgeBlockerLookupandReservationRefMover. 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_idstays 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.changeClientandPOST /api/v1/unit-loads/{id}/change-clientare 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, theUnitLoadClientChangedevent and theOpenPickGuardport that served them are deleted, with the web client's unused request type and hook. TenantContext.ownerScopedis 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.UnprimedTenantContextExceptionstays 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.requireDefaultFacilityIdand pass the result. The tasks module does the same through the nullablefindDefaultFacilityId. Each of those calls is a bridge that the conversion of its own module deletes, as in ADR 0042.ExtinguishService.extinguishtakes 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.jsonshrinks: the stock ports leave the list of ports without a scope, and the registered raw-scope andclient_idpredicate 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-
clientIdports 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
ownerScopedfor 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 aboveservices/inventory-service/karyo-inventory-core/src/main/kotlin/com/karyo/inventory/service/- the implementations and
FacilityRows.kt
- the implementations and
services/fulfillment-service/karyo-fulfillment-core/src/main/kotlin/com/karyo/fulfillment/service/ExtinguishService.ktconfig/scope-consumers.jsonandservices/karyo-app/src/test/kotlin/com/karyo/security/guards/- the registry and the guards that hold itservices/karyo-app/src/test/kotlin/com/karyo/security/CrossOwnerWriteTest.ktandsecurity/ReadIsolationTest.kt- the behaviour above
Related
- ADR 0006 and ADR 0007 - the module boundary the ports sit on
- ADR 0039 - the registry
- Identity and tenancy