@yaks/session 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 (393) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +1392 -2
  3. package/_dist/admission.d.ts +15 -0
  4. package/_dist/admission.d.ts.map +1 -0
  5. package/_dist/announce.d.ts +17 -0
  6. package/_dist/announce.d.ts.map +1 -0
  7. package/_dist/answer.d.ts +200 -0
  8. package/_dist/answer.d.ts.map +1 -0
  9. package/_dist/append.d.ts +10 -0
  10. package/_dist/append.d.ts.map +1 -0
  11. package/_dist/archive.d.ts +6 -0
  12. package/_dist/archive.d.ts.map +1 -0
  13. package/_dist/asks.d.ts +11 -0
  14. package/_dist/asks.d.ts.map +1 -0
  15. package/_dist/audit.d.ts +15 -0
  16. package/_dist/audit.d.ts.map +1 -0
  17. package/_dist/bounce.d.ts +19 -0
  18. package/_dist/bounce.d.ts.map +1 -0
  19. package/_dist/bus.d.ts +8 -0
  20. package/_dist/bus.d.ts.map +1 -0
  21. package/_dist/cache.d.ts +45 -0
  22. package/_dist/cache.d.ts.map +1 -0
  23. package/_dist/child-notice.d.ts +6 -0
  24. package/_dist/child-notice.d.ts.map +1 -0
  25. package/_dist/children.d.ts +59 -0
  26. package/_dist/children.d.ts.map +1 -0
  27. package/_dist/cli.d.ts +8 -0
  28. package/_dist/cli.d.ts.map +1 -0
  29. package/_dist/comp.d.ts +9 -0
  30. package/_dist/comp.d.ts.map +1 -0
  31. package/_dist/compact.d.ts +19 -0
  32. package/_dist/compact.d.ts.map +1 -0
  33. package/_dist/conversation.d.ts +158 -0
  34. package/_dist/conversation.d.ts.map +1 -0
  35. package/_dist/cost.d.ts +13 -0
  36. package/_dist/cost.d.ts.map +1 -0
  37. package/_dist/deferred.d.ts +79 -0
  38. package/_dist/deferred.d.ts.map +1 -0
  39. package/_dist/ended.d.ts +27 -0
  40. package/_dist/ended.d.ts.map +1 -0
  41. package/_dist/files.d.ts +73 -0
  42. package/_dist/files.d.ts.map +1 -0
  43. package/_dist/fork.d.ts +23 -0
  44. package/_dist/fork.d.ts.map +1 -0
  45. package/_dist/graft.d.ts +33 -0
  46. package/_dist/graft.d.ts.map +1 -0
  47. package/_dist/graph.d.ts +14 -0
  48. package/_dist/graph.d.ts.map +1 -0
  49. package/_dist/handoff.d.ts +29 -0
  50. package/_dist/handoff.d.ts.map +1 -0
  51. package/_dist/imported.d.ts +63 -0
  52. package/_dist/imported.d.ts.map +1 -0
  53. package/_dist/intake.d.ts +43 -0
  54. package/_dist/intake.d.ts.map +1 -0
  55. package/_dist/joined.d.ts +35 -0
  56. package/_dist/joined.d.ts.map +1 -0
  57. package/_dist/lease.d.ts +21 -0
  58. package/_dist/lease.d.ts.map +1 -0
  59. package/_dist/lines.d.ts +13 -0
  60. package/_dist/lines.d.ts.map +1 -0
  61. package/_dist/mod.d.ts +107 -0
  62. package/_dist/mod.d.ts.map +1 -0
  63. package/_dist/naming.d.ts +10 -0
  64. package/_dist/naming.d.ts.map +1 -0
  65. package/_dist/native.d.ts +13 -0
  66. package/_dist/native.d.ts.map +1 -0
  67. package/_dist/parts.d.ts +145 -0
  68. package/_dist/parts.d.ts.map +1 -0
  69. package/_dist/plugin.d.ts +33 -0
  70. package/_dist/plugin.d.ts.map +1 -0
  71. package/_dist/project.d.ts +67 -0
  72. package/_dist/project.d.ts.map +1 -0
  73. package/_dist/prose.d.ts +7 -0
  74. package/_dist/prose.d.ts.map +1 -0
  75. package/_dist/providers.d.ts +36 -0
  76. package/_dist/providers.d.ts.map +1 -0
  77. package/_dist/reach.d.ts +39 -0
  78. package/_dist/reach.d.ts.map +1 -0
  79. package/_dist/react.d.ts +236 -0
  80. package/_dist/react.d.ts.map +1 -0
  81. package/_dist/readers.d.ts +221 -0
  82. package/_dist/readers.d.ts.map +1 -0
  83. package/_dist/reap.d.ts +21 -0
  84. package/_dist/reap.d.ts.map +1 -0
  85. package/_dist/recall.d.ts +32 -0
  86. package/_dist/recall.d.ts.map +1 -0
  87. package/_dist/routes.d.ts +15 -0
  88. package/_dist/routes.d.ts.map +1 -0
  89. package/_dist/run.d.ts +81 -0
  90. package/_dist/run.d.ts.map +1 -0
  91. package/_dist/service.d.ts +131 -0
  92. package/_dist/service.d.ts.map +1 -0
  93. package/_dist/source.d.ts +9 -0
  94. package/_dist/source.d.ts.map +1 -0
  95. package/_dist/start.d.ts +29 -0
  96. package/_dist/start.d.ts.map +1 -0
  97. package/_dist/state.d.ts +75 -0
  98. package/_dist/state.d.ts.map +1 -0
  99. package/_dist/status.d.ts +38 -0
  100. package/_dist/status.d.ts.map +1 -0
  101. package/_dist/tail.d.ts +121 -0
  102. package/_dist/tail.d.ts.map +1 -0
  103. package/_dist/timing.d.ts +9 -0
  104. package/_dist/timing.d.ts.map +1 -0
  105. package/_dist/told.d.ts +17 -0
  106. package/_dist/told.d.ts.map +1 -0
  107. package/_dist/tools.d.ts +41 -0
  108. package/_dist/tools.d.ts.map +1 -0
  109. package/_dist/transcript.d.ts +5 -0
  110. package/_dist/transcript.d.ts.map +1 -0
  111. package/_dist/turn.d.ts +49 -0
  112. package/_dist/turn.d.ts.map +1 -0
  113. package/_dist/unknown.d.ts +6 -0
  114. package/_dist/unknown.d.ts.map +1 -0
  115. package/_dist/views.d.ts +9 -0
  116. package/_dist/views.d.ts.map +1 -0
  117. package/_dist/vocab.d.ts +9 -0
  118. package/_dist/vocab.d.ts.map +1 -0
  119. package/_dist/wait.d.ts +15 -0
  120. package/_dist/wait.d.ts.map +1 -0
  121. package/_dist/wake.d.ts +116 -0
  122. package/_dist/wake.d.ts.map +1 -0
  123. package/_dist/watch.d.ts +52 -0
  124. package/_dist/watch.d.ts.map +1 -0
  125. package/_dist/who.d.ts +84 -0
  126. package/_dist/who.d.ts.map +1 -0
  127. package/_dist/window.d.ts +43 -0
  128. package/_dist/window.d.ts.map +1 -0
  129. package/admission.js +137 -0
  130. package/admission.js.map +1 -0
  131. package/admission.ts +164 -0
  132. package/announce.js +123 -0
  133. package/announce.js.map +1 -0
  134. package/announce.ts +126 -0
  135. package/answer.js +372 -0
  136. package/answer.js.map +1 -0
  137. package/answer.ts +426 -0
  138. package/append.js +125 -0
  139. package/append.js.map +1 -0
  140. package/append.ts +205 -0
  141. package/archive.js +62 -0
  142. package/archive.js.map +1 -0
  143. package/archive.ts +62 -0
  144. package/asks.js +80 -0
  145. package/asks.js.map +1 -0
  146. package/asks.ts +106 -0
  147. package/attempt.js +46 -0
  148. package/attempt.js.map +1 -0
  149. package/attempt.ts +60 -0
  150. package/audit.js +57 -0
  151. package/audit.js.map +1 -0
  152. package/audit.ts +65 -0
  153. package/bounce.js +29 -0
  154. package/bounce.js.map +1 -0
  155. package/bounce.ts +34 -0
  156. package/bus.js +568 -0
  157. package/bus.js.map +1 -0
  158. package/bus.ts +683 -0
  159. package/cache.js +169 -0
  160. package/cache.js.map +1 -0
  161. package/cache.ts +230 -0
  162. package/child-notice.js +128 -0
  163. package/child-notice.js.map +1 -0
  164. package/child-notice.ts +136 -0
  165. package/children.js +937 -0
  166. package/children.js.map +1 -0
  167. package/children.ts +925 -0
  168. package/claude-control.js +8 -0
  169. package/claude-control.js.map +1 -0
  170. package/claude-control.ts +12 -0
  171. package/cli.js +23 -0
  172. package/cli.js.map +1 -0
  173. package/cli.ts +28 -0
  174. package/comp.js +76 -0
  175. package/comp.js.map +1 -0
  176. package/comp.ts +78 -0
  177. package/compact.js +157 -0
  178. package/compact.js.map +1 -0
  179. package/compact.ts +209 -0
  180. package/compact_ask.js +280 -0
  181. package/compact_ask.js.map +1 -0
  182. package/compact_ask.ts +199 -0
  183. package/continue.js +131 -0
  184. package/continue.js.map +1 -0
  185. package/continue.ts +143 -0
  186. package/conversation.js +337 -0
  187. package/conversation.js.map +1 -0
  188. package/conversation.ts +593 -0
  189. package/cost.js +111 -0
  190. package/cost.js.map +1 -0
  191. package/cost.ts +93 -0
  192. package/deferred.js +207 -0
  193. package/deferred.js.map +1 -0
  194. package/deferred.ts +219 -0
  195. package/deno.json +1 -0
  196. package/dependencies.js +114 -0
  197. package/dependencies.js.map +1 -0
  198. package/dependencies.ts +110 -0
  199. package/draining.js +20 -0
  200. package/draining.js.map +1 -0
  201. package/draining.ts +26 -0
  202. package/ended.js +210 -0
  203. package/ended.js.map +1 -0
  204. package/ended.ts +225 -0
  205. package/files.js +285 -0
  206. package/files.js.map +1 -0
  207. package/files.ts +318 -0
  208. package/fork.js +96 -0
  209. package/fork.js.map +1 -0
  210. package/fork.ts +124 -0
  211. package/graft.js +97 -0
  212. package/graft.js.map +1 -0
  213. package/graft.ts +88 -0
  214. package/graph.js +49 -0
  215. package/graph.js.map +1 -0
  216. package/graph.ts +61 -0
  217. package/handoff.js +111 -0
  218. package/handoff.js.map +1 -0
  219. package/handoff.ts +147 -0
  220. package/help.js +432 -0
  221. package/help.js.map +1 -0
  222. package/help.ts +431 -0
  223. package/hooks.js +253 -0
  224. package/hooks.js.map +1 -0
  225. package/hooks.ts +333 -0
  226. package/imported.js +259 -0
  227. package/imported.js.map +1 -0
  228. package/imported.ts +201 -0
  229. package/inspect.js +353 -0
  230. package/inspect.js.map +1 -0
  231. package/inspect.ts +378 -0
  232. package/intake.js +115 -0
  233. package/intake.js.map +1 -0
  234. package/intake.ts +124 -0
  235. package/joined.js +67 -0
  236. package/joined.js.map +1 -0
  237. package/joined.ts +69 -0
  238. package/lease.js +146 -0
  239. package/lease.js.map +1 -0
  240. package/lease.ts +151 -0
  241. package/lifecycle.js +49 -0
  242. package/lifecycle.js.map +1 -0
  243. package/lifecycle.ts +46 -0
  244. package/lines.js +171 -0
  245. package/lines.js.map +1 -0
  246. package/lines.ts +177 -0
  247. package/listen.js +604 -0
  248. package/listen.js.map +1 -0
  249. package/listen.ts +976 -0
  250. package/log.js +78 -0
  251. package/log.js.map +1 -0
  252. package/log.ts +88 -0
  253. package/mod.js +107 -0
  254. package/mod.js.map +1 -0
  255. package/mod.ts +113 -0
  256. package/name.js +51 -0
  257. package/name.js.map +1 -0
  258. package/name.ts +84 -0
  259. package/naming.js +96 -0
  260. package/naming.js.map +1 -0
  261. package/naming.ts +102 -0
  262. package/native.js +17 -0
  263. package/native.js.map +1 -0
  264. package/native.ts +17 -0
  265. package/package.json +133 -4
  266. package/parts.js +334 -0
  267. package/parts.js.map +1 -0
  268. package/parts.ts +383 -0
  269. package/plugin.js +77 -0
  270. package/plugin.js.map +1 -0
  271. package/plugin.ts +98 -0
  272. package/project.js +354 -0
  273. package/project.js.map +1 -0
  274. package/project.ts +407 -0
  275. package/prompt.js +57 -0
  276. package/prompt.js.map +1 -0
  277. package/prompt.ts +92 -0
  278. package/prose.js +43 -0
  279. package/prose.js.map +1 -0
  280. package/prose.ts +63 -0
  281. package/providers.js +279 -0
  282. package/providers.js.map +1 -0
  283. package/providers.ts +306 -0
  284. package/pruning.js +80 -0
  285. package/pruning.js.map +1 -0
  286. package/pruning.ts +147 -0
  287. package/reach.js +923 -0
  288. package/reach.js.map +1 -0
  289. package/reach.ts +1107 -0
  290. package/react.js +2259 -0
  291. package/react.js.map +1 -0
  292. package/react.ts +2864 -0
  293. package/readers.js +956 -0
  294. package/readers.js.map +1 -0
  295. package/readers.ts +1017 -0
  296. package/reap.js +60 -0
  297. package/reap.js.map +1 -0
  298. package/reap.ts +65 -0
  299. package/recall.js +67 -0
  300. package/recall.js.map +1 -0
  301. package/recall.ts +114 -0
  302. package/reopen.js +45 -0
  303. package/reopen.js.map +1 -0
  304. package/reopen.ts +44 -0
  305. package/result-entry.js +32 -0
  306. package/result-entry.js.map +1 -0
  307. package/result-entry.ts +34 -0
  308. package/resume.js +266 -0
  309. package/resume.js.map +1 -0
  310. package/resume.ts +283 -0
  311. package/routes.js +129 -0
  312. package/routes.js.map +1 -0
  313. package/routes.ts +147 -0
  314. package/routine.js +179 -0
  315. package/routine.js.map +1 -0
  316. package/routine.ts +191 -0
  317. package/run.js +932 -0
  318. package/run.js.map +1 -0
  319. package/run.ts +999 -0
  320. package/service.js +806 -0
  321. package/service.js.map +1 -0
  322. package/service.ts +911 -0
  323. package/source.js +26 -0
  324. package/source.js.map +1 -0
  325. package/source.ts +40 -0
  326. package/start.js +197 -0
  327. package/start.js.map +1 -0
  328. package/start.ts +214 -0
  329. package/state.js +247 -0
  330. package/state.js.map +1 -0
  331. package/state.ts +404 -0
  332. package/status.js +1222 -0
  333. package/status.js.map +1 -0
  334. package/status.ts +1555 -0
  335. package/status_bench.ts +50 -0
  336. package/statusline.js +187 -0
  337. package/statusline.js.map +1 -0
  338. package/statusline.ts +232 -0
  339. package/tail.js +748 -0
  340. package/tail.js.map +1 -0
  341. package/tail.ts +815 -0
  342. package/talk.js +58 -0
  343. package/talk.js.map +1 -0
  344. package/talk.ts +51 -0
  345. package/task-context.js +41 -0
  346. package/task-context.js.map +1 -0
  347. package/task-context.ts +53 -0
  348. package/texted.js +65 -0
  349. package/texted.js.map +1 -0
  350. package/texted.ts +65 -0
  351. package/timing.js +37 -0
  352. package/timing.js.map +1 -0
  353. package/timing.ts +52 -0
  354. package/told.js +56 -0
  355. package/told.js.map +1 -0
  356. package/told.ts +54 -0
  357. package/tools.js +595 -0
  358. package/tools.js.map +1 -0
  359. package/tools.ts +633 -0
  360. package/transcript.js +34 -0
  361. package/transcript.js.map +1 -0
  362. package/transcript.ts +41 -0
  363. package/turn.js +47 -0
  364. package/turn.js.map +1 -0
  365. package/turn.ts +68 -0
  366. package/unknown.js +8 -0
  367. package/unknown.js.map +1 -0
  368. package/unknown.ts +9 -0
  369. package/views.js +15 -0
  370. package/views.js.map +1 -0
  371. package/views.ts +21 -0
  372. package/vocab.js +24 -0
  373. package/vocab.js.map +1 -0
  374. package/vocab.json +1573 -0
  375. package/vocab.ts +30 -0
  376. package/wait.js +30 -0
  377. package/wait.js.map +1 -0
  378. package/wait.ts +45 -0
  379. package/wake.js +552 -0
  380. package/wake.js.map +1 -0
  381. package/wake.ts +597 -0
  382. package/watch.js +520 -0
  383. package/watch.js.map +1 -0
  384. package/watch.ts +596 -0
  385. package/who.js +309 -0
  386. package/who.js.map +1 -0
  387. package/who.ts +347 -0
  388. package/window.js +179 -0
  389. package/window.js.map +1 -0
  390. package/window.ts +268 -0
  391. package/woken.js +129 -0
  392. package/woken.js.map +1 -0
  393. package/woken.ts +127 -0
package/watch.ts ADDED
@@ -0,0 +1,596 @@
1
+ // Who hears a task change. A task's watchers are the session that filed it,
2
+ // every session whose own words mention its id (what it said, or what it was
3
+ // told; a tool's result is what it read, not what it said), the writer of any
4
+ // task, comment or letter whose text mentions it, and each actor whose
5
+ // @yaks/notify `subscription` watches it; an actor muting it hears nothing.
6
+ // A person who commented on it watches it too: they spoke to that work.
7
+ // When the task is completed, cancelled or answered, or someone comments on
8
+ // it, `session_watch` knocks each of them once (./listen.ts says the knock).
9
+ //
10
+ // A knock goes to the watcher's session while it can hear, and once that
11
+ // session is over, to its project's operator: the session a person opened for
12
+ // that project (`session.operator`) that spoke last and is not over, else the
13
+ // project itself, held for whoever works for it next. A watcher that is a
14
+ // persona or a project is that project's from the start.
15
+ //
16
+ // A person hears through their assistant: the session standing here for the
17
+ // conversation they hold with it in another store (`session.person`,
18
+ // ./who.ts `standIn`), which whoever keeps that door passes on (@yaks/tunnel).
19
+ // Their comment carries no session, since a comment written through one is an
20
+ // agent's, so the watcher is the person and the session is found from them. A
21
+ // person with no such conversation is knocked nowhere, and nobody is told of
22
+ // their own act. A task assigned to a person knocks that same session, and
23
+ // nobody else: an assignment is news to the one it names.
24
+ //
25
+ // A person's assistant hears news, not traffic: work they spoke to finishing
26
+ // (completed, cancelled or answered), work newly assigned to them, and a
27
+ // comment that answers one of their own (`comment.reply_to`). Any other
28
+ // comment knocks no session standing for a person, however it watches.
29
+ //
30
+ // Each change knocks a target once, named for the target and the change, so
31
+ // two watchers routed to one session make one knock and a run that is tried
32
+ // again knocks nobody twice. A person's comment already reaches the session
33
+ // that filed the task and the one holding it (./listen.ts), so those two are
34
+ // not knocked for it.
35
+ //
36
+ // Nobody is knocked about their own write, and a session's subagents write
37
+ // for it: a subagent runs inside its parent's process and hands what it did
38
+ // back as the result of the parent's own call, so a knock about its comment
39
+ // would tell the parent what it just read (`writers`). A helper writes in its
40
+ // operator's place (./help.ts), so its operator, and the project it helps, are
41
+ // not knocked about what it wrote: that knock would be routine and start the
42
+ // next helper on the same work (`standing`).
43
+ //
44
+ // A mark that names nobody who made it, no `by` and no `via`, records an act in
45
+ // another store at that store's time, as @yaks/heal closes a bug's task when
46
+ // the tracker resolves or archives the bug. That store tells of the act, so the
47
+ // mark knocks nobody.
48
+ //
49
+ // One event is one knock. A managed run that ends leaves its whole report as
50
+ // a comment on its task that knocks whoever started it (`reported`, which
51
+ // @yaks/spawn's completion writes by). That receipt is the knock for
52
+ // the run's work on the task: the session it knocks is not knocked for the
53
+ // comment a second time, and nobody it reaches, that session or a watcher
54
+ // knocked about the comment, is knocked a little earlier for the run marking
55
+ // the task completed or cancelled. Each would be a second helper's batch for
56
+ // one report (./help.ts). Only a person's assistant, which hears no such
57
+ // comment, hears of the mark.
58
+
59
+ import { line, sub } from '@yaks/query'
60
+ import type { Bundle, Comp, Eid, Graph } from 'jsr:@yaks/graph@0.2.36'
61
+ import { addressed, argsOf } from '@yaks/graph/call'
62
+ import { Refused } from '@yaks/graph/errors'
63
+ import { derivedEid } from '@yaks/graph/identity'
64
+ import { HUMAN, human, mentions } from '@yaks/id'
65
+ import { CLAIM, SESSION } from './comp.js'
66
+ import { ended, type Projects, projects, taken } from './listen.js'
67
+ import { HELPING } from './routine.js'
68
+
69
+ type G = Pick<Graph, 'vocab' | 'read' | 'get' | 'apply'>
70
+ // A reader that also resolves the ids a call was given (`T-12`) to eids.
71
+ type Addressing = G & Pick<Graph, 'address'>
72
+
73
+ let comp = (b: Bundle | undefined, name: string): Comp =>
74
+ (b?.[name] ?? {}) as Comp
75
+ let str = (v: unknown): string => typeof v == 'string' ? v : ''
76
+ let fold = (text: string, max = 200): string => {
77
+ let line = text.replace(/\s+/g, ' ').trim()
78
+ return line.length > max ? `${line.slice(0, max - 1)}…` : line
79
+ }
80
+
81
+ /** The sessions and actors that mention the task `id` names: the sessions
82
+ * whose own entries do, and the writer of each other entity whose `doc.body`
83
+ * does, by the session it came through or else the actor. An entry keeps the
84
+ * id as it was typed; a body stores the task's eid (@yaks/graph `prose`), or
85
+ * the id where it was written before, and a search for the id finds both. */
86
+ let mentioners = async (g: G, task: Eid, id: string): Promise<Eid[]> => {
87
+ // A human id, a prefix and a number: a bare hash is not how anyone
88
+ // mentions a task.
89
+ if (!HUMAN.test(id)) return []
90
+ let said = g.vocab.prop('content', 'body') && g.vocab.comp('entry')
91
+ ? await g.read(
92
+ line`.content.body~=${id}&.entry&!result&!notice&.fields=entry.session,content.body&.limit=500`,
93
+ )
94
+ : []
95
+ let written = g.vocab.prop('doc', 'body')
96
+ ? await g.read(
97
+ line`${id}&.doc&.fields=created.by,created.via,doc.body&.limit=200`,
98
+ )
99
+ : []
100
+ return [
101
+ ...said.filter((b) => mentions(str(comp(b, 'content').body), id))
102
+ .map((b) => str(comp(b, 'entry').session)),
103
+ ...written.filter((b) => {
104
+ let body = str(comp(b, 'doc').body)
105
+ return b.entity.eid != task &&
106
+ (mentions(body, task) || mentions(body, id))
107
+ }).map((b) => str(comp(b, 'created').via) || str(comp(b, 'created').by)),
108
+ ].filter(Boolean)
109
+ }
110
+
111
+ /** The actors whose subscription to `task` is in `mode`. */
112
+ let subscribed = async (g: G, task: Eid, mode: string): Promise<Eid[]> =>
113
+ g.vocab.prop('subscription', 'mode')
114
+ ? (await g.read(
115
+ line`.subscription.target=${task}&.subscription.mode=${mode}&.fields=subscription.actor`,
116
+ )).map((b) => str(comp(b, 'subscription').actor)).filter(Boolean)
117
+ : []
118
+
119
+ // A person's own words: by a person and through no session, as
120
+ // `session_start` reads a person's comment.
121
+ let OWN = line`.created.by.person&!created.via.${SESSION}`
122
+
123
+ /** The people who commented on `task` themselves. */
124
+ let spoke = async (g: G, task: Eid): Promise<Eid[]> =>
125
+ g.vocab.comp('person') && g.vocab.comp('comment')
126
+ ? (await g.read(
127
+ line`.comment.target=${task}&${sub(OWN)}&.fields=created.by&.limit=500`,
128
+ )).map((b) => str(comp(b, 'created').by)).filter(Boolean)
129
+ : []
130
+
131
+ /** The person whose own comment `row` answers, if it answers one. */
132
+ let answered = async (g: G, row: Bundle): Promise<Eid> => {
133
+ let to = str(comp(row, 'comment').reply_to)
134
+ if (!to || !g.vocab.comp('person')) return ''
135
+ let [own] = await g.read(
136
+ line`.entity.eid=${to}&${sub(OWN)}&.fields=created.by`,
137
+ )
138
+ return str(comp(own, 'created').by)
139
+ }
140
+
141
+ /** The session standing here for the conversation `person` holds with their
142
+ * assistant in another store (./who.ts `standIn`): the newest one said to
143
+ * be theirs. */
144
+ export let standingFor = async (
145
+ g: G,
146
+ person: Eid,
147
+ ): Promise<Eid | undefined> =>
148
+ g.vocab.prop(SESSION, 'person')
149
+ ? (await g.read(
150
+ line`.${SESSION}.person=${person}&.${SESSION}.door&.order=-created.at&.limit=1&.fields=entity.eid`,
151
+ ))[0]?.entity.eid
152
+ : undefined
153
+
154
+ /** Everyone watching `task`, as sessions or actors, and those muting it. */
155
+ export let watchers = async (
156
+ g: G,
157
+ task: Bundle,
158
+ ): Promise<{ watching: Eid[]; muting: Set<Eid> }> => {
159
+ let made = comp(task, 'created')
160
+ return {
161
+ watching: [
162
+ ...new Set([
163
+ str(made.via) || str(made.by),
164
+ ...await mentioners(g, task.entity.eid, human(g.vocab)(task)),
165
+ ...await spoke(g, task.entity.eid),
166
+ ...await subscribed(g, task.entity.eid, 'watch'),
167
+ ].filter(Boolean)),
168
+ ],
169
+ muting: new Set(await subscribed(g, task.entity.eid, 'mute')),
170
+ }
171
+ }
172
+
173
+ // The project a watcher answers to: a session's persona's home, a persona's
174
+ // home, or the project itself. None for anyone else.
175
+ let projectOf = (rows: Map<Eid, Bundle>, eid: Eid): Eid => {
176
+ let b = rows.get(eid)
177
+ if (b?.project) return eid
178
+ let persona = b?.[SESSION] ? str(comp(b, SESSION).persona) : eid
179
+ return str(comp(rows.get(persona), 'persona').home)
180
+ }
181
+
182
+ // A project and every project it is filed under, nearest first.
183
+ let above = (tree: Projects, project: Eid): Eid[] => {
184
+ let found = [project]
185
+ for (let up = tree.get(project); up && !found.includes(up);) {
186
+ found.push(up)
187
+ up = tree.get(up)
188
+ }
189
+ return found
190
+ }
191
+
192
+ /** The operator of a project still able to hear: a session a person opened
193
+ * wearing a persona homed there or at a project it is filed under (the
194
+ * projects ./listen.ts gives such a session what is held for), not over, the
195
+ * one that spoke last. */
196
+ export let operatorOf = async (
197
+ g: G,
198
+ project: Eid,
199
+ ): Promise<Eid | undefined> => {
200
+ if (!g.vocab.prop(SESSION, 'operator')) return
201
+ let homes = above(await projects(g), project)
202
+ if (!homes.length) return
203
+ let open = await g.read(
204
+ line`.${SESSION}.operator=true&.${SESSION}.persona.persona.home=${homes}&!${SESSION}.ended&.fields=${SESSION}.ended`,
205
+ )
206
+ let gone = await ended(g, open)
207
+ let live = open.filter((s) => !gone.has(s.entity.eid))
208
+ let last = await Promise.all(live.map(async (s) => {
209
+ let [entry] = await g.read(
210
+ line`.entry.session=${s.entity.eid}&.order=-entry.seq&.limit=1&.fields=created.at`,
211
+ )
212
+ return { eid: s.entity.eid, at: str(comp(entry, 'created').at) }
213
+ }))
214
+ return last.toSorted((a, b) => b.at.localeCompare(a.at))[0]?.eid
215
+ }
216
+
217
+ /** Where each watcher's knock goes: its session while that can hear, else
218
+ * its project's operator, else the project. A person's goes to the session
219
+ * standing for their conversation with their assistant ({@link standingFor}),
220
+ * unless the act is their own (`by`). Anyone muting the task is left out, and
221
+ * with `relay` false, so is a session that is over. */
222
+ export let targets = async (
223
+ g: G,
224
+ watching: Eid[],
225
+ muting: Set<Eid>,
226
+ relay = true,
227
+ by?: Eid,
228
+ ): Promise<Eid[]> => {
229
+ let rows = new Map(
230
+ (watching.length
231
+ ? await g.get(
232
+ watching,
233
+ [SESSION, 'persona', 'project', 'person'].filter((c) =>
234
+ g.vocab.comp(c)
235
+ ),
236
+ )
237
+ : [])
238
+ .map((b) => [b.entity.eid, b]),
239
+ )
240
+ let homes = [
241
+ ...new Set(
242
+ [...rows.values()].map((b) => str(comp(b, SESSION).persona))
243
+ .filter(Boolean),
244
+ ),
245
+ ]
246
+ let personas = homes.length && g.vocab.comp('persona')
247
+ ? await g.get(homes, ['persona'])
248
+ : []
249
+ for (let b of personas) {
250
+ rows.set(b.entity.eid, b)
251
+ }
252
+ let sessions = [...rows.values()].filter((b) => b[SESSION])
253
+ let gone = await ended(g, sessions)
254
+ let operators = new Map<Eid, Eid | undefined>()
255
+ let out = new Set<Eid>()
256
+ for (let w of watching) {
257
+ let project = projectOf(rows, w)
258
+ if (muting.has(w) || (project && muting.has(project))) continue
259
+ if (rows.get(w)?.[SESSION]) {
260
+ if (!gone.has(w)) out.add(w)
261
+ if (!gone.has(w) || !relay) continue
262
+ }
263
+ if (rows.get(w)?.person) {
264
+ let heard = w == by ? undefined : await standingFor(g, w)
265
+ if (heard) out.add(heard)
266
+ continue
267
+ }
268
+ if (!project) continue
269
+ if (!operators.has(project)) {
270
+ operators.set(project, await operatorOf(g, project))
271
+ }
272
+ out.add(operators.get(project) ?? project)
273
+ }
274
+ return [...out]
275
+ }
276
+
277
+ // How far a line of sessions is followed up before it stops: a longer one is
278
+ // a cycle someone wrote.
279
+ let DEPTH = 8
280
+
281
+ // What places a session in its line, read alone: the rest of `session` (its
282
+ // computed status above all) is derived from its whole log.
283
+ let placed = async (g: G, eid: Eid): Promise<Bundle | undefined> =>
284
+ (await g.read(
285
+ line`.entity.eid=${eid}&.${SESSION}&.fields=${
286
+ [`${SESSION}.source`, `${SESSION}.ended`, `${SESSION}.transcript`]
287
+ .concat(g.vocab.comp('spawned') ? ['spawned.parent'] : [])
288
+ .concat(g.vocab.comp('helping') ? ['helping.project'] : [])
289
+ }`,
290
+ ))[0]
291
+
292
+ let managed = async (g: G, eid: Eid): Promise<Bundle | undefined> =>
293
+ g.vocab.comp('process')
294
+ ? (await g.get([eid], ['process', 'exit']))[0]
295
+ : undefined
296
+
297
+ /** The sessions a write through `via` is the own write of: that session, and
298
+ * while it is a subagent (a transcript its harness keeps in a file and runs
299
+ * inside its parent's process, as a Claude Code subagent is), each session up
300
+ * its line. A managed run or a native child is a line of work of its own. */
301
+ export let writers = async (g: G, via: Eid): Promise<Set<Eid>> => {
302
+ let own = new Set<Eid>()
303
+ for (let at = via; at && !own.has(at) && own.size < DEPTH;) {
304
+ own.add(at)
305
+ let s = await placed(g, at)
306
+ let parent = str(comp(s, 'spawned').parent)
307
+ at = parent && comp(s, SESSION).transcript &&
308
+ !(await managed(g, at))?.process
309
+ ? parent
310
+ : ''
311
+ }
312
+ return own
313
+ }
314
+
315
+ /** Whom a write through `via` is the own write of because `via` helps them
316
+ * (./help.ts): a helper takes its operator's routine in its place, so what it
317
+ * writes is that operator's own. The project it helps, each project filed
318
+ * under it, and that project's operator hear nothing of it; a knock to any of
319
+ * them would be routine, and start the next helper on the first one's work. */
320
+ let standing = async (g: G, via: Eid): Promise<(target: Eid) => boolean> => {
321
+ let [s] = via && g.vocab.comp(HELPING) ? await g.get([via], [HELPING]) : []
322
+ let helped = str(comp(s, HELPING).project)
323
+ if (!helped) return () => false
324
+ let tree = await projects(g)
325
+ let operator = await operatorOf(g, helped)
326
+ return (t) => t == operator || above(tree, t).includes(helped)
327
+ }
328
+
329
+ // The task an id a call was given names, or none where it names nothing now.
330
+ let named = async (g: Addressing, id: string): Promise<Eid | ''> => {
331
+ if (!id) return ''
332
+ try {
333
+ return (await addressed(g, [id], 'task'))[0]
334
+ } catch (error) {
335
+ if (error instanceof Refused) return ''
336
+ throw error
337
+ }
338
+ }
339
+
340
+ /**
341
+ * Where a managed run's ending is reported: the task its spawn call names,
342
+ * else the tasks it holds, and the session to knock, which is the one that
343
+ * started it or, that one being over, the nearest up its own line that can
344
+ * still hear. @yaks/spawn's completion writes its receipt by this, and
345
+ * {@link knocks} leaves that receipt to be the one knock. A helper's own
346
+ * ending (./help.ts) knocks nobody: what it did is in the graph, and what
347
+ * needs its operator it handed up itself.
348
+ *
349
+ * ```ts
350
+ * import { reported } from '@yaks/session'
351
+ *
352
+ * // let { tasks, to } = await reported(graph, run)
353
+ * ```
354
+ */
355
+ export let reported = async (
356
+ g: Addressing,
357
+ run: Eid,
358
+ ): Promise<{ tasks: Eid[]; to?: Eid }> => {
359
+ let row = await placed(g, run)
360
+ let source = str(comp(row, SESSION).source)
361
+ let [call] = source ? await g.get([source], ['call', 'entry', 'created']) : []
362
+ // Claims may already have been released by the terminal-entry effect, or
363
+ // by the agent itself. The originating spawn call still names its task, as
364
+ // its caller wrote it (`T-12`).
365
+ let task = call?.call ? await named(g, str(argsOf(call).task)) : ''
366
+ let tasks = task
367
+ ? [task]
368
+ : g.vocab.comp('task')
369
+ ? (await g.read(line`.task&.${CLAIM}.session=${run}`)).map((b) =>
370
+ b.entity.eid
371
+ )
372
+ : []
373
+ if (row?.helping) return { tasks }
374
+ let seen = new Set<Eid>([run])
375
+ let at = str(comp(row, 'spawned').parent) ||
376
+ str(comp(call, 'entry').session) || str(comp(call, 'created').via)
377
+ while (at && !seen.has(at) && seen.size < DEPTH) {
378
+ seen.add(at)
379
+ let s = await placed(g, at)
380
+ if (!s) break
381
+ if (!(await ended(g, [s])).has(at)) return { tasks, to: at }
382
+ at = str(comp(s, 'spawned').parent)
383
+ }
384
+ return { tasks }
385
+ }
386
+
387
+ // Who hears a change another way, so is not knocked for it. A comment: the
388
+ // session it knocks itself. A mark a managed run made while still at work on
389
+ // the task: everyone its receipt reaches, which is the session the receipt
390
+ // knocks and every watcher the receipt, a comment, knocks in turn. A person's
391
+ // assistant hears no comment that does not answer them, so for it the mark is
392
+ // the news.
393
+ let told = async (
394
+ g: Addressing,
395
+ row: Bundle,
396
+ task: Eid,
397
+ via: Eid,
398
+ ): Promise<(target: Eid, person: Eid) => boolean> => {
399
+ let none = () => false
400
+ if (row.comment) {
401
+ let to = str(comp(row, 'knock').target)
402
+ return (t) => t == to
403
+ }
404
+ let run = via ? await managed(g, via) : undefined
405
+ if (!run?.process || run.exit) return none
406
+ let { tasks, to } = await reported(g, via)
407
+ if (!tasks.includes(task)) return none
408
+ return (t, person) => t == to || !person
409
+ }
410
+
411
+ /** A change worth telling: what happened, what it says (the task's title, or
412
+ * the comment's words), who did it and through what. */
413
+ type Change = {
414
+ key: string
415
+ what: string
416
+ text: string
417
+ by: Eid
418
+ via: Eid
419
+ decision?: boolean
420
+ }
421
+
422
+ // A task's newest mark: answered, completed or cancelled.
423
+ let marked = (g: G, task: Bundle): Change | undefined => {
424
+ let id = human(g.vocab)(task)
425
+ let decided = comp(task, 'decided')
426
+ let mark = task.decided && task.completed
427
+ ? { name: 'decided', what: `${id} answered "${str(decided.choice)}"` }
428
+ : task.cancelled
429
+ ? { name: 'cancelled', what: `${id} cancelled` }
430
+ : task.completed
431
+ ? { name: 'completed', what: `${id} completed` }
432
+ : undefined
433
+ if (!mark) return
434
+ let c = comp(task, mark.name)
435
+ // A mark naming nobody who made it records an act in another store, at that
436
+ // store's time: @yaks/heal closes a bug's task with the tracker's
437
+ // resolution or archival. That store tells it, so it knocks nobody here.
438
+ if (!str(c.by) && !str(c.via)) return
439
+ let reason = mark.name == 'cancelled' && str(c.reason)
440
+ ? ` (${fold(str(c.reason))})`
441
+ : ''
442
+ return {
443
+ key: `${task.entity.eid} ${mark.name} ${str(c.at)}`,
444
+ what: `${mark.what}${reason}`,
445
+ text: str(comp(task, 'doc').title),
446
+ by: str(c.by),
447
+ via: str(c.via),
448
+ decision: mark.name == 'decided',
449
+ }
450
+ }
451
+
452
+ // An open task's assignment to a person, as it stands: to whom, and by its
453
+ // last writer. `at` is when the commit that assigned it owed its run, so each
454
+ // assignment is a change of its own and a run tried again is the same one.
455
+ let assigned = async (
456
+ g: G,
457
+ task: Bundle,
458
+ at = '',
459
+ ): Promise<Change & { to: Eid } | undefined> => {
460
+ let to = str(comp(task, 'filed').assignee)
461
+ if (!to || task.completed || task.cancelled) return
462
+ let [who] = await g.get(
463
+ [to],
464
+ ['doc', 'person'].filter((c) => g.vocab.comp(c)),
465
+ )
466
+ if (!who?.person) return
467
+ let wrote = comp(task, 'updated').by ? 'updated' : 'created'
468
+ return {
469
+ to,
470
+ key: `${task.entity.eid} assigned ${to} ${at}`,
471
+ what: `${human(g.vocab)(task)} assigned to ${
472
+ str(comp(who, 'doc').title) || human(g.vocab)(who)
473
+ }`,
474
+ text: str(comp(task, 'doc').title),
475
+ by: str(comp(task, wrote).by),
476
+ via: str(comp(task, wrote).via),
477
+ }
478
+ }
479
+
480
+ /** The change a run of `session_watch` is for: the component that moved
481
+ * (`filed` for an assignment) and when its commit owed the run. */
482
+ export type Moved = { name?: string; at?: string }
483
+
484
+ /**
485
+ * The knocks a change to a task owes its watchers: the task itself (a mark)
486
+ * or a comment on it. None where `eid` is neither, the change is not one a
487
+ * watcher hears, or every watcher wrote it or was told already. Where what
488
+ * `moved` is the task's `filed`, the change is its assignment, owed to the
489
+ * person it names alone.
490
+ *
491
+ * ```ts
492
+ * import { knocks } from '@yaks/session'
493
+ *
494
+ * // await graph.apply(await knocks(graph, task), { trusted: true })
495
+ * ```
496
+ */
497
+ export let knocks = async (
498
+ g: Addressing,
499
+ eid: Eid,
500
+ moved: Moved = {},
501
+ ): Promise<Bundle[]> => {
502
+ if (!g.vocab.comp('knock')) return []
503
+ let [row] = await g.get([eid])
504
+ let on = row?.comment ? str(comp(row, 'comment').target) : eid
505
+ let [task] = on != eid ? await g.get([on]) : [row]
506
+ if (!task?.task) return []
507
+ let made = comp(row, 'created')
508
+ let given = moved.name == 'filed'
509
+ ? await assigned(g, task, moved.at)
510
+ : undefined
511
+ let change: Change | undefined = moved.name == 'filed'
512
+ ? given
513
+ : row.comment
514
+ ? {
515
+ key: `${eid} comment`,
516
+ what: `${human(g.vocab)(row)} on ${human(g.vocab)(task)}`,
517
+ text: str(comp(row, 'doc').body),
518
+ by: str(made.by),
519
+ via: str(made.via),
520
+ }
521
+ : marked(g, task)
522
+ if (!change) return []
523
+ let { watching, muting } = await watchers(g, task)
524
+ if (given) watching = [given.to]
525
+ let replied = row.comment ? await answered(g, row) : ''
526
+ // A person's comment reaches the session that filed the task, and the one
527
+ // holding it, through ./listen.ts already.
528
+ let [writer] = change.via ? await g.get([change.via], [SESSION]) : []
529
+ if (row.comment && !writer?.[SESSION]) {
530
+ let told = [str(comp(task, 'created').via), str(comp(task, CLAIM).session)]
531
+ watching = watching.filter((w) => !told.includes(w))
532
+ }
533
+ let own = change.via ? await writers(g, change.via) : new Set<Eid>()
534
+ let knows = await told(g, row, task.entity.eid, change.via)
535
+ watching = watching.filter((w) => !own.has(w))
536
+ let worked = !!row.comment && (await taken(g, [eid])).has(eid)
537
+ let stands = await standing(g, change.via)
538
+ let to = (await targets(g, watching, muting, !worked, change.by))
539
+ .filter((t) => !own.has(t) && !stands(t))
540
+ // The person each target stands for, if it stands for one.
541
+ let stood = new Map(
542
+ (to.length ? await g.get(to, [SESSION]) : []).map((b) => [
543
+ b.entity.eid,
544
+ str(comp(b, SESSION).person),
545
+ ]),
546
+ )
547
+ to = to.filter((t) => !knows(t, stood.get(t) ?? ''))
548
+ // A comment reaches a person's assistant only when it answers them.
549
+ if (row.comment) {
550
+ to = to.filter((t) => [replied, ''].includes(stood.get(t) ?? ''))
551
+ }
552
+ if (!to.length) return []
553
+ let actorFields = change.decision
554
+ ? ['doc', 'persona', 'project'].filter((c) => g.vocab.comp(c))
555
+ : ['doc']
556
+ let [who] = change.by ? await g.get([change.by], actorFields) : []
557
+ let name = who ? str(comp(who, 'doc').title) || human(g.vocab)(who) : ''
558
+ let prefix = change.decision && (who?.persona || who?.project)
559
+ ? 'Agent decision: '
560
+ : ''
561
+ // The words whole: a knock cut short costs its reader a second command,
562
+ // and with it a second read of its whole context.
563
+ let text = fold(change.text, Infinity)
564
+ let body = `${prefix}${change.what}${name ? ` by ${name}` : ''}` +
565
+ (text ? `: ${text}` : '')
566
+ let named = to.map((target) => ({
567
+ target,
568
+ eid: derivedEid(`session watch ${target} ${change.key}`),
569
+ }))
570
+ let had = new Set(
571
+ (await g.get(named.map((n) => n.eid), ['knock']))
572
+ .filter((b) => b.knock).map((b) => b.entity.eid),
573
+ )
574
+ return named.filter((n) => !had.has(n.eid)).map((n) => ({
575
+ entity: { eid: n.eid },
576
+ knock: {
577
+ target: n.target,
578
+ // What it tells of: the comment, or the task a mark or an assignment
579
+ // moved, so a reader tells a task's change from talk about it.
580
+ about: row.comment ? eid : task.entity.eid,
581
+ ...change.by ? { from: change.by } : {},
582
+ },
583
+ doc: { body },
584
+ }))
585
+ }
586
+
587
+ /** The code behind `session_watch`: knock the watchers of the task `eid` is,
588
+ * or that the comment `eid` is on, for what `moved`. */
589
+ export let watch = async (
590
+ g: Addressing,
591
+ eid: Eid,
592
+ moved?: Moved,
593
+ ): Promise<void> => {
594
+ let owed = await knocks(g, eid, moved)
595
+ if (owed.length) await g.apply(owed, { trusted: true })
596
+ }