@cjhyy/code-shell-core 0.7.1 → 0.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (595) hide show
  1. package/README.md +36 -23
  2. package/dist/agent/agent-definition-registry.d.ts +2 -2
  3. package/dist/agent/agent-definition-registry.js +47 -10
  4. package/dist/agent/agent-definition.d.ts +5 -0
  5. package/dist/agent/agent-definition.js +14 -0
  6. package/dist/automation/index.d.ts +7 -4
  7. package/dist/automation/index.js +4 -2
  8. package/dist/automation/runner.js +3 -1
  9. package/dist/automation/scheduler.d.ts +57 -1
  10. package/dist/automation/scheduler.js +123 -23
  11. package/dist/automation/store.d.ts +6 -2
  12. package/dist/automation/store.js +7 -3
  13. package/dist/capabilities/index.d.ts +109 -0
  14. package/dist/capabilities/index.js +80 -0
  15. package/dist/capability-control/disabled-lists.d.ts +2 -1
  16. package/dist/capability-control/disabled-lists.js +3 -5
  17. package/dist/capability-control/overlay.d.ts +14 -1
  18. package/dist/capability-control/overlay.js +40 -0
  19. package/dist/capability-control/service.js +6 -4
  20. package/dist/cli/agent-server-stdio.d.ts +1 -1
  21. package/dist/cli/agent-server-stdio.js +66 -6
  22. package/dist/cli/agent-server-tcp.js +7 -3
  23. package/dist/context/compaction.d.ts +39 -3
  24. package/dist/context/compaction.js +151 -39
  25. package/dist/context/manager.d.ts +17 -1
  26. package/dist/context/manager.js +36 -1
  27. package/dist/credentials/access.d.ts +3 -1
  28. package/dist/credentials/access.js +24 -3
  29. package/dist/credentials/index.d.ts +1 -1
  30. package/dist/credentials/index.js +1 -1
  31. package/dist/credentials/oauth.d.ts +4 -1
  32. package/dist/credentials/oauth.js +14 -0
  33. package/dist/credentials/store.d.ts +15 -1
  34. package/dist/credentials/store.js +17 -4
  35. package/dist/credentials/types.d.ts +24 -0
  36. package/dist/credentials/use-credential-tool.js +15 -1
  37. package/dist/engine/auxiliary-pipeline.d.ts +40 -0
  38. package/dist/engine/auxiliary-pipeline.js +181 -0
  39. package/dist/{settings → engine}/disk-defaults.d.ts +2 -2
  40. package/dist/{settings → engine}/disk-defaults.js +5 -2
  41. package/dist/engine/dynamic-tool-defs.d.ts +4 -0
  42. package/dist/engine/dynamic-tool-defs.js +4 -0
  43. package/dist/engine/engine.d.ts +254 -195
  44. package/dist/engine/engine.js +1812 -2041
  45. package/dist/engine/file-history-hook.d.ts +11 -0
  46. package/dist/engine/file-history-hook.js +31 -0
  47. package/dist/engine/goal-judge-context.d.ts +9 -0
  48. package/dist/engine/goal-judge-context.js +234 -0
  49. package/dist/engine/injected-context-cache.d.ts +6 -0
  50. package/dist/engine/injected-context-cache.js +13 -0
  51. package/dist/engine/input-attachments.d.ts +1 -1
  52. package/dist/engine/model-facade.d.ts +3 -3
  53. package/dist/engine/model-facade.js +15 -14
  54. package/dist/engine/permission-controller.d.ts +55 -0
  55. package/dist/engine/permission-controller.js +148 -0
  56. package/dist/engine/prompt-cache-diagnostics.d.ts +58 -0
  57. package/dist/engine/prompt-cache-diagnostics.js +178 -0
  58. package/dist/engine/run-accounting.d.ts +41 -0
  59. package/dist/engine/run-accounting.js +126 -0
  60. package/dist/engine/run-context.d.ts +44 -0
  61. package/dist/engine/run-context.js +88 -0
  62. package/dist/engine/run-environment.d.ts +30 -0
  63. package/dist/engine/run-environment.js +77 -0
  64. package/dist/engine/run-finalize.d.ts +58 -0
  65. package/dist/engine/run-finalize.js +245 -0
  66. package/dist/engine/run-goal.d.ts +58 -0
  67. package/dist/engine/run-goal.js +188 -0
  68. package/dist/engine/run-image-input.d.ts +1 -1
  69. package/dist/engine/run-image-input.js +5 -3
  70. package/dist/engine/run-session-open.d.ts +41 -0
  71. package/dist/engine/run-session-open.js +186 -0
  72. package/dist/engine/run-setup.d.ts +42 -0
  73. package/dist/engine/run-setup.js +58 -0
  74. package/dist/engine/run-tooling.d.ts +125 -0
  75. package/dist/engine/run-tooling.js +283 -0
  76. package/dist/engine/run-types.d.ts +139 -0
  77. package/dist/engine/run-types.js +12 -0
  78. package/dist/engine/run-workspace.d.ts +50 -0
  79. package/dist/engine/run-workspace.js +114 -0
  80. package/dist/engine/steer-queue.d.ts +1 -1
  81. package/dist/engine/subagent-spawner.d.ts +32 -0
  82. package/dist/engine/subagent-spawner.js +287 -0
  83. package/dist/engine/tool-summary.d.ts +2 -2
  84. package/dist/engine/tool-summary.js +2 -2
  85. package/dist/engine/turn-loop.d.ts +49 -10
  86. package/dist/engine/turn-loop.js +370 -104
  87. package/dist/engine/types.d.ts +30 -3
  88. package/dist/engine/types.js +1 -1
  89. package/dist/{engine/goal.d.ts → goal/lifecycle.d.ts} +76 -4
  90. package/dist/goal/lifecycle.js +482 -0
  91. package/dist/hooks/events.d.ts +24 -10
  92. package/dist/hooks/goal-stop-hook.d.ts +17 -28
  93. package/dist/hooks/goal-stop-hook.js +110 -465
  94. package/dist/hooks/registry.js +1 -1
  95. package/dist/index.d.ts +48 -108
  96. package/dist/index.extension.d.ts +51 -0
  97. package/dist/index.extension.js +39 -0
  98. package/dist/index.internal.d.ts +86 -0
  99. package/dist/index.internal.js +92 -0
  100. package/dist/index.js +34 -123
  101. package/dist/links/cli.d.ts +48 -0
  102. package/dist/links/cli.js +617 -0
  103. package/dist/links/http.d.ts +40 -0
  104. package/dist/links/http.js +166 -0
  105. package/dist/links/index.d.ts +4 -0
  106. package/dist/links/index.js +3 -0
  107. package/dist/links/link-action-tool.d.ts +5 -0
  108. package/dist/links/link-action-tool.js +241 -0
  109. package/dist/links/providers.d.ts +8 -0
  110. package/dist/links/providers.js +665 -0
  111. package/dist/links/types.d.ts +44 -0
  112. package/dist/llm/client-base.d.ts +7 -0
  113. package/dist/llm/client-base.js +60 -0
  114. package/dist/llm/model-pool.d.ts +1 -1
  115. package/dist/llm/model-pool.js +1 -1
  116. package/dist/llm/providers/anthropic.d.ts +1 -0
  117. package/dist/llm/providers/anthropic.js +9 -0
  118. package/dist/llm/providers/openai.d.ts +1 -0
  119. package/dist/llm/providers/openai.js +17 -0
  120. package/dist/model-catalog/index.js +15 -2
  121. package/dist/model-catalog/resolve.d.ts +13 -1
  122. package/dist/model-catalog/resolve.js +37 -2
  123. package/dist/model-catalog/types.d.ts +74 -48
  124. package/dist/model-catalog/types.js +16 -5
  125. package/dist/model-catalog/upsert.d.ts +3 -1
  126. package/dist/model-catalog/upsert.js +9 -0
  127. package/dist/onboarding.d.ts +0 -1
  128. package/dist/onboarding.js +1 -22
  129. package/dist/panel-apps/bindings.d.ts +24 -0
  130. package/dist/panel-apps/bindings.js +69 -0
  131. package/dist/panel-apps/github-archive.d.ts +21 -0
  132. package/dist/panel-apps/github-archive.js +98 -0
  133. package/dist/panel-apps/index.d.ts +4 -0
  134. package/dist/panel-apps/index.js +4 -0
  135. package/dist/panel-apps/installer.d.ts +68 -0
  136. package/dist/panel-apps/installer.js +622 -0
  137. package/dist/panel-apps/manifest.d.ts +364 -0
  138. package/dist/panel-apps/manifest.js +189 -0
  139. package/dist/panel-apps/paths.d.ts +13 -0
  140. package/dist/panel-apps/paths.js +38 -0
  141. package/dist/panel-apps/registry.d.ts +51 -0
  142. package/dist/panel-apps/registry.js +99 -0
  143. package/dist/panel-apps/runtime.d.ts +80 -0
  144. package/dist/panel-apps/runtime.js +154 -0
  145. package/dist/plugins/gitOps.js +8 -1
  146. package/dist/plugins/installedPlugins.d.ts +2 -1
  147. package/dist/plugins/installedPlugins.js +146 -8
  148. package/dist/plugins/installer/codex/convertAgents.d.ts +4 -1
  149. package/dist/plugins/installer/codex/convertAgents.js +62 -8
  150. package/dist/plugins/installer/codex/convertCommands.d.ts +5 -5
  151. package/dist/plugins/installer/codex/convertCommands.js +35 -12
  152. package/dist/plugins/installer/codex/convertHooks.d.ts +10 -0
  153. package/dist/plugins/installer/codex/convertHooks.js +82 -0
  154. package/dist/plugins/installer/codex/convertSkills.js +41 -6
  155. package/dist/plugins/installer/install.d.ts +7 -1
  156. package/dist/plugins/installer/install.js +21 -87
  157. package/dist/plugins/installer/installFromArchive.d.ts +26 -4
  158. package/dist/plugins/installer/installFromArchive.js +44 -27
  159. package/dist/plugins/installer/installFromNpm.d.ts +31 -0
  160. package/dist/plugins/installer/installFromNpm.js +273 -0
  161. package/dist/plugins/installer/loadPluginAgents.js +17 -4
  162. package/dist/plugins/installer/loadPluginMcp.d.ts +14 -0
  163. package/dist/plugins/installer/loadPluginMcp.js +245 -35
  164. package/dist/plugins/installer/normalizeManifest.d.ts +13 -0
  165. package/dist/plugins/installer/normalizeManifest.js +306 -0
  166. package/dist/plugins/installer/npmTar.d.ts +13 -0
  167. package/dist/plugins/installer/npmTar.js +321 -0
  168. package/dist/plugins/installer/parseSource.d.ts +11 -0
  169. package/dist/plugins/installer/parseSource.js +48 -0
  170. package/dist/plugins/installer/preview.d.ts +101 -0
  171. package/dist/plugins/installer/preview.js +336 -0
  172. package/dist/plugins/installer/projectPluginSource.d.ts +14 -0
  173. package/dist/plugins/installer/projectPluginSource.js +140 -0
  174. package/dist/plugins/installer/types.d.ts +775 -0
  175. package/dist/plugins/installer/types.js +100 -0
  176. package/dist/plugins/installer/unzip.d.ts +11 -0
  177. package/dist/plugins/installer/unzip.js +123 -9
  178. package/dist/plugins/installer/update.d.ts +2 -2
  179. package/dist/plugins/installer/update.js +43 -4
  180. package/dist/plugins/loadPluginHooks.d.ts +19 -2
  181. package/dist/plugins/loadPluginHooks.js +97 -86
  182. package/dist/plugins/pluginAutomationTemplates.d.ts +18 -0
  183. package/dist/plugins/pluginAutomationTemplates.js +50 -0
  184. package/dist/plugins/pluginCatalog.d.ts +45 -0
  185. package/dist/plugins/pluginCatalog.js +87 -0
  186. package/dist/plugins/pluginCommandHook.d.ts +6 -0
  187. package/dist/plugins/pluginCommandHook.js +25 -8
  188. package/dist/plugins/pluginCommandsLoader.d.ts +19 -0
  189. package/dist/plugins/pluginCommandsLoader.js +181 -7
  190. package/dist/plugins/pluginContent.d.ts +10 -1
  191. package/dist/plugins/pluginContent.js +87 -30
  192. package/dist/plugins/pluginHookApproval.d.ts +30 -0
  193. package/dist/plugins/pluginHookApproval.js +160 -0
  194. package/dist/plugins/pluginHookIntegrity.d.ts +69 -0
  195. package/dist/plugins/pluginHookIntegrity.js +361 -0
  196. package/dist/plugins/pluginInstaller.js +71 -12
  197. package/dist/plugins/pluginMcpApproval.d.ts +19 -0
  198. package/dist/plugins/pluginMcpApproval.js +110 -0
  199. package/dist/plugins/pluginMcpIntegrity.d.ts +21 -0
  200. package/dist/plugins/pluginMcpIntegrity.js +74 -0
  201. package/dist/plugins/types.d.ts +30 -0
  202. package/dist/plugins/varRewrite.d.ts +9 -5
  203. package/dist/plugins/varRewrite.js +26 -17
  204. package/dist/preset/index.d.ts +11 -8
  205. package/dist/preset/index.js +62 -203
  206. package/dist/product/define.js +9 -3
  207. package/dist/product/types.d.ts +0 -2
  208. package/dist/profile/activation.d.ts +36 -0
  209. package/dist/profile/activation.js +57 -0
  210. package/dist/profile/catalog-store.d.ts +77 -0
  211. package/dist/profile/catalog-store.js +346 -0
  212. package/dist/profile/catalog.d.ts +54 -0
  213. package/dist/profile/catalog.js +152 -0
  214. package/dist/profile/index.d.ts +7 -0
  215. package/dist/profile/index.js +7 -0
  216. package/dist/profile/requirements.d.ts +76 -0
  217. package/dist/profile/requirements.js +117 -0
  218. package/dist/profile/resolve.d.ts +11 -0
  219. package/dist/profile/resolve.js +41 -0
  220. package/dist/profile/store.d.ts +17 -0
  221. package/dist/profile/store.js +167 -0
  222. package/dist/profile/types.d.ts +321 -0
  223. package/dist/profile/types.js +104 -0
  224. package/dist/prompt/composer.d.ts +46 -7
  225. package/dist/prompt/composer.js +78 -48
  226. package/dist/prompt/instruction-scanner.d.ts +6 -5
  227. package/dist/prompt/instruction-scanner.js +8 -23
  228. package/dist/prompt/section-loader.d.ts +2 -2
  229. package/dist/prompt/section-loader.js +7 -4
  230. package/dist/prompt/sections/browser.md +10 -9
  231. package/dist/prompt/sections/harness-base.md +9 -0
  232. package/dist/protocol/chat-session-manager.d.ts +64 -1
  233. package/dist/protocol/chat-session-manager.js +104 -0
  234. package/dist/protocol/chat-session.d.ts +61 -10
  235. package/dist/protocol/chat-session.js +135 -25
  236. package/dist/protocol/client.d.ts +33 -3
  237. package/dist/protocol/client.js +96 -5
  238. package/dist/protocol/index.d.ts +2 -2
  239. package/dist/protocol/index.js +2 -2
  240. package/dist/protocol/mobile-remote-types.d.ts +406 -0
  241. package/dist/protocol/mobile-remote-types.js +1 -0
  242. package/dist/protocol/server.d.ts +127 -3
  243. package/dist/protocol/server.js +1309 -182
  244. package/dist/protocol/types.d.ts +152 -34
  245. package/dist/protocol/types.js +16 -0
  246. package/dist/run/ArtifactTracker.d.ts +6 -1
  247. package/dist/run/ArtifactTracker.js +29 -21
  248. package/dist/run/EngineRunner.d.ts +2 -0
  249. package/dist/run/EngineRunner.js +1 -0
  250. package/dist/run/RunManager.d.ts +26 -0
  251. package/dist/run/RunManager.js +82 -11
  252. package/dist/run/factory.d.ts +3 -0
  253. package/dist/run/factory.js +2 -0
  254. package/dist/runtime/safe-spawn.d.ts +6 -0
  255. package/dist/runtime/safe-spawn.js +8 -13
  256. package/dist/services/auto-dream.d.ts +13 -1
  257. package/dist/services/auto-dream.js +56 -8
  258. package/dist/services/dream-consolidation.js +10 -1
  259. package/dist/services/memory-orchestrator.js +6 -2
  260. package/dist/services/oauth.js +4 -1
  261. package/dist/services/session-memory.d.ts +4 -4
  262. package/dist/services/session-memory.js +13 -10
  263. package/dist/session/memory.d.ts +26 -7
  264. package/dist/session/memory.js +79 -21
  265. package/dist/session/session-manager.d.ts +182 -28
  266. package/dist/session/session-manager.js +910 -130
  267. package/dist/session/session-message.d.ts +22 -0
  268. package/dist/session/session-message.js +1 -0
  269. package/dist/session/transcript.d.ts +46 -5
  270. package/dist/session/transcript.js +159 -3
  271. package/dist/settings/feature-flags.d.ts +40 -0
  272. package/dist/settings/feature-flags.js +40 -0
  273. package/dist/settings/manager.d.ts +39 -1
  274. package/dist/settings/manager.js +102 -41
  275. package/dist/settings/migrate-config.js +4 -2
  276. package/dist/settings/schema.d.ts +511 -177
  277. package/dist/settings/schema.js +98 -25
  278. package/dist/skills/scanner.d.ts +6 -2
  279. package/dist/skills/scanner.js +160 -9
  280. package/dist/sources/adapter.d.ts +14 -0
  281. package/dist/sources/adapter.js +7 -0
  282. package/dist/sources/adapters/local-files.d.ts +10 -0
  283. package/dist/sources/adapters/local-files.js +127 -0
  284. package/dist/sources/adapters/mcp-resource.d.ts +17 -0
  285. package/dist/sources/adapters/mcp-resource.js +41 -0
  286. package/dist/sources/adapters/mock.d.ts +3 -0
  287. package/dist/sources/adapters/mock.js +29 -0
  288. package/dist/sources/binding.d.ts +6 -0
  289. package/dist/sources/binding.js +24 -0
  290. package/dist/sources/catalog.d.ts +6 -0
  291. package/dist/sources/catalog.js +64 -0
  292. package/dist/sources/context-summary.d.ts +8 -0
  293. package/dist/sources/context-summary.js +13 -0
  294. package/dist/sources/credential-status.d.ts +2 -0
  295. package/dist/sources/credential-status.js +13 -0
  296. package/dist/sources/index.d.ts +10 -0
  297. package/dist/sources/index.js +10 -0
  298. package/dist/sources/resolve.d.ts +28 -0
  299. package/dist/sources/resolve.js +47 -0
  300. package/dist/sources/truncate-utf8.d.ts +7 -0
  301. package/dist/sources/truncate-utf8.js +18 -0
  302. package/dist/sources/types.d.ts +71 -0
  303. package/dist/sources/types.js +26 -0
  304. package/dist/testing/fetch-stub.d.ts +32 -0
  305. package/dist/testing/fetch-stub.js +24 -0
  306. package/dist/themes/image.d.ts +18 -0
  307. package/dist/themes/image.js +117 -0
  308. package/dist/themes/index.d.ts +4 -0
  309. package/dist/themes/index.js +4 -0
  310. package/dist/themes/installer.d.ts +50 -0
  311. package/dist/themes/installer.js +290 -0
  312. package/dist/themes/manifest.d.ts +102 -0
  313. package/dist/themes/manifest.js +86 -0
  314. package/dist/themes/paths.d.ts +14 -0
  315. package/dist/themes/paths.js +33 -0
  316. package/dist/tool-system/browser-bridge.d.ts +53 -11
  317. package/dist/tool-system/browser-bridge.js +8 -9
  318. package/dist/tool-system/builtin/agent-heartbeat.d.ts +2 -2
  319. package/dist/tool-system/builtin/agent-heartbeat.js +20 -6
  320. package/dist/tool-system/builtin/agent-notifications.d.ts +127 -72
  321. package/dist/tool-system/builtin/agent-notifications.js +291 -101
  322. package/dist/tool-system/builtin/agent-output-file.js +1 -3
  323. package/dist/tool-system/builtin/agent-progress.d.ts +6 -0
  324. package/dist/tool-system/builtin/agent-progress.js +83 -0
  325. package/dist/tool-system/builtin/agent-registry.d.ts +38 -2
  326. package/dist/tool-system/builtin/agent-registry.js +178 -9
  327. package/dist/tool-system/builtin/agent-transcript-translator.js +1 -1
  328. package/dist/tool-system/builtin/agent.d.ts +2 -0
  329. package/dist/tool-system/builtin/agent.js +274 -33
  330. package/dist/tool-system/builtin/background-jobs.d.ts +55 -2
  331. package/dist/tool-system/builtin/background-jobs.js +96 -4
  332. package/dist/tool-system/builtin/background-work.d.ts +6 -0
  333. package/dist/tool-system/builtin/background-work.js +8 -1
  334. package/dist/tool-system/builtin/bash.js +5 -2
  335. package/dist/tool-system/builtin/browser-tools.d.ts +6 -3
  336. package/dist/tool-system/builtin/browser-tools.js +89 -23
  337. package/dist/tool-system/builtin/edit-model-catalog.d.ts +6 -0
  338. package/dist/tool-system/builtin/edit-model-catalog.js +172 -12
  339. package/dist/tool-system/builtin/edit.js +2 -6
  340. package/dist/tool-system/builtin/file-cache.d.ts +2 -0
  341. package/dist/tool-system/builtin/file-cache.js +4 -0
  342. package/dist/tool-system/builtin/generate-image.js +17 -10
  343. package/dist/tool-system/builtin/generate-video.js +26 -7
  344. package/dist/tool-system/builtin/grep.js +2 -2
  345. package/dist/tool-system/builtin/image-uploader.js +7 -1
  346. package/dist/tool-system/builtin/index.d.ts +32 -7
  347. package/dist/tool-system/builtin/index.js +237 -167
  348. package/dist/tool-system/builtin/memory.d.ts +6 -0
  349. package/dist/tool-system/builtin/memory.js +36 -15
  350. package/dist/tool-system/builtin/panel.d.ts +9 -0
  351. package/dist/tool-system/builtin/panel.js +314 -0
  352. package/dist/tool-system/builtin/read.js +5 -1
  353. package/dist/tool-system/builtin/send-message-to-session.d.ts +6 -0
  354. package/dist/tool-system/builtin/send-message-to-session.js +67 -0
  355. package/dist/tool-system/builtin/sources.d.ts +8 -0
  356. package/dist/tool-system/builtin/sources.js +137 -0
  357. package/dist/tool-system/builtin/tool-search.js +7 -2
  358. package/dist/tool-system/builtin/update-automation-memory.js +2 -0
  359. package/dist/tool-system/builtin/web-fetch.js +17 -13
  360. package/dist/tool-system/builtin/web-search.js +1 -3
  361. package/dist/tool-system/capability-module.d.ts +93 -0
  362. package/dist/tool-system/capability-module.js +53 -0
  363. package/dist/tool-system/context.d.ts +77 -19
  364. package/dist/tool-system/executor.js +75 -50
  365. package/dist/tool-system/external-tool-exposure.d.ts +60 -0
  366. package/dist/tool-system/external-tool-exposure.js +304 -0
  367. package/dist/tool-system/mcp-manager.d.ts +11 -1
  368. package/dist/tool-system/mcp-manager.js +19 -4
  369. package/dist/tool-system/mcp-tool-policy.d.ts +9 -0
  370. package/dist/tool-system/mcp-tool-policy.js +34 -0
  371. package/dist/tool-system/panel-bridge.d.ts +53 -0
  372. package/dist/tool-system/panel-bridge.js +1 -0
  373. package/dist/tool-system/path-policy.js +99 -2
  374. package/dist/tool-system/permission.d.ts +26 -0
  375. package/dist/tool-system/permission.js +46 -0
  376. package/dist/tool-system/registry.d.ts +12 -1
  377. package/dist/tool-system/registry.js +31 -7
  378. package/dist/tool-system/sandbox/index.js +3 -8
  379. package/dist/tool-system/sandbox/seatbelt.js +2 -6
  380. package/dist/tool-system/session-tool-host.d.ts +139 -0
  381. package/dist/tool-system/session-tool-host.js +167 -0
  382. package/dist/tool-system/testing/tool-registry-harness.d.ts +31 -0
  383. package/dist/tool-system/testing/tool-registry-harness.js +51 -0
  384. package/dist/tool-system/validation.d.ts +14 -7
  385. package/dist/tool-system/validation.js +701 -7
  386. package/dist/types.d.ts +186 -20
  387. package/dist/{cc-orchestrator → utils}/cwd-normalize.d.ts +1 -0
  388. package/dist/{cc-orchestrator → utils}/cwd-normalize.js +1 -0
  389. package/dist/utils/file-mutex.d.ts +65 -0
  390. package/dist/utils/file-mutex.js +145 -0
  391. package/dist/utils/json.d.ts +2 -4
  392. package/dist/utils/json.js +2 -4
  393. package/dist/utils/lockfile.d.ts +30 -1
  394. package/dist/utils/lockfile.js +2 -2
  395. package/dist/utils/secret-scrubber.d.ts +8 -0
  396. package/dist/utils/secret-scrubber.js +227 -0
  397. package/dist/utils/toolDisplay.js +1 -1
  398. package/package.json +20 -5
  399. package/THIRD_PARTY_NOTICES.md +0 -206
  400. package/dist/arena/arena.d.ts +0 -45
  401. package/dist/arena/arena.js +0 -362
  402. package/dist/arena/context/context-tools.d.ts +0 -16
  403. package/dist/arena/context/context-tools.js +0 -272
  404. package/dist/arena/context/within-root.d.ts +0 -7
  405. package/dist/arena/context/within-root.js +0 -15
  406. package/dist/arena/detect-mode.d.ts +0 -20
  407. package/dist/arena/detect-mode.js +0 -78
  408. package/dist/arena/digest-builder.d.ts +0 -25
  409. package/dist/arena/digest-builder.js +0 -120
  410. package/dist/arena/index.d.ts +0 -29
  411. package/dist/arena/index.js +0 -29
  412. package/dist/arena/iterate/convergence.d.ts +0 -25
  413. package/dist/arena/iterate/convergence.js +0 -103
  414. package/dist/arena/iterate/formats/index.d.ts +0 -22
  415. package/dist/arena/iterate/formats/index.js +0 -283
  416. package/dist/arena/iterate/index.d.ts +0 -11
  417. package/dist/arena/iterate/index.js +0 -9
  418. package/dist/arena/iterate/iterative-arena.d.ts +0 -23
  419. package/dist/arena/iterate/iterative-arena.js +0 -237
  420. package/dist/arena/iterate/parse.d.ts +0 -42
  421. package/dist/arena/iterate/parse.js +0 -123
  422. package/dist/arena/iterate/phases/argue.d.ts +0 -22
  423. package/dist/arena/iterate/phases/argue.js +0 -165
  424. package/dist/arena/iterate/phases/revise.d.ts +0 -16
  425. package/dist/arena/iterate/phases/revise.js +0 -62
  426. package/dist/arena/iterate/phases/tournament.d.ts +0 -34
  427. package/dist/arena/iterate/phases/tournament.js +0 -113
  428. package/dist/arena/iterate/tools/web-tools.d.ts +0 -13
  429. package/dist/arena/iterate/tools/web-tools.js +0 -54
  430. package/dist/arena/iterate/types.d.ts +0 -152
  431. package/dist/arena/iterate/types.js +0 -8
  432. package/dist/arena/ledger.d.ts +0 -47
  433. package/dist/arena/ledger.js +0 -159
  434. package/dist/arena/lenses/architecture.d.ts +0 -5
  435. package/dist/arena/lenses/architecture.js +0 -22
  436. package/dist/arena/lenses/engineering.d.ts +0 -5
  437. package/dist/arena/lenses/engineering.js +0 -22
  438. package/dist/arena/lenses/general.d.ts +0 -5
  439. package/dist/arena/lenses/general.js +0 -20
  440. package/dist/arena/lenses/index.d.ts +0 -16
  441. package/dist/arena/lenses/index.js +0 -47
  442. package/dist/arena/lenses/product.d.ts +0 -5
  443. package/dist/arena/lenses/product.js +0 -22
  444. package/dist/arena/model-presets.d.ts +0 -23
  445. package/dist/arena/model-presets.js +0 -44
  446. package/dist/arena/phases/adjudication.d.ts +0 -25
  447. package/dist/arena/phases/adjudication.js +0 -142
  448. package/dist/arena/phases/build-consensus.d.ts +0 -30
  449. package/dist/arena/phases/build-consensus.js +0 -85
  450. package/dist/arena/phases/claim-registry.d.ts +0 -26
  451. package/dist/arena/phases/claim-registry.js +0 -60
  452. package/dist/arena/phases/cross-review.d.ts +0 -47
  453. package/dist/arena/phases/cross-review.js +0 -224
  454. package/dist/arena/phases/debate-rounds.d.ts +0 -28
  455. package/dist/arena/phases/debate-rounds.js +0 -161
  456. package/dist/arena/phases/participant-research.d.ts +0 -39
  457. package/dist/arena/phases/participant-research.js +0 -316
  458. package/dist/arena/phases/planning-detail-expansion.d.ts +0 -39
  459. package/dist/arena/phases/planning-detail-expansion.js +0 -120
  460. package/dist/arena/planner.d.ts +0 -28
  461. package/dist/arena/planner.js +0 -312
  462. package/dist/arena/providers/docs.d.ts +0 -7
  463. package/dist/arena/providers/docs.js +0 -111
  464. package/dist/arena/providers/git.d.ts +0 -8
  465. package/dist/arena/providers/git.js +0 -174
  466. package/dist/arena/providers/index.d.ts +0 -32
  467. package/dist/arena/providers/index.js +0 -132
  468. package/dist/arena/providers/none.d.ts +0 -7
  469. package/dist/arena/providers/none.js +0 -11
  470. package/dist/arena/providers/repo.d.ts +0 -7
  471. package/dist/arena/providers/repo.js +0 -258
  472. package/dist/arena/render/session.d.ts +0 -17
  473. package/dist/arena/render/session.js +0 -190
  474. package/dist/arena/render/terminal.d.ts +0 -34
  475. package/dist/arena/render/terminal.js +0 -286
  476. package/dist/arena/strategies/discussion.d.ts +0 -25
  477. package/dist/arena/strategies/discussion.js +0 -143
  478. package/dist/arena/strategies/index.d.ts +0 -15
  479. package/dist/arena/strategies/index.js +0 -28
  480. package/dist/arena/strategies/language-wrapper.d.ts +0 -17
  481. package/dist/arena/strategies/language-wrapper.js +0 -102
  482. package/dist/arena/strategies/lens-wrapper.d.ts +0 -16
  483. package/dist/arena/strategies/lens-wrapper.js +0 -236
  484. package/dist/arena/strategies/planning.d.ts +0 -30
  485. package/dist/arena/strategies/planning.js +0 -225
  486. package/dist/arena/strategies/review.d.ts +0 -26
  487. package/dist/arena/strategies/review.js +0 -168
  488. package/dist/arena/strategies/utils.d.ts +0 -36
  489. package/dist/arena/strategies/utils.js +0 -603
  490. package/dist/arena/tools/selector.d.ts +0 -17
  491. package/dist/arena/tools/selector.js +0 -61
  492. package/dist/arena/transitions.d.ts +0 -53
  493. package/dist/arena/transitions.js +0 -97
  494. package/dist/arena/types.d.ts +0 -518
  495. package/dist/arena/types.js +0 -27
  496. package/dist/cc-orchestrator/agent-adapter.d.ts +0 -54
  497. package/dist/cc-orchestrator/agent-adapter.js +0 -143
  498. package/dist/cc-orchestrator/cc-capability.d.ts +0 -19
  499. package/dist/cc-orchestrator/cc-capability.js +0 -53
  500. package/dist/cc-orchestrator/codex-session-discovery.d.ts +0 -24
  501. package/dist/cc-orchestrator/codex-session-discovery.js +0 -191
  502. package/dist/cc-orchestrator/codex-session-history.d.ts +0 -38
  503. package/dist/cc-orchestrator/codex-session-history.js +0 -247
  504. package/dist/cc-orchestrator/external-agent-bindings.d.ts +0 -27
  505. package/dist/cc-orchestrator/external-agent-bindings.js +0 -150
  506. package/dist/cc-orchestrator/external-agent-changes.d.ts +0 -19
  507. package/dist/cc-orchestrator/external-agent-changes.js +0 -231
  508. package/dist/cc-orchestrator/external-agent-driver.d.ts +0 -19
  509. package/dist/cc-orchestrator/external-agent-driver.js +0 -284
  510. package/dist/cc-orchestrator/external-agent-session-store.d.ts +0 -23
  511. package/dist/cc-orchestrator/external-agent-session-store.js +0 -146
  512. package/dist/cc-orchestrator/index.d.ts +0 -8
  513. package/dist/cc-orchestrator/index.js +0 -8
  514. package/dist/cc-orchestrator/relevance-judge.d.ts +0 -15
  515. package/dist/cc-orchestrator/relevance-judge.js +0 -29
  516. package/dist/cc-orchestrator/session-discovery.d.ts +0 -46
  517. package/dist/cc-orchestrator/session-discovery.js +0 -125
  518. package/dist/cc-orchestrator/session-history.d.ts +0 -54
  519. package/dist/cc-orchestrator/session-history.js +0 -150
  520. package/dist/cron/cron-runtime.d.ts +0 -2
  521. package/dist/cron/cron-runtime.js +0 -2
  522. package/dist/cron/cron-store.d.ts +0 -2
  523. package/dist/cron/cron-store.js +0 -2
  524. package/dist/cron/scheduler.d.ts +0 -7
  525. package/dist/cron/scheduler.js +0 -7
  526. package/dist/engine/goal.js +0 -206
  527. package/dist/external-agents/config.d.ts +0 -2
  528. package/dist/external-agents/config.js +0 -15
  529. package/dist/external-agents/types.d.ts +0 -31
  530. package/dist/git/parse-log.d.ts +0 -13
  531. package/dist/git/parse-log.js +0 -21
  532. package/dist/git/utils.d.ts +0 -49
  533. package/dist/git/utils.js +0 -161
  534. package/dist/git/worktree/crud.d.ts +0 -72
  535. package/dist/git/worktree/crud.js +0 -235
  536. package/dist/git/worktree/diff.d.ts +0 -14
  537. package/dist/git/worktree/diff.js +0 -82
  538. package/dist/git/worktree/git-exec.d.ts +0 -7
  539. package/dist/git/worktree/git-exec.js +0 -87
  540. package/dist/git/worktree/index.d.ts +0 -5
  541. package/dist/git/worktree/index.js +0 -5
  542. package/dist/git/worktree/query.d.ts +0 -43
  543. package/dist/git/worktree/query.js +0 -128
  544. package/dist/git/worktree/slug.d.ts +0 -11
  545. package/dist/git/worktree/slug.js +0 -58
  546. package/dist/git/worktree.d.ts +0 -1
  547. package/dist/git/worktree.js +0 -5
  548. package/dist/lsp/client.d.ts +0 -41
  549. package/dist/lsp/client.js +0 -186
  550. package/dist/lsp/manager.d.ts +0 -40
  551. package/dist/lsp/manager.js +0 -155
  552. package/dist/lsp/root-path.d.ts +0 -9
  553. package/dist/lsp/root-path.js +0 -12
  554. package/dist/lsp/servers.d.ts +0 -16
  555. package/dist/lsp/servers.js +0 -60
  556. package/dist/prompt/sections/coding.md +0 -35
  557. package/dist/quota/credentials.d.ts +0 -3
  558. package/dist/quota/credentials.js +0 -80
  559. package/dist/quota/index.d.ts +0 -36
  560. package/dist/quota/index.js +0 -155
  561. package/dist/quota/types.d.ts +0 -48
  562. package/dist/quota/types.js +0 -13
  563. package/dist/review/review-prompt.d.ts +0 -28
  564. package/dist/review/review-prompt.js +0 -81
  565. package/dist/state.d.ts +0 -160
  566. package/dist/state.js +0 -267
  567. package/dist/tool-system/builtin/apply-patch/applier.d.ts +0 -26
  568. package/dist/tool-system/builtin/apply-patch/applier.js +0 -308
  569. package/dist/tool-system/builtin/apply-patch/backup-targets.d.ts +0 -10
  570. package/dist/tool-system/builtin/apply-patch/backup-targets.js +0 -30
  571. package/dist/tool-system/builtin/apply-patch/index.d.ts +0 -20
  572. package/dist/tool-system/builtin/apply-patch/index.js +0 -105
  573. package/dist/tool-system/builtin/apply-patch/parser.d.ts +0 -17
  574. package/dist/tool-system/builtin/apply-patch/parser.js +0 -212
  575. package/dist/tool-system/builtin/apply-patch/seek-sequence.d.ts +0 -18
  576. package/dist/tool-system/builtin/apply-patch/seek-sequence.js +0 -123
  577. package/dist/tool-system/builtin/apply-patch/types.d.ts +0 -49
  578. package/dist/tool-system/builtin/apply-patch/types.js +0 -13
  579. package/dist/tool-system/builtin/arena.d.ts +0 -31
  580. package/dist/tool-system/builtin/arena.js +0 -416
  581. package/dist/tool-system/builtin/brief.d.ts +0 -6
  582. package/dist/tool-system/builtin/brief.js +0 -41
  583. package/dist/tool-system/builtin/check-quota.d.ts +0 -15
  584. package/dist/tool-system/builtin/check-quota.js +0 -34
  585. package/dist/tool-system/builtin/drive-claude-code.d.ts +0 -51
  586. package/dist/tool-system/builtin/drive-claude-code.js +0 -698
  587. package/dist/tool-system/builtin/lsp.d.ts +0 -7
  588. package/dist/tool-system/builtin/lsp.js +0 -144
  589. package/dist/tool-system/builtin/notebook-edit.d.ts +0 -7
  590. package/dist/tool-system/builtin/notebook-edit.js +0 -127
  591. package/dist/tool-system/builtin/worktree.d.ts +0 -11
  592. package/dist/tool-system/builtin/worktree.js +0 -363
  593. /package/dist/{external-agents → links}/types.js +0 -0
  594. /package/dist/{engine/session-usage.d.ts → session/usage.d.ts} +0 -0
  595. /package/dist/{engine/session-usage.js → session/usage.js} +0 -0
@@ -0,0 +1,482 @@
1
+ /**
2
+ * Goal mode P0 — run-scoped budget guardrails.
3
+ *
4
+ * `GoalConfig` is the normalized internal shape. Public entry points
5
+ * (protocol/server, engine run) accept `string | GoalConfig` for
6
+ * back-compat and call `normalizeGoal()` once at the boundary; everything
7
+ * inward uses `GoalConfig`.
8
+ *
9
+ * The budget tracker is RUN-scoped (not per-turn like token-budget.ts):
10
+ * it accumulates total tokens across turns and stamps a wall-clock start,
11
+ * so an unattended goal run can't burn tokens or wall time without bound.
12
+ */
13
+ import { createHash, randomUUID } from "node:crypto";
14
+ function goalLifecycleConfig(goal) {
15
+ const { goalId: _goalId, revision: _revision, paused: _paused, ...config } = goal;
16
+ return config;
17
+ }
18
+ /** Build one canonical lifecycle record from the compatibility GoalConfig view. */
19
+ export function createGoalLifecycle(goal, phase = goal.paused === true
20
+ ? "paused"
21
+ : "active", nowMs = Date.now()) {
22
+ const base = {
23
+ version: 1,
24
+ goalId: goal.goalId ?? randomUUID(),
25
+ revision: Math.max(1, Math.floor(goal.revision ?? 1)),
26
+ config: goalLifecycleConfig(goal),
27
+ updatedAtMs: nowMs,
28
+ };
29
+ return { ...base, phase };
30
+ }
31
+ /** Compatibility view consumed by Engine/protocol while persistence uses the union. */
32
+ export function goalConfigFromLifecycle(lifecycle) {
33
+ return {
34
+ ...lifecycle.config,
35
+ goalId: lifecycle.goalId,
36
+ revision: lifecycle.revision,
37
+ ...(lifecycle.phase === "paused" ? { paused: true } : {}),
38
+ };
39
+ }
40
+ export function isGoalLifecycleArmable(lifecycle) {
41
+ return lifecycle.phase === "active" || lifecycle.phase === "waiting";
42
+ }
43
+ export function isGoalLifecycleCurrent(lifecycle) {
44
+ return lifecycle.phase !== "terminal";
45
+ }
46
+ /** Strict persisted decoder. Unknown versions and partial unions fail closed. */
47
+ export function decodeGoalLifecycle(value) {
48
+ if (!value || typeof value !== "object" || Array.isArray(value))
49
+ return undefined;
50
+ const candidate = value;
51
+ if (candidate.version !== 1)
52
+ return undefined;
53
+ if (typeof candidate.goalId !== "string" || candidate.goalId.length === 0)
54
+ return undefined;
55
+ if (typeof candidate.revision !== "number" ||
56
+ !Number.isInteger(candidate.revision) ||
57
+ candidate.revision < 1) {
58
+ return undefined;
59
+ }
60
+ if (typeof candidate.updatedAtMs !== "number" ||
61
+ !Number.isFinite(candidate.updatedAtMs) ||
62
+ candidate.updatedAtMs < 0) {
63
+ return undefined;
64
+ }
65
+ if (!candidate.config ||
66
+ typeof candidate.config !== "object" ||
67
+ Array.isArray(candidate.config)) {
68
+ return undefined;
69
+ }
70
+ const config = candidate.config;
71
+ // Identity and mutable lifecycle state have one canonical owner. Reject
72
+ // persisted payloads that duplicate them inside config instead of silently
73
+ // accepting two potentially conflicting truths.
74
+ for (const lifecycleKey of ["goalId", "revision", "paused"]) {
75
+ if (Object.prototype.hasOwnProperty.call(config, lifecycleKey))
76
+ return undefined;
77
+ }
78
+ if (typeof config.objective !== "string" || config.objective.trim().length === 0)
79
+ return undefined;
80
+ for (const key of ["tokenBudget", "timeBudgetMs", "maxTurns", "maxStopBlocks"]) {
81
+ const field = config[key];
82
+ if (field !== undefined &&
83
+ (typeof field !== "number" || !Number.isFinite(field) || field <= 0)) {
84
+ return undefined;
85
+ }
86
+ }
87
+ for (const key of ["maxTurns", "maxStopBlocks"]) {
88
+ const field = config[key];
89
+ if (field !== undefined && !Number.isInteger(field))
90
+ return undefined;
91
+ }
92
+ if (config.setAtMs !== undefined &&
93
+ (typeof config.setAtMs !== "number" || !Number.isFinite(config.setAtMs) || config.setAtMs < 0)) {
94
+ return undefined;
95
+ }
96
+ const phase = candidate.phase;
97
+ const base = {
98
+ version: 1,
99
+ goalId: candidate.goalId,
100
+ revision: candidate.revision,
101
+ config: { ...config, objective: config.objective.trim() },
102
+ updatedAtMs: candidate.updatedAtMs,
103
+ };
104
+ const hasWaitingFields = Object.prototype.hasOwnProperty.call(candidate, "waitingFor") ||
105
+ Object.prototype.hasOwnProperty.call(candidate, "waitingSinceMs");
106
+ const hasTerminalField = Object.prototype.hasOwnProperty.call(candidate, "terminal");
107
+ if (phase === "active" || phase === "paused") {
108
+ if (hasWaitingFields || hasTerminalField)
109
+ return undefined;
110
+ return { ...base, phase };
111
+ }
112
+ if (phase === "waiting") {
113
+ if (hasTerminalField)
114
+ return undefined;
115
+ if (candidate.waitingFor !== "finite_background_work" ||
116
+ typeof candidate.waitingSinceMs !== "number" ||
117
+ !Number.isFinite(candidate.waitingSinceMs) ||
118
+ candidate.waitingSinceMs < 0) {
119
+ return undefined;
120
+ }
121
+ return {
122
+ ...base,
123
+ phase,
124
+ waitingFor: "finite_background_work",
125
+ waitingSinceMs: candidate.waitingSinceMs,
126
+ };
127
+ }
128
+ if (phase === "terminal") {
129
+ if (hasWaitingFields)
130
+ return undefined;
131
+ if (!candidate.terminal || typeof candidate.terminal !== "object")
132
+ return undefined;
133
+ const terminal = candidate.terminal;
134
+ if (Object.keys(terminal).some((key) => key !== "reason" && key !== "atMs"))
135
+ return undefined;
136
+ const reasons = new Set([
137
+ "completed",
138
+ "cancelled",
139
+ "user_cleared",
140
+ "stop_blocks_exhausted",
141
+ "token_budget_exhausted",
142
+ "time_budget_exhausted",
143
+ "max_turns_exhausted",
144
+ ]);
145
+ if (!reasons.has(terminal.reason) ||
146
+ typeof terminal.atMs !== "number" ||
147
+ !Number.isFinite(terminal.atMs) ||
148
+ terminal.atMs < 0) {
149
+ return undefined;
150
+ }
151
+ return {
152
+ ...base,
153
+ phase,
154
+ terminal: { reason: terminal.reason, atMs: terminal.atMs },
155
+ };
156
+ }
157
+ return undefined;
158
+ }
159
+ export function waitGoalLifecycle(lifecycle, nowMs = Date.now()) {
160
+ if (lifecycle.phase !== "active")
161
+ return undefined;
162
+ return {
163
+ ...lifecycle,
164
+ phase: "waiting",
165
+ updatedAtMs: nowMs,
166
+ waitingSinceMs: nowMs,
167
+ waitingFor: "finite_background_work",
168
+ };
169
+ }
170
+ export function armGoalLifecycle(lifecycle, nowMs = Date.now()) {
171
+ if (lifecycle.phase === "active")
172
+ return lifecycle;
173
+ if (lifecycle.phase !== "waiting")
174
+ return undefined;
175
+ const { waitingSinceMs: _waitingSinceMs, waitingFor: _waitingFor, ...base } = lifecycle;
176
+ return { ...base, phase: "active", updatedAtMs: nowMs };
177
+ }
178
+ export function terminateGoalLifecycle(lifecycle, reason, nowMs = Date.now()) {
179
+ if (lifecycle.phase === "terminal")
180
+ return lifecycle;
181
+ return {
182
+ version: 1,
183
+ goalId: lifecycle.goalId,
184
+ revision: lifecycle.revision,
185
+ config: lifecycle.config,
186
+ phase: "terminal",
187
+ updatedAtMs: nowMs,
188
+ terminal: { reason, atMs: nowMs },
189
+ };
190
+ }
191
+ /** True only when both values identify the same concrete goal instance. */
192
+ export function isSameGoalInstance(left, right) {
193
+ if (!left || !right)
194
+ return false;
195
+ if (left.goalId !== undefined || right.goalId !== undefined) {
196
+ return !!left.goalId && left.goalId === right.goalId;
197
+ }
198
+ return left.objective === right.objective && left.setAtMs === right.setAtMs;
199
+ }
200
+ /** Same concrete goal and same edit/pause/resume revision. */
201
+ export function isSameGoalVersion(left, right) {
202
+ return (isSameGoalInstance(left, right) &&
203
+ Math.max(1, Math.floor(left?.revision ?? 1)) === Math.max(1, Math.floor(right?.revision ?? 1)));
204
+ }
205
+ /** Bound stale-writer protection while retaining enough recent terminal identities. */
206
+ export const MAX_GOAL_TERMINALS = 64;
207
+ /**
208
+ * Deterministic identity for a pre-goalId persisted goal. Two detached
209
+ * snapshots of the same session/legacy signature must migrate to the same id.
210
+ */
211
+ export function deriveLegacyGoalId(sessionId, goal) {
212
+ const signature = JSON.stringify([
213
+ sessionId,
214
+ goal.objective.trim(),
215
+ goal.setAtMs ?? null,
216
+ goal.tokenBudget ?? null,
217
+ goal.timeBudgetMs ?? null,
218
+ goal.maxTurns ?? null,
219
+ goal.maxStopBlocks ?? null,
220
+ ]);
221
+ return `legacy-${createHash("sha256").update(signature).digest("hex")}`;
222
+ }
223
+ /** Merge terminal identities oldest→newest, dedupe by instance, and cap growth. */
224
+ export function mergeGoalTerminals(...sources) {
225
+ const merged = new Map();
226
+ let order = 0;
227
+ for (const source of sources) {
228
+ const terminals = Array.isArray(source) ? source : source ? [source] : [];
229
+ for (const terminal of terminals) {
230
+ if (!terminal || typeof terminal.objective !== "string")
231
+ continue;
232
+ const key = terminal.goalId
233
+ ? `id:${terminal.goalId}:rev:${terminal.revision ?? 1}`
234
+ : `legacy:${terminal.objective}\0${terminal.setAtMs ?? ""}`;
235
+ const existing = merged.get(key);
236
+ const incomingAt = terminal.terminatedAtMs ?? Number.NEGATIVE_INFINITY;
237
+ const existingAt = existing?.terminal.terminatedAtMs ?? Number.NEGATIVE_INFINITY;
238
+ if (!existing || incomingAt >= existingAt) {
239
+ merged.set(key, { terminal, order: existing?.order ?? order });
240
+ }
241
+ order++;
242
+ }
243
+ }
244
+ return [...merged.values()]
245
+ .sort((left, right) => {
246
+ const leftAt = left.terminal.terminatedAtMs ?? Number.NEGATIVE_INFINITY;
247
+ const rightAt = right.terminal.terminatedAtMs ?? Number.NEGATIVE_INFINITY;
248
+ return leftAt - rightAt || left.order - right.order;
249
+ })
250
+ .slice(-MAX_GOAL_TERMINALS)
251
+ .map(({ terminal }) => terminal);
252
+ }
253
+ /** Whether this concrete goal is present in the bounded terminal identity set. */
254
+ export function isGoalTerminated(goal, terminals, latest) {
255
+ if (!goal)
256
+ return false;
257
+ return mergeGoalTerminals(terminals, latest).some((terminal) => {
258
+ if (goal.goalId) {
259
+ return terminal.goalId === goal.goalId && (terminal.revision ?? 1) === (goal.revision ?? 1);
260
+ }
261
+ // A legacy detached snapshot has no goalId. Compare its historical
262
+ // objective+setAt signature even when the terminal was recorded after the
263
+ // live instance was migrated to a UUID. Newly armed goals always have an
264
+ // id, so this fallback cannot suppress a genuine new instance.
265
+ return goal.objective === terminal.objective && goal.setAtMs === terminal.setAtMs;
266
+ });
267
+ }
268
+ /** Record one terminal identity on a state-like holder without losing prior identities. */
269
+ export function recordGoalTerminal(state, goal, reason, terminatedAtMs = Date.now()) {
270
+ const goalId = goal.goalId ?? randomUUID();
271
+ goal.goalId = goalId;
272
+ const revision = goal.revision ?? 1;
273
+ goal.revision = revision;
274
+ const terminal = {
275
+ goalId,
276
+ revision,
277
+ objective: goal.objective,
278
+ setAtMs: goal.setAtMs,
279
+ reason,
280
+ terminatedAtMs,
281
+ };
282
+ state.goalTerminals = mergeGoalTerminals(state.goalTerminals, state.goalTerminal, terminal);
283
+ state.goalTerminal = terminal;
284
+ return terminal;
285
+ }
286
+ /**
287
+ * Default consecutive-stop-block cap for goal runs. The real safety net for an
288
+ * unattended goal is the token/time budget + maxTurns; this cap only stops a
289
+ * goal that the judge keeps re-blocking with NO progress between blocks. 8 was
290
+ * too tight — a complex goal can legitimately get "not yet" several times while
291
+ * advancing — so we allow a generous streak before declaring it stuck.
292
+ */
293
+ export const GOAL_DEFAULT_MAX_STOP_BLOCKS = 25;
294
+ /**
295
+ * Consecutive-stop-block cap for NON-goal (interactive) runs. The 25-block goal
296
+ * default is far too loose for a plain interactive session: a plugin `on_stop`
297
+ * hook that keeps returning continueSession would loop 25× before the backstop
298
+ * bites (~3× the model calls). Non-goal runs fall back to this tighter cap —
299
+ * the historical value before the goal feature raised the goal-mode default.
300
+ */
301
+ export const INTERACTIVE_DEFAULT_MAX_STOP_BLOCKS = 8;
302
+ /**
303
+ * Resolve the consecutive-stop-block cap for a run. Precedence:
304
+ * 1. explicit `configMaxStopBlocks` (engine/caller override)
305
+ * 2. `goal.maxStopBlocks`
306
+ * 3. GOAL_DEFAULT_MAX_STOP_BLOCKS when a goal is active,
307
+ * INTERACTIVE_DEFAULT_MAX_STOP_BLOCKS otherwise (don't leak the loose goal
308
+ * cap onto plain interactive runs — see B3 in the redesign doc).
309
+ * Pure + injectable so engine and tests agree.
310
+ */
311
+ export function resolveMaxStopBlocks(configMaxStopBlocks, goal) {
312
+ if (typeof configMaxStopBlocks === "number" && configMaxStopBlocks > 0) {
313
+ return Math.floor(configMaxStopBlocks);
314
+ }
315
+ if (goal?.maxStopBlocks && goal.maxStopBlocks > 0)
316
+ return goal.maxStopBlocks;
317
+ return goal ? GOAL_DEFAULT_MAX_STOP_BLOCKS : INTERACTIVE_DEFAULT_MAX_STOP_BLOCKS;
318
+ }
319
+ /**
320
+ * Turn ceiling applied when a goal is active and neither the GoalConfig nor
321
+ * the engine config set an explicit maxTurns. Higher than the interactive
322
+ * default (100) because goal runs are unattended and the stop-hook keeps the
323
+ * loop going until the objective is met — the real safety backstops are the
324
+ * token/time budgets and maxStopBlocks, not this ceiling. A round 300 gives
325
+ * long goals room while still bounding a pathological loop.
326
+ */
327
+ export const GOAL_DEFAULT_MAX_TURNS = 300;
328
+ /**
329
+ * Coerce a raw goal input into a normalized GoalConfig, or undefined when
330
+ * there is effectively no goal (empty objective). Drops non-positive
331
+ * budgets (0 / negative → "no limit", same as absent).
332
+ */
333
+ export function normalizeGoal(raw) {
334
+ if (raw == null)
335
+ return undefined;
336
+ const obj = typeof raw === "string" ? { objective: raw } : { ...raw };
337
+ const objective = (obj.objective ?? "").trim();
338
+ if (!objective)
339
+ return undefined;
340
+ const out = { objective };
341
+ if (typeof obj.goalId === "string" && obj.goalId.trim())
342
+ out.goalId = obj.goalId;
343
+ if (typeof obj.revision === "number" && obj.revision > 0) {
344
+ out.revision = Math.floor(obj.revision);
345
+ }
346
+ if (obj.paused === true)
347
+ out.paused = true;
348
+ if (typeof obj.tokenBudget === "number" && obj.tokenBudget > 0)
349
+ out.tokenBudget = obj.tokenBudget;
350
+ if (typeof obj.timeBudgetMs === "number" && obj.timeBudgetMs > 0)
351
+ out.timeBudgetMs = obj.timeBudgetMs;
352
+ if (typeof obj.maxTurns === "number" && obj.maxTurns > 0)
353
+ out.maxTurns = Math.floor(obj.maxTurns);
354
+ if (typeof obj.maxStopBlocks === "number" && obj.maxStopBlocks > 0) {
355
+ out.maxStopBlocks = Math.floor(obj.maxStopBlocks);
356
+ }
357
+ // Preserve a positive goal-set timestamp so a resumed/inherited goal keeps
358
+ // the anchor for relative deadlines. Non-positive/junk is dropped, matching
359
+ // the budget fields (a bogus anchor is worse than none).
360
+ if (typeof obj.setAtMs === "number" && obj.setAtMs > 0)
361
+ out.setAtMs = obj.setAtMs;
362
+ return out;
363
+ }
364
+ /**
365
+ * Decide the goal-set timestamp (setAtMs) to persist when a send supplies an
366
+ * explicit goal. Pure so the engine and tests agree on the anchor rule:
367
+ * - A NEW or CHANGED objective → stamp `nowMs` (the user just expressed a
368
+ * fresh goal; any relative deadline in it is anchored to now).
369
+ * - The SAME objective continuing (bare re-send inheriting the stored goal) →
370
+ * keep the stored anchor, so a re-send doesn't silently move the deadline.
371
+ * Falls back to `nowMs` if the stored goal predates this field.
372
+ * `stored` is the previously-persisted active goal (or undefined for the first
373
+ * goal on a session).
374
+ */
375
+ export function resolveGoalSetAt(explicitObjective, stored, nowMs) {
376
+ const sameGoalContinues = !!stored && stored.objective === explicitObjective;
377
+ return sameGoalContinues ? (stored.setAtMs ?? nowMs) : nowMs;
378
+ }
379
+ /** Interactive (no-goal) turn ceiling — a single prompt rarely needs more. */
380
+ export const INTERACTIVE_DEFAULT_MAX_TURNS = 100;
381
+ /**
382
+ * Resolve the turn ceiling for a run, honoring goal mode.
383
+ *
384
+ * Precedence (first defined wins):
385
+ * 1. `configMaxTurns` — an explicit engine/caller override (always wins).
386
+ * 2. `goal.maxTurns` — a per-goal cap from the GoalConfig.
387
+ * 3. `GOAL_DEFAULT_MAX_TURNS` when a goal is active (raise the unattended
388
+ * ceiling above the interactive default).
389
+ * 4. `INTERACTIVE_DEFAULT_MAX_TURNS` otherwise.
390
+ *
391
+ * Pure + injectable so the engine and tests agree on the rule.
392
+ */
393
+ export function resolveMaxTurns(configMaxTurns, goal) {
394
+ if (typeof configMaxTurns === "number" && configMaxTurns > 0)
395
+ return configMaxTurns;
396
+ if (goal)
397
+ return goal.maxTurns ?? GOAL_DEFAULT_MAX_TURNS;
398
+ return INTERACTIVE_DEFAULT_MAX_TURNS;
399
+ }
400
+ /** Warn when this many turns (or fewer) remain before maxTurns. */
401
+ export const APPROACH_TURNS = 5;
402
+ /** Warn when this many consecutive blocks (or fewer) remain before the cap. */
403
+ export const APPROACH_STOP_BLOCKS = 3;
404
+ /**
405
+ * Compute proximity to the two stop ceilings. Pure so turn-loop and tests
406
+ * agree. `nearest` favors stopBlocks when both are near, because the stop-block
407
+ * cap (default 25) is reached far sooner than maxTurns (default 300) for a
408
+ * goal the judge keeps re-blocking — it's the limit that actually bites.
409
+ */
410
+ export function limitProximity(turnCount, maxTurns, stopBlockCount, maxStopBlocks) {
411
+ const turnsRemaining = maxTurns - turnCount;
412
+ const stopBlocksRemaining = maxStopBlocks - stopBlockCount;
413
+ const turnsNear = turnsRemaining <= APPROACH_TURNS;
414
+ const blocksNear = stopBlocksRemaining <= APPROACH_STOP_BLOCKS;
415
+ return {
416
+ turnsRemaining,
417
+ stopBlocksRemaining,
418
+ approaching: turnsNear || blocksNear,
419
+ nearest: blocksNear ? "stopBlocks" : "turns",
420
+ };
421
+ }
422
+ /**
423
+ * Compute the new turn ceiling + goal budgets after a mid-run extension
424
+ * (TODO 3.1). Pure so the TurnLoop and tests agree on the arithmetic:
425
+ * - addTurns bumps the maxTurns ceiling (floored, positive only).
426
+ * - addTokenBudget/addTimeBudgetMs raise the goal's caps. When a cap was
427
+ * previously unset ("unlimited"), we seed it from CURRENT USAGE before
428
+ * adding — `tokensUsed` for tokens, `elapsedMs` for time — so extending an
429
+ * unbounded run sets a fresh cap ABOVE current usage rather than below it.
430
+ * (Seeding time from 0 was bug B1: a long-running unbounded goal would get
431
+ * a cap below its elapsed time and force-stop immediately on extend.)
432
+ * - addStopBlocks is handled by the caller (TurnLoop.extend) against the live
433
+ * config, not here, since it isn't part of the goal budget shape.
434
+ * `tokensUsed`/`elapsedMs` are the tracker's running totals (used only to seed
435
+ * unset caps). Returns the resulting limits; never mutates its inputs.
436
+ */
437
+ export function applyGoalExtension(currentMaxTurns, goal, tokensUsed, elapsedMs, ext) {
438
+ let maxTurns = currentMaxTurns;
439
+ if (typeof ext.addTurns === "number" && ext.addTurns > 0) {
440
+ maxTurns += Math.floor(ext.addTurns);
441
+ }
442
+ let tokenBudget = goal?.tokenBudget;
443
+ let timeBudgetMs = goal?.timeBudgetMs;
444
+ if (goal) {
445
+ if (typeof ext.addTokenBudget === "number" && ext.addTokenBudget > 0) {
446
+ tokenBudget = (tokenBudget ?? tokensUsed) + Math.floor(ext.addTokenBudget);
447
+ }
448
+ if (typeof ext.addTimeBudgetMs === "number" && ext.addTimeBudgetMs > 0) {
449
+ // Seed an unset cap from elapsed time (not 0), mirroring the token branch,
450
+ // so the new cap lands above current usage. Fixes B1.
451
+ timeBudgetMs = (timeBudgetMs ?? elapsedMs) + Math.floor(ext.addTimeBudgetMs);
452
+ }
453
+ }
454
+ return { maxTurns, tokenBudget, timeBudgetMs };
455
+ }
456
+ /** Create a run-scoped tracker. `nowMs` is injected for testability. */
457
+ export function createGoalBudgetTracker(goal, nowMs) {
458
+ return { goal, startedAtMs: nowMs, tokensUsed: 0 };
459
+ }
460
+ /** Add this turn's token usage to the running total. */
461
+ export function recordGoalUsage(tracker, turnTokens) {
462
+ if (turnTokens > 0)
463
+ tracker.tokensUsed += turnTokens;
464
+ }
465
+ /**
466
+ * Has the run exceeded any configured budget? `nowMs` is injected so callers
467
+ * pass the current clock (and tests pass a fixed value).
468
+ */
469
+ export function goalBudgetExceeded(tracker, nowMs) {
470
+ return goalBudgetTerminationReason(tracker, nowMs) !== undefined;
471
+ }
472
+ /** Identify which goal budget forced termination, preserving token-first precedence. */
473
+ export function goalBudgetTerminationReason(tracker, nowMs) {
474
+ const { tokenBudget, timeBudgetMs } = tracker.goal;
475
+ if (typeof tokenBudget === "number" && tracker.tokensUsed > tokenBudget) {
476
+ return "token_budget_exhausted";
477
+ }
478
+ if (typeof timeBudgetMs === "number" && nowMs - tracker.startedAtMs > timeBudgetMs) {
479
+ return "time_budget_exhausted";
480
+ }
481
+ return undefined;
482
+ }
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Hook event type definitions.
3
3
  */
4
- import type { GoalTerminationReason } from "../engine/goal.js";
4
+ import type { GoalTerminationReason } from "../goal/lifecycle.js";
5
5
  /**
6
6
  * Lifecycle hooks the engine emits.
7
7
  *
@@ -37,6 +37,10 @@ import type { GoalTerminationReason } from "../engine/goal.js";
37
37
  * consecutive-block cap (maxStopBlocks)
38
38
  * and maxTurns. ctx.data carries `goal`,
39
39
  * `finalText`, `turnCount`. Distinct from
40
+ * the built-in Goal judge's recent
41
+ * conversation, which stays on a private
42
+ * TurnLoop-to-hook closure and is never
43
+ * exposed through HookContext.
40
44
  * HookResult.stop, which controls the hook
41
45
  * CHAIN, not agent termination.
42
46
  * - pre_tool_use / post_tool_use (executor.ts) — pre_tool_use honors
@@ -63,14 +67,24 @@ import type { GoalTerminationReason } from "../engine/goal.js";
63
67
  * window/emergency), `beforeTokens`,
64
68
  * `afterTokens`. Microcompact is
65
69
  * intentionally suppressed.
66
- * - notification (agent.ts) fired when a background
67
- * sub-agent transitions to a terminal
68
- * state. ctx.data carries `kind`
69
- * ("agent_completed" / "agent_failed" /
70
- * "agent_cancelled"), `agentId`,
71
- * `name`, `description`, plus
72
- * `finalText` (completed) or `error`
73
- * (failed). Fired void — handler
70
+ * - notification — the fan-in "something happened"
71
+ * channel; ctx.data.kind discriminates:
72
+ * · agent_completed / agent_failed /
73
+ * agent_cancelled (agent.ts) bg
74
+ * sub-agent terminal states, plus
75
+ * agentId/name/finalText/error.
76
+ * · approval_requested /
77
+ * approval_resolved (engine.ts
78
+ * PermissionClassifier) — an
79
+ * interactive approval is waiting /
80
+ * was answered; data carries
81
+ * toolName, riskLevel, approved.
82
+ * · mcp_server_connected /
83
+ * mcp_server_failed (engine.ts ←
84
+ * MCPManager.connectAll) — per-server
85
+ * connect outcome; data carries
86
+ * server, error.
87
+ * Fired void — handler
74
88
  * latency does not block the main
75
89
  * loop. Not consumed by the engine
76
90
  * (bg-agent feed renders the same
@@ -88,7 +102,7 @@ import type { GoalTerminationReason } from "../engine/goal.js";
88
102
  * ContextManager; current implementation only knows
89
103
  * after-the-fact (use post_compact instead).
90
104
  */
91
- export type HookEventName = "on_agent_start" | "on_agent_end" | "on_turn_start" | "on_turn_end" | "on_stop" | "on_tool_start" | "on_tool_end" | "on_permission_check" | "on_session_start" | "on_session_end" | "pre_tool_use" | "post_tool_use" | "user_prompt_submit" | "pre_compact" | "post_compact" | "file_changed" | "notification";
105
+ export type HookEventName = "on_agent_start" | "on_agent_end" | "on_turn_start" | "on_turn_end" | "on_stop" | "on_tool_start" | "on_tool_end" | "on_permission_check" | "on_session_start" | "on_session_end" | "pre_tool_use" | "post_tool_use" | "user_prompt_submit" | "agent_direction_submit" | "pre_compact" | "post_compact" | "file_changed" | "notification";
92
106
  export interface HookContext {
93
107
  eventName: HookEventName;
94
108
  data: Record<string, unknown>;
@@ -1,6 +1,6 @@
1
1
  import type { HookHandler } from "./registry.js";
2
- import type { LLMResponse, TokenUsage, ToolResult } from "../types.js";
3
- import { type GoalConfig } from "../engine/goal.js";
2
+ import type { LLMResponse, Message, TokenUsage } from "../types.js";
3
+ import { type GoalConfig } from "../goal/lifecycle.js";
4
4
  /** Narrow LLM surface the judge needs — just a one-shot completion. */
5
5
  export interface GoalJudgeLLM {
6
6
  /** Main-generation request timeout; the judge derives a shorter ceiling. */
@@ -30,6 +30,8 @@ export interface GoalStopHookOptions {
30
30
  log: GoalLogger;
31
31
  /** Override the goal instead of reading ctx.data.goal (mainly for tests). */
32
32
  goal?: string | GoalConfig;
33
+ /** Live goal accessor used by edit/pause controls during a running loop. */
34
+ getGoal?: () => GoalConfig | undefined;
33
35
  /**
34
36
  * Re-check whether the session STILL has a live persisted goal, evaluated
35
37
  * fresh each time the hook fires. The hook's `goal` above is frozen at
@@ -48,7 +50,10 @@ export interface GoalStopHookOptions {
48
50
  * a later bare send doesn't re-inherit a satisfied goal. Optional so tests /
49
51
  * non-persistent callers can omit it.
50
52
  */
51
- onMet?: () => void;
53
+ /** Return false when durable cleanup failed; the hook will keep Goal mode active. */
54
+ onMet?: () => boolean | void;
55
+ /** Persist active → waiting before allowing a finite-background-work stop. */
56
+ onWaiting?: () => boolean | void;
52
57
  /**
53
58
  * Clock source for the current time fed to the judge. Injectable so tests
54
59
  * pin a fixed instant (the judge prompt is a fresh sub-call, not part of the
@@ -68,33 +73,17 @@ export interface GoalStopHookOptions {
68
73
  * Private usage seam into TurnLoop's Goal ledger. Returning a termination
69
74
  * reason prevents verdict side effects and lets the loop stop immediately.
70
75
  */
71
- onJudgeUsage?: (usage: TokenUsage | undefined) => "token_budget_exhausted" | "time_budget_exhausted" | undefined;
76
+ onJudgeUsage?: (usage: TokenUsage | undefined, judgedGoal?: GoalConfig) => "token_budget_exhausted" | "time_budget_exhausted" | undefined;
72
77
  }
73
78
  export interface GoalJudgeRuntimeContext {
74
- /** Recent irreversible projections from this run, newest evidence retained by TurnLoop. */
75
- toolResults: GoalJudgeToolResult[];
76
- progress: {
77
- turnCount: number;
78
- /** One-based natural-stop/judge round for this run. */
79
- stopRound: number;
80
- elapsedMs: number;
81
- tokensUsed: number;
82
- tokenBudget?: number;
83
- timeBudgetMs?: number;
84
- maxTurns?: number;
85
- maxStopBlocks?: number;
86
- };
79
+ conversation: Message[];
80
+ renderedConversation: string;
81
+ digest: string;
82
+ selectedRoundCount: number;
83
+ sourceRoundCount: number;
84
+ estimatedTokens: number;
85
+ chars: number;
86
+ truncated: boolean;
87
87
  }
88
- export interface GoalJudgeToolResult {
89
- turnCount: number;
90
- toolName: string;
91
- status: "success" | "error";
92
- /** Bounded plain text only; absent for sensitive or purely non-text results. */
93
- text?: string;
94
- /** True when image/binary/structured blocks were replaced with a placeholder. */
95
- omittedNonText?: true;
96
- }
97
- /** Build the bounded, irreversible value retained beyond the current model round. */
98
- export declare function projectGoalJudgeToolResult(result: ToolResult, turnCount: number, sensitiveByMetadata?: boolean): GoalJudgeToolResult;
99
88
  export declare function createGoalStopHook(opts: GoalStopHookOptions): HookHandler;
100
89
  export {};