Skip to main content

Invariants

Every rule below is enforced in the domain or the application layer — never in the HTTP adapter — and every one has a failing-path test, not just a happy-path one. The Aggregate Design Canvas lists the same invariants grouped per aggregate, with the commands and events around them.

The core invariant: no orphan slots, ever​

Registering a LocationSlot is a chain-of-custody check, not a bare insert.

A LocationCode carries its own hierarchy, so the code itself declares which parents must exist. RegisterLocationSlot resolves them in order and rejects the registration if any link is missing or not Active:

Source: internal/application/usecases/register_location_slot.go (register, resolveChain), internal/domain/slot/location_slot.go, internal/adapters/inbound/http/errors.go. The order of the checks is the order of the code: the duplicate-code lookup runs before the chain walk.

The rejection branches are exercised by the domain and use-case tests (internal/domain/slot/*_test.go, internal/application/usecases/slot_test.go) and the Gherkin scenarios in features/.

Per-aggregate invariants​

Site​

InvariantEnforced inFailure
SiteCode non-emptysite.NewSiteErrEmptySiteCode → 400
SiteCode is [A-Z0-9] onlysite.NewSiteErrInvalidSiteCode → 400
Name non-emptysite.NewSiteErrEmptySiteName → 400
SiteCode globally uniqueRegisterSite use caseErrDuplicateSite → 409
Cannot decommission twiceSite.DecommissionErrAlreadyDecommissioned → 409

Uniqueness lives in the use case on purpose: a single aggregate cannot see its siblings, so it cannot enforce a global constraint without reaching outside itself.

Zone​

InvariantEnforced inFailure
Scoped to exactly one Sitezone.NewZoneErrEmptySiteCode → 400
Area and Zone codes non-empty, [A-Z0-9] onlyzone.NewZoneErrEmptyAreaCode / ErrEmptyZoneCode / ErrInvalidCode → 400
TemperatureClass is one of Ambient/Chilled/Frozenzone.NewZoneErrUnknownTemperatureClass → 422
Parent Site must existRegisterZone use caseErrSiteNotFound → 404
Parent Site must be ActiveRegisterZone use caseErrSiteNotActive → 409
(site, area, zone) uniqueRegisterZone use caseErrDuplicateZone → 409

Aisle​

InvariantEnforced inFailure
Scoped to exactly one Zoneaisle.NewAisleErrEmptyZoneID → 400
Aisle code non-empty, [A-Z0-9] onlyaisle.NewAisleErrEmptyAisleCode / ErrInvalidAisleCode → 400
SequenceHint non-negativeaisle.NewAisleErrNegativeSequenceHint → 422
Direction is one of OneWay/TwoWayaisle.NewAisleErrUnknownDirection → 422
Parent Zone must existRegisterAisle use caseErrZoneNotFound → 404
Parent Zone must be ActiveRegisterAisle use caseErrZoneNotActive → 409
(zone, aisleCode) uniqueRegisterAisle use caseErrDuplicateAisle → 409

LocationType and PlacementRule​

InvariantEnforced inFailure
LocationType name non-emptyplacement.NewLocationTypeErrEmptyLocationTypeName → 400
Default capacity strictly positiveshared.NewCapacityErrInvalidMaxWeight / ErrInvalidMaxVolume → 422
Role is a known LocationRole (defaults to Storage)placement.ParseLocationRoleErrUnknownLocationRole → 422
Capacity required only for Storage, Staging, Drop, Consolidation rolesplacement.NewLocationTypeErrInvalidMaxWeight → 422
LocationType name uniqueRegisterLocationType use caseErrDuplicateLocationType → 409
Rule id non-empty and uniqueplacement + use caseErrEmptyRuleID → 400 / ErrDuplicatePlacementRule → 409
Rule effect is Allow or Denyplacement.ParseEffectErrUnknownEffect → 422
Rule predicate constrains somethingplacementErrEmptyPredicate → 422
Rule references an existing LocationTypeDefinePlacementRule use caseErrLocationTypeNotFound → 404

A predicate that sets none of zoneCode, temperatureClass or hazmat would match every zone in the building. That is almost always a typo, so it is rejected rather than accepted as a facility-wide rule.

LocationSlot​

InvariantEnforced inFailure
LocationCode is exactly 7 valid segmentsshared.NewLocationCode / ParseLocationCodeErrMalformedLocationCode / ErrEmptyLocationSegment / ErrInvalidLocationSegment → 400
LocationCode globally unique (it is the identity)RegisterLocationSlot use caseErrDuplicateLocationCode → 409
Site → Zone → Aisle chain resolvesRegisterLocationSlot use caseErrSiteNotFound / ErrZoneNotFound / ErrAisleNotFound → 404
Every link in that chain is ActiveRegisterLocationSlot use caseErrSiteNotActive / ErrZoneNotActive / ErrAisleNotActive → 409
Supplied zone attributes match the code's own zoneslot.NewLocationSlotErrZoneMismatch → 422
Capacity envelope strictly positive (when the role requires capacity)shared.NewCapacity / slot.NewLocationSlotErrInvalidMaxWeight / ErrInvalidMaxVolume → 422
A Dock slot has a valid dockFlow; a WorkCenter slot has at least one valid activity; no other role carries eitherslot.NewFunctionalAttributesErrDockFlowRequired / ErrWorkCenterActivitiesRequired / ErrFunctionalAttributesNotAllowed / ErrUnknownDockFlow / ErrUnknownActivity → 422
Satisfies every applicable PlacementRuleslot.NewLocationSlot via RuleSet.CheckErrPlacementRuleViolated → 422, naming the violated rule
Cannot decommission twiceLocationSlot.DecommissionErrAlreadyDecommissioned → 409
A decommissioned code is never resurrected by re-registrationRegisterLocationSlot use caseErrDuplicateLocationCode → 409
Geometry is real: non-negative z, strictly positive dimensionsshared.NewPoint3D / NewDimensionsErrInvalidZ / ErrInvalidDimensions → 422
Geometry and pick sequence are frozen once decommissioned; pick sequence non-negativeLocationSlot.SetGeometry / SetPickSequenceErrSlotDecommissioned → 409 / ErrNegativePickSequence → 422

Geometry and travel (ADR 0017)​

InvariantEnforced inFailure
A cross-aisle connects two distinct aisles, both in the named zoneaisle.NewCrossAisle + RegisterCrossAisle use caseErrCrossAisleSameAisle / ErrCrossAisleAisleMismatch → 422
One cross-aisle per (zone, from, to, bay)RegisterCrossAisle use caseErrDuplicateCrossAisle → 409
A fixed structure has an id, site, known kind, real footprint and label; id uniquestructure.NewFixedStructure + use caseErrEmptyID / ErrEmptyLabel → 400, ErrUnknownKind / ErrEmptyFootprint → 422, ErrDuplicateFixedStructure → 409
A zone's bay and level pitch are both positiveZone.SetPitchErrInvalidPitch → 422
Travel distance is never guessed: same-zone requests route over the zone graph; cross-zone requests return a straight-line estimate only when both slots carry recorded positionsEstimateTravelDistance use case (crossZoneBeeline), travel.GraphErrNoRouteBetweenZones / ErrNoRoute / ErrUnknownNode → 422
A slot write built from a stale read is rejectedSlotRepo.Save (Postgres, version column, ADR 0025)ports.ErrConcurrentModification → 409 concurrent-modification

Placement-rule evaluation​

RuleSet.Check(locationType, attrs) runs in a fixed order:

  1. Any matching Deny rule naming this LocationType rejects it. Deny always wins.
  2. If any matching Allow rule exists for the zone at all, the zone becomes an allow-list: the LocationType must be named by one of them, or it is rejected.
  3. Otherwise the zone is unconstrained and the placement is permitted.

The rejection error always names the specific rule violated:

location type violates a placement rule for this zone: PalletRack is denied in
zone WH1-STOR-FRZ by rule [RULE-FRZ-NO-SHELF: Deny PalletRack where
temperatureClass=Frozen]

and, for the allow-list branch:

location type violates a placement rule for this zone: zone WH1-STOR-HAZ allows
only the location types named by its Allow rules, and Shelf is not among them
(rules: [...])

Naming the rule is not cosmetic. During a 500-row bulk import, "rejected" is useless and "rejected by RULE-FRZ-NO-SHELF" is actionable.

Lifecycle invariant: decommission is one-way​

There is no reactivation use case in v1. A decommissioned Site, Zone, Aisle or LocationSlot stays decommissioned, and re-registering a decommissioned LocationCode is rejected as a duplicate rather than quietly resurrecting the slot. The reasoning is recorded in ADR 0005.

UnderMaintenance is a legal persisted state — e.g. loaded from an external facility-management system — that the read models render. v1 exposes no use case that sets it, and a slot in it can still be decommissioned.

Bulk import: atomic per row, never all-or-nothing​

ImportFacilityLayout applies every invariant above to each row independently. A row that fails is reported with its index, its location code and the exact error; the other rows still commit. A 500-row export with 3 bad rows creates the other 497 and tells you precisely which 3 and why. Within a row, each step (create a missing site, zone or aisle, register the slot, set its geometry) commits in its own unit of work, so a row rejected at the slot step keeps any parent it already created (internal/application/usecases/import_facility_layout.go). See Bulk import and ADR 0006.

How these are verified​

LayerMechanism
DomainTable-driven unit tests per aggregate, both paths
ApplicationUse-case tests against the in-memory test doubles
HTTPOne httptest per endpoint, asserting status and RFC 7807 body
End-to-endgodog/Gherkin scenarios over the real HTTP API in features/
PersistenceBuild-tagged integration tests that boot a throwaway testcontainers Postgres (and Kafka) and never skip (internal/adapters/outbound/postgres/integration_test.go:34-37)
Architecturearch-go fitness test — the domain may not import the application layer

The combined statement coverage gate across internal/domain/... and internal/application/... is ≥ 90%, matching the other fleet services.