Karyo
Documentation/ Architecture

Modules and boundaries

Karyo 2.1.1rendered at build time
On this page

How Karyo's Gradle modules fit together, the rule that keeps them apart, and the three couplings the module graph does not show.

Derived from settings.gradle.kts, every services/**/build.gradle.kts and libs/**/build.gradle.kts, and the CDI wiring they produce. verifyModuleBoundaries re-derives the dependency edges from Gradle's project model on every test run.

The shape

One deployable, :services:karyo-app, aggregates every module as an ordinary Gradle project dependency (services/karyo-app/build.gradle.kts, dependencies). There is no second process (ADR 0001). The build has 55 subprojects: six shared libraries, 22 -api modules, 24 -core modules, the karyo-demo leaf, the on-demand karyo-inventory-ext-example, and the app itself.

Each domain is a karyo-{domain}-api / karyo-{domain}-core pair: the api module holds DTOs, SPI interfaces and cross-module event payloads, the core module holds JPA entities, services and REST resources (ADR 0006).

Seven -api modules have a -core beside them implemented by the engine modules in this repository: monitors, forecasting, slotting, simulation, cross-docking, waves and order streaming (see the engine boundaries). Two further engines, cartonization and document templates, have no api of their own: they implement SPIs declared elsewhere - PackoutStrategy in karyo-fulfillment-api and DocumentTemplateProvider in libs/karyo-documents. A tenth engine, 3PL billing, has no module of its own: it ships inside karyo-monitors-core.

At the Karyo 2.0.0 release all ten engines are ordinary modules of the one build (ADR 0029).

Shared libraries: karyo-common (base entities, Patchable<T>, warehouse time zone), karyo-events (the outbox), karyo-security (tenant context and scope), karyo-license (the GET /api/v1/license compatibility bridge), karyo-documents (rendering), karyo-sequence (number generation).

Identity boundary

karyo-auth-api owns provider-neutral principal, binding, capability and administration contracts. karyo-auth-core owns principal persistence and the /api/v1/principals management surface. The aggregator owns protocol wiring: OIDC validation-profile selection, exact binding augmentation, normalized /api/v1/me, and optional Keycloak administration/audit adapters. Other cores receive typed ActorSnapshot and principal UUID values from karyo-security; they do not parse provider claims or depend on another core's identity implementation.

The rule and its exceptions

core depends on its own api and on foreign api modules, never on a foreign core (ADR 0006). verifyModuleBoundaries reads every project's dependencies for -core edges and finds two exceptions, and only two:

Module Foreign cores What its build file says
karyo-demo 8 "Deliberate cross-core coupling" for a production-disabled leaf (services/demo-service/karyo-demo/build.gradle.kts, dependencies (cross-core waiver comment))
karyo-monitors-core 1, karyo-replenishment-core Labelled, not justified

The aggregator is not an exception: assembling every core is its job. ADR 0006 treats both as waivers, not precedent. A third, karyo-ai-core, left with the Copilot (ADR 0041). The module of that name that returned with the judgment engine seam (ADR 0064) is a different module and depends on no foreign core.

The second edge belongs to the event-monitors engine, whose below-reorder-point detector calls ReplenishmentService.needs(facility) (.../replenishment/service/ReplenishmentService.kt, ReplenishmentService.needs). karyo-replenishment-api contains no SPI it could have used instead. Why the edge was accepted rather than an SPI added is not recorded. The edge points the safe way in an intra-repository graph: karyo-monitors-core reaches into another engine's implementation module, never the reverse.

The pattern that keeps the graph clean when a dependency would otherwise point the wrong way is declare-in-your-own-api, implement-elsewhere: PurgeBlockerLookup (.../inventory/api/spi/PurgeBlockerLookup.kt), PickZoneLookup (.../fulfillment/spi/PickZoneLookup.kt).

Three couplings the module graph does not show

This is the most important structural fact in this document. The Gradle graph is one of four coupling channels, and reading it alone will mislead you about what depends on what.

1. The shared schema. Some modules read other domains' tables directly through native SQL and declare no dependency on the owning module at all:

Reader Reads Owner
KpiViewRepository in karyo-reporting-core karyo.kpi_* projections reporting's own views and V1009 daily SQL functions over inventory, orders and fulfillment; functions require a Facility and clock
The demand forecasting engine karyo.picks, inventory on-hand, karyo.item_data (item names) fulfillment, inventory, product
The slotting engine's readers karyo.stock_units, karyo.unit_loads, karyo.storage_locations, karyo.zones, karyo.picks, karyo.fix_assignments, karyo.item_data inventory, layout, fulfillment and product
The reorder simulation engine fulfillment tables fulfillment
The flow model's readers in karyo-flow-core karyo.picks, karyo.pick_orders, karyo.transport_orders, karyo.count_orders, karyo.shipments, karyo.shipment_orders, karyo.asns, karyo.asn_lines, karyo.delivery_orders, karyo.delivery_order_lines, karyo.stock_units, karyo.unit_loads, karyo.fix_assignments, karyo.storage_locations, karyo.zones fulfillment, tasks, stocktaking, orders, inventory and layout

The three read-side insight engines - forecasting, slotting and simulation - have no domain -core on their Gradle classpath at all. Read their build files and they look like isolated leaves. They are not: they read this repository's schema directly, and changing one of those tables, or an encoding their queries filter on, changes a contract they depend on. Only two encodings are linked at compile time, as described below (see Data and persistence).

Two of the encodings those queries filter on are linked at compile time instead of copied. A stock unit's state comes from StockState.ON_STOCK in karyo-inventory-api (OnHandReader, SlotReader, StockExpiryReader, ExpiryRiskContextReader) and a pick's type from PickingType.EXTINGUISH in karyo-fulfillment-api (DemandReader, VelocityReader, SimDemandReader, CategoryVolumeRepository, FlowHistoryReader). Those readers therefore depend on an -api module, which the rule allows, and still on no foreign -core. Renaming or renumbering either constant is a compile error in every reader. Every other state number and column name a reader filters on remains a bare literal.

Reading the shared schema directly is a recorded decision for reporting (ADR 0011). It has a cost: the coupling is invisible to the build, so it breaks at runtime rather than at compile time.

The simulation engine has one more runtime dependency that no Gradle edge declares. SimulationService takes the ewma ForecastModel or else the first one it finds, and karyo-simulation-core depends on the forecasting API only. karyo-app packages both cores, so the shipped product always resolves a model. An assembly without karyo-forecasting-core would fail on the first simulation request with an unmapped NoSuchElementException, an HTTP 500.

2. The CDI event bus. Observers bind by payload type across the whole aggregated classpath. The wave engine observes WavePickActivityEvent, which karyo-fulfillment-core fires. There is no Gradle edge from fulfillment to the wave engine and there should not be - the payload type deliberately lives in karyo-fulfillment-api "so wave-core can @Observes it without a cross-core Gradle edge" (.../fulfillment/event/WavePickActivityEvent.kt, WavePickActivityEvent). The dependency is real, it points the right way, and it is only visible if you read the observer.

3. SPI implementor resolution. A core depends on a foreign api to get an interface, but which bean implements it is resolved by CDI at runtime. The graph tells you a module consumes a contract; it does not tell you who satisfies it - and in the assembled application the engines are among the satisfiers.

What enforces the rule

verifyModuleBoundaries (buildSrc/src/main/kotlin/com/karyo/conventions/VerifyModuleBoundaries.kt, wired in the root build.gradle.kts) reads every project's dependencies from Gradle's own project model, so it sees a dependency however a build file declares it, and fails when a module other than the aggregator karyo-app has a project dependency on a -core. The two waivers above are the only edges it lets through, and it also fails when a waiver no longer matches a real edge, so a retired waiver cannot linger in the list. The task is a dependency of every Test task and of every check, so a new core-to-core edge fails ./gradlew test instead of waiting for review. To add a module that must read another domain, depend on that domain's -api and bind the implementation by CDI, as the pattern above does. The waiver list is in VerifyModuleBoundaries; changing it needs an amendment to ADR 0006 first.

The task sees Gradle project edges only. The three couplings above are invisible to it. The encodings a reader filters on are linked to the owning modules' constants as described under the shared schema, the buildSrc plugin versions are read from gradle/libs.versions.toml rather than restated, and the template allowlist and the catalogue defaults each have a test (DocumentTemplateAllowlistTest, CatalogueDefaultsTest).

A catalogue key has one default, the catalogue's, and a runtime caller cannot pass its own. One recorded exception remains. Quarkus reads a few catalogue keys at boot, outside the runtime lookup, and needs a value to start: karyo.agent.resource-uri, karyo.identity.provider-type, the karyo.shipping.ship-from keys, the judgment engine's provider, model, base URL and timeout, and the %dev and %prod OIDC audience. application.yaml restates the catalogue default for each as a literal fallback. CatalogueDefaultsTest pins every such literal to the catalogue and fails when one drifts. The two MCP keys that follow karyo.agent.resource-uri (quarkus.oidc.agent.token.audience and quarkus.oidc.agent.resource-metadata.resource) are not literals: they reference it as ${karyo.agent.resource-uri}, so they cannot drift from it.

api edges between API modules

Most -api modules declare api(project(":libs:...")) for the shared libraries whose types appear in their DTOs. Three declare api(...) on another -api module, because their own public signatures expose its types and consumers need them transitively:

Module Depends on Why
karyo-wave-api karyo-orders-api WaveOrderView and ShortageView SPIs
karyo-flow-api karyo-layout-api the flow model's floor and distance types
karyo-flow-api karyo-inventory-api PickSourceOrder, in Candidate.sourcing: the rule the simulator's policy P takes stock by is the one live stock selection applies
karyo-orders-api karyo-inventory-api PickSourceOrder, the typed pick-source policy owned by stock selection, in the strategy DTOs and PickingStrategyView

Any other edge between -api modules needs the same reason: the public types leak, so implementation(...) would not compile for a consumer.

Aggregator declarations

services/karyo-app/build.gradle.kts declares every core, the engine modules included (dependencies). It additionally declares the union of Quarkus extensions explicitly even though they arrive transitively (dependencies (Quarkus extension union)), "so the aggregator app is the single source of truth for the assembled image". That is a deliberate redundancy and the file says so.

karyo-inventory-ext-example is always a subproject, but it reaches the image only when -PkaryoInventoryExample=true (providers.gradleProperty("karyoInventoryExample")), with the comment "Deliberate build-time augmentation, never a runtime JAR upload." That matches the extensibility contract: extensions are on the classpath before Quarkus augmentation, never uploaded into a running image (ADR 0019).