35. Site transfer-capability command raises SiteCapabilityChanged
Status
Accepted (2026-10-12, gap-fix wave decisions D1 and D2). Extends
ADR 0025's version guard
to the Site aggregate. Implements the SiteCapabilityChanged contract that
apis/asyncapi.yaml already declared. Amended by
ADR 0041: a periodic heartbeat also
re-publishes the current snapshot (revision + 1) so consumers' freshness
windows hold; the PUT's idempotent no-op below is unchanged, but the revision
is no longer a count of capability changes.
Context
com.warehouse.wms.facility-layout.site.SiteCapabilityChanged was declared
in the AsyncAPI contract, had a domain event (shared.NewSiteCapabilityChanged),
a wire DTO (internal/adapters/kafka/cloudevents/wire.go), a partition key
(the site code), golden wire files and a contract fitness test. No use case,
REST route or MCP tool raised it. network-inventory-planning (NIP) consumes
it into its site_capability read model and approves an inter-warehouse
transfer only when the origin site may donate and the destination site may
receive. A site NIP has never heard about is capable of nothing, so with no
producer every transfer was refused.
Whether a building may act as a transfer origin or destination is site master
data. facility-layout owns the Site aggregate and is the only context that
may change it; NIP must not infer capability from layout topology.
The contract fixes the shape: a full last-write-wins snapshot
(site_code, transfer_origin_enabled, transfer_destination_enabled,
capability_revision) where consumers keep the highest revision. The payload
is snake_case while every other facility-layout payload is camelCase.
ADR 0025 left sites unversioned because RegisterSite was its only writer.
A capability command adds a read-modify-write on the same row. Two concurrent
changes would both load revision N and both commit revision N+1 with
different values; NIP keeps whichever arrives first at N+1 and drops the
other, while the sites row holds the last write. The read model and the
system of record would disagree.
Decision
- The capability lives on the Site aggregate. A
TransferCapabilityvalue object (originEnabled,destinationEnabled) plus acapabilityRevision(int64). A new site is capable of nothing at revision 0, which means no snapshot was ever published.Site.ChangeTransferCapability(c):- rejects a decommissioned site (
ErrSiteDecommissioned, REST/MCP slugsite-not-active, the slug used for the same condition elsewhere); - treats the first change as a change even when it restates the default, because nothing has been published yet;
- treats an identical snapshot afterwards as a no-op (no revision, no event);
- otherwise replaces the snapshot and advances the revision by exactly one.
- rejects a decommissioned site (
- Command:
PUT /sites/{siteCode}/transfer-capabilitywith body{"transferOriginEnabled": bool, "transferDestinationEnabled": bool}. Both fields are required: an omitted or null field is 400missing-transfer-capability-field, never a silentfalse. The response (200) is the resulting{siteCode, transferOriginEnabled, transferDestinationEnabled, capabilityRevision}. Unknown site: 404site-not-found. The PUT replaces a full snapshot and a repeat is a no-op, so it is idempotent by construction and is not wrapped by the Idempotency-Key middleware (ADR 0019), like the geometry PUTs.GET /sites/{siteCode}/transfer-capabilityreturns the same shape. - Use case
ChangeSiteTransferCapabilityloads the site, applies the change and, only when it produced a revision, saves the site and publishesshared.NewSiteCapabilityChanged(now, code, origin, destination, revision)inside oneatomicallyunit of work. With Postgres andEVENT_PUBLISHER=kafkathe event leaves through the transactional outbox (ADR 0018) as a CloudEvents 1.0 message (ADR 0024) onwarehouse.facility.eventsandwarehouse.facility.analytics, keyed and subjected by the site code. The analytics projector ignores it. - Persistence and concurrency. Migration
0008_site_transfer_capabilityaddstransfer_origin_enabled,transfer_destination_enabled(default false),capability_revision(default 0) andversion(default 1) tosites. All additive.SiteRepo.Savebecomes the ADR 0025 single-statement version-guarded upsert (... ON CONFLICT (code) DO UPDATE ... WHERE sites.version = $n,RowsAffected() == 0→ports.ErrConcurrentModification, REST 409concurrent-modification). The loser's outbox row rolls back with its transaction, so two committed snapshots never share a revision. There is noIf-Matchheader: this repository's mutable resources use the server-side version column only (ADR 0025), and a full-snapshot PUT has no stale-field hazard for the caller. - MCP:
list_sitesentries (and thesiteheader ofget_site_layout) gaintransferOriginEnabled,transferDestinationEnabledandcapabilityRevision. No write tool is added; the MCP server stays read-only. - Payload casing is kept (D2).
SiteCapabilityChangedkeeps its snake_casedatafields. It is in the AsyncAPI contract and NIP decodes exactly those names. Recasing them would be a breaking.v2with a dual-publish migration for no consumer benefit. The inconsistency with every other camelCase payload is recorded here and in the AsyncAPI message description; a future.v2of this event, if one is ever needed for another reason, should take the camelCase names then.
Consequences
Positive
- NIP's
site_capabilityread model receives facts, so transfer approval can succeed for sites an operator enabled. - The revision is owned by the aggregate and guarded by the row version, so
last-write-wins on
capability_revisionis safe: the newest snapshot NIP keeps is the one thesitesrow holds. - A retried PUT never inflates the revision or republishes.
Negative / accepted
- A concurrent capability change of the same site gets 409 and must re-read and retry; there is no automatic retry (fleet pattern).
- Every
siteswrite is now version-guarded.RegisterSiteandImportFacilityLayoutonly save sites they have just proved absent, so they see no behaviour change. RehydrateSitegained parameters; only the Postgres adapter calls it.- One payload in this context stays snake_case.
- Existing sites start capable of nothing at revision 0. An operator must PUT each site that may take part in transfers; until then NIP refuses transfers involving it (fail-closed, as before).
Verification
- Domain:
internal/domain/site/capability_test.go(first change, idempotent repeat, +1 per change, decommissioned site rejected, rehydration). - Use case:
internal/application/usecases/transfer_capability_test.go(event payload and revision, no-op repeat, unknown/retired site, infrastructure failures). - REST:
internal/adapters/inbound/http/transfer_capability_test.go. - BDD:
features/site_transfer_capability.feature. - MCP:
TestServer_ListSitesOverTheWireasserts the capability fields. - Integration (testcontainers Postgres,
internal/adapters/outbound/postgres/site_capability_integration_test.go): the outbox row is the exact CloudEvent NIP decodes (type, subject, key, dataschema, snake_casedatadecoded through a hand-mirrored copy of NIP's DTO); an identical repeat writes no row; two concurrent changes of one site produce exactly one winner and oneErrConcurrentModification. - Contract:
apis/openapi.yaml(getSiteTransferCapability,setSiteTransferCapability) under the Schemathesiscontractjob.