@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/who.js ADDED
@@ -0,0 +1,309 @@
1
+ // The transcript a caller names, and the actor it writes as. One resolution
2
+ // for both, because the CLI and the HTTP server are asking the same question.
3
+ //
4
+ // `--session` used to mean two things: an eid or a human-readable id to
5
+ // `claim take`, and the harness's own id for the run to
6
+ // `session brief|wrap|context`. So `claim take --session S-37703` locked
7
+ // something for the session a person could see, and `session wrap S-37703`
8
+ // returned `[]` and released nothing — the same id, two different entities. It
9
+ // means the SESSION entity now, resolved the way any id is resolved here:
10
+ // addressed first (an eid, `S-37703`, a name the graph resolves), and where
11
+ // nothing matches, read as the harness's own id for a transcript — which is the
12
+ // only one of the three that may not exist yet, and the one
13
+ // `session context --hook -` mints.
14
+ //
15
+ // The same sequence resolves an HTTP request. A request names which run it
16
+ // speaks for (in `x-via`, the fleet's own header for the instrument behind a
17
+ // write), and what it writes is attributed `by` the actor that run works as
18
+ // and `via` the run itself. An actor is one of a handful, and never a
19
+ // session: there are thousands of sessions, and the owner's word is that
20
+ // sessions never go in `by` (M-31958). The actor is the session's persona; the
21
+ // owner, verbatim: "the persona should always be the actor. it used to be the
22
+ // project. i was saying i think actually the persona is the right one", and
23
+ // "i guess you could have fallbacks still, but it should be rare to have
24
+ // agents with no persona". So a run works as its persona, else the actor it
25
+ // names (`session.actor`), else the project it is filed in, else whatever the
26
+ // session that spawned it works as (`actorOf`).
27
+ //
28
+ // A session created from the harness's own id takes its eid from that id
29
+ // (`sessionEid`): the id itself where it is a uuid, as Claude Code's and
30
+ // Codex's are, since an eid is a uuid; one derived from it otherwise. So
31
+ // whoever holds the harness's id holds the eid, and the hook and the importer
32
+ // that both create a session for one id land on one entity. A subagent's id
33
+ // (Claude Code's agent id, its transcript `agent-<id>.jsonl`) names it within
34
+ // its parent, so its eid derives from the two (`subagentSession`).
35
+ import { after } from "@yaks/fp";
36
+ import { and, eq, fields, limit, line, list, or, present, sub } from "@yaks/query";
37
+ import { writers } from "@yaks/graph";
38
+ import { hook } from "@yaks/graph/plugin";
39
+ import { addressed } from "@yaks/graph/call";
40
+ import { identityEid } from "@yaks/graph/identity";
41
+ import { TOMBSTONE } from "@yaks/graph/bundle";
42
+ import { SESSION } from "./comp.js";
43
+ // The properties a read projects, as its `.fields` clause.
44
+ let projected = (names)=>names.length ? line`.fields=${names}` : line`.fields=`;
45
+ /** Whether a request came from a person or their command, rather than an
46
+ * agent or automation. An agent may speak for an identity, so both the author
47
+ * and the instrument matter. A transcript's own stamp is not its parent. */ export let humanCaller = async (g, actor, self)=>{
48
+ let ids = [
49
+ ...new Set([
50
+ actor?.by,
51
+ actor?.via
52
+ ].filter((id)=>!!id && id != self))
53
+ ];
54
+ let kinds = [
55
+ 'session',
56
+ 'effect',
57
+ 'builder'
58
+ ].filter((name)=>g.vocab.comp(name));
59
+ if (!ids.length || !kinds.length) return true;
60
+ let callers = await g.read(and(eq('entity.eid', list(...ids)), or(...kinds.map((name)=>present(name))), fields('entity.eid'), limit(1)));
61
+ return !callers.length;
62
+ };
63
+ // A uuid, whatever its version.
64
+ let UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
65
+ /**
66
+ * The eid of the session a harness knows by `id`: the id itself where it is a
67
+ * uuid, and otherwise one derived from it. A subagent's, whose id names it
68
+ * within its `parent`, derives from both.
69
+ *
70
+ * ```ts
71
+ * import { sessionEid } from '@yaks/session'
72
+ * import { assertEquals } from '@std/assert'
73
+ *
74
+ * let id = '49805559-ca98-4c0a-873e-45c19ec7316c'
75
+ * assertEquals(sessionEid(id), id)
76
+ * ```
77
+ */ export let sessionEid = (id, parent)=>parent ? identityEid(SESSION, [
78
+ parent,
79
+ id
80
+ ]) : UUID.test(id) ? id : identityEid(SESSION, [
81
+ id
82
+ ]);
83
+ /** A command speaks for its own transcript, never its launcher's. Clearing
84
+ * the other harness IDs also matters when `yak` chooses the first one it sees. */ export let sessionEnv = (session, env)=>{
85
+ let { CLAUDE_CODE_SESSION_ID: _claude, CODEX_THREAD_ID: _codex, TASKS_SESSION: _tasks, ...rest } = env;
86
+ return {
87
+ ...rest,
88
+ TASKS_SESSION: session
89
+ };
90
+ };
91
+ /**
92
+ * The transcripts these ids are the runner's own ids of (`session.id`), each
93
+ * to its eid: the key only a session has, which is how an id meant to name a
94
+ * session resolves where no plugin knows it as an eid or a name (the
95
+ * plugin's `address`, asked with the kind `session`).
96
+ */ export let runners = (tx, ids)=>ids.reduce((at, id)=>after(at, (found)=>after(tx.read(line`.${SESSION}.id=${id} .fields=entity.eid`), ([b])=>b ? new Map([
97
+ ...found,
98
+ [
99
+ id,
100
+ b.entity.eid
101
+ ]
102
+ ]) : found)), new Map());
103
+ /**
104
+ * The transcript an id names: the entity it addresses, else the one carrying it
105
+ * as its runner's own id. Nothing is minted here — resolution only reads, and a
106
+ * tool that wants a transcript created does that itself. `{ id }` selects only
107
+ * the runner's own session id, without trying entity addresses. `fields` selects only
108
+ * the properties the caller reads, avoiding derived transcript history.
109
+ */ export let sessionFor = async (g, said, fields)=>{
110
+ if (typeof said != 'string') {
111
+ if (!said.id) return undefined;
112
+ return (await g.read(line`.${SESSION}.id=${said.id} ${sub(fields ? projected(fields) : line`*`)}`))[0];
113
+ }
114
+ if (!said) return undefined;
115
+ let [eid] = await addressed(g, [
116
+ said
117
+ ]);
118
+ let get = async (id)=>(fields ? await g.read(line`.entity.eid=${id} .${SESSION} ${sub(projected(fields))}`) : await g.get([
119
+ id
120
+ ]))[0];
121
+ let row = await get(eid);
122
+ // A projected query already required session presence; its selected
123
+ // properties may all be absent on a native transcript.
124
+ if (row && (fields || row[SESSION]) && row[TOMBSTONE] == null) return row;
125
+ let run = (await runners(g, [
126
+ said
127
+ ])).get(said);
128
+ return run ? await get(run) : undefined;
129
+ };
130
+ // How far `actorOf` follows a chain of spawning sessions before it stops: a
131
+ // chain longer than this is a cycle someone wrote, not a family.
132
+ let DEPTH = 8;
133
+ let str = (v)=>typeof v == 'string' ? v : '';
134
+ /**
135
+ * The actor a session works as: its persona, else the actor it names
136
+ * (`session.actor`), else the project it is filed in (`filed.project`), else
137
+ * whatever the session that spawned it works as, so a subagent works as the
138
+ * persona it runs under. Never a session; `undefined` where nothing names one.
139
+ * A session with no persona anywhere up its line should be rare, and a door
140
+ * that starts one is worth fixing rather than falling back for. Each session
141
+ * is read for those properties alone: the rest of `session` (its computed
142
+ * `status` above all) costs a command its compile and answers nothing here.
143
+ */ export let actorOf = async (g, session)=>{
144
+ let fields = [
145
+ `${SESSION}.persona`,
146
+ `${SESSION}.actor`,
147
+ 'filed.project',
148
+ 'spawned.parent'
149
+ ].filter((f)=>g.vocab.prop(...f.split('.')));
150
+ let seen = new Set();
151
+ for(let at = session; at && !seen.has(at) && seen.size < DEPTH;){
152
+ seen.add(at);
153
+ let [s] = await g.read(line`.entity.eid=${at} .${SESSION} ${sub(projected(fields))}`);
154
+ if (!s) return;
155
+ let named = s[SESSION];
156
+ let project = str(s.filed?.project);
157
+ let by = str(named?.persona) || str(named?.actor) || project;
158
+ if (by) return by;
159
+ at = str(s.spawned?.parent);
160
+ }
161
+ };
162
+ /**
163
+ * The actor a transcript writes as: `by` the actor it works as
164
+ * ({@link actorOf}), the identity that survives a `/clear`, and `via` the run
165
+ * itself. A session that works as nobody leaves `by` unsaid rather than
166
+ * standing in it.
167
+ */ export let speaking = async (g, s)=>{
168
+ let via = s.entity.eid;
169
+ let by = await actorOf(g, via);
170
+ return by ? {
171
+ by,
172
+ via
173
+ } : {
174
+ via
175
+ };
176
+ };
177
+ /**
178
+ * The session standing in this graph for a conversation held in another
179
+ * store and reached through `door` (a tunnel), written where it is new, and
180
+ * the actor a request from it writes as: `by` the `persona` such a caller
181
+ * works as here, `via` that session. Its eid derives from the door and the
182
+ * conversation's id, as a subagent's does from its parent and its own, so
183
+ * every request from one conversation speaks through one session and no id a
184
+ * request sends names one of this graph's own runs. Where the id is an eid,
185
+ * `session.source` keeps it: the conversation is an entity, in its own store.
186
+ * A request naming no conversation speaks through the door's one session.
187
+ * `session.door` says the session is the door's, which is what holds a write
188
+ * through it to its own writer ({@link attributing}). Where the door says
189
+ * whose conversation it is, and that is a person here, `session.person`
190
+ * keeps them: what is news to them is told to it (./watch.ts).
191
+ */ export let standIn = async (g, door, conversation = '', persona, person)=>{
192
+ let via = sessionEid(conversation, door);
193
+ let [had] = await g.get([
194
+ via
195
+ ], [
196
+ SESSION
197
+ ]);
198
+ let named = had?.[TOMBSTONE] == null ? had?.[SESSION] : undefined;
199
+ if (!named || named.door != door || persona && named.persona != persona || person && named.person != person) {
200
+ await g.apply([
201
+ {
202
+ entity: {
203
+ eid: via
204
+ },
205
+ [SESSION]: {
206
+ door,
207
+ ...persona ? {
208
+ persona
209
+ } : {},
210
+ ...person ? {
211
+ person
212
+ } : {},
213
+ ...UUID.test(conversation) ? {
214
+ source: conversation
215
+ } : {}
216
+ }
217
+ }
218
+ ], {
219
+ trusted: true
220
+ });
221
+ }
222
+ return persona ? {
223
+ by: persona,
224
+ via
225
+ } : {
226
+ via
227
+ };
228
+ };
229
+ /**
230
+ * Who a write through a session standing at a door is by is the door's word,
231
+ * and nothing the write says: a request naming somebody else as the one a mark
232
+ * is by (`$decider`, @yaks/kernel) is dropped from it, so the mark is its
233
+ * writer's. A hosted assistant answering a decision through a tunnel answers
234
+ * as itself, whoever it names; the person's own answer arrives as the person
235
+ * (@yaks/tunnel `x-yak-by`), through no session.
236
+ */ export let attributing = hook([
237
+ '$decider'
238
+ ], (bundles, tx)=>{
239
+ let writer = writers(bundles);
240
+ let through = (b)=>b.$decider == null ? undefined : writer(b.entity.eid).via;
241
+ let vias = [
242
+ ...new Set(bundles.flatMap((b)=>through(b) ?? []))
243
+ ];
244
+ if (!vias.length) return bundles;
245
+ return after(tx.get(vias, [
246
+ SESSION
247
+ ]), (rows)=>{
248
+ let doors = new Set(rows.filter((s)=>s[SESSION]?.door).map((s)=>s.entity.eid));
249
+ return bundles.map((b)=>{
250
+ let via = through(b);
251
+ if (!via || !doors.has(via)) return b;
252
+ let { $decider: _, ...own } = b;
253
+ return own;
254
+ });
255
+ });
256
+ });
257
+ /**
258
+ * The session of a subagent its harness knows by `id` within the session it
259
+ * knows by `parent`: its eid, and the rows that write it where the graph has
260
+ * not met it yet (with the parent, where that is new too), else none.
261
+ */ export let spawning = async (g, parent, id)=>{
262
+ let eid = sessionEid(id, parent);
263
+ let [s] = await g.get([
264
+ eid
265
+ ], [
266
+ SESSION
267
+ ]);
268
+ if (s?.[SESSION] && s[TOMBSTONE] == null) return {
269
+ eid,
270
+ rows: []
271
+ };
272
+ let up = (await sessionFor(g, parent, [
273
+ 'session.id'
274
+ ]))?.entity.eid;
275
+ return {
276
+ eid,
277
+ rows: [
278
+ ...up ? [] : [
279
+ {
280
+ entity: {
281
+ eid: '$parent'
282
+ },
283
+ [SESSION]: {
284
+ id: parent
285
+ }
286
+ }
287
+ ],
288
+ {
289
+ entity: {
290
+ eid
291
+ },
292
+ [SESSION]: {
293
+ id
294
+ },
295
+ spawned: {
296
+ parent: up ?? '$parent'
297
+ }
298
+ }
299
+ ]
300
+ };
301
+ };
302
+ /** The session of a subagent ({@link spawning}), written where it is new.
303
+ * Answers its eid, or nothing where either id is empty. */ export let subagentSession = async (g, parent, id)=>{
304
+ if (!parent || !id) return;
305
+ let { eid, rows } = await spawning(g, parent, id);
306
+ if (rows.length) await g.apply(rows);
307
+ return eid;
308
+ };
309
+ //# sourceMappingURL=who.js.map
package/who.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"who.js","sources":["./who.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAC5E,8EAA8E;AAC9E,EAAE;AACF,wEAAwE;AACxE,wDAAwD;AACxD,yEAAyE;AACzE,2EAA2E;AAC3E,+EAA+E;AAC/E,0EAA0E;AAC1E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Q,MAAM;QAAU;MACrC;KACD;EACH;AACF,EAAC;AAED;yDACyD,GACzD,OAAO,IAAI,kBAAkB,OAC3B,GACA,QACA;EAEA,IAAI,CAAC,UAAU,CAAC,IAAI;EACpB,IAAI,EAAE,GAAG,EAAE,IAAI,EAAE,GAAG,MAAM,SAAS,GAAG,QAAQ;EAC9C,IAAI,KAAK,MAAM,EAAE,MAAM,EAAE,KAAK,CAAC;EAC/B,OAAO;AACT,EAAC"}
package/who.ts ADDED
@@ -0,0 +1,347 @@
1
+ // The transcript a caller names, and the actor it writes as. One resolution
2
+ // for both, because the CLI and the HTTP server are asking the same question.
3
+ //
4
+ // `--session` used to mean two things: an eid or a human-readable id to
5
+ // `claim take`, and the harness's own id for the run to
6
+ // `session brief|wrap|context`. So `claim take --session S-37703` locked
7
+ // something for the session a person could see, and `session wrap S-37703`
8
+ // returned `[]` and released nothing — the same id, two different entities. It
9
+ // means the SESSION entity now, resolved the way any id is resolved here:
10
+ // addressed first (an eid, `S-37703`, a name the graph resolves), and where
11
+ // nothing matches, read as the harness's own id for a transcript — which is the
12
+ // only one of the three that may not exist yet, and the one
13
+ // `session context --hook -` mints.
14
+ //
15
+ // The same sequence resolves an HTTP request. A request names which run it
16
+ // speaks for (in `x-via`, the fleet's own header for the instrument behind a
17
+ // write), and what it writes is attributed `by` the actor that run works as
18
+ // and `via` the run itself. An actor is one of a handful, and never a
19
+ // session: there are thousands of sessions, and the owner's word is that
20
+ // sessions never go in `by` (M-31958). The actor is the session's persona; the
21
+ // owner, verbatim: "the persona should always be the actor. it used to be the
22
+ // project. i was saying i think actually the persona is the right one", and
23
+ // "i guess you could have fallbacks still, but it should be rare to have
24
+ // agents with no persona". So a run works as its persona, else the actor it
25
+ // names (`session.actor`), else the project it is filed in, else whatever the
26
+ // session that spawned it works as (`actorOf`).
27
+ //
28
+ // A session created from the harness's own id takes its eid from that id
29
+ // (`sessionEid`): the id itself where it is a uuid, as Claude Code's and
30
+ // Codex's are, since an eid is a uuid; one derived from it otherwise. So
31
+ // whoever holds the harness's id holds the eid, and the hook and the importer
32
+ // that both create a session for one id land on one entity. A subagent's id
33
+ // (Claude Code's agent id, its transcript `agent-<id>.jsonl`) names it within
34
+ // its parent, so its eid derives from the two (`subagentSession`).
35
+
36
+ import { after } from '@yaks/fp'
37
+ import {
38
+ and,
39
+ type Built,
40
+ eq,
41
+ fields,
42
+ limit,
43
+ line,
44
+ list,
45
+ or,
46
+ present,
47
+ sub,
48
+ } from '@yaks/query'
49
+ import type { Actor, Bundle, Comp, Eid, Graph, Hook, Tx } from '@yaks/graph'
50
+ import { writers } from '@yaks/graph'
51
+ import { hook } from '@yaks/graph/plugin'
52
+ import { addressed } from '@yaks/graph/call'
53
+ import { identityEid } from '@yaks/graph/identity'
54
+ import { TOMBSTONE } from '@yaks/graph/bundle'
55
+ import { SESSION } from './comp.js'
56
+
57
+ // The properties a read projects, as its `.fields` clause.
58
+ let projected = (names: string[]): Built =>
59
+ names.length ? line`.fields=${names}` : line`.fields=`
60
+
61
+ /** Whether a request came from a person or their command, rather than an
62
+ * agent or automation. An agent may speak for an identity, so both the author
63
+ * and the instrument matter. A transcript's own stamp is not its parent. */
64
+ export let humanCaller = async (
65
+ g: Pick<Graph, 'read' | 'vocab'>,
66
+ actor: Actor | null,
67
+ self?: Eid,
68
+ ): Promise<boolean> => {
69
+ let ids = [
70
+ ...new Set(
71
+ [actor?.by, actor?.via].filter((id): id is Eid => !!id && id != self),
72
+ ),
73
+ ]
74
+ let kinds = ['session', 'effect', 'builder'].filter((name) =>
75
+ g.vocab.comp(name)
76
+ )
77
+ if (!ids.length || !kinds.length) return true
78
+ let callers = await g.read(and(
79
+ eq('entity.eid', list(...ids)),
80
+ or(...kinds.map((name) => present(name))),
81
+ fields('entity.eid'),
82
+ limit(1),
83
+ ))
84
+ return !callers.length
85
+ }
86
+
87
+ // A uuid, whatever its version.
88
+ let UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
89
+
90
+ /**
91
+ * The eid of the session a harness knows by `id`: the id itself where it is a
92
+ * uuid, and otherwise one derived from it. A subagent's, whose id names it
93
+ * within its `parent`, derives from both.
94
+ *
95
+ * ```ts
96
+ * import { sessionEid } from '@yaks/session'
97
+ * import { assertEquals } from '@std/assert'
98
+ *
99
+ * let id = '49805559-ca98-4c0a-873e-45c19ec7316c'
100
+ * assertEquals(sessionEid(id), id)
101
+ * ```
102
+ */
103
+ export let sessionEid = (id: string, parent?: string): Eid =>
104
+ parent
105
+ ? identityEid(SESSION, [parent, id])
106
+ : UUID.test(id)
107
+ ? id
108
+ : identityEid(SESSION, [id])
109
+
110
+ /** A command speaks for its own transcript, never its launcher's. Clearing
111
+ * the other harness IDs also matters when `yak` chooses the first one it sees. */
112
+ export let sessionEnv = (
113
+ session: string,
114
+ env: Record<string, string>,
115
+ ): Record<string, string> => {
116
+ let {
117
+ CLAUDE_CODE_SESSION_ID: _claude,
118
+ CODEX_THREAD_ID: _codex,
119
+ TASKS_SESSION: _tasks,
120
+ ...rest
121
+ } = env
122
+ return { ...rest, TASKS_SESSION: session }
123
+ }
124
+
125
+ /**
126
+ * The transcripts these ids are the runner's own ids of (`session.id`), each
127
+ * to its eid: the key only a session has, which is how an id meant to name a
128
+ * session resolves where no plugin knows it as an eid or a name (the
129
+ * plugin's `address`, asked with the kind `session`).
130
+ */
131
+ export let runners = (
132
+ tx: Pick<Tx, 'read'>,
133
+ ids: string[],
134
+ ): Map<string, Eid> | Promise<Map<string, Eid>> =>
135
+ ids.reduce<Map<string, Eid> | Promise<Map<string, Eid>>>(
136
+ (at, id) =>
137
+ after(at, (found) =>
138
+ after(
139
+ tx.read(line`.${SESSION}.id=${id} .fields=entity.eid`),
140
+ ([b]) => b ? new Map([...found, [id, b.entity.eid]]) : found,
141
+ )),
142
+ new Map(),
143
+ )
144
+
145
+ /**
146
+ * The transcript an id names: the entity it addresses, else the one carrying it
147
+ * as its runner's own id. Nothing is minted here — resolution only reads, and a
148
+ * tool that wants a transcript created does that itself. `{ id }` selects only
149
+ * the runner's own session id, without trying entity addresses. `fields` selects only
150
+ * the properties the caller reads, avoiding derived transcript history.
151
+ */
152
+ export let sessionFor = async (
153
+ g: Pick<Graph, 'address' | 'get' | 'read'>,
154
+ said: string | { id: string },
155
+ fields?: string[],
156
+ ): Promise<Bundle | undefined> => {
157
+ if (typeof said != 'string') {
158
+ if (!said.id) return undefined
159
+ return (await g.read(
160
+ line`.${SESSION}.id=${said.id} ${
161
+ sub(fields ? projected(fields) : line`*`)
162
+ }`,
163
+ ))[0]
164
+ }
165
+ if (!said) return undefined
166
+ let [eid] = await addressed(g, [said])
167
+ let get = async (id: Eid): Promise<Bundle | undefined> =>
168
+ (fields
169
+ ? await g.read(
170
+ line`.entity.eid=${id} .${SESSION} ${sub(projected(fields))}`,
171
+ )
172
+ : await g.get([id]))[0]
173
+ let row = await get(eid)
174
+ // A projected query already required session presence; its selected
175
+ // properties may all be absent on a native transcript.
176
+ if (row && (fields || row[SESSION]) && row[TOMBSTONE] == null) return row
177
+ let run = (await runners(g, [said])).get(said)
178
+ return run ? await get(run) : undefined
179
+ }
180
+
181
+ // How far `actorOf` follows a chain of spawning sessions before it stops: a
182
+ // chain longer than this is a cycle someone wrote, not a family.
183
+ let DEPTH = 8
184
+
185
+ let str = (v: unknown): string => typeof v == 'string' ? v : ''
186
+
187
+ /**
188
+ * The actor a session works as: its persona, else the actor it names
189
+ * (`session.actor`), else the project it is filed in (`filed.project`), else
190
+ * whatever the session that spawned it works as, so a subagent works as the
191
+ * persona it runs under. Never a session; `undefined` where nothing names one.
192
+ * A session with no persona anywhere up its line should be rare, and a door
193
+ * that starts one is worth fixing rather than falling back for. Each session
194
+ * is read for those properties alone: the rest of `session` (its computed
195
+ * `status` above all) costs a command its compile and answers nothing here.
196
+ */
197
+ export let actorOf = async (
198
+ g: Pick<Graph, 'read' | 'vocab'>,
199
+ session: Eid,
200
+ ): Promise<Eid | undefined> => {
201
+ let fields = [
202
+ `${SESSION}.persona`,
203
+ `${SESSION}.actor`,
204
+ 'filed.project',
205
+ 'spawned.parent',
206
+ ].filter((f) => g.vocab.prop(...f.split('.') as [string, string]))
207
+ let seen = new Set<Eid>()
208
+ for (let at: Eid = session; at && !seen.has(at) && seen.size < DEPTH;) {
209
+ seen.add(at)
210
+ let [s] = await g.read(
211
+ line`.entity.eid=${at} .${SESSION} ${sub(projected(fields))}`,
212
+ )
213
+ if (!s) return
214
+ let named = s[SESSION] as Comp | undefined
215
+ let project = str((s.filed as Comp | undefined)?.project)
216
+ let by = str(named?.persona) || str(named?.actor) || project
217
+ if (by) return by as Eid
218
+ at = str((s.spawned as Comp | undefined)?.parent) as Eid
219
+ }
220
+ }
221
+
222
+ /**
223
+ * The actor a transcript writes as: `by` the actor it works as
224
+ * ({@link actorOf}), the identity that survives a `/clear`, and `via` the run
225
+ * itself. A session that works as nobody leaves `by` unsaid rather than
226
+ * standing in it.
227
+ */
228
+ export let speaking = async (
229
+ g: Pick<Graph, 'read' | 'vocab'>,
230
+ s: Bundle,
231
+ ): Promise<Actor> => {
232
+ let via = s.entity.eid
233
+ let by = await actorOf(g, via)
234
+ return by ? { by, via } : { via }
235
+ }
236
+
237
+ /**
238
+ * The session standing in this graph for a conversation held in another
239
+ * store and reached through `door` (a tunnel), written where it is new, and
240
+ * the actor a request from it writes as: `by` the `persona` such a caller
241
+ * works as here, `via` that session. Its eid derives from the door and the
242
+ * conversation's id, as a subagent's does from its parent and its own, so
243
+ * every request from one conversation speaks through one session and no id a
244
+ * request sends names one of this graph's own runs. Where the id is an eid,
245
+ * `session.source` keeps it: the conversation is an entity, in its own store.
246
+ * A request naming no conversation speaks through the door's one session.
247
+ * `session.door` says the session is the door's, which is what holds a write
248
+ * through it to its own writer ({@link attributing}). Where the door says
249
+ * whose conversation it is, and that is a person here, `session.person`
250
+ * keeps them: what is news to them is told to it (./watch.ts).
251
+ */
252
+ export let standIn = async (
253
+ g: Pick<Graph, 'get' | 'apply'>,
254
+ door: string,
255
+ conversation = '',
256
+ persona?: Eid,
257
+ person?: Eid,
258
+ ): Promise<Actor> => {
259
+ let via = sessionEid(conversation, door)
260
+ let [had] = await g.get([via], [SESSION])
261
+ let named = had?.[TOMBSTONE] == null
262
+ ? had?.[SESSION] as Comp | undefined
263
+ : undefined
264
+ if (
265
+ !named || named.door != door || (persona && named.persona != persona) ||
266
+ (person && named.person != person)
267
+ ) {
268
+ await g.apply([{
269
+ entity: { eid: via },
270
+ [SESSION]: {
271
+ door,
272
+ ...persona ? { persona } : {},
273
+ ...person ? { person } : {},
274
+ ...UUID.test(conversation) ? { source: conversation } : {},
275
+ },
276
+ }], { trusted: true })
277
+ }
278
+ return persona ? { by: persona, via } : { via }
279
+ }
280
+
281
+ /**
282
+ * Who a write through a session standing at a door is by is the door's word,
283
+ * and nothing the write says: a request naming somebody else as the one a mark
284
+ * is by (`$decider`, @yaks/kernel) is dropped from it, so the mark is its
285
+ * writer's. A hosted assistant answering a decision through a tunnel answers
286
+ * as itself, whoever it names; the person's own answer arrives as the person
287
+ * (@yaks/tunnel `x-yak-by`), through no session.
288
+ */
289
+ export let attributing: Hook = hook(['$decider'], (bundles, tx) => {
290
+ let writer = writers(bundles)
291
+ let through = (b: Bundle) =>
292
+ b.$decider == null ? undefined : writer(b.entity.eid).via
293
+ let vias = [...new Set(bundles.flatMap((b) => through(b) ?? []))]
294
+ if (!vias.length) return bundles
295
+ return after(tx.get(vias, [SESSION]), (rows) => {
296
+ let doors = new Set(
297
+ rows.filter((s) => (s[SESSION] as Comp | undefined)?.door)
298
+ .map((s) => s.entity.eid),
299
+ )
300
+ return bundles.map((b) => {
301
+ let via = through(b)
302
+ if (!via || !doors.has(via)) return b
303
+ let { $decider: _, ...own } = b
304
+ return own
305
+ })
306
+ })
307
+ })
308
+
309
+ /**
310
+ * The session of a subagent its harness knows by `id` within the session it
311
+ * knows by `parent`: its eid, and the rows that write it where the graph has
312
+ * not met it yet (with the parent, where that is new too), else none.
313
+ */
314
+ export let spawning = async (
315
+ g: Pick<Graph, 'get' | 'read' | 'address'>,
316
+ parent: string,
317
+ id: string,
318
+ ): Promise<{ eid: Eid; rows: Bundle[] }> => {
319
+ let eid = sessionEid(id, parent)
320
+ let [s] = await g.get([eid], [SESSION])
321
+ if (s?.[SESSION] && s[TOMBSTONE] == null) return { eid, rows: [] }
322
+ let up = (await sessionFor(g, parent, ['session.id']))?.entity.eid
323
+ return {
324
+ eid,
325
+ rows: [
326
+ ...up ? [] : [{ entity: { eid: '$parent' }, [SESSION]: { id: parent } }],
327
+ {
328
+ entity: { eid },
329
+ [SESSION]: { id },
330
+ spawned: { parent: up ?? '$parent' },
331
+ },
332
+ ],
333
+ }
334
+ }
335
+
336
+ /** The session of a subagent ({@link spawning}), written where it is new.
337
+ * Answers its eid, or nothing where either id is empty. */
338
+ export let subagentSession = async (
339
+ g: Pick<Graph, 'get' | 'read' | 'address' | 'apply'>,
340
+ parent: string,
341
+ id: string,
342
+ ): Promise<Eid | undefined> => {
343
+ if (!parent || !id) return
344
+ let { eid, rows } = await spawning(g, parent, id)
345
+ if (rows.length) await g.apply(rows)
346
+ return eid
347
+ }