import type { PlayRunLedgerEventSource } from './run-ledger'; const PLAY_RUN_LEDGER_EVENT_SOURCES: readonly PlayRunLedgerEventSource[] = [ 'worker', 'convex', 'coordinator', 'system', ]; /** * Run terminal source state machine. * * Coordinator terminals are transport heals from a live cache and stay * provisional. Producer terminals are authoritative and must win over heals. */ export function normalizePlayRunLedgerTerminalSource( value: unknown, ): PlayRunLedgerEventSource | null { return PLAY_RUN_LEDGER_EVENT_SOURCES.includes( value as PlayRunLedgerEventSource, ) ? (value as PlayRunLedgerEventSource) : null; } export function isAuthoritativePlayRunTerminalSource( value: unknown, ): value is Exclude { const source = normalizePlayRunLedgerTerminalSource(value); return source !== null && source !== 'coordinator'; } export function nextPlayRunLedgerTerminalSource(input: { currentSource: unknown; eventSource: PlayRunLedgerEventSource; }): PlayRunLedgerEventSource { if (input.eventSource !== 'coordinator') { return input.eventSource; } const current = normalizePlayRunLedgerTerminalSource(input.currentSource); return isAuthoritativePlayRunTerminalSource(current) ? current : 'coordinator'; }