@estiva-app/conversation 0.39.0 → 0.41.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,409 @@
1
+ /*
2
+ SPEC §11: marking things read and getting it onto the relay — the one queue
3
+ Ship and Leaf both use (TOPIC-3), so an app writes read state by
4
+ construction rather than by copying another app's queue.
5
+
6
+ One queue per tab, because a slot is one replaceable blob per installation:
7
+ two queues would each hold a partial context map and take turns overwriting
8
+ one another's markers.
9
+
10
+ Whole blob, always. The coordinate is replaced, not patched, so every
11
+ publish carries the complete map, and the slot is read back from the relay
12
+ before every write: every tab of this browser shares the slot, and a blob
13
+ read before another tab's publish would write over what that tab added. A
14
+ partial blob does not fail; it silently drops every context it left out, and
15
+ because the cross-slot merge is grow-only the loss stays invisible until
16
+ this is the last slot standing.
17
+
18
+ Debounced on marks, never on a poll: a refresh is not somebody reading
19
+ anything, and each publish costs an encrypt, a sign and a publish.
20
+
21
+ why: https://github.com/estiva-app/estiva-docs/blob/main/decisions/a-reply-is-read-only-when-its-thread-is-opened.md
22
+ */
23
+ import type { SignedEvent, UnsignedEvent } from '@estiva-app/protocol'
24
+ import type { QueryFn } from './decorations.js'
25
+ import { KIND_APP_DATA } from './kinds.js'
26
+ import {
27
+ advanceContexts as advance,
28
+ splitFloors,
29
+ loadSlotIdentity,
30
+ parseReadStateBlob,
31
+ publishReadState,
32
+ readStateDTag,
33
+ rotateSlotId,
34
+ toContextSeconds,
35
+ type Nip44,
36
+ type RandomBytes,
37
+ type SlotIdentity,
38
+ type SlotStorage,
39
+ } from './readState.js'
40
+
41
+ type Timer = unknown
42
+ const timers = globalThis as unknown as { setTimeout(run: () => void, ms: number): Timer; clearTimeout(id: Timer): void }
43
+ const consoleWarn = (...args: unknown[]): void => (globalThis as unknown as { console?: { warn(...a: unknown[]): void } }).console?.warn(...args)
44
+
45
+ /** Long enough to batch a few marks, short enough to survive a tab close. */
46
+ export const READ_STATE_PUBLISH_DEBOUNCE_MS = 3000
47
+
48
+ /** How long something must be on screen before it counts as read (Peek's, Ship's and Leaf's). */
49
+ export const READ_DWELL_MS = 1500
50
+
51
+ /** The longest wait between attempts while publishing keeps failing. */
52
+ export const READ_STATE_MAX_RETRY_MS = 60_000
53
+
54
+ /** The part of a browser's `document` the queue and the dwell listen to. */
55
+ export interface VisibilitySource {
56
+ readonly hidden: boolean
57
+ addEventListener(type: 'visibilitychange', listener: () => void): void
58
+ removeEventListener(type: 'visibilitychange', listener: () => void): void
59
+ }
60
+
61
+ /** What a flush did, for an app's diagnostic. */
62
+ export type ReadStateAttempt =
63
+ | 'nothing-pending'
64
+ | 'no-session'
65
+ | 'not-hydrated'
66
+ | 'unchanged'
67
+ | 'published'
68
+ | 'rejected'
69
+ | 'failed'
70
+
71
+ export interface ReadStateQueueOptions {
72
+ /** Where the slot id lives (`localStorage`); read when the slot is first needed. It may throw, or be absent. */
73
+ storage: () => SlotStorage | undefined
74
+ /** The app's own key (`ship.readstate.slot`, `leaf.readstate.slot`), so it keeps the slot it has. */
75
+ storageKey: string
76
+ /** Names the app in a new `client_id`: `ship` → `ship-1a2b3c4d`. */
77
+ clientPrefix: string
78
+ random: RandomBytes
79
+ /** Who is signed in now, or undefined. A mark made by nobody is dropped; a change of person drops the last one's queue. */
80
+ pubkey: () => string | undefined
81
+ /**
82
+ * Whether a write can go out now — false while an access token is being renewed. The markers wait and the
83
+ * queue comes back, backing off. Absent: always.
84
+ */
85
+ canWrite?: () => boolean
86
+ query: QueryFn
87
+ nip44: Nip44
88
+ sign: (event: UnsignedEvent) => Promise<SignedEvent>
89
+ publish: (event: SignedEvent) => Promise<{ ok: boolean; reason?: string }>
90
+ /** Each mark that moved a context, at once — before the publish — so a page can draw it read now. */
91
+ onMarked?: (contexts: Readonly<Record<string, number>>) => void
92
+ /** Each flush's outcome, for a diagnostic. */
93
+ onAttempt?: (outcome: ReadStateAttempt, detail?: string, contexts?: number) => void
94
+ debounceMs?: number
95
+ }
96
+
97
+ export interface ReadStateQueue {
98
+ /** Mark these contexts read at `atMs`; the publish follows, debounced. Contexts come from this package's builders. */
99
+ markRead(contexts: readonly (string | undefined)[], atMs?: number): void
100
+ /** Publish what is pending now. */
101
+ flush(): Promise<void>
102
+ /** This installation's slot, minted on first use. */
103
+ identity(): SlotIdentity
104
+ /** Publish before the tab goes away: `visibilitychange` is the one a phone fires. Returns the uninstall. */
105
+ installFlushOnHide(doc: VisibilitySource): () => void
106
+ /** What the queue holds now, copied, for a diagnostic: what can no probe outside the tab can see. */
107
+ state(): ReadStateQueueState
108
+ /** Drop the tab's state, timers included, so a test starts clean. */
109
+ reset(): void
110
+ }
111
+
112
+ export interface ReadStateQueueState {
113
+ /** Everything this slot believes it has read. */
114
+ known: Record<string, number>
115
+ /** Marked but not yet in an accepted publish. Non-empty for long is the smell. */
116
+ pending: Record<string, number>
117
+ /** What the relay is believed to hold for this slot. */
118
+ published: Record<string, number> | undefined
119
+ hydrated: boolean
120
+ /** The wait before the next retry, 0 when none is backing off. */
121
+ retryMs: number
122
+ }
123
+
124
+ export function createReadStateQueue(options: ReadStateQueueOptions): ReadStateQueue {
125
+ const { storageKey, clientPrefix, random, query, nip44 } = options
126
+ const debounceMs = options.debounceMs ?? READ_STATE_PUBLISH_DEBOUNCE_MS
127
+ const canWrite = options.canWrite ?? (() => true)
128
+ const attempt = (outcome: ReadStateAttempt, detail?: string, contexts?: number) => options.onAttempt?.(outcome, detail, contexts)
129
+
130
+ let identity: SlotIdentity | undefined
131
+ /** Whose read state this queue holds. */
132
+ let owner: string | undefined
133
+ /** Everything this slot holds: its own blob from the relay, plus what was marked here. */
134
+ let known: Record<string, number> = {}
135
+ /** Marked but not yet in an accepted publish. */
136
+ let pending: Record<string, number> = {}
137
+ /** What the relay is believed to hold, so an unchanged blob is not published again. */
138
+ let published: Record<string, number> | undefined
139
+ /** The last content seen at our coordinate and what it held, so an unchanged slot is not decrypted again. */
140
+ let ownCopy: { content: string; contexts: Record<string, number> } | undefined
141
+ let hydrated = false
142
+ /** When the copy of our slot read back was written (unix seconds), so the next publish is dated after it. */
143
+ let slotAt = 0
144
+ let timer: Timer | undefined
145
+ let retryMs = 0
146
+ let inFlight: Promise<void> = Promise.resolve()
147
+
148
+ function currentIdentity(): SlotIdentity {
149
+ identity ??= loadSlotIdentity({ storage: safe(options.storage), storageKey, clientPrefix, random })
150
+ return identity
151
+ }
152
+
153
+ function advanceContexts(current: Record<string, number>, updates: Record<string, number>): Record<string, number> {
154
+ const { contexts, evicted } = advance(current, updates)
155
+ if (evicted.length > 0) consoleWarn(`[read-state] over the byte cap; ${evicted.length} oldest markers dropped`)
156
+ return contexts
157
+ }
158
+
159
+ /** Another person in this tab: nothing of the last one's — what they read, what waits to be published — carries over. */
160
+ function forPerson(pubkey: string): void {
161
+ if (owner === pubkey) return
162
+ owner = pubkey
163
+ known = {}
164
+ pending = {}
165
+ published = undefined
166
+ ownCopy = undefined
167
+ hydrated = false
168
+ }
169
+
170
+ /**
171
+ * Read this installation's own blob back. Only our own coordinate: the write needs nothing of the other slots,
172
+ * and each would cost a `/nip44/decrypt` from the quota the rest of the app shares. Unreachable is not empty, and
173
+ * a slot that would not decrypt this time (a 429 above all) is not ours to write over: both throw, leaving the
174
+ * queue unhydrated so it comes back.
175
+ */
176
+ async function hydrate(pubkey: string): Promise<void> {
177
+ hydrated = false
178
+ const slot = currentIdentity()
179
+ const events = await query([{ kinds: [KIND_APP_DATA], authors: [pubkey], '#d': [readStateDTag(slot.slotId)] }])
180
+ const newest = events.filter((e) => e.pubkey === pubkey).sort((a, b) => b.created_at - a.created_at)[0]
181
+ let own: Record<string, number> = {}
182
+ slotAt = newest?.created_at ?? 0
183
+ if (newest && ownCopy?.content === newest.content) own = ownCopy.contexts
184
+ else if (newest) {
185
+ const blob = parseReadStateBlob(await nip44.decrypt(newest.content))
186
+ if (blob?.client_id === slot.clientId) {
187
+ own = blob.contexts
188
+ ownCopy = { content: newest.content, contexts: own }
189
+ } else {
190
+ // Another installation's blob at our coordinate (a copied profile), or one that will never decrypt (a rotated
191
+ // key): take a new slot id and start it empty, never writing over it.
192
+ identity = rotateSlotId(slot, { storage: safe(options.storage), storageKey, random })
193
+ ownCopy = undefined
194
+ }
195
+ }
196
+ if (owner !== pubkey) return
197
+ // Seeded from our own slot, never the merge: a slot is this installation's account of what it read, and seeding
198
+ // from the merge grows every slot to everything the person has read. The floor goes in as `current`, since
199
+ // `advanceContexts` refuses one among the updates.
200
+ const { floors, markers: marked } = splitFloors(known)
201
+ const held = { ...own }
202
+ for (const [floor, at] of Object.entries(floors)) held[floor] = Math.max(at, own[floor] ?? 0)
203
+ known = advanceContexts(held, marked)
204
+ published = own
205
+ hydrated = true
206
+ }
207
+
208
+ function schedule(ms: number): void {
209
+ timer = timers.setTimeout(() => {
210
+ timer = undefined
211
+ void flush()
212
+ }, ms)
213
+ }
214
+
215
+ /** Markers are still pending after a failed attempt: come back, backing off so an outage is not a busy loop. */
216
+ function retryLater(): void {
217
+ if (timer !== undefined) return
218
+ retryMs = retryMs === 0 ? debounceMs : Math.min(retryMs * 2, READ_STATE_MAX_RETRY_MS)
219
+ schedule(retryMs)
220
+ }
221
+
222
+ function flush(): Promise<void> {
223
+ if (timer !== undefined) {
224
+ timers.clearTimeout(timer)
225
+ timer = undefined
226
+ }
227
+ inFlight = inFlight.then(async () => {
228
+ if (Object.keys(pending).length === 0) return attempt('nothing-pending')
229
+ const pubkey = options.pubkey()
230
+ if (!pubkey || !canWrite()) {
231
+ attempt('no-session', 'retrying')
232
+ return retryLater()
233
+ }
234
+ // Marked by someone else who has since left this tab: theirs, never this person's.
235
+ forPerson(pubkey)
236
+ if (Object.keys(pending).length === 0) return attempt('nothing-pending')
237
+ try {
238
+ await hydrate(pubkey)
239
+ } catch (error) {
240
+ consoleWarn('[read-state] could not read the slot back:', error)
241
+ }
242
+ if (!hydrated) {
243
+ attempt('not-hydrated', 'relay unreachable, or our slot would not decrypt; retrying')
244
+ return retryLater()
245
+ }
246
+ const sending = pending
247
+ const contexts = { ...known }
248
+ // Our slot as read back: what it holds is carried verbatim, keys this version does not know included (§11.1).
249
+ const slot = ownCopy?.contexts
250
+ pending = {}
251
+ if (published && sameContexts(published, contexts)) return attempt('unchanged')
252
+ try {
253
+ // Signed only as the person whose markers these are: a sign-in in another tab since the flush began would
254
+ // put them in that person's slot. What goes out is remembered, so reading it back costs no decrypt.
255
+ const result = await publishReadState(
256
+ {
257
+ sign: (event) => (options.pubkey() === pubkey ? options.sign(event) : Promise.reject(new Error('the person signed in changed'))),
258
+ publish: async (event) => {
259
+ const answer = await options.publish(event)
260
+ if (answer.ok) ownCopy = { content: event.content, contexts }
261
+ return answer
262
+ },
263
+ nip44,
264
+ },
265
+ pubkey,
266
+ currentIdentity(),
267
+ contexts,
268
+ // After the copy read back, never in the same second: on a tie the relay keeps the lower id and answers
269
+ // "duplicate" as accepted, so a larger blob published within the second (a flush on hide right after the
270
+ // debounced one) could lose to the smaller one with nothing to say so.
271
+ Math.max(Date.now(), (slotAt + 1) * 1000),
272
+ slot,
273
+ )
274
+ if (result.ok) {
275
+ published = contexts
276
+ retryMs = 0
277
+ return attempt('published', undefined, Object.keys(contexts).length)
278
+ }
279
+ consoleWarn('[read-state] publish refused:', result.reason)
280
+ attempt('rejected', result.reason)
281
+ } catch (error) {
282
+ consoleWarn('[read-state] publish failed:', error)
283
+ attempt('failed', error instanceof Error ? error.message : String(error))
284
+ }
285
+ pending = advanceContexts(pending, sending)
286
+ retryLater()
287
+ })
288
+ // A throw nobody expected (an app's callback) must not leave the chain rejected: every later flush would be skipped
289
+ // and nothing marked in this tab would reach the relay again.
290
+ const run = inFlight
291
+ inFlight = run.catch((error: unknown) => {
292
+ consoleWarn('[read-state] flush failed:', error)
293
+ if (Object.keys(pending).length > 0) retryLater()
294
+ })
295
+ return run
296
+ }
297
+
298
+ function markRead(contexts: readonly (string | undefined)[], atMs: number = Date.now()): void {
299
+ const pubkey = options.pubkey()
300
+ if (!pubkey) return
301
+ forPerson(pubkey)
302
+ const seconds = toContextSeconds(atMs)
303
+ const moved: Record<string, number> = {}
304
+ for (const context of contexts) {
305
+ if (!context) continue
306
+ const advanced = advanceContexts(known, { [context]: seconds })
307
+ // Not newer, or not a context the blob takes: nothing to publish.
308
+ if (advanced[context] === known[context]) continue
309
+ known = advanced
310
+ pending = advanceContexts(pending, { [context]: seconds })
311
+ moved[context] = seconds
312
+ }
313
+ if (Object.keys(moved).length === 0) return
314
+ options.onMarked?.(moved)
315
+ if (timer !== undefined) timers.clearTimeout(timer)
316
+ schedule(debounceMs)
317
+ }
318
+
319
+ function installFlushOnHide(doc: VisibilitySource): () => void {
320
+ const onHidden = () => {
321
+ if (doc.hidden) void flush()
322
+ }
323
+ doc.addEventListener('visibilitychange', onHidden)
324
+ return () => doc.removeEventListener('visibilitychange', onHidden)
325
+ }
326
+
327
+ function reset(): void {
328
+ if (timer !== undefined) timers.clearTimeout(timer)
329
+ timer = undefined
330
+ identity = undefined
331
+ owner = undefined
332
+ known = {}
333
+ pending = {}
334
+ published = undefined
335
+ ownCopy = undefined
336
+ hydrated = false
337
+ slotAt = 0
338
+ retryMs = 0
339
+ inFlight = Promise.resolve()
340
+ }
341
+
342
+ const state = (): ReadStateQueueState => ({
343
+ known: { ...known },
344
+ pending: { ...pending },
345
+ published: published ? { ...published } : undefined,
346
+ hydrated,
347
+ retryMs,
348
+ })
349
+
350
+ return { markRead, flush, identity: currentIdentity, installFlushOnHide, state, reset }
351
+ }
352
+
353
+ /**
354
+ * Mark `contexts` read once they have stayed on screen for {@link READ_DWELL_MS}: a conversation glanced past is not
355
+ * read. Only while the page is shown, and again when it comes back. `newestAtMs` is the newest message drawn for
356
+ * them: the mark never runs past it or past the clock, and a message dated ahead of the clock is read once the clock
357
+ * gets there, asked again at most a minute apart (a date far ahead would overflow the timer). With none drawn, the
358
+ * clock. Returns the cleanup — what a React effect returns.
359
+ */
360
+ export function watchDwell(
361
+ contexts: readonly string[],
362
+ newestAtMs: number | undefined,
363
+ mark: (contexts: readonly string[], atMs: number) => void,
364
+ doc: VisibilitySource,
365
+ dwellMs: number = READ_DWELL_MS,
366
+ ): () => void {
367
+ if (contexts.length === 0) return () => {}
368
+ let t: Timer | undefined
369
+ const fire = () => {
370
+ const now = Date.now()
371
+ mark(contexts, newestAtMs === undefined ? now : Math.min(newestAtMs, now))
372
+ if (newestAtMs !== undefined && newestAtMs > now) t = timers.setTimeout(fire, Math.min(newestAtMs - now, 60_000))
373
+ }
374
+ const arm = () => {
375
+ timers.clearTimeout(t)
376
+ if (!doc.hidden) t = timers.setTimeout(fire, dwellMs)
377
+ }
378
+ arm()
379
+ doc.addEventListener('visibilitychange', arm)
380
+ return () => {
381
+ timers.clearTimeout(t)
382
+ doc.removeEventListener('visibilitychange', arm)
383
+ }
384
+ }
385
+
386
+ /**
387
+ * The key a dwell restarts on: the set of contexts, not the array a render made. JSON, so it is never split back on a
388
+ * character an address may hold; {@link dwellContexts} turns it back.
389
+ */
390
+ export function dwellKey(contexts: readonly (string | undefined)[]): string {
391
+ return JSON.stringify(contexts.filter((c): c is string => Boolean(c)).sort())
392
+ }
393
+
394
+ export function dwellContexts(key: string): string[] {
395
+ return JSON.parse(key) as string[]
396
+ }
397
+
398
+ function safe(storage: () => SlotStorage | undefined): SlotStorage | undefined {
399
+ try {
400
+ return storage()
401
+ } catch {
402
+ return undefined
403
+ }
404
+ }
405
+
406
+ function sameContexts(a: Record<string, number>, b: Record<string, number>): boolean {
407
+ const keys = Object.keys(a)
408
+ return keys.length === Object.keys(b).length && keys.every((k) => a[k] === b[k])
409
+ }