0034. Site-scoped staffing gap (optional canonical siteCode)
Status
Accepted. Resolves the open question left by ADR 0033 ("the staffing gap still counts active assignments across every building") and refines ADR 0029 and ADR 0003 without editing them; where this record and one of those disagree, this record wins.
Context
CountActiveByPath counted every ACTIVE LaborAssignment on a path across the
whole database, because neither labor_assignment nor associate_shift
carried a building, shift or site. The ShiftPlan is keyed by
buildingId + shiftId, so the gap compared one building's planned heads with
every building's active heads. That is wrong for a multi-site network, and
the rest of the fleet is converging on a canonical Site:
facility-layout owns the Site aggregate (its SiteCode), warehouse-planning
requires site_id on capacity plans (the same facility-layout Site code), and
network flows are site-scoped.
Decision taken on 2026-10-06 (architect, final): site-scope the staffing gap, additively and backward compatibly.
Decision
- Identifier. The canonical Site code is the facility-layout Site code
(
WH1,SIM1, …; facility-layout publishes it assite_codeonSiteCapabilityChanged, warehouse-planning carries it assite_id). It is an optionalsiteCodeon the REST/MCP surface (camelCase, like every other field there) andsite_codein the snake_case analytics event payload. This context treats it as an opaque string, accepted as given: only surrounding whitespace is trimmed, a blank value means "no site", and there is no live lookup or validation against facility-layout (this context keeps its data local and declarative). - Write side.
POST /associates/{id}/shift-startsaccepts an optionalsiteCode, persisted on theAssociateShiftaggregate asassociate_shift.site_code— a nullable column added by the additive migration000005(plus a partial index on the non-NULL values). A restart (the documented upsert) replaces the whole roster entry, site included, just like certifications; a restart withoutsiteCodetherefore makes the associate site-unknown again. - Read side.
GET /paths/{pathId}/staffing-gap,GET /buildings/{buildingId}/shifts/{shiftId}/staffing-gapand the MCP toolget_staffing_gapaccept an optionalsiteCode. When it is non-empty,activeHeadscounts only assignments whose associate has an active (not-ended) shift at that site (labor_assignmentjoined toassociate_shiftonassociate_id,site_code = :siteCode AND ended = FALSE). When it is absent or blank the query behaves exactly as before (fleet-wide, via the unchangedCountActiveByPath). The response echoessiteCodeonly when the request was scoped. - Legacy rows.
NULLsite never matches anysiteCode: associates started before the migration (or without a site) count only in unscoped queries. An unknown code simply counts 0. - Event.
PathUnderstaffed(analytics stream,v1) gains an optionalsite_code, omitted when the gap was computed unscoped (additive, no.v2,dataschemaunchanged). Consumers MUST treat absence as "unscoped" (every site together), never as a particular site. The Kafka key and CloudEventssubjectstay the path id.PathUnderstaffedis not on the integration topic. - Unchanged. Idempotency-Key behaviour (ADR 0027) — the two guarded POSTs
do not carry a site;
ShiftPlanstays keyed bybuildingId + shiftId(mapping a building to a site is not decided here);LaborAssignmentgains no site (the associate's shift is the single place the site lives);AssociateShiftStartedis not extended.
Consequences
- A multi-site deployment gets a correct per-site gap by starting shifts with
siteCodeand querying with it. Unscoped callers (warehouse-ops-agent via MCP, e2e-tests via REST, the console) are untouched: every new request and response field is optional. - The site is only as trustworthy as what the caller sends: a typo'd code makes an associate invisible to the intended site's gap. Accepted trade-off for not coupling this context to facility-layout (ADR 0002 spirit).
- A site-scoped gap is compared with the building's plan for the shift, so
callers must pick a consistent
buildingId/siteCodepair themselves. - The additive column can be dropped by the
downmigration only; no forward migration drops anything. The unuseddomain_eventtable is kept and documented as legacy (additive migrations only). - Ecosystem check (sibling
origin/developgreps): nothing consumesPathUnderstaffedoutside this repo's own analytics projector (which ignores unknown fields; covered by a test); warehouse-ops-agent calls MCPget_staffing_gapwithbuildingId/shiftId/pathIdand e2e-tests callstart-shift/staffing-gapwithout a site — all remain valid.