Skip to content

Architecture / Decision records / ADR-HIS-044

ADR-HIS-044 Service template: the unit build, the service kit and its conventions

Status Accepted
Decided 2026-11-24 by session FN-S03
Record Drafted 2026-11-24; approved 2026-11-25 by the Enterprise Architecture Board
Where the rule lives services/_template/ (its README.md, new-unit.sh, service-kit/, buildSrc/); the agent instruction file CLAUDE.md, section 3

Context

Every domain service unit of architecture section 5.2 is a Java 21 and Spring Boot modular monolith (ADR-HIS-005) in a folder of its own, with its own Gradle build and lockfile (ADR-HIS-039). Card FN-S03 asks for a template that carries what every service must: health endpoints, token validation, the call to the policy decision point, audit emission, the transactional outbox and idempotent inbox, schema migrations, telemetry with correlation identifiers, and the test layout. Twenty-five build sessions will make their units from it, side by side, and none of them may change another unit's folder (session rule 10).

The specifications fix what these duties do (architecture sections 6.2, 6.3, 7.5, 8; IF-ISE-001, IF-ISE-002, IF-ESS-003, IF-ESS-008, DEC-ESS-018), not how a unit is built. Three contracts the template needs do not exist yet: the decision API of the policy decision service and the audit intake channel (both D10, fixed by the ESS contract sessions), and the event envelope's wire form and schema registration (FN-S04).

Options considered

  • How units share the cross-cutting code: a library published once and depended on by every unit; a Gradle composite build including services/_template from every unit; or a copy of the kit in each unit, made when the unit is made.
  • Persistence access: JPA with Hibernate, or Spring's JdbcClient with SQL.
  • Schema layout: one schema and one Flyway history per unit, or one schema and one history per module.
  • Outbox relay: a separate relay process (change data capture with Debezium), or a relay inside each unit polling its outbox table.
  • Backbone client: Spring for Apache Kafka, or the Kafka client library behind the kit's own transport interface.

Decision

  • A copy of the kit in each unit. services/_template/new-unit.sh <unit> <MODULE-ID>... copies the build, the kit (service-kit/), the application (app/) and the Dockerfile into services/<unit>/, and makes one empty module per identifier from skeleton/module/. From then on the copy is the unit's code. The template is itself a unit (_template, domain tpl) that builds and passes its tests; its example module MOD-TPL-000 (number 000 is never a real module) is copied only with --with-example, for the template check.
  • One Gradle build per unit, Gradle 9.9.2 through the wrapper, with conventions in buildSrc (his.java-module, his.service-app). Every folder mod-* is a module subproject, named by the module identifier in lowercase; the application depends on all of them. Every configuration is locked; ./gradlew dependencies --write-locks writes every subproject's gradle.lockfile. Spring Boot 4.1.3 is the BOM (the 4.2 line was five days old). Compiler warnings fail the build.
  • The kit is a Spring Boot auto-configuration in the package care.cema.his.kit (not a module, so outside the module naming rule). A module declares what it brings as beans: ModuleSchema, EventTopic, EventSubscription. The kit maps the HIS_* variables of his-env env (ADR-HIS-043) to Spring properties at the lowest precedence, so a unit runs against the integration environment with no settings of its own and production overrides each one.
  • JdbcClient and SQL; one schema per module. Each module owns the schema mod_<d>_<nnn> of the unit's database, with its own Flyway history; the kit's tables are in his_kit. Modules use each other only through an api package, checked by an ArchUnit test in every unit.
  • Outbox and relay in the unit. Events and audit records are rows of his_kit.outbox, written in the transaction of the change, with a gapless per-aggregate sequence from his_kit.aggregate_sequence. A relay in the unit polls the outbox, one instance at a time (a PostgreSQL advisory lock), and publishes in outbox order. This is the producer pattern of CMP-ISE-014.
  • Inbox in the unit. A consumed event is applied in one transaction with its marker in his_kit.inbox, keyed on the subscription and event_id. An ordered subscription keeps the last applied sequence per aggregate in his_kit.inbox_position and holds early events in his_kit.inbox_held. Each subscription is the consumer group <unit>.<name> with the dead-letter topic <topic>.dlt.<unit>.<name>.
  • Backbone behind the kit's EventTransport (INF-001), with a Kafka adapter on the Kafka client library: idempotent producer, acks=all, offsets committed after the inbox commits. Topics are created at start only when his.events.kafka.create-topics is set, which the local environment does; production topics are made by the event backbone platform unit.
  • Policy enforcement fails closed, and tells an outage from a refusal. PolicyEnforcer.require(...) asks the decision point with subject, action, resource, patient, encounter, purpose, facility and environment. A Deny, or a Permit with an obligation the caller did not declare, is answered 403 unauthorized. An error, a timeout (250 ms, the CFG-ISE-031 call timeout) or an unreadable answer is also a Deny. It is answered 503 unavailable, "Authorization service unavailable", with a retry indication, as D02-ISE section 9.1 and DEC-ESS-018 require for internal writes and other internal paths; CUX-67 forbids showing an outage as "No permission". Every refusal is audited in a transaction of its own, an outage as access-check-unavailable. Decisions are not cached yet, so the Tier 1 cached-Permit read path of DEC-ESS-018 (CFG-ESS-025) is not offered: during an outage every path is refused.
  • Token audience is optional. The token is checked for signature, issuer and expiry, and for audience when HIS_OIDC_AUDIENCE is set. Patient tokens are kept out of staff units by the issuer: patients are a separate identity population (DEC-ESS-001) with a realm of their own. The test realm sets no staff audience, so a required audience waits until the ESS identity contract names the audiences.
  • Audit through the outbox. When the buffer of unpublished audit events is full (DEC-ESS-018), a Tier 1 unit carries on, with the outbox as its local durable log; any other unit answers "unavailable".
  • Request conventions. The header names of IF-ISE-001, which the specification does not spell: X-Correlation-Id, X-Facility, X-Purpose-Of-Use, Idempotency-Key; W3C traceparent for tracing; the API version in the path (/v1/). Errors are RFC 9457 problems with code (one per IF-ISE-001 error class) and correlationId.
  • Telemetry. Traces and metrics over OTLP through Spring Boot's OpenTelemetry support, sampling everything by default. Logs are ECS JSON on standard output. An event carries the traceparent of the span that wrote it, and its consumer continues that trace.
  • Provisional contracts, each in one class. The decision API (POST {url}/v1/decisions, HttpPolicyDecisionClient), the audit intake (his.ess.auditintake.v1, type his.ess.AuditEventSubmitted, AuditProperties), and the CloudEvents mapping of the envelope (structured JSON with lowercase extension attributes, EnvelopeCodec) are the template's until their contract sessions fix them.
  • Tests on the light profile. Each test context gets a scratch database in the session's PostgreSQL, test tokens signed by a key of the test, a loopback backbone and a fake policy decision point that denies by default. The template check (services/_template/check.sh) runs a unit made from the template on the full profile.

Rationale

A published library would need a repository and a release process before any unit can build, and its versions would have to be kept in step across 25 units. A composite build would let a template change break every unit at once, from a folder none of their sessions owns. A copy keeps each unit an independent build, as ADR-HIS-039 requires. Its cost is drift: a fix needed by every unit is made in each. Nearly all of the kit is one small package, so that cost is low, and FN-S05 can report kit drift across units.

JdbcClient keeps the SQL visible and the dependency set small. The clinical persistence model is still to be proven in RC-5 (architecture 7.6), and a unit that needs JPA adds it to its own modules. A schema and history per module keep the module boundaries in the database too, and spare modules from sharing a migration numbering.

A relay inside the unit needs nothing outside the unit's own database and the broker, works the same in the local environment, and keeps the order of each aggregate with one lock. Change data capture is selected for analytics (technology stack row 31) and can replace the polling relay later without changing the outbox table. The Kafka client library under the kit's own interface keeps Kafka out of domain code and gives explicit control of when an offset is committed. Spring for Apache Kafka would add a second retry and dead-letter mechanism beside the inbox's.

Failing closed on every policy error follows IF-ESS-003 and architecture section 8.3: a missing or stale attribute gives Deny, never a silent Permit.

Consequences

  • A build session runs new-unit.sh once, in its own unit folder, as the first step of its first card on that unit, then ./gradlew dependencies --write-locks and ./gradlew build. CLAUDE.md section 3 says so.
  • Units start with the same kit and may then diverge. A change every unit needs is made in the template by a session whose card covers it, and in each unit by its own sessions.
  • The provisional decision API, audit intake and envelope mapping are replaced when their contracts exist. No unit has been made yet, so the first to change is the template itself. The contract sessions name the change in their handovers.
  • Until the policy decision point is in the integration environment, tests use the fake and the template check uses a stub that permits. A unit cannot start without HIS_POLICY_URL.
  • Not in the template, and left for the cards that need them: replay of commands by Idempotency-Key, optimistic concurrency (ETag, If-Match), caching of policy decisions with invalidation, the retry on another replica and the circuit breaker of CFG-ISE-031, the Tier 1 cached-Permit read path of CFG-ESS-025, a required token audience, Break-the-Glass, mTLS between workloads, secrets from OpenBao, and the outbox relay of the platform (CMP-ISE-014) if one is built.