@diffra-ai/durable 0.0.0-stage → 0.2.2

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 (411) hide show
  1. package/CHANGELOG.md +440 -0
  2. package/README.md +1153 -2
  3. package/dist/codemode/codemode.d.ts +174 -0
  4. package/dist/codemode/codemode.d.ts.map +1 -0
  5. package/dist/codemode/codemode.js +884 -0
  6. package/dist/codemode/codemode.js.map +1 -0
  7. package/dist/codemode/describe.d.ts +38 -0
  8. package/dist/codemode/describe.d.ts.map +1 -0
  9. package/dist/codemode/describe.js +127 -0
  10. package/dist/codemode/describe.js.map +1 -0
  11. package/dist/codemode/index.d.ts +3 -0
  12. package/dist/codemode/index.d.ts.map +1 -0
  13. package/dist/codemode/index.js +3 -0
  14. package/dist/codemode/index.js.map +1 -0
  15. package/dist/codemode/models.d.ts +29 -0
  16. package/dist/codemode/models.d.ts.map +1 -0
  17. package/dist/codemode/models.js +242 -0
  18. package/dist/codemode/models.js.map +1 -0
  19. package/dist/codemode/output.d.ts +43 -0
  20. package/dist/codemode/output.d.ts.map +1 -0
  21. package/dist/codemode/output.js +168 -0
  22. package/dist/codemode/output.js.map +1 -0
  23. package/dist/documents.d.ts +66 -0
  24. package/dist/documents.d.ts.map +1 -0
  25. package/dist/documents.js +98 -0
  26. package/dist/documents.js.map +1 -0
  27. package/dist/entries.d.ts +31 -0
  28. package/dist/entries.d.ts.map +1 -0
  29. package/dist/entries.js +31 -0
  30. package/dist/entries.js.map +1 -0
  31. package/dist/env/decode.d.ts +16 -0
  32. package/dist/env/decode.d.ts.map +1 -0
  33. package/dist/env/decode.js +28 -0
  34. package/dist/env/decode.js.map +1 -0
  35. package/dist/env/index.d.ts +308 -0
  36. package/dist/env/index.d.ts.map +1 -0
  37. package/dist/env/index.js +49 -0
  38. package/dist/env/index.js.map +1 -0
  39. package/dist/env/line-scan.d.ts +13 -0
  40. package/dist/env/line-scan.d.ts.map +1 -0
  41. package/dist/env/line-scan.js +147 -0
  42. package/dist/env/line-scan.js.map +1 -0
  43. package/dist/env/node-watch.d.ts +29 -0
  44. package/dist/env/node-watch.d.ts.map +1 -0
  45. package/dist/env/node-watch.js +487 -0
  46. package/dist/env/node-watch.js.map +1 -0
  47. package/dist/env/node.d.ts +56 -0
  48. package/dist/env/node.d.ts.map +1 -0
  49. package/dist/env/node.js +1185 -0
  50. package/dist/env/node.js.map +1 -0
  51. package/dist/errors.d.ts +48 -0
  52. package/dist/errors.d.ts.map +1 -0
  53. package/dist/errors.js +76 -0
  54. package/dist/errors.js.map +1 -0
  55. package/dist/harness/agent.d.ts +46 -0
  56. package/dist/harness/agent.d.ts.map +1 -0
  57. package/dist/harness/agent.js +334 -0
  58. package/dist/harness/agent.js.map +1 -0
  59. package/dist/harness/compaction.d.ts +68 -0
  60. package/dist/harness/compaction.d.ts.map +1 -0
  61. package/dist/harness/compaction.js +431 -0
  62. package/dist/harness/compaction.js.map +1 -0
  63. package/dist/harness/context.d.ts +84 -0
  64. package/dist/harness/context.d.ts.map +1 -0
  65. package/dist/harness/context.js +301 -0
  66. package/dist/harness/context.js.map +1 -0
  67. package/dist/harness/define.d.ts +25 -0
  68. package/dist/harness/define.d.ts.map +1 -0
  69. package/dist/harness/define.js +32 -0
  70. package/dist/harness/define.js.map +1 -0
  71. package/dist/harness/generation.d.ts +117 -0
  72. package/dist/harness/generation.d.ts.map +1 -0
  73. package/dist/harness/generation.js +1098 -0
  74. package/dist/harness/generation.js.map +1 -0
  75. package/dist/harness/harness.d.ts +22 -0
  76. package/dist/harness/harness.d.ts.map +1 -0
  77. package/dist/harness/harness.js +438 -0
  78. package/dist/harness/harness.js.map +1 -0
  79. package/dist/harness/inbox.d.ts +84 -0
  80. package/dist/harness/inbox.d.ts.map +1 -0
  81. package/dist/harness/inbox.js +135 -0
  82. package/dist/harness/inbox.js.map +1 -0
  83. package/dist/harness/json.d.ts +26 -0
  84. package/dist/harness/json.d.ts.map +1 -0
  85. package/dist/harness/json.js +51 -0
  86. package/dist/harness/json.js.map +1 -0
  87. package/dist/harness/live.d.ts +285 -0
  88. package/dist/harness/live.d.ts.map +1 -0
  89. package/dist/harness/live.js +257 -0
  90. package/dist/harness/live.js.map +1 -0
  91. package/dist/harness/parks.d.ts +23 -0
  92. package/dist/harness/parks.d.ts.map +1 -0
  93. package/dist/harness/parks.js +52 -0
  94. package/dist/harness/parks.js.map +1 -0
  95. package/dist/harness/partial-changes.d.ts +54 -0
  96. package/dist/harness/partial-changes.d.ts.map +1 -0
  97. package/dist/harness/partial-changes.js +163 -0
  98. package/dist/harness/partial-changes.js.map +1 -0
  99. package/dist/harness/prompt.d.ts +27 -0
  100. package/dist/harness/prompt.d.ts.map +1 -0
  101. package/dist/harness/prompt.js +131 -0
  102. package/dist/harness/prompt.js.map +1 -0
  103. package/dist/harness/provider.d.ts +16 -0
  104. package/dist/harness/provider.d.ts.map +1 -0
  105. package/dist/harness/provider.js +33 -0
  106. package/dist/harness/provider.js.map +1 -0
  107. package/dist/harness/registry-reader.d.ts +11 -0
  108. package/dist/harness/registry-reader.d.ts.map +1 -0
  109. package/dist/harness/registry-reader.js +25 -0
  110. package/dist/harness/registry-reader.js.map +1 -0
  111. package/dist/harness/registry.d.ts +6 -0
  112. package/dist/harness/registry.d.ts.map +1 -0
  113. package/dist/harness/registry.js +101 -0
  114. package/dist/harness/registry.js.map +1 -0
  115. package/dist/harness/scheduler.d.ts +195 -0
  116. package/dist/harness/scheduler.d.ts.map +1 -0
  117. package/dist/harness/scheduler.js +3073 -0
  118. package/dist/harness/scheduler.js.map +1 -0
  119. package/dist/harness/submissions.d.ts +44 -0
  120. package/dist/harness/submissions.d.ts.map +1 -0
  121. package/dist/harness/submissions.js +265 -0
  122. package/dist/harness/submissions.js.map +1 -0
  123. package/dist/harness/task-graph.d.ts +75 -0
  124. package/dist/harness/task-graph.d.ts.map +1 -0
  125. package/dist/harness/task-graph.js +224 -0
  126. package/dist/harness/task-graph.js.map +1 -0
  127. package/dist/harness/telemetry.d.ts +307 -0
  128. package/dist/harness/telemetry.d.ts.map +1 -0
  129. package/dist/harness/telemetry.js +140 -0
  130. package/dist/harness/telemetry.js.map +1 -0
  131. package/dist/harness/tool.d.ts +106 -0
  132. package/dist/harness/tool.d.ts.map +1 -0
  133. package/dist/harness/tool.js +1182 -0
  134. package/dist/harness/tool.js.map +1 -0
  135. package/dist/harness/transcript.d.ts +72 -0
  136. package/dist/harness/transcript.d.ts.map +1 -0
  137. package/dist/harness/transcript.js +264 -0
  138. package/dist/harness/transcript.js.map +1 -0
  139. package/dist/harness/types.d.ts +1146 -0
  140. package/dist/harness/types.d.ts.map +1 -0
  141. package/dist/harness/types.js +2 -0
  142. package/dist/harness/types.js.map +1 -0
  143. package/dist/harness/usage.d.ts +19 -0
  144. package/dist/harness/usage.d.ts.map +1 -0
  145. package/dist/harness/usage.js +57 -0
  146. package/dist/harness/usage.js.map +1 -0
  147. package/dist/harness/util.d.ts +25 -0
  148. package/dist/harness/util.d.ts.map +1 -0
  149. package/dist/harness/util.js +76 -0
  150. package/dist/harness/util.js.map +1 -0
  151. package/dist/harness/view.d.ts +69 -0
  152. package/dist/harness/view.d.ts.map +1 -0
  153. package/dist/harness/view.js +265 -0
  154. package/dist/harness/view.js.map +1 -0
  155. package/dist/ids.d.ts +6 -0
  156. package/dist/ids.d.ts.map +1 -0
  157. package/dist/ids.js +9 -0
  158. package/dist/ids.js.map +1 -0
  159. package/dist/images/cloudflare.d.ts +4 -0
  160. package/dist/images/cloudflare.d.ts.map +1 -0
  161. package/dist/images/cloudflare.js +9 -0
  162. package/dist/images/cloudflare.js.map +1 -0
  163. package/dist/images/exif.d.ts +3 -0
  164. package/dist/images/exif.d.ts.map +1 -0
  165. package/dist/images/exif.js +71 -0
  166. package/dist/images/exif.js.map +1 -0
  167. package/dist/images/fit.d.ts +41 -0
  168. package/dist/images/fit.d.ts.map +1 -0
  169. package/dist/images/fit.js +108 -0
  170. package/dist/images/fit.js.map +1 -0
  171. package/dist/images/index.d.ts +18 -0
  172. package/dist/images/index.d.ts.map +1 -0
  173. package/dist/images/index.js +125 -0
  174. package/dist/images/index.js.map +1 -0
  175. package/dist/images/node-worker.d.ts +19 -0
  176. package/dist/images/node-worker.d.ts.map +1 -0
  177. package/dist/images/node-worker.js +17 -0
  178. package/dist/images/node-worker.js.map +1 -0
  179. package/dist/images/node.d.ts +21 -0
  180. package/dist/images/node.d.ts.map +1 -0
  181. package/dist/images/node.js +121 -0
  182. package/dist/images/node.js.map +1 -0
  183. package/dist/index.d.ts +29 -0
  184. package/dist/index.d.ts.map +1 -0
  185. package/dist/index.js +23 -0
  186. package/dist/index.js.map +1 -0
  187. package/dist/search.d.ts +29 -0
  188. package/dist/search.d.ts.map +1 -0
  189. package/dist/search.js +148 -0
  190. package/dist/search.js.map +1 -0
  191. package/dist/session/cached-storage.d.ts +72 -0
  192. package/dist/session/cached-storage.d.ts.map +1 -0
  193. package/dist/session/cached-storage.js +269 -0
  194. package/dist/session/cached-storage.js.map +1 -0
  195. package/dist/session/conversation-tails.d.ts +33 -0
  196. package/dist/session/conversation-tails.d.ts.map +1 -0
  197. package/dist/session/conversation-tails.js +101 -0
  198. package/dist/session/conversation-tails.js.map +1 -0
  199. package/dist/session/forks.d.ts +10 -0
  200. package/dist/session/forks.d.ts.map +1 -0
  201. package/dist/session/forks.js +42 -0
  202. package/dist/session/forks.js.map +1 -0
  203. package/dist/session/keys.d.ts +69 -0
  204. package/dist/session/keys.d.ts.map +1 -0
  205. package/dist/session/keys.js +161 -0
  206. package/dist/session/keys.js.map +1 -0
  207. package/dist/session/session.d.ts +287 -0
  208. package/dist/session/session.d.ts.map +1 -0
  209. package/dist/session/session.js +1701 -0
  210. package/dist/session/session.js.map +1 -0
  211. package/dist/session/transaction.d.ts +205 -0
  212. package/dist/session/transaction.d.ts.map +1 -0
  213. package/dist/session/transaction.js +1059 -0
  214. package/dist/session/transaction.js.map +1 -0
  215. package/dist/session/writer.d.ts +74 -0
  216. package/dist/session/writer.d.ts.map +1 -0
  217. package/dist/session/writer.js +294 -0
  218. package/dist/session/writer.js.map +1 -0
  219. package/dist/signals.d.ts +180 -0
  220. package/dist/signals.d.ts.map +1 -0
  221. package/dist/signals.js +236 -0
  222. package/dist/signals.js.map +1 -0
  223. package/dist/storage/indexeddb.d.ts +69 -0
  224. package/dist/storage/indexeddb.d.ts.map +1 -0
  225. package/dist/storage/indexeddb.js +883 -0
  226. package/dist/storage/indexeddb.js.map +1 -0
  227. package/dist/storage/jsonl/index.d.ts +2 -0
  228. package/dist/storage/jsonl/index.d.ts.map +1 -0
  229. package/dist/storage/jsonl/index.js +2 -0
  230. package/dist/storage/jsonl/index.js.map +1 -0
  231. package/dist/storage/jsonl/node.d.ts +6 -0
  232. package/dist/storage/jsonl/node.d.ts.map +1 -0
  233. package/dist/storage/jsonl/node.js +8 -0
  234. package/dist/storage/jsonl/node.js.map +1 -0
  235. package/dist/storage/jsonl/storage.d.ts +206 -0
  236. package/dist/storage/jsonl/storage.d.ts.map +1 -0
  237. package/dist/storage/jsonl/storage.js +799 -0
  238. package/dist/storage/jsonl/storage.js.map +1 -0
  239. package/dist/storage/memory.d.ts +68 -0
  240. package/dist/storage/memory.d.ts.map +1 -0
  241. package/dist/storage/memory.js +761 -0
  242. package/dist/storage/memory.js.map +1 -0
  243. package/dist/storage/scan.d.ts +18 -0
  244. package/dist/storage/scan.d.ts.map +1 -0
  245. package/dist/storage/scan.js +29 -0
  246. package/dist/storage/scan.js.map +1 -0
  247. package/dist/storage/sqlite/cloudflare.d.ts +9 -0
  248. package/dist/storage/sqlite/cloudflare.d.ts.map +1 -0
  249. package/dist/storage/sqlite/cloudflare.js +11 -0
  250. package/dist/storage/sqlite/cloudflare.js.map +1 -0
  251. package/dist/storage/sqlite/commit.d.ts +52 -0
  252. package/dist/storage/sqlite/commit.d.ts.map +1 -0
  253. package/dist/storage/sqlite/commit.js +371 -0
  254. package/dist/storage/sqlite/commit.js.map +1 -0
  255. package/dist/storage/sqlite/database.d.ts +2 -0
  256. package/dist/storage/sqlite/database.d.ts.map +1 -0
  257. package/dist/storage/sqlite/database.js +2 -0
  258. package/dist/storage/sqlite/database.js.map +1 -0
  259. package/dist/storage/sqlite/index.d.ts +4 -0
  260. package/dist/storage/sqlite/index.d.ts.map +1 -0
  261. package/dist/storage/sqlite/index.js +3 -0
  262. package/dist/storage/sqlite/index.js.map +1 -0
  263. package/dist/storage/sqlite/migrations.d.ts +12 -0
  264. package/dist/storage/sqlite/migrations.d.ts.map +1 -0
  265. package/dist/storage/sqlite/migrations.js +121 -0
  266. package/dist/storage/sqlite/migrations.js.map +1 -0
  267. package/dist/storage/sqlite/node.d.ts +8 -0
  268. package/dist/storage/sqlite/node.d.ts.map +1 -0
  269. package/dist/storage/sqlite/node.js +12 -0
  270. package/dist/storage/sqlite/node.js.map +1 -0
  271. package/dist/storage/sqlite/rows.d.ts +33 -0
  272. package/dist/storage/sqlite/rows.d.ts.map +1 -0
  273. package/dist/storage/sqlite/rows.js +53 -0
  274. package/dist/storage/sqlite/rows.js.map +1 -0
  275. package/dist/storage/sqlite/schema.d.ts +45 -0
  276. package/dist/storage/sqlite/schema.d.ts.map +1 -0
  277. package/dist/storage/sqlite/schema.js +50 -0
  278. package/dist/storage/sqlite/schema.js.map +1 -0
  279. package/dist/storage/sqlite/statements.d.ts +13 -0
  280. package/dist/storage/sqlite/statements.d.ts.map +1 -0
  281. package/dist/storage/sqlite/statements.js +88 -0
  282. package/dist/storage/sqlite/statements.js.map +1 -0
  283. package/dist/storage/sqlite/storage.d.ts +86 -0
  284. package/dist/storage/sqlite/storage.d.ts.map +1 -0
  285. package/dist/storage/sqlite/storage.js +534 -0
  286. package/dist/storage/sqlite/storage.js.map +1 -0
  287. package/dist/storage/store/commit.d.ts +33 -0
  288. package/dist/storage/store/commit.d.ts.map +1 -0
  289. package/dist/storage/store/commit.js +286 -0
  290. package/dist/storage/store/commit.js.map +1 -0
  291. package/dist/storage/store/documents.d.ts +43 -0
  292. package/dist/storage/store/documents.d.ts.map +1 -0
  293. package/dist/storage/store/documents.js +102 -0
  294. package/dist/storage/store/documents.js.map +1 -0
  295. package/dist/storage/store/index.d.ts +2 -0
  296. package/dist/storage/store/index.d.ts.map +1 -0
  297. package/dist/storage/store/index.js +2 -0
  298. package/dist/storage/store/index.js.map +1 -0
  299. package/dist/storage/store/queries.d.ts +49 -0
  300. package/dist/storage/store/queries.d.ts.map +1 -0
  301. package/dist/storage/store/queries.js +93 -0
  302. package/dist/storage/store/queries.js.map +1 -0
  303. package/dist/storage/store/storage.d.ts +60 -0
  304. package/dist/storage/store/storage.d.ts.map +1 -0
  305. package/dist/storage/store/storage.js +438 -0
  306. package/dist/storage/store/storage.js.map +1 -0
  307. package/dist/storage/store/tables.d.ts +54 -0
  308. package/dist/storage/store/tables.d.ts.map +1 -0
  309. package/dist/storage/store/tables.js +108 -0
  310. package/dist/storage/store/tables.js.map +1 -0
  311. package/dist/tasks.d.ts +6 -0
  312. package/dist/tasks.d.ts.map +1 -0
  313. package/dist/tasks.js +5 -0
  314. package/dist/tasks.js.map +1 -0
  315. package/dist/testing/assertions.d.ts +5 -0
  316. package/dist/testing/assertions.d.ts.map +1 -0
  317. package/dist/testing/assertions.js +25 -0
  318. package/dist/testing/assertions.js.map +1 -0
  319. package/dist/testing/commit-writes.d.ts +5 -0
  320. package/dist/testing/commit-writes.d.ts.map +1 -0
  321. package/dist/testing/commit-writes.js +12 -0
  322. package/dist/testing/commit-writes.js.map +1 -0
  323. package/dist/testing/env-conformance.d.ts +7 -0
  324. package/dist/testing/env-conformance.d.ts.map +1 -0
  325. package/dist/testing/env-conformance.js +531 -0
  326. package/dist/testing/env-conformance.js.map +1 -0
  327. package/dist/testing/index.d.ts +8 -0
  328. package/dist/testing/index.d.ts.map +1 -0
  329. package/dist/testing/index.js +7 -0
  330. package/dist/testing/index.js.map +1 -0
  331. package/dist/testing/runner.d.ts +12 -0
  332. package/dist/testing/runner.d.ts.map +1 -0
  333. package/dist/testing/runner.js +23 -0
  334. package/dist/testing/runner.js.map +1 -0
  335. package/dist/testing/storage-benchmark.d.ts +41 -0
  336. package/dist/testing/storage-benchmark.d.ts.map +1 -0
  337. package/dist/testing/storage-benchmark.js +351 -0
  338. package/dist/testing/storage-benchmark.js.map +1 -0
  339. package/dist/testing/storage-conformance.d.ts +4 -0
  340. package/dist/testing/storage-conformance.d.ts.map +1 -0
  341. package/dist/testing/storage-conformance.js +1554 -0
  342. package/dist/testing/storage-conformance.js.map +1 -0
  343. package/dist/testing/types.d.ts +39 -0
  344. package/dist/testing/types.d.ts.map +1 -0
  345. package/dist/testing/types.js +2 -0
  346. package/dist/testing/types.js.map +1 -0
  347. package/dist/timers.d.ts +21 -0
  348. package/dist/timers.d.ts.map +1 -0
  349. package/dist/timers.js +72 -0
  350. package/dist/timers.js.map +1 -0
  351. package/dist/tool-search/index.d.ts +2 -0
  352. package/dist/tool-search/index.d.ts.map +1 -0
  353. package/dist/tool-search/index.js +2 -0
  354. package/dist/tool-search/index.js.map +1 -0
  355. package/dist/tool-search/tool-search.d.ts +17 -0
  356. package/dist/tool-search/tool-search.d.ts.map +1 -0
  357. package/dist/tool-search/tool-search.js +96 -0
  358. package/dist/tool-search/tool-search.js.map +1 -0
  359. package/dist/tools/bash.d.ts +55 -0
  360. package/dist/tools/bash.d.ts.map +1 -0
  361. package/dist/tools/bash.js +139 -0
  362. package/dist/tools/bash.js.map +1 -0
  363. package/dist/tools/edit-diff.d.ts +89 -0
  364. package/dist/tools/edit-diff.d.ts.map +1 -0
  365. package/dist/tools/edit-diff.js +386 -0
  366. package/dist/tools/edit-diff.js.map +1 -0
  367. package/dist/tools/edit.d.ts +18 -0
  368. package/dist/tools/edit.d.ts.map +1 -0
  369. package/dist/tools/edit.js +117 -0
  370. package/dist/tools/edit.js.map +1 -0
  371. package/dist/tools/env.d.ts +5 -0
  372. package/dist/tools/env.d.ts.map +1 -0
  373. package/dist/tools/env.js +7 -0
  374. package/dist/tools/env.js.map +1 -0
  375. package/dist/tools/file-mutation-queue.d.ts +9 -0
  376. package/dist/tools/file-mutation-queue.d.ts.map +1 -0
  377. package/dist/tools/file-mutation-queue.js +52 -0
  378. package/dist/tools/file-mutation-queue.js.map +1 -0
  379. package/dist/tools/image-processor.d.ts +57 -0
  380. package/dist/tools/image-processor.d.ts.map +1 -0
  381. package/dist/tools/image-processor.js +49 -0
  382. package/dist/tools/image-processor.js.map +1 -0
  383. package/dist/tools/image.d.ts +20 -0
  384. package/dist/tools/image.d.ts.map +1 -0
  385. package/dist/tools/image.js +178 -0
  386. package/dist/tools/image.js.map +1 -0
  387. package/dist/tools/index.d.ts +46 -0
  388. package/dist/tools/index.d.ts.map +1 -0
  389. package/dist/tools/index.js +23 -0
  390. package/dist/tools/index.js.map +1 -0
  391. package/dist/tools/path-utils.d.ts +5 -0
  392. package/dist/tools/path-utils.d.ts.map +1 -0
  393. package/dist/tools/path-utils.js +26 -0
  394. package/dist/tools/path-utils.js.map +1 -0
  395. package/dist/tools/read.d.ts +30 -0
  396. package/dist/tools/read.d.ts.map +1 -0
  397. package/dist/tools/read.js +239 -0
  398. package/dist/tools/read.js.map +1 -0
  399. package/dist/tools/write.d.ts +10 -0
  400. package/dist/tools/write.d.ts.map +1 -0
  401. package/dist/tools/write.js +31 -0
  402. package/dist/tools/write.js.map +1 -0
  403. package/dist/truncate.d.ts +65 -0
  404. package/dist/truncate.d.ts.map +1 -0
  405. package/dist/truncate.js +124 -0
  406. package/dist/truncate.js.map +1 -0
  407. package/dist/types.d.ts +1244 -0
  408. package/dist/types.d.ts.map +1 -0
  409. package/dist/types.js +3 -0
  410. package/dist/types.js.map +1 -0
  411. package/package.json +179 -5
@@ -0,0 +1,135 @@
1
+ import { copyJson } from "@diffra-ai/chord/json";
2
+ import { defineDoc } from "../documents.js";
3
+ import { UserEntry } from "../entries.js";
4
+ export const InboxDoc = defineDoc({
5
+ kind: "diffra.inbox",
6
+ version: 1,
7
+ scope: "conversation",
8
+ history: "latest",
9
+ fork: "initial",
10
+ initial: () => ({ items: [] }),
11
+ checkpointWhen: (value) => value.items.length === 0,
12
+ });
13
+ /**
14
+ * Read what a boundary needs. Table reads must precede the commit's first table write, so callers prepare the
15
+ * boundary at the start of their commit.
16
+ */
17
+ export async function prepareBoundary(tx, conversationId, modes) {
18
+ const head = (await tx.latestHeadMarker(conversationId))?.head;
19
+ const inbox = await tx.doc(InboxDoc, conversationId);
20
+ return {
21
+ conversationId,
22
+ inbox,
23
+ steeringMode: modes.steeringMode,
24
+ followUpMode: modes.followUpMode,
25
+ head,
26
+ };
27
+ }
28
+ /**
29
+ * Place the queued items a boundary selects (spec §6): every write, the first or all steers, and at `final` the first
30
+ * or all follow-ups. A selected reset turns a `postTools` boundary into `final`. Writes are placed first and user
31
+ * items after them, each in ID order, so user items queued before a reset run in the new context. A write whose head
32
+ * targets an entry before the active range, including a range started earlier in this commit, is stale. Selected and
33
+ * stale items are removed positionally.
34
+ */
35
+ export async function applyBoundary(tx, boundary, at, now) {
36
+ const { conversationId, inbox } = boundary;
37
+ const items = inbox.items;
38
+ const reset = hasQueuedReset(items);
39
+ const final = at === "final" || reset;
40
+ const pick = (mode, queueMode) => {
41
+ const indexes = items.flatMap((item, index) => (item.mode === mode ? [index] : []));
42
+ return queueMode === "all" ? indexes : indexes.slice(0, 1);
43
+ };
44
+ const writes = items.flatMap((item, index) => (item.mode === "write" ? [index] : []));
45
+ const users = [...pick("steer", boundary.steeringMode), ...(final ? pick("followUp", boundary.followUpMode) : [])];
46
+ users.sort((a, b) => a - b);
47
+ // `appendEntry()` copies the drafts' values; the items are removed only afterwards.
48
+ let first;
49
+ for (const index of writes) {
50
+ const item = items[index];
51
+ const draft = item.entry;
52
+ if (isStale(boundary, draft)) {
53
+ tx.settleSubmission(item.id, { status: "unanswered", reason: "stale" });
54
+ continue;
55
+ }
56
+ const entry = await tx.appendEntry(conversationId, draft);
57
+ if (draft.head !== undefined)
58
+ boundary.head = draft.head === "self" ? entry.id : draft.head;
59
+ tx.placeSubmission(item.id, entry.id);
60
+ first ??= entry.id;
61
+ }
62
+ const placed = [];
63
+ for (const index of users) {
64
+ const item = items[index];
65
+ const entry = await appendInput(tx, conversationId, item.content, item.entry, now);
66
+ tx.placeSubmission(item.id, entry.id);
67
+ placed.push(item.id);
68
+ first ??= entry.id;
69
+ }
70
+ const removed = [...writes, ...users].sort((a, b) => b - a);
71
+ for (const index of removed)
72
+ items.splice(index, 1);
73
+ return { users: placed, first, reset };
74
+ }
75
+ /** Append the entry an input is placed as: `entry`'s kind and data, or `diffra.user`, with the user message of `content`. */
76
+ export function appendInput(tx, conversationId, content, entry, now) {
77
+ const message = { role: "user", content, timestamp: now };
78
+ if (entry === undefined)
79
+ return tx.appendEntry(UserEntry, conversationId, { model: [message] });
80
+ return tx.appendEntry(conversationId, { ...pickInputEntry(entry), model: [message] });
81
+ }
82
+ /** The kind and data of an input's entry as strict JSON, without other fields an untyped caller might pass. */
83
+ export function pickInputEntry({ kind, data }) {
84
+ const picked = data === undefined ? { kind } : { kind, data };
85
+ return copyJson(picked, { omitUndefinedProperties: true });
86
+ }
87
+ /**
88
+ * Whether a write is display-only: it has no `model`, `head`, or `edits` (present counts, even if empty), so it changes
89
+ * nothing the model sees and needs no boundary.
90
+ */
91
+ export function isDisplayOnly(entry) {
92
+ return entry.model === undefined && entry.head === undefined && entry.edits === undefined;
93
+ }
94
+ /** Whether a reset (`head: "self"` write) is queued; entries appended before it leave the active range when it lands. */
95
+ export function hasQueuedReset(items) {
96
+ return items.some((item) => item.mode === "write" && item.entry.head === "self");
97
+ }
98
+ /** Whether a head write targets an entry before the active range, so placing it would bring back cut history. */
99
+ export function isStale(boundary, entry) {
100
+ return typeof entry.head === "number" && boundary.head !== undefined && entry.head < boundary.head;
101
+ }
102
+ /** Remove a withdrawn submission's item; the caller settles the submission. */
103
+ export async function removeInboxItem(tx, conversationId, id) {
104
+ const items = (await tx.doc(InboxDoc, conversationId)).items;
105
+ const index = items.findIndex((item) => item.id === id);
106
+ if (index >= 0)
107
+ items.splice(index, 1);
108
+ }
109
+ /**
110
+ * Withdraw the queued inputs of a conversation as `Conversation.abort()` and abort cascades do: each settles
111
+ * `unanswered` with reason `aborted` and leaves the inbox, except those submitted with `onAbort: "keep"`; queued writes
112
+ * stay for later placement.
113
+ */
114
+ export function withdrawAbortedInputs(tx, conversationId) {
115
+ return withdrawInputs(tx, conversationId, "aborted", (item) => item.onAbort !== "keep");
116
+ }
117
+ /**
118
+ * Withdraw every queued input of a conversation, as a failed run does under `afterFailedRun: "withdraw"`: each settles
119
+ * `unanswered` with `reason` and leaves the inbox; queued writes stay for later placement.
120
+ */
121
+ export function withdrawQueuedInputs(tx, conversationId, reason) {
122
+ return withdrawInputs(tx, conversationId, reason, () => true);
123
+ }
124
+ /** Settle and remove the queued inputs `withdraws` selects, keeping the order of the items that stay. */
125
+ async function withdrawInputs(tx, conversationId, reason, withdraws) {
126
+ const items = (await tx.doc(InboxDoc, conversationId)).items;
127
+ for (let index = items.length - 1; index >= 0; index--) {
128
+ const item = items[index];
129
+ if (item.mode === "write" || !withdraws(item))
130
+ continue;
131
+ tx.settleSubmission(item.id, { status: "unanswered", reason });
132
+ items.splice(index, 1);
133
+ }
134
+ }
135
+ //# sourceMappingURL=inbox.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"inbox.js","sourceRoot":"","sources":["../../src/harness/inbox.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAA2B,MAAM,uBAAuB,CAAC;AAC1E,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAoB1C,MAAM,CAAC,MAAM,QAAQ,GAAG,SAAS,CAAa;IAC7C,IAAI,EAAE,cAAc;IACpB,OAAO,EAAE,CAAC;IACV,KAAK,EAAE,cAAc;IACrB,OAAO,EAAE,QAAQ;IACjB,IAAI,EAAE,SAAS;IACf,OAAO,EAAE,GAAG,EAAE,CAAC,CAAC,EAAE,KAAK,EAAE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,CAAC,IAAI,CAAC,EAAE,EAAE,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,CAAC,CAAC;QAC/D,KAAK,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;IACxB,CAAC;AACF,CAAC","sourcesContent":["import type { Draft } from \"@diffra-ai/chord/delta\";\nimport { copyJson, type JsonRepresentation } from \"@diffra-ai/chord/json\";\nimport { defineDoc } from \"../documents.ts\";\nimport { UserEntry } from \"../entries.ts\";\nimport type { ConversationId, EntryDraft, EntryId, EntryRecord, JsonObject, SubmissionId, Tx } from \"../types.ts\";\nimport type { InputEntry, QueueMode, Settings, UserInput } from \"./types.ts\";\n\n/** A queued submission: user input for a run, with the entry it is placed as when not `diffra.user`, or an entry write. */\nexport type InboxItem =\n\t| {\n\t\t\tid: SubmissionId;\n\t\t\tmode: \"steer\" | \"followUp\";\n\t\t\tcontent: JsonRepresentation<UserInput>;\n\t\t\tentry?: JsonRepresentation<InputEntry>;\n\t\t\t/** Submitted with `onAbort: \"keep\"`: an abort leaves it queued. */\n\t\t\tonAbort?: \"keep\";\n\t }\n\t/** `entry` is an `EntryDraft`, stored as plain JSON. */\n\t| { id: SubmissionId; mode: \"write\"; entry: JsonObject };\n\n/** Built-in queue of one conversation's submissions waiting for a boundary, in ID order. */\nexport type InboxState = { items: InboxItem[] };\n\nexport const InboxDoc = defineDoc<InboxState>({\n\tkind: \"diffra.inbox\",\n\tversion: 1,\n\tscope: \"conversation\",\n\thistory: \"latest\",\n\tfork: \"initial\",\n\tinitial: () => ({ items: [] }),\n\tcheckpointWhen: (value) => value.items.length === 0,\n});\n\n/** The settings a boundary reads, on the Session line. */\nexport type QueueModes = Pick<Settings, \"steeringMode\" | \"followUpMode\">;\n\n/** What a boundary reads before the commit's first table write, and the newest head it has seen so far. */\nexport type Boundary = {\n\treadonly conversationId: ConversationId;\n\treadonly inbox: Draft<InboxState>;\n\treadonly steeringMode: QueueMode;\n\treadonly followUpMode: QueueMode;\n\t/** Start of the active range, the newest head marker's `head`; advanced by heads written in this commit. */\n\thead: EntryId | undefined;\n};\n\n/**\n * Selected user items, in ID order, the first entry the boundary placed, a write's or a user item's (a run it starts\n * begins there, `LiveRun.first`), and whether a `head: \"self\"` write (a reset) was placed.\n */\nexport type BoundaryResult = {\n\treadonly users: SubmissionId[];\n\treadonly first: EntryId | undefined;\n\treadonly reset: boolean;\n};\n\n/**\n * Read what a boundary needs. Table reads must precede the commit's first table write, so callers prepare the\n * boundary at the start of their commit.\n */\nexport async function prepareBoundary(tx: Tx, conversationId: ConversationId, modes: QueueModes): Promise<Boundary> {\n\tconst head = (await tx.latestHeadMarker(conversationId))?.head;\n\tconst inbox = await tx.doc(InboxDoc, conversationId);\n\treturn {\n\t\tconversationId,\n\t\tinbox,\n\t\tsteeringMode: modes.steeringMode,\n\t\tfollowUpMode: modes.followUpMode,\n\t\thead,\n\t};\n}\n\n/**\n * Place the queued items a boundary selects (spec §6): every write, the first or all steers, and at `final` the first\n * or all follow-ups. A selected reset turns a `postTools` boundary into `final`. Writes are placed first and user\n * items after them, each in ID order, so user items queued before a reset run in the new context. A write whose head\n * targets an entry before the active range, including a range started earlier in this commit, is stale. Selected and\n * stale items are removed positionally.\n */\nexport async function applyBoundary(\n\ttx: Tx,\n\tboundary: Boundary,\n\tat: \"postTools\" | \"final\",\n\tnow: number,\n): Promise<BoundaryResult> {\n\tconst { conversationId, inbox } = boundary;\n\tconst items = inbox.items;\n\tconst reset = hasQueuedReset(items);\n\tconst final = at === \"final\" || reset;\n\tconst pick = (mode: \"steer\" | \"followUp\", queueMode: QueueMode): number[] => {\n\t\tconst indexes = items.flatMap((item, index) => (item.mode === mode ? [index] : []));\n\t\treturn queueMode === \"all\" ? indexes : indexes.slice(0, 1);\n\t};\n\tconst writes = items.flatMap((item, index) => (item.mode === \"write\" ? [index] : []));\n\tconst users = [...pick(\"steer\", boundary.steeringMode), ...(final ? pick(\"followUp\", boundary.followUpMode) : [])];\n\tusers.sort((a, b) => a - b);\n\n\t// `appendEntry()` copies the drafts' values; the items are removed only afterwards.\n\tlet first: EntryId | undefined;\n\tfor (const index of writes) {\n\t\tconst item = items[index] as Draft<Extract<InboxItem, { mode: \"write\" }>>;\n\t\tconst draft = item.entry as unknown as EntryDraft;\n\t\tif (isStale(boundary, draft)) {\n\t\t\ttx.settleSubmission(item.id, { status: \"unanswered\", reason: \"stale\" });\n\t\t\tcontinue;\n\t\t}\n\t\tconst entry = await tx.appendEntry(conversationId, draft);\n\t\tif (draft.head !== undefined) boundary.head = draft.head === \"self\" ? entry.id : draft.head;\n\t\ttx.placeSubmission(item.id, entry.id);\n\t\tfirst ??= entry.id;\n\t}\n\tconst placed: SubmissionId[] = [];\n\tfor (const index of users) {\n\t\tconst item = items[index] as Draft<Extract<InboxItem, { mode: \"steer\" | \"followUp\" }>>;\n\t\tconst entry = await appendInput(tx, conversationId, item.content as UserInput, item.entry as InputEntry, now);\n\t\ttx.placeSubmission(item.id, entry.id);\n\t\tplaced.push(item.id);\n\t\tfirst ??= entry.id;\n\t}\n\tconst removed = [...writes, ...users].sort((a, b) => b - a);\n\tfor (const index of removed) items.splice(index, 1);\n\treturn { users: placed, first, reset };\n}\n\n/** Append the entry an input is placed as: `entry`'s kind and data, or `diffra.user`, with the user message of `content`. */\nexport function appendInput(\n\ttx: Tx,\n\tconversationId: ConversationId,\n\tcontent: UserInput,\n\tentry: InputEntry | undefined,\n\tnow: number,\n): Promise<EntryRecord> {\n\tconst message = { role: \"user\", content, timestamp: now } as const;\n\tif (entry === undefined) return tx.appendEntry(UserEntry, conversationId, { model: [message] });\n\treturn tx.appendEntry(conversationId, { ...pickInputEntry(entry), model: [message] });\n}\n\n/** The kind and data of an input's entry as strict JSON, without other fields an untyped caller might pass. */\nexport function pickInputEntry({ kind, data }: InputEntry): JsonRepresentation<InputEntry> {\n\tconst picked = data === undefined ? { kind } : { kind, data };\n\treturn copyJson(picked, { omitUndefinedProperties: true }) as JsonRepresentation<InputEntry>;\n}\n\n/**\n * Whether a write is display-only: it has no `model`, `head`, or `edits` (present counts, even if empty), so it changes\n * nothing the model sees and needs no boundary.\n */\nexport function isDisplayOnly(entry: EntryDraft): boolean {\n\treturn entry.model === undefined && entry.head === undefined && entry.edits === undefined;\n}\n\n/** Whether a reset (`head: \"self\"` write) is queued; entries appended before it leave the active range when it lands. */\nexport function hasQueuedReset(items: readonly InboxItem[]): boolean {\n\treturn items.some((item) => item.mode === \"write\" && item.entry.head === \"self\");\n}\n\n/** Whether a head write targets an entry before the active range, so placing it would bring back cut history. */\nexport function isStale(boundary: Boundary, entry: EntryDraft): boolean {\n\treturn typeof entry.head === \"number\" && boundary.head !== undefined && entry.head < boundary.head;\n}\n\n/** Remove a withdrawn submission's item; the caller settles the submission. */\nexport async function removeInboxItem(tx: Tx, conversationId: ConversationId, id: SubmissionId): Promise<void> {\n\tconst items = (await tx.doc(InboxDoc, conversationId)).items;\n\tconst index = items.findIndex((item) => item.id === id);\n\tif (index >= 0) items.splice(index, 1);\n}\n\n/**\n * Withdraw the queued inputs of a conversation as `Conversation.abort()` and abort cascades do: each settles\n * `unanswered` with reason `aborted` and leaves the inbox, except those submitted with `onAbort: \"keep\"`; queued writes\n * stay for later placement.\n */\nexport function withdrawAbortedInputs(tx: Tx, conversationId: ConversationId): Promise<void> {\n\treturn withdrawInputs(tx, conversationId, \"aborted\", (item) => item.onAbort !== \"keep\");\n}\n\n/**\n * Withdraw every queued input of a conversation, as a failed run does under `afterFailedRun: \"withdraw\"`: each settles\n * `unanswered` with `reason` and leaves the inbox; queued writes stay for later placement.\n */\nexport function withdrawQueuedInputs(tx: Tx, conversationId: ConversationId, reason: string): Promise<void> {\n\treturn withdrawInputs(tx, conversationId, reason, () => true);\n}\n\n/** Settle and remove the queued inputs `withdraws` selects, keeping the order of the items that stay. */\nasync function withdrawInputs(\n\ttx: Tx,\n\tconversationId: ConversationId,\n\treason: string,\n\twithdraws: (item: Exclude<InboxItem, { mode: \"write\" }>) => boolean,\n): Promise<void> {\n\tconst items = (await tx.doc(InboxDoc, conversationId)).items;\n\tfor (let index = items.length - 1; index >= 0; index--) {\n\t\tconst item = items[index]!;\n\t\tif (item.mode === \"write\" || !withdraws(item)) continue;\n\t\ttx.settleSubmission(item.id, { status: \"unanswered\", reason });\n\t\titems.splice(index, 1);\n\t}\n}\n"]}
@@ -0,0 +1,26 @@
1
+ import type { JsonValue } from "@diffra-ai/chord/json";
2
+ /** How `assignJson()` writes one value instead of assigning it leaf by leaf. */
3
+ export type AssignPlan =
4
+ /** The string grew by `text` since it was last assigned: append it, when the lengths agree. */
5
+ {
6
+ readonly kind: "append";
7
+ readonly text: string;
8
+ }
9
+ /** Leave the value as it is; the caller writes it. */
10
+ | {
11
+ readonly kind: "keep";
12
+ }
13
+ /** Plans for some members or items of a container. */
14
+ | {
15
+ readonly kind: "fields";
16
+ readonly fields: ReadonlyMap<string | number, AssignPlan>;
17
+ };
18
+ /**
19
+ * Assign `value` at `target[key]` leaf by leaf. Chord records a container assignment as one full set and only emits an
20
+ * append when a string leaf is reassigned with a longer string, so writing the partial whole would store and publish the
21
+ * complete message on every flush. That append costs a compare of the whole string, though; where `plan` says what a
22
+ * string member grew by, and the committed string plus that text has the new string's length, the text is appended
23
+ * with `appendText()` instead.
24
+ */
25
+ export declare function assignJson<T extends object, K extends keyof T & (string | number)>(target: T, key: K, value: T[K] & JsonValue, plan?: AssignPlan): void;
26
+ //# sourceMappingURL=json.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"json.d.ts","sourceRoot":"","sources":["../../src/harness/json.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAEvD,gFAAgF;AAChF,MAAM,MAAM,UAAU;AACrB,+FAA+F;AAC7F;IAAE,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;CAAE;AACpD,sDAAsD;GACpD;IAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;CAAE;AAC3B,sDAAsD;GACpD;IAAE,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,WAAW,CAAC,MAAM,GAAG,MAAM,EAAE,UAAU,CAAC,CAAA;CAAE,CAAC;AAE1F;;;;;;GAMG;AACH,wBAAgB,UAAU,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,MAAM,CAAC,GAAG,CAAC,MAAM,GAAG,MAAM,CAAC,EACjF,MAAM,EAAE,CAAC,EACT,GAAG,EAAE,CAAC,EACN,KAAK,EAAE,CAAC,CAAC,CAAC,CAAC,GAAG,SAAS,EACvB,IAAI,CAAC,EAAE,UAAU,GACf,IAAI,CAiCN"}
@@ -0,0 +1,51 @@
1
+ import { appendText } from "@diffra-ai/chord/delta";
2
+ /**
3
+ * Assign `value` at `target[key]` leaf by leaf. Chord records a container assignment as one full set and only emits an
4
+ * append when a string leaf is reassigned with a longer string, so writing the partial whole would store and publish the
5
+ * complete message on every flush. That append costs a compare of the whole string, though; where `plan` says what a
6
+ * string member grew by, and the committed string plus that text has the new string's length, the text is appended
7
+ * with `appendText()` instead.
8
+ */
9
+ export function assignJson(target, key, value, plan) {
10
+ if (plan?.kind === "keep")
11
+ return;
12
+ // Every target is a document container: JSON members or items, whatever its declared type names them.
13
+ const slots = target;
14
+ // A `__proto__` member the target lacks reads through to Object.prototype; merging into that would pollute every
15
+ // object in the process. Other inherited members are functions, which are not merged into.
16
+ const current = key === "__proto__" && !Object.hasOwn(slots, key) ? undefined : slots[key];
17
+ const fields = plan?.kind === "fields" ? plan.fields : undefined;
18
+ if (isRecord(current) && isRecord(value)) {
19
+ for (const name of Object.keys(current))
20
+ if (!Object.hasOwn(value, name))
21
+ delete current[name];
22
+ for (const [name, child] of Object.entries(value))
23
+ assignJson(current, name, child, fields?.get(name));
24
+ return;
25
+ }
26
+ if (Array.isArray(current) && Array.isArray(value) && current.length <= value.length) {
27
+ const items = current;
28
+ for (let index = 0; index < value.length; index++) {
29
+ if (index < items.length)
30
+ assignJson(items, index, value[index], fields?.get(index));
31
+ else
32
+ items.push(value[index]);
33
+ }
34
+ return;
35
+ }
36
+ if (plan?.kind === "append" &&
37
+ typeof current === "string" &&
38
+ typeof value === "string" &&
39
+ !Array.isArray(target) &&
40
+ current.length + plan.text.length === value.length) {
41
+ // The cast names what the checks above established: `target[key]` is a string.
42
+ appendText(target, String(key), plan.text);
43
+ return;
44
+ }
45
+ if (current !== value)
46
+ slots[key] = value;
47
+ }
48
+ function isRecord(value) {
49
+ return typeof value === "object" && value !== null && !Array.isArray(value);
50
+ }
51
+ //# sourceMappingURL=json.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"json.js","sourceRoot":"","sources":["../../src/harness/json.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAc,MAAM,wBAAwB,CAAC;AAYhE;;;;;;GAMG;AACH,MAAM,UAAU,UAAU,CACzB,MAAS,EACT,GAAM,EACN,KAAuB,EACvB,IAAiB;IAEjB,IAAI,IAAI,EAAE,IAAI,KAAK,MAAM;QAAE,OAAO;IAClC,sGAAsG;IACtG,MAAM,KAAK,GAAG,MAA4C,CAAC;IAC3D,iHAAiH;IACjH,2FAA2F;IAC3F,MAAM,OAAO,GAA0B,GAAG,KAAK,WAAW,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAClH,MAAM,MAAM,GAAG,IAAI,EAAE,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC;IACjE,IAAI,QAAQ,CAAC,OAAO,CAAC,IAAI,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;QAC1C,KAAK,MAAM,IAAI,IAAI,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC;Y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C;QAC5E,OAAO;IACR,CAAC;IACD,IAAI,OAAO,KAAK,KAAK;QAAE,KAAK,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;AAC3C,CAAC;AAED,SAAS,QAAQ,CAAC,KAA4B;IAC7C,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAC7E,CAAC","sourcesContent":["import { appendText, type Draft } from \"@diffra-ai/chord/delta\";\nimport type { JsonValue } from \"@diffra-ai/chord/json\";\n\n/** How `assignJson()` writes one value instead of assigning it leaf by leaf. */\nexport type AssignPlan =\n\t/** The string grew by `text` since it was last assigned: append it, when the lengths agree. */\n\t| { readonly kind: \"append\"; readonly text: string }\n\t/** Leave the value as it is; the caller writes it. */\n\t| { readonly kind: \"keep\" }\n\t/** Plans for some members or items of a container. */\n\t| { readonly kind: \"fields\"; readonly fields: ReadonlyMap<string | number, AssignPlan> };\n\n/**\n * Assign `value` at `target[key]` leaf by leaf. Chord records a container assignment as one full set and only emits an\n * append when a string leaf is reassigned with a longer string, so writing the partial whole would store and publish the\n * complete message on every flush. That append costs a compare of the whole string, though; where `plan` says what a\n * string member grew by, and the committed string plus that text has the new string's length, the text is appended\n * with `appendText()` instead.\n */\nexport function assignJson<T extends object, K extends keyof T & (string | number)>(\n\ttarget: T,\n\tkey: K,\n\tvalue: T[K] & JsonValue,\n\tplan?: AssignPlan,\n): void {\n\tif (plan?.kind === \"keep\") return;\n\t// Every target is a document container: JSON members or items, whatever its declared type names them.\n\tconst slots = target as Record<string | number, JsonValue>;\n\t// A `__proto__` member the target lacks reads through to Object.prototype; merging into that would pollute every\n\t// object in the process. Other inherited members are functions, which are not merged into.\n\tconst current: JsonValue | undefined = key === \"__proto__\" && !Object.hasOwn(slots, key) ? undefined : slots[key];\n\tconst fields = plan?.kind === \"fields\" ? plan.fields : undefined;\n\tif (isRecord(current) && isRecord(value)) {\n\t\tfor (const name of Object.keys(current)) if (!Object.hasOwn(value, name)) delete current[name];\n\t\tfor (const [name, child] of Object.entries(value)) assignJson(current, name, child, fields?.get(name));\n\t\treturn;\n\t}\n\tif (Array.isArray(current) && Array.isArray(value) && current.length <= value.length) {\n\t\tconst items = current as JsonValue[];\n\t\tfor (let index = 0; index < value.length; index++) {\n\t\t\tif (index < items.length) assignJson(items, index, value[index]!, fields?.get(index));\n\t\t\telse items.push(value[index]!);\n\t\t}\n\t\treturn;\n\t}\n\tif (\n\t\tplan?.kind === \"append\" &&\n\t\ttypeof current === \"string\" &&\n\t\ttypeof value === \"string\" &&\n\t\t!Array.isArray(target) &&\n\t\tcurrent.length + plan.text.length === value.length\n\t) {\n\t\t// The cast names what the checks above established: `target[key]` is a string.\n\t\tappendText(target as Draft<Record<string, string>>, String(key), plan.text);\n\t\treturn;\n\t}\n\tif (current !== value) slots[key] = value;\n}\n\nfunction isRecord(value: JsonValue | undefined): value is Record<string, JsonValue> {\n\treturn typeof value === \"object\" && value !== null && !Array.isArray(value);\n}\n"]}
@@ -0,0 +1,285 @@
1
+ import type { AssistantMessage, Usage } from "@diffra-ai/ai";
2
+ import type { Draft } from "@diffra-ai/chord/delta";
3
+ import type { JsonRepresentation, JsonValue } from "@diffra-ai/chord/json";
4
+ import type { Transaction } from "../session/transaction.ts";
5
+ import type { CommitOptions, EntryId, JsonObject, SubmissionId, SubmissionSettlement, TaskId, TaskRecord, Tx } from "../types.ts";
6
+ import type { SchedulerOutcome } from "./scheduler.ts";
7
+ import type { CompactionReason, CompactionResult, ToolDiagnostic } from "./types.ts";
8
+ /** What a running tool call publishes, shared by model-issued and nested calls. */
9
+ type SlotProgress = {
10
+ callId: string;
11
+ name: string;
12
+ /** `sleeping` or `waiting` while the call is parked (spec suspendable §4.2); a parked slot holds no output. */
13
+ status: "pending" | "running" | "sleeping" | "waiting" | "done";
14
+ /** Retained running output and what the bounds dropped. */
15
+ output?: string;
16
+ droppedBytes?: number;
17
+ droppedLines?: number;
18
+ /** Last `details()` value. */
19
+ details?: JsonValue;
20
+ /** Diagnostics recorded through `api.diagnostic()`. */
21
+ diagnostics?: ToolDiagnostic[];
22
+ };
23
+ /** Presentation of one tool call of the current round. */
24
+ export type ToolSlot = SlotProgress & {
25
+ /** Absent for a call not started yet (sequential round). */
26
+ taskId?: TaskId;
27
+ /** A call to a tool its request did not offer: its task settles `tool_unavailable` without running anything. */
28
+ unoffered?: true;
29
+ /** Result entry once done; absent when the tool task faulted or was orphaned. */
30
+ entry?: EntryId;
31
+ };
32
+ /**
33
+ * Presentation of one nested call: a call a running tool made through `executeTool()`. `callId` is
34
+ * `<parentCallId>/<key>`, the call's ID, unique because keys contain no `/`. The Harness relates
35
+ * slots by task ID: `taskId` is the call's tool task, `parentTaskId` the calling one, model-issued or nested.
36
+ */
37
+ export type NestedToolSlot = SlotProgress & {
38
+ parentCallId: string;
39
+ parentTaskId: TaskId;
40
+ taskId: TaskId;
41
+ /**
42
+ * What the call was made with, replaced by the arguments it runs with, after repair, `beforeTool`, and coercion,
43
+ * once it starts executing. A model-issued call's arguments are in its assistant entry instead.
44
+ */
45
+ arguments: JsonObject;
46
+ /** Once done: how the call ended, for a status line. The result itself is in the caller's `NestedCallDoc`. */
47
+ summary?: NestedToolSummary;
48
+ };
49
+ /** How a nested call ended: an error flag, its execution time, its spend, and up to 500 characters of error text. */
50
+ export type NestedToolSummary = {
51
+ isError: boolean;
52
+ durationMs?: number;
53
+ usage?: JsonRepresentation<Usage>;
54
+ error?: string;
55
+ };
56
+ /** Presentation of one live compaction task (spec §8.7). */
57
+ export type CompactionStatus = {
58
+ taskId: TaskId<CompactionResult>;
59
+ /** What started it: `compact()`, a threshold, or a context overflow. */
60
+ trigger: CompactionReason;
61
+ /** Whether a generation waits for it: a compaction the generation owns. */
62
+ blocking: boolean;
63
+ attempt: number;
64
+ /** Durable backoff before the next summarization attempt. */
65
+ retry?: {
66
+ at: number;
67
+ error: string;
68
+ };
69
+ };
70
+ /** The newest compaction that failed since a compaction last produced a summary (spec §8.7). */
71
+ export type CompactionFailure = {
72
+ taskId: TaskId<CompactionResult>;
73
+ /** What started the compaction that failed. */
74
+ trigger: CompactionReason;
75
+ /** The failed or faulted outcome's error message, or why the scheduler orphaned the task. */
76
+ error: string;
77
+ };
78
+ /**
79
+ * Why a run ended without an answer:
80
+ * - `model_error`: the model's response failed, after any retries, or the compaction a request needed failed;
81
+ * - `no_model`: no model is configured, or the configured one is not available;
82
+ * - `aborted`: the run was aborted (`Conversation.abort()`, `abortTask()`); `detail` says when its abort handler was
83
+ * cut off `settings.restartLimit` times and did not finish;
84
+ * - `reset`: a queued reset started a new context at the post-tools boundary, before an answer (a tool's handoff
85
+ * continues the run);
86
+ * - `faulted`: the run's task threw where it cannot recover, or storage rejected its commit;
87
+ * - `orphaned`: the run's task was aborted while no registered definition could take it;
88
+ * - `interrupted`: the host stopped while the model was answering, more often than `settings.retry` retries;
89
+ * - `restart_limit`: the run's generation was cut off `settings.restartLimit` times in a row; a request or poll it was
90
+ * cut off during counts as a failed attempt first, so this ends one whose charge itself keeps failing;
91
+ * - `nothing_to_answer`: the context did not end in a user message or tool results (empty, or ending in an answer), so
92
+ * no request was sent, as for a continue after an answer;
93
+ * - `turn_limit`: a tool round reached the conversation's `maxTurns`; a continue resumes on its results.
94
+ */
95
+ export type RunFailureReason = "model_error" | "no_model" | "aborted" | "reset" | "faulted" | "orphaned" | "interrupted" | "restart_limit" | "nothing_to_answer" | "turn_limit";
96
+ /** The newest run that ended without an answer (spec §8.2). */
97
+ export type RunFailure = {
98
+ /** The task that ended the run: the run's task at its end. */
99
+ taskId: TaskId;
100
+ /** The run's inputs, settled `unanswered`. */
101
+ inputs: SubmissionId[];
102
+ reason: RunFailureReason;
103
+ /**
104
+ * `model_error`: the error text; `no_model`: which model is missing; `faulted`: the error message; `orphaned`: why
105
+ * the task could not run (`missing_task`, `task_too_old`, `migration_failed`); `interrupted` and `restart_limit`: what
106
+ * was cut off, and how often; `turn_limit`: the tool rounds the run took, `"1 turn"` or `"N turns"`.
107
+ */
108
+ detail?: string;
109
+ /** The failed run's `run.first`, so a continue's `afterRun` hooks see its history too. */
110
+ first?: EntryId;
111
+ };
112
+ /** Run control (spec §8.2). */
113
+ export type LiveRun = {
114
+ /** The task that settles the run's inputs: its generation, handed from one to the next. */
115
+ taskId: TaskId;
116
+ /** The run's placed inputs, settled when it ends; `[]` once it answered and runs only its `afterRun` tasks. */
117
+ inputs: SubmissionId[];
118
+ /** Tool rounds so far, which `AgentState.maxTurns` bounds; an `onYield` continuation starts the count over. */
119
+ turns: number;
120
+ /**
121
+ * The run's first entry: the first one the boundary that started it placed, a continue's the failed run's it
122
+ * continues; otherwise its first tool-calling answer. A run started before it was recorded may lack it; `afterRun`
123
+ * hooks then get the answer as the first entry.
124
+ */
125
+ first?: EntryId;
126
+ /**
127
+ * Present once the run answered and its generation waits for the tasks `afterRun` hooks asked for, listed once
128
+ * created: queued items stay queued until they are terminal, and the run then ends without a failure.
129
+ */
130
+ afterRun?: {
131
+ tasks?: TaskId[];
132
+ };
133
+ /**
134
+ * Present while tasks an `onYield` hook asked for decide whether the answer stands (spec suspendable §6): the
135
+ * answer's entry, which the tasks read, and the tasks. The inputs stay open; every exit but a continuation settles
136
+ * them `done` with this answer.
137
+ */
138
+ yield?: {
139
+ answer: EntryId;
140
+ tasks: TaskId[];
141
+ };
142
+ };
143
+ /** Built-in live conversation state: run control and presentation of the current generation and tool round. */
144
+ export type LiveState = {
145
+ /** Run control; present exactly while busy, also while a run that answered runs its `afterRun` tasks. */
146
+ run?: LiveRun;
147
+ /** Presentation of the current generation attempt. */
148
+ generation?: {
149
+ attempt: number;
150
+ /** Committed throttled partial of the in-flight response. */
151
+ message?: JsonRepresentation<AssistantMessage>;
152
+ /** Durable backoff before the next attempt. */
153
+ retry?: {
154
+ at: number;
155
+ error: string;
156
+ };
157
+ /** Provider-side deferred response being polled. */
158
+ deferred?: {
159
+ pollAt: number;
160
+ };
161
+ };
162
+ /** The current tool round in call order, from the tool-calling answer until the generation's `tools` phase ends it. */
163
+ tools?: ToolSlot[];
164
+ /**
165
+ * Nested calls of running tool calls, in creation order, so a parent precedes its children and task IDs ascend. A
166
+ * call's nested slots are removed when the call settles.
167
+ */
168
+ nestedTools?: NestedToolSlot[];
169
+ /** Live compaction tasks in task ID order; absent when none. */
170
+ compactions?: CompactionStatus[];
171
+ /**
172
+ * The newest compaction failure, so a client can show it after the task ended. A compaction that produces a summary
173
+ * removes it; one that is aborted or finds nothing to compact leaves it.
174
+ */
175
+ compactionFailure?: CompactionFailure;
176
+ /**
177
+ * Why the newest run ended without an answer, so a client can show it after the run ended. Set in the commit that
178
+ * ends the run; removed when a later commit starts a run. A run started in the same commit, by
179
+ * `afterFailedRun: "continue"` or after a reset, keeps it.
180
+ */
181
+ runFailure?: RunFailure;
182
+ };
183
+ export declare const LiveDoc: import("../types.ts").ConversationDocToken<LiveState>;
184
+ /**
185
+ * How the progress throttles commit a partial answer and running tool output into `diffra.live`: deferred, so storage gets
186
+ * them together, at the next commit that reads or writes `diffra.live` or after `settings.progress.persistWithinMs`. Every
187
+ * commit that consumes or clears streamed fields reads `diffra.live`, so it stores them with its own changes (spec §8.2).
188
+ */
189
+ export declare const PROGRESS_COMMIT: CommitOptions;
190
+ /**
191
+ * Built-in task kinds that end themselves once cut off `settings.restartLimit` times (`TaskRecord.restarts`); the
192
+ * scheduler faults a task of any other kind instead (spec §5.4).
193
+ */
194
+ export declare const LIMITS_RESTARTS: ReadonlySet<string>;
195
+ /** Whether `task` was cut off `settings.restartLimit` times in a row, so its phase ends it instead of running. */
196
+ export declare function cutOffTooOften(task: {
197
+ readonly restarts?: number;
198
+ }, runtime: {
199
+ readonly settings: {
200
+ readonly restartLimit: number;
201
+ };
202
+ }): boolean;
203
+ /**
204
+ * Whether a task at the limit may still do the cleanup it does when it ends, such as converting a partial: only at the
205
+ * limit. Past it, that cleanup may be what cut the task off, so the task ends without it.
206
+ */
207
+ export declare function salvageable(task: {
208
+ readonly restarts?: number;
209
+ }, runtime: {
210
+ readonly settings: {
211
+ readonly restartLimit: number;
212
+ };
213
+ }): boolean;
214
+ /** How often a task was cut off, for a message: "once", or "3 times in a row". */
215
+ export declare function cutOffCount(task: {
216
+ readonly restarts?: number;
217
+ }): string;
218
+ /** How a run ends: with an answer, or without one, settling its inputs `unanswered` and recording why. */
219
+ export type RunEnd = {
220
+ readonly status: "done";
221
+ readonly answer: EntryId;
222
+ } | {
223
+ readonly status: "unanswered";
224
+ readonly reason: RunFailureReason;
225
+ readonly detail?: string;
226
+ /** The inputs' settlement; default `unanswered` with `reason` and `detail`. */
227
+ readonly settlement?: SubmissionSettlement;
228
+ };
229
+ /**
230
+ * End the run owned by `taskId`: settle each of its inputs, remove `run`, and, without an answer, record
231
+ * `runFailure`. A run that answered already, and runs only its `afterRun` tasks, records no failure, however it ends.
232
+ * Always removes `generation`, `tools`, and `nestedTools`, whose presentation belongs to the ending run.
233
+ */
234
+ export declare function endRun(tx: Tx, live: Draft<LiveState>, taskId: TaskId, end: RunEnd): void;
235
+ /** Add the status of a compaction task created in this commit; statuses stay in task ID order. */
236
+ export declare function addCompactionStatus(live: Draft<LiveState>, status: CompactionStatus): void;
237
+ /** The status of compaction task `taskId`, if listed. */
238
+ export declare function compactionStatus(live: Draft<LiveState>, taskId: TaskId): Draft<CompactionStatus> | undefined;
239
+ /** Remove the status of compaction task `taskId`, and the list once empty. */
240
+ export declare function removeCompactionStatus(live: Draft<LiveState>, taskId: TaskId): void;
241
+ /** Remove the status of compaction task `taskId` and record that it failed with `error`. */
242
+ export declare function failCompactionStatus(live: Draft<LiveState>, taskId: TaskId, error: string): void;
243
+ /**
244
+ * The slot of tool task `taskId`, model-issued or nested, if `diffra.live` still lists it. Nested slots are found by binary
245
+ * search: a tool may make thousands of nested calls, each of which looks up its slot in several commits, and every
246
+ * element a search reads costs a node in the draft's change tracker. Their task IDs ascend: each slot is appended in
247
+ * the commit that creates its task, task IDs ascend in commit order, and removal keeps the order.
248
+ */
249
+ export declare function toolSlot(live: Draft<LiveState>, taskId: TaskId): Draft<ToolSlot | NestedToolSlot> | undefined;
250
+ /** A tool task's slot in `diffra.live`, told apart by whose call it is: a model-issued call's, or a nested call's. */
251
+ export type FoundSlot = {
252
+ readonly kind: "model";
253
+ readonly slot: Draft<ToolSlot>;
254
+ } | {
255
+ readonly kind: "nested";
256
+ readonly slot: Draft<NestedToolSlot>;
257
+ };
258
+ /** The slot of tool task `taskId` with whose call it is, if `diffra.live` still lists it (see `toolSlot`). */
259
+ export declare function findSlot(live: Draft<LiveState>, taskId: TaskId): FoundSlot | undefined;
260
+ /**
261
+ * Mark a slot done and clear its running output; the caller sets a model-issued call's result `entry`, or a nested
262
+ * call's `summary`. Neither is set when the task faulted or was orphaned.
263
+ */
264
+ export declare function finishSlot(slot: Draft<ToolSlot | NestedToolSlot>): void;
265
+ /** Remove the nested slots below tool task `taskId`, transitively; the list holds parents before their children. */
266
+ export declare function removeNestedSlots(live: Draft<LiveState>, taskId: TaskId): void;
267
+ /** Remove what a tool published while running; its result entry or a rerun replaces it. */
268
+ export declare function clearProgress(slot: Draft<SlotProgress>): void;
269
+ /** Remove a slot's running output only: a parked call, and its next run, keep its details and diagnostics. */
270
+ export declare function clearOutput(slot: Draft<SlotProgress>): void;
271
+ /**
272
+ * Harness cleanup for a terminal outcome the scheduler writes itself (`faulted` or `orphaned`). A run task ends its
273
+ * run; a tool task's slot is marked done without an entry, and context derivation synthesizes the missing result; a
274
+ * compaction task's status is removed and its failure recorded.
275
+ * Ignores other kinds so it never creates `diffra.live` elsewhere. The scheduler calls this without knowing task kinds;
276
+ * the Harness passes it in (spec §5.4).
277
+ * REMINDER: a committed generation partial becomes an aborted assistant entry here, exactly as in the generation abort
278
+ * handler, so the transcript keeps what the model produced and `diffra.usage` counts its spend. The scheduler's commit has
279
+ * no task scope, so that entry has no `byTaskId`. Faults come from task bugs
280
+ * or malformed provider data (a non-JSON value in a response), or a commit the Storage rejected without effect; an
281
+ * uncertain storage failure poisons the Session instead and writes no outcome.
282
+ */
283
+ export declare function settleSchedulerOutcome(tx: Transaction, record: TaskRecord<JsonValue, JsonValue, JsonValue>, outcome: SchedulerOutcome): Promise<void>;
284
+ export {};
285
+ //# sourceMappingURL=live.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"live.d.ts","sourceRoot":"","sources":["../../src/harness/live.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AAC7D,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,wBAAwB,CAAC;AACpD,OAAO,KAAK,EAAE,kBAAkB,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAE3E,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAC;AAC7D,OAAO,KAAK,EACX,aAAa,EACb,OAAO,EACP,UAAU,EACV,YAAY,EACZ,oBAAoB,EACpB,MAAM,EACN,UAAU,EACV,EAAE,EACF,MAAM,aAAa,CAAC;AAErB,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AACvD,OAAO,KAAK,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAErF,mFAAmF;AACnF,KAAK,YAAY,GAAG;IACnB,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,+GAA+G;IAC/G,MAAM,EAAE,SAAS,GAAG,SAAS,GAAG,UAAU,GAAG,SAAS,GAAG,MAAM,CAAC;IAChE,2DAA2D;IAC3D,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,8BAA8B;IAC9B,OAAO,CAAC,EAAE,SAAS,CAAC;IACpB,uDAAuD;IACvD,WAAW,CAAC,EAAE,cAAc,EAAE,CAAC;CAC/B,CAAC;AAEF,0DAA0D;AAC1D,MAAM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