@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.
- package/LICENSE +21 -0
- package/dist/ActivityStore-CEy3oY41.mjs +90 -0
- package/dist/ActivityStore-CEy3oY41.mjs.map +1 -0
- package/dist/ActivityStore-CPXWRHGz.d.mts +91 -0
- package/dist/Admin-BcQDrWKY.mjs +329 -0
- package/dist/Admin-BcQDrWKY.mjs.map +1 -0
- package/dist/Admin-BfolLkHj.d.mts +5678 -0
- package/dist/Agent-Bn3zMhDH.mjs +60 -0
- package/dist/Agent-Bn3zMhDH.mjs.map +1 -0
- package/dist/Agent-DSVEpq4e.d.mts +219 -0
- package/dist/AgentError-RkUaetX8.d.mts +128 -0
- package/dist/AgentError-yFdnS9bl.mjs +141 -0
- package/dist/AgentError-yFdnS9bl.mjs.map +1 -0
- package/dist/AgentPolicy-CI7LRBQa.mjs +116 -0
- package/dist/AgentPolicy-CI7LRBQa.mjs.map +1 -0
- package/dist/AgentPolicy-D-GmZQex.d.mts +128 -0
- package/dist/AgentRegistration-Dg53p6du.mjs +14 -0
- package/dist/AgentRegistration-Dg53p6du.mjs.map +1 -0
- package/dist/AgentRuntime-CoGy_5dm.d.mts +751 -0
- package/dist/AgentRuntime-CzP4I4RB.mjs +5249 -0
- package/dist/AgentRuntime-CzP4I4RB.mjs.map +1 -0
- package/dist/AgentUpdates-7xT0It_R.d.mts +54 -0
- package/dist/AgentUpdates-BS_6gR5K.mjs +66 -0
- package/dist/AgentUpdates-BS_6gR5K.mjs.map +1 -0
- package/dist/BrowserUse-C9vict2M.d.mts +196 -0
- package/dist/BrowserUse-DjEyFDLZ.mjs +156 -0
- package/dist/BrowserUse-DjEyFDLZ.mjs.map +1 -0
- package/dist/Budget-CXh_ChFJ.d.mts +112 -0
- package/dist/Budget-j1UkthgY.mjs +351 -0
- package/dist/Budget-j1UkthgY.mjs.map +1 -0
- package/dist/CodeExecutor-B_Vt1Ghy.mjs +281 -0
- package/dist/CodeExecutor-B_Vt1Ghy.mjs.map +1 -0
- package/dist/CodeExecutor-D5-Rda75.d.mts +198 -0
- package/dist/CodeMode-B7JvfKAD.d.mts +159 -0
- package/dist/CodeMode-BI7WhUkr.mjs +555 -0
- package/dist/CodeMode-BI7WhUkr.mjs.map +1 -0
- package/dist/CommittedActivity-DXyCKCtX.mjs +146 -0
- package/dist/CommittedActivity-DXyCKCtX.mjs.map +1 -0
- package/dist/CommittedActivity-LWOKXqdi.d.mts +62 -0
- package/dist/Compaction-C0hWAmOG.mjs +15 -0
- package/dist/Compaction-C0hWAmOG.mjs.map +1 -0
- package/dist/Compaction-e5cNeFmY.d.mts +5 -0
- package/dist/ContextCompactor-Chm9BLrD.d.mts +165 -0
- package/dist/ContextCompactor-DV4NOQvg.mjs +134 -0
- package/dist/ContextCompactor-DV4NOQvg.mjs.map +1 -0
- package/dist/ContextHistory-C80rW5M7.mjs +76 -0
- package/dist/ContextHistory-C80rW5M7.mjs.map +1 -0
- package/dist/ContextHistory-akQ1Td7a.d.mts +65 -0
- package/dist/ContextTools-917r7gpv.d.mts +186 -0
- package/dist/ContextTools-p8JDPo-W.mjs +88 -0
- package/dist/ContextTools-p8JDPo-W.mjs.map +1 -0
- package/dist/ContextWindow-BJZI4MMt.d.mts +61 -0
- package/dist/ContextWindow-kbFcs1IF.mjs +63 -0
- package/dist/ContextWindow-kbFcs1IF.mjs.map +1 -0
- package/dist/Digest-BDToAbaX.d.mts +25 -0
- package/dist/Digest-CU6IWj-2.mjs +106 -0
- package/dist/Digest-CU6IWj-2.mjs.map +1 -0
- package/dist/DurableAgentRuntime-BHXo8GPJ.mjs +8432 -0
- package/dist/DurableAgentRuntime-BHXo8GPJ.mjs.map +1 -0
- package/dist/DurableFailpoint-BD5nowNl.mjs +98 -0
- package/dist/DurableFailpoint-BD5nowNl.mjs.map +1 -0
- package/dist/DurableFailpoint-Cqi5tyof.d.mts +42 -0
- package/dist/DurableStep-CzVIho7y.mjs +54 -0
- package/dist/DurableStep-CzVIho7y.mjs.map +1 -0
- package/dist/DurableStep-cI_Ho_rU.d.mts +109 -0
- package/dist/EventSource-BwZVkEQi.mjs +66 -0
- package/dist/EventSource-BwZVkEQi.mjs.map +1 -0
- package/dist/EventSource-C0TCBeIf.d.mts +52 -0
- package/dist/FailureDiagnostic-DdHV2eFs.mjs +304 -0
- package/dist/FailureDiagnostic-DdHV2eFs.mjs.map +1 -0
- package/dist/FailureDiagnostic-DpzYNI8f.d.mts +232 -0
- package/dist/IdGenerator-BCzwV0ZI.mjs +29 -0
- package/dist/IdGenerator-BCzwV0ZI.mjs.map +1 -0
- package/dist/IdGenerator-BY8NH74o.d.mts +26 -0
- package/dist/Identifiers-LWd4837I.mjs +41 -0
- package/dist/Identifiers-LWd4837I.mjs.map +1 -0
- package/dist/Identifiers-qafW4EP0.d.mts +38 -0
- package/dist/InMemory-BrTm6XEO.mjs +57 -0
- package/dist/InMemory-BrTm6XEO.mjs.map +1 -0
- package/dist/InMemory-C3qXFT5k.d.mts +46 -0
- package/dist/InMemory.d.mts +2 -0
- package/dist/InMemory.mjs +1 -0
- package/dist/InteractiveBrowser-B-hlNJy5.mjs +237 -0
- package/dist/InteractiveBrowser-B-hlNJy5.mjs.map +1 -0
- package/dist/InteractiveBrowser-nnML0r7w.d.mts +258 -0
- package/dist/LifecyclePublication-BDc_ug5v.mjs +204 -0
- package/dist/LifecyclePublication-BDc_ug5v.mjs.map +1 -0
- package/dist/Memory-CT5bDZqi.d.mts +57 -0
- package/dist/Memory-Dl9ecPpm.mjs +204 -0
- package/dist/Memory-Dl9ecPpm.mjs.map +1 -0
- package/dist/MemoryNamespace-BtnlH_E6.d.mts +35 -0
- package/dist/MemoryNamespace-DZSpjLj9.mjs +116 -0
- package/dist/MemoryNamespace-DZSpjLj9.mjs.map +1 -0
- package/dist/MemoryNotes-4CRzkPBD.d.mts +425 -0
- package/dist/MemoryNotes-i41TsH11.mjs +132 -0
- package/dist/MemoryNotes-i41TsH11.mjs.map +1 -0
- package/dist/MemoryReference-Dd3KSMa7.d.mts +77 -0
- package/dist/MemoryReference-RFU0HQJG.mjs +108 -0
- package/dist/MemoryReference-RFU0HQJG.mjs.map +1 -0
- package/dist/MemoryRevalidation--aAh_Pfq.mjs +136 -0
- package/dist/MemoryRevalidation--aAh_Pfq.mjs.map +1 -0
- package/dist/MemoryRevalidation-FsJ8E61W.d.mts +64 -0
- package/dist/MemoryStore-Bg5RunTk.d.mts +187 -0
- package/dist/MemoryStore-E0J99hb2.mjs +245 -0
- package/dist/MemoryStore-E0J99hb2.mjs.map +1 -0
- package/dist/MessageDelivery-BE2Yf3FC.mjs +615 -0
- package/dist/MessageDelivery-BE2Yf3FC.mjs.map +1 -0
- package/dist/MessageDelivery-Dh6GZBt_.d.mts +1129 -0
- package/dist/Messaging-BqWHYf93.d.mts +216 -0
- package/dist/Messaging-DgSiv4f4.mjs +154 -0
- package/dist/Messaging-DgSiv4f4.mjs.map +1 -0
- package/dist/Messaging-DhM-APnP.d.mts +1074 -0
- package/dist/MessagingHost-Bm5e-kAX.mjs +117 -0
- package/dist/MessagingHost-Bm5e-kAX.mjs.map +1 -0
- package/dist/MessagingHost-CubWeYKN.d.mts +80 -0
- package/dist/OperationAuthorizer-VSiHCC2S.mjs +72 -0
- package/dist/OperationAuthorizer-VSiHCC2S.mjs.map +1 -0
- package/dist/OperationAuthorizer-y-F-Ox_Q.d.mts +62 -0
- package/dist/Output-DQ8mWcgf.mjs +7 -0
- package/dist/Output-DQ8mWcgf.mjs.map +1 -0
- package/dist/Output-SbOdlBFL.d.mts +22 -0
- package/dist/PageCapture-BfRURUOA.mjs +275 -0
- package/dist/PageCapture-BfRURUOA.mjs.map +1 -0
- package/dist/PageCapture-C_mE72Sd.d.mts +216 -0
- package/dist/PageScreenshot-BJTASs4o.mjs +53 -0
- package/dist/PageScreenshot-BJTASs4o.mjs.map +1 -0
- package/dist/PageScreenshot-CaoaH7mc.d.mts +48 -0
- package/dist/PersistentHistory-BrtZ0g2j.d.mts +14 -0
- package/dist/PersistentHistory-CTAVhHKH.mjs +147 -0
- package/dist/PersistentHistory-CTAVhHKH.mjs.map +1 -0
- package/dist/PreparedInputAdmission-Ci18uY4s.d.mts +973 -0
- package/dist/PreparedInputAdmission-adqVJJa6.mjs +95 -0
- package/dist/PreparedInputAdmission-adqVJJa6.mjs.map +1 -0
- package/dist/ProvisionalText-BcKpPYKQ.d.mts +70 -0
- package/dist/ProvisionalText-DXddTK4c.mjs +47 -0
- package/dist/ProvisionalText-DXddTK4c.mjs.map +1 -0
- package/dist/Receipt-B-FlCaNH.mjs +35 -0
- package/dist/Receipt-B-FlCaNH.mjs.map +1 -0
- package/dist/Receipt-B4feBxMY.d.mts +31 -0
- package/dist/RecordFormat-DoNh-AFe.mjs +79 -0
- package/dist/RecordFormat-DoNh-AFe.mjs.map +1 -0
- package/dist/RecordFormat-ZMus8Dx8.d.mts +52 -0
- package/dist/Records-C645aQks.mjs +1145 -0
- package/dist/Records-C645aQks.mjs.map +1 -0
- package/dist/Records-C9I1nCL9.d.mts +1423 -0
- package/dist/Recovery-zWy_kmZq.mjs +736 -0
- package/dist/Recovery-zWy_kmZq.mjs.map +1 -0
- package/dist/Redaction-C0zLBAn6.d.mts +28 -0
- package/dist/Redaction-CtyCBMEj.mjs +116 -0
- package/dist/Redaction-CtyCBMEj.mjs.map +1 -0
- package/dist/RememberingStore-BO-OnCgP.mjs +23 -0
- package/dist/RememberingStore-BO-OnCgP.mjs.map +1 -0
- package/dist/RememberingStore-DgqXmILx.d.mts +14 -0
- package/dist/RunContinuation-BVAHoYlN.d.mts +127 -0
- package/dist/RunContinuation-Bpfp087i.mjs +1060 -0
- package/dist/RunContinuation-Bpfp087i.mjs.map +1 -0
- package/dist/RunEvent-CwgDzXcH.d.mts +465 -0
- package/dist/RunEvent-DJxENnJi.mjs +345 -0
- package/dist/RunEvent-DJxENnJi.mjs.map +1 -0
- package/dist/RunEventSink-B8uM8bw2.mjs +22 -0
- package/dist/RunEventSink-B8uM8bw2.mjs.map +1 -0
- package/dist/RunEventSink-f-T4jbNB.d.mts +105 -0
- package/dist/RunHooks-Bob22Bcj.d.mts +40 -0
- package/dist/RunHooks-DWuLBVdU.mjs +60 -0
- package/dist/RunHooks-DWuLBVdU.mjs.map +1 -0
- package/dist/RunJournal-C_1eqQdr.mjs +1107 -0
- package/dist/RunJournal-C_1eqQdr.mjs.map +1 -0
- package/dist/RunOptions-BVSa3BO2.d.mts +560 -0
- package/dist/RunOptions-COEUCrx4.mjs +61 -0
- package/dist/RunOptions-COEUCrx4.mjs.map +1 -0
- package/dist/RunPolicyUsage-B-JXRZYO.mjs +25 -0
- package/dist/RunPolicyUsage-B-JXRZYO.mjs.map +1 -0
- package/dist/RunPolicyUsage-jvJo0vJp.d.mts +21 -0
- package/dist/RunStorage-CsQQ0Y5u.mjs +304 -0
- package/dist/RunStorage-CsQQ0Y5u.mjs.map +1 -0
- package/dist/Sandbox-37vXP0BV.d.mts +24 -0
- package/dist/Sandbox-Bze6pHYc.mjs +30 -0
- package/dist/Sandbox-Bze6pHYc.mjs.map +1 -0
- package/dist/Schedule-BY0lzbij.mjs +336 -0
- package/dist/Schedule-BY0lzbij.mjs.map +1 -0
- package/dist/ScheduleTransition-CjVP48I8.d.mts +22 -0
- package/dist/ScheduleTransition-DtNzlN9k.mjs +180 -0
- package/dist/ScheduleTransition-DtNzlN9k.mjs.map +1 -0
- package/dist/Scheduling-BpJN9E1j.mjs +699 -0
- package/dist/Scheduling-BpJN9E1j.mjs.map +1 -0
- package/dist/Scheduling-DYTM29s9.d.mts +57 -0
- package/dist/SemanticMemory-BRV0cuWg.mjs +300 -0
- package/dist/SemanticMemory-BRV0cuWg.mjs.map +1 -0
- package/dist/SemanticMemory-rgWlhW87.d.mts +431 -0
- package/dist/SemanticMemoryIndex-D7yNEWpt.d.mts +826 -0
- package/dist/SemanticMemoryIndex-godQDjNx.mjs +186 -0
- package/dist/SemanticMemoryIndex-godQDjNx.mjs.map +1 -0
- package/dist/SemanticMemoryRevalidation-Bm3_pfsI.d.mts +44 -0
- package/dist/SemanticMemoryRevalidation-D8VV-eU6.mjs +179 -0
- package/dist/SemanticMemoryRevalidation-D8VV-eU6.mjs.map +1 -0
- package/dist/Subagent-CSIRUgUo.mjs +1315 -0
- package/dist/Subagent-CSIRUgUo.mjs.map +1 -0
- package/dist/Subagent-CXEI5YZ8.d.mts +2368 -0
- package/dist/SubagentContract-B7vQPueI.d.mts +176 -0
- package/dist/SubagentContract-C-7AxTm_.mjs +187 -0
- package/dist/SubagentContract-C-7AxTm_.mjs.map +1 -0
- package/dist/SubagentHost-BEnICu7g.mjs +70 -0
- package/dist/SubagentHost-BEnICu7g.mjs.map +1 -0
- package/dist/SubagentHost-CKhz1vbH.d.mts +167 -0
- package/dist/SubagentReservations-BYNAHPpm.mjs +465 -0
- package/dist/SubagentReservations-BYNAHPpm.mjs.map +1 -0
- package/dist/SubagentReservations-WrmIVzO5.d.mts +185 -0
- package/dist/SubmissionLedger-WG0beEyt.mjs +921 -0
- package/dist/SubmissionLedger-WG0beEyt.mjs.map +1 -0
- package/dist/SubmissionStatus-CPZsOM5a.mjs +17 -0
- package/dist/SubmissionStatus-CPZsOM5a.mjs.map +1 -0
- package/dist/Subscription-BhQbmUD-.mjs +340 -0
- package/dist/Subscription-BhQbmUD-.mjs.map +1 -0
- package/dist/Subscription-CC--_OnU.d.mts +815 -0
- package/dist/SubscriptionInput-BEeB0Cj8.d.mts +42 -0
- package/dist/SubscriptionInput-Xcd4JjEs.mjs +53 -0
- package/dist/SubscriptionInput-Xcd4JjEs.mjs.map +1 -0
- package/dist/SubscriptionTransition-CWt9UVH0.d.mts +23 -0
- package/dist/SubscriptionTransition-lrf733c8.mjs +154 -0
- package/dist/SubscriptionTransition-lrf733c8.mjs.map +1 -0
- package/dist/Subscriptions-4K_qu_2f.mjs +638 -0
- package/dist/Subscriptions-4K_qu_2f.mjs.map +1 -0
- package/dist/Subscriptions-BF3aSf8X.d.mts +75 -0
- package/dist/Thread-B8JApGHa.mjs +283 -0
- package/dist/Thread-B8JApGHa.mjs.map +1 -0
- package/dist/Thread-BnAtkJl4.d.mts +116 -0
- package/dist/ThreadArchiveRange-1PMdjpdJ.mjs +64 -0
- package/dist/ThreadArchiveRange-1PMdjpdJ.mjs.map +1 -0
- package/dist/ThreadContextHistory-BSStMOLX.mjs +211 -0
- package/dist/ThreadContextHistory-BSStMOLX.mjs.map +1 -0
- package/dist/ThreadContextHistory-lTZ8Ftq1.d.mts +33 -0
- package/dist/ThreadContextHistoryProjection-Cp7_RR6V.d.mts +59 -0
- package/dist/ThreadContextHistoryProjection-_8uNz-h1.mjs +149 -0
- package/dist/ThreadContextHistoryProjection-_8uNz-h1.mjs.map +1 -0
- package/dist/ThreadHistory-BkyGfv5E.d.mts +76 -0
- package/dist/ThreadHistory-Blt_W31N.mjs +69 -0
- package/dist/ThreadHistory-Blt_W31N.mjs.map +1 -0
- package/dist/ThreadImport-C8Tqiezd.mjs +491 -0
- package/dist/ThreadImport-C8Tqiezd.mjs.map +1 -0
- package/dist/ThreadImport-DaSmrlRi.d.mts +843 -0
- package/dist/ThreadInvariants-DyPhMeo-.d.mts +23 -0
- package/dist/ThreadInvariants-fniroLRY.mjs +154 -0
- package/dist/ThreadInvariants-fniroLRY.mjs.map +1 -0
- package/dist/ThreadProjectionMaintenance-D6fBjr1q.mjs +37 -0
- package/dist/ThreadProjectionMaintenance-D6fBjr1q.mjs.map +1 -0
- package/dist/ThreadProjectionMaintenance-DjUy7kCa.d.mts +51 -0
- package/dist/ThreadStore-BMUAp5Ta.mjs +937 -0
- package/dist/ThreadStore-BMUAp5Ta.mjs.map +1 -0
- package/dist/ThreadTransfer-B3NkyO_y.mjs +154 -0
- package/dist/ThreadTransfer-B3NkyO_y.mjs.map +1 -0
- package/dist/ThreadTransfer-BM02cbOo.d.mts +63 -0
- package/dist/ThreadWork-C-xa2Boi.mjs +484 -0
- package/dist/ThreadWork-C-xa2Boi.mjs.map +1 -0
- package/dist/ToolBroker-DkoVyx6T.mjs +42 -0
- package/dist/ToolBroker-DkoVyx6T.mjs.map +1 -0
- package/dist/ToolBroker-Dy2xM6ht.d.mts +134 -0
- package/dist/ToolDiscovery-DHySvLQd.mjs +211 -0
- package/dist/ToolDiscovery-DHySvLQd.mjs.map +1 -0
- package/dist/ToolDiscovery-VjamzX3q.d.mts +119 -0
- package/dist/ToolExposure-Cn7xETn5.d.mts +6 -0
- package/dist/ToolExposure-D7id9Gt5.d.mts +37 -0
- package/dist/ToolExposure-DCvQWFIa.mjs +9 -0
- package/dist/ToolExposure-DCvQWFIa.mjs.map +1 -0
- package/dist/ToolExposure-DNME8BkG.d.mts +44 -0
- package/dist/ToolExposure-DPnq8Won.mjs +30 -0
- package/dist/ToolExposure-DPnq8Won.mjs.map +1 -0
- package/dist/ToolExposure-DaGaF5tj.mjs +20 -0
- package/dist/ToolExposure-DaGaF5tj.mjs.map +1 -0
- package/dist/ToolExposure.d.mts +4 -0
- package/dist/ToolExposure.mjs +4 -0
- package/dist/ToolReconciler-6bb7xlip.d.mts +81 -0
- package/dist/ToolReconciler-DJQd3SZA.mjs +87 -0
- package/dist/ToolReconciler-DJQd3SZA.mjs.map +1 -0
- package/dist/ToolResult-By5KAje4.d.mts +79 -0
- package/dist/ToolResult-DwZxjlHw.mjs +160 -0
- package/dist/ToolResult-DwZxjlHw.mjs.map +1 -0
- package/dist/ToolResultStorage-BmoRewhh.mjs +46 -0
- package/dist/ToolResultStorage-BmoRewhh.mjs.map +1 -0
- package/dist/ToolResultStorage-Bp5fm0do.d.mts +49 -0
- package/dist/ToolResults-C4-HsMCp.d.mts +33 -0
- package/dist/ToolResults-nCgBlTp2.mjs +45 -0
- package/dist/ToolResults-nCgBlTp2.mjs.map +1 -0
- package/dist/Usage-B0xgME5f.mjs +340 -0
- package/dist/Usage-B0xgME5f.mjs.map +1 -0
- package/dist/Usage-Bc-jXe_9.d.mts +128 -0
- package/dist/WakeScheduler-DVYj66C-.mjs +74 -0
- package/dist/WakeScheduler-DVYj66C-.mjs.map +1 -0
- package/dist/WakeScheduler-SFqj67Sk.d.mts +74 -0
- package/dist/WebCapture-CA4ZarW2.mjs +64 -0
- package/dist/WebCapture-CA4ZarW2.mjs.map +1 -0
- package/dist/WebCapture-pzIeUJ5W.d.mts +49 -0
- package/dist/WebSearch-CD0V--qh.d.mts +18 -0
- package/dist/WebSearch-DnbOzRL_.mjs +30 -0
- package/dist/WebSearch-DnbOzRL_.mjs.map +1 -0
- package/dist/Worker-CQu8Qp1j.mjs +267 -0
- package/dist/Worker-CQu8Qp1j.mjs.map +1 -0
- package/dist/Worker-xxucY_0b.d.mts +468 -0
- package/dist/WorkerAdmission-BhFjx86s.mjs +162 -0
- package/dist/WorkerAdmission-BhFjx86s.mjs.map +1 -0
- package/dist/WorkerAdmission-BnRuXLWF.d.mts +171 -0
- package/dist/WorkerHost-DtYzi54D.d.mts +150 -0
- package/dist/WorkerHost-DuQug9Bq.mjs +73 -0
- package/dist/WorkerHost-DuQug9Bq.mjs.map +1 -0
- package/dist/agent-registration-CNs2twrr.mjs +262 -0
- package/dist/agent-registration-CNs2twrr.mjs.map +1 -0
- package/dist/capabilities/BrowserUse.d.mts +2 -0
- package/dist/capabilities/BrowserUse.mjs +1 -0
- package/dist/capabilities/Budget.d.mts +2 -0
- package/dist/capabilities/Budget.mjs +1 -0
- package/dist/capabilities/CodeMode.d.mts +2 -0
- package/dist/capabilities/CodeMode.mjs +1 -0
- package/dist/capabilities/ContextTools.d.mts +2 -0
- package/dist/capabilities/ContextTools.mjs +1 -0
- package/dist/capabilities/MemoryNotes.d.mts +2 -0
- package/dist/capabilities/MemoryNotes.mjs +1 -0
- package/dist/capabilities/Messaging.d.mts +3 -0
- package/dist/capabilities/Messaging.mjs +4 -0
- package/dist/capabilities/Redaction.d.mts +2 -0
- package/dist/capabilities/Redaction.mjs +1 -0
- package/dist/capabilities/RunHooks.d.mts +2 -0
- package/dist/capabilities/RunHooks.mjs +1 -0
- package/dist/capabilities/SemanticMemory.d.mts +2 -0
- package/dist/capabilities/SemanticMemory.mjs +1 -0
- package/dist/capabilities/Subagent.d.mts +4 -0
- package/dist/capabilities/Subagent.mjs +6 -0
- package/dist/capabilities/SubagentReservations.d.mts +2 -0
- package/dist/capabilities/SubagentReservations.mjs +1 -0
- package/dist/capabilities/ToolDiscovery.d.mts +2 -0
- package/dist/capabilities/ToolDiscovery.mjs +1 -0
- package/dist/capabilities/ToolResults.d.mts +2 -0
- package/dist/capabilities/ToolResults.mjs +1 -0
- package/dist/capabilities/WebCapture.d.mts +2 -0
- package/dist/capabilities/WebCapture.mjs +1 -0
- package/dist/capabilities/WebSearch.d.mts +2 -0
- package/dist/capabilities/WebSearch.mjs +1 -0
- package/dist/compaction-C3hXuO4Z.mjs +539 -0
- package/dist/compaction-C3hXuO4Z.mjs.map +1 -0
- package/dist/completion-B_WDnSg-.d.mts +11 -0
- package/dist/core/Agent.d.mts +2 -0
- package/dist/core/Agent.mjs +1 -0
- package/dist/core/AgentError.d.mts +2 -0
- package/dist/core/AgentError.mjs +1 -0
- package/dist/core/AgentPolicy.d.mts +2 -0
- package/dist/core/AgentPolicy.mjs +1 -0
- package/dist/core/AgentUpdates.d.mts +2 -0
- package/dist/core/AgentUpdates.mjs +1 -0
- package/dist/core/FailureDiagnostic.d.mts +2 -0
- package/dist/core/FailureDiagnostic.mjs +1 -0
- package/dist/core/IdGenerator.d.mts +2 -0
- package/dist/core/IdGenerator.mjs +1 -0
- package/dist/core/Identifiers.d.mts +2 -0
- package/dist/core/Identifiers.mjs +1 -0
- package/dist/core/Memory.d.mts +2 -0
- package/dist/core/Memory.mjs +1 -0
- package/dist/core/MemoryNamespace.d.mts +2 -0
- package/dist/core/MemoryNamespace.mjs +1 -0
- package/dist/core/MemoryReference.d.mts +2 -0
- package/dist/core/MemoryReference.mjs +1 -0
- package/dist/core/MemoryRevalidation.d.mts +2 -0
- package/dist/core/MemoryRevalidation.mjs +1 -0
- package/dist/core/MemoryStore.d.mts +2 -0
- package/dist/core/MemoryStore.mjs +1 -0
- package/dist/core/ProvisionalText.d.mts +2 -0
- package/dist/core/ProvisionalText.mjs +1 -0
- package/dist/core/Receipt.d.mts +2 -0
- package/dist/core/Receipt.mjs +1 -0
- package/dist/core/RememberingStore.d.mts +2 -0
- package/dist/core/RememberingStore.mjs +1 -0
- package/dist/core/RunEvent.d.mts +3 -0
- package/dist/core/RunEvent.mjs +1 -0
- package/dist/core/RunPolicyUsage.d.mts +2 -0
- package/dist/core/RunPolicyUsage.mjs +1 -0
- package/dist/core/SemanticMemoryIndex.d.mts +2 -0
- package/dist/core/SemanticMemoryIndex.mjs +1 -0
- package/dist/core/SemanticMemoryRevalidation.d.mts +2 -0
- package/dist/core/SemanticMemoryRevalidation.mjs +1 -0
- package/dist/core/SubagentContract.d.mts +2 -0
- package/dist/core/SubagentContract.mjs +1 -0
- package/dist/core/Thread.d.mts +2 -0
- package/dist/core/Thread.mjs +1 -0
- package/dist/core/ToolResult.d.mts +2 -0
- package/dist/core/ToolResult.mjs +1 -0
- package/dist/core/ToolResultStorage.d.mts +2 -0
- package/dist/core/ToolResultStorage.mjs +1 -0
- package/dist/core/Usage.d.mts +2 -0
- package/dist/core/Usage.mjs +1 -0
- package/dist/core/Worker.d.mts +3 -0
- package/dist/core/Worker.mjs +3 -0
- package/dist/durable/ActivityStore.d.mts +2 -0
- package/dist/durable/ActivityStore.mjs +1 -0
- package/dist/durable/Admin.d.mts +2 -0
- package/dist/durable/Admin.mjs +1 -0
- package/dist/durable/AgentRegistration.d.mts +3 -0
- package/dist/durable/AgentRegistration.mjs +2 -0
- package/dist/durable/CommittedActivity.d.mts +2 -0
- package/dist/durable/CommittedActivity.mjs +1 -0
- package/dist/durable/Digest.d.mts +2 -0
- package/dist/durable/Digest.mjs +3 -0
- package/dist/durable/DurableAgentRuntime.d.mts +3 -0
- package/dist/durable/DurableAgentRuntime.mjs +4 -0
- package/dist/durable/DurableFailpoint.d.mts +2 -0
- package/dist/durable/DurableFailpoint.mjs +1 -0
- package/dist/durable/DurableFailpointTestControl.d.mts +14 -0
- package/dist/durable/DurableFailpointTestControl.mjs +21 -0
- package/dist/durable/DurableFailpointTestControl.mjs.map +1 -0
- package/dist/durable/EventSource.d.mts +2 -0
- package/dist/durable/EventSource.mjs +1 -0
- package/dist/durable/LifecyclePublication.d.mts +2 -0
- package/dist/durable/LifecyclePublication.mjs +1 -0
- package/dist/durable/MessageDelivery.d.mts +2 -0
- package/dist/durable/MessageDelivery.mjs +1 -0
- package/dist/durable/MessageDeliveryStoreConformance.d.mts +401 -0
- package/dist/durable/MessageDeliveryStoreConformance.mjs +273 -0
- package/dist/durable/MessageDeliveryStoreConformance.mjs.map +1 -0
- package/dist/durable/OperationAuthorizer.d.mts +2 -0
- package/dist/durable/OperationAuthorizer.mjs +1 -0
- package/dist/durable/PersistentHistory.d.mts +2 -0
- package/dist/durable/PersistentHistory.mjs +1 -0
- package/dist/durable/PreparedInputAdmission.d.mts +2 -0
- package/dist/durable/PreparedInputAdmission.mjs +1 -0
- package/dist/durable/RecordFormat.d.mts +2 -0
- package/dist/durable/RecordFormat.mjs +1 -0
- package/dist/durable/Records.d.mts +2 -0
- package/dist/durable/Records.mjs +1 -0
- package/dist/durable/Recovery.d.mts +2 -0
- package/dist/durable/Recovery.mjs +1 -0
- package/dist/durable/RunContinuation.d.mts +4 -0
- package/dist/durable/RunContinuation.mjs +4 -0
- package/dist/durable/RunJournal.d.mts +2 -0
- package/dist/durable/RunJournal.mjs +1 -0
- package/dist/durable/RunStorage.d.mts +2 -0
- package/dist/durable/RunStorage.mjs +1 -0
- package/dist/durable/Schedule.d.mts +2 -0
- package/dist/durable/Schedule.mjs +1 -0
- package/dist/durable/ScheduleStoreConformance.d.mts +16 -0
- package/dist/durable/ScheduleStoreConformance.mjs +421 -0
- package/dist/durable/ScheduleStoreConformance.mjs.map +1 -0
- package/dist/durable/ScheduleTransition.d.mts +2 -0
- package/dist/durable/ScheduleTransition.mjs +1 -0
- package/dist/durable/Scheduling.d.mts +2 -0
- package/dist/durable/Scheduling.mjs +1 -0
- package/dist/durable/SettlementPublisher.d.mts +2 -0
- package/dist/durable/SettlementPublisher.mjs +99 -0
- package/dist/durable/SettlementPublisher.mjs.map +1 -0
- package/dist/durable/SqlMemoryStore.d.mts +729 -0
- package/dist/durable/SqlMemoryStore.mjs +626 -0
- package/dist/durable/SqlMemoryStore.mjs.map +1 -0
- package/dist/durable/SqlStorageOwner.d.mts +18 -0
- package/dist/durable/SqlStorageOwner.mjs +6 -0
- package/dist/durable/SqlStorageOwner.mjs.map +1 -0
- package/dist/durable/SubmissionLedger.d.mts +3 -0
- package/dist/durable/SubmissionLedger.mjs +3 -0
- package/dist/durable/SubmissionLedgerConformance.d.mts +31 -0
- package/dist/durable/SubmissionLedgerConformance.mjs +2090 -0
- package/dist/durable/SubmissionLedgerConformance.mjs.map +1 -0
- package/dist/durable/SubmissionStatus.d.mts +2 -0
- package/dist/durable/SubmissionStatus.mjs +1 -0
- package/dist/durable/Subscription.d.mts +2 -0
- package/dist/durable/Subscription.mjs +1 -0
- package/dist/durable/SubscriptionInput.d.mts +2 -0
- package/dist/durable/SubscriptionInput.mjs +1 -0
- package/dist/durable/SubscriptionStoreConformance.d.mts +15 -0
- package/dist/durable/SubscriptionStoreConformance.mjs +816 -0
- package/dist/durable/SubscriptionStoreConformance.mjs.map +1 -0
- package/dist/durable/SubscriptionTransition.d.mts +2 -0
- package/dist/durable/SubscriptionTransition.mjs +1 -0
- package/dist/durable/Subscriptions.d.mts +2 -0
- package/dist/durable/Subscriptions.mjs +1 -0
- package/dist/durable/ThreadArchiveRange.d.mts +2 -0
- package/dist/durable/ThreadArchiveRange.mjs +1 -0
- package/dist/durable/ThreadContextHistory.d.mts +2 -0
- package/dist/durable/ThreadContextHistory.mjs +1 -0
- package/dist/durable/ThreadContextHistoryProjection.d.mts +2 -0
- package/dist/durable/ThreadContextHistoryProjection.mjs +1 -0
- package/dist/durable/ThreadImport.d.mts +2 -0
- package/dist/durable/ThreadImport.mjs +1 -0
- package/dist/durable/ThreadInvariants.d.mts +2 -0
- package/dist/durable/ThreadInvariants.mjs +1 -0
- package/dist/durable/ThreadProjectionMaintenance.d.mts +2 -0
- package/dist/durable/ThreadProjectionMaintenance.mjs +1 -0
- package/dist/durable/ThreadReader.d.mts +2 -0
- package/dist/durable/ThreadReader.mjs +1 -0
- package/dist/durable/ThreadStore.d.mts +3 -0
- package/dist/durable/ThreadStore.mjs +3 -0
- package/dist/durable/ThreadStoreConformance.d.mts +53 -0
- package/dist/durable/ThreadStoreConformance.mjs +402 -0
- package/dist/durable/ThreadStoreConformance.mjs.map +1 -0
- package/dist/durable/ThreadTransfer.d.mts +2 -0
- package/dist/durable/ThreadTransfer.mjs +1 -0
- package/dist/durable/ThreadWork.d.mts +2 -0
- package/dist/durable/ThreadWork.mjs +1 -0
- package/dist/durable/ToolReconciler.d.mts +2 -0
- package/dist/durable/ToolReconciler.mjs +1 -0
- package/dist/durable/WakeScheduler.d.mts +2 -0
- package/dist/durable/WakeScheduler.mjs +1 -0
- package/dist/durable/WorkerAdmission.d.mts +2 -0
- package/dist/durable/WorkerAdmission.mjs +1 -0
- package/dist/durable/WorkerHost.d.mts +2 -0
- package/dist/durable/WorkerHost.mjs +1 -0
- package/dist/engine/AgentRuntime.d.mts +2 -0
- package/dist/engine/AgentRuntime.mjs +1 -0
- package/dist/engine/Compaction.d.mts +4 -0
- package/dist/engine/Compaction.mjs +2 -0
- package/dist/engine/ContextCompactor.d.mts +2 -0
- package/dist/engine/ContextCompactor.mjs +1 -0
- package/dist/engine/ContextHistory.d.mts +2 -0
- package/dist/engine/ContextHistory.mjs +1 -0
- package/dist/engine/ContextWindow.d.mts +2 -0
- package/dist/engine/ContextWindow.mjs +1 -0
- package/dist/engine/DurableStep.d.mts +2 -0
- package/dist/engine/DurableStep.mjs +3 -0
- package/dist/engine/MessagingHost.d.mts +2 -0
- package/dist/engine/MessagingHost.mjs +1 -0
- package/dist/engine/Output.d.mts +3 -0
- package/dist/engine/Output.mjs +2 -0
- package/dist/engine/RunEventSink.d.mts +2 -0
- package/dist/engine/RunEventSink.mjs +1 -0
- package/dist/engine/RunOptions.d.mts +2 -0
- package/dist/engine/RunOptions.mjs +1 -0
- package/dist/engine/SubagentHost.d.mts +2 -0
- package/dist/engine/SubagentHost.mjs +1 -0
- package/dist/engine/ThreadHistory.d.mts +2 -0
- package/dist/engine/ThreadHistory.mjs +1 -0
- package/dist/engine/ToolBroker.d.mts +2 -0
- package/dist/engine/ToolBroker.mjs +1 -0
- package/dist/index.d.mts +90 -0
- package/dist/index.mjs +101 -0
- package/dist/output-contract-CYA4oBhe.mjs +128 -0
- package/dist/output-contract-CYA4oBhe.mjs.map +1 -0
- package/dist/rolldown-runtime-D3KUYPsf.mjs +12 -0
- package/dist/run-context-reader-CHox63hg.d.mts +17 -0
- package/dist/sandbox/CodeExecutor.d.mts +2 -0
- package/dist/sandbox/CodeExecutor.mjs +1 -0
- package/dist/sandbox/InteractiveBrowser.d.mts +2 -0
- package/dist/sandbox/InteractiveBrowser.mjs +1 -0
- package/dist/sandbox/PageCapture.d.mts +2 -0
- package/dist/sandbox/PageCapture.mjs +1 -0
- package/dist/sandbox/PageScreenshot.d.mts +2 -0
- package/dist/sandbox/PageScreenshot.mjs +1 -0
- package/dist/sandbox/Sandbox.d.mts +2 -0
- package/dist/sandbox/Sandbox.mjs +1 -0
- package/dist/subagent-policy-xRJhrc0a.mjs +133 -0
- package/dist/subagent-policy-xRJhrc0a.mjs.map +1 -0
- package/dist/tool-execution-Cl7v8fXU.mjs +16 -0
- package/dist/tool-execution-Cl7v8fXU.mjs.map +1 -0
- package/dist/tool-operations-CauPonM8.mjs +84 -0
- package/dist/tool-operations-CauPonM8.mjs.map +1 -0
- package/dist/utf8-Bl0ZhhGe.mjs +27 -0
- package/dist/utf8-Bl0ZhhGe.mjs.map +1 -0
- package/package.json +491 -3
- package/src/InMemory.ts +79 -0
- package/src/ToolExposure.ts +2 -0
- package/src/capabilities/BrowserUse.ts +212 -0
- package/src/capabilities/Budget.ts +544 -0
- package/src/capabilities/CodeMode.ts +1129 -0
- package/src/capabilities/ContextTools.ts +131 -0
- package/src/capabilities/MemoryNotes.ts +177 -0
- package/src/capabilities/Messaging.ts +260 -0
- package/src/capabilities/Redaction.ts +163 -0
- package/src/capabilities/RunHooks.ts +116 -0
- package/src/capabilities/SemanticMemory.ts +402 -0
- package/src/capabilities/Subagent.ts +2343 -0
- package/src/capabilities/SubagentReservations.ts +807 -0
- package/src/capabilities/ToolDiscovery.ts +291 -0
- package/src/capabilities/ToolResults.ts +50 -0
- package/src/capabilities/WebCapture.ts +64 -0
- package/src/capabilities/WebSearch.ts +45 -0
- package/src/capabilities/internal/subagent-background.ts +1137 -0
- package/src/capabilities/internal/subagent-contract.ts +92 -0
- package/src/capabilities/internal/subagent-policy.ts +205 -0
- package/src/capabilities/internal/subagent-reporting.ts +152 -0
- package/src/core/Agent.ts +528 -0
- package/src/core/AgentError.ts +189 -0
- package/src/core/AgentPolicy.ts +194 -0
- package/src/core/AgentUpdates.ts +104 -0
- package/src/core/FailureDiagnostic.ts +392 -0
- package/src/core/IdGenerator.ts +42 -0
- package/src/core/Identifiers.ts +64 -0
- package/src/core/Memory.ts +283 -0
- package/src/core/MemoryNamespace.ts +202 -0
- package/src/core/MemoryReference.ts +120 -0
- package/src/core/MemoryRevalidation.ts +151 -0
- package/src/core/MemoryStore.ts +371 -0
- package/src/core/Messaging.ts +72 -0
- package/src/core/ProvisionalText.ts +53 -0
- package/src/core/Receipt.ts +42 -0
- package/src/core/RememberingStore.ts +22 -0
- package/src/core/RunEvent.ts +345 -0
- package/src/core/RunPolicyUsage.ts +19 -0
- package/src/core/SemanticMemoryIndex.ts +259 -0
- package/src/core/SemanticMemoryRevalidation.ts +220 -0
- package/src/core/SubagentContract.ts +263 -0
- package/src/core/Thread.ts +447 -0
- package/src/core/ToolExposure.ts +75 -0
- package/src/core/ToolResult.ts +204 -0
- package/src/core/ToolResultStorage.ts +56 -0
- package/src/core/Usage.ts +562 -0
- package/src/core/Worker.ts +236 -0
- package/src/core/internal/agent-updates.ts +42 -0
- package/src/core/internal/completion.ts +33 -0
- package/src/core/internal/json.ts +17 -0
- package/src/core/internal/memory-document.ts +39 -0
- package/src/core/internal/message-status.ts +60 -0
- package/src/core/internal/strict-schema.ts +14 -0
- package/src/core/internal/tool-execution.ts +33 -0
- package/src/core/internal/utf8.ts +32 -0
- package/src/durable/ActivityStore.ts +131 -0
- package/src/durable/Admin.ts +504 -0
- package/src/durable/AgentRegistration.ts +16 -0
- package/src/durable/CommittedActivity.ts +262 -0
- package/src/durable/Digest.ts +57 -0
- package/src/durable/DurableAgentRuntime.ts +12758 -0
- package/src/durable/DurableFailpoint.ts +108 -0
- package/src/durable/DurableFailpointTestControl.ts +39 -0
- package/src/durable/EventSource.ts +179 -0
- package/src/durable/LifecyclePublication.ts +343 -0
- package/src/durable/MessageDelivery.ts +949 -0
- package/src/durable/MessageDeliveryStoreConformance.ts +407 -0
- package/src/durable/OperationAuthorizer.ts +89 -0
- package/src/durable/PersistentHistory.ts +322 -0
- package/src/durable/PreparedInputAdmission.ts +128 -0
- package/src/durable/RecordFormat.ts +113 -0
- package/src/durable/Records.ts +1396 -0
- package/src/durable/Recovery.ts +1022 -0
- package/src/durable/RunContinuation.ts +1946 -0
- package/src/durable/RunJournal.ts +1926 -0
- package/src/durable/RunStorage.ts +533 -0
- package/src/durable/Schedule.ts +445 -0
- package/src/durable/ScheduleStoreConformance.ts +642 -0
- package/src/durable/ScheduleTransition.ts +267 -0
- package/src/durable/Scheduling.ts +1031 -0
- package/src/durable/SettlementPublisher.ts +172 -0
- package/src/durable/SqlMemoryStore.ts +1042 -0
- package/src/durable/SqlStorageOwner.ts +25 -0
- package/src/durable/SubmissionLedger.ts +1247 -0
- package/src/durable/SubmissionLedgerConformance.ts +4695 -0
- package/src/durable/SubmissionStatus.ts +14 -0
- package/src/durable/Subscription.ts +527 -0
- package/src/durable/SubscriptionInput.ts +145 -0
- package/src/durable/SubscriptionStoreConformance.ts +1275 -0
- package/src/durable/SubscriptionTransition.ts +330 -0
- package/src/durable/Subscriptions.ts +1245 -0
- package/src/durable/ThreadArchiveRange.ts +91 -0
- package/src/durable/ThreadContextHistory.ts +338 -0
- package/src/durable/ThreadContextHistoryProjection.ts +219 -0
- package/src/durable/ThreadImport.ts +1199 -0
- package/src/durable/ThreadInvariants.ts +312 -0
- package/src/durable/ThreadProjectionMaintenance.ts +72 -0
- package/src/durable/ThreadReader.ts +21 -0
- package/src/durable/ThreadStore.ts +1258 -0
- package/src/durable/ThreadStoreConformance.ts +877 -0
- package/src/durable/ThreadTransfer.ts +263 -0
- package/src/durable/ThreadWork.ts +713 -0
- package/src/durable/ToolReconciler.ts +112 -0
- package/src/durable/WakeScheduler.ts +125 -0
- package/src/durable/WorkerAdmission.ts +98 -0
- package/src/durable/WorkerHost.ts +204 -0
- package/src/durable/internal/StorageOwner.ts +181 -0
- package/src/durable/internal/agent-registration.ts +713 -0
- package/src/durable/internal/agent-updates.ts +338 -0
- package/src/durable/internal/canonical-json.ts +110 -0
- package/src/durable/internal/evidence.ts +29 -0
- package/src/durable/internal/foreign-diagnostic.ts +48 -0
- package/src/durable/internal/history-prompt.ts +87 -0
- package/src/durable/internal/initial-context.ts +215 -0
- package/src/durable/internal/journal-metadata.ts +188 -0
- package/src/durable/internal/message-status.ts +18 -0
- package/src/durable/internal/messaging-host.ts +615 -0
- package/src/durable/internal/prompt-footprint.ts +47 -0
- package/src/durable/internal/prompt-history-cache.ts +604 -0
- package/src/durable/internal/record-encoding.ts +258 -0
- package/src/durable/internal/record-ownership.ts +68 -0
- package/src/durable/internal/run-context-reader.ts +51 -0
- package/src/durable/internal/run-context.ts +211 -0
- package/src/durable/internal/schedule-time.ts +304 -0
- package/src/durable/internal/thread-import-reader.ts +85 -0
- package/src/durable/internal/thread-initialization.ts +181 -0
- package/src/durable/internal/thread-transfer-source.ts +7 -0
- package/src/durable/internal/tool-operations.ts +138 -0
- package/src/durable/internal/transfer-dependencies.ts +40 -0
- package/src/durable/internal/usage-totals.ts +36 -0
- package/src/durable/internal/worker-host.ts +4041 -0
- package/src/durable/internal/worker-runtime.ts +10 -0
- package/src/engine/AgentRuntime.ts +100 -0
- package/src/engine/Compaction.ts +11 -0
- package/src/engine/ContextCompactor.ts +302 -0
- package/src/engine/ContextHistory.ts +75 -0
- package/src/engine/ContextWindow.ts +81 -0
- package/src/engine/DurableStep.ts +115 -0
- package/src/engine/MessagingHost.ts +118 -0
- package/src/engine/Output.ts +2 -0
- package/src/engine/RunEventSink.ts +124 -0
- package/src/engine/RunOptions.ts +664 -0
- package/src/engine/SubagentHost.ts +231 -0
- package/src/engine/ThreadHistory.ts +125 -0
- package/src/engine/ToolBroker.ts +159 -0
- package/src/engine/ToolExposure.ts +49 -0
- package/src/engine/internal/agent-runtime.ts +11137 -0
- package/src/engine/internal/bounded-value.ts +312 -0
- package/src/engine/internal/compaction.ts +673 -0
- package/src/engine/internal/error-diagnostic.ts +39 -0
- package/src/engine/internal/image-tokens.ts +61 -0
- package/src/engine/internal/interpreter-options.ts +569 -0
- package/src/engine/internal/output-contract.ts +226 -0
- package/src/engine/internal/primitive-delta.ts +103 -0
- package/src/engine/internal/provider-result-staging.ts +211 -0
- package/src/engine/internal/provisional-text.ts +107 -0
- package/src/engine/internal/tool-derivative.ts +47 -0
- package/src/engine/internal/tool-exposure.ts +228 -0
- package/src/engine/internal/tool-telemetry.ts +488 -0
- package/src/index.ts +100 -0
- package/src/sandbox/CodeExecutor.ts +352 -0
- package/src/sandbox/InteractiveBrowser.ts +343 -0
- package/src/sandbox/PageCapture.ts +391 -0
- package/src/sandbox/PageScreenshot.ts +83 -0
- package/src/sandbox/Sandbox.ts +29 -0
- package/README.md +0 -3
|
@@ -0,0 +1,1946 @@
|
|
|
1
|
+
import * as Context from 'effect/Context'
|
|
2
|
+
import * as DateTime from 'effect/DateTime'
|
|
3
|
+
import * as Effect from 'effect/Effect'
|
|
4
|
+
import * as Option from 'effect/Option'
|
|
5
|
+
import * as Result from 'effect/Result'
|
|
6
|
+
import * as Schema from 'effect/Schema'
|
|
7
|
+
import * as Semaphore from 'effect/Semaphore'
|
|
8
|
+
import * as Stream from 'effect/Stream'
|
|
9
|
+
|
|
10
|
+
import { AgentPersistenceCapacityError } from '../core/AgentError'
|
|
11
|
+
import type { RunId, SubmissionId, ThreadId } from '../core/Identifiers'
|
|
12
|
+
import { ToolCallId } from '../core/Identifiers'
|
|
13
|
+
import { utf8ByteLength } from '../core/internal/utf8'
|
|
14
|
+
import { resolveEvidence } from './internal/evidence'
|
|
15
|
+
import { continuationUsage } from './internal/usage-totals'
|
|
16
|
+
export { reference, resolveEvidence } from './internal/evidence'
|
|
17
|
+
import { captureRecord, type ProgressAppendRecord, recordEncoding } from './internal/record-encoding'
|
|
18
|
+
import {
|
|
19
|
+
canonicalRunIds,
|
|
20
|
+
executionRunIds,
|
|
21
|
+
isPreContinuationFact,
|
|
22
|
+
isTerminalBudgetFact,
|
|
23
|
+
} from './internal/record-ownership'
|
|
24
|
+
export { terminalUsageCharge } from './internal/record-ownership'
|
|
25
|
+
import { readRunContext, validateContextBoundary } from './internal/run-context'
|
|
26
|
+
import {
|
|
27
|
+
CanonicalBatch,
|
|
28
|
+
type CanonicalRecordEnvelope,
|
|
29
|
+
CanonicalSequence,
|
|
30
|
+
type ContinuationAccounting,
|
|
31
|
+
type DeploymentId,
|
|
32
|
+
EvidenceReference,
|
|
33
|
+
MAX_PERSISTED_JSON_BYTES,
|
|
34
|
+
MAX_RUN_CONTINUATION_BYTES,
|
|
35
|
+
MAX_RUN_EVIDENCE_BYTES,
|
|
36
|
+
MAX_RUN_EVIDENCE_RECORDS,
|
|
37
|
+
MAX_RUN_RECOVERY_SUFFIX_BYTES,
|
|
38
|
+
MAX_RUN_RECOVERY_SUFFIX_RECORDS,
|
|
39
|
+
MAX_RUN_TERMINAL_BYTES,
|
|
40
|
+
MAX_TURN_CANONICAL_BYTES,
|
|
41
|
+
type ModelResponseRecorded,
|
|
42
|
+
RecordEnvelope,
|
|
43
|
+
RecordId,
|
|
44
|
+
RUN_TERMINAL_RESERVE_BYTES,
|
|
45
|
+
RUN_TERMINAL_RESERVE_RECORDS,
|
|
46
|
+
RunContinuation,
|
|
47
|
+
type ToolCallSettled,
|
|
48
|
+
} from './Records'
|
|
49
|
+
import {
|
|
50
|
+
runCompletedRecordId,
|
|
51
|
+
runIdForSubmission,
|
|
52
|
+
subagentRequestedRecordId,
|
|
53
|
+
subagentStartedRecordId,
|
|
54
|
+
toolCallResultBatchId,
|
|
55
|
+
toolCallSettledRecordId,
|
|
56
|
+
} from './RunJournal'
|
|
57
|
+
import type { RunStorageSession } from './RunStorage'
|
|
58
|
+
import {
|
|
59
|
+
submissionAbortRecordId,
|
|
60
|
+
submissionInputRecordId,
|
|
61
|
+
submissionSettlementBatchId,
|
|
62
|
+
submissionSettlementRecordId,
|
|
63
|
+
} from './SubmissionLedger'
|
|
64
|
+
import {
|
|
65
|
+
CommitBatch,
|
|
66
|
+
ExecutionCheckpoint,
|
|
67
|
+
ExecutionUpdate,
|
|
68
|
+
getRecord,
|
|
69
|
+
getRunInput,
|
|
70
|
+
ThreadReader,
|
|
71
|
+
ThreadStoreError,
|
|
72
|
+
} from './ThreadStore'
|
|
73
|
+
|
|
74
|
+
export { EvidenceReference, RunContextRecorded, RunContinuation } from './Records'
|
|
75
|
+
|
|
76
|
+
/** Writer and progress sequencer selected at the local owner's resource boundary. */
|
|
77
|
+
export class CurrentRunWriter extends Context.Service<
|
|
78
|
+
CurrentRunWriter,
|
|
79
|
+
Pick<RunStorageSession, 'threadId' | 'tail' | 'append' | 'appendInput' | 'withProgress'>
|
|
80
|
+
>()('@effect-agent/thread/CurrentRunWriter') {}
|
|
81
|
+
|
|
82
|
+
/** Settlement publication keeps its original atomic authority and claim-scoped implementation. */
|
|
83
|
+
export class CurrentRunSettlement extends Context.Service<
|
|
84
|
+
CurrentRunSettlement,
|
|
85
|
+
Pick<RunStorageSession, 'threadId' | 'tail' | 'publishSettlement' | 'withProgress'>
|
|
86
|
+
>()('@effect-agent/thread/CurrentRunSettlement') {}
|
|
87
|
+
|
|
88
|
+
/** Canonical reads pinned to the append owner's current transaction or mutation gate. */
|
|
89
|
+
export class ProgressAppendReader extends Context.Service<
|
|
90
|
+
ProgressAppendReader,
|
|
91
|
+
{
|
|
92
|
+
readonly previous: (
|
|
93
|
+
runId: RunId,
|
|
94
|
+
) => Effect.Effect<RunContinuation | undefined, ThreadStoreError>
|
|
95
|
+
readonly initial: (
|
|
96
|
+
next: RunContinuation,
|
|
97
|
+
) => Effect.Effect<ReadonlyArray<RecordEnvelope>, ThreadStoreError>
|
|
98
|
+
}
|
|
99
|
+
>()('@effect-agent/thread/ProgressAppendReader') {}
|
|
100
|
+
|
|
101
|
+
export { canonicalRunIds, executionRunIds, isPreContinuationFact, isWorkHandoff } from './internal/record-ownership'
|
|
102
|
+
|
|
103
|
+
const failure = (message: string, cause?: unknown) =>
|
|
104
|
+
ThreadStoreError.make({
|
|
105
|
+
operation: 'RunContinuation',
|
|
106
|
+
message,
|
|
107
|
+
...(cause === undefined ? {} : { cause }),
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
const capacityFailure = (message: string) => failure(message, AgentPersistenceCapacityError.make({ message }))
|
|
111
|
+
|
|
112
|
+
const isExecutionProgressRecord = (
|
|
113
|
+
record: RecordEnvelope,
|
|
114
|
+
): record is RecordEnvelope & { readonly payload: RunContinuation } => record.payload._tag === 'RunContinuation'
|
|
115
|
+
|
|
116
|
+
const validateBatchShape = Schema.decodeUnknownSync(
|
|
117
|
+
Schema.Struct({
|
|
118
|
+
batchId: CanonicalBatch.fields.batchId,
|
|
119
|
+
producerId: CanonicalBatch.fields.producerId,
|
|
120
|
+
records: Schema.NonEmptyArray(Schema.Unknown).check(Schema.isMaxLength(256)),
|
|
121
|
+
}),
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
const decodeExecutionFrontier = Schema.decodeUnknownEffect(
|
|
125
|
+
Schema.Struct({
|
|
126
|
+
canonicalRecords: ExecutionCheckpoint.fields.canonicalRecords,
|
|
127
|
+
throughSequence: ExecutionCheckpoint.fields.throughSequence,
|
|
128
|
+
}),
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
/** One private snapshot for every retry; callers cannot change facts while awaiting the gate. */
|
|
132
|
+
const captureFacts = (batch: CommitBatch) =>
|
|
133
|
+
Effect.try({
|
|
134
|
+
try: () =>
|
|
135
|
+
Result.gen(function*() {
|
|
136
|
+
const inputs = batch.records
|
|
137
|
+
|
|
138
|
+
const {
|
|
139
|
+
batchId,
|
|
140
|
+
producerId,
|
|
141
|
+
records: slots,
|
|
142
|
+
} = validateBatchShape({
|
|
143
|
+
batchId: batch.batchId,
|
|
144
|
+
producerId: batch.producerId,
|
|
145
|
+
records: inputs,
|
|
146
|
+
})
|
|
147
|
+
|
|
148
|
+
const first = yield* captureRecord(inputs[0]!)
|
|
149
|
+
const records: [RecordEnvelope, ...Array<RecordEnvelope>] = [first.canonical]
|
|
150
|
+
|
|
151
|
+
for (let index = 1; index < slots.length; index++) {
|
|
152
|
+
records.push((yield* captureRecord(inputs[index]!)).canonical)
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return Object.freeze(
|
|
156
|
+
CanonicalBatch.make(
|
|
157
|
+
{ batchId, producerId, records: Object.freeze(records) },
|
|
158
|
+
{ disableChecks: true },
|
|
159
|
+
),
|
|
160
|
+
)
|
|
161
|
+
}),
|
|
162
|
+
catch: (cause) => failure('Cannot capture canonical progress facts', cause),
|
|
163
|
+
}).pipe(
|
|
164
|
+
Effect.flatMap(Effect.fromResult),
|
|
165
|
+
Effect.mapError((cause) =>
|
|
166
|
+
Schema.is(ThreadStoreError)(cause)
|
|
167
|
+
? cause
|
|
168
|
+
: failure('Cannot capture canonical progress facts', cause)
|
|
169
|
+
),
|
|
170
|
+
)
|
|
171
|
+
|
|
172
|
+
/** Exact UTF-8 wire accounting reuses the append's privately captured Schema encoding. */
|
|
173
|
+
export const canonicalRecordBytes = (record: RecordEnvelope): number => {
|
|
174
|
+
const encoded = recordEncoding(record)
|
|
175
|
+
|
|
176
|
+
if (Result.isFailure(encoded)) {
|
|
177
|
+
// oxlint-disable-next-line effect/no-untyped-throw, effect/no-native-error -- Public synchronous byte-accounting validation must reject malformed records before returning a number.
|
|
178
|
+
throw encoded.failure.cause ?? new RangeError(encoded.failure.message)
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return encoded.success.bytes
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export type { ProgressAppendRecord } from './internal/record-encoding'
|
|
185
|
+
|
|
186
|
+
/** Capture before acquiring the mutation owner; summaries never cross a wire boundary. */
|
|
187
|
+
export const prepareProgressAppend = (
|
|
188
|
+
records: ReadonlyArray<RecordEnvelope>,
|
|
189
|
+
): ReadonlyArray<ProgressAppendRecord> => {
|
|
190
|
+
const encodings = Result.all(records.map(captureRecord))
|
|
191
|
+
|
|
192
|
+
if (Result.isFailure(encodings)) {
|
|
193
|
+
// oxlint-disable-next-line effect/no-untyped-throw, effect/no-native-error -- Public synchronous progress construction must reject malformed records before acquiring a mutation owner.
|
|
194
|
+
throw encodings.failure.cause ?? new RangeError(encodings.failure.message)
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
return Object.freeze(encodings.success.map((record) => record.progress))
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** These values belong to the active Run, not its permanent conversation history. */
|
|
201
|
+
export const isOperationalRecord = ({ payload }: RecordEnvelope): boolean =>
|
|
202
|
+
payload._tag === 'RunStarted' ||
|
|
203
|
+
payload._tag === 'RunContextRecorded' ||
|
|
204
|
+
payload._tag === 'RunCompleted'
|
|
205
|
+
|
|
206
|
+
export const readContinuation = Effect.fnUntraced(function*(
|
|
207
|
+
threadId: ThreadId,
|
|
208
|
+
runId: RunId,
|
|
209
|
+
throughSequence: CanonicalSequence,
|
|
210
|
+
) {
|
|
211
|
+
const reader = yield* ThreadReader
|
|
212
|
+
|
|
213
|
+
const loaded = yield* reader
|
|
214
|
+
.readExecution({ threadId, runId })
|
|
215
|
+
.pipe(
|
|
216
|
+
Effect.catchTag('ThreadNotMaterialized', (cause) =>
|
|
217
|
+
throughSequence === 0
|
|
218
|
+
? Effect.succeedNone
|
|
219
|
+
: failure('Active Run history is not materialized', cause)),
|
|
220
|
+
)
|
|
221
|
+
|
|
222
|
+
if (Option.isNone(loaded)) return Option.none()
|
|
223
|
+
const value = loaded.value
|
|
224
|
+
|
|
225
|
+
if (
|
|
226
|
+
value.progress.runId !== runId ||
|
|
227
|
+
value.throughSequence > throughSequence ||
|
|
228
|
+
canonicalRecordBytes(value.progressRecord) > MAX_RUN_CONTINUATION_BYTES ||
|
|
229
|
+
value.operational.some(
|
|
230
|
+
(record) => !isOperationalRecord(record) || !executionRunIds(record).includes(runId),
|
|
231
|
+
) ||
|
|
232
|
+
new Set(value.operational.map((record) => record.recordId)).size !== value.operational.length
|
|
233
|
+
) {
|
|
234
|
+
return yield* failure('Invalid active Run checkpoint')
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
return Option.some({
|
|
238
|
+
...value,
|
|
239
|
+
checkpoint: value,
|
|
240
|
+
get progress() {
|
|
241
|
+
return value.progress
|
|
242
|
+
},
|
|
243
|
+
record: value.progressRecord,
|
|
244
|
+
continuation: value.progress,
|
|
245
|
+
})
|
|
246
|
+
})
|
|
247
|
+
|
|
248
|
+
/** Resolve an active-only fact without presenting it as a canonical envelope or sequence. */
|
|
249
|
+
export const resolveExecutionEvidence = Effect.fnUntraced(function*(
|
|
250
|
+
threadId: ThreadId,
|
|
251
|
+
checkpoint: ExecutionCheckpoint,
|
|
252
|
+
ref: EvidenceReference,
|
|
253
|
+
) {
|
|
254
|
+
const operational = checkpoint.operational.find((record) => record.recordId === ref.recordId)
|
|
255
|
+
const record = operational ?? (yield* resolveEvidence(threadId, ref)).record
|
|
256
|
+
|
|
257
|
+
return record
|
|
258
|
+
})
|
|
259
|
+
|
|
260
|
+
/** Each selected page is at most eight records; total resident encoded evidence is bounded. */
|
|
261
|
+
export const runEvidence = (
|
|
262
|
+
threadId: ThreadId,
|
|
263
|
+
submissionId: SubmissionId,
|
|
264
|
+
throughSequence: CanonicalSequence,
|
|
265
|
+
afterSequence: CanonicalSequence = CanonicalSequence.make(0),
|
|
266
|
+
): Stream.Stream<CanonicalRecordEnvelope, ThreadStoreError, ThreadReader> =>
|
|
267
|
+
Stream.unwrap(
|
|
268
|
+
Effect.map(ThreadReader, (reader) =>
|
|
269
|
+
Stream.suspend(() => {
|
|
270
|
+
let after = afterSequence
|
|
271
|
+
let count = 0
|
|
272
|
+
let bytes = 0
|
|
273
|
+
const runId = runIdForSubmission(submissionId)
|
|
274
|
+
|
|
275
|
+
const controls = new Set([
|
|
276
|
+
submissionInputRecordId(submissionId),
|
|
277
|
+
submissionAbortRecordId(submissionId),
|
|
278
|
+
submissionSettlementRecordId(submissionId),
|
|
279
|
+
])
|
|
280
|
+
|
|
281
|
+
const next = (): Stream.Stream<CanonicalRecordEnvelope, ThreadStoreError> =>
|
|
282
|
+
Stream.unwrap(
|
|
283
|
+
Stream.runCollect(
|
|
284
|
+
reader.read({
|
|
285
|
+
threadId,
|
|
286
|
+
selection: { _tag: 'RunEvidence', runId, submissionId, throughSequence },
|
|
287
|
+
page: { limit: 8, ...(after === 0 ? {} : { afterSequence: after }) },
|
|
288
|
+
}),
|
|
289
|
+
).pipe(
|
|
290
|
+
Effect.mapError((cause) =>
|
|
291
|
+
cause._tag === 'ThreadStoreError'
|
|
292
|
+
? cause
|
|
293
|
+
: failure('Selected Run disappeared', cause)
|
|
294
|
+
),
|
|
295
|
+
Effect.flatMap((page) =>
|
|
296
|
+
Effect.gen(function*() {
|
|
297
|
+
if (page.length > 8) {
|
|
298
|
+
return yield* failure('Run evidence page exceeds its record bound')
|
|
299
|
+
}
|
|
300
|
+
for (const entry of page) {
|
|
301
|
+
if (
|
|
302
|
+
entry.threadId !== threadId ||
|
|
303
|
+
entry.sequence <= after ||
|
|
304
|
+
entry.sequence > throughSequence ||
|
|
305
|
+
entry.record.payload._tag === 'RunContinuation' ||
|
|
306
|
+
(!canonicalRunIds(entry.record).includes(runId) &&
|
|
307
|
+
!controls.has(entry.record.recordId))
|
|
308
|
+
) {
|
|
309
|
+
return yield* failure('Run evidence page has invalid identity or ordering')
|
|
310
|
+
}
|
|
311
|
+
after = entry.sequence
|
|
312
|
+
count += 1
|
|
313
|
+
bytes += yield* Effect.try({
|
|
314
|
+
try: () => canonicalRecordBytes(entry.record),
|
|
315
|
+
catch: (cause) => failure('Selected Run evidence is malformed', cause),
|
|
316
|
+
})
|
|
317
|
+
if (
|
|
318
|
+
count > MAX_RUN_EVIDENCE_RECORDS + RUN_TERMINAL_RESERVE_RECORDS ||
|
|
319
|
+
bytes > MAX_RUN_EVIDENCE_BYTES + MAX_RUN_TERMINAL_BYTES
|
|
320
|
+
) {
|
|
321
|
+
return yield* failure('Selected Run evidence exceeds its recovery bound')
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
return page.length < 8
|
|
326
|
+
? Stream.fromIterable(page)
|
|
327
|
+
: Stream.concat(Stream.fromIterable(page), Stream.suspend(next))
|
|
328
|
+
})
|
|
329
|
+
),
|
|
330
|
+
),
|
|
331
|
+
)
|
|
332
|
+
|
|
333
|
+
return next()
|
|
334
|
+
})),
|
|
335
|
+
)
|
|
336
|
+
|
|
337
|
+
/** A bounded selected-Run suffix, never a Thread-wide tail or an implicit replay fallback. */
|
|
338
|
+
export const validateSuffix = (
|
|
339
|
+
records: ReadonlyArray<CanonicalRecordEnvelope>,
|
|
340
|
+
through: CanonicalSequence,
|
|
341
|
+
) => {
|
|
342
|
+
const suffix = records.filter((record) => record.sequence > through)
|
|
343
|
+
|
|
344
|
+
return (
|
|
345
|
+
suffix.length <= MAX_RUN_RECOVERY_SUFFIX_RECORDS &&
|
|
346
|
+
suffix.reduce((bytes, record) => bytes + canonicalRecordBytes(record.record), 0) <=
|
|
347
|
+
MAX_RUN_RECOVERY_SUFFIX_BYTES
|
|
348
|
+
)
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* Selected semantic evidence without expanding model context. Worker acknowledgements and
|
|
353
|
+
* delivery repair must validate locator completeness before treating missing effects as closed.
|
|
354
|
+
*/
|
|
355
|
+
export const readRunEvidenceSnapshot = Effect.fnUntraced(function*(
|
|
356
|
+
threadId: ThreadId,
|
|
357
|
+
submissionId: SubmissionId,
|
|
358
|
+
throughSequence: CanonicalSequence,
|
|
359
|
+
) {
|
|
360
|
+
const records = yield* Stream.runCollect(runEvidence(threadId, submissionId, throughSequence))
|
|
361
|
+
const runId = runIdForSubmission(submissionId)
|
|
362
|
+
const own = records.filter((entry) => executionRunIds(entry.record).includes(runId))
|
|
363
|
+
const progress = yield* readContinuation(threadId, runId, throughSequence)
|
|
364
|
+
|
|
365
|
+
if (Option.isNone(progress)) {
|
|
366
|
+
const terminal = own.findLast(
|
|
367
|
+
({ record }) => record.payload._tag === 'SubmissionSettled' && record.payload.runId === runId,
|
|
368
|
+
)
|
|
369
|
+
|
|
370
|
+
if (terminal?.record.payload._tag === 'SubmissionSettled') {
|
|
371
|
+
if (
|
|
372
|
+
terminal.record.payload.historyRecordCount === undefined ||
|
|
373
|
+
own.filter((entry) => entry.sequence <= terminal.sequence).length !==
|
|
374
|
+
terminal.record.payload.historyRecordCount
|
|
375
|
+
) {
|
|
376
|
+
return yield* failure('Settled Run history is incomplete')
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
return records
|
|
380
|
+
}
|
|
381
|
+
if (own.some(({ record }) => !isPreContinuationFact(record))) {
|
|
382
|
+
return yield* failure('Active execution has no durable checkpoint')
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
return records
|
|
386
|
+
}
|
|
387
|
+
const checkpoint = progress.value
|
|
388
|
+
const cursor = checkpoint.continuation
|
|
389
|
+
|
|
390
|
+
if (cursor.submissionId !== submissionId) {
|
|
391
|
+
return yield* failure('Active checkpoint has another admitted owner')
|
|
392
|
+
}
|
|
393
|
+
if (
|
|
394
|
+
own.filter((entry) => entry.sequence <= checkpoint.throughSequence).length !==
|
|
395
|
+
checkpoint.canonicalRecords ||
|
|
396
|
+
!validateSuffix(own, checkpoint.throughSequence)
|
|
397
|
+
) {
|
|
398
|
+
return yield* failure('Selected execution history is incomplete or exceeds its suffix bound')
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
const resolve = (ref: EvidenceReference) => resolveExecutionEvidence(threadId, checkpoint.checkpoint, ref)
|
|
402
|
+
|
|
403
|
+
const input = yield* resolve(cursor.originalInput)
|
|
404
|
+
|
|
405
|
+
if (
|
|
406
|
+
input.payload._tag !== 'UserInputRecorded' ||
|
|
407
|
+
input.payload.submissionId !== submissionId ||
|
|
408
|
+
input.payload.kind !== 'user' ||
|
|
409
|
+
input.payload.runId !== runId
|
|
410
|
+
) {
|
|
411
|
+
return yield* failure('Active checkpoint has no original accepted input')
|
|
412
|
+
}
|
|
413
|
+
yield* resolve(cursor.lastFact)
|
|
414
|
+
if (cursor.savedContext !== undefined) {
|
|
415
|
+
const context = yield* resolve(cursor.savedContext)
|
|
416
|
+
|
|
417
|
+
if (context.payload._tag !== 'RunContextRecorded' || context.payload.runId !== runId) {
|
|
418
|
+
return yield* failure('Active checkpoint has invalid saved context')
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
if (cursor.latestResponse !== undefined) {
|
|
422
|
+
const response = yield* resolve(cursor.latestResponse)
|
|
423
|
+
|
|
424
|
+
if (
|
|
425
|
+
response.payload._tag !== 'ModelResponseRecorded' ||
|
|
426
|
+
response.payload.runId !== runId ||
|
|
427
|
+
response.payload.turn !== cursor.accounting.committedTurns
|
|
428
|
+
) {
|
|
429
|
+
return yield* failure('Active checkpoint has invalid operation evidence')
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
if (cursor.terminal !== undefined) {
|
|
433
|
+
const terminal = yield* resolve(cursor.terminal)
|
|
434
|
+
|
|
435
|
+
if (
|
|
436
|
+
terminal.payload._tag !== 'RunCompleted' &&
|
|
437
|
+
terminal.payload._tag !== 'RunFailed' &&
|
|
438
|
+
terminal.payload._tag !== 'SubmissionSettled'
|
|
439
|
+
) {
|
|
440
|
+
return yield* failure('Active checkpoint has invalid terminal evidence')
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
return records
|
|
445
|
+
})
|
|
446
|
+
|
|
447
|
+
/** Called inside the adapter's existing mutation, before publishing any fact or index. */
|
|
448
|
+
export const validateProgressAppend = Effect.fnUntraced(function*(
|
|
449
|
+
records: ReadonlyArray<ProgressAppendRecord>,
|
|
450
|
+
updates: ReadonlyArray<ExecutionUpdate> = [],
|
|
451
|
+
): Effect.fn.Return<void, ThreadStoreError, ProgressAppendReader> {
|
|
452
|
+
const reader = yield* ProgressAppendReader
|
|
453
|
+
|
|
454
|
+
if (records.some((record) => record.continuation !== undefined)) {
|
|
455
|
+
return yield* failure('Interpreter progress must be committed as active execution state')
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
const operational = new Map(
|
|
459
|
+
updates.flatMap((update) => update.facts.map((fact) => [fact.recordId, fact] as const)),
|
|
460
|
+
)
|
|
461
|
+
|
|
462
|
+
const factsAndProgress = [
|
|
463
|
+
...records,
|
|
464
|
+
...prepareProgressAppend([...operational.values()]),
|
|
465
|
+
...prepareProgressAppend(updates.map((update) => update.checkpoint.progressRecord)),
|
|
466
|
+
]
|
|
467
|
+
|
|
468
|
+
const seen = new Set<RunId>()
|
|
469
|
+
let progressStarted = false
|
|
470
|
+
|
|
471
|
+
for (const record of factsAndProgress) {
|
|
472
|
+
const next = record.continuation
|
|
473
|
+
|
|
474
|
+
if (next === undefined) {
|
|
475
|
+
if (progressStarted) {
|
|
476
|
+
return yield* failure('Execution facts cannot follow their continuation')
|
|
477
|
+
}
|
|
478
|
+
continue
|
|
479
|
+
}
|
|
480
|
+
progressStarted = true
|
|
481
|
+
if (record.recordBytes > MAX_RUN_CONTINUATION_BYTES) {
|
|
482
|
+
return yield* failure('Encoded continuation exceeds its record byte bound')
|
|
483
|
+
}
|
|
484
|
+
if (seen.has(next.runId)) {
|
|
485
|
+
return yield* failure('One atomic batch cannot publish conflicting Run progress')
|
|
486
|
+
}
|
|
487
|
+
seen.add(next.runId)
|
|
488
|
+
|
|
489
|
+
const update = updates.find((value) => value.checkpoint.progress.runId === next.runId)
|
|
490
|
+
|
|
491
|
+
if (
|
|
492
|
+
update === undefined ||
|
|
493
|
+
update.previousRevision + 1 !== next.revision ||
|
|
494
|
+
update.facts.some((fact) => !isOperationalRecord(fact)) ||
|
|
495
|
+
(update.retire && (next.position !== 'settled' || next.terminal === undefined))
|
|
496
|
+
) {
|
|
497
|
+
return yield* failure('Invalid active execution transition')
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
const facts = factsAndProgress.filter(
|
|
501
|
+
(fact) => fact.continuation === undefined && fact.runIds.includes(next.runId),
|
|
502
|
+
)
|
|
503
|
+
|
|
504
|
+
const frontier = facts.find((fact) => fact.recordId === next.lastFact.recordId)
|
|
505
|
+
const prior = yield* reader.previous(next.runId)
|
|
506
|
+
|
|
507
|
+
const retained = prior === undefined
|
|
508
|
+
? (yield* reader.initial(next)).filter((fact) => executionRunIds(fact).includes(next.runId))
|
|
509
|
+
: []
|
|
510
|
+
|
|
511
|
+
const retainedBytes = retained.reduce((bytes, fact) => bytes + canonicalRecordBytes(fact), 0)
|
|
512
|
+
const factBytes = facts.reduce((bytes, fact) => bytes + fact.recordBytes, 0)
|
|
513
|
+
const turnBase = prior?.turn === next.turn ? prior.turnBytes : retainedBytes
|
|
514
|
+
const closing = facts.filter((fact) => fact.terminal)
|
|
515
|
+
|
|
516
|
+
if (
|
|
517
|
+
frontier === undefined ||
|
|
518
|
+
retained.length > MAX_RUN_RECOVERY_SUFFIX_RECORDS ||
|
|
519
|
+
retainedBytes > MAX_RUN_RECOVERY_SUFFIX_BYTES ||
|
|
520
|
+
retained.some(
|
|
521
|
+
(fact) => !isPreContinuationFact(fact) || !executionRunIds(fact).includes(next.runId),
|
|
522
|
+
) ||
|
|
523
|
+
frontier.recordId !== next.lastFact.recordId ||
|
|
524
|
+
next.revision !== (prior?.revision ?? 0) + 1 ||
|
|
525
|
+
next.recordBytes < (prior?.recordBytes ?? 0) ||
|
|
526
|
+
next.turn < (prior?.turn ?? 0) ||
|
|
527
|
+
(prior !== undefined && next.turn === prior.turn && next.turnBytes <= prior.turnBytes) ||
|
|
528
|
+
next.recordBytes !== (prior?.recordBytes ?? retainedBytes) + factBytes ||
|
|
529
|
+
next.terminalUsageBytes !==
|
|
530
|
+
(prior?.terminalUsageBytes ?? 0) +
|
|
531
|
+
facts.reduce((bytes, fact) => bytes + fact.terminalUsageBytes, 0) ||
|
|
532
|
+
next.turnBytes !== turnBase + factBytes + record.recordBytes ||
|
|
533
|
+
next.recordCount !== (prior?.recordCount ?? retained.length) + facts.length ||
|
|
534
|
+
next.terminalRecords !== (prior?.terminalRecords ?? 0) + closing.length ||
|
|
535
|
+
next.terminalBytes !==
|
|
536
|
+
(prior?.terminalBytes ?? 0) +
|
|
537
|
+
closing.reduce((bytes, fact) => bytes + fact.recordBytes, 0) +
|
|
538
|
+
(closing.length > 0 ? record.recordBytes : 0) ||
|
|
539
|
+
(prior !== undefined &&
|
|
540
|
+
(prior.submissionId !== next.submissionId ||
|
|
541
|
+
!Schema.toEquivalence(EvidenceReference)(prior.originalInput, next.originalInput) ||
|
|
542
|
+
(prior.savedContext !== undefined &&
|
|
543
|
+
(next.savedContext === undefined ||
|
|
544
|
+
!Schema.toEquivalence(EvidenceReference)(prior.savedContext, next.savedContext))))) ||
|
|
545
|
+
(prior !== undefined &&
|
|
546
|
+
(
|
|
547
|
+
[
|
|
548
|
+
'committedTurns',
|
|
549
|
+
'toolCalls',
|
|
550
|
+
'programmaticToolCalls',
|
|
551
|
+
'modelRestarts',
|
|
552
|
+
'modelCalls',
|
|
553
|
+
'inputTokens',
|
|
554
|
+
'outputTokens',
|
|
555
|
+
'costMicrousd',
|
|
556
|
+
'unobservedModelCalls',
|
|
557
|
+
'accountedToolTurn',
|
|
558
|
+
] satisfies ReadonlyArray<keyof ContinuationAccounting>
|
|
559
|
+
).some((field) => next.accounting[field] < prior.accounting[field])) ||
|
|
560
|
+
(prior?.accounting.finalizationUsed === true && !next.accounting.finalizationUsed)
|
|
561
|
+
) {
|
|
562
|
+
return yield* failure('Run continuation conflicts with its canonical frontier or revision')
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
})
|
|
566
|
+
|
|
567
|
+
interface ChildResultCapacity {
|
|
568
|
+
readonly resultBytes: number
|
|
569
|
+
readonly remainingBytes: number
|
|
570
|
+
readonly remainingRecords: number
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
interface ProgressState {
|
|
574
|
+
readonly continuation: RunContinuation
|
|
575
|
+
readonly record: RecordEnvelope
|
|
576
|
+
readonly response?: ModelResponseRecorded
|
|
577
|
+
readonly results: ReadonlyMap<ToolCallId, ToolCallSettled>
|
|
578
|
+
readonly completionBytes?: number
|
|
579
|
+
readonly childResults: ReadonlyMap<ToolCallId, ChildResultCapacity>
|
|
580
|
+
readonly checkpoint?: ExecutionCheckpoint
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
type CapacityState = Omit<ProgressState, 'continuation' | 'record'> & {
|
|
584
|
+
readonly continuation: Pick<
|
|
585
|
+
RunContinuation,
|
|
586
|
+
| 'runId'
|
|
587
|
+
| 'position'
|
|
588
|
+
| 'turn'
|
|
589
|
+
| 'turnBytes'
|
|
590
|
+
| 'recordBytes'
|
|
591
|
+
| 'recordCount'
|
|
592
|
+
| 'accounting'
|
|
593
|
+
| 'terminalUsageBytes'
|
|
594
|
+
| 'terminalBytes'
|
|
595
|
+
| 'terminalRecords'
|
|
596
|
+
>
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
/** Headers/identities must themselves fit a continuation. Leave room for a result's cursor too. */
|
|
600
|
+
const resultCapacity = (state: CapacityState): { bytes: number; records: number } => {
|
|
601
|
+
const response = state.response
|
|
602
|
+
|
|
603
|
+
if (
|
|
604
|
+
response === undefined ||
|
|
605
|
+
state.continuation.position === 'settling' ||
|
|
606
|
+
state.continuation.position === 'settled' ||
|
|
607
|
+
state.continuation.accounting.accountedToolTurn >= response.turn
|
|
608
|
+
) {
|
|
609
|
+
return { bytes: 0, records: 0 }
|
|
610
|
+
}
|
|
611
|
+
let bytes = 0
|
|
612
|
+
let records = 0
|
|
613
|
+
|
|
614
|
+
for (const operation of response.toolOperations) {
|
|
615
|
+
if (state.results.has(operation.toolCallId)) continue
|
|
616
|
+
|
|
617
|
+
const identity = JSON.stringify({
|
|
618
|
+
recordId: toolCallSettledRecordId(response.runId, response.turn, operation.toolCallId),
|
|
619
|
+
payload: {
|
|
620
|
+
_tag: 'ToolCallSettled',
|
|
621
|
+
runId: response.runId,
|
|
622
|
+
toolCallId: operation.toolCallId,
|
|
623
|
+
toolName: operation.toolName,
|
|
624
|
+
result: null,
|
|
625
|
+
isFailure: false,
|
|
626
|
+
},
|
|
627
|
+
})
|
|
628
|
+
|
|
629
|
+
// An uncertain ordinary call may later close with exact supplier/operator truth, rather
|
|
630
|
+
// than the interpreter's truncated result. Attached joins retain their frozen allocation.
|
|
631
|
+
const resultBytes = operation.executionKind === 'ordinary' && operation.executionClass !== 'readonly'
|
|
632
|
+
? MAX_PERSISTED_JSON_BYTES
|
|
633
|
+
: Math.max(
|
|
634
|
+
response.toolResultMaxBytes,
|
|
635
|
+
state.childResults.get(operation.toolCallId)?.resultBytes ?? 0,
|
|
636
|
+
)
|
|
637
|
+
|
|
638
|
+
const joining = state.childResults.get(operation.toolCallId)
|
|
639
|
+
|
|
640
|
+
bytes += utf8ByteLength(identity) +
|
|
641
|
+
resultBytes +
|
|
642
|
+
(joining?.remainingBytes ?? 0) +
|
|
643
|
+
response.toolSelectionMaxBytes +
|
|
644
|
+
2 * MAX_RUN_CONTINUATION_BYTES +
|
|
645
|
+
256
|
|
646
|
+
records += joining?.remainingRecords ?? 1
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
return { bytes, records }
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
const emptyAccounting = (): ContinuationAccounting => ({
|
|
653
|
+
committedTurns: 0,
|
|
654
|
+
toolCalls: 0,
|
|
655
|
+
programmaticToolCalls: 0,
|
|
656
|
+
consecutiveToolFailures: 0,
|
|
657
|
+
finalizationUsed: false,
|
|
658
|
+
modelRestarts: 0,
|
|
659
|
+
modelCalls: 0,
|
|
660
|
+
unobservedModelCalls: 0,
|
|
661
|
+
inputTokens: 0,
|
|
662
|
+
outputTokens: 0,
|
|
663
|
+
lastInputTokens: 0,
|
|
664
|
+
lastOutputTokens: 0,
|
|
665
|
+
costMicrousd: 0,
|
|
666
|
+
accountedToolTurn: 0,
|
|
667
|
+
})
|
|
668
|
+
|
|
669
|
+
/** Recompute semantic progress solely from immutable owning facts. No port grants authority. */
|
|
670
|
+
const advanceFacts = Effect.fnUntraced(function*(
|
|
671
|
+
previous: ProgressState | undefined,
|
|
672
|
+
facts: ReadonlyArray<RecordEnvelope>,
|
|
673
|
+
submissionId: SubmissionId,
|
|
674
|
+
initialBytes: number,
|
|
675
|
+
) {
|
|
676
|
+
const accounting = { ...(previous?.continuation.accounting ?? emptyAccounting()) }
|
|
677
|
+
let savedContext = previous?.continuation.savedContext
|
|
678
|
+
let latestResponse = previous?.continuation.latestResponse
|
|
679
|
+
let terminal = previous?.continuation.terminal
|
|
680
|
+
let position = previous?.continuation.position ?? 'starting'
|
|
681
|
+
let response = previous?.response
|
|
682
|
+
let results = new Map(previous?.results)
|
|
683
|
+
let childResults = new Map(previous?.childResults)
|
|
684
|
+
let completionBytes = previous?.completionBytes
|
|
685
|
+
let factBytes = 0
|
|
686
|
+
let turn = previous?.continuation.turn ?? 1
|
|
687
|
+
let usageCharge = 0
|
|
688
|
+
let closingBytes = 0
|
|
689
|
+
let closingCount = 0
|
|
690
|
+
|
|
691
|
+
for (const fact of facts) {
|
|
692
|
+
const payload = fact.payload
|
|
693
|
+
const captured = recordEncoding(fact)
|
|
694
|
+
|
|
695
|
+
if (Result.isFailure(captured)) {
|
|
696
|
+
return yield* failure('Cannot account canonical progress facts', captured.failure)
|
|
697
|
+
}
|
|
698
|
+
const encoded = captured.success
|
|
699
|
+
|
|
700
|
+
factBytes += encoded.bytes
|
|
701
|
+
if ('turn' in payload) turn = Math.max(turn, payload.turn)
|
|
702
|
+
usageCharge += encoded.progress.terminalUsageBytes
|
|
703
|
+
if (encoded.progress.terminal) {
|
|
704
|
+
closingBytes += encoded.bytes
|
|
705
|
+
closingCount++
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
switch (payload._tag) {
|
|
709
|
+
case 'RunStarted':
|
|
710
|
+
position = 'awaiting-model'
|
|
711
|
+
break
|
|
712
|
+
case 'RunContextRecorded':
|
|
713
|
+
if (savedContext !== undefined) {
|
|
714
|
+
return yield* failure("A Run's original context cannot be replaced")
|
|
715
|
+
}
|
|
716
|
+
savedContext = EvidenceReference.make({ recordId: fact.recordId })
|
|
717
|
+
position = 'awaiting-model'
|
|
718
|
+
break
|
|
719
|
+
case 'ModelResponseRecorded': {
|
|
720
|
+
if (savedContext === undefined) {
|
|
721
|
+
return yield* failure('A model response has no saved original Run context')
|
|
722
|
+
}
|
|
723
|
+
if (payload.turn !== accounting.committedTurns + 1) {
|
|
724
|
+
return yield* failure('Canonical Turn progress must advance once')
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
const messages = payload.messages
|
|
728
|
+
|
|
729
|
+
const calls = messages.content.flatMap((message) =>
|
|
730
|
+
message.role === 'assistant' && typeof message.content !== 'string'
|
|
731
|
+
? message.content.filter((part) => part.type === 'tool-call')
|
|
732
|
+
: []
|
|
733
|
+
)
|
|
734
|
+
|
|
735
|
+
const summary = payload.modelUsage === undefined
|
|
736
|
+
? undefined
|
|
737
|
+
: yield* continuationUsage(payload.modelUsage).pipe(
|
|
738
|
+
Effect.mapError((cause) => failure('Invalid usage evidence', cause)),
|
|
739
|
+
)
|
|
740
|
+
|
|
741
|
+
accounting.committedTurns = payload.turn
|
|
742
|
+
accounting.toolCalls += calls.length
|
|
743
|
+
accounting.modelCalls += summary?.modelCalls ?? 1
|
|
744
|
+
accounting.inputTokens += summary?.inputTokens ?? payload.inputTokens ?? 0
|
|
745
|
+
accounting.outputTokens += summary?.outputTokens ?? payload.outputTokens ?? 0
|
|
746
|
+
accounting.costMicrousd += summary?.costMicrousd ?? payload.costMicrousd ?? 0
|
|
747
|
+
accounting.lastInputTokens = summary?.inputTokens ?? payload.inputTokens ?? 0
|
|
748
|
+
accounting.lastOutputTokens = summary?.outputTokens ?? payload.outputTokens ?? 0
|
|
749
|
+
accounting.unobservedModelCalls += payload.unobservedModelCalls ?? 0
|
|
750
|
+
latestResponse = EvidenceReference.make({ recordId: fact.recordId })
|
|
751
|
+
response = payload
|
|
752
|
+
results = new Map()
|
|
753
|
+
childResults = new Map()
|
|
754
|
+
position = payload.toolOperations.length === 0 ? 'awaiting-model' : 'processing-operations'
|
|
755
|
+
break
|
|
756
|
+
}
|
|
757
|
+
case 'ToolCallSettled':
|
|
758
|
+
results.set(payload.toolCallId, payload)
|
|
759
|
+
childResults.delete(payload.toolCallId)
|
|
760
|
+
break
|
|
761
|
+
case 'ToolCallUnknown':
|
|
762
|
+
position = 'unknown'
|
|
763
|
+
break
|
|
764
|
+
case 'ToolCallResolved':
|
|
765
|
+
position = 'processing-operations'
|
|
766
|
+
break
|
|
767
|
+
case 'ToolApprovalRequested':
|
|
768
|
+
position = 'waiting-approval'
|
|
769
|
+
break
|
|
770
|
+
case 'ToolApprovalDecided':
|
|
771
|
+
position = 'processing-operations'
|
|
772
|
+
break
|
|
773
|
+
case 'SubagentRequested':
|
|
774
|
+
childResults.set(payload.toolCallId, {
|
|
775
|
+
resultBytes: Math.min(
|
|
776
|
+
MAX_PERSISTED_JSON_BYTES,
|
|
777
|
+
payload.budget?.allocation.resultBytes ?? MAX_PERSISTED_JSON_BYTES,
|
|
778
|
+
),
|
|
779
|
+
remainingBytes: RUN_TERMINAL_RESERVE_BYTES,
|
|
780
|
+
remainingRecords: 4,
|
|
781
|
+
})
|
|
782
|
+
position = 'waiting-dependency'
|
|
783
|
+
break
|
|
784
|
+
case 'SubagentStarted':
|
|
785
|
+
{
|
|
786
|
+
const reserved = childResults.get(payload.toolCallId)
|
|
787
|
+
|
|
788
|
+
if (reserved !== undefined) {
|
|
789
|
+
childResults.set(payload.toolCallId, {
|
|
790
|
+
...reserved,
|
|
791
|
+
remainingBytes: Math.max(
|
|
792
|
+
0,
|
|
793
|
+
reserved.remainingBytes - canonicalRecordBytes(fact) - MAX_RUN_CONTINUATION_BYTES,
|
|
794
|
+
),
|
|
795
|
+
remainingRecords: reserved.remainingRecords - 1,
|
|
796
|
+
})
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
position = 'waiting-dependency'
|
|
800
|
+
break
|
|
801
|
+
case 'RunPolicyUsageReserved':
|
|
802
|
+
if (
|
|
803
|
+
payload.programmaticToolCalls < accounting.programmaticToolCalls ||
|
|
804
|
+
(accounting.finalizationUsed && !payload.finalizationUsed)
|
|
805
|
+
) {
|
|
806
|
+
return yield* failure('Run reservations cannot be refunded')
|
|
807
|
+
}
|
|
808
|
+
accounting.programmaticToolCalls = payload.programmaticToolCalls
|
|
809
|
+
accounting.finalizationUsed = payload.finalizationUsed
|
|
810
|
+
break
|
|
811
|
+
case 'ModelCallAborted': {
|
|
812
|
+
if (payload.restart !== (accounting.modelRestarts ?? 0) + 1) {
|
|
813
|
+
return yield* failure('Model restart progress must advance once')
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
const summary = yield* continuationUsage(payload.modelUsage).pipe(
|
|
817
|
+
Effect.mapError((cause) => failure('Invalid cancelled model usage', cause)),
|
|
818
|
+
)
|
|
819
|
+
|
|
820
|
+
accounting.modelRestarts = payload.restart
|
|
821
|
+
accounting.modelCalls += summary.modelCalls
|
|
822
|
+
accounting.inputTokens += summary.inputTokens
|
|
823
|
+
accounting.outputTokens += summary.outputTokens
|
|
824
|
+
accounting.costMicrousd += summary.costMicrousd
|
|
825
|
+
accounting.unobservedModelCalls += payload.unobservedModelCalls
|
|
826
|
+
break
|
|
827
|
+
}
|
|
828
|
+
case 'RunCompleted':
|
|
829
|
+
case 'RunFailed':
|
|
830
|
+
completionBytes = payload._tag === 'RunCompleted'
|
|
831
|
+
? utf8ByteLength(JSON.stringify(payload.output)) +
|
|
832
|
+
(payload.runDisposition === undefined
|
|
833
|
+
? 0
|
|
834
|
+
: utf8ByteLength(JSON.stringify(payload.runDisposition)))
|
|
835
|
+
: undefined
|
|
836
|
+
terminal = EvidenceReference.make({ recordId: fact.recordId })
|
|
837
|
+
position = 'settling'
|
|
838
|
+
break
|
|
839
|
+
case 'SubmissionSettled':
|
|
840
|
+
if (payload.submissionId !== submissionId) break
|
|
841
|
+
terminal = EvidenceReference.make({ recordId: fact.recordId })
|
|
842
|
+
position = 'settled'
|
|
843
|
+
if (payload.usageSummary !== undefined) {
|
|
844
|
+
accounting.modelCalls = payload.usageSummary.modelCalls
|
|
845
|
+
accounting.inputTokens = payload.usageSummary.inputTokens.total
|
|
846
|
+
accounting.outputTokens = payload.usageSummary.outputTokens.total
|
|
847
|
+
accounting.costMicrousd = payload.usageSummary.costMicrousd
|
|
848
|
+
accounting.unobservedModelCalls = payload.usageSummary.unobservedModelCalls ?? accounting.unobservedModelCalls
|
|
849
|
+
}
|
|
850
|
+
break
|
|
851
|
+
default:
|
|
852
|
+
break
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
if (
|
|
856
|
+
response !== undefined &&
|
|
857
|
+
response.turn > accounting.accountedToolTurn &&
|
|
858
|
+
response.toolOperations.every((operation) => results.has(operation.toolCallId))
|
|
859
|
+
) {
|
|
860
|
+
const parts = response.messages.content.flatMap((message) =>
|
|
861
|
+
message.role === 'assistant' && typeof message.content !== 'string' ? message.content : []
|
|
862
|
+
)
|
|
863
|
+
|
|
864
|
+
for (const call of parts) {
|
|
865
|
+
if (call.type !== 'tool-call') continue
|
|
866
|
+
|
|
867
|
+
const result = call.providerExecuted
|
|
868
|
+
? parts.find((part) => part.type === 'tool-result' && part.id === call.id)
|
|
869
|
+
: results.get(ToolCallId.make(call.id))
|
|
870
|
+
|
|
871
|
+
if (
|
|
872
|
+
result === undefined ||
|
|
873
|
+
!('isFailure' in result) ||
|
|
874
|
+
// Only canonical application settlements carry budget-rejection authority.
|
|
875
|
+
(!call.providerExecuted && 'budgetRejected' in result && result.budgetRejected === true)
|
|
876
|
+
) {
|
|
877
|
+
continue
|
|
878
|
+
}
|
|
879
|
+
accounting.consecutiveToolFailures = result.isFailure
|
|
880
|
+
? accounting.consecutiveToolFailures + 1
|
|
881
|
+
: 0
|
|
882
|
+
}
|
|
883
|
+
accounting.accountedToolTurn = response.turn
|
|
884
|
+
results.clear()
|
|
885
|
+
if (position !== 'settling' && position !== 'settled') position = 'awaiting-model'
|
|
886
|
+
}
|
|
887
|
+
const last = facts.at(-1)
|
|
888
|
+
|
|
889
|
+
if (last === undefined) return yield* failure('Progress has no owning fact')
|
|
890
|
+
if (previous?.continuation.position === 'settled') position = 'settled'
|
|
891
|
+
else if (previous?.continuation.position === 'settling' && position !== 'settled') {
|
|
892
|
+
position = 'settling'
|
|
893
|
+
}
|
|
894
|
+
|
|
895
|
+
const turnBase = (previous?.continuation.turn === turn ? previous.continuation.turnBytes : initialBytes) +
|
|
896
|
+
factBytes
|
|
897
|
+
|
|
898
|
+
const terminalUsageBytes = (previous?.continuation.terminalUsageBytes ?? 0) + usageCharge
|
|
899
|
+
|
|
900
|
+
return {
|
|
901
|
+
accounting,
|
|
902
|
+
savedContext,
|
|
903
|
+
latestResponse,
|
|
904
|
+
terminal,
|
|
905
|
+
position,
|
|
906
|
+
response,
|
|
907
|
+
results,
|
|
908
|
+
childResults,
|
|
909
|
+
completionBytes,
|
|
910
|
+
last,
|
|
911
|
+
factBytes,
|
|
912
|
+
turn,
|
|
913
|
+
turnBase,
|
|
914
|
+
usageCharge,
|
|
915
|
+
terminalUsageBytes,
|
|
916
|
+
terminalBytes: (previous?.continuation.terminalBytes ?? 0) + closingBytes,
|
|
917
|
+
terminalRecords: (previous?.continuation.terminalRecords ?? 0) + closingCount,
|
|
918
|
+
closing: closingCount > 0,
|
|
919
|
+
}
|
|
920
|
+
})
|
|
921
|
+
|
|
922
|
+
/**
|
|
923
|
+
* Verify retained history. Mutable checkpoint revisions are deliberately not historical proof.
|
|
924
|
+
* Active original context is still checked against its exact retained historyDigest.
|
|
925
|
+
*/
|
|
926
|
+
export const verifyRunContinuations = Effect.fnUntraced(function*(
|
|
927
|
+
records: ReadonlyArray<CanonicalRecordEnvelope>,
|
|
928
|
+
options?: {
|
|
929
|
+
readonly verifyContext?: boolean
|
|
930
|
+
readonly executions?: ReadonlyArray<ExecutionCheckpoint>
|
|
931
|
+
},
|
|
932
|
+
) {
|
|
933
|
+
const inputs = records.filter(
|
|
934
|
+
({ record }) =>
|
|
935
|
+
record.payload._tag === 'UserInputRecorded' &&
|
|
936
|
+
record.payload.kind === 'user' &&
|
|
937
|
+
record.payload.submissionId !== undefined,
|
|
938
|
+
)
|
|
939
|
+
|
|
940
|
+
if (records.some(({ record }) => record.payload._tag === 'RunContinuation')) {
|
|
941
|
+
return yield* failure('Mutable interpreter progress is not canonical history')
|
|
942
|
+
}
|
|
943
|
+
for (const input of inputs) {
|
|
944
|
+
const payload = input.record.payload
|
|
945
|
+
|
|
946
|
+
if (payload._tag !== 'UserInputRecorded' || payload.submissionId === undefined) continue
|
|
947
|
+
const runId = runIdForSubmission(payload.submissionId)
|
|
948
|
+
const own = records.filter((entry) => executionRunIds(entry.record).includes(runId))
|
|
949
|
+
|
|
950
|
+
const terminal = own.findLast(
|
|
951
|
+
({ record }) => record.payload._tag === 'SubmissionSettled' && record.payload.runId === runId,
|
|
952
|
+
)
|
|
953
|
+
|
|
954
|
+
if (terminal?.record.payload._tag === 'SubmissionSettled') {
|
|
955
|
+
if (
|
|
956
|
+
terminal.record.payload.historyRecordCount === undefined ||
|
|
957
|
+
own.filter((entry) => entry.sequence <= terminal.sequence).length !==
|
|
958
|
+
terminal.record.payload.historyRecordCount
|
|
959
|
+
) {
|
|
960
|
+
return yield* failure('Terminal history differs from its retained fact count')
|
|
961
|
+
}
|
|
962
|
+
continue
|
|
963
|
+
}
|
|
964
|
+
if (options?.verifyContext === false) continue
|
|
965
|
+
const checkpoint = options?.executions?.find((value) => value.progress.runId === runId)
|
|
966
|
+
|
|
967
|
+
if (checkpoint === undefined) {
|
|
968
|
+
if (own.some(({ record }) => !isPreContinuationFact(record))) {
|
|
969
|
+
return yield* failure('Active execution is missing its authoritative checkpoint')
|
|
970
|
+
}
|
|
971
|
+
continue
|
|
972
|
+
}
|
|
973
|
+
|
|
974
|
+
const saved = checkpoint.operational.find(
|
|
975
|
+
({ payload }) => payload._tag === 'RunContextRecorded',
|
|
976
|
+
)
|
|
977
|
+
|
|
978
|
+
if (saved?.payload._tag === 'RunContextRecorded') {
|
|
979
|
+
yield* validateContextBoundary(saved.payload, input).pipe(
|
|
980
|
+
Effect.mapError((cause) => failure('Context has an invalid admission boundary', cause)),
|
|
981
|
+
)
|
|
982
|
+
yield* readRunContext(saved.payload, input, saved.recordId).pipe(
|
|
983
|
+
Effect.mapError((cause) => failure('Context differs from its original history', cause)),
|
|
984
|
+
)
|
|
985
|
+
}
|
|
986
|
+
}
|
|
987
|
+
})
|
|
988
|
+
|
|
989
|
+
/**
|
|
990
|
+
* Prepare progress for the existing atomic append/publication. Accept the returned state only
|
|
991
|
+
* after that append commits; a failed or stale producer cannot publish speculative progress.
|
|
992
|
+
* One writer retains bounded metadata for its active Run, not its historical results or IDs.
|
|
993
|
+
*/
|
|
994
|
+
export const makeProgressWriter = Effect.fnUntraced(function*(
|
|
995
|
+
threadId: ThreadId,
|
|
996
|
+
deploymentId: DeploymentId,
|
|
997
|
+
) {
|
|
998
|
+
const reader = yield* ThreadReader
|
|
999
|
+
const gate = yield* Semaphore.make(1)
|
|
1000
|
+
const cached = new Map<RunId, ProgressState>()
|
|
1001
|
+
|
|
1002
|
+
let verifiedEmpty:
|
|
1003
|
+
| {
|
|
1004
|
+
readonly runId: RunId
|
|
1005
|
+
readonly through: CanonicalSequence
|
|
1006
|
+
readonly writer: CurrentRunWriter['Service']
|
|
1007
|
+
}
|
|
1008
|
+
| undefined
|
|
1009
|
+
|
|
1010
|
+
const reservations = new Map<
|
|
1011
|
+
RecordId,
|
|
1012
|
+
{
|
|
1013
|
+
readonly runId: RunId
|
|
1014
|
+
readonly turn: number
|
|
1015
|
+
readonly bytes: number
|
|
1016
|
+
readonly records: number
|
|
1017
|
+
}
|
|
1018
|
+
>()
|
|
1019
|
+
|
|
1020
|
+
const stagedUsage = new Map<RunId, number>()
|
|
1021
|
+
|
|
1022
|
+
const provide = <A, E>(effect: Effect.Effect<A, E, ThreadReader>) =>
|
|
1023
|
+
Effect.provideService(effect, ThreadReader, reader)
|
|
1024
|
+
|
|
1025
|
+
const loadUncachedState = Effect.fnUntraced(function*(runId: RunId, after: CanonicalSequence) {
|
|
1026
|
+
const loaded = yield* provide(readContinuation(threadId, runId, after))
|
|
1027
|
+
|
|
1028
|
+
if (Option.isNone(loaded)) return undefined
|
|
1029
|
+
const continuation = loaded.value.continuation
|
|
1030
|
+
let response: ModelResponseRecorded | undefined
|
|
1031
|
+
let completionBytes: number | undefined
|
|
1032
|
+
const results = new Map<ToolCallId, ToolCallSettled>()
|
|
1033
|
+
|
|
1034
|
+
const childResults = new Map<ToolCallId, ChildResultCapacity>()
|
|
1035
|
+
|
|
1036
|
+
if (continuation.latestResponse !== undefined) {
|
|
1037
|
+
const evidence = yield* provide(resolveEvidence(threadId, continuation.latestResponse))
|
|
1038
|
+
|
|
1039
|
+
if (
|
|
1040
|
+
evidence.record.payload._tag !== 'ModelResponseRecorded' ||
|
|
1041
|
+
evidence.record.payload.runId !== runId
|
|
1042
|
+
) {
|
|
1043
|
+
return yield* failure('Continuation operation reference is invalid')
|
|
1044
|
+
}
|
|
1045
|
+
response = evidence.record.payload
|
|
1046
|
+
if (continuation.accounting.accountedToolTurn < response.turn) {
|
|
1047
|
+
for (const operation of response.toolOperations) {
|
|
1048
|
+
const found = yield* provide(
|
|
1049
|
+
getRecord({
|
|
1050
|
+
threadId,
|
|
1051
|
+
recordId: toolCallSettledRecordId(runId, response.turn, operation.toolCallId),
|
|
1052
|
+
}),
|
|
1053
|
+
)
|
|
1054
|
+
|
|
1055
|
+
if (Option.isSome(found)) {
|
|
1056
|
+
const result = found.value.record.payload
|
|
1057
|
+
|
|
1058
|
+
if (
|
|
1059
|
+
result._tag !== 'ToolCallSettled' ||
|
|
1060
|
+
result.runId !== runId ||
|
|
1061
|
+
result.toolCallId !== operation.toolCallId ||
|
|
1062
|
+
result.toolName !== operation.toolName
|
|
1063
|
+
) {
|
|
1064
|
+
return yield* failure('Continuation result identity is invalid')
|
|
1065
|
+
}
|
|
1066
|
+
results.set(operation.toolCallId, result)
|
|
1067
|
+
} else if (operation.executionKind === 'delegation') {
|
|
1068
|
+
const requested = yield* provide(
|
|
1069
|
+
getRecord({
|
|
1070
|
+
threadId,
|
|
1071
|
+
recordId: subagentRequestedRecordId(runId, operation.toolCallId),
|
|
1072
|
+
}),
|
|
1073
|
+
)
|
|
1074
|
+
|
|
1075
|
+
if (
|
|
1076
|
+
Option.isSome(requested) &&
|
|
1077
|
+
requested.value.record.payload._tag === 'SubagentRequested'
|
|
1078
|
+
) {
|
|
1079
|
+
if (
|
|
1080
|
+
requested.value.record.payload.runId !== runId ||
|
|
1081
|
+
requested.value.record.payload.toolCallId !== operation.toolCallId
|
|
1082
|
+
) {
|
|
1083
|
+
return yield* failure('Continuation child request identity is invalid')
|
|
1084
|
+
}
|
|
1085
|
+
|
|
1086
|
+
const started = yield* provide(
|
|
1087
|
+
getRecord({
|
|
1088
|
+
threadId,
|
|
1089
|
+
recordId: subagentStartedRecordId(runId, operation.toolCallId),
|
|
1090
|
+
}),
|
|
1091
|
+
)
|
|
1092
|
+
|
|
1093
|
+
if (
|
|
1094
|
+
Option.isSome(started) &&
|
|
1095
|
+
(started.value.record.payload._tag !== 'SubagentStarted' ||
|
|
1096
|
+
started.value.record.payload.runId !== runId ||
|
|
1097
|
+
started.value.record.payload.toolCallId !== operation.toolCallId ||
|
|
1098
|
+
started.value.record.payload.childThreadId !==
|
|
1099
|
+
requested.value.record.payload.childThreadId)
|
|
1100
|
+
) {
|
|
1101
|
+
return yield* failure('Continuation child start identity is invalid')
|
|
1102
|
+
}
|
|
1103
|
+
|
|
1104
|
+
const startedBytes = Option.isSome(started)
|
|
1105
|
+
? canonicalRecordBytes(started.value.record) + MAX_RUN_CONTINUATION_BYTES
|
|
1106
|
+
: 0
|
|
1107
|
+
|
|
1108
|
+
childResults.set(operation.toolCallId, {
|
|
1109
|
+
resultBytes: Math.min(
|
|
1110
|
+
MAX_PERSISTED_JSON_BYTES,
|
|
1111
|
+
requested.value.record.payload.budget?.allocation.resultBytes ??
|
|
1112
|
+
MAX_PERSISTED_JSON_BYTES,
|
|
1113
|
+
),
|
|
1114
|
+
remainingBytes: Math.max(0, RUN_TERMINAL_RESERVE_BYTES - startedBytes),
|
|
1115
|
+
remainingRecords: Option.isSome(started) ? 3 : 4,
|
|
1116
|
+
})
|
|
1117
|
+
}
|
|
1118
|
+
}
|
|
1119
|
+
}
|
|
1120
|
+
}
|
|
1121
|
+
}
|
|
1122
|
+
if (continuation.position === 'settling' && continuation.terminal !== undefined) {
|
|
1123
|
+
const terminal = yield* provide(
|
|
1124
|
+
resolveExecutionEvidence(threadId, loaded.value.checkpoint, continuation.terminal),
|
|
1125
|
+
)
|
|
1126
|
+
|
|
1127
|
+
const payload = terminal.payload
|
|
1128
|
+
|
|
1129
|
+
if (payload._tag === 'RunCompleted') {
|
|
1130
|
+
completionBytes = utf8ByteLength(JSON.stringify(payload.output)) +
|
|
1131
|
+
(payload.runDisposition === undefined
|
|
1132
|
+
? 0
|
|
1133
|
+
: utf8ByteLength(JSON.stringify(payload.runDisposition)))
|
|
1134
|
+
}
|
|
1135
|
+
}
|
|
1136
|
+
|
|
1137
|
+
return {
|
|
1138
|
+
continuation,
|
|
1139
|
+
record: loaded.value.record,
|
|
1140
|
+
checkpoint: loaded.value.checkpoint,
|
|
1141
|
+
...(response === undefined ? {} : { response }),
|
|
1142
|
+
results,
|
|
1143
|
+
childResults,
|
|
1144
|
+
...(completionBytes === undefined ? {} : { completionBytes }),
|
|
1145
|
+
}
|
|
1146
|
+
})
|
|
1147
|
+
|
|
1148
|
+
const checkFutureShapes = (
|
|
1149
|
+
state: ProgressState,
|
|
1150
|
+
cursorBytes?: number,
|
|
1151
|
+
): Effect.Effect<void, ThreadStoreError> => {
|
|
1152
|
+
const progress = state.continuation
|
|
1153
|
+
|
|
1154
|
+
if (progress.position === 'settled') return Effect.void
|
|
1155
|
+
const size = cursorBytes ?? canonicalRecordBytes(state.record)
|
|
1156
|
+
const textBytes = (value: string) => utf8ByteLength(JSON.stringify(value))
|
|
1157
|
+
const refBytes = (value: EvidenceReference) => utf8ByteLength(JSON.stringify(value))
|
|
1158
|
+
|
|
1159
|
+
const ref = (recordId: RecordId) => EvidenceReference.make({ recordId })
|
|
1160
|
+
|
|
1161
|
+
const fits = (batchId: string, lastFact: EvidenceReference, terminal?: EvidenceReference) =>
|
|
1162
|
+
// Only these identities change. The fixed allowance covers scalar and timestamp growth.
|
|
1163
|
+
size +
|
|
1164
|
+
textBytes(JSON.stringify(['continuation@1', progress.runId, batchId])) -
|
|
1165
|
+
textBytes(state.record.recordId) +
|
|
1166
|
+
textBytes(deploymentId) -
|
|
1167
|
+
textBytes(state.record.deploymentId) +
|
|
1168
|
+
refBytes(lastFact) -
|
|
1169
|
+
refBytes(progress.lastFact) +
|
|
1170
|
+
(terminal === undefined
|
|
1171
|
+
? 0
|
|
1172
|
+
: refBytes(terminal) -
|
|
1173
|
+
(progress.terminal === undefined ? 0 : refBytes(progress.terminal)) +
|
|
1174
|
+
12) +
|
|
1175
|
+
1024 <=
|
|
1176
|
+
MAX_RUN_CONTINUATION_BYTES
|
|
1177
|
+
|
|
1178
|
+
if (
|
|
1179
|
+
!fits(
|
|
1180
|
+
submissionSettlementBatchId(progress.submissionId),
|
|
1181
|
+
ref(submissionSettlementRecordId(progress.submissionId)),
|
|
1182
|
+
ref(runCompletedRecordId(progress.runId)),
|
|
1183
|
+
)
|
|
1184
|
+
) {
|
|
1185
|
+
return Effect.fail(capacityFailure('Run has no room for a terminal continuation envelope'))
|
|
1186
|
+
}
|
|
1187
|
+
|
|
1188
|
+
if (
|
|
1189
|
+
state.response !== undefined &&
|
|
1190
|
+
progress.accounting.accountedToolTurn < state.response.turn
|
|
1191
|
+
) {
|
|
1192
|
+
for (const operation of state.response.toolOperations) {
|
|
1193
|
+
if (state.results.has(operation.toolCallId)) continue
|
|
1194
|
+
if (
|
|
1195
|
+
!fits(
|
|
1196
|
+
toolCallResultBatchId(progress.runId, state.response.turn, operation.toolCallId),
|
|
1197
|
+
ref(toolCallSettledRecordId(progress.runId, state.response.turn, operation.toolCallId)),
|
|
1198
|
+
)
|
|
1199
|
+
) {
|
|
1200
|
+
return Effect.fail(
|
|
1201
|
+
capacityFailure('Tool dispatch has no room for its result continuation envelope'),
|
|
1202
|
+
)
|
|
1203
|
+
}
|
|
1204
|
+
}
|
|
1205
|
+
}
|
|
1206
|
+
|
|
1207
|
+
return Effect.void
|
|
1208
|
+
}
|
|
1209
|
+
|
|
1210
|
+
const checkCapacity = (
|
|
1211
|
+
state: CapacityState,
|
|
1212
|
+
consumed: ReadonlySet<RecordId> = new Set(),
|
|
1213
|
+
extra?: { readonly turn: number; readonly bytes: number; readonly records: number },
|
|
1214
|
+
committedUsageBytes = 0,
|
|
1215
|
+
): Effect.Effect<void, ThreadStoreError> => {
|
|
1216
|
+
const progress = state.continuation
|
|
1217
|
+
const pending = resultCapacity(state)
|
|
1218
|
+
|
|
1219
|
+
const terminal = progress.position === 'settled'
|
|
1220
|
+
? 0
|
|
1221
|
+
: Math.max(
|
|
1222
|
+
0,
|
|
1223
|
+
RUN_TERMINAL_RESERVE_BYTES +
|
|
1224
|
+
2 * (state.completionBytes ?? 0) +
|
|
1225
|
+
2 *
|
|
1226
|
+
(progress.terminalUsageBytes +
|
|
1227
|
+
Math.max(0, (stagedUsage.get(progress.runId) ?? 0) - committedUsageBytes)) -
|
|
1228
|
+
progress.terminalBytes,
|
|
1229
|
+
)
|
|
1230
|
+
|
|
1231
|
+
let turnBytes = (extra !== undefined && extra.turn !== progress.turn ? 0 : progress.turnBytes) +
|
|
1232
|
+
pending.bytes +
|
|
1233
|
+
terminal +
|
|
1234
|
+
(extra?.bytes ?? 0)
|
|
1235
|
+
|
|
1236
|
+
let recordBytes = progress.recordBytes + pending.bytes + terminal + (extra?.bytes ?? 0)
|
|
1237
|
+
|
|
1238
|
+
let recordCount = progress.recordCount +
|
|
1239
|
+
pending.records +
|
|
1240
|
+
(progress.position === 'settled'
|
|
1241
|
+
? 0
|
|
1242
|
+
: Math.max(0, RUN_TERMINAL_RESERVE_RECORDS - progress.terminalRecords)) +
|
|
1243
|
+
(extra?.records ?? 0)
|
|
1244
|
+
|
|
1245
|
+
for (const [recordId, reservation] of reservations) {
|
|
1246
|
+
if (reservation.runId !== progress.runId || consumed.has(recordId)) continue
|
|
1247
|
+
if (reservation.turn === (extra?.turn ?? progress.turn)) turnBytes += reservation.bytes
|
|
1248
|
+
recordBytes += reservation.bytes
|
|
1249
|
+
recordCount += reservation.records
|
|
1250
|
+
}
|
|
1251
|
+
if (
|
|
1252
|
+
turnBytes > MAX_TURN_CANONICAL_BYTES ||
|
|
1253
|
+
recordBytes > MAX_RUN_EVIDENCE_BYTES ||
|
|
1254
|
+
recordCount > MAX_RUN_EVIDENCE_RECORDS
|
|
1255
|
+
) {
|
|
1256
|
+
return Effect.fail(
|
|
1257
|
+
failure(
|
|
1258
|
+
'Run has no remaining canonical dispatch capacity',
|
|
1259
|
+
AgentPersistenceCapacityError.make({
|
|
1260
|
+
message: 'Run has no room for the next result and a bounded terminal settlement',
|
|
1261
|
+
}),
|
|
1262
|
+
),
|
|
1263
|
+
)
|
|
1264
|
+
}
|
|
1265
|
+
|
|
1266
|
+
return Effect.void
|
|
1267
|
+
}
|
|
1268
|
+
|
|
1269
|
+
const prepare = Effect.fnUntraced(function*(
|
|
1270
|
+
batch: CanonicalBatch,
|
|
1271
|
+
after: CanonicalSequence,
|
|
1272
|
+
mode: 'commit' | 'preflight' = 'commit',
|
|
1273
|
+
) {
|
|
1274
|
+
if (batch.records.some((record) => record.payload._tag === 'RunContinuation')) {
|
|
1275
|
+
return yield* failure('A caller cannot supply interpreter progress to the progress writer')
|
|
1276
|
+
}
|
|
1277
|
+
|
|
1278
|
+
// Every private caller passes captureFacts' owned snapshot. Keep a mutable
|
|
1279
|
+
// list for settlement accounting without capturing that snapshot a second time.
|
|
1280
|
+
const ownedRecords = [...batch.records]
|
|
1281
|
+
|
|
1282
|
+
const groups = new Map<RunId, Array<RecordEnvelope>>()
|
|
1283
|
+
|
|
1284
|
+
for (const record of ownedRecords) {
|
|
1285
|
+
for (const runId of executionRunIds(record)) {
|
|
1286
|
+
const facts = groups.get(runId) ?? []
|
|
1287
|
+
|
|
1288
|
+
facts.push(record)
|
|
1289
|
+
groups.set(runId, facts)
|
|
1290
|
+
}
|
|
1291
|
+
}
|
|
1292
|
+
const executions: Array<ExecutionUpdate> = []
|
|
1293
|
+
const accepted = new Map<RunId, ProgressState>()
|
|
1294
|
+
let executionCount = 0
|
|
1295
|
+
|
|
1296
|
+
for (const [runId, facts] of groups) {
|
|
1297
|
+
const empty = verifiedEmpty?.runId === runId &&
|
|
1298
|
+
verifiedEmpty.through === after &&
|
|
1299
|
+
// Settlement preparation need not have a Run writer; absence disables reuse.
|
|
1300
|
+
verifiedEmpty.writer ===
|
|
1301
|
+
Option.getOrUndefined(yield* Effect.serviceOption(CurrentRunWriter))
|
|
1302
|
+
|
|
1303
|
+
const previous = empty
|
|
1304
|
+
? undefined
|
|
1305
|
+
: (cached.get(runId) ?? (yield* loadUncachedState(runId, after)))
|
|
1306
|
+
|
|
1307
|
+
let original = facts.find(
|
|
1308
|
+
(record) =>
|
|
1309
|
+
record.payload._tag === 'UserInputRecorded' &&
|
|
1310
|
+
record.payload.kind === 'user' &&
|
|
1311
|
+
record.payload.runId === runId &&
|
|
1312
|
+
record.payload.submissionId !== undefined,
|
|
1313
|
+
)
|
|
1314
|
+
|
|
1315
|
+
let originalCount = 0
|
|
1316
|
+
let initialBytes = 0
|
|
1317
|
+
let originalSequence = previous?.checkpoint?.originalSequence
|
|
1318
|
+
|
|
1319
|
+
if (previous === undefined && original === undefined) {
|
|
1320
|
+
const input = yield* provide(getRunInput({ threadId, runId }))
|
|
1321
|
+
|
|
1322
|
+
if (
|
|
1323
|
+
Option.isNone(input) ||
|
|
1324
|
+
input.value.record.payload._tag !== 'UserInputRecorded' ||
|
|
1325
|
+
input.value.record.payload.submissionId === undefined
|
|
1326
|
+
) {
|
|
1327
|
+
continue
|
|
1328
|
+
}
|
|
1329
|
+
|
|
1330
|
+
original = input.value.record
|
|
1331
|
+
originalSequence = input.value.sequence
|
|
1332
|
+
}
|
|
1333
|
+
|
|
1334
|
+
const submissionId = previous?.continuation.submissionId ??
|
|
1335
|
+
(original?.payload._tag === 'UserInputRecorded'
|
|
1336
|
+
? original.payload.submissionId
|
|
1337
|
+
: undefined)
|
|
1338
|
+
|
|
1339
|
+
if (submissionId === undefined) {
|
|
1340
|
+
return yield* failure('Run progress has no original admission identity')
|
|
1341
|
+
}
|
|
1342
|
+
if (runId !== runIdForSubmission(submissionId)) {
|
|
1343
|
+
return yield* failure('Run progress conflicts with its original admission')
|
|
1344
|
+
}
|
|
1345
|
+
if (previous === undefined && original === undefined) {
|
|
1346
|
+
return yield* failure('Run has no accepted original input')
|
|
1347
|
+
}
|
|
1348
|
+
|
|
1349
|
+
if (previous === undefined && !empty) {
|
|
1350
|
+
const settled = yield* provide(
|
|
1351
|
+
getRecord({
|
|
1352
|
+
threadId,
|
|
1353
|
+
recordId: submissionSettlementRecordId(submissionId),
|
|
1354
|
+
}),
|
|
1355
|
+
)
|
|
1356
|
+
|
|
1357
|
+
if (
|
|
1358
|
+
Option.isSome(settled) &&
|
|
1359
|
+
settled.value.sequence <= after &&
|
|
1360
|
+
settled.value.record.payload._tag === 'SubmissionSettled' &&
|
|
1361
|
+
settled.value.record.payload.runId === runId &&
|
|
1362
|
+
settled.value.record.payload.historyRecordCount !== undefined &&
|
|
1363
|
+
facts.every(
|
|
1364
|
+
({ payload }) =>
|
|
1365
|
+
payload._tag === 'WorkerReportPrepared' ||
|
|
1366
|
+
payload._tag === 'SubmissionSettled' ||
|
|
1367
|
+
payload._tag === 'ToolCallSettled' ||
|
|
1368
|
+
payload._tag === 'ToolCallResolved' ||
|
|
1369
|
+
payload._tag === 'SubagentJoined',
|
|
1370
|
+
)
|
|
1371
|
+
) {
|
|
1372
|
+
// Independent obligations can record factual closure after interpreter retirement.
|
|
1373
|
+
continue
|
|
1374
|
+
}
|
|
1375
|
+
|
|
1376
|
+
const selected = yield* provide(
|
|
1377
|
+
Stream.runCollect(
|
|
1378
|
+
runEvidence(threadId, submissionId, after).pipe(
|
|
1379
|
+
Stream.take(MAX_RUN_RECOVERY_SUFFIX_RECORDS + 1),
|
|
1380
|
+
),
|
|
1381
|
+
),
|
|
1382
|
+
)
|
|
1383
|
+
|
|
1384
|
+
const existing = selected.filter(({ record }) => executionRunIds(record).includes(runId))
|
|
1385
|
+
|
|
1386
|
+
if (
|
|
1387
|
+
!validateSuffix(existing, CanonicalSequence.make(0)) ||
|
|
1388
|
+
existing.some(({ record }) => !isPreContinuationFact(record))
|
|
1389
|
+
) {
|
|
1390
|
+
return yield* failure(
|
|
1391
|
+
`Run execution has no active checkpoint or exceeds its preparation bound (${
|
|
1392
|
+
facts.map((fact) => fact.payload._tag).join(', ')
|
|
1393
|
+
})`,
|
|
1394
|
+
)
|
|
1395
|
+
}
|
|
1396
|
+
originalCount = existing.length
|
|
1397
|
+
initialBytes = existing.reduce(
|
|
1398
|
+
(bytes, entry) => bytes + canonicalRecordBytes(entry.record),
|
|
1399
|
+
0,
|
|
1400
|
+
)
|
|
1401
|
+
}
|
|
1402
|
+
|
|
1403
|
+
const canonicalRecords = (previous?.checkpoint?.canonicalRecords ?? originalCount) +
|
|
1404
|
+
facts.filter((fact) => !isOperationalRecord(fact)).length
|
|
1405
|
+
|
|
1406
|
+
for (let index = 0; index < facts.length; index++) {
|
|
1407
|
+
const fact = facts[index]!
|
|
1408
|
+
const payload = fact.payload
|
|
1409
|
+
|
|
1410
|
+
if (payload._tag !== 'SubmissionSettled' || payload.runId !== runId) continue
|
|
1411
|
+
|
|
1412
|
+
const retained = yield* Effect.fromResult(
|
|
1413
|
+
captureRecord(
|
|
1414
|
+
RecordEnvelope.make({
|
|
1415
|
+
...fact,
|
|
1416
|
+
payload: { ...payload, historyRecordCount: canonicalRecords },
|
|
1417
|
+
}),
|
|
1418
|
+
),
|
|
1419
|
+
).pipe(
|
|
1420
|
+
Effect.map((record) => record.canonical),
|
|
1421
|
+
Effect.mapError((cause) => failure('Cannot capture canonical progress facts', cause)),
|
|
1422
|
+
)
|
|
1423
|
+
|
|
1424
|
+
facts[index] = retained
|
|
1425
|
+
ownedRecords[ownedRecords.indexOf(fact)] = retained
|
|
1426
|
+
}
|
|
1427
|
+
|
|
1428
|
+
const originalInput = previous !== undefined
|
|
1429
|
+
? previous.continuation.originalInput
|
|
1430
|
+
: original === undefined
|
|
1431
|
+
? yield* failure('Run progress has no original input')
|
|
1432
|
+
: EvidenceReference.make({ recordId: original.recordId })
|
|
1433
|
+
|
|
1434
|
+
const next = yield* advanceFacts(previous, facts, submissionId, initialBytes)
|
|
1435
|
+
|
|
1436
|
+
const {
|
|
1437
|
+
accounting,
|
|
1438
|
+
savedContext,
|
|
1439
|
+
latestResponse,
|
|
1440
|
+
terminal,
|
|
1441
|
+
position,
|
|
1442
|
+
response,
|
|
1443
|
+
results,
|
|
1444
|
+
childResults,
|
|
1445
|
+
completionBytes,
|
|
1446
|
+
last,
|
|
1447
|
+
factBytes,
|
|
1448
|
+
turn,
|
|
1449
|
+
turnBase,
|
|
1450
|
+
usageCharge,
|
|
1451
|
+
terminalUsageBytes,
|
|
1452
|
+
} = next
|
|
1453
|
+
|
|
1454
|
+
// Facts and prior state are already checked. Validate the complete candidate at
|
|
1455
|
+
// record capture, after byte accounting; preflight has its own boundary below.
|
|
1456
|
+
let continuation = RunContinuation.make(
|
|
1457
|
+
{
|
|
1458
|
+
version: 1,
|
|
1459
|
+
runId,
|
|
1460
|
+
submissionId,
|
|
1461
|
+
revision: (previous?.continuation.revision ?? 0) + 1,
|
|
1462
|
+
recordCount: (previous?.continuation.recordCount ?? originalCount) + facts.length,
|
|
1463
|
+
recordBytes: (previous?.continuation.recordBytes ?? initialBytes) + factBytes,
|
|
1464
|
+
turn,
|
|
1465
|
+
turnBytes: turnBase,
|
|
1466
|
+
terminalUsageBytes,
|
|
1467
|
+
terminalBytes: next.terminalBytes,
|
|
1468
|
+
terminalRecords: next.terminalRecords,
|
|
1469
|
+
originalInput,
|
|
1470
|
+
...(savedContext === undefined ? {} : { savedContext }),
|
|
1471
|
+
...(latestResponse === undefined ? {} : { latestResponse }),
|
|
1472
|
+
...(terminal === undefined ? {} : { terminal }),
|
|
1473
|
+
lastFact: EvidenceReference.make({ recordId: last.recordId }),
|
|
1474
|
+
position,
|
|
1475
|
+
accounting,
|
|
1476
|
+
},
|
|
1477
|
+
{ disableChecks: true },
|
|
1478
|
+
)
|
|
1479
|
+
|
|
1480
|
+
const header = {
|
|
1481
|
+
recordId: RecordId.make(JSON.stringify(['continuation@1', runId, batch.batchId])),
|
|
1482
|
+
family: 'thread' as const,
|
|
1483
|
+
schemaVersion: 1 as const,
|
|
1484
|
+
createdAt: last.createdAt,
|
|
1485
|
+
deploymentId: last.deploymentId,
|
|
1486
|
+
}
|
|
1487
|
+
|
|
1488
|
+
// Continuation fields are JSON; only the envelope DateTime needs encoding.
|
|
1489
|
+
// Key order does not change byte width. Measure without capturing a provisional record,
|
|
1490
|
+
// then compare against the final Schema encoding before accepting its accounting.
|
|
1491
|
+
const provisional = {
|
|
1492
|
+
...header,
|
|
1493
|
+
createdAt: DateTime.formatIso(header.createdAt),
|
|
1494
|
+
payload: continuation,
|
|
1495
|
+
} satisfies typeof RecordEnvelope.Encoded
|
|
1496
|
+
|
|
1497
|
+
const initialCursorBytes = utf8ByteLength(JSON.stringify(provisional))
|
|
1498
|
+
|
|
1499
|
+
const initialWidth = String(continuation.turnBytes).length + String(continuation.terminalBytes).length
|
|
1500
|
+
|
|
1501
|
+
let turnBytes = continuation.turnBytes
|
|
1502
|
+
let terminalBytes = continuation.terminalBytes
|
|
1503
|
+
|
|
1504
|
+
for (let attempts = 0;; attempts++) {
|
|
1505
|
+
const cursorBytes = initialCursorBytes +
|
|
1506
|
+
String(turnBytes).length +
|
|
1507
|
+
String(terminalBytes).length -
|
|
1508
|
+
initialWidth
|
|
1509
|
+
|
|
1510
|
+
const nextTurnBytes = turnBase + cursorBytes
|
|
1511
|
+
const nextTerminalBytes = next.terminalBytes + (next.closing ? cursorBytes : 0)
|
|
1512
|
+
|
|
1513
|
+
if (nextTurnBytes === turnBytes && nextTerminalBytes === terminalBytes) break
|
|
1514
|
+
if (attempts >= 4) return yield* failure('Continuation byte accounting did not converge')
|
|
1515
|
+
turnBytes = nextTurnBytes
|
|
1516
|
+
terminalBytes = nextTerminalBytes
|
|
1517
|
+
}
|
|
1518
|
+
|
|
1519
|
+
// Byte accounting changes only these counters; the full candidate is checked below.
|
|
1520
|
+
continuation = RunContinuation.make(
|
|
1521
|
+
{ ...continuation, turnBytes, terminalBytes },
|
|
1522
|
+
{ disableChecks: true },
|
|
1523
|
+
)
|
|
1524
|
+
|
|
1525
|
+
const proposedRecord = RecordEnvelope.make(
|
|
1526
|
+
{ ...header, payload: continuation },
|
|
1527
|
+
{ disableChecks: true },
|
|
1528
|
+
)
|
|
1529
|
+
|
|
1530
|
+
const cursorBytes = turnBytes - turnBase
|
|
1531
|
+
|
|
1532
|
+
if (cursorBytes > MAX_RUN_CONTINUATION_BYTES) {
|
|
1533
|
+
return yield* capacityFailure(
|
|
1534
|
+
'Encoded continuation exceeds 8192 bytes including its envelope',
|
|
1535
|
+
)
|
|
1536
|
+
}
|
|
1537
|
+
executionCount++
|
|
1538
|
+
if (
|
|
1539
|
+
mode === 'preflight' &&
|
|
1540
|
+
previous?.checkpoint !== undefined &&
|
|
1541
|
+
facts.every(({ payload }) => payload._tag === 'ModelResponseRecorded')
|
|
1542
|
+
) {
|
|
1543
|
+
// Preflight does not capture a record. Check its complete payload before
|
|
1544
|
+
// dispatch; a commit receives the same proof from record capture below.
|
|
1545
|
+
continuation = yield* RunContinuation.makeEffect({ ...continuation }).pipe(
|
|
1546
|
+
Effect.mapError(() => capacityFailure('Canonical continuation exceeds its protocol bounds')),
|
|
1547
|
+
)
|
|
1548
|
+
yield* decodeExecutionFrontier({
|
|
1549
|
+
canonicalRecords,
|
|
1550
|
+
throughSequence: after + ownedRecords.filter((fact) => !isOperationalRecord(fact)).length,
|
|
1551
|
+
}).pipe(Effect.mapError((cause) => failure('Active execution exceeds its bounds', cause)))
|
|
1552
|
+
|
|
1553
|
+
const state: ProgressState = {
|
|
1554
|
+
continuation,
|
|
1555
|
+
record: proposedRecord,
|
|
1556
|
+
...(response === undefined ? {} : { response }),
|
|
1557
|
+
results,
|
|
1558
|
+
childResults,
|
|
1559
|
+
...(completionBytes === undefined ? {} : { completionBytes }),
|
|
1560
|
+
}
|
|
1561
|
+
|
|
1562
|
+
yield* checkCapacity(
|
|
1563
|
+
state,
|
|
1564
|
+
new Set(batch.records.map(({ recordId }) => recordId)),
|
|
1565
|
+
undefined,
|
|
1566
|
+
usageCharge,
|
|
1567
|
+
)
|
|
1568
|
+
yield* checkFutureShapes(state, cursorBytes)
|
|
1569
|
+
continue
|
|
1570
|
+
}
|
|
1571
|
+
|
|
1572
|
+
const record = yield* Effect.fromResult(captureRecord(proposedRecord)).pipe(
|
|
1573
|
+
Effect.map((record) => record.canonical),
|
|
1574
|
+
Effect.mapError((cause) =>
|
|
1575
|
+
Schema.isSchemaError(cause.cause)
|
|
1576
|
+
? capacityFailure('Canonical continuation exceeds its protocol bounds')
|
|
1577
|
+
: failure('Cannot capture canonical progress facts', cause)
|
|
1578
|
+
),
|
|
1579
|
+
)
|
|
1580
|
+
|
|
1581
|
+
if (!isExecutionProgressRecord(record)) {
|
|
1582
|
+
return yield* failure('Captured execution has no Run continuation')
|
|
1583
|
+
}
|
|
1584
|
+
if (canonicalRecordBytes(record) !== cursorBytes) {
|
|
1585
|
+
return yield* failure('Continuation byte accounting differs from its Schema encoding')
|
|
1586
|
+
}
|
|
1587
|
+
|
|
1588
|
+
const operational = new Map(
|
|
1589
|
+
(previous?.checkpoint?.operational ?? []).map((fact) => [fact.recordId, fact]),
|
|
1590
|
+
)
|
|
1591
|
+
|
|
1592
|
+
const changed = facts.filter(isOperationalRecord)
|
|
1593
|
+
|
|
1594
|
+
for (const fact of changed) {
|
|
1595
|
+
const prior = operational.get(fact.recordId)
|
|
1596
|
+
|
|
1597
|
+
if (
|
|
1598
|
+
prior !== undefined &&
|
|
1599
|
+
(yield* Effect.fromResult(recordEncoding(prior)).pipe(
|
|
1600
|
+
Effect.mapError((cause) => failure('Cannot encode active Run fact', cause)),
|
|
1601
|
+
)).json !==
|
|
1602
|
+
(yield* Effect.fromResult(recordEncoding(fact)).pipe(
|
|
1603
|
+
Effect.mapError((cause) => failure('Cannot encode active Run fact', cause)),
|
|
1604
|
+
)).json
|
|
1605
|
+
) {
|
|
1606
|
+
return yield* failure('An active Run fact cannot change after becoming durable')
|
|
1607
|
+
}
|
|
1608
|
+
operational.set(fact.recordId, fact)
|
|
1609
|
+
}
|
|
1610
|
+
|
|
1611
|
+
// The records above already belong to this writer. PreparedAppend captures the
|
|
1612
|
+
// complete update at the storage boundary, preserving their private encodings.
|
|
1613
|
+
const checkpoint = ExecutionCheckpoint.make(
|
|
1614
|
+
{
|
|
1615
|
+
version: 2,
|
|
1616
|
+
progressRecord: record,
|
|
1617
|
+
operational: [...operational.values()],
|
|
1618
|
+
canonicalRecords,
|
|
1619
|
+
originalSequence: originalSequence ??
|
|
1620
|
+
CanonicalSequence.make(
|
|
1621
|
+
after +
|
|
1622
|
+
ownedRecords
|
|
1623
|
+
.filter((fact) => !isOperationalRecord(fact))
|
|
1624
|
+
.findIndex((fact) => fact.recordId === originalInput.recordId) +
|
|
1625
|
+
1,
|
|
1626
|
+
),
|
|
1627
|
+
throughSequence: CanonicalSequence.make(
|
|
1628
|
+
after + ownedRecords.filter((fact) => !isOperationalRecord(fact)).length,
|
|
1629
|
+
),
|
|
1630
|
+
batchId: batch.batchId,
|
|
1631
|
+
},
|
|
1632
|
+
{ disableChecks: true },
|
|
1633
|
+
)
|
|
1634
|
+
|
|
1635
|
+
const state = {
|
|
1636
|
+
continuation: record.payload,
|
|
1637
|
+
record,
|
|
1638
|
+
checkpoint,
|
|
1639
|
+
...(response === undefined ? {} : { response }),
|
|
1640
|
+
results,
|
|
1641
|
+
childResults,
|
|
1642
|
+
...(completionBytes === undefined ? {} : { completionBytes }),
|
|
1643
|
+
}
|
|
1644
|
+
|
|
1645
|
+
if (
|
|
1646
|
+
!facts.every((fact) => isTerminalBudgetFact(fact) && fact.payload._tag !== 'RunCompleted')
|
|
1647
|
+
) {
|
|
1648
|
+
yield* checkCapacity(
|
|
1649
|
+
state,
|
|
1650
|
+
new Set(batch.records.map(({ recordId }) => recordId)),
|
|
1651
|
+
undefined,
|
|
1652
|
+
usageCharge,
|
|
1653
|
+
)
|
|
1654
|
+
}
|
|
1655
|
+
yield* checkFutureShapes(state)
|
|
1656
|
+
executions.push(
|
|
1657
|
+
ExecutionUpdate.make(
|
|
1658
|
+
{
|
|
1659
|
+
previousRevision: previous?.continuation.revision ?? 0,
|
|
1660
|
+
checkpoint,
|
|
1661
|
+
facts: changed,
|
|
1662
|
+
// Pending operations and handoffs retain their own recovery rows and original facts.
|
|
1663
|
+
retire: position === 'settled',
|
|
1664
|
+
},
|
|
1665
|
+
{ disableChecks: true },
|
|
1666
|
+
),
|
|
1667
|
+
)
|
|
1668
|
+
accepted.set(runId, state)
|
|
1669
|
+
}
|
|
1670
|
+
|
|
1671
|
+
if (ownedRecords.length + executionCount > 256) {
|
|
1672
|
+
return yield* failure('Progress and facts exceed the atomic batch bound')
|
|
1673
|
+
}
|
|
1674
|
+
|
|
1675
|
+
const prepared = CommitBatch.make(
|
|
1676
|
+
{
|
|
1677
|
+
...batch,
|
|
1678
|
+
records: ownedRecords.filter((fact) => !isOperationalRecord(fact)),
|
|
1679
|
+
},
|
|
1680
|
+
{ disableChecks: true },
|
|
1681
|
+
)
|
|
1682
|
+
|
|
1683
|
+
return {
|
|
1684
|
+
batch: prepared,
|
|
1685
|
+
executions,
|
|
1686
|
+
accept: () => {
|
|
1687
|
+
for (const record of batch.records) reservations.delete(record.recordId)
|
|
1688
|
+
for (const [runId, state] of accepted) {
|
|
1689
|
+
if (executions.find((update) => update.checkpoint.progress.runId === runId)?.retire) {
|
|
1690
|
+
cached.delete(runId)
|
|
1691
|
+
} else cached.set(runId, state)
|
|
1692
|
+
}
|
|
1693
|
+
// Administrative writers can address many old Runs; retention is still bounded.
|
|
1694
|
+
while (cached.size > 8) {
|
|
1695
|
+
const first = cached.keys().next().value
|
|
1696
|
+
|
|
1697
|
+
if (first !== undefined) cached.delete(first)
|
|
1698
|
+
}
|
|
1699
|
+
},
|
|
1700
|
+
}
|
|
1701
|
+
})
|
|
1702
|
+
|
|
1703
|
+
const tail = Effect.flatMap(CurrentRunWriter, (writer) =>
|
|
1704
|
+
writer.threadId !== threadId
|
|
1705
|
+
? Effect.fail(failure('Progress writer belongs to another Thread'))
|
|
1706
|
+
: Effect.map(writer.tail, (position) => position.sequence))
|
|
1707
|
+
|
|
1708
|
+
const commitCaptured = Effect.fnUntraced(
|
|
1709
|
+
function*(batch: CanonicalBatch) {
|
|
1710
|
+
const writer = yield* CurrentRunWriter
|
|
1711
|
+
|
|
1712
|
+
for (let retries = 0;; retries++) {
|
|
1713
|
+
const prepared = yield* prepare(batch, yield* tail).pipe(
|
|
1714
|
+
Effect.catchTag(
|
|
1715
|
+
'ThreadNotMaterialized',
|
|
1716
|
+
(cause) => failure('Selected Run disappeared during progress publication', cause),
|
|
1717
|
+
),
|
|
1718
|
+
)
|
|
1719
|
+
|
|
1720
|
+
const result = yield* writer.append(prepared.batch, prepared.executions).pipe(
|
|
1721
|
+
Effect.catchTag('AppendConflict', (conflict) => {
|
|
1722
|
+
if (conflict.reason !== 'tail' || retries >= 8) return Effect.fail(conflict)
|
|
1723
|
+
cached.clear()
|
|
1724
|
+
|
|
1725
|
+
return Effect.as(Effect.void, undefined)
|
|
1726
|
+
}),
|
|
1727
|
+
)
|
|
1728
|
+
|
|
1729
|
+
if (result === undefined) continue
|
|
1730
|
+
|
|
1731
|
+
if (result.replayed) cached.clear()
|
|
1732
|
+
else prepared.accept()
|
|
1733
|
+
|
|
1734
|
+
return result
|
|
1735
|
+
}
|
|
1736
|
+
},
|
|
1737
|
+
(effect) => Effect.flatMap(CurrentRunWriter, (writer) => writer.withProgress(gate.withPermits(1)(effect))),
|
|
1738
|
+
)
|
|
1739
|
+
|
|
1740
|
+
const publishCaptured = Effect.fnUntraced(
|
|
1741
|
+
function*(batch: CanonicalBatch) {
|
|
1742
|
+
const publisher = yield* CurrentRunSettlement
|
|
1743
|
+
|
|
1744
|
+
if (publisher.threadId !== threadId) {
|
|
1745
|
+
return yield* failure('Settlement publisher belongs to another Thread')
|
|
1746
|
+
}
|
|
1747
|
+
|
|
1748
|
+
for (let retries = 0;; retries++) {
|
|
1749
|
+
const prepared = yield* prepare(batch, (yield* publisher.tail).sequence).pipe(
|
|
1750
|
+
Effect.catchTag(
|
|
1751
|
+
'ThreadNotMaterialized',
|
|
1752
|
+
(cause) => failure('Selected Run disappeared during progress publication', cause),
|
|
1753
|
+
),
|
|
1754
|
+
)
|
|
1755
|
+
|
|
1756
|
+
const first = prepared.batch.records[0]
|
|
1757
|
+
|
|
1758
|
+
if (first === undefined) {
|
|
1759
|
+
return yield* failure('Settlement must publish a retained terminal fact')
|
|
1760
|
+
}
|
|
1761
|
+
|
|
1762
|
+
// prepare retains captured records within the validated batch bound.
|
|
1763
|
+
// The explicit first element proves this retained batch is still non-empty.
|
|
1764
|
+
const terminalBatch = CanonicalBatch.make(
|
|
1765
|
+
{
|
|
1766
|
+
...prepared.batch,
|
|
1767
|
+
records: [first, ...prepared.batch.records.slice(1)],
|
|
1768
|
+
},
|
|
1769
|
+
{ disableChecks: true },
|
|
1770
|
+
)
|
|
1771
|
+
|
|
1772
|
+
const result = yield* publisher.publishSettlement(terminalBatch, prepared.executions).pipe(
|
|
1773
|
+
Effect.catchTag('AppendConflict', (conflict) => {
|
|
1774
|
+
if (conflict.reason !== 'tail' || retries >= 8) return Effect.fail(conflict)
|
|
1775
|
+
cached.clear()
|
|
1776
|
+
|
|
1777
|
+
return Effect.as(Effect.void, undefined)
|
|
1778
|
+
}),
|
|
1779
|
+
)
|
|
1780
|
+
|
|
1781
|
+
if (result === undefined) continue
|
|
1782
|
+
|
|
1783
|
+
if (result.replayed) cached.clear()
|
|
1784
|
+
else prepared.accept()
|
|
1785
|
+
|
|
1786
|
+
return result
|
|
1787
|
+
}
|
|
1788
|
+
},
|
|
1789
|
+
(effect) => Effect.flatMap(CurrentRunSettlement, (writer) => writer.withProgress(gate.withPermits(1)(effect))),
|
|
1790
|
+
)
|
|
1791
|
+
|
|
1792
|
+
const commit = (batch: CommitBatch) => captureFacts(batch).pipe(Effect.flatMap(commitCaptured))
|
|
1793
|
+
|
|
1794
|
+
const publish = (batch: CanonicalBatch) => captureFacts(batch).pipe(Effect.flatMap(publishCaptured))
|
|
1795
|
+
|
|
1796
|
+
const preflight = (batch: CanonicalBatch) =>
|
|
1797
|
+
gate.withPermits(1)(
|
|
1798
|
+
Effect.gen(function*() {
|
|
1799
|
+
const owned = yield* captureFacts(batch)
|
|
1800
|
+
|
|
1801
|
+
yield* prepare(owned, yield* tail, 'preflight')
|
|
1802
|
+
}),
|
|
1803
|
+
)
|
|
1804
|
+
|
|
1805
|
+
const reserve = (runId: RunId, probe: CanonicalBatch, bytes: number) =>
|
|
1806
|
+
gate.withPermits(1)(
|
|
1807
|
+
Effect.gen(function*() {
|
|
1808
|
+
const after = yield* tail
|
|
1809
|
+
|
|
1810
|
+
yield* prepare(yield* captureFacts(probe), after)
|
|
1811
|
+
const recordId = probe.records[0].recordId
|
|
1812
|
+
const state = cached.get(runId) ?? (yield* loadUncachedState(runId, after))
|
|
1813
|
+
|
|
1814
|
+
if (state === undefined) return yield* failure('Dispatch has no canonical Run progress')
|
|
1815
|
+
if (reservations.has(recordId)) {
|
|
1816
|
+
return yield* failure('Dispatch capacity is already reserved')
|
|
1817
|
+
}
|
|
1818
|
+
yield* checkCapacity(state, new Set(), {
|
|
1819
|
+
turn: state.continuation.turn,
|
|
1820
|
+
bytes,
|
|
1821
|
+
records: 1,
|
|
1822
|
+
})
|
|
1823
|
+
reservations.set(recordId, { runId, turn: state.continuation.turn, bytes, records: 1 })
|
|
1824
|
+
|
|
1825
|
+
return yield* Effect.succeed(
|
|
1826
|
+
gate.withPermits(1)(
|
|
1827
|
+
Effect.sync(() => {
|
|
1828
|
+
reservations.delete(recordId)
|
|
1829
|
+
}),
|
|
1830
|
+
),
|
|
1831
|
+
)
|
|
1832
|
+
}),
|
|
1833
|
+
)
|
|
1834
|
+
|
|
1835
|
+
const defer = (batch: CanonicalBatch) =>
|
|
1836
|
+
gate.withPermits(1)(
|
|
1837
|
+
Effect.gen(function*() {
|
|
1838
|
+
const after = yield* tail
|
|
1839
|
+
|
|
1840
|
+
const owned = yield* captureFacts(batch)
|
|
1841
|
+
|
|
1842
|
+
yield* prepare(owned, after, 'preflight')
|
|
1843
|
+
|
|
1844
|
+
const response = owned.records.find(
|
|
1845
|
+
(record) => record.payload._tag === 'ModelResponseRecorded',
|
|
1846
|
+
)
|
|
1847
|
+
|
|
1848
|
+
if (response === undefined || response.payload._tag !== 'ModelResponseRecorded') {
|
|
1849
|
+
return yield* failure('Deferred dispatch has no response')
|
|
1850
|
+
}
|
|
1851
|
+
|
|
1852
|
+
const progress = cached.get(response.payload.runId) ??
|
|
1853
|
+
(yield* loadUncachedState(response.payload.runId, after))
|
|
1854
|
+
|
|
1855
|
+
if (progress === undefined) return yield* failure('Deferred dispatch has no Run progress')
|
|
1856
|
+
|
|
1857
|
+
const pending = resultCapacity({
|
|
1858
|
+
...progress,
|
|
1859
|
+
continuation: {
|
|
1860
|
+
...progress.continuation,
|
|
1861
|
+
accounting: { ...progress.continuation.accounting, accountedToolTurn: 0 },
|
|
1862
|
+
},
|
|
1863
|
+
response: response.payload,
|
|
1864
|
+
results: new Map(),
|
|
1865
|
+
childResults: new Map(),
|
|
1866
|
+
})
|
|
1867
|
+
|
|
1868
|
+
const bytes = owned.records.reduce((total, record) => total + canonicalRecordBytes(record), 0) +
|
|
1869
|
+
MAX_RUN_CONTINUATION_BYTES +
|
|
1870
|
+
pending.bytes
|
|
1871
|
+
|
|
1872
|
+
const records = owned.records.length + pending.records
|
|
1873
|
+
|
|
1874
|
+
yield* checkCapacity(progress, new Set(), { turn: response.payload.turn, bytes, records })
|
|
1875
|
+
reservations.set(response.recordId, {
|
|
1876
|
+
runId: response.payload.runId,
|
|
1877
|
+
turn: response.payload.turn,
|
|
1878
|
+
bytes,
|
|
1879
|
+
records,
|
|
1880
|
+
})
|
|
1881
|
+
|
|
1882
|
+
return owned
|
|
1883
|
+
}),
|
|
1884
|
+
)
|
|
1885
|
+
|
|
1886
|
+
const checkState = (state: ProgressState | undefined, recovered: boolean) => {
|
|
1887
|
+
if (state === undefined) return Effect.fail(failure('Execution has no canonical Run progress'))
|
|
1888
|
+
|
|
1889
|
+
const capacity = () =>
|
|
1890
|
+
state.continuation.position === 'settling' || state.continuation.position === 'settled'
|
|
1891
|
+
? Effect.void
|
|
1892
|
+
: checkCapacity(state)
|
|
1893
|
+
|
|
1894
|
+
// Accepted states were shape-checked, but usage and reservations remain live inputs.
|
|
1895
|
+
return recovered ? Effect.andThen(checkFutureShapes(state), capacity) : capacity()
|
|
1896
|
+
}
|
|
1897
|
+
|
|
1898
|
+
const check = (runId: RunId) =>
|
|
1899
|
+
gate.withPermits(1)(
|
|
1900
|
+
Effect.flatMap(CurrentRunWriter, (writer) => {
|
|
1901
|
+
if (writer.threadId !== threadId) {
|
|
1902
|
+
return Effect.fail(failure('Progress writer belongs to another Thread'))
|
|
1903
|
+
}
|
|
1904
|
+
|
|
1905
|
+
const retained = cached.get(runId)
|
|
1906
|
+
|
|
1907
|
+
if (retained !== undefined) return checkState(retained, false)
|
|
1908
|
+
|
|
1909
|
+
return Effect.flatMap(writer.tail, (position) =>
|
|
1910
|
+
Effect.flatMap(loadUncachedState(runId, position.sequence), (state) => checkState(state, true)))
|
|
1911
|
+
}),
|
|
1912
|
+
)
|
|
1913
|
+
|
|
1914
|
+
return {
|
|
1915
|
+
commit,
|
|
1916
|
+
publish,
|
|
1917
|
+
preflight,
|
|
1918
|
+
reserve,
|
|
1919
|
+
defer,
|
|
1920
|
+
check,
|
|
1921
|
+
/** Scope a verified empty execution/evidence view to one owned initialization body.
|
|
1922
|
+
* The caller holds progress+mutation gates; a changed writer or tail never reuses it. */
|
|
1923
|
+
withVerifiedEmptyRun: <A, E, R>(
|
|
1924
|
+
runId: RunId,
|
|
1925
|
+
through: CanonicalSequence,
|
|
1926
|
+
body: Effect.Effect<A, E, R>,
|
|
1927
|
+
): Effect.Effect<A, E, R | CurrentRunWriter> =>
|
|
1928
|
+
Effect.flatMap(CurrentRunWriter, (writer) => {
|
|
1929
|
+
const view = { runId, through, writer }
|
|
1930
|
+
|
|
1931
|
+
verifiedEmpty = view
|
|
1932
|
+
|
|
1933
|
+
return body.pipe(
|
|
1934
|
+
Effect.ensuring(
|
|
1935
|
+
Effect.sync(() => {
|
|
1936
|
+
if (verifiedEmpty === view) verifiedEmpty = undefined
|
|
1937
|
+
}),
|
|
1938
|
+
),
|
|
1939
|
+
)
|
|
1940
|
+
}),
|
|
1941
|
+
stageUsage: (runId: RunId, bytes: number) => {
|
|
1942
|
+
if (bytes === 0) stagedUsage.delete(runId)
|
|
1943
|
+
else stagedUsage.set(runId, bytes)
|
|
1944
|
+
},
|
|
1945
|
+
}
|
|
1946
|
+
})
|