Intelligence, Advanced Charting & Trading Platform
feature/chartnova-multimarket-chart-ws-foundation
diagnostics/chartnova-blank-screen-2026-10-15
diagnostics/chartnova-stage2-v27-preview-2026-10-15
docs/chartnova-stage1-reconcile-manifest-2026-10-15
feature/chartnova-ios-widget
feature/chartnova-light-catalog-lazy-series-2026-10-15
feature/chartnova-market-terminal
feature/chartnova-multimarket-chart-ws-foundation
feature/chartnova-neon-direct-history
feature/chartnova-neon-history
feature/paper-trading-risk-controls-2026-10-14
fix/alpha-vantage-live-payload
fix/chartnova-progressive-loading-2026-10-15
fix/live-proof-runner-cwd-2026-10-14
foundation/phase-0
foundation/postgres-core
main
ops/chartnova-emergency-rollback-2026-10-15
phase-1/candle-builder
phase-1/data-proof-artifact
phase-1/data-proof-metrics
phase-1/data-proof-report
phase-1/external-gates-state
phase-1/gold-api-fallback
phase-1/ingestion-pipeline
phase-1/live-proof-runner
phase-1/market-calendar
phase-1/market-domain
phase-1/neon-live-proof
phase-1/persistence-contract
phase-1/postgres-ci
phase-1/provider-adapters
phase-1/provider-recovery
phase-1/quality-propagation
phase-1/spike-detection
phase-1/xau-provider-adapter
risk/chartnova-alert-runtime-atomic-2026-10-22
risk/chartnova-atomic-outbox-2026-10-22
risk/chartnova-seven-track-safety-2026-10-21
safety/p0-triage-2026-10-13
| 1 | const VALID_STATUS = new Set(['HEALTHY', 'DEGRADED', 'DOWN']); |
| 2 | |
| 3 | function iso(value, name) { |
| 4 | const d = new Date(value); |
| 5 | if (Number.isNaN(d.valueOf())) throw new TypeError(`${name} must be a valid date/time`); |
| 6 | return d.toISOString(); |
| 7 | } |
| 8 | |
| 9 | function latestTimestamp(previous) { |
| 10 | const values = [previous?.lastSuccessAt, previous?.lastFailureAt, previous?.recoveredAt] |
| 11 | .filter(Boolean) |
| 12 | .map(v => new Date(v).valueOf()) |
| 13 | .filter(Number.isFinite); |
| 14 | return values.length ? Math.max(...values) : null; |
| 15 | } |
| 16 | |
| 17 | export function nextProviderState({ |
| 18 | previous, |
| 19 | event, |
| 20 | downAfterFailures = 3, |
| 21 | }) { |
| 22 | if (!previous || typeof previous !== 'object') throw new TypeError('previous is required'); |
| 23 | if (!VALID_STATUS.has(previous.status)) throw new RangeError(`unsupported provider status: ${previous.status}`); |
| 24 | if (!Number.isInteger(previous.consecutiveFailures) || previous.consecutiveFailures < 0) { |
| 25 | throw new RangeError('consecutiveFailures must be a non-negative integer'); |
| 26 | } |
| 27 | if (!event || !['SUCCESS', 'FAILURE'].includes(event.type)) { |
| 28 | throw new RangeError('event.type must be SUCCESS or FAILURE'); |
| 29 | } |
| 30 | if (!Number.isInteger(downAfterFailures) || downAfterFailures < 1) { |
| 31 | throw new RangeError('downAfterFailures must be a positive integer'); |
| 32 | } |
| 33 | |
| 34 | const at = iso(event.at, 'event.at'); |
| 35 | const atMs = new Date(at).valueOf(); |
| 36 | const previousLatest = latestTimestamp(previous); |
| 37 | if (previousLatest !== null && atMs < previousLatest) { |
| 38 | throw new RangeError('provider event chronology cannot move backwards'); |
| 39 | } |
| 40 | |
| 41 | if (event.type === 'SUCCESS') { |
| 42 | return { |
| 43 | status: 'HEALTHY', |
| 44 | consecutiveFailures: 0, |
| 45 | lastSuccessAt: at, |
| 46 | lastFailureAt: previous.lastFailureAt ?? null, |
| 47 | recoveredAt: previous.status === 'DOWN' ? at : null, |
| 48 | }; |
| 49 | } |
| 50 | |
| 51 | const consecutiveFailures = previous.consecutiveFailures + 1; |
| 52 | return { |
| 53 | status: consecutiveFailures >= downAfterFailures ? 'DOWN' : 'DEGRADED', |
| 54 | consecutiveFailures, |
| 55 | lastSuccessAt: previous.lastSuccessAt ?? null, |
| 56 | lastFailureAt: at, |
| 57 | recoveredAt: null, |
| 58 | }; |
| 59 | } |