@ouro.bot/cli 0.1.0-alpha.42 → 0.1.0-alpha.421

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