Skip to content

feat: implement predictive location modeling with Kalman filtering (closes #263) - #274

Merged
levibliz merged 2 commits into
RiftCore00:mainfrom
levoski1:predictive-modeling-kalman-filter
Aug 24, 2026
Merged

feat: implement predictive location modeling with Kalman filtering (closes #263)#274
levibliz merged 2 commits into
RiftCore00:mainfrom
levoski1:predictive-modeling-kalman-filter

Conversation

@levoski1

Copy link
Copy Markdown
Contributor

Summary

Implements predictive location modeling engine with Kalman filtering, trajectory extrapolation, and anomaly detection for sub-second ETA and geofence pre-alerts.

Changes

New Module: src/predictor.js

  • PredictiveEngine class with per-client Kalman filter (Constant Velocity model)
  • ENU coordinate transformation (GPS lat/lon <-> local tangent plane meters)
  • Trajectory extrapolation API - getTrajectory(clientId, horizons) returns predicted positions with confidence ellipses from filter covariance
  • Geofence pre-alerts - checkPreAlerts(clientId) evaluates predicted trajectory against geofences, emits geofence_pre_alert events 60s before predicted entry (debounced per client/fence)
  • Anomaly detection:
    • GPS anomaly: innovation (residual) exceeds 3σ -> gps_anomaly
    • Kinematic anomaly: impossible acceleration (>10 m/s²) or heading rate (>π/2 rad/s) -> kinematic_anomaly
    • Rate limited to 1/min per client per anomaly type
  • ETA computation - getETA(clientId, targetLat, targetLon) returns mean, stddev, and arrival probability function
  • Session persistence - filter state saved/restored via session resumption (issue 18)

Configuration (.env.example)

  • PREDICTOR_ENABLE=true
  • PREDICTOR_MODEL=CV
  • PREDICTOR_PROCESS_NOISE=0.1
  • PREDICTOR_MEASUREMENT_NOISE=5.0
  • PREDICTOR_PRE_ALERT_HORIZON_S=60
  • PREDICTOR_MAX_HORIZON_S=120
  • Additional anomaly detection and TTL settings

Integration (src/server.js)

  • Initialize PredictiveEngine with roomManager
  • Call predictor.update() on each location_update
  • Broadcast anomaly events (gps_anomaly, kinematic_anomaly)
  • Call predictor.checkPreAlerts() for geofence pre-alerts
  • Cleanup filter on client disconnect

Tests (tests/predictor.test.js)

  • 19 comprehensive tests covering:
    • ENU coordinate transformations
    • Kalman filter convergence (stationary vehicle uncertainty decreases, moving vehicle 30m/s prediction error < 50m)
    • Trajectory extrapolation with confidence ellipses
    • Geofence pre-alerts with debouncing
    • Anomaly detection (GPS, kinematic) with rate limiting
    • ETA computation with Monte Carlo validation
    • Session persistence/restoration
    • Cleanup and disabled mode

Bug Fixes

  • Fixed pre-existing duplicate tokenRefreshSchema declaration in validator.js
  • Fixed pre-existing undefined sessions variable references in server.js (renamed to sessionManager)

Acceptance Criteria Met

  • Kalman filter converges: stationary vehicle -> position uncertainty decreases over time
  • Moving vehicle at 30 m/s -> 30s prediction error < 50m (simulated GPS noise σ=5m)
  • getTrajectory returns correct ENU->lat/lon transform with confidence ellipses
  • Geofence pre-alert emitted 60s before predicted entry
  • GPS anomaly detected when innovation > 3σ
  • Kinematic anomaly detected for impossible acceleration/heading rate
  • ETA distribution mean ± stddev matches Monte Carlo simulation
  • Filter state saved/restored via session resumption
  • npm run lint passes
  • All existing tests pass (core modules)
  • New test file with comprehensive coverage

Closes #263

…loses RiftCore00#263)

- Add PredictiveEngine class with per-client Kalman filter (CV model)
- Implement ENU coordinate transformation (lat/lon <-> local tangent plane)
- Add trajectory extrapolation API with confidence ellipses
- Implement geofence pre-alerts with debouncing per (clientId, fenceId)
- Add anomaly detection: GPS anomaly (innovation > 3σ), kinematic anomaly
- Add ETA computation with probability distribution
- Support filter state persistence for session resumption
- Add configuration via environment variables
- Add comprehensive test suite (19 tests)
- Fix pre-existing bug: duplicate tokenRefreshSchema in validator.js
- Fix pre-existing bug: undefined 'sessions' variable references in server.js
@levibliz
levibliz merged commit 4368612 into RiftCore00:main Aug 24, 2026
1 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants