Offline and idempotency
The key rule
Every command (POST/PUT) sends an Idempotency-Key header. A key names one operator action.
| Situation | Key |
|---|---|
| First submit of an action | new (UUID v4) |
| Automatic replay after a network failure / 500 / 502 / 503 / 504 / 429 | same |
| Replay after reconnect | same |
| Replay after a page reload | same (read back from the outbox) |
| Operator taps the same button again while the action is still pending, body unchanged | same |
| Same action, body changed (e.g. a different quantity) | new (the service rejects a key reused with a different body) |
| Same action again after it has a definitive answer | new |
| Lease renewal (one per timer tick) | new per tick; never replayed |
Order of events for a command, in CommandRunner.submit:
- build the outbox entry (
key,kind,method,path,body,dedupeId,meta), - write it to storage,
- send it (skipped when the browser reports no network),
- on any HTTP answer other than 429/500/502/503/504, remove it and report the answer; otherwise leave it for replay (until the per-entry attempt cap marks it stuck).
crypto.randomUUID only exists in secure contexts, so keys fall back to getRandomValues (an RF gun on plain http://<lan-host> is not a secure context).
Definitive vs retryable
- Definitive (entry removed, reported to the operator): every 2xx and every 4xx. Retrying a
409or422cannot change the answer. One 4xx must never be sent again for a stronger reason: yard-management's409 carrier-suspendedis answered after theREJECTEDvisit is recorded, so each new request would record another. On a replay it ends like any 4xx (entry removed, shown once as described below), and the gate screen takes away the form of that truck, never the form of another (Yard flow). - Retryable (entry kept, same key): network failure, timeout (15 s),
429,500,502,503,504. A500can be thrown after the command was applied and only the response was lost; every resend carries the entry's key, so replaying it cannot double-apply.
Who shows a replayed answer
An answer the operator waited for is shown by the screen that awaited it. An answer that arrives on a replay (reconnect, interval, reload) has no caller, and by the time it is known the entry has left the outbox. The rule: every refusal, and every accepted answer the operator must act on, is shown exactly once, in the words of the context that owns the command, whichever screen is in view.
Mechanism (CommandRunner in src/lib/commands.ts):
onSettled(listener): every listener hears every replayed answer and returnstruewhen it showed it to the operator.onUnhandledSettled(listener): called only when noonSettledlistener returnedtrue.
| Command of | Shown by, when in view | Otherwise |
|---|---|---|
| fulfillment-execution | StationProvider (always mounted) in the notice bar, shared table problemMessages.ts. It answers true for its own commands and ignores the others | never unhandled |
| inbound-receiving | ReceivingScreen when the entry's meta.receiptId is the receipt on screen | global notice, receivingMessages.ts, led by "Saved scan (receipt <id>):" |
| yard-management | the yard screen that owns the kind (gate: check-in, reject, check-out; hostler: claim, move completion; dock: door work, move completion) | global notice, yardMessages.ts, led by "Saved check-in (truck <PLATE>):" and the like |
The global notices are SettledNotices (src/app/SettledNotices.tsx, in Layout under the notice bar); the words come from describeSettled (src/app/settledFallback.ts), which picks the context by the entry's path and calls describeYardSettled or describeReceivingSettled. Each notice stays until it is dismissed, also across screens; a later one never replaces an earlier one, and an identical one is counted ("This happened 3 times.") instead of stacked. They are kept in memory, like every notice.
What the global notices say:
- Every refusal (any problem answer), in the owning context's words. A slug that context's table does not know falls back to the service title and "Try again", as it does on the screen.
- Accepted answers the operator must act on: a gate command (a driver is waiting: where each trailer goes after a check-in, a
REJECTEDvisit as a turn-away, a recorded rejection, a check-out), a hostler claim (a lease is running), and a receiving scan the service did not book or flagged with a finding. - Not announced: a scan that was simply received, a closed receipt, a completed move, a door-work step. The pending count in the offline banner going down is their signal.
Attempt cap and stuck entries
Each entry stops being replayed automatically after maxAttempts failed attempts (default 8) and is marked stuck: it stays in the outbox (terminal for auto-retry, same key kept) and stops blocking the commands queued behind it. StuckOutboxBanner (in Layout, next to the offline banner) shows the count and lets the operator Send again (same key, fresh round of attempts) or Discard. Re-tapping the action on its screen does the same as Send again.
The outbox
wfc.outbox.v1 in localStorage: FIFO, validated on load (a corrupt entry, or one whose path is not a same-origin /api/... path, is dropped). Replays run: at page load, on the browser's online event, every 5 s while anything non-stuck is pending, and on "Retry now". A replay stops at the first command that still cannot be delivered, so order is preserved; stuck entries are skipped instead of stopping the pass. A new command submitted behind older pending ones first tries to drain them (stuck ones excepted).
What is queued (kind): register, check-in, check-out, claim-next, pick-scan, complete, break-case, receive-scan, close-receipt. Not queued: renew-lease (persist: false; the route is POST .../lease-renewals). Reads (GET) are plain fetches, never queued.
Replays after a lost answer
Since the fleet REST guideline cutover (ADR 0001), fulfillment-execution and inbound-receiving key every POST and PATCH: the same key and body returns the stored answer with Idempotency-Replayed: true, a missing key is 400 idempotency-key-required, a malformed one 400 idempotency-key-invalid, and the same key with another request 409 idempotency-key-reused (a client bug, not retried). So a command whose answer was lost is applied once when the outbox replays it with its original key. This is read from the specs; it has not been exercised against a live deployment from this repo.
Defence in depth stays in the client, because the stub and older deployments may not dedupe:
- complete (
POST .../completions): a replay that was not deduplicated gets409 task-already-completed; the client treats that as success ("This task was already completed"). - claim-next (
POST .../task-claims): a replay that was not deduplicated can lease a second task. The client keeps the task in hand and tells the operator to ignore the extra one; its lease returns it to the pool by itself. - check-in (
POST .../check-ins): a replay that was not deduplicated can get409 station-occupied, shown as is.
Queued commands survive the route renames
A command queued by an older build holds the old path (for example .../tasks/t-1/complete), which now answers 404. Outbox rewrites such a path when it reads the store (src/lib/outboxMigration.ts): claim-next to task-claims, check-in/check-out to check-ins/check-outs, complete to completions, break-case to case-breaks, renew-lease to lease-renewals, seal-package to package-seals, /packages/{id}/slam to slam-runs, the receipt close to closures, and the appointment and ASN check-in/cancel to check-ins/cancellations. The entry keeps its Idempotency-Key, dedupeId, body and queue position, the rewritten store is saved back, and a path that is already new or not recognised is left alone (a queued write is never dropped by the migration).
Offline banner
OfflineBanner is always mounted (in Layout) and renders nothing while the network is believed to work and nothing is pending. It shows while the browser reports offline or the last request never reached the edge (Wi-Fi with no route), and stays visible, as "Sending saved actions", while saved commands are pending, with a "Retry now" button and a count.
Service worker
public/sw.js, hand-written. It precaches the app shell only (the file list is stamped at build time by build/precacheManifest.ts).
| Request | Handling |
|---|---|
| non-GET (every command) | not intercepted |
anything under /api/ | not intercepted: never stored, never served from cache |
| cross-origin | not intercepted |
| GET of a precached shell file | cache first |
| navigation | network first, cached index.html when offline |
Cache names are versioned by a content hash of the shell; old ones are deleted on activate. nginx.conf serves sw.js, index.html and the manifest with no-cache.
Tests: src/pwa/sw.test.ts (loads public/sw.js into a sandbox and asserts the table above), src/lib/lib.test.ts (key reuse, persistence, replay order), src/lib/settledHandled.test.ts and src/app/SettledNotices.test.tsx (who shows a replayed answer), src/lib/outboxRetry.test.ts (retryable 500/abort, attempt cap, stuck entries), src/components/StuckOutboxBanner.test.tsx, and the offline cases in src/features/**/*.test.tsx and scripts/e2e-station-flow.mjs.
inbound-receiving (receiving dock)
inbound-receiving's POST /receipts/{id}/scans requires the key and replays the stored answer for a repeated key (and 409 idempotency-key-reused for a changed body), per its OpenAPI. Each physical scan is its own action (own key); a saved or lost-answer scan is replayed with its key, also after a reload. See Receiving flow.