Waves
Delivery waves of a station (aggregate DeliveryWave).
Introduction
REST contract of the last-mile-delivery bounded context of the retail network (logistics tier), derived from retail-network-docs model/model.yaml (contexts[last-mile-delivery].rest, .aggregates, .publishes, conventions.errors) and adjusted by the orchestrator's contracts-delta and Gap Register resolutions (G5, G11, G16, G17, G34). The context runs the network's delivery stations: it schedules delivery waves (induct cutoff, delivery window, stop capacity), inducts arrived packages into them, plans routes for delivery service partners (DSPs) and records delivery attempts. No endpoint carries an address or any other PII: packages are identified by tracking number and grouped into stops by the opaque `stopKey` that shipping-and-tracking derives at label time. No authentication, consistent with the rest of the network. Every state changing POST takes a required `Idempotency-Key` header (table idempotency_keys, 24 h): a missing key is 400 `idempotency-key-required`, a key that is not 1 to 128 printable ASCII characters is 400 `idempotency-key-invalid`, a key reused with another method, path or body is 409 `idempotency-key-reused`, and a repeated request replays the stored answer with the header `Idempotency-Replayed: true`. A 2xx is stored and a 5xx never; a refusal is stored (and replayed) only when it is final for the request, and a refusal that depends on state that can change is not stored, so its key is released and the retry executes again (the lists are on the `Idempotency-Key` parameter; ADR 0019). Every collection is paged with `limit` and `cursor` and answers `{items, nextCursor}`; an undeclared query parameter is 400 `unknown-query-parameter`. Errors are RFC 9457 `application/problem+json`; the `type` is a slug. The shared slugs are `not-found` (404), `illegal-transition` (409), `concurrent-update` (409) and `validation-failed` (422), plus one slug per model Err name (kebab-case without the `Err` prefix). Request bodies are decoded strictly (unknown fields, an empty body, trailing data and a body over 64 KiB are 422 `validation-failed`); a NUL character (U+0000) in the query string or a body is 422 `validation-failed`, in the path it is 404 `resource-not-found`. A path no route serves is 404 `resource-not-found`, a method a path does not serve is 405 `method-not-allowed` (problem+json), and an unmapped failure is an opaque 500 `internal-error` (never stored for replay). Every problem body carries `type`, `title`, `status`, `detail` and `instance`. The first eight operations of this file are the model's REST list; `getWaveStops`, `cancelWave`, `getRoute`, `deactivatePartner` and `declareWaveCost` are the additions of Gap Register G11, G17, G34 and roadmap F7 (ADR 0002, ADR 0011). The read-only analytics reports (tag reports, ADR 0012) are served by cmd/lm-reports on its own port (per-path server) from the separate analytical database.
Cancel a wave (CancelWave)
OPEN to CANCELLED, publishes DeliveryWaveCancelled (an integration event, Gap Register G5). Every package inducted in the wave is re-inducted onto the next eligible wave in the same transaction (Gap Register G17); a package with no eligible wave is deferred (PackageInductionDeferred).
Close induction of a wave (CloseInduction)
OPEN to INDUCTION_CLOSED. No event is published.
Contract a delivery service partner (ContractPartner)
Creates an ACTIVE partner. Van capacity must be greater than zero (ErrInvalidVanCapacity, 422 invalid-van-capacity) and stationNodeId a known DELIVERY_STATION (ErrUnknownStation, 422 unknown-station).
Deactivate a delivery service partner (DeactivatePartner)
ACTIVE to INACTIVE (Gap Register G34). An inactive partner can be given no new route; routes already planned are unaffected.
Declare the stop cost of a wave (DeclareCost)
Records the planner's declared marginal cost of ONE delivery stop on the wave (driver time plus mileage share), integer cents, single-currency study (roadmap feature F7, provider B; ADR 0011), and publishes WaveCostDeclared for order-sourcing's money-denominated AssignmentCost. The cost is per stop (the G11 stopKey grouping), never per package. 0 is a legal declared zero marginal cost; a wave with no declaration reports 0 and leaves a consumer's estimate incomplete. Allowed while the wave still runs (OPEN, INDUCTION_CLOSED or DISPATCHED, illegal-transition once CANCELLED or COMPLETED); a later declaration overwrites the earlier one and publishes again.
Declare the stop emission of a wave (DeclareEmission)
Records the planner's declared marginal greenhouse emission of ONE delivery stop on the wave (the stop's share of the route's driving), in grams of CO2 equivalent (roadmap feature C9; ADR 0016), and publishes WaveEmissionDeclared for order-sourcing's estimatedCo2eGrams. The emission is per stop (the G11 stopKey grouping), never per package. 0 is a legal declared zero marginal emission; a wave with no declaration reports 0 and leaves a consumer's estimate incomplete. Allowed while the wave still runs (OPEN, INDUCTION_CLOSED or DISPATCHED, illegal-transition once CANCELLED or COMPLETED); a later declaration overwrites the earlier one and publishes again. Ranking is untouched by this fact: carbon is recorded, not optimised, in this slice.
Dispatch a wave (DispatchWave)
INDUCTION_CLOSED to DISPATCHED. No event is published for the wave itself. Packages inducted in the wave that no route holds are re-inducted onto the next wave (Gap Register G17 mechanism). A wave with no route left to run completes at once (policy CompleteWaveOnLastRoute), so the response may show COMPLETED.
Delivery attempt outcomes and first-attempt success per day and station
Per UTC day and per delivery station (the station of the attempt's route, UNKNOWN while its PackageOutForDelivery is not projected): attempts, deliveredAttempts and failedAttempts are the DeliveryAttempted of the day by outcome; a success is DELIVERED or COLLECTED (both count in deliveredAttempts, ADR 0017), a failure FAILED; packagesAttempted are the packages whose FIRST attempt fell on the day, packagesWithFirstAttemptSuccess those of them delivered at it; firstAttemptSuccessRate = packagesWithFirstAttemptSuccess / packagesAttempted; meanAttemptsPerPackage = all the attempts of those packages (any day) / packagesAttempted; both null without a first attempt. returnsInitiated counts the PackageReturnInitiated of the day (its CloudEvents time) at the station of the package's latest attempt. Days without any of these are omitted.
Share of deliveries inside their day-of ETA window per day and station
Per UTC day of the DELIVERED attempt and per delivery station (the station of the attempt's route, UNKNOWN while its PackageOutForDelivery is not projected). deliveriesWithEta counts the delivered packages that had a StopEtaWindowAssigned window for the same route (ADR 0013); deliveriesInsideEta those delivered with etaStart <= attempt instant <= etaEnd (both bounds are inside), deliveriesBeforeEta before etaStart and deliveriesAfterEta after etaEnd; etaHitRate = deliveriesInsideEta / deliveriesWithEta, null when no delivery of the day and station had an ETA. Only the attempt that DELIVERED counts; a delivered package whose ETA was never projected (no ETA was raised, or the projection has not caught up) is not in the denominator. Days without a delivery are omitted. The windows are a deterministic spread of the route's stops over the wave window, so the first weeks of etaHitRate are a baseline, not a verdict.
How far the analytics projection is behind
asOf is the CloudEvents time of the newest event the projector applied; lagSeconds is now minus asOf, never negative. Both are null until the first event. One endpoint for every report: they all read the same projection.
Route efficiency per day and delivery station
Per UTC day of completion and per delivery station (stationNodeId), over the completed routes: a route counts as completed when every package it took out for delivery has an attempt (the precondition of CompleteRoute, which raises no event) and its RoutePlanned is projected; its day is the day of its last attempt. routesCompleted is their count; stopsPerRoute = stops / routesCompleted (stops = the routes' RoutePlanned stopCount); packagesPerStop = packages attempted / distinct stops attempted (every stop of a completed route was attempted, so the denominator is the stops); declaredCostPerStopCents = the stop-weighted mean of the waves' declared costPerStopCents over the routes whose wave declared a cost, null when none did; costPerPackageCents = declaredCostPerStopCents x the stops of those routes / their packages, null when undefined. Days without a completed route are omitted.
Get a route with its stops and attempts
Returns the route, its stops (with derived outcomes) and every recorded attempt.
List the stops of a wave (packages grouped by stopKey)
Returns the packages inducted into the wave grouped into stops by the opaque stopKey (Gap Register G11), ordered by the first induction of each stop. A package with no label data is its own stop. The planner builds PlanRoute from this list. No address is ever returned.
Wave stop-capacity utilisation per service date and station
Per serviceDate and delivery station of the scheduled waves: wavesScheduled, wavesCancelled (those with a DeliveryWaveCancelled), stopCapacity (the sum of the scheduled stopCapacity) and stopsInducted (the sum of each wave's highest assignedStops of DeliveryWaveCapacityChanged, 0 without one); utilisation = stopsInducted / stopCapacity, null when the capacity is 0. Cancelled waves stay in every sum. Dates without a wave are omitted.
List the waves of a station for a service date
Returns the waves of the station on the service date ordered by inductCutoffAt then waveId, one keyset page at a time. A station or date with no wave is an empty list.
Partners
Partners
Plan a DSP van route in a wave (PlanRoute)
Creates a PLANNED route and publishes RoutePlanned (analytics stream only). The wave must be INDUCTION_CLOSED (illegal-transition), the partner ACTIVE, of the wave's station and the stops at most the partner's van capacity (ErrRouteOverCapacity, 409 route-over-capacity). Every package must be inducted in the wave and not on another route, and the packages of one stop must share one stopKey (G34).
Record a delivery attempt (RecordAttempt)
Records DELIVERED, FAILED or COLLECTED for one package on an OUT_FOR_DELIVERY route and publishes DeliveryAttempted. COLLECTED is valid only for a package inducted into the wave as a PICKUP (ADR 0017, activation-gated; every induction is DELIVERY until then). Errors: ErrRouteNotStarted (409 route-not-started), ErrAlreadyDelivered (409 already-delivered). A FAILED attempt counts in the package's attempt history and re-inducts the package onto the next wave (policy RetryNextWave); the third failure publishes PackageReturnInitiated instead. The attempt that records the last outstanding package completes the route, and the last completed route completes the wave.
reports
reports
Routes
Routes
Schedule a delivery wave (ScheduleWave)
Creates an OPEN wave and publishes DeliveryWaveScheduled. Enforces inductCutoffAt < windowStart < windowEnd (ErrInvalidWaveTimes, 422 invalid-wave-times) and that stationNodeId is a DELIVERY_STATION known from node-topology (ErrUnknownStation, 422 unknown-station). Packages that arrived while no wave was eligible are inducted into the new wave in arrival order (policy InductDeferredOnSchedule).
Start a route (StartRoute)
PLANNED to OUT_FOR_DELIVERY; the route's wave must be DISPATCHED (illegal-transition otherwise). Publishes one PackageOutForDelivery per tracking number on the route.
Waves
Waves