Skip to main content

Routes

DSP van routes and delivery attempts (aggregate Route).

📄️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.

📄️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.

📄️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.

📄️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.

📄️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.

📄️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.