Karyo
Documentation/ Decision records

ADR 0006: `-api` modules hold contracts, `-core` modules hold implementations, and no core depends on another

Karyo 2.1.1rendered at build time
On this page

Status: Accepted

Context

Every module runs in one process (ADR 0001), so nothing at runtime stops one module from reaching into another's entities and services. The boundary between modules is only as strong as the build graph that describes it. Held, it keeps each domain understandable on its own, lets a client's extension compile against contracts rather than implementations (ADR 0019), and lets the engine modules plug into the product through published seams (ADR 0029).

Decision

  • Each domain is a pair of modules. karyo-<domain>-api holds DTOs, SPI interfaces and the event payloads other modules observe. karyo-<domain>-core holds JPA entities, repositories, services and REST resources.
  • A -core depends on its own -api, on other modules' -api, and on libs/*. It never depends on another module's -core, apart from the two waivers below.
  • When a dependency would point the wrong way, the module that needs the answer declares the interface in its own -api and the module that has the answer implements it. PurgeBlockerLookup is declared in inventory and implemented by fulfillment, orders, stocktaking and tasks (PurgeBlockerLookup); PickZoneLookup is declared in fulfillment (PickZoneLookup).
  • An event payload that another module observes is declared in the firing module's -api, so the observer needs no edge to the firing module's core (WavePickActivityEvent).
  • -api modules depend on each other with implementation, except where a module's own public signatures expose the other's types, which needs api(...). The edges that do are listed in Modules and boundaries.
  • Every module whose beans the application must discover carries a META-INF/beans.xml; thirty-two modules do.
  • Two waivers, recorded as waivers and not as precedent. A third, karyo-ai-core (the Copilot's tool wrappers over domain services), was retired with the Copilot (ADR 0041):
    1. karyo-demo depends on eight other cores. It is a leaf module that is off in production and invisible when off - its routes answer 404 unless KARYO_DEMO=on - and it reuses the domain entities and repositories to construct back-dated demo data (services/demo-service/karyo-demo/build.gradle.kts, dependencies (cross-core waiver comment); DemoEnabledFilter; services/karyo-app/src/main/resources/application.yaml, karyo.demo).
    2. The monitors engine, karyo-monitors-core, depends on karyo-replenishment-core to call the replenishment service's needs query. Its reason is not recorded. karyo-replenishment-api holds only its DTOs and ReplenishmentStrategy, so there is no SPI the engine could have used instead.
  • A new -core-to--core dependency needs, at minimum, a justification comment in the build file. Unless it is a production-disabled leaf, the right answer is an SPI in the owning -api.

Consequences

  • The contract surface is explicit: each -api is what other modules, extensions and the engine modules may rely on.
  • Which bean satisfies an SPI is resolved by CDI at runtime. The graph shows who consumes a contract, not who fulfils it.
  • A Gradle task enforces the rule. verifyModuleBoundaries reads the project dependencies from Gradle's own project model and fails on a new -core-to--core edge outside the waivers above, and on a waiver that no longer matches an edge. It runs before every test task and with check. There is still no ArchUnit test or Detekt rule: the task looks at declared project dependencies and nothing else.
  • The Gradle graph is one of four coupling channels, not the whole picture. Modules also couple through the shared schema (ADR 0011), through CDI events that observers bind by type (ADR 0008), and through SPI implementations resolved at runtime (Modules and boundaries).
  • Every cross-module question costs an interface in the owning -api: thirty-seven lookup, port and guard interfaces sit in the -api modules.
  • A -core cannot boot on its own, so its tests run in the aggregator against the real implementation of every SPI it consumes (ADR 0001).

Alternatives considered

  • One module per domain, with no -api/-core split. Rejected: contract and implementation would blur into one unit that can only be consumed whole, and extracting a module later would be impossible. The split costs little.
  • A network boundary between modules. Rejected; see ADR 0001.

Evidence

  • settings.gradle.kts, include - every domain as an -api/-core pair
  • services/demo-service/karyo-demo/build.gradle.kts, dependencies (cross-core waiver comment) - waiver 1 and its comment
  • services/demo-service/karyo-demo/src/main/kotlin/com/karyo/demo/api/v1/DemoEnabledFilter.kt, DemoEnabledFilter - the demo routes answer 404 when off
  • services/inventory-service/karyo-inventory-api/src/main/kotlin/com/karyo/inventory/api/spi/PurgeBlockerLookup.kt, PurgeBlockerLookup - declare here, implement there, four implementors
  • services/fulfillment-service/karyo-fulfillment-api/src/main/kotlin/com/karyo/fulfillment/event/WavePickActivityEvent.kt, WavePickActivityEvent - a payload placed in -api to avoid a core edge
  • services/wave-service/karyo-wave-api/build.gradle.kts, api(project(":services:order-service:karyo-orders-api")) - one of the api(...) edges between -api modules
  • buildSrc/src/main/kotlin/com/karyo/conventions/VerifyModuleBoundaries.kt, VerifyModuleBoundaries - the rule and its two waivers as a Gradle task
  • Modules and boundaries
  • ADR 0001 - why the boundary is a build rule rather than a network
  • ADR 0007 - the data half of the same boundary
  • ADR 0019 - extensions build against -api modules
  • ADR 0029 - the engine modules plug into -api seams