---
title: Contracts and Messaging
summary: Owned APIs and events connect independently maintained BookWorm capabilities without sharing business state.
owners:
  - platform-engineering-team
badges:
  - content: Cross-cutting Concept
    backgroundColor: purple
    textColor: purple
---

## Purpose and Scope

Contracts and messaging preserve service autonomy by preventing cross-service data access and making every interaction explicit. They govern synchronous APIs, AI and tool protocols, integration events, and the durability assumptions of asynchronous delivery across all capabilities.

## Visual Reference

**Contract and Delivery Pipeline**

<MermaidFileLoader file="mermaids/002-contracts-and-messaging-diagram-1.mmd" />

## Core Rules and Standards

1. **Keep data ownership local.** No capability reads or writes another capability's authoritative store.
2. **Use the owned contract.** REST, gRPC, AI tool, agent, or event schemas belong to the owner and must describe stable externally visible behavior.
3. **Version compatibility explicitly.** Prefer additive evolution and backward-compatible defaults; treat renaming or removing fields and changing semantics as breaking changes.
4. **Carry meaningful envelope metadata.** Include message identity, event type and version, source, correlation, trace context, and causation where applicable.
5. **Treat delivery as at least once.** Consumers must tolerate duplicates, retries, restart recovery, and out-of-order work unless a documented protocol provides stronger guarantees.
6. **Make side effects idempotent.** Use stable operation or event identifiers to detect duplicate execution and verify authoritative state before compensating.
7. **Route exhausted work safely.** Retry only bounded, classified failures and isolate poison or permanently invalid records for review.

<Admonition type="warning">
	Schema compatibility is necessary but not sufficient. A field-compatible change can still break a
	consumer if names, units, meaning, ordering, or ownership semantics change.
</Admonition>

## Mechanism / How It Works

| Contract Type            | Standard                                                                              | Evolution Rule                                                                             |
| :----------------------- | :------------------------------------------------------------------------------------ | :----------------------------------------------------------------------------------------- |
| HTTP                     | Versioned resources and explicit media behavior                                       | Add optional fields and endpoints; retire old behavior through a published transition      |
| gRPC                     | Stable typed schemas and validated operations                                         | Use schema compatibility checks and avoid renaming or repurposing fields                   |
| Agent and tool protocols | Authenticated, least-privilege operations with machine-readable schemas               | Change permissions or tool behavior only through reviewed policy and consumer updates      |
| Integration events       | Immutable intent, stable event identity, and self-contained enough data for consumers | Add fields without changing existing meaning; publish new event types for semantic changes |

## Messaging Pipeline

| Stage     | Responsibility                                                                                                  |
| :-------- | :-------------------------------------------------------------------------------------------------------------- |
| Publish   | Validate and serialize from the owner; assign message identity and correlation                                  |
| Transport | Preserve envelope metadata and delivery acknowledgement                                                         |
| Persist   | Select outbox, inbox, or idempotency storage according to durability and duplicate needs                        |
| Consume   | Validate compatibility, enforce authorization, execute idempotently, and commit state atomically where possible |
| Fail      | Classify transient, permanent, and poison failures; retry transient work; route exhausted work for inspection   |

## Verification and Governance

- Run consumer and provider contract checks before merging contract changes.
- Verify additive and breaking schema changes against published baselines.
- Test duplicate delivery, replay, missing metadata, partial commit, and exhausted retries.
- Confirm production partitioning, retention, access, and replay ownership with the broker operator.
- Document event semantics and business effects, not only payload shape.

## Related Documentation

- [[doc|platform/006-api-and-contracts]] — Contract inventory.
- [[doc|arc42/08-crosscutting-concepts/006-testing-and-change-safety]] — Contract and compatibility gates.
- [[doc|arc42/08-crosscutting-concepts/003-validation-errors-and-resilience]] — Failure and retry standards.
