@ouro.bot/cli 0.1.0-alpha.36 → 0.1.0-alpha.361

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 (326) hide show
  1. package/README.md +194 -184
  2. package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/agent.json +3 -2
  3. package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/SOUL.md +1 -1
  4. package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/the-serpent.md +1 -1
  5. package/changelog.json +2155 -0
  6. package/dist/arc/attention-types.js +8 -0
  7. package/dist/arc/cares.js +140 -0
  8. package/dist/arc/episodes.js +117 -0
  9. package/dist/arc/intentions.js +133 -0
  10. package/dist/arc/json-store.js +117 -0
  11. package/dist/arc/obligations.js +237 -0
  12. package/dist/arc/packets.js +193 -0
  13. package/dist/arc/presence.js +185 -0
  14. package/dist/arc/task-lifecycle.js +65 -0
  15. package/dist/heart/active-work.js +832 -0
  16. package/dist/heart/agent-entry.js +37 -2
  17. package/dist/heart/attachments/image-normalize.js +194 -0
  18. package/dist/heart/attachments/materialize.js +97 -0
  19. package/dist/heart/attachments/originals.js +88 -0
  20. package/dist/heart/attachments/render.js +29 -0
  21. package/dist/heart/attachments/sources/adapter.js +2 -0
  22. package/dist/heart/attachments/sources/bluebubbles.js +156 -0
  23. package/dist/heart/attachments/sources/cli-local-file.js +78 -0
  24. package/dist/heart/attachments/sources/index.js +16 -0
  25. package/dist/heart/attachments/store.js +103 -0
  26. package/dist/heart/attachments/types.js +93 -0
  27. package/dist/heart/auth/auth-flow.js +463 -0
  28. package/dist/heart/bridges/manager.js +358 -0
  29. package/dist/heart/bridges/state-machine.js +135 -0
  30. package/dist/heart/bridges/store.js +123 -0
  31. package/dist/heart/bundle-state.js +168 -0
  32. package/dist/heart/commitments.js +111 -0
  33. package/dist/heart/config-registry.js +304 -0
  34. package/dist/heart/config.js +53 -21
  35. package/dist/heart/core.js +743 -252
  36. package/dist/heart/cross-chat-delivery.js +131 -0
  37. package/dist/heart/daemon/agent-config-check.js +561 -0
  38. package/dist/heart/daemon/agent-discovery.js +79 -3
  39. package/dist/heart/daemon/agent-service.js +360 -0
  40. package/dist/heart/daemon/agentic-repair.js +185 -0
  41. package/dist/heart/daemon/bluebubbles-health-diagnostics.js +122 -0
  42. package/dist/heart/daemon/cadence.js +70 -0
  43. package/dist/heart/daemon/cli-defaults.js +591 -0
  44. package/dist/heart/daemon/cli-exec.js +2649 -0
  45. package/dist/heart/daemon/cli-help.js +306 -0
  46. package/dist/heart/daemon/cli-parse.js +913 -0
  47. package/dist/heart/daemon/cli-render-doctor.js +57 -0
  48. package/dist/heart/daemon/cli-render.js +560 -0
  49. package/dist/heart/daemon/cli-types.js +8 -0
  50. package/dist/heart/daemon/daemon-cli.js +30 -1171
  51. package/dist/heart/daemon/daemon-entry.js +358 -3
  52. package/dist/heart/daemon/daemon-health.js +141 -0
  53. package/dist/heart/daemon/daemon-runtime-sync.js +157 -12
  54. package/dist/heart/daemon/daemon-tombstone.js +236 -0
  55. package/dist/heart/daemon/daemon.js +757 -58
  56. package/dist/heart/daemon/doctor-types.js +8 -0
  57. package/dist/heart/daemon/doctor.js +465 -0
  58. package/dist/heart/daemon/health-monitor.js +79 -1
  59. package/dist/heart/daemon/hooks/agent-config-v2.js +33 -0
  60. package/dist/heart/daemon/hooks/bundle-meta.js +115 -1
  61. package/dist/heart/daemon/http-health-probe.js +80 -0
  62. package/dist/heart/daemon/inner-status.js +89 -0
  63. package/dist/heart/daemon/interactive-repair.js +91 -0
  64. package/dist/heart/daemon/launchd.js +46 -9
  65. package/dist/heart/daemon/log-tailer.js +82 -12
  66. package/dist/heart/daemon/logs-prune.js +105 -0
  67. package/dist/heart/daemon/message-router.js +17 -8
  68. package/dist/heart/daemon/os-cron-deps.js +134 -0
  69. package/dist/heart/daemon/ouro-bot-entry.js +1 -1
  70. package/dist/heart/daemon/process-manager.js +201 -0
  71. package/dist/heart/daemon/provider-discovery.js +140 -0
  72. package/dist/heart/daemon/pulse.js +475 -0
  73. package/dist/heart/daemon/run-hooks.js +2 -0
  74. package/dist/heart/daemon/runtime-logging.js +67 -16
  75. package/dist/heart/daemon/runtime-metadata.js +101 -0
  76. package/dist/heart/daemon/runtime-mode.js +67 -0
  77. package/dist/heart/daemon/safe-mode.js +161 -0
  78. package/dist/heart/daemon/sense-manager.js +72 -3
  79. package/dist/heart/daemon/session-id-resolver.js +131 -0
  80. package/dist/heart/daemon/skill-management-installer.js +94 -0
  81. package/dist/heart/daemon/socket-client.js +307 -0
  82. package/dist/heart/daemon/stale-bundle-prune.js +96 -0
  83. package/dist/heart/daemon/startup-tui.js +237 -0
  84. package/dist/heart/daemon/task-scheduler.js +3 -25
  85. package/dist/heart/daemon/thoughts.js +510 -0
  86. package/dist/heart/daemon/up-progress.js +135 -0
  87. package/dist/heart/delegation.js +62 -0
  88. package/dist/heart/habits/habit-migration.js +181 -0
  89. package/dist/heart/habits/habit-parser.js +140 -0
  90. package/dist/heart/habits/habit-scheduler.js +371 -0
  91. package/dist/heart/{daemon → hatch}/hatch-flow.js +52 -120
  92. package/dist/heart/{daemon → hatch}/hatch-specialist.js +3 -3
  93. package/dist/heart/{daemon → hatch}/specialist-prompt.js +10 -7
  94. package/dist/heart/{daemon → hatch}/specialist-tools.js +56 -10
  95. package/dist/heart/identity.js +154 -59
  96. package/dist/heart/kept-notes.js +357 -0
  97. package/dist/heart/kicks.js +2 -20
  98. package/dist/heart/machine-identity.js +161 -0
  99. package/dist/heart/mcp/mcp-server.js +653 -0
  100. package/dist/heart/migrate-config.js +127 -0
  101. package/dist/heart/model-capabilities.js +59 -0
  102. package/dist/heart/outlook/outlook-http-hooks.js +64 -0
  103. package/dist/heart/outlook/outlook-http-response.js +7 -0
  104. package/dist/heart/outlook/outlook-http-routes.js +232 -0
  105. package/dist/heart/outlook/outlook-http-static.js +99 -0
  106. package/dist/heart/outlook/outlook-http-transport.js +116 -0
  107. package/dist/heart/outlook/outlook-http.js +99 -0
  108. package/dist/heart/outlook/outlook-read.js +28 -0
  109. package/dist/heart/outlook/outlook-types.js +27 -0
  110. package/dist/heart/outlook/outlook-view.js +195 -0
  111. package/dist/heart/outlook/readers/agent-machine.js +359 -0
  112. package/dist/heart/outlook/readers/continuity-readers.js +332 -0
  113. package/dist/heart/outlook/readers/runtime-readers.js +660 -0
  114. package/dist/heart/outlook/readers/sessions.js +232 -0
  115. package/dist/heart/outlook/readers/shared.js +111 -0
  116. package/dist/heart/progress-story.js +42 -0
  117. package/dist/heart/provider-attempt.js +133 -0
  118. package/dist/heart/provider-binding-resolver.js +240 -0
  119. package/dist/heart/provider-credential-pool.js +395 -0
  120. package/dist/heart/provider-failover.js +274 -0
  121. package/dist/heart/provider-models.js +81 -0
  122. package/dist/heart/provider-ping.js +227 -0
  123. package/dist/heart/provider-state.js +208 -0
  124. package/dist/heart/provider-visibility.js +183 -0
  125. package/dist/heart/providers/anthropic-token.js +163 -0
  126. package/dist/heart/providers/anthropic.js +177 -50
  127. package/dist/heart/providers/azure.js +102 -11
  128. package/dist/heart/providers/error-classification.js +63 -0
  129. package/dist/heart/providers/github-copilot.js +145 -0
  130. package/dist/heart/providers/minimax-vlm.js +189 -0
  131. package/dist/heart/providers/minimax.js +28 -6
  132. package/dist/heart/providers/openai-codex.js +38 -23
  133. package/dist/heart/session-activity.js +190 -0
  134. package/dist/heart/session-events.js +855 -0
  135. package/dist/heart/session-transcript.js +167 -0
  136. package/dist/heart/start-of-turn-packet.js +345 -0
  137. package/dist/heart/streaming.js +36 -27
  138. package/dist/heart/sync.js +332 -0
  139. package/dist/heart/target-resolution.js +127 -0
  140. package/dist/heart/tempo.js +93 -0
  141. package/dist/heart/temporal-view.js +41 -0
  142. package/dist/heart/tool-activity-callbacks.js +36 -0
  143. package/dist/heart/tool-description.js +135 -0
  144. package/dist/heart/tool-friction.js +55 -0
  145. package/dist/heart/tool-loop.js +200 -0
  146. package/dist/heart/turn-context.js +362 -0
  147. package/dist/heart/turn-coordinator.js +28 -0
  148. package/dist/heart/{daemon → versioning}/ouro-bot-global-installer.js +1 -1
  149. package/dist/heart/{daemon → versioning}/ouro-bot-wrapper.js +1 -1
  150. package/dist/heart/versioning/ouro-path-installer.js +296 -0
  151. package/dist/heart/versioning/ouro-version-manager.js +295 -0
  152. package/dist/heart/{daemon → versioning}/staged-restart.js +40 -8
  153. package/dist/heart/{daemon → versioning}/update-checker.js +12 -2
  154. package/dist/heart/{daemon → versioning}/update-hooks.js +63 -59
  155. package/dist/mind/bundle-manifest.js +7 -1
  156. package/dist/mind/context.js +141 -94
  157. package/dist/mind/diary-integrity.js +60 -0
  158. package/dist/mind/{memory.js → diary.js} +84 -96
  159. package/dist/mind/embedding-provider.js +60 -0
  160. package/dist/mind/file-state.js +179 -0
  161. package/dist/mind/first-impressions.js +14 -1
  162. package/dist/mind/friends/channel.js +56 -0
  163. package/dist/mind/friends/group-context.js +144 -0
  164. package/dist/mind/friends/resolver.js +38 -1
  165. package/dist/mind/friends/store-file.js +58 -3
  166. package/dist/mind/friends/trust-explanation.js +74 -0
  167. package/dist/mind/friends/types.js +9 -1
  168. package/dist/mind/journal-index.js +161 -0
  169. package/dist/mind/note-search.js +268 -0
  170. package/dist/mind/obligation-steering.js +221 -0
  171. package/dist/mind/pending.js +74 -7
  172. package/dist/mind/prompt.js +1013 -112
  173. package/dist/mind/provenance-trust.js +26 -0
  174. package/dist/mind/scrutiny.js +173 -0
  175. package/dist/mind/token-estimate.js +8 -12
  176. package/dist/nerves/cli-logging.js +7 -1
  177. package/dist/nerves/coverage/audit-rules.js +15 -6
  178. package/dist/nerves/coverage/audit.js +28 -2
  179. package/dist/nerves/coverage/cli.js +1 -1
  180. package/dist/nerves/coverage/file-completeness.js +83 -5
  181. package/dist/nerves/coverage/run-artifacts.js +1 -1
  182. package/dist/nerves/event-buffer.js +111 -0
  183. package/dist/nerves/index.js +224 -4
  184. package/dist/nerves/observation.js +20 -0
  185. package/dist/nerves/redact.js +79 -0
  186. package/dist/nerves/runtime.js +5 -1
  187. package/dist/outlook-ui/assets/index-LwChZTgL.css +1 -0
  188. package/dist/outlook-ui/assets/index-xTdv64BV.js +61 -0
  189. package/dist/outlook-ui/index.html +15 -0
  190. package/dist/repertoire/ado-client.js +15 -56
  191. package/dist/repertoire/ado-semantic.js +11 -10
  192. package/dist/repertoire/api-client.js +97 -0
  193. package/dist/repertoire/bitwarden-store.js +319 -0
  194. package/dist/repertoire/bundle-templates.js +72 -0
  195. package/dist/repertoire/bw-installer.js +79 -0
  196. package/dist/repertoire/coding/codex-jsonl.js +64 -0
  197. package/dist/repertoire/coding/context-pack.js +330 -0
  198. package/dist/repertoire/coding/feedback.js +197 -30
  199. package/dist/repertoire/coding/manager.js +158 -9
  200. package/dist/repertoire/coding/spawner.js +55 -9
  201. package/dist/repertoire/coding/tools.js +170 -7
  202. package/dist/repertoire/commerce-errors.js +109 -0
  203. package/dist/repertoire/commerce-self-test.js +156 -0
  204. package/dist/repertoire/credential-access.js +527 -0
  205. package/dist/repertoire/duffel-client.js +185 -0
  206. package/dist/repertoire/github-client.js +14 -55
  207. package/dist/repertoire/graph-client.js +11 -52
  208. package/dist/repertoire/guardrails.js +375 -0
  209. package/dist/repertoire/mcp-client.js +255 -0
  210. package/dist/repertoire/mcp-manager.js +305 -0
  211. package/dist/repertoire/mcp-tools.js +63 -0
  212. package/dist/repertoire/shell-sessions.js +133 -0
  213. package/dist/repertoire/skills.js +15 -24
  214. package/dist/repertoire/stripe-client.js +131 -0
  215. package/dist/repertoire/tasks/board.js +43 -5
  216. package/dist/repertoire/tasks/fix.js +182 -0
  217. package/dist/repertoire/tasks/index.js +28 -10
  218. package/dist/repertoire/tasks/lifecycle.js +2 -2
  219. package/dist/repertoire/tasks/parser.js +3 -2
  220. package/dist/repertoire/tasks/scanner.js +194 -37
  221. package/dist/repertoire/tasks/transitions.js +16 -79
  222. package/dist/repertoire/tool-results.js +29 -0
  223. package/dist/repertoire/tools-attachments.js +316 -0
  224. package/dist/repertoire/tools-base.js +45 -771
  225. package/dist/repertoire/tools-bluebubbles.js +1 -0
  226. package/dist/repertoire/tools-bridge.js +141 -0
  227. package/dist/repertoire/tools-bundle.js +984 -0
  228. package/dist/repertoire/tools-config.js +185 -0
  229. package/dist/repertoire/tools-continuity.js +248 -0
  230. package/dist/repertoire/tools-credential.js +182 -0
  231. package/dist/repertoire/tools-files.js +342 -0
  232. package/dist/repertoire/tools-flight.js +224 -0
  233. package/dist/repertoire/tools-flow.js +105 -0
  234. package/dist/repertoire/tools-github.js +1 -7
  235. package/dist/repertoire/tools-notes.js +376 -0
  236. package/dist/repertoire/tools-session.js +739 -0
  237. package/dist/repertoire/tools-shell.js +120 -0
  238. package/dist/repertoire/tools-stripe.js +180 -0
  239. package/dist/repertoire/tools-surface.js +243 -0
  240. package/dist/repertoire/tools-teams.js +12 -62
  241. package/dist/repertoire/tools-travel.js +125 -0
  242. package/dist/repertoire/tools-user-profile.js +144 -0
  243. package/dist/repertoire/tools-vault.js +110 -0
  244. package/dist/repertoire/tools.js +144 -138
  245. package/dist/repertoire/travel-api-client.js +360 -0
  246. package/dist/repertoire/user-profile.js +118 -0
  247. package/dist/repertoire/vault-setup.js +241 -0
  248. package/dist/scripts/claude-code-hook.js +41 -0
  249. package/dist/scripts/claude-code-stop-hook.js +47 -0
  250. package/dist/senses/attention-queue.js +116 -0
  251. package/dist/senses/bluebubbles/attachment-cache.js +53 -0
  252. package/dist/senses/bluebubbles/attachment-download.js +137 -0
  253. package/dist/senses/{bluebubbles-client.js → bluebubbles/client.js} +225 -9
  254. package/dist/senses/bluebubbles/entry.js +13 -0
  255. package/dist/senses/bluebubbles/inbound-log.js +113 -0
  256. package/dist/senses/bluebubbles/index.js +1620 -0
  257. package/dist/senses/{bluebubbles-media.js → bluebubbles/media.js} +121 -70
  258. package/dist/senses/{bluebubbles-model.js → bluebubbles/model.js} +43 -12
  259. package/dist/senses/{bluebubbles-mutation-log.js → bluebubbles/mutation-log.js} +46 -6
  260. package/dist/senses/bluebubbles/replay.js +129 -0
  261. package/dist/senses/bluebubbles/runtime-state.js +109 -0
  262. package/dist/senses/{bluebubbles-session-cleanup.js → bluebubbles/session-cleanup.js} +1 -1
  263. package/dist/senses/cli/bracketed-paste.js +82 -0
  264. package/dist/senses/cli/image-paste.js +287 -0
  265. package/dist/senses/cli/image-ref-navigation.js +75 -0
  266. package/dist/senses/cli/ink-app.js +156 -0
  267. package/dist/senses/cli/inline-diff.js +64 -0
  268. package/dist/senses/cli/input-keys.js +174 -0
  269. package/dist/senses/cli/kill-ring.js +86 -0
  270. package/dist/senses/cli/message-list.js +51 -0
  271. package/dist/senses/cli/ouro-tui.js +605 -0
  272. package/dist/senses/cli/spinner-imperative.js +135 -0
  273. package/dist/senses/cli/spinner.js +101 -0
  274. package/dist/senses/cli/status-line.js +60 -0
  275. package/dist/senses/cli/streaming-markdown.js +526 -0
  276. package/dist/senses/cli/tool-display.js +83 -0
  277. package/dist/senses/cli/tool-render.js +85 -0
  278. package/dist/senses/cli/tui-store.js +240 -0
  279. package/dist/senses/cli/virtual-list.js +35 -0
  280. package/dist/senses/cli-entry.js +1 -1
  281. package/dist/senses/cli-layout.js +187 -0
  282. package/dist/senses/cli.js +587 -249
  283. package/dist/senses/commands.js +66 -3
  284. package/dist/senses/continuity.js +94 -0
  285. package/dist/senses/habit-turn-message.js +108 -0
  286. package/dist/senses/inner-dialog-worker.js +112 -19
  287. package/dist/senses/inner-dialog.js +636 -86
  288. package/dist/senses/pipeline.js +603 -0
  289. package/dist/senses/proactive-content-guard.js +51 -0
  290. package/dist/senses/shared-turn.js +205 -0
  291. package/dist/senses/surface-tool.js +68 -0
  292. package/dist/senses/teams.js +693 -160
  293. package/dist/senses/trust-gate.js +112 -2
  294. package/package.json +29 -7
  295. package/skills/agent-commerce.md +106 -0
  296. package/skills/browser-navigation.md +110 -0
  297. package/skills/commerce-setup-guide.md +116 -0
  298. package/skills/commerce-setup.md +84 -0
  299. package/skills/configure-dev-tools.md +81 -0
  300. package/skills/travel-planning.md +138 -0
  301. package/dist/heart/daemon/ouro-path-installer.js +0 -178
  302. package/dist/heart/daemon/subagent-installer.js +0 -134
  303. package/dist/mind/associative-recall.js +0 -197
  304. package/dist/senses/bluebubbles-entry.js +0 -11
  305. package/dist/senses/bluebubbles.js +0 -558
  306. package/dist/senses/debug-activity.js +0 -127
  307. package/subagents/README.md +0 -73
  308. package/subagents/work-doer.md +0 -235
  309. package/subagents/work-merger.md +0 -618
  310. package/subagents/work-planner.md +0 -382
  311. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/basilisk.md +0 -0
  312. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/jafar.md +0 -0
  313. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/jormungandr.md +0 -0
  314. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/kaa.md +0 -0
  315. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/medusa.md +0 -0
  316. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/monty.md +0 -0
  317. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/nagini.md +0 -0
  318. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/ouroboros.md +0 -0
  319. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/python.md +0 -0
  320. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/quetzalcoatl.md +0 -0
  321. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/sir-hiss.md +0 -0
  322. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/the-snake.md +0 -0
  323. /package/dist/heart/{daemon → hatch}/hatch-animation.js +0 -0
  324. /package/dist/heart/{daemon → hatch}/specialist-orchestrator.js +0 -0
  325. /package/dist/heart/{daemon → versioning}/ouro-uti.js +0 -0
  326. /package/dist/heart/{daemon → versioning}/wrapper-publish-guard.js +0 -0
@@ -1,97 +1,122 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.hasToolIntent = exports.buildSystem = exports.toResponsesTools = exports.toResponsesInput = exports.streamResponsesApi = exports.streamChatCompletion = exports.getToolsForChannel = exports.summarizeArgs = exports.execTool = exports.tools = void 0;
4
3
  exports.createProviderRegistry = createProviderRegistry;
5
4
  exports.resetProviderRuntime = resetProviderRuntime;
6
5
  exports.getModel = getModel;
7
6
  exports.getProvider = getProvider;
8
7
  exports.createSummarize = createSummarize;
9
8
  exports.getProviderDisplayLabel = getProviderDisplayLabel;
9
+ exports.isExternalStateQuery = isExternalStateQuery;
10
+ exports.getSettleRetryError = getSettleRetryError;
10
11
  exports.stripLastToolCalls = stripLastToolCalls;
11
12
  exports.repairOrphanedToolCalls = repairOrphanedToolCalls;
12
- exports.isTransientError = isTransientError;
13
- exports.classifyTransientError = classifyTransientError;
14
13
  exports.runAgent = runAgent;
15
14
  const config_1 = require("./config");
16
15
  const identity_1 = require("./identity");
17
16
  const tools_1 = require("../repertoire/tools");
18
17
  const channel_1 = require("../mind/friends/channel");
19
- // Kick detection preserved but disabled — see comment in agent loop below.
20
- // import { detectKick } from "./kicks";
21
- // import type { KickReason } from "./kicks";
18
+ const tools_2 = require("../repertoire/tools");
22
19
  const runtime_1 = require("../nerves/runtime");
23
20
  const context_1 = require("../mind/context");
24
21
  const prompt_1 = require("../mind/prompt");
25
- const associative_recall_1 = require("../mind/associative-recall");
22
+ const kept_notes_1 = require("./kept-notes");
26
23
  const anthropic_1 = require("./providers/anthropic");
27
24
  const azure_1 = require("./providers/azure");
28
25
  const minimax_1 = require("./providers/minimax");
29
26
  const openai_codex_1 = require("./providers/openai-codex");
30
- let _providerRuntime = null;
27
+ const github_copilot_1 = require("./providers/github-copilot");
28
+ const identity_2 = require("./identity");
29
+ const socket_client_1 = require("./daemon/socket-client");
30
+ const obligations_1 = require("../arc/obligations");
31
+ const tool_loop_1 = require("./tool-loop");
32
+ const packets_1 = require("../arc/packets");
33
+ const tool_friction_1 = require("./tool-friction");
34
+ const provider_models_1 = require("./provider-models");
35
+ const provider_attempt_1 = require("./provider-attempt");
36
+ const _providerRuntimes = {
37
+ human: null,
38
+ agent: null,
39
+ };
40
+ function getProviderRuntimeFingerprint(facing) {
41
+ const config = (0, identity_1.loadAgentConfig)();
42
+ const facingConfig = facing === "human" ? config.humanFacing : config.agentFacing;
43
+ const provider = facingConfig.provider;
44
+ const model = facingConfig.model;
45
+ const providerConfig = (0, config_1.getProviderConfig)(provider);
46
+ return JSON.stringify({ provider, model, ...providerConfig });
47
+ }
31
48
  function createProviderRegistry() {
32
49
  const factories = {
33
50
  azure: azure_1.createAzureProviderRuntime,
34
51
  anthropic: anthropic_1.createAnthropicProviderRuntime,
35
52
  minimax: minimax_1.createMinimaxProviderRuntime,
36
53
  "openai-codex": openai_codex_1.createOpenAICodexProviderRuntime,
54
+ "github-copilot": github_copilot_1.createGithubCopilotProviderRuntime,
37
55
  };
38
56
  return {
39
- resolve() {
40
- const provider = (0, identity_1.loadAgentConfig)().provider;
41
- return factories[provider]();
57
+ resolve(provider, model) {
58
+ const resolvedProvider = provider ?? (0, identity_1.loadAgentConfig)().humanFacing.provider;
59
+ const resolvedModel = model ?? (0, identity_1.loadAgentConfig)().humanFacing.model;
60
+ return factories[resolvedProvider](resolvedModel);
42
61
  },
43
62
  };
44
63
  }
45
- function getProviderRuntime() {
46
- if (!_providerRuntime) {
47
- try {
48
- _providerRuntime = createProviderRegistry().resolve();
49
- }
50
- catch (error) {
51
- const msg = error instanceof Error ? error.message : String(error);
52
- (0, runtime_1.emitNervesEvent)({
53
- level: "error",
54
- event: "engine.provider_init_error",
55
- component: "engine",
56
- message: msg,
57
- meta: {},
58
- });
59
- // eslint-disable-next-line no-console -- pre-boot guard: provider init failure
60
- console.error(`\n[fatal] ${msg}\n`);
61
- process.exit(1);
62
- throw new Error("unreachable");
63
- }
64
- if (!_providerRuntime) {
65
- (0, runtime_1.emitNervesEvent)({
66
- level: "error",
67
- event: "engine.provider_init_error",
68
- component: "engine",
69
- message: "provider runtime could not be initialized.",
70
- meta: {},
71
- });
72
- process.exit(1);
73
- throw new Error("unreachable");
64
+ function getProviderRuntime(facing = "human") {
65
+ try {
66
+ const fingerprint = getProviderRuntimeFingerprint(facing);
67
+ const cached = _providerRuntimes[facing];
68
+ if (!cached || cached.fingerprint !== fingerprint) {
69
+ const config = (0, identity_1.loadAgentConfig)();
70
+ const facingConfig = facing === "human" ? config.humanFacing : config.agentFacing;
71
+ const runtime = createProviderRegistry().resolve(facingConfig.provider, facingConfig.model);
72
+ _providerRuntimes[facing] = runtime ? { fingerprint, runtime } : null;
74
73
  }
75
74
  }
76
- return _providerRuntime;
75
+ catch (error) {
76
+ const msg = error instanceof Error ? error.message : String(error);
77
+ (0, runtime_1.emitNervesEvent)({
78
+ level: "error",
79
+ event: "engine.provider_init_error",
80
+ component: "engine",
81
+ message: msg,
82
+ meta: {},
83
+ });
84
+ // eslint-disable-next-line no-console -- pre-boot guard: provider init failure
85
+ console.error(`\n[fatal] ${msg}\n`);
86
+ process.exit(1);
87
+ throw new Error("unreachable");
88
+ }
89
+ if (!_providerRuntimes[facing]) {
90
+ (0, runtime_1.emitNervesEvent)({
91
+ level: "error",
92
+ event: "engine.provider_init_error",
93
+ component: "engine",
94
+ message: "provider runtime could not be initialized.",
95
+ meta: {},
96
+ });
97
+ process.exit(1);
98
+ throw new Error("unreachable");
99
+ }
100
+ return _providerRuntimes[facing].runtime;
77
101
  }
78
102
  /**
79
- * Clear the cached provider runtime so the next call to getProviderRuntime()
80
- * re-creates it from current config. Used by the adoption specialist to
81
- * switch provider context without restarting the process.
103
+ * Clear the cached provider runtime so the next access re-creates it from
104
+ * current config. Runtime access also auto-refreshes when the selected
105
+ * provider fingerprint changes on disk.
82
106
  */
83
107
  function resetProviderRuntime() {
84
- _providerRuntime = null;
108
+ _providerRuntimes.human = null;
109
+ _providerRuntimes.agent = null;
85
110
  }
86
- function getModel() {
87
- return getProviderRuntime().model;
111
+ function getModel(facing = "human") {
112
+ return getProviderRuntime(facing).model;
88
113
  }
89
- function getProvider() {
90
- return getProviderRuntime().id;
114
+ function getProvider(facing = "human") {
115
+ return getProviderRuntime(facing).id;
91
116
  }
92
- function createSummarize() {
117
+ function createSummarize(facing = "human") {
93
118
  return async (transcript, instruction) => {
94
- const runtime = getProviderRuntime();
119
+ const runtime = getProviderRuntime(facing);
95
120
  const client = runtime.client;
96
121
  const response = await client.chat.completions.create({
97
122
  model: runtime.model,
@@ -104,34 +129,138 @@ function createSummarize() {
104
129
  return response.choices?.[0]?.message?.content ?? transcript;
105
130
  };
106
131
  }
107
- function getProviderDisplayLabel() {
108
- const model = getModel();
132
+ function getProviderDisplayLabel(facing = "human") {
133
+ const config = (0, identity_1.loadAgentConfig)();
134
+ const facingConfig = facing === "human" ? config.humanFacing : config.agentFacing;
135
+ const provider = facingConfig.provider;
136
+ const model = facingConfig.model || "unknown";
109
137
  const providerLabelBuilders = {
110
- azure: () => `azure openai (${(0, config_1.getAzureConfig)().deployment || "default"}, model: ${model})`,
138
+ azure: () => {
139
+ const azureCfg = (0, config_1.getAzureConfig)();
140
+ return `azure openai (${azureCfg.deployment || "default"}, model: ${model})`;
141
+ },
111
142
  anthropic: () => `anthropic (${model})`,
112
143
  minimax: () => `minimax (${model})`,
113
144
  "openai-codex": () => `openai codex (${model})`,
145
+ /* v8 ignore next -- branch: tested via display label unit test @preserve */
146
+ "github-copilot": () => `github copilot (${model})`,
147
+ };
148
+ return providerLabelBuilders[provider]();
149
+ }
150
+ // Sole-call tools must be the only tool call in a turn. When they appear
151
+ // alongside other tools, the sole-call tool is rejected with this message.
152
+ const SOLE_CALL_REJECTION = {
153
+ settle: "rejected: settle must be the only tool call. finish your work first, then call settle alone.",
154
+ observe: "rejected: observe must be the only tool call. call observe alone when you want to stay silent.",
155
+ rest: "rejected: rest must be the only tool call. finish your work first, then call rest alone.",
156
+ };
157
+ function parseSettlePayload(argumentsText) {
158
+ try {
159
+ const parsed = JSON.parse(argumentsText);
160
+ if (typeof parsed === "string") {
161
+ return { answer: parsed };
162
+ }
163
+ if (!parsed || typeof parsed !== "object") {
164
+ return {};
165
+ }
166
+ const answer = typeof parsed.answer === "string" ? parsed.answer : undefined;
167
+ const rawIntent = parsed.intent;
168
+ const intent = rawIntent === "complete" || rawIntent === "blocked" || rawIntent === "direct_reply"
169
+ ? rawIntent
170
+ : undefined;
171
+ return { answer, intent };
172
+ }
173
+ catch {
174
+ return {};
175
+ }
176
+ }
177
+ function parsePonderPayload(argumentsText) {
178
+ try {
179
+ const parsed = JSON.parse(argumentsText);
180
+ return parsed && typeof parsed === "object" ? parsed : {};
181
+ }
182
+ catch {
183
+ return {};
184
+ }
185
+ }
186
+ function parseSuccessCriteria(raw) {
187
+ if (typeof raw !== "string")
188
+ return null;
189
+ const criteria = raw
190
+ .split("\n")
191
+ .map((line) => line.replace(/^\s*[-*]\s*/, "").trim())
192
+ .filter((line) => line.length > 0);
193
+ return criteria.length > 0 ? criteria : null;
194
+ }
195
+ function parsePacketPayload(raw) {
196
+ if (typeof raw !== "string")
197
+ return null;
198
+ try {
199
+ const parsed = JSON.parse(raw);
200
+ return parsed && typeof parsed === "object" && !Array.isArray(parsed)
201
+ ? parsed
202
+ : null;
203
+ }
204
+ catch {
205
+ return null;
206
+ }
207
+ }
208
+ function normalizeLegacyPonderArgs(parsed) {
209
+ if (typeof parsed.thought !== "string" || parsed.thought.trim().length === 0) {
210
+ return parsed;
211
+ }
212
+ return {
213
+ action: "create",
214
+ kind: "reflection",
215
+ objective: parsed.thought.trim(),
216
+ summary: typeof parsed.say === "string" ? parsed.say.trim() : "",
217
+ success_criteria: "- preserve the thread for later work",
218
+ payload_json: "{}",
114
219
  };
115
- return providerLabelBuilders[getProvider()]();
116
220
  }
117
- // Re-export tools, execTool, summarizeArgs from ./tools for backward compat
118
- var tools_2 = require("../repertoire/tools");
119
- Object.defineProperty(exports, "tools", { enumerable: true, get: function () { return tools_2.tools; } });
120
- Object.defineProperty(exports, "execTool", { enumerable: true, get: function () { return tools_2.execTool; } });
121
- Object.defineProperty(exports, "summarizeArgs", { enumerable: true, get: function () { return tools_2.summarizeArgs; } });
122
- Object.defineProperty(exports, "getToolsForChannel", { enumerable: true, get: function () { return tools_2.getToolsForChannel; } });
123
- // Re-export streaming functions for backward compat
124
- var streaming_1 = require("./streaming");
125
- Object.defineProperty(exports, "streamChatCompletion", { enumerable: true, get: function () { return streaming_1.streamChatCompletion; } });
126
- Object.defineProperty(exports, "streamResponsesApi", { enumerable: true, get: function () { return streaming_1.streamResponsesApi; } });
127
- Object.defineProperty(exports, "toResponsesInput", { enumerable: true, get: function () { return streaming_1.toResponsesInput; } });
128
- Object.defineProperty(exports, "toResponsesTools", { enumerable: true, get: function () { return streaming_1.toResponsesTools; } });
129
- // Re-export prompt functions for backward compat
130
- var prompt_2 = require("../mind/prompt");
131
- Object.defineProperty(exports, "buildSystem", { enumerable: true, get: function () { return prompt_2.buildSystem; } });
132
- // Re-export kick utilities for backward compat
133
- var kicks_1 = require("./kicks");
134
- Object.defineProperty(exports, "hasToolIntent", { enumerable: true, get: function () { return kicks_1.hasToolIntent; } });
221
+ function buildPonderResult(packet, action, returnObligationId) {
222
+ return JSON.stringify({
223
+ ok: true,
224
+ packet_id: packet.id,
225
+ action,
226
+ status: packet.status,
227
+ return_obligation_id: returnObligationId,
228
+ }, null, 2);
229
+ }
230
+ /** Returns true when a tool call queries external state (GitHub, npm registry). */
231
+ function isExternalStateQuery(toolName, args) {
232
+ if (toolName !== "shell")
233
+ return false;
234
+ const cmd = String(args.command ?? "");
235
+ return /\bgh\s+(pr|run|api|issue)\b/.test(cmd) || /\bnpm\s+(view|info|show)\b/.test(cmd);
236
+ }
237
+ function getSettleRetryError(mustResolveBeforeHandoff, intent, sawSteeringFollowUp, _delegationDecision, sawSendMessageSelf, sawPonder, _sawQuerySession, currentObligation, innerJob, sawExternalStateQuery) {
238
+ // Delegation adherence removed: the delegation decision is surfaced in the
239
+ // system prompt as a suggestion. Hard-gating settle caused infinite
240
+ // rejection loops where the agent couldn't respond to the user at all.
241
+ // The agent is free to follow or ignore the delegation hint.
242
+ // 2. Pending obligation not addressed
243
+ if (innerJob?.obligationStatus === "pending" && !sawSendMessageSelf && !sawPonder) {
244
+ return "you're still holding something from an earlier conversation -- someone is waiting for your answer. finish the thought first, or ponder to keep working on it privately.";
245
+ }
246
+ // 3. mustResolveBeforeHandoff + missing intent
247
+ if (mustResolveBeforeHandoff && !intent) {
248
+ return "your settle is missing required intent. when you must keep going until done or blocked, call settle again with answer plus intent=complete, blocked, or direct_reply.";
249
+ }
250
+ // 4. mustResolveBeforeHandoff + direct_reply without follow-up
251
+ if (mustResolveBeforeHandoff && intent === "direct_reply" && !sawSteeringFollowUp) {
252
+ return "your settle used intent=direct_reply without a newer steering follow-up. continue the unresolved work, or call settle again with intent=complete or blocked when appropriate.";
253
+ }
254
+ // 5. mustResolveBeforeHandoff + complete while a live return loop is still active
255
+ if (mustResolveBeforeHandoff && intent === "complete" && currentObligation && !sawSteeringFollowUp) {
256
+ return "you still owe the live session a visible return on this work. don't end the turn yet — continue until you've brought back the external-state update, or use intent=blocked with the concrete blocker.";
257
+ }
258
+ // 6. External-state grounding: obligation + complete requires fresh external verification
259
+ if (intent === "complete" && currentObligation && !sawExternalStateQuery && !sawSteeringFollowUp) {
260
+ return "you're claiming this work is complete, but the external state hasn't been verified this turn. ground your claim with a fresh check (gh pr view, npm view, gh run view, etc.) before calling settle.";
261
+ }
262
+ return null;
263
+ }
135
264
  function upsertSystemPrompt(messages, systemText) {
136
265
  const systemMessage = { role: "system", content: systemText };
137
266
  if (messages[0]?.role === "system") {
@@ -237,49 +366,67 @@ function isContextOverflow(err) {
237
366
  return true;
238
367
  return false;
239
368
  }
240
- // Detect transient network errors worth retrying
241
- function isTransientError(err) {
242
- if (!(err instanceof Error))
243
- return false;
244
- const msg = err.message || "";
245
- const code = err.code || "";
246
- // Node.js network error codes
247
- if (["ECONNRESET", "ECONNREFUSED", "ENOTFOUND", "ETIMEDOUT", "EPIPE",
248
- "EAI_AGAIN", "EHOSTUNREACH", "ENETUNREACH", "ECONNABORTED"].includes(code))
249
- return true;
250
- // OpenAI SDK / fetch errors
251
- if (msg.includes("fetch failed"))
252
- return true;
253
- if (msg.includes("network") && !msg.includes("context"))
254
- return true;
255
- if (msg.includes("ECONNRESET") || msg.includes("ETIMEDOUT"))
256
- return true;
257
- if (msg.includes("socket hang up"))
258
- return true;
259
- if (msg.includes("getaddrinfo"))
260
- return true;
261
- // HTTP 429 / 500 / 502 / 503 / 504
262
- const status = err.status;
263
- if (status === 429 || status === 500 || status === 502 || status === 503 || status === 504)
264
- return true;
265
- return false;
369
+ const RETRY_LABELS = {
370
+ "auth-failure": "auth error",
371
+ "usage-limit": "usage limit",
372
+ "rate-limit": "rate limited",
373
+ "server-error": "server error",
374
+ "network-error": "network error",
375
+ "unknown": "error",
376
+ };
377
+ function waitForProviderRetry(delayMs, signal) {
378
+ if (!signal) {
379
+ return new Promise((resolve) => {
380
+ setTimeout(resolve, delayMs);
381
+ });
382
+ }
383
+ return new Promise((resolve, reject) => {
384
+ let timer;
385
+ const onAbort = () => {
386
+ clearTimeout(timer);
387
+ reject(new provider_attempt_1.ProviderAttemptAbortError());
388
+ };
389
+ timer = setTimeout(() => {
390
+ signal.removeEventListener("abort", onAbort);
391
+ resolve();
392
+ }, delayMs);
393
+ if (signal.aborted) {
394
+ onAbort();
395
+ return;
396
+ }
397
+ signal.addEventListener("abort", onAbort, { once: true });
398
+ });
266
399
  }
267
- function classifyTransientError(err) {
268
- if (!(err instanceof Error))
269
- return "unknown error";
270
- const status = err.status;
271
- if (status === 429)
272
- return "rate limited";
273
- if (status === 401 || status === 403)
274
- return "auth error";
275
- if (status && status >= 500)
276
- return "server error";
277
- return "network error";
400
+ function buildAuthFailureGuidance(provider, model, agentName, detail) {
401
+ const mismatch = (0, provider_models_1.getProviderModelMismatchMessage)(provider, model);
402
+ const modelLabel = model
403
+ ? mismatch
404
+ ? `${provider} [configured model: ${model}]`
405
+ : `${provider} (${model})`
406
+ : provider;
407
+ const lines = [`${modelLabel} authentication failed.`];
408
+ const cleanDetail = detail.replace(/\s+/g, " ").trim();
409
+ if (cleanDetail)
410
+ lines.push(`provider detail: ${cleanDetail.length > 300 ? `${cleanDetail.slice(0, 297)}...` : cleanDetail}`);
411
+ lines.push("");
412
+ lines.push("To keep using this provider:");
413
+ lines.push(` 1. Run \`ouro auth --agent ${agentName} --provider ${provider}\``);
414
+ if (mismatch) {
415
+ const defaultModel = (0, provider_models_1.getDefaultModelForProvider)(provider);
416
+ lines.push("");
417
+ lines.push("Config warning:");
418
+ lines.push(` - ${mismatch}`);
419
+ lines.push(" - Repair the configured model with:");
420
+ lines.push(` \`ouro config model --agent ${agentName} --facing human ${defaultModel}\``);
421
+ lines.push(` \`ouro config model --agent ${agentName} --facing agent ${defaultModel}\``);
422
+ }
423
+ lines.push("");
424
+ lines.push(`To use another configured provider instead, run \`ouro auth switch --agent ${agentName} --provider <provider>\`.`);
425
+ return lines.join("\n");
278
426
  }
279
- const MAX_RETRIES = 3;
280
- const RETRY_BASE_MS = 2000;
281
427
  async function runAgent(messages, callbacks, channel, signal, options) {
282
- const providerRuntime = getProviderRuntime();
428
+ const facing = (0, channel_1.channelToFacing)(channel);
429
+ const providerRuntime = getProviderRuntime(facing);
283
430
  const provider = providerRuntime.id;
284
431
  const toolChoiceRequired = options?.toolChoiceRequired ?? true;
285
432
  const traceId = options?.traceId;
@@ -305,7 +452,12 @@ async function runAgent(messages, callbacks, channel, signal, options) {
305
452
  // so turn execution remains consistent and non-fatal.
306
453
  if (channel) {
307
454
  try {
308
- const refreshed = await (0, prompt_1.buildSystem)(channel, options, currentContext);
455
+ const buildSystemOptions = {
456
+ ...options,
457
+ providerCapabilities: providerRuntime.capabilities,
458
+ supportedReasoningEfforts: providerRuntime.supportedReasoningEfforts,
459
+ };
460
+ const refreshed = await (0, prompt_1.buildSystem)(channel, buildSystemOptions, currentContext);
309
461
  upsertSystemPrompt(messages, refreshed);
310
462
  }
311
463
  catch (error) {
@@ -327,32 +479,114 @@ async function runAgent(messages, callbacks, channel, signal, options) {
327
479
  });
328
480
  }
329
481
  }
330
- await (0, associative_recall_1.injectAssociativeRecall)(messages);
331
- // kickCount and lastKickReason preserved but unused while kick detection is disabled.
332
- // let kickCount = 0;
333
- // let lastKickReason: KickReason | null = null;
482
+ if (channel) {
483
+ await (0, kept_notes_1.injectKeptNotes)(messages, {
484
+ channel,
485
+ friend: currentContext?.friend,
486
+ judge: async (input) => (0, kept_notes_1.createKeptNotesJudge)(getProviderRuntime("agent"), signal)(input),
487
+ signal,
488
+ traceId,
489
+ });
490
+ }
334
491
  let done = false;
335
492
  let lastUsage;
336
493
  let overflowRetried = false;
337
- let retryCount = 0;
494
+ let outcome = "settled";
495
+ let completion;
496
+ let terminalError;
497
+ let terminalErrorClassification;
498
+ let sawSteeringFollowUp = false;
499
+ let mustResolveBeforeHandoffActive = options?.mustResolveBeforeHandoff === true;
500
+ let currentReasoningEffort = "medium";
501
+ let sawSendMessageSelf = false;
502
+ let sawPonder = false;
503
+ let sawQuerySession = false;
504
+ let sawBridgeManage = false;
505
+ let sawExternalStateQuery = false;
506
+ const toolLoopState = (0, tool_loop_1.createToolLoopState)();
507
+ const toolFrictionLedger = (0, tool_friction_1.createToolFrictionLedger)();
508
+ const finishTerminalProviderError = (error, classification) => {
509
+ terminalError = error;
510
+ terminalErrorClassification = classification;
511
+ /* v8 ignore start — auth-failure guidance: tested via provider error classification tests @preserve */
512
+ if (terminalErrorClassification === "auth-failure") {
513
+ const agentName = (0, identity_2.getAgentName)();
514
+ const currentProvider = providerRuntime.id;
515
+ callbacks.onError(new Error(buildAuthFailureGuidance(currentProvider, providerRuntime.model, agentName, terminalError.message)), "terminal");
516
+ }
517
+ else {
518
+ callbacks.onError(terminalError, "terminal");
519
+ }
520
+ /* v8 ignore stop */
521
+ (0, runtime_1.emitNervesEvent)({
522
+ level: "error",
523
+ event: "engine.error",
524
+ trace_id: traceId,
525
+ component: "engine",
526
+ message: terminalError.message,
527
+ meta: {
528
+ provider: providerRuntime.id,
529
+ model: providerRuntime.model,
530
+ errorClassification: terminalErrorClassification,
531
+ },
532
+ });
533
+ stripLastToolCalls(messages);
534
+ outcome = "errored";
535
+ done = true;
536
+ };
338
537
  // Prevent MaxListenersExceeded warning — each iteration adds a listener
339
538
  try {
340
539
  require("events").setMaxListeners(50, signal);
341
540
  }
342
541
  catch { /* unsupported */ }
343
542
  const toolPreferences = currentContext?.friend?.toolPreferences;
344
- const baseTools = options?.tools ?? (0, tools_1.getToolsForChannel)(channel ? (0, channel_1.getChannelCapabilities)(channel) : undefined, toolPreferences && Object.keys(toolPreferences).length > 0 ? toolPreferences : undefined, currentContext);
543
+ const baseTools = options?.tools ?? (0, tools_1.getToolsForChannel)(channel ? (0, channel_1.getChannelCapabilities)(channel) : undefined, toolPreferences && Object.keys(toolPreferences).length > 0 ? toolPreferences : undefined, currentContext, providerRuntime.capabilities, options?.mcpManager, providerRuntime.model);
544
+ // Augment tool context with reasoning effort controls from provider
545
+ const augmentedToolContext = options?.toolContext
546
+ ? {
547
+ ...options.toolContext,
548
+ supportedReasoningEfforts: providerRuntime.supportedReasoningEfforts,
549
+ setReasoningEffort: (level) => { currentReasoningEffort = level; },
550
+ activeWorkFrame: options?.activeWorkFrame,
551
+ }
552
+ : undefined;
345
553
  // Rebase provider-owned turn state from canonical messages at user-turn start.
346
554
  // This prevents stale provider caches from replaying prior-turn context.
347
555
  providerRuntime.resetTurnState(messages);
348
556
  while (!done) {
349
- // When toolChoiceRequired is true (the default), include final_answer
350
- // so the model can signal completion. With tool_choice: required, the
351
- // model must call a tool every turn final_answer is how it exits.
352
- // Overridable via options.toolChoiceRequired = false (e.g. CLI).
353
- const activeTools = toolChoiceRequired ? [...baseTools, tools_1.finalAnswerTool] : baseTools;
557
+ // Channel-based tool filtering:
558
+ // - Inner dialog: exclude send_message (delivery via surface), observe (no one to observe)
559
+ // - All outward channels (1:1, group, reaction): observe available
560
+ //
561
+ // ponder, settle/rest, surface, and observe are always assembled based on channel context.
562
+ // ponder is available in ALL channels (outer: think privately, inner: keep turning).
563
+ // Inner dialog gets restTool instead of settleTool (rest = end turn, gated by attention queue).
564
+ // toolChoiceRequired only controls whether tool_choice: "required" is set in the API call.
565
+ const isInnerDialog = channel === "inner";
566
+ const filteredBaseTools = isInnerDialog
567
+ ? baseTools.filter((t) => t.function.name !== "send_message")
568
+ : baseTools;
569
+ const activeTools = [
570
+ ...filteredBaseTools,
571
+ tools_1.ponderTool,
572
+ ...(isInnerDialog ? [tools_2.surfaceToolDef, tools_1.restTool] : []),
573
+ ...(!isInnerDialog ? [tools_1.observeTool] : []),
574
+ ...(!isInnerDialog ? [tools_1.settleTool] : []),
575
+ ];
354
576
  const steeringFollowUps = options?.drainSteeringFollowUps?.() ?? [];
355
577
  if (steeringFollowUps.length > 0) {
578
+ const hasSupersedingFollowUp = steeringFollowUps.some((followUp) => followUp.effect === "clear_and_supersede");
579
+ if (hasSupersedingFollowUp) {
580
+ mustResolveBeforeHandoffActive = false;
581
+ options?.setMustResolveBeforeHandoff?.(false);
582
+ outcome = "superseded";
583
+ break;
584
+ }
585
+ if (steeringFollowUps.some((followUp) => followUp.effect === "set_no_handoff")) {
586
+ mustResolveBeforeHandoffActive = true;
587
+ options?.setMustResolveBeforeHandoff?.(true);
588
+ }
589
+ sawSteeringFollowUp = true;
356
590
  for (const followUp of steeringFollowUps) {
357
591
  messages.push({ role: "user", content: followUp.text });
358
592
  }
@@ -360,22 +594,76 @@ async function runAgent(messages, callbacks, channel, signal, options) {
360
594
  }
361
595
  // Yield so pending I/O (stdin Ctrl-C) can be processed between iterations
362
596
  await new Promise((r) => setImmediate(r));
363
- if (signal?.aborted)
597
+ if (signal?.aborted) {
598
+ outcome = "aborted";
364
599
  break;
600
+ }
365
601
  try {
366
- callbacks.onModelStart();
367
- const result = await providerRuntime.streamTurn({
368
- messages,
369
- activeTools,
370
- callbacks,
371
- signal,
372
- traceId,
373
- toolChoiceRequired,
602
+ const callProviderTurn = async () => {
603
+ callbacks.onModelStart();
604
+ try {
605
+ return await providerRuntime.streamTurn({
606
+ messages,
607
+ activeTools,
608
+ callbacks,
609
+ signal,
610
+ traceId,
611
+ toolChoiceRequired,
612
+ reasoningEffort: currentReasoningEffort,
613
+ eagerSettleStreaming: true,
614
+ });
615
+ }
616
+ catch (error) {
617
+ if (signal?.aborted)
618
+ throw new provider_attempt_1.ProviderAttemptAbortError();
619
+ throw error;
620
+ }
621
+ };
622
+ const callProviderTurnWithOverflowRecovery = async () => {
623
+ try {
624
+ return await callProviderTurn();
625
+ }
626
+ catch (error) {
627
+ if (error instanceof provider_attempt_1.ProviderAttemptAbortError)
628
+ throw error;
629
+ if (isContextOverflow(error) && !overflowRetried) {
630
+ overflowRetried = true;
631
+ stripLastToolCalls(messages);
632
+ const { maxTokens, contextMargin } = (0, config_1.getContextConfig)();
633
+ const trimmed = (0, context_1.trimMessages)(messages, maxTokens, contextMargin, maxTokens * 2);
634
+ messages.splice(0, messages.length, ...trimmed);
635
+ providerRuntime.resetTurnState(messages);
636
+ callbacks.onError(new Error("context trimmed, retrying..."), "transient");
637
+ return callProviderTurn();
638
+ }
639
+ throw error;
640
+ }
641
+ };
642
+ const attempt = await (0, provider_attempt_1.runProviderAttempt)({
643
+ operation: "turn",
644
+ provider: providerRuntime.id,
645
+ model: providerRuntime.model,
646
+ run: callProviderTurnWithOverflowRecovery,
647
+ classifyError: (error) => providerRuntime.classifyError(error),
648
+ onRetry: (record, maxAttempts) => {
649
+ const delayMs = record.delayMs;
650
+ const seconds = delayMs / 1000;
651
+ const cause = RETRY_LABELS[record.classification];
652
+ callbacks.onError(new Error(`${cause}, retrying in ${seconds}s (${record.attempt}/${maxAttempts})...`), "transient");
653
+ },
654
+ sleep: async (delayMs) => {
655
+ await waitForProviderRetry(delayMs, signal);
656
+ providerRuntime.resetTurnState(messages);
657
+ },
374
658
  });
659
+ if (!attempt.ok) {
660
+ finishTerminalProviderError(attempt.error, attempt.classification);
661
+ continue;
662
+ }
663
+ const result = attempt.value;
375
664
  // Track usage from the latest API call
376
665
  if (result.usage)
377
666
  lastUsage = result.usage;
378
- retryCount = 0; // reset on success
379
667
  // SHARED: build CC-format assistant message from TurnResult
380
668
  const msg = {
381
669
  role: "assistant",
@@ -394,52 +682,76 @@ async function runAgent(messages, callbacks, channel, signal, options) {
394
682
  if (reasoningItems.length > 0) {
395
683
  msg._reasoning_items = reasoningItems;
396
684
  }
685
+ // Store thinking blocks (Anthropic) on the assistant message for round-tripping
686
+ const thinkingItems = result.outputItems.filter((item) => "type" in item && (item.type === "thinking" || item.type === "redacted_thinking"));
687
+ if (thinkingItems.length > 0) {
688
+ msg._thinking_blocks = thinkingItems;
689
+ }
690
+ // Phase annotation for Codex provider
691
+ const hasPhaseAnnotation = providerRuntime.capabilities.has("phase-annotation");
692
+ const isSoleSettle = result.toolCalls.length === 1 && result.toolCalls[0].name === "settle";
693
+ if (hasPhaseAnnotation) {
694
+ msg.phase = isSoleSettle ? "settle" : "commentary";
695
+ }
397
696
  if (!result.toolCalls.length) {
398
- // Kick detection is disabled while tool_choice: required + final_answer
399
- // is the primary loop control mechanism. The model should never reach
400
- // this path (tool_choice: required forces a tool call), but if it does,
401
- // accept the response as-is rather than risk false-positive kicks.
402
- //
403
- // Preserved for future use — re-enable by uncommenting:
404
- // const kick = detectKick(result.content, options);
405
- // if (kick) {
406
- // kickCount++;
407
- // lastKickReason = kick.reason;
408
- // callbacks.onKick?.();
409
- // const kickContent = result.content
410
- // ? result.content + "\n\n" + kick.message
411
- // : kick.message;
412
- // messages.push({ role: "assistant", content: kickContent });
413
- // providerRuntime.resetTurnState(messages);
414
- // continue;
415
- // }
697
+ // No tool calls accept response as-is.
698
+ // (Kick detection disabled; tool_choice: required + settle
699
+ // is the primary loop control. See src/heart/kicks.ts to re-enable.)
416
700
  messages.push(msg);
417
701
  done = true;
418
702
  }
419
703
  else {
420
- // Check for final_answer sole call: intercept before tool execution
421
- const isSoleFinalAnswer = result.toolCalls.length === 1 && result.toolCalls[0].name === "final_answer";
422
- if (isSoleFinalAnswer) {
423
- // Extract answer from the tool call arguments.
424
- // Supports: {"answer":"text"}, "text" (JSON string), retry on failure.
425
- let answer;
426
- try {
427
- const parsed = JSON.parse(result.toolCalls[0].arguments);
428
- if (typeof parsed === "string") {
429
- answer = parsed;
430
- }
431
- else if (parsed.answer != null) {
432
- answer = parsed.answer;
433
- }
434
- // else: valid JSON but no answer field — answer stays undefined (retry)
704
+ // Check for settle sole call: intercept before tool execution
705
+ if (isSoleSettle) {
706
+ /* v8 ignore next -- defensive: JSON.parse catch for malformed settle args @preserve */
707
+ const settleArgs = (() => { try {
708
+ return JSON.parse(result.toolCalls[0].arguments);
435
709
  }
436
710
  catch {
437
- // JSON parsing failed (e.g. truncated output) — answer stays undefined (retry)
711
+ return {};
712
+ } })();
713
+ callbacks.onToolStart("settle", settleArgs);
714
+ // Inner dialog attention queue gate: reject settle if items remain
715
+ const attentionQueue = (augmentedToolContext ?? options?.toolContext)?.delegatedOrigins;
716
+ if (isInnerDialog && attentionQueue && attentionQueue.length > 0) {
717
+ callbacks.onToolEnd("settle", (0, tools_1.summarizeArgs)("settle", settleArgs), false);
718
+ callbacks.onClearText?.();
719
+ messages.push(msg);
720
+ const gateMessage = "you're holding thoughts someone is waiting for — surface them before you settle.";
721
+ messages.push({ role: "tool", tool_call_id: result.toolCalls[0].id, content: gateMessage });
722
+ providerRuntime.appendToolOutput(result.toolCalls[0].id, gateMessage);
723
+ continue;
438
724
  }
439
- if (answer != null) {
440
- if (result.finalAnswerStreamed) {
725
+ // Extract answer from the tool call arguments.
726
+ // Supports: {"answer":"text","intent":"..."} or "text" (JSON string).
727
+ const { answer, intent } = parseSettlePayload(result.toolCalls[0].arguments);
728
+ // Inner dialog settle: no CompletionMetadata, "(settled)" ack
729
+ if (isInnerDialog) {
730
+ callbacks.onToolEnd("settle", (0, tools_1.summarizeArgs)("settle", settleArgs), true);
731
+ messages.push(msg);
732
+ const settled = "(settled)";
733
+ messages.push({ role: "tool", tool_call_id: result.toolCalls[0].id, content: settled });
734
+ providerRuntime.appendToolOutput(result.toolCalls[0].id, settled);
735
+ outcome = "settled";
736
+ done = true;
737
+ continue;
738
+ }
739
+ const retryError = getSettleRetryError(mustResolveBeforeHandoffActive, intent, sawSteeringFollowUp, options?.delegationDecision, sawSendMessageSelf, sawPonder, sawQuerySession, options?.currentObligation ?? null, options?.activeWorkFrame?.inner?.job, sawExternalStateQuery);
740
+ const deliveredAnswer = answer;
741
+ const validDirectReply = mustResolveBeforeHandoffActive && intent === "direct_reply" && sawSteeringFollowUp;
742
+ const validTerminalIntent = intent === "complete" || intent === "blocked";
743
+ const validClosure = deliveredAnswer != null
744
+ && !retryError
745
+ && (!mustResolveBeforeHandoffActive || validDirectReply || validTerminalIntent);
746
+ if (validClosure) {
747
+ callbacks.onToolEnd("settle", (0, tools_1.summarizeArgs)("settle", settleArgs), true);
748
+ completion = {
749
+ answer: deliveredAnswer,
750
+ intent: validDirectReply ? "direct_reply" : intent === "blocked" ? "blocked" : "complete",
751
+ };
752
+ if (result.settleStreamed) {
441
753
  // The streaming layer already parsed and emitted the answer
442
- // progressively via FinalAnswerParser. Skip clearing and
754
+ // progressively via SettleParser. Skip clearing and
443
755
  // re-emitting to avoid double-delivery.
444
756
  }
445
757
  else {
@@ -447,37 +759,110 @@ async function runAgent(messages, callbacks, channel, signal, options) {
447
759
  callbacks.onClearText?.();
448
760
  // Emit the answer through the callback pipeline so channels receive it.
449
761
  // Never truncate -- channel adapters handle splitting long messages.
450
- callbacks.onTextChunk(answer);
762
+ callbacks.onTextChunk(deliveredAnswer);
451
763
  }
452
- // Keep the full assistant message (with tool_calls) for debuggability,
453
- // plus a synthetic tool response so the conversation stays valid on resume.
454
764
  messages.push(msg);
455
- messages.push({ role: "tool", tool_call_id: result.toolCalls[0].id, content: "(delivered)" });
456
- providerRuntime.appendToolOutput(result.toolCalls[0].id, "(delivered)");
457
- done = true;
765
+ if (validDirectReply) {
766
+ const resumeWork = "direct reply delivered. resume the unresolved obligation now and keep working until you can finish or clearly report that you are blocked.";
767
+ messages.push({ role: "tool", tool_call_id: result.toolCalls[0].id, content: resumeWork });
768
+ providerRuntime.appendToolOutput(result.toolCalls[0].id, resumeWork);
769
+ }
770
+ else {
771
+ const delivered = "(delivered)";
772
+ messages.push({ role: "tool", tool_call_id: result.toolCalls[0].id, content: delivered });
773
+ providerRuntime.appendToolOutput(result.toolCalls[0].id, delivered);
774
+ outcome = intent === "blocked" ? "blocked" : "settled";
775
+ done = true;
776
+ }
458
777
  }
459
778
  else {
460
- // Answer is undefined -- the model's final_answer was incomplete or
779
+ // Answer is undefined -- the model's settle was incomplete or
461
780
  // malformed. Clear any partial streamed text or noise, then push the
462
781
  // assistant msg + error tool result and let the model try again.
782
+ callbacks.onToolEnd("settle", (0, tools_1.summarizeArgs)("settle", settleArgs), false);
463
783
  callbacks.onClearText?.();
464
- const retryError = "your final_answer was incomplete or malformed. call final_answer again with your complete response.";
465
784
  messages.push(msg);
466
- messages.push({ role: "tool", tool_call_id: result.toolCalls[0].id, content: retryError });
467
- providerRuntime.appendToolOutput(result.toolCalls[0].id, retryError);
785
+ const toolRetryMessage = retryError
786
+ ?? "your settle was incomplete or malformed. call settle again with your complete response.";
787
+ messages.push({ role: "tool", tool_call_id: result.toolCalls[0].id, content: toolRetryMessage });
788
+ providerRuntime.appendToolOutput(result.toolCalls[0].id, toolRetryMessage);
468
789
  }
469
790
  continue;
470
791
  }
792
+ // Check for observe sole call: intercept before tool execution
793
+ const isSoleObserve = result.toolCalls.length === 1 && result.toolCalls[0].name === "observe";
794
+ if (isSoleObserve) {
795
+ /* v8 ignore next -- defensive: JSON.parse catch for malformed observe args @preserve */
796
+ const observeArgs = (() => { try {
797
+ return JSON.parse(result.toolCalls[0].arguments);
798
+ }
799
+ catch {
800
+ return {};
801
+ } })();
802
+ let reason;
803
+ if (typeof observeArgs?.reason === "string")
804
+ reason = observeArgs.reason;
805
+ callbacks.onToolStart("observe", observeArgs);
806
+ (0, runtime_1.emitNervesEvent)({
807
+ component: "engine",
808
+ event: "engine.observe",
809
+ message: "agent observed without responding",
810
+ meta: { ...(reason ? { reason } : {}) },
811
+ });
812
+ callbacks.onToolEnd("observe", (0, tools_1.summarizeArgs)("observe", observeArgs), true);
813
+ messages.push(msg);
814
+ const silenced = "(silenced)";
815
+ messages.push({ role: "tool", tool_call_id: result.toolCalls[0].id, content: silenced });
816
+ providerRuntime.appendToolOutput(result.toolCalls[0].id, silenced);
817
+ outcome = "observed";
818
+ done = true;
819
+ continue;
820
+ }
821
+ // Check for rest sole call: intercept before tool execution
822
+ const isSoleRest = result.toolCalls.length === 1 && result.toolCalls[0].name === "rest";
823
+ if (isSoleRest) {
824
+ const restArgs = (() => { try {
825
+ return JSON.parse(result.toolCalls[0].arguments);
826
+ }
827
+ catch {
828
+ return {};
829
+ } })();
830
+ callbacks.onToolStart("rest", restArgs);
831
+ // Attention queue gate: reject rest if items remain
832
+ const attentionQueue = (augmentedToolContext ?? options?.toolContext)?.delegatedOrigins;
833
+ if (attentionQueue && attentionQueue.length > 0) {
834
+ callbacks.onToolEnd("rest", (0, tools_1.summarizeArgs)("rest", restArgs), false);
835
+ messages.push(msg);
836
+ const gateMessage = "you're holding thoughts someone is waiting for — surface them before you rest.";
837
+ messages.push({ role: "tool", tool_call_id: result.toolCalls[0].id, content: gateMessage });
838
+ providerRuntime.appendToolOutput(result.toolCalls[0].id, gateMessage);
839
+ continue;
840
+ }
841
+ callbacks.onToolEnd("rest", (0, tools_1.summarizeArgs)("rest", restArgs), true);
842
+ messages.push(msg);
843
+ const ack = "(resting)";
844
+ messages.push({ role: "tool", tool_call_id: result.toolCalls[0].id, content: ack });
845
+ providerRuntime.appendToolOutput(result.toolCalls[0].id, ack);
846
+ (0, runtime_1.emitNervesEvent)({
847
+ component: "engine",
848
+ event: "engine.rested",
849
+ message: "resting until next heartbeat",
850
+ meta: { ...(typeof restArgs?.status === "string" ? { status: restArgs.status } : {}) },
851
+ });
852
+ outcome = "rested";
853
+ done = true;
854
+ continue;
855
+ }
471
856
  messages.push(msg);
472
- // SHARED: execute tools (final_answer in mixed calls is rejected inline)
857
+ // Execute tools (sole-call tools in mixed calls are rejected inline)
473
858
  for (const tc of result.toolCalls) {
474
859
  if (signal?.aborted)
475
860
  break;
476
- // Intercept final_answer in mixed call: reject it
477
- if (tc.name === "final_answer") {
478
- const rejection = "rejected: final_answer must be the only tool call. Finish your work first, then call final_answer alone.";
479
- messages.push({ role: "tool", tool_call_id: tc.id, content: rejection });
480
- providerRuntime.appendToolOutput(tc.id, rejection);
861
+ // Reject sole-call tools when mixed with other tool calls
862
+ const soleCallRejection = SOLE_CALL_REJECTION[tc.name];
863
+ if (soleCallRejection) {
864
+ messages.push({ role: "tool", tool_call_id: tc.id, content: soleCallRejection });
865
+ providerRuntime.appendToolOutput(tc.id, soleCallRejection);
481
866
  continue;
482
867
  }
483
868
  let args = {};
@@ -487,94 +872,195 @@ async function runAgent(messages, callbacks, channel, signal, options) {
487
872
  catch {
488
873
  /* ignore */
489
874
  }
490
- const argSummary = (0, tools_1.summarizeArgs)(tc.name, args);
491
- // Confirmation check for mutate tools
492
- if ((0, tools_1.isConfirmationRequired)(tc.name) && !options?.skipConfirmation) {
493
- let decision = "denied";
494
- if (callbacks.onConfirmAction) {
495
- decision = await callbacks.onConfirmAction(tc.name, args);
875
+ if (tc.name === "send_message" && args.friendId === "self") {
876
+ sawSendMessageSelf = true;
877
+ }
878
+ if (tc.name === "ponder") {
879
+ const parsedArgs = normalizeLegacyPonderArgs(parsePonderPayload(tc.arguments));
880
+ const argSummary = (0, tools_1.summarizeArgs)(tc.name, parsedArgs);
881
+ callbacks.onToolStart(tc.name, parsedArgs);
882
+ let toolResult;
883
+ let success = false;
884
+ try {
885
+ const action = parsedArgs.action ?? "create";
886
+ const currentSession = (augmentedToolContext ?? options?.toolContext)?.currentSession;
887
+ const currentOrigin = currentSession
888
+ ? { friendId: currentSession.friendId, channel: currentSession.channel, key: currentSession.key }
889
+ : undefined;
890
+ const isInnerChannel = currentOrigin?.friendId === "self" && currentOrigin?.channel === "inner";
891
+ const successCriteria = parseSuccessCriteria(parsedArgs.success_criteria);
892
+ const payload = parsePacketPayload(parsedArgs.payload_json);
893
+ let packet;
894
+ let returnObligationId = null;
895
+ let resultAction = "created";
896
+ if (action === "create") {
897
+ const kind = parsedArgs.kind;
898
+ const objective = typeof parsedArgs.objective === "string" ? parsedArgs.objective.trim() : "";
899
+ const summary = typeof parsedArgs.summary === "string" ? parsedArgs.summary.trim() : "";
900
+ if (!kind || !objective || !successCriteria || !payload) {
901
+ throw new Error("ponder create requires kind, objective, success_criteria, and valid payload_json.");
902
+ }
903
+ const agentRoot = (0, identity_2.getAgentRoot)();
904
+ let relatedObligationId;
905
+ if (currentOrigin && !isInnerChannel) {
906
+ try {
907
+ const obligation = (0, obligations_1.createObligation)(agentRoot, {
908
+ origin: currentOrigin,
909
+ content: objective,
910
+ });
911
+ relatedObligationId = obligation.id;
912
+ }
913
+ catch {
914
+ relatedObligationId = undefined;
915
+ }
916
+ }
917
+ const frictionSignature = kind === "harness_friction" && typeof payload.frictionSignature === "string"
918
+ ? payload.frictionSignature
919
+ : null;
920
+ const existing = frictionSignature && currentOrigin
921
+ ? (0, packets_1.findHarnessFrictionPacket)(agentRoot, currentOrigin, frictionSignature)
922
+ : null;
923
+ if (existing) {
924
+ resultAction = "revised";
925
+ returnObligationId = existing.relatedReturnObligationId ?? null;
926
+ packet = existing.status === "drafting"
927
+ ? (0, packets_1.revisePonderPacket)(agentRoot, existing.id, {
928
+ kind,
929
+ objective,
930
+ summary,
931
+ successCriteria,
932
+ payload,
933
+ })
934
+ : existing;
935
+ }
936
+ else {
937
+ returnObligationId = (0, obligations_1.generateObligationId)(Date.now());
938
+ packet = (0, packets_1.createPonderPacket)(agentRoot, {
939
+ kind,
940
+ objective,
941
+ summary,
942
+ successCriteria,
943
+ ...(currentOrigin ? { origin: currentOrigin } : {}),
944
+ ...(relatedObligationId ? { relatedObligationId } : {}),
945
+ relatedReturnObligationId: returnObligationId,
946
+ ...(parsedArgs.follows_packet_id ? { followsPacketId: parsedArgs.follows_packet_id } : {}),
947
+ payload,
948
+ });
949
+ (0, obligations_1.createReturnObligation)((0, identity_2.getAgentName)(), {
950
+ id: returnObligationId,
951
+ origin: currentOrigin ?? { friendId: "self", channel: "inner", key: "dialog" },
952
+ status: "queued",
953
+ delegatedContent: (summary || objective).length > 120 ? `${(summary || objective).slice(0, 117)}...` : (summary || objective),
954
+ packetId: packet.id,
955
+ createdAt: Date.now(),
956
+ });
957
+ }
958
+ }
959
+ else if (action === "revise") {
960
+ const packetId = typeof parsedArgs.packet_id === "string" ? parsedArgs.packet_id.trim() : "";
961
+ const kind = parsedArgs.kind;
962
+ const objective = typeof parsedArgs.objective === "string" ? parsedArgs.objective.trim() : "";
963
+ const summary = typeof parsedArgs.summary === "string" ? parsedArgs.summary.trim() : "";
964
+ if (!packetId || !kind || !objective || !successCriteria || !payload) {
965
+ throw new Error("ponder revise requires packet_id, kind, objective, success_criteria, and valid payload_json.");
966
+ }
967
+ packet = (0, packets_1.revisePonderPacket)((0, identity_2.getAgentRoot)(), packetId, {
968
+ kind,
969
+ objective,
970
+ summary,
971
+ successCriteria,
972
+ payload,
973
+ });
974
+ returnObligationId = packet.relatedReturnObligationId ?? null;
975
+ resultAction = "revised";
976
+ }
977
+ else {
978
+ throw new Error("ponder requires action=create or revise.");
979
+ }
980
+ try {
981
+ await (0, socket_client_1.requestInnerWake)((0, identity_2.getAgentName)());
982
+ }
983
+ catch { /* daemon may not be running */ }
984
+ sawPonder = true;
985
+ toolResult = buildPonderResult(packet, resultAction, returnObligationId);
986
+ success = true;
987
+ (0, runtime_1.emitNervesEvent)({
988
+ component: "engine",
989
+ event: "engine.ponder_packet",
990
+ message: "ponder packet touched",
991
+ meta: {
992
+ action: resultAction,
993
+ packetId: packet.id,
994
+ kind: packet.kind,
995
+ status: packet.status,
996
+ },
997
+ });
496
998
  }
497
- if (decision !== "confirmed") {
498
- const cancelled = "Action cancelled by user.";
499
- callbacks.onToolStart(tc.name, args);
500
- callbacks.onToolEnd(tc.name, argSummary, false);
501
- messages.push({ role: "tool", tool_call_id: tc.id, content: cancelled });
502
- providerRuntime.appendToolOutput(tc.id, cancelled);
503
- continue;
999
+ catch (error) {
1000
+ toolResult = error instanceof Error ? error.message : String(error);
504
1001
  }
1002
+ callbacks.onToolEnd(tc.name, argSummary, success);
1003
+ messages.push({ role: "tool", tool_call_id: tc.id, content: toolResult });
1004
+ providerRuntime.appendToolOutput(tc.id, toolResult);
1005
+ continue;
1006
+ }
1007
+ /* v8 ignore next -- flag tested via truth-check integration tests @preserve */
1008
+ if (tc.name === "query_session")
1009
+ sawQuerySession = true;
1010
+ /* v8 ignore next -- flag tested via truth-check integration tests @preserve */
1011
+ if (tc.name === "bridge_manage")
1012
+ sawBridgeManage = true;
1013
+ /* v8 ignore next -- flag tested via truth-check integration tests @preserve */
1014
+ if (isExternalStateQuery(tc.name, args))
1015
+ sawExternalStateQuery = true;
1016
+ const argSummary = (0, tools_1.summarizeArgs)(tc.name, args);
1017
+ const toolLoop = (0, tool_loop_1.detectToolLoop)(toolLoopState, tc.name, args);
1018
+ if (toolLoop.stuck) {
1019
+ const rejection = `loop guard: ${toolLoop.message}`;
1020
+ callbacks.onToolStart(tc.name, args);
1021
+ callbacks.onToolEnd(tc.name, argSummary, false);
1022
+ messages.push({ role: "tool", tool_call_id: tc.id, content: rejection });
1023
+ providerRuntime.appendToolOutput(tc.id, rejection);
1024
+ continue;
505
1025
  }
506
1026
  callbacks.onToolStart(tc.name, args);
507
1027
  let toolResult;
508
1028
  let success;
509
1029
  try {
510
1030
  const execToolFn = options?.execTool ?? tools_1.execTool;
511
- toolResult = await execToolFn(tc.name, args, options?.toolContext);
1031
+ toolResult = await execToolFn(tc.name, args, augmentedToolContext ?? options?.toolContext);
512
1032
  success = true;
513
1033
  }
514
1034
  catch (e) {
515
1035
  toolResult = `error: ${e}`;
516
1036
  success = false;
517
1037
  }
518
- callbacks.onToolEnd(tc.name, argSummary, success);
1038
+ toolResult = (0, tool_friction_1.rewriteToolResultForModel)(tc.name, toolResult, toolFrictionLedger);
1039
+ (0, tool_loop_1.recordToolOutcome)(toolLoopState, tc.name, args, toolResult, success);
1040
+ callbacks.onToolEnd(tc.name, (0, tools_1.buildToolResultSummary)(tc.name, args, toolResult, success), success);
519
1041
  messages.push({ role: "tool", tool_call_id: tc.id, content: toolResult });
520
1042
  providerRuntime.appendToolOutput(tc.id, toolResult);
1043
+ callbacks.onToolResult?.(messages);
521
1044
  }
522
1045
  }
523
1046
  }
524
1047
  catch (e) {
525
1048
  // Abort is not an error — just stop cleanly
526
- if (signal?.aborted) {
1049
+ if (e instanceof provider_attempt_1.ProviderAttemptAbortError || signal?.aborted) {
527
1050
  stripLastToolCalls(messages);
1051
+ outcome = "aborted";
528
1052
  break;
529
1053
  }
530
- // Context overflow: trim aggressively and retry once
531
- if (isContextOverflow(e) && !overflowRetried) {
532
- overflowRetried = true;
533
- stripLastToolCalls(messages);
534
- const { maxTokens, contextMargin } = (0, config_1.getContextConfig)();
535
- const trimmed = (0, context_1.trimMessages)(messages, maxTokens, contextMargin, maxTokens * 2);
536
- messages.splice(0, messages.length, ...trimmed);
537
- providerRuntime.resetTurnState(messages);
538
- callbacks.onError(new Error("context trimmed, retrying..."), "transient");
539
- continue;
1054
+ const errorForClassification = e instanceof Error ? e : /* v8 ignore next -- defensive @preserve */ new Error(String(e));
1055
+ let providerClassification;
1056
+ try {
1057
+ providerClassification = providerRuntime.classifyError(errorForClassification);
540
1058
  }
541
- // Transient errors: retry with exponential backoff
542
- if (isTransientError(e) && retryCount < MAX_RETRIES) {
543
- retryCount++;
544
- const delay = RETRY_BASE_MS * Math.pow(2, retryCount - 1);
545
- const cause = classifyTransientError(e);
546
- callbacks.onError(new Error(`${cause}, retrying in ${delay / 1000}s (${retryCount}/${MAX_RETRIES})...`), "transient");
547
- // Wait with abort support
548
- const aborted = await new Promise((resolve) => {
549
- const timer = setTimeout(() => resolve(false), delay);
550
- if (signal) {
551
- const onAbort = () => { clearTimeout(timer); resolve(true); };
552
- if (signal.aborted) {
553
- clearTimeout(timer);
554
- resolve(true);
555
- return;
556
- }
557
- signal.addEventListener("abort", onAbort, { once: true });
558
- }
559
- });
560
- if (aborted) {
561
- stripLastToolCalls(messages);
562
- break;
563
- }
564
- providerRuntime.resetTurnState(messages);
565
- continue;
1059
+ catch {
1060
+ /* v8 ignore next -- defensive: classifyError should not throw @preserve */
1061
+ providerClassification = "unknown";
566
1062
  }
567
- callbacks.onError(e instanceof Error ? e : new Error(String(e)), "terminal");
568
- (0, runtime_1.emitNervesEvent)({
569
- level: "error",
570
- event: "engine.error",
571
- trace_id: traceId,
572
- component: "engine",
573
- message: e instanceof Error ? e.message : String(e),
574
- meta: {},
575
- });
576
- stripLastToolCalls(messages);
577
- done = true;
1063
+ finishTerminalProviderError(errorForClassification, providerClassification);
578
1064
  }
579
1065
  }
580
1066
  (0, runtime_1.emitNervesEvent)({
@@ -582,7 +1068,12 @@ async function runAgent(messages, callbacks, channel, signal, options) {
582
1068
  trace_id: traceId,
583
1069
  component: "engine",
584
1070
  message: "runAgent turn completed",
585
- meta: { done },
1071
+ meta: { done, sawPonder, sawQuerySession, sawBridgeManage },
586
1072
  });
587
- return { usage: lastUsage };
1073
+ return {
1074
+ usage: lastUsage,
1075
+ outcome,
1076
+ completion,
1077
+ ...(terminalError ? { error: terminalError, errorClassification: terminalErrorClassification } : {}),
1078
+ };
588
1079
  }