Karyo
Documentation/ Architecture

Events and the outbox

Karyo 2.1.1rendered at build time
On this page

How a state change in one module reaches another module in the same process, and how it reaches anything outside the process.

Derived from libs/karyo-events/, every @Observes site, and the two webhook schedulers.

Two mechanisms, and where they overlap

Internal state-change notification is synchronous CDI events (ADR 0008). There is no broker. A module fires a typed payload; observers on the aggregated classpath receive it. The core code fires events from 18 call sites.

Anything outside the process learns of a change from the outbox: a row written to outbox_events in the same transaction as the change (ADR 0009). The core code writes outbox rows from 57 call sites, and the event catalogue lists every type they produce.

The two mechanisms are independent. A change reaches in-process observers only if its module fires an event, and reaches the outside only if it writes an outbox row. Some changes do both - fired for an observer and written for the webhook relay - but most outbox rows have no in-process event beside them. .../crossdock/event/CrossDockEvents.kt, CrossDockMatchFoundEvent, CrossDockStagedEvent, CrossDockExpiredEvent, CrossDockCompletedEvent, CrossDockCancelledEvent describes dual publishing as "the same dual-publish convention every other module uses", which overstates it.

Transaction phases are load-bearing

The default (IN_PROGRESS) joins the firing transaction. AFTER_SUCCESS runs after commit. The choice is deliberate at every site, and the clearest example is goods receipt:

Observer Phase Effect
The cross-docking engine's receiving observer default, in-transaction Claims the received line for cross-docking, inside the receipt transaction
TaskService.onGoodsReceiptLineReceived (.../tasks/service/TaskService.kt, TaskService.onGoodsReceiptLineReceived) AFTER_SUCCESS Creates the putaway task, after the receipt commits

That ordering is what makes the design work. CrossDockLookup states it (.../tasks/spi/CrossDockLookup.kt, CrossDockLookup): the engine's interceptor runs synchronously inside the receiving transaction, before the putaway observer's AFTER_SUCCESS phase, so by the time that observer runs, an existing cross-dock order for the line is authoritative. When no engine answers, the lookup is treated as "never intercepted" and the warehouse falls back to its ordinary always-putaway behaviour. Cross-docking is an engine capability; receiving must not depend on it. (The lookup bean is always present, and while a client's cross-docking rungs are off it answers false; see Unconfigured states.)

PackingService.onAutoPackEvent is blunter: "@Observes(during = TransactionPhase.AFTER_SUCCESS) is load-bearing, not decoration". The auto-putaway observer carries the same constraint.

The outbox is active

OutboxService writes aggregate_type, aggregate_id, event_type, a JSONB payload, business_unit_id (the Business Unit), facility_id and published = false. publishInFacility writes an operational event with its Facility. publishForBusinessUnit writes a master-data event with a null facility_id. There is no unscoped publish (ADR 0045). The code is libs/karyo-events/src/main/kotlin/com/karyo/events/outbox/OutboxService.kt. The writer requires a PublishedEventType enum value and takes the row's aggregate_type from it. Its wire name follows aggregate.event, with lowercase hyphenated words. The event catalogue owns the names and their upgrade impact. V1__create_outbox.sql indexes unpublished rows partially.

It has one live reader:

  • The webhook relay. WebhookFanoutScheduler and WebhookDeliveryScheduler both run on {karyo.webhooks.poll-interval} with ConcurrentExecution.SKIP (.../relay/WebhookFanoutScheduler.kt, WebhookFanoutScheduler.scheduled, .../relay/WebhookDeliveryScheduler.kt, WebhookDeliveryScheduler). Fan-out matches subscriptions to outbox rows by Facility: a subscription receives its own Facility's events and the master-data events (null facility_id) of its Business Unit (.../relay/WebhookFanoutScheduler.kt, WebhookFanoutScheduler.runOnce).

OutboxEventRepository.recentForTenant is a second read path with no production caller since the Copilot's recent-activity tool was retired (ADR 0041). The agent recentActivity tool reads the inventory journal instead (ADR 0047).

DomainEvent<T> is unused

libs/karyo-events/src/main/kotlin/com/karyo/events/DomainEvent.kt defines an envelope (eventId, eventType, source, correlationId, businessUnitId, schemaVersion, payload). Its own KDoc (DomainEvent) says no production code constructs one, that internal notifications are synchronous CDI events, and that OutboxService writes the bare payload rather than wrapping it. The KDoc points to PublishedEventType for the aggregate.event naming contract. The webhook relay's WebhookEnvelope mirrors the shape when it builds a delivery.

The KDoc also cites a system-architecture document as the source of the envelope; no such document is in this repository. What actually leaves the process, and in what shape, is the webhook event catalogue.