@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/service.js ADDED
@@ -0,0 +1,806 @@
1
+ // The duty a host runs at `@yaks/session/service` for as long as it is up:
2
+ // every session a harness ran on this machine, read into the graph from the
3
+ // transcript file the harness keeps (./tail.ts), and pending addressed items
4
+ // delivered to native sessions as the graph's commits make them due
5
+ // (./bus.ts `follow`), beside the transcripts and never waiting on them.
6
+ //
7
+ // The duty opens by freeing the locks whose holder is gone (./reap.ts). Start-up
8
+ // is the one moment that answer is fresh, and holding the duty is what makes
9
+ // this process the one to give it: the lease is renewed for as long as the
10
+ // process that stays up runs, so a command started beside it leaves the reap
11
+ // to it rather than writing the same releases again. A one-shot command reaps
12
+ // and reads nothing: its tails would outlive the lease it gives back.
13
+ //
14
+ // Claude Code keeps each session as `<project>/<session>.jsonl` under its
15
+ // projects directory, and appends to it as the session runs. A transcript
16
+ // written to in the last `full` days is followed as it grows, at full depth:
17
+ // what the person typed, what the harness put in front of the model, what the
18
+ // model said and thought, and every tool call with its result. An older one is
19
+ // read in lazily, one file per pass, and only its prose. A look reads each
20
+ // file for a slice of time, so a long backlog is read over many looks and what
21
+ // a live session says never waits behind it. A session the graph has not met
22
+ // is created under the harness's id for it, and one a person typed into is
23
+ // marked `operator`, which is what tells the person's words apart from an
24
+ // agent's brief.
25
+ //
26
+ // A session whose transcript asks a provider for a run (`using{provider}` on
27
+ // an entry nobody imported) is a managed run: @yaks/spawn launched it and reads
28
+ // its stdout, and its transcript file is left alone.
29
+ //
30
+ // A subagent keeps a transcript of its own, `<session>/subagents/agent-<id>.jsonl`
31
+ // beside its parent's, and is a session of its own: spawned by the session
32
+ // whose directory holds it, from the call its `agent-<id>.meta.json` names,
33
+ // and read like any other, as it is written.
34
+ //
35
+ // A session keeps its full depth for `full` days after it last said anything.
36
+ // Once an hour the duty finds every imported session older than that, from
37
+ // whichever importer, and strips it to its prose: each imported entry the
38
+ // prose-only read would have left out goes, a small batch between looks, so
39
+ // what a session says now never waits behind it. What a person typed and what
40
+ // the model said stay, and so does everything nobody imported.
41
+ import { line, sub } from "@yaks/query";
42
+ import * as fs from "@yaks/file/io";
43
+ import { sleep } from "@yaks/fp";
44
+ import { TOMBSTONE } from "@yaks/graph/bundle";
45
+ import { follow } from "./bus.js";
46
+ import { SESSION } from "./comp.js";
47
+ import { accounted, antigravity, claude, codexRollout, grok, subagent } from "./readers.js";
48
+ import { reapLeases } from "./reap.js";
49
+ import { endGone } from "./ended.js";
50
+ import { behind, callOf, pull, recount, rest, tail, transcriptOf } from "./tail.js";
51
+ import { sessionEid, sessionFor } from "./who.js";
52
+ import { step, walker } from "./parts.js";
53
+ import { archive } from "./archive.js";
54
+ import { accountRows, header, held, homes, runningOf, transcriptsOf, versionOf } from "./files.js";
55
+ export { held, homes, rollouts, transcripts } from "./files.js";
56
+ let str = (v)=>typeof v == 'string' ? v : '';
57
+ /** How long a session keeps its full depth once it stops being written. */ export let FULL = 14 * 24 * 60 * 60 * 1000;
58
+ /** Whether a session is a managed run: one @yaks/spawn launches, claiming
59
+ * its `process` before the launch, and reads from the run's own stream, so
60
+ * the transcript file the run also keeps is not read a second time. A model
61
+ * taking a conversation over here (@yaks/harness `session_send`) makes none:
62
+ * the harness that ran it may write to its file again. */ export let managed = async (g, session)=>{
63
+ if (!g.vocab.comp('process')) return false;
64
+ let [s] = await g.get([
65
+ session
66
+ ], [
67
+ 'process'
68
+ ]);
69
+ return !!s?.process;
70
+ };
71
+ // What the harness says of a subagent in the meta file beside its
72
+ // transcript: the call that started it, and what that call said it was for.
73
+ let meta = (path)=>{
74
+ try {
75
+ let m = JSON.parse(fs.readTextFileSync(`${path.slice(0, -'.jsonl'.length)}.meta.json`));
76
+ let said = (v)=>typeof v == 'string' && v ? v : undefined;
77
+ return {
78
+ call: said(m?.toolUseId),
79
+ title: said(m?.description)
80
+ };
81
+ } catch {
82
+ return {} // no meta file: the link names no call
83
+ ;
84
+ }
85
+ };
86
+ // A subagent's session, linked to the session that started it and to the call
87
+ // that did, where the graph holds it, and titled by what that call said it
88
+ // was for: written the first time its transcript is opened, with the parent
89
+ // where the graph has not met it yet. One the hook door met first, or one
90
+ // opened before it was titled, gains its call and title then.
91
+ let spawned = async (g, f)=>{
92
+ let eid = sessionEid(f.id, f.parent);
93
+ let { call: id, title } = meta(f.path);
94
+ let doc = title && g.vocab.comp('doc') ? {
95
+ doc: {
96
+ title
97
+ }
98
+ } : {};
99
+ let [s] = await g.get([
100
+ eid
101
+ ], [
102
+ 'spawned',
103
+ 'doc'
104
+ ]);
105
+ let parent = s?.spawned && s[TOMBSTONE] == null ? str(s.spawned.parent) : (await sessionFor(g, f.parent, [
106
+ 'session.id'
107
+ ]))?.entity.eid;
108
+ let call = parent && id ? callOf(parent, id) : undefined;
109
+ let [made] = call && !s?.spawned?.call ? await g.get([
110
+ call
111
+ ]) : [];
112
+ let linked = made?.entry && made[TOMBSTONE] == null ? {
113
+ call
114
+ } : {};
115
+ if (s?.spawned && s[TOMBSTONE] == null) {
116
+ let more = {
117
+ ...s.doc ? {} : doc,
118
+ ...linked.call ? {
119
+ spawned: linked
120
+ } : {}
121
+ };
122
+ if (Object.keys(more).length) {
123
+ await g.apply([
124
+ {
125
+ entity: {
126
+ eid
127
+ },
128
+ ...more
129
+ }
130
+ ], {
131
+ trusted: true,
132
+ stamp: false
133
+ });
134
+ }
135
+ return eid;
136
+ }
137
+ await g.apply([
138
+ ...parent ? [] : [
139
+ {
140
+ entity: {
141
+ eid: '$parent'
142
+ },
143
+ [SESSION]: {
144
+ id: f.parent
145
+ }
146
+ }
147
+ ],
148
+ {
149
+ entity: {
150
+ eid
151
+ },
152
+ [SESSION]: {
153
+ id: f.id
154
+ },
155
+ spawned: {
156
+ parent: parent ?? '$parent',
157
+ ...linked
158
+ },
159
+ ...doc
160
+ }
161
+ ]);
162
+ return eid;
163
+ };
164
+ // Write what the graph does not already hold as given. A row it holds is being
165
+ // corrected, which is no edit to it, so its `updated` stays; only a new row is
166
+ // stamped. A write stamps even when it changes nothing, so a transcript opened
167
+ // again by each new process would otherwise rewrite every session it names.
168
+ let correct = async (g, bundles)=>{
169
+ let held = new Map((await g.get(bundles.map((b)=>b.entity.eid))).map((b)=>[
170
+ b.entity.eid,
171
+ b
172
+ ]));
173
+ let isNew = (b)=>{
174
+ let had = held.get(b.entity.eid);
175
+ return !had || had[TOMBSTONE] != null;
176
+ };
177
+ let lacking = bundles.filter((b)=>isNew(b) || Object.entries(b).some(([c, comp])=>c != 'entity' && Object.entries(comp).some(([p, v])=>held.get(b.entity.eid)[c]?.[p] !== v)));
178
+ let fresh = lacking.filter(isNew);
179
+ let known = lacking.filter((b)=>!isNew(b));
180
+ if (fresh.length) await g.apply(fresh, {
181
+ trusted: true
182
+ });
183
+ if (known.length) await g.apply(known, {
184
+ trusted: true,
185
+ stamp: false
186
+ });
187
+ };
188
+ // A tail on a transcript, or null where its session is a managed run.
189
+ let opened = async (g, f, signal)=>{
190
+ if (f.runner == 'codex') {
191
+ let s = await sessionFor(g, f.id, [
192
+ 'session.id',
193
+ 'session.actor',
194
+ 'session.persona'
195
+ ]);
196
+ if (s && await managed(g, s.entity.eid)) return null;
197
+ let version = f.path.endsWith('.zst') ? versionOf(await stat(f.path)) : undefined;
198
+ let read = version ? await transcriptOf(g, f.path) : undefined;
199
+ if (version && read?.finished == version) return null;
200
+ let stream = version ? archive(f.path, signal) : undefined;
201
+ let prefix = '';
202
+ let meta;
203
+ try {
204
+ if (stream) {
205
+ while(!prefix.includes('\n') && !stream.done && !signal?.aborted && prefix.length < 1024 * 1024){
206
+ prefix += await stream.read() ?? '';
207
+ }
208
+ try {
209
+ meta = JSON.parse(prefix.split('\n')[0]).payload ?? {};
210
+ } catch {
211
+ meta = {};
212
+ }
213
+ } else meta = header(f.path);
214
+ if (signal?.aborted) {
215
+ await stream?.close();
216
+ return null;
217
+ }
218
+ let source = meta.source;
219
+ f.parent = source?.subagent?.thread_spawn?.parent_thread_id;
220
+ // A thread is the session that already holds its id, a managed run's
221
+ // above all, whose eid is its own and not the thread's; only one the
222
+ // graph has not met takes its eid from the id. A subagent's parent is
223
+ // found the same way, so it is spawned by the run that started it and
224
+ // works as that run's persona.
225
+ let eid = s?.entity.eid ?? sessionEid(f.id);
226
+ let up = f.parent ? (await sessionFor(g, f.parent, [
227
+ 'session.id'
228
+ ]))?.entity.eid : undefined;
229
+ let parent = f.parent && (up ?? sessionEid(f.parent));
230
+ // `codex exec` is a program's run, so its first words are a brief, not a
231
+ // person's. A session wears what its checkout carries, as a hook says it
232
+ // does for Claude Code (./tools.ts); Codex has no hook to say it.
233
+ let automatic = !!parent || meta.source == 'exec';
234
+ let named = s?.[SESSION];
235
+ let worn = !parent && !named?.actor && !named?.persona && typeof meta.cwd == 'string' ? await (await import("@yaks/persona")).carried(g, meta.cwd) : undefined;
236
+ await correct(g, [
237
+ ...parent && !up ? [
238
+ {
239
+ entity: {
240
+ eid: parent
241
+ },
242
+ session: {
243
+ id: f.parent
244
+ }
245
+ }
246
+ ] : [],
247
+ {
248
+ entity: {
249
+ eid
250
+ },
251
+ session: {
252
+ id: f.id,
253
+ ...automatic ? {
254
+ operator: false
255
+ } : {},
256
+ ...worn ? {
257
+ persona: worn.persona.entity.eid
258
+ } : {}
259
+ },
260
+ ...parent ? {
261
+ spawned: {
262
+ parent
263
+ }
264
+ } : {}
265
+ }
266
+ ]);
267
+ return await tail(g, f.path, {
268
+ session: eid,
269
+ id: f.id,
270
+ stream,
271
+ prefix,
272
+ version
273
+ });
274
+ } catch (error) {
275
+ await stream?.close();
276
+ throw error;
277
+ }
278
+ }
279
+ if (f.parent) {
280
+ return tail(g, f.path, {
281
+ session: await spawned(g, {
282
+ ...f,
283
+ parent: f.parent
284
+ }),
285
+ id: f.id
286
+ });
287
+ }
288
+ let s = await sessionFor(g, f.id, [
289
+ 'session.id'
290
+ ]);
291
+ if (s && await managed(g, s.entity.eid)) return null;
292
+ return tail(g, f.path, {
293
+ ...s ? {
294
+ session: s.entity.eid
295
+ } : {},
296
+ id: f.id
297
+ });
298
+ };
299
+ // How a transcript's lines read: a subagent's are all side conversations.
300
+ let format = (f, t = {})=>f.runner == 'grok' ? accounted(grok, f.account) : f.runner == 'antigravity' ? accounted(antigravity, f.account) : f.runner == 'codex' ? (e)=>{
301
+ let said = accounted(codexRollout, f.account)(e);
302
+ if (e.type == 'session_meta' && e.payload?.id != f.id) return {
303
+ entries: []
304
+ };
305
+ if (f.parent || t.operator === false) {
306
+ said.entries = said.entries.map((c)=>c.content && !c.output && !c.result ? {
307
+ ...c,
308
+ notice: {}
309
+ } : c);
310
+ }
311
+ return said;
312
+ } : f.parent ? subagent : claude;
313
+ let stat = async (path)=>{
314
+ try {
315
+ return await fs.stat(path);
316
+ } catch {
317
+ return undefined;
318
+ }
319
+ };
320
+ let pending = (t, size)=>behind(t) || (t.stream ? !t.stream.done : t.at < size);
321
+ /** How long a look reads one transcript before it turns to the next, in
322
+ * milliseconds. */ export let SLICE = 100;
323
+ /** How long a transcript is unwritten before a look marks the lines it read
324
+ * that made no entry (./tail.ts `rest`), in milliseconds. */ export let QUIET = 5000;
325
+ /** How long one look reads in all, in milliseconds. */ export let BUDGET = 1000;
326
+ /** How often a look lists every transcript again, in milliseconds. Listing
327
+ * and asking after each of the thousands a box keeps is a look's dearest
328
+ * part, so the looks between listings ask only after the transcripts open or
329
+ * written in the last {@link HOT}, and the next one to read in. A new
330
+ * transcript, or an old one written again, is found by the next listing. */ export let SCAN = 30_000;
331
+ /** How lately a transcript was written for the looks between listings to ask
332
+ * after it, in milliseconds. */ export let HOT = 10 * 60_000;
333
+ // Every transcript, listed again once `scan` has passed since the last
334
+ // listing; `all` says whether this look listed them.
335
+ let listed = async (from, seen, now, scan)=>{
336
+ let last = seen.listed;
337
+ if (last && now - last.at < scan) return {
338
+ files: last.files,
339
+ all: false
340
+ };
341
+ let files = await held(from);
342
+ seen.listed = {
343
+ at: now,
344
+ files
345
+ };
346
+ return {
347
+ files,
348
+ all: true
349
+ };
350
+ };
351
+ /**
352
+ * One look at every transcript the accounts `from` keep: each written to in the last
353
+ * `full` milliseconds is read on at full depth, and one older transcript not
354
+ * yet read to its end is read in, its prose only. The one least behind is read
355
+ * first, each for a `slice` (and at least a line), until the look has read for
356
+ * its `budget`: a long backlog is read over many looks, and what a live
357
+ * session says next never waits behind it. A look whose `signal` aborts stops
358
+ * between lines, and the next process reads on from there. Every transcript is
359
+ * listed again only each `scan` ({@link SCAN}).
360
+ */ export let look = async (g, from, seen, o = {})=>{
361
+ let now = o.now ?? Date.now();
362
+ let full = o.full ?? FULL;
363
+ let start = performance.now();
364
+ let spent = ()=>performance.now() - start >= (o.budget ?? BUDGET);
365
+ let old;
366
+ let read = ()=>({
367
+ person: o.person,
368
+ signal: o.signal,
369
+ until: performance.now() + (o.slice ?? SLICE)
370
+ });
371
+ // Each transcript with lines to read: how far behind a tail on it is, or
372
+ // when it was written where none is open yet.
373
+ let owed = [];
374
+ let { files, all } = await listed(from, seen, now, o.scan ?? SCAN);
375
+ let written = seen.written ??= new Map();
376
+ for (let f of files){
377
+ let t = seen.tails.get(f.path);
378
+ if (!all) {
379
+ // Between listings: what is open or written lately, and the next old
380
+ // transcript to read in.
381
+ let was = written.get(f.path);
382
+ let hot = !!t || was != null && now - was < HOT;
383
+ let next = !old && was != null && !seen.done.has(f.path);
384
+ if (!hot && !next) continue;
385
+ }
386
+ let st = await stat(f.path);
387
+ if (st) written.set(f.path, st.mtime?.getTime() ?? 0);
388
+ if (f.path.endsWith('.zst')) {
389
+ let version = versionOf(st);
390
+ seen.versions ??= new Map();
391
+ if (seen.versions.has(f.path) && seen.versions.get(f.path) != version) {
392
+ await t?.stream?.close();
393
+ seen.tails.delete(f.path);
394
+ seen.done.delete(f.path);
395
+ seen.retry?.delete(f.path);
396
+ t = undefined;
397
+ }
398
+ seen.versions.set(f.path, version);
399
+ }
400
+ if ((seen.retry?.get(f.path) ?? 0) > now) continue;
401
+ let at = st?.mtime?.getTime() ?? 0;
402
+ if (!st || t === null) continue;
403
+ if (t && !pending(t, st.size)) {
404
+ // Read to a file now quiet: its last lines are marked read.
405
+ if (now - at >= QUIET) await rest(g, t).catch(o.report ?? console.error);
406
+ continue;
407
+ }
408
+ if (now - at >= full) {
409
+ if (!seen.done.has(f.path)) old ??= {
410
+ f,
411
+ at,
412
+ prose: true
413
+ };
414
+ continue;
415
+ }
416
+ owed.push({
417
+ f,
418
+ at,
419
+ ...t ? {
420
+ lag: st.size - t.at + t.rest.length
421
+ } : {}
422
+ });
423
+ }
424
+ // The tails already open, least behind first, then the rest, most recently
425
+ // written first: a live session's next lines lead every look. Of the rest,
426
+ // sessions come before subagents, as `transcripts()` lists them: a subagent
427
+ // writes after the call that started it, so its transcript is the newer one,
428
+ // and opened first it would find no call to link to.
429
+ owed.sort((a, b)=>a.lag != null && b.lag != null ? a.lag - b.lag : a.lag != null ? -1 : b.lag != null ? 1 : Number(!!a.f.parent) - Number(!!b.f.parent) || b.at - a.at);
430
+ // One slice cannot be monopolized by growing tails. The rest of the look
431
+ // retains live priority; a cold source and old backfill both get a turn.
432
+ let turns = seen.turns ??= new Map();
433
+ let candidates = [
434
+ ...owed,
435
+ ...old ? [
436
+ old
437
+ ] : []
438
+ ].filter(({ f })=>!f.parent || !owed.some((parent)=>parent.f.id == f.parent && !parent.f.parent && !seen.tails.has(parent.f.path)));
439
+ // New sources join behind those already waiting, including when the old
440
+ // backfill finishes a small file and discovers the next one.
441
+ for (let { f } of candidates.toSorted((a, b)=>a.at - b.at)){
442
+ if (!turns.has(f.path)) turns.set(f.path, seen.turn = (seen.turn ?? 0) + 1);
443
+ }
444
+ let fair = candidates.sort((a, b)=>turns.get(a.f.path) - turns.get(b.f.path))[0];
445
+ if (fair) {
446
+ owed = [
447
+ fair,
448
+ ...owed.filter((d)=>d != fair)
449
+ ];
450
+ if (fair == old) old = undefined;
451
+ }
452
+ for (let [i, { f, prose }] of owed.entries()){
453
+ if (o.signal?.aborted || i && spent()) return;
454
+ let t = seen.tails.get(f.path);
455
+ try {
456
+ t ??= await opened(g, f, o.signal);
457
+ seen.tails.set(f.path, t);
458
+ if (t) await pull(g, t, format(f, t), {
459
+ ...read(),
460
+ prose
461
+ });
462
+ turns.set(f.path, seen.turn = (seen.turn ?? 0) + 1);
463
+ if (prose && !o.signal?.aborted && !(t && pending(t, (await stat(f.path))?.size ?? 0))) {
464
+ seen.done.add(f.path);
465
+ seen.tails.delete(f.path);
466
+ }
467
+ } catch (error) {
468
+ seen.tails.delete(f.path);
469
+ await t?.stream?.close();
470
+ if (!f.path.endsWith('.zst')) throw error;
471
+ (seen.retry ??= new Map()).set(f.path, now + 60000);
472
+ (o.report ?? console.error)(error);
473
+ }
474
+ }
475
+ if (!old || o.signal?.aborted || owed.length && spent()) return;
476
+ // Keep the cursor and unread bytes between backfill slices.
477
+ let f = old.f;
478
+ let t = seen.tails.get(f.path);
479
+ try {
480
+ t ??= await opened(g, f, o.signal);
481
+ seen.tails.set(f.path, t);
482
+ if (t) await pull(g, t, format(f, t), {
483
+ ...read(),
484
+ prose: true
485
+ });
486
+ turns.set(f.path, seen.turn = (seen.turn ?? 0) + 1);
487
+ } catch (error) {
488
+ seen.tails.delete(f.path);
489
+ await t?.stream?.close();
490
+ if (!f.path.endsWith('.zst')) throw error;
491
+ (seen.retry ??= new Map()).set(f.path, now + 60000);
492
+ (o.report ?? console.error)(error);
493
+ return;
494
+ }
495
+ if (!o.signal?.aborted && !(t && pending(t, (await stat(f.path))?.size ?? 0))) {
496
+ seen.done.add(f.path);
497
+ seen.tails.delete(f.path);
498
+ }
499
+ };
500
+ // What a strip takes from an imported entry: all of it where there is no
501
+ // prose (a call), and each whose prose sits beside a kind the prose-only read
502
+ // leaves out (`prose` in ./tail.ts), of the kinds the vocabulary has. An entry
503
+ // that records what a turn cost stays, since the session's `cost` is its sum.
504
+ let STRIPPED = [
505
+ 'reasoning',
506
+ 'notice',
507
+ 'result',
508
+ 'error',
509
+ 'refusal',
510
+ 'exception',
511
+ 'stop',
512
+ 'usage'
513
+ ];
514
+ let stripped = (g)=>{
515
+ let kept = STRIPPED.filter((c)=>g.vocab.comp(c)).reduce((q, c)=>line`${sub(q)}|.${c}`, line`!content`);
516
+ return g.vocab.comp('cost') ? line`.lines&!using&!cost&(${sub(kept)})` : line`.lines&!using&(${sub(kept)})`;
517
+ };
518
+ /**
519
+ * One bounded page of imported entries to strip from a quiet session. Empty
520
+ * pages give the duty its turn back after a recent or already stripped session.
521
+ * The newest entry is checked before every page, so a session that speaks again
522
+ * keeps its remaining depth. Identity cursors survive the entries' deletion.
523
+ */ export let stale = async function*(g, o = {}) {
524
+ let before = new Date((o.now ?? Date.now()) - (o.full ?? FULL)).toISOString();
525
+ let page = (query, after, limit = 1)=>after ? line`${sub(query)} .fields=entity.eid .order=entity.eid .limit=${limit} .after=${after}` : line`${sub(query)} .fields=entity.eid .order=entity.eid .limit=${limit}`;
526
+ let after;
527
+ while(!o.signal?.aborted){
528
+ let [found] = await g.rows(page(line`.session`, after));
529
+ if (!found) return;
530
+ after = found.eid;
531
+ let cursor;
532
+ while(!o.signal?.aborted){
533
+ let [last] = await g.rows(line`.entry.session=${after} .order=-entry.seq .limit=1 .fields=created.at`);
534
+ if ((last?.['created.at'] ?? '') >= before) break;
535
+ let rows = await g.rows(page(line`${sub(stripped(g))} .entry.session=${after}`, cursor, BATCH));
536
+ if (!rows.length) break;
537
+ cursor = rows[rows.length - 1].eid;
538
+ yield rows.map((b)=>b.eid);
539
+ }
540
+ yield [];
541
+ }
542
+ };
543
+ /** How many entries one step of a strip deletes. */ export let BATCH = 50;
544
+ /** One step of a strip: these entries, deleted. */ export let strip = async (g, eids)=>{
545
+ if (eids.length) {
546
+ await g.apply(eids.map((eid)=>({
547
+ entity: {
548
+ eid
549
+ },
550
+ $delete: true
551
+ })));
552
+ }
553
+ };
554
+ // How often the duty looks for sessions to strip.
555
+ let HOUR = 60 * 60 * 1000;
556
+ /** Free the locks whose holder is gone, then read transcripts in, end the
557
+ * sessions whose process is gone and strip the stale sessions until `signal`
558
+ * aborts. A pass that fails is logged and
559
+ * made again on the next, where the transcript stands. What the config's
560
+ * `person` typed is signed with them. */ export let service = async (host, options = {}, signal = AbortSignal.abort())=>{
561
+ await reapLeases(host.graph.storage);
562
+ if (signal.aborted) return;
563
+ let bus = follow(host, signal, {
564
+ every: options.every,
565
+ quiet: options.quiet
566
+ });
567
+ let said = host.config?.person;
568
+ let person = said && ((await host.graph.address([
569
+ said
570
+ ])).get(said) ?? said);
571
+ let seen = {
572
+ tails: new Map(),
573
+ done: new Set()
574
+ };
575
+ let trimming;
576
+ let due = 0;
577
+ let reconcile = 0;
578
+ let from = [];
579
+ let named = new Set();
580
+ let found = 0;
581
+ try {
582
+ while(!signal.aborted){
583
+ try {
584
+ // The accounts are found again as often as transcripts are listed,
585
+ // so one signed in while the server runs is read too.
586
+ if (Date.now() >= found) {
587
+ found = Date.now() + (options.scan ?? SCAN);
588
+ from = await homes(options.homes, options.find);
589
+ let fresh = from.filter((h)=>!named.has(h.dir));
590
+ if (fresh.length) {
591
+ await host.graph.apply(accountRows(fresh), {
592
+ trusted: true
593
+ });
594
+ }
595
+ for (let h of fresh)named.add(h.dir);
596
+ }
597
+ await look(host.graph, from, seen, {
598
+ ...person ? {
599
+ person: person
600
+ } : {},
601
+ full: options.full,
602
+ scan: options.scan,
603
+ signal
604
+ });
605
+ if (Date.now() >= reconcile) {
606
+ reconcile = Date.now() + HOUR;
607
+ await endGone(host.graph, {
608
+ claude: from.filter((h)=>h.runner == 'claude').map((h)=>({
609
+ projects: transcriptsOf(h),
610
+ running: runningOf(h)
611
+ }))
612
+ });
613
+ }
614
+ if (!trimming && Date.now() >= due) {
615
+ trimming = stale(host.graph, {
616
+ full: options.full,
617
+ signal
618
+ });
619
+ }
620
+ if (trimming) {
621
+ let page;
622
+ try {
623
+ page = await trimming.next();
624
+ } catch (e) {
625
+ trimming = undefined;
626
+ throw e;
627
+ }
628
+ if (page.done) {
629
+ trimming = undefined;
630
+ due = Date.now() + HOUR;
631
+ } else {
632
+ // The iterator has advanced before deletion, so a failed page is
633
+ // skipped until the next hourly pass rather than retried forever.
634
+ await strip(host.graph, page.value);
635
+ }
636
+ }
637
+ } catch (e) {
638
+ console.error('transcripts —', e);
639
+ }
640
+ await sleep(options.every ?? 1000, signal);
641
+ }
642
+ } finally{
643
+ await bus;
644
+ await Promise.all([
645
+ ...seen.tails.values()
646
+ ].map((t)=>t?.stream?.close()));
647
+ }
648
+ };
649
+ // How many model requests a session's entries hold.
650
+ let requests = async (g, session)=>session ? (await g.rows(line`.entry.session=${session} .ask .usage .fields=entity.eid`)).length : 0;
651
+ // A transcript read to its end, as a look reads it but with no budget: at
652
+ // full depth where it was written in the last `full` milliseconds, else its
653
+ // prose. A managed run's is left alone. Answers how many requests it read.
654
+ let through = async (g, f, o)=>{
655
+ let st = await stat(f.path);
656
+ if (!st) return 0;
657
+ let prose = o.now - (st.mtime?.getTime() ?? 0) >= o.full;
658
+ let t = null;
659
+ try {
660
+ t = await opened(g, f, o.signal);
661
+ if (!t || !pending(t, st.size)) return 0;
662
+ let had = await requests(g, t.session);
663
+ while(!o.signal?.aborted && pending(t, st.size)){
664
+ let at = t.at;
665
+ await pull(g, t, format(f, t), {
666
+ ...o,
667
+ prose
668
+ });
669
+ if (t.at == at && !behind(t) && !t.stream) break;
670
+ }
671
+ await rest(g, t);
672
+ return await requests(g, t.session) - had;
673
+ } catch (error) {
674
+ if (!f.path.endsWith('.zst')) throw error;
675
+ console.error(error);
676
+ return 0;
677
+ } finally{
678
+ await t?.stream?.close();
679
+ }
680
+ };
681
+ /**
682
+ * Read every transcript written since `since` to its end, as the duty would
683
+ * but at once, then again for its model requests (./tail.ts `recount`), and
684
+ * walk the graph's own sessions for theirs: what was written before requests
685
+ * were read and measured; and title each subagent read before sessions were
686
+ * titled. What `person` typed is signed with them. A managed run's
687
+ * transcript is @yaks/spawn's (`recounted` there). Answers how many requests
688
+ * it measured.
689
+ *
690
+ * ```ts
691
+ * import { recounted } from '@yaks/session/service'
692
+ *
693
+ * // await recounted(graph, '2026-10-13')
694
+ * ```
695
+ */ export let recounted = async (g, since, o = {})=>{
696
+ let after = Date.parse(since);
697
+ let now = o.now ?? Date.now();
698
+ let from = await homes(o.homes, o.find);
699
+ if (from.length) await g.apply(accountRows(from), {
700
+ trusted: true
701
+ });
702
+ let all = await held(from);
703
+ let found = [];
704
+ for (let f of all){
705
+ if (((await stat(f.path))?.mtime?.getTime() ?? 0) >= after) found.push(f);
706
+ }
707
+ let wrote = 0;
708
+ for (let f of found){
709
+ if (o.signal?.aborted) break;
710
+ wrote += await through(g, f, {
711
+ person: o.person,
712
+ full: o.full ?? FULL,
713
+ now,
714
+ signal: o.signal
715
+ });
716
+ }
717
+ for (let f of found.filter((f)=>!f.path.endsWith('.zst'))){
718
+ if (o.signal?.aborted) break;
719
+ let session = (await transcriptOf(g, f.path))?.session;
720
+ if (!session) continue;
721
+ if (await managed(g, String(session))) continue;
722
+ // A subagent read before sessions were titled is titled now.
723
+ if (f.runner != 'codex' && f.parent) {
724
+ await spawned(g, {
725
+ ...f,
726
+ parent: f.parent
727
+ });
728
+ }
729
+ let [s] = await g.get([
730
+ String(session)
731
+ ], [
732
+ SESSION
733
+ ]);
734
+ if (f.runner == 'codex') {
735
+ let source = header(f.path).source;
736
+ f.parent = source?.subagent?.thread_spawn?.parent_thread_id;
737
+ }
738
+ let operator = s?.[SESSION]?.operator;
739
+ wrote += await recount(g, f.path, format(f, {
740
+ operator
741
+ }), {
742
+ session: String(session),
743
+ signal: o.signal
744
+ });
745
+ }
746
+ return wrote + await natives(g, since, o.signal);
747
+ };
748
+ /**
749
+ * The copy each provider keeps of its threads on this machine (Claude Code's
750
+ * transcripts, Codex's rollouts), by the provider's id for the thread: where
751
+ * it is and how its lines read. A managed run's own log can be tidied away
752
+ * while its provider's copy stays (@yaks/spawn `recounted`).
753
+ *
754
+ * ```ts
755
+ * import { kept } from '@yaks/session/service'
756
+ *
757
+ * // let copy = (await kept()).get(thread)
758
+ * ```
759
+ */ export let kept = async (o = {})=>new Map((await held(await homes(o.homes, o.find))).filter((f)=>!f.parent && !f.path.endsWith('.zst')).map((f)=>[
760
+ f.id,
761
+ {
762
+ path: f.path,
763
+ read: format(f)
764
+ }
765
+ ]));
766
+ // What each request of the graph's own sessions carried (./parts.ts), walked
767
+ // from their entries as the graph holds them, for the requests written before
768
+ // the runner recorded it at send time (./react.ts). Their fixed part never
769
+ // reached the graph, so it is sized from each session's first request.
770
+ let natives = async (g, since, signal)=>{
771
+ let owed = await g.rows(line`.ask .usage !lines !context_parts .created.at>=${since} .fields=entry.session`);
772
+ let sessions = [
773
+ ...new Set(owed.map((r)=>r['entry.session']))
774
+ ].filter((s)=>!!s);
775
+ let names = new Map((await g.read('.tool *')).map((b)=>[
776
+ b.entity.eid,
777
+ String(b.tool.name)
778
+ ]));
779
+ let wrote = 0;
780
+ for (let session of sessions){
781
+ if (signal?.aborted) break;
782
+ let w = walker({
783
+ names
784
+ });
785
+ let [held] = await g.get([
786
+ session
787
+ ], [
788
+ 'fixed'
789
+ ]);
790
+ let bundles = [];
791
+ for (let b of (await g.read(line`.entry.session=${session} .order=entry.seq *`))){
792
+ let made = step(w, b);
793
+ if (!b.usage || b.context_parts) continue;
794
+ bundles.push(...made.filter((m)=>!m.fixed || !held?.fixed));
795
+ }
796
+ for(let i = 0; i < bundles.length; i += BATCH){
797
+ await g.apply(bundles.slice(i, i + BATCH), {
798
+ trusted: true,
799
+ stamp: false
800
+ });
801
+ }
802
+ wrote += bundles.filter((b)=>b.context_parts).length;
803
+ }
804
+ return wrote;
805
+ };
806
+ //# sourceMappingURL=service.js.map