Sequence Diagrams
Two parts. Part A has the four scenarios the pack must show, each as one story
across several use cases: inbound adapter → use cases → aggregates → outbox → relay →
Kafka → neighbour. Part B has one diagram per significant command use case of
Use Cases, in the hexagonal layout of the built service:
inbound adapter → use case → aggregate → repository → outbox (same transaction) → relay →
Kafka. The relay and Kafka share one lifeline ("relay to Kafka") to keep each diagram at
six participants or fewer. alt blocks show error branches, optimistic-concurrency
version checks and idempotency (REST Idempotency-Key, Kafka inbox on the CloudEvents
id). Error names are the model's; HTTP statuses of domain errors follow decision D-10: 409 conflicts, 422 value rules (see
Use Cases).
Route parameters are written without braces. The relay publishes integration events to
warehouse.yard-management.events and, as a twin, to warehouse.yard-management.analytics; analytics-only events
go to the analytics topic alone. Both are keyed by the aggregate root id.
Part A: the four scenarios
A1. Live unload
An inbound carrier with a booked door: check-in, the visiting driver's move to the door, docking, door work closed by the receipt, check-out.
Source: internal/domain/yardvisit/visit.go, internal/domain/trailer/trailer.go, internal/domain/trailer/work.go, internal/domain/yardmove/yardmove.go, internal/application/usecases/targeting.go, internal/application/usecases/gate_checkin.go, internal/application/usecases/consumers.go, internal/domain/trailer/events.go, internal/adapters/outbound/kafka/encoder.go.
Omitted: the failure branches (Part B), the analytics events (VisitCheckedOut,
DoorWorkStarted, YardMove*; DoorWorkCompleted and TrailerDeparted also go to the integration topic since
2026-11-09), and inbound-receiving's own receipt flow. Depart from the door under CheckOutVisit raises
TrailerUndocked and TrailerDeparted (decisions Y-2, Y-6). The dock operator or gate clerk completes the
first move through the completion route (decision Y-3).
A2. Drop, then a later door call
A carrier drops a trailer whose door is busy; the trailer waits in a spot; when the door frees, a hostler moves it in; after unloading it is returned to a spot.
Source: internal/domain/trailer/trailer.go, internal/domain/trailer/work.go, internal/domain/yardmove/yardmove.go, internal/application/usecases/targeting.go, internal/application/usecases/doors.go, internal/domain/trailer/events.go, internal/adapters/outbound/kafka/encoder.go.
Omitted: the door work commands (A1), YardMoveRequested, YardMoveClaimed,
YardMoveCompleted and TrailerParked (analytics only), the idempotency and inbox rows.
CallToDoor reacts to the own TrailerUndocked; this page reads that as a consumer of
the own topic, as dock-scheduling consumes it. StartMoving from DOCKED (the claim of a FROM_DOOR move)
raises TrailerUndocked (decision Y-6); the trailer carries its appointment through appointment_id (decision X3);
the first move to a plain spot has the reason TO_SPOT (decision Y-4).
A3. Gate rejection
A walk-in at a site that requires appointments; then a seal mismatch under both policies.
Source: internal/domain/yardvisit/visit.go, internal/domain/yardpolicy/yardpolicy.go, internal/application/usecases/gate_checkin.go, internal/domain/trailer/events.go, internal/adapters/outbound/kafka/encoder.go, internal/domain/shared/shared.go.
Omitted: ErrTrailerAlreadyOnSite (reason TRAILER_ALREADY_ON_SITE), UNKNOWN_APPOINTMENT
and SITE_REQUEST rejections; the detail that dock-scheduling now receives VisitRejected and cancels the
appointment with TURNED_AWAY (decision X2). A refused CheckInVisit creates nothing and RejectAtGate records the
rejection (decision Y-5). The seal number arrives with the trailer at check-in, and a rejected visit's
SealExceptionRaised has a null trailer_id (decisions Y-5, Y-13).
A4. A hostler claims a move, the lease expires, another hostler completes it
Source: internal/domain/trailer/trailer.go, internal/domain/trailer/work.go, internal/domain/yardmove/yardmove.go, internal/application/usecases/moves.go, internal/adapters/inbound/kafka/consumer.go, apis/openapi.yaml, internal/adapters/inbound/http/server.go.
Omitted: the Idempotency-Key rows; ErrHostlerBusy when a hostler already holds a
CLAIMED move; the late completion after expiry when nobody has re-claimed the move,
which is ErrIllegalTransition (the move is REQUESTED). The expiry returns the move to
REQUESTED and ReturnToOrigin brings the trailer back (decision Y-1), raises YardMoveLeaseExpired (decision
Y-10) and has no lease renewal. A refusal at ArriveAt still happens when the hostler is already at the
location; the destination was also validated when the move was requested (decision Y-8).
Part B: one diagram per significant command
Commands not drawn follow a pattern shown here: RejectAtGate (A3); RecordArrival,
DirectOnCheckIn inside B1; StartMoving inside B4; ArriveAt inside B5; Depart
inside B2; RecordSeal, BreakSeal, RecordLoadState, ReleaseHold as B9 (load, check
state, mutate, version check, optional outbox row); OpenYardCheck, RecordObservation
and DeclareYardPolicy as B11 without the outbox.
B1. CheckInVisit
Source: internal/domain/yardvisit/visit.go, internal/domain/trailer/trailer.go, internal/domain/trailer/work.go, internal/application/usecases/targeting.go, internal/application/usecases/gate_checkin.go, migrations/0001_yard-management.up.sql, migrations/0002_idempotency_keys.up.sql.
Omitted: the REJECT seal outcome (A3); the choice of door, holding spot or parking spot
(A2); each domain error is answered 409 or 422 (decision D-10). The first move's events
(YardMoveRequested, YardMoveClaimed) go to the analytics topic; the move is created CLAIMED, so the
service raises both (decision Y-1).
B2. CheckOutVisit
Source: internal/domain/yardvisit/visit.go, internal/domain/trailer/trailer.go, internal/domain/trailer/work.go, internal/domain/trailer/events.go, internal/adapters/outbound/kafka/encoder.go.
Omitted: the idempotency-key check (as in B1). Depart is called from here for each trailer taken out
(decision Y-6). For a live visit the trailer leaves from the door: released means no open move and either
PARKED or DOCKED with door work DONE (decision Y-2). A DOCKED trailer with door work not DONE is already
refused as ErrTrailerNotReleased, so the ErrDoorWorkOpen of Depart drawn above is not reached through this route.
B3. RequestMove
Source: internal/domain/yardmove/yardmove.go, internal/application/usecases/doors.go.
Omitted: the idempotency-key check; the Kafka inbox row when a policy is the caller. The
destination is validated at the request (known slot, closure, flow, hold, ErrDestinationReserved,
ErrTrailerHasOpenMove) and again at arrival (decision Y-8).
B4. ClaimNextMove
Source: internal/domain/trailer/trailer.go, internal/domain/trailer/work.go, internal/domain/yardmove/yardmove.go.
Omitted: the idempotency-key check. The claim order is the lowest band, then due_by, then request time
(decision Y-11); with nothing to claim the answer is 404 no-claimable-move; a claim from a door raises
TrailerUndocked (decision Y-6).
B5. CompleteMove
Source: internal/domain/trailer/trailer.go, internal/domain/trailer/work.go, internal/domain/yardmove/yardmove.go, internal/application/views/views.go.
Omitted: the idempotency-key check. For a VISITING_DRIVER move there is no lease; the dock
operator or gate clerk completes it through the same route (decision Y-3). The partial unique index on active
occupancy is the race-safe backstop for ErrLocationOccupied.
B6. ExpireLease
Source: internal/domain/yardmove/yardmove.go, internal/application/usecases/moves.go.
Omitted: the outbox row for YardMoveLeaseExpired (analytics only, decision Y-10) and the
ReturnToOrigin step on the trailer (decision Y-1), both in the same unit of work as the move. Each
move is its own transaction, so one conflict does not stop the sweep.
B7. CancelMove
Source: internal/domain/yardmove/yardmove.go, internal/application/usecases/consumers.go.
Omitted: the idempotency check and the inbox row. A cancelled CLAIMED hostler move runs ReturnToOrigin in the same
unit of work, so the trailer goes back where it was (decision Y-1); the policy finds the move through the
appointment_id that the move carries (decisions X3, Y-14).
B8. StartDoorWork and CompleteDoorWork
Source: internal/domain/trailer/trailer.go, internal/domain/trailer/work.go, internal/application/usecases/doors.go, internal/application/usecases/consumers.go, internal/domain/trailer/events.go, internal/adapters/outbound/kafka/encoder.go.
Omitted: the AutoCompleteUnload caller (a ReceiptClosed consumer: inbox row instead of
the idempotency key, joining ReceiptAtDoorView to the trailer through receipt_id, decision Y-7) and the
follow-up RequestMove of ClearDoorAfterWork for a DROP trailer (B3). AutoCompleteUnload completes from
WAITING or IN_PROGRESS; an operator's completion needs IN_PROGRESS (assumption A11).
B9. PlaceHold
Source: internal/domain/trailer/trailer.go, internal/domain/trailer/work.go, internal/domain/shared/shared.go.
Omitted: the idempotency-key check. A hold on an already DOCKED trailer is allowed and door work waits for
the release (decision Y-13); a held trailer is refused docking at ArriveAt (B5).
B10. DwellSweep
Source: internal/domain/yardpolicy/yardpolicy.go, internal/application/usecases/checks_policy.go, internal/domain/trailer/events.go, internal/adapters/outbound/kafka/encoder.go.
Omitted: a kind with no threshold (skipped). The marker that makes the sweep "once per
trailer and threshold crossing" is dwell_alerted_at on the trailer (decision Y-13); the sweep has no aggregate
command of its own.
B11. CloseYardCheck
Source: internal/domain/yardcheck/yardcheck.go, internal/domain/trailer/events.go, internal/adapters/outbound/kafka/encoder.go.
Omitted: the idempotency-key check. OpenYardCheck and RecordObservation are the same
shape without the outbox row (ErrYardCheckOpen and ErrLocationAlreadyObserved as the
guards). The discrepancy rules are decision Y-11 (a location not observed is MISSING when the model records a trailer
there) and nothing applies a discrepancy to a trailer: closing only reports; exception-management opens one case
per discrepancy.