@yaks/tracker 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 (198) hide show
  1. package/Histogram.js +71 -0
  2. package/Histogram.js.map +1 -0
  3. package/Histogram.ts +100 -0
  4. package/LICENSE +202 -0
  5. package/README.md +639 -2
  6. package/_dist/brief.d.ts +25 -0
  7. package/_dist/brief.d.ts.map +1 -0
  8. package/_dist/changes.d.ts +9 -0
  9. package/_dist/changes.d.ts.map +1 -0
  10. package/_dist/chart.d.ts +24 -0
  11. package/_dist/chart.d.ts.map +1 -0
  12. package/_dist/digest.d.ts +64 -0
  13. package/_dist/digest.d.ts.map +1 -0
  14. package/_dist/effects.d.ts +73 -0
  15. package/_dist/effects.d.ts.map +1 -0
  16. package/_dist/errors.d.ts +14 -0
  17. package/_dist/errors.d.ts.map +1 -0
  18. package/_dist/everywhere.d.ts +13 -0
  19. package/_dist/everywhere.d.ts.map +1 -0
  20. package/_dist/fault.d.ts +29 -0
  21. package/_dist/fault.d.ts.map +1 -0
  22. package/_dist/file.d.ts +6 -0
  23. package/_dist/file.d.ts.map +1 -0
  24. package/_dist/fixtime.d.ts +20 -0
  25. package/_dist/fixtime.d.ts.map +1 -0
  26. package/_dist/frames.d.ts +6 -0
  27. package/_dist/frames.d.ts.map +1 -0
  28. package/_dist/graph.d.ts +3 -0
  29. package/_dist/graph.d.ts.map +1 -0
  30. package/_dist/group.d.ts +21 -0
  31. package/_dist/group.d.ts.map +1 -0
  32. package/_dist/instrument.d.ts +37 -0
  33. package/_dist/instrument.d.ts.map +1 -0
  34. package/_dist/intake.d.ts +21 -0
  35. package/_dist/intake.d.ts.map +1 -0
  36. package/_dist/merge.d.ts +11 -0
  37. package/_dist/merge.d.ts.map +1 -0
  38. package/_dist/mod.d.ts +7 -0
  39. package/_dist/mod.d.ts.map +1 -0
  40. package/_dist/model.d.ts +20 -0
  41. package/_dist/model.d.ts.map +1 -0
  42. package/_dist/outage.d.ts +7 -0
  43. package/_dist/outage.d.ts.map +1 -0
  44. package/_dist/people.d.ts +3 -0
  45. package/_dist/people.d.ts.map +1 -0
  46. package/_dist/poll.d.ts +101 -0
  47. package/_dist/poll.d.ts.map +1 -0
  48. package/_dist/poll_thread.d.ts +8 -0
  49. package/_dist/poll_thread.d.ts.map +1 -0
  50. package/_dist/reading.d.ts +34 -0
  51. package/_dist/reading.d.ts.map +1 -0
  52. package/_dist/remote.d.ts +61 -0
  53. package/_dist/remote.d.ts.map +1 -0
  54. package/_dist/report.d.ts +324 -0
  55. package/_dist/report.d.ts.map +1 -0
  56. package/_dist/resolution.d.ts +22 -0
  57. package/_dist/resolution.d.ts.map +1 -0
  58. package/_dist/routes.d.ts +33 -0
  59. package/_dist/routes.d.ts.map +1 -0
  60. package/_dist/script.d.ts +8 -0
  61. package/_dist/script.d.ts.map +1 -0
  62. package/_dist/service.d.ts +30 -0
  63. package/_dist/service.d.ts.map +1 -0
  64. package/_dist/sync_health.d.ts +4 -0
  65. package/_dist/sync_health.d.ts.map +1 -0
  66. package/_dist/ticket.d.ts +14 -0
  67. package/_dist/ticket.d.ts.map +1 -0
  68. package/_dist/tools.d.ts +39 -0
  69. package/_dist/tools.d.ts.map +1 -0
  70. package/_dist/trace-intake.d.ts +25 -0
  71. package/_dist/trace-intake.d.ts.map +1 -0
  72. package/_dist/views.d.ts +11 -0
  73. package/_dist/views.d.ts.map +1 -0
  74. package/_dist/vocab.d.ts +9 -0
  75. package/_dist/vocab.d.ts.map +1 -0
  76. package/box.json +51 -0
  77. package/brief.js +86 -0
  78. package/brief.js.map +1 -0
  79. package/brief.ts +120 -0
  80. package/changes.js +57 -0
  81. package/changes.js.map +1 -0
  82. package/changes.ts +70 -0
  83. package/chart.js +186 -0
  84. package/chart.js.map +1 -0
  85. package/chart.ts +193 -0
  86. package/deno.json +1 -0
  87. package/digest.js +329 -0
  88. package/digest.js.map +1 -0
  89. package/digest.ts +406 -0
  90. package/effects.js +136 -0
  91. package/effects.js.map +1 -0
  92. package/effects.ts +178 -0
  93. package/errors.js +130 -0
  94. package/errors.js.map +1 -0
  95. package/errors.ts +185 -0
  96. package/everywhere.js +46 -0
  97. package/everywhere.js.map +1 -0
  98. package/everywhere.ts +66 -0
  99. package/fault.js +65 -0
  100. package/fault.js.map +1 -0
  101. package/fault.ts +107 -0
  102. package/file.js +87 -0
  103. package/file.js.map +1 -0
  104. package/file.ts +93 -0
  105. package/fixtime.js +67 -0
  106. package/fixtime.js.map +1 -0
  107. package/fixtime.ts +86 -0
  108. package/frames.js +77 -0
  109. package/frames.js.map +1 -0
  110. package/frames.ts +69 -0
  111. package/graph.js +7 -0
  112. package/graph.js.map +1 -0
  113. package/graph.ts +8 -0
  114. package/group.js +444 -0
  115. package/group.js.map +1 -0
  116. package/group.ts +533 -0
  117. package/inspect.js +471 -0
  118. package/inspect.js.map +1 -0
  119. package/inspect.ts +693 -0
  120. package/instrument.js +69 -0
  121. package/instrument.js.map +1 -0
  122. package/instrument.ts +93 -0
  123. package/intake.js +43 -0
  124. package/intake.js.map +1 -0
  125. package/intake.ts +84 -0
  126. package/merge.js +126 -0
  127. package/merge.js.map +1 -0
  128. package/merge.ts +115 -0
  129. package/migrate.ts +36 -0
  130. package/mod.js +8 -0
  131. package/mod.js.map +1 -0
  132. package/mod.ts +19 -0
  133. package/model.js +8 -0
  134. package/model.js.map +1 -0
  135. package/model.ts +27 -0
  136. package/outage.js +129 -0
  137. package/outage.js.map +1 -0
  138. package/outage.ts +158 -0
  139. package/package.json +133 -4
  140. package/page.d.ts +2 -0
  141. package/page.js +905 -0
  142. package/people.js +51 -0
  143. package/people.js.map +1 -0
  144. package/people.ts +82 -0
  145. package/poll.js +277 -0
  146. package/poll.js.map +1 -0
  147. package/poll.ts +411 -0
  148. package/poll_thread.js +104 -0
  149. package/poll_thread.js.map +1 -0
  150. package/poll_thread.ts +126 -0
  151. package/poll_worker.ts +45 -0
  152. package/reading.js +122 -0
  153. package/reading.js.map +1 -0
  154. package/reading.ts +162 -0
  155. package/reconcile.js +113 -0
  156. package/reconcile.js.map +1 -0
  157. package/reconcile.ts +130 -0
  158. package/remote.js +312 -0
  159. package/remote.js.map +1 -0
  160. package/remote.ts +397 -0
  161. package/report.js +825 -0
  162. package/report.js.map +1 -0
  163. package/report.ts +944 -0
  164. package/resolution.js +114 -0
  165. package/resolution.js.map +1 -0
  166. package/resolution.ts +123 -0
  167. package/routes.js +130 -0
  168. package/routes.js.map +1 -0
  169. package/routes.ts +163 -0
  170. package/script.js +13 -0
  171. package/script.js.map +1 -0
  172. package/script.ts +16 -0
  173. package/service.js +174 -0
  174. package/service.js.map +1 -0
  175. package/service.ts +215 -0
  176. package/spread.js +94 -0
  177. package/spread.js.map +1 -0
  178. package/spread.ts +101 -0
  179. package/sync_health.js +36 -0
  180. package/sync_health.js.map +1 -0
  181. package/sync_health.ts +65 -0
  182. package/ticket.js +6 -0
  183. package/ticket.js.map +1 -0
  184. package/ticket.ts +24 -0
  185. package/tools.js +305 -0
  186. package/tools.js.map +1 -0
  187. package/tools.ts +411 -0
  188. package/trace-intake.js +164 -0
  189. package/trace-intake.js.map +1 -0
  190. package/trace-intake.ts +148 -0
  191. package/views.js +247 -0
  192. package/views.js.map +1 -0
  193. package/views.ts +254 -0
  194. package/vocab.js +20 -0
  195. package/vocab.js.map +1 -0
  196. package/vocab.json +834 -0
  197. package/vocab.ts +14 -0
  198. package/yak-tracker@.service +22 -0
package/poll.ts ADDED
@@ -0,0 +1,411 @@
1
+ // The platform tracker is a Worker behind signed doors, with no subscription a
2
+ // box process can hold. A follower polls its `bugs` read instead and gets the
3
+ // shape of a client watch: the whole answer, paged by global eid. Each poll
4
+ // sends the tag of the answer it holds: a tracker where nothing moved since
5
+ // says so with no row read, and one where something did names what moved, so
6
+ // the poll reads only those again. A whole read is left for the first poll,
7
+ // and for a tag the tracker no longer knows what moved since. Marks go
8
+ // through the same signed transport.
9
+ //
10
+ // A poll the tracker cannot answer for now is a log line, and the next poll
11
+ // tries again; what a failed poll is, and when it is reported, is
12
+ // ./outage.ts's, as it is for the tracker service's syncs.
13
+ import type { Bundle } from 'jsr:@yaks/graph@0.2.36'
14
+ import { minted } from '@yaks/graph/identity'
15
+ import {
16
+ mark,
17
+ Misconfigured,
18
+ type Remote,
19
+ unanswered,
20
+ Unavailable,
21
+ } from './remote.js'
22
+ import { type Read, read } from './routes.js'
23
+ import type { Mark } from './model.ts'
24
+ import { type Clock, system } from '@yaks/fp'
25
+ import { outage } from './outage.js'
26
+ import { pollThread } from './poll_thread.js'
27
+
28
+ export { OUTAGE } from './outage.js'
29
+ export { pollThread }
30
+ export { changes } from './changes.js'
31
+
32
+ /** A polled answer of the platform tracker, with its mark door. */
33
+ export type Polled = {
34
+ /** the query each poll asks the Worker */
35
+ query: string
36
+ /** the bugs it answered last */
37
+ readonly value: Bundle[]
38
+ /** a poll has answered and the latest one did not fail */
39
+ readonly ready: boolean
40
+ /** called after each poll, answered or failed */
41
+ subscribe: (fn: (bundles: Bundle[]) => void) => () => void
42
+ /** stop polling */
43
+ close: () => void
44
+ /** send each bundle's `resolved`, `archived` or `taken` mark to the
45
+ * Worker. Its marks carry no precondition, so a bundle's `$was` is not
46
+ * checked. */
47
+ mutate: (bundles: Bundle[]) => Promise<Bundle[]>
48
+ /** the bugs among `eids`, whatever the poll's query, asked once */
49
+ get: (eids: string[]) => Promise<Bundle[]>
50
+ }
51
+
52
+ /** The Worker's doors a poll asks: a page of its bugs, and a mark. */
53
+ export type Doors = {
54
+ /** scopes whose bugs moved since the held tag, through the platform */
55
+ changes?: (tag?: string, signal?: AbortSignal) => Promise<Read>
56
+ /** a page of the bugs `params` asks for (`limit`, `query`), or that
57
+ * nothing moved since `tag` */
58
+ bugs: (
59
+ params: URLSearchParams,
60
+ signal?: AbortSignal,
61
+ tag?: string,
62
+ ) => Promise<Read>
63
+ /** a mark on one bug and what it says: a resolution's explanation, the
64
+ * fixer's `task` with `taken`; in the platform's tracker, or the space's
65
+ * `scope` names */
66
+ mark: (
67
+ bug: string,
68
+ name: Mark,
69
+ said: Record<string, string>,
70
+ scope?: string,
71
+ ) => Promise<unknown>
72
+ }
73
+
74
+ /** The Worker's doors over the signed transport (./remote.ts), through `go`. */
75
+ export let signed = (remote: Remote, go: typeof fetch = fetch): Doors => ({
76
+ changes: (tag, signal) =>
77
+ read({ remote }, 'changes', new URLSearchParams(), signal, go, tag),
78
+ bugs: (params, signal, tag) =>
79
+ read({ remote }, 'bugs', params, signal, go, tag),
80
+ mark: (bug, name, said, scope) => mark(remote, bug, name, go, said, scope),
81
+ })
82
+
83
+ /** `doors` asking the tracker of the space `scope` names, or as they ask
84
+ * without one: the platform's. */
85
+ export let scoped = (doors: Doors, scope?: string): Doors =>
86
+ scope
87
+ ? {
88
+ bugs: (params, signal, tag) => {
89
+ params.set('scope', scope)
90
+ return doors.bugs(params, signal, tag)
91
+ },
92
+ mark: (bug, name, said) => doors.mark(bug, name, said, scope),
93
+ }
94
+ : doors
95
+
96
+ export type PollOpts = {
97
+ /** A shared change feed's version of this scope. An unchanged version
98
+ * skips its bug read; a failed read never acknowledges the version. */
99
+ version?: (scope?: string) => Promise<string | undefined>
100
+ /** time between polls, in ms (default 30000) */
101
+ every?: number
102
+ /** how long one page of a poll may take before the poll counts as failed,
103
+ * in ms (default: the request's own bound, a minute), so every poll
104
+ * settles */
105
+ timeout?: number
106
+ /** a failure to fix: a poll that failed other than by the tracker being
107
+ * out of reach, at once, and the tracker not answering for OUTAGE, once */
108
+ report?: (error: unknown) => void
109
+ /** a poll the tracker could not answer, which the next one retries, and
110
+ * its answering again (default: `console.warn`) */
111
+ log?: (line: string) => void
112
+ /** where it asks (default: signed doors on a dedicated worker event loop),
113
+ * which several polls may share */
114
+ doors?: Doors
115
+ /** the global eid of the space whose tracker it polls; the platform's
116
+ * without one */
117
+ scope?: string
118
+ /** what time it is, and how a wait is set — between polls, a hung poll's
119
+ * and a page's deadline (default: the runtime's), so a test can hold the
120
+ * clock */
121
+ clock?: Clock
122
+ }
123
+
124
+ const PAGE = 100, PAGES = 100
125
+
126
+ /** A page of the bugs `params` asks for, or that nothing moved since `tag`;
127
+ * a refusal throws. */
128
+ export type Page = (
129
+ params: URLSearchParams,
130
+ tag?: string,
131
+ ) => Promise<Extract<Read, { ok: true }>>
132
+
133
+ /** The pages `doors` answers. A refusal throws, as `Unavailable` (./remote.ts)
134
+ * where the tracker could not answer for now: a 503 or 504 (./routes.ts
135
+ * `read`), or a page past its `timeout`; as `Misconfigured` where this side
136
+ * could not set the read up, a 500. A stop ends a page as itself, never
137
+ * as the read it cut short. */
138
+ export let pages = (
139
+ doors: Doors,
140
+ stop?: AbortSignal,
141
+ { timeout, clock = system }: { timeout?: number; clock?: Clock } = {},
142
+ ): Page =>
143
+ async (params, tag) => {
144
+ stop?.throwIfAborted()
145
+ // A page past its deadline aborts as `AbortSignal.timeout` would, so the
146
+ // transport names it a timeout (./remote.ts `late`).
147
+ let late = new AbortController()
148
+ let cancel = timeout
149
+ ? clock.after(
150
+ timeout,
151
+ () => late.abort(new DOMException('Signal timed out.', 'TimeoutError')),
152
+ )
153
+ : () => {}
154
+ let signal = timeout
155
+ ? AbortSignal.any([late.signal, ...stop ? [stop] : []])
156
+ : stop
157
+ try {
158
+ let got = await doors.bugs(params, signal, tag).catch((error) => {
159
+ throw unanswered(error)
160
+ })
161
+ stop?.throwIfAborted()
162
+ if (got.ok) return got
163
+ throw got.status == 503 || got.status == 504
164
+ ? new Unavailable(got.error)
165
+ : got.status == 500
166
+ ? new Misconfigured(got.error)
167
+ : Error(got.error)
168
+ } finally {
169
+ cancel()
170
+ }
171
+ }
172
+
173
+ /** What brings an answer read at a tag up to date: the rows read, at the tag
174
+ * the tracker stands at now. With `moved`, they are the answer among those
175
+ * entities, which moved since; without, the whole answer. */
176
+ export type Fresh = { rows: Bundle[]; tag?: string; moved?: string[] }
177
+
178
+ let asking = (query: string) =>
179
+ new URLSearchParams({ limit: String(PAGE), query })
180
+
181
+ // A page in eid order past `after`, no longer than asked: anything else would
182
+ // loop, or skip a bug.
183
+ let checked = (value: unknown, after = ''): Bundle[] => {
184
+ let rows = value as Bundle[]
185
+ if (
186
+ !Array.isArray(rows) || rows.length > PAGE ||
187
+ rows.some((row, i) =>
188
+ !row?.bug || typeof row.entity?.eid != 'string' ||
189
+ row.entity.eid <= (i ? rows[i - 1].entity.eid : after)
190
+ )
191
+ ) throw Error('Remote tracker returned an invalid page of bugs')
192
+ return rows
193
+ }
194
+
195
+ /** Every bug `query` answers, a page at a time in eid order, and the tag its
196
+ * first page was read at. */
197
+ let whole = async (page: Page, query: string): Promise<Fresh> => {
198
+ let rows: Bundle[] = [], after = '', tag: string | undefined
199
+ for (let n = 0; n < PAGES; n++) {
200
+ let got = await page(asking(
201
+ `${query} .order=entity.eid${after ? ` .after=${after}` : ''}`,
202
+ ))
203
+ tag ??= got.tag
204
+ let some = checked(got.value, after)
205
+ rows.push(...some)
206
+ if (some.length < PAGE) return { rows, tag }
207
+ after = some.at(-1)!.entity.eid
208
+ }
209
+ throw Error(`Remote tracker answered more than ${PAGES} pages of bugs`)
210
+ }
211
+
212
+ /** The bugs among `eids` that `query` answers, a page of ids to a request,
213
+ * which keeps each request's address short and its answer to one page. */
214
+ let among = async (
215
+ page: Page,
216
+ query: string,
217
+ eids: string[],
218
+ ): Promise<Bundle[]> => {
219
+ let rows: Bundle[] = []
220
+ for (let at = 0; at < eids.length; at += PAGE) {
221
+ let some = eids.slice(at, at + PAGE).join(',')
222
+ let got = await page(
223
+ asking(`${query} .entity.eid=${some} .order=entity.eid`),
224
+ )
225
+ rows.push(...checked(got.value))
226
+ }
227
+ return rows
228
+ }
229
+
230
+ /** What brings the answer to `query` read at tag `held` up to date: nothing
231
+ * when nothing moved, the bugs among those that moved when the tracker names
232
+ * them, and otherwise, or with no tag held, the whole answer. */
233
+ export let since = async (
234
+ page: Page,
235
+ query: string,
236
+ held?: string,
237
+ ): Promise<Fresh | undefined> => {
238
+ if (held) {
239
+ let got = await page(new URLSearchParams({ since: held }), held)
240
+ if (got.unchanged) return
241
+ let moved = (got.value as { moved?: unknown } | null)?.moved
242
+ if (
243
+ Array.isArray(moved) &&
244
+ moved.every((eid) => typeof eid == 'string' && minted(eid))
245
+ ) return { rows: await among(page, query, moved), tag: got.tag, moved }
246
+ }
247
+ return await whole(page, query)
248
+ }
249
+
250
+ /** The answer `rows` brought up to date by `fresh`, in eid order.
251
+ *
252
+ * ```ts
253
+ * import { renewed } from '@yaks/tracker/poll'
254
+ * import { equal } from '@yaks/testing'
255
+ * let bug = (eid: string, fault = eid) => ({ entity: { eid }, bug: { fault } })
256
+ * let rows = [bug('a'), bug('b'), bug('c')]
257
+ * equal(
258
+ * renewed(rows, { rows: [bug('b', 'new'), bug('d')], moved: ['b', 'c', 'd'] }),
259
+ * [bug('a'), bug('b', 'new'), bug('d')],
260
+ * )
261
+ * equal(renewed(rows, { rows: [bug('e')] }), [bug('e')])
262
+ * ```
263
+ */
264
+ export let renewed = (rows: Bundle[], fresh: Fresh): Bundle[] => {
265
+ if (!fresh.moved) return fresh.rows
266
+ let moved = new Set(fresh.moved)
267
+ return [...rows.filter((b) => !moved.has(b.entity.eid)), ...fresh.rows]
268
+ .sort((a, b) => a.entity.eid < b.entity.eid ? -1 : 1)
269
+ }
270
+
271
+ // The mark a bundle carries, if it carries one.
272
+ let markOf = (b: Bundle): Mark | undefined =>
273
+ b.resolved
274
+ ? 'resolved'
275
+ : b.archived
276
+ ? 'archived'
277
+ : b.taken
278
+ ? 'taken'
279
+ : undefined
280
+
281
+ /** What a mark says beside its stamps, which the Worker writes itself. */
282
+ let saying = (mark: unknown): Record<string, string> =>
283
+ Object.fromEntries(
284
+ Object.entries(mark ?? {}).filter(([k, v]) =>
285
+ typeof v == 'string' && !['at', 'by', 'via'].includes(k)
286
+ ),
287
+ )
288
+
289
+ /**
290
+ * Poll the platform tracker's bugs, or a space tracker's, until closed.
291
+ *
292
+ * ```ts
293
+ * import { poll } from '@yaks/tracker/poll'
294
+ *
295
+ * // Supply doors for an in-memory example; omit them for the signed worker.
296
+ * let open = poll({ url: 'https://tracker.example' }, '.bug', {
297
+ * doors: {
298
+ * bugs: async () => ({ ok: true, value: [] }),
299
+ * mark: async () => {},
300
+ * },
301
+ * })
302
+ * open.ready // false: no poll has answered
303
+ * open.close()
304
+ * ```
305
+ */
306
+ export let poll = (
307
+ remote: Remote,
308
+ query = '.bug',
309
+ opts: PollOpts = {},
310
+ ): Polled => {
311
+ let thread = opts.doors ? undefined : pollThread(remote)
312
+ let doors = scoped(opts.doors ?? thread!, opts.scope)
313
+ let clock = opts.clock ?? system
314
+ let value: Bundle[] = [], ready = false
315
+ // The tag `value` was read at.
316
+ let held: string | undefined
317
+ let seen: string | undefined
318
+ let missing = { version: '', eids: new Set<string>() }
319
+ let listeners = new Set<(bundles: Bundle[]) => void>()
320
+ let stop = new AbortController()
321
+ let page = pages(doors, stop.signal, { timeout: opts.timeout, clock })
322
+ let every = opts.every ?? 30_000
323
+ let watched = outage({
324
+ name: 'Tracker poll',
325
+ every,
326
+ report: opts.report,
327
+ log: opts.log,
328
+ clock,
329
+ signal: stop.signal,
330
+ })
331
+ let pass = async () => {
332
+ try {
333
+ await watched(async () => {
334
+ let version = await opts.version?.(opts.scope)
335
+ if (ready && version != null && version == seen) return
336
+ let fresh = await since(page, query, held)
337
+ if (fresh) {
338
+ value = renewed(value, fresh)
339
+ held = fresh.tag
340
+ }
341
+ ready = true
342
+ seen = version
343
+ })
344
+ } catch {
345
+ if (stop.signal.aborted) return
346
+ ready = false
347
+ }
348
+ for (let fn of listeners) fn(value)
349
+ }
350
+ // A wait that the time or a close ends, whichever comes first, leaving no
351
+ // listener behind it on a poll that runs for days.
352
+ let wait = (ms: number) =>
353
+ new Promise<void>((done) => {
354
+ let end = () => {
355
+ cancel()
356
+ stop.signal.removeEventListener('abort', end)
357
+ done()
358
+ }
359
+ let cancel = clock.after(ms, end)
360
+ stop.signal.addEventListener('abort', end, { once: true })
361
+ })
362
+ ;(async () => {
363
+ while (!stop.signal.aborted) {
364
+ await pass()
365
+ if (!stop.signal.aborted) await wait(every)
366
+ }
367
+ })()
368
+ return {
369
+ query,
370
+ get value() {
371
+ return value
372
+ },
373
+ get ready() {
374
+ return ready
375
+ },
376
+ subscribe: (fn) => {
377
+ listeners.add(fn)
378
+ return () => void listeners.delete(fn)
379
+ },
380
+ close: () => {
381
+ stop.abort()
382
+ watched.close()
383
+ thread?.close()
384
+ },
385
+ mutate: async (bundles) => {
386
+ for (let b of bundles) {
387
+ let name = markOf(b)
388
+ if (name) await doors.mark(b.entity.eid, name, saying(b[name]))
389
+ }
390
+ return bundles
391
+ },
392
+ get: async (eids) => {
393
+ let version = await opts.version?.(opts.scope)
394
+ if (version != missing.version) {
395
+ missing = { version: version ?? '', eids: new Set() }
396
+ }
397
+ // Work left on the box may name a bug in another tracker. A scope
398
+ // already asked about it owes no further read until its bugs move.
399
+ let due = version ? eids.filter((eid) => !missing.eids.has(eid)) : eids
400
+ let absent = missing.eids
401
+ let rows = await among(page, '.bug', due)
402
+ let found = new Set(rows.map((row) => row.entity.eid))
403
+ if (version) {
404
+ for (let eid of due) {
405
+ if (!found.has(eid)) absent.add(eid)
406
+ }
407
+ }
408
+ return rows
409
+ },
410
+ }
411
+ }
package/poll_thread.js ADDED
@@ -0,0 +1,104 @@
1
+ // Only the poll's signed network doors run here. Graph reconciliation stays on
2
+ // its host; signing, fetch, body reads and attempt deadlines share no event
3
+ // loop with its SQL/effect work.
4
+ /** Signed poll doors with their own event loop. Close owns and terminates only
5
+ * this worker, rejecting outstanding reads and marks. A failed worker is
6
+ * replaced on the next poll, never by retrying an ambiguous mark here. */ export let pollThread = (remote)=>{
7
+ let worker;
8
+ let closed = false, next = 0;
9
+ let pending = new Map();
10
+ let fail = (error)=>{
11
+ worker?.terminate();
12
+ worker = undefined;
13
+ for (let p of pending.values()){
14
+ p.clean();
15
+ p.reject(error);
16
+ }
17
+ pending.clear();
18
+ };
19
+ let start = ()=>{
20
+ if (worker) return worker;
21
+ let w = new Worker(new URL('./poll_worker.ts', import.meta.url), {
22
+ type: 'module'
23
+ });
24
+ worker = w;
25
+ w.onmessage = ({ data })=>{
26
+ let p = pending.get(data.id);
27
+ if (!p) return;
28
+ pending.delete(data.id);
29
+ p.clean();
30
+ if ('error' in data) {
31
+ let error = new Error(data.error.message);
32
+ error.name = data.error.name;
33
+ p.reject(error);
34
+ } else p.resolve(data.value);
35
+ };
36
+ w.onerror = (event)=>{
37
+ event.preventDefault();
38
+ fail(new Error('Remote tracker poll worker failed'));
39
+ };
40
+ return w;
41
+ };
42
+ let ask = (call, signal)=>{
43
+ if (closed) {
44
+ return Promise.reject(new Error('Remote tracker poll is closed'));
45
+ }
46
+ if (signal?.aborted) return Promise.reject(signal.reason);
47
+ return new Promise((resolve, reject)=>{
48
+ let id = ++next;
49
+ let abort = ()=>{
50
+ worker?.postMessage({
51
+ abort: id
52
+ });
53
+ pending.delete(id);
54
+ clean();
55
+ reject(signal.reason);
56
+ };
57
+ let clean = ()=>signal?.removeEventListener('abort', abort);
58
+ try {
59
+ let w = start();
60
+ pending.set(id, {
61
+ resolve,
62
+ reject,
63
+ clean
64
+ });
65
+ signal?.addEventListener('abort', abort, {
66
+ once: true
67
+ });
68
+ w.postMessage({
69
+ ...call,
70
+ id,
71
+ remote: {
72
+ url: remote.url,
73
+ secret: remote.secret
74
+ }
75
+ });
76
+ } catch {
77
+ fail(new Error('Remote tracker poll worker failed'));
78
+ reject(new Error('Remote tracker poll worker failed'));
79
+ }
80
+ });
81
+ };
82
+ return {
83
+ changes: (tag, signal)=>ask({
84
+ params: '',
85
+ tag,
86
+ changes: true
87
+ }, signal),
88
+ bugs: (params, signal, tag)=>ask({
89
+ params: params.toString(),
90
+ tag
91
+ }, signal),
92
+ mark: (bug, name, said, scope)=>ask({
93
+ bug,
94
+ name,
95
+ said,
96
+ scope
97
+ }),
98
+ close: ()=>{
99
+ closed = true;
100
+ fail(new Error('Remote tracker poll is closed'));
101
+ }
102
+ };
103
+ };
104
+ //# sourceMappingURL=poll_thread.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"poll_thread.js","sources":["./poll_thread.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,4EAA4E;AAC5E,iCAAiC;AA8BjC;;wEAEwE,GACxE,OAAO,IAAI,aAAa,CAAC;EACvB,IAAI;EACJ,IAAI,SAAS,OAAO,OAAO;EAC3B,IAAI,UAAU,IAAI;EAKlB,IAAI,OAAO,CAAC;IACV,QAAQ;IACR,SAAS;IACT,KAAK,IAAI,KAAK,QAAQ,MAAM,GAAI;MAC9B,EAAE,KAAK;MACP,EAAE,MAAM,CAAC;IACX;IACA,QAAQ,KAAK;EACf;EACA,IAAI,QAAQ;IACV,IAAI,QAAQ,OAAO;IACnB,IAAI,IAAI,IAAI,OAAO,IAAI,IAAI,oBAAoB,YAAY,GAAG,GAAG;MAC/D,MAAM;IACR;IACA,SAAS;IACT,EAAE,SAAS,GAAG,CAAC,EAAE,IAAI,EAAuB;MAC1C,IAAI,IAAI,QAAQ,GAAG,CAAC,KAAK,EAAE;MAC3B,IAAI,CAAC,GAAG;MACR,QAAQ,MAAM,CAAC,KAAK,EAAE;MACtB,EAAE,KAAK;MACP,IAAI,WAAW,MAAM;QACnB,IAAI,QAAQ,IAAI,MAAM,KAAK,KAAK,CAAC,OAAO;QACxC,MAAM,IAAI,GAAG,KAAK,KAAK,CAAC,IAAI;QAC5B,EAAE,MAAM,CAAC;MACX,OAAO,EAAE,OAAO,CAAC,KAAK,KAAK;IAC7B;IACA,EAAE,OAAO,GAAG,CAAC;MACX,MAAM,cAAc;MACpB,KAAK,IAAI,MAAM;IACjB;IACA,OAAO;EACT;EACA,IAAI,MAAM,CACR,MAMA;IAEA,IAAI,QAAQ;MACV,OAAO,QAAQ,MAAM,CAAC,IAAI,MAAM;IAClC;IACA,IAAI,QAAQ,SAAS,OAAO,QAAQ,MAAM,CAAC,OAAO,MAAM;IACxD,OAAO,IAAI,QAAQ,CAAC,SAAS;MAC3B,IAAI,KAAK,EAAE;MACX,IAAI,QAAQ;QACV,QAAQ,YAAY;UAAE,OAAO;QAAG;QAChC,QAAQ,MAAM,CAAC;QACf;QACA,OAAO,OAAQ,MAAM;MACvB;MACA,IAAI,QAAQ,IAAM,QAAQ,oBAAoB,SAAS;MACvD,IAAI;QACF,IAAI,IAAI;QACR,QAAQ,GAAG,CAAC,IAAI;UAAE;UAAS;UAAQ;QAAM;QACzC,QAAQ,iBAAiB,SAAS,OAAO;UAAE,MAAM;QAAK;QACtD,EAAE,WAAW,CACX;UACE,GAAG,IAAI;UACP;UACA,QAAQ;YAAE,KAAK,OAAO,GAAG;YAAE,QAAQ,OAAO,MAAM;UAAC;QACnD;MAEJ,EAAE,OAAM;QACN,KAAK,IAAI,MAAM;QACf,OAAO,IAAI,MAAM;MACnB;IACF;EACF;EACA,OAAO;IACL,SAAS,CAAC,KAAK,SACb,IAAI;QAAE,QAAQ;QAAI;QAAK,SAAS;MAAK,GAAG;IAC1C,MAAM,CAAC,QAAQ,QAAQ,MACrB,IAAI;QAAE,QAAQ,OAAO,QAAQ;QAAI;MAAI,GAAG;IAC1C,MAAM,CAAC,KAAK,MAAM,MAAM,QAAU,IAAI;QAAE;QAAK;QAAM;QAAM;MAAM;IAC/D,OAAO;MACL,SAAS;MACT,KAAK,IAAI,MAAM;IACjB;EACF;AACF,EAAC"}
package/poll_thread.ts ADDED
@@ -0,0 +1,126 @@
1
+ // Only the poll's signed network doors run here. Graph reconciliation stays on
2
+ // its host; signing, fetch, body reads and attempt deadlines share no event
3
+ // loop with its SQL/effect work.
4
+ import type { Doors } from './poll.ts'
5
+ import type { Remote } from './remote.ts'
6
+ import type { Read } from './routes.ts'
7
+ import type { Mark } from './model.ts'
8
+
9
+ // Each ask carries the remote as it stands, read at use as every door's is,
10
+ // so a fixed address or secret reaches the next poll without a restart.
11
+ export type Said =
12
+ | {
13
+ id: number
14
+ remote: Remote
15
+ params: string
16
+ tag?: string
17
+ changes?: boolean
18
+ }
19
+ | {
20
+ id: number
21
+ remote: Remote
22
+ bug: string
23
+ name: Mark
24
+ said: Record<string, string>
25
+ scope?: string
26
+ }
27
+ | { abort: number }
28
+
29
+ export type Heard =
30
+ | { id: number; value: unknown }
31
+ | { id: number; error: { name: string; message: string } }
32
+
33
+ /** Signed poll doors with their own event loop. Close owns and terminates only
34
+ * this worker, rejecting outstanding reads and marks. A failed worker is
35
+ * replaced on the next poll, never by retrying an ambiguous mark here. */
36
+ export let pollThread = (remote: Remote): Doors & { close: () => void } => {
37
+ let worker: Worker | undefined
38
+ let closed = false, next = 0
39
+ let pending = new Map<number, {
40
+ resolve: (value: unknown) => void
41
+ reject: (error: Error) => void
42
+ clean: () => void
43
+ }>()
44
+ let fail = (error: Error) => {
45
+ worker?.terminate()
46
+ worker = undefined
47
+ for (let p of pending.values()) {
48
+ p.clean()
49
+ p.reject(error)
50
+ }
51
+ pending.clear()
52
+ }
53
+ let start = () => {
54
+ if (worker) return worker
55
+ let w = new Worker(new URL('./poll_worker.ts', import.meta.url), {
56
+ type: 'module',
57
+ })
58
+ worker = w
59
+ w.onmessage = ({ data }: MessageEvent<Heard>) => {
60
+ let p = pending.get(data.id)
61
+ if (!p) return
62
+ pending.delete(data.id)
63
+ p.clean()
64
+ if ('error' in data) {
65
+ let error = new Error(data.error.message)
66
+ error.name = data.error.name
67
+ p.reject(error)
68
+ } else p.resolve(data.value)
69
+ }
70
+ w.onerror = (event) => {
71
+ event.preventDefault()
72
+ fail(new Error('Remote tracker poll worker failed'))
73
+ }
74
+ return w
75
+ }
76
+ let ask = (
77
+ call: { params: string; tag?: string; changes?: boolean } | {
78
+ bug: string
79
+ name: Mark
80
+ said: Record<string, string>
81
+ scope?: string
82
+ },
83
+ signal?: AbortSignal,
84
+ ): Promise<unknown> => {
85
+ if (closed) {
86
+ return Promise.reject(new Error('Remote tracker poll is closed'))
87
+ }
88
+ if (signal?.aborted) return Promise.reject(signal.reason)
89
+ return new Promise((resolve, reject) => {
90
+ let id = ++next
91
+ let abort = () => {
92
+ worker?.postMessage({ abort: id } satisfies Said)
93
+ pending.delete(id)
94
+ clean()
95
+ reject(signal!.reason)
96
+ }
97
+ let clean = () => signal?.removeEventListener('abort', abort)
98
+ try {
99
+ let w = start()
100
+ pending.set(id, { resolve, reject, clean })
101
+ signal?.addEventListener('abort', abort, { once: true })
102
+ w.postMessage(
103
+ {
104
+ ...call,
105
+ id,
106
+ remote: { url: remote.url, secret: remote.secret },
107
+ } satisfies Said,
108
+ )
109
+ } catch {
110
+ fail(new Error('Remote tracker poll worker failed'))
111
+ reject(new Error('Remote tracker poll worker failed'))
112
+ }
113
+ })
114
+ }
115
+ return {
116
+ changes: (tag, signal) =>
117
+ ask({ params: '', tag, changes: true }, signal) as Promise<Read>,
118
+ bugs: (params, signal, tag) =>
119
+ ask({ params: params.toString(), tag }, signal) as Promise<Read>,
120
+ mark: (bug, name, said, scope) => ask({ bug, name, said, scope }),
121
+ close: () => {
122
+ closed = true
123
+ fail(new Error('Remote tracker poll is closed'))
124
+ },
125
+ }
126
+ }
package/poll_worker.ts ADDED
@@ -0,0 +1,45 @@
1
+ import { freshFetch } from 'jsr:@yaks/process@0.2.36/http'
2
+ // This worker opens no graph, services or reporting loop. Credentials stay in
3
+ // its messages and never in a worker URL or an error.
4
+ import { signed } from './poll.ts'
5
+ import type { Heard, Said } from './poll_thread.ts'
6
+
7
+ let go = freshFetch()
8
+ let running = new Map<number, AbortController>()
9
+ let tell = (value: Heard) => self.postMessage(value)
10
+ self.onmessage = ({ data }: MessageEvent<Said>) => {
11
+ if ('abort' in data) {
12
+ running.get(data.abort)?.abort()
13
+ return
14
+ }
15
+ let doors = signed(data.remote, go)
16
+ let stop = new AbortController()
17
+ running.set(data.id, stop)
18
+ void (async () => {
19
+ try {
20
+ let value = 'params' in data
21
+ ? data.changes
22
+ ? await doors.changes!(data.tag, stop.signal)
23
+ : await doors.bugs(
24
+ new URLSearchParams(data.params),
25
+ stop.signal,
26
+ data.tag,
27
+ )
28
+ : await doors.mark(data.bug, data.name, data.said, data.scope)
29
+ tell({ id: data.id, value })
30
+ } catch (error) {
31
+ // The signed doors already redact transport errors and remote addresses.
32
+ tell({
33
+ id: data.id,
34
+ error: {
35
+ name: error instanceof Error ? error.name : 'Error',
36
+ message: error instanceof Error
37
+ ? error.message
38
+ : 'Remote tracker is unavailable',
39
+ },
40
+ })
41
+ } finally {
42
+ running.delete(data.id)
43
+ }
44
+ })()
45
+ }