Karyo
Documentation/ Decision records

ADR 0046: Monitors, reporting, the demand readers and the document archive work in a Facility

Karyo 2.1.1rendered at build time
On this page

Status: Accepted. Converts the last consumers that ADR 0045 left on the Business Unit key: the event monitors, reporting, forecasting, simulation, slotting and the document archive. It removes the default-Facility bridge, OrganizationLookup.requireDefaultFacilityId.

Context

After ADR 0045 the operational flow named its Facility, but the readers still found their rows by the owning Business Unit. The monitor evaluator enumerated owners and ran every detector for each. The KPI and occupancy views grouped by client_id. The forecasting, simulation and slotting readers filtered stock, picks and locations by client_id. A stored document, a saved report and an alert belonged to the owner only. BinBelowReorderDetector reached the replenishment scan through the bridge.

A Business Unit with two Facilities would have mixed them: one alert list, one KPI series, one demand history and one archive for two warehouses. 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 reader names its Facility. DemandReader, OnHandReader, SimDemandReader, VelocityReader, SlotReader, FixAssignmentReader, the detector readers and the KPI, category-volume, occupancy and report-definition repositories take a typed FacilityId and filter on facility_id. The services that call them (ForecastService, SimulationService, SlottingService, the reporting services) take the Facility, and the resources pass operationalFacilityId.
  • Monitors run per Facility. MonitorEvaluator enumerates every active Facility in the organization registry (OrganizationLookup.activeFacilityIds), not the Facilities that hold stock, because detectors also read orders, counts and putaway work. Its first sweep of a Facility seeds that Facility's default monitor_config rows. It runs each Facility in its own failure boundary: one Facility's failure is logged and the others still run. Its registry scope is PER_FACILITY. DetectionContext carries the Facility, so BinBelowReorderDetector calls the replenishment scan for it and no longer resolves a default Facility.
  • Monitor configuration, alerts and deliveries are Facility-owned. monitor_config is unique on (facility_id, monitor_key), so two Facilities of one Business Unit configure their monitors apart. Alert extends FacilityEntity. An alert delivery repeats the Facility of its alert, so the delivery log is read per Facility without a join (V1106). Channel settings (recipients, webhook URL) still resolve through the Business Unit of the Facility.
  • An alert is a Facility event. It is written with publishInFacility, not as a master-data event, because an alert describes the state of one Facility.
  • Reporting reads one Facility. The KPI and occupancy views are keyed by facility_id, and a saved report definition belongs to one Facility (V1008). The views are dropped and created again, because a view that renames its first column cannot be replaced in place.
  • The document archive belongs to a Facility. DocumentStore.store takes the Facility of the document's subject entity, never the acting principal's. StoredDocument extends FacilityEntity, the list and read routes filter on the readable Facilities, and a write outside the writable Facilities is refused with Forbidden (V1304). DocumentStoredEvent and DocumentDeletedEvent carry facilityId and are Facility events. Document templates stay owned by the Business Unit.
  • The default-Facility bridge is deleted. OrganizationLookup.requireDefaultFacilityId and the nullable findDefaultFacilityId have no caller. OrganizationLookup.findDefaultFacility stays, because two callers legitimately start from a Business Unit and need its only Facility: the demo seeder and warehouse setup, whose request names the unit it works for.
  • The registry shrinks. The converted resources move from operationalBusinessUnitId to operationalFacilityId, the document store from readScope/writeScope to readFacilityScope/writeFacilityScope, the client_id predicate entries of the converted readers and repositories are removed, and PER_OWNER has no job left.

Consequences

  • Two Facilities of one Business Unit have separate alerts, monitor settings, KPIs, forecasts, simulations, slotting facts, saved reports and stored documents.
  • A request that reads or writes one of these needs a Facility, from the principal's grant or from the X-Karyo-Facility-Id header, as every other converted area does.
  • A test that seeds a row for one of these must provision the Facility first (TestOrganizations.facilityOf); the row triggers stamp facility_id from client_id afterwards.
  • No module reads ambient scope for a Facility now except through the registered accessors, and no caller reaches a port through the unit's default Facility except the two named above.
  • client_id stays on the rows as data, as ADRs 0043 to 0045 decided.

Alternatives considered

  • Keep the monitors per Business Unit and fan out to Facilities inside each detector. Rejected for the reason in ADR 0044: the bridge hides which Facility a job works in and fails for a unit with two.
  • Keep the document archive keyed by owner. Rejected: a document records an entity of one Facility, and an owner-keyed archive would reopen the cross-Facility read the other conversions close.
  • Delete findDefaultFacility with the rest of the bridge. Not done: the demo seeder and warehouse setup have a Business Unit and no Facility in hand, and a second lookup would say the same thing.

Evidence

  • services/monitoring-service/karyo-monitors-core/ - MonitorEvaluator, AlertService, MonitorConfigService and the detector readers
  • services/reporting-service/karyo-reporting-core/ and services/karyo-app/src/main/resources/db/migration/reporting/V1008__facility_reporting.sql
  • services/forecasting-service/, services/simulation-service/ and services/slotting-service/ - the readers under -core
  • services/document-service/karyo-docstore-core/, libs/karyo-documents/ and .../db/migration/docstore/V1304__facility_documents.sql
  • .../db/migration/monitors/V1106__facility_monitoring.sql
  • config/scope-consumers.json and services/karyo-app/src/test/kotlin/com/karyo/security/guards/ - the registry and the guards that hold it