9f996e377406 | atagen | 2026-10-25 03:32 UTC | 1 | export type HistoryRepairPolicy = |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 2 | | 'StartRecent' |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 3 | | 'Bridge' |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 4 | | { BridgeShortGaps: { threshold_ms: number } }; |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 5 | |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 6 | export interface HistoryRepairBudget { |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 7 | max_pages: number; |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 8 | max_events: number; |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 9 | max_duration_ms: number; |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 10 | } |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 11 | |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 12 | export interface HistoryRepairFailure { |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 13 | kind: string; |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 14 | message: string; |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 15 | server?: { status: number; errcode: string; message: string } | null; |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 16 | [key: string]: unknown; |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 17 | } |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 18 | |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 19 | export interface HistoryRepairReport { |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 20 | room_id: string; |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 21 | requested_policy: HistoryRepairPolicy; |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 22 | strategy: 'Recent' | 'Overlap' | 'Bridge'; |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 23 | coverage_duration_ms: number | null; |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 24 | threshold_evaluated: boolean; |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 25 | pages: number; |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 26 | events: number; |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 27 | phase: 'FetchingRecent' | 'Bridging' | 'Ready' | 'Paused'; |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 28 | continuity: 'Unresolved' | 'Verified' | 'AccessibleStart'; |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 29 | recent_available: boolean; |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 30 | stop: 'Cancelled' | 'Budget' | 'HistoryUnavailable' | 'NoAnchor' | 'CursorStalled' | null; |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 31 | failure: HistoryRepairFailure | null; |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 32 | } |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 33 | |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 34 | export const DEFAULT_HISTORY_REPAIR_BUDGET: HistoryRepairBudget = { |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 35 | max_pages: 10, |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 36 | max_events: 1000, |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 37 | max_duration_ms: 15000 |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 38 | }; |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 39 | |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 40 | function record(value: unknown): Record<string, unknown> | null { |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 41 | return typeof value === 'object' && value !== null && !Array.isArray(value) |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 42 | ? (value as Record<string, unknown>) |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 43 | : null; |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 44 | } |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 45 | |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 46 | function safeNonnegativeInteger(value: unknown): value is number { |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 47 | return typeof value === 'number' && Number.isSafeInteger(value) && value >= 0; |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 48 | } |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 49 | |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 50 | export function parseHistoryRepairPolicy(value: unknown): HistoryRepairPolicy | null { |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 51 | if (value === 'StartRecent' || value === 'Bridge') return value; |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 52 | const outer = record(value); |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 53 | const details = outer && record(outer['BridgeShortGaps']); |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 54 | if (!outer || Object.keys(outer).length !== 1 || !details || Object.keys(details).length !== 1 || !safeNonnegativeInteger(details['threshold_ms'])) return null; |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 55 | return { BridgeShortGaps: { threshold_ms: details['threshold_ms'] } }; |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 56 | } |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 57 | |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 58 | function isOneOf<T extends string>(value: unknown, values: readonly T[]): value is T { |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 59 | return typeof value === 'string' && (values as readonly string[]).includes(value); |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 60 | } |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 61 | |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 62 | export function parseHistoryRepairReport(value: unknown): HistoryRepairReport | null { |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 63 | const data = record(value); |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 64 | if (!data) return null; |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 65 | const policy = parseHistoryRepairPolicy(data['requested_policy']); |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 66 | const failure = data['failure']; |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 67 | const failureData = failure === null ? null : record(failure); |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 68 | if ( |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 69 | typeof data['room_id'] !== 'string' || !policy || |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 70 | !isOneOf(data['strategy'], ['Recent', 'Overlap', 'Bridge']) || |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 71 | !(data['coverage_duration_ms'] === null || safeNonnegativeInteger(data['coverage_duration_ms'])) || |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 72 | typeof data['threshold_evaluated'] !== 'boolean' || |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 73 | !safeNonnegativeInteger(data['pages']) || !safeNonnegativeInteger(data['events']) || |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 74 | !isOneOf(data['phase'], ['FetchingRecent', 'Bridging', 'Ready', 'Paused']) || |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 75 | !isOneOf(data['continuity'], ['Unresolved', 'Verified', 'AccessibleStart']) || |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 76 | typeof data['recent_available'] !== 'boolean' || |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 77 | !(data['stop'] === null || isOneOf(data['stop'], ['Cancelled', 'Budget', 'HistoryUnavailable', 'NoAnchor', 'CursorStalled'])) || |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 78 | !(failure === null || (failureData && typeof failureData['kind'] === 'string' && typeof failureData['message'] === 'string')) |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 79 | ) return null; |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 80 | return { |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 81 | room_id: data['room_id'], |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 82 | requested_policy: policy, |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 83 | strategy: data['strategy'], |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 84 | coverage_duration_ms: data['coverage_duration_ms'], |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 85 | threshold_evaluated: data['threshold_evaluated'], |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 86 | pages: data['pages'], |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 87 | events: data['events'], |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 88 | phase: data['phase'], |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 89 | continuity: data['continuity'], |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 90 | recent_available: data['recent_available'], |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 91 | stop: data['stop'], |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 92 | failure: failureData as HistoryRepairFailure | null |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 93 | }; |
9f996e377406 | atagen | 2026-10-25 03:32 UTC | 94 | } |