@yaks/inbox 0.0.0-stage → 0.2.36

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.
Files changed (88) hide show
  1. package/LICENSE +202 -0
  2. package/Person.tsx +275 -0
  3. package/Project.tsx +83 -0
  4. package/README.md +311 -2
  5. package/_dist/actions-browser.d.ts +8 -0
  6. package/_dist/actions-browser.d.ts.map +1 -0
  7. package/_dist/actions.d.ts +22 -0
  8. package/_dist/actions.d.ts.map +1 -0
  9. package/_dist/asks.d.ts +15 -0
  10. package/_dist/asks.d.ts.map +1 -0
  11. package/_dist/graph.d.ts +12 -0
  12. package/_dist/graph.d.ts.map +1 -0
  13. package/_dist/letters.d.ts +24 -0
  14. package/_dist/letters.d.ts.map +1 -0
  15. package/_dist/mail.d.ts +4 -0
  16. package/_dist/mail.d.ts.map +1 -0
  17. package/_dist/mod.d.ts +7 -0
  18. package/_dist/mod.d.ts.map +1 -0
  19. package/_dist/options.d.ts +18 -0
  20. package/_dist/options.d.ts.map +1 -0
  21. package/_dist/queries.d.ts +23 -0
  22. package/_dist/queries.d.ts.map +1 -0
  23. package/_dist/read.d.ts +18 -0
  24. package/_dist/read.d.ts.map +1 -0
  25. package/_dist/reader.d.ts +25 -0
  26. package/_dist/reader.d.ts.map +1 -0
  27. package/_dist/service.d.ts +8 -0
  28. package/_dist/service.d.ts.map +1 -0
  29. package/_dist/threads.d.ts +33 -0
  30. package/_dist/threads.d.ts.map +1 -0
  31. package/_dist/tools.d.ts +12 -0
  32. package/_dist/tools.d.ts.map +1 -0
  33. package/_dist/vocab.d.ts +5 -0
  34. package/_dist/vocab.d.ts.map +1 -0
  35. package/actions-browser.js +165 -0
  36. package/actions-browser.js.map +1 -0
  37. package/actions-browser.ts +158 -0
  38. package/actions.js +34 -0
  39. package/actions.js.map +1 -0
  40. package/actions.ts +53 -0
  41. package/asks.js +76 -0
  42. package/asks.js.map +1 -0
  43. package/asks.ts +94 -0
  44. package/deno.json +1 -0
  45. package/front.js +41 -0
  46. package/front.js.map +1 -0
  47. package/front.json +68 -0
  48. package/front.ts +38 -0
  49. package/graph.js +176 -0
  50. package/graph.js.map +1 -0
  51. package/graph.ts +182 -0
  52. package/inspect.tsx +22 -0
  53. package/letters.js +233 -0
  54. package/letters.js.map +1 -0
  55. package/letters.ts +325 -0
  56. package/mail.js +122 -0
  57. package/mail.js.map +1 -0
  58. package/mail.ts +148 -0
  59. package/mod.js +11 -0
  60. package/mod.js.map +1 -0
  61. package/mod.ts +24 -0
  62. package/options.js +15 -0
  63. package/options.js.map +1 -0
  64. package/options.ts +42 -0
  65. package/package.json +76 -4
  66. package/queries.js +240 -0
  67. package/queries.js.map +1 -0
  68. package/queries.ts +302 -0
  69. package/read.js +453 -0
  70. package/read.js.map +1 -0
  71. package/read.ts +476 -0
  72. package/reader.js +105 -0
  73. package/reader.js.map +1 -0
  74. package/reader.ts +147 -0
  75. package/service.js +29 -0
  76. package/service.js.map +1 -0
  77. package/service.ts +45 -0
  78. package/threads.js +182 -0
  79. package/threads.js.map +1 -0
  80. package/threads.ts +276 -0
  81. package/tools.js +101 -0
  82. package/tools.js.map +1 -0
  83. package/tools.ts +103 -0
  84. package/views.tsx +30 -0
  85. package/vocab.js +12 -0
  86. package/vocab.js.map +1 -0
  87. package/vocab.json +242 -0
  88. package/vocab.ts +11 -0
package/queries.js ADDED
@@ -0,0 +1,240 @@
1
+ import { joined, line, sub } from "@yaks/query";
2
+ import { threadOf } from "./threads.js";
3
+ /** A vocabulary is data: consumers provide only its component/property lookup. */ export let words = (vocab)=>{
4
+ let known = new Map();
5
+ return (path)=>{
6
+ let yes = known.get(path);
7
+ if (yes !== undefined) return yes;
8
+ let [name, prop] = path.split('.');
9
+ yes = path == 'entity.eid' || !!vocab.comp(name) && (!prop || vocab.props(name).includes(prop));
10
+ known.set(path, yes);
11
+ return yes;
12
+ };
13
+ };
14
+ let NONE = line``;
15
+ // Each set of values is sorted, so the same set always reads as one line.
16
+ let select = (prop, items)=>{
17
+ let v = [
18
+ ...new Set([
19
+ ...items
20
+ ].filter((i)=>!!i))
21
+ ].sort();
22
+ return v.length ? line`.${prop}=${v}` : NONE;
23
+ };
24
+ // Lines one after another, each joined to the one before by `by`.
25
+ let chain = (all, by)=>all.reduce((a, l)=>a ? by(a, l) : l, NONE);
26
+ let bar = (all)=>chain(all, (a, b)=>line`${sub(a)}|${sub(b)}`);
27
+ let union = (arms, has)=>joined(arms.filter((q)=>q && has(q.slice(1).split(/[&=]/)[0])), '|');
28
+ // Explicit optional components preserve sparse host vocabularies, and include
29
+ // message bodies for said/received search. No unrelated delivery job payloads.
30
+ let fields = [
31
+ 'doc',
32
+ 'conversation',
33
+ 'content',
34
+ 'comment',
35
+ 'entry',
36
+ 'output',
37
+ 'notice',
38
+ 'reasoning',
39
+ 'result',
40
+ 'exception',
41
+ 'refusal',
42
+ 'ask',
43
+ 'call',
44
+ 'stop',
45
+ 'commit',
46
+ 'prompt',
47
+ 'session',
48
+ 'project',
49
+ 'design',
50
+ 'task',
51
+ 'decision',
52
+ 'filed',
53
+ 'mail',
54
+ 'mail_notice',
55
+ 'notified',
56
+ 'knock',
57
+ 'deliver',
58
+ 'notification',
59
+ 'bug',
60
+ 'created',
61
+ 'opened',
62
+ 'archived',
63
+ 'completed',
64
+ 'cancelled',
65
+ 'failed',
66
+ 'blocked',
67
+ 'broken',
68
+ 'resolved',
69
+ 'regressed',
70
+ 'decided',
71
+ 'exit',
72
+ 'requires',
73
+ 'edge'
74
+ ];
75
+ let read = (q, has)=>q ? fields.filter(has).reduce((l, s)=>line`${sub(l)}&?${s}`, line`(${sub(q)})`) : NONE;
76
+ /** First read: direct address, assigned work, watched work and attention,
77
+ * of only the components `within` names when it names any. */ export let candidates = (who, has = ()=>true, within)=>{
78
+ let targets = [
79
+ who.actor,
80
+ who.session,
81
+ ...who.claims ?? [],
82
+ ...who.watching ?? []
83
+ ];
84
+ let authored = select('created.by', [
85
+ who.actor
86
+ ]);
87
+ let kinds = [
88
+ 'doc',
89
+ 'conversation',
90
+ 'task',
91
+ 'session',
92
+ 'design',
93
+ 'bug',
94
+ 'mail',
95
+ 'mail_notice',
96
+ 'comment',
97
+ 'entry'
98
+ ].filter(has).map((name)=>line`.${name}`);
99
+ let kind = bar(kinds);
100
+ if (authored && kind) authored = line`${sub(authored)}&(${sub(kind)})`;
101
+ let arms = union([
102
+ select('comment.target', targets),
103
+ select('deliver.to', [
104
+ who.actor,
105
+ who.session,
106
+ who.scope
107
+ ]),
108
+ select('notification.to', [
109
+ who.actor,
110
+ who.session,
111
+ who.scope
112
+ ]),
113
+ select('mail.to', who.addrs ?? []),
114
+ select('mail.from', who.addrs ?? []),
115
+ select('mail.target', [
116
+ who.scope,
117
+ who.session,
118
+ ...who.watching ?? []
119
+ ]),
120
+ select('filed.assignee', [
121
+ who.actor
122
+ ]),
123
+ authored,
124
+ select('entity.eid', who.attention?.keys() ?? []),
125
+ select('decided.by', [
126
+ who.actor
127
+ ]),
128
+ select('session.actor', [
129
+ who.actor
130
+ ]),
131
+ select('entity.eid', who.watching ?? []),
132
+ who.actor && who.operator ? line`.bug` : NONE,
133
+ who.actor && who.operator ? line`.decision` : NONE
134
+ ], has);
135
+ if (!within || !arms) return read(arms, has);
136
+ let held = union(within.map((name)=>line`.${name}`), has);
137
+ return held ? read(line`(${sub(arms)})&(${sub(held)})`, has) : NONE;
138
+ };
139
+ // The lane policy consumes human inputs, visible answers and state changes.
140
+ // Model attempts, tools, reasoning and passive notices do not enter messages,
141
+ // latest, activity or attention. Text/direction search still sees all words.
142
+ let activityEntries = (has)=>{
143
+ let input = has('content') ? [
144
+ 'output',
145
+ 'prompt',
146
+ 'notice',
147
+ 'result',
148
+ 'refusal',
149
+ 'exception',
150
+ 'ask',
151
+ 'call'
152
+ ].filter(has).reduce((l, name)=>line`${sub(l)}&!${name}`, line`.content`) : NONE;
153
+ let output = has('output') ? has('reasoning') ? line`.output&!reasoning` : line`.output` : NONE;
154
+ return joined([
155
+ input,
156
+ output,
157
+ ...[
158
+ 'exception',
159
+ 'refusal',
160
+ 'stop'
161
+ ].filter(has).map((name)=>line`.${name}`)
162
+ ].filter(Boolean), '|');
163
+ };
164
+ /** Second read: complete roots and policy-relevant conversation. Searches
165
+ * additionally read words that the attention policy does not otherwise use. */ export let discussion = (rows, has = ()=>true, search = {})=>{
166
+ let targets = rows.filter((r)=>!r.comps.edge && !r.comps.subscription).map((r)=>threadOf(r));
167
+ let entries = select('entry.session', targets);
168
+ let activity = activityEntries(has);
169
+ if (entries && !search.text?.trim() && !search.direction && activity) {
170
+ entries = line`${sub(entries)}&(${sub(activity)})`;
171
+ }
172
+ return read(union([
173
+ select('entity.eid', [
174
+ ...targets,
175
+ ...rows.map((r)=>String(r.comps.mail?.reply_to ?? ''))
176
+ ]),
177
+ select('mail.message_id', rows.map((r)=>String(r.comps.mail?.reply_to ?? ''))),
178
+ select('comment.target', targets),
179
+ select('mail.target', targets),
180
+ select('knock.target', targets),
181
+ entries,
182
+ select('commit.target', targets)
183
+ ], has), has);
184
+ };
185
+ /** Third/fourth reads: requires edges and their dependent task state. */ export let requirements = (rows, has = ()=>true)=>has('requires') && has('edge') ? read(rows.length ? line`${sub(select('edge.to', rows.map((r)=>threadOf(r))))}&.requires` : NONE, has) : NONE;
186
+ export let dependents = (edges, has = ()=>true)=>read(select('entity.eid', edges.map((r)=>String(r.comps.edge?.from ?? ''))), has);
187
+ const BUDGET = 48 * 1024;
188
+ /** Keep derived inbox reads below the socket's message budget without dropping
189
+ * threads. Each half asks the same query over a disjoint set of input rows;
190
+ * callers union the answers and wait for every half before claiming readiness.
191
+ */ export let boundedReads = (rows, read)=>{
192
+ let out = new Set();
193
+ let split = (part)=>{
194
+ let query = read(part);
195
+ if (!query) return;
196
+ // Leave room for the subscription envelope, including escaped query text.
197
+ // A character is at most three bytes, so a short query needs no count.
198
+ let text = JSON.stringify(query);
199
+ if (text.length * 3 <= BUDGET || new TextEncoder().encode(text).length <= BUDGET) {
200
+ out.add(query);
201
+ return;
202
+ }
203
+ if (part.length < 2) throw new RangeError('inbox row exceeds query budget');
204
+ let middle = Math.floor(part.length / 2);
205
+ split(part.slice(0, middle));
206
+ split(part.slice(middle));
207
+ };
208
+ // Identity-based buckets keep unrelated reads byte-for-byte stable when a
209
+ // root is added or removed. Array-midpoint splitting shifts every boundary.
210
+ let buckets = Array.from({
211
+ length: 16
212
+ }, ()=>[]);
213
+ let bucket = (id)=>{
214
+ let hash = 2166136261;
215
+ for (let c of id)hash = Math.imul(hash ^ c.charCodeAt(0), 16777619);
216
+ return (hash >>> 0) % buckets.length;
217
+ };
218
+ for (let row of rows.toSorted((a, b)=>a.eid.localeCompare(b.eid))){
219
+ buckets[bucket(row.eid)].push(row);
220
+ }
221
+ for (let part of buckets)if (part.length) split(part);
222
+ return [
223
+ ...out
224
+ ];
225
+ };
226
+ /** One input per thread before splitting derived reads. Repeating a session
227
+ * across disjoint batches would download that session's whole discussion in
228
+ * each batch, even though the caller unions the same rows afterward. */ export let threadRoots = (rows)=>{
229
+ let roots = new Map();
230
+ for (let row of rows){
231
+ let id = threadOf(row);
232
+ let prior = roots.get(id);
233
+ if (!prior || row.eid == id) roots.set(id, row);
234
+ }
235
+ return [
236
+ ...roots.values()
237
+ ];
238
+ };
239
+ export let summaryQuery = (actor, search = {}, thread)=>line`.inbox_summary.actor=${actor}${sub(search.text ? line`&.inbox_summary.text=${search.text}` : NONE)}${sub(search.direction ? line`&.inbox_summary.direction=${search.direction}` : NONE)}${sub(search.all ? line`&.inbox_summary.all=true` : NONE)}${sub(search.lane ? line`&.inbox_summary.lane=${search.lane}` : NONE)}${sub(thread ? line`&.inbox_summary.thread=${thread}` : NONE)}`;
240
+ //# sourceMappingURL=queries.js.map
package/queries.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"queries.js","sources":["./queries.ts"],"names":[],"mappings":"AAAA,SAAqB,MAAM,EAAE,IAAI,EAAE,GAAG,2BAAgC;AAGtE,SAAS,QAAQ,uBAAsB;A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AAG,IAAI,IAAI,MAAM,GAAG,CAAC,IAAI;EAC7C;EACA,OAAO;OAAI,MAAM,MAAM;GAAG;AAC5B,EAAC;AAED,OAAO,IAAI,eAAe,CACxB,OACA,SAAiB,CAAC,CAAC,EACnB,SAEA,IAAI,CAAC,qBAAqB,EAAE,MAAM,EAChC,IAAI,OAAO,IAAI,GAAG,IAAI,CAAC,qBAAqB,EAAE,OAAO,IAAI,CAAC,CAAC,GAAG,MAC/D,EACC,IACE,OAAO,SAAS,GACZ,IAAI,CAAC,0BAA0B,EAAE,OAAO,SAAS,CAAC,CAAC,GACnD,MAEP,EAAE,IAAI,OAAO,GAAG,GAAG,IAAI,CAAC,wBAAwB,CAAC,GAAG,MAAM,EACzD,IAAI,OAAO,IAAI,GAAG,IAAI,CAAC,qBAAqB,EAAE,OAAO,IAAI,CAAC,CAAC,GAAG,MAC/D,EAAE,IAAI,SAAS,IAAI,CAAC,uBAAuB,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,CAAA"}
package/queries.ts ADDED
@@ -0,0 +1,302 @@
1
+ import { type Built, joined, line, sub } from '@yaks/query'
2
+ import { type Reader, type Row } from './reader.js'
3
+ import type { Search } from './threads.js'
4
+ import { threadOf } from './threads.js'
5
+
6
+ /** Optional words are queried only when the host declares them. */
7
+ export type Words = (path: string) => boolean
8
+ /** A vocabulary is data: consumers provide only its component/property lookup. */
9
+ export let words = (
10
+ vocab: {
11
+ comp: (name: string) => unknown
12
+ props: (name: string) => string[]
13
+ },
14
+ ): Words => {
15
+ let known = new Map<string, boolean>()
16
+ return (path) => {
17
+ let yes = known.get(path)
18
+ if (yes !== undefined) return yes
19
+ let [name, prop] = path.split('.')
20
+ yes = path == 'entity.eid' ||
21
+ (!!vocab.comp(name) && (!prop || vocab.props(name).includes(prop)))
22
+ known.set(path, yes)
23
+ return yes
24
+ }
25
+ }
26
+ let NONE = line``
27
+ // Each set of values is sorted, so the same set always reads as one line.
28
+ let select = (prop: string, items: Iterable<string | undefined>): Built => {
29
+ let v = [...new Set([...items].filter((i): i is string => !!i))].sort()
30
+ return v.length ? line`.${prop}=${v}` : NONE
31
+ }
32
+ // Lines one after another, each joined to the one before by `by`.
33
+ let chain = (all: Built[], by: (a: Built, b: Built) => Built): Built =>
34
+ all.reduce<Built>((a, l) => a ? by(a, l) : l, NONE)
35
+ let bar = (all: Built[]) => chain(all, (a, b) => line`${sub(a)}|${sub(b)}`)
36
+ let union = (arms: Built[], has: Words) =>
37
+ joined(arms.filter((q) => q && has(q.slice(1).split(/[&=]/)[0])), '|')
38
+ // Explicit optional components preserve sparse host vocabularies, and include
39
+ // message bodies for said/received search. No unrelated delivery job payloads.
40
+ let fields = [
41
+ 'doc',
42
+ 'conversation',
43
+ 'content',
44
+ 'comment',
45
+ 'entry',
46
+ 'output',
47
+ 'notice',
48
+ 'reasoning',
49
+ 'result',
50
+ 'exception',
51
+ 'refusal',
52
+ 'ask',
53
+ 'call',
54
+ 'stop',
55
+ 'commit',
56
+ 'prompt',
57
+ 'session',
58
+ 'project',
59
+ 'design',
60
+ 'task',
61
+ 'decision',
62
+ 'filed',
63
+ 'mail',
64
+ 'mail_notice',
65
+ 'notified',
66
+ 'knock',
67
+ 'deliver',
68
+ 'notification',
69
+ 'bug',
70
+ 'created',
71
+ 'opened',
72
+ 'archived',
73
+ 'completed',
74
+ 'cancelled',
75
+ 'failed',
76
+ 'blocked',
77
+ 'broken',
78
+ 'resolved',
79
+ 'regressed',
80
+ 'decided',
81
+ 'exit',
82
+ 'requires',
83
+ 'edge',
84
+ ]
85
+ let read = (q: Built, has: Words): Built =>
86
+ q
87
+ ? fields.filter(has).reduce<Built>(
88
+ (l, s) => line`${sub(l)}&?${s}`,
89
+ line`(${sub(q)})`,
90
+ )
91
+ : NONE
92
+
93
+ /** First read: direct address, assigned work, watched work and attention,
94
+ * of only the components `within` names when it names any. */
95
+ export let candidates = (
96
+ who: Reader,
97
+ has: Words = () => true,
98
+ within?: string[],
99
+ ): Built => {
100
+ let targets = [
101
+ who.actor,
102
+ who.session,
103
+ ...who.claims ?? [],
104
+ ...who.watching ?? [],
105
+ ]
106
+ let authored = select('created.by', [who.actor])
107
+ let kinds = [
108
+ 'doc',
109
+ 'conversation',
110
+ 'task',
111
+ 'session',
112
+ 'design',
113
+ 'bug',
114
+ 'mail',
115
+ 'mail_notice',
116
+ 'comment',
117
+ 'entry',
118
+ ].filter(has).map((name) => line`.${name}`)
119
+ let kind = bar(kinds)
120
+ if (authored && kind) authored = line`${sub(authored)}&(${sub(kind)})`
121
+ let arms = union([
122
+ select('comment.target', targets),
123
+ select('deliver.to', [who.actor, who.session, who.scope]),
124
+ select('notification.to', [who.actor, who.session, who.scope]),
125
+ select('mail.to', who.addrs ?? []),
126
+ select('mail.from', who.addrs ?? []),
127
+ select('mail.target', [who.scope, who.session, ...who.watching ?? []]),
128
+ select('filed.assignee', [who.actor]),
129
+ authored,
130
+ select('entity.eid', who.attention?.keys() ?? []),
131
+ select('decided.by', [who.actor]),
132
+ select('session.actor', [who.actor]),
133
+ select('entity.eid', who.watching ?? []),
134
+ who.actor && who.operator ? line`.bug` : NONE,
135
+ who.actor && who.operator ? line`.decision` : NONE,
136
+ ], has)
137
+ if (!within || !arms) return read(arms, has)
138
+ let held = union(within.map((name) => line`.${name}`), has)
139
+ return held ? read(line`(${sub(arms)})&(${sub(held)})`, has) : NONE
140
+ }
141
+
142
+ // The lane policy consumes human inputs, visible answers and state changes.
143
+ // Model attempts, tools, reasoning and passive notices do not enter messages,
144
+ // latest, activity or attention. Text/direction search still sees all words.
145
+ let activityEntries = (has: Words): Built => {
146
+ let input = has('content')
147
+ ? [
148
+ 'output',
149
+ 'prompt',
150
+ 'notice',
151
+ 'result',
152
+ 'refusal',
153
+ 'exception',
154
+ 'ask',
155
+ 'call',
156
+ ]
157
+ .filter(has).reduce<Built>(
158
+ (l, name) => line`${sub(l)}&!${name}`,
159
+ line`.content`,
160
+ )
161
+ : NONE
162
+ let output = has('output')
163
+ ? has('reasoning') ? line`.output&!reasoning` : line`.output`
164
+ : NONE
165
+ return joined(
166
+ [
167
+ input,
168
+ output,
169
+ ...['exception', 'refusal', 'stop'].filter(has).map((name) =>
170
+ line`.${name}`
171
+ ),
172
+ ].filter(Boolean),
173
+ '|',
174
+ )
175
+ }
176
+
177
+ /** Second read: complete roots and policy-relevant conversation. Searches
178
+ * additionally read words that the attention policy does not otherwise use. */
179
+ export let discussion = (
180
+ rows: Row[],
181
+ has: Words = () => true,
182
+ search: Search = {},
183
+ ): Built => {
184
+ let targets = rows.filter((r) => !r.comps.edge && !r.comps.subscription).map(
185
+ (r) => threadOf(r),
186
+ )
187
+ let entries = select('entry.session', targets)
188
+ let activity = activityEntries(has)
189
+ if (entries && !search.text?.trim() && !search.direction && activity) {
190
+ entries = line`${sub(entries)}&(${sub(activity)})`
191
+ }
192
+ return read(
193
+ union([
194
+ select('entity.eid', [
195
+ ...targets,
196
+ ...rows.map((r) => String(r.comps.mail?.reply_to ?? '')),
197
+ ]),
198
+ select(
199
+ 'mail.message_id',
200
+ rows.map((r) => String(r.comps.mail?.reply_to ?? '')),
201
+ ),
202
+ select('comment.target', targets),
203
+ select('mail.target', targets),
204
+ select('knock.target', targets),
205
+ entries,
206
+ select('commit.target', targets),
207
+ ], has),
208
+ has,
209
+ )
210
+ }
211
+
212
+ /** Third/fourth reads: requires edges and their dependent task state. */
213
+ export let requirements = (rows: Row[], has: Words = () => true): Built =>
214
+ has('requires') && has('edge')
215
+ ? read(
216
+ rows.length
217
+ ? line`${
218
+ sub(select('edge.to', rows.map((r) => threadOf(r))))
219
+ }&.requires`
220
+ : NONE,
221
+ has,
222
+ )
223
+ : NONE
224
+ export let dependents = (edges: Row[], has: Words = () => true): Built =>
225
+ read(
226
+ select('entity.eid', edges.map((r) => String(r.comps.edge?.from ?? ''))),
227
+ has,
228
+ )
229
+
230
+ const BUDGET = 48 * 1024
231
+
232
+ /** Keep derived inbox reads below the socket's message budget without dropping
233
+ * threads. Each half asks the same query over a disjoint set of input rows;
234
+ * callers union the answers and wait for every half before claiming readiness.
235
+ */
236
+ export let boundedReads = (
237
+ rows: Row[],
238
+ read: (rows: Row[]) => Built,
239
+ ): Built[] => {
240
+ let out = new Set<Built>()
241
+ let split = (part: Row[]) => {
242
+ let query = read(part)
243
+ if (!query) return
244
+ // Leave room for the subscription envelope, including escaped query text.
245
+ // A character is at most three bytes, so a short query needs no count.
246
+ let text = JSON.stringify(query)
247
+ if (
248
+ text.length * 3 <= BUDGET ||
249
+ new TextEncoder().encode(text).length <= BUDGET
250
+ ) {
251
+ out.add(query)
252
+ return
253
+ }
254
+ if (part.length < 2) throw new RangeError('inbox row exceeds query budget')
255
+ let middle = Math.floor(part.length / 2)
256
+ split(part.slice(0, middle))
257
+ split(part.slice(middle))
258
+ }
259
+ // Identity-based buckets keep unrelated reads byte-for-byte stable when a
260
+ // root is added or removed. Array-midpoint splitting shifts every boundary.
261
+ let buckets: Row[][] = Array.from({ length: 16 }, () => [])
262
+ let bucket = (id: string) => {
263
+ let hash = 2166136261
264
+ for (let c of id) hash = Math.imul(hash ^ c.charCodeAt(0), 16777619)
265
+ return (hash >>> 0) % buckets.length
266
+ }
267
+ for (let row of rows.toSorted((a, b) => a.eid.localeCompare(b.eid))) {
268
+ buckets[bucket(row.eid)].push(row)
269
+ }
270
+ for (let part of buckets) if (part.length) split(part)
271
+ return [...out]
272
+ }
273
+
274
+ /** One input per thread before splitting derived reads. Repeating a session
275
+ * across disjoint batches would download that session's whole discussion in
276
+ * each batch, even though the caller unions the same rows afterward. */
277
+ export let threadRoots = (rows: Row[]): Row[] => {
278
+ let roots = new Map<string, Row>()
279
+ for (let row of rows) {
280
+ let id = threadOf(row)
281
+ let prior = roots.get(id)
282
+ if (!prior || row.eid == id) roots.set(id, row)
283
+ }
284
+ return [...roots.values()]
285
+ }
286
+
287
+ export let summaryQuery = (
288
+ actor: string,
289
+ search: Search = {},
290
+ thread?: string,
291
+ ): Built =>
292
+ line`.inbox_summary.actor=${actor}${
293
+ sub(search.text ? line`&.inbox_summary.text=${search.text}` : NONE)
294
+ }${
295
+ sub(
296
+ search.direction
297
+ ? line`&.inbox_summary.direction=${search.direction}`
298
+ : NONE,
299
+ )
300
+ }${sub(search.all ? line`&.inbox_summary.all=true` : NONE)}${
301
+ sub(search.lane ? line`&.inbox_summary.lane=${search.lane}` : NONE)
302
+ }${sub(thread ? line`&.inbox_summary.thread=${thread}` : NONE)}`