private-outbox-cleanup.ts (6426B)
1 import { canonicalLocalId } from '../private-handles.ts'; 2 import { LOCAL_PERSISTENCE_BUDGETS } from '../config/budgets.ts'; 3 import { 4 privateSessionOwnership, 5 recheckPrivateSession, 6 subscribePrivateSessionClose, 7 withIdlePrivateLocalStorage, 8 type PrivateSession 9 } from '../runtime/private-session.ts'; 10 import { 11 privateRecordIdentity, 12 privateRecordSnapshot, 13 privateRecordWire, 14 type PrivateRecordHandle 15 } from './private-records.ts'; 16 import { 17 listPrivateSendRecords, 18 removePrivateSettledSendRecords, 19 type PrivateStorageRepository, 20 type PrivateStorageFailure 21 } from './private-storage.ts'; 22 import { privateRecordSettlement } from './local-inventory.ts'; 23 declare const cleanupBrand: unique symbol; 24 export type PrivateOutboxCleanup = Readonly<{ [cleanupBrand]: true }>; 25 type Result = Readonly<{ 26 status: 'removed' | 'invalid' | 'busy' | 'stopped' | PrivateStorageFailure; 27 removed?: number; 28 }>; 29 type Controller = { run(review: unknown): Promise<Result>; stop(): void }; 30 const actions = new WeakMap<PrivateOutboxCleanup, Controller>(); 31 function selectIds(selection: unknown): string[] | undefined { 32 try { 33 if ( 34 !Array.isArray(selection) || 35 !selection.length || 36 selection.length > LOCAL_PERSISTENCE_BUDGETS.unfinishedPrivateSends 37 ) 38 return undefined; 39 const ids = Array.from<unknown>(selection).map(canonicalLocalId); 40 return ids.some((id) => !id) || new Set(ids).size !== ids.length 41 ? undefined 42 : (ids as string[]); 43 } catch { 44 return undefined; 45 } 46 } 47 // Explicit current-owner whole-wire preview. Ciphertext handles remain private 48 // to this one-use action; copied IDs/objects never grant local deletion. 49 export async function capturePrivateOutboxCleanup( 50 repository: PrivateStorageRepository, 51 session: PrivateSession, 52 selection: unknown, 53 review: unknown 54 ): Promise<PrivateOutboxCleanup | undefined> { 55 const ownership = privateSessionOwnership(session), 56 ids = selectIds(selection); 57 if ( 58 typeof window === 'undefined' || 59 !navigator.locks?.request || 60 review !== 'reviewed_settled_outbox_selection' || 61 !ownership?.current() || 62 !ids 63 ) 64 return undefined; 65 try { 66 const scan = await listPrivateSendRecords(repository); 67 if (!scan.ok || !ownership.current()) return undefined; 68 const selected = ids.map((id) => { 69 const handle = scan.value.find( 70 (row) => privateRecordIdentity(row)?.id === id 71 ), 72 record = handle && privateRecordSnapshot(handle, ownership.owner, id), 73 wire = handle && privateRecordWire(handle, ownership.owner, id); 74 if ( 75 !handle || 76 !record || 77 !wire || 78 privateRecordSettlement(record) !== 'settled' 79 ) 80 return undefined; 81 return handle; 82 }); 83 if (selected.some((handle) => !handle)) return undefined; 84 const token = Object.freeze({}) as PrivateOutboxCleanup, 85 cancellation = new AbortController(); 86 let captured = selected as PrivateRecordHandle[], 87 stopped = false, 88 used = false, 89 off = () => {}; 90 function stop() { 91 if (stopped) return; 92 stopped = true; 93 cancellation.abort(); 94 captured = []; 95 off(); 96 } 97 actions.set(token, { 98 stop, 99 async run(consent) { 100 if (stopped || !ownership.current()) return { status: 'stopped' }; 101 if (consent !== 'reviewed_delete_local_settled_sends' || used) 102 return { status: 'invalid' }; 103 used = true; 104 const original = captured.slice(); 105 try { 106 if ( 107 !(await recheckPrivateSession(session)) || 108 stopped || 109 !ownership.current() 110 ) 111 return { 112 status: stopped || !ownership.current() ? 'stopped' : 'busy' 113 }; 114 return await navigator.locks.request( 115 'harvestcircle:owner:' + ownership.owner, 116 { mode: 'exclusive', ifAvailable: true }, 117 async (lock): Promise<Result> => { 118 if (!lock) return { status: 'busy' }; 119 if (stopped || !ownership.current()) return { status: 'stopped' }; 120 let attempted = false; 121 const job = await withIdlePrivateLocalStorage<Result>( 122 session, 123 async () => { 124 if (stopped || !ownership.current()) 125 return { status: 'stopped' }; 126 attempted = true; 127 const committed = await removePrivateSettledSendRecords( 128 repository, 129 session, 130 original, 131 consent, 132 cancellation.signal 133 ); 134 if (!committed.ok) return { status: committed.reason }; 135 const readback = await listPrivateSendRecords(repository); 136 if ( 137 !readback.ok || 138 stopped || 139 !ownership.current() || 140 readback.value.some( 141 (row) => 142 !privateRecordIdentity(row) || 143 ids.includes(privateRecordIdentity(row)!.id) 144 ) 145 ) 146 return { status: 'unknown_completion' }; 147 return { 148 status: 'removed', 149 removed: committed.value.removed 150 }; 151 } 152 ); 153 return job.ok 154 ? job.value 155 : { 156 status: attempted 157 ? 'unknown_completion' 158 : stopped || !ownership.current() 159 ? 'stopped' 160 : 'busy' 161 }; 162 } 163 ); 164 } catch { 165 return { status: 'unknown_completion' }; 166 } finally { 167 captured = []; 168 off(); 169 } 170 } 171 }); 172 off = subscribePrivateSessionClose(session, stop); 173 if (!ownership.current()) { 174 stop(); 175 return undefined; 176 } 177 return token; 178 } catch { 179 return undefined; 180 } 181 } 182 export function deleteReviewedSettledSends( 183 token: PrivateOutboxCleanup, 184 review: unknown 185 ): Promise<Result> { 186 return ( 187 actions.get(token)?.run(review) ?? Promise.resolve({ status: 'invalid' }) 188 ); 189 } 190 export function stopPrivateOutboxCleanup(token: PrivateOutboxCleanup): void { 191 actions.get(token)?.stop(); 192 }