@estiva-app/interop 0.95.0 → 0.97.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,177 @@
1
+ /**
2
+ * Your topics, lean: only the topics the viewer is in, newest activity first —
3
+ * what a sidebar draws (ALO-17), where {@link readTopics} reads every topic the
4
+ * viewer can open, the viewer's whole part in each and their text.
5
+ *
6
+ * The same answer as {@link readTopics}, the filter every app applies (in it,
7
+ * not archived, its own or with a parent, not muted, not a space) and
8
+ * {@link readTopicActivity} over what is left, for fewer requests and bytes:
9
+ *
10
+ * 1. every topic's root, and the viewer's mute list, in one request;
11
+ * 2. the changes naming the viewer on any topic and the team lists naming
12
+ * them (one request), beside every topic's current fields by the list read
13
+ * (another: a relay refuses a list read mixed with other filters) — each
14
+ * topic's title, archive and parent come from that answer, and only the
15
+ * topics the viewer is in are read further;
16
+ * 3. their listed ids, what the list read skipped and the manifests of their
17
+ * parents, in one request;
18
+ * 4. their parents, a level a request by the list read, to see whether one is
19
+ * archived, while their activity is read.
20
+ *
21
+ * Without the list read (the relay does not advertise it), round 2 reads each
22
+ * topic's changes plain, as {@link readTopics} does. A topic the list read
23
+ * does not answer (deleted) is read plain, as a parent is.
24
+ */
25
+ import { mutedFilter, parseMutedBlob, MUTED_D_TAG } from '@estiva-app/conversation';
26
+ import { FILE_KINDS } from '@estiva-app/protocol';
27
+ import { addressPointerOf, createProjectionCache, isArchived, markTopicsArchivedWith, projectTopics, seedManifests, } from './projection.js';
28
+ import { answeredBy, changesNamingFilter, eventsByTopic, listReadPlan, listsFrom, newestChanges, readAll, readTopicOf, resolveIds, spacesOf, teamListFilters, topicListFilter, topicRoots, } from './topics.js';
29
+ import { activityTopics, byActivity, readTopicActivity } from './activity.js';
30
+ const KIND_TOPIC = 30840;
31
+ const KIND_CHANGE = 1851;
32
+ const KIND_APP_DATA = 30078;
33
+ /** As {@link resolveForeignRoots} reads a file's changes when it reads them plain. */
34
+ const PLAIN_CHANGES_LIMIT = 500;
35
+ /**
36
+ * The viewer's topics, newest activity first, in about ten requests on a
37
+ * workspace like Miky's (53 topics, 12 his): see the module's rounds.
38
+ *
39
+ * `listRead`, where the relay advertises the list read (NIP-11
40
+ * `file_list.newest`): the topics and their parents are read as their
41
+ * current fields (§6.13); `readTopics(query, me, { listRead, parentsListRead:
42
+ * listRead })` reads the same. `decrypt` opens the mute list (NIP-44 to
43
+ * self); `cache` keeps the parents' manifests between reads.
44
+ */
45
+ export async function readYourTopics(query, me, options) {
46
+ const listRead = options.listRead ?? false;
47
+ const cache = options.cache ?? createProjectionCache();
48
+ // 1. Every topic's root, and the mute list.
49
+ const first = gathered(query);
50
+ const rootsRead = readAll(first.query, [{ kinds: [KIND_TOPIC] }]);
51
+ const mutedOpened = first.query([mutedFilter(me)]).then((events) => mutedOf(events, me, options.decrypt));
52
+ // Its failure is the roots' too, and said by them.
53
+ mutedOpened.catch(() => { });
54
+ first.send();
55
+ const roots = topicRoots(await rootsRead);
56
+ const addresses = [...roots.keys()];
57
+ if (addresses.length === 0)
58
+ return { me, topics: [], at: new Map(), muted: await mutedOpened };
59
+ // 2. How the viewer is in each, which are spaces, and each topic's fields.
60
+ const second = gathered(query);
61
+ const partRead = readAll(second.query, [changesNamingFilter(me, addresses)]);
62
+ const statesRead = readAll(second.query, teamListFilters(roots));
63
+ // Without the list read: each topic's changes as `resolveForeignRoots` and `topicLists` read them.
64
+ const objectChangesRead = listRead ? Promise.resolve([]) : second.query(addresses.map(plainChangesFilter));
65
+ const listChangesRead = listRead ? Promise.resolve([]) : readAll(second.query, addresses.map(topicListFilter));
66
+ second.send();
67
+ const [part, states, listed, objectChanges, listChanges, muted] = await Promise.all([
68
+ partRead,
69
+ statesRead,
70
+ listRead ? newestChanges(query, addresses) : Promise.resolve([]),
71
+ objectChangesRead,
72
+ listChangesRead,
73
+ mutedOpened,
74
+ ]);
75
+ const spaces = spacesOf(states, addresses);
76
+ const byTopic = eventsByTopic(part);
77
+ const facts = { roots, byTopic, states, spaces };
78
+ const mine = (object) => {
79
+ const topic = readTopicOf(object, me, facts);
80
+ return topic && topic.yours && (options.spaces || !topic.space) && !muted?.has(topic.address) ? topic : undefined;
81
+ };
82
+ // A topic the list read did not answer (deleted) is read plain below, as a parent is.
83
+ const answered = new Set(topicRoots(listed).keys());
84
+ const byList = listRead ? addresses.filter((address) => answered.has(address)) : addresses;
85
+ const objects = projectTopics([...byList.map((address) => roots.get(address)), ...(listRead ? listed : objectChanges)], listRead);
86
+ if (listRead) {
87
+ const unanswered = addresses.filter((address) => !answered.has(address));
88
+ // Asked about only when one would be the viewer's, by its root alone: whether they are in it does not depend on its fields.
89
+ const maybe = projectTopics(unanswered.map((address) => roots.get(address)), false).flatMap((object) => (mine(object) ? [object.address] : []));
90
+ if (maybe.length > 0)
91
+ objects.push(...projectTopics([...maybe.map((address) => roots.get(address)), ...(await query(maybe.map(plainChangesFilter)))], false));
92
+ }
93
+ const candidates = [];
94
+ for (const object of objects) {
95
+ const topic = mine(object);
96
+ if (topic && !isArchived(topic.object))
97
+ candidates.push(topic);
98
+ }
99
+ if (candidates.length === 0)
100
+ return { me, topics: [], at: new Map(), muted };
101
+ // 3. Their listed ids, what the list read skipped, and their parents' manifests.
102
+ const asked = candidates.map((topic) => topic.address);
103
+ const listEvents = listRead ? listed : listChanges;
104
+ const plan = listReadPlan(asked, roots, listEvents, listRead);
105
+ const parents = candidates.flatMap((topic) => {
106
+ const parent = topic.object.parentRef ? addressPointerOf(topic.object.parentRef) : null;
107
+ return parent ? [parent] : [];
108
+ });
109
+ const third = gathered(query);
110
+ const resolvedRead = plan.ids.length ? resolveIds(third.query, plan.ids, options.fileKinds) : Promise.resolve(new Map());
111
+ const againRead = readAll(third.query, plan.again);
112
+ const seeded = seedManifests(parents, third.query, cache);
113
+ third.send();
114
+ const [resolved, again] = await Promise.all([resolvedRead, againRead, seeded]);
115
+ const lists = listsFrom(asked, roots, [...listEvents, ...again], resolved);
116
+ const topics = candidates.map((topic) => ({ ...topic, listed: lists.get(topic.address) ?? [] }));
117
+ // 4. Their parents, while their activity is read: one archived with a parent is left out after.
118
+ const [activity] = await Promise.all([
119
+ readTopicActivity(query, activityTopics(topics), { nowSeconds: options.nowSeconds }),
120
+ markTopicsArchivedWith(topics.map((topic) => topic.object), query, cache, listRead ? FILE_KINDS : []),
121
+ ]);
122
+ const open = topics.filter((topic) => !isArchived(topic.object));
123
+ const newest = byActivity(activity.at);
124
+ open.sort((a, b) => newest(a.address, b.address));
125
+ return { me, topics: open, at: new Map(open.map((topic) => [topic.address, activity.at.get(topic.address) ?? 0])), muted };
126
+ }
127
+ /** A topic's changes as {@link resolveForeignRoots} reads them plain: the newest 500 naming it by `a`. */
128
+ function plainChangesFilter(address) {
129
+ return { kinds: [KIND_CHANGE], '#a': [address], limit: PLAIN_CHANGES_LIMIT };
130
+ }
131
+ /** The viewer's muted keys from a read of their mute list: none without one, `undefined` when it does not open. */
132
+ async function mutedOf(events, me, decrypt) {
133
+ const list = events
134
+ .filter((e) => e.pubkey === me && e.kind === KIND_APP_DATA && e.tags.some((t) => t[0] === 'd' && t[1] === MUTED_D_TAG))
135
+ .sort((a, b) => b.created_at - a.created_at)[0];
136
+ if (!list)
137
+ return new Set();
138
+ const parsed = parseMutedBlob((await decrypt(list.content).catch(() => undefined)) ?? '');
139
+ return parsed ? new Set(parsed.keys) : undefined;
140
+ }
141
+ /**
142
+ * A query that holds every request made through it until {@link send}, then
143
+ * asks them all as one request and hands each caller what its own filters
144
+ * would have been answered alone ({@link answeredBy}). A request made after
145
+ * `send` (a later page), and a list read, which a relay takes only alone, go
146
+ * straight through.
147
+ */
148
+ function gathered(query) {
149
+ let held = [];
150
+ return {
151
+ query: (filters) => {
152
+ if (!held || filters.some((filter) => 'fields' in filter))
153
+ return query(filters);
154
+ const asks = held;
155
+ return new Promise((resolve, reject) => asks.push({ filters, resolve, reject }));
156
+ },
157
+ send() {
158
+ const asks = held ?? [];
159
+ held = null;
160
+ if (asks.length === 0)
161
+ return;
162
+ query(asks.flatMap((ask) => ask.filters)).then((events) => {
163
+ for (const ask of asks) {
164
+ const page = new Map();
165
+ for (const filter of ask.filters)
166
+ for (const event of answeredBy(events, filter))
167
+ page.set(event.id, event);
168
+ ask.resolve([...page.values()]);
169
+ }
170
+ }, (error) => {
171
+ for (const ask of asks)
172
+ ask.reject(error);
173
+ });
174
+ },
175
+ };
176
+ }
177
+ //# sourceMappingURL=yourTopics.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"yourTopics.js","sourceRoot":"","sources":["../src/yourTopics.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,OAAO,EAAE,WAAW,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAA;AACnF,OAAO,EAAE,UAAU,EAAoB,MAAM,sBAAsB,CAAA;AACnE,OAAO,EACL,gBAAgB,EAChB,qBAAqB,EACrB,UAAU,EACV,sBAAsB,EACtB,aAAa,EACb,aAAa,GAId,MAAM,iBAAiB,CAAA;AACxB,OAAO,EACL,UAAU,EACV,mBAAmB,EACnB,aAAa,EACb,YAAY,EACZ,SAAS,EACT,aAAa,EACb,OAAO,EACP,WAAW,EACX,UAAU,EACV,QAAQ,EACR,eAAe,EACf,eAAe,EACf,UAAU,GAGX,MAAM,aAAa,CAAA;AACpB,OAAO,EAAE,cAAc,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAA;AAE7E,MAAM,UAAU,GAAG,KAAK,CAAA;AACxB,MAAM,WAAW,GAAG,IAAI,CAAA;AACxB,MAAM,aAAa,GAAG,KAAK,CAAA;AAC3B,sFAAsF;AACtF,MAAM,mBAAmB,GAAG,GAAG,CAAA;AAmB/B;;;;;;;;;GASG;AACH,MAAM,CAAC,KAAK,UAAU,cAAc,CAClC,KAAc,EACd,EAAU,EACV,OAKC;IAED,MAAM,QAAQ,GAAG,OAAO,CAAC,QAAQ,IAAI,KAAK,CAAA;IAC1C,MA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AM;YAC7B,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAC5C,CAAC,MAAM,EAAE,EAAE;gBACT,KAAK,MAAM,GAAG,IAAI,IAAI,EAAE,CAAC;oBACvB,MAAM,IAAI,GAAG,IAAI,GAAG,EAAuB,CAAA;oBAC3C,KAAK,MAAM,MAAM,IAAI,GAAG,CAAC,OAAO;wBAAE,KAAK,MAAM,KAAK,IAAI,UAAU,CAAC,MAAM,EAAE,MAAM,CAAC;4BAAE,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,EAAE,KAAK,CAAC,CAAA;oBAC3G,GAAG,CAAC,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC,CAAA;gBACjC,CAAC;YACH,CAAC,EACD,CAAC,KAAc,EAAE,EAAE;gBACjB,KAAK,MAAM,GAAG,IAAI,IAAI;oBAAE,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;YAC3C,CAAC,CACF,CAAA;QACH,CAAC;KACF,CAAA;AACH,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@estiva-app/interop",
3
- "version": "0.95.0",
3
+ "version": "0.97.0",
4
4
  "description": "Render and act on another app's objects from its published NIP-89 manifest. The owner defines the projection; the consumer decides how it looks.",
5
5
  "license": "MIT",
6
6
  "type": "module",
package/src/activity.ts CHANGED
@@ -33,12 +33,14 @@
33
33
  * The rule takes any set of topics, so an Explore page orders every topic the
34
34
  * reader can open with the same read ({@link readTopicActivity}), and a live
35
35
  * app keeps it current with {@link watchTopicActivity}.
36
- * why: https://github.com/estiva-app/estiva-docs/blob/main/decisions/each-file-on-a-topic-keeps-its-own-place-and-look.md
36
+ *
37
+ * No decision record states this order of topics: the rule lives here, and
38
+ * writing it up as SPEC §6.14 text is on the project's Later list.
37
39
  */
38
- import { fileIdOf, MAX_FILTERS_PER_QUERY, type LiveRelay, type SignedEvent } from '@estiva-app/protocol'
40
+ import { fileIdOf, type LiveRelay, type SignedEvent } from '@estiva-app/protocol'
39
41
  import { isAbout, KIND_COMMENT } from '@estiva-app/conversation'
40
42
  import { folderOf, KIND_MESSAGE, type QueryFn } from './projection.js'
41
- import { answers, readAll, type ReadTopic } from './topics.js'
43
+ import { answeredByEach, answers, firstPages, nextPages, type ReadTopic } from './topics.js'
42
44
  import { LISTED_WATCH_REBUILD_MS, MAX_FILTERS_PER_REQ, openWhenKnown } from './listedConversations.js'
43
45
 
44
46
  const KIND_CHANGE = 1851
@@ -91,7 +93,7 @@ export interface TopicActivity {
91
93
  /** Each topic's team, as asked. */
92
94
  folders: Map<string, string | null>
93
95
  /**
94
- * Each file a topic lists directly (not a file under one), by address: the
96
+ * Each file a topic lists directly, and each file one level under one (ACT-2), by address: the
95
97
  * comments about it (`kind:1111`, on it as the fold reads it, never by a
96
98
  * crossed tag) that the read already fetched to time its topics, oldest
97
99
  * first. The read asks nothing more for them, so a file holds its newest
@@ -99,6 +101,12 @@ export interface TopicActivity {
99
101
  * appends each new one in place (TOPI-3).
100
102
  */
101
103
  comments: Map<string, SignedEvent[]>
104
+ /**
105
+ * Each file found under a listed one, by address: the file it sits directly under (its one `a`, §6.7), so a
106
+ * reader tells a listed project's issue from a page two levels down (ACT-2: what names you counts one level
107
+ * under a listed file). {@link watchTopicActivity} adds each new one in place.
108
+ */
109
+ parents: Map<string, string>
102
110
  }
103
111
 
104
112
  /** {@link ActivityTopic}s from {@link readTopics}' answer: each topic with its resolved list. */
@@ -175,62 +183,76 @@ function isOn(event: SignedEvent, address: string, folder: string | null): boole
175
183
  return tagValues(event, 'A')[0] === address
176
184
  }
177
185
 
178
- async function queryChunked(query: QueryFn, filters: readonly Record<string, unknown>[]): Promise<SignedEvent[]> {
179
- const runs: Promise<SignedEvent[]>[] = []
180
- for (let i = 0; i < filters.length; i += MAX_FILTERS_PER_QUERY) runs.push(query(filters.slice(i, i + MAX_FILTERS_PER_QUERY)))
181
- return (await Promise.all(runs)).flat()
182
- }
183
-
184
186
  /**
185
- * Every file under the given ones, any depth: a project's issues, a
186
- * document's pages, each by its root's `a` (§6.7). One request per level.
187
- * Each found file with its root's team and its root's time.
187
+ * The files under the given ones, a level at a time: a project's issues, a
188
+ * document's pages, each by its root's `a` (§6.7), to {@link MAX_DEPTH}
189
+ * levels. The caller sends {@link FilesUnder.asking} in its request (every
190
+ * one, however many: paged past the relay's cap rather than cut at it) and
191
+ * hands the answer to {@link FilesUnder.heard}, which says the files a level
192
+ * held once its last page is in, each with its root's team and time.
188
193
  */
189
- async function filesUnder(
190
- query: QueryFn,
191
- parents: readonly string[],
192
- ): Promise<{ parent: string; file: ActivityFile; at: number }[]> {
193
- const out: { parent: string; file: ActivityFile; at: number }[] = []
194
+ interface FilesUnder {
195
+ asking: (Record<string, unknown> & { limit: number })[]
196
+ heard(events: readonly SignedEvent[]): { parent: string; file: ActivityFile; at: number }[]
197
+ }
198
+
199
+ function filesUnder(parents: readonly string[]): FilesUnder {
194
200
  // Each file under one is answered once per parent, even when it is listed itself (on another topic, say);
195
201
  // each parent is walked once.
196
202
  const found = new Set<string>()
197
203
  let level = parents.filter((address) => CHILD_KINDS[kindOf(address) ?? -1])
198
204
  const walked = new Set(level)
199
- for (let depth = 0; depth < MAX_DEPTH && level.length > 0; depth++) {
205
+ let depth = 0
206
+ let roots = new Map<string, SignedEvent>()
207
+ const ask = (): (Record<string, unknown> & { limit: number })[] => {
208
+ if (depth >= MAX_DEPTH || level.length === 0) return []
200
209
  const kinds = [...new Set(level.flatMap((address) => CHILD_KINDS[kindOf(address)!]!))]
201
210
  const filters: Record<string, unknown>[] = []
202
211
  for (let i = 0; i < level.length; i += VALUES_PER_FILTER) filters.push({ kinds, '#a': level.slice(i, i + VALUES_PER_FILTER) })
203
- // Every one, however many: paged past the relay's cap rather than cut at it.
204
- const roots = await readAll(query, filters)
205
- const asked = new Set(level)
206
- const next: string[] = []
207
- // A replaceable root the relay still holds twice is one file: its newest names its team.
208
- const newest = new Map<string, { root: SignedEvent; parent: string }>()
209
- for (const root of roots) {
210
- const d = tagValues(root, 'd')[0]
211
- // A root's parent is its one `a` (§6.7): a later one is no parent.
212
- const parent = tagValues(root, 'a')[0]
213
- if (!d || !parent || !asked.has(parent) || !(CHILD_KINDS[kindOf(parent)!] ?? []).includes(root.kind)) continue
214
- const address = `${root.kind}:${root.pubkey}:${d}`
215
- const held = newest.get(address)
216
- if (!held || root.created_at > held.root.created_at) newest.set(address, { root, parent })
217
- }
218
- const perParent = new Map<string, number>()
219
- for (const [address, { root, parent }] of [...newest].sort((a, b) => b[1].root.created_at - a[1].root.created_at)) {
220
- const count = perParent.get(parent) ?? 0
221
- if (count >= MAX_CHILDREN) continue
222
- perParent.set(parent, count + 1)
223
- if (found.has(`${parent} ${address}`)) continue
224
- found.add(`${parent} ${address}`)
225
- out.push({ parent, file: { address, folder: folderOf(root) }, at: root.created_at })
226
- if (CHILD_KINDS[root.kind] && !walked.has(address)) {
227
- walked.add(address)
228
- next.push(address)
212
+ return firstPages(filters)
213
+ }
214
+ const walk: FilesUnder = {
215
+ asking: ask(),
216
+ heard(events) {
217
+ if (walk.asking.length === 0) return []
218
+ for (const event of events) if (walk.asking.some((filter) => answers(event, filter))) roots.set(event.id, event)
219
+ walk.asking = nextPages(walk.asking, events)
220
+ if (walk.asking.length > 0) return []
221
+ const out: { parent: string; file: ActivityFile; at: number }[] = []
222
+ const asked = new Set(level)
223
+ const next: string[] = []
224
+ // A replaceable root the relay still holds twice is one file: its newest names its team.
225
+ const newest = new Map<string, { root: SignedEvent; parent: string }>()
226
+ for (const root of roots.values()) {
227
+ const d = tagValues(root, 'd')[0]
228
+ // A root's parent is its one `a` (§6.7): a later one is no parent.
229
+ const parent = tagValues(root, 'a')[0]
230
+ if (!d || !parent || !asked.has(parent) || !(CHILD_KINDS[kindOf(parent)!] ?? []).includes(root.kind)) continue
231
+ const address = `${root.kind}:${root.pubkey}:${d}`
232
+ const held = newest.get(address)
233
+ if (!held || root.created_at > held.root.created_at) newest.set(address, { root, parent })
229
234
  }
230
- }
231
- level = next
235
+ const perParent = new Map<string, number>()
236
+ for (const [address, { root, parent }] of [...newest].sort((a, b) => b[1].root.created_at - a[1].root.created_at)) {
237
+ const count = perParent.get(parent) ?? 0
238
+ if (count >= MAX_CHILDREN) continue
239
+ perParent.set(parent, count + 1)
240
+ if (found.has(`${parent} ${address}`)) continue
241
+ found.add(`${parent} ${address}`)
242
+ out.push({ parent, file: { address, folder: folderOf(root) }, at: root.created_at })
243
+ if (CHILD_KINDS[root.kind] && !walked.has(address)) {
244
+ walked.add(address)
245
+ next.push(address)
246
+ }
247
+ }
248
+ level = next
249
+ depth++
250
+ roots = new Map()
251
+ walk.asking = ask()
252
+ return out
253
+ },
232
254
  }
233
- return out
255
+ return walk
234
256
  }
235
257
 
236
258
  /** One thing to find the newest counting event of: a filter, the file it is on, and the topics its answer is news for. */
@@ -246,12 +268,16 @@ interface Probe {
246
268
  * Each topic's newest activity (the rule above), for any set of topics the
247
269
  * reader holds — theirs, or every one they can open.
248
270
  *
249
- * Reads, whatever the number of topics: one request per level of files under
250
- * the listed ones (a project's issues, then each further level of pages),
251
- * then the newest few events of each topic's conversation and own changes and
252
- * of each file's changes and comments, every target one filter in the same
253
- * request (another per {@link MAX_FILTERS_PER_QUERY}). A target whose newest
254
- * events are all tidying is asked again from older ones, up to a few rounds.
271
+ * Reads, whatever the number of topics, one request a tick: the newest few
272
+ * events of each topic's conversation and own changes and of each listed
273
+ * file's changes and comments, every target one filter, beside the first
274
+ * level of files under the listed ones (a project's issues, a document's
275
+ * pages); then each tick the next level, the first page of each file the
276
+ * level before found, and every target still open, from older events. A
277
+ * target whose newest events are all tidying is asked again from older ones,
278
+ * up to a few pages. No filter names a team (`#h`), the only values a relay
279
+ * caps per request, so however many filters a tick holds it is one request:
280
+ * a topic list with one level of files under it is about four.
255
281
  *
256
282
  * Measured 9 Nov 2026 on production over the 47 topics the agent can read
257
283
  * (Miky is in 24 of them): about 1.9 MB in 7 requests, 1 s. Most of it is the
@@ -272,13 +298,14 @@ export async function readTopicActivity(
272
298
  const files = new Map<string, ActivityFile[]>()
273
299
  const folders = new Map<string, string | null>()
274
300
  const comments = new Map<string, SignedEvent[]>()
301
+ const parents = new Map<string, string>()
275
302
  for (const topic of topics) {
276
303
  at.set(topic.address, topic.createdAt ? capped(topic.createdAt) : 0)
277
304
  files.set(topic.address, [...topic.files])
278
305
  folders.set(topic.address, topic.folder ?? null)
279
306
  for (const file of topic.files) if (!comments.has(file.address)) comments.set(file.address, [])
280
307
  }
281
- if (topics.length === 0) return { at, files, folders, comments }
308
+ if (topics.length === 0) return { at, files, folders, comments, parents }
282
309
  const bump = (topic: string, when: number) => {
283
310
  if (capped(when) > (at.get(topic) ?? 0)) at.set(topic, capped(when))
284
311
  }
@@ -315,65 +342,88 @@ export async function readTopicActivity(
315
342
  }
316
343
  for (const [file, on] of listed) first.push(...fileProbes({ address: file, folder: folderOfFile.get(file) ?? null }, [...on], true))
317
344
 
318
- // The topics and their listed files are read while the files under them are found, and those as soon as they are.
319
- const deep = async () => {
320
- const under = await filesUnder(query, [...listed.keys()])
321
- // Parents come before their children in `under`, so a page two levels down inherits its topics.
322
- for (const { parent, file, at: created } of under) {
345
+ // One request a tick: the next level of files under the listed ones, the
346
+ // first page of each file found by the level before, and every probe still
347
+ // open, a page further back. No filter here names a team (`#h`), the only
348
+ // values a relay caps per request, so none is split across requests.
349
+ const under = filesUnder([...listed.keys()])
350
+ // Each file found under a listed one, as last found, and the topics it has been read for.
351
+ const asFound = new Map<string, ActivityFile>()
352
+ const readFor = new Map<string, Set<string>>()
353
+ type Open = { probe: Probe; filter: Record<string, unknown>; round: number }
354
+ const opened = (probes: readonly Probe[]): Open[] => probes.map((probe) => ({ probe, filter: { ...probe.filter, limit: FIRST_PAGE }, round: 0 }))
355
+ let open = opened(first)
356
+ while (under.asking.length > 0 || open.length > 0) {
357
+ const asked = [...under.asking, ...open.map((o) => o.filter)]
358
+ const events = await query(asked)
359
+ const found = under.heard(events)
360
+ // Parents come before their children in a level, so a page two levels down inherits its topics.
361
+ for (const { parent, file, at: created } of found) {
362
+ parents.set(file.address, parent)
323
363
  for (const topic of topicsOf.get(parent) ?? []) {
324
364
  if (!topicsOf.get(file.address)?.has(topic)) files.get(topic)!.push(file)
325
365
  onTopic(file.address, topic)
326
366
  // A new issue filed in a listed project is news.
327
367
  bump(topic, created)
328
368
  }
369
+ asFound.set(file.address, file)
329
370
  }
371
+ open = stillOpen(open, events)
330
372
  const then: Probe[] = []
331
- const asFound = new Map(under.map((found) => [found.file.address, found.file]))
332
373
  for (const [file, target] of asFound) {
333
- // Only for the topics it was not already read for as a listed file.
334
- const fresh = [...(topicsOf.get(file) ?? [])].filter((topic) => !listed.get(file)?.has(topic))
335
- if (fresh.length > 0) then.push(...fileProbes(target, fresh))
374
+ // Only for the topics it was not already read for, as a listed file or found before.
375
+ const done = readFor.get(file) ?? new Set(listed.get(file))
376
+ const fresh = [...(topicsOf.get(file) ?? [])].filter((topic) => !done.has(topic))
377
+ if (fresh.length === 0) continue
378
+ for (const topic of fresh) done.add(topic)
379
+ readFor.set(file, done)
380
+ // One level under a listed file, its comments are kept too: what names the reader there lights the topic (ACT-2).
381
+ const kept = listed.has(parents.get(file) ?? '') && !listed.has(file)
382
+ if (kept && !comments.has(file)) comments.set(file, [])
383
+ then.push(...fileProbes(target, fresh, kept))
336
384
  }
337
- await newestOf(then)
385
+ open.push(...opened(then))
338
386
  }
339
- await Promise.all([deep(), newestOf(first)])
340
387
  for (const list of comments.values()) list.sort(oldestFirst)
341
- return { at, files, folders, comments }
388
+ return { at, files, folders, comments, parents }
342
389
 
343
- /** Per probe, the newest event that counts for one of its topics. Read in rounds; each round asks only what is still open. */
344
- async function newestOf(probes: readonly Probe[]): Promise<void> {
345
- let open = probes.map((probe) => ({ probe, filter: { ...probe.filter, limit: FIRST_PAGE } as Record<string, unknown> }))
346
- for (let round = 0; round < MAX_ROUNDS && open.length > 0; round++) {
347
- const events = await queryChunked(query, open.map((o) => o.filter))
348
- const still: typeof open = []
349
- for (const o of open) {
350
- const page = events.filter((event) => answers(event, o.filter)).sort((a, b) => b.created_at - a.created_at)
351
- if (o.probe.keepsComments) {
352
- const kept = comments.get(o.probe.target.address)
353
- for (const event of page) {
354
- if (!kept || event.kind !== KIND_COMMENT || !isOn(event, o.probe.target.address, o.probe.target.folder)) continue
355
- if (!kept.some((held) => held.id === event.id)) kept.push(event)
356
- }
357
- }
358
- const left = new Set(o.probe.topics)
390
+ /**
391
+ * Per probe, the newest event that counts for one of its topics, from the
392
+ * page this tick answered it: the probes still open, each a page further
393
+ * back, up to {@link MAX_ROUNDS} pages each.
394
+ */
395
+ function stillOpen(open: readonly Open[], events: readonly SignedEvent[]): Open[] {
396
+ const still: Open[] = []
397
+ // The answer indexed once a tick, not filtered and sorted once per probe: a listed project's issues make thousands.
398
+ const pageOf = answeredByEach(events)
399
+ for (const o of open) {
400
+ // Only what this filter alone would have been answered: the request holds every other probe's answer too.
401
+ const page = pageOf(o.filter)
402
+ if (o.probe.keepsComments) {
403
+ const kept = comments.get(o.probe.target.address)
359
404
  for (const event of page) {
360
- if (!isOn(event, o.probe.target.address, o.probe.target.folder)) continue
361
- for (const topic of [...left]) {
362
- if (!countsAsActivity(event, topic)) continue
363
- bump(topic, event.created_at)
364
- left.delete(topic)
365
- }
366
- if (left.size === 0) break
405
+ if (!kept || event.kind !== KIND_COMMENT || !isOn(event, o.probe.target.address, o.probe.target.folder)) continue
406
+ if (!kept.some((held) => held.id === event.id)) kept.push(event)
407
+ }
408
+ }
409
+ const left = new Set(o.probe.topics)
410
+ for (const event of page) {
411
+ if (!isOn(event, o.probe.target.address, o.probe.target.folder)) continue
412
+ for (const topic of [...left]) {
413
+ if (!countsAsActivity(event, topic)) continue
414
+ bump(topic, event.created_at)
415
+ left.delete(topic)
367
416
  }
368
- const limit = o.filter.limit as number
369
- if (left.size === 0 || page.length < limit) continue
370
- const until = page[page.length - 1]!.created_at
371
- // A second holding a whole page on its own: ask a bigger page of it rather than step past it.
372
- const same = until === (o.filter.until as number | undefined)
373
- still.push({ probe: { ...o.probe, topics: [...left] }, filter: { ...o.probe.filter, until, limit: same ? limit * 4 : limit * 2 } })
417
+ if (left.size === 0) break
374
418
  }
375
- open = still
419
+ const limit = o.filter.limit as number
420
+ if (left.size === 0 || page.length < limit || o.round + 1 >= MAX_ROUNDS) continue
421
+ const until = page[page.length - 1]!.created_at
422
+ // A second holding a whole page on its own: ask a bigger page of it rather than step past it.
423
+ const same = until === (o.filter.until as number | undefined)
424
+ still.push({ probe: { ...o.probe, topics: [...left] }, filter: { ...o.probe.filter, until, limit: same ? limit * 4 : limit * 2 }, round: o.round + 1 })
376
425
  }
426
+ return still
377
427
  }
378
428
  }
379
429
 
@@ -515,9 +565,14 @@ export function watchTopicActivity(
515
565
  const parent = d && first && byAddress.has(first) && CHILD_KINDS[kindOf(first) ?? -1]?.includes(event.kind) ? first : undefined
516
566
  if (parent && d) {
517
567
  const file = { address: `${event.kind}:${event.pubkey}:${d}`, folder: folderOf(event) }
568
+ // `??=`: an activity built before `parents` existed (an older read, a stub) is given one rather than thrown on.
569
+ const parents = (activity.parents ??= new Map())
518
570
  const fresh = !byAddress.has(file.address)
519
571
  const listen = fresh && (children.get(parent) ?? 0) < MAX_CHILDREN
520
572
  if (listen) children.set(parent, (children.get(parent) ?? 0) + 1)
573
+ if (listen && !parents.has(file.address)) parents.set(file.address, parent)
574
+ // A new file one level under a listed one: its comments are kept from now on, as the read keeps them.
575
+ if (listen && activity.comments.has(parent) && !parents.has(parent) && !activity.comments.has(file.address)) activity.comments.set(file.address, [])
521
576
  for (const topic of byAddress.get(parent)!) {
522
577
  if (listen) {
523
578
  activity.files.get(topic)?.push(file)
package/src/index.ts CHANGED
@@ -224,6 +224,13 @@ export {
224
224
  type ActivityWatch,
225
225
  } from './activity.js'
226
226
 
227
+ /**
228
+ * Your topics alone, lean: the topics the viewer is in, not archived, muted or
229
+ * a space, newest activity first — what a sidebar draws, in about ten
230
+ * requests where {@link readTopics} and {@link readTopicActivity} take twenty.
231
+ */
232
+ export { readYourTopics, type YourTopicsRead } from './yourTopics.js'
233
+
227
234
  /**
228
235
  * A topic's All: the conversations of every file on its list, read in one go
229
236
  * and kept live, each file's events ready for the shared fold (SPEC §11.8).