Skip to main content

Use cases

Synced from task-interleaving

This page is a copy of docs/docs/ddd/use-cases.md on develop, derived from that repository's code. Edit it there, then re-sync.

The application layer is internal/application/usecases. Every write runs in one ports.UnitOfWork: load, apply the aggregate command, save guarded by the version that was loaded, and hand the raised facts to the outbox (ports.EventSink), so the aggregate, its events and, for consumers, the claim of the CloudEvents id commit or roll back together. Reads use the event-fed local copies (ports.TaskPool, ports.LocationBook); nothing here calls a sibling context.

Hints are issued by event consumers, never by a REST call, and nothing here assigns work to a worker. There are 14 use cases: 6 REST queries and commands, 7 Kafka-driven consumer effects and 1 scheduler.

Use cases​

Triggers: REST routes are those of apis/openapi.yaml; Kafka types are the full CloudEvents type of the producer (com.warehouse.wes.fulfillment-execution.task.* or com.warehouse.wms.facility-layout.*); the scheduler is the hint-expiry worker.

#Use case (file)TriggerInputsInvariants checkedEvents raised
1DeclarePolicy (declare_policy.go)REST PUT /sites/{siteId}/policysite id; maxDetourM, maxCandidates, hintTtlSeconds, eligibleTaskTypes, optional verticalWeight (default 1.0); optional If-Match versionsite id token (1 to 64 runes, no whitespace, control characters or /); maxDetourM finite and above 0; maxCandidates 1 to 5; TTL above 0 (and at most 2147483647 s over REST); non-empty eligible set of PICK, PACK, REBIN, SLAM, RELOCATE, REPLENISH; weight finite and at least 0. First declaration: no If-Match (version 1). Replacement: If-Match must equal the current version (ErrVersionMismatch, 412); a lost write race is ErrConcurrentModification (409).InterleavingPolicyDeclared (analytics topic only)
2GetPolicy (get_policy.go)REST GET /sites/{siteId}/policysite idvalid site id; no row = ErrPolicyNotFound (404, the site is inert)none
3ListHints (list_hints.go)REST GET /hintsoptional stationId, siteId, state, cursor, limitlimit 1 to 500 (default 100); state one of Active, Consumed, Expired, Superseded; cursor must be one this API issued; newest first, keyset pagednone
4GetHint (get_hint.go)REST GET /hints/{hintId}hint idid starts with hint-, no whitespace, control characters or /; unknown id is ErrHintNotFound (404)none
5GetStation (get_station.go)REST GET /stations/{stationId}station idvalid station id; a station with no recorded completion is ErrStationNotFound (404)none
6Recommend (recommend.go)REST POST /recommendations (needs Idempotency-Key in Postgres mode)stationId; optional fromLocationthe origin is fromLocation, else the station's recorded position (ErrStationPositionUnknown, 422); the origin has known geometry and a site (ErrOriginUnknown, 422); the site has a policy (ErrPolicyNotDeclared, 422). Read-only: issues nothing, publishes nothing.none
7ApplyTaskAvailable (apply_task_available.go)Kafka ...task.TaskAvailabletask_id, task_type, site_id, from_location, to_location, required_capabilities, priority_bandtask_id, task_type, from_location not blank; site_id a valid site id (else ErrInvalidEvent, dead-lettered at once). A task already COMPLETED never re-enters the pool (stale). Otherwise upserted as AVAILABLE; a re-entry forgets the old claimer.none
8ApplyTaskClaimed (apply_task_claimed.go)Kafka ...task.TaskClaimedtask_id, task_type, station_id, claimed_at (falls back to the CloudEvents time)task_id not blank and a valid station id. COMPLETED tasks are stale. The task is marked CLAIMED by the station (removed from the candidates; a never-seen task is recorded minimally). If the station has a trip and StationTrip.RecordClaim accepts the claim (Active hint, inside its window, task is a candidate) the hint is consumed.InterleavingHintConsumed (analytics) when a hint is consumed
9ApplyTaskCompleted (apply_task_completed.go)Kafka ...task.TaskCompletedtask_id, station_id (else the claimer in the pool copy), CloudEvents timetask_id not blank. An already COMPLETED task is stale. The task is marked COMPLETED. If the completer, the task's to_location or site_id is unknown the outcome is ignored. Otherwise the station's position moves to the end of the task (RecordCompletion: older or equal instants are stale), and, if the site has a policy, the origin has geometry and at least one candidate qualifies, a hint is issued; any earlier Active hint is Superseded.InterleavingHintIssued (events and analytics topics); no event when no hint is issued
10ApplyZone (apply_layout.go)Kafka ...zone.ZoneRegisteredzoneId, siteCode, zoneCodezoneId and siteCode not blank; upsertnone
11ApplyLocationRegisteredKafka ...locationslot.LocationSlotRegisteredlocationCode, zoneIdboth not blank; upsert (a late registration cannot undo a decommission)none
12ApplyLocationDecommissionedKafka ...locationslot.LocationSlotDecommissionedlocationCodenot blank; the location is retired for good, its row and geometry stay for originsnone
13ApplyGeometryUpdatedKafka ...locationslot.LocationGeometryUpdatedlocationCode, xM, yM, zMcode not blank; all three coordinates present (consumer) and finite (use case); stored whether or not the location was registered yetnone
14ExpireHints (expire_hints.go)scheduler: inbound/expiry worker every HINT_EXPIRY_INTERVAL (default 5 s; the first sweep runs at start-up)the clockup to 200 stations with an Active hint at or past expires_at, each in its own unit of work; only an Active hint expires; a lost race is skipped until the next sweepInterleavingHintExpired (analytics)

The consumer use cases (7 to 13) all go through Intake: the CloudEvents id is claimed in processed_events first (consumer name task-interleaving-fulfillment or task-interleaving-layout) and a repeat is the outcome duplicate. Outcomes: applied, duplicate, stale, ignored. A blank id is ErrInvalidEvent.

Hint ids are minted by ports.IDGenerator (memory.RandomIDs), the clock by ports.Clock; both are injected so the BDD suite can fake time.

Domain operations behind them​

The use cases call these operations; the invariants live here, not in the adapters.

Domain operationRule
policy.New(site, Settings) / Policy.WithChanges(Settings)Settings validated as in use case 1; version 1 on New, +1 per WithChanges, which replaces all settings.
policy.Restore(site, Settings, version)Loads a persisted row; every invariant plus version >= 1, so a corrupt row can never become a live aggregate.
stationtrip.New(StationID)Token rules on the station id.
StationTrip.RecordCompletion(task, at, Position)Task id not blank, instant not zero, site a valid token, location not blank. A completion not strictly newer than the recorded one returns false, changes nothing and raises nothing.
recommend.Recommend(Input)Pure ranking: model not nil (ErrNoModel), origin geometry known (ErrOriginUnknown). A candidate is of the station's site, of an eligible type, not the completed task, with known geometry and a detour at most MaxDetourM (equal qualifies). Ranked by detour then task id, distinct, cut to MaxCandidates, numbered from 1.
StationTrip.IssueHint(id, candidates, Policy, now)Position recorded (ErrNoPosition), valid hint id, now not zero, policy of the station's site (ErrSiteMismatch), 1 to MaxCandidates distinct eligible candidates within the detour limit ranked 1..n. Any Active hint becomes Superseded; the new hint is Active and expires at now + HintTTL.
StationTrip.RecordClaim(task, now)Consumes only with an Active hint, now not before IssuedAt, now strictly before ExpiresAt, and the task one of its candidates; otherwise false, no change.
StationTrip.Expire(now)Only an Active hint, only at or after ExpiresAt; Consumed, Expired and Superseded are terminal.
travel.Rectilinear.DistanceUnknown when either place has no point or a non-finite coordinate, never 0; else |dx| + |dy| + verticalWeight * |dz|, rounded to 0.1 m.

Errors and the problem types they become​

The HTTP adapter maps each error in internal/adapters/inbound/http/errors.go; the full table with remedies is in Troubleshooting.

ErrorProblem slug (status)
policy.ErrInvalidSiteIDinvalid-site-id (400)
ErrInvalidMaxDetour, ErrInvalidCandidates, ErrInvalidHintTTL, ErrInvalidWeightinvalid-policy (400)
policy.ErrInvalidTaskTypesinvalid-task-type (400)
stationtrip.ErrInvalidStationID / ErrInvalidHintIDinvalid-station-id / invalid-hint-id (400)
usecases.ErrInvalidLimit / ErrInvalidCursor / ErrInvalidStateinvalid-limit / invalid-cursor / invalid-state (400)
repository.ErrPolicyNotFound / ErrHintNotFound / ErrStationNotFoundpolicy-not-found / hint-not-found / station-not-found (404)
repository.ErrConcurrentModificationconcurrent-modification (409)
usecases.ErrVersionMismatchversion-mismatch (412)
usecases.ErrPolicyNotDeclared, recommend.ErrOriginUnknown, usecases.ErrStationPositionUnknownpolicy-not-declared, origin-unknown, station-position-unknown (422)
anything else (including recommend.ErrNoModel)internal-error (500), real error only in the log

The stationtrip errors ErrNoCandidates, ErrTooManyCandidates, ErrDuplicateCandidate, ErrCandidateNotEligible, ErrCandidateDetour, ErrCandidateRanking, ErrSiteMismatch, ErrInvalidTask, ErrInvalidTime and ErrInvalidPosition guard the consumer path; no REST route can raise them. ErrInvalidEvent (consumer path) is the "can never be applied" signal that sends a message to the dead-letter topic.

Where the code differs from the ADR text​

StationTrip.RecordClaim also refuses a claim whose instant is before the hint's IssuedAt (trip.go, now.Before(s.hint.IssuedAt)). ADR 0002 invariant 5 states only the expiry bound. The code is stricter; the test TestRecordClaimTimeWindow pins it ("one nanosecond before issue").