Phase 4 · API & Batch Integration
Priority: Medium Status: Pending Depends on: Phase 3 (trained model)
Context
Section titled “Context”- FastAPI service:
kronos-service/main.py(port 8200) - Batch script:
scripts/kronos-batch-predict.py(hourly cron) - Gateway cache:
mcp-servers/gateway/src/handlers/ml-handlers.ts(4-tier lookup)
Overview
Section titled “Overview”Wire event conditioning into the production pipeline. API accepts optional events. Batch script fetches calendar and builds event vectors. Gateway serves cached predictions unchanged.
Requirements
Section titled “Requirements”Functional
Section titled “Functional”/predictendpoint accepts optionaleventsfield/predict-batchpasses events per symbol- Batch script queries
economic_calendarfor upcoming events - Event vectors aligned to OHLCV timestamps before calling Kronos
- Fallback: if events unavailable, use events=None (base model behavior)
Non-Functional
Section titled “Non-Functional”- No latency increase when events not provided
- Event vector construction <10ms per symbol
- Backward compatible — existing callers work without changes
Architecture
Section titled “Architecture”API Schema Change
Section titled “API Schema Change”class PredictRequest(BaseModel): symbol: str timeframe: str = "1d" pred_len: int = 120 temperature: float = 1.0 sample_count: int = 1 lookback: int = 512 as_of: Optional[str] = None events: Optional[List[List[float]]] = None # NEW: (T, 20) or (T+pred_len, 20) — see phase-01Batch Prediction Flow
Section titled “Batch Prediction Flow”1. Fetch OHLCV from Supabase (existing)2. Fetch economic_calendar for lookback + prediction window (NEW)3. Build event tensor via EventEncoder (NEW)4. Call Kronos predictor with events=events_tensor (MODIFIED)5. Compute envelope from samples (existing)6. Insert into ml_predictions with event_context field (MODIFIED)ml_predictions Schema Extension
Section titled “ml_predictions Schema Extension”ALTER TABLE ml_predictions ADD COLUMN IF NOT EXISTS event_context JSONB DEFAULT NULL;-- Example: {"events_active": ["fomc"], "surprise_directions": {"fomc": "hawkish"}}Implementation Steps
Section titled “Implementation Steps”- Modify
kronos-service/main.py— accept events in/predictand/predict-batch - Modify
kronos-service/main.py— load EventEncoder at startup alongside model - Modify
scripts/kronos-batch-predict.py— fetch calendar data before prediction - Modify
scripts/kronos-batch-predict.py— build event vectors per symbol - Modify
scripts/kronos-batch-predict.py— include event_context in ml_predictions insert - Add migration:
ml_predictions.event_contextcolumn - Test: batch prediction with and without events produces valid output
- Test: API
/predictwith events=None returns same result as before
Key Files
Section titled “Key Files”- Modify:
kronos-service/main.py— API endpoints - Modify:
scripts/kronos-batch-predict.py— batch pipeline - Create:
supabase/migrations/YYYYMMDD_ml_predictions_event_context.sql - Read:
kronos-service/db_loader.py(Supabase connection pattern)
Success Criteria
Section titled “Success Criteria”- API
/predictwith events returns predictions (no crash) - API
/predictwithout events returns identical results to current production - Batch script completes for all 25 symbols in <5 minutes
- event_context populated in ml_predictions for event-day predictions
- Gateway continues serving cached predictions without changes