Skip to content

Implementation plan / Session plan / Handover FN-S04

Handover FN-S04 Foundation: Contract and event tooling

Card: FN-S04. Issue: cema-care/his#4. Branch: session/FN-S04.

What was built and where

Everything is in contracts/common/. Its README.md is the reference for contract, build and integrate sessions. It covers the layout of a domain's contract folder, the conventions, the shared types, the envelope, the compatibility rules, registration, stubs, contract tests and the commands. The decisions are in ADR-HIS-045 Contract tooling: formats, envelope, shared types, compatibility and contract tests.

  • Formats and layout. A domain's folder contracts/<domain>/ holds these, with the layout of contracts/common/example/:
  • contract.yaml: the index of APIs and events, with each event's version, aggregate, topic, facility scope and sequencing;
  • api/<name>.v<major>.openapi.yaml, in OpenAPI 3.1;
  • events/<EventName>.v<major>.schema.json, in JSON Schema 2020-12;
  • the generated stubs/;
  • a package.json (in the README) that links the tool from ../common.
  • Shared type library types/types.v1.json: the types of architecture 6.6 in FHIR R4 field names. Decimals are strings.
  • Identifier, Coding, CodeableConcept;
  • Quantity with UCUM, Money;
  • Instant, ZonedInstant, Date, Period;
  • Reference with its person, encounter and facility forms;
  • Actor, Provenance, StatusWithReason;
  • FacilityCode, and Problem with the error classes of IF-ISE-001.
  • Event envelope envelope/envelope.v1.json: the CloudEvents 1.0 structured-mode form that the service template's EnvelopeCodec writes, now fixed. The template does not change. checkEvent adds the rules a JSON Schema cannot state:
  • facility is required for facility-owned aggregates and absent for enterprise-scoped ones;
  • sequence is required when the event is sequenced;
  • the source must be the owning domain;
  • the aggregate type must match;
  • the major version must be known.
  • Common API parts openapi/components.v1.yaml: the headers of IF-ISE-001 (X-Correlation-Id, X-Facility, X-Purpose-Of-Use, Idempotency-Key, If-Match), the bearer scheme, and the error answers as Problem.
  • The tool his-contract (TypeScript in src/, run directly by Node.js 22.18 or later; library in src/index.ts):
  • lint: the conventions. These are:
    • event, topic and subject names;
    • agreement with the event catalogue;
    • open event payloads;
    • the common headers and error answers;
    • x-his-facility on every operation: X-Facility required when owned;
    • x-his-cross-facility: hidden | policy on an owned operation by identifier, with 404 documented when hidden (review round 1);
    • If-Match on updates;
    • examples that match their schemas;
  • check: lint, plus compatibility with a git revision (by default origin/main), plus stubs up to date. check-common checks the shared library itself;
  • stub: generates the stubs. serve runs the stub server, which records violations;
  • verify: the provider contract tests, with the facility denial cases;
  • validate-event: checks events against the envelope and the contract;
  • register: registers event schemas in Apicurio Registry 3. topics lists the topics of a contract.
  • Worked example contracts/common/example/: the contract of the template's probe module, domain tpl. It holds recordProbe, readProbe and the event his.tpl.TemplateProbeRecorded on his.tpl.templateprobe.v1, with its generated stubs.
  • Check contracts/common/scripts/ends-when.sh: the "Ends when" check of this card, through the command line, repeatable by any later session.
  • CLAUDE.md section 3: the FN-S04 entry is marked as landed, with how a contract folder is made, built, tested and used.
  • Decision record index: the row of ADR-HIS-045 and the count of 45, in the form build_adrs.py writes. This was checked by running the generator on a scratch copy and diffing; the result was identical. The plan build was not run in the checkout.

Dependencies, locked in contracts/common/pnpm-lock.yaml:

Kind Packages
Runtime ajv 8.20.0, ajv-formats 3.0.1, yaml 2.9.1
Development typescript 5.9.3, @types/node 22.20.5, vitest 3.2.7 (the web packages' versions)

Checks and their result

Node.js is not installed on this server. The checks ran in the node:22-bookworm container (Node.js 22.23.5, pnpm 10.34.9 through corepack), as FN-S06's did. The integration environment ran under HIS_SESSION=FN-S04 (his-fn-s04).

Check Command Result
Type check pnpm build in contracts/common Passed
Tests, light (no registry) pnpm test After review round 1: 85 passed, 1 skipped (the live registry test)
Tests, full profile his-env start full, HIS_SCHEMA_REGISTRY_URL from his-env env, then pnpm test 80 of 80 passed. Afterwards the subject his.tpl.TemplateProbeRecorded was listed by the Confluent-compatible API, with the rules INTEGRITY and VALIDITY
Ends when scripts/ends-when.sh (with HIS_SCHEMA_REGISTRY_URL on the full profile) Passed: (1) check-common and check example pass; (2) removing note from the Probe answer fails with 4 breaking findings; (3) the stub generated from the example passes all 10 provider cases of verify, with no violation recorded; registration is unchanged on a second run
A domain's folder links the tool A trial folder contracts/zzz with the README's package.json, pnpm install, pnpm build, pnpm test Installs, and the lockfile holds only link:../common. The lint refused the copied example event as "not in the event catalogue", as it must for a real domain. The trial folder was not committed
Environment reset his-env reset Nothing of the session left (0 containers)
Decision record index matches the generator build_adrs.py on a scratch copy, then diff Identical
Link check of docs/ python3 docs/implementation_plan/tools/check_links.py 0 broken links, 0 duplicate anchors
Links in CLAUDE.md and the READMEs of contracts/common A one-off script applying the same rule 41 links, 0 broken
Documentation site build mkdocs build Builds

Requirements and test scenarios

The card implements no functional requirement and lists no test scenario. The tests are in contracts/common/test/, named FN-S04 and the rule they show:

Card item Tests
Ends when: the example contract passes the check compat.test.ts "Ends when: the example contract passes the check" (against itself, as new, and the shared library)
Ends when: a breaking change fails the check compat.test.ts "Ends when: a breaking change fails the check": 17 breaking changes, among them a field removed, a type changed, a new required field or header, an operation, event or answer removed, a topic or scope changed, an enum grown in output, no new version and a version gone back. "additive changes are compatible": 9 compatible changes, among them a new major beside the old
Ends when: a stub generated from it answers a contract test harness.test.ts "Ends when: a stub generated from the example answers a contract test"
Shared type library (6.6), FHIR R4 types.test.ts "the shared type library, aligned with FHIR R4"; compat.test.ts "the shared types change only compatibly"
Event envelope (6.3, IF-ISE-002) types.test.ts "the event envelope" (including what the template's EnvelopeCodec writes); harness.test.ts "events checked against the contract, on both sides"
Facility identifier and denial cases (7.5) harness.test.ts: 5 provider cases tagged ARC-7.5 on the example (4 when the read is policy), a provider that serves any facility fails them, the stub keeps a consumer's records to its facility, and the scope rules of events; lint.test.ts (owned operations require X-Facility and declare x-his-cross-facility; hidden documents 404)
File layout and conventions lint.test.ts (17 cases, including the event catalogue)
Registration in the schema registry registry.test.ts (fake registry: subject, version, rules, idempotent, immutable; live Apicurio on the full profile)
Stub generation harness.test.ts "stub generation" (committed stubs up to date)
Contract tests on both sides harness.test.ts "consumer side" (violations recorded) and "provider side" (a non-conforming provider fails)

Review round 1

  • Cross-facility reads are declared per operation. Before this change, the harness, the stub and the lint required a 404 on every cross-facility read of a facility-owned record. Architecture 7.5 states that only for administrative and operational functions; clinical access crosses facilities under the policy decision. Each owned operation by identifier now declares x-his-cross-facility:
  • hidden: 404, and the harness tests it. The example's readProbe is hidden, as the template's service-layer filter answers;
  • policy: the stub answers the record, and the harness fixes no answer.
  • Changing the declaration is breaking. The tests are in lint.test.ts, compat.test.ts and harness.test.ts ("a read declared x-his-cross-facility: policy ...").
  • The stub records a missing credential as a violation, so a consumer test catches a consumer that forgets its token (harness.test.ts "records a consumer that forgets its credential").
  • actor and actorkind stay optional in the envelope. The kit's DomainEventPublisher leaves them out when the context has no actor, which happens for events published outside a request. Requiring them would refuse events the template writes. Follow-up below.

Differences from the card or specification

  • The example is the template's probe contract, under contracts/common/example/. It is not in a domain folder, because those belong to the domains' contract sessions (CLAUDE.md section 2). Its domain code tpl is exempt from the event catalogue check.
  • The envelope is the template's mapping, unchanged (ADR-HIS-045). schemaversion may be a bare major ("1"), which is what the template writes, and it means 1.0.
  • The registry does not decide compatibility. Architecture 6.3 says compatibility "is checked as backward within a major version". Apicurio's rule spans every version of an artifact, so it cannot allow a new major to break. The check in the pull request decides compatibility; the registry carries VALIDITY and INTEGRITY.
  • Contract tests verify against the contract, not consumer pacts (ADR-HIS-045). The provider's contract is fixed first and approved by the domain leads.
  • No Java harness. A Java unit runs his-contract verify and validate-event from its contract test. A JUnit helper belongs in the service template (follow-up below).

Questions for a person

None. ADR-HIS-045 awaits the approval of the Enterprise Architecture Board, which is given on the merged work.

Follow-ups outside the card

  • FN-S05 (pipeline): on any change under contracts/:
  • in contracts/common: pnpm install --frozen-lockfile, pnpm build, pnpm test, pnpm check;
  • in each domain folder with a package.json: pnpm install --frozen-lockfile, pnpm build, pnpm test;
  • fetch origin/main first; use Node.js 22.18 or later;
  • on merge, his-contract register each changed domain against the shared registry.
  • Service template (a session whose card covers services/_template):
  • a JUnit helper for contract tests: it starts the module, runs his-contract verify against it and fails the test on a failed case, behind the contract tag, in the folder the template README names;
  • a test hook that writes published events as JSON lines for validate-event;
  • DomainEvent should take the contract's <major>.<minor> as its schemaVersion;
  • running verify against the real template unit also needs a policy decision point that denies an unpermitted facility, because the check's stub permits everything.
  • Actor on every event: architecture 6.3 lists actor and purpose for audit and policy. The kit omits actor and actorkind when there is no actor in context. Once the kit always sets the acting service as actor (a template card), envelope.v1.json can require both. That is a tightening of the envelope, so it is safe only before the first unit publishes.
  • Event backbone (MOD-ISE-004): create topics in shared environments from his-contract topics of every domain (ADR-HIS-045).
  • Contract sessions (RC-1 onward): copy the example's layout and the README's package.json. Where a section 7.3 entity lacks attributes, write them into the specification in the same merge request (6.6 point 3).
  • Leftover on the server: the untracked trial folder contracts/zzz/ in this worktree, with its node_modules, could not be removed in this session (deletion was not permitted). It is not committed and goes with the worktree.