continuation.ts (7043B)
1 import { 2 RELAY_BUDGETS, 3 PUBLIC_QUERY_BUDGETS, 4 PUBLIC_SEARCH_BUDGETS, 5 PUBLIC_SEARCH_RUN_BUDGETS 6 } from '../config/budgets.ts'; 7 import { publicHeadSnapshot, type PublicHead } from './heads.ts'; 8 export type ChronologicalCoverage = 9 'unchecked' | 'checking' | 'bounded-eose' | 'partial'; 10 export type ChronologicalProgress = Readonly<{ 11 windows: number; 12 active: boolean; 13 until: number | undefined; 14 gap: boolean; 15 coverage: ChronologicalCoverage; 16 observed: number; 17 definitiveAbsence: false; 18 }>; 19 export type ChronologicalSource = Readonly<{ 20 source: string; 21 saturated: boolean; 22 eventIds: readonly string[]; 23 }>; 24 export interface ChronologicalWindow { 25 readonly until: number | undefined; 26 readonly observe: (head: PublicHead) => void; 27 readonly finish: ( 28 saturated: boolean, 29 coverage: 'bounded-eose' | 'partial', 30 sources?: readonly ChronologicalSource[] 31 ) => void; 32 } 33 export interface ChronologicalContinuation { 34 readonly begin: () => ChronologicalWindow; 35 readonly snapshot: () => ChronologicalProgress; 36 readonly restart: () => ChronologicalContinuation; 37 } 38 // Pure bookkeeping, held only in the coordinator's private session. The 39 // coordinator alone associates verified chronological callbacks and real scope 40 // outcomes; sample/auxiliary deliveries never reach this owner. 41 function continuation( 42 seedUntil?: number, 43 seedIds: readonly string[] = [], 44 seedPartial = false 45 ): ChronologicalContinuation { 46 let until = seedUntil, 47 windows = 0, 48 active = false, 49 gap = false, 50 partial = seedPartial, 51 observed = 0; 52 let boundary = new Map(seedIds.map((id) => [id, true] as const)); 53 const owner: ChronologicalContinuation = { 54 begin() { 55 if (active) throw new Error('food_search_chronological_pending'); 56 if (gap) throw new Error('food_search_chronological_gap'); 57 if (windows >= PUBLIC_SEARCH_RUN_BUDGETS.chronologicalWindows) 58 throw new Error('food_search_chronological_window_limit'); 59 windows++; 60 active = true; 61 const requestedUntil = until, 62 ids = new Map<string, number>(); 63 let settled = false; 64 return { 65 until: requestedUntil, 66 observe(head) { 67 if (settled) 68 throw new Error('food_search_chronological_window_closed'); 69 const value = publicHeadSnapshot(head); 70 if ( 71 value.kind !== 30402 || 72 (requestedUntil !== undefined && value.created_at > requestedUntil) 73 ) 74 return; 75 if ( 76 !ids.has(value.id) && 77 ids.size >= PUBLIC_QUERY_BUDGETS.coordinatesPerRun 78 ) { 79 gap = true; 80 partial = true; 81 return; 82 } 83 ids.set(value.id, value.created_at); 84 }, 85 finish(saturated, coverage, sources) { 86 if (settled) return; 87 const facts = sources ?? [ 88 { source: '', saturated, eventIds: Array.from(ids.keys()) } 89 ]; 90 if ( 91 facts.length > RELAY_BUDGETS.public || 92 facts.some( 93 (source) => 94 source.eventIds.length > PUBLIC_QUERY_BUDGETS.coordinatesPerRun 95 ) 96 ) 97 throw new Error('food_search_chronological_source_limit'); 98 settled = true; 99 active = false; 100 partial = partial || coverage === 'partial'; 101 observed = ids.size; 102 const times = Array.from(ids.values()); 103 let oldest = times.length 104 ? times.reduce((minimum, value) => Math.min(minimum, value)) 105 : undefined; 106 let protectedUntil: number | undefined; 107 for (const source of facts) { 108 if (!source.saturated) continue; 109 const sourceTimes = source.eventIds 110 .filter((id) => ids.has(id)) 111 .map((id) => ids.get(id)!); 112 const sourceOldest = sourceTimes.length 113 ? sourceTimes.reduce((minimum, value) => Math.min(minimum, value)) 114 : undefined; 115 if (sourceOldest === undefined) { 116 gap = true; 117 partial = true; 118 continue; 119 } 120 protectedUntil = 121 protectedUntil === undefined 122 ? sourceOldest 123 : Math.max(protectedUntil, sourceOldest); 124 const progress = 125 requestedUntil === undefined || 126 sourceOldest < requestedUntil || 127 source.eventIds.some( 128 (id) => 129 ids.get(id) === sourceOldest && 130 !boundary.has(source.source + '\0' + id) 131 ); 132 if (!progress) { 133 gap = true; 134 partial = true; 135 } 136 } 137 // Every saturated source's unresolved boundary is protected. Another 138 // source's older item must never move a shared cursor below it. 139 oldest = 140 gap && requestedUntil !== undefined 141 ? requestedUntil 142 : (protectedUntil ?? oldest); 143 if (oldest !== undefined) { 144 if (oldest !== requestedUntil) boundary = new Map(); 145 for (const source of facts) 146 for (const id of source.eventIds) { 147 if (ids.get(id) !== oldest) continue; 148 const key = source.source + '\0' + id; 149 if ( 150 !boundary.has(key) && 151 boundary.size >= PUBLIC_QUERY_BUDGETS.coordinatesPerRun 152 ) { 153 gap = true; 154 partial = true; 155 break; 156 } 157 boundary.set(key, true); 158 } 159 until = oldest; 160 } 161 } 162 }; 163 }, 164 snapshot: () => ({ 165 windows, 166 active, 167 until, 168 gap, 169 coverage: active 170 ? 'checking' 171 : partial || gap 172 ? 'partial' 173 : windows 174 ? 'bounded-eose' 175 : 'unchecked', 176 observed, 177 definitiveAbsence: false 178 }), 179 restart() { 180 if (active) throw new Error('food_search_chronological_pending'); 181 if (gap) throw new Error('food_search_chronological_gap'); 182 if (until === undefined) 183 throw new Error('food_search_chronological_boundary_missing'); 184 return continuation(until, Array.from(boundary.keys()), partial); 185 } 186 }; 187 return owner; 188 } 189 export function createChronologicalContinuation(): ChronologicalContinuation { 190 return continuation(); 191 } 192 193 // Local pages preserve the caller's observed ordering. No clocks or transport. 194 export function localSearchPage<T>( 195 rows: readonly T[], 196 pages: number 197 ): Readonly<{ rows: readonly T[]; hasMore: boolean }> { 198 if ( 199 !Number.isSafeInteger(pages) || 200 pages < 1 || 201 pages > 202 Math.ceil( 203 PUBLIC_QUERY_BUDGETS.coordinatesPerRun / PUBLIC_SEARCH_BUDGETS.pageRows 204 ) 205 ) 206 throw new Error('food_search_local_page_invalid'); 207 const count = pages * PUBLIC_SEARCH_BUDGETS.pageRows; 208 return { rows: rows.slice(0, count), hasMore: rows.length > count }; 209 }