@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,1244 @@
1
+ import type { Message, Models } from "@diffra-ai/ai";
2
+ import type { Context } from "@diffra-ai/chord/context";
3
+ import type { Draft, Op } from "@diffra-ai/chord/delta";
4
+ import type { JsonValue } from "@diffra-ai/chord/json";
5
+ import type { WatchCall, WatchEnd, WatchHandle } from "@diffra-ai/chord/watch";
6
+ import type { TelemetryCarrier } from "@diffra-ai/telemetry";
7
+ import type { ExecutionEnv } from "./env/index.ts";
8
+ import type { Agent, ContextView, ConversationHandle, RegistrySnapshot, Settings, SettledTask } from "./harness/types.ts";
9
+ import type { SignalKey, SignalResult, SignalState, SignalToken } from "./signals.ts";
10
+ /** JSON object used as the root of every durable document. */
11
+ export type JsonObject = {
12
+ [key: string]: JsonValue;
13
+ };
14
+ declare const idBrand: unique symbol;
15
+ /** Erased nominal number identifying one durable record kind. */
16
+ export type Id<Kind extends string, Type = unknown> = number & {
17
+ readonly [idBrand]: {
18
+ readonly kind: Kind;
19
+ readonly type: Type;
20
+ };
21
+ };
22
+ export type ConversationId = Id<"conversation">;
23
+ export type EntryId = Id<"entry">;
24
+ export type TaskId<Result = unknown> = Id<"task", Result>;
25
+ export type SubmissionId = Id<"submission">;
26
+ export type DocumentId = Id<"document">;
27
+ declare const seqBrand: unique symbol;
28
+ /** Strictly increasing sequence assigned to one atomic storage commit; gaps are permitted. */
29
+ export type Seq = number & {
30
+ readonly [seqBrand]: "sequence";
31
+ };
32
+ /** The root conversation always uses this reserved ID. */
33
+ export declare const ROOT_CONVERSATION_ID: ConversationId;
34
+ /** Conversation document that retains only its current state. */
35
+ export type LatestConversationSemantics = {
36
+ readonly scope: "conversation";
37
+ readonly history: "latest";
38
+ readonly fork: "current" | "initial";
39
+ };
40
+ /** Conversation document whose history remains addressable for as-of reads. */
41
+ export type RewindableConversationSemantics = {
42
+ readonly scope: "conversation";
43
+ readonly history: "rewindable";
44
+ readonly fork: "asOf" | "current" | "initial";
45
+ };
46
+ /** Ownership and lifetime of a document; only conversation documents declare history and fork behavior. */
47
+ export type DocumentSemantics = {
48
+ readonly scope: "session";
49
+ readonly history?: never;
50
+ readonly fork?: never;
51
+ } | LatestConversationSemantics | RewindableConversationSemantics | {
52
+ readonly scope: "task";
53
+ readonly history?: never;
54
+ readonly fork?: never;
55
+ };
56
+ /** Stored replay state supplied to a document's checkpoint predicate. */
57
+ export type CheckpointInfo = {
58
+ /** Deltas already stored after the newest base, excluding the change being evaluated. */
59
+ readonly deltasSinceBase: number;
60
+ };
61
+ /** Definition fields shared by singleton documents and document families. */
62
+ export type CommonDocDefinition<T extends JsonObject> = {
63
+ /** Stable persisted kind; part of the public protocol. */
64
+ readonly kind: string;
65
+ /** Positive integer version of the stored value shape. */
66
+ readonly version: number;
67
+ initial(): T;
68
+ migrate?(value: JsonObject, fromVersion: number): T;
69
+ /** Required policy: return true to store this ordinary change as a complete base instead of a delta. */
70
+ checkpointWhen(value: Readonly<T>, ops: readonly Op[], info: CheckpointInfo): boolean;
71
+ };
72
+ /** Singleton document definition. */
73
+ export type DocDefinition<T extends JsonObject> = CommonDocDefinition<T> & DocumentSemantics;
74
+ /** Keyed document family definition; `initial(seed)` runs only when a member is absent. */
75
+ export type DocFamilyDefinition<T extends JsonObject, I extends JsonValue> = Omit<CommonDocDefinition<T>, "initial"> & DocumentSemantics & {
76
+ readonly family: true;
77
+ initial(seed: I): T;
78
+ };
79
+ declare const docType: unique symbol;
80
+ /** Typed singleton document token passed explicitly to typed access. */
81
+ export interface DocToken<T extends JsonObject, D extends DocDefinition<T>> {
82
+ readonly definition: D;
83
+ readonly [docType]?: T;
84
+ }
85
+ /** Typed document family token passed explicitly to typed access. */
86
+ export interface DocFamilyToken<T extends JsonObject, I extends JsonValue, D extends DocFamilyDefinition<T, I>> {
87
+ readonly definition: D;
88
+ readonly [docType]?: T;
89
+ }
90
+ export type SessionDocToken<T extends JsonObject> = DocToken<T, CommonDocDefinition<T> & {
91
+ readonly scope: "session";
92
+ }>;
93
+ export type ConversationDocToken<T extends JsonObject> = DocToken<T, CommonDocDefinition<T> & (LatestConversationSemantics | RewindableConversationSemantics)>;
94
+ export type RewindableConversationDocToken<T extends JsonObject> = DocToken<T, CommonDocDefinition<T> & RewindableConversationSemantics>;
95
+ export type TaskDocToken<T extends JsonObject> = DocToken<T, CommonDocDefinition<T> & {
96
+ readonly scope: "task";
97
+ }>;
98
+ export type SessionDocFamilyToken<T extends JsonObject, I extends JsonValue> = DocFamilyToken<T, I, DocFamilyDefinition<T, I> & {
99
+ readonly scope: "session";
100
+ }>;
101
+ export type ConversationDocFamilyToken<T extends JsonObject, I extends JsonValue> = DocFamilyToken<T, I, DocFamilyDefinition<T, I> & (LatestConversationSemantics | RewindableConversationSemantics)>;
102
+ export type RewindableConversationDocFamilyToken<T extends JsonObject, I extends JsonValue> = DocFamilyToken<T, I, DocFamilyDefinition<T, I> & RewindableConversationSemantics>;
103
+ export type TaskDocFamilyToken<T extends JsonObject, I extends JsonValue> = DocFamilyToken<T, I, DocFamilyDefinition<T, I> & {
104
+ readonly scope: "task";
105
+ }>;
106
+ /** The task a report is about. */
107
+ export type ReportTask = {
108
+ readonly id: TaskId;
109
+ /** The task's registered name. */
110
+ readonly kind: string;
111
+ readonly conversationId: ConversationId;
112
+ };
113
+ /**
114
+ * A host callback, by its `HarnessOptions` name; `env` is the environment builder, `models` a provider call, and
115
+ * `telemetry` a telemetry adapter call made while a commit is published, which must not fail it.
116
+ */
117
+ export type HostCallback = "now" | "settings" | "registry" | "env" | "models" | "setTimer" | "onWake" | "telemetry";
118
+ /**
119
+ * What a report to `HarnessOptions.onReport` is about, by `kind` (`Report` adds the error). `extension`, where present,
120
+ * names the extension whose code failed, when known; for a task's own code, the one that installed the definition the
121
+ * task ran.
122
+ * - `storage_failed`: the error failed the Session, which closes: a Storage error, often a platform reset.
123
+ * - `task_faulted`: the task ended `faulted`: its phase threw (`reason: "threw"`), or its abort handler was cut off
124
+ * `settings.restartLimit` times (`reason: "restart_limit"`), as was a phase of a task of an extension's kind.
125
+ * - `task_interrupted`: crashes ended the task: it ended itself after being cut off `settings.restartLimit` times in a
126
+ * row (`reason: "restart_limit"`; a tool call, a compaction, a generation), or a generation's run ended because the
127
+ * host kept stopping while the model answered (`reason: "interrupted"`). `restarts` is the task's count when it ended.
128
+ * - `migration_failed`: the task's stored state could not be migrated; it is blocked until the registry changes.
129
+ * - `task_outdated`: a running task keeps running under its old definition, which was uninstalled (`missing_task`)
130
+ * or replaced by one that cannot take over its state: older than the task (`task_too_old`), or newer without a
131
+ * `migrate` (`migration_failed`).
132
+ * - `task_error`: the task's own `runtime.report()`, or a failure in running it.
133
+ * - `extension_error`: a hook, a section, a wrapper, or a tool's invalid structured output; with the task that ran it,
134
+ * or, outside a task, the conversation whose agent was resolved.
135
+ * - `host_error`: a host callback threw; with the task that called it, if one did.
136
+ * - `listener_error`: a commit, close, watch, view, or document listener threw.
137
+ */
138
+ export type ReportInfo = {
139
+ readonly kind: "storage_failed";
140
+ } | {
141
+ readonly kind: "task_faulted";
142
+ readonly task: ReportTask;
143
+ readonly extension?: string;
144
+ readonly reason: "threw" | "restart_limit";
145
+ } | {
146
+ readonly kind: "task_interrupted";
147
+ readonly task: ReportTask;
148
+ readonly extension?: string;
149
+ readonly reason: "restart_limit" | "interrupted";
150
+ readonly restarts: number;
151
+ } | {
152
+ readonly kind: "migration_failed";
153
+ readonly task: ReportTask;
154
+ readonly extension?: string;
155
+ } | {
156
+ readonly kind: "task_outdated";
157
+ readonly task: ReportTask;
158
+ readonly extension?: string;
159
+ readonly reason: "missing_task" | "task_too_old" | "migration_failed";
160
+ } | {
161
+ readonly kind: "task_error";
162
+ readonly task: ReportTask;
163
+ readonly extension?: string;
164
+ } | {
165
+ readonly kind: "extension_error";
166
+ readonly extension?: string;
167
+ readonly task?: ReportTask;
168
+ readonly conversationId?: ConversationId;
169
+ } | {
170
+ readonly kind: "host_error";
171
+ readonly callback: HostCallback;
172
+ readonly task?: ReportTask;
173
+ } | {
174
+ readonly kind: "listener_error";
175
+ };
176
+ /** One report to `HarnessOptions.onReport`: what it is about (`ReportInfo`, by `kind`) and the `error` reported. */
177
+ export type Report = ReportInfo & {
178
+ readonly error: unknown;
179
+ };
180
+ /** Whose code a task's `runtime.report()` is about when not the task's own: an extension's, or a host callback. */
181
+ export type ReportFrom = {
182
+ readonly kind: "extension";
183
+ readonly extension?: string;
184
+ } | {
185
+ readonly kind: "host";
186
+ readonly callback: HostCallback;
187
+ };
188
+ /**
189
+ * A host timer, given as `HarnessOptions.setTimer`: call `callback` once, about `delayMs` from now, and return
190
+ * a function that disarms it. `background` timers only free memory, and a host may decline one by returning
191
+ * `undefined`, for example where a pending timer keeps the process from hibernating; the Harness then frees that memory
192
+ * at later commits. Other timers pace or resume work: one the host throws on or declines is armed with `setTimeout`.
193
+ * A timer should not fire early; callers check their clock again when it fires.
194
+ */
195
+ export type SetTimer = (callback: () => void, delayMs: number, options: {
196
+ readonly background: boolean;
197
+ }) => (() => void) | undefined;
198
+ /** Live task record reserved by one invocation. */
199
+ export type RunningTask<I, S, R> = TaskRecord<I, S, R> & {
200
+ readonly state: Extract<TaskState<S, R>, {
201
+ readonly status: "running";
202
+ }>;
203
+ };
204
+ /**
205
+ * Next state a task commits for itself: a replacement checkpoint, a wait on tasks, a sleep, or its outcome. A returned `terminal` state is
206
+ * stored as `completing` while ordinary owned work below the task is live (spec §5.5). A task never writes its own
207
+ * `wake`: a commit of the same checkpoint keeps it, any other state drops it.
208
+ */
209
+ export type NextTaskState<S, R> = {
210
+ readonly status: "running";
211
+ readonly checkpoint: S;
212
+ readonly wake?: never;
213
+ readonly outcome?: never;
214
+ } | Extract<TaskState<S, R>, {
215
+ readonly status: "waiting" | "sleeping" | "terminal";
216
+ }>;
217
+ /**
218
+ * Runs one checkpoint phase. It must commit a changed checkpoint or a terminal outcome through `runtime.commit()`;
219
+ * returning without durable progress faults the task.
220
+ */
221
+ export type PhaseHandler<I, P, S, R, H extends object> = (task: RunningTask<I, P, R>, runtime: TaskRuntime<I, S, R, H>, context: Context) => Promise<void>;
222
+ /** Dispatches one hook of a task to every matching registered handler, in registry order of the phase snapshot. */
223
+ export interface HookRunner<H extends object> {
224
+ /**
225
+ * Call `invoke` with each matching handler named `name`. An ordinary throw from `invoke` is reported and the next
226
+ * handler runs; once the invocation is signalled, the error propagates. Composition happens inside `invoke`.
227
+ */
228
+ each<K extends keyof H>(name: K, invoke: (handler: NonNullable<H[K]>) => void | Promise<void>): Promise<void>;
229
+ }
230
+ /**
231
+ * Operations of one task invocation. Every operation rejects after the invocation ends; watches acquired through it
232
+ * stop at invocation end.
233
+ */
234
+ export interface TaskRuntime<I, S, R, H extends object> extends DocumentObserver, DocumentReader {
235
+ readonly taskId: TaskId<R>;
236
+ readonly conversationId: ConversationId;
237
+ /**
238
+ * Aborted when the run is signalled by `abortTask()`, the Harness closes, or the invocation ends. Work still using it
239
+ * after the invocation ended, such as a detached wait, is cancelled; it could not write anything anyway.
240
+ */
241
+ readonly signal: AbortSignal;
242
+ /** Registry snapshot of the current phase; refreshed at every phase boundary. */
243
+ readonly registry: RegistrySnapshot;
244
+ /** The task's conversation's agent, resolved at most once per phase, at first use, and fixed for the phase. */
245
+ agent(context: Context): Promise<Agent>;
246
+ /** `HarnessOptions.settings`, resolved at each access. */
247
+ readonly settings: Settings;
248
+ /** `HarnessOptions.models`, resolved at each access. */
249
+ readonly models: Models;
250
+ /** Calls `HarnessOptions.env` for the task's conversation; rejects with its error. */
251
+ env(context: Context): Promise<ExecutionEnv | undefined>;
252
+ /** Handlers of this task's name from the extensions its conversation selects, in extension order. */
253
+ readonly hooks: HookRunner<H>;
254
+ /**
255
+ * Commit on the Session line after rereading the task. Rejects when the task is terminal, the invocation ended, the
256
+ * Harness is closing, or, in a run invocation, the task carries an abort mark. A returned state replaces the task's
257
+ * state in the same commit; returning nothing leaves it unchanged. `tx.createTask()` defaults to the task's
258
+ * conversation. Inside the callback read only through `tx`: `snapshot()`, `watchDoc()`, `getTask()`, and the other Session and runtime reads may queue on the line the callback holds, and then never resolve.
259
+ * `options.persistence` may defer storing a commit that changes only documents (`CommitPersistence`); one that returns
260
+ * a state is immediate.
261
+ */
262
+ commit(change: (tx: Tx, current: RunningTask<I, S, R>) => NextTaskState<S, R> | undefined | Promise<NextTaskState<S, R> | undefined>, context: Context, options?: CommitOptions): Promise<void>;
263
+ /** Read a durable memo of this task. */
264
+ memo<T extends JsonValue>(name: string, context: Context): Promise<T | undefined>;
265
+ /** Store `candidate` unless a memo already exists; return the durable winner. */
266
+ memo<T extends JsonValue>(name: string, candidate: T, context: Context): Promise<T>;
267
+ /**
268
+ * A signal key's committed state, the value migrated to the token's version; undefined while nothing came. Throws when
269
+ * the stored value cannot be migrated or the migrated value does not match the schema.
270
+ */
271
+ readSignal<T>(token: SignalToken<T>, key: string, context: Context): Promise<SignalState<T> | undefined>;
272
+ /** Committed task record. */
273
+ getTask<T>(id: TaskId<T>, context: Context): Promise<TaskRecord<JsonValue, JsonValue, T> | undefined>;
274
+ /** Resolve with the task's terminal receipt; rejects when the invocation ends. */
275
+ waitForTask<T>(id: TaskId<T>, context: Context): Promise<SettledTask<T>>;
276
+ /**
277
+ * Abort a task this task owns, as `Harness.abortTask()` does, and resolve once it is terminal. Rejects for a task
278
+ * another owns; resolves at once for a terminal one.
279
+ */
280
+ abortOwned(id: TaskId, context: Context): Promise<void>;
281
+ /**
282
+ * Live tasks this task owns directly, in ID order: also those an earlier invocation created, before a restart. Tasks
283
+ * in conversations it owns are not included. Copies of the committed records.
284
+ */
285
+ ownedTasks(context: Context): Promise<TaskRecord<JsonValue, JsonValue, JsonValue>[]>;
286
+ /** Outcomes of terminal tasks, in order; rejects when one is missing or not terminal. Used after a wait. */
287
+ outcomes<T>(ids: readonly TaskId<T>[], context: Context): Promise<TaskOutcome<T>[]>;
288
+ /**
289
+ * Invocation-bound handle of an existing conversation, for example one this task owns; `undefined` when absent. Its
290
+ * operations and the submissions it returns reject after the invocation ends; admitted work stays durable.
291
+ */
292
+ conversation(id: ConversationId, context: Context): Promise<ConversationHandle | undefined>;
293
+ /** Committed entry visible from the task's conversation. */
294
+ entry(id: EntryId, context: Context): Promise<EntryRecord | undefined>;
295
+ /** Undefined when the entry is absent, not visible, or has another kind. */
296
+ entry<D extends JsonValue>(token: Entry<D>, id: EntryId, context: Context): Promise<TypedEntry<D> | undefined>;
297
+ /**
298
+ * Committed raw history of the task's conversation, fork-aware, as `Conversation.entries()`: every entry, also those a
299
+ * compaction summary or reset left behind. Entries never change, so a range bounded by entry IDs reads the same
300
+ * before and after a compaction, and in a rerun after a crash.
301
+ */
302
+ entries(query: Omit<EntryQuery, "conversationId">, limit: number, cursor: Cursor | undefined, context: Context): Promise<Page<EntryRecord, Cursor>>;
303
+ /** Committed raw active transcript and model context, optionally cut off at the visible entry `at`. */
304
+ context(conversationId: ConversationId, context: Context, options?: {
305
+ readonly at?: EntryId;
306
+ }): Promise<ContextView>;
307
+ /** The Harness clock. */
308
+ now(): number;
309
+ /**
310
+ * Call `callback` once after about `delayMs` on the Harness's timer (`HarnessOptions.setTimer`), for pacing work
311
+ * inside a phase, such as throttled progress commits. Not durable: the invocation's end disarms it, and after its end
312
+ * nothing is armed. Returns a function that disarms it. Wait for time between phases with `sleepUntil()`.
313
+ */
314
+ setTimer(callback: () => void, delayMs: number): () => void;
315
+ /**
316
+ * Forward a non-fatal failure to `HarnessOptions.onReport`, with this task in its info: a `task_error` about the
317
+ * task's own code, or, with `from`, an `extension_error` about extension code it called, such as a section, or a
318
+ * `host_error` about a host callback, such as the environment builder.
319
+ */
320
+ report(error: unknown, from?: ReportFrom): void;
321
+ /**
322
+ * Commit a sleep: the task holds no invocation until the Harness clock reaches `until`, then resumes at `checkpoint`.
323
+ * Resolves once the sleep is stored, not when the time comes; the phase returns right after. Returning
324
+ * `{ status: "sleeping", until, checkpoint }` from `commit()` is the same sleep, for one that must commit together
325
+ * with other writes.
326
+ */
327
+ sleepUntil(until: number, checkpoint: S, context: Context): Promise<void>;
328
+ }
329
+ /** Executable durable state machine definition, registered in the registry by `name`. */
330
+ export type TaskDefinition<I, S extends {
331
+ phase: string;
332
+ }, R, H extends object> = {
333
+ /** Registered task kind persisted in `TaskRecord.kind`. */
334
+ readonly name: string;
335
+ /** Definition version persisted with live input and checkpoints. */
336
+ readonly version: number;
337
+ /** First durable checkpoint for a newly created task. */
338
+ initial(input: I): S;
339
+ /** Exhaustive phase map; each handler receives the task narrowed to its phase. */
340
+ readonly phases: {
341
+ readonly [P in S["phase"]]: PhaseHandler<I, Extract<S, {
342
+ phase: P;
343
+ }>, S, R, H>;
344
+ };
345
+ /** Runs in a fresh invocation after an abort mark and must commit a terminal outcome. */
346
+ abort(task: RunningTask<I, S, R>, runtime: TaskRuntime<I, S, R, H>, context: Context): Promise<void>;
347
+ /** Convert a record stored by any older supported version; runs at reservation. */
348
+ migrate?(input: JsonValue, checkpoint: JsonValue, fromVersion: number): {
349
+ input: I;
350
+ checkpoint: S;
351
+ };
352
+ readonly hooks?: H;
353
+ };
354
+ /** Typed executable task definition. */
355
+ export interface Task<I, S extends {
356
+ phase: string;
357
+ }, R, H extends object> {
358
+ readonly definition: TaskDefinition<I, S, R, H>;
359
+ }
360
+ /** Who owns a task: its conversation (a top-level task) or another task of the same conversation (a child task). */
361
+ export type TaskOwnership = {
362
+ readonly kind: "conversation";
363
+ } | {
364
+ readonly kind: "task";
365
+ readonly taskId: TaskId;
366
+ };
367
+ /**
368
+ * How a waiting task treats the tasks it waits on (spec §5.5): `allSettled` resumes once every task in `on` is terminal,
369
+ * `failFast` too but aborts the others once one fails, `any` once at least one is terminal.
370
+ */
371
+ export type JoinPolicy = "failFast" | "allSettled" | "any";
372
+ /**
373
+ * Why parked work runs again, as the reservation that woke it recorded: `time` (the sleep's `until`, no signals listed),
374
+ * `expired` (`until` came before any listed signal), `signal` (the key whose value woke it; read the value with
375
+ * `readSignal()`), `tasks` (the terminal tasks of the wait's `on` at the reservation). Kept until the task commits another
376
+ * checkpoint, parks, or ends.
377
+ */
378
+ export type WakeReason = {
379
+ readonly reason: "time";
380
+ } | {
381
+ readonly reason: "expired";
382
+ } | {
383
+ readonly reason: "signal";
384
+ readonly signal: string;
385
+ readonly key: string;
386
+ } | {
387
+ readonly reason: "tasks";
388
+ readonly ended: readonly TaskId[];
389
+ };
390
+ /** Creation options for a durable task. */
391
+ export type TaskOptions = {
392
+ /** Omitted: inherit the creator's carrier. "root": start at the adapter root without a stored carrier. */
393
+ readonly trace?: TelemetryCarrier | "root";
394
+ /** Immutable creation-time links, attached to the spans of this task's first invocation. */
395
+ readonly links?: readonly TelemetryCarrier[];
396
+ /** Required: a task always names its owner (spec §5.5). */
397
+ readonly ownership: TaskOwnership;
398
+ /**
399
+ * Default: the owner task's conversation, or the transaction's bound conversation; required for conversation-owned
400
+ * tasks created by Session commits that are not bound to a conversation.
401
+ */
402
+ readonly conversationId?: ConversationId;
403
+ /** Conversation-owned tasks only: excluded from ordinary idle waits, conversation aborts, and cascades. */
404
+ readonly background?: boolean;
405
+ /**
406
+ * Its creator awaits it only in memory and does not resume after a restart, so it is pointless once the Harness that
407
+ * created it is gone. When scheduling starts in a later Harness, the task, if still live, is abort-marked with reason
408
+ * `restart`, and the mark cascades to its ordinary owned work, before any of it runs again.
409
+ */
410
+ readonly abandonOnRestart?: boolean;
411
+ };
412
+ /** Ownership selected explicitly whenever a conversation is created. */
413
+ export type ConversationOwnership = {
414
+ readonly kind: "ownerless";
415
+ } | {
416
+ readonly kind: "task";
417
+ readonly taskId: TaskId;
418
+ };
419
+ /** Immutable identity, history ancestry, and task ownership of a transcript scope. */
420
+ export type ConversationRecord = {
421
+ readonly id: ConversationId;
422
+ /** Fork source and inclusive parent entry through which history is inherited. */
423
+ readonly parent?: {
424
+ readonly conversationId: ConversationId;
425
+ readonly at: EntryId;
426
+ };
427
+ /** Creator edge used for attribution, subtree abort, and subtree idle waits. */
428
+ readonly owner?: {
429
+ readonly conversationId: ConversationId;
430
+ readonly taskId: TaskId;
431
+ };
432
+ };
433
+ /** An immutable override of one visible entry's contribution to model context. */
434
+ export type ContextEdit = {
435
+ /** Entry whose model messages are omitted or replaced. */
436
+ readonly target: EntryId;
437
+ } & ({
438
+ readonly action: "omit";
439
+ readonly messages?: never;
440
+ } | {
441
+ readonly action: "replace";
442
+ /** Messages contributed instead of the target entry's model messages. */
443
+ readonly messages: readonly Message[];
444
+ });
445
+ /** Immutable transcript event with separate model-facing and application-facing payloads. */
446
+ export type EntryRecord = {
447
+ readonly id: EntryId;
448
+ readonly conversationId: ConversationId;
449
+ /** Application-defined entry discriminator. */
450
+ readonly kind: string;
451
+ /** Messages contributed to model context; absent for display or bookkeeping entries. */
452
+ readonly model?: readonly Message[];
453
+ /** JSON payload consumed by views, extensions, or bookkeeping logic. */
454
+ readonly data?: JsonValue;
455
+ /** First entry in the active context selected by this entry. */
456
+ readonly head?: EntryId;
457
+ /** Context-only overrides of earlier visible entries. */
458
+ readonly edits?: readonly ContextEdit[];
459
+ /** Task that appended this entry, when it was produced by durable work. */
460
+ readonly byTaskId?: TaskId;
461
+ };
462
+ /** Entry content supplied before the Session assigns identity and task attribution. */
463
+ export type EntryDraft = Omit<EntryRecord, "id" | "conversationId" | "byTaskId" | "head"> & {
464
+ /** `"self"` starts active context at the newly assigned entry ID. */
465
+ readonly head?: EntryId | "self";
466
+ };
467
+ /** Entry whose `data` has type `D`; `never` means the kind carries no data. */
468
+ export type TypedEntry<D extends JsonValue> = Omit<EntryRecord, "data"> & ([D] extends [never] ? {
469
+ readonly data?: never;
470
+ } : {
471
+ readonly data: D;
472
+ });
473
+ /** Entry content of a typed kind; the token supplies `kind`. */
474
+ export type TypedEntryDraft<D extends JsonValue> = Omit<EntryDraft, "kind" | "data"> & ([D] extends [never] ? {
475
+ readonly data?: never;
476
+ } : {
477
+ readonly data: D;
478
+ });
479
+ /** Typed entry kind with a narrowing guard. */
480
+ export interface Entry<D extends JsonValue = never> {
481
+ readonly kind: string;
482
+ is(entry: EntryRecord | undefined): entry is TypedEntry<D>;
483
+ }
484
+ /** Identity fields shared by every durable submission state. */
485
+ type SubmissionRecordBase = {
486
+ /** Adapter-owned context of the admission span; absent without propagation. */
487
+ readonly trace?: TelemetryCarrier;
488
+ readonly id: SubmissionId;
489
+ readonly conversationId: ConversationId;
490
+ /** Host-provided deduplication key, scoped to the conversation. */
491
+ readonly requestId?: string;
492
+ /** The task to deliver `SubmissionSettled` to under `key` when the submission settles. */
493
+ readonly notify?: SubmissionNotify;
494
+ };
495
+ /** Where a settling submission delivers `SubmissionSettled`: a task, and the key it sleeps on. */
496
+ export type SubmissionNotify = {
497
+ readonly taskId: TaskId;
498
+ readonly key: string;
499
+ };
500
+ /** Durable lifecycle of one admitted user input, passive entry write, or continue. */
501
+ export type SubmissionRecord = (SubmissionRecordBase & {
502
+ readonly type: "input";
503
+ } & ({
504
+ /** Admitted but not yet represented in the transcript. */
505
+ readonly status: "queued";
506
+ readonly entry?: never;
507
+ readonly answer?: never;
508
+ readonly reason?: never;
509
+ readonly detail?: never;
510
+ } | {
511
+ /** Added to the transcript and owned by an active run. */
512
+ readonly status: "placed";
513
+ readonly entry: EntryId;
514
+ readonly answer?: never;
515
+ readonly reason?: never;
516
+ readonly detail?: never;
517
+ } | {
518
+ /** Successfully answered user input. */
519
+ readonly status: "done";
520
+ readonly entry: EntryId;
521
+ readonly answer: EntryId;
522
+ readonly reason?: never;
523
+ readonly detail?: never;
524
+ } | {
525
+ /** Terminal input that can no longer receive an answer. */
526
+ readonly status: "unanswered";
527
+ readonly entry?: EntryId;
528
+ readonly answer?: never;
529
+ readonly reason: string;
530
+ readonly detail?: JsonValue;
531
+ })) | (SubmissionRecordBase & {
532
+ readonly type: "write";
533
+ } & ({
534
+ /** Admitted but not yet appended to the transcript. */
535
+ readonly status: "queued";
536
+ readonly entry?: never;
537
+ readonly answer?: never;
538
+ readonly reason?: never;
539
+ readonly detail?: never;
540
+ } | {
541
+ /** Successfully appended passive entry. */
542
+ readonly status: "done";
543
+ readonly entry: EntryId;
544
+ readonly answer?: never;
545
+ readonly reason?: never;
546
+ readonly detail?: never;
547
+ } | {
548
+ /** Terminal passive write that could not be placed. */
549
+ readonly status: "unanswered";
550
+ readonly entry?: never;
551
+ readonly answer?: never;
552
+ readonly reason: string;
553
+ readonly detail?: JsonValue;
554
+ })) | (SubmissionRecordBase & {
555
+ /** A run started without a user message; it places no entry and is never queued. */
556
+ readonly type: "continue";
557
+ } & ({
558
+ /** Owned by an active run. */
559
+ readonly status: "placed";
560
+ readonly entry?: never;
561
+ readonly answer?: never;
562
+ readonly reason?: never;
563
+ readonly detail?: never;
564
+ } | {
565
+ /** The run answered. */
566
+ readonly status: "done";
567
+ readonly entry?: never;
568
+ readonly answer: EntryId;
569
+ readonly reason?: never;
570
+ readonly detail?: never;
571
+ } | {
572
+ /** The run ended without an answer. */
573
+ readonly status: "unanswered";
574
+ readonly entry?: never;
575
+ readonly answer?: never;
576
+ readonly reason: string;
577
+ readonly detail?: JsonValue;
578
+ }));
579
+ /** Terminal status staged for a submission; identity, type, and entry come from its current record. */
580
+ export type SubmissionSettlement = {
581
+ readonly status: "done";
582
+ readonly answer: EntryId;
583
+ } | {
584
+ readonly status: "unanswered";
585
+ readonly reason: string;
586
+ readonly detail?: JsonValue;
587
+ };
588
+ /** Submission fields supplied before the Session assigns an ID. */
589
+ export type SubmissionCreate = SubmissionRecord extends infer Record ? Record extends SubmissionRecord ? Omit<Record, "id"> : never : never;
590
+ /** JSON-safe error snapshot persisted instead of a runtime `Error` object. */
591
+ export type TaskOutcomeError = {
592
+ readonly message: string;
593
+ /** Optional structured diagnostic data for inspection or recovery. */
594
+ readonly detail?: JsonValue;
595
+ };
596
+ /** Durable reason and optional result recorded when a task becomes terminal. */
597
+ export type TaskOutcome<R> = {
598
+ readonly status: "completed";
599
+ readonly result: R;
600
+ readonly error?: never;
601
+ readonly reason?: never;
602
+ }
603
+ /** Expected task or domain failure explicitly committed by its implementation. */
604
+ | {
605
+ readonly status: "failed";
606
+ readonly error: TaskOutcomeError;
607
+ readonly result?: R;
608
+ readonly reason?: never;
609
+ }
610
+ /** Explicit cancellation handled by the task's abort protocol. */
611
+ | {
612
+ readonly status: "aborted";
613
+ readonly reason?: string;
614
+ readonly result?: R;
615
+ readonly error?: never;
616
+ }
617
+ /** Task that cannot resume because its definition or migration is unavailable. */
618
+ | {
619
+ readonly status: "orphaned";
620
+ readonly reason: string;
621
+ readonly result?: never;
622
+ readonly error?: never;
623
+ }
624
+ /** Runtime-detected contract failure, such as an uncaught throw or no durable progress. */
625
+ | {
626
+ readonly status: "faulted";
627
+ readonly error: TaskOutcomeError;
628
+ readonly result?: never;
629
+ readonly reason?: never;
630
+ };
631
+ /** Complete durable execution state of a task. */
632
+ export type TaskState<S, R> = {
633
+ /** Eligible for scheduling. */
634
+ readonly status: "pending";
635
+ /** Complete durable state from which execution resumes. */
636
+ readonly checkpoint: S;
637
+ /** Why the task woke from its last wait or sleep, kept while it runs at the checkpoint it woke at. */
638
+ readonly wake?: WakeReason;
639
+ readonly outcome?: never;
640
+ } | {
641
+ /** Reserved by one in-memory task invocation. */
642
+ readonly status: "running";
643
+ /** Complete durable state from which execution resumes. */
644
+ readonly checkpoint: S;
645
+ /** Why the task woke from its last wait or sleep, kept while it runs at the checkpoint it woke at. */
646
+ readonly wake?: WakeReason;
647
+ readonly outcome?: never;
648
+ } | {
649
+ /** Parked without an invocation until every task in `on` is terminal; then resumes at `checkpoint`. */
650
+ readonly status: "waiting";
651
+ readonly checkpoint: S;
652
+ readonly on: readonly TaskId[];
653
+ readonly policy: JoinPolicy;
654
+ readonly outcome?: never;
655
+ } | {
656
+ /**
657
+ * Parked without an invocation until the Harness clock reaches `until`, in milliseconds on the
658
+ * `HarnessOptions.now` clock, or one of `signals` holds a value no wake used, whichever comes first; then resumes
659
+ * at `checkpoint`. Needs `until` or a non-empty `signals`. An abort mark does not wait for it.
660
+ */
661
+ readonly status: "sleeping";
662
+ readonly checkpoint: S;
663
+ readonly until?: number;
664
+ readonly signals?: readonly SignalKey<unknown>[];
665
+ readonly outcome?: never;
666
+ } | {
667
+ /** Outcome decided; becomes terminal once no ordinary owned work below is live. Runs no more code. */
668
+ readonly status: "completing";
669
+ readonly checkpoint?: never;
670
+ readonly outcome: TaskOutcome<R>;
671
+ } | {
672
+ /** Permanently settled durable result receipt. */
673
+ readonly status: "terminal";
674
+ readonly checkpoint?: never;
675
+ readonly outcome: TaskOutcome<R>;
676
+ };
677
+ /** Identity, definition, and scheduling fields shared by every task state. */
678
+ type TaskRecordBase<I, R> = {
679
+ /** Adapter-owned context that created this task; phase invocations extract it as their parent. */
680
+ readonly trace?: TelemetryCarrier;
681
+ /** Immutable creation-time links for the task's first invocation. */
682
+ readonly links?: readonly TelemetryCarrier[];
683
+ readonly id: TaskId<R>;
684
+ readonly conversationId: ConversationId;
685
+ /** Registered task definition name. */
686
+ readonly kind: string;
687
+ /** Definition version used to migrate live input and checkpoints. */
688
+ readonly version: number;
689
+ /** Original task input retained while the task is live or terminal. */
690
+ readonly input: I;
691
+ /** Owning task of a child task; absent for a task its conversation owns. Immutable. */
692
+ readonly owner?: TaskId;
693
+ /** Whether this conversation-owned task is excluded from ordinary idle waits, conversation aborts, and cascades. */
694
+ readonly background: boolean;
695
+ /** Durable abort mark checked before run-mode progress is committed. */
696
+ readonly abortRequested: boolean;
697
+ /**
698
+ * Why the mark was set when not by an abort request: `restart` for a task abandoned after a restart
699
+ * (`TaskOptions.abandonOnRestart`) or below one. Such a task waits for its definition instead of becoming `orphaned`.
700
+ * A later abort request clears it.
701
+ */
702
+ readonly abortReason?: "restart";
703
+ /** Set from `TaskOptions.abandonOnRestart`. Immutable. */
704
+ readonly abandonOnRestart?: true;
705
+ /**
706
+ * Wall-clock milliseconds of the first change to `running`, stamped by the Session. Kept through waits and recovery,
707
+ * so the span to `endedAt` includes them. Absent before the task first runs, and on records written by earlier
708
+ * versions.
709
+ */
710
+ readonly startedAt?: number;
711
+ /** Wall-clock milliseconds of the change to `terminal`, stamped by the Session; absent while live. */
712
+ readonly endedAt?: number;
713
+ /**
714
+ * How many times in a row a crash, eviction, or reset cut the task off while it ran, without the task changing its
715
+ * state since; open counts them, and a close does not. At `HarnessSettings.restartLimit` the task ends (the built-in
716
+ * tasks end themselves, visibly; the scheduler faults any other kind), and a generation reads it to charge an
717
+ * interrupted model request as a failed attempt.
718
+ */
719
+ readonly restarts?: number;
720
+ };
721
+ /** Complete replacement record for one durable task state machine. */
722
+ export type TaskRecord<I, S, R> = TaskRecordBase<I, R> & ({
723
+ readonly state: Extract<TaskState<S, R>, {
724
+ readonly status: "pending" | "running" | "waiting" | "sleeping";
725
+ }>;
726
+ /** Small first-writer-wins values retained while the task can run. */
727
+ readonly memos?: Readonly<Record<string, JsonValue>>;
728
+ } | {
729
+ readonly state: Extract<TaskState<S, R>, {
730
+ readonly status: "completing" | "terminal";
731
+ }>;
732
+ readonly memos?: never;
733
+ });
734
+ /** Persisted lifecycle record for one create-to-retire document incarnation. */
735
+ export type DocumentRecord = {
736
+ /** Unique incarnation ID; never reused when the same logical document is recreated. */
737
+ readonly id: DocumentId;
738
+ /** Stable document definition kind. */
739
+ readonly kind: string;
740
+ /** Family member key; absent for singleton documents. */
741
+ readonly key?: string;
742
+ /** Commit that created the incarnation, stamped by storage. */
743
+ readonly createdAt: Seq;
744
+ /** Commit that retired the incarnation; absent while it is current. */
745
+ readonly retiredAt?: Seq;
746
+ } & ({
747
+ readonly scope: {
748
+ readonly kind: "session";
749
+ };
750
+ readonly history?: never;
751
+ readonly fork?: never;
752
+ } | ({
753
+ readonly scope: {
754
+ readonly kind: "conversation";
755
+ readonly conversationId: ConversationId;
756
+ };
757
+ } & ({
758
+ /** Retain only current state. */
759
+ readonly history: "latest";
760
+ /** Initialize a fork from current source state or the definition's initial value. */
761
+ readonly fork: "current" | "initial";
762
+ } | {
763
+ /** Retain history needed for as-of reads. */
764
+ readonly history: "rewindable";
765
+ /** Initialize a fork at its cutoff, from current state, or from the initial value. */
766
+ readonly fork: "asOf" | "current" | "initial";
767
+ })) | {
768
+ readonly scope: {
769
+ readonly kind: "task";
770
+ readonly taskId: TaskId;
771
+ };
772
+ readonly history?: never;
773
+ readonly fork?: never;
774
+ });
775
+ /** Fields supplied when storage creates and stamps a new `DocumentRecord`. */
776
+ export type DocumentCreate = DocumentRecord extends infer Record ? Record extends DocumentRecord ? Omit<Record, "createdAt" | "retiredAt"> : never : never;
777
+ /** One ordered scan result and its optional continuation state. */
778
+ export type Page<T, C> = {
779
+ readonly items: readonly T[];
780
+ readonly next?: C;
781
+ };
782
+ /**
783
+ * Backend-owned JSON continuation state that callers only round-trip to the same scan. It carries the scan's order: a
784
+ * scan given a cursor continues in that order, and rejects a different `order` in its query.
785
+ */
786
+ export type Cursor = Readonly<Record<string, JsonValue>>;
787
+ /** ID order of a scan: `ascending` is oldest first, `descending` newest first. */
788
+ export type ScanOrder = "ascending" | "descending";
789
+ /** Optional filters for an ordered conversation scan. */
790
+ export type ConversationQuery = {
791
+ readonly ownerConversationId?: ConversationId;
792
+ readonly ownerTaskId?: TaskId;
793
+ /** Default `ascending`; with a cursor, the cursor's order. */
794
+ readonly order?: ScanOrder;
795
+ };
796
+ /** Inclusive ID bounds for a scan of one conversation's fork-aware history. */
797
+ export type EntryQuery = {
798
+ readonly conversationId: ConversationId;
799
+ /** Oldest entry ID that may be returned. */
800
+ readonly minEntryId?: EntryId;
801
+ /** Newest entry ID that may be returned. */
802
+ readonly maxEntryId?: EntryId;
803
+ /** Default `descending`; with a cursor, the cursor's order. */
804
+ readonly order?: ScanOrder;
805
+ };
806
+ /** Optional filters for an ordered scan of durable task records. */
807
+ export type TaskQuery = {
808
+ readonly conversationId?: ConversationId;
809
+ readonly kind?: string;
810
+ readonly status?: TaskState<JsonValue, JsonValue>["status"];
811
+ readonly abortRequested?: boolean;
812
+ readonly background?: boolean;
813
+ /** Default `ascending`; with a cursor, the cursor's order. */
814
+ readonly order?: ScanOrder;
815
+ };
816
+ /** Optional filters for an ordered scan of submission records. */
817
+ export type SubmissionQuery = {
818
+ readonly conversationId?: ConversationId;
819
+ readonly status?: SubmissionRecord["status"];
820
+ /** Default `ascending`; with a cursor, the cursor's order. */
821
+ readonly order?: ScanOrder;
822
+ };
823
+ /** Current state or one historical commit sequence used for document membership and content reads. */
824
+ export type DocumentPoint = Seq | "current";
825
+ /** Exact logical identity of a singleton or one keyed family member. */
826
+ export type DocumentAddress = {
827
+ readonly kind: string;
828
+ readonly scope: DocumentRecord["scope"];
829
+ /** Absent selects the singleton; present selects one family member. */
830
+ readonly key?: string;
831
+ };
832
+ /** Ordered scan of document incarnations alive in one exact scope at one point. */
833
+ export type DocumentQuery = {
834
+ readonly scope: DocumentRecord["scope"];
835
+ readonly at: DocumentPoint;
836
+ readonly kind?: string;
837
+ };
838
+ /** Complete checkpoint or Chord operation batch selected by the owning Session. */
839
+ export type DocumentContent = {
840
+ readonly version: number;
841
+ readonly kind: "base";
842
+ readonly value: JsonObject;
843
+ } | {
844
+ readonly version: number;
845
+ readonly kind: "delta";
846
+ readonly ops: readonly Op[];
847
+ };
848
+ /** Exact persisted source selected for a definition-free document copy. */
849
+ export type DocumentCopySource = {
850
+ readonly id: DocumentId;
851
+ readonly at: DocumentPoint;
852
+ };
853
+ /** Detached materialized value and stored definition version at a selected point. */
854
+ export type StoredDocument = {
855
+ readonly record: DocumentRecord;
856
+ readonly version: number;
857
+ readonly value: JsonObject;
858
+ /** Deltas replayed after the selected base to materialize `value`. */
859
+ readonly deltasSinceBase: number;
860
+ };
861
+ /** One record or document mutation in an atomic storage commit. */
862
+ export type StorageWrite = {
863
+ readonly type: "conversation";
864
+ readonly value: ConversationRecord;
865
+ } | {
866
+ readonly type: "entry";
867
+ readonly value: EntryRecord;
868
+ } | {
869
+ readonly type: "task";
870
+ readonly value: TaskRecord<JsonValue, JsonValue, JsonValue>;
871
+ } | {
872
+ readonly type: "submission";
873
+ readonly value: SubmissionRecord;
874
+ } | {
875
+ readonly type: "document.create";
876
+ readonly record: DocumentCreate;
877
+ readonly content: Extract<DocumentContent, {
878
+ readonly kind: "base";
879
+ }>;
880
+ } | {
881
+ readonly type: "document.copy";
882
+ readonly record: DocumentCreate;
883
+ readonly source: DocumentCopySource;
884
+ } | {
885
+ readonly type: "document.change";
886
+ readonly id: DocumentId;
887
+ readonly content: DocumentContent;
888
+ } | {
889
+ readonly type: "document.retire";
890
+ readonly id: DocumentId;
891
+ };
892
+ /**
893
+ * Committed change of one document incarnation: `type: "document"` with `kind: "change"` (a creation or an update) or
894
+ * `kind: "retire"` (this commit retired the incarnation), or a `document.copy`.
895
+ */
896
+ export type DocumentCommitChange = {
897
+ readonly type: "document";
898
+ readonly kind: "change";
899
+ readonly record: DocumentRecord;
900
+ /** Conversation owning the document; task documents derive it from their task record. Undefined only for Session documents. */
901
+ readonly conversationId: ConversationId | undefined;
902
+ /** Definition version of `value`. */
903
+ readonly version: number;
904
+ /** Exact adopted immutable revision. */
905
+ readonly value: JsonObject;
906
+ /** Exact adopted operations for an ordinary update; empty for creation. */
907
+ readonly ops: readonly Op[];
908
+ } | {
909
+ readonly type: "document";
910
+ readonly kind: "retire";
911
+ readonly record: DocumentRecord;
912
+ /** Conversation owning the document; task documents derive it from their task record. Undefined only for Session documents. */
913
+ readonly conversationId: ConversationId | undefined;
914
+ } | {
915
+ /** Definition-free child initialization; consumers hydrate through watch acquisition. */
916
+ readonly type: "document.copy";
917
+ readonly record: DocumentRecord;
918
+ readonly conversationId: ConversationId;
919
+ readonly source: DocumentCopySource;
920
+ };
921
+ /** Complete table record committed without another publication copy. */
922
+ export type TableCommitChange = Extract<StorageWrite, {
923
+ readonly type: "conversation" | "entry" | "task" | "submission";
924
+ }>;
925
+ export type CommitChange = TableCommitChange | DocumentCommitChange;
926
+ /**
927
+ * Every immutable change one Session storage transaction stored, with its `seq`. Change order is unspecified. A commit
928
+ * held in memory (`CommitPersistence`) is published once it is stored: by the next commit that reads or writes its
929
+ * document, alone (`seq` of its own), or with that commit, whose document change then carries the held operations
930
+ * followed by its own.
931
+ */
932
+ export type CommitPublication = {
933
+ readonly seq: Seq;
934
+ readonly changes: readonly CommitChange[];
935
+ };
936
+ /**
937
+ * Transaction surface of one Session commit callback.
938
+ * Table reads and creation results are trusted immutable values and may be shared with internal commit state; the
939
+ * Session may freeze them (its caches and context ranges keep them), so mutating one can throw.
940
+ */
941
+ export interface Tx {
942
+ conversation(id: ConversationId): Promise<ConversationRecord | undefined>;
943
+ entry(id: EntryId): Promise<EntryRecord | undefined>;
944
+ /** Undefined when the entry is absent or has another kind. */
945
+ entry<D extends JsonValue>(token: Entry<D>, id: EntryId): Promise<TypedEntry<D> | undefined>;
946
+ task(id: TaskId): Promise<TaskRecord<JsonValue, JsonValue, JsonValue> | undefined>;
947
+ scanConversations(query: ConversationQuery, limit: number, cursor?: Cursor): Promise<Page<ConversationRecord, Cursor>>;
948
+ scanEntries(query: EntryQuery, limit: number, cursor?: Cursor): Promise<Page<EntryRecord, Cursor>>;
949
+ /** Newest visible entry of the conversation that carries a `head`. */
950
+ latestHeadMarker(conversationId: ConversationId): Promise<(EntryRecord & {
951
+ readonly head: EntryId;
952
+ }) | undefined>;
953
+ /** ID of the newest entry visible from the conversation; `undefined` when it has none. */
954
+ latestEntryId(conversationId: ConversationId): Promise<EntryId | undefined>;
955
+ scanTasks(query: TaskQuery, limit: number, cursor?: Cursor): Promise<Page<TaskRecord<JsonValue, JsonValue, JsonValue>, Cursor>>;
956
+ /** Committed submission record, such as one of `diffra.live.run.inputs`; a placed input's `entry` is its transcript entry. */
957
+ submission(id: SubmissionId): Promise<SubmissionRecord | undefined>;
958
+ /** Committed submission with a conversation-scoped request ID. */
959
+ submissionByRequest(conversationId: ConversationId, requestId: string): Promise<SubmissionRecord | undefined>;
960
+ /** Create a conversation with explicitly selected ownership. */
961
+ createConversation(options: {
962
+ readonly ownership: ConversationOwnership;
963
+ }): Promise<ConversationRecord>;
964
+ /** Create a history fork at one concrete visible entry with explicitly selected ownership. */
965
+ forkConversation(parentConversationId: ConversationId, at: EntryId, options: {
966
+ readonly ownership: ConversationOwnership;
967
+ }): Promise<ConversationRecord>;
968
+ /** Returned records are Session-owned immutable values and may be shared with commit listeners. */
969
+ appendEntry(conversationId: ConversationId, value: EntryDraft): Promise<EntryRecord>;
970
+ /** The token supplies `kind` and types `data`. */
971
+ appendEntry<D extends JsonValue>(token: Entry<D>, conversationId: ConversationId, value: TypedEntryDraft<NoInfer<D>>): Promise<TypedEntry<D>>;
972
+ createTask<I, S extends {
973
+ phase: string;
974
+ }, R, H extends object>(task: Task<I, S, R, H>, input: I, options: TaskOptions): Promise<TaskId<R>>;
975
+ /**
976
+ * Create a raw submission record with a fresh ID. No admission rules apply: no busy check, no inbox queueing, no
977
+ * placement. Use `Conversation.submit()` or a conversation handle unless the caller implements admission itself.
978
+ */
979
+ createSubmission(create: SubmissionCreate): Promise<SubmissionRecord>;
980
+ /**
981
+ * Settle a queued or placed submission; only a placed input or continue can be answered, and a settled submission stays
982
+ * unchanged. Resolved against this transaction's latest record of the submission, so it works after table writes.
983
+ * Run tasks settle the inputs they answer.
984
+ */
985
+ settleSubmission(id: SubmissionId, settlement: SubmissionSettlement): void;
986
+ /**
987
+ * Place a queued submission at `entry`: an input becomes `placed`, a write `done`. Resolved like
988
+ * `settleSubmission()`. Inbox boundaries place the submissions they select.
989
+ */
990
+ placeSubmission(id: SubmissionId, entry: EntryId): void;
991
+ /**
992
+ * Deliver `value` to task `taskId` under `key`: stored in the task until a sleep on the key wakes on it, also when the
993
+ * task does not sleep on it yet. Reads the task, so it throws `ReadAfterWrite` after the commit's first table write;
994
+ * the result is provisional until the commit is stored. Checks nothing about the caller.
995
+ */
996
+ signal<T>(taskId: TaskId, token: SignalToken<T>, key: string, value: T): Promise<SignalResult>;
997
+ doc<T extends JsonObject>(token: SessionDocToken<T>): Promise<Draft<T>>;
998
+ doc<T extends JsonObject>(token: ConversationDocToken<T>, conversationId: ConversationId): Promise<Draft<T>>;
999
+ doc<T extends JsonObject>(token: TaskDocToken<T>, taskId: TaskId): Promise<Draft<T>>;
1000
+ doc<T extends JsonObject, I extends JsonValue>(token: SessionDocFamilyToken<T, I>, key: string, seed: I): Promise<Draft<T>>;
1001
+ doc<T extends JsonObject, I extends JsonValue>(token: ConversationDocFamilyToken<T, I>, conversationId: ConversationId, key: string, seed: I): Promise<Draft<T>>;
1002
+ doc<T extends JsonObject, I extends JsonValue>(token: TaskDocFamilyToken<T, I>, taskId: TaskId, key: string, seed: I): Promise<Draft<T>>;
1003
+ retireDoc<T extends JsonObject>(token: SessionDocToken<T>): Promise<void>;
1004
+ retireDoc<T extends JsonObject>(token: ConversationDocToken<T>, conversationId: ConversationId): Promise<void>;
1005
+ retireDoc<T extends JsonObject>(token: TaskDocToken<T>, taskId: TaskId): Promise<void>;
1006
+ retireDoc<T extends JsonObject, I extends JsonValue>(token: SessionDocFamilyToken<T, I>, key: string): Promise<void>;
1007
+ retireDoc<T extends JsonObject, I extends JsonValue>(token: ConversationDocFamilyToken<T, I>, conversationId: ConversationId, key: string): Promise<void>;
1008
+ retireDoc<T extends JsonObject, I extends JsonValue>(token: TaskDocFamilyToken<T, I>, taskId: TaskId, key: string): Promise<void>;
1009
+ }
1010
+ /**
1011
+ * Watch of one document address (kind, scope, and key), from the incarnation found at acquisition on. A retirement is a
1012
+ * call with `retired` set, the last value the watch was given and no ops (a change made in the retiring commit itself
1013
+ * is not shown: that commit publishes the retirement only); the watch goes on, and a document created at the address
1014
+ * later arrives as `[["r", value]]`, folded with what is pending. It ends `ended` when its scope's owner ends: a task
1015
+ * document's watch once the retirement call of the commit that makes its task terminal returned, every watch at Session
1016
+ * close (a pending retirement dropped); `failed` with the error that failed the Session.
1017
+ */
1018
+ export type DocumentWatch<T extends JsonObject> = WatchHandle<Readonly<T>>;
1019
+ /**
1020
+ * A synchronous observer of committed Durable state, for adapters such as a Chord source (`observeDoc()`,
1021
+ * `Harness.observeConversation()`, `Harness.observeTaskGraph()`), with the shapes of Chord's watch: `advance` gets a
1022
+ * `WatchCall`, `end` a `WatchEnd`. `advance` gets every change, unfolded, as memory adopts it: for a stored commit at its
1023
+ * publication once Storage stored it, for a deferred one before it is stored. A view or the graph gets at most one call
1024
+ * per commit, a document observer one per change at its address (two for a commit that retires and recreates it); the
1025
+ * task graph also advances outside any commit's publication, on the Session line, when a registry change or a failed
1026
+ * migration changes a
1027
+ * `blocked` reason. Durable's failures are final, so every call is a `value` call; a failure ends the observer. Calls
1028
+ * may come before the attaching promise resolves; their ops apply from `Observation.value`. `advance` must only record
1029
+ * the call, synchronously and quickly, never await, call the Session, or throw. A throw is reported as
1030
+ * `listener_error`, detaches the observer, and ends it `listener_error` with the error. `end` is called once: `ended`
1031
+ * when the observed scope's owner ends (a task document's task, after its retirement change, inside that commit's
1032
+ * `adopt`), `failed` with "session closed" when the Session closes (an adapter follows the next Session) or with the
1033
+ * error that failed the Session; never after `detach()`.
1034
+ */
1035
+ export type Observer<T> = {
1036
+ advance(call: WatchCall<T>, context: Context): void;
1037
+ end(end: WatchEnd): void;
1038
+ };
1039
+ /**
1040
+ * A synchronous observer of stored commits (`Session.observeCommits()`): `advance` gets each `CommitPublication`, `end`
1041
+ * the Session's close (`failed` with "session closed", or with the error that failed the Session).
1042
+ */
1043
+ export type CommitObserver = {
1044
+ advance(publication: CommitPublication, context: Context): void;
1045
+ end(end: WatchEnd): void;
1046
+ };
1047
+ /** An attached `CommitObserver`: how to detach it. Idempotent: no `advance` or `end` follows it. */
1048
+ export type CommitObservation = {
1049
+ detach(): void;
1050
+ };
1051
+ /** An attached `Observer`: the value at attachment, which every later `advance` starts from, and how to detach it. */
1052
+ export type Observation<T> = {
1053
+ readonly value: T;
1054
+ /** Idempotent: no `advance` or `end` follows it, not even for the rest of a commit underway. */
1055
+ detach(): void;
1056
+ };
1057
+ /** Committed document reads. */
1058
+ export type DocumentReader = Pick<Session, "snapshot" | "snapshotAsOf">;
1059
+ /** Non-creating document watch acquisition shared by Session and later invocation APIs. */
1060
+ export interface DocumentObserver {
1061
+ watchDoc<T extends JsonObject>(token: SessionDocToken<T>, context: Context): Promise<DocumentWatch<T> | undefined>;
1062
+ watchDoc<T extends JsonObject>(token: ConversationDocToken<T>, conversationId: ConversationId, context: Context): Promise<DocumentWatch<T> | undefined>;
1063
+ watchDoc<T extends JsonObject>(token: TaskDocToken<T>, taskId: TaskId, context: Context): Promise<DocumentWatch<T> | undefined>;
1064
+ watchDoc<T extends JsonObject, I extends JsonValue>(token: SessionDocFamilyToken<T, I>, key: string, context: Context): Promise<DocumentWatch<T> | undefined>;
1065
+ watchDoc<T extends JsonObject, I extends JsonValue>(token: ConversationDocFamilyToken<T, I>, conversationId: ConversationId, key: string, context: Context): Promise<DocumentWatch<T> | undefined>;
1066
+ watchDoc<T extends JsonObject, I extends JsonValue>(token: TaskDocFamilyToken<T, I>, taskId: TaskId, key: string, context: Context): Promise<DocumentWatch<T> | undefined>;
1067
+ }
1068
+ /** Why a Session ended, by `kind`: `close()`, or a failed Storage call, whose error it carries. */
1069
+ export type SessionEnd = {
1070
+ readonly kind: "closed";
1071
+ } | {
1072
+ readonly kind: "failed";
1073
+ readonly error: unknown;
1074
+ };
1075
+ /**
1076
+ * When a commit reaches Storage. `immediate`, the default: before `commit()` resolves. `deferred`: when every write of
1077
+ * the commit changes an existing conversation document with `history: "latest"` and `fork: "initial"` (no creation,
1078
+ * migration, or retirement), the Session adopts it in memory, shows it to watches and views, and resolves, but stores it
1079
+ * later: with the next commit that reads (`tx.doc()`) or writes that document, before the document leaves memory or
1080
+ * retires, at `close()`, and at the latest about `withinMs` after the commit (default `settings.progress.persistWithinMs`,
1081
+ * 1,000 ms; `Infinity` waits for one of the others). A commit that writes anything else is immediate. A crash loses what
1082
+ * is held, so only state whose loss is acceptable, such as streamed progress, should be deferred, and nothing durable may
1083
+ * depend on it without reading it in a commit.
1084
+ */
1085
+ export type CommitPersistence = {
1086
+ readonly kind: "immediate";
1087
+ } | {
1088
+ readonly kind: "deferred";
1089
+ readonly withinMs?: number;
1090
+ };
1091
+ /** Options of one Session commit. */
1092
+ export type CommitOptions = {
1093
+ readonly persistence?: CommitPersistence;
1094
+ };
1095
+ /** Owner of one mutation line, its records, and its tracked documents. */
1096
+ export interface Session extends DocumentObserver {
1097
+ /**
1098
+ * Run one atomic transaction on the Session mutation line. Inside the callback read only through `tx`: `snapshot()`, `watchDoc()`, `getTask()`, and the other Session and runtime reads may queue on the line the callback holds, and then never resolve.
1099
+ * `options.persistence` may defer storing it (`CommitPersistence`). The callback may run while an earlier commit is
1100
+ * being stored: at its first read or write of what an unpublished commit wrote, it waits until that commit is stored
1101
+ * and published; code before that runs early, so act on what it computes only once the commit resolves. Resolves
1102
+ * once the commit is stored and published, after every earlier commit's outcome.
1103
+ */
1104
+ commit<T>(change: (tx: Tx) => T | Promise<T>, context: Context, options?: CommitOptions): Promise<T>;
1105
+ /** Seal admission, settle admitted commits, then close storage. */
1106
+ close(context: Context): Promise<void>;
1107
+ /**
1108
+ * Settles once the Session has closed, Storage included: after `close()`, or after the first failed Storage call,
1109
+ * which closes the Session itself. A failed Session is reopened from Storage; nothing else is left to clean up.
1110
+ */
1111
+ readonly closed: Promise<SessionEnd>;
1112
+ /**
1113
+ * Attach a synchronous `Observer` to one document address, atomically on the Session line, as `watchDoc()` acquires
1114
+ * a watch: the observation's value includes every commit before it, and `advance` gets every later one, retirements
1115
+ * and later incarnations included. `undefined` when the address is absent. `context` governs only the attachment:
1116
+ * cancelled while attaching, the promise rejects and nothing stays attached.
1117
+ */
1118
+ observeDoc<T extends JsonObject>(token: SessionDocToken<T>, observer: Observer<Readonly<T>>, context: Context): Promise<Observation<Readonly<T>> | undefined>;
1119
+ observeDoc<T extends JsonObject>(token: ConversationDocToken<T>, conversationId: ConversationId, observer: Observer<Readonly<T>>, context: Context): Promise<Observation<Readonly<T>> | undefined>;
1120
+ observeDoc<T extends JsonObject>(token: TaskDocToken<T>, taskId: TaskId, observer: Observer<Readonly<T>>, context: Context): Promise<Observation<Readonly<T>> | undefined>;
1121
+ observeDoc<T extends JsonObject, I extends JsonValue>(token: SessionDocFamilyToken<T, I>, key: string, observer: Observer<Readonly<T>>, context: Context): Promise<Observation<Readonly<T>> | undefined>;
1122
+ observeDoc<T extends JsonObject, I extends JsonValue>(token: ConversationDocFamilyToken<T, I>, conversationId: ConversationId, key: string, observer: Observer<Readonly<T>>, context: Context): Promise<Observation<Readonly<T>> | undefined>;
1123
+ observeDoc<T extends JsonObject, I extends JsonValue>(token: TaskDocFamilyToken<T, I>, taskId: TaskId, key: string, observer: Observer<Readonly<T>>, context: Context): Promise<Observation<Readonly<T>> | undefined>;
1124
+ /**
1125
+ * Observe stored commits, each with exactly what it stored, except that a document the commit retires is published
1126
+ * as its retirement only; a commit held in memory is published when it is stored (`CommitPublication`). Commits are
1127
+ * published in line order once Storage stored them, a macrotask apart; a publication queues one dispatch, which runs
1128
+ * a microtask later, before every wait the commit resolves and every abort it queues, and calls the observers
1129
+ * attached by then; a commit published while none was attached has no dispatch. There is no order relative to watch listeners. `advance` runs synchronously and must not block or
1130
+ * call Session APIs; a throw is reported as `listener_error`, detaches the observer, and ends it `listener_error`.
1131
+ * `end` is called once, when the Session closes (`ended`, after its last publication) or fails (`failed`), never
1132
+ * after `detach()`. Attaches synchronously; the observation has no `value`: commits are events, not a state, and the
1133
+ * Session does not know Storage's newest `seq` before its own first commit.
1134
+ */
1135
+ observeCommits(observer: CommitObserver): CommitObservation;
1136
+ /**
1137
+ * Observe close synchronously when it begins, also when a failed Storage call closes the Session. The listener must
1138
+ * not block or call Session APIs.
1139
+ */
1140
+ subscribeClose(listener: () => void): () => void;
1141
+ snapshot<T extends JsonObject>(token: SessionDocToken<T>, context: Context): Promise<Readonly<T> | undefined>;
1142
+ snapshot<T extends JsonObject>(token: ConversationDocToken<T>, conversationId: ConversationId, context: Context): Promise<Readonly<T> | undefined>;
1143
+ snapshot<T extends JsonObject>(token: TaskDocToken<T>, taskId: TaskId, context: Context): Promise<Readonly<T> | undefined>;
1144
+ snapshot<T extends JsonObject, I extends JsonValue>(token: SessionDocFamilyToken<T, I>, key: string, context: Context): Promise<Readonly<T> | undefined>;
1145
+ snapshot<T extends JsonObject, I extends JsonValue>(token: ConversationDocFamilyToken<T, I>, conversationId: ConversationId, key: string, context: Context): Promise<Readonly<T> | undefined>;
1146
+ snapshot<T extends JsonObject, I extends JsonValue>(token: TaskDocFamilyToken<T, I>, taskId: TaskId, key: string, context: Context): Promise<Readonly<T> | undefined>;
1147
+ snapshotAsOf<T extends JsonObject>(token: RewindableConversationDocToken<T>, conversationId: ConversationId, at: EntryId, context: Context): Promise<Readonly<T> | undefined>;
1148
+ snapshotAsOf<T extends JsonObject, I extends JsonValue>(token: RewindableConversationDocFamilyToken<T, I>, conversationId: ConversationId, key: string, at: EntryId, context: Context): Promise<Readonly<T> | undefined>;
1149
+ }
1150
+ /**
1151
+ * What `Storage.commit()` stored: the number of durable batches (`stored`: every one), and, when one was refused
1152
+ * (`refused`), why.
1153
+ */
1154
+ export type CommitResult = {
1155
+ readonly kind: "stored";
1156
+ readonly stored: number;
1157
+ } | {
1158
+ readonly kind: "refused";
1159
+ readonly stored: number;
1160
+ readonly refused: unknown;
1161
+ };
1162
+ /**
1163
+ * Atomic persistence boundary for Session records.
1164
+ *
1165
+ * Storage trusts the owning Session to supply semantically valid records, references,
1166
+ * ancestry, and transitions. Implementations enforce atomicity, global ID ownership,
1167
+ * immutable conversation/entry creation, document record consistency, and detached values;
1168
+ * Session serializes commits.
1169
+ *
1170
+ * An error a method throws is final: it fails the Session, which nothing retries, so retry transient failures inside
1171
+ * the method. Two errors of a read fail only that read: a `StorageRequestError` for an invalid request (an unknown
1172
+ * conversation, a foreign or malformed cursor, history a document does not keep), which must have no durable effect, and a
1173
+ * rejection because its caller's context was aborted. Any error from `commit()` or `mintId()` is fatal.
1174
+ */
1175
+ export interface Storage {
1176
+ /**
1177
+ * Persist `batches` in order, each as one ordinary atomic commit with its own seq, checked against the state the
1178
+ * earlier ones left, in as few round trips as the backend can. Every batch has at least one write: a commit without
1179
+ * writes never reaches Storage, so the Session never sends an empty batch. Stops at the first batch Durable's own checks refuse
1180
+ * (global IDs, document actions, copy resolution): the batches before it are durable, it and the ones after are not.
1181
+ * Calls `receive(index, seq)` once per durable batch, in order, synchronously, after all of them are durable and
1182
+ * before the next commit starts. Resolves with the number of durable batches and the refusal, if any. Rejects for an
1183
+ * unknown outcome (connection lost, statement or commit failed); `receive` calls made before the rejection still
1184
+ * stand. `receive` is a durability receipt, not publication: the Session publishes later. Once it resolved, later
1185
+ * reads through this storage observe the stored batches.
1186
+ */
1187
+ commit(batches: readonly (readonly StorageWrite[])[], receive: (index: number, seq: Seq) => void, context: Context): Promise<CommitResult>;
1188
+ /**
1189
+ * Optional. Run `publish`, the Session's publication of one stored or held commit (its cache write-through,
1190
+ * adoption, and observers), inside this storage's own scope, such as a store's attached host batch, so what the
1191
+ * publication marks lands together. Call `publish` exactly once, now or soon after; the Session publishes the next
1192
+ * commit only once it ran. Without it, the Session publishes directly.
1193
+ */
1194
+ publishing?(publish: () => void, context: Context): void;
1195
+ /**
1196
+ * Return a fresh branded candidate from the Session-global numeric ID namespace, greater than every ID minted since
1197
+ * this storage was opened and every ID stored, also across reopen: scans order by ID as creation order, and the
1198
+ * Harness relies on it.
1199
+ */
1200
+ mintId<I extends Id<string>>(): Promise<I>;
1201
+ /** Look up one conversation by exact ID. */
1202
+ conversation(id: ConversationId, context: Context): Promise<ConversationRecord | undefined>;
1203
+ /** Scan conversations in `query.order` (default ascending) by ID. */
1204
+ scanConversations(query: ConversationQuery, limit: number, cursor: Cursor | undefined, context: Context): Promise<Page<ConversationRecord, Cursor>>;
1205
+ /** Look up one global entry and the sequence of the commit that persisted it. */
1206
+ entry(id: EntryId, context: Context): Promise<{
1207
+ readonly entry: EntryRecord;
1208
+ readonly commitSeq: Seq;
1209
+ } | undefined>;
1210
+ /** Look up one entry only when it is visible through the requested conversation's ancestry. */
1211
+ entry(conversationId: ConversationId, id: EntryId, context: Context): Promise<{
1212
+ readonly entry: EntryRecord;
1213
+ readonly commitSeq: Seq;
1214
+ } | undefined>;
1215
+ /**
1216
+ * Return the newest visible entry with a `head` at or below the optional inclusive cutoff.
1217
+ * The returned entry is the marker; its `head` value is the range's actual lower bound.
1218
+ */
1219
+ findLatestHeadMarker(conversationId: ConversationId, atOrBeforeEntryId: EntryId | undefined, context: Context): Promise<(EntryRecord & {
1220
+ readonly head: EntryId;
1221
+ }) | undefined>;
1222
+ /** Scan the inclusive visible range in `query.order` (default descending), returning at most `limit` entries. */
1223
+ scanEntries(query: EntryQuery, limit: number, cursor: Cursor | undefined, context: Context): Promise<Page<EntryRecord, Cursor>>;
1224
+ /** Look up the latest complete record for one task. */
1225
+ task(id: TaskId, context: Context): Promise<TaskRecord<JsonValue, JsonValue, JsonValue> | undefined>;
1226
+ /** Scan task records matching every supplied filter in `query.order` (default ascending) by ID. */
1227
+ scanTasks(query: TaskQuery, limit: number, cursor: Cursor | undefined, context: Context): Promise<Page<TaskRecord<JsonValue, JsonValue, JsonValue>, Cursor>>;
1228
+ /** Look up the latest complete record for one admitted submission. */
1229
+ submission(id: SubmissionId, context: Context): Promise<SubmissionRecord | undefined>;
1230
+ /** Scan submissions matching every supplied filter in `query.order` (default ascending) by ID. */
1231
+ scanSubmissions(query: SubmissionQuery, limit: number, cursor: Cursor | undefined, context: Context): Promise<Page<SubmissionRecord, Cursor>>;
1232
+ /** Find a submission by its conversation-scoped host deduplication key. */
1233
+ submissionByRequest(conversationId: ConversationId, requestId: string, context: Context): Promise<SubmissionRecord | undefined>;
1234
+ /** Resolve the incarnation occupying one exact logical address at the selected point. */
1235
+ findDocument(address: DocumentAddress, at: DocumentPoint, context: Context): Promise<DocumentRecord | undefined>;
1236
+ /** Materialize one specific incarnation by ID at the selected point without following a replacement at its address. */
1237
+ document(id: DocumentId, at: DocumentPoint, context: Context): Promise<StoredDocument | undefined>;
1238
+ /** Scan incarnations alive in one exact scope at the selected point. */
1239
+ scanDocuments(query: DocumentQuery, limit: number, cursor: Cursor | undefined, context: Context): Promise<Page<DocumentRecord, Cursor>>;
1240
+ /** Release backend resources; all later operations must reject. */
1241
+ close(context: Context): Promise<void>;
1242
+ }
1243
+ export {};
1244
+ //# sourceMappingURL=types.d.ts.map