What actually runs
Runtime loops
Each loop is implemented, annotated on the source file with @docs, and callable over HTTP.
Click a route to hit the live server. Open the file path in the repo — the comment at the top of that file is the same story in engineer voice.
Loop · Every 3–5s per online driver
GPS ingest
Hot path writes presence only. Postgres is sampled every 30 seconds so 100k drivers do not melt the database.
- Driver app or socket sends lat/lng.
- Cell ids computed with H3 resolution 9 (block) and 8 (neighborhood).
- Previous cell membership is removed; new cell set is updated.
- A 30s sample flag decides whether to park a durable row in the outbox.
backend/modules/geo/application/ingest.ts
LocationIngest.ping — Redis/memory upsert + downsample key
backend/modules/geo/infrastructure/presence-store.ts
PresenceStore.upsert — H3 r8/r9 membership sets, 45s TTL
backend/realtime/socket-server.ts
driver:ping — Same ingest over WebSocket
Loop · On search / dispatch tick
Proximity lookup
k-ring over H3 cells, then haversine rank. Mapbox/Google/OSRM only if you add keys.
- Convert the query point to H3.
- Load driver ids from every cell in the radius ring.
- Filter by vehicle class and available status.
- Attach distance and ETA (haversine in demo).
backend/modules/geo/domain/h3.ts
cellsInRadius — gridDisk for radius
backend/modules/geo/application/routing.ts
estimateEta — Provider chain, haversine last
Loop · Worker every 2s, or POST /dispatch/match
Batch matching
Hungarian assignment over open searching trips vs available drivers in nearby cells.
- Group open trips by H3 r8.
- Load available drivers in an 8 km ring.
- Build a cost matrix; dummy cells are padded so extras stay unmatched.
- Transition winners requested/searching → matched with an idempotency key.
backend/modules/dispatch/domain/hungarian.ts
hungarian — Min-cost assignment
backend/modules/dispatch/domain/matching.ts
batchMatch — Distance, acceptance, class, traffic
backend/modules/dispatch/domain/surge.ts
surgeMultiplier — Per-cell EMA surge
backend/workers/index.ts
matchOpenTrips loop — Poll + AMQP trip.requested
Loop · On each state change
Trip lifecycle
Strict FSM with actor checks. Idempotency-Key required on HTTP. Same-state retries are no-ops.
- requested → searching (system)
- searching → matched (system/admin)
- matched → driver_en_route → driver_arrived → in_progress → completed
- Cancel is allowed until in_progress; after that only safety/admin.
backend/modules/trips/domain/states.ts
assertTransition — Legal edges + actors
backend/modules/trips/application/lifecycle.ts
TripLifecycle.transition — Version bump, events, push
app/api/v1/trips/[id]/transitions/route.ts
POST — Header Idempotency-Key
Loop · Booking hold, capture at completion, payout on demand
Escrow & ledger
Double-entry cents. Demo writes the journal without Stripe. Production refuses if GO_LIVE blockers remain.
- Authorize hold (stripe_pending ↔ platform_clearing).
- Capture splits fees, insurance pool, host payable, optional driver share.
- Refund reverses clearing to the processor.
- Cash-out moves payable to stripe_pending (Connect transfer in production).
backend/modules/payments/domain/ledger.ts
holdAtBooking / captureAndSplit — Zero-sum journals
backend/modules/payments/application/escrow.ts
EscrowService — Stripe when keys exist
backend/shared/golive/status.ts
assertProductionMoney — Go-live gate
Loop · On driver_verified → policy_bound
PVSP per-trip binder
Named insured = driver. Additional insured = host. Lula/Tint/ABI or simulated. Marketplace is never the policyholder.
- Lease opens requested.
- Identity + MVR mark driver_verified.
- Binder API: $45k/$90k/$15k, VIN, term.
- Eight-angle photos, then key-unlock timestamp.
- Post-trip photos terminate the binder. Claims POST to the carrier.
backend/modules/pvsp/domain/states.ts
assertPvspTransition — CRT FSM
backend/modules/pvsp/application/binder.ts
InsuranceBindingService.bind — InsurTech or sim_
backend/modules/pvsp/application/agreements.ts
CrtAgreementService.sign — Hold harmless hash
Loop · Arrival and return
Keyless access & inspection
Four timestamped photos, then BLE/cellular unlock. Webhook is optional.
- Driver arrives (FSM).
- Pre-trip inspection with four photos + odometer.
- Unlock command; simulated provider unless IOT_UNLOCK_WEBHOOK is set.
- Post-trip inspection closes the evidence pack.
backend/modules/fleet/application/inspection.ts
InspectionService.submit — ≥4 photos
backend/modules/fleet/application/access.ts
VehicleAccess.unlock — Arrival-gated
backend/modules/fleet/application/insurance.ts
quoteTripInsurance — Period 1/2/3
Loop · On trigger / speed anomaly
SOS & telemetry
Share link, dispatch push, live lat/lng. Speed > 40 m/s is flagged.
- Authenticated actor posts tripId + coordinates.
- Share URL minted on the public site.
- Push to safety-dispatch (log channel in demo).
- Socket rooms trip:{id} and safety-dispatch receive the event.
backend/modules/safety/application/sos.ts
SafetyService.trigger — Event + notify
backend/realtime/socket-server.ts
sos:trigger — Broadcast to trip room
Loop · Vendor webhook / on-demand GET
Telematics hook-in
One ping shape. Demo is neighborhood-only simulated. Flip TELEMATICS_PROVIDER when hardware is chosen.
- Vendor POSTs a location payload (or demo writes a simulated ping).
- Adapter normalizes to neighborhood + odometer + ignition.
- Hosts read /owners/monitor without a street pin.
- Desk reads /admin/telematics and may see coordinates.
lib/telematics/vendors.ts
TELEMATICS_VENDORS — API catalog
lib/telematics/adapter.ts
normalizeVendorPayload — lat/lng/odometer/ignition
app/api/v1/fleet/telematics/route.ts
POST — Webhook + desk secret