@systemfsoftware/agent 0.0.0-stage → 0.1.0-beta.192

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 (717) hide show
  1. package/LICENSE +21 -0
  2. package/dist/ActivityStore-CEy3oY41.mjs +90 -0
  3. package/dist/ActivityStore-CEy3oY41.mjs.map +1 -0
  4. package/dist/ActivityStore-CPXWRHGz.d.mts +91 -0
  5. package/dist/Admin-BcQDrWKY.mjs +329 -0
  6. package/dist/Admin-BcQDrWKY.mjs.map +1 -0
  7. package/dist/Admin-BfolLkHj.d.mts +5678 -0
  8. package/dist/Agent-Bn3zMhDH.mjs +60 -0
  9. package/dist/Agent-Bn3zMhDH.mjs.map +1 -0
  10. package/dist/Agent-DSVEpq4e.d.mts +219 -0
  11. package/dist/AgentError-RkUaetX8.d.mts +128 -0
  12. package/dist/AgentError-yFdnS9bl.mjs +141 -0
  13. package/dist/AgentError-yFdnS9bl.mjs.map +1 -0
  14. package/dist/AgentPolicy-CI7LRBQa.mjs +116 -0
  15. package/dist/AgentPolicy-CI7LRBQa.mjs.map +1 -0
  16. package/dist/AgentPolicy-D-GmZQex.d.mts +128 -0
  17. package/dist/AgentRegistration-Dg53p6du.mjs +14 -0
  18. package/dist/AgentRegistration-Dg53p6du.mjs.map +1 -0
  19. package/dist/AgentRuntime-CoGy_5dm.d.mts +751 -0
  20. package/dist/AgentRuntime-CzP4I4RB.mjs +5249 -0
  21. package/dist/AgentRuntime-CzP4I4RB.mjs.map +1 -0
  22. package/dist/AgentUpdates-7xT0It_R.d.mts +54 -0
  23. package/dist/AgentUpdates-BS_6gR5K.mjs +66 -0
  24. package/dist/AgentUpdates-BS_6gR5K.mjs.map +1 -0
  25. package/dist/BrowserUse-C9vict2M.d.mts +196 -0
  26. package/dist/BrowserUse-DjEyFDLZ.mjs +156 -0
  27. package/dist/BrowserUse-DjEyFDLZ.mjs.map +1 -0
  28. package/dist/Budget-CXh_ChFJ.d.mts +112 -0
  29. package/dist/Budget-j1UkthgY.mjs +351 -0
  30. package/dist/Budget-j1UkthgY.mjs.map +1 -0
  31. package/dist/CodeExecutor-B_Vt1Ghy.mjs +281 -0
  32. package/dist/CodeExecutor-B_Vt1Ghy.mjs.map +1 -0
  33. package/dist/CodeExecutor-D5-Rda75.d.mts +198 -0
  34. package/dist/CodeMode-B7JvfKAD.d.mts +159 -0
  35. package/dist/CodeMode-BI7WhUkr.mjs +555 -0
  36. package/dist/CodeMode-BI7WhUkr.mjs.map +1 -0
  37. package/dist/CommittedActivity-DXyCKCtX.mjs +146 -0
  38. package/dist/CommittedActivity-DXyCKCtX.mjs.map +1 -0
  39. package/dist/CommittedActivity-LWOKXqdi.d.mts +62 -0
  40. package/dist/Compaction-C0hWAmOG.mjs +15 -0
  41. package/dist/Compaction-C0hWAmOG.mjs.map +1 -0
  42. package/dist/Compaction-e5cNeFmY.d.mts +5 -0
  43. package/dist/ContextCompactor-Chm9BLrD.d.mts +165 -0
  44. package/dist/ContextCompactor-DV4NOQvg.mjs +134 -0
  45. package/dist/ContextCompactor-DV4NOQvg.mjs.map +1 -0
  46. package/dist/ContextHistory-C80rW5M7.mjs +76 -0
  47. package/dist/ContextHistory-C80rW5M7.mjs.map +1 -0
  48. package/dist/ContextHistory-akQ1Td7a.d.mts +65 -0
  49. package/dist/ContextTools-917r7gpv.d.mts +186 -0
  50. package/dist/ContextTools-p8JDPo-W.mjs +88 -0
  51. package/dist/ContextTools-p8JDPo-W.mjs.map +1 -0
  52. package/dist/ContextWindow-BJZI4MMt.d.mts +61 -0
  53. package/dist/ContextWindow-kbFcs1IF.mjs +63 -0
  54. package/dist/ContextWindow-kbFcs1IF.mjs.map +1 -0
  55. package/dist/Digest-BDToAbaX.d.mts +25 -0
  56. package/dist/Digest-CU6IWj-2.mjs +106 -0
  57. package/dist/Digest-CU6IWj-2.mjs.map +1 -0
  58. package/dist/DurableAgentRuntime-BHXo8GPJ.mjs +8432 -0
  59. package/dist/DurableAgentRuntime-BHXo8GPJ.mjs.map +1 -0
  60. package/dist/DurableFailpoint-BD5nowNl.mjs +98 -0
  61. package/dist/DurableFailpoint-BD5nowNl.mjs.map +1 -0
  62. package/dist/DurableFailpoint-Cqi5tyof.d.mts +42 -0
  63. package/dist/DurableStep-CzVIho7y.mjs +54 -0
  64. package/dist/DurableStep-CzVIho7y.mjs.map +1 -0
  65. package/dist/DurableStep-cI_Ho_rU.d.mts +109 -0
  66. package/dist/EventSource-BwZVkEQi.mjs +66 -0
  67. package/dist/EventSource-BwZVkEQi.mjs.map +1 -0
  68. package/dist/EventSource-C0TCBeIf.d.mts +52 -0
  69. package/dist/FailureDiagnostic-DdHV2eFs.mjs +304 -0
  70. package/dist/FailureDiagnostic-DdHV2eFs.mjs.map +1 -0
  71. package/dist/FailureDiagnostic-DpzYNI8f.d.mts +232 -0
  72. package/dist/IdGenerator-BCzwV0ZI.mjs +29 -0
  73. package/dist/IdGenerator-BCzwV0ZI.mjs.map +1 -0
  74. package/dist/IdGenerator-BY8NH74o.d.mts +26 -0
  75. package/dist/Identifiers-LWd4837I.mjs +41 -0
  76. package/dist/Identifiers-LWd4837I.mjs.map +1 -0
  77. package/dist/Identifiers-qafW4EP0.d.mts +38 -0
  78. package/dist/InMemory-BrTm6XEO.mjs +57 -0
  79. package/dist/InMemory-BrTm6XEO.mjs.map +1 -0
  80. package/dist/InMemory-C3qXFT5k.d.mts +46 -0
  81. package/dist/InMemory.d.mts +2 -0
  82. package/dist/InMemory.mjs +1 -0
  83. package/dist/InteractiveBrowser-B-hlNJy5.mjs +237 -0
  84. package/dist/InteractiveBrowser-B-hlNJy5.mjs.map +1 -0
  85. package/dist/InteractiveBrowser-nnML0r7w.d.mts +258 -0
  86. package/dist/LifecyclePublication-BDc_ug5v.mjs +204 -0
  87. package/dist/LifecyclePublication-BDc_ug5v.mjs.map +1 -0
  88. package/dist/Memory-CT5bDZqi.d.mts +57 -0
  89. package/dist/Memory-Dl9ecPpm.mjs +204 -0
  90. package/dist/Memory-Dl9ecPpm.mjs.map +1 -0
  91. package/dist/MemoryNamespace-BtnlH_E6.d.mts +35 -0
  92. package/dist/MemoryNamespace-DZSpjLj9.mjs +116 -0
  93. package/dist/MemoryNamespace-DZSpjLj9.mjs.map +1 -0
  94. package/dist/MemoryNotes-4CRzkPBD.d.mts +425 -0
  95. package/dist/MemoryNotes-i41TsH11.mjs +132 -0
  96. package/dist/MemoryNotes-i41TsH11.mjs.map +1 -0
  97. package/dist/MemoryReference-Dd3KSMa7.d.mts +77 -0
  98. package/dist/MemoryReference-RFU0HQJG.mjs +108 -0
  99. package/dist/MemoryReference-RFU0HQJG.mjs.map +1 -0
  100. package/dist/MemoryRevalidation--aAh_Pfq.mjs +136 -0
  101. package/dist/MemoryRevalidation--aAh_Pfq.mjs.map +1 -0
  102. package/dist/MemoryRevalidation-FsJ8E61W.d.mts +64 -0
  103. package/dist/MemoryStore-Bg5RunTk.d.mts +187 -0
  104. package/dist/MemoryStore-E0J99hb2.mjs +245 -0
  105. package/dist/MemoryStore-E0J99hb2.mjs.map +1 -0
  106. package/dist/MessageDelivery-BE2Yf3FC.mjs +615 -0
  107. package/dist/MessageDelivery-BE2Yf3FC.mjs.map +1 -0
  108. package/dist/MessageDelivery-Dh6GZBt_.d.mts +1129 -0
  109. package/dist/Messaging-BqWHYf93.d.mts +216 -0
  110. package/dist/Messaging-DgSiv4f4.mjs +154 -0
  111. package/dist/Messaging-DgSiv4f4.mjs.map +1 -0
  112. package/dist/Messaging-DhM-APnP.d.mts +1074 -0
  113. package/dist/MessagingHost-Bm5e-kAX.mjs +117 -0
  114. package/dist/MessagingHost-Bm5e-kAX.mjs.map +1 -0
  115. package/dist/MessagingHost-CubWeYKN.d.mts +80 -0
  116. package/dist/OperationAuthorizer-VSiHCC2S.mjs +72 -0
  117. package/dist/OperationAuthorizer-VSiHCC2S.mjs.map +1 -0
  118. package/dist/OperationAuthorizer-y-F-Ox_Q.d.mts +62 -0
  119. package/dist/Output-DQ8mWcgf.mjs +7 -0
  120. package/dist/Output-DQ8mWcgf.mjs.map +1 -0
  121. package/dist/Output-SbOdlBFL.d.mts +22 -0
  122. package/dist/PageCapture-BfRURUOA.mjs +275 -0
  123. package/dist/PageCapture-BfRURUOA.mjs.map +1 -0
  124. package/dist/PageCapture-C_mE72Sd.d.mts +216 -0
  125. package/dist/PageScreenshot-BJTASs4o.mjs +53 -0
  126. package/dist/PageScreenshot-BJTASs4o.mjs.map +1 -0
  127. package/dist/PageScreenshot-CaoaH7mc.d.mts +48 -0
  128. package/dist/PersistentHistory-BrtZ0g2j.d.mts +14 -0
  129. package/dist/PersistentHistory-CTAVhHKH.mjs +147 -0
  130. package/dist/PersistentHistory-CTAVhHKH.mjs.map +1 -0
  131. package/dist/PreparedInputAdmission-Ci18uY4s.d.mts +973 -0
  132. package/dist/PreparedInputAdmission-adqVJJa6.mjs +95 -0
  133. package/dist/PreparedInputAdmission-adqVJJa6.mjs.map +1 -0
  134. package/dist/ProvisionalText-BcKpPYKQ.d.mts +70 -0
  135. package/dist/ProvisionalText-DXddTK4c.mjs +47 -0
  136. package/dist/ProvisionalText-DXddTK4c.mjs.map +1 -0
  137. package/dist/Receipt-B-FlCaNH.mjs +35 -0
  138. package/dist/Receipt-B-FlCaNH.mjs.map +1 -0
  139. package/dist/Receipt-B4feBxMY.d.mts +31 -0
  140. package/dist/RecordFormat-DoNh-AFe.mjs +79 -0
  141. package/dist/RecordFormat-DoNh-AFe.mjs.map +1 -0
  142. package/dist/RecordFormat-ZMus8Dx8.d.mts +52 -0
  143. package/dist/Records-C645aQks.mjs +1145 -0
  144. package/dist/Records-C645aQks.mjs.map +1 -0
  145. package/dist/Records-C9I1nCL9.d.mts +1423 -0
  146. package/dist/Recovery-zWy_kmZq.mjs +736 -0
  147. package/dist/Recovery-zWy_kmZq.mjs.map +1 -0
  148. package/dist/Redaction-C0zLBAn6.d.mts +28 -0
  149. package/dist/Redaction-CtyCBMEj.mjs +116 -0
  150. package/dist/Redaction-CtyCBMEj.mjs.map +1 -0
  151. package/dist/RememberingStore-BO-OnCgP.mjs +23 -0
  152. package/dist/RememberingStore-BO-OnCgP.mjs.map +1 -0
  153. package/dist/RememberingStore-DgqXmILx.d.mts +14 -0
  154. package/dist/RunContinuation-BVAHoYlN.d.mts +127 -0
  155. package/dist/RunContinuation-Bpfp087i.mjs +1060 -0
  156. package/dist/RunContinuation-Bpfp087i.mjs.map +1 -0
  157. package/dist/RunEvent-CwgDzXcH.d.mts +465 -0
  158. package/dist/RunEvent-DJxENnJi.mjs +345 -0
  159. package/dist/RunEvent-DJxENnJi.mjs.map +1 -0
  160. package/dist/RunEventSink-B8uM8bw2.mjs +22 -0
  161. package/dist/RunEventSink-B8uM8bw2.mjs.map +1 -0
  162. package/dist/RunEventSink-f-T4jbNB.d.mts +105 -0
  163. package/dist/RunHooks-Bob22Bcj.d.mts +40 -0
  164. package/dist/RunHooks-DWuLBVdU.mjs +60 -0
  165. package/dist/RunHooks-DWuLBVdU.mjs.map +1 -0
  166. package/dist/RunJournal-C_1eqQdr.mjs +1107 -0
  167. package/dist/RunJournal-C_1eqQdr.mjs.map +1 -0
  168. package/dist/RunOptions-BVSa3BO2.d.mts +560 -0
  169. package/dist/RunOptions-COEUCrx4.mjs +61 -0
  170. package/dist/RunOptions-COEUCrx4.mjs.map +1 -0
  171. package/dist/RunPolicyUsage-B-JXRZYO.mjs +25 -0
  172. package/dist/RunPolicyUsage-B-JXRZYO.mjs.map +1 -0
  173. package/dist/RunPolicyUsage-jvJo0vJp.d.mts +21 -0
  174. package/dist/RunStorage-CsQQ0Y5u.mjs +304 -0
  175. package/dist/RunStorage-CsQQ0Y5u.mjs.map +1 -0
  176. package/dist/Sandbox-37vXP0BV.d.mts +24 -0
  177. package/dist/Sandbox-Bze6pHYc.mjs +30 -0
  178. package/dist/Sandbox-Bze6pHYc.mjs.map +1 -0
  179. package/dist/Schedule-BY0lzbij.mjs +336 -0
  180. package/dist/Schedule-BY0lzbij.mjs.map +1 -0
  181. package/dist/ScheduleTransition-CjVP48I8.d.mts +22 -0
  182. package/dist/ScheduleTransition-DtNzlN9k.mjs +180 -0
  183. package/dist/ScheduleTransition-DtNzlN9k.mjs.map +1 -0
  184. package/dist/Scheduling-BpJN9E1j.mjs +699 -0
  185. package/dist/Scheduling-BpJN9E1j.mjs.map +1 -0
  186. package/dist/Scheduling-DYTM29s9.d.mts +57 -0
  187. package/dist/SemanticMemory-BRV0cuWg.mjs +300 -0
  188. package/dist/SemanticMemory-BRV0cuWg.mjs.map +1 -0
  189. package/dist/SemanticMemory-rgWlhW87.d.mts +431 -0
  190. package/dist/SemanticMemoryIndex-D7yNEWpt.d.mts +826 -0
  191. package/dist/SemanticMemoryIndex-godQDjNx.mjs +186 -0
  192. package/dist/SemanticMemoryIndex-godQDjNx.mjs.map +1 -0
  193. package/dist/SemanticMemoryRevalidation-Bm3_pfsI.d.mts +44 -0
  194. package/dist/SemanticMemoryRevalidation-D8VV-eU6.mjs +179 -0
  195. package/dist/SemanticMemoryRevalidation-D8VV-eU6.mjs.map +1 -0
  196. package/dist/Subagent-CSIRUgUo.mjs +1315 -0
  197. package/dist/Subagent-CSIRUgUo.mjs.map +1 -0
  198. package/dist/Subagent-CXEI5YZ8.d.mts +2368 -0
  199. package/dist/SubagentContract-B7vQPueI.d.mts +176 -0
  200. package/dist/SubagentContract-C-7AxTm_.mjs +187 -0
  201. package/dist/SubagentContract-C-7AxTm_.mjs.map +1 -0
  202. package/dist/SubagentHost-BEnICu7g.mjs +70 -0
  203. package/dist/SubagentHost-BEnICu7g.mjs.map +1 -0
  204. package/dist/SubagentHost-CKhz1vbH.d.mts +167 -0
  205. package/dist/SubagentReservations-BYNAHPpm.mjs +465 -0
  206. package/dist/SubagentReservations-BYNAHPpm.mjs.map +1 -0
  207. package/dist/SubagentReservations-WrmIVzO5.d.mts +185 -0
  208. package/dist/SubmissionLedger-WG0beEyt.mjs +921 -0
  209. package/dist/SubmissionLedger-WG0beEyt.mjs.map +1 -0
  210. package/dist/SubmissionStatus-CPZsOM5a.mjs +17 -0
  211. package/dist/SubmissionStatus-CPZsOM5a.mjs.map +1 -0
  212. package/dist/Subscription-BhQbmUD-.mjs +340 -0
  213. package/dist/Subscription-BhQbmUD-.mjs.map +1 -0
  214. package/dist/Subscription-CC--_OnU.d.mts +815 -0
  215. package/dist/SubscriptionInput-BEeB0Cj8.d.mts +42 -0
  216. package/dist/SubscriptionInput-Xcd4JjEs.mjs +53 -0
  217. package/dist/SubscriptionInput-Xcd4JjEs.mjs.map +1 -0
  218. package/dist/SubscriptionTransition-CWt9UVH0.d.mts +23 -0
  219. package/dist/SubscriptionTransition-lrf733c8.mjs +154 -0
  220. package/dist/SubscriptionTransition-lrf733c8.mjs.map +1 -0
  221. package/dist/Subscriptions-4K_qu_2f.mjs +638 -0
  222. package/dist/Subscriptions-4K_qu_2f.mjs.map +1 -0
  223. package/dist/Subscriptions-BF3aSf8X.d.mts +75 -0
  224. package/dist/Thread-B8JApGHa.mjs +283 -0
  225. package/dist/Thread-B8JApGHa.mjs.map +1 -0
  226. package/dist/Thread-BnAtkJl4.d.mts +116 -0
  227. package/dist/ThreadArchiveRange-1PMdjpdJ.mjs +64 -0
  228. package/dist/ThreadArchiveRange-1PMdjpdJ.mjs.map +1 -0
  229. package/dist/ThreadContextHistory-BSStMOLX.mjs +211 -0
  230. package/dist/ThreadContextHistory-BSStMOLX.mjs.map +1 -0
  231. package/dist/ThreadContextHistory-lTZ8Ftq1.d.mts +33 -0
  232. package/dist/ThreadContextHistoryProjection-Cp7_RR6V.d.mts +59 -0
  233. package/dist/ThreadContextHistoryProjection-_8uNz-h1.mjs +149 -0
  234. package/dist/ThreadContextHistoryProjection-_8uNz-h1.mjs.map +1 -0
  235. package/dist/ThreadHistory-BkyGfv5E.d.mts +76 -0
  236. package/dist/ThreadHistory-Blt_W31N.mjs +69 -0
  237. package/dist/ThreadHistory-Blt_W31N.mjs.map +1 -0
  238. package/dist/ThreadImport-C8Tqiezd.mjs +491 -0
  239. package/dist/ThreadImport-C8Tqiezd.mjs.map +1 -0
  240. package/dist/ThreadImport-DaSmrlRi.d.mts +843 -0
  241. package/dist/ThreadInvariants-DyPhMeo-.d.mts +23 -0
  242. package/dist/ThreadInvariants-fniroLRY.mjs +154 -0
  243. package/dist/ThreadInvariants-fniroLRY.mjs.map +1 -0
  244. package/dist/ThreadProjectionMaintenance-D6fBjr1q.mjs +37 -0
  245. package/dist/ThreadProjectionMaintenance-D6fBjr1q.mjs.map +1 -0
  246. package/dist/ThreadProjectionMaintenance-DjUy7kCa.d.mts +51 -0
  247. package/dist/ThreadStore-BMUAp5Ta.mjs +937 -0
  248. package/dist/ThreadStore-BMUAp5Ta.mjs.map +1 -0
  249. package/dist/ThreadTransfer-B3NkyO_y.mjs +154 -0
  250. package/dist/ThreadTransfer-B3NkyO_y.mjs.map +1 -0
  251. package/dist/ThreadTransfer-BM02cbOo.d.mts +63 -0
  252. package/dist/ThreadWork-C-xa2Boi.mjs +484 -0
  253. package/dist/ThreadWork-C-xa2Boi.mjs.map +1 -0
  254. package/dist/ToolBroker-DkoVyx6T.mjs +42 -0
  255. package/dist/ToolBroker-DkoVyx6T.mjs.map +1 -0
  256. package/dist/ToolBroker-Dy2xM6ht.d.mts +134 -0
  257. package/dist/ToolDiscovery-DHySvLQd.mjs +211 -0
  258. package/dist/ToolDiscovery-DHySvLQd.mjs.map +1 -0
  259. package/dist/ToolDiscovery-VjamzX3q.d.mts +119 -0
  260. package/dist/ToolExposure-Cn7xETn5.d.mts +6 -0
  261. package/dist/ToolExposure-D7id9Gt5.d.mts +37 -0
  262. package/dist/ToolExposure-DCvQWFIa.mjs +9 -0
  263. package/dist/ToolExposure-DCvQWFIa.mjs.map +1 -0
  264. package/dist/ToolExposure-DNME8BkG.d.mts +44 -0
  265. package/dist/ToolExposure-DPnq8Won.mjs +30 -0
  266. package/dist/ToolExposure-DPnq8Won.mjs.map +1 -0
  267. package/dist/ToolExposure-DaGaF5tj.mjs +20 -0
  268. package/dist/ToolExposure-DaGaF5tj.mjs.map +1 -0
  269. package/dist/ToolExposure.d.mts +4 -0
  270. package/dist/ToolExposure.mjs +4 -0
  271. package/dist/ToolReconciler-6bb7xlip.d.mts +81 -0
  272. package/dist/ToolReconciler-DJQd3SZA.mjs +87 -0
  273. package/dist/ToolReconciler-DJQd3SZA.mjs.map +1 -0
  274. package/dist/ToolResult-By5KAje4.d.mts +79 -0
  275. package/dist/ToolResult-DwZxjlHw.mjs +160 -0
  276. package/dist/ToolResult-DwZxjlHw.mjs.map +1 -0
  277. package/dist/ToolResultStorage-BmoRewhh.mjs +46 -0
  278. package/dist/ToolResultStorage-BmoRewhh.mjs.map +1 -0
  279. package/dist/ToolResultStorage-Bp5fm0do.d.mts +49 -0
  280. package/dist/ToolResults-C4-HsMCp.d.mts +33 -0
  281. package/dist/ToolResults-nCgBlTp2.mjs +45 -0
  282. package/dist/ToolResults-nCgBlTp2.mjs.map +1 -0
  283. package/dist/Usage-B0xgME5f.mjs +340 -0
  284. package/dist/Usage-B0xgME5f.mjs.map +1 -0
  285. package/dist/Usage-Bc-jXe_9.d.mts +128 -0
  286. package/dist/WakeScheduler-DVYj66C-.mjs +74 -0
  287. package/dist/WakeScheduler-DVYj66C-.mjs.map +1 -0
  288. package/dist/WakeScheduler-SFqj67Sk.d.mts +74 -0
  289. package/dist/WebCapture-CA4ZarW2.mjs +64 -0
  290. package/dist/WebCapture-CA4ZarW2.mjs.map +1 -0
  291. package/dist/WebCapture-pzIeUJ5W.d.mts +49 -0
  292. package/dist/WebSearch-CD0V--qh.d.mts +18 -0
  293. package/dist/WebSearch-DnbOzRL_.mjs +30 -0
  294. package/dist/WebSearch-DnbOzRL_.mjs.map +1 -0
  295. package/dist/Worker-CQu8Qp1j.mjs +267 -0
  296. package/dist/Worker-CQu8Qp1j.mjs.map +1 -0
  297. package/dist/Worker-xxucY_0b.d.mts +468 -0
  298. package/dist/WorkerAdmission-BhFjx86s.mjs +162 -0
  299. package/dist/WorkerAdmission-BhFjx86s.mjs.map +1 -0
  300. package/dist/WorkerAdmission-BnRuXLWF.d.mts +171 -0
  301. package/dist/WorkerHost-DtYzi54D.d.mts +150 -0
  302. package/dist/WorkerHost-DuQug9Bq.mjs +73 -0
  303. package/dist/WorkerHost-DuQug9Bq.mjs.map +1 -0
  304. package/dist/agent-registration-CNs2twrr.mjs +262 -0
  305. package/dist/agent-registration-CNs2twrr.mjs.map +1 -0
  306. package/dist/capabilities/BrowserUse.d.mts +2 -0
  307. package/dist/capabilities/BrowserUse.mjs +1 -0
  308. package/dist/capabilities/Budget.d.mts +2 -0
  309. package/dist/capabilities/Budget.mjs +1 -0
  310. package/dist/capabilities/CodeMode.d.mts +2 -0
  311. package/dist/capabilities/CodeMode.mjs +1 -0
  312. package/dist/capabilities/ContextTools.d.mts +2 -0
  313. package/dist/capabilities/ContextTools.mjs +1 -0
  314. package/dist/capabilities/MemoryNotes.d.mts +2 -0
  315. package/dist/capabilities/MemoryNotes.mjs +1 -0
  316. package/dist/capabilities/Messaging.d.mts +3 -0
  317. package/dist/capabilities/Messaging.mjs +4 -0
  318. package/dist/capabilities/Redaction.d.mts +2 -0
  319. package/dist/capabilities/Redaction.mjs +1 -0
  320. package/dist/capabilities/RunHooks.d.mts +2 -0
  321. package/dist/capabilities/RunHooks.mjs +1 -0
  322. package/dist/capabilities/SemanticMemory.d.mts +2 -0
  323. package/dist/capabilities/SemanticMemory.mjs +1 -0
  324. package/dist/capabilities/Subagent.d.mts +4 -0
  325. package/dist/capabilities/Subagent.mjs +6 -0
  326. package/dist/capabilities/SubagentReservations.d.mts +2 -0
  327. package/dist/capabilities/SubagentReservations.mjs +1 -0
  328. package/dist/capabilities/ToolDiscovery.d.mts +2 -0
  329. package/dist/capabilities/ToolDiscovery.mjs +1 -0
  330. package/dist/capabilities/ToolResults.d.mts +2 -0
  331. package/dist/capabilities/ToolResults.mjs +1 -0
  332. package/dist/capabilities/WebCapture.d.mts +2 -0
  333. package/dist/capabilities/WebCapture.mjs +1 -0
  334. package/dist/capabilities/WebSearch.d.mts +2 -0
  335. package/dist/capabilities/WebSearch.mjs +1 -0
  336. package/dist/compaction-C3hXuO4Z.mjs +539 -0
  337. package/dist/compaction-C3hXuO4Z.mjs.map +1 -0
  338. package/dist/completion-B_WDnSg-.d.mts +11 -0
  339. package/dist/core/Agent.d.mts +2 -0
  340. package/dist/core/Agent.mjs +1 -0
  341. package/dist/core/AgentError.d.mts +2 -0
  342. package/dist/core/AgentError.mjs +1 -0
  343. package/dist/core/AgentPolicy.d.mts +2 -0
  344. package/dist/core/AgentPolicy.mjs +1 -0
  345. package/dist/core/AgentUpdates.d.mts +2 -0
  346. package/dist/core/AgentUpdates.mjs +1 -0
  347. package/dist/core/FailureDiagnostic.d.mts +2 -0
  348. package/dist/core/FailureDiagnostic.mjs +1 -0
  349. package/dist/core/IdGenerator.d.mts +2 -0
  350. package/dist/core/IdGenerator.mjs +1 -0
  351. package/dist/core/Identifiers.d.mts +2 -0
  352. package/dist/core/Identifiers.mjs +1 -0
  353. package/dist/core/Memory.d.mts +2 -0
  354. package/dist/core/Memory.mjs +1 -0
  355. package/dist/core/MemoryNamespace.d.mts +2 -0
  356. package/dist/core/MemoryNamespace.mjs +1 -0
  357. package/dist/core/MemoryReference.d.mts +2 -0
  358. package/dist/core/MemoryReference.mjs +1 -0
  359. package/dist/core/MemoryRevalidation.d.mts +2 -0
  360. package/dist/core/MemoryRevalidation.mjs +1 -0
  361. package/dist/core/MemoryStore.d.mts +2 -0
  362. package/dist/core/MemoryStore.mjs +1 -0
  363. package/dist/core/ProvisionalText.d.mts +2 -0
  364. package/dist/core/ProvisionalText.mjs +1 -0
  365. package/dist/core/Receipt.d.mts +2 -0
  366. package/dist/core/Receipt.mjs +1 -0
  367. package/dist/core/RememberingStore.d.mts +2 -0
  368. package/dist/core/RememberingStore.mjs +1 -0
  369. package/dist/core/RunEvent.d.mts +3 -0
  370. package/dist/core/RunEvent.mjs +1 -0
  371. package/dist/core/RunPolicyUsage.d.mts +2 -0
  372. package/dist/core/RunPolicyUsage.mjs +1 -0
  373. package/dist/core/SemanticMemoryIndex.d.mts +2 -0
  374. package/dist/core/SemanticMemoryIndex.mjs +1 -0
  375. package/dist/core/SemanticMemoryRevalidation.d.mts +2 -0
  376. package/dist/core/SemanticMemoryRevalidation.mjs +1 -0
  377. package/dist/core/SubagentContract.d.mts +2 -0
  378. package/dist/core/SubagentContract.mjs +1 -0
  379. package/dist/core/Thread.d.mts +2 -0
  380. package/dist/core/Thread.mjs +1 -0
  381. package/dist/core/ToolResult.d.mts +2 -0
  382. package/dist/core/ToolResult.mjs +1 -0
  383. package/dist/core/ToolResultStorage.d.mts +2 -0
  384. package/dist/core/ToolResultStorage.mjs +1 -0
  385. package/dist/core/Usage.d.mts +2 -0
  386. package/dist/core/Usage.mjs +1 -0
  387. package/dist/core/Worker.d.mts +3 -0
  388. package/dist/core/Worker.mjs +3 -0
  389. package/dist/durable/ActivityStore.d.mts +2 -0
  390. package/dist/durable/ActivityStore.mjs +1 -0
  391. package/dist/durable/Admin.d.mts +2 -0
  392. package/dist/durable/Admin.mjs +1 -0
  393. package/dist/durable/AgentRegistration.d.mts +3 -0
  394. package/dist/durable/AgentRegistration.mjs +2 -0
  395. package/dist/durable/CommittedActivity.d.mts +2 -0
  396. package/dist/durable/CommittedActivity.mjs +1 -0
  397. package/dist/durable/Digest.d.mts +2 -0
  398. package/dist/durable/Digest.mjs +3 -0
  399. package/dist/durable/DurableAgentRuntime.d.mts +3 -0
  400. package/dist/durable/DurableAgentRuntime.mjs +4 -0
  401. package/dist/durable/DurableFailpoint.d.mts +2 -0
  402. package/dist/durable/DurableFailpoint.mjs +1 -0
  403. package/dist/durable/DurableFailpointTestControl.d.mts +14 -0
  404. package/dist/durable/DurableFailpointTestControl.mjs +21 -0
  405. package/dist/durable/DurableFailpointTestControl.mjs.map +1 -0
  406. package/dist/durable/EventSource.d.mts +2 -0
  407. package/dist/durable/EventSource.mjs +1 -0
  408. package/dist/durable/LifecyclePublication.d.mts +2 -0
  409. package/dist/durable/LifecyclePublication.mjs +1 -0
  410. package/dist/durable/MessageDelivery.d.mts +2 -0
  411. package/dist/durable/MessageDelivery.mjs +1 -0
  412. package/dist/durable/MessageDeliveryStoreConformance.d.mts +401 -0
  413. package/dist/durable/MessageDeliveryStoreConformance.mjs +273 -0
  414. package/dist/durable/MessageDeliveryStoreConformance.mjs.map +1 -0
  415. package/dist/durable/OperationAuthorizer.d.mts +2 -0
  416. package/dist/durable/OperationAuthorizer.mjs +1 -0
  417. package/dist/durable/PersistentHistory.d.mts +2 -0
  418. package/dist/durable/PersistentHistory.mjs +1 -0
  419. package/dist/durable/PreparedInputAdmission.d.mts +2 -0
  420. package/dist/durable/PreparedInputAdmission.mjs +1 -0
  421. package/dist/durable/RecordFormat.d.mts +2 -0
  422. package/dist/durable/RecordFormat.mjs +1 -0
  423. package/dist/durable/Records.d.mts +2 -0
  424. package/dist/durable/Records.mjs +1 -0
  425. package/dist/durable/Recovery.d.mts +2 -0
  426. package/dist/durable/Recovery.mjs +1 -0
  427. package/dist/durable/RunContinuation.d.mts +4 -0
  428. package/dist/durable/RunContinuation.mjs +4 -0
  429. package/dist/durable/RunJournal.d.mts +2 -0
  430. package/dist/durable/RunJournal.mjs +1 -0
  431. package/dist/durable/RunStorage.d.mts +2 -0
  432. package/dist/durable/RunStorage.mjs +1 -0
  433. package/dist/durable/Schedule.d.mts +2 -0
  434. package/dist/durable/Schedule.mjs +1 -0
  435. package/dist/durable/ScheduleStoreConformance.d.mts +16 -0
  436. package/dist/durable/ScheduleStoreConformance.mjs +421 -0
  437. package/dist/durable/ScheduleStoreConformance.mjs.map +1 -0
  438. package/dist/durable/ScheduleTransition.d.mts +2 -0
  439. package/dist/durable/ScheduleTransition.mjs +1 -0
  440. package/dist/durable/Scheduling.d.mts +2 -0
  441. package/dist/durable/Scheduling.mjs +1 -0
  442. package/dist/durable/SettlementPublisher.d.mts +2 -0
  443. package/dist/durable/SettlementPublisher.mjs +99 -0
  444. package/dist/durable/SettlementPublisher.mjs.map +1 -0
  445. package/dist/durable/SqlMemoryStore.d.mts +729 -0
  446. package/dist/durable/SqlMemoryStore.mjs +626 -0
  447. package/dist/durable/SqlMemoryStore.mjs.map +1 -0
  448. package/dist/durable/SqlStorageOwner.d.mts +18 -0
  449. package/dist/durable/SqlStorageOwner.mjs +6 -0
  450. package/dist/durable/SqlStorageOwner.mjs.map +1 -0
  451. package/dist/durable/SubmissionLedger.d.mts +3 -0
  452. package/dist/durable/SubmissionLedger.mjs +3 -0
  453. package/dist/durable/SubmissionLedgerConformance.d.mts +31 -0
  454. package/dist/durable/SubmissionLedgerConformance.mjs +2090 -0
  455. package/dist/durable/SubmissionLedgerConformance.mjs.map +1 -0
  456. package/dist/durable/SubmissionStatus.d.mts +2 -0
  457. package/dist/durable/SubmissionStatus.mjs +1 -0
  458. package/dist/durable/Subscription.d.mts +2 -0
  459. package/dist/durable/Subscription.mjs +1 -0
  460. package/dist/durable/SubscriptionInput.d.mts +2 -0
  461. package/dist/durable/SubscriptionInput.mjs +1 -0
  462. package/dist/durable/SubscriptionStoreConformance.d.mts +15 -0
  463. package/dist/durable/SubscriptionStoreConformance.mjs +816 -0
  464. package/dist/durable/SubscriptionStoreConformance.mjs.map +1 -0
  465. package/dist/durable/SubscriptionTransition.d.mts +2 -0
  466. package/dist/durable/SubscriptionTransition.mjs +1 -0
  467. package/dist/durable/Subscriptions.d.mts +2 -0
  468. package/dist/durable/Subscriptions.mjs +1 -0
  469. package/dist/durable/ThreadArchiveRange.d.mts +2 -0
  470. package/dist/durable/ThreadArchiveRange.mjs +1 -0
  471. package/dist/durable/ThreadContextHistory.d.mts +2 -0
  472. package/dist/durable/ThreadContextHistory.mjs +1 -0
  473. package/dist/durable/ThreadContextHistoryProjection.d.mts +2 -0
  474. package/dist/durable/ThreadContextHistoryProjection.mjs +1 -0
  475. package/dist/durable/ThreadImport.d.mts +2 -0
  476. package/dist/durable/ThreadImport.mjs +1 -0
  477. package/dist/durable/ThreadInvariants.d.mts +2 -0
  478. package/dist/durable/ThreadInvariants.mjs +1 -0
  479. package/dist/durable/ThreadProjectionMaintenance.d.mts +2 -0
  480. package/dist/durable/ThreadProjectionMaintenance.mjs +1 -0
  481. package/dist/durable/ThreadReader.d.mts +2 -0
  482. package/dist/durable/ThreadReader.mjs +1 -0
  483. package/dist/durable/ThreadStore.d.mts +3 -0
  484. package/dist/durable/ThreadStore.mjs +3 -0
  485. package/dist/durable/ThreadStoreConformance.d.mts +53 -0
  486. package/dist/durable/ThreadStoreConformance.mjs +402 -0
  487. package/dist/durable/ThreadStoreConformance.mjs.map +1 -0
  488. package/dist/durable/ThreadTransfer.d.mts +2 -0
  489. package/dist/durable/ThreadTransfer.mjs +1 -0
  490. package/dist/durable/ThreadWork.d.mts +2 -0
  491. package/dist/durable/ThreadWork.mjs +1 -0
  492. package/dist/durable/ToolReconciler.d.mts +2 -0
  493. package/dist/durable/ToolReconciler.mjs +1 -0
  494. package/dist/durable/WakeScheduler.d.mts +2 -0
  495. package/dist/durable/WakeScheduler.mjs +1 -0
  496. package/dist/durable/WorkerAdmission.d.mts +2 -0
  497. package/dist/durable/WorkerAdmission.mjs +1 -0
  498. package/dist/durable/WorkerHost.d.mts +2 -0
  499. package/dist/durable/WorkerHost.mjs +1 -0
  500. package/dist/engine/AgentRuntime.d.mts +2 -0
  501. package/dist/engine/AgentRuntime.mjs +1 -0
  502. package/dist/engine/Compaction.d.mts +4 -0
  503. package/dist/engine/Compaction.mjs +2 -0
  504. package/dist/engine/ContextCompactor.d.mts +2 -0
  505. package/dist/engine/ContextCompactor.mjs +1 -0
  506. package/dist/engine/ContextHistory.d.mts +2 -0
  507. package/dist/engine/ContextHistory.mjs +1 -0
  508. package/dist/engine/ContextWindow.d.mts +2 -0
  509. package/dist/engine/ContextWindow.mjs +1 -0
  510. package/dist/engine/DurableStep.d.mts +2 -0
  511. package/dist/engine/DurableStep.mjs +3 -0
  512. package/dist/engine/MessagingHost.d.mts +2 -0
  513. package/dist/engine/MessagingHost.mjs +1 -0
  514. package/dist/engine/Output.d.mts +3 -0
  515. package/dist/engine/Output.mjs +2 -0
  516. package/dist/engine/RunEventSink.d.mts +2 -0
  517. package/dist/engine/RunEventSink.mjs +1 -0
  518. package/dist/engine/RunOptions.d.mts +2 -0
  519. package/dist/engine/RunOptions.mjs +1 -0
  520. package/dist/engine/SubagentHost.d.mts +2 -0
  521. package/dist/engine/SubagentHost.mjs +1 -0
  522. package/dist/engine/ThreadHistory.d.mts +2 -0
  523. package/dist/engine/ThreadHistory.mjs +1 -0
  524. package/dist/engine/ToolBroker.d.mts +2 -0
  525. package/dist/engine/ToolBroker.mjs +1 -0
  526. package/dist/index.d.mts +90 -0
  527. package/dist/index.mjs +101 -0
  528. package/dist/output-contract-CYA4oBhe.mjs +128 -0
  529. package/dist/output-contract-CYA4oBhe.mjs.map +1 -0
  530. package/dist/rolldown-runtime-D3KUYPsf.mjs +12 -0
  531. package/dist/run-context-reader-CHox63hg.d.mts +17 -0
  532. package/dist/sandbox/CodeExecutor.d.mts +2 -0
  533. package/dist/sandbox/CodeExecutor.mjs +1 -0
  534. package/dist/sandbox/InteractiveBrowser.d.mts +2 -0
  535. package/dist/sandbox/InteractiveBrowser.mjs +1 -0
  536. package/dist/sandbox/PageCapture.d.mts +2 -0
  537. package/dist/sandbox/PageCapture.mjs +1 -0
  538. package/dist/sandbox/PageScreenshot.d.mts +2 -0
  539. package/dist/sandbox/PageScreenshot.mjs +1 -0
  540. package/dist/sandbox/Sandbox.d.mts +2 -0
  541. package/dist/sandbox/Sandbox.mjs +1 -0
  542. package/dist/subagent-policy-xRJhrc0a.mjs +133 -0
  543. package/dist/subagent-policy-xRJhrc0a.mjs.map +1 -0
  544. package/dist/tool-execution-Cl7v8fXU.mjs +16 -0
  545. package/dist/tool-execution-Cl7v8fXU.mjs.map +1 -0
  546. package/dist/tool-operations-CauPonM8.mjs +84 -0
  547. package/dist/tool-operations-CauPonM8.mjs.map +1 -0
  548. package/dist/utf8-Bl0ZhhGe.mjs +27 -0
  549. package/dist/utf8-Bl0ZhhGe.mjs.map +1 -0
  550. package/package.json +491 -3
  551. package/src/InMemory.ts +79 -0
  552. package/src/ToolExposure.ts +2 -0
  553. package/src/capabilities/BrowserUse.ts +212 -0
  554. package/src/capabilities/Budget.ts +544 -0
  555. package/src/capabilities/CodeMode.ts +1129 -0
  556. package/src/capabilities/ContextTools.ts +131 -0
  557. package/src/capabilities/MemoryNotes.ts +177 -0
  558. package/src/capabilities/Messaging.ts +260 -0
  559. package/src/capabilities/Redaction.ts +163 -0
  560. package/src/capabilities/RunHooks.ts +116 -0
  561. package/src/capabilities/SemanticMemory.ts +402 -0
  562. package/src/capabilities/Subagent.ts +2343 -0
  563. package/src/capabilities/SubagentReservations.ts +807 -0
  564. package/src/capabilities/ToolDiscovery.ts +291 -0
  565. package/src/capabilities/ToolResults.ts +50 -0
  566. package/src/capabilities/WebCapture.ts +64 -0
  567. package/src/capabilities/WebSearch.ts +45 -0
  568. package/src/capabilities/internal/subagent-background.ts +1137 -0
  569. package/src/capabilities/internal/subagent-contract.ts +92 -0
  570. package/src/capabilities/internal/subagent-policy.ts +205 -0
  571. package/src/capabilities/internal/subagent-reporting.ts +152 -0
  572. package/src/core/Agent.ts +528 -0
  573. package/src/core/AgentError.ts +189 -0
  574. package/src/core/AgentPolicy.ts +194 -0
  575. package/src/core/AgentUpdates.ts +104 -0
  576. package/src/core/FailureDiagnostic.ts +392 -0
  577. package/src/core/IdGenerator.ts +42 -0
  578. package/src/core/Identifiers.ts +64 -0
  579. package/src/core/Memory.ts +283 -0
  580. package/src/core/MemoryNamespace.ts +202 -0
  581. package/src/core/MemoryReference.ts +120 -0
  582. package/src/core/MemoryRevalidation.ts +151 -0
  583. package/src/core/MemoryStore.ts +371 -0
  584. package/src/core/Messaging.ts +72 -0
  585. package/src/core/ProvisionalText.ts +53 -0
  586. package/src/core/Receipt.ts +42 -0
  587. package/src/core/RememberingStore.ts +22 -0
  588. package/src/core/RunEvent.ts +345 -0
  589. package/src/core/RunPolicyUsage.ts +19 -0
  590. package/src/core/SemanticMemoryIndex.ts +259 -0
  591. package/src/core/SemanticMemoryRevalidation.ts +220 -0
  592. package/src/core/SubagentContract.ts +263 -0
  593. package/src/core/Thread.ts +447 -0
  594. package/src/core/ToolExposure.ts +75 -0
  595. package/src/core/ToolResult.ts +204 -0
  596. package/src/core/ToolResultStorage.ts +56 -0
  597. package/src/core/Usage.ts +562 -0
  598. package/src/core/Worker.ts +236 -0
  599. package/src/core/internal/agent-updates.ts +42 -0
  600. package/src/core/internal/completion.ts +33 -0
  601. package/src/core/internal/json.ts +17 -0
  602. package/src/core/internal/memory-document.ts +39 -0
  603. package/src/core/internal/message-status.ts +60 -0
  604. package/src/core/internal/strict-schema.ts +14 -0
  605. package/src/core/internal/tool-execution.ts +33 -0
  606. package/src/core/internal/utf8.ts +32 -0
  607. package/src/durable/ActivityStore.ts +131 -0
  608. package/src/durable/Admin.ts +504 -0
  609. package/src/durable/AgentRegistration.ts +16 -0
  610. package/src/durable/CommittedActivity.ts +262 -0
  611. package/src/durable/Digest.ts +57 -0
  612. package/src/durable/DurableAgentRuntime.ts +12758 -0
  613. package/src/durable/DurableFailpoint.ts +108 -0
  614. package/src/durable/DurableFailpointTestControl.ts +39 -0
  615. package/src/durable/EventSource.ts +179 -0
  616. package/src/durable/LifecyclePublication.ts +343 -0
  617. package/src/durable/MessageDelivery.ts +949 -0
  618. package/src/durable/MessageDeliveryStoreConformance.ts +407 -0
  619. package/src/durable/OperationAuthorizer.ts +89 -0
  620. package/src/durable/PersistentHistory.ts +322 -0
  621. package/src/durable/PreparedInputAdmission.ts +128 -0
  622. package/src/durable/RecordFormat.ts +113 -0
  623. package/src/durable/Records.ts +1396 -0
  624. package/src/durable/Recovery.ts +1022 -0
  625. package/src/durable/RunContinuation.ts +1946 -0
  626. package/src/durable/RunJournal.ts +1926 -0
  627. package/src/durable/RunStorage.ts +533 -0
  628. package/src/durable/Schedule.ts +445 -0
  629. package/src/durable/ScheduleStoreConformance.ts +642 -0
  630. package/src/durable/ScheduleTransition.ts +267 -0
  631. package/src/durable/Scheduling.ts +1031 -0
  632. package/src/durable/SettlementPublisher.ts +172 -0
  633. package/src/durable/SqlMemoryStore.ts +1042 -0
  634. package/src/durable/SqlStorageOwner.ts +25 -0
  635. package/src/durable/SubmissionLedger.ts +1247 -0
  636. package/src/durable/SubmissionLedgerConformance.ts +4695 -0
  637. package/src/durable/SubmissionStatus.ts +14 -0
  638. package/src/durable/Subscription.ts +527 -0
  639. package/src/durable/SubscriptionInput.ts +145 -0
  640. package/src/durable/SubscriptionStoreConformance.ts +1275 -0
  641. package/src/durable/SubscriptionTransition.ts +330 -0
  642. package/src/durable/Subscriptions.ts +1245 -0
  643. package/src/durable/ThreadArchiveRange.ts +91 -0
  644. package/src/durable/ThreadContextHistory.ts +338 -0
  645. package/src/durable/ThreadContextHistoryProjection.ts +219 -0
  646. package/src/durable/ThreadImport.ts +1199 -0
  647. package/src/durable/ThreadInvariants.ts +312 -0
  648. package/src/durable/ThreadProjectionMaintenance.ts +72 -0
  649. package/src/durable/ThreadReader.ts +21 -0
  650. package/src/durable/ThreadStore.ts +1258 -0
  651. package/src/durable/ThreadStoreConformance.ts +877 -0
  652. package/src/durable/ThreadTransfer.ts +263 -0
  653. package/src/durable/ThreadWork.ts +713 -0
  654. package/src/durable/ToolReconciler.ts +112 -0
  655. package/src/durable/WakeScheduler.ts +125 -0
  656. package/src/durable/WorkerAdmission.ts +98 -0
  657. package/src/durable/WorkerHost.ts +204 -0
  658. package/src/durable/internal/StorageOwner.ts +181 -0
  659. package/src/durable/internal/agent-registration.ts +713 -0
  660. package/src/durable/internal/agent-updates.ts +338 -0
  661. package/src/durable/internal/canonical-json.ts +110 -0
  662. package/src/durable/internal/evidence.ts +29 -0
  663. package/src/durable/internal/foreign-diagnostic.ts +48 -0
  664. package/src/durable/internal/history-prompt.ts +87 -0
  665. package/src/durable/internal/initial-context.ts +215 -0
  666. package/src/durable/internal/journal-metadata.ts +188 -0
  667. package/src/durable/internal/message-status.ts +18 -0
  668. package/src/durable/internal/messaging-host.ts +615 -0
  669. package/src/durable/internal/prompt-footprint.ts +47 -0
  670. package/src/durable/internal/prompt-history-cache.ts +604 -0
  671. package/src/durable/internal/record-encoding.ts +258 -0
  672. package/src/durable/internal/record-ownership.ts +68 -0
  673. package/src/durable/internal/run-context-reader.ts +51 -0
  674. package/src/durable/internal/run-context.ts +211 -0
  675. package/src/durable/internal/schedule-time.ts +304 -0
  676. package/src/durable/internal/thread-import-reader.ts +85 -0
  677. package/src/durable/internal/thread-initialization.ts +181 -0
  678. package/src/durable/internal/thread-transfer-source.ts +7 -0
  679. package/src/durable/internal/tool-operations.ts +138 -0
  680. package/src/durable/internal/transfer-dependencies.ts +40 -0
  681. package/src/durable/internal/usage-totals.ts +36 -0
  682. package/src/durable/internal/worker-host.ts +4041 -0
  683. package/src/durable/internal/worker-runtime.ts +10 -0
  684. package/src/engine/AgentRuntime.ts +100 -0
  685. package/src/engine/Compaction.ts +11 -0
  686. package/src/engine/ContextCompactor.ts +302 -0
  687. package/src/engine/ContextHistory.ts +75 -0
  688. package/src/engine/ContextWindow.ts +81 -0
  689. package/src/engine/DurableStep.ts +115 -0
  690. package/src/engine/MessagingHost.ts +118 -0
  691. package/src/engine/Output.ts +2 -0
  692. package/src/engine/RunEventSink.ts +124 -0
  693. package/src/engine/RunOptions.ts +664 -0
  694. package/src/engine/SubagentHost.ts +231 -0
  695. package/src/engine/ThreadHistory.ts +125 -0
  696. package/src/engine/ToolBroker.ts +159 -0
  697. package/src/engine/ToolExposure.ts +49 -0
  698. package/src/engine/internal/agent-runtime.ts +11137 -0
  699. package/src/engine/internal/bounded-value.ts +312 -0
  700. package/src/engine/internal/compaction.ts +673 -0
  701. package/src/engine/internal/error-diagnostic.ts +39 -0
  702. package/src/engine/internal/image-tokens.ts +61 -0
  703. package/src/engine/internal/interpreter-options.ts +569 -0
  704. package/src/engine/internal/output-contract.ts +226 -0
  705. package/src/engine/internal/primitive-delta.ts +103 -0
  706. package/src/engine/internal/provider-result-staging.ts +211 -0
  707. package/src/engine/internal/provisional-text.ts +107 -0
  708. package/src/engine/internal/tool-derivative.ts +47 -0
  709. package/src/engine/internal/tool-exposure.ts +228 -0
  710. package/src/engine/internal/tool-telemetry.ts +488 -0
  711. package/src/index.ts +100 -0
  712. package/src/sandbox/CodeExecutor.ts +352 -0
  713. package/src/sandbox/InteractiveBrowser.ts +343 -0
  714. package/src/sandbox/PageCapture.ts +391 -0
  715. package/src/sandbox/PageScreenshot.ts +83 -0
  716. package/src/sandbox/Sandbox.ts +29 -0
  717. package/README.md +0 -3
@@ -0,0 +1,1199 @@
1
+ import * as Context from 'effect/Context'
2
+ import * as Effect from 'effect/Effect'
3
+ import * as Pull from 'effect/Pull'
4
+ import * as Schema from 'effect/Schema'
5
+ import type * as Scope from 'effect/Scope'
6
+ import * as Stream from 'effect/Stream'
7
+
8
+ import { InputMessage } from '../capabilities/Messaging'
9
+ import type { RunId } from '../core/Identifiers'
10
+ import { SubmissionId, ThreadId, ToolCallId } from '../core/Identifiers'
11
+ import { WorkerRef } from '../core/Worker'
12
+ import { canonicalJson, digestJson, utf8ByteLength } from './Digest'
13
+ import { readRunContext, validateContextBoundary } from './internal/run-context'
14
+ import { ThreadImportReader } from './internal/thread-import-reader'
15
+ import { ImportSourcePhase } from './internal/thread-transfer-source'
16
+ import { toolOperationStates } from './internal/tool-operations'
17
+ import {
18
+ MessageDeliveryRecord,
19
+ prepareCanonicalMessageDeliveries,
20
+ sameMessageDeliveryIdentity,
21
+ } from './MessageDelivery'
22
+ import { decodeExportRecord } from './RecordFormat'
23
+ import {
24
+ CanonicalBatch,
25
+ CanonicalRecordEnvelope,
26
+ CanonicalSequence,
27
+ CURRENT_RECORD_FORMAT,
28
+ RecordEnvelope,
29
+ RecordJson,
30
+ SubtreeBudgetReserved,
31
+ } from './Records'
32
+ import { executionRunIds, isOperationalRecord, isPreContinuationFact, validateSuffix } from './RunContinuation'
33
+ import { runIdForSubmission } from './RunJournal'
34
+ import { validateCanonicalSettlement, validateJoinedSettlement } from './SettlementPublisher'
35
+ import {
36
+ AbortIntent,
37
+ ApprovalDecisionIntent,
38
+ ApprovalPendingSuspension,
39
+ InputAppliedMarker,
40
+ submissionAbortRecordId,
41
+ submissionInputRecordId,
42
+ submissionSettlementRecordId,
43
+ SubmissionState,
44
+ SuspensionSnapshot,
45
+ UnknownResolutionIntent,
46
+ unknownResolutionKind,
47
+ } from './SubmissionLedger'
48
+ import {
49
+ type ExecutionCheckpoint,
50
+ makeTransferFactCheck,
51
+ MAX_THREAD_EXPORT_PAGE_BYTES,
52
+ MAX_THREAD_EXPORT_PAGE_RECORDS,
53
+ ThreadAbortFact,
54
+ ThreadAdmission,
55
+ ThreadApprovalFact,
56
+ ThreadExport,
57
+ ThreadResolutionFact,
58
+ type ThreadStoreError,
59
+ transferPageFits,
60
+ } from './ThreadStore'
61
+ import { sameTransferManifest, transferDependencies, transferSections } from './ThreadTransfer'
62
+ import { workIndexChanges } from './ThreadWork'
63
+
64
+ export { ThreadDeliveryImportReader, ThreadImportReader } from './internal/thread-import-reader'
65
+
66
+ const sameInputMessage = Schema.toEquivalence(InputMessage)
67
+
68
+ /** One owned wire page. Canonical batches precede admissions; all admissions precede accepted commands.
69
+ * Every fact section is required, including empty sections. This order permits indexed atomic staging. */
70
+ export const ThreadArchive = Schema.Struct({
71
+ ...ThreadExport.fields,
72
+ records: Schema.Array(
73
+ Schema.Struct({ ...CanonicalRecordEnvelope.fields, record: RecordJson }),
74
+ ).check(Schema.isMaxLength(MAX_THREAD_EXPORT_PAGE_RECORDS)),
75
+ })
76
+
77
+ export type ThreadArchive = typeof ThreadArchive.Type
78
+
79
+ const admissionFitsTransfer = makeTransferFactCheck(ThreadAdmission)
80
+ const abortFitsTransfer = makeTransferFactCheck(ThreadAbortFact)
81
+ const approvalFitsTransfer = makeTransferFactCheck(ThreadApprovalFact)
82
+ const resolutionFitsTransfer = makeTransferFactCheck(ThreadResolutionFact)
83
+ const deliveryFitsTransfer = makeTransferFactCheck(MessageDeliveryRecord)
84
+
85
+ export class ThreadImportResult extends Schema.Class<ThreadImportResult>(
86
+ '@effect-agent/thread/ThreadImportResult',
87
+ )({
88
+ threadId: ThreadId,
89
+ format: Schema.String,
90
+ tailSequence: CanonicalSequence,
91
+ recordCount: Schema.Natural,
92
+ submissionCount: Schema.Natural,
93
+ }) {}
94
+
95
+ export class ThreadImportRejected extends Schema.TaggedError<ThreadImportRejected>()(
96
+ 'ThreadImportRejected',
97
+ {
98
+ threadId: Schema.optionalKey(ThreadId),
99
+ reason: Schema.Literals([
100
+ 'unsupported-format',
101
+ 'invalid-archive',
102
+ 'target-not-empty',
103
+ 'unsupported-obligations',
104
+ 'destination-conflict',
105
+ 'admission-policy-conflict',
106
+ 'admission-policy-unavailable',
107
+ 'unsupported-capacity',
108
+ ]),
109
+ message: Schema.String,
110
+ },
111
+ ) {}
112
+
113
+ /** One owner transaction consumes the stream; E and R are preserved, including rollback on interruption. */
114
+ export class ThreadImport extends Context.Service<
115
+ ThreadImport,
116
+ {
117
+ readonly import: <E, R>(
118
+ pages: Stream.Stream<ThreadArchive, E, R>,
119
+ ) => Effect.Effect<ThreadImportResult, E | ThreadImportRejected | ThreadStoreError, R>
120
+ }
121
+ >()('@effect-agent/thread/ThreadImport') {}
122
+
123
+ export class RebuiltSubmission extends Schema.Class<RebuiltSubmission>(
124
+ '@effect-agent/thread/RebuiltSubmission',
125
+ )({
126
+ admission: ThreadAdmission,
127
+ state: SubmissionState,
128
+ inputApplied: Schema.optionalKey(InputAppliedMarker),
129
+ joinedHostSubmissionId: Schema.optionalKey(SubmissionId),
130
+ settlement: Schema.optionalKey(RecordEnvelope),
131
+ suspension: Schema.optionalKey(SuspensionSnapshot),
132
+ unknownToolCallIds: Schema.Array(ToolCallId),
133
+ abort: Schema.optionalKey(AbortIntent),
134
+ approvals: Schema.Array(ApprovalDecisionIntent),
135
+ resolutions: Schema.Array(UnknownResolutionIntent),
136
+ }) {}
137
+
138
+ export interface PreparedImportBatch {
139
+ readonly batch: CanonicalBatch
140
+ readonly batchJson: string
141
+ readonly recordJson: ReadonlyArray<string>
142
+ readonly firstSequence: CanonicalSequence
143
+ readonly lastSequence: CanonicalSequence
144
+ }
145
+
146
+ export interface PreparedImportPage {
147
+ readonly archive: ThreadArchive
148
+ readonly records: ReadonlyArray<CanonicalRecordEnvelope>
149
+ readonly batches: ReadonlyArray<PreparedImportBatch>
150
+ }
151
+
152
+ /** Resolve every declared immutable reference, even in a preparation without a continuation. */
153
+ export const verifyImportedReferences = Effect.fnUntraced(function*(
154
+ entry: CanonicalRecordEnvelope,
155
+ ) {
156
+ const reader = yield* ThreadImportReader
157
+ const resolve = reader.record
158
+ const payload = entry.record.payload
159
+
160
+ const deliveries = yield* prepareCanonicalMessageDeliveries(entry.threadId, [entry.record]).pipe(
161
+ Effect.mapError(() => invalid('Invalid canonical delivery', entry.threadId)),
162
+ )
163
+
164
+ for (const expected of deliveries) {
165
+ const retained = yield* reader.delivery(expected.key.messageId)
166
+
167
+ if (retained === undefined || !sameMessageDeliveryIdentity(retained, expected)) {
168
+ return yield* invalid('Canonical delivery has no matching outbox row', entry.threadId)
169
+ }
170
+ }
171
+
172
+ if (payload._tag === 'WorkHandoffCompleted') {
173
+ const preparation = yield* resolve(payload.preparationId)
174
+
175
+ if (
176
+ preparation === undefined ||
177
+ preparation.threadId !== entry.threadId ||
178
+ preparation.sequence >= entry.sequence ||
179
+ entry.record.recordId !== `work-handoff-completed:${payload.preparationId}`
180
+ ) {
181
+ return yield* invalid('Missing or ambiguous handoff preparation', entry.threadId)
182
+ }
183
+ const changes = yield* workIndexChanges(preparation, 'none')
184
+
185
+ if (
186
+ !changes.some(
187
+ (change) =>
188
+ change._tag === 'Put' &&
189
+ change.entry.owner._tag === 'Handoff' &&
190
+ change.entry.id === payload.ownerId,
191
+ )
192
+ ) {
193
+ return yield* invalid(
194
+ "Handoff closure does not identify its preparation's exact owner",
195
+ entry.threadId,
196
+ )
197
+ }
198
+ }
199
+
200
+ if (payload._tag === 'RunContextRecorded') {
201
+ const owner = yield* reader.runOwner(payload.runId)
202
+
203
+ const original = owner === undefined ? undefined : yield* resolve(submissionInputRecordId(owner.submissionId))
204
+
205
+ if (
206
+ original === undefined ||
207
+ original.threadId !== entry.threadId ||
208
+ original.sequence >= entry.sequence
209
+ ) {
210
+ return yield* invalid('Saved context has no original admitted input', entry.threadId)
211
+ }
212
+ yield* validateContextBoundary(payload, original).pipe(
213
+ Effect.mapError((error) => invalid(error.message, entry.threadId)),
214
+ )
215
+ }
216
+
217
+ const references = payload._tag === 'WorkerInputRefused'
218
+ ? [payload.reservation, payload.stop]
219
+ : payload._tag === 'RunContextRecorded'
220
+ ? payload.retained
221
+ : payload._tag === 'RunContinuation'
222
+ ? [
223
+ payload.originalInput,
224
+ payload.savedContext,
225
+ payload.latestResponse,
226
+ payload.terminal,
227
+ payload.lastFact,
228
+ ].filter((ref) => ref !== undefined)
229
+ : []
230
+
231
+ for (const ref of references) {
232
+ const evidence = yield* resolve(ref.recordId)
233
+
234
+ if (
235
+ evidence === undefined ||
236
+ evidence.threadId !== entry.threadId ||
237
+ evidence.sequence >= entry.sequence ||
238
+ ('sequence' in ref && evidence.sequence !== ref.sequence)
239
+ ) {
240
+ return yield* invalid('Missing, foreign or forward canonical dependency', entry.threadId)
241
+ }
242
+ }
243
+ if (payload._tag === 'WorkerInputRefused') {
244
+ const request = (yield* resolve(payload.reservation.recordId))!.record.payload
245
+ const stop = (yield* resolve(payload.stop.recordId))!.record.payload
246
+ const retained = yield* reader.delivery(payload.messageId)
247
+
248
+ if (
249
+ retained === undefined ||
250
+ retained.key.ownerThreadId !== entry.threadId ||
251
+ retained.key.messageId !== payload.messageId ||
252
+ retained.status !== 'refused' ||
253
+ retained.receipt !== null ||
254
+ retained.settlement !== null ||
255
+ retained.version !== payload.deliveryVersion ||
256
+ retained.envelopeDigest !== payload.envelopeDigest ||
257
+ retained.envelope.threadId !== payload.workerThreadId ||
258
+ retained.envelope.deliveryPrincipal !== payload.receiver.principal ||
259
+ retained.envelope.admissionKey !== payload.receiver.idempotencyKey ||
260
+ retained.envelope.inputDigest !== payload.receiver.inputDigest ||
261
+ retained.envelope.workerAdmission === undefined ||
262
+ retained.envelope.workerAdmission.messageId !== payload.messageId ||
263
+ retained.envelope.workerAdmission.origin.source.threadId !== entry.threadId ||
264
+ stop._tag !== 'WorkerStopRequested' ||
265
+ !Schema.toEquivalence(WorkerRef)(
266
+ stop.command.worker,
267
+ retained.envelope.workerAdmission.origin.worker,
268
+ )
269
+ ) {
270
+ return yield* invalid(
271
+ 'Worker refusal has no exact refused delivery and source stop proof',
272
+ entry.threadId,
273
+ )
274
+ }
275
+ if (request._tag === 'WorkerInputRequested') {
276
+ if (
277
+ request.inputDigest !== retained.envelope.inputDigest ||
278
+ !Schema.toEquivalence(MessageDeliveryRecord.fields.envelope.fields.workerAdmission)(
279
+ request.admission,
280
+ retained.envelope.workerAdmission,
281
+ )
282
+ ) {
283
+ return yield* invalid(
284
+ 'Worker refusal reservation differs from its exact request',
285
+ entry.threadId,
286
+ )
287
+ }
288
+ } else if (
289
+ request._tag !== 'SubtreeBudgetReserved' ||
290
+ request.childThreadId !== payload.workerThreadId ||
291
+ request.reservationId !== payload.messageId ||
292
+ request.lifetime !== 'background' ||
293
+ request.executionRunId !== retained.envelope.workerAdmission.executionRunId ||
294
+ request.sourceSubmissionId !== retained.envelope.workerAdmission.sourceSubmissionId ||
295
+ request.depth !== retained.envelope.workerAdmission.origin.depth ||
296
+ !Schema.toEquivalence(SubtreeBudgetReserved.fields.policy)(
297
+ request.policy,
298
+ retained.envelope.workerAdmission.origin.policy,
299
+ ) ||
300
+ !Schema.toEquivalence(SubtreeBudgetReserved.fields.grant)(
301
+ request.grant,
302
+ retained.envelope.workerAdmission.origin.grant,
303
+ ) ||
304
+ !Schema.toEquivalence(SubtreeBudgetReserved.fields.budget)(
305
+ request.budget,
306
+ retained.envelope.workerAdmission.origin.budget,
307
+ )
308
+ ) {
309
+ return yield* invalid('Worker refusal has no exact source reservation', entry.threadId)
310
+ }
311
+ }
312
+ })
313
+
314
+ const sequence = Schema.decodeSync(CanonicalSequence)
315
+
316
+ const digest = (value: Schema.Json) =>
317
+ digestJson(value).pipe(Effect.mapError(() => invalid('Unable to digest imported facts')))
318
+
319
+ export const invalidThreadArchive = (message: string, threadId?: ThreadId) =>
320
+ ThreadImportRejected.make({
321
+ reason: 'invalid-archive',
322
+ message,
323
+ ...(threadId === undefined ? {} : { threadId }),
324
+ })
325
+
326
+ const invalid = invalidThreadArchive
327
+
328
+ /** Validate authoritative active state against the same staged/captured history and admissions. */
329
+ export const validateImportedExecution = Effect.fnUntraced(function*(
330
+ threadId: ThreadId,
331
+ checkpoint: ExecutionCheckpoint,
332
+ tailSequence: CanonicalSequence,
333
+ ) {
334
+ const reader = yield* ThreadImportReader
335
+ const progress = checkpoint.progress
336
+ const admission = yield* reader.admission(progress.submissionId)
337
+
338
+ const own = (yield* reader.runRecords(progress.runId)).filter((entry) =>
339
+ executionRunIds(entry.record).includes(progress.runId)
340
+ )
341
+
342
+ const local = new Map(checkpoint.operational.map((record) => [record.recordId, record]))
343
+
344
+ if (
345
+ admission === undefined ||
346
+ admission.threadId !== threadId ||
347
+ runIdForSubmission(admission.submissionId) !== progress.runId ||
348
+ checkpoint.progressRecord.recordId !==
349
+ JSON.stringify(['continuation@1', progress.runId, checkpoint.batchId]) ||
350
+ checkpoint.throughSequence > tailSequence ||
351
+ checkpoint.originalSequence > checkpoint.throughSequence ||
352
+ local.size !== checkpoint.operational.length ||
353
+ checkpoint.operational.some(
354
+ (record) => !isOperationalRecord(record) || !executionRunIds(record).includes(progress.runId),
355
+ ) ||
356
+ own.filter((entry) => entry.sequence <= checkpoint.throughSequence).length !==
357
+ checkpoint.canonicalRecords ||
358
+ !validateSuffix(own, checkpoint.throughSequence)
359
+ ) {
360
+ return yield* invalid(
361
+ 'Active execution identity, revision or history boundary is invalid',
362
+ threadId,
363
+ )
364
+ }
365
+ for (const id of local.keys()) {
366
+ if ((yield* reader.record(id)) !== undefined) {
367
+ return yield* invalid('Operational identity collides with retained history', threadId)
368
+ }
369
+ }
370
+
371
+ const resolve = Effect.fnUntraced(function*(ref: typeof progress.originalInput) {
372
+ const retained = yield* reader.record(ref.recordId)
373
+ const record = local.get(ref.recordId) ?? retained?.record
374
+
375
+ if (
376
+ record === undefined ||
377
+ (retained !== undefined &&
378
+ (retained.threadId !== threadId || retained.sequence > checkpoint.throughSequence)) ||
379
+ !executionRunIds(record).includes(progress.runId)
380
+ ) {
381
+ return yield* invalid('Active execution reference is missing, foreign or corrupt', threadId)
382
+ }
383
+
384
+ return record
385
+ })
386
+
387
+ const original = yield* reader.record(progress.originalInput.recordId)
388
+ const input = (yield* resolve(progress.originalInput)).payload
389
+
390
+ if (
391
+ original === undefined ||
392
+ original.sequence !== checkpoint.originalSequence ||
393
+ input._tag !== 'UserInputRecorded' ||
394
+ input.kind !== 'user' ||
395
+ input.runId !== progress.runId ||
396
+ input.submissionId !== progress.submissionId
397
+ ) {
398
+ return yield* invalid('Active execution has no exact original admitted input', threadId)
399
+ }
400
+ yield* resolve(progress.lastFact)
401
+ if (progress.savedContext !== undefined) {
402
+ const context = yield* resolve(progress.savedContext)
403
+
404
+ if (context.payload._tag !== 'RunContextRecorded' || !local.has(context.recordId)) {
405
+ return yield* invalid('Saved context is not owned active state', threadId)
406
+ }
407
+ yield* validateContextBoundary(context.payload, original).pipe(
408
+ Effect.mapError((cause) => invalid(cause.message, threadId)),
409
+ )
410
+ yield* readRunContext(context.payload, original, progress.savedContext.recordId).pipe(
411
+ Effect.mapError((cause) => invalid(cause.message, threadId)),
412
+ )
413
+ }
414
+ if (progress.latestResponse !== undefined) {
415
+ const response = (yield* resolve(progress.latestResponse)).payload
416
+
417
+ if (
418
+ response._tag !== 'ModelResponseRecorded' ||
419
+ response.turn !== progress.accounting.committedTurns ||
420
+ progress.savedContext === undefined
421
+ ) {
422
+ return yield* invalid('Active execution has invalid declared operation evidence', threadId)
423
+ }
424
+ } else if (progress.accounting.committedTurns !== 0) {
425
+ return yield* invalid('Active accounting has no corresponding response', threadId)
426
+ }
427
+ if (progress.terminal !== undefined) {
428
+ const terminal = (yield* resolve(progress.terminal)).payload
429
+
430
+ if (
431
+ terminal._tag !== 'RunCompleted' &&
432
+ terminal._tag !== 'RunFailed' &&
433
+ terminal._tag !== 'SubmissionSettled'
434
+ ) {
435
+ return yield* invalid('Active execution has invalid terminal intent', threadId)
436
+ }
437
+ if (progress.position === 'settled' && terminal._tag !== 'SubmissionSettled') {
438
+ return yield* invalid('Settled active state has no canonical terminal outcome', threadId)
439
+ }
440
+ }
441
+ })
442
+
443
+ /** An executed unfinished Run cannot be imported without its authoritative operational owner. */
444
+ export const validateImportedRunExecution = Effect.fnUntraced(function*(
445
+ threadId: ThreadId,
446
+ runId: RunId,
447
+ tailSequence: CanonicalSequence,
448
+ ) {
449
+ const reader = yield* ThreadImportReader
450
+ const checkpoint = yield* reader.execution(runId)
451
+
452
+ if (checkpoint !== undefined) {
453
+ return yield* validateImportedExecution(threadId, checkpoint, tailSequence)
454
+ }
455
+ const records = yield* reader.runRecords(runId)
456
+
457
+ if (
458
+ records.some(
459
+ ({ record }) =>
460
+ record.payload._tag === 'UserInputRecorded' &&
461
+ record.payload.kind === 'user' &&
462
+ record.payload.submissionId !== undefined,
463
+ ) &&
464
+ !records.some(
465
+ ({ record }) => record.payload._tag === 'SubmissionSettled' && record.payload.runId === runId,
466
+ ) &&
467
+ records.some(({ record }) => !isPreContinuationFact(record))
468
+ ) {
469
+ return yield* invalid('Unfinished execution is missing its authoritative checkpoint', threadId)
470
+ }
471
+ })
472
+
473
+ /** Own the first page before entering a destination gate; reuse the same scoped source pull. */
474
+ export const captureImportSource = <E, R, A, E2, R2>(
475
+ pages: Stream.Stream<ThreadArchive, E, R>,
476
+ use: (captured: Stream.Stream<ThreadArchive, E, R>) => Effect.Effect<A, E2, R2>,
477
+ ): Effect.Effect<A, E | E2 | ThreadImportRejected, Exclude<R | R2, Scope.Scope>> =>
478
+ Effect.scoped(
479
+ Effect.suspend(() => {
480
+ const phase = { writing: false }
481
+
482
+ return Effect.gen(function*() {
483
+ const pull = yield* Stream.toPull(Stream.rechunk(pages, 1))
484
+
485
+ const first = yield* Pull.catchDone(pull, () => Effect.fail(invalid('Missing transfer pages')))
486
+
487
+ const codec = Schema.fromJsonString(ThreadArchive)
488
+
489
+ const wire = yield* Schema.encodeEffect(codec)(first[0]).pipe(
490
+ Effect.mapError((cause) => invalid(`Malformed first transfer page: ${cause.message}`)),
491
+ )
492
+
493
+ if (utf8ByteLength(wire) > MAX_THREAD_EXPORT_PAGE_BYTES) {
494
+ return yield* invalid('Transfer page exceeds 32 MiB')
495
+ }
496
+
497
+ const owned = yield* Schema.decodeEffect(codec)(wire).pipe(
498
+ Effect.mapError((cause) => invalid(`Malformed first transfer page: ${cause.message}`)),
499
+ )
500
+
501
+ phase.writing = true
502
+
503
+ return yield* use(
504
+ Stream.succeed(owned).pipe(Stream.concat(Stream.fromPull(Effect.succeed(pull)))),
505
+ )
506
+ }).pipe(Effect.provideService(ImportSourcePhase, phase))
507
+ }),
508
+ )
509
+
510
+ /** Constant-size fold state; identity uniqueness belongs to staged destination indexes. */
511
+ export interface ThreadImportProgress {
512
+ manifest: ThreadArchive | undefined
513
+ sequence: number
514
+ ended: boolean
515
+ cursor: string | undefined
516
+ counts: {
517
+ admissions: number
518
+ aborts: number
519
+ approvals: number
520
+ resolutions: number
521
+ deliveries: number
522
+ }
523
+ }
524
+
525
+ export const makeThreadImportProgress = (): ThreadImportProgress => ({
526
+ manifest: undefined,
527
+ sequence: 0,
528
+ ended: false,
529
+ cursor: undefined,
530
+ counts: { admissions: 0, aborts: 0, approvals: 0, resolutions: 0, deliveries: 0 },
531
+ })
532
+
533
+ /** Capture before awaiting any writes; the retained manifest never retains page payload arrays. */
534
+ export const prepareImportPage = Effect.fnUntraced(function*(
535
+ state: ThreadImportProgress,
536
+ input: ThreadArchive,
537
+ options?: { readonly verification?: boolean },
538
+ ) {
539
+ const codec = Schema.fromJsonString(ThreadArchive)
540
+
541
+ const wire = yield* Schema.encodeEffect(codec)(input).pipe(
542
+ Effect.mapError(() => invalid('Malformed transfer page')),
543
+ )
544
+
545
+ if (utf8ByteLength(wire) > MAX_THREAD_EXPORT_PAGE_BYTES) {
546
+ return yield* invalid('Transfer page exceeds 32 MiB')
547
+ }
548
+
549
+ const archive = yield* Schema.decodeEffect(codec)(wire).pipe(
550
+ Effect.mapError(() => invalid('Malformed transfer page')),
551
+ )
552
+
553
+ const { threadId } = archive
554
+
555
+ // A final source page may omit its cursor. Restored facts must remain exportable after
556
+ // later admissions, commands or a worker seal change the manifest and continuation.
557
+ if (
558
+ !transferPageFits(threadId) ||
559
+ archive.admissions.some((fact) => !admissionFitsTransfer(threadId, fact)) ||
560
+ archive.commands.aborts.some((fact) => !abortFitsTransfer(threadId, fact)) ||
561
+ archive.commands.approvals.some((fact) => !approvalFitsTransfer(threadId, fact)) ||
562
+ archive.commands.resolutions.some((fact) => !resolutionFitsTransfer(threadId, fact)) ||
563
+ archive.deliveries.some((fact) => !deliveryFitsTransfer(threadId, fact))
564
+ ) {
565
+ return yield* ThreadImportRejected.make({
566
+ threadId,
567
+ reason: 'unsupported-capacity',
568
+ message: 'Imported fact exceeds its bounded transfer representation',
569
+ })
570
+ }
571
+
572
+ if (state.ended) return yield* invalid('Page after the final transfer page', threadId)
573
+ if (archive.format !== CURRENT_RECORD_FORMAT) {
574
+ return yield* ThreadImportRejected.make({
575
+ threadId,
576
+ reason: 'unsupported-format',
577
+ message: `Only ${CURRENT_RECORD_FORMAT} can be imported`,
578
+ })
579
+ }
580
+ if (state.manifest !== undefined && !sameTransferManifest(state.manifest, archive)) {
581
+ return yield* invalid('Transfer snapshot changed', threadId)
582
+ }
583
+ if (
584
+ archive.fromSequence !== state.sequence + 1 ||
585
+ (archive.cursor !== undefined && archive.cursor === state.cursor)
586
+ ) {
587
+ return yield* invalid('Transfer page position is invalid', threadId)
588
+ }
589
+ if (archive.records.length > 0 && transferSections.some((section) => state.counts[section] > 0)) {
590
+ return yield* invalid('Canonical records follow immutable fact sections', threadId)
591
+ }
592
+ if (
593
+ archive.admissions.length > 0 &&
594
+ state.counts.aborts +
595
+ state.counts.approvals +
596
+ state.counts.resolutions +
597
+ state.counts.deliveries >
598
+ 0
599
+ ) {
600
+ return yield* invalid('Admissions follow accepted command sections', threadId)
601
+ }
602
+ const records: Array<CanonicalRecordEnvelope> = []
603
+ const prepared: Array<PreparedImportBatch> = []
604
+ let index = 0
605
+
606
+ for (const batch of archive.batches) {
607
+ const current: Array<RecordEnvelope> = []
608
+ const encoded: Array<RecordJson> = []
609
+ const firstSequence = sequence(state.sequence + 1)
610
+
611
+ while (archive.records[index]?.batchId === batch.batchId) {
612
+ const entry = archive.records[index]!
613
+
614
+ if (entry.threadId !== threadId || entry.sequence !== state.sequence + 1) {
615
+ return yield* invalid('Canonical identity or sequence mismatch', threadId)
616
+ }
617
+
618
+ const record = yield* decodeExportRecord(archive.format, entry.record).pipe(
619
+ Effect.mapError((e) => invalid(e.message, threadId)),
620
+ )
621
+
622
+ if (record.payload._tag === 'RunContinuation') {
623
+ return yield* invalid('Mutable interpreter progress is not canonical history', threadId)
624
+ }
625
+
626
+ records.push(CanonicalRecordEnvelope.make({ ...entry, record }))
627
+ current.push(record)
628
+ encoded.push(entry.record)
629
+ index++
630
+ state.sequence++
631
+ }
632
+
633
+ const canonical = yield* Schema.decodeUnknownEffect(
634
+ Schema.toType(Schema.Struct(CanonicalBatch.fields)),
635
+ )({ ...batch, records: current }).pipe(
636
+ Effect.mapError(() => invalid('Empty or oversized canonical batch', threadId)),
637
+ Effect.map((value) => CanonicalBatch.make(value)),
638
+ )
639
+
640
+ const batchJson = canonicalJson({ ...batch, records: encoded })
641
+
642
+ if (utf8ByteLength(batchJson) > 16 * 1024 * 1024) {
643
+ return yield* invalid('Canonical batch exceeds 16 MiB', threadId)
644
+ }
645
+ prepared.push({
646
+ batch: canonical,
647
+ batchJson,
648
+ recordJson: encoded.map(canonicalJson),
649
+ firstSequence,
650
+ lastSequence: sequence(state.sequence),
651
+ })
652
+ }
653
+ if (index !== archive.records.length || state.sequence > archive.tailSequence) {
654
+ return yield* invalid(
655
+ 'Unbound canonical records or records beyond the captured tail',
656
+ threadId,
657
+ )
658
+ }
659
+ if (
660
+ archive.admissions.length +
661
+ archive.commands.aborts.length +
662
+ archive.commands.approvals.length +
663
+ archive.commands.resolutions.length +
664
+ archive.deliveries.length >
665
+ 0 &&
666
+ state.sequence !== archive.tailSequence
667
+ ) {
668
+ return yield* invalid('Immutable sections precede the complete canonical prefix', threadId)
669
+ }
670
+ if (
671
+ archive.commands.aborts.length +
672
+ archive.commands.approvals.length +
673
+ archive.commands.resolutions.length >
674
+ 0 &&
675
+ state.counts.admissions + archive.admissions.length !== archive.snapshot.admissions
676
+ ) {
677
+ return yield* invalid('Accepted commands precede the complete admission directory', threadId)
678
+ }
679
+
680
+ const sectionLength = (section: (typeof transferSections)[number]) =>
681
+ section === 'admissions'
682
+ ? archive.admissions.length
683
+ : section === 'deliveries'
684
+ ? archive.deliveries.length
685
+ : archive.commands[section].length
686
+
687
+ for (let index = 0; index < transferSections.length; index++) {
688
+ const section = transferSections[index]!
689
+
690
+ if (sectionLength(section) === 0) continue
691
+ if (
692
+ transferSections
693
+ .slice(0, index)
694
+ .some((prior) => state.counts[prior] + sectionLength(prior) !== archive.snapshot[prior]) ||
695
+ transferSections.slice(index + 1).some((later) => state.counts[later] !== 0)
696
+ ) {
697
+ return yield* invalid('Transfer fact sections are out of order', threadId)
698
+ }
699
+ }
700
+ const required = transferDependencies(records)
701
+
702
+ if (canonicalJson(required) !== canonicalJson([...archive.dependencies].sort())) {
703
+ return yield* invalid('Cross-range dependency directory is incomplete or ambiguous', threadId)
704
+ }
705
+ for (const section of transferSections) {
706
+ state.counts[section] += sectionLength(section)
707
+ if (state.counts[section] > archive.snapshot[section]) {
708
+ return yield* invalid(`Too many ${section} facts`, threadId)
709
+ }
710
+ }
711
+ for (const admission of archive.admissions) {
712
+ if (!options?.verification && admission.settledAt === undefined) {
713
+ return yield* invalid(
714
+ 'Closed admission is missing its stable settlement timestamp',
715
+ threadId,
716
+ )
717
+ }
718
+ if (
719
+ admission.threadId !== threadId ||
720
+ (yield* digest(admission.inputPayload)) !== admission.inputDigest
721
+ ) {
722
+ return yield* invalid('Admission identity or input digest mismatch', threadId)
723
+ }
724
+ }
725
+ for (const delivery of archive.deliveries) {
726
+ const encoded = yield* Schema.encodeEffect(MessageDeliveryRecord)(delivery).pipe(
727
+ Effect.mapError(() => invalid('Invalid delivery fact', threadId)),
728
+ )
729
+
730
+ if (
731
+ delivery.key.ownerThreadId !== threadId ||
732
+ (!options?.verification &&
733
+ (delivery.leaseUntilMillis !== null ||
734
+ !['processed', 'refused'].includes(delivery.status))) ||
735
+ (yield* digest(encoded.envelope)) !== delivery.envelopeDigest
736
+ ) {
737
+ return yield* invalid('Delivery ownership, lease or envelope digest mismatch', threadId)
738
+ }
739
+ }
740
+ state.manifest = {
741
+ ...archive,
742
+ records: [],
743
+ batches: [],
744
+ admissions: [],
745
+ commands: { aborts: [], approvals: [], resolutions: [] },
746
+ deliveries: [],
747
+ dependencies: [],
748
+ }
749
+ state.cursor = archive.cursor
750
+ state.ended = archive.cursor === undefined
751
+
752
+ return { archive, records, batches: prepared } satisfies PreparedImportPage
753
+ })
754
+
755
+ export const finishThreadImport = Effect.fnUntraced(function*(state: ThreadImportProgress) {
756
+ const page = state.manifest
757
+
758
+ if (
759
+ page === undefined ||
760
+ !state.ended ||
761
+ state.sequence !== page.tailSequence ||
762
+ transferSections.some((section) => state.counts[section] !== page.snapshot[section])
763
+ ) {
764
+ return yield* invalid(
765
+ 'Transfer stream ended before its complete canonical sequence and fact sections',
766
+ )
767
+ }
768
+
769
+ return ThreadImportResult.make({
770
+ threadId: page.threadId,
771
+ format: page.format,
772
+ tailSequence: page.tailSequence,
773
+ recordCount: state.sequence,
774
+ submissionCount: state.counts.admissions,
775
+ })
776
+ })
777
+
778
+ /** An executed receipt must settle strictly after its own input. FIFO inputs are checked in one pass. */
779
+ export const verifyImportedInputOrder = Effect.fnUntraced(function*(
780
+ admission: Pick<ThreadAdmission, 'threadId' | 'submissionId' | 'queueSequence'>,
781
+ ) {
782
+ const reader = yield* ThreadImportReader
783
+ const settlement = yield* reader.record(submissionSettlementRecordId(admission.submissionId))
784
+
785
+ if (settlement === undefined) return
786
+ const applied = yield* reader.record(submissionInputRecordId(admission.submissionId))
787
+ const payload = settlement.record.payload
788
+
789
+ if (
790
+ applied === undefined &&
791
+ payload._tag === 'SubmissionSettled' &&
792
+ payload.outcome === 'aborted'
793
+ ) {
794
+ return
795
+ }
796
+ if (applied === undefined || applied.sequence >= settlement.sequence) {
797
+ return yield* invalid(
798
+ 'Executed settlement has no preceding canonical input',
799
+ admission.threadId,
800
+ )
801
+ }
802
+ })
803
+
804
+ /** Rebuild ONE admitted submission from exact staged evidence; terminal state does not erase uncertainty. */
805
+ export const rebuildImportedSubmission = Effect.fnUntraced(function*(admission: ThreadAdmission) {
806
+ const reader = yield* ThreadImportReader
807
+ const threadId = admission.threadId
808
+ const inputEntry = yield* reader.record(submissionInputRecordId(admission.submissionId))
809
+ const payload = inputEntry?.record.payload
810
+
811
+ if (
812
+ inputEntry !== undefined &&
813
+ (payload?._tag !== 'UserInputRecorded' ||
814
+ payload.submissionId !== admission.submissionId ||
815
+ (payload.kind !== 'user' && payload.kind !== 'steering') ||
816
+ (payload.messageAdmission === undefined
817
+ ? admission.messageAdmission !== undefined
818
+ : admission.messageAdmission === undefined ||
819
+ !sameInputMessage(payload.messageAdmission, admission.messageAdmission)) ||
820
+ canonicalJson(payload.input) !== canonicalJson(admission.inputPayload))
821
+ ) {
822
+ return yield* invalid('Canonical input disagrees with its immutable admission', threadId)
823
+ }
824
+ const runId = payload?._tag === 'UserInputRecorded' ? payload.runId : undefined
825
+ const host = runId === undefined ? undefined : yield* reader.runOwner(runId)
826
+
827
+ if (inputEntry !== undefined && host === undefined) {
828
+ return yield* invalid('Canonical Run has no admitted owner', threadId)
829
+ }
830
+
831
+ const joined = host !== undefined && host.submissionId !== admission.submissionId
832
+ ? host.submissionId
833
+ : undefined
834
+
835
+ if (
836
+ payload?._tag === 'UserInputRecorded' &&
837
+ payload.kind !== (joined === undefined ? 'user' : 'steering')
838
+ ) {
839
+ return yield* invalid('Canonical input kind disagrees with its admitted Run owner', threadId)
840
+ }
841
+
842
+ if (joined !== undefined && host !== undefined && host.queueSequence >= admission.queueSequence) {
843
+ return yield* invalid('Joined input precedes its host admission', threadId)
844
+ }
845
+
846
+ const settlement = (yield* reader.record(submissionSettlementRecordId(admission.submissionId)))
847
+ ?.record
848
+
849
+ if (settlement !== undefined) {
850
+ yield* validateCanonicalSettlement(settlement, admission).pipe(
851
+ Effect.mapError(() => invalid('Canonical settlement disagrees with its receipt', threadId)),
852
+ )
853
+ if (settlement.payload._tag !== 'SubmissionSettled' || settlement.payload.runId !== runId) {
854
+ return yield* invalid('Settlement Run does not own its canonical input', threadId)
855
+ }
856
+ if (joined !== undefined) {
857
+ const hostSettlement = (yield* reader.record(submissionSettlementRecordId(joined)))?.record
858
+ .payload
859
+
860
+ if (hostSettlement?._tag !== 'SubmissionSettled') {
861
+ return yield* invalid('Joined settlement has no host settlement', threadId)
862
+ }
863
+ yield* validateJoinedSettlement(settlement.payload, hostSettlement).pipe(
864
+ Effect.mapError(() => invalid('Joined settlement conflicts with its host', threadId)),
865
+ )
866
+ }
867
+ }
868
+ const ownRun = runIdForSubmission(admission.submissionId)
869
+
870
+ const runRecords = yield* reader.runRecords(ownRun)
871
+
872
+ if (inputEntry === undefined && runRecords.some(({ record }) => !isPreContinuationFact(record))) {
873
+ return yield* invalid('Admitted Run executed without its original canonical input', threadId)
874
+ }
875
+ const operations = toolOperationStates(runRecords, ownRun)
876
+ const operationCounts = new Map<ToolCallId, number>()
877
+
878
+ for (const state of operations) {
879
+ operationCounts.set(
880
+ state.operation.toolCallId,
881
+ (operationCounts.get(state.operation.toolCallId) ?? 0) + 1,
882
+ )
883
+ }
884
+ const toolRecords = new Map<ToolCallId, Array<CanonicalRecordEnvelope>>()
885
+
886
+ for (const entry of runRecords) {
887
+ const payload = entry.record.payload
888
+
889
+ if (!('toolCallId' in payload)) continue
890
+ const entries = toolRecords.get(payload.toolCallId) ?? []
891
+
892
+ entries.push(entry)
893
+ toolRecords.set(payload.toolCallId, entries)
894
+ }
895
+ const commands = yield* reader.commands(admission.submissionId)
896
+ const approvals = commands.approvals.map((intent) => ApprovalDecisionIntent.make(intent))
897
+ const resolutions = commands.resolutions.map((intent) => UnknownResolutionIntent.make(intent))
898
+ const acceptedAbort = commands.aborts[0]
899
+
900
+ if (commands.aborts.length > 1) return yield* invalid('Duplicate accepted abort', threadId)
901
+ const canonicalAbort = yield* reader.record(submissionAbortRecordId(admission.submissionId))
902
+
903
+ if (canonicalAbort !== undefined && acceptedAbort === undefined) {
904
+ return yield* invalid('Canonical abort has no immutable accepted command', threadId)
905
+ }
906
+
907
+ const abort = canonicalAbort?.record.payload._tag === 'AbortRequested'
908
+ ? AbortIntent.make({
909
+ ...canonicalAbort.record.payload,
910
+ requestedAt: acceptedAbort?.requestedAt ?? canonicalAbort.record.createdAt,
911
+ canonicalRecordId: canonicalAbort.record.recordId,
912
+ })
913
+ : acceptedAbort === undefined
914
+ ? undefined
915
+ : AbortIntent.make(acceptedAbort)
916
+
917
+ if (
918
+ acceptedAbort !== undefined &&
919
+ abort !== undefined &&
920
+ (acceptedAbort.author !== abort.author || acceptedAbort.reason !== abort.reason)
921
+ ) {
922
+ return yield* invalid('Accepted abort conflicts with the canonical command', threadId)
923
+ }
924
+ for (const command of [...approvals, ...resolutions]) {
925
+ if (operationCounts.get(command.toolCallId) !== 1) {
926
+ return yield* invalid(
927
+ 'Accepted tool command has no unambiguous canonical declaration',
928
+ threadId,
929
+ )
930
+ }
931
+ }
932
+ for (const command of approvals) {
933
+ const requested = (toolRecords.get(command.toolCallId) ?? []).filter(
934
+ ({ record: { payload } }) =>
935
+ payload._tag === 'ToolApprovalRequested' && payload.toolCallId === command.toolCallId,
936
+ )
937
+
938
+ const decided = (toolRecords.get(command.toolCallId) ?? []).filter(
939
+ ({ record: { payload } }) => payload._tag === 'ToolApprovalDecided' && payload.toolCallId === command.toolCallId,
940
+ )
941
+
942
+ if (
943
+ requested.length !== 1 ||
944
+ decided.length > 1 ||
945
+ decided.some(
946
+ ({ record: { payload } }) =>
947
+ payload._tag === 'ToolApprovalDecided' &&
948
+ (payload.decision !== command.decision ||
949
+ payload.resolver !== command.resolver ||
950
+ payload.reason !== command.reason),
951
+ )
952
+ ) {
953
+ return yield* invalid(
954
+ 'Accepted approval conflicts with its canonical request or decision',
955
+ threadId,
956
+ )
957
+ }
958
+ }
959
+ for (const command of resolutions) {
960
+ if (command.resolution._tag === 'AbortSubmission' && abort === undefined) {
961
+ return yield* invalid(
962
+ 'AbortSubmission resolution has no accepted or canonical abort',
963
+ threadId,
964
+ )
965
+ }
966
+
967
+ const resolved = (toolRecords.get(command.toolCallId) ?? []).filter(
968
+ ({ record: { payload } }) =>
969
+ payload._tag === 'ToolCallResolved' &&
970
+ payload.toolCallId === command.toolCallId &&
971
+ (payload.resolution === 'safe-retry' ? 'execution' : 'factual') ===
972
+ unknownResolutionKind(command.resolution),
973
+ )
974
+
975
+ const expected = command.resolution._tag === 'CompletedWithResult'
976
+ ? command.resolution.isFailure
977
+ ? 'failed-with-error'
978
+ : 'completed-with-result'
979
+ : command.resolution._tag === 'NeverHappened'
980
+ ? 'never-started'
981
+ : command.resolution._tag === 'SafeToRetry'
982
+ ? 'safe-retry'
983
+ : undefined
984
+
985
+ if (
986
+ resolved.length > 1 ||
987
+ resolved.some(
988
+ ({ record: { payload } }) =>
989
+ payload._tag === 'ToolCallResolved' &&
990
+ (payload.resolution !== expected ||
991
+ payload.author !== command.author ||
992
+ payload.reason !== command.reason),
993
+ )
994
+ ) {
995
+ return yield* invalid('Accepted resolution conflicts with its canonical audit', threadId)
996
+ }
997
+ if (command.resolution._tag === 'CompletedWithResult') {
998
+ const result = command.resolution
999
+
1000
+ const outcomes = (toolRecords.get(command.toolCallId) ?? []).filter(
1001
+ ({ record: { payload } }) => payload._tag === 'ToolCallSettled' && payload.toolCallId === command.toolCallId,
1002
+ )
1003
+
1004
+ if (
1005
+ outcomes.length > 1 ||
1006
+ outcomes.some(
1007
+ ({ record: { payload } }) =>
1008
+ payload._tag === 'ToolCallSettled' &&
1009
+ (payload.isFailure !== result.isFailure ||
1010
+ canonicalJson(payload.result) !== canonicalJson(result.result)),
1011
+ )
1012
+ ) {
1013
+ return yield* invalid(
1014
+ 'Accepted resolution conflicts with the canonical tool result',
1015
+ threadId,
1016
+ )
1017
+ }
1018
+ }
1019
+ }
1020
+
1021
+ const unknown = operations
1022
+ .filter(
1023
+ (state) =>
1024
+ !state.settled &&
1025
+ !state.resolved &&
1026
+ (state.unknown ||
1027
+ (!state.dispatchBlocked &&
1028
+ state.operation.executionKind === 'ordinary' &&
1029
+ state.operation.executionClass !== 'readonly')),
1030
+ )
1031
+ .map((state) => state.operation.toolCallId)
1032
+
1033
+ const acceptedApprovals = new Set(approvals.map((intent) => intent.toolCallId))
1034
+
1035
+ const pendingApprovals = runRecords
1036
+ .filter(({ record: { payload } }) => payload._tag === 'ToolApprovalRequested')
1037
+ .flatMap(({ record }) => {
1038
+ if (record.payload._tag !== 'ToolApprovalRequested') return []
1039
+ const id = record.payload.toolCallId
1040
+
1041
+ return acceptedApprovals.has(id) ||
1042
+ (toolRecords.get(id) ?? []).some(
1043
+ ({ record: { payload } }) => payload._tag === 'ToolApprovalDecided' && payload.toolCallId === id,
1044
+ )
1045
+ ? []
1046
+ : [id]
1047
+ })
1048
+
1049
+ const suspension = pendingApprovals.length === 0
1050
+ ? undefined
1051
+ : SuspensionSnapshot.make({
1052
+ reason: ApprovalPendingSuspension.make({
1053
+ toolCallIds: [pendingApprovals[0], ...pendingApprovals.slice(1)],
1054
+ }),
1055
+ suspendedAt: runRecords.at(-1)?.record.createdAt ?? admission.createdAt,
1056
+ })
1057
+
1058
+ // An admitted input need not have reached the log. Its immutable fact is the work;
1059
+ // recovery materializes an admitted Thread before it becomes ready.
1060
+ const state = settlement !== undefined
1061
+ ? 'settled'
1062
+ : joined !== undefined
1063
+ ? 'joined'
1064
+ : unknown.length > 0
1065
+ ? 'unknown'
1066
+ : suspension !== undefined
1067
+ ? 'suspended'
1068
+ : inputEntry !== undefined
1069
+ ? 'input-applied'
1070
+ : (yield* reader.hasAgent(admission.agentId))
1071
+ ? 'ready'
1072
+ : 'admitted'
1073
+
1074
+ return RebuiltSubmission.make({
1075
+ admission,
1076
+ state,
1077
+ ...(inputEntry === undefined
1078
+ ? {}
1079
+ : {
1080
+ inputApplied: InputAppliedMarker.make({
1081
+ recordId: inputEntry.record.recordId,
1082
+ sequence: inputEntry.sequence,
1083
+ }),
1084
+ }),
1085
+ ...(joined === undefined ? {} : { joinedHostSubmissionId: joined }),
1086
+ ...(settlement === undefined ? {} : { settlement }),
1087
+ ...(state !== 'suspended' || suspension === undefined ? {} : { suspension }),
1088
+ unknownToolCallIds: unknown,
1089
+ ...(abort === undefined ? {} : { abort }),
1090
+ approvals,
1091
+ resolutions,
1092
+ })
1093
+ })
1094
+
1095
+ /** Immediate persistent history has no ledger/checkpoint owner, but still needs a terminal Run fact. */
1096
+ export const validateClosedRun = Effect.fnUntraced(function*(threadId: ThreadId, runId: RunId) {
1097
+ const reader = yield* ThreadImportReader
1098
+ const records = yield* reader.runRecords(runId)
1099
+
1100
+ const opened = records.some(
1101
+ ({ record: { payload } }) =>
1102
+ (payload._tag === 'UserInputRecorded' ||
1103
+ payload._tag === 'RunStarted' ||
1104
+ payload._tag === 'ModelResponseRecorded') &&
1105
+ payload.runId === runId,
1106
+ )
1107
+
1108
+ const closed = records.some(
1109
+ ({ record: { payload } }) =>
1110
+ (payload._tag === 'RunCompleted' ||
1111
+ payload._tag === 'RunFailed' ||
1112
+ payload._tag === 'SubmissionSettled') &&
1113
+ payload.runId === runId,
1114
+ )
1115
+
1116
+ if (opened && !closed) return yield* invalid('Transfer contains an unfinished Run', threadId)
1117
+ })
1118
+
1119
+ /** Idle transfer keeps closed receipts, never the authority to finish their former work. */
1120
+ export const rebuildClosedSubmission = Effect.fnUntraced(function*(admission: ThreadAdmission) {
1121
+ if (admission.settledAt === undefined) {
1122
+ return yield* invalid(
1123
+ 'Closed admission is missing its stable settlement timestamp',
1124
+ admission.threadId,
1125
+ )
1126
+ }
1127
+ const rebuilt = yield* rebuildImportedSubmission(admission)
1128
+ const reader = yield* ThreadImportReader
1129
+
1130
+ if (
1131
+ rebuilt.state !== 'settled' ||
1132
+ rebuilt.unknownToolCallIds.length > 0 ||
1133
+ rebuilt.suspension !== undefined
1134
+ ) {
1135
+ return yield* invalid('Transfer contains unfinished accepted work', admission.threadId)
1136
+ }
1137
+ if (rebuilt.abort !== undefined && rebuilt.abort.canonicalRecordId === undefined) {
1138
+ return yield* invalid('Transfer contains an unapplied abort', admission.threadId)
1139
+ }
1140
+ const records = yield* reader.runRecords(runIdForSubmission(admission.submissionId))
1141
+
1142
+ const decidedApprovals = new Set(
1143
+ records.flatMap(({ record: { payload } }) => payload._tag === 'ToolApprovalDecided' ? [payload.toolCallId] : []),
1144
+ )
1145
+
1146
+ if (
1147
+ records.some(
1148
+ ({ record: { payload } }) =>
1149
+ payload._tag === 'ToolApprovalRequested' && !decidedApprovals.has(payload.toolCallId),
1150
+ )
1151
+ ) {
1152
+ return yield* invalid('Transfer contains an unresolved approval', admission.threadId)
1153
+ }
1154
+ for (const approval of rebuilt.approvals) {
1155
+ if (
1156
+ !records.some(
1157
+ ({ record: { payload } }) =>
1158
+ payload._tag === 'ToolApprovalDecided' && payload.toolCallId === approval.toolCallId,
1159
+ )
1160
+ ) {
1161
+ return yield* invalid('Transfer contains an unapplied approval', admission.threadId)
1162
+ }
1163
+ }
1164
+ for (const resolution of rebuilt.resolutions) {
1165
+ if (
1166
+ resolution.resolution._tag !== 'AbortSubmission' &&
1167
+ !records.some(
1168
+ ({ record: { payload } }) =>
1169
+ payload._tag === 'ToolCallResolved' &&
1170
+ payload.toolCallId === resolution.toolCallId &&
1171
+ (payload.resolution === 'safe-retry' ? 'execution' : 'factual') ===
1172
+ unknownResolutionKind(resolution.resolution),
1173
+ )
1174
+ ) {
1175
+ return yield* invalid('Transfer contains an unapplied resolution', admission.threadId)
1176
+ }
1177
+ }
1178
+ yield* verifyImportedInputOrder(admission)
1179
+
1180
+ return rebuilt
1181
+ })
1182
+
1183
+ /** Encode one page at a time and retain upstream stream errors and requirements. */
1184
+ export const reencodeThread = Effect.fnUntraced(function*<E, R>(
1185
+ source: Stream.Stream<ThreadExport, E, R>,
1186
+ ) {
1187
+ const target = yield* ThreadImport
1188
+
1189
+ return yield* target.import(
1190
+ source.pipe(
1191
+ Stream.mapEffect((page) =>
1192
+ Schema.encodeEffect(ThreadExport)(page).pipe(
1193
+ Effect.flatMap(Schema.decodeUnknownEffect(ThreadArchive)),
1194
+ Effect.mapError(() => invalid('Unable to encode source page', page.threadId)),
1195
+ )
1196
+ ),
1197
+ ),
1198
+ )
1199
+ })