maka-agent 0.2.1-dev.91.20261018 → 0.2.2-dev.97.20261024

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (418) hide show
  1. package/DISCLAIMER-WIP +1 -1
  2. package/LICENSE +15 -11
  3. package/README.md +173 -1
  4. package/README.zh-CN.md +156 -0
  5. package/THIRD_PARTY_NOTICES.txt +12 -12
  6. package/dist/cli-core.js +1 -0
  7. package/dist/pi-goal.js +13 -0
  8. package/dist/pi-tui-runner.js +44 -1
  9. package/dist/run-command-core.js +256 -48
  10. package/dist/run-session-command.js +534 -0
  11. package/dist/run-session-result.js +38 -0
  12. package/dist/run-turn-outcome.js +174 -0
  13. package/dist/runtime-host-cli.js +32 -17
  14. package/dist/runtime-host-run-command.js +53 -165
  15. package/dist/runtime-host-run-mcp.js +3 -0
  16. package/dist/runtime-host-session-channel.js +3 -1
  17. package/dist/runtime-host-session-driver.js +5 -0
  18. package/dist/runtime-host-setup-command.js +114 -7
  19. package/dist/tui-copy-catalog.js +24 -0
  20. package/native/runtime-host-windows-task-launcher/prebuilds/win32-x64/maka-runtime-host-task-launcher.exe +0 -0
  21. package/node_modules/@ai-sdk/provider-utils/node_modules/undici/docs/docs/api/Client.md +2 -0
  22. package/node_modules/@ai-sdk/provider-utils/node_modules/undici/lib/core/connect.js +10 -4
  23. package/node_modules/@ai-sdk/provider-utils/node_modules/undici/lib/core/symbols.js +1 -0
  24. package/node_modules/@ai-sdk/provider-utils/node_modules/undici/lib/dispatcher/agent.js +6 -2
  25. package/node_modules/@ai-sdk/provider-utils/node_modules/undici/lib/dispatcher/client-h1.js +0 -2
  26. package/node_modules/@ai-sdk/provider-utils/node_modules/undici/lib/dispatcher/client-h2.js +0 -4
  27. package/node_modules/@ai-sdk/provider-utils/node_modules/undici/lib/dispatcher/client.js +3 -2
  28. package/node_modules/@ai-sdk/provider-utils/node_modules/undici/lib/dispatcher/dispatcher-base.js +12 -0
  29. package/node_modules/@ai-sdk/provider-utils/node_modules/undici/lib/handler/cache-handler.js +48 -0
  30. package/node_modules/@ai-sdk/provider-utils/node_modules/undici/lib/handler/deduplication-handler.js +57 -13
  31. package/node_modules/@ai-sdk/provider-utils/node_modules/undici/lib/handler/redirect-handler.js +8 -1
  32. package/node_modules/@ai-sdk/provider-utils/node_modules/undici/lib/interceptor/decompress.js +15 -5
  33. package/node_modules/@ai-sdk/provider-utils/node_modules/undici/lib/web/cookies/index.js +3 -1
  34. package/node_modules/@ai-sdk/provider-utils/node_modules/undici/lib/web/cookies/parse.js +138 -137
  35. package/node_modules/@ai-sdk/provider-utils/node_modules/undici/lib/web/cookies/util.js +21 -2
  36. package/node_modules/@ai-sdk/provider-utils/node_modules/undici/lib/web/eventsource/eventsource-stream.js +90 -9
  37. package/node_modules/@ai-sdk/provider-utils/node_modules/undici/lib/web/eventsource/eventsource.js +4 -1
  38. package/node_modules/@ai-sdk/provider-utils/node_modules/undici/lib/web/fetch/index.js +3 -3
  39. package/node_modules/@ai-sdk/provider-utils/node_modules/undici/lib/web/infra/index.js +14 -0
  40. package/node_modules/@ai-sdk/provider-utils/node_modules/undici/lib/web/subresource-integrity/subresource-integrity.js +5 -5
  41. package/node_modules/@ai-sdk/provider-utils/node_modules/undici/lib/web/websocket/receiver.js +13 -0
  42. package/node_modules/@ai-sdk/provider-utils/node_modules/undici/package.json +1 -1
  43. package/node_modules/@maka/acp-executor-plugin/dist/acp-catalog-store.js +87 -0
  44. package/node_modules/@maka/acp-executor-plugin/dist/acp-catalog.js +98 -21
  45. package/node_modules/@maka/acp-executor-plugin/dist/index.js +90 -14
  46. package/node_modules/@maka/acp-executor-plugin/dist/plugin.mjs +3997 -56
  47. package/node_modules/@maka/core/dist/assistant-turn-ids.js +34 -0
  48. package/node_modules/@maka/core/dist/bot-chat-settings.js +10 -1
  49. package/node_modules/@maka/core/dist/event-wait-delivery.js +67 -0
  50. package/node_modules/@maka/core/dist/event-wait.js +75 -10
  51. package/node_modules/@maka/core/dist/executor-catalog.js +3 -1
  52. package/node_modules/@maka/core/dist/goal.js +54 -2
  53. package/node_modules/@maka/core/dist/model-catalog.js +19 -1
  54. package/node_modules/@maka/core/dist/model-metadata.generated.js +4 -14
  55. package/node_modules/@maka/core/dist/model-metadata.js +42 -1
  56. package/node_modules/@maka/core/dist/models-dev-projection.js +32 -1
  57. package/node_modules/@maka/core/dist/provider-registry.js +16 -2
  58. package/node_modules/@maka/core/dist/runtime-event-store.js +0 -1
  59. package/node_modules/@maka/core/dist/runtime-event.js +24 -2
  60. package/node_modules/@maka/core/dist/runtime-invocation.js +10 -0
  61. package/node_modules/@maka/core/dist/session-checkin.js +31 -0
  62. package/node_modules/@maka/core/dist/tool-ledger-scanner.js +1 -9
  63. package/node_modules/@maka/core/dist/tool-recovery-bundle.js +1 -14
  64. package/node_modules/@maka/core/dist/turn-origin.js +14 -2
  65. package/node_modules/@maka/core/dist/usage-stats/types.js +1 -0
  66. package/node_modules/@maka/core/package.json +3 -0
  67. package/node_modules/@maka/eval/node_modules/undici/README.md +25 -3
  68. package/node_modules/@maka/eval/node_modules/undici/docs/docs/api/CacheStore.md +24 -9
  69. package/node_modules/@maka/eval/node_modules/undici/docs/docs/api/Client.md +11 -6
  70. package/node_modules/@maka/eval/node_modules/undici/docs/docs/api/Dispatcher.md +3 -0
  71. package/node_modules/@maka/eval/node_modules/undici/docs/docs/api/Errors.md +76 -1
  72. package/node_modules/@maka/eval/node_modules/undici/docs/docs/api/H2CClient.md +3 -2
  73. package/node_modules/@maka/eval/node_modules/undici/docs/docs/api/Interceptors.md +28 -8
  74. package/node_modules/@maka/eval/node_modules/undici/docs/docs/api/RedirectHandler.md +13 -10
  75. package/node_modules/@maka/eval/node_modules/undici/docs/docs/index.md +12 -12
  76. package/node_modules/@maka/eval/node_modules/undici/docs/docs/type-map.json +66 -66
  77. package/node_modules/@maka/eval/node_modules/undici/lib/cache/memory-cache-store.js +48 -13
  78. package/node_modules/@maka/eval/node_modules/undici/lib/cache/sqlite-cache-store.js +83 -5
  79. package/node_modules/@maka/eval/node_modules/undici/lib/core/connect.js +10 -4
  80. package/node_modules/@maka/eval/node_modules/undici/lib/core/constants.js +12 -1
  81. package/node_modules/@maka/eval/node_modules/undici/lib/core/errors.js +38 -1
  82. package/node_modules/@maka/eval/node_modules/undici/lib/core/request.js +16 -1
  83. package/node_modules/@maka/eval/node_modules/undici/lib/core/symbols.js +2 -0
  84. package/node_modules/@maka/eval/node_modules/undici/lib/core/util.js +26 -1
  85. package/node_modules/@maka/eval/node_modules/undici/lib/dispatcher/agent.js +6 -2
  86. package/node_modules/@maka/eval/node_modules/undici/lib/dispatcher/client-h1.js +116 -80
  87. package/node_modules/@maka/eval/node_modules/undici/lib/dispatcher/client-h2.js +124 -17
  88. package/node_modules/@maka/eval/node_modules/undici/lib/dispatcher/client.js +4 -1
  89. package/node_modules/@maka/eval/node_modules/undici/lib/dispatcher/dispatcher-base.js +3 -2
  90. package/node_modules/@maka/eval/node_modules/undici/lib/dispatcher/dispatcher1-wrapper.js +20 -3
  91. package/node_modules/@maka/eval/node_modules/undici/lib/dispatcher/parser-h1.js +1064 -0
  92. package/node_modules/@maka/eval/node_modules/undici/lib/dispatcher/pool-base.js +85 -31
  93. package/node_modules/@maka/eval/node_modules/undici/lib/dispatcher/pool.js +11 -8
  94. package/node_modules/@maka/eval/node_modules/undici/lib/dispatcher/proxy-agent.js +18 -8
  95. package/node_modules/@maka/eval/node_modules/undici/lib/dispatcher/retry-agent.js +10 -0
  96. package/node_modules/@maka/eval/node_modules/undici/lib/dispatcher/round-robin-pool.js +15 -5
  97. package/node_modules/@maka/eval/node_modules/undici/lib/handler/cache-handler.js +153 -15
  98. package/node_modules/@maka/eval/node_modules/undici/lib/handler/deduplication-handler.js +112 -51
  99. package/node_modules/@maka/eval/node_modules/undici/lib/handler/redirect-handler.js +28 -8
  100. package/node_modules/@maka/eval/node_modules/undici/lib/handler/retry-handler.js +44 -11
  101. package/node_modules/@maka/eval/node_modules/undici/lib/interceptor/cache.js +11 -3
  102. package/node_modules/@maka/eval/node_modules/undici/lib/interceptor/decompress.js +22 -9
  103. package/node_modules/@maka/eval/node_modules/undici/lib/interceptor/dns.js +11 -0
  104. package/node_modules/@maka/eval/node_modules/undici/lib/interceptor/response-error.js +72 -28
  105. package/node_modules/@maka/eval/node_modules/undici/lib/milo/LICENSE.md +7 -0
  106. package/node_modules/@maka/eval/node_modules/undici/lib/milo/no-simd.js +1002 -0
  107. package/node_modules/@maka/eval/node_modules/undici/lib/milo/simd.js +1002 -0
  108. package/node_modules/@maka/eval/node_modules/undici/lib/mock/mock-agent.js +11 -0
  109. package/node_modules/@maka/eval/node_modules/undici/lib/mock/snapshot-recorder.js +3 -2
  110. package/node_modules/@maka/eval/node_modules/undici/lib/mock/snapshot-utils.js +3 -2
  111. package/node_modules/@maka/eval/node_modules/undici/lib/util/cache.js +11 -5
  112. package/node_modules/@maka/eval/node_modules/undici/lib/web/cookies/util.js +19 -2
  113. package/node_modules/@maka/eval/node_modules/undici/lib/web/eventsource/eventsource-stream.js +69 -15
  114. package/node_modules/@maka/eval/node_modules/undici/lib/web/eventsource/eventsource.js +25 -2
  115. package/node_modules/@maka/eval/node_modules/undici/lib/web/fetch/body.js +47 -38
  116. package/node_modules/@maka/eval/node_modules/undici/lib/web/fetch/formdata-parser.js +12 -4
  117. package/node_modules/@maka/eval/node_modules/undici/lib/web/fetch/headers.js +2 -2
  118. package/node_modules/@maka/eval/node_modules/undici/lib/web/fetch/index.js +8 -4
  119. package/node_modules/@maka/eval/node_modules/undici/lib/web/subresource-integrity/subresource-integrity.js +5 -5
  120. package/node_modules/@maka/eval/node_modules/undici/lib/web/websocket/receiver.js +25 -4
  121. package/node_modules/@maka/eval/node_modules/undici/package.json +9 -7
  122. package/node_modules/@maka/mcp/package.json +1 -1
  123. package/node_modules/@maka/runtime/dist/agent-run.js +5 -1
  124. package/node_modules/@maka/runtime/dist/agent-swarm-status-tool.js +0 -1
  125. package/node_modules/@maka/runtime/dist/ai-sdk-compaction.js +29 -15
  126. package/node_modules/@maka/runtime/dist/ai-sdk-message-projection.js +10 -1
  127. package/node_modules/@maka/runtime/dist/ai-sdk-turn.js +125 -131
  128. package/node_modules/@maka/runtime/dist/bots/bot-registry.js +19 -3
  129. package/node_modules/@maka/runtime/dist/bots/feishu-bridge.js +178 -6
  130. package/node_modules/@maka/runtime/dist/bots/wechat-bridge.js +163 -9
  131. package/node_modules/@maka/runtime/dist/context-budget-policy.js +9 -2
  132. package/node_modules/@maka/runtime/dist/event-wait-tool.js +69 -0
  133. package/node_modules/@maka/runtime/dist/goal-continuation.js +195 -117
  134. package/node_modules/@maka/runtime/dist/goal-state.js +8 -0
  135. package/node_modules/@maka/runtime/dist/history-compact-checkpoint.js +34 -4
  136. package/node_modules/@maka/runtime/dist/history-compaction.js +7 -0
  137. package/node_modules/@maka/runtime/dist/memory-extraction.js +0 -2
  138. package/node_modules/@maka/runtime/dist/model-factory.js +26 -6
  139. package/node_modules/@maka/runtime/dist/model-fetcher.js +8 -1
  140. package/node_modules/@maka/runtime/dist/model-history.js +16 -3
  141. package/node_modules/@maka/runtime/dist/openai-codex-history-compactor.js +15 -5
  142. package/node_modules/@maka/runtime/dist/plan-tools.js +0 -3
  143. package/node_modules/@maka/runtime/dist/plugin-executor-backend.js +49 -2
  144. package/node_modules/@maka/runtime/dist/plugin-executor-service.js +12 -3
  145. package/node_modules/@maka/runtime/dist/provider-error-classification.js +2 -0
  146. package/node_modules/@maka/runtime/dist/recovery-resolver.js +2 -1
  147. package/node_modules/@maka/runtime/dist/runtime-context.js +53 -0
  148. package/node_modules/@maka/runtime/dist/runtime-event-read-model.js +3 -0
  149. package/node_modules/@maka/runtime/dist/sandbox-boundary-path.js +79 -2
  150. package/node_modules/@maka/runtime/dist/session-checkin.js +331 -0
  151. package/node_modules/@maka/runtime/dist/session-event-runtime-mapper.js +7 -0
  152. package/node_modules/@maka/runtime/dist/session-manager.js +5 -1
  153. package/node_modules/@maka/runtime/dist/shell-run-manager.js +34 -0
  154. package/node_modules/@maka/runtime/dist/stream-graph-supervisor-tools.js +0 -3
  155. package/node_modules/@maka/runtime/dist/telemetry/builtin-pricing.js +21 -2
  156. package/node_modules/@maka/runtime/dist/tool-runtime.js +11 -2
  157. package/node_modules/@maka/runtime/dist/workers/filesystem-worker.mjs +50 -1
  158. package/node_modules/@maka/runtime/node_modules/@slack/socket-mode/node_modules/undici/docs/docs/api/Client.md +2 -0
  159. package/node_modules/@maka/runtime/node_modules/@slack/socket-mode/node_modules/undici/lib/core/connect.js +10 -4
  160. package/node_modules/@maka/runtime/node_modules/@slack/socket-mode/node_modules/undici/lib/core/symbols.js +1 -0
  161. package/node_modules/@maka/runtime/node_modules/@slack/socket-mode/node_modules/undici/lib/dispatcher/agent.js +6 -2
  162. package/node_modules/@maka/runtime/node_modules/@slack/socket-mode/node_modules/undici/lib/dispatcher/client-h1.js +0 -2
  163. package/node_modules/@maka/runtime/node_modules/@slack/socket-mode/node_modules/undici/lib/dispatcher/client-h2.js +0 -4
  164. package/node_modules/@maka/runtime/node_modules/@slack/socket-mode/node_modules/undici/lib/dispatcher/client.js +3 -2
  165. package/node_modules/@maka/runtime/node_modules/@slack/socket-mode/node_modules/undici/lib/dispatcher/dispatcher-base.js +12 -0
  166. package/node_modules/@maka/runtime/node_modules/@slack/socket-mode/node_modules/undici/lib/handler/cache-handler.js +48 -0
  167. package/node_modules/@maka/runtime/node_modules/@slack/socket-mode/node_modules/undici/lib/handler/deduplication-handler.js +57 -13
  168. package/node_modules/@maka/runtime/node_modules/@slack/socket-mode/node_modules/undici/lib/handler/redirect-handler.js +8 -1
  169. package/node_modules/@maka/runtime/node_modules/@slack/socket-mode/node_modules/undici/lib/interceptor/decompress.js +15 -5
  170. package/node_modules/@maka/runtime/node_modules/@slack/socket-mode/node_modules/undici/lib/web/cookies/index.js +3 -1
  171. package/node_modules/@maka/runtime/node_modules/@slack/socket-mode/node_modules/undici/lib/web/cookies/parse.js +138 -137
  172. package/node_modules/@maka/runtime/node_modules/@slack/socket-mode/node_modules/undici/lib/web/cookies/util.js +21 -2
  173. package/node_modules/@maka/runtime/node_modules/@slack/socket-mode/node_modules/undici/lib/web/eventsource/eventsource-stream.js +90 -9
  174. package/node_modules/@maka/runtime/node_modules/@slack/socket-mode/node_modules/undici/lib/web/eventsource/eventsource.js +4 -1
  175. package/node_modules/@maka/runtime/node_modules/@slack/socket-mode/node_modules/undici/lib/web/fetch/index.js +3 -3
  176. package/node_modules/@maka/runtime/node_modules/@slack/socket-mode/node_modules/undici/lib/web/infra/index.js +14 -0
  177. package/node_modules/@maka/runtime/node_modules/@slack/socket-mode/node_modules/undici/lib/web/subresource-integrity/subresource-integrity.js +5 -5
  178. package/node_modules/@maka/runtime/node_modules/@slack/socket-mode/node_modules/undici/lib/web/websocket/receiver.js +13 -0
  179. package/node_modules/@maka/runtime/node_modules/@slack/socket-mode/node_modules/undici/package.json +1 -1
  180. package/node_modules/@maka/runtime/node_modules/@slack/socket-mode/package.json +1 -1
  181. package/node_modules/@maka/runtime/node_modules/undici/README.md +25 -3
  182. package/node_modules/@maka/runtime/node_modules/undici/docs/docs/api/CacheStore.md +24 -9
  183. package/node_modules/@maka/runtime/node_modules/undici/docs/docs/api/Client.md +11 -6
  184. package/node_modules/@maka/runtime/node_modules/undici/docs/docs/api/Dispatcher.md +3 -0
  185. package/node_modules/@maka/runtime/node_modules/undici/docs/docs/api/Errors.md +76 -1
  186. package/node_modules/@maka/runtime/node_modules/undici/docs/docs/api/H2CClient.md +3 -2
  187. package/node_modules/@maka/runtime/node_modules/undici/docs/docs/api/Interceptors.md +28 -8
  188. package/node_modules/@maka/runtime/node_modules/undici/docs/docs/api/RedirectHandler.md +13 -10
  189. package/node_modules/@maka/runtime/node_modules/undici/docs/docs/index.md +12 -12
  190. package/node_modules/@maka/runtime/node_modules/undici/docs/docs/type-map.json +66 -66
  191. package/node_modules/@maka/runtime/node_modules/undici/lib/cache/memory-cache-store.js +48 -13
  192. package/node_modules/@maka/runtime/node_modules/undici/lib/cache/sqlite-cache-store.js +83 -5
  193. package/node_modules/@maka/runtime/node_modules/undici/lib/core/connect.js +10 -4
  194. package/node_modules/@maka/runtime/node_modules/undici/lib/core/constants.js +12 -1
  195. package/node_modules/@maka/runtime/node_modules/undici/lib/core/errors.js +38 -1
  196. package/node_modules/@maka/runtime/node_modules/undici/lib/core/request.js +16 -1
  197. package/node_modules/@maka/runtime/node_modules/undici/lib/core/symbols.js +2 -0
  198. package/node_modules/@maka/runtime/node_modules/undici/lib/core/util.js +26 -1
  199. package/node_modules/@maka/runtime/node_modules/undici/lib/dispatcher/agent.js +6 -2
  200. package/node_modules/@maka/runtime/node_modules/undici/lib/dispatcher/client-h1.js +116 -80
  201. package/node_modules/@maka/runtime/node_modules/undici/lib/dispatcher/client-h2.js +124 -17
  202. package/node_modules/@maka/runtime/node_modules/undici/lib/dispatcher/client.js +4 -1
  203. package/node_modules/@maka/runtime/node_modules/undici/lib/dispatcher/dispatcher-base.js +3 -2
  204. package/node_modules/@maka/runtime/node_modules/undici/lib/dispatcher/dispatcher1-wrapper.js +20 -3
  205. package/node_modules/@maka/runtime/node_modules/undici/lib/dispatcher/parser-h1.js +1064 -0
  206. package/node_modules/@maka/runtime/node_modules/undici/lib/dispatcher/pool-base.js +85 -31
  207. package/node_modules/@maka/runtime/node_modules/undici/lib/dispatcher/pool.js +11 -8
  208. package/node_modules/@maka/runtime/node_modules/undici/lib/dispatcher/proxy-agent.js +18 -8
  209. package/node_modules/@maka/runtime/node_modules/undici/lib/dispatcher/retry-agent.js +10 -0
  210. package/node_modules/@maka/runtime/node_modules/undici/lib/dispatcher/round-robin-pool.js +15 -5
  211. package/node_modules/@maka/runtime/node_modules/undici/lib/handler/cache-handler.js +153 -15
  212. package/node_modules/@maka/runtime/node_modules/undici/lib/handler/deduplication-handler.js +112 -51
  213. package/node_modules/@maka/runtime/node_modules/undici/lib/handler/redirect-handler.js +28 -8
  214. package/node_modules/@maka/runtime/node_modules/undici/lib/handler/retry-handler.js +44 -11
  215. package/node_modules/@maka/runtime/node_modules/undici/lib/interceptor/cache.js +11 -3
  216. package/node_modules/@maka/runtime/node_modules/undici/lib/interceptor/decompress.js +22 -9
  217. package/node_modules/@maka/runtime/node_modules/undici/lib/interceptor/dns.js +11 -0
  218. package/node_modules/@maka/runtime/node_modules/undici/lib/interceptor/response-error.js +72 -28
  219. package/node_modules/@maka/runtime/node_modules/undici/lib/milo/LICENSE.md +7 -0
  220. package/node_modules/@maka/runtime/node_modules/undici/lib/milo/no-simd.js +1002 -0
  221. package/node_modules/@maka/runtime/node_modules/undici/lib/milo/simd.js +1002 -0
  222. package/node_modules/@maka/runtime/node_modules/undici/lib/mock/mock-agent.js +11 -0
  223. package/node_modules/@maka/runtime/node_modules/undici/lib/mock/snapshot-recorder.js +3 -2
  224. package/node_modules/@maka/runtime/node_modules/undici/lib/mock/snapshot-utils.js +3 -2
  225. package/node_modules/@maka/runtime/node_modules/undici/lib/util/cache.js +11 -5
  226. package/node_modules/@maka/runtime/node_modules/undici/lib/web/cookies/util.js +19 -2
  227. package/node_modules/@maka/runtime/node_modules/undici/lib/web/eventsource/eventsource-stream.js +69 -15
  228. package/node_modules/@maka/runtime/node_modules/undici/lib/web/eventsource/eventsource.js +25 -2
  229. package/node_modules/@maka/runtime/node_modules/undici/lib/web/fetch/body.js +47 -38
  230. package/node_modules/@maka/runtime/node_modules/undici/lib/web/fetch/formdata-parser.js +12 -4
  231. package/node_modules/@maka/runtime/node_modules/undici/lib/web/fetch/headers.js +2 -2
  232. package/node_modules/@maka/runtime/node_modules/undici/lib/web/fetch/index.js +8 -4
  233. package/node_modules/@maka/runtime/node_modules/undici/lib/web/subresource-integrity/subresource-integrity.js +5 -5
  234. package/node_modules/@maka/runtime/node_modules/undici/lib/web/websocket/receiver.js +25 -4
  235. package/node_modules/@maka/runtime/node_modules/undici/package.json +9 -7
  236. package/node_modules/@maka/runtime/package.json +3 -0
  237. package/node_modules/@maka/runtime-host/dist/assistant/bot/bridge.js +8 -0
  238. package/node_modules/@maka/runtime-host/dist/assistant/bot/capabilities.js +1 -0
  239. package/node_modules/@maka/runtime-host/dist/assistant/bot/controller.js +92 -22
  240. package/node_modules/@maka/runtime-host/dist/assistant/bot/host.js +11 -2
  241. package/node_modules/@maka/runtime-host/dist/assistant/bot/store.js +71 -2
  242. package/node_modules/@maka/runtime-host/dist/assistant/bot/turn-ids.js +38 -0
  243. package/node_modules/@maka/runtime-host/dist/assistant/index-initiative/assistant.js +7 -0
  244. package/node_modules/@maka/runtime-host/dist/assistant/index-initiative/controller.js +193 -32
  245. package/node_modules/@maka/runtime-host/dist/assistant/index-initiative/discovery-capabilities.js +69 -0
  246. package/node_modules/@maka/runtime-host/dist/assistant/index-initiative/host.js +15 -28
  247. package/node_modules/@maka/runtime-host/dist/assistant/index-initiative/notebook.js +286 -0
  248. package/node_modules/@maka/runtime-host/dist/assistant/index-initiative/prompt.js +31 -6
  249. package/node_modules/@maka/runtime-host/dist/assistant/index-initiative/store.js +18 -1
  250. package/node_modules/@maka/runtime-host/dist/assistant/memory-network/controller.js +203 -71
  251. package/node_modules/@maka/runtime-host/dist/assistant/memory-network/host.js +21 -5
  252. package/node_modules/@maka/runtime-host/dist/assistant/proactive-matters/condition-observer.js +94 -0
  253. package/node_modules/@maka/runtime-host/dist/assistant/proactive-matters/condition-store.js +253 -0
  254. package/node_modules/@maka/runtime-host/dist/assistant/proactive-matters/condition.js +133 -0
  255. package/node_modules/@maka/runtime-host/dist/assistant/proactive-matters/controller.js +27 -14
  256. package/node_modules/@maka/runtime-host/dist/assistant/proactive-matters/delegation.js +37 -5
  257. package/node_modules/@maka/runtime-host/dist/assistant/proactive-matters/delivery.js +1 -1
  258. package/node_modules/@maka/runtime-host/dist/assistant/proactive-matters/host.js +16 -2
  259. package/node_modules/@maka/runtime-host/dist/assistant/proactive-matters/matter.js +13 -0
  260. package/node_modules/@maka/runtime-host/dist/assistant/proactive-matters/prompt.js +7 -5
  261. package/node_modules/@maka/runtime-host/dist/assistant/proactive-matters/review.js +68 -14
  262. package/node_modules/@maka/runtime-host/dist/assistant/proactive-matters/settlement-context.js +126 -0
  263. package/node_modules/@maka/runtime-host/dist/assistant/proactive-matters/store.js +92 -6
  264. package/node_modules/@maka/runtime-host/dist/assistant/proactive-matters/tools.js +93 -23
  265. package/node_modules/@maka/runtime-host/dist/assistant/service.js +72 -16
  266. package/node_modules/@maka/runtime-host/dist/client/host-profile.js +30 -1
  267. package/node_modules/@maka/runtime-host/dist/client/ssh-operator-activation.js +9 -2
  268. package/node_modules/@maka/runtime-host/dist/protocol/goal.js +119 -1
  269. package/node_modules/@maka/runtime-host/dist/protocol/index.js +10 -3
  270. package/node_modules/@maka/runtime-host/dist/protocol/operations.js +8 -1
  271. package/node_modules/@maka/runtime-host/dist/protocol/personal-assistant.js +1 -0
  272. package/node_modules/@maka/runtime-host/dist/protocol/plugin-platform.js +7 -1
  273. package/node_modules/@maka/runtime-host/dist/protocol/session-checkin.js +133 -0
  274. package/node_modules/@maka/runtime-host/dist/server/agent-settings-tools.js +0 -1
  275. package/node_modules/@maka/runtime-host/dist/server/canonical-session-projection.js +3 -2
  276. package/node_modules/@maka/runtime-host/dist/server/event-wait-manager.js +151 -20
  277. package/node_modules/@maka/runtime-host/dist/server/event-wait-observation-store.js +19 -0
  278. package/node_modules/@maka/runtime-host/dist/server/execution-composition.js +127 -11
  279. package/node_modules/@maka/runtime-host/dist/server/execution-model-authority.js +47 -3
  280. package/node_modules/@maka/runtime-host/dist/server/execution-model-composition.js +11 -0
  281. package/node_modules/@maka/runtime-host/dist/server/external-session-coordinator.js +5 -2
  282. package/node_modules/@maka/runtime-host/dist/server/goal-coordinator.js +343 -53
  283. package/node_modules/@maka/runtime-host/dist/server/goal-event-waits.js +457 -0
  284. package/node_modules/@maka/runtime-host/dist/server/goal-execution-coordinator.js +23 -6
  285. package/node_modules/@maka/runtime-host/dist/server/goal-projection.js +24 -3
  286. package/node_modules/@maka/runtime-host/dist/server/hosted-execution-recovery.js +5 -1
  287. package/node_modules/@maka/runtime-host/dist/server/index.js +2 -0
  288. package/node_modules/@maka/runtime-host/dist/server/interactive-run-composer.js +44 -2
  289. package/node_modules/@maka/runtime-host/dist/server/personal-assistant-workspace.js +78 -0
  290. package/node_modules/@maka/runtime-host/dist/server/root-turn-coordinator.js +8 -0
  291. package/node_modules/@maka/runtime-host/dist/server/session-catalog-coordinator.js +73 -3
  292. package/node_modules/@maka/runtime-host/dist/server/session-checkin.js +257 -0
  293. package/node_modules/@maka/runtime-host/dist/server/session-checkpoint-coordinator.js +282 -0
  294. package/node_modules/@maka/runtime-host/dist/server/session-checkpoint-publication.js +250 -0
  295. package/node_modules/@maka/runtime-host/dist/server/shell-run-event-wait-access.js +166 -0
  296. package/node_modules/@maka/runtime-host/dist/server/shell-run-event-wait-adapter.js +59 -0
  297. package/node_modules/@maka/runtime-host/dist/server/skill-catalog-repository.js +4 -2
  298. package/node_modules/@maka/storage/dist/agent-run-store-contract.js +14 -3
  299. package/node_modules/@maka/storage/dist/agent-run-store.js +10 -0
  300. package/node_modules/@maka/storage/dist/codex-session-adapter.js +20 -3
  301. package/node_modules/@maka/storage/dist/context-offload-snapshot.js +2 -1
  302. package/node_modules/@maka/storage/dist/event-wait-authority.js +18 -1
  303. package/node_modules/@maka/storage/dist/event-wait-bridge.js +336 -0
  304. package/node_modules/@maka/storage/dist/execution-stores.js +39 -1
  305. package/node_modules/@maka/storage/dist/external-session-images.js +131 -0
  306. package/node_modules/@maka/storage/dist/external-session-importer.js +12 -2
  307. package/node_modules/@maka/storage/dist/goal-authority.js +2 -0
  308. package/node_modules/@maka/storage/dist/local-execution-persistence.js +27 -1
  309. package/node_modules/@maka/storage/dist/operational-state-store.js +1 -1
  310. package/node_modules/@maka/storage/dist/production-session-snapshot.js +14 -6
  311. package/node_modules/@maka/storage/dist/runtime-event-persistence.js +1 -0
  312. package/node_modules/@maka/storage/dist/session-bundle-policy.js +116 -16
  313. package/node_modules/@maka/storage/dist/session-checkpoint-publication-store.js +204 -0
  314. package/node_modules/@maka/storage/dist/session-store.js +37 -8
  315. package/node_modules/@maka/storage/dist/snapshot-operation-gate.js +65 -0
  316. package/node_modules/@maka/storage/dist/sqlite-context-offload-schema.js +3 -3
  317. package/node_modules/@maka/storage/dist/sqlite-context-offload-store.js +4 -2
  318. package/node_modules/@maka/storage/dist/sqlite-event-wait-authority.js +81 -8
  319. package/node_modules/@maka/storage/dist/sqlite-event-wait-bridge.js +94 -0
  320. package/node_modules/@maka/storage/dist/sqlite-reclamation-checkpoint.js +37 -0
  321. package/node_modules/@maka/storage/dist/sqlite-runtime-schema.js +7 -8
  322. package/node_modules/@maka/storage/dist/sqlite-runtime-store.js +38 -162
  323. package/node_modules/@maka/storage/dist/sqlite-workflow-schema.js +7 -4
  324. package/node_modules/@maka/storage/dist/tool-commit-validation.js +1 -1
  325. package/node_modules/@maka/storage/package.json +4 -0
  326. package/node_modules/@modelcontextprotocol/client/dist/index-B-4n9P0J.d.mts.map +1 -0
  327. package/node_modules/@modelcontextprotocol/client/dist/index-J2FaQ-KY.d.cts.map +1 -0
  328. package/node_modules/@modelcontextprotocol/client/dist/index.cjs +76 -22
  329. package/node_modules/@modelcontextprotocol/client/dist/index.d.cts.map +1 -1
  330. package/node_modules/@modelcontextprotocol/client/dist/index.d.mts.map +1 -1
  331. package/node_modules/@modelcontextprotocol/client/dist/index.mjs +76 -22
  332. package/node_modules/@modelcontextprotocol/client/dist/shimsWorkerd.cjs +1 -1
  333. package/node_modules/@modelcontextprotocol/client/dist/shimsWorkerd.mjs +1 -1
  334. package/node_modules/@modelcontextprotocol/client/dist/{src-oZBjf3U1.cjs → src-DpAS0jfg.cjs} +105 -8
  335. package/node_modules/@modelcontextprotocol/client/dist/{src-xKF0PJN-.mjs → src-WCy6ifGf.mjs} +88 -9
  336. package/node_modules/@modelcontextprotocol/client/dist/stdio.cjs +1 -1
  337. package/node_modules/@modelcontextprotocol/client/dist/stdio.mjs +1 -1
  338. package/node_modules/@modelcontextprotocol/client/node_modules/@modelcontextprotocol/core/package.json +2 -2
  339. package/node_modules/@modelcontextprotocol/client/package.json +5 -5
  340. package/node_modules/fast-uri/.github/workflows/release.yml +35 -0
  341. package/node_modules/fast-uri/README.md +8 -0
  342. package/node_modules/fast-uri/index.js +98 -18
  343. package/node_modules/fast-uri/lib/utils.js +52 -0
  344. package/node_modules/fast-uri/package.json +1 -1
  345. package/node_modules/follow-redirects/index.js +10 -1
  346. package/node_modules/follow-redirects/package.json +1 -1
  347. package/node_modules/ip-address/README.md +104 -104
  348. package/node_modules/ip-address/dist/ipv4.js +7 -0
  349. package/node_modules/ip-address/dist/ipv6.js +7 -4
  350. package/node_modules/ip-address/dist/v6/constants.js +3 -0
  351. package/node_modules/ip-address/package.json +2 -1
  352. package/node_modules/undici/README.md +25 -3
  353. package/node_modules/undici/docs/docs/api/CacheStore.md +24 -9
  354. package/node_modules/undici/docs/docs/api/Client.md +11 -6
  355. package/node_modules/undici/docs/docs/api/Dispatcher.md +3 -0
  356. package/node_modules/undici/docs/docs/api/Errors.md +76 -1
  357. package/node_modules/undici/docs/docs/api/H2CClient.md +3 -2
  358. package/node_modules/undici/docs/docs/api/Interceptors.md +28 -8
  359. package/node_modules/undici/docs/docs/api/RedirectHandler.md +13 -10
  360. package/node_modules/undici/docs/docs/index.md +12 -12
  361. package/node_modules/undici/docs/docs/type-map.json +66 -66
  362. package/node_modules/undici/lib/cache/memory-cache-store.js +48 -13
  363. package/node_modules/undici/lib/cache/sqlite-cache-store.js +83 -5
  364. package/node_modules/undici/lib/core/connect.js +10 -4
  365. package/node_modules/undici/lib/core/constants.js +12 -1
  366. package/node_modules/undici/lib/core/errors.js +38 -1
  367. package/node_modules/undici/lib/core/request.js +16 -1
  368. package/node_modules/undici/lib/core/symbols.js +2 -0
  369. package/node_modules/undici/lib/core/util.js +26 -1
  370. package/node_modules/undici/lib/dispatcher/agent.js +6 -2
  371. package/node_modules/undici/lib/dispatcher/client-h1.js +116 -80
  372. package/node_modules/undici/lib/dispatcher/client-h2.js +124 -17
  373. package/node_modules/undici/lib/dispatcher/client.js +4 -1
  374. package/node_modules/undici/lib/dispatcher/dispatcher-base.js +3 -2
  375. package/node_modules/undici/lib/dispatcher/dispatcher1-wrapper.js +20 -3
  376. package/node_modules/undici/lib/dispatcher/parser-h1.js +1064 -0
  377. package/node_modules/undici/lib/dispatcher/pool-base.js +85 -31
  378. package/node_modules/undici/lib/dispatcher/pool.js +11 -8
  379. package/node_modules/undici/lib/dispatcher/proxy-agent.js +18 -8
  380. package/node_modules/undici/lib/dispatcher/retry-agent.js +10 -0
  381. package/node_modules/undici/lib/dispatcher/round-robin-pool.js +15 -5
  382. package/node_modules/undici/lib/handler/cache-handler.js +153 -15
  383. package/node_modules/undici/lib/handler/deduplication-handler.js +112 -51
  384. package/node_modules/undici/lib/handler/redirect-handler.js +28 -8
  385. package/node_modules/undici/lib/handler/retry-handler.js +44 -11
  386. package/node_modules/undici/lib/interceptor/cache.js +11 -3
  387. package/node_modules/undici/lib/interceptor/decompress.js +22 -9
  388. package/node_modules/undici/lib/interceptor/dns.js +11 -0
  389. package/node_modules/undici/lib/interceptor/response-error.js +72 -28
  390. package/node_modules/undici/lib/milo/LICENSE.md +7 -0
  391. package/node_modules/undici/lib/milo/no-simd.js +1002 -0
  392. package/node_modules/undici/lib/milo/simd.js +1002 -0
  393. package/node_modules/undici/lib/mock/mock-agent.js +11 -0
  394. package/node_modules/undici/lib/mock/snapshot-recorder.js +3 -2
  395. package/node_modules/undici/lib/mock/snapshot-utils.js +3 -2
  396. package/node_modules/undici/lib/util/cache.js +11 -5
  397. package/node_modules/undici/lib/web/cookies/util.js +19 -2
  398. package/node_modules/undici/lib/web/eventsource/eventsource-stream.js +69 -15
  399. package/node_modules/undici/lib/web/eventsource/eventsource.js +25 -2
  400. package/node_modules/undici/lib/web/fetch/body.js +47 -38
  401. package/node_modules/undici/lib/web/fetch/formdata-parser.js +12 -4
  402. package/node_modules/undici/lib/web/fetch/headers.js +2 -2
  403. package/node_modules/undici/lib/web/fetch/index.js +8 -4
  404. package/node_modules/undici/lib/web/subresource-integrity/subresource-integrity.js +5 -5
  405. package/node_modules/undici/lib/web/websocket/receiver.js +25 -4
  406. package/node_modules/undici/package.json +9 -7
  407. package/node_modules/zod/package.json +1 -1
  408. package/node_modules/zod/v4/core/compile.cjs +8 -0
  409. package/node_modules/zod/v4/core/compile.js +8 -0
  410. package/node_modules/zod/v4/core/schemas.cjs +30 -0
  411. package/node_modules/zod/v4/core/schemas.js +30 -0
  412. package/node_modules/zod/v4/core/util.cjs +15 -0
  413. package/node_modules/zod/v4/core/util.js +14 -0
  414. package/node_modules/zod/v4/core/versions.cjs +1 -1
  415. package/node_modules/zod/v4/core/versions.js +1 -1
  416. package/package.json +2 -2
  417. package/node_modules/@modelcontextprotocol/client/dist/index-D_CiY-T3.d.cts.map +0 -1
  418. package/node_modules/@modelcontextprotocol/client/dist/index-D_QDtbiv.d.mts.map +0 -1
@@ -16,6 +16,7 @@
16
16
  * specific language governing permissions and limitations
17
17
  * under the License.
18
18
  */
19
+ import { HostGoalEventWaitBridge } from './goal-event-waits.js';
19
20
  import { randomUUID } from 'node:crypto';
20
21
  import { sameGoalControlLease, } from '@maka/core/goal';
21
22
  import { GoalContinuationCoordinator, } from '@maka/runtime/goal-continuation';
@@ -24,6 +25,8 @@ import { buildGoalTools } from '@maka/runtime/goal-tools';
24
25
  import { isSessionNotFoundError } from '@maka/storage/execution-stores';
25
26
  import { authenticateInteractiveGoalAuthorityWriter, } from '@maka/storage/goal-authority';
26
27
  import { projectGoalState } from './goal-projection.js';
28
+ import { GOAL_EVENT_WAIT_SUMMARY_LIMITS, } from '../protocol/goal.js';
29
+ import { truncateUtf8 } from '@maka/core/diagnostic-log';
27
30
  import { waitForHostedExecutionTerminal } from './hosted-execution-wait.js';
28
31
  import { goalTurnOutcomeFromHostedExecution } from './goal-execution-coordinator.js';
29
32
  import { buildGoalEvaluationContext } from './goal-evaluation-context.js';
@@ -33,6 +36,7 @@ export class HostGoalCoordinator {
33
36
  'goal.query': (input) => this.#query(input.sessionId),
34
37
  'goal.arm': (input) => this.#arm(input),
35
38
  'goal.control': (input) => this.#control(input),
39
+ 'goal.event-wait.cancel': (input) => this.#cancelEventWait(input),
36
40
  };
37
41
  manager;
38
42
  continuation;
@@ -50,7 +54,7 @@ export class HostGoalCoordinator {
50
54
  /**
51
55
  * Token count per Session as of the last continuation read, which is what a
52
56
  * settling Turn reports so the Goal can measure its budget. It is not a
53
- * baseline for a new Goal: only an evaluation writes here, so a Session that
57
+ * baseline for a new Goal: only evaluation context reads refresh it. A Session that
54
58
  * has never run one has no entry, and `tokensBaselinePending` on the Goal is
55
59
  * what carries that until the first Turn carrying it settles.
56
60
  */
@@ -60,6 +64,10 @@ export class HostGoalCoordinator {
60
64
  #queuedAuthorityCommits = new Map();
61
65
  #persistenceLane = Promise.resolve();
62
66
  #persistenceFailure;
67
+ eventWaitBridge;
68
+ #jointLane = Promise.resolve();
69
+ #jointBusy = false;
70
+ #deferredGoalChanges = [];
63
71
  #prepared = false;
64
72
  #draining = false;
65
73
  constructor(options) {
@@ -84,6 +92,28 @@ export class HostGoalCoordinator {
84
92
  const tokenCache = this.#tokenCache;
85
93
  this.continuation = new GoalContinuationCoordinator({
86
94
  goalManager: this.manager,
95
+ eventWait: {
96
+ isGated: (sessionId) => !!this.#authorityBySession.get(sessionId)?.record.activeEventWait,
97
+ ownsSource: (sessionId, turnId, controlLease) => {
98
+ const record = this.#authorityBySession.get(sessionId)?.record;
99
+ const gate = record?.activeEventWait;
100
+ return (record?.goal.status === 'active' &&
101
+ !!gate &&
102
+ !gate.settled &&
103
+ gate.sourceExecution.sessionId === sessionId &&
104
+ gate.sourceExecution.turnId === turnId &&
105
+ sameGoalControlLease(gate.controlLease, controlLease) &&
106
+ sameGoalControlLease(record.controlLease, controlLease));
107
+ },
108
+ settleCompleted: async (sessionId, turnId, controlLease) => {
109
+ if (!this.eventWaitBridge)
110
+ throw new Error('Event wait persistence is unavailable');
111
+ await this.eventWaitBridge.settleCompleted(sessionId, turnId, controlLease);
112
+ },
113
+ },
114
+ mutateGoal: (operation) => this.#mutateGoal(() => operation({
115
+ recordPendingContinuation: (pending) => this.#recordPendingContinuation(pending, () => this.#flushGoalPersistence()),
116
+ })),
87
117
  acquireActivity: () => this.#acquireResidency(),
88
118
  evaluator: options.evaluator,
89
119
  getRecentContext: async (sessionId) => {
@@ -104,13 +134,63 @@ export class HostGoalCoordinator {
104
134
  settleCurrentExecution: (sessionId, turnId) => this.#settleCurrentExecution(sessionId, turnId),
105
135
  },
106
136
  });
107
- this.tools = Object.freeze(buildGoalTools({
108
- goalManager: this.manager,
109
- goalContinuation: this.continuation,
110
- isAvailable: () => !this.#draining,
111
- flush: (sessionId) => this.#flushGoalState(sessionId),
112
- now,
113
- }));
137
+ if (options.stores.eventWaitStore && options.stores.runtimeEventStore) {
138
+ this.eventWaitBridge = new HostGoalEventWaitBridge({
139
+ stores: {
140
+ eventWaitStore: options.stores.eventWaitStore,
141
+ runtimeEventStore: options.stores.runtimeEventStore,
142
+ agentRunStore: options.stores.agentRunStore,
143
+ },
144
+ authority: (sessionId) => this.#authorityBySession.get(sessionId),
145
+ changed: this.#onProjectionChanged,
146
+ mutate: (sessionId, operation) => this.#mutateWait(sessionId, operation),
147
+ settleAdditional: (sessionId, tokensNow, controlLease) => this.#mutateGoal(async () => {
148
+ const goal = this.manager.get(sessionId);
149
+ if (goal?.status === 'active' &&
150
+ this.manager.matchesControlLease(sessionId, controlLease))
151
+ this.manager.settleTurn(sessionId, {
152
+ checkpoint: { goalId: goal.id, revision: goal.revision },
153
+ verdict: 'continue',
154
+ tokensNow,
155
+ reason: goal.lastReason ?? 'Explicit event wait remains outstanding.',
156
+ });
157
+ }),
158
+ getTokens: async (sessionId, throughTurnId) => {
159
+ const { messages } = await options.readSessionContext(sessionId);
160
+ return tokenCount(messages, throughTurnId);
161
+ },
162
+ gate: (sessionId) => this.continuation.gateEventWait(sessionId),
163
+ canRegister: (sessionId, turnId) => this.continuation.mutationStanding(sessionId, turnId).kind === 'held',
164
+ deliver: (pending) => this.continuation.recoverPendingContinuation(pending),
165
+ pause: (sessionId, reason, controlLease, waitId) => this.#mutateGoal(async () => {
166
+ if (!this.manager.matchesControlLease(sessionId, controlLease) ||
167
+ this.#authorityBySession.get(sessionId)?.record.activeEventWait?.waitId !== waitId)
168
+ return;
169
+ this.manager.pause(sessionId, { reason });
170
+ this.continuation.invalidateSession(sessionId);
171
+ }),
172
+ onError: (error) => {
173
+ this.#persistenceFailure ??= error;
174
+ this.#requestDrain();
175
+ },
176
+ now,
177
+ newId: this.#newId,
178
+ }, options.eventWaitSources);
179
+ }
180
+ this.tools = Object.freeze([
181
+ ...buildGoalTools({
182
+ goalManager: this.manager,
183
+ goalContinuation: this.continuation,
184
+ isAvailable: () => !this.#draining,
185
+ // These tool implementations execute inside the same lane as joint writes.
186
+ flush: () => this.#flushGoalPersistence(),
187
+ now,
188
+ }).map((tool) => ({
189
+ ...tool,
190
+ impl: (input, context) => this.#mutateGoal(() => tool.impl(input, context)),
191
+ })),
192
+ ...(this.eventWaitBridge?.tools ?? []),
193
+ ]);
114
194
  }
115
195
  async prepareRecovery() {
116
196
  if (this.#prepared)
@@ -167,10 +247,128 @@ export class HostGoalCoordinator {
167
247
  this.continuation.recoverActiveGoal(snapshot.record.goal.sessionId);
168
248
  }
169
249
  }
250
+ await this.eventWaitBridge?.start();
170
251
  }
171
252
  readProjection(sessionId) {
253
+ // Canonical/query callers hold the Session lane. Do not wait for the
254
+ // mutation lane here: a committed wait write can have a delayed receipt
255
+ // while Root admission must still publish and start. #project rechecks the
256
+ // adopted binding after I/O and excludes durably admitted/suppressed waits.
257
+ return this.#project(sessionId);
258
+ }
259
+ async #project(sessionId) {
260
+ const authority = this.#authorityBySession.get(sessionId);
261
+ const gate = authority?.record.activeEventWait;
262
+ const wait = gate && this.#stores.eventWaitStore
263
+ ? await this.#stores.eventWaitStore.read({ sessionId, waitId: gate.waitId })
264
+ : undefined;
172
265
  const goal = this.manager.get(sessionId);
173
- return goal ? projectGoalState(goal) : null;
266
+ if (!goal)
267
+ return null;
268
+ const projection = projectGoalState(goal);
269
+ const current = this.#authorityBySession.get(sessionId);
270
+ if (!wait ||
271
+ !gate ||
272
+ !current ||
273
+ current.record.activeEventWait?.waitId !== gate.waitId ||
274
+ !sameGoalControlLease(current.record.controlLease, wait.record.goalControlLease) ||
275
+ !this.manager.matchesControlLease(sessionId, wait.record.goalControlLease) ||
276
+ wait.record.delivery ||
277
+ wait.record.status === 'cancelled' ||
278
+ !['active', 'waiting'].includes(goal.status))
279
+ return projection;
280
+ const r = wait.record, observation = r.observation;
281
+ // Checking is an attempt reservation, not a new source fact. Its retained
282
+ // checkpoint describes the last completed observation until this read settles.
283
+ const observationOutcome = observation?.outcome === 'checking' && observation.lastObservedAt !== null
284
+ ? observation.consecutiveErrors > 0
285
+ ? 'unknown'
286
+ : 'pending'
287
+ : observation?.outcome;
288
+ return {
289
+ ...projection,
290
+ eventWait: {
291
+ waitId: r.waitId,
292
+ controlGeneration: r.goalControlLease.generation,
293
+ waitRevision: wait.authorityRevision,
294
+ resource: {
295
+ providerId: r.resource.providerId,
296
+ resourceType: r.resource.resourceType,
297
+ resourceId: truncateUtf8(r.resource.resourceId, GOAL_EVENT_WAIT_SUMMARY_LIMITS.resourceIdBytes, '…'),
298
+ },
299
+ reason: gate.reason,
300
+ deadlineAt: r.deadlineAt,
301
+ phase: r.status === 'waiting' ? 'observing' : 'awaiting_admission',
302
+ observation: r.status !== 'waiting'
303
+ ? null
304
+ : {
305
+ state: observationOutcome === 'unknown'
306
+ ? 'unknown'
307
+ : observationOutcome === 'pending'
308
+ ? 'pending'
309
+ : 'checking',
310
+ lastObservedAt: observation?.lastObservedAt ?? null,
311
+ reasonCode: observation?.reason
312
+ ? truncateUtf8(observation.reason, GOAL_EVENT_WAIT_SUMMARY_LIMITS.reasonCodeBytes, '…')
313
+ : null,
314
+ },
315
+ outcome: r.status === 'resolved'
316
+ ? r.resolution.outcome
317
+ : r.status === 'expired'
318
+ ? 'expired'
319
+ : null,
320
+ },
321
+ };
322
+ }
323
+ #cancelEventWait(input) {
324
+ return this.#sessionAdmission.run(input.sessionId, () => this.#mutateGoal(async () => {
325
+ if (this.#draining)
326
+ return hostDraining();
327
+ let header;
328
+ try {
329
+ header = await this.#stores.sessionStore.readHeaderSnapshot(input.sessionId);
330
+ }
331
+ catch (error) {
332
+ if (isSessionNotFoundError(error))
333
+ return notFound('Session does not exist');
334
+ throw error;
335
+ }
336
+ if (header.isArchived)
337
+ return sessionArchived('Archived Session cannot cancel a wait');
338
+ const wait = await this.#stores.eventWaitStore?.read({
339
+ sessionId: input.sessionId,
340
+ waitId: input.waitId,
341
+ });
342
+ const current = this.#authorityBySession.get(input.sessionId)?.record;
343
+ let outcome = 'conflict';
344
+ if (wait?.record.goalControlLease.goalId === input.goalId &&
345
+ wait.record.goalControlLease.generation === input.controlGeneration) {
346
+ if (wait.record.delivery?.kind === 'admitted')
347
+ outcome = 'already_admitted';
348
+ else if (wait.record.status === 'cancelled' ||
349
+ wait.record.delivery?.kind === 'suppressed')
350
+ outcome = 'already_cancelled';
351
+ else if (current?.activeEventWait?.waitId === input.waitId &&
352
+ current.goal.id === input.goalId &&
353
+ current.controlLease.generation === input.controlGeneration &&
354
+ current.goal.revision === input.expectedGoalRevision) {
355
+ this.manager.pause(input.sessionId, {
356
+ reason: 'Event wait cancelled. The background task keeps running.',
357
+ });
358
+ this.continuation.invalidateSession(input.sessionId);
359
+ await this.#flushGoalPersistence();
360
+ outcome = 'cancelled';
361
+ }
362
+ }
363
+ return {
364
+ ok: true,
365
+ result: {
366
+ sessionId: input.sessionId,
367
+ outcome,
368
+ goal: await this.#project(input.sessionId),
369
+ },
370
+ };
371
+ }));
174
372
  }
175
373
  beginObservedTurn(sessionId, turnId) {
176
374
  return this.continuation.beginObservedTurn(sessionId, turnId);
@@ -284,6 +482,7 @@ export class HostGoalCoordinator {
284
482
  return;
285
483
  this.#draining = true;
286
484
  this.#recoveryAbort.abort();
485
+ this.eventWaitBridge?.beginDrain();
287
486
  this.continuation.dispose();
288
487
  this.manager.dispose();
289
488
  this.#tokenCache.clear();
@@ -295,6 +494,8 @@ export class HostGoalCoordinator {
295
494
  this.beginDrain();
296
495
  return Promise.all([
297
496
  this.continuation.close(),
497
+ this.eventWaitBridge?.close(),
498
+ this.#jointLane,
298
499
  this.#flushGoalState(),
299
500
  ...this.#recoveryWaits,
300
501
  ]).then(() => undefined);
@@ -312,7 +513,7 @@ export class HostGoalCoordinator {
312
513
  }
313
514
  return {
314
515
  ok: true,
315
- result: { sessionId, goal: this.readProjection(sessionId) },
516
+ result: { sessionId, goal: await this.readProjection(sessionId) },
316
517
  };
317
518
  });
318
519
  }
@@ -330,7 +531,7 @@ export class HostGoalCoordinator {
330
531
  * after it is the first the Goal drives.
331
532
  */
332
533
  #arm(input) {
333
- return this.#sessionAdmission.run(input.sessionId, async () => {
534
+ return this.#sessionAdmission.run(input.sessionId, () => this.#mutateGoal(async () => {
334
535
  // Admission is a queue, and the composition begins to drain without
335
536
  // waiting for it to empty. An arm let through before that can still be
336
537
  // waiting behind another operation when the Goal manager is disposed,
@@ -358,15 +559,14 @@ export class HostGoalCoordinator {
358
559
  if (created.kind === 'unfinished') {
359
560
  return operationConflict(`Session already has an unfinished Goal in status ${created.goal.status}`);
360
561
  }
361
- await this.#flushGoalState(input.sessionId);
362
562
  return {
363
563
  ok: true,
364
564
  result: { sessionId: input.sessionId, goal: projectGoalState(created.goal) },
365
565
  };
366
- });
566
+ }));
367
567
  }
368
568
  #control(input) {
369
- return this.#sessionAdmission.run(input.sessionId, async () => {
569
+ return this.#sessionAdmission.run(input.sessionId, () => this.#mutateGoal(async () => {
370
570
  let header;
371
571
  try {
372
572
  header = await this.#stores.sessionStore.readHeaderSnapshot(input.sessionId);
@@ -405,28 +605,43 @@ export class HostGoalCoordinator {
405
605
  if (!changed) {
406
606
  return operationConflict(`Goal cannot ${input.action} from status ${current.status}`);
407
607
  }
408
- await this.#flushGoalState(input.sessionId);
409
608
  return {
410
609
  ok: true,
411
610
  result: { sessionId: input.sessionId, goal: projectGoalState(changed) },
412
611
  };
413
- });
612
+ }));
414
613
  }
415
614
  #enqueueGoalState(goal, controlLease) {
615
+ if (this.#jointBusy) {
616
+ this.#deferredGoalChanges.push([goal, controlLease]);
617
+ return;
618
+ }
416
619
  const current = this.#authorityBySession.get(goal.sessionId);
417
620
  this.#enqueueAuthorityCommit(goal.sessionId, {
418
621
  schemaVersion: 1,
419
622
  goal,
420
623
  controlLease,
624
+ ...(current?.record.activeEventWait &&
625
+ sameGoalControlLease(current.record.controlLease, controlLease) &&
626
+ ['active', 'waiting'].includes(goal.status)
627
+ ? { activeEventWait: current.record.activeEventWait }
628
+ : {}),
421
629
  currentExecution: current?.record.goal.id === goal.id && goal.revision >= current.record.goal.revision
422
630
  ? current.record.currentExecution
423
631
  : null,
424
- pendingContinuation: current?.record.goal.id === goal.id && goal.revision === current.record.goal.revision
425
- ? current.record.pendingContinuation
632
+ pendingContinuation: current?.record.goal.id === goal.id &&
633
+ sameGoalControlLease(current.record.controlLease, controlLease) &&
634
+ ['active', 'waiting'].includes(goal.status) &&
635
+ current.record.pendingContinuation &&
636
+ (goal.revision === current.record.goal.revision || current.record.activeEventWait)
637
+ ? {
638
+ ...current.record.pendingContinuation,
639
+ checkpoint: { goalId: goal.id, revision: goal.revision },
640
+ }
426
641
  : null,
427
642
  });
428
643
  }
429
- async #recordPendingContinuation(pending) {
644
+ async #recordPendingContinuation(pending, flush = () => this.#flushGoalState()) {
430
645
  const sessionId = this.manager.getSessionIdByGoalId(pending.checkpoint.goalId);
431
646
  const authority = sessionId ? this.#authorityBySession.get(sessionId) : undefined;
432
647
  if (!authority ||
@@ -440,34 +655,36 @@ export class HostGoalCoordinator {
440
655
  currentExecution: null,
441
656
  pendingContinuation: pending,
442
657
  });
443
- await this.#flushGoalState(sessionId);
658
+ await flush();
444
659
  }
445
- async #clearPendingContinuation(sessionId, controlLease) {
446
- const authority = this.#authorityBySession.get(sessionId);
447
- if (!authority ||
448
- !sameGoalControlLease(authority.record.controlLease, controlLease) ||
449
- !authority.record.pendingContinuation) {
450
- return;
451
- }
452
- this.#enqueueAuthorityCommit(sessionId, {
453
- ...authority.record,
454
- pendingContinuation: null,
660
+ #clearPendingContinuation(sessionId, controlLease) {
661
+ return this.#mutateGoal(() => {
662
+ const authority = this.#authorityBySession.get(sessionId);
663
+ if (!authority ||
664
+ !sameGoalControlLease(authority.record.controlLease, controlLease) ||
665
+ !authority.record.pendingContinuation) {
666
+ return;
667
+ }
668
+ this.#enqueueAuthorityCommit(sessionId, {
669
+ ...authority.record,
670
+ pendingContinuation: null,
671
+ });
455
672
  });
456
- await this.#flushGoalState(sessionId);
457
673
  }
458
- async #recordCurrentExecution(current) {
459
- const authority = this.#authorityBySession.get(current.execution.sessionId);
460
- if (!authority ||
461
- authority.record.goal.id !== current.checkpoint.goalId ||
462
- !sameGoalControlLease(authority.record.controlLease, current.controlLease)) {
463
- throw new Error('Goal execution no longer matches its durable authority');
464
- }
465
- this.#enqueueAuthorityCommit(current.execution.sessionId, {
466
- ...authority.record,
467
- currentExecution: current,
468
- pendingContinuation: null,
674
+ #recordCurrentExecution(current) {
675
+ return this.#mutateGoal(() => {
676
+ const authority = this.#authorityBySession.get(current.execution.sessionId);
677
+ if (!authority ||
678
+ authority.record.goal.id !== current.checkpoint.goalId ||
679
+ !sameGoalControlLease(authority.record.controlLease, current.controlLease)) {
680
+ throw new Error('Goal execution no longer matches its durable authority');
681
+ }
682
+ this.#enqueueAuthorityCommit(current.execution.sessionId, {
683
+ ...authority.record,
684
+ currentExecution: current,
685
+ pendingContinuation: null,
686
+ });
469
687
  });
470
- await this.#flushGoalState(current.execution.sessionId);
471
688
  }
472
689
  async #recoverCurrentExecution(current) {
473
690
  const durableAdmission = await this.#stores.agentRunStore.readRootTurnAdmission(current.execution.sessionId, current.execution.turnId);
@@ -504,15 +721,18 @@ export class HostGoalCoordinator {
504
721
  .finally(() => this.#recoveryWaits.delete(tracked));
505
722
  this.#recoveryWaits.add(tracked);
506
723
  }
507
- async #settleCurrentExecution(sessionId, turnId) {
508
- const authority = this.#authorityBySession.get(sessionId);
509
- if (!authority || authority.record.currentExecution?.execution.turnId !== turnId)
510
- return;
511
- this.#enqueueAuthorityCommit(sessionId, {
512
- ...authority.record,
513
- currentExecution: null,
724
+ #settleCurrentExecution(sessionId, turnId) {
725
+ return this.#mutateGoal(() => {
726
+ // A revoked registration can finish while a new wait commit is awaiting acknowledgement.
727
+ // Read the adopted authority inside the lane before clearing this Turn's ownership.
728
+ const authority = this.#authorityBySession.get(sessionId);
729
+ if (!authority || authority.record.currentExecution?.execution.turnId !== turnId)
730
+ return;
731
+ this.#enqueueAuthorityCommit(sessionId, {
732
+ ...authority.record,
733
+ currentExecution: null,
734
+ });
514
735
  });
515
- await this.#flushGoalState(sessionId);
516
736
  }
517
737
  #enqueueAuthorityCommit(sessionId, record) {
518
738
  const current = this.#authorityBySession.get(sessionId);
@@ -595,6 +815,56 @@ export class HostGoalCoordinator {
595
815
  })
596
816
  .finally(() => residency.release());
597
817
  }
818
+ #mutateGoal(operation) {
819
+ const task = this.#jointLane.then(async () => {
820
+ await this.#flushGoalPersistence();
821
+ const result = await operation();
822
+ await this.#flushGoalPersistence();
823
+ return result;
824
+ });
825
+ this.#jointLane = task.catch(() => undefined);
826
+ return task;
827
+ }
828
+ #mutateWait(sessionId, operation) {
829
+ const task = this.#jointLane.then(async () => {
830
+ this.#jointBusy = true;
831
+ try {
832
+ // This task owns the joint lane; only prior ordinary writes can be awaited here.
833
+ await this.#flushGoalPersistence();
834
+ const result = await operation();
835
+ this.#adoptWaitAuthority(result.goal);
836
+ return result;
837
+ }
838
+ finally {
839
+ this.#jointBusy = false;
840
+ for (const [goal, lease] of this.#deferredGoalChanges.splice(0))
841
+ this.#enqueueGoalState(goal, lease);
842
+ }
843
+ });
844
+ this.#jointLane = task.catch(() => undefined);
845
+ return task;
846
+ }
847
+ #adoptWaitAuthority(snapshot) {
848
+ const current = this.#authorityBySession.get(snapshot.record.goal.sessionId);
849
+ // Root admission may commit while an idempotent queue operation is returning.
850
+ if (current && current.authorityRevision > snapshot.authorityRevision)
851
+ return;
852
+ this.#authorityBySession.set(snapshot.record.goal.sessionId, snapshot);
853
+ // A control operation may have changed the live manager while I/O was pending.
854
+ if (!this.#deferredGoalChanges.some(([g]) => g.sessionId === snapshot.record.goal.sessionId)) {
855
+ this.manager.adoptAuthority(snapshot.record.goal, snapshot.record.controlLease);
856
+ this.#syncResidency(snapshot.record.goal, this.#acquireResidency);
857
+ }
858
+ this.#onProjectionChanged(snapshot.record.goal.sessionId);
859
+ }
860
+ async admitted(input) {
861
+ if (input.descriptor.kind !== 'goal' || !input.descriptor.eventWait)
862
+ return;
863
+ const snapshot = await this.#store.read(input.sessionId);
864
+ if (!snapshot)
865
+ throw new Error('Admitted event wait lost Goal authority');
866
+ this.#adoptWaitAuthority(snapshot);
867
+ }
598
868
  async #deleteOrphanedAuthority(snapshot) {
599
869
  const result = await this.#store.commit({
600
870
  sessionId: snapshot.record.goal.sessionId,
@@ -606,6 +876,12 @@ export class HostGoalCoordinator {
606
876
  }
607
877
  }
608
878
  async #flushGoalState(_sessionId) {
879
+ // A control change made during a joint operation is enqueued in its finally block.
880
+ // Acknowledge it only after that operation and the resulting ordinary write finish.
881
+ await this.#jointLane;
882
+ await this.#flushGoalPersistence();
883
+ }
884
+ async #flushGoalPersistence() {
609
885
  await this.#persistenceLane;
610
886
  if (this.#persistenceFailure !== undefined)
611
887
  throw this.#persistenceFailure;
@@ -621,8 +897,22 @@ export class HostGoalCoordinator {
621
897
  this.#residencies.delete(goal.sessionId);
622
898
  }
623
899
  }
624
- function tokenCount(messages) {
625
- return messages.reduce((total, message) => {
900
+ function tokenCount(messages, throughTurnId) {
901
+ // Root admission can release the Session before Goal settlement reads its facts.
902
+ // Keep later user Turns out of this Turn's baseline, including when the wait
903
+ // source spans multiple physical Runs through handoff.
904
+ let end = messages.length;
905
+ if (throughTurnId !== undefined) {
906
+ while (end > 0) {
907
+ const message = messages[end - 1];
908
+ if ('turnId' in message && message.turnId === throughTurnId)
909
+ break;
910
+ end--;
911
+ }
912
+ }
913
+ if (throughTurnId !== undefined && end === 0)
914
+ throw new Error('Completed Goal Turn is missing from token accounting facts');
915
+ return messages.slice(0, end).reduce((total, message) => {
626
916
  if (message.type !== 'token_usage')
627
917
  return total;
628
918
  return total + (message.total ?? message.input + message.output);