@animalabs/agent-framework 0.21.1 → 0.22.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (348) hide show
  1. package/README.md +54 -4
  2. package/dist/src/agent.d.ts +17 -4
  3. package/dist/src/agent.d.ts.map +1 -1
  4. package/dist/src/agent.js +105 -43
  5. package/dist/src/agent.js.map +1 -1
  6. package/dist/src/api/server.d.ts.map +1 -1
  7. package/dist/src/api/server.js +41 -4
  8. package/dist/src/api/server.js.map +1 -1
  9. package/dist/src/code-execution/py-runner.d.ts +22 -1
  10. package/dist/src/code-execution/py-runner.d.ts.map +1 -1
  11. package/dist/src/code-execution/py-runner.js +64 -9
  12. package/dist/src/code-execution/py-runner.js.map +1 -1
  13. package/dist/src/code-execution/runtime-py.d.ts +4 -0
  14. package/dist/src/code-execution/runtime-py.d.ts.map +1 -1
  15. package/dist/src/code-execution/runtime-py.js +215 -38
  16. package/dist/src/code-execution/runtime-py.js.map +1 -1
  17. package/dist/src/framework.d.ts +569 -59
  18. package/dist/src/framework.d.ts.map +1 -1
  19. package/dist/src/framework.js +3091 -542
  20. package/dist/src/framework.js.map +1 -1
  21. package/dist/src/gate/event-gate.d.ts +21 -0
  22. package/dist/src/gate/event-gate.d.ts.map +1 -1
  23. package/dist/src/gate/event-gate.js +40 -7
  24. package/dist/src/gate/event-gate.js.map +1 -1
  25. package/dist/src/index.d.ts +7 -2
  26. package/dist/src/index.d.ts.map +1 -1
  27. package/dist/src/index.js +4 -0
  28. package/dist/src/index.js.map +1 -1
  29. package/dist/src/mcpl/channel-registry.d.ts.map +1 -1
  30. package/dist/src/mcpl/channel-registry.js +26 -3
  31. package/dist/src/mcpl/channel-registry.js.map +1 -1
  32. package/dist/src/mcpl/conversation-router.d.ts +28 -1
  33. package/dist/src/mcpl/conversation-router.d.ts.map +1 -1
  34. package/dist/src/mcpl/conversation-router.js +17 -0
  35. package/dist/src/mcpl/conversation-router.js.map +1 -1
  36. package/dist/src/mcpl/feature-set-manager.d.ts +11 -0
  37. package/dist/src/mcpl/feature-set-manager.d.ts.map +1 -1
  38. package/dist/src/mcpl/feature-set-manager.js +31 -7
  39. package/dist/src/mcpl/feature-set-manager.js.map +1 -1
  40. package/dist/src/mcpl/index.d.ts +4 -0
  41. package/dist/src/mcpl/index.d.ts.map +1 -1
  42. package/dist/src/mcpl/index.js +1 -0
  43. package/dist/src/mcpl/index.js.map +1 -1
  44. package/dist/src/mcpl/prose-stream-router.d.ts +27 -6
  45. package/dist/src/mcpl/prose-stream-router.d.ts.map +1 -1
  46. package/dist/src/mcpl/prose-stream-router.js +85 -22
  47. package/dist/src/mcpl/prose-stream-router.js.map +1 -1
  48. package/dist/src/mcpl/push-coalescer.d.ts +32 -4
  49. package/dist/src/mcpl/push-coalescer.d.ts.map +1 -1
  50. package/dist/src/mcpl/push-coalescer.js +46 -13
  51. package/dist/src/mcpl/push-coalescer.js.map +1 -1
  52. package/dist/src/mcpl/push-handler.d.ts +5 -3
  53. package/dist/src/mcpl/push-handler.d.ts.map +1 -1
  54. package/dist/src/mcpl/push-handler.js +41 -11
  55. package/dist/src/mcpl/push-handler.js.map +1 -1
  56. package/dist/src/mcpl/reference-fetcher.d.ts +2 -1
  57. package/dist/src/mcpl/reference-fetcher.d.ts.map +1 -1
  58. package/dist/src/mcpl/reference-fetcher.js +20 -9
  59. package/dist/src/mcpl/reference-fetcher.js.map +1 -1
  60. package/dist/src/mcpl/references.d.ts +29 -6
  61. package/dist/src/mcpl/references.d.ts.map +1 -1
  62. package/dist/src/mcpl/references.js +57 -16
  63. package/dist/src/mcpl/references.js.map +1 -1
  64. package/dist/src/mcpl/server-connection.d.ts +32 -1
  65. package/dist/src/mcpl/server-connection.d.ts.map +1 -1
  66. package/dist/src/mcpl/server-connection.js +76 -11
  67. package/dist/src/mcpl/server-connection.js.map +1 -1
  68. package/dist/src/mcpl/server-registry.d.ts +5 -2
  69. package/dist/src/mcpl/server-registry.d.ts.map +1 -1
  70. package/dist/src/mcpl/server-registry.js +25 -7
  71. package/dist/src/mcpl/server-registry.js.map +1 -1
  72. package/dist/src/mcpl/tool-lifecycle.d.ts +52 -2
  73. package/dist/src/mcpl/tool-lifecycle.d.ts.map +1 -1
  74. package/dist/src/mcpl/tool-lifecycle.js +47 -1
  75. package/dist/src/mcpl/tool-lifecycle.js.map +1 -1
  76. package/dist/src/mcpl/tool-pattern-check.d.ts +51 -0
  77. package/dist/src/mcpl/tool-pattern-check.d.ts.map +1 -0
  78. package/dist/src/mcpl/tool-pattern-check.js +79 -0
  79. package/dist/src/mcpl/tool-pattern-check.js.map +1 -0
  80. package/dist/src/mcpl/types.d.ts +15 -5
  81. package/dist/src/mcpl/types.d.ts.map +1 -1
  82. package/dist/src/mcpl/types.js.map +1 -1
  83. package/dist/src/mcpl/visible-content.d.ts +39 -0
  84. package/dist/src/mcpl/visible-content.d.ts.map +1 -0
  85. package/dist/src/mcpl/visible-content.js +54 -0
  86. package/dist/src/mcpl/visible-content.js.map +1 -0
  87. package/dist/src/module-registry.d.ts +90 -6
  88. package/dist/src/module-registry.d.ts.map +1 -1
  89. package/dist/src/module-registry.js +260 -18
  90. package/dist/src/module-registry.js.map +1 -1
  91. package/dist/src/modules/workspace/branch-intent.d.ts +72 -0
  92. package/dist/src/modules/workspace/branch-intent.d.ts.map +1 -0
  93. package/dist/src/modules/workspace/branch-intent.js +83 -0
  94. package/dist/src/modules/workspace/branch-intent.js.map +1 -0
  95. package/dist/src/modules/workspace/disk-agreement.d.ts +167 -0
  96. package/dist/src/modules/workspace/disk-agreement.d.ts.map +1 -0
  97. package/dist/src/modules/workspace/disk-agreement.js +259 -0
  98. package/dist/src/modules/workspace/disk-agreement.js.map +1 -0
  99. package/dist/src/modules/workspace/effects.d.ts +84 -0
  100. package/dist/src/modules/workspace/effects.d.ts.map +1 -0
  101. package/dist/src/modules/workspace/effects.js +368 -0
  102. package/dist/src/modules/workspace/effects.js.map +1 -0
  103. package/dist/src/modules/workspace/index.d.ts +155 -17
  104. package/dist/src/modules/workspace/index.d.ts.map +1 -1
  105. package/dist/src/modules/workspace/index.js +1101 -424
  106. package/dist/src/modules/workspace/index.js.map +1 -1
  107. package/dist/src/modules/workspace/observe.d.ts +288 -0
  108. package/dist/src/modules/workspace/observe.d.ts.map +1 -0
  109. package/dist/src/modules/workspace/observe.js +825 -0
  110. package/dist/src/modules/workspace/observe.js.map +1 -0
  111. package/dist/src/modules/workspace/reconcile.d.ts +238 -0
  112. package/dist/src/modules/workspace/reconcile.d.ts.map +1 -0
  113. package/dist/src/modules/workspace/reconcile.js +909 -0
  114. package/dist/src/modules/workspace/reconcile.js.map +1 -0
  115. package/dist/src/modules/workspace/sync.d.ts +6 -59
  116. package/dist/src/modules/workspace/sync.d.ts.map +1 -1
  117. package/dist/src/modules/workspace/sync.js +6 -257
  118. package/dist/src/modules/workspace/sync.js.map +1 -1
  119. package/dist/src/modules/workspace/types.d.ts +77 -10
  120. package/dist/src/modules/workspace/types.d.ts.map +1 -1
  121. package/dist/src/modules/workspace/types.js.map +1 -1
  122. package/dist/src/operator-log.d.ts +41 -0
  123. package/dist/src/operator-log.d.ts.map +1 -1
  124. package/dist/src/operator-log.js.map +1 -1
  125. package/dist/src/provider-waits.d.ts +216 -0
  126. package/dist/src/provider-waits.d.ts.map +1 -0
  127. package/dist/src/provider-waits.js +482 -0
  128. package/dist/src/provider-waits.js.map +1 -0
  129. package/dist/src/record-journal.d.ts +127 -0
  130. package/dist/src/record-journal.d.ts.map +1 -0
  131. package/dist/src/record-journal.js +202 -0
  132. package/dist/src/record-journal.js.map +1 -0
  133. package/dist/src/refusal-reactions.d.ts +14 -12
  134. package/dist/src/refusal-reactions.d.ts.map +1 -1
  135. package/dist/src/refusal-reactions.js +14 -12
  136. package/dist/src/refusal-reactions.js.map +1 -1
  137. package/dist/src/shutdown.d.ts +13 -0
  138. package/dist/src/shutdown.d.ts.map +1 -0
  139. package/dist/src/shutdown.js +46 -0
  140. package/dist/src/shutdown.js.map +1 -0
  141. package/dist/src/silent-heartbeat.d.ts +74 -0
  142. package/dist/src/silent-heartbeat.d.ts.map +1 -0
  143. package/dist/src/silent-heartbeat.js +77 -0
  144. package/dist/src/silent-heartbeat.js.map +1 -0
  145. package/dist/src/tool-presentation.d.ts +50 -0
  146. package/dist/src/tool-presentation.d.ts.map +1 -0
  147. package/dist/src/tool-presentation.js +235 -0
  148. package/dist/src/tool-presentation.js.map +1 -0
  149. package/dist/src/tool-result-guard.d.ts +142 -6
  150. package/dist/src/tool-result-guard.d.ts.map +1 -1
  151. package/dist/src/tool-result-guard.js +357 -36
  152. package/dist/src/tool-result-guard.js.map +1 -1
  153. package/dist/src/tool-result-history.d.ts +13 -0
  154. package/dist/src/tool-result-history.d.ts.map +1 -1
  155. package/dist/src/tool-result-history.js +21 -1
  156. package/dist/src/tool-result-history.js.map +1 -1
  157. package/dist/src/types/agent.d.ts +18 -2
  158. package/dist/src/types/agent.d.ts.map +1 -1
  159. package/dist/src/types/events.d.ts +16 -0
  160. package/dist/src/types/events.d.ts.map +1 -1
  161. package/dist/src/types/framework.d.ts +12 -0
  162. package/dist/src/types/framework.d.ts.map +1 -1
  163. package/dist/src/types/module.d.ts +26 -4
  164. package/dist/src/types/module.d.ts.map +1 -1
  165. package/dist/src/types/trace.d.ts +87 -3
  166. package/dist/src/types/trace.d.ts.map +1 -1
  167. package/dist/test/batched-wake-locus.test.d.ts +2 -0
  168. package/dist/test/batched-wake-locus.test.d.ts.map +1 -0
  169. package/dist/test/batched-wake-locus.test.js +166 -0
  170. package/dist/test/batched-wake-locus.test.js.map +1 -0
  171. package/dist/test/code-execution.test.js +386 -2
  172. package/dist/test/code-execution.test.js.map +1 -1
  173. package/dist/test/component-defaults.test.d.ts +2 -0
  174. package/dist/test/component-defaults.test.d.ts.map +1 -0
  175. package/dist/test/component-defaults.test.js +54 -0
  176. package/dist/test/component-defaults.test.js.map +1 -0
  177. package/dist/test/conversation-routing.test.js +57 -1
  178. package/dist/test/conversation-routing.test.js.map +1 -1
  179. package/dist/test/discord-awareness-outbox.test.js +3 -0
  180. package/dist/test/discord-awareness-outbox.test.js.map +1 -1
  181. package/dist/test/ephemeral-settle.test.js +3 -0
  182. package/dist/test/ephemeral-settle.test.js.map +1 -1
  183. package/dist/test/event-gate.test.js +118 -0
  184. package/dist/test/event-gate.test.js.map +1 -1
  185. package/dist/test/explicit-prose-routing.test.js +975 -0
  186. package/dist/test/explicit-prose-routing.test.js.map +1 -1
  187. package/dist/test/framework.test.js +160 -1
  188. package/dist/test/framework.test.js.map +1 -1
  189. package/dist/test/helpers/coalescing-fixture.d.ts.map +1 -1
  190. package/dist/test/helpers/coalescing-fixture.js +3 -2
  191. package/dist/test/helpers/coalescing-fixture.js.map +1 -1
  192. package/dist/test/helpers/pytc-stop-rows.d.ts +11 -0
  193. package/dist/test/helpers/pytc-stop-rows.d.ts.map +1 -0
  194. package/dist/test/helpers/pytc-stop-rows.js +143 -0
  195. package/dist/test/helpers/pytc-stop-rows.js.map +1 -0
  196. package/dist/test/inference-exhaustion-rewind.test.js +142 -14
  197. package/dist/test/inference-exhaustion-rewind.test.js.map +1 -1
  198. package/dist/test/inference-failure-bounds.test.d.ts +2 -0
  199. package/dist/test/inference-failure-bounds.test.d.ts.map +1 -0
  200. package/dist/test/inference-failure-bounds.test.js +1201 -0
  201. package/dist/test/inference-failure-bounds.test.js.map +1 -0
  202. package/dist/test/late-events-after-stop.test.d.ts +2 -0
  203. package/dist/test/late-events-after-stop.test.d.ts.map +1 -0
  204. package/dist/test/late-events-after-stop.test.js +122 -0
  205. package/dist/test/late-events-after-stop.test.js.map +1 -0
  206. package/dist/test/mcpl-awareness-barrier.test.js +141 -0
  207. package/dist/test/mcpl-awareness-barrier.test.js.map +1 -1
  208. package/dist/test/mcpl-coalescing.test.js +97 -22
  209. package/dist/test/mcpl-coalescing.test.js.map +1 -1
  210. package/dist/test/mcpl-config-diagnostics.test.d.ts +2 -0
  211. package/dist/test/mcpl-config-diagnostics.test.d.ts.map +1 -0
  212. package/dist/test/mcpl-config-diagnostics.test.js +452 -0
  213. package/dist/test/mcpl-config-diagnostics.test.js.map +1 -0
  214. package/dist/test/mcpl-empty-content.test.d.ts +2 -0
  215. package/dist/test/mcpl-empty-content.test.d.ts.map +1 -0
  216. package/dist/test/mcpl-empty-content.test.js +368 -0
  217. package/dist/test/mcpl-empty-content.test.js.map +1 -0
  218. package/dist/test/mcpl-manifest-changed.test.js +2 -1
  219. package/dist/test/mcpl-manifest-changed.test.js.map +1 -1
  220. package/dist/test/mcpl-reference-fetcher.test.js +232 -4
  221. package/dist/test/mcpl-reference-fetcher.test.js.map +1 -1
  222. package/dist/test/mcpl-reference-fields.test.js +69 -2
  223. package/dist/test/mcpl-reference-fields.test.js.map +1 -1
  224. package/dist/test/mcpl-tool-lifecycle-emitter.test.js +106 -1
  225. package/dist/test/mcpl-tool-lifecycle-emitter.test.js.map +1 -1
  226. package/dist/test/mcpl-tool-lifecycle-script.test.d.ts +2 -0
  227. package/dist/test/mcpl-tool-lifecycle-script.test.d.ts.map +1 -0
  228. package/dist/test/mcpl-tool-lifecycle-script.test.js +288 -0
  229. package/dist/test/mcpl-tool-lifecycle-script.test.js.map +1 -0
  230. package/dist/test/mcpl-ws-transport.test.js +103 -0
  231. package/dist/test/mcpl-ws-transport.test.js.map +1 -1
  232. package/dist/test/ops-alert.test.js +2 -1
  233. package/dist/test/ops-alert.test.js.map +1 -1
  234. package/dist/test/placed-reaction-baseline.test.d.ts +2 -0
  235. package/dist/test/placed-reaction-baseline.test.d.ts.map +1 -0
  236. package/dist/test/placed-reaction-baseline.test.js +312 -0
  237. package/dist/test/placed-reaction-baseline.test.js.map +1 -0
  238. package/dist/test/prose-stream-router.test.js +41 -0
  239. package/dist/test/prose-stream-router.test.js.map +1 -1
  240. package/dist/test/provider-acceleration-cooldown.test.js +5 -1
  241. package/dist/test/provider-acceleration-cooldown.test.js.map +1 -1
  242. package/dist/test/provider-waits.test.d.ts +2 -0
  243. package/dist/test/provider-waits.test.d.ts.map +1 -0
  244. package/dist/test/provider-waits.test.js +2196 -0
  245. package/dist/test/provider-waits.test.js.map +1 -0
  246. package/dist/test/reaction-baseline-platform.test.d.ts +2 -0
  247. package/dist/test/reaction-baseline-platform.test.d.ts.map +1 -0
  248. package/dist/test/reaction-baseline-platform.test.js +124 -0
  249. package/dist/test/reaction-baseline-platform.test.js.map +1 -0
  250. package/dist/test/record-journal.test.d.ts +6 -0
  251. package/dist/test/record-journal.test.d.ts.map +1 -0
  252. package/dist/test/record-journal.test.js +269 -0
  253. package/dist/test/record-journal.test.js.map +1 -0
  254. package/dist/test/refusal-reaction-baseline.test.js +10 -3
  255. package/dist/test/refusal-reaction-baseline.test.js.map +1 -1
  256. package/dist/test/save-recent-image.test.js +240 -2
  257. package/dist/test/save-recent-image.test.js.map +1 -1
  258. package/dist/test/shutdown-errors.test.d.ts +2 -0
  259. package/dist/test/shutdown-errors.test.d.ts.map +1 -0
  260. package/dist/test/shutdown-errors.test.js +662 -0
  261. package/dist/test/shutdown-errors.test.js.map +1 -0
  262. package/dist/test/silent-heartbeat-rows.test.d.ts +2 -0
  263. package/dist/test/silent-heartbeat-rows.test.d.ts.map +1 -0
  264. package/dist/test/silent-heartbeat-rows.test.js +451 -0
  265. package/dist/test/silent-heartbeat-rows.test.js.map +1 -0
  266. package/dist/test/silent-heartbeat.test.js +49 -38
  267. package/dist/test/silent-heartbeat.test.js.map +1 -1
  268. package/dist/test/stop-with-work-in-flight.test.d.ts +23 -0
  269. package/dist/test/stop-with-work-in-flight.test.d.ts.map +1 -0
  270. package/dist/test/stop-with-work-in-flight.test.js +672 -0
  271. package/dist/test/stop-with-work-in-flight.test.js.map +1 -0
  272. package/dist/test/surgery-marks-receipt.test.d.ts +2 -0
  273. package/dist/test/surgery-marks-receipt.test.d.ts.map +1 -0
  274. package/dist/test/surgery-marks-receipt.test.js +540 -0
  275. package/dist/test/surgery-marks-receipt.test.js.map +1 -0
  276. package/dist/test/tool-batch-hook.test.d.ts +8 -0
  277. package/dist/test/tool-batch-hook.test.d.ts.map +1 -0
  278. package/dist/test/tool-batch-hook.test.js +275 -0
  279. package/dist/test/tool-batch-hook.test.js.map +1 -0
  280. package/dist/test/tool-presentation.test.d.ts +2 -0
  281. package/dist/test/tool-presentation.test.d.ts.map +1 -0
  282. package/dist/test/tool-presentation.test.js +268 -0
  283. package/dist/test/tool-presentation.test.js.map +1 -0
  284. package/dist/test/tool-result-guard.test.js +2545 -19
  285. package/dist/test/tool-result-guard.test.js.map +1 -1
  286. package/dist/test/tool-result-history.test.js +2 -1
  287. package/dist/test/tool-result-history.test.js.map +1 -1
  288. package/dist/test/turn-trigger-provenance.test.js +35 -0
  289. package/dist/test/turn-trigger-provenance.test.js.map +1 -1
  290. package/dist/test/unconvertible-tool-result.test.d.ts +2 -0
  291. package/dist/test/unconvertible-tool-result.test.d.ts.map +1 -0
  292. package/dist/test/unconvertible-tool-result.test.js +358 -0
  293. package/dist/test/unconvertible-tool-result.test.js.map +1 -0
  294. package/dist/test/workspace-materialize-freshness.test.d.ts +16 -0
  295. package/dist/test/workspace-materialize-freshness.test.d.ts.map +1 -0
  296. package/dist/test/workspace-materialize-freshness.test.js +252 -0
  297. package/dist/test/workspace-materialize-freshness.test.js.map +1 -0
  298. package/dist/test/workspace-three-way.test.d.ts +12 -0
  299. package/dist/test/workspace-three-way.test.d.ts.map +1 -0
  300. package/dist/test/workspace-three-way.test.js +3194 -0
  301. package/dist/test/workspace-three-way.test.js.map +1 -0
  302. package/dist/tsconfig.tsbuildinfo +1 -1
  303. package/package.json +2 -2
  304. package/src/agent.ts +124 -45
  305. package/src/api/server.ts +43 -4
  306. package/src/code-execution/py-runner.ts +66 -9
  307. package/src/code-execution/runtime-py.ts +215 -38
  308. package/src/framework.ts +3191 -544
  309. package/src/gate/event-gate.ts +59 -7
  310. package/src/index.ts +18 -2
  311. package/src/mcpl/channel-registry.ts +24 -3
  312. package/src/mcpl/conversation-router.ts +34 -1
  313. package/src/mcpl/feature-set-manager.ts +36 -11
  314. package/src/mcpl/index.ts +4 -0
  315. package/src/mcpl/prose-stream-router.ts +79 -23
  316. package/src/mcpl/push-coalescer.ts +62 -13
  317. package/src/mcpl/push-handler.ts +41 -12
  318. package/src/mcpl/reference-fetcher.ts +18 -8
  319. package/src/mcpl/references.ts +57 -13
  320. package/src/mcpl/server-connection.ts +95 -13
  321. package/src/mcpl/server-registry.ts +24 -7
  322. package/src/mcpl/tool-lifecycle.ts +77 -2
  323. package/src/mcpl/tool-pattern-check.ts +106 -0
  324. package/src/mcpl/types.ts +15 -5
  325. package/src/mcpl/visible-content.ts +57 -0
  326. package/src/module-registry.ts +269 -25
  327. package/src/modules/workspace/branch-intent.ts +123 -0
  328. package/src/modules/workspace/disk-agreement.ts +325 -0
  329. package/src/modules/workspace/effects.ts +362 -0
  330. package/src/modules/workspace/index.ts +1098 -446
  331. package/src/modules/workspace/observe.ts +865 -0
  332. package/src/modules/workspace/reconcile.ts +1157 -0
  333. package/src/modules/workspace/sync.ts +6 -336
  334. package/src/modules/workspace/types.ts +75 -10
  335. package/src/operator-log.ts +30 -0
  336. package/src/provider-waits.ts +523 -0
  337. package/src/record-journal.ts +246 -0
  338. package/src/refusal-reactions.ts +14 -12
  339. package/src/shutdown.ts +49 -0
  340. package/src/silent-heartbeat.ts +141 -0
  341. package/src/tool-presentation.ts +188 -0
  342. package/src/tool-result-guard.ts +384 -34
  343. package/src/tool-result-history.ts +25 -1
  344. package/src/types/agent.ts +18 -2
  345. package/src/types/events.ts +16 -0
  346. package/src/types/framework.ts +12 -0
  347. package/src/types/module.ts +27 -4
  348. package/src/types/trace.ts +94 -3
@@ -0,0 +1,482 @@
1
+ /**
2
+ * Provider waits: a provider's stated `retry-after`, kept as a deadline that
3
+ * outlives the process.
4
+ *
5
+ * When a provider answers "retry after N", calling the same model again
6
+ * before then adds traffic the provider has said will fail. The framework
7
+ * therefore records the wait per (agent, model) and admits no call to that
8
+ * model for that agent until it passes. The wait is a fact about the provider,
9
+ * not about the conversation, so it is kept in typed Chronicle records
10
+ * (RecordJournal): a restart honours it, and a branch switch or rollback
11
+ * does not rewind it.
12
+ *
13
+ * - A later, shorter wait for the same (agent, model) never shortens an
14
+ * outstanding longer one: the binding deadline is the maximum. Requests
15
+ * that finish at different times can carry different hints.
16
+ * - A negative wait is already over (a provider's "retry now", or an HTTP
17
+ * date already past), -Infinity included: it binds nothing, the same as 0.
18
+ * - A wait that cannot be represented as an instant (NaN, +Infinity, or past
19
+ * the last instant a Date can hold) becomes an indefinite wait rather than
20
+ * a shorter one, held until explicitly released.
21
+ * - A wait ends when its instant passes (nothing is written), or when an
22
+ * operator releases it (recorded). Selecting a different model is not a
23
+ * release: that model proceeds, and the old model's wait still applies if it
24
+ * is selected again before the wait passes.
25
+ *
26
+ * One writer per record type (RecordJournal's contract): the framework that
27
+ * owns the store.
28
+ */
29
+ import { RecordJournal } from './record-journal.js';
30
+ import { safeSlice } from './safe-slice.js';
31
+ export const PROVIDER_WAIT_RECORD_TYPE = 'framework/provider-wait';
32
+ /** Checkpoint the reduced waits after this many entries, bounding replay at open. */
33
+ const CHECKPOINT_EVERY = 64;
34
+ /** The last instant a JavaScript Date can represent (ECMA-262 time value range). */
35
+ const MAX_DATE_MS = 8.64e15;
36
+ /**
37
+ * The instant a stated wait ends, or null when it cannot be represented:
38
+ * the wait is then held until released, never shortened. A negative wait
39
+ * ends at `now`: it is already over, never indefinite.
40
+ */
41
+ export function waitDeadline(retryAfterMs, now) {
42
+ if (typeof retryAfterMs !== 'number' || Number.isNaN(retryAfterMs))
43
+ return null;
44
+ // Over before it can't be represented: -Infinity is as negative as any.
45
+ if (retryAfterMs <= 0)
46
+ return Math.ceil(now);
47
+ if (!Number.isFinite(retryAfterMs))
48
+ return null;
49
+ const until = Math.ceil(now + Math.max(0, retryAfterMs));
50
+ if (!Number.isSafeInteger(until) || until > MAX_DATE_MS)
51
+ return null;
52
+ return until;
53
+ }
54
+ const keyOf = (agent, model) => `${agent}\u0000${model}`;
55
+ /** Whether `next` binds longer than `current` (null is longer than any instant). */
56
+ function outlasts(next, current) {
57
+ if (current === null)
58
+ return false;
59
+ if (next === null)
60
+ return true;
61
+ return next > current;
62
+ }
63
+ function reduce(waits, entry) {
64
+ if (entry.kind === 'set') {
65
+ const key = keyOf(entry.agent, entry.model);
66
+ const current = waits.get(key);
67
+ if (current && !outlasts(entry.until, current.until))
68
+ return;
69
+ waits.set(key, { agent: entry.agent, model: entry.model, until: entry.until, reason: entry.reason, setAt: entry.at });
70
+ return;
71
+ }
72
+ for (const [key, wait] of waits) {
73
+ if (wait.agent === entry.agent && (entry.model === null || wait.model === entry.model))
74
+ waits.delete(key);
75
+ }
76
+ }
77
+ /** How often a binding act this process could not record is offered to the journal again, absent another act. */
78
+ const RETRY_WRITE_MS = 30_000;
79
+ /** How often unreadable recorded waits are read again, by default (`retryReadMs`). */
80
+ const RETRY_READ_MS = 30_000;
81
+ /**
82
+ * The model that stands for every model: how list() shows the hold for
83
+ * unreadable records, and what release() takes as "every model" (the same as
84
+ * omitting it). No provider model is named that.
85
+ */
86
+ export const EVERY_MODEL = '*';
87
+ /** A control character (C0, C1, U+2028, U+2029): in a log line, one can start
88
+ * a line that reads as the framework's own. */
89
+ const CONTROL_CHARACTER = /[\u0000-\u001f\u007f-\u009f\u2028\u2029]/;
90
+ /** Text from a caller or a provider, with each control character a space, so
91
+ * it can't forge a log line where it is printed. */
92
+ export function withoutControls(text) {
93
+ return text.replace(new RegExp(CONTROL_CHARACTER.source, 'g'), ' ');
94
+ }
95
+ /**
96
+ * Whether a release's `model`, as an operator surface receives it, names a
97
+ * scope: omitted or EVERY_MODEL for every model, any other nonempty string
98
+ * without a control character for that model. Anything else is malformed and
99
+ * is refused, never taken as "every model" (and never logged).
100
+ */
101
+ export function isReleaseModel(model) {
102
+ return model === undefined || (typeof model === 'string' && model !== '' && !CONTROL_CHARACTER.test(model));
103
+ }
104
+ const errorText = (error) => (error instanceof Error ? error.message : String(error));
105
+ const live = (wait, now) => wait && (wait.until === null || wait.until > now) ? wait : undefined;
106
+ // What a record must hold to be read as provider waits: every field the
107
+ // reduction, or a reader of its waits, uses. A record that parses but lacks
108
+ // one is unreadable (fail closed), never read as "no waits".
109
+ const fieldsOf = (value) => typeof value === 'object' && value !== null && !Array.isArray(value) ? value : undefined;
110
+ const isText = (value) => typeof value === 'string';
111
+ /** A number a Date can hold (NaN and the infinities fail the comparison). */
112
+ const isInstant = (value) => typeof value === 'number' && Math.abs(value) <= MAX_DATE_MS;
113
+ const isDeadline = (value) => value === null || isInstant(value);
114
+ function isWait(value) {
115
+ const wait = fieldsOf(value);
116
+ return wait !== undefined && isText(wait.agent) && isText(wait.model) && isDeadline(wait.until) &&
117
+ isText(wait.reason) && isInstant(wait.setAt);
118
+ }
119
+ /** A release's `by` and `at` are never read back, so they are not required. */
120
+ function isEntry(value) {
121
+ const entry = fieldsOf(value);
122
+ if (entry === undefined || !isText(entry.agent))
123
+ return false;
124
+ if (entry.kind === 'set') {
125
+ return isText(entry.model) && isDeadline(entry.until) && isText(entry.reason) && isInstant(entry.at);
126
+ }
127
+ return entry.kind === 'released' && (entry.model === null || isText(entry.model));
128
+ }
129
+ function isSnapshot(value) {
130
+ const waits = fieldsOf(value)?.waits;
131
+ if (!Array.isArray(waits) || !waits.every(isWait))
132
+ return false;
133
+ // The writer keeps one wait per (agent, model). A repeated key has no
134
+ // reading that is sure to keep the longer wait.
135
+ return new Set(waits.map((wait) => keyOf(wait.agent, wait.model))).size === waits.length;
136
+ }
137
+ export class ProviderWaits {
138
+ /**
139
+ * The reduction of exactly what the journal holds: its latest checkpoint and
140
+ * every entry after it, as loaded or as appended and synced. A checkpoint
141
+ * records this, so it covers the same ordered acts the journal does.
142
+ */
143
+ durable = new Map();
144
+ /**
145
+ * Acts of this process not yet known to be durably recorded, in the order
146
+ * they were made. They bind here from the moment they are made, and are
147
+ * offered to the journal, in order, at every later act until one lands.
148
+ * Once the journal can't be written again (an append that threw inside the
149
+ * store: RecordJournal.needsReopen), they are offered no more: the act that
150
+ * threw may still be in the store's log, where the next start finds it,
151
+ * and every later one holds in this process only.
152
+ */
153
+ pending = [];
154
+ /** What binds in this process: `durable` with `pending` applied in order. */
155
+ view = new Map();
156
+ /**
157
+ * Set while the recorded waits cannot be read. Their absence is then
158
+ * unknown, not "no waits": every (agent, model) is held (fail closed) until
159
+ * they can be read or an operator releases the agent's waits. Releases made
160
+ * meanwhile are kept in `waived` (agent, model or EVERY_MODEL) for this
161
+ * process; a restart that still cannot read them holds again.
162
+ */
163
+ unreadable = null;
164
+ lastWriteTry = 0;
165
+ /**
166
+ * How often unreadable recorded waits are read again. The hold that stands
167
+ * for them ends without any act (the records read again), so whoever holds
168
+ * on a wait with no deadline asks active() again this often.
169
+ */
170
+ retryReadMs;
171
+ store;
172
+ journal;
173
+ now;
174
+ log;
175
+ onUnreadable;
176
+ /**
177
+ * `store` null keeps waits in memory only (a framework without a store has
178
+ * no restart to survive). `onUnreadable` hears each time the recorded waits
179
+ * become unreadable, once per episode (a restart that still cannot read them
180
+ * is a new one): every (agent, model) is then held until they read again or
181
+ * an operator releases, and a cause that persists (a malformed or newer
182
+ * record) never reads again, so a person has to hear of it.
183
+ */
184
+ constructor(store, opts = {}) {
185
+ this.now = opts.now ?? Date.now;
186
+ this.log = opts.log ?? ((line) => console.error(line));
187
+ this.onUnreadable = opts.onUnreadable ?? (() => { });
188
+ this.retryReadMs = opts.retryReadMs ?? RETRY_READ_MS;
189
+ this.store = store;
190
+ this.journal = store ? new RecordJournal(store, { type: PROVIDER_WAIT_RECORD_TYPE }) : null;
191
+ this.load();
192
+ }
193
+ /**
194
+ * The wait binding (agent, model) now, if any. While the recorded waits
195
+ * cannot be read, an indefinite wait saying so, unless an operator released
196
+ * this agent's waits since.
197
+ */
198
+ active(agent, model) {
199
+ this.retry();
200
+ if (this.unreadable && !this.waived(agent, model))
201
+ return this.unreadableWait(agent, model);
202
+ const wait = live(this.view.get(keyOf(agent, model)), this.now());
203
+ return wait ? { ...wait } : undefined;
204
+ }
205
+ /**
206
+ * Whether a recorded wait binds this agent now, unreadable history aside:
207
+ * the agent of a wait the listing shows, even one the framework no longer
208
+ * has (a disposed conversation fork), which a release can then name.
209
+ */
210
+ recorded(agent) {
211
+ const now = this.now();
212
+ for (const wait of this.view.values())
213
+ if (wait.agent === agent && live(wait, now))
214
+ return true;
215
+ return false;
216
+ }
217
+ /**
218
+ * Every wait binding now, for one agent or all. While the recorded waits
219
+ * cannot be read, it leads with an indefinite wait on EVERY_MODEL saying
220
+ * so (for the agent asked about, or for EVERY_MODEL agents).
221
+ */
222
+ list(agent) {
223
+ this.retry();
224
+ const result = [];
225
+ if (this.unreadable && (agent === undefined || !this.waived(agent, EVERY_MODEL))) {
226
+ result.push(this.unreadableWait(agent ?? EVERY_MODEL, EVERY_MODEL));
227
+ }
228
+ const now = this.now();
229
+ for (const wait of this.view.values()) {
230
+ if (agent !== undefined && wait.agent !== agent)
231
+ continue;
232
+ if (live(wait, now))
233
+ result.push({ ...wait });
234
+ }
235
+ return result;
236
+ }
237
+ /** Record a provider's stated wait for (agent, model). */
238
+ set(agent, model, retryAfterMs, reason) {
239
+ const at = this.now();
240
+ // Decide against what the journal now holds: an earlier ambiguous write
241
+ // is reconciled first.
242
+ this.reconcile();
243
+ const entry = { kind: 'set', agent, model, until: waitDeadline(retryAfterMs, at), reason, at };
244
+ const before = live(this.view.get(keyOf(agent, model)), at);
245
+ // A wait already over (0, or a "retry now") binds nothing: with nothing
246
+ // binding either, there is nothing to record.
247
+ if (before === undefined && entry.until !== null && entry.until <= at) {
248
+ this.flush();
249
+ return { wait: { agent, model, until: entry.until, reason, setAt: at }, changed: false };
250
+ }
251
+ const changed = before === undefined || outlasts(entry.until, before.until);
252
+ if (changed)
253
+ this.apply(entry);
254
+ // An unchanged binding is not proof it was recorded: offer what is pending.
255
+ this.flush();
256
+ return { wait: { ...this.view.get(keyOf(agent, model)) }, changed };
257
+ }
258
+ /**
259
+ * Release (agent, model)'s wait, or every wait of the agent when `model` is
260
+ * omitted; recorded. While the recorded waits cannot be read, it also lifts
261
+ * the hold that stands for them, exactly as far as the release names (that
262
+ * model, or every model), in this process only. Each lifted wait says how
263
+ * far its release reaches (ProviderWaitRelease). A release that finds
264
+ * nothing binding lifts and records nothing, and lists nothing. That
265
+ * includes a replay of an earlier release. It still offers this process's
266
+ * unrecorded acts to the journal again.
267
+ */
268
+ release(agent, model, by) {
269
+ // EVERY_MODEL, as list() shows it, names every model: the same release
270
+ // as omitting the model, in this process and in the record alike.
271
+ if (model === EVERY_MODEL)
272
+ model = undefined;
273
+ this.reconcile();
274
+ const now = this.now();
275
+ const released = [...this.view.values()]
276
+ .filter((wait) => wait.agent === agent && (model === undefined || wait.model === model) && live(wait, now))
277
+ .map((wait) => ({ ...wait }));
278
+ const target = model ?? EVERY_MODEL;
279
+ let override;
280
+ if (this.unreadable && !this.waived(agent, target)) {
281
+ override = this.unreadableWait(agent, target);
282
+ this.unreadable.waived.add(keyOf(agent, target));
283
+ }
284
+ if (released.length === 0 && !override) {
285
+ // Nothing binds to lift, so this release lists nothing. It may be a
286
+ // replay of one whose record hasn't landed ('pending'): offer this
287
+ // process's unrecorded acts to the journal again now.
288
+ this.flush();
289
+ return [];
290
+ }
291
+ const entry = { kind: 'released', agent, model: model ?? null, by, at: now };
292
+ this.apply(entry);
293
+ this.flush();
294
+ const recorded = !this.pending.includes(entry);
295
+ return [
296
+ ...(override ? [{ wait: override, release: 'in-process override' }] : []),
297
+ ...released.map((wait) => ({ wait, release: recorded ? 'recorded' : 'pending' })),
298
+ ];
299
+ }
300
+ waived(agent, model) {
301
+ const waived = this.unreadable?.waived;
302
+ return waived !== undefined && (waived.has(keyOf(agent, EVERY_MODEL)) || waived.has(keyOf(agent, model)));
303
+ }
304
+ unreadableWait(agent, model) {
305
+ return {
306
+ agent, model, until: null, setAt: this.unreadable.at,
307
+ reason: `recorded provider waits could not be read (${this.unreadable.error}); held until they can be read or an operator releases this agent's waits`,
308
+ };
309
+ }
310
+ /** Make an act bind in this process, ahead of its record. */
311
+ apply(entry) {
312
+ this.pending.push(entry);
313
+ reduce(this.view, entry);
314
+ }
315
+ /** Rebuild what binds from the journal's reduction and this process's pending acts, in order. */
316
+ rebuildView() {
317
+ const view = new Map();
318
+ for (const [key, wait] of this.durable)
319
+ view.set(key, { ...wait });
320
+ for (const entry of this.pending)
321
+ reduce(view, entry);
322
+ this.view = view;
323
+ }
324
+ /**
325
+ * Read the journal into `durable`. A failure leaves the history unreadable
326
+ * (fail closed, reported once), never empty. So does a record that parses
327
+ * but cannot be read as provider waits: a checkpoint also stands for every
328
+ * entry before it, so a malformed one read as empty would drop them all.
329
+ */
330
+ load() {
331
+ if (!this.journal)
332
+ return true;
333
+ const now = this.now();
334
+ try {
335
+ const { snapshot, entries } = this.journal.load();
336
+ // RecordJournal shows a checkpoint whose snapshot is null as no
337
+ // checkpoint at all (it still skips the entries it covers); this writer
338
+ // never writes one, so finding such a record means it is malformed.
339
+ const malformedCheckpoint = snapshot === null
340
+ ? this.store.getRecordIdsByType(this.journal.checkpointType).length > 0
341
+ : !isSnapshot(snapshot);
342
+ if (malformedCheckpoint)
343
+ throw new Error('a provider-wait checkpoint is malformed');
344
+ for (const { id, entry } of entries) {
345
+ if (!isEntry(entry))
346
+ throw new Error(`provider-wait record ${id} is malformed`);
347
+ }
348
+ const loaded = new Map();
349
+ for (const wait of snapshot?.waits ?? [])
350
+ loaded.set(keyOf(wait.agent, wait.model), { ...wait });
351
+ for (const { entry } of entries)
352
+ reduce(loaded, entry);
353
+ this.durable = loaded;
354
+ }
355
+ catch (error) {
356
+ if (this.unreadable) {
357
+ this.unreadable.lastTry = now;
358
+ }
359
+ else {
360
+ this.unreadable = { error: safeSlice(errorText(error), 0, 300), at: now, lastTry: now, waived: new Set() };
361
+ this.log(`[provider-wait] recorded provider waits could not be read (${this.unreadable.error}): ` +
362
+ 'no provider call is admitted until they can be read or an operator releases an agent\'s waits ' +
363
+ '(release-provider-wait); inspection is unaffected');
364
+ try {
365
+ this.onUnreadable(this.unreadable.error);
366
+ }
367
+ catch { /* reporting never changes the hold */ }
368
+ }
369
+ return false;
370
+ }
371
+ const wasUnreadable = this.unreadable !== null;
372
+ this.unreadable = null;
373
+ this.rebuildView();
374
+ // What binds is the journal's reduction with this process's unrecorded
375
+ // acts applied after it: a release made meanwhile lifts what it names.
376
+ if (wasUnreadable)
377
+ this.log(`[provider-wait] recorded provider waits are readable again; ${loaded(this.view, now)} bind`);
378
+ return true;
379
+ }
380
+ /** Reconcile an ambiguous write before deciding anything against the journal. */
381
+ reconcile() {
382
+ if (this.journal?.needsReconcile)
383
+ this.load();
384
+ }
385
+ /** Offer unrecorded acts and unreadable history again, at most every so often, absent other acts. */
386
+ retry() {
387
+ const now = this.now();
388
+ if (this.unreadable && now - this.unreadable.lastTry >= this.retryReadMs)
389
+ this.load();
390
+ if (this.pending.length > 0 && now - this.lastWriteTry >= RETRY_WRITE_MS) {
391
+ this.reconcile();
392
+ this.flush();
393
+ }
394
+ }
395
+ /**
396
+ * Record pending acts in order, each durable before the next: a lost wait
397
+ * would admit an early call after a crash. The first that fails stays
398
+ * pending with every act after it (they still bind in this process) and is
399
+ * reported; an ambiguous one is reconciled before the next attempt, so an
400
+ * act may be recorded twice, which reduces the same. An append or
401
+ * checkpoint that throws inside the store leaves the journal unwritable
402
+ * until the store is reopened (RecordJournal.needsReopen), so nothing is
403
+ * written after it: that is said once, where it happens, and the acts stay
404
+ * pending, binding in this process. A checkpoint records the
405
+ * journal's own reduction, never this process's unrecorded acts, and never
406
+ * while the history is unreadable (it would cover entries that were never
407
+ * reduced).
408
+ */
409
+ flush() {
410
+ if (!this.journal) {
411
+ for (const entry of this.pending.splice(0))
412
+ reduce(this.durable, entry);
413
+ return;
414
+ }
415
+ this.lastWriteTry = this.now();
416
+ while (this.pending.length > 0) {
417
+ if (this.journal.needsReconcile && !this.load())
418
+ return;
419
+ if (this.journal.needsReconcile)
420
+ return;
421
+ // Offering again would only fail again; reportUnwritable said so when
422
+ // the write threw.
423
+ if (this.journal.needsReopen)
424
+ return;
425
+ const entry = this.pending[0];
426
+ try {
427
+ this.journal.append(entry, { durable: true });
428
+ }
429
+ catch (error) {
430
+ if (this.journal.needsReopen) {
431
+ this.reportUnwritable(error, `${entry.kind === 'set' ? 'a wait' : 'a release'} for agent=${entry.agent} model=${entry.model ?? EVERY_MODEL}`);
432
+ return;
433
+ }
434
+ this.log(`[provider-wait] could not record ${entry.kind} for agent=${entry.agent} model=${entry.model ?? EVERY_MODEL} ` +
435
+ `(${this.pending.length} act(s) pending); it holds in this process and is offered again at the next act: ${safeSlice(errorText(error), 0, 300)}`);
436
+ return;
437
+ }
438
+ this.pending.shift();
439
+ reduce(this.durable, entry);
440
+ }
441
+ if (this.unreadable || this.journal.needsReopen || this.journal.entriesSinceCheckpoint < CHECKPOINT_EVERY)
442
+ return;
443
+ const now = this.now();
444
+ try {
445
+ this.journal.checkpoint({ waits: [...this.durable.values()].filter((wait) => live(wait, now)) }, { durable: true });
446
+ }
447
+ catch (error) {
448
+ if (this.journal.needsReopen) {
449
+ this.reportUnwritable(error, 'a checkpoint');
450
+ return;
451
+ }
452
+ this.log(`[provider-wait] could not checkpoint provider waits; the journal still replays in full: ${safeSlice(errorText(error), 0, 300)}`);
453
+ }
454
+ }
455
+ /**
456
+ * Say that the journal can't be written again: the store's append threw
457
+ * inside the store, and only a reopen (as at the next start) reads its log
458
+ * back. Called where that first happens; flush() writes nothing after it,
459
+ * so it is said once for the journal's life. What `writing` names may still
460
+ * be in the store's log, and the next start loads it if so. A stated wait
461
+ * still holds here, which is always safe; a later act loses only its
462
+ * record, so after a restart the agent's next call goes to the provider,
463
+ * and a wait it states again starts a new hold. No alert: a failed write
464
+ * is log-only.
465
+ */
466
+ reportUnwritable(error, writing) {
467
+ this.log(`[provider-wait] nothing more is written to the provider-wait journal until the store is reopened, ` +
468
+ `as at the next start: the store's append of ${writing} failed (${safeSlice(errorText(error), 0, 300)}), and that ` +
469
+ `record may still be in the store's log, where the next start finds it. Every wait and release after it holds ` +
470
+ `in this process only (${this.pending.length} act(s) pending now). After a restart, such a wait no longer binds: ` +
471
+ 'the agent\'s next call to that model goes to the provider, and a wait the provider states again starts a new hold. ' +
472
+ 'Such a release no longer lifts its wait');
473
+ }
474
+ }
475
+ function loaded(waits, now) {
476
+ let count = 0;
477
+ for (const wait of waits.values())
478
+ if (live(wait, now))
479
+ count++;
480
+ return count;
481
+ }
482
+ //# sourceMappingURL=provider-waits.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"provider-waits.js","sourceRoot":"","sources":["../../src/provider-waits.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAGH,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAE5C,MAAM,CAAC,MAAM,yBAAyB,GAAG,yBAAyB,CAAC;AAEnE,qFAAqF;AACrF,MAAM,gBAAgB,GAAG,EAAE,CAAC;AAC5B,oFAAoF;AACpF,MAAM,WAAW,GAAG,OAAO,CAAC;AAqB5B;;;;GAIG;AACH,MAAM,UAAU,YAAY,CAAC,YAAqB,EAAE,GAAW;IAC7D,IAAI,OAAO,YAAY,KAAK,QAAQ,IAAI,MAAM,CAAC,KAAK,CAAC,YAAY,CAAC;QAAE,OAAO,IAAI,CAAC;IAChF,wEAAwE;IACxE,IAAI,YAAY,IAAI,CAAC;QAAE,OAAO,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAC7C,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,YAAY,CAAC;QAAE,OAAO,IAAI,CAAC;IAChD,MAAM,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,YAAY,CAAC,CAAC,CAAC;IACzD,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,KAAK,CAAC,IAAI,KAAK,GAAG,WAA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AC;YACD,IAAI,CAAC,GAAG,CAAC,2FAA2F,SAAS,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE,CAAC,EAAE,GAAG,CAAC,EAAE,CAAC,CAAC;QAC7I,CAAC;IACH,CAAC;IAED;;;;;;;;;;OAUG;IACK,gBAAgB,CAAC,KAAc,EAAE,OAAe;QACtD,IAAI,CAAC,GAAG,CAAC,oGAAoG;YAC3G,+CAA+C,OAAO,YAAY,SAAS,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE,CAAC,EAAE,GAAG,CAAC,cAAc;YACnH,+GAA+G;YAC/G,yBAAyB,IAAI,CAAC,OAAO,CAAC,MAAM,sEAAsE;YAClH,qHAAqH;YACrH,yCAAyC,CAAC,CAAC;IAC/C,CAAC;CACF;AAED,SAAS,MAAM,CAAC,KAAgC,EAAE,GAAW;IAC3D,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,KAAK,MAAM,IAAI,IAAI,KAAK,CAAC,MAAM,EAAE;QAAE,IAAI,IAAI,CAAC,IAAI,EAAE,GAAG,CAAC;YAAE,KAAK,EAAE,CAAC;IAChE,OAAO,KAAK,CAAC;AACf,CAAC"}
@@ -0,0 +1,127 @@
1
+ /**
2
+ * An append-only journal of typed Chronicle records that does not follow
3
+ * branch switches.
4
+ *
5
+ * Chronicle's branch state — snapshot, tree and log states — rewinds with a
6
+ * historical rollback (createBranchAt + switchBranch). Typed records do not:
7
+ * `getRecordIdsByType` enumerates them from every branch, in global order, and
8
+ * they survive `deleteBranch`, reopen and a hard kill. A journal is for state
9
+ * that must not rewind with the conversation but should live and die with its
10
+ * store: what a workspace's disk last agreed with, what reached a recipient,
11
+ * what became of a suppressed draft.
12
+ *
13
+ * Durability. A record append reaches the OS before `append` returns, so it
14
+ * survives a killed process, while branch-state commits are buffered until
15
+ * `store.sync()`: a record appended after a state commit can outlive that
16
+ * commit. Two barriers, each named where it's needed and neither implied by
17
+ * the `{id, sequence}` an append returns:
18
+ * - `afterCommittedState: true` syncs BEFORE appending — for an entry that
19
+ * asserts state committed just before it, so the entry can never survive
20
+ * without that state;
21
+ * - `durable: true` syncs AFTER appending — for an entry that must be on
22
+ * stable storage before the caller acts on it (an attempt before its
23
+ * outbound request, an intent before its disk effect).
24
+ *
25
+ * Checkpoints bound the replay at open: `checkpoint(snapshot)` records the
26
+ * caller's reduced state together with the last entry it covers, and `load()`
27
+ * returns the latest checkpoint's snapshot plus only the entries appended
28
+ * after it. A journal that never checkpoints replays everything. What entries
29
+ * mean, how they reduce, and when to checkpoint stay with the caller.
30
+ *
31
+ * One writer per record type: a checkpoint covers every entry up to the last
32
+ * one THIS journal loaded or appended, so entries another instance appended
33
+ * to the same type in between would be covered without being reduced.
34
+ *
35
+ * An ambiguous write must be resolved before the journal writes again. Once
36
+ * `append` or `checkpoint` has handed a record to the store, a failure leaves
37
+ * a record the caller didn't reduce, or may leave one, and a later checkpoint
38
+ * would cover it without its ever entering the reducer. There are two kinds:
39
+ * - The store's append itself throws. Its record may still be in the store's
40
+ * log, outside this process's view of the store: the store writes a
41
+ * record to its log, syncs the log every so often, and only then indexes
42
+ * the record, which is what `getRecordIdsByType` reads. So a failed sync
43
+ * leaves a record that reopening the store will find and no `load()` on
44
+ * this store can. Nothing but a reopen rereads the log, so from then on
45
+ * every `append` and `checkpoint` throws, for the life of the journal. A
46
+ * journal on the store reopened, as at the next start, loads that record
47
+ * with its tail for the caller to reduce.
48
+ * - A failure after the append returned: a `durable` barrier, or reading
49
+ * the id it returned. The record is in this process's view, so every
50
+ * further `append` and `checkpoint` throws until `load()` has replayed the
51
+ * tail, that record included, for the caller to reduce.
52
+ * A failure BEFORE the record reaches the store (the `afterCommittedState`
53
+ * sync) wrote nothing and leaves the journal usable.
54
+ */
55
+ import type { JsStore } from '@animalabs/chronicle';
56
+ /** Where an appended record landed. Not a durability receipt — see the barriers. */
57
+ export interface JournalRecordRef {
58
+ /** Chronicle record id: global, increasing in append order. */
59
+ id: string;
60
+ /** The record's sequence on the branch current when it was appended. */
61
+ sequence: number;
62
+ }
63
+ export interface JournalEntry<Entry> extends JournalRecordRef {
64
+ entry: Entry;
65
+ }
66
+ export interface JournalLoad<Entry, Snapshot> {
67
+ /** The latest checkpoint's snapshot, or null when none was written. */
68
+ snapshot: Snapshot | null;
69
+ /** Every entry appended after that checkpoint, in append order. */
70
+ entries: Array<JournalEntry<Entry>>;
71
+ }
72
+ export interface AppendOptions {
73
+ /** `store.sync()` before appending: the entry asserts state committed just before it. */
74
+ afterCommittedState?: boolean;
75
+ /** `store.sync()` after appending: the entry must be on stable storage before the caller acts. */
76
+ durable?: boolean;
77
+ }
78
+ export declare class RecordJournal<Entry, Snapshot = never> {
79
+ private readonly store;
80
+ /** Record type of the entries. */
81
+ readonly type: string;
82
+ /** Record type of the checkpoints. */
83
+ readonly checkpointType: string;
84
+ /** Highest entry id this journal has loaded or appended (0 when none). */
85
+ private lastEntryId;
86
+ private sinceCheckpoint;
87
+ /** Set by a failure after the store's append returned; cleared by load(). */
88
+ private unreconciled;
89
+ /** Set when the store's append itself threw: its record may be in the log
90
+ * but not in this process's view of the store. Never cleared. */
91
+ private unreadable;
92
+ constructor(store: JsStore, opts: {
93
+ type: string;
94
+ checkpointType?: string;
95
+ });
96
+ /** Entries appended since the last checkpoint (or since load, when there was none). */
97
+ get entriesSinceCheckpoint(): number;
98
+ /**
99
+ * The latest checkpoint and every entry after it, whichever branch each was
100
+ * appended on. The caller rebuilds its reduced state from exactly these —
101
+ * at open, and to reconcile after an ambiguous write. Also positions the
102
+ * journal, so later checkpoints cover what was loaded.
103
+ */
104
+ load(): JournalLoad<Entry, Snapshot>;
105
+ /** True after a failure that load() can resolve, until it has replayed the
106
+ * tail. An append that threw inside the store is never resolved here: the
107
+ * journal stays unwritable until a journal on the reopened store loads it. */
108
+ get needsReconcile(): boolean;
109
+ /** True once an append threw inside the store, for the rest of the
110
+ * journal's life. What `load()` returns may then lack that append's record,
111
+ * so a caller that decides from the journal's contents can't trust them
112
+ * until a journal on the reopened store has loaded it. */
113
+ get needsReopen(): boolean;
114
+ /** Append one entry. The returned ref says where it landed, not that it's durable. */
115
+ append(entry: Entry, opts?: AppendOptions): JournalRecordRef;
116
+ /**
117
+ * Record the caller's reduced state as covering every entry this journal has
118
+ * loaded or appended. Like an entry, it's durable only with `durable`.
119
+ */
120
+ checkpoint(snapshot: Snapshot, opts?: {
121
+ durable?: boolean;
122
+ }): JournalRecordRef;
123
+ /** Hand one record to the store; any failure from here on is ambiguous. */
124
+ private write;
125
+ private assertReconciled;
126
+ }
127
+ //# sourceMappingURL=record-journal.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"record-journal.d.ts","sourceRoot":"","sources":["../../src/record-journal.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqDG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAEpD,oFAAoF;AACpF,MAAM,WAAW,gBAAgB;IAC/B,+DAA+D;IAC/D,EAAE,EAAE,MAAM,CAAC;IACX,wEAAwE;IACxE,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,YAAY,CAAC,KAAK,CAAE,SAAQ,gBAAgB;IAC3D,KAAK,EAAE,KAAK,CAAC;CACd;AAED,MAAM,WAAW,WAAW,CAAC,KAAK,EAAE,QAAQ;IAC1C,uEAAuE;IACvE,QAAQ,EAAE,QAAQ,GAAG,IAAI,CAAC;IAC1B,mEAAmE;IACnE,OAAO,EAAE,KAAK,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC,CAAC;CACrC;AAED,MAAM,WAAW,aAAa;IAC5B,yFAAyF;IACzF,mBAAmB,CAAC,EAAE,OAAO,CAAC;IAC9B,kGAAkG;IAClG,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB;AAsBD,qBAAa,aAAa,CAAC,KAAK,EAAE,QAAQ,GAAG,KAAK;IAChD,OAAO,CAAC,QAAQ,CAAC,KAAK,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