Karyo
Documentation/ What Karyo does in a warehouse

Stock model and states

Karyo 2.1.1rendered at build time
On this page

What Karyo believes is physically in the warehouse, and the rules that govern it. Everything downstream - allocation, picking, counting, replenishment - is a read or a write against this model, so its edges are the edges of the whole product.

The two entities

There are exactly two, and the distinction is load-bearing.

A UnitLoad is a physical carrier: a pallet, a tote, a pick bin, a carton. It has a globally unique labelId (services/inventory-service/karyo-inventory-core/src/main/kotlin/com/karyo/inventory/domain/model/UnitLoad.kt, UnitLoad.labelId, unique = true), a location, a type, and a lockType of its own. It may nest inside another unit load through carrierUnitLoad (UnitLoad.carrierUnitLoad), and isCarrier (UnitLoad.isCarrier) marks one that holds others.

A StockUnit is a quantity of one product on one unit load (.../domain/model/StockUnit.kt). It carries amount, reservedAmount, an optional lotNumber, serialNumber and bestBefore, a state, a lockType, and a strategyDate - the FIFO key. A unit load may hold several stock units; the count is what makes it "mixed".

The single derived value everything else depends on:

val availableAmount: BigDecimal get() = amount.subtract(reservedAmount)

StockUnit

amount is what is physically there. reservedAmount is what someone else has already promised away. Only the difference is offerable. There is no third quantity - no "allocated but not reserved", no "in transit" - so every planning question in the system reduces to this subtraction.

The six stock states

karyo-inventory-api/.../vo/StockState.kt, StockState:

State Code Meaning
UNDEFINED 0 Entity default, never written by a business path
INCOMING 100 Received but not yet accepted onto stock
ON_STOCK 300 The only pickable state
PICKED 600 On a pick container
PACKED 650 In a shipping unit
SHIPPED 680 Handed to the carrier
DELETABLE 1000 Soft-deleted; awaiting the reaper

Transitions go through one chokepoint, StockService.changeState (.../service/StockService.kt, StockService.changeState), which enforces forward-only movement with one exception: DELETABLE is reachable from anywhere.

if (newState <= su.state && newState != StockState.DELETABLE.code) {
    throw InventoryException.InvalidStateTransition(id, su.state, newState)
}

StockService.changeState

Forward-only means not step-by-step. changeState will happily jump 300 → 650 if a caller asks, and the equivalent jump does happen at the order level (see picking). There is exactly one sanctioned backward write in the whole model, StockPicker.unpackContainer, which writes the state field directly rather than going through the chokepoint, precisely because the guard would refuse it (.../service/DefaultStockPicker.kt, DefaultStockPicker.unpackContainer).

changeState also stamps modified by hand (StockService.changeState) because nothing else does - no @PreUpdate, no @UpdateTimestamp, and the column default only fires on insert. That single line is what makes the purge retention window mean anything.

ON_STOCK is the whole gate

StockUnitRepository.findForSelection - the query behind every allocation decision - filters state = 300 and nothing else about where the stock is:

itemDataId = ?1 and facilityId = ?2 and state = 300 and amount > reservedAmount

.../repository/StockUnitRepository.kt, StockUnitRepository.findForSelection

There is no Location Class, zone or usage predicate. Stock that is ON_STOCK and unlocked is allocatable wherever it physically sits - including a goods-in dock, a pack-staging location or a cross-dock staging slot. It matters most on the quality-hold release path: the release raises a putaway, but until that putaway finishes the stock is still in receiving and allocatable there - see receiving and quality holds.

Two lock enums that disagree

There are two LockType enums and they are not compatible. Both KDocs warn about it in almost identical language.

Stock locks (karyo-inventory-api/.../vo/LockType.kt, LockType): UNLOCKED(0), GENERAL(1), STOCKTAKING(7), QUALITY_FAULT(103), LOT_EXPIRED(202), LOT_TOO_YOUNG(203), SHIPPED(405).

Location locks (karyo-layout-api/.../vo/LockType.kt, LockType): UNLOCKED(0), GENERAL(1), QUARANTINE(2), STOCKTAKING(7), DAMAGE(8).

They share only 0, 1 and 7. Codes 2 and 8 exist only for locations; 103, 202, 203 and 405 only for stock. Passing a code across the boundary makes the receiving enum's fromCode throw. Reconciling the two is deliberately not done; the warnings in both KDocs (karyo-inventory-api/.../vo/LockType.kt, LockType, karyo-layout-api/.../vo/LockType.kt, LockType) are the guard.

SHIPPED(405) has no writer anywhere in Karyo, and its own KDoc says so (karyo-inventory-api/.../vo/LockType.kt, LockType). Dispatch flips state to SHIPPED(680) and then straight on to DELETABLE; it never sets a lock.

The pallet lock is a read-time invariant

A unit load's lockType is not cascaded once and forgotten. findForSelection joins it directly:

and lockType = 0 and unitLoad.lockType = 0

StockUnitRepository.findForSelection

The consequence is stated in the field's own KDoc (UnitLoad.lockType): stock that lands on an already-locked pallet after the lock was applied - a late receive, a transfer, carrier nesting - is unpickable even though its own lockType is still 0. The per-stock cascade in UnitLoadService.lock/unlock (.../service/UnitLoadService.kt, UnitLoadService) still runs, but it drives journalling and per-stock visibility rather than being the enforcement.

unlock only clears a stock unit whose lock equals the code the unit load itself carried, so a stock unit that was individually locked under a different code survives a pallet unlock (UnitLoadService).

The summary read area replenishment uses to decide whether an area is deficient reads the same pair, su.lockType = 0 and su.unitLoad.lockType = 0 (StockUnitRepository.summaryInLocations), so stock on a locked pallet in an area does not count towards that area's on-hand. See replenishment.

The journal

Every mutation writes an InventoryJournal row through JournalService.record. The record types are CREATED(1), CHANGED(2), PICKED(3), TRANSFERRED(5), COUNTED(7) and DELETED(8) (karyo-inventory-api/.../vo/JournalRecordType.kt, JournalRecordType).

Authentication records belong to the separate Company sign-in log, not to any Facility's movement history. Migration V1216 preserves every old authentication row and removes it from the inventory journal before those codes retire. A database constraint rejects future authentication rows in that journal. ADR 0049 defines the audience and preservation rule.

Reservation changes are journalled as CHANGED with the delta amount, not as a distinct type (StockService.reserveStock), so a reservation and an adjustment are the same record type in the history and are told apart only by the correlation and activity codes.

Weight

UnitLoad.weight is the effective weight: the operator's measured value when one is set, otherwise the calculated one (UnitLoad.weight). The calculation is the type's tare plus every non-gone stock unit on the load, and it is always a full recompute, never incremental. The reasoning is recorded in the field's KDoc (UnitLoad.weightCalculated): an incremental total with a full-recalculation fallback for when the increment goes wrong is a cache with a repair hatch; a full recompute at each of the few mutation points costs one batched read and cannot drift.

The manual override beating the calculation is a deliberate design decision (UnitLoad.weightMeasure).

Deletion and the reaper

StockService.deleteStock is a soft delete: it flips state to DELETABLE(1000) and journals. The row survives, and findByUnitLoadId still returns it - which is why DefaultStockCountingPort.findStockAtLocation filters state != DELETABLE explicitly before planning a count (.../service/DefaultStockCountingPort.kt, DefaultStockCountingPort.findStockAtLocation).

Hard deletion is a separate, opt-in scheduled reaper. StockPurgeScheduler defaults off (karyo.inventory.purge.enabled, defaultValue = "false", .../service/StockPurgeScheduler.kt, StockPurgeScheduler) with the reason stated at StockPurgeScheduler: "this is the one irreversible operation in this codebase, so an existing deployment must opt in explicitly".

The retention window is a per-client runtime property defaulting to 30 days, floored at 1 day at the point of use (.../service/StockPurgeService.kt, StockPurgeService.MIN_RETENTION_DAYS, StockPurgeService.purge). The floor exists because the property catalogue validates INTEGER without domain knowledge and would otherwise accept 0, collapsing the grace period on the next tick (StockPurgeService.MIN_RETENTION_DAYS).

Each candidate purges in its own transaction, so one poison row blocks only itself (StockPurgeService.purgeStockUnitInNewTransaction). Live references are excluded through the PurgeBlockerLookup SPI, which four modules implement - orders, tasks, fulfillment and stocktaking each answer for their own open work.

Dispatch pre-empts the reaper for shipped stock: shipContainer flips PACKED → SHIPPED, then promotes the same units straight to DELETABLE in the same transaction (DefaultStockPicker.shipContainer). The KDoc explains why (DefaultStockPicker.shipContainer): occupancy reads filter state < DELETABLE, so the promotion removes shipped stock from every occupancy answer immediately, regardless of whether the reaper is even switched on. SHIPPED remains the recorded ship state in the journal and outbox; DELETABLE is the database end state.

Concurrency

BaseEntity carries @Version (libs/karyo-common/.../domain/BaseEntity.kt, BaseEntity), so stock mutations are guarded by optimistic locking. There is no pessimistic lock on StockUnit; the only PESSIMISTIC_WRITE reads in this repository are on UnitLoad (UnitLoadRepository.findByIdForUpdate) and on the sequence-number table (SequenceNumberStateRepository).

That choice is correct for correctness - two concurrent reserves of the same unit cannot both commit - but the losing transaction's failure is not modelled anywhere. See allocation and reservation.