@pellux/goodvibes-tui 0.18.4

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 (732) hide show
  1. package/.goodvibes/GOODVIBES.md +35 -0
  2. package/.goodvibes/agents/reviewer.md +89 -0
  3. package/.goodvibes/skills/add-provider/SKILL.md +199 -0
  4. package/CHANGELOG.md +1681 -0
  5. package/README.md +1607 -0
  6. package/bin/goodvibes +66 -0
  7. package/docs/README.md +32 -0
  8. package/docs/foundation-artifacts/README.md +16 -0
  9. package/docs/foundation-artifacts/knowledge-graphql.graphql +397 -0
  10. package/docs/foundation-artifacts/knowledge-store.sql +183 -0
  11. package/docs/foundation-artifacts/operator-contract.json +55157 -0
  12. package/docs/foundation-artifacts/peer-contract.json +2384 -0
  13. package/package.json +114 -0
  14. package/scripts/postinstall.mjs +203 -0
  15. package/src/acp/connection.ts +447 -0
  16. package/src/acp/index.ts +7 -0
  17. package/src/acp/manager.ts +133 -0
  18. package/src/adapters/bluebubbles/index.ts +127 -0
  19. package/src/adapters/discord/index.ts +297 -0
  20. package/src/adapters/github/index.ts +73 -0
  21. package/src/adapters/google-chat/index.ts +119 -0
  22. package/src/adapters/imessage/index.ts +92 -0
  23. package/src/adapters/index.ts +15 -0
  24. package/src/adapters/matrix/index.ts +116 -0
  25. package/src/adapters/mattermost/index.ts +151 -0
  26. package/src/adapters/msteams/index.ts +180 -0
  27. package/src/adapters/ntfy/index.ts +118 -0
  28. package/src/adapters/signal/index.ts +92 -0
  29. package/src/adapters/slack/index.ts +323 -0
  30. package/src/adapters/telegram/index.ts +160 -0
  31. package/src/adapters/types.ts +97 -0
  32. package/src/adapters/webhook/index.ts +178 -0
  33. package/src/adapters/whatsapp/index.ts +135 -0
  34. package/src/agents/message-bus-core.ts +312 -0
  35. package/src/agents/message-bus.ts +2 -0
  36. package/src/agents/orchestrator-prompts.ts +351 -0
  37. package/src/agents/orchestrator-runner.ts +668 -0
  38. package/src/agents/orchestrator.ts +437 -0
  39. package/src/agents/session.ts +108 -0
  40. package/src/agents/worktree.ts +153 -0
  41. package/src/agents/wrfc-config.ts +47 -0
  42. package/src/agents/wrfc-controller.ts +747 -0
  43. package/src/agents/wrfc-gate-runtime.ts +75 -0
  44. package/src/agents/wrfc-reporting.ts +284 -0
  45. package/src/agents/wrfc-runtime-events.ts +150 -0
  46. package/src/agents/wrfc-types.ts +67 -0
  47. package/src/automation/delivery-manager.ts +368 -0
  48. package/src/automation/index.ts +72 -0
  49. package/src/automation/manager-runtime-delivery.ts +139 -0
  50. package/src/automation/manager-runtime-events.ts +131 -0
  51. package/src/automation/manager-runtime-execution.ts +511 -0
  52. package/src/automation/manager-runtime-helpers.ts +433 -0
  53. package/src/automation/manager-runtime-job-mutations.ts +175 -0
  54. package/src/automation/manager-runtime-reconcile.ts +148 -0
  55. package/src/automation/manager-runtime-scheduling.ts +189 -0
  56. package/src/automation/manager-runtime-sync.ts +54 -0
  57. package/src/automation/manager-runtime.ts +721 -0
  58. package/src/automation/manager.ts +10 -0
  59. package/src/automation/service.ts +242 -0
  60. package/src/channels/builtin/account-actions.ts +490 -0
  61. package/src/channels/builtin/accounts.ts +433 -0
  62. package/src/channels/builtin/contracts.ts +405 -0
  63. package/src/channels/builtin/plugins.ts +308 -0
  64. package/src/channels/builtin/rendering.ts +174 -0
  65. package/src/channels/builtin/setup-schema.ts +504 -0
  66. package/src/channels/builtin/shared.ts +96 -0
  67. package/src/channels/builtin/surfaces.ts +57 -0
  68. package/src/channels/builtin/targets.ts +693 -0
  69. package/src/channels/builtin-runtime.ts +443 -0
  70. package/src/channels/delivery/shared.ts +199 -0
  71. package/src/channels/delivery/strategies-bridge.ts +246 -0
  72. package/src/channels/delivery/strategies-core.ts +299 -0
  73. package/src/channels/delivery/strategies-enterprise.ts +178 -0
  74. package/src/channels/delivery/types.ts +59 -0
  75. package/src/channels/delivery-router.ts +127 -0
  76. package/src/channels/index.ts +77 -0
  77. package/src/channels/plugin-registry.ts +551 -0
  78. package/src/channels/provider-runtime.ts +330 -0
  79. package/src/channels/reply-pipeline.ts +522 -0
  80. package/src/channels/route-manager.ts +340 -0
  81. package/src/channels/surface-registry.ts +186 -0
  82. package/src/config/helper-model.ts +233 -0
  83. package/src/config/index.ts +193 -0
  84. package/src/config/manager.ts +404 -0
  85. package/src/config/secrets.ts +547 -0
  86. package/src/config/service-registry.ts +329 -0
  87. package/src/config/subscription-auth.ts +31 -0
  88. package/src/config/subscription-providers.ts +127 -0
  89. package/src/config/tool-llm.ts +110 -0
  90. package/src/control-plane/approval-broker.ts +351 -0
  91. package/src/control-plane/gateway.ts +713 -0
  92. package/src/control-plane/index.ts +54 -0
  93. package/src/control-plane/media-contract-schemas.ts +208 -0
  94. package/src/control-plane/method-catalog-admin.ts +136 -0
  95. package/src/control-plane/method-catalog-channels.ts +591 -0
  96. package/src/control-plane/method-catalog-control-automation.ts +475 -0
  97. package/src/control-plane/method-catalog-control-core.ts +594 -0
  98. package/src/control-plane/method-catalog-control.ts +8 -0
  99. package/src/control-plane/method-catalog-events.ts +74 -0
  100. package/src/control-plane/method-catalog-knowledge.ts +531 -0
  101. package/src/control-plane/method-catalog-media.ts +279 -0
  102. package/src/control-plane/method-catalog-runtime.ts +304 -0
  103. package/src/control-plane/method-catalog-shared.ts +223 -0
  104. package/src/control-plane/method-catalog.ts +242 -0
  105. package/src/control-plane/operator-contract-schemas-admin.ts +639 -0
  106. package/src/control-plane/operator-contract-schemas-channels.ts +375 -0
  107. package/src/control-plane/operator-contract-schemas-control.ts +226 -0
  108. package/src/control-plane/operator-contract-schemas-domains.ts +4 -0
  109. package/src/control-plane/operator-contract-schemas-knowledge.ts +582 -0
  110. package/src/control-plane/operator-contract-schemas-media.ts +297 -0
  111. package/src/control-plane/operator-contract-schemas-permissions.ts +100 -0
  112. package/src/control-plane/operator-contract-schemas-remote.ts +38 -0
  113. package/src/control-plane/operator-contract-schemas-runtime.ts +563 -0
  114. package/src/control-plane/operator-contract-schemas-shared.ts +85 -0
  115. package/src/control-plane/operator-contract-schemas-telemetry.ts +349 -0
  116. package/src/control-plane/operator-contract-schemas.ts +6 -0
  117. package/src/control-plane/operator-contract.ts +163 -0
  118. package/src/control-plane/session-broker.ts +780 -0
  119. package/src/core/compaction-sections.ts +492 -0
  120. package/src/core/compaction-types.ts +147 -0
  121. package/src/core/composer-state.ts +59 -0
  122. package/src/core/context-compaction.ts +542 -0
  123. package/src/core/conversation-compaction.ts +68 -0
  124. package/src/core/conversation-diff.ts +55 -0
  125. package/src/core/conversation-rendering.ts +343 -0
  126. package/src/core/conversation-utils.ts +72 -0
  127. package/src/core/conversation.ts +775 -0
  128. package/src/core/event-replay.ts +287 -0
  129. package/src/core/orchestrator-context-runtime.ts +407 -0
  130. package/src/core/orchestrator-follow-up-runtime.ts +134 -0
  131. package/src/core/orchestrator-runtime.ts +132 -0
  132. package/src/core/orchestrator-tool-runtime.ts +468 -0
  133. package/src/core/orchestrator-turn-helpers.ts +355 -0
  134. package/src/core/orchestrator-turn-loop.ts +443 -0
  135. package/src/core/orchestrator.ts +733 -0
  136. package/src/core/plan-command-handler.ts +169 -0
  137. package/src/core/system-message-router.ts +210 -0
  138. package/src/core/transcript-events/classify.ts +95 -0
  139. package/src/core/transcript-events/index.ts +15 -0
  140. package/src/daemon/cli.ts +88 -0
  141. package/src/daemon/control-plane.ts +522 -0
  142. package/src/daemon/facade-composition.ts +397 -0
  143. package/src/daemon/facade.ts +638 -0
  144. package/src/daemon/helpers.ts +74 -0
  145. package/src/daemon/http/router-route-contexts.ts +370 -0
  146. package/src/daemon/http/router.ts +531 -0
  147. package/src/daemon/http-listener.ts +301 -0
  148. package/src/daemon/index.ts +3 -0
  149. package/src/daemon/server.ts +1 -0
  150. package/src/daemon/service-manager.ts +413 -0
  151. package/src/daemon/surface-actions.ts +183 -0
  152. package/src/daemon/surface-delivery.ts +530 -0
  153. package/src/daemon/surface-policy.ts +60 -0
  154. package/src/daemon/transport-events.ts +110 -0
  155. package/src/daemon/types.ts +191 -0
  156. package/src/export/markdown.ts +213 -0
  157. package/src/export/session-export.ts +633 -0
  158. package/src/git/index.ts +1 -0
  159. package/src/git/service.ts +414 -0
  160. package/src/hooks/chain-engine.ts +414 -0
  161. package/src/hooks/dispatcher.ts +414 -0
  162. package/src/hooks/hook-api.ts +170 -0
  163. package/src/hooks/index.ts +48 -0
  164. package/src/hooks/runners/agent.ts +93 -0
  165. package/src/hooks/runners/prompt.ts +69 -0
  166. package/src/hooks/workbench.ts +360 -0
  167. package/src/input/autocomplete.ts +96 -0
  168. package/src/input/bookmark-modal.ts +115 -0
  169. package/src/input/command-registry.ts +300 -0
  170. package/src/input/commands/branch-runtime.ts +72 -0
  171. package/src/input/commands/config.ts +515 -0
  172. package/src/input/commands/control-room-runtime.ts +255 -0
  173. package/src/input/commands/conversation-runtime.ts +207 -0
  174. package/src/input/commands/diff-runtime.ts +161 -0
  175. package/src/input/commands/discovery-runtime.ts +45 -0
  176. package/src/input/commands/eval.ts +204 -0
  177. package/src/input/commands/experience-runtime.ts +278 -0
  178. package/src/input/commands/git-runtime.ts +81 -0
  179. package/src/input/commands/guidance-runtime.ts +101 -0
  180. package/src/input/commands/health-runtime.ts +434 -0
  181. package/src/input/commands/hooks-runtime.ts +148 -0
  182. package/src/input/commands/incident-runtime.ts +95 -0
  183. package/src/input/commands/integration-runtime.ts +394 -0
  184. package/src/input/commands/intelligence-runtime.ts +223 -0
  185. package/src/input/commands/knowledge.ts +368 -0
  186. package/src/input/commands/local-auth-runtime.ts +105 -0
  187. package/src/input/commands/local-provider-runtime.ts +170 -0
  188. package/src/input/commands/local-runtime.ts +458 -0
  189. package/src/input/commands/local-setup-review.ts +192 -0
  190. package/src/input/commands/local-setup-transfer.ts +134 -0
  191. package/src/input/commands/local-setup.ts +292 -0
  192. package/src/input/commands/managed-runtime.ts +208 -0
  193. package/src/input/commands/marketplace-runtime.ts +290 -0
  194. package/src/input/commands/mcp-runtime.ts +202 -0
  195. package/src/input/commands/memory-product-runtime.ts +111 -0
  196. package/src/input/commands/memory.ts +151 -0
  197. package/src/input/commands/notify-runtime.ts +83 -0
  198. package/src/input/commands/operator-panel-runtime.ts +141 -0
  199. package/src/input/commands/operator-runtime.ts +392 -0
  200. package/src/input/commands/permissions-runtime.ts +104 -0
  201. package/src/input/commands/planning-runtime.ts +97 -0
  202. package/src/input/commands/platform-access-runtime.ts +422 -0
  203. package/src/input/commands/platform-runtime.ts +6 -0
  204. package/src/input/commands/platform-sandbox-qemu.ts +137 -0
  205. package/src/input/commands/platform-sandbox-runtime.ts +406 -0
  206. package/src/input/commands/platform-sandbox-session.ts +128 -0
  207. package/src/input/commands/platform-services-runtime.ts +246 -0
  208. package/src/input/commands/policy-dispatch.ts +339 -0
  209. package/src/input/commands/policy.ts +17 -0
  210. package/src/input/commands/product-runtime.ts +351 -0
  211. package/src/input/commands/profile-sync-runtime.ts +99 -0
  212. package/src/input/commands/provider-accounts-runtime.ts +113 -0
  213. package/src/input/commands/provider.ts +363 -0
  214. package/src/input/commands/quit-shared.ts +162 -0
  215. package/src/input/commands/recall-bundle.ts +132 -0
  216. package/src/input/commands/recall-capture.ts +152 -0
  217. package/src/input/commands/recall-query.ts +229 -0
  218. package/src/input/commands/recall-review.ts +98 -0
  219. package/src/input/commands/recall-shared.ts +22 -0
  220. package/src/input/commands/remote-runtime-pool.ts +106 -0
  221. package/src/input/commands/remote-runtime-setup.ts +199 -0
  222. package/src/input/commands/remote-runtime.ts +531 -0
  223. package/src/input/commands/replay-runtime.ts +18 -0
  224. package/src/input/commands/runtime-services.ts +279 -0
  225. package/src/input/commands/schedule-runtime.ts +332 -0
  226. package/src/input/commands/services-runtime.ts +207 -0
  227. package/src/input/commands/session-content.ts +408 -0
  228. package/src/input/commands/session-workflow.ts +464 -0
  229. package/src/input/commands/session.ts +376 -0
  230. package/src/input/commands/settings-sync-runtime.ts +173 -0
  231. package/src/input/commands/share-runtime.ts +114 -0
  232. package/src/input/commands/shell-core.ts +320 -0
  233. package/src/input/commands/skills-runtime.ts +221 -0
  234. package/src/input/commands/subscription-runtime.ts +434 -0
  235. package/src/input/commands/tasks-runtime.ts +230 -0
  236. package/src/input/commands/teamwork-runtime.ts +374 -0
  237. package/src/input/commands/teleport-runtime.ts +57 -0
  238. package/src/input/commands/worktree-runtime.ts +137 -0
  239. package/src/input/commands.ts +127 -0
  240. package/src/input/file-picker.ts +192 -0
  241. package/src/input/handler-command-route.ts +106 -0
  242. package/src/input/handler-content-actions.ts +465 -0
  243. package/src/input/handler-feed-routes.ts +541 -0
  244. package/src/input/handler-feed.ts +361 -0
  245. package/src/input/handler-modal-routes.ts +335 -0
  246. package/src/input/handler-modal-stack.ts +237 -0
  247. package/src/input/handler-modal-token-routes.ts +272 -0
  248. package/src/input/handler-picker-routes.ts +416 -0
  249. package/src/input/handler-prompt-buffer.ts +320 -0
  250. package/src/input/handler-shortcuts.ts +195 -0
  251. package/src/input/handler-ui-state.ts +294 -0
  252. package/src/input/handler.ts +798 -0
  253. package/src/input/input-history.ts +267 -0
  254. package/src/input/keybindings.ts +256 -0
  255. package/src/input/model-picker.ts +730 -0
  256. package/src/input/panel-integration-actions.ts +77 -0
  257. package/src/input/profile-picker-modal.ts +222 -0
  258. package/src/input/search.ts +100 -0
  259. package/src/input/selection-modal.ts +163 -0
  260. package/src/input/selection.ts +135 -0
  261. package/src/input/session-picker-modal.ts +136 -0
  262. package/src/input/settings-modal.ts +718 -0
  263. package/src/input/submission-intent.ts +18 -0
  264. package/src/input/submission-router.ts +64 -0
  265. package/src/integrations/index.ts +42 -0
  266. package/src/integrations/notifier.ts +206 -0
  267. package/src/integrations/webhooks.ts +177 -0
  268. package/src/knowledge/consolidation.ts +346 -0
  269. package/src/knowledge/graphql.ts +324 -0
  270. package/src/knowledge/index.ts +60 -0
  271. package/src/knowledge/ingest-compile.ts +386 -0
  272. package/src/knowledge/ingest-context.ts +18 -0
  273. package/src/knowledge/ingest-inputs.ts +387 -0
  274. package/src/knowledge/ingest.ts +20 -0
  275. package/src/knowledge/internal.ts +257 -0
  276. package/src/knowledge/knowledge-api.ts +432 -0
  277. package/src/knowledge/lint.ts +121 -0
  278. package/src/knowledge/memory-sync.ts +62 -0
  279. package/src/knowledge/packet.ts +370 -0
  280. package/src/knowledge/scheduling.ts +283 -0
  281. package/src/knowledge/service.ts +715 -0
  282. package/src/main.ts +798 -0
  283. package/src/mcp/client.ts +383 -0
  284. package/src/mcp/index.ts +12 -0
  285. package/src/mcp/mcp-api.ts +90 -0
  286. package/src/mcp/registry.ts +508 -0
  287. package/src/media/builtin-image-understanding.ts +303 -0
  288. package/src/media/builtin-providers.ts +26 -0
  289. package/src/media/index.ts +18 -0
  290. package/src/multimodal/index.ts +13 -0
  291. package/src/multimodal/service.ts +492 -0
  292. package/src/panels/agent-inspector-panel.ts +515 -0
  293. package/src/panels/agent-inspector-shared.ts +94 -0
  294. package/src/panels/agent-logs-panel.ts +539 -0
  295. package/src/panels/agent-logs-shared.ts +129 -0
  296. package/src/panels/approval-panel.ts +169 -0
  297. package/src/panels/automation-control-panel.ts +253 -0
  298. package/src/panels/base-panel.ts +72 -0
  299. package/src/panels/builtin/agent.ts +88 -0
  300. package/src/panels/builtin/development.ts +111 -0
  301. package/src/panels/builtin/knowledge.ts +26 -0
  302. package/src/panels/builtin/operations.ts +385 -0
  303. package/src/panels/builtin/session.ts +61 -0
  304. package/src/panels/builtin/shared.ts +240 -0
  305. package/src/panels/builtin-panels.ts +23 -0
  306. package/src/panels/cockpit-panel.ts +183 -0
  307. package/src/panels/communication-panel.ts +191 -0
  308. package/src/panels/context-visualizer-panel.ts +199 -0
  309. package/src/panels/control-plane-panel.ts +266 -0
  310. package/src/panels/cost-tracker-panel.ts +444 -0
  311. package/src/panels/debug-panel.ts +432 -0
  312. package/src/panels/diff-panel.ts +518 -0
  313. package/src/panels/docs-panel.ts +283 -0
  314. package/src/panels/eval-panel.ts +399 -0
  315. package/src/panels/file-explorer-panel.ts +556 -0
  316. package/src/panels/file-preview-panel.ts +412 -0
  317. package/src/panels/forensics-panel.ts +364 -0
  318. package/src/panels/git-panel.ts +630 -0
  319. package/src/panels/hooks-panel.ts +274 -0
  320. package/src/panels/incident-review-panel.ts +247 -0
  321. package/src/panels/index.ts +48 -0
  322. package/src/panels/intelligence-panel.ts +176 -0
  323. package/src/panels/knowledge-panel.ts +328 -0
  324. package/src/panels/local-auth-panel.ts +146 -0
  325. package/src/panels/marketplace-panel.ts +223 -0
  326. package/src/panels/mcp-panel.ts +260 -0
  327. package/src/panels/memory-panel.ts +293 -0
  328. package/src/panels/ops-control-panel.ts +184 -0
  329. package/src/panels/ops-strategy-panel.ts +235 -0
  330. package/src/panels/orchestration-panel.ts +254 -0
  331. package/src/panels/panel-list-panel.ts +508 -0
  332. package/src/panels/panel-manager.ts +538 -0
  333. package/src/panels/panel-picker.ts +106 -0
  334. package/src/panels/plan-dashboard-panel.ts +272 -0
  335. package/src/panels/plugins-panel.ts +201 -0
  336. package/src/panels/policy-panel.ts +308 -0
  337. package/src/panels/polish.ts +668 -0
  338. package/src/panels/provider-account-snapshot.ts +259 -0
  339. package/src/panels/provider-accounts-panel.ts +221 -0
  340. package/src/panels/provider-health-domains.ts +211 -0
  341. package/src/panels/provider-health-panel.ts +725 -0
  342. package/src/panels/provider-health-tracker.ts +115 -0
  343. package/src/panels/provider-stats-panel.ts +366 -0
  344. package/src/panels/remote-panel.ts +449 -0
  345. package/src/panels/routes-panel.ts +228 -0
  346. package/src/panels/sandbox-panel.ts +289 -0
  347. package/src/panels/schedule-panel.ts +344 -0
  348. package/src/panels/search-focus.ts +32 -0
  349. package/src/panels/security-panel.ts +329 -0
  350. package/src/panels/services-panel.ts +271 -0
  351. package/src/panels/session-browser-panel.ts +399 -0
  352. package/src/panels/session-maintenance.ts +125 -0
  353. package/src/panels/settings-sync-panel.ts +164 -0
  354. package/src/panels/skills-panel.ts +475 -0
  355. package/src/panels/subscription-panel.ts +273 -0
  356. package/src/panels/symbol-outline-panel.ts +486 -0
  357. package/src/panels/system-messages-panel.ts +224 -0
  358. package/src/panels/tasks-panel.ts +448 -0
  359. package/src/panels/thinking-panel.ts +304 -0
  360. package/src/panels/token-budget-panel.ts +469 -0
  361. package/src/panels/tool-inspector-panel.ts +434 -0
  362. package/src/panels/types.ts +44 -0
  363. package/src/panels/watchers-panel.ts +241 -0
  364. package/src/panels/welcome-panel.ts +64 -0
  365. package/src/panels/worktree-panel.ts +180 -0
  366. package/src/panels/wrfc-panel.ts +480 -0
  367. package/src/permissions/briefs/build.ts +88 -0
  368. package/src/permissions/manager.ts +356 -0
  369. package/src/permissions/prompt.ts +184 -0
  370. package/src/plugins/api.ts +383 -0
  371. package/src/plugins/loader.ts +304 -0
  372. package/src/plugins/manager.ts +481 -0
  373. package/src/profiles/shape.ts +58 -0
  374. package/src/providers/amazon-bedrock-mantle.ts +50 -0
  375. package/src/providers/amazon-bedrock.ts +61 -0
  376. package/src/providers/anthropic-compat.ts +373 -0
  377. package/src/providers/anthropic-sdk-provider.ts +230 -0
  378. package/src/providers/anthropic-vertex.ts +59 -0
  379. package/src/providers/anthropic.ts +469 -0
  380. package/src/providers/auto-register.ts +417 -0
  381. package/src/providers/builtin-catalog.ts +326 -0
  382. package/src/providers/builtin-registry.ts +575 -0
  383. package/src/providers/cache-planner.ts +258 -0
  384. package/src/providers/capabilities.ts +601 -0
  385. package/src/providers/custom-loader.ts +425 -0
  386. package/src/providers/discovered-compat.ts +18 -0
  387. package/src/providers/discovered-factory.ts +61 -0
  388. package/src/providers/discovered-traits.ts +138 -0
  389. package/src/providers/gemini.ts +462 -0
  390. package/src/providers/github-copilot.ts +254 -0
  391. package/src/providers/index.ts +47 -0
  392. package/src/providers/interface.ts +185 -0
  393. package/src/providers/llama-cpp.ts +402 -0
  394. package/src/providers/lm-studio-helpers.ts +367 -0
  395. package/src/providers/lm-studio.ts +484 -0
  396. package/src/providers/model-catalog-cache.ts +221 -0
  397. package/src/providers/model-catalog-notifications.ts +97 -0
  398. package/src/providers/model-catalog-synthetic.ts +202 -0
  399. package/src/providers/model-catalog.ts +211 -0
  400. package/src/providers/model-limits.ts +280 -0
  401. package/src/providers/ollama.ts +469 -0
  402. package/src/providers/openai-codex.ts +472 -0
  403. package/src/providers/openai-compat.ts +615 -0
  404. package/src/providers/openai.ts +231 -0
  405. package/src/providers/optimizer.ts +381 -0
  406. package/src/providers/provider-api.ts +553 -0
  407. package/src/providers/registry-helpers.ts +34 -0
  408. package/src/providers/registry-models.ts +77 -0
  409. package/src/providers/registry-types.ts +67 -0
  410. package/src/providers/registry.ts +729 -0
  411. package/src/providers/runtime-metadata.ts +149 -0
  412. package/src/providers/runtime-snapshot.ts +130 -0
  413. package/src/providers/synthetic.ts +561 -0
  414. package/src/providers/tier-prompts.ts +84 -0
  415. package/src/providers/tool-formats.ts +414 -0
  416. package/src/renderer/agent-detail-modal.ts +285 -0
  417. package/src/renderer/autocomplete-overlay.ts +154 -0
  418. package/src/renderer/block-actions.ts +76 -0
  419. package/src/renderer/bookmark-modal.ts +101 -0
  420. package/src/renderer/bottom-bar.ts +58 -0
  421. package/src/renderer/buffer.ts +34 -0
  422. package/src/renderer/code-block.ts +373 -0
  423. package/src/renderer/compositor.ts +261 -0
  424. package/src/renderer/context-inspector.ts +219 -0
  425. package/src/renderer/conversation-layout.ts +67 -0
  426. package/src/renderer/conversation-overlays.ts +123 -0
  427. package/src/renderer/conversation-surface.ts +260 -0
  428. package/src/renderer/diff-view.ts +132 -0
  429. package/src/renderer/diff.ts +122 -0
  430. package/src/renderer/file-picker-overlay.ts +101 -0
  431. package/src/renderer/file-tree.ts +153 -0
  432. package/src/renderer/git-status.ts +89 -0
  433. package/src/renderer/help-overlay.ts +247 -0
  434. package/src/renderer/history-search-overlay.ts +73 -0
  435. package/src/renderer/layout-engine.ts +97 -0
  436. package/src/renderer/layout.ts +32 -0
  437. package/src/renderer/live-tail-modal.ts +156 -0
  438. package/src/renderer/markdown.ts +777 -0
  439. package/src/renderer/modal-factory.ts +467 -0
  440. package/src/renderer/modal-utils.ts +24 -0
  441. package/src/renderer/model-picker-overlay.ts +396 -0
  442. package/src/renderer/overlay-box.ts +165 -0
  443. package/src/renderer/overlay-viewport.ts +104 -0
  444. package/src/renderer/panel-composite.ts +80 -0
  445. package/src/renderer/panel-picker-overlay.ts +202 -0
  446. package/src/renderer/panel-tab-bar.ts +69 -0
  447. package/src/renderer/panel-workspace-bar.ts +38 -0
  448. package/src/renderer/process-indicator.ts +96 -0
  449. package/src/renderer/process-modal.ts +295 -0
  450. package/src/renderer/profile-picker-modal.ts +129 -0
  451. package/src/renderer/progress.ts +98 -0
  452. package/src/renderer/search-overlay.ts +54 -0
  453. package/src/renderer/selection-modal-overlay.ts +214 -0
  454. package/src/renderer/semantic-diff.ts +369 -0
  455. package/src/renderer/session-picker-modal.ts +127 -0
  456. package/src/renderer/settings-modal.ts +701 -0
  457. package/src/renderer/shell-surface.ts +88 -0
  458. package/src/renderer/surface-layout.ts +101 -0
  459. package/src/renderer/syntax-highlighter.ts +542 -0
  460. package/src/renderer/system-message.ts +83 -0
  461. package/src/renderer/tab-strip.ts +108 -0
  462. package/src/renderer/text-layout.ts +31 -0
  463. package/src/renderer/thinking.ts +17 -0
  464. package/src/renderer/tool-call.ts +233 -0
  465. package/src/renderer/ui-factory.ts +524 -0
  466. package/src/renderer/ui-primitives.ts +96 -0
  467. package/src/runtime/auth/inspection.ts +125 -0
  468. package/src/runtime/bootstrap-background.ts +147 -0
  469. package/src/runtime/bootstrap-command-context.ts +265 -0
  470. package/src/runtime/bootstrap-command-parts.ts +357 -0
  471. package/src/runtime/bootstrap-core.ts +375 -0
  472. package/src/runtime/bootstrap-helpers.ts +88 -0
  473. package/src/runtime/bootstrap-hook-bridge.ts +271 -0
  474. package/src/runtime/bootstrap-runtime-events.ts +254 -0
  475. package/src/runtime/bootstrap-services.ts +197 -0
  476. package/src/runtime/bootstrap-shell.ts +262 -0
  477. package/src/runtime/bootstrap.ts +488 -0
  478. package/src/runtime/compaction/index.ts +90 -0
  479. package/src/runtime/compaction/lifecycle.ts +167 -0
  480. package/src/runtime/compaction/manager.ts +474 -0
  481. package/src/runtime/compaction/quality-score.ts +279 -0
  482. package/src/runtime/compaction/resume-repair.ts +183 -0
  483. package/src/runtime/compaction/strategies/autocompact.ts +65 -0
  484. package/src/runtime/compaction/strategies/boundary-commit.ts +106 -0
  485. package/src/runtime/compaction/strategies/collapse.ts +90 -0
  486. package/src/runtime/compaction/strategies/index.ts +23 -0
  487. package/src/runtime/compaction/strategies/microcompact.ts +74 -0
  488. package/src/runtime/compaction/strategies/reactive.ts +89 -0
  489. package/src/runtime/compaction/types.ts +221 -0
  490. package/src/runtime/context.ts +158 -0
  491. package/src/runtime/diagnostics/actions.ts +776 -0
  492. package/src/runtime/diagnostics/index.ts +99 -0
  493. package/src/runtime/diagnostics/panels/agents.ts +252 -0
  494. package/src/runtime/diagnostics/panels/events.ts +188 -0
  495. package/src/runtime/diagnostics/panels/health.ts +242 -0
  496. package/src/runtime/diagnostics/panels/index.ts +24 -0
  497. package/src/runtime/diagnostics/panels/ops.ts +156 -0
  498. package/src/runtime/diagnostics/panels/policy.ts +176 -0
  499. package/src/runtime/diagnostics/panels/tasks.ts +251 -0
  500. package/src/runtime/diagnostics/panels/tool-calls.ts +267 -0
  501. package/src/runtime/diagnostics/provider.ts +262 -0
  502. package/src/runtime/ecosystem/catalog.ts +606 -0
  503. package/src/runtime/ecosystem/recommendations.ts +117 -0
  504. package/src/runtime/emitters/agents.ts +96 -0
  505. package/src/runtime/emitters/automation.ts +112 -0
  506. package/src/runtime/emitters/communication.ts +53 -0
  507. package/src/runtime/emitters/compaction.ts +161 -0
  508. package/src/runtime/emitters/control-plane.ts +65 -0
  509. package/src/runtime/emitters/deliveries.ts +65 -0
  510. package/src/runtime/emitters/forensics.ts +17 -0
  511. package/src/runtime/emitters/index.ts +59 -0
  512. package/src/runtime/emitters/knowledge.ts +129 -0
  513. package/src/runtime/emitters/mcp.ts +95 -0
  514. package/src/runtime/emitters/ops.ts +163 -0
  515. package/src/runtime/emitters/orchestration.ts +87 -0
  516. package/src/runtime/emitters/permissions.ts +98 -0
  517. package/src/runtime/emitters/planner.ts +23 -0
  518. package/src/runtime/emitters/plugins.ts +78 -0
  519. package/src/runtime/emitters/providers.ts +30 -0
  520. package/src/runtime/emitters/routes.ts +57 -0
  521. package/src/runtime/emitters/security.ts +53 -0
  522. package/src/runtime/emitters/session.ts +93 -0
  523. package/src/runtime/emitters/surfaces.ts +57 -0
  524. package/src/runtime/emitters/tasks.ts +69 -0
  525. package/src/runtime/emitters/tools.ts +140 -0
  526. package/src/runtime/emitters/transport.ts +78 -0
  527. package/src/runtime/emitters/turn.ts +155 -0
  528. package/src/runtime/emitters/ui.ts +57 -0
  529. package/src/runtime/emitters/watchers.ts +57 -0
  530. package/src/runtime/emitters/workflows.ts +79 -0
  531. package/src/runtime/eval/index.ts +48 -0
  532. package/src/runtime/eval/runner.ts +163 -0
  533. package/src/runtime/eval/suites.ts +264 -0
  534. package/src/runtime/events/domain-map.ts +148 -0
  535. package/src/runtime/events/index.ts +194 -0
  536. package/src/runtime/events/turn.ts +60 -0
  537. package/src/runtime/events/workflows.ts +17 -0
  538. package/src/runtime/forensics/collector.ts +693 -0
  539. package/src/runtime/forensics/index.ts +23 -0
  540. package/src/runtime/foundation-clients.ts +78 -0
  541. package/src/runtime/foundation-services.ts +96 -0
  542. package/src/runtime/guidance.ts +183 -0
  543. package/src/runtime/health/effect-handlers.ts +189 -0
  544. package/src/runtime/health/index.ts +70 -0
  545. package/src/runtime/health/wiring.ts +115 -0
  546. package/src/runtime/index.ts +174 -0
  547. package/src/runtime/integration/helpers.ts +640 -0
  548. package/src/runtime/lifecycle.ts +107 -0
  549. package/src/runtime/mcp/index.ts +68 -0
  550. package/src/runtime/mcp/manager.ts +513 -0
  551. package/src/runtime/network/inbound.ts +131 -0
  552. package/src/runtime/network/index.ts +30 -0
  553. package/src/runtime/network/outbound.ts +292 -0
  554. package/src/runtime/network/shared.ts +82 -0
  555. package/src/runtime/operator-client.ts +235 -0
  556. package/src/runtime/ops/control-plane.ts +363 -0
  557. package/src/runtime/ops/index.ts +122 -0
  558. package/src/runtime/ops/playbooks/index.ts +10 -0
  559. package/src/runtime/ops/playbooks/session-unrecoverable.ts +196 -0
  560. package/src/runtime/ops/playbooks/stuck-turn.ts +197 -0
  561. package/src/runtime/ops/runtime-context.ts +100 -0
  562. package/src/runtime/ops-api.ts +27 -0
  563. package/src/runtime/orchestration/spawn-policy.ts +83 -0
  564. package/src/runtime/peer-client.ts +404 -0
  565. package/src/runtime/perf/index.ts +57 -0
  566. package/src/runtime/perf/slo-collector.ts +375 -0
  567. package/src/runtime/permissions/index.ts +190 -0
  568. package/src/runtime/permissions/policy-runtime.ts +175 -0
  569. package/src/runtime/permissions/preflight.ts +101 -0
  570. package/src/runtime/permissions/rule-suggestions.ts +36 -0
  571. package/src/runtime/plugins/hot-reload.ts +221 -0
  572. package/src/runtime/plugins/index.ts +84 -0
  573. package/src/runtime/plugins/lifecycle.ts +95 -0
  574. package/src/runtime/plugins/manager.ts +474 -0
  575. package/src/runtime/plugins/manifest.ts +167 -0
  576. package/src/runtime/plugins/quarantine.ts +202 -0
  577. package/src/runtime/plugins/trust.ts +291 -0
  578. package/src/runtime/plugins/types.ts +205 -0
  579. package/src/runtime/provider-accounts/registry.ts +326 -0
  580. package/src/runtime/remote/distributed-runtime-contract-schemas.ts +386 -0
  581. package/src/runtime/remote/index.ts +488 -0
  582. package/src/runtime/remote/runner-registry.ts +438 -0
  583. package/src/runtime/remote/supervisor.ts +70 -0
  584. package/src/runtime/runtime-hook-api.ts +5 -0
  585. package/src/runtime/runtime-knowledge-api.ts +14 -0
  586. package/src/runtime/runtime-mcp-api.ts +5 -0
  587. package/src/runtime/runtime-ops-api.ts +86 -0
  588. package/src/runtime/runtime-provider-api.ts +18 -0
  589. package/src/runtime/sandbox/backend.ts +291 -0
  590. package/src/runtime/sandbox/manager.ts +364 -0
  591. package/src/runtime/sandbox/provisioning.ts +422 -0
  592. package/src/runtime/sandbox/session-registry.ts +289 -0
  593. package/src/runtime/services.ts +541 -0
  594. package/src/runtime/session-maintenance.ts +188 -0
  595. package/src/runtime/session-persistence.ts +288 -0
  596. package/src/runtime/session-return-context.ts +195 -0
  597. package/src/runtime/settings/control-plane-store.ts +258 -0
  598. package/src/runtime/settings/control-plane.ts +599 -0
  599. package/src/runtime/shell-command-extensions.ts +54 -0
  600. package/src/runtime/shell-command-ops.ts +207 -0
  601. package/src/runtime/shell-command-platform.ts +47 -0
  602. package/src/runtime/shell-command-services.ts +143 -0
  603. package/src/runtime/shell-command-workspace.ts +31 -0
  604. package/src/runtime/store/domains/conversation.ts +181 -0
  605. package/src/runtime/store/domains/domain-read-matrix.ts +17 -0
  606. package/src/runtime/store/domains/index.ts +222 -0
  607. package/src/runtime/store/domains/panels.ts +117 -0
  608. package/src/runtime/store/domains/permissions.ts +143 -0
  609. package/src/runtime/store/domains/ui-perf.ts +103 -0
  610. package/src/runtime/store/helpers/reducers/conversation.ts +228 -0
  611. package/src/runtime/store/helpers/reducers/lifecycle.ts +440 -0
  612. package/src/runtime/store/helpers/reducers/shared.ts +60 -0
  613. package/src/runtime/store/helpers/reducers/sync.ts +555 -0
  614. package/src/runtime/store/helpers/reducers.ts +30 -0
  615. package/src/runtime/store/index.ts +304 -0
  616. package/src/runtime/store/selectors/index.ts +354 -0
  617. package/src/runtime/store/state.ts +137 -0
  618. package/src/runtime/tasks/adapters/acp-adapter.ts +211 -0
  619. package/src/runtime/tasks/adapters/agent-adapter.ts +208 -0
  620. package/src/runtime/tasks/adapters/index.ts +16 -0
  621. package/src/runtime/tasks/adapters/process-adapter.ts +214 -0
  622. package/src/runtime/tasks/adapters/scheduler-adapter.ts +193 -0
  623. package/src/runtime/tasks/index.ts +68 -0
  624. package/src/runtime/tasks/manager.ts +415 -0
  625. package/src/runtime/telemetry/api-helpers.ts +517 -0
  626. package/src/runtime/telemetry/api.ts +768 -0
  627. package/src/runtime/telemetry/index.ts +178 -0
  628. package/src/runtime/telemetry/instrumentation/domain-bridge-agent-session.ts +440 -0
  629. package/src/runtime/telemetry/instrumentation/domain-bridge-plugin-mcp.ts +200 -0
  630. package/src/runtime/telemetry/instrumentation/domain-bridge-shared.ts +18 -0
  631. package/src/runtime/telemetry/instrumentation/domain-bridge-transport-task.ts +204 -0
  632. package/src/runtime/telemetry/instrumentation/domain-bridge.ts +125 -0
  633. package/src/runtime/telemetry/instrumentation/index.ts +67 -0
  634. package/src/runtime/tools/context.ts +114 -0
  635. package/src/runtime/tools/index.ts +46 -0
  636. package/src/runtime/tools/phased-executor.ts +448 -0
  637. package/src/runtime/tools/phases/budget.ts +130 -0
  638. package/src/runtime/tools/phases/execute.ts +69 -0
  639. package/src/runtime/tools/phases/index.ts +13 -0
  640. package/src/runtime/tools/phases/map-output.ts +98 -0
  641. package/src/runtime/tools/phases/permission.ts +133 -0
  642. package/src/runtime/tools/phases/posthook.ts +57 -0
  643. package/src/runtime/tools/phases/prehook.ts +68 -0
  644. package/src/runtime/tools/phases/validate.ts +53 -0
  645. package/src/runtime/transports/direct.ts +73 -0
  646. package/src/runtime/transports/http-helpers.ts +218 -0
  647. package/src/runtime/transports/http-types.ts +364 -0
  648. package/src/runtime/transports/http.ts +629 -0
  649. package/src/runtime/transports/realtime.ts +50 -0
  650. package/src/runtime/transports/remote-events.ts +16 -0
  651. package/src/runtime/transports/shared.ts +39 -0
  652. package/src/runtime/transports/ui-runtime-events.ts +35 -0
  653. package/src/runtime/ui/index.ts +39 -0
  654. package/src/runtime/ui/model-picker/data-provider.ts +182 -0
  655. package/src/runtime/ui/model-picker/health-enrichment.ts +228 -0
  656. package/src/runtime/ui/model-picker/index.ts +59 -0
  657. package/src/runtime/ui/model-picker/types.ts +149 -0
  658. package/src/runtime/ui/provider-health/data-provider.ts +244 -0
  659. package/src/runtime/ui/provider-health/fallback-visualizer.ts +69 -0
  660. package/src/runtime/ui/provider-health/index.ts +46 -0
  661. package/src/runtime/ui/provider-health/types.ts +146 -0
  662. package/src/runtime/ui-events.ts +46 -0
  663. package/src/runtime/ui-read-model-helpers.ts +32 -0
  664. package/src/runtime/ui-read-models-core.ts +95 -0
  665. package/src/runtime/ui-read-models-observability-maintenance.ts +81 -0
  666. package/src/runtime/ui-read-models-observability-options.ts +5 -0
  667. package/src/runtime/ui-read-models-observability-remote.ts +73 -0
  668. package/src/runtime/ui-read-models-observability-security.ts +172 -0
  669. package/src/runtime/ui-read-models-observability-system.ts +217 -0
  670. package/src/runtime/ui-read-models-observability.ts +59 -0
  671. package/src/runtime/ui-read-models-operations.ts +203 -0
  672. package/src/runtime/ui-read-models.ts +61 -0
  673. package/src/runtime/ui-service-queries.ts +114 -0
  674. package/src/runtime/ui-services.ts +163 -0
  675. package/src/runtime/worktree/registry.ts +252 -0
  676. package/src/scripts/process-messages.ts +42 -0
  677. package/src/sessions/manager.ts +388 -0
  678. package/src/shell/blocking-input.ts +89 -0
  679. package/src/shell/ui-openers.ts +163 -0
  680. package/src/state/file-watcher.ts +294 -0
  681. package/src/state/index.ts +56 -0
  682. package/src/state/knowledge-injection.ts +214 -0
  683. package/src/state/memory-embedding-http.ts +642 -0
  684. package/src/state/memory-embeddings.ts +312 -0
  685. package/src/state/memory-ingest.ts +132 -0
  686. package/src/state/memory-registry.ts +111 -0
  687. package/src/state/memory-store-helpers.ts +160 -0
  688. package/src/state/memory-store.ts +728 -0
  689. package/src/state/memory-vector-store.ts +418 -0
  690. package/src/templates/manager.ts +187 -0
  691. package/src/tools/agent/index.ts +610 -0
  692. package/src/tools/agent/manager.ts +476 -0
  693. package/src/tools/analyze/git-modes.ts +380 -0
  694. package/src/tools/analyze/index.ts +128 -0
  695. package/src/tools/channel/agent-tools.ts +16 -0
  696. package/src/tools/channel/index.ts +268 -0
  697. package/src/tools/control/index.ts +90 -0
  698. package/src/tools/edit/core.ts +619 -0
  699. package/src/tools/edit/index.ts +4 -0
  700. package/src/tools/edit/phased.ts +33 -0
  701. package/src/tools/fetch/index.ts +3 -0
  702. package/src/tools/fetch/phased.ts +34 -0
  703. package/src/tools/fetch/runtime.ts +499 -0
  704. package/src/tools/index.ts +186 -0
  705. package/src/tools/mcp/index.ts +190 -0
  706. package/src/tools/remote-trigger/index.ts +130 -0
  707. package/src/tools/repl/index.ts +318 -0
  708. package/src/tools/shared/auto-heal.ts +282 -0
  709. package/src/tools/state/index.ts +688 -0
  710. package/src/tools/web-search/index.ts +38 -0
  711. package/src/tools/write/index.ts +604 -0
  712. package/src/tools/write/phased.ts +41 -0
  713. package/src/types/generated/foundation-client-types.ts +22 -0
  714. package/src/types/sql-js.d.ts +15 -0
  715. package/src/utils/splash-lines.ts +46 -0
  716. package/src/version.ts +17 -0
  717. package/src/watchers/index.ts +11 -0
  718. package/src/watchers/registry.ts +517 -0
  719. package/src/web-search/index.ts +26 -0
  720. package/src/web-search/provider-registry.ts +64 -0
  721. package/src/web-search/providers/brave.ts +100 -0
  722. package/src/web-search/providers/duckduckgo.ts +270 -0
  723. package/src/web-search/providers/exa.ts +77 -0
  724. package/src/web-search/providers/firecrawl.ts +90 -0
  725. package/src/web-search/providers/perplexity.ts +86 -0
  726. package/src/web-search/providers/searxng.ts +88 -0
  727. package/src/web-search/providers/shared.ts +249 -0
  728. package/src/web-search/providers/tavily.ts +90 -0
  729. package/src/web-search/service.ts +142 -0
  730. package/src/widget/index.ts +2 -0
  731. package/src/widget/types.ts +9 -0
  732. package/src/widget/widget.ts +8 -0
@@ -0,0 +1,668 @@
1
+ import { ConversationManager } from '../core/conversation.ts';
2
+ import { ToolRegistry } from '@pellux/goodvibes-sdk/platform/tools/registry';
3
+ import { join } from 'node:path';
4
+ import type { ProviderRegistry } from '../providers/registry.ts';
5
+ import { logger } from '@pellux/goodvibes-sdk/platform/utils/logger';
6
+ import { ConsecutiveErrorBreaker } from '@pellux/goodvibes-sdk/platform/core/circuit-breaker';
7
+ import { isRateLimitOrQuotaError, isContextSizeExceededError } from '@pellux/goodvibes-sdk/platform/types/errors';
8
+ import { AgentSession } from './session.ts';
9
+ import type { ProviderOptimizer } from '../providers/optimizer.ts';
10
+ import {
11
+ estimateTokens,
12
+ estimateConversationTokens,
13
+ compactSmallWindow,
14
+ } from '../core/context-compaction.ts';
15
+ import type { AgentRecord } from '../tools/agent/index.ts';
16
+ import type { LLMProvider, StreamDelta } from '../providers/interface.ts';
17
+ import type { ToolResult } from '@pellux/goodvibes-sdk/platform/types/tools';
18
+ import type { ProcessManager } from '@pellux/goodvibes-sdk/platform/tools/shared/process-manager';
19
+ import type { FeatureFlagManager } from '@pellux/goodvibes-sdk/platform/runtime/feature-flags/manager';
20
+ import type { RuntimeEventBus } from '../runtime/events/index.ts';
21
+ import { summarizeToolArgs } from '@pellux/goodvibes-sdk/platform/agents/orchestrator-utils';
22
+ import { buildLayeredOrchestratorSystemPrompt, buildOrchestratorSystemPrompt } from './orchestrator-prompts.ts';
23
+ import type { AgentMessageBus } from './message-bus.ts';
24
+ import type { KnowledgeService } from '../knowledge/index.ts';
25
+ import type { ArchetypeLoader } from '@pellux/goodvibes-sdk/platform/agents/archetypes';
26
+ import { summarizeError } from '@pellux/goodvibes-sdk/platform/utils/error-display';
27
+
28
+ const MAX_TURNS = 50;
29
+ const NETWORK_RETRY_DELAYS_MS = [5_000, 10_000, 20_000, 40_000, 60_000];
30
+ const RATE_LIMIT_RETRY_DELAY_MS = 60_000;
31
+ const RATE_LIMIT_MAX_RETRIES = 3;
32
+ const CONTEXT_COMPACT_THRESHOLD = 0.85;
33
+ const MIN_WINDOW_FOR_LLM_COMPACT = 12_000;
34
+
35
+ type EmitterContext = import('../runtime/emitters/index.ts').EmitterContext;
36
+
37
+ export interface AgentOrchestratorRunContext {
38
+ readonly workingDirectory: string;
39
+ readonly runtimeBus: RuntimeEventBus | null;
40
+ readonly featureFlagManager: FeatureFlagManager | null;
41
+ readonly emitterContext: (agentId: string) => EmitterContext;
42
+ readonly emitAgentProgress: (recordId: string, progress: string) => void;
43
+ readonly emitOrchestrationProgress: (record: AgentRecord, progress: string) => void;
44
+ readonly emitAgentStarted: (recordId: string) => void;
45
+ readonly emitAgentCancelledEvent: (recordId: string, reason: string) => void;
46
+ readonly emitOrchestrationCancelled: (record: AgentRecord, reason: string) => void;
47
+ readonly emitAgentFailedEvent: (recordId: string, error: string, durationMs: number) => void;
48
+ readonly emitOrchestrationFailed: (record: AgentRecord, error: string) => void;
49
+ readonly emitAgentCompletedEvent: (recordId: string, durationMs: number, output: string, toolCallsMade: number) => void;
50
+ readonly emitOrchestrationCompleted: (record: AgentRecord, output: string) => void;
51
+ readonly emitStreamDelta: (recordId: string, content: string, accumulated: string) => void;
52
+ readonly processManager?: ProcessManager;
53
+ readonly messageBus: Pick<AgentMessageBus, 'getMessages'>;
54
+ readonly knowledgeService?: Pick<KnowledgeService, 'buildPromptPacketSync'>;
55
+ readonly memoryRegistry?: Pick<import('../state/index.ts').MemoryRegistry, 'getAll' | 'searchSemantic'>;
56
+ readonly archetypeLoader?: Pick<ArchetypeLoader, 'loadArchetype'>;
57
+ readonly getFullRegistry: () => ToolRegistry;
58
+ readonly buildScopedRegistry: (allowedNames: string[], fullRegistry: ToolRegistry) => ToolRegistry;
59
+ readonly providerRegistry: Pick<ProviderRegistry, 'getCurrentModel' | 'getForModel' | 'get' | 'listModels' | 'getContextWindowForModel'>;
60
+ readonly providerOptimizer?: Pick<ProviderOptimizer, 'recordFallbackTransition'>;
61
+ readonly resolveProviderForRecord: (
62
+ providerRegistry: Pick<ProviderRegistry, 'getCurrentModel' | 'getForModel' | 'get' | 'listModels'>,
63
+ record: AgentRecord,
64
+ currentModel: { id: string; provider: string },
65
+ ) => { provider: LLMProvider; modelId: string; requestedModelId: string };
66
+ readonly resolveFallbackModelRoutes: (
67
+ providerRegistry: Pick<ProviderRegistry, 'listModels' | 'getForModel'>,
68
+ record: AgentRecord,
69
+ currentModel: { id: string; provider: string },
70
+ primaryRequestedModelId: string,
71
+ ) => Array<{ provider: LLMProvider; modelId: string; requestedModelId: string }>;
72
+ }
73
+
74
+ function isNetworkError(err: unknown): boolean {
75
+ if (!(err instanceof Error)) return false;
76
+ const msg = err.message.toLowerCase();
77
+ return (
78
+ msg.includes('fetch failed') ||
79
+ msg.includes('econnrefused') ||
80
+ msg.includes('enotfound') ||
81
+ msg.includes('network error') ||
82
+ msg.includes('network timeout') ||
83
+ msg.includes('networkerror') ||
84
+ msg.includes('econnreset') ||
85
+ msg.includes('etimedout') ||
86
+ msg.includes('socket hang up') ||
87
+ msg.includes('dns') ||
88
+ msg.includes('connection lost') ||
89
+ msg.includes('epipe') ||
90
+ msg.includes('ehostunreach')
91
+ );
92
+ }
93
+
94
+ function applyContextWindowAwareness(
95
+ context: AgentOrchestratorRunContext,
96
+ record: AgentRecord,
97
+ modelId: string,
98
+ modelWindow: number,
99
+ conversation: ConversationManager,
100
+ systemPrompt: string,
101
+ toolTokens: number,
102
+ turn: number,
103
+ ): string {
104
+ if (!(context.featureFlagManager?.isEnabled('agent-context-window-awareness') ?? true)) {
105
+ return systemPrompt;
106
+ }
107
+
108
+ if (modelWindow === 0) {
109
+ logger.debug(`[agent-context-window-awareness] Context window is 0/unknown for model ${modelId}, skipping context validation`);
110
+ return systemPrompt;
111
+ }
112
+
113
+ const messages = conversation.getMessagesForLLM();
114
+ const msgTokens = estimateConversationTokens(messages);
115
+ const sysTokens = estimateTokens(systemPrompt);
116
+ const totalEstimate = msgTokens + sysTokens + toolTokens;
117
+ const threshold = Math.floor(modelWindow * CONTEXT_COMPACT_THRESHOLD);
118
+
119
+ if (totalEstimate <= threshold) {
120
+ return systemPrompt;
121
+ }
122
+
123
+ logger.warn(
124
+ `[AgentOrchestrator] context-window awareness: estimated ${totalEstimate} tokens exceeds ${threshold} (${Math.round(CONTEXT_COMPACT_THRESHOLD * 100)}% of ${modelWindow}) - compacting`,
125
+ { agentId: record.id, turn, msgTokens, sysTokens, toolTokens, contextWindow: modelWindow },
126
+ );
127
+ record.progress = `Turn ${turn} · Compacting context…`;
128
+
129
+ if (modelWindow <= MIN_WINDOW_FOR_LLM_COMPACT) {
130
+ conversation.replaceMessagesForLLM(compactSmallWindow(messages));
131
+ } else {
132
+ conversation.replaceMessagesForLLM(compactSmallWindow(messages, Math.max(10, Math.floor(messages.length / 2))));
133
+ }
134
+
135
+ const remainingAfterMsgs = modelWindow - estimateConversationTokens(conversation.getMessagesForLLM()) - toolTokens;
136
+ const currentSysTokens = estimateTokens(systemPrompt);
137
+ if (currentSysTokens > remainingAfterMsgs * CONTEXT_COMPACT_THRESHOLD) {
138
+ logger.warn(
139
+ `[AgentOrchestrator] context-window awareness: system prompt (${currentSysTokens} tokens) too large for remaining window (${remainingAfterMsgs}) - applying layered trim`,
140
+ { agentId: record.id },
141
+ );
142
+ return buildLayeredOrchestratorSystemPrompt(record, remainingAfterMsgs, context);
143
+ }
144
+
145
+ return systemPrompt;
146
+ }
147
+
148
+ function cleanupLeakedProcesses(
149
+ processManager: ProcessManager | undefined,
150
+ preAgentProcessIds: Set<string>,
151
+ ): void {
152
+ const pm = processManager;
153
+ if (!pm) return;
154
+ for (const p of pm.list()) {
155
+ if (!preAgentProcessIds.has(p.id)) {
156
+ pm.stop(p.id);
157
+ }
158
+ }
159
+ }
160
+
161
+ async function disposeSession(session: AgentSession): Promise<void> {
162
+ try {
163
+ await session.dispose();
164
+ } catch {
165
+ // non-fatal
166
+ }
167
+ }
168
+
169
+ async function executeToolCalls(
170
+ toolCalls: Awaited<ReturnType<LLMProvider['chat']>>['toolCalls'],
171
+ toolRegistry: ToolRegistry,
172
+ session: AgentSession,
173
+ turn: number,
174
+ record: AgentRecord,
175
+ callHistory: string[],
176
+ callHistoryWindow: number,
177
+ context: AgentOrchestratorRunContext,
178
+ ): Promise<ToolResult[]> {
179
+ const results: ToolResult[] = [];
180
+
181
+ for (const originalCall of toolCalls) {
182
+ const call = { ...originalCall, arguments: { ...originalCall.arguments } };
183
+ const argsSummary = summarizeToolArgs(call.arguments as Record<string, unknown>);
184
+ record.progress = `Turn ${turn} · ${call.name}${argsSummary}`;
185
+ record.toolCallCount++;
186
+ context.emitAgentProgress(record.id, record.progress);
187
+ context.emitOrchestrationProgress(record, record.progress);
188
+
189
+ if (call.name === 'exec' || call.name === 'precision_exec') {
190
+ call.arguments = structuredClone(call.arguments);
191
+ const execArgs = call.arguments as Record<string, unknown>;
192
+ if (Array.isArray(execArgs.commands)) {
193
+ for (const cmd of execArgs.commands as Record<string, unknown>[]) {
194
+ cmd.background = false;
195
+ if (!cmd.timeout_ms) cmd.timeout_ms = 600_000;
196
+ }
197
+ }
198
+ if (!execArgs.timeout_ms) execArgs.timeout_ms = 600_000;
199
+ }
200
+
201
+ const callSig = `${call.name}::${JSON.stringify(call.arguments)}`;
202
+ try {
203
+ const result = await toolRegistry.execute(call.id, call.name, call.arguments);
204
+ results.push({ ...result, callId: call.id });
205
+ session.appendMessage({
206
+ type: 'tool_execution',
207
+ turn,
208
+ toolName: call.name,
209
+ toolCallId: call.id,
210
+ success: result.success !== false,
211
+ args: JSON.stringify(call.arguments).slice(0, 500),
212
+ resultPreview: (result.output ?? result.error ?? '').slice(0, 500),
213
+ timestamp: new Date().toISOString(),
214
+ });
215
+ } catch (err) {
216
+ const toolErr = summarizeError(err);
217
+ results.push({
218
+ callId: call.id,
219
+ success: false,
220
+ error: toolErr,
221
+ });
222
+ session.appendMessage({
223
+ type: 'tool_execution',
224
+ turn,
225
+ toolName: call.name,
226
+ toolCallId: call.id,
227
+ success: false,
228
+ args: JSON.stringify(call.arguments).slice(0, 500),
229
+ resultPreview: toolErr.slice(0, 500),
230
+ timestamp: new Date().toISOString(),
231
+ });
232
+ }
233
+
234
+ callHistory.push(callSig);
235
+ if (callHistory.length > callHistoryWindow) callHistory.shift();
236
+ }
237
+
238
+ return results;
239
+ }
240
+
241
+ async function finalizeAgentRun(
242
+ context: AgentOrchestratorRunContext,
243
+ record: AgentRecord,
244
+ session: AgentSession | null,
245
+ preAgentProcessIds: Set<string>,
246
+ ): Promise<void> {
247
+ const statusAfterLoop = (record as { status: string }).status;
248
+ if (statusAfterLoop !== 'failed' && statusAfterLoop !== 'cancelled') {
249
+ record.status = 'completed';
250
+ }
251
+ record.completedAt = Date.now();
252
+ cleanupLeakedProcesses(context.processManager, preAgentProcessIds);
253
+
254
+ if (context.runtimeBus && record.status !== 'failed' && statusAfterLoop !== 'cancelled') {
255
+ context.emitAgentCompletedEvent(
256
+ record.id,
257
+ (record.completedAt ?? Date.now()) - record.startedAt,
258
+ record.fullOutput ?? '',
259
+ record.toolCallCount,
260
+ );
261
+ context.emitOrchestrationCompleted(record, record.fullOutput ?? '');
262
+ }
263
+
264
+ if (record.status === 'failed') {
265
+ context.emitAgentFailedEvent(
266
+ record.id,
267
+ record.error ?? 'Circuit breaker tripped',
268
+ Date.now() - record.startedAt,
269
+ );
270
+ context.emitOrchestrationFailed(record, record.error ?? 'Circuit breaker tripped');
271
+ logger.error(`Agent ${record.id} circuit-breaker terminated`, { error: record.error, toolCallCount: record.toolCallCount });
272
+ session?.appendMessage({
273
+ type: 'session_end',
274
+ status: 'failed',
275
+ error: record.error,
276
+ toolCallCount: record.toolCallCount,
277
+ durationMs: Date.now() - record.startedAt,
278
+ timestamp: new Date().toISOString(),
279
+ });
280
+ } else if (statusAfterLoop === 'cancelled') {
281
+ context.emitAgentCancelledEvent(record.id, 'Agent cancelled');
282
+ context.emitOrchestrationCancelled(record, 'Agent cancelled');
283
+ logger.info(`Agent ${record.id} cancelled (detected post-loop)`, { toolCallCount: record.toolCallCount });
284
+ session?.appendMessage({
285
+ type: 'session_end',
286
+ status: 'cancelled',
287
+ toolCallCount: record.toolCallCount,
288
+ durationMs: Date.now() - record.startedAt,
289
+ timestamp: new Date().toISOString(),
290
+ });
291
+ } else {
292
+ logger.info(`Agent ${record.id} completed`, { toolCallCount: record.toolCallCount });
293
+ session?.appendMessage({
294
+ type: 'session_end',
295
+ status: 'completed',
296
+ toolCallCount: record.toolCallCount,
297
+ durationMs: Date.now() - record.startedAt,
298
+ timestamp: new Date().toISOString(),
299
+ });
300
+ }
301
+
302
+ if (session) {
303
+ await disposeSession(session);
304
+ }
305
+ }
306
+
307
+ async function handleAgentRunFailure(
308
+ context: AgentOrchestratorRunContext,
309
+ record: AgentRecord,
310
+ conversation: ConversationManager | null,
311
+ session: AgentSession | null,
312
+ preAgentProcessIds: Set<string>,
313
+ err: unknown,
314
+ ): Promise<void> {
315
+ const message = summarizeError(err, {
316
+ ...(record.provider ? { provider: record.provider } : {}),
317
+ });
318
+ if (conversation) {
319
+ const lastMessages = conversation.getMessagesForLLM();
320
+ const lastAssistant = [...lastMessages].reverse().find((m) => m.role === 'assistant');
321
+ if (lastAssistant) {
322
+ record.fullOutput = typeof lastAssistant.content === 'string' ? lastAssistant.content : '';
323
+ }
324
+ }
325
+ record.status = 'failed';
326
+ record.error = message;
327
+ record.completedAt = Date.now();
328
+ cleanupLeakedProcesses(context.processManager, preAgentProcessIds);
329
+ context.emitAgentFailedEvent(record.id, message, Date.now() - record.startedAt);
330
+ context.emitOrchestrationFailed(record, message);
331
+ logger.error(`Agent ${record.id} failed`, { error: message });
332
+ if (session) {
333
+ session.appendMessage({
334
+ type: 'session_end',
335
+ status: 'failed',
336
+ error: message,
337
+ toolCallCount: record.toolCallCount,
338
+ durationMs: Date.now() - record.startedAt,
339
+ timestamp: new Date().toISOString(),
340
+ });
341
+ await disposeSession(session);
342
+ }
343
+ }
344
+
345
+ export async function runAgentTask(
346
+ context: AgentOrchestratorRunContext,
347
+ record: AgentRecord,
348
+ ): Promise<void> {
349
+ record.status = 'running';
350
+ record.progress = 'Initialising…';
351
+ record.usage = {
352
+ inputTokens: record.usage?.inputTokens ?? 0,
353
+ outputTokens: record.usage?.outputTokens ?? 0,
354
+ cacheReadTokens: record.usage?.cacheReadTokens ?? 0,
355
+ cacheWriteTokens: record.usage?.cacheWriteTokens ?? 0,
356
+ ...(record.usage?.reasoningTokens !== undefined ? { reasoningTokens: record.usage.reasoningTokens } : {}),
357
+ llmCallCount: record.usage?.llmCallCount ?? 0,
358
+ turnCount: record.usage?.turnCount ?? 0,
359
+ reasoningSummaryCount: record.usage?.reasoningSummaryCount ?? 0,
360
+ };
361
+ context.emitAgentStarted(record.id);
362
+ context.emitAgentProgress(record.id, record.progress);
363
+ context.emitOrchestrationProgress(record, record.progress);
364
+
365
+ let session: AgentSession | null = null;
366
+ let conversation: ConversationManager | null = null;
367
+ const preAgentProcessIds = new Set((context.processManager?.list() ?? []).map((p) => p.id));
368
+
369
+ try {
370
+ const providerRegistry = context.providerRegistry;
371
+ const currentModel = providerRegistry.getCurrentModel();
372
+ const primaryRoute = context.resolveProviderForRecord(providerRegistry, record, currentModel);
373
+ let activeRoute = primaryRoute;
374
+ let fallbackRouteIndex = 0;
375
+ const fallbackRoutes = context.resolveFallbackModelRoutes(
376
+ providerRegistry,
377
+ record,
378
+ currentModel,
379
+ primaryRoute.requestedModelId,
380
+ );
381
+ const modelId = primaryRoute.modelId;
382
+ record.model = record.model ?? primaryRoute.requestedModelId;
383
+ record.provider = record.provider ?? activeRoute.provider.name;
384
+
385
+ session = new AgentSession(record.id, modelId, record.provider ?? currentModel.provider ?? 'unknown', {
386
+ sessionsDir: join(context.workingDirectory, '.goodvibes', 'tui', 'sessions'),
387
+ stateDir: join(context.workingDirectory, '.goodvibes', 'state'),
388
+ });
389
+ session.appendMessage({ type: 'session_config', template: record.template, task: record.task, tools: record.tools, model: modelId, provider: record.provider ?? 'unknown', timestamp: new Date().toISOString() });
390
+
391
+ const toolRegistry = context.buildScopedRegistry(record.tools, context.getFullRegistry());
392
+ const toolDefinitions = toolRegistry.getToolDefinitions();
393
+ const toolTokens = toolDefinitions.length > 0
394
+ ? estimateTokens(JSON.stringify(toolDefinitions))
395
+ : 0;
396
+
397
+ conversation = new ConversationManager(() => 80);
398
+ conversation.addUserMessage(record.task);
399
+
400
+ let systemPrompt = buildOrchestratorSystemPrompt(record, undefined, context);
401
+
402
+ let continueLoop = true;
403
+ let turn = 0;
404
+ record.progress = 'Turn 1 · Thinking…';
405
+ context.emitAgentProgress(record.id, record.progress);
406
+ context.emitOrchestrationProgress(record, record.progress);
407
+
408
+ const callHistory: string[] = [];
409
+ const LOOP_SYSTEM_THRESHOLD = 3;
410
+ const LOOP_USER_THRESHOLD = 5;
411
+ const CALL_HISTORY_WINDOW = 20;
412
+ const circuitBreaker = new ConsecutiveErrorBreaker();
413
+
414
+ while (continueLoop) {
415
+ if ((record as { status: string }).status === 'cancelled') {
416
+ record.completedAt = Date.now();
417
+ context.emitAgentCancelledEvent(record.id, 'Agent cancelled');
418
+ cleanupLeakedProcesses(context.processManager, preAgentProcessIds);
419
+ if (session) {
420
+ session.appendMessage({ type: 'session_end', status: 'cancelled', turn, timestamp: new Date().toISOString() });
421
+ await disposeSession(session);
422
+ }
423
+ return;
424
+ }
425
+ if (++turn > MAX_TURNS) {
426
+ const lastMessages = conversation.getMessagesForLLM();
427
+ const lastAssistant = [...lastMessages].reverse().find(m => m.role === 'assistant');
428
+ if (lastAssistant) {
429
+ record.fullOutput = typeof lastAssistant.content === 'string' ? lastAssistant.content : '';
430
+ }
431
+ record.status = 'failed';
432
+ record.error = `Exceeded maximum turn limit (${MAX_TURNS})`;
433
+ if (session) {
434
+ session.appendMessage({ type: 'session_end', status: 'max_turns_exceeded', turn, timestamp: new Date().toISOString() });
435
+ await disposeSession(session);
436
+ }
437
+ context.emitAgentFailedEvent(record.id, record.error, Date.now() - record.startedAt);
438
+ return;
439
+ }
440
+ session.appendMessage({ type: 'llm_request', turn, messageCount: conversation.getMessagesForLLM().length, timestamp: new Date().toISOString() });
441
+ const pending = context.messageBus.getMessages(record.id);
442
+ for (const msg of pending) {
443
+ const kindLabel = msg.kind[0]!.toUpperCase() + msg.kind.slice(1);
444
+ conversation.addUserMessage(`[${kindLabel} from ${msg.from}]: ${msg.content}`);
445
+ }
446
+
447
+ if (context.featureFlagManager?.isEnabled('agent-context-window-awareness') ?? true) {
448
+ const modelDef = providerRegistry.listModels().find(
449
+ (m) =>
450
+ m.id === activeRoute.modelId ||
451
+ m.registryKey === activeRoute.modelId ||
452
+ m.id === activeRoute.requestedModelId ||
453
+ m.registryKey === activeRoute.requestedModelId,
454
+ ) ?? providerRegistry.getCurrentModel();
455
+ const contextWindow = context.providerRegistry.getContextWindowForModel(modelDef);
456
+ systemPrompt = applyContextWindowAwareness(
457
+ context,
458
+ record,
459
+ activeRoute.modelId,
460
+ contextWindow,
461
+ conversation,
462
+ systemPrompt,
463
+ toolTokens,
464
+ turn,
465
+ );
466
+ }
467
+
468
+ let response: Awaited<ReturnType<LLMProvider['chat']>>;
469
+ {
470
+ let networkAttempt = 0;
471
+ let rateLimitAttempt = 0;
472
+ let contextRetried = false;
473
+ // eslint-disable-next-line no-constant-condition
474
+ while (true) {
475
+ let streamAccumulated = '';
476
+ record.streamingContent = undefined;
477
+
478
+ const onDelta = (delta: StreamDelta) => {
479
+ if (delta.content) {
480
+ streamAccumulated += delta.content;
481
+ record.streamingContent = streamAccumulated;
482
+ const snippet = streamAccumulated.length > 100
483
+ ? '...' + streamAccumulated.slice(-97)
484
+ : streamAccumulated;
485
+ record.progress = snippet.replace(/\n/g, ' ').trim() || 'Streaming...';
486
+ }
487
+ context.emitStreamDelta(record.id, delta.content ?? '', streamAccumulated);
488
+ };
489
+
490
+ try {
491
+ response = await activeRoute.provider.chat({
492
+ model: activeRoute.modelId,
493
+ messages: conversation.getMessagesForLLM(),
494
+ tools: toolDefinitions.length > 0 ? toolDefinitions : undefined,
495
+ systemPrompt,
496
+ ...(record.reasoningEffort ? { reasoningEffort: record.reasoningEffort } : {}),
497
+ onDelta,
498
+ });
499
+ break;
500
+ } catch (chatErr) {
501
+ if (
502
+ isContextSizeExceededError(chatErr) &&
503
+ !contextRetried &&
504
+ (context.featureFlagManager?.isEnabled('agent-context-window-awareness') ?? true)
505
+ ) {
506
+ contextRetried = true;
507
+ logger.warn(
508
+ `[AgentOrchestrator] context-window awareness: context size exceeded on turn ${turn} - emergency compaction and retry`,
509
+ { agentId: record.id, error: chatErr instanceof Error ? chatErr.message : String(chatErr) },
510
+ );
511
+ record.progress = `Turn ${turn} · Context exceeded, compacting…`;
512
+ context.emitAgentProgress(record.id, record.progress);
513
+ context.emitOrchestrationProgress(record, record.progress);
514
+ const currentMessages = conversation.getMessagesForLLM();
515
+ const compacted = compactSmallWindow(
516
+ currentMessages,
517
+ Math.max(5, Math.floor(currentMessages.length / 3)),
518
+ );
519
+ conversation.replaceMessagesForLLM(compacted);
520
+ systemPrompt = buildLayeredOrchestratorSystemPrompt(record, 0, context);
521
+ } else if (fallbackRouteIndex < fallbackRoutes.length) {
522
+ const previousRoute = activeRoute;
523
+ activeRoute = fallbackRoutes[fallbackRouteIndex++]!;
524
+ const reason = chatErr instanceof Error ? chatErr.message : String(chatErr);
525
+ logger.warn('[AgentOrchestrator] switching to fallback model', {
526
+ agentId: record.id,
527
+ from: previousRoute.requestedModelId,
528
+ to: activeRoute.requestedModelId,
529
+ reason,
530
+ });
531
+ context.providerOptimizer?.recordFallbackTransition(previousRoute.requestedModelId, activeRoute.requestedModelId, reason);
532
+ record.model = activeRoute.requestedModelId;
533
+ record.provider = activeRoute.provider.name;
534
+ record.progress = `Model fallback → ${activeRoute.requestedModelId}`;
535
+ context.emitAgentProgress(record.id, record.progress);
536
+ context.emitOrchestrationProgress(record, record.progress);
537
+ } else if (isNetworkError(chatErr) && networkAttempt < NETWORK_RETRY_DELAYS_MS.length) {
538
+ const delayMs = NETWORK_RETRY_DELAYS_MS[networkAttempt]!;
539
+ const delaySec = Math.round(delayMs / 1000);
540
+ logger.warn(
541
+ `Agent ${record.id}: network error on turn ${turn}, retrying in ${delaySec}s (attempt ${networkAttempt + 1}/${NETWORK_RETRY_DELAYS_MS.length})`,
542
+ { error: chatErr instanceof Error ? chatErr.message : String(chatErr) },
543
+ );
544
+ record.progress = `Network error, retrying in ${delaySec}s…`;
545
+ context.emitAgentProgress(record.id, record.progress);
546
+ context.emitOrchestrationProgress(record, record.progress);
547
+ networkAttempt++;
548
+ await new Promise<void>((resolve) => setTimeout(resolve, delayMs));
549
+ if ((record as { status: string }).status === 'cancelled') {
550
+ throw new Error('Agent cancelled during network retry');
551
+ }
552
+ } else if (isRateLimitOrQuotaError(chatErr) && rateLimitAttempt < RATE_LIMIT_MAX_RETRIES) {
553
+ const delaySec = Math.round(RATE_LIMIT_RETRY_DELAY_MS / 1000);
554
+ logger.warn(
555
+ `Agent ${record.id}: rate limited on turn ${turn}, retrying in ${delaySec}s (attempt ${rateLimitAttempt + 1}/${RATE_LIMIT_MAX_RETRIES})`,
556
+ { error: chatErr instanceof Error ? chatErr.message : String(chatErr) },
557
+ );
558
+ record.progress = `Rate limited, retrying in ${delaySec}s…`;
559
+ context.emitAgentProgress(record.id, record.progress);
560
+ context.emitOrchestrationProgress(record, record.progress);
561
+ rateLimitAttempt++;
562
+ await new Promise<void>((resolve) => setTimeout(resolve, RATE_LIMIT_RETRY_DELAY_MS));
563
+ if ((record as { status: string }).status === 'cancelled') {
564
+ throw new Error('Agent cancelled during rate limit retry');
565
+ }
566
+ } else {
567
+ throw chatErr;
568
+ }
569
+ }
570
+ }
571
+ record.streamingContent = undefined;
572
+ record.progress = `Turn ${turn} · Thinking…`;
573
+ }
574
+
575
+ session.appendMessage({ type: 'llm_response', turn, contentLength: response.content.length, toolCallCount: response.toolCalls.length, usage: response.usage, timestamp: new Date().toISOString() });
576
+ record.usage = {
577
+ inputTokens: (record.usage?.inputTokens ?? 0) + response.usage.inputTokens,
578
+ outputTokens: (record.usage?.outputTokens ?? 0) + response.usage.outputTokens,
579
+ cacheReadTokens: (record.usage?.cacheReadTokens ?? 0) + (response.usage.cacheReadTokens ?? 0),
580
+ cacheWriteTokens: (record.usage?.cacheWriteTokens ?? 0) + (response.usage.cacheWriteTokens ?? 0),
581
+ ...(record.usage?.reasoningTokens !== undefined ? { reasoningTokens: record.usage.reasoningTokens } : {}),
582
+ llmCallCount: (record.usage?.llmCallCount ?? 0) + 1,
583
+ turnCount: (record.usage?.turnCount ?? 0) + 1,
584
+ reasoningSummaryCount: (record.usage?.reasoningSummaryCount ?? 0) + (response.reasoningSummary ? 1 : 0),
585
+ };
586
+
587
+ if (response.toolCalls.length > 0) {
588
+ conversation.addAssistantMessage(response.content, { toolCalls: response.toolCalls, usage: response.usage });
589
+ const results = await executeToolCalls(
590
+ response.toolCalls,
591
+ toolRegistry,
592
+ session,
593
+ turn,
594
+ record,
595
+ callHistory,
596
+ CALL_HISTORY_WINDOW,
597
+ context,
598
+ );
599
+ conversation.addToolResults(results);
600
+
601
+ const allFailed = results.length > 0 && results.every(r => r.success === false);
602
+ if (allFailed) {
603
+ const cbResult = circuitBreaker.recordAllFailed();
604
+ logger.warn(`Agent ${record.id}: consecutive all-error turn ${circuitBreaker.consecutiveErrors}`);
605
+ if (cbResult === 'break') {
606
+ conversation.addSystemMessage(
607
+ `CIRCUIT BREAKER: You have made ${circuitBreaker.consecutiveErrors} consecutive turns where ALL tool calls failed. ` +
608
+ `The agent loop is stopping to prevent an infinite failure cycle. ` +
609
+ `Report what you were trying to do and what errors you encountered.`,
610
+ );
611
+ record.status = 'failed';
612
+ record.error = `Circuit breaker tripped after ${circuitBreaker.consecutiveErrors} consecutive all-error turns`;
613
+ record.completedAt = Date.now();
614
+ continueLoop = false;
615
+ } else if (cbResult === 'warn') {
616
+ conversation.addSystemMessage(
617
+ `You have made ${circuitBreaker.consecutiveErrors} consecutive tool calls that ALL failed. ` +
618
+ `Stop attempting the same approach. Describe what you're trying to do and what's going wrong, ` +
619
+ `then try a completely different strategy.`,
620
+ );
621
+ }
622
+ } else if (results.length > 0) {
623
+ circuitBreaker.recordSuccess();
624
+ }
625
+
626
+ const sigCounts = new Map<string, { count: number; toolName: string }>();
627
+ for (const sig of callHistory) {
628
+ const name = sig.slice(0, sig.indexOf('::'));
629
+ const entry = sigCounts.get(sig);
630
+ if (entry) {
631
+ entry.count++;
632
+ } else {
633
+ sigCounts.set(sig, { count: 1, toolName: name });
634
+ }
635
+ }
636
+ let worstCount = 0;
637
+ let worstTool = '';
638
+ for (const [_sig, { count, toolName }] of sigCounts) {
639
+ if (count > worstCount) {
640
+ worstCount = count;
641
+ worstTool = toolName;
642
+ }
643
+ }
644
+ if (worstCount >= LOOP_USER_THRESHOLD) {
645
+ logger.warn(`Agent ${record.id}: loop detected — ${worstTool} called ${worstCount} times with identical args`);
646
+ conversation.addUserMessage(
647
+ `You are repeating the same tool call. ${worstTool} has been called ${worstCount} times with identical arguments and results. Do NOT call ${worstTool} with these arguments again. Identify what you were trying to accomplish and take a different action.`,
648
+ );
649
+ } else if (worstCount >= LOOP_SYSTEM_THRESHOLD) {
650
+ logger.warn(`Agent ${record.id}: possible loop — ${worstTool} called ${worstCount} times with identical args`);
651
+ conversation.addSystemMessage(
652
+ `You have already executed this exact call (${worstTool}) ${worstCount} times with identical arguments. The results from your previous calls are already in your conversation history. Review them and proceed to the next step.`,
653
+ );
654
+ }
655
+ record.progress = `Turn ${turn} · Thinking…`;
656
+ } else {
657
+ conversation.addAssistantMessage(response.content, { usage: response.usage });
658
+ record.fullOutput = response.content;
659
+ record.progress = response.content.slice(0, 200) || 'Done.';
660
+ continueLoop = false;
661
+ }
662
+ }
663
+
664
+ await finalizeAgentRun(context, record, session, preAgentProcessIds);
665
+ } catch (err) {
666
+ await handleAgentRunFailure(context, record, conversation, session, preAgentProcessIds, err);
667
+ }
668
+ }