@neta-art/cohub-cli 10.24.0 → 10.25.1

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 (369) hide show
  1. package/bin/cohub.js +4 -1
  2. package/dist/board-gpu/page.js +1566 -71230
  3. package/dist/bootstrap.js +67 -27
  4. package/dist/build.json +1 -1
  5. package/dist/{board-kernel/FilterPipe.js → chunks/Geometry.js} +10447 -10469
  6. package/dist/chunks/about.js +102 -0
  7. package/dist/chunks/app-realtime.js +2216 -0
  8. package/dist/chunks/app-realtime2.js +2 -0
  9. package/dist/chunks/app-ref.js +88 -0
  10. package/dist/chunks/apps.js +1423 -0
  11. package/dist/chunks/auth.js +175 -0
  12. package/dist/chunks/avatar.js +146 -0
  13. package/dist/chunks/binary-pins.js +5 -0
  14. package/dist/chunks/board-export-resources.js +232 -0
  15. package/dist/chunks/board-export.js +874 -0
  16. package/dist/chunks/board-export2.js +2 -0
  17. package/dist/{board-kernel.js → chunks/board-kernel.js} +18032 -28512
  18. package/dist/chunks/board-kernel2.js +4 -0
  19. package/dist/chunks/board-profile.js +69 -0
  20. package/dist/chunks/board-simulate.js +315 -0
  21. package/dist/chunks/board-time.js +34 -0
  22. package/dist/chunks/boards.js +1943 -0
  23. package/dist/{board-kernel → chunks}/browserAll.js +9 -8
  24. package/dist/chunks/channels.js +64 -0
  25. package/dist/chunks/client.js +12606 -0
  26. package/dist/chunks/command-input.js +17 -0
  27. package/dist/chunks/commander.js +2962 -0
  28. package/dist/chunks/cron-jobs.js +163 -0
  29. package/dist/chunks/daemon.js +545 -0
  30. package/dist/chunks/daemon2.js +2 -0
  31. package/dist/chunks/desktop.js +172 -0
  32. package/dist/chunks/endpoint-options.js +26 -0
  33. package/dist/chunks/env-proxy.js +35 -0
  34. package/dist/chunks/environment.js +154 -0
  35. package/dist/chunks/feedback.js +120 -0
  36. package/dist/chunks/generations.js +379 -0
  37. package/dist/chunks/host.js +9 -0
  38. package/dist/chunks/http-put.js +131 -0
  39. package/dist/chunks/id.js +2416 -0
  40. package/dist/{board-kernel → chunks}/init.js +1 -1
  41. package/dist/chunks/install.js +11959 -0
  42. package/dist/chunks/launch.js +374 -0
  43. package/dist/chunks/local-media.js +47 -0
  44. package/dist/chunks/me.js +134 -0
  45. package/dist/chunks/media.js +315 -0
  46. package/dist/chunks/models.js +242 -0
  47. package/dist/chunks/operands.js +46 -0
  48. package/dist/chunks/package-paths.js +13 -0
  49. package/dist/chunks/policy.js +221 -0
  50. package/dist/chunks/preferences.js +29 -0
  51. package/dist/chunks/profile.js +64 -0
  52. package/dist/chunks/prompts.js +41 -0
  53. package/dist/chunks/public.js +2 -0
  54. package/dist/chunks/public2.js +244 -0
  55. package/dist/chunks/references.js +197 -0
  56. package/dist/chunks/referrals.js +90 -0
  57. package/dist/chunks/rolldown-runtime.js +26 -0
  58. package/dist/chunks/rooms-connect.js +2 -0
  59. package/dist/chunks/rooms-connect2.js +151 -0
  60. package/dist/chunks/rooms-rtc.js +311 -0
  61. package/dist/chunks/rooms.js +211 -0
  62. package/dist/chunks/run.js +220 -0
  63. package/dist/chunks/runtime.js +638 -0
  64. package/dist/chunks/runtime2.js +286 -0
  65. package/dist/chunks/runtimes.js +48 -0
  66. package/dist/chunks/safe-remote-image.js +198 -0
  67. package/dist/chunks/schemas.js +6017 -0
  68. package/dist/chunks/search.js +113 -0
  69. package/dist/chunks/self-update.js +238 -0
  70. package/dist/chunks/serve.js +1246 -0
  71. package/dist/chunks/session-status.js +24 -0
  72. package/dist/chunks/skills.js +35 -0
  73. package/dist/chunks/spaces.js +5080 -0
  74. package/dist/chunks/src.js +405 -0
  75. package/dist/chunks/supervisor.js +2513 -0
  76. package/dist/chunks/supervisor2.js +2 -0
  77. package/dist/chunks/tasks.js +82 -0
  78. package/dist/chunks/themes.js +2898 -0
  79. package/dist/chunks/version.js +2 -0
  80. package/dist/chunks/version2.js +22 -0
  81. package/dist/chunks/version3.js +18 -0
  82. package/dist/chunks/web.js +7 -0
  83. package/dist/{board-kernel → chunks}/webworkerAll.js +1 -1
  84. package/dist/runtime/native/pi-extension.js +174 -166
  85. package/dist/runtime/worker.js +27 -16
  86. package/package.json +8 -9
  87. package/dist/about.d.ts +0 -2
  88. package/dist/about.js +0 -71
  89. package/dist/app-download.d.ts +0 -11
  90. package/dist/app-download.js +0 -252
  91. package/dist/app-ref.d.ts +0 -5
  92. package/dist/app-ref.js +0 -8
  93. package/dist/app-target.d.ts +0 -26
  94. package/dist/app-target.js +0 -55
  95. package/dist/auth.d.ts +0 -45
  96. package/dist/auth.js +0 -307
  97. package/dist/avatar.d.ts +0 -55
  98. package/dist/avatar.js +0 -104
  99. package/dist/board-command-support.d.ts +0 -6
  100. package/dist/board-command-support.js +0 -88
  101. package/dist/board-export-resources.d.ts +0 -12
  102. package/dist/board-export-resources.js +0 -51
  103. package/dist/board-export.d.ts +0 -110
  104. package/dist/board-export.js +0 -592
  105. package/dist/board-fonts.d.ts +0 -11
  106. package/dist/board-fonts.js +0 -10
  107. package/dist/board-gpu/daemon.d.ts +0 -11
  108. package/dist/board-gpu/daemon.js +0 -295
  109. package/dist/board-gpu/index.d.ts +0 -33
  110. package/dist/board-gpu/index.js +0 -189
  111. package/dist/board-gpu/protocol.d.ts +0 -77
  112. package/dist/board-gpu/protocol.js +0 -1
  113. package/dist/board-kernel.d.ts +0 -5
  114. package/dist/board-media-export.d.ts +0 -17
  115. package/dist/board-media-export.js +0 -164
  116. package/dist/board-media.d.ts +0 -5
  117. package/dist/board-media.js +0 -66
  118. package/dist/board-profile.d.ts +0 -21
  119. package/dist/board-profile.js +0 -67
  120. package/dist/board-simulate.d.ts +0 -67
  121. package/dist/board-simulate.js +0 -259
  122. package/dist/board-time.d.ts +0 -3
  123. package/dist/board-time.js +0 -34
  124. package/dist/bootstrap.d.ts +0 -2
  125. package/dist/client.d.ts +0 -5
  126. package/dist/client.js +0 -24
  127. package/dist/command-input.d.ts +0 -4
  128. package/dist/command-input.js +0 -16
  129. package/dist/commands/app-commerce.d.ts +0 -2
  130. package/dist/commands/app-commerce.js +0 -228
  131. package/dist/commands/app-domains.d.ts +0 -2
  132. package/dist/commands/app-domains.js +0 -133
  133. package/dist/commands/apps.d.ts +0 -4
  134. package/dist/commands/apps.js +0 -827
  135. package/dist/commands/auth.d.ts +0 -2
  136. package/dist/commands/auth.js +0 -186
  137. package/dist/commands/boards/authoring.d.ts +0 -2
  138. package/dist/commands/boards/authoring.js +0 -166
  139. package/dist/commands/boards/examples.d.ts +0 -10
  140. package/dist/commands/boards/examples.js +0 -118
  141. package/dist/commands/boards.d.ts +0 -13
  142. package/dist/commands/boards.js +0 -836
  143. package/dist/commands/channels.d.ts +0 -2
  144. package/dist/commands/channels.js +0 -74
  145. package/dist/commands/cron-jobs.d.ts +0 -2
  146. package/dist/commands/cron-jobs.js +0 -155
  147. package/dist/commands/decision.d.ts +0 -4
  148. package/dist/commands/decision.js +0 -113
  149. package/dist/commands/desktop.d.ts +0 -10
  150. package/dist/commands/desktop.js +0 -233
  151. package/dist/commands/feedback.d.ts +0 -2
  152. package/dist/commands/feedback.js +0 -131
  153. package/dist/commands/generations.d.ts +0 -2
  154. package/dist/commands/generations.js +0 -427
  155. package/dist/commands/host.d.ts +0 -2
  156. package/dist/commands/host.js +0 -11
  157. package/dist/commands/me.d.ts +0 -2
  158. package/dist/commands/me.js +0 -121
  159. package/dist/commands/models.d.ts +0 -28
  160. package/dist/commands/models.js +0 -271
  161. package/dist/commands/profile.d.ts +0 -2
  162. package/dist/commands/profile.js +0 -81
  163. package/dist/commands/prompts.d.ts +0 -2
  164. package/dist/commands/prompts.js +0 -37
  165. package/dist/commands/public.d.ts +0 -20
  166. package/dist/commands/public.js +0 -291
  167. package/dist/commands/references.d.ts +0 -2
  168. package/dist/commands/references.js +0 -193
  169. package/dist/commands/referrals.d.ts +0 -2
  170. package/dist/commands/referrals.js +0 -88
  171. package/dist/commands/rooms-connect.d.ts +0 -17
  172. package/dist/commands/rooms-connect.js +0 -140
  173. package/dist/commands/rooms-rtc-binary.d.ts +0 -2
  174. package/dist/commands/rooms-rtc-binary.js +0 -107
  175. package/dist/commands/rooms-rtc.d.ts +0 -21
  176. package/dist/commands/rooms-rtc.js +0 -171
  177. package/dist/commands/rooms.d.ts +0 -3
  178. package/dist/commands/rooms.js +0 -277
  179. package/dist/commands/run.d.ts +0 -13
  180. package/dist/commands/run.js +0 -246
  181. package/dist/commands/runtime.d.ts +0 -3
  182. package/dist/commands/runtime.js +0 -320
  183. package/dist/commands/sandboxd-binary.d.ts +0 -11
  184. package/dist/commands/sandboxd-binary.js +0 -238
  185. package/dist/commands/search.d.ts +0 -2
  186. package/dist/commands/search.js +0 -114
  187. package/dist/commands/session-access.d.ts +0 -2
  188. package/dist/commands/session-access.js +0 -70
  189. package/dist/commands/skills.d.ts +0 -2
  190. package/dist/commands/skills.js +0 -34
  191. package/dist/commands/space-activity.d.ts +0 -26
  192. package/dist/commands/space-activity.js +0 -147
  193. package/dist/commands/space-commerce.d.ts +0 -2
  194. package/dist/commands/space-commerce.js +0 -514
  195. package/dist/commands/space-displays.d.ts +0 -2
  196. package/dist/commands/space-displays.js +0 -620
  197. package/dist/commands/space-files.d.ts +0 -5
  198. package/dist/commands/space-files.js +0 -215
  199. package/dist/commands/space-invitations.d.ts +0 -29
  200. package/dist/commands/space-invitations.js +0 -189
  201. package/dist/commands/space-turns.d.ts +0 -49
  202. package/dist/commands/space-turns.js +0 -205
  203. package/dist/commands/space-wallet.d.ts +0 -2
  204. package/dist/commands/space-wallet.js +0 -141
  205. package/dist/commands/space-webhooks.d.ts +0 -7
  206. package/dist/commands/space-webhooks.js +0 -73
  207. package/dist/commands/spaces.d.ts +0 -25
  208. package/dist/commands/spaces.js +0 -2170
  209. package/dist/commands/tasks.d.ts +0 -8
  210. package/dist/commands/tasks.js +0 -80
  211. package/dist/commands/version.d.ts +0 -2
  212. package/dist/commands/version.js +0 -21
  213. package/dist/endpoint-options.d.ts +0 -4
  214. package/dist/endpoint-options.js +0 -26
  215. package/dist/env-proxy.d.ts +0 -5
  216. package/dist/env-proxy.js +0 -20
  217. package/dist/help-path.d.ts +0 -18
  218. package/dist/help-path.js +0 -223
  219. package/dist/host/auth.d.ts +0 -20
  220. package/dist/host/auth.js +0 -119
  221. package/dist/host/login-env.d.ts +0 -1
  222. package/dist/host/login-env.js +0 -31
  223. package/dist/host/runtime.d.ts +0 -35
  224. package/dist/host/runtime.js +0 -395
  225. package/dist/host/runtimes.d.ts +0 -9
  226. package/dist/host/runtimes.js +0 -42
  227. package/dist/host/serve.d.ts +0 -2
  228. package/dist/host/serve.js +0 -117
  229. package/dist/host/workspaces.d.ts +0 -2
  230. package/dist/host/workspaces.js +0 -18
  231. package/dist/http-put.d.ts +0 -25
  232. package/dist/http-put.js +0 -132
  233. package/dist/index.d.ts +0 -2
  234. package/dist/index.js +0 -129
  235. package/dist/launcher.d.ts +0 -4
  236. package/dist/launcher.js +0 -38
  237. package/dist/local-media.d.ts +0 -2
  238. package/dist/local-media.js +0 -30
  239. package/dist/network-error.d.ts +0 -3
  240. package/dist/network-error.js +0 -22
  241. package/dist/output.d.ts +0 -46
  242. package/dist/output.js +0 -244
  243. package/dist/runtime/archive-store.d.ts +0 -54
  244. package/dist/runtime/archive-store.js +0 -379
  245. package/dist/runtime/cli-path.d.ts +0 -5
  246. package/dist/runtime/cli-path.js +0 -27
  247. package/dist/runtime/codex-usage.d.ts +0 -7
  248. package/dist/runtime/codex-usage.js +0 -11
  249. package/dist/runtime/connection.d.ts +0 -42
  250. package/dist/runtime/connection.js +0 -789
  251. package/dist/runtime/diagnostics.d.ts +0 -107
  252. package/dist/runtime/diagnostics.js +0 -392
  253. package/dist/runtime/harnesses/chained-projection.d.ts +0 -40
  254. package/dist/runtime/harnesses/chained-projection.js +0 -95
  255. package/dist/runtime/harnesses/chained-transcript.d.ts +0 -24
  256. package/dist/runtime/harnesses/chained-transcript.js +0 -62
  257. package/dist/runtime/harnesses/claude-approval.d.ts +0 -34
  258. package/dist/runtime/harnesses/claude-approval.js +0 -124
  259. package/dist/runtime/harnesses/claude-transcript.d.ts +0 -8
  260. package/dist/runtime/harnesses/claude-transcript.js +0 -138
  261. package/dist/runtime/harnesses/claude.d.ts +0 -8
  262. package/dist/runtime/harnesses/claude.js +0 -225
  263. package/dist/runtime/harnesses/codex-approval.d.ts +0 -25
  264. package/dist/runtime/harnesses/codex-approval.js +0 -69
  265. package/dist/runtime/harnesses/codex.d.ts +0 -4
  266. package/dist/runtime/harnesses/codex.js +0 -269
  267. package/dist/runtime/harnesses/cursor-transcript.d.ts +0 -37
  268. package/dist/runtime/harnesses/cursor-transcript.js +0 -183
  269. package/dist/runtime/harnesses/cursor.d.ts +0 -24
  270. package/dist/runtime/harnesses/cursor.js +0 -620
  271. package/dist/runtime/harnesses/index.d.ts +0 -16
  272. package/dist/runtime/harnesses/index.js +0 -73
  273. package/dist/runtime/harnesses/pi.d.ts +0 -4
  274. package/dist/runtime/harnesses/pi.js +0 -247
  275. package/dist/runtime/harnesses/process.d.ts +0 -31
  276. package/dist/runtime/harnesses/process.js +0 -75
  277. package/dist/runtime/harnesses/stream-json.d.ts +0 -48
  278. package/dist/runtime/harnesses/stream-json.js +0 -336
  279. package/dist/runtime/harnesses/types.d.ts +0 -117
  280. package/dist/runtime/harnesses/types.js +0 -1
  281. package/dist/runtime/harnesses/workbuddy-transcript.d.ts +0 -8
  282. package/dist/runtime/harnesses/workbuddy-transcript.js +0 -127
  283. package/dist/runtime/harnesses/workbuddy.d.ts +0 -21
  284. package/dist/runtime/harnesses/workbuddy.js +0 -123
  285. package/dist/runtime/instance.d.ts +0 -20
  286. package/dist/runtime/instance.js +0 -247
  287. package/dist/runtime/json-rpc.d.ts +0 -53
  288. package/dist/runtime/json-rpc.js +0 -212
  289. package/dist/runtime/launch.d.ts +0 -25
  290. package/dist/runtime/launch.js +0 -247
  291. package/dist/runtime/local-workspace.d.ts +0 -15
  292. package/dist/runtime/local-workspace.js +0 -52
  293. package/dist/runtime/model-catalog.d.ts +0 -12
  294. package/dist/runtime/model-catalog.js +0 -84
  295. package/dist/runtime/native/adapters.d.ts +0 -15
  296. package/dist/runtime/native/adapters.js +0 -11
  297. package/dist/runtime/native/approvals.d.ts +0 -18
  298. package/dist/runtime/native/approvals.js +0 -54
  299. package/dist/runtime/native/archive.d.ts +0 -19
  300. package/dist/runtime/native/archive.js +0 -142
  301. package/dist/runtime/native/attach.d.ts +0 -36
  302. package/dist/runtime/native/attach.js +0 -128
  303. package/dist/runtime/native/codex-link.d.ts +0 -60
  304. package/dist/runtime/native/codex-link.js +0 -322
  305. package/dist/runtime/native/config.d.ts +0 -16
  306. package/dist/runtime/native/config.js +0 -24
  307. package/dist/runtime/native/daemon.d.ts +0 -92
  308. package/dist/runtime/native/daemon.js +0 -380
  309. package/dist/runtime/native/execution.d.ts +0 -26
  310. package/dist/runtime/native/execution.js +0 -34
  311. package/dist/runtime/native/identity.d.ts +0 -9
  312. package/dist/runtime/native/identity.js +0 -15
  313. package/dist/runtime/native/ingest.d.ts +0 -263
  314. package/dist/runtime/native/ingest.js +0 -884
  315. package/dist/runtime/native/install.d.ts +0 -9
  316. package/dist/runtime/native/install.js +0 -113
  317. package/dist/runtime/native/paths.d.ts +0 -2
  318. package/dist/runtime/native/paths.js +0 -7
  319. package/dist/runtime/native/pi-extension.d.ts +0 -36
  320. package/dist/runtime/native/pi-link.d.ts +0 -69
  321. package/dist/runtime/native/pi-link.js +0 -269
  322. package/dist/runtime/native/results.d.ts +0 -41
  323. package/dist/runtime/native/results.js +0 -128
  324. package/dist/runtime/native/sessions.d.ts +0 -45
  325. package/dist/runtime/native/sessions.js +0 -87
  326. package/dist/runtime/native/transcript.d.ts +0 -89
  327. package/dist/runtime/native/transcript.js +0 -656
  328. package/dist/runtime/native/translate.d.ts +0 -46
  329. package/dist/runtime/native/translate.js +0 -299
  330. package/dist/runtime/native/transport.d.ts +0 -7
  331. package/dist/runtime/native/transport.js +0 -16
  332. package/dist/runtime/native/version.d.ts +0 -5
  333. package/dist/runtime/native/version.js +0 -21
  334. package/dist/runtime/preferences.d.ts +0 -7
  335. package/dist/runtime/preferences.js +0 -22
  336. package/dist/runtime/presentation.d.ts +0 -32
  337. package/dist/runtime/presentation.js +0 -124
  338. package/dist/runtime/process-group.d.ts +0 -5
  339. package/dist/runtime/process-group.js +0 -161
  340. package/dist/runtime/projection-store.d.ts +0 -27
  341. package/dist/runtime/projection-store.js +0 -30
  342. package/dist/runtime/space-binding.d.ts +0 -48
  343. package/dist/runtime/space-binding.js +0 -342
  344. package/dist/runtime/supervisor.d.ts +0 -19
  345. package/dist/runtime/supervisor.js +0 -441
  346. package/dist/runtime/turn-projection.d.ts +0 -41
  347. package/dist/runtime/turn-projection.js +0 -120
  348. package/dist/runtime/worker.d.ts +0 -1
  349. package/dist/safe-remote-image.d.ts +0 -36
  350. package/dist/safe-remote-image.js +0 -191
  351. package/dist/self-update.d.ts +0 -7
  352. package/dist/self-update.js +0 -202
  353. package/dist/session-status.d.ts +0 -10
  354. package/dist/session-status.js +0 -25
  355. package/dist/space-files/limit.d.ts +0 -2
  356. package/dist/space-files/limit.js +0 -16
  357. package/dist/space-files/operands.d.ts +0 -17
  358. package/dist/space-files/operands.js +0 -33
  359. package/dist/space-files/transfer.d.ts +0 -53
  360. package/dist/space-files/transfer.js +0 -297
  361. package/dist/space-files/upload.d.ts +0 -17
  362. package/dist/space-files/upload.js +0 -76
  363. package/dist/space.d.ts +0 -44
  364. package/dist/space.js +0 -192
  365. package/dist/version.d.ts +0 -2
  366. package/dist/version.js +0 -17
  367. package/dist/web.d.ts +0 -1
  368. package/dist/web.js +0 -4
  369. /package/dist/{board-kernel → chunks}/lexer.js +0 -0
@@ -1,884 +0,0 @@
1
- import { appendFile, mkdir, open, readFile, readdir, rename, stat } from "node:fs/promises";
2
- import { join } from "node:path";
3
- import { NATIVE_INGEST_MAX_TURNS, SETTLED_TURN_STATUSES } from "@neta-art/cohub";
4
- import { acceptsDirectory, acceptsTranscript, readSessionHeader, readTranscript, transcriptRoots } from "./adapters.js";
5
- import { SizeLimitError } from "./transcript.js";
6
- import { nativeTurnId } from "./identity.js";
7
- import { harnessAdapter, transcriptFormat } from "../harnesses/index.js";
8
- import { containsNativePath } from "./paths.js";
9
- const missing = (error) => error?.code === "ENOENT";
10
- /** Transcripts read in parallel per lane. The live lane never waits behind a backfill. */
11
- const LIVE_CONCURRENCY = 2;
12
- export const DEFAULT_IMPORT_CONCURRENCY = 4;
13
- export const MAX_IMPORT_CONCURRENCY = 8;
14
- /** A harness writes one Turn as many appends; filesystem events are coalesced for this long. */
15
- const WATCH_DEBOUNCE_MS = 150;
16
- /** A quiet transcript whose last reply ended is settled after this long, unless the file records Turn ends. */
17
- const QUIET_SETTLE_MS = 10_000;
18
- const SETTLED_STATUSES = new Set(SETTLED_TURN_STATUSES);
19
- /** A running Turn that shows signs of life tells the server at most this often. */
20
- const ALIVE_INTERVAL_MS = 5 * 60_000;
21
- /** A failed read is retried this many times with backoff; afterwards the next write or reconnect retries. */
22
- const RETRY_ATTEMPTS = 10;
23
- const RETRY_BASE_MS = 1_000;
24
- const RETRY_MAX_MS = 5 * 60_000;
25
- /**
26
- * Turns per ingest request. The server writes each Turn in its own transactions, so a batch is kept
27
- * small enough to answer well within the request timeout; the protocol allows more.
28
- */
29
- const INGEST_BATCH_TURNS = Math.min(50, NATIVE_INGEST_MAX_TURNS);
30
- const IMPORT_FAILURES_KEPT = 100;
31
- const fileVersion = (info) => `${info.ino}:${info.size}:${info.mtimeMs}`;
32
- /** Headers are read this many at a time; each costs one small read. */
33
- const HEADER_CONCURRENCY = 16;
34
- /** Whether a session in `cwd` belongs to the directory bound at `root`. */
35
- export const owns = (root, cwd, nested = []) => Boolean(cwd) && containsNativePath(root, cwd) && !nested.some((inner) => containsNativePath(inner, cwd));
36
- /** Every transcript of the bound directory and its subdirectories, newest first. */
37
- export async function discoverTranscripts(cwd, harnesses, options = {}) {
38
- const found = [];
39
- for (const harness of harnesses) {
40
- for (const root of transcriptRoots(harness)) {
41
- const pending = [root];
42
- while (pending.length) {
43
- options.signal?.throwIfAborted();
44
- const directory = pending.pop();
45
- const entries = await readdir(directory, { withFileTypes: true }).catch((error) => { if (missing(error) || error.code === "ENOTDIR")
46
- return []; throw error; });
47
- for (const entry of entries) {
48
- const path = join(directory, entry.name);
49
- if (entry.isDirectory()) {
50
- if (acceptsDirectory(path, root, harness, cwd))
51
- pending.push(path);
52
- }
53
- else if (entry.isFile() && acceptsTranscript(path, harness, cwd)) {
54
- const info = await stat(path).catch(() => null);
55
- if (info)
56
- found.push({ harness, path, size: info.size, mtimeMs: info.mtimeMs, ino: info.ino });
57
- }
58
- }
59
- }
60
- }
61
- }
62
- found.sort((left, right) => right.mtimeMs - left.mtimeMs);
63
- if (!options.headers)
64
- return found;
65
- // Only user conversations of this directory remain; Codex's own work threads are not listed.
66
- const kept = new Array(found.length).fill(null);
67
- let next = 0;
68
- await Promise.all(Array.from({ length: Math.min(HEADER_CONCURRENCY, found.length) }, async () => {
69
- while (next < found.length) {
70
- const index = next++;
71
- const candidate = found[index];
72
- const header = await readSessionHeader(candidate.path, candidate.harness, options.signal).catch(() => null);
73
- if (header?.thread === "user" && owns(cwd, header.cwd, options.nested))
74
- kept[index] = { ...candidate, nativeSessionId: header.nativeSessionId };
75
- }
76
- }));
77
- return kept.filter((candidate) => candidate !== null);
78
- }
79
- /** Ingest batches stay well under the Runtime frame limit. */
80
- const BATCH_MAX_BYTES = 8 * 1024 * 1024;
81
- /** Split in order into batches of at most `count` items and the byte budget. */
82
- function* chunks(items, count, size = () => 0) {
83
- let batch = [];
84
- let bytes = 0;
85
- for (const item of items) {
86
- const itemBytes = size(item);
87
- if (batch.length && (batch.length >= count || bytes + itemBytes > BATCH_MAX_BYTES)) {
88
- yield batch;
89
- batch = [];
90
- bytes = 0;
91
- }
92
- batch.push(item);
93
- bytes += itemBytes;
94
- }
95
- if (batch.length)
96
- yield batch;
97
- }
98
- /**
99
- * The daemon's data plane. A harness's transcript is both the durable record of a conversation and
100
- * the local outbox.
101
- */
102
- export class NativeIngest {
103
- options;
104
- states = new Map();
105
- live = new Map();
106
- busy = new Set();
107
- reads = new Set();
108
- timers = new Map();
109
- failures = new Map();
110
- /** File versions that hit a size limit; they are not read again until the file changes. */
111
- oversized = new Map();
112
- aliveAt = new Map();
113
- runningPaths = new Set();
114
- /** Files a live Cohub execution is writing in a format that cannot mark its Turn. */
115
- executing = new Set();
116
- writing = Promise.resolve();
117
- foreign = new Set();
118
- importQueue = [];
119
- runningLive = 0;
120
- runningImport = 0;
121
- job = { state: "idle", filter: {}, concurrency: DEFAULT_IMPORT_CONCURRENCY, files: 0, done: 0, turns: 0, bytes: 0, totalBytes: 0, startedAt: null, failed: [] };
122
- stopped = false;
123
- dirty = new Set();
124
- jobDirty = false;
125
- journalLines = 0;
126
- writeTimer = null;
127
- watchers = [];
128
- transport = null;
129
- harnesses;
130
- enabledAt;
131
- watching = null;
132
- nested = [];
133
- constructor(options) {
134
- this.options = options;
135
- this.harnesses = [...options.harnesses];
136
- this.enabledAt = options.enabledAt;
137
- }
138
- owns(cwd) {
139
- return owns(this.options.cwd, cwd, this.nested);
140
- }
141
- exclude(nested) {
142
- this.nested = nested.filter((root) => !containsNativePath(root, this.options.cwd) && containsNativePath(this.options.cwd, root));
143
- }
144
- configure(harnesses, enabledAt) {
145
- const added = harnesses.filter((harness) => !this.harnesses.includes(harness));
146
- const paused = this.harnesses.filter((harness) => !harnesses.includes(harness));
147
- this.harnesses = [...harnesses];
148
- this.enabledAt = enabledAt;
149
- if (this.watching)
150
- for (const harness of added)
151
- void this.watchHarness(harness, this.watching);
152
- // A paused harness's running Turn is no longer followed: the server treats it as one Cohub
153
- // cannot stop, so a silence it never reports the end of asks the user.
154
- for (const path of this.runningPaths) {
155
- const state = this.states.get(path);
156
- if (state && paused.includes(state.harness))
157
- void this.reportControl(state);
158
- }
159
- if (this.transport)
160
- void this.scan().catch((error) => this.report(this.options.cwd, error));
161
- }
162
- get ledgerPath() { return join(this.options.root, "native", "ledger.jsonl"); }
163
- async load() {
164
- let text;
165
- try {
166
- text = await readFile(this.ledgerPath, "utf8");
167
- }
168
- catch (error) {
169
- if (missing(error))
170
- return;
171
- throw error;
172
- }
173
- const lines = text.split("\n").filter(Boolean);
174
- for (const line of lines) {
175
- let entry;
176
- // A torn last line from a crash is dropped; the cache is rebuilt from transcripts and server.
177
- try {
178
- entry = JSON.parse(line);
179
- }
180
- catch {
181
- continue;
182
- }
183
- if ("import" in entry)
184
- this.job = { ...this.job, ...entry.import };
185
- else if (entry.path && entry.state)
186
- this.states.set(entry.path, entry.state);
187
- }
188
- for (const [path, state] of this.states)
189
- if (state.runningTurnId)
190
- this.runningPaths.add(path);
191
- this.journalLines = lines.length;
192
- if (this.overgrown())
193
- await this.compact();
194
- }
195
- connect(transport) {
196
- this.transport = transport;
197
- if (!transport)
198
- return;
199
- void this.scan().then(() => this.resync()).catch((error) => this.report(this.options.cwd, error));
200
- }
201
- /**
202
- * Record that Cohub created or chose this transcript for a Session, before any Turn of it runs.
203
- * `through` says the server has the file's Turns up to `turnKey` (none when null) and `turnId` heads them.
204
- */
205
- adopt(session, sessionId, through) {
206
- const state = this.states.get(session.path) ?? this.blank(session.path, session.nativeSessionId, session.cwd, session.harness, "user", { size: -1, mtimeMs: 0, ino: 0 });
207
- Object.assign(state, { sessionId, nativeSessionId: session.nativeSessionId });
208
- if (through)
209
- Object.assign(state, { throughTurnKey: through.turnKey, throughTurnId: through.turnId, headTurnId: through.turnId });
210
- if (!this.states.has(session.path))
211
- state.origin = "cohub";
212
- // The harness wrote elsewhere than expected; the execution follows the file.
213
- for (const other of this.states.values()) {
214
- if (other === state || other.harness !== session.harness || other.nativeSessionId !== session.nativeSessionId || !other.executingTurnId)
215
- continue;
216
- state.executingTurnId = other.executingTurnId;
217
- other.executingTurnId = null;
218
- if (this.executing.delete(other.path))
219
- this.executing.add(session.path);
220
- this.markDirty(other.path);
221
- }
222
- this.states.set(session.path, state);
223
- this.markDirty(session.path);
224
- }
225
- /** A Cohub Turn starts writing this file; a format that cannot mark it is not read until it ends. */
226
- begin(session, sessionId, turnId) {
227
- if (transcriptFormat(session.harness).marksCohubTurns)
228
- return;
229
- this.adopt(session, sessionId);
230
- const state = this.states.get(session.path);
231
- state.executingTurnId = turnId;
232
- this.executing.add(session.path);
233
- this.markDirty(session.path);
234
- }
235
- /** A Cohub Turn ended, whether or not it produced a result. */
236
- end(turnId) {
237
- for (const state of this.states.values()) {
238
- if (state.executingTurnId !== turnId)
239
- continue;
240
- this.executing.delete(state.path);
241
- this.touch(state.path);
242
- }
243
- }
244
- advance(path, turnId) {
245
- this.end(turnId);
246
- const state = this.states.get(path);
247
- if (!state)
248
- return;
249
- state.headTurnId = turnId;
250
- this.markDirty(path);
251
- this.touch(path);
252
- }
253
- touch(path) {
254
- this.live.set(path, true);
255
- this.drainLive();
256
- }
257
- running(harness, nativeSessionId) {
258
- // A paused harness is neither synced nor previewed.
259
- if (!this.harnesses.includes(harness))
260
- return null;
261
- for (const path of this.runningPaths) {
262
- const state = this.states.get(path);
263
- if (state?.harness === harness && state.nativeSessionId === nativeSessionId)
264
- return state;
265
- }
266
- return null;
267
- }
268
- stop(sessionId, turnId) {
269
- for (const path of this.runningPaths) {
270
- const state = this.states.get(path);
271
- if (state?.sessionId !== sessionId || state.runningTurnId !== turnId)
272
- continue;
273
- this.options.hooks?.abortTurn?.(state);
274
- return true;
275
- }
276
- return false;
277
- }
278
- alive(harness, nativeSessionId) {
279
- const state = this.running(harness, nativeSessionId);
280
- if (!state?.runningTurnId)
281
- return;
282
- const now = Date.now();
283
- if (now - (this.aliveAt.get(state.runningTurnId) ?? 0) < ALIVE_INTERVAL_MS)
284
- return;
285
- this.aliveAt.set(state.runningTurnId, now);
286
- void this.reportControl(state, true);
287
- }
288
- /**
289
- * A native client came or went, so whether Cohub can stop this session's running Turn may have
290
- * changed.
291
- */
292
- controlChanged(harness, nativeSessionId) {
293
- const state = this.running(harness, nativeSessionId);
294
- if (!state)
295
- return;
296
- void this.reportControl(state).then((controllable) => { if (controllable === false)
297
- this.touch(state.path); });
298
- }
299
- status() {
300
- return {
301
- // Cohub Turns of other harnesses are tracked here too; only synced transcripts count.
302
- transcripts: [...this.states.values()].filter((state) => this.harnesses.includes(state.harness)).length,
303
- live: this.live.size + this.runningLive,
304
- running: [...this.states.values()].filter((state) => state.runningTurnId).length,
305
- import: { ...this.job, queued: this.importQueue.length, running: this.runningImport },
306
- };
307
- }
308
- transcriptFor(sessionId, harness, headTurnId) {
309
- // A paused harness's files are not read, so a terminal's may hold Turns its head does not show;
310
- // only a file Cohub created, whose head Cohub itself advances, is continued then.
311
- const synced = this.harnesses.includes(harness);
312
- for (const state of this.states.values()) {
313
- if (state.sessionId === sessionId && state.harness === harness && state.headTurnId === headTurnId && state.threadKind === "user" && (synced || state.origin === "cohub"))
314
- return state;
315
- }
316
- return null;
317
- }
318
- async scan() {
319
- const enabledAt = Date.parse(this.enabledAt);
320
- for (const candidate of await discoverTranscripts(this.options.cwd, this.harnesses)) {
321
- const state = this.states.get(candidate.path);
322
- if (state ? !this.unchanged(state, candidate) : candidate.mtimeMs >= enabledAt)
323
- this.live.set(candidate.path, this.live.get(candidate.path) ?? false);
324
- }
325
- this.drainLive();
326
- if (this.job.state === "running")
327
- await this.startImport(this.job.filter, this.job.concurrency, { resume: true });
328
- }
329
- /** `resume` is automatic resumption: it skips file versions that already hit a size limit, which only an explicit import retries. */
330
- async plan(filter = {}, signal, options = {}) {
331
- const harnesses = this.harnesses.filter((harness) => !filter.harnesses?.length || filter.harnesses.includes(harness));
332
- const candidates = await discoverTranscripts(this.options.cwd, harnesses, { headers: true, nested: this.nested, signal });
333
- // A transcript whose first read failed is still untracked (size -1), so a later import retries it.
334
- return candidates.filter((candidate) => (this.states.get(candidate.path)?.size ?? -1) < 0 && !this.live.has(candidate.path)
335
- && !(options.resume && this.oversized.get(candidate.path) === fileVersion(candidate))
336
- && (!filter.nativeSessionId || candidate.nativeSessionId === filter.nativeSessionId));
337
- }
338
- /**
339
- * Start or resume a backfill. What was imported is tracked, so resuming simply plans again; the
340
- * server recognizes any Turn it already has, so an interrupted batch is never duplicated.
341
- */
342
- async startImport(filter = {}, concurrency = DEFAULT_IMPORT_CONCURRENCY, options = {}) {
343
- const queue = await this.plan(filter, undefined, options);
344
- const resuming = this.job.state === "running" || this.job.state === "paused";
345
- const same = resuming && JSON.stringify(this.job.filter) === JSON.stringify(filter);
346
- const base = same ? this.job : { ...this.job, done: 0, turns: 0, bytes: 0, failed: [], startedAt: new Date().toISOString() };
347
- this.importQueue = queue;
348
- this.job = {
349
- ...base,
350
- state: queue.length || this.runningImport ? "running" : "done",
351
- filter,
352
- concurrency: Math.min(MAX_IMPORT_CONCURRENCY, Math.max(1, Math.trunc(concurrency))),
353
- files: base.done + queue.length,
354
- totalBytes: base.bytes + queue.reduce((sum, candidate) => sum + candidate.size, 0),
355
- };
356
- this.markJobDirty();
357
- this.drainImport();
358
- return { ...this.job };
359
- }
360
- pauseImport() {
361
- if (this.job.state === "running")
362
- this.job.state = "paused";
363
- this.importQueue = [];
364
- this.markJobDirty();
365
- return { ...this.job };
366
- }
367
- async watch(signal) {
368
- this.watching = signal;
369
- for (const harness of this.harnesses)
370
- await this.watchHarness(harness, signal);
371
- }
372
- async watchHarness(harness, signal) {
373
- const { watch } = await import("node:fs");
374
- for (const root of transcriptRoots(harness)) {
375
- await mkdir(root, { recursive: true, mode: 0o700 }).catch(() => undefined);
376
- let timer = null;
377
- try {
378
- const watcher = watch(root, { recursive: true }, (_event, name) => {
379
- if (!name)
380
- return;
381
- const path = join(root, name.toString());
382
- if (this.foreign.has(path) || !acceptsTranscript(path, harness, this.options.cwd))
383
- return;
384
- if (!this.live.has(path))
385
- this.live.set(path, false);
386
- timer ??= setTimeout(() => { timer = null; this.drainLive(); }, WATCH_DEBOUNCE_MS);
387
- timer.unref?.();
388
- });
389
- // An overflowed or failed watch cannot say what changed; a scan finds out once.
390
- watcher.on("error", (error) => { this.report(root, error); void this.scan().catch(() => undefined); });
391
- signal.addEventListener("abort", () => watcher.close(), { once: true });
392
- this.watchers.push(watcher);
393
- }
394
- catch (error) {
395
- // Without a watch, transcripts sync when the Runtime (re)connects or the control plane reports.
396
- this.options.hooks?.onError?.(error, { path: root, harness });
397
- }
398
- }
399
- }
400
- async resync() {
401
- for (const path of [...this.runningPaths]) {
402
- const state = this.states.get(path);
403
- if (state)
404
- await this.reportControl(state, true);
405
- }
406
- }
407
- async reportControl(state, always = false) {
408
- const transport = this.transport;
409
- const { sessionId, runningTurnId } = state;
410
- if (!transport || !sessionId || !runningTurnId)
411
- return undefined;
412
- const controllable = this.harnesses.includes(state.harness) ? this.options.hooks?.controllable?.(state) : false;
413
- if (!always && controllable === state.controllable)
414
- return undefined;
415
- state.controllable = controllable;
416
- this.markDirty(state.path);
417
- const result = await transport.status(sessionId, runningTurnId, controllable).catch(() => {
418
- // Reported again on the next change or reconnect.
419
- state.controllable = undefined;
420
- return null;
421
- });
422
- // The server settled it (a confirmed stop): nothing is left to follow until the file changes.
423
- if (result?.status && SETTLED_STATUSES.has(result.status)) {
424
- if (state.runningTurnId === runningTurnId)
425
- this.endRunning(state);
426
- }
427
- else if (result?.abortRequested)
428
- this.options.hooks?.abortTurn?.(state);
429
- return controllable;
430
- }
431
- async close(signal) {
432
- this.stopped = true;
433
- if (this.writeTimer)
434
- clearTimeout(this.writeTimer);
435
- for (const timer of this.timers.values())
436
- clearTimeout(timer);
437
- this.timers.clear();
438
- for (const watcher of this.watchers)
439
- watcher.close();
440
- this.watchers.length = 0;
441
- await Promise.allSettled(this.reads);
442
- await this.write();
443
- await this.options.archives.flush(signal).catch(() => undefined);
444
- }
445
- unchanged(state, info) {
446
- return state.size === info.size && state.mtimeMs === info.mtimeMs && state.ino === info.ino;
447
- }
448
- drainLive() {
449
- while (!this.stopped && this.transport && this.runningLive < LIVE_CONCURRENCY && this.live.size) {
450
- const [path, force] = this.live.entries().next().value;
451
- this.live.delete(path);
452
- this.runningLive += 1;
453
- void this.sync(path, "live", force).then(() => { this.failures.delete(path); }, (error) => this.retryLater(path, error))
454
- .finally(() => { this.runningLive -= 1; this.drainLive(); });
455
- }
456
- }
457
- retryLater(path, error) {
458
- this.report(path, error);
459
- if (this.stopped || !this.transport || error instanceof SizeLimitError)
460
- return;
461
- const attempts = (this.failures.get(path) ?? 0) + 1;
462
- if (attempts > RETRY_ATTEMPTS) {
463
- this.failures.delete(path);
464
- return;
465
- }
466
- this.failures.set(path, attempts);
467
- this.recheck(path, Math.min(RETRY_MAX_MS, RETRY_BASE_MS * 2 ** (attempts - 1)));
468
- }
469
- drainImport() {
470
- while (!this.stopped && this.transport && this.job.state === "running" && this.runningImport < this.job.concurrency && this.importQueue.length) {
471
- const candidate = this.importQueue.shift();
472
- this.runningImport += 1;
473
- // A failed conversation is retried by the next `runtime import`.
474
- void this.sync(candidate.path, "import", true).catch((error) => {
475
- this.report(candidate.path, error);
476
- if (this.job.failed.length < IMPORT_FAILURES_KEPT)
477
- this.job.failed.push({ path: candidate.path, message: error instanceof Error ? error.message : String(error) });
478
- }).finally(() => {
479
- this.runningImport -= 1;
480
- this.job.done += 1;
481
- this.job.bytes += candidate.size;
482
- if (this.job.state === "running" && !this.importQueue.length && !this.runningImport)
483
- this.job.state = "done";
484
- this.markJobDirty();
485
- this.drainImport();
486
- });
487
- }
488
- }
489
- report(path, error) {
490
- this.options.hooks?.onError?.(error, { path, harness: this.states.get(path)?.harness ?? this.harnesses[0] });
491
- }
492
- async sync(path, lane, force) {
493
- // One reader per transcript: a conversation reopened during a backfill is read by one lane.
494
- if (this.busy.has(path)) {
495
- if (lane === "live")
496
- setTimeout(() => { this.live.set(path, force || (this.live.get(path) ?? false)); this.drainLive(); }, WATCH_DEBOUNCE_MS).unref?.();
497
- return;
498
- }
499
- this.busy.add(path);
500
- const read = this.read(path, lane, force);
501
- this.reads.add(read);
502
- try {
503
- await read;
504
- }
505
- finally {
506
- this.busy.delete(path);
507
- this.reads.delete(read);
508
- }
509
- }
510
- async read(path, lane, force) {
511
- if (this.foreign.has(path) || this.executing.has(path))
512
- return;
513
- const info = await stat(path).catch((error) => { if (missing(error))
514
- return null; throw error; });
515
- if (!info?.isFile())
516
- return;
517
- const version = fileVersion(info);
518
- if (lane === "live" && this.oversized.get(path) === version)
519
- return;
520
- this.oversized.delete(path);
521
- try {
522
- await this.readVersion(path, lane, force, info);
523
- }
524
- catch (error) {
525
- if (error instanceof SizeLimitError)
526
- this.oversized.set(path, version);
527
- throw error;
528
- }
529
- }
530
- async readVersion(path, lane, force, info) {
531
- const previous = this.states.get(path);
532
- // Unchanged bytes can still hold a Turn whose end the control plane has only now reported.
533
- if (previous && this.unchanged(previous, info) && !(force && previous.runningTurnId))
534
- return;
535
- const harness = previous?.harness ?? this.harnessFor(path);
536
- // A paused harness uploads nothing, not even for transcripts it already tracks.
537
- if (!harness || !this.harnesses.includes(harness))
538
- return;
539
- if (previous && (previous.threadKind !== "user" || !this.owns(previous.cwd)))
540
- return;
541
- if (!previous) {
542
- // A header is read once per file: it names the project and the thread's purpose.
543
- const header = await readSessionHeader(path, harness);
544
- if (!header)
545
- return;
546
- if (!this.owns(header.cwd)) {
547
- this.foreign.add(path);
548
- return;
549
- }
550
- if (header.thread !== "user") {
551
- // Codex's own reviews, compaction runs and spawned agents are not chats; remember and skip them.
552
- this.states.set(path, this.blank(path, header.nativeSessionId, header.cwd, harness, header.thread, info));
553
- this.markDirty(path);
554
- return;
555
- }
556
- }
557
- if (previous && !force && previous.ino === info.ino && previous.size >= 0 && info.size >= previous.scannedBytes) {
558
- // Nothing new since the last search (a repeated event), or new bytes without a boundary.
559
- if (info.size === previous.scannedBytes)
560
- return;
561
- if (!await this.crossesBoundary(path, harness, previous.scannedBytes, info.size)) {
562
- previous.scannedBytes = info.size;
563
- if (previous.runningTurnId)
564
- this.options.hooks?.onGrowth?.(previous);
565
- return;
566
- }
567
- }
568
- const transcript = await readTranscript(path, harness);
569
- const state = previous ?? this.blank(path, transcript.nativeSessionId, transcript.cwd, harness, "user", { size: -1, mtimeMs: 0, ino: 0 });
570
- this.states.set(path, state);
571
- // The file counts as read only once the server confirmed it; a failed send is retried.
572
- await this.apply(state, transcript.turns, lane, info.mtimeMs);
573
- Object.assign(state, { size: info.size, mtimeMs: info.mtimeMs, ino: info.ino, scannedBytes: info.size });
574
- this.markDirty(path);
575
- }
576
- async crossesBoundary(path, harness, from, to) {
577
- // New bytes of a known transcript are only searched for records that begin or end a Turn.
578
- const tokens = transcriptFormat(harness).boundaries;
579
- const overlap = Math.max(0, ...tokens.map((token) => token.length));
580
- const file = await open(path, "r");
581
- try {
582
- const chunk = Buffer.alloc(Math.min(to - from + overlap, 1024 * 1024));
583
- for (let position = from; position < to;) {
584
- const start = Math.max(0, position - overlap);
585
- const { bytesRead } = await file.read(chunk, 0, Math.min(chunk.length, to - start), start);
586
- if (bytesRead <= position - start)
587
- break;
588
- const bytes = chunk.subarray(0, bytesRead);
589
- // A token that ends inside the bytes already searched was seen before; it does not count again.
590
- const fresh = position - start;
591
- for (const token of tokens) {
592
- for (let index = bytes.indexOf(token); index >= 0; index = bytes.indexOf(token, index + 1))
593
- if (index + token.length > fresh)
594
- return true;
595
- }
596
- position = start + bytesRead;
597
- }
598
- return false;
599
- }
600
- finally {
601
- await file.close();
602
- }
603
- }
604
- blank(path, nativeSessionId, cwd, harness, threadKind, info) {
605
- return { harness, path, nativeSessionId, cwd, threadKind, size: info.size, mtimeMs: info.mtimeMs, ino: info.ino, scannedBytes: Math.max(0, info.size), throughTurnKey: null, throughTurnId: null, headTurnId: null, sessionId: null, runningTurnId: null, runningTurnKey: null, origin: "native" };
606
- }
607
- harnessFor(path) {
608
- for (const harness of this.harnesses) {
609
- if (transcriptRoots(harness).some((root) => containsNativePath(root, path)) && acceptsTranscript(path, harness, this.options.cwd))
610
- return harness;
611
- }
612
- return null;
613
- }
614
- tailSettled(state, tail, mtimeMs) {
615
- const format = transcriptFormat(state.harness);
616
- if (format.turnEndRecorded)
617
- return tail.result !== null;
618
- const certain = this.options.hooks?.settled?.(state, mtimeMs);
619
- if (certain != null)
620
- return certain;
621
- if (tail.ended)
622
- return true;
623
- if (!format.replyEnded?.(tail))
624
- return false;
625
- const idleMs = Date.now() - mtimeMs;
626
- if (idleMs >= QUIET_SETTLE_MS)
627
- return true;
628
- this.recheck(state.path, QUIET_SETTLE_MS - idleMs);
629
- return false;
630
- }
631
- recheck(path, delayMs) {
632
- clearTimeout(this.timers.get(path));
633
- const timer = setTimeout(() => { this.timers.delete(path); this.touch(path); }, Math.max(0, delayMs) + 50);
634
- timer.unref?.();
635
- this.timers.set(path, timer);
636
- }
637
- async known(transport, turnIds) {
638
- const known = new Map();
639
- for (const chunk of chunks([...new Set(turnIds)], NATIVE_INGEST_MAX_TURNS)) {
640
- for (const entry of await transport.known(chunk))
641
- known.set(entry.turnId, entry);
642
- }
643
- return known;
644
- }
645
- async apply(state, turns, lane, mtimeMs) {
646
- const transport = this.transport;
647
- if (!transport)
648
- throw new Error("Runtime is offline");
649
- const derived = (turn) => nativeTurnId(this.options.spaceId, state.harness, state.nativeSessionId, turn.key);
650
- const from = state.throughTurnKey ? turns.findIndex((turn) => turn.key === state.throughTurnKey) + 1 : 0;
651
- const pending = turns.slice(from);
652
- // One question to the server replaces every local receipt: which of these do you have?
653
- const known = pending.length ? await this.known(transport, pending.flatMap((turn) => turn.cloudTurnId ? [turn.cloudTurnId, derived(turn)] : [derived(turn)])) : new Map();
654
- const cohub = (turn) => turn.cloudTurnId && known.has(turn.cloudTurnId) ? turn.cloudTurnId : null;
655
- const tail = turns.at(-1);
656
- // A Cohub file whose latest Cohub Turn the file cannot name (its format marks none, or it predates
657
- // the marker) holds only Turns the server already has, up to its current end.
658
- const cohubHead = state.executingTurnId ?? state.headTurnId;
659
- const unmarked = !transcriptFormat(state.harness).marksCohubTurns || state.size < 0;
660
- if (state.origin === "cohub" && cohubHead && unmarked && state.throughTurnId !== cohubHead && !turns.some((turn) => turn.cloudTurnId === cohubHead)) {
661
- const through = tail ? cohub(tail) ?? cohubHead : cohubHead;
662
- Object.assign(state, { throughTurnKey: tail?.key ?? state.throughTurnKey, throughTurnId: through, headTurnId: through, executingTurnId: null });
663
- return;
664
- }
665
- state.executingTurnId = null;
666
- const head = !tail ? null : pending.length ? cohub(tail) ?? derived(tail) : state.throughTurnId;
667
- const candidates = [];
668
- // A head adopted without a transcript key (Cohub's Turn left no record) still parents what follows.
669
- let parentTurnId = from > 0 || !state.throughTurnKey ? state.throughTurnId : null;
670
- for (const [offset, turn] of pending.entries()) {
671
- const marked = cohub(turn);
672
- if (marked) {
673
- // Cohub started this Turn, so the server already has it; the marker exists only so the
674
- // native conversation can be resumed.
675
- parentTurnId = marked;
676
- if (!candidates.length) {
677
- state.throughTurnKey = turn.key;
678
- state.throughTurnId = marked;
679
- }
680
- continue;
681
- }
682
- const settled = offset < pending.length - 1 || this.tailSettled(state, turn, mtimeMs);
683
- // History that never ended is left to the live lane; its transcript reports it again.
684
- if (!settled && lane === "import")
685
- break;
686
- // A prompt still being recorded would change the Turn's identity; it is sent once complete.
687
- if (!settled && transcriptFormat(state.harness).userFinalMarked && !turn.userFinal)
688
- break;
689
- const turnId = derived(turn);
690
- candidates.push({ turn, turnId, parentTurnId, settled });
691
- parentTurnId = turnId;
692
- }
693
- if (!candidates.length) {
694
- state.headTurnId = head;
695
- return;
696
- }
697
- let prefix = 0;
698
- for (const item of candidates) {
699
- const recorded = known.get(item.turnId);
700
- if (!recorded?.settled)
701
- break;
702
- // The server settled it meanwhile (a confirmed stop, or an earlier send that was not answered).
703
- if (state.runningTurnId === item.turnId)
704
- this.endRunning(state);
705
- state.sessionId = recorded.sessionId;
706
- state.throughTurnKey = item.turn.key;
707
- state.throughTurnId = item.turnId;
708
- prefix += 1;
709
- }
710
- // A running Turn is registered once; until it ends, only the realtime path speaks of it.
711
- const todo = candidates.slice(prefix).filter((item) => {
712
- const recorded = known.get(item.turnId);
713
- if (!item.settled && recorded) {
714
- this.markRunning(state, recorded.sessionId, item);
715
- return false;
716
- }
717
- return !recorded?.settled;
718
- });
719
- let lastSettled = null;
720
- const controllable = this.options.hooks?.controllable?.(state);
721
- const payloads = todo.map((item) => ({
722
- item,
723
- turn: {
724
- turnId: item.turnId,
725
- parentTurnId: item.parentTurnId,
726
- userContent: item.turn.userContent,
727
- startedAt: item.turn.startedAt,
728
- ...(this.imported(item.turn) ? { origin: "local_import" } : {}),
729
- ...(!item.settled && controllable !== undefined ? { controllable } : {}),
730
- result: item.settled ? this.complete(item.turn.result ?? this.cutOff(item, pending)) : null,
731
- },
732
- }));
733
- for (const batch of chunks(payloads, INGEST_BATCH_TURNS, (payload) => JSON.stringify(payload.turn).length)) {
734
- const result = await transport.ingest({
735
- type: "ingest",
736
- input: { harness: state.harness, nativeSessionId: state.nativeSessionId, turns: batch.map((payload) => payload.turn) },
737
- });
738
- const outcomes = new Map(result.turns.map((entry) => [entry.turnId, entry]));
739
- for (const { item } of batch) {
740
- const entry = outcomes.get(item.turnId);
741
- // A Turn the server did not take stops the file here; the next read retries from it.
742
- if (!entry)
743
- throw new Error("Server did not record a native Turn");
744
- state.sessionId = entry.sessionId;
745
- if (!entry.settled) {
746
- state.controllable = controllable;
747
- this.markRunning(state, entry.sessionId, item);
748
- continue;
749
- }
750
- if (state.runningTurnId === entry.turnId)
751
- this.endRunning(state);
752
- state.throughTurnKey = item.turn.key;
753
- state.throughTurnId = entry.turnId;
754
- lastSettled = item;
755
- if (lane === "import")
756
- this.job.turns += 1;
757
- }
758
- this.markDirty(state.path);
759
- }
760
- // Only the newest settled state needs raw bytes: every older version is a prefix of it.
761
- state.headTurnId = head;
762
- if (lastSettled)
763
- await this.archive(state, lastSettled.turn, lastSettled.turnId);
764
- }
765
- markRunning(state, sessionId, item) {
766
- const changed = state.runningTurnId !== item.turnId;
767
- state.sessionId = sessionId;
768
- state.runningTurnId = item.turnId;
769
- state.runningTurnKey = item.turn.key;
770
- this.runningPaths.add(state.path);
771
- if (!changed)
772
- return;
773
- this.options.hooks?.onRunning?.(state, item.turnId);
774
- // Ask once: a stop may have reached the server before this Runtime knew the Turn.
775
- this.aliveAt.set(item.turnId, Date.now());
776
- void this.reportControl(state, true);
777
- }
778
- endRunning(state) {
779
- if (state.runningTurnId)
780
- this.aliveAt.delete(state.runningTurnId);
781
- state.runningTurnId = null;
782
- state.runningTurnKey = null;
783
- state.controllable = undefined;
784
- this.runningPaths.delete(state.path);
785
- this.options.hooks?.onRunning?.(state, null);
786
- }
787
- cutOff(item, pending) {
788
- const next = pending[pending.indexOf(item.turn) + 1];
789
- return { messages: item.turn.messages, status: "interrupted", completedAt: next?.startedAt ?? item.turn.startedAt };
790
- }
791
- imported(turn) {
792
- return Date.parse(turn.startedAt) < Date.parse(this.enabledAt);
793
- }
794
- async archive(state, turn, turnId) {
795
- if (!state.sessionId || !harnessAdapter(state.harness).session.archive)
796
- return;
797
- try {
798
- const source = turn.path ?? state.path;
799
- const archiveSource = transcriptFormat(state.harness).archiveSource;
800
- await this.options.archives.stage({
801
- sessionId: state.sessionId,
802
- harness: state.harness,
803
- nativeSessionId: state.nativeSessionId,
804
- path: archiveSource ? await archiveSource(source, join(this.options.root, "native", "rollouts")) : source,
805
- sizeBytes: turn.endBytes,
806
- expectedChecksum: turn.sha256,
807
- }, turnId);
808
- }
809
- catch (error) {
810
- // Capture never blocks the ledger: a later pass retries, and a rewritten file starts a new
811
- // archive baseline instead of failing the sync.
812
- this.options.hooks?.onError?.(error, { path: state.path, harness: state.harness });
813
- }
814
- }
815
- message(message) {
816
- return {
817
- content: message.content,
818
- provider: message.provider ?? null,
819
- model: message.model ?? null,
820
- usage: (message.usage ?? null),
821
- stopReason: message.stopReason ?? null,
822
- errorMessage: message.errorMessage ?? null,
823
- };
824
- }
825
- complete(result) {
826
- return { messages: result.messages.map((message) => this.message(message)), status: result.status, completedAt: result.completedAt };
827
- }
828
- markDirty(path) {
829
- this.dirty.add(path);
830
- this.scheduleWrite();
831
- }
832
- markJobDirty() {
833
- this.jobDirty = true;
834
- this.scheduleWrite();
835
- }
836
- scheduleWrite() {
837
- if (this.stopped)
838
- return;
839
- this.writeTimer ??= setTimeout(() => { this.writeTimer = null; void this.write().catch((error) => this.report(this.ledgerPath, error)); }, 500);
840
- this.writeTimer.unref?.();
841
- }
842
- write() {
843
- this.writing = this.writing.then(() => this.append(), () => this.append());
844
- return this.writing;
845
- }
846
- async append() {
847
- const entries = [];
848
- for (const path of this.dirty) {
849
- const state = this.states.get(path);
850
- if (state)
851
- entries.push({ path, state });
852
- }
853
- if (this.jobDirty)
854
- entries.push({ import: this.job });
855
- this.dirty.clear();
856
- this.jobDirty = false;
857
- if (!entries.length)
858
- return;
859
- await mkdir(join(this.options.root, "native"), { recursive: true, mode: 0o700 });
860
- await appendFile(this.ledgerPath, entries.map((entry) => `${JSON.stringify(entry)}\n`).join(""), { mode: 0o600 });
861
- this.journalLines += entries.length;
862
- // A long-running Runtime keeps the journal near its content, not just a fresh one.
863
- if (this.overgrown())
864
- await this.compact();
865
- }
866
- overgrown() {
867
- return this.journalLines > 2 * this.states.size + 256;
868
- }
869
- async compact() {
870
- const entries = [...this.states].map(([path, state]) => ({ path, state }));
871
- entries.push({ import: this.job });
872
- const temporary = `${this.ledgerPath}.${process.pid}.tmp`;
873
- const file = await open(temporary, "w", 0o600);
874
- try {
875
- await file.writeFile(entries.map((entry) => `${JSON.stringify(entry)}\n`).join(""));
876
- await file.sync();
877
- }
878
- finally {
879
- await file.close();
880
- }
881
- await rename(temporary, this.ledgerPath);
882
- this.journalLines = entries.length;
883
- }
884
- }