readiness.ts (6152B)
1 import { 2 identitySessionSnapshot, 3 identityMessagingOwnership, 4 type IdentitySession 5 } from '../runtime/identity-session.ts'; 6 import { 7 privateSessionOwnership, 8 type PrivateSession 9 } from '../runtime/private-session.ts'; 10 import { 11 readRelayPolicy, 12 publicRelayTargets, 13 inboxRelayTargets, 14 type RelayPolicy 15 } from '../config/relays.ts'; 16 import { inboxPreferenceSnapshot } from '../nostr/inbox-preferences.ts'; 17 import { 18 inboxResolutionSnapshot, 19 inboxResolverCurrentAfterSample, 20 type InboxResolver 21 } from './resolve-inbox.ts'; 22 import { createInboxRoutePlan } from './inbox-routing.ts'; 23 24 export type OwnInboxAccessContext = Readonly<{ 25 owner: string; 26 session: symbol; 27 preferenceId: string; 28 readTargets: readonly string[]; 29 archiveTargets: readonly string[]; 30 }>; 31 export type InboxAccessObservation = Readonly<{ 32 owner: string; 33 session: symbol; 34 preferenceId: string; 35 readTargets: readonly string[]; 36 archiveTargets: readonly string[]; 37 receive: 'qualified_exercised'; 38 archive: 'qualified_exercised'; 39 current(): boolean; 40 }>; 41 // Internal trusted observation port, like the discovery request/generation ports. 42 // It must report real qualified, exercised access for these exact bindings, not 43 // infer it from manifest flags, an EOSE or a caller boolean. No production port 44 // is configured here. Controlled test observations qualify only the projection. 45 export type ObserveInboxAccess = ( 46 context: OwnInboxAccessContext 47 ) => InboxAccessObservation | undefined; 48 export type MessagingReadiness = Readonly<{ 49 browse: true; 50 draft: true; 51 preferenceWrite: 'not_requested'; 52 ownConfiguration: 'compatible' | 'missing' | 'unsupported' | 'unknown'; 53 ownAccess: 'qualified_exercised' | 'unavailable'; 54 newListingReady: boolean; 55 sendReady: boolean; 56 }>; 57 function sameTargets(a: readonly string[], b: readonly string[]): boolean { 58 return ( 59 a.length === b.length && a.every((origin, index) => origin === b[index]) 60 ); 61 } 62 // Detached informational projection only. No prompt, transport, publication or 63 // private effect permission. Effect owners still require fresh qualified proof, 64 // account recheck and explicit review; a cached boolean never authorizes them. 65 export function messagingReadinessSnapshot( 66 identity: IdentitySession, 67 session: PrivateSession | undefined, 68 policy: RelayPolicy, 69 own: InboxResolver | undefined, 70 peer?: Readonly<{ author: unknown; resolver: InboxResolver }>, 71 observeAccess?: ObserveInboxAccess 72 ): MessagingReadiness { 73 const blocked: MessagingReadiness = { 74 browse: true, 75 draft: true, 76 preferenceWrite: 'not_requested', 77 ownConfiguration: 'unknown', 78 ownAccess: 'unavailable', 79 newListingReady: false, 80 sendReady: false 81 }; 82 try { 83 const identityState = identitySessionSnapshot(identity); 84 const capture = identityMessagingOwnership(identity); 85 const privateOwner = session && privateSessionOwnership(session); 86 if ( 87 identityState.state !== 'messaging_capable' || 88 !capture || 89 !privateOwner || 90 privateOwner.owner !== capture.owner || 91 privateOwner.session !== capture.session || 92 !own 93 ) 94 return blocked; 95 const manifest = readRelayPolicy(policy); 96 const resolution = inboxResolutionSnapshot(own); 97 if (resolution.author !== capture.owner) return blocked; 98 if ( 99 resolution.status !== 'ready' || 100 !resolution.head || 101 !resolution.knownBase 102 ) 103 return { 104 ...blocked, 105 ownConfiguration: 106 resolution.status === 'missing' 107 ? 'missing' 108 : resolution.status === 'unsupported' 109 ? 'unsupported' 110 : 'unknown' 111 }; 112 const preference = inboxPreferenceSnapshot(resolution.head); 113 const discovery = publicRelayTargets(policy, 'read'); 114 if ( 115 !preference || 116 preference.status !== 'supported' || 117 preference.author !== capture.owner || 118 preference.id !== resolution.knownBase.id || 119 resolution.coverage !== 'bounded-eose' || 120 discovery.length === 0 || 121 discovery.length !== resolution.sources.length || 122 !discovery.every((origin) => 123 resolution.sources.some( 124 (row) => row.source === origin && row.state === 'eose' 125 ) 126 ) 127 ) 128 return blocked; 129 const readTargets = inboxRelayTargets(policy, preference.relays, 'read'); 130 const archiveTargets = inboxRelayTargets( 131 policy, 132 preference.relays, 133 'write' 134 ); 135 if (readTargets.length === 0 || archiveTargets.length === 0) 136 return { ...blocked, ownConfiguration: 'unsupported' }; 137 const configured = { ...blocked, ownConfiguration: 'compatible' as const }; 138 if (!manifest.messagingEnabled || !observeAccess) return configured; 139 // Detached input prevents a port from changing our expected routes. 140 const context: OwnInboxAccessContext = { 141 owner: capture.owner, 142 session: capture.session, 143 preferenceId: preference.id, 144 readTargets: readTargets.slice(), 145 archiveTargets: archiveTargets.slice() 146 }; 147 const access = observeAccess(context); 148 const matches = 149 !!access && 150 access.owner === capture.owner && 151 access.session === capture.session && 152 access.preferenceId === preference.id && 153 access.receive === 'qualified_exercised' && 154 access.archive === 'qualified_exercised' && 155 sameTargets(access.readTargets, readTargets) && 156 sameTargets(access.archiveTargets, archiveTargets); 157 const plan = 158 peer && 159 createInboxRoutePlan( 160 policy, 161 capture.owner, 162 peer.author, 163 own, 164 peer.resolver 165 ); 166 // Sample-free ownership fences after all clocks and trusted port reads. 167 const available = 168 matches && 169 access.current() && 170 capture.current() && 171 privateOwner.current() && 172 inboxResolverCurrentAfterSample(own); 173 if (!available) return configured; 174 return { 175 ...configured, 176 ownAccess: 'qualified_exercised', 177 newListingReady: true, 178 sendReady: 179 !!plan && !!peer && inboxResolverCurrentAfterSample(peer.resolver) 180 }; 181 } catch { 182 return blocked; 183 } 184 }