@ouro.bot/cli 0.1.0-alpha.6 → 0.1.0-alpha.600

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 (440) hide show
  1. package/README.md +229 -183
  2. package/RepairGuide.ouro/agent.json +5 -0
  3. package/RepairGuide.ouro/psyche/IDENTITY.md +19 -0
  4. package/RepairGuide.ouro/psyche/SOUL.md +55 -0
  5. package/RepairGuide.ouro/skills/diagnose-broken-remote.md +63 -0
  6. package/RepairGuide.ouro/skills/diagnose-stacked-typed-issues.md +35 -0
  7. package/RepairGuide.ouro/skills/diagnose-sync-blocked.md +54 -0
  8. package/RepairGuide.ouro/skills/diagnose-vault-expired.md +60 -0
  9. package/SerpentGuide.ouro/agent.json +83 -0
  10. package/SerpentGuide.ouro/psyche/SOUL.md +25 -0
  11. package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/monty.md +2 -2
  12. package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/the-serpent.md +1 -1
  13. package/assets/ouroboros.png +0 -0
  14. package/changelog.json +4182 -0
  15. package/dist/arc/attention-types.js +8 -0
  16. package/dist/arc/cares.js +140 -0
  17. package/dist/arc/episodes.js +117 -0
  18. package/dist/arc/intentions.js +133 -0
  19. package/dist/arc/json-store.js +117 -0
  20. package/dist/arc/obligations.js +254 -0
  21. package/dist/arc/packets.js +193 -0
  22. package/dist/arc/presence.js +185 -0
  23. package/dist/arc/task-lifecycle.js +65 -0
  24. package/dist/heart/active-work.js +989 -0
  25. package/dist/heart/agent-entry.js +69 -3
  26. package/dist/heart/attachments/image-normalize.js +194 -0
  27. package/dist/heart/attachments/materialize.js +97 -0
  28. package/dist/heart/attachments/originals.js +88 -0
  29. package/dist/heart/attachments/render.js +29 -0
  30. package/dist/heart/attachments/sources/adapter.js +2 -0
  31. package/dist/heart/attachments/sources/bluebubbles.js +156 -0
  32. package/dist/heart/attachments/sources/cli-local-file.js +78 -0
  33. package/dist/heart/attachments/sources/index.js +16 -0
  34. package/dist/heart/attachments/store.js +103 -0
  35. package/dist/heart/attachments/types.js +93 -0
  36. package/dist/heart/auth/auth-flow.js +479 -0
  37. package/dist/heart/awaiting/await-alert.js +146 -0
  38. package/dist/heart/awaiting/await-expiry.js +108 -0
  39. package/dist/heart/awaiting/await-loader.js +91 -0
  40. package/dist/heart/awaiting/await-parser.js +141 -0
  41. package/dist/heart/awaiting/await-runtime-state.js +97 -0
  42. package/dist/heart/awaiting/await-scheduler.js +377 -0
  43. package/dist/heart/background-operations.js +281 -0
  44. package/dist/heart/bridges/manager.js +358 -0
  45. package/dist/heart/bridges/state-machine.js +135 -0
  46. package/dist/heart/bridges/store.js +123 -0
  47. package/dist/heart/bundle-state.js +168 -0
  48. package/dist/heart/commitments.js +142 -0
  49. package/dist/heart/config-registry.js +322 -0
  50. package/dist/heart/config.js +164 -135
  51. package/dist/heart/core.js +1069 -260
  52. package/dist/heart/cross-chat-delivery.js +131 -0
  53. package/dist/heart/daemon/agent-config-check.js +419 -0
  54. package/dist/heart/daemon/agent-discovery.js +180 -0
  55. package/dist/heart/daemon/agent-service.js +522 -0
  56. package/dist/heart/daemon/agentic-repair.js +547 -0
  57. package/dist/heart/daemon/bluebubbles-health-diagnostics.js +122 -0
  58. package/dist/heart/daemon/boot-sync-probe.js +197 -0
  59. package/dist/heart/daemon/cadence.js +70 -0
  60. package/dist/heart/daemon/cli-defaults.js +776 -0
  61. package/dist/heart/daemon/cli-exec.js +7571 -0
  62. package/dist/heart/daemon/cli-help.js +498 -0
  63. package/dist/heart/daemon/cli-parse.js +1599 -0
  64. package/dist/heart/daemon/cli-render-doctor.js +57 -0
  65. package/dist/heart/daemon/cli-render.js +763 -0
  66. package/dist/heart/daemon/cli-types.js +8 -0
  67. package/dist/heart/daemon/connect-bay.js +323 -0
  68. package/dist/heart/daemon/daemon-cli.js +30 -758
  69. package/dist/heart/daemon/daemon-entry.js +540 -8
  70. package/dist/heart/daemon/daemon-health.js +176 -0
  71. package/dist/heart/daemon/daemon-rollup.js +57 -0
  72. package/dist/heart/daemon/daemon-runtime-sync.js +287 -0
  73. package/dist/heart/daemon/daemon-tombstone.js +236 -0
  74. package/dist/heart/daemon/daemon.js +972 -20
  75. package/dist/heart/daemon/dns-workflow.js +394 -0
  76. package/dist/heart/daemon/doctor-types.js +8 -0
  77. package/dist/heart/daemon/doctor.js +873 -0
  78. package/dist/heart/daemon/health-monitor.js +122 -1
  79. package/dist/heart/daemon/hooks/agent-config-v2.js +33 -0
  80. package/dist/heart/daemon/hooks/bundle-meta.js +206 -0
  81. package/dist/heart/daemon/http-health-probe.js +80 -0
  82. package/dist/heart/daemon/human-command-screens.js +234 -0
  83. package/dist/heart/daemon/human-readiness.js +114 -0
  84. package/dist/heart/daemon/inner-status.js +89 -0
  85. package/dist/heart/daemon/interactive-repair.js +394 -0
  86. package/dist/heart/daemon/launchd.js +188 -0
  87. package/dist/heart/daemon/log-tailer.js +82 -12
  88. package/dist/heart/daemon/logs-prune.js +110 -0
  89. package/dist/heart/daemon/mcp-canary.js +297 -0
  90. package/dist/heart/daemon/message-router.js +17 -8
  91. package/dist/heart/daemon/os-cron-deps.js +135 -0
  92. package/dist/heart/daemon/os-cron.js +14 -12
  93. package/dist/heart/daemon/ouro-bot-entry.js +4 -2
  94. package/dist/heart/daemon/ouro-entry.js +3 -1
  95. package/dist/heart/daemon/process-manager.js +381 -26
  96. package/dist/heart/daemon/provider-discovery.js +137 -0
  97. package/dist/heart/daemon/provider-ping-progress.js +83 -0
  98. package/dist/heart/daemon/pulse.js +475 -0
  99. package/dist/heart/daemon/readiness-repair.js +365 -0
  100. package/dist/heart/daemon/run-hooks.js +39 -0
  101. package/dist/heart/daemon/runtime-logging.js +67 -16
  102. package/dist/heart/daemon/runtime-metadata.js +191 -0
  103. package/dist/heart/daemon/runtime-mode.js +67 -0
  104. package/dist/heart/daemon/safe-mode.js +161 -0
  105. package/dist/heart/daemon/sense-manager.js +731 -0
  106. package/dist/heart/daemon/session-id-resolver.js +131 -0
  107. package/dist/heart/daemon/skill-management-installer.js +94 -0
  108. package/dist/heart/daemon/socket-client.js +349 -0
  109. package/dist/heart/daemon/stale-bundle-prune.js +96 -0
  110. package/dist/heart/daemon/startup-tui.js +330 -0
  111. package/dist/heart/daemon/task-scheduler.js +3 -25
  112. package/dist/heart/daemon/terminal-ui.js +499 -0
  113. package/dist/heart/daemon/thoughts.js +524 -0
  114. package/dist/heart/daemon/up-progress.js +366 -0
  115. package/dist/heart/daemon/vault-items.js +56 -0
  116. package/dist/heart/delegation.js +62 -0
  117. package/dist/heart/habits/habit-migration.js +189 -0
  118. package/dist/heart/habits/habit-parser.js +140 -0
  119. package/dist/heart/habits/habit-runtime-state.js +100 -0
  120. package/dist/heart/habits/habit-scheduler.js +372 -0
  121. package/dist/heart/{daemon → hatch}/hatch-animation.js +10 -3
  122. package/dist/heart/{daemon → hatch}/hatch-flow.js +34 -136
  123. package/dist/heart/{daemon → hatch}/hatch-specialist.js +6 -8
  124. package/dist/heart/hatch/specialist-orchestrator.js +129 -0
  125. package/dist/heart/hatch/specialist-prompt.js +102 -0
  126. package/dist/heart/hatch/specialist-tools.js +306 -0
  127. package/dist/heart/identity.js +281 -67
  128. package/dist/heart/kept-notes.js +357 -0
  129. package/dist/heart/kicks.js +2 -20
  130. package/dist/heart/machine-identity.js +161 -0
  131. package/dist/heart/mail-import-discovery.js +353 -0
  132. package/dist/heart/mailbox/mailbox-http-hooks.js +66 -0
  133. package/dist/heart/mailbox/mailbox-http-response.js +7 -0
  134. package/dist/heart/mailbox/mailbox-http-routes.js +246 -0
  135. package/dist/heart/mailbox/mailbox-http-static.js +103 -0
  136. package/dist/heart/mailbox/mailbox-http-transport.js +116 -0
  137. package/dist/heart/mailbox/mailbox-http.js +99 -0
  138. package/dist/heart/mailbox/mailbox-read.js +31 -0
  139. package/dist/heart/mailbox/mailbox-types.js +27 -0
  140. package/dist/heart/mailbox/mailbox-view.js +195 -0
  141. package/dist/heart/mailbox/readers/agent-machine.js +382 -0
  142. package/dist/heart/mailbox/readers/continuity-readers.js +338 -0
  143. package/dist/heart/mailbox/readers/mail.js +367 -0
  144. package/dist/heart/mailbox/readers/runtime-readers.js +651 -0
  145. package/dist/heart/mailbox/readers/sessions.js +232 -0
  146. package/dist/heart/mailbox/readers/shared.js +111 -0
  147. package/dist/heart/mcp/mcp-server.js +656 -0
  148. package/dist/heart/migrate-config.js +100 -0
  149. package/dist/heart/model-capabilities.js +59 -0
  150. package/dist/heart/platform.js +81 -0
  151. package/dist/heart/progress-story.js +42 -0
  152. package/dist/heart/provider-attempt.js +134 -0
  153. package/dist/heart/provider-binding-resolver.js +267 -0
  154. package/dist/heart/provider-credentials.js +425 -0
  155. package/dist/heart/provider-failover.js +301 -0
  156. package/dist/heart/provider-models.js +81 -0
  157. package/dist/heart/provider-ping.js +262 -0
  158. package/dist/heart/provider-readiness-cache.js +40 -0
  159. package/dist/heart/provider-visibility.js +188 -0
  160. package/dist/heart/providers/anthropic-token.js +131 -0
  161. package/dist/heart/providers/anthropic.js +202 -50
  162. package/dist/heart/providers/azure.js +104 -13
  163. package/dist/heart/providers/error-classification.js +127 -0
  164. package/dist/heart/providers/github-copilot.js +145 -0
  165. package/dist/heart/providers/minimax-vlm.js +189 -0
  166. package/dist/heart/providers/minimax.js +29 -7
  167. package/dist/heart/providers/openai-codex.js +63 -39
  168. package/dist/heart/runtime-capability-check.js +170 -0
  169. package/dist/heart/runtime-credentials.js +367 -0
  170. package/dist/heart/runtime-cwd.js +87 -0
  171. package/dist/heart/sense-truth.js +70 -0
  172. package/dist/heart/session-activity.js +190 -0
  173. package/dist/heart/session-events.js +1149 -0
  174. package/dist/heart/session-playback-cli-main.js +5 -0
  175. package/dist/heart/session-playback-cli.js +36 -0
  176. package/dist/heart/session-playback.js +231 -0
  177. package/dist/heart/session-stats-cli-main.js +5 -0
  178. package/dist/heart/session-stats.js +182 -0
  179. package/dist/heart/session-transcript.js +243 -0
  180. package/dist/heart/start-of-turn-packet.js +345 -0
  181. package/dist/heart/streaming.js +129 -34
  182. package/dist/heart/sync-classification.js +176 -0
  183. package/dist/heart/sync.js +449 -0
  184. package/dist/heart/target-resolution.js +127 -0
  185. package/dist/heart/tempo.js +93 -0
  186. package/dist/heart/temporal-view.js +41 -0
  187. package/dist/heart/timeouts.js +101 -0
  188. package/dist/heart/tool-activity-callbacks.js +59 -0
  189. package/dist/heart/tool-description.js +143 -0
  190. package/dist/heart/tool-friction.js +55 -0
  191. package/dist/heart/tool-loop.js +200 -0
  192. package/dist/heart/turn-context.js +421 -0
  193. package/dist/heart/turn-coordinator.js +28 -0
  194. package/dist/heart/versioning/ouro-bot-global-installer.js +129 -0
  195. package/dist/heart/{daemon → versioning}/ouro-bot-wrapper.js +1 -1
  196. package/dist/heart/versioning/ouro-path-installer.js +426 -0
  197. package/dist/heart/{daemon → versioning}/ouro-uti.js +11 -2
  198. package/dist/heart/versioning/ouro-version-manager.js +295 -0
  199. package/dist/heart/versioning/staged-restart.js +146 -0
  200. package/dist/heart/versioning/update-checker.js +116 -0
  201. package/dist/heart/versioning/update-hooks.js +142 -0
  202. package/dist/heart/versioning/wrapper-publish-guard.js +86 -0
  203. package/dist/mailbox-ui/assets/index-B-461hes.js +61 -0
  204. package/dist/mailbox-ui/assets/index-BPr5vNuM.css +1 -0
  205. package/dist/mailbox-ui/index.html +15 -0
  206. package/dist/mailroom/attention.js +167 -0
  207. package/dist/mailroom/autonomy.js +209 -0
  208. package/dist/mailroom/blob-store.js +700 -0
  209. package/dist/mailroom/body-cache.js +61 -0
  210. package/dist/mailroom/core.js +788 -0
  211. package/dist/mailroom/entry.js +160 -0
  212. package/dist/mailroom/file-store.js +457 -0
  213. package/dist/mailroom/mbox-import.js +393 -0
  214. package/dist/mailroom/migration.js +164 -0
  215. package/dist/mailroom/outbound.js +380 -0
  216. package/dist/mailroom/policy.js +263 -0
  217. package/dist/mailroom/reader.js +233 -0
  218. package/dist/mailroom/search-cache.js +268 -0
  219. package/dist/mailroom/search-relevance.js +319 -0
  220. package/dist/mailroom/smtp-ingress.js +176 -0
  221. package/dist/mailroom/source-state.js +176 -0
  222. package/dist/mailroom/thread.js +109 -0
  223. package/dist/mailroom/travel-extract.js +89 -0
  224. package/dist/mind/bundle-manifest.js +77 -1
  225. package/dist/mind/context.js +174 -94
  226. package/dist/mind/diary-integrity.js +60 -0
  227. package/dist/mind/{memory.js → diary.js} +84 -96
  228. package/dist/mind/embedding-provider.js +60 -0
  229. package/dist/mind/file-state.js +179 -0
  230. package/dist/mind/first-impressions.js +16 -2
  231. package/dist/mind/friends/channel.js +74 -0
  232. package/dist/mind/friends/group-context.js +144 -0
  233. package/dist/mind/friends/resolver.js +54 -2
  234. package/dist/mind/friends/store-file.js +58 -3
  235. package/dist/mind/friends/trust-explanation.js +74 -0
  236. package/dist/mind/friends/types.js +10 -2
  237. package/dist/mind/journal-index.js +161 -0
  238. package/dist/mind/note-search.js +268 -0
  239. package/dist/mind/obligation-steering.js +221 -0
  240. package/dist/mind/pending.js +76 -9
  241. package/dist/mind/phrases.js +1 -0
  242. package/dist/mind/prompt-refresh.js +3 -2
  243. package/dist/mind/prompt.js +1267 -130
  244. package/dist/mind/provenance-trust.js +26 -0
  245. package/dist/mind/scrutiny.js +173 -0
  246. package/dist/mind/token-estimate.js +8 -12
  247. package/dist/nerves/cli-logging.js +22 -3
  248. package/dist/nerves/coverage/audit-rules.js +15 -6
  249. package/dist/nerves/coverage/audit.js +28 -2
  250. package/dist/nerves/coverage/cli.js +1 -1
  251. package/dist/nerves/coverage/contract.js +5 -5
  252. package/dist/nerves/coverage/file-completeness.js +129 -5
  253. package/dist/nerves/coverage/run-artifacts.js +1 -1
  254. package/dist/nerves/event-buffer.js +111 -0
  255. package/dist/nerves/index.js +224 -4
  256. package/dist/nerves/observation.js +20 -0
  257. package/dist/nerves/redact.js +79 -0
  258. package/dist/nerves/review/cli-main.js +5 -0
  259. package/dist/nerves/review/cli.js +156 -0
  260. package/dist/nerves/review/core.js +152 -0
  261. package/dist/nerves/runtime.js +5 -1
  262. package/dist/repertoire/ado-client.js +17 -56
  263. package/dist/repertoire/ado-semantic.js +11 -10
  264. package/dist/repertoire/api-client.js +97 -0
  265. package/dist/repertoire/bitwarden-store.js +997 -0
  266. package/dist/repertoire/bundle-templates.js +72 -0
  267. package/dist/repertoire/bw-installer.js +180 -0
  268. package/dist/repertoire/coding/codex-jsonl.js +64 -0
  269. package/dist/repertoire/coding/context-pack.js +330 -0
  270. package/dist/repertoire/coding/feedback.js +301 -0
  271. package/dist/repertoire/coding/index.js +4 -1
  272. package/dist/repertoire/coding/manager.js +220 -13
  273. package/dist/repertoire/coding/spawner.js +58 -12
  274. package/dist/repertoire/coding/tools.js +209 -7
  275. package/dist/repertoire/commerce-errors.js +109 -0
  276. package/dist/repertoire/commerce-self-test.js +156 -0
  277. package/dist/repertoire/credential-access.js +178 -0
  278. package/dist/repertoire/data/ado-endpoints.json +188 -0
  279. package/dist/repertoire/duffel-client.js +185 -0
  280. package/dist/repertoire/github-client.js +14 -55
  281. package/dist/repertoire/graph-client.js +11 -52
  282. package/dist/repertoire/guardrails.js +396 -0
  283. package/dist/repertoire/mcp-client.js +295 -0
  284. package/dist/repertoire/mcp-manager.js +362 -0
  285. package/dist/repertoire/mcp-tools.js +63 -0
  286. package/dist/repertoire/shell-sessions.js +133 -0
  287. package/dist/repertoire/skills.js +15 -24
  288. package/dist/repertoire/stripe-client.js +131 -0
  289. package/dist/repertoire/tasks/board.js +43 -5
  290. package/dist/repertoire/tasks/fix.js +182 -0
  291. package/dist/repertoire/tasks/index.js +39 -13
  292. package/dist/repertoire/tasks/lifecycle.js +2 -2
  293. package/dist/repertoire/tasks/parser.js +3 -2
  294. package/dist/repertoire/tasks/scanner.js +194 -37
  295. package/dist/repertoire/tasks/transitions.js +16 -79
  296. package/dist/repertoire/tool-results.js +29 -0
  297. package/dist/repertoire/tools-attachments.js +317 -0
  298. package/dist/repertoire/tools-awaiting.js +360 -0
  299. package/dist/repertoire/tools-base.js +56 -707
  300. package/dist/repertoire/tools-bluebubbles.js +94 -0
  301. package/dist/repertoire/tools-bridge.js +142 -0
  302. package/dist/repertoire/tools-bundle.js +984 -0
  303. package/dist/repertoire/tools-config.js +185 -0
  304. package/dist/repertoire/tools-continuity.js +248 -0
  305. package/dist/repertoire/tools-credential.js +381 -0
  306. package/dist/repertoire/tools-files.js +342 -0
  307. package/dist/repertoire/tools-flight.js +224 -0
  308. package/dist/repertoire/tools-flow.js +119 -0
  309. package/dist/repertoire/tools-github.js +1 -7
  310. package/dist/repertoire/tools-mail.js +1916 -0
  311. package/dist/repertoire/tools-notes.js +421 -0
  312. package/dist/repertoire/tools-obligations.js +142 -0
  313. package/dist/repertoire/tools-runtime.js +61 -0
  314. package/dist/repertoire/tools-session.js +809 -0
  315. package/dist/repertoire/tools-shell.js +120 -0
  316. package/dist/repertoire/tools-stripe.js +180 -0
  317. package/dist/repertoire/tools-surface.js +345 -0
  318. package/dist/repertoire/tools-teams.js +64 -61
  319. package/dist/repertoire/tools-travel.js +125 -0
  320. package/dist/repertoire/tools-trip.js +604 -0
  321. package/dist/repertoire/tools-user-profile.js +144 -0
  322. package/dist/repertoire/tools-vault.js +40 -0
  323. package/dist/repertoire/tools-voice.js +144 -0
  324. package/dist/repertoire/tools.js +154 -98
  325. package/dist/repertoire/travel-api-client.js +360 -0
  326. package/dist/repertoire/user-profile.js +131 -0
  327. package/dist/repertoire/vault-setup.js +246 -0
  328. package/dist/repertoire/vault-unlock.js +594 -0
  329. package/dist/scripts/claude-code-hook.js +41 -0
  330. package/dist/scripts/claude-code-stop-hook.js +47 -0
  331. package/dist/senses/attention-queue.js +116 -0
  332. package/dist/senses/await-turn-message.js +58 -0
  333. package/dist/senses/bluebubbles/active-turns.js +216 -0
  334. package/dist/senses/bluebubbles/attachment-cache.js +53 -0
  335. package/dist/senses/bluebubbles/attachment-download.js +137 -0
  336. package/dist/senses/bluebubbles/client.js +685 -0
  337. package/dist/senses/bluebubbles/entry.js +77 -0
  338. package/dist/senses/bluebubbles/inbound-log.js +126 -0
  339. package/dist/senses/bluebubbles/index.js +2548 -0
  340. package/dist/senses/bluebubbles/media.js +389 -0
  341. package/dist/senses/{bluebubbles-model.js → bluebubbles/model.js} +45 -16
  342. package/dist/senses/{bluebubbles-mutation-log.js → bluebubbles/mutation-log.js} +46 -6
  343. package/dist/senses/bluebubbles/processed-log.js +133 -0
  344. package/dist/senses/bluebubbles/replay.js +137 -0
  345. package/dist/senses/bluebubbles/runtime-state.js +137 -0
  346. package/dist/senses/bluebubbles/session-cleanup.js +72 -0
  347. package/dist/senses/bluebubbles-meta-guard.js +40 -0
  348. package/dist/senses/cli/bracketed-paste.js +82 -0
  349. package/dist/senses/cli/image-paste.js +287 -0
  350. package/dist/senses/cli/image-ref-navigation.js +75 -0
  351. package/dist/senses/cli/ink-app.js +156 -0
  352. package/dist/senses/cli/inline-diff.js +64 -0
  353. package/dist/senses/cli/input-keys.js +174 -0
  354. package/dist/senses/cli/kill-ring.js +86 -0
  355. package/dist/senses/cli/message-list.js +51 -0
  356. package/dist/senses/cli/ouro-tui.js +607 -0
  357. package/dist/senses/cli/spinner-imperative.js +135 -0
  358. package/dist/senses/cli/spinner.js +101 -0
  359. package/dist/senses/cli/status-line.js +60 -0
  360. package/dist/senses/cli/streaming-markdown.js +526 -0
  361. package/dist/senses/cli/tool-display.js +85 -0
  362. package/dist/senses/cli/tool-render.js +85 -0
  363. package/dist/senses/cli/tui-store.js +240 -0
  364. package/dist/senses/cli/virtual-list.js +35 -0
  365. package/dist/senses/cli-entry.js +60 -8
  366. package/dist/senses/cli-layout.js +187 -0
  367. package/dist/senses/cli.js +777 -264
  368. package/dist/senses/commands.js +66 -3
  369. package/dist/senses/continuity.js +94 -0
  370. package/dist/senses/habit-turn-message.js +108 -0
  371. package/dist/senses/inner-dialog-worker.js +209 -16
  372. package/dist/senses/inner-dialog.js +682 -91
  373. package/dist/senses/mail-entry.js +66 -0
  374. package/dist/senses/mail.js +379 -0
  375. package/dist/senses/pipeline.js +751 -0
  376. package/dist/senses/proactive-content-guard.js +51 -0
  377. package/dist/senses/shared-turn.js +392 -0
  378. package/dist/senses/surface-tool.js +70 -0
  379. package/dist/senses/teams-entry.js +60 -8
  380. package/dist/senses/teams.js +925 -195
  381. package/dist/senses/trust-gate.js +207 -2
  382. package/dist/senses/voice/audio-playback.js +237 -0
  383. package/dist/senses/voice/audio-routing.js +119 -0
  384. package/dist/senses/voice/elevenlabs.js +202 -0
  385. package/dist/senses/voice/floor-control.js +431 -0
  386. package/dist/senses/voice/floor-controller.js +115 -0
  387. package/dist/senses/voice/golden-path.js +116 -0
  388. package/dist/senses/voice/index.js +29 -0
  389. package/dist/senses/voice/meeting.js +113 -0
  390. package/dist/senses/voice/outbound.js +190 -0
  391. package/dist/senses/voice/phone.js +33 -0
  392. package/dist/senses/voice/playback.js +139 -0
  393. package/dist/senses/voice/realtime-eval.js +496 -0
  394. package/dist/senses/voice/realtime-trace.js +531 -0
  395. package/dist/senses/voice/transcript.js +70 -0
  396. package/dist/senses/voice/turn.js +191 -0
  397. package/dist/senses/voice/twilio-phone-runtime.js +807 -0
  398. package/dist/senses/voice/twilio-phone.js +5077 -0
  399. package/dist/senses/voice/types.js +2 -0
  400. package/dist/senses/voice/whisper.js +161 -0
  401. package/dist/senses/voice-entry.js +81 -0
  402. package/dist/senses/voice-realtime-eval-command.js +99 -0
  403. package/dist/senses/voice-realtime-eval-entry.js +21 -0
  404. package/dist/senses/voice-twilio-entry.js +87 -0
  405. package/dist/trips/core.js +138 -0
  406. package/dist/trips/store.js +265 -0
  407. package/package.json +52 -7
  408. package/skills/agent-commerce.md +106 -0
  409. package/skills/browser-navigation.md +117 -0
  410. package/skills/commerce-setup-guide.md +116 -0
  411. package/skills/commerce-setup.md +84 -0
  412. package/skills/configure-dev-tools.md +99 -0
  413. package/skills/travel-planning.md +138 -0
  414. package/AdoptionSpecialist.ouro/agent.json +0 -20
  415. package/AdoptionSpecialist.ouro/psyche/SOUL.md +0 -22
  416. package/dist/heart/daemon/specialist-orchestrator.js +0 -160
  417. package/dist/heart/daemon/specialist-prompt.js +0 -40
  418. package/dist/heart/daemon/specialist-session.js +0 -142
  419. package/dist/heart/daemon/specialist-tools.js +0 -128
  420. package/dist/heart/daemon/subagent-installer.js +0 -125
  421. package/dist/inner-worker-entry.js +0 -4
  422. package/dist/mind/associative-recall.js +0 -197
  423. package/dist/senses/bluebubbles-client.js +0 -279
  424. package/dist/senses/bluebubbles-entry.js +0 -11
  425. package/dist/senses/bluebubbles.js +0 -332
  426. package/subagents/README.md +0 -73
  427. package/subagents/work-doer.md +0 -233
  428. package/subagents/work-merger.md +0 -624
  429. package/subagents/work-planner.md +0 -373
  430. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/basilisk.md +0 -0
  431. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/jafar.md +0 -0
  432. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/jormungandr.md +0 -0
  433. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/kaa.md +0 -0
  434. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/medusa.md +0 -0
  435. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/nagini.md +0 -0
  436. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/ouroboros.md +0 -0
  437. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/python.md +0 -0
  438. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/quetzalcoatl.md +0 -0
  439. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/sir-hiss.md +0 -0
  440. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/the-snake.md +0 -0
@@ -1,96 +1,150 @@
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.isChatStyleChannel = isChatStyleChannel;
10
+ exports.isExternalStateQuery = isExternalStateQuery;
11
+ exports.getSettleRetryError = getSettleRetryError;
10
12
  exports.stripLastToolCalls = stripLastToolCalls;
11
- exports.isTransientError = isTransientError;
12
- exports.classifyTransientError = classifyTransientError;
13
+ exports.repairOrphanedToolCalls = repairOrphanedToolCalls;
13
14
  exports.runAgent = runAgent;
14
15
  const config_1 = require("./config");
15
16
  const identity_1 = require("./identity");
16
17
  const tools_1 = require("../repertoire/tools");
17
18
  const channel_1 = require("../mind/friends/channel");
18
- // Kick detection preserved but disabled — see comment in agent loop below.
19
- // import { detectKick } from "./kicks";
20
- // import type { KickReason } from "./kicks";
19
+ const tools_2 = require("../repertoire/tools");
21
20
  const runtime_1 = require("../nerves/runtime");
22
21
  const context_1 = require("../mind/context");
23
22
  const prompt_1 = require("../mind/prompt");
24
- const associative_recall_1 = require("../mind/associative-recall");
23
+ const kept_notes_1 = require("./kept-notes");
24
+ const error_classification_1 = require("./providers/error-classification");
25
25
  const anthropic_1 = require("./providers/anthropic");
26
26
  const azure_1 = require("./providers/azure");
27
27
  const minimax_1 = require("./providers/minimax");
28
28
  const openai_codex_1 = require("./providers/openai-codex");
29
- let _providerRuntime = null;
30
- function createProviderRegistry() {
31
- const factories = {
32
- azure: azure_1.createAzureProviderRuntime,
33
- anthropic: anthropic_1.createAnthropicProviderRuntime,
34
- minimax: minimax_1.createMinimaxProviderRuntime,
35
- "openai-codex": openai_codex_1.createOpenAICodexProviderRuntime,
29
+ const github_copilot_1 = require("./providers/github-copilot");
30
+ const identity_2 = require("./identity");
31
+ const socket_client_1 = require("./daemon/socket-client");
32
+ const obligations_1 = require("../arc/obligations");
33
+ const tool_loop_1 = require("./tool-loop");
34
+ const packets_1 = require("../arc/packets");
35
+ const tool_friction_1 = require("./tool-friction");
36
+ const provider_models_1 = require("./provider-models");
37
+ const provider_credentials_1 = require("./provider-credentials");
38
+ const provider_attempt_1 = require("./provider-attempt");
39
+ const _providerRuntimes = {
40
+ human: null,
41
+ agent: null,
42
+ };
43
+ function providerLaneForFacing(facing) {
44
+ return facing === "human" ? "outward" : "inner";
45
+ }
46
+ function resolveRuntimeProviderBinding(facing) {
47
+ const lane = providerLaneForFacing(facing);
48
+ const config = (0, identity_1.loadAgentConfig)();
49
+ const facingConfig = facing === "human" ? config.humanFacing : config.agentFacing;
50
+ return { lane, provider: facingConfig.provider, model: facingConfig.model };
51
+ }
52
+ async function getProviderRuntimeFingerprint(facing) {
53
+ const agentName = (0, identity_2.getAgentName)();
54
+ const binding = resolveRuntimeProviderBinding(facing);
55
+ const credential = await (0, provider_credentials_1.readProviderCredentialRecord)(agentName, binding.provider);
56
+ if (!credential.ok) {
57
+ throw new Error([
58
+ `${binding.lane} provider ${binding.provider} (${binding.model}) has no credentials for ${agentName}.`,
59
+ credential.error,
60
+ `Run \`ouro auth --agent ${agentName} --provider ${binding.provider}\`.`,
61
+ ].join("\n"));
62
+ }
63
+ return {
64
+ binding,
65
+ fingerprint: JSON.stringify({
66
+ lane: binding.lane,
67
+ provider: binding.provider,
68
+ model: binding.model,
69
+ credentialRevision: credential.record.revision,
70
+ }),
71
+ credential: credential.record,
36
72
  };
73
+ }
74
+ function createProviderRegistry() {
37
75
  return {
38
- resolve() {
39
- const provider = (0, identity_1.loadAgentConfig)().provider;
40
- return factories[provider]();
76
+ resolve(provider, model, credential) {
77
+ const providerConfig = { ...credential.config, ...credential.credentials };
78
+ switch (provider) {
79
+ case "azure":
80
+ return (0, azure_1.createAzureProviderRuntime)(model, providerConfig);
81
+ case "anthropic":
82
+ return (0, anthropic_1.createAnthropicProviderRuntime)(model, providerConfig);
83
+ case "minimax":
84
+ return (0, minimax_1.createMinimaxProviderRuntime)(model, providerConfig);
85
+ case "openai-codex":
86
+ return (0, openai_codex_1.createOpenAICodexProviderRuntime)(model, providerConfig);
87
+ case "github-copilot":
88
+ return (0, github_copilot_1.createGithubCopilotProviderRuntime)(model, providerConfig);
89
+ }
41
90
  },
42
91
  };
43
92
  }
44
- function getProviderRuntime() {
45
- if (!_providerRuntime) {
46
- try {
47
- _providerRuntime = createProviderRegistry().resolve();
48
- }
49
- catch (error) {
50
- const msg = error instanceof Error ? error.message : String(error);
51
- (0, runtime_1.emitNervesEvent)({
52
- level: "error",
53
- event: "engine.provider_init_error",
54
- component: "engine",
55
- message: msg,
56
- meta: {},
57
- });
58
- // eslint-disable-next-line no-console -- pre-boot guard: provider init failure
59
- console.error(`\n[fatal] ${msg}\n`);
60
- process.exit(1);
61
- throw new Error("unreachable");
62
- }
63
- if (!_providerRuntime) {
64
- (0, runtime_1.emitNervesEvent)({
65
- level: "error",
66
- event: "engine.provider_init_error",
67
- component: "engine",
68
- message: "provider runtime could not be initialized.",
69
- meta: {},
70
- });
71
- process.exit(1);
72
- throw new Error("unreachable");
93
+ async function getProviderRuntime(facing = "human") {
94
+ try {
95
+ const { binding, fingerprint, credential } = await getProviderRuntimeFingerprint(facing);
96
+ const cached = _providerRuntimes[facing];
97
+ if (!cached || cached.fingerprint !== fingerprint) {
98
+ const runtime = createProviderRegistry().resolve(binding.provider, binding.model, credential);
99
+ _providerRuntimes[facing] = runtime ? { fingerprint, runtime } : null;
73
100
  }
74
101
  }
75
- return _providerRuntime;
102
+ catch (error) {
103
+ const msg = error instanceof Error ? error.message : String(error);
104
+ (0, runtime_1.emitNervesEvent)({
105
+ level: "error",
106
+ event: "engine.provider_init_error",
107
+ component: "engine",
108
+ message: msg,
109
+ meta: { facing },
110
+ });
111
+ // eslint-disable-next-line no-console -- pre-boot guard: provider init failure
112
+ console.error(`\n[fatal] ${msg}\n`);
113
+ throw error instanceof Error ? error : new Error(msg);
114
+ }
115
+ if (!_providerRuntimes[facing]) {
116
+ const msg = "provider runtime could not be initialized.";
117
+ (0, runtime_1.emitNervesEvent)({
118
+ level: "error",
119
+ event: "engine.provider_init_error",
120
+ component: "engine",
121
+ message: msg,
122
+ meta: { facing },
123
+ });
124
+ // eslint-disable-next-line no-console -- pre-boot guard: provider init failure
125
+ console.error(`\n[fatal] ${msg}\n`);
126
+ throw new Error(msg);
127
+ }
128
+ return _providerRuntimes[facing].runtime;
76
129
  }
77
130
  /**
78
- * Clear the cached provider runtime so the next call to getProviderRuntime()
79
- * re-creates it from current config. Used by the adoption specialist to
80
- * switch provider context without restarting the process.
131
+ * Clear the cached provider runtime so the next access re-creates it from
132
+ * current config. Runtime access also auto-refreshes when the selected
133
+ * provider fingerprint changes on disk.
81
134
  */
82
135
  function resetProviderRuntime() {
83
- _providerRuntime = null;
136
+ _providerRuntimes.human = null;
137
+ _providerRuntimes.agent = null;
84
138
  }
85
- function getModel() {
86
- return getProviderRuntime().model;
139
+ function getModel(facing = "human") {
140
+ return resolveRuntimeProviderBinding(facing).model;
87
141
  }
88
- function getProvider() {
89
- return getProviderRuntime().id;
142
+ function getProvider(facing = "human") {
143
+ return resolveRuntimeProviderBinding(facing).provider;
90
144
  }
91
- function createSummarize() {
145
+ function createSummarize(facing = "human") {
92
146
  return async (transcript, instruction) => {
93
- const runtime = getProviderRuntime();
147
+ const runtime = await getProviderRuntime(facing);
94
148
  const client = runtime.client;
95
149
  const response = await client.chat.completions.create({
96
150
  model: runtime.model,
@@ -103,34 +157,168 @@ function createSummarize() {
103
157
  return response.choices?.[0]?.message?.content ?? transcript;
104
158
  };
105
159
  }
106
- function getProviderDisplayLabel() {
107
- const model = getModel();
160
+ function getProviderDisplayLabel(facing = "human") {
161
+ const binding = resolveRuntimeProviderBinding(facing);
162
+ const provider = binding.provider;
163
+ const model = binding.model || "unknown";
108
164
  const providerLabelBuilders = {
109
- azure: () => `azure openai (${(0, config_1.getAzureConfig)().deployment || "default"}, model: ${model})`,
165
+ azure: () => {
166
+ return `azure openai (model: ${model})`;
167
+ },
110
168
  anthropic: () => `anthropic (${model})`,
111
169
  minimax: () => `minimax (${model})`,
112
170
  "openai-codex": () => `openai codex (${model})`,
171
+ /* v8 ignore next -- branch: tested via display label unit test @preserve */
172
+ "github-copilot": () => `github copilot (${model})`,
113
173
  };
114
- return providerLabelBuilders[getProvider()]();
174
+ return providerLabelBuilders[provider]();
175
+ }
176
+ /**
177
+ * Strip <think>...</think> blocks for the violation-detection check at the
178
+ * end of a streaming turn. Used to tell legitimate text-only responses
179
+ * apart from the MiniMax-M2.7 "only thinking, no tool call" violation
180
+ * shape. Mirrors the more thorough stripThinkBlocks helper in
181
+ * senses/shared-turn.ts (which is for operator-facing output) — kept
182
+ * inline here to avoid pulling senses/ into the core module's import graph.
183
+ */
184
+ function stripThinkBlocksForViolationCheck(input) {
185
+ let out = input;
186
+ for (;;) {
187
+ const open = out.indexOf("<think>");
188
+ if (open === -1)
189
+ break;
190
+ const close = out.indexOf("</think>", open + "<think>".length);
191
+ if (close === -1) {
192
+ out = out.slice(0, open);
193
+ break;
194
+ }
195
+ out = out.slice(0, open) + out.slice(close + "</think>".length);
196
+ }
197
+ return out.trim();
198
+ }
199
+ function hasFreshPendingWork(options) {
200
+ const pendingMessages = options?.pendingMessages;
201
+ if (!Array.isArray(pendingMessages))
202
+ return false;
203
+ return pendingMessages.some((message) => typeof message?.content === "string"
204
+ && message.content.trim().length > 0);
205
+ }
206
+ /** Chat-style channels expose the `speak` tool — outer human-conversation channels
207
+ * where mid-turn delivery is meaningful. Inner dialog has `ponder`. MCP returns
208
+ * synchronously. Mail is batch. Anything else (unknown channel) treats as non-chat. */
209
+ function isChatStyleChannel(channel) {
210
+ return channel === "cli" || channel === "teams" || channel === "bluebubbles" || channel === "voice";
211
+ }
212
+ // Sole-call tools must be the only tool call in a turn. When they appear
213
+ // alongside other tools, the sole-call tool is rejected with this message.
214
+ const SOLE_CALL_REJECTION = {
215
+ settle: "rejected: settle must be the only tool call. finish your work first, then call settle alone.",
216
+ observe: "rejected: observe must be the only tool call. call observe alone when you want to stay silent.",
217
+ rest: "rejected: rest must be the only tool call. finish your work first, then call rest alone.",
218
+ };
219
+ function parseSettlePayload(argumentsText) {
220
+ try {
221
+ const parsed = JSON.parse(argumentsText);
222
+ if (typeof parsed === "string") {
223
+ return { answer: parsed };
224
+ }
225
+ if (!parsed || typeof parsed !== "object") {
226
+ return {};
227
+ }
228
+ const answer = typeof parsed.answer === "string" ? parsed.answer : undefined;
229
+ const rawIntent = parsed.intent;
230
+ const intent = rawIntent === "complete" || rawIntent === "blocked" || rawIntent === "direct_reply"
231
+ ? rawIntent
232
+ : undefined;
233
+ return { answer, intent };
234
+ }
235
+ catch {
236
+ return {};
237
+ }
238
+ }
239
+ function parsePonderPayload(argumentsText) {
240
+ try {
241
+ const parsed = JSON.parse(argumentsText);
242
+ return parsed && typeof parsed === "object" ? parsed : {};
243
+ }
244
+ catch {
245
+ return {};
246
+ }
247
+ }
248
+ function parseSuccessCriteria(raw) {
249
+ if (typeof raw !== "string")
250
+ return null;
251
+ const criteria = raw
252
+ .split("\n")
253
+ .map((line) => line.replace(/^\s*[-*]\s*/, "").trim())
254
+ .filter((line) => line.length > 0);
255
+ return criteria.length > 0 ? criteria : null;
256
+ }
257
+ function parsePacketPayload(raw) {
258
+ if (typeof raw !== "string")
259
+ return null;
260
+ try {
261
+ const parsed = JSON.parse(raw);
262
+ return parsed && typeof parsed === "object" && !Array.isArray(parsed)
263
+ ? parsed
264
+ : null;
265
+ }
266
+ catch {
267
+ return null;
268
+ }
269
+ }
270
+ function normalizeLegacyPonderArgs(parsed) {
271
+ if (typeof parsed.thought !== "string" || parsed.thought.trim().length === 0) {
272
+ return parsed;
273
+ }
274
+ return {
275
+ action: "create",
276
+ kind: "reflection",
277
+ objective: parsed.thought.trim(),
278
+ summary: typeof parsed.say === "string" ? parsed.say.trim() : "",
279
+ success_criteria: "- preserve the thread for later work",
280
+ payload_json: "{}",
281
+ };
282
+ }
283
+ function buildPonderResult(packet, action, returnObligationId) {
284
+ return JSON.stringify({
285
+ ok: true,
286
+ packet_id: packet.id,
287
+ action,
288
+ status: packet.status,
289
+ return_obligation_id: returnObligationId,
290
+ }, null, 2);
291
+ }
292
+ /** Returns true when a tool call queries external state (GitHub, npm registry). */
293
+ function isExternalStateQuery(toolName, args) {
294
+ if (toolName !== "shell")
295
+ return false;
296
+ const cmd = String(args.command ?? "");
297
+ return /\bgh\s+(pr|run|api|issue)\b/.test(cmd) || /\bnpm\s+(view|info|show)\b/.test(cmd);
298
+ }
299
+ function getSettleRetryError(mustResolveBeforeHandoff, intent, sawSteeringFollowUp, _delegationDecision, sawSendMessageSelf, sawPonder, _sawQuerySession, currentObligation, innerJob, _sawExternalStateQuery) {
300
+ // Delegation adherence removed: the delegation decision is surfaced in the
301
+ // system prompt as a suggestion. Hard-gating settle caused infinite
302
+ // rejection loops where the agent couldn't respond to the user at all.
303
+ // The agent is free to follow or ignore the delegation hint.
304
+ // 2. Pending obligation not addressed
305
+ if (innerJob?.obligationStatus === "pending" && !sawSendMessageSelf && !sawPonder) {
306
+ 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.";
307
+ }
308
+ // 3. mustResolveBeforeHandoff + missing intent
309
+ if (mustResolveBeforeHandoff && !intent) {
310
+ 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.";
311
+ }
312
+ // 4. mustResolveBeforeHandoff + direct_reply without follow-up
313
+ if (mustResolveBeforeHandoff && intent === "direct_reply" && !sawSteeringFollowUp) {
314
+ 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.";
315
+ }
316
+ // 5. mustResolveBeforeHandoff + complete while a live return loop is still active
317
+ if (mustResolveBeforeHandoff && intent === "complete" && currentObligation && !sawSteeringFollowUp) {
318
+ 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.";
319
+ }
320
+ return null;
115
321
  }
116
- // Re-export tools, execTool, summarizeArgs from ./tools for backward compat
117
- var tools_2 = require("../repertoire/tools");
118
- Object.defineProperty(exports, "tools", { enumerable: true, get: function () { return tools_2.tools; } });
119
- Object.defineProperty(exports, "execTool", { enumerable: true, get: function () { return tools_2.execTool; } });
120
- Object.defineProperty(exports, "summarizeArgs", { enumerable: true, get: function () { return tools_2.summarizeArgs; } });
121
- Object.defineProperty(exports, "getToolsForChannel", { enumerable: true, get: function () { return tools_2.getToolsForChannel; } });
122
- // Re-export streaming functions for backward compat
123
- var streaming_1 = require("./streaming");
124
- Object.defineProperty(exports, "streamChatCompletion", { enumerable: true, get: function () { return streaming_1.streamChatCompletion; } });
125
- Object.defineProperty(exports, "streamResponsesApi", { enumerable: true, get: function () { return streaming_1.streamResponsesApi; } });
126
- Object.defineProperty(exports, "toResponsesInput", { enumerable: true, get: function () { return streaming_1.toResponsesInput; } });
127
- Object.defineProperty(exports, "toResponsesTools", { enumerable: true, get: function () { return streaming_1.toResponsesTools; } });
128
- // Re-export prompt functions for backward compat
129
- var prompt_2 = require("../mind/prompt");
130
- Object.defineProperty(exports, "buildSystem", { enumerable: true, get: function () { return prompt_2.buildSystem; } });
131
- // Re-export kick utilities for backward compat
132
- var kicks_1 = require("./kicks");
133
- Object.defineProperty(exports, "hasToolIntent", { enumerable: true, get: function () { return kicks_1.hasToolIntent; } });
134
322
  function upsertSystemPrompt(messages, systemText) {
135
323
  const systemMessage = { role: "system", content: systemText };
136
324
  if (messages[0]?.role === "system") {
@@ -160,6 +348,83 @@ function stripLastToolCalls(messages) {
160
348
  }
161
349
  }
162
350
  }
351
+ // Roles that end a tool-result scan. When scanning forward from an assistant
352
+ // message, stop at the next assistant or user message (tool results must be
353
+ // adjacent to their originating assistant message).
354
+ const TOOL_SCAN_BOUNDARY_ROLES = new Set(["assistant", "user"]);
355
+ // Repair orphaned tool_calls and tool results anywhere in the message history.
356
+ // 1. If an assistant message has tool_calls but missing tool results, inject synthetic error results.
357
+ // 2. If a tool result's tool_call_id doesn't match any tool_calls in a preceding assistant message, remove it.
358
+ // This prevents 400 errors from the API after an aborted turn.
359
+ //
360
+ // Position-aware: a tool result is orphaned when its tool_call_id hasn't been
361
+ // defined by an assistant message AT THIS POSITION yet. MiniMax-M2.7 reuses
362
+ // canonical tool_call_ids across turns, so the global-set check that this
363
+ // function used previously kept misordered tool results that MiniMax then
364
+ // rejected with error 2013 ("tool result's tool id not found"). Walking
365
+ // in order matches what MiniMax actually enforces.
366
+ function repairOrphanedToolCalls(messages) {
367
+ // Pass 1: walk in order, accumulate seen tool_call_ids per-position, and
368
+ // mark tool results for removal if their id hasn't been defined yet.
369
+ const seenCallIds = new Set();
370
+ const removeIndices = [];
371
+ for (let i = 0; i < messages.length; i++) {
372
+ const msg = messages[i];
373
+ if (msg.role === "assistant") {
374
+ const asst = msg;
375
+ if (asst.tool_calls) {
376
+ for (const tc of asst.tool_calls)
377
+ seenCallIds.add(tc.id);
378
+ }
379
+ continue;
380
+ }
381
+ if (msg.role === "tool") {
382
+ const toolMsg = msg;
383
+ if (!seenCallIds.has(toolMsg.tool_call_id)) {
384
+ removeIndices.push(i);
385
+ }
386
+ }
387
+ }
388
+ // Splice from the end so earlier indices stay valid.
389
+ for (let i = removeIndices.length - 1; i >= 0; i--) {
390
+ messages.splice(removeIndices[i], 1);
391
+ }
392
+ // Pass 3: inject synthetic results for tool_calls missing their tool results
393
+ for (let i = 0; i < messages.length; i++) {
394
+ const msg = messages[i];
395
+ if (msg.role !== "assistant")
396
+ continue;
397
+ const asst = msg;
398
+ if (!asst.tool_calls || asst.tool_calls.length === 0)
399
+ continue;
400
+ // Collect tool result IDs that follow this assistant message
401
+ const resultIds = new Set();
402
+ for (let j = i + 1; j < messages.length; j++) {
403
+ const following = messages[j];
404
+ if (following.role === "tool") {
405
+ resultIds.add(following.tool_call_id);
406
+ }
407
+ else if (TOOL_SCAN_BOUNDARY_ROLES.has(following.role)) {
408
+ break;
409
+ }
410
+ }
411
+ const missing = asst.tool_calls.filter((tc) => !resultIds.has(tc.id));
412
+ if (missing.length > 0) {
413
+ // AX rule: the agent must see what happened. Don't say "interrupted"
414
+ // — that's vague. Tell them the result was lost, possible causes,
415
+ // and what to do next.
416
+ const syntheticResults = missing.map((tc) => ({
417
+ role: "tool",
418
+ tool_call_id: tc.id,
419
+ content: "error: this tool call's result was lost — the previous turn ended before the tool finished (provider rejection, daemon interrupt, or the tool itself errored). if the work needs to be done, retry the tool call now.",
420
+ }));
421
+ let insertAt = i + 1;
422
+ while (insertAt < messages.length && messages[insertAt].role === "tool")
423
+ insertAt++;
424
+ messages.splice(insertAt, 0, ...syntheticResults);
425
+ }
426
+ }
427
+ }
163
428
  // Detect context overflow errors from Azure or MiniMax
164
429
  function isContextOverflow(err) {
165
430
  if (!(err instanceof Error))
@@ -174,49 +439,67 @@ function isContextOverflow(err) {
174
439
  return true;
175
440
  return false;
176
441
  }
177
- // Detect transient network errors worth retrying
178
- function isTransientError(err) {
179
- if (!(err instanceof Error))
180
- return false;
181
- const msg = err.message || "";
182
- const code = err.code || "";
183
- // Node.js network error codes
184
- if (["ECONNRESET", "ECONNREFUSED", "ENOTFOUND", "ETIMEDOUT", "EPIPE",
185
- "EAI_AGAIN", "EHOSTUNREACH", "ENETUNREACH", "ECONNABORTED"].includes(code))
186
- return true;
187
- // OpenAI SDK / fetch errors
188
- if (msg.includes("fetch failed"))
189
- return true;
190
- if (msg.includes("network") && !msg.includes("context"))
191
- return true;
192
- if (msg.includes("ECONNRESET") || msg.includes("ETIMEDOUT"))
193
- return true;
194
- if (msg.includes("socket hang up"))
195
- return true;
196
- if (msg.includes("getaddrinfo"))
197
- return true;
198
- // HTTP 429 / 500 / 502 / 503 / 504
199
- const status = err.status;
200
- if (status === 429 || status === 500 || status === 502 || status === 503 || status === 504)
201
- return true;
202
- return false;
442
+ const RETRY_LABELS = {
443
+ "auth-failure": "auth error",
444
+ "usage-limit": "usage limit",
445
+ "rate-limit": "rate limited",
446
+ "server-error": "server error",
447
+ "network-error": "network error",
448
+ "unknown": "error",
449
+ };
450
+ function waitForProviderRetry(delayMs, signal) {
451
+ if (!signal) {
452
+ return new Promise((resolve) => {
453
+ setTimeout(resolve, delayMs);
454
+ });
455
+ }
456
+ return new Promise((resolve, reject) => {
457
+ let timer;
458
+ const onAbort = () => {
459
+ clearTimeout(timer);
460
+ reject(new provider_attempt_1.ProviderAttemptAbortError());
461
+ };
462
+ timer = setTimeout(() => {
463
+ signal.removeEventListener("abort", onAbort);
464
+ resolve();
465
+ }, delayMs);
466
+ if (signal.aborted) {
467
+ onAbort();
468
+ return;
469
+ }
470
+ signal.addEventListener("abort", onAbort, { once: true });
471
+ });
203
472
  }
204
- function classifyTransientError(err) {
205
- if (!(err instanceof Error))
206
- return "unknown error";
207
- const status = err.status;
208
- if (status === 429)
209
- return "rate limited";
210
- if (status === 401 || status === 403)
211
- return "auth error";
212
- if (status && status >= 500)
213
- return "server error";
214
- return "network error";
473
+ function buildAuthFailureGuidance(provider, model, agentName, detail) {
474
+ const mismatch = (0, provider_models_1.getProviderModelMismatchMessage)(provider, model);
475
+ const modelLabel = model
476
+ ? mismatch
477
+ ? `${provider} [configured model: ${model}]`
478
+ : `${provider} (${model})`
479
+ : provider;
480
+ const lines = [`${modelLabel} authentication failed.`];
481
+ const cleanDetail = detail.replace(/\s+/g, " ").trim();
482
+ if (cleanDetail)
483
+ lines.push(`provider detail: ${cleanDetail.length > 300 ? `${cleanDetail.slice(0, 297)}...` : cleanDetail}`);
484
+ lines.push("");
485
+ lines.push("To keep using this provider:");
486
+ lines.push(` 1. Run \`ouro auth --agent ${agentName} --provider ${provider}\``);
487
+ if (mismatch) {
488
+ const defaultModel = (0, provider_models_1.getDefaultModelForProvider)(provider);
489
+ lines.push("");
490
+ lines.push("Config warning:");
491
+ lines.push(` - ${mismatch}`);
492
+ lines.push(" - Repair the configured model with:");
493
+ lines.push(` \`ouro config model --agent ${agentName} --facing human ${defaultModel}\``);
494
+ lines.push(` \`ouro config model --agent ${agentName} --facing agent ${defaultModel}\``);
495
+ }
496
+ lines.push("");
497
+ lines.push(`To use another configured provider instead, run \`ouro auth switch --agent ${agentName} --provider <provider>\`.`);
498
+ return lines.join("\n");
215
499
  }
216
- const MAX_RETRIES = 3;
217
- const RETRY_BASE_MS = 2000;
218
500
  async function runAgent(messages, callbacks, channel, signal, options) {
219
- const providerRuntime = getProviderRuntime();
501
+ const facing = (0, channel_1.channelToFacing)(channel);
502
+ let providerRuntime = await getProviderRuntime(facing);
220
503
  const provider = providerRuntime.id;
221
504
  const toolChoiceRequired = options?.toolChoiceRequired ?? true;
222
505
  const traceId = options?.traceId;
@@ -240,10 +523,17 @@ async function runAgent(messages, callbacks, channel, signal, options) {
240
523
  // Refresh system prompt at start of each turn when channel is provided.
241
524
  // If refresh fails, keep existing system prompt (or inject a minimal safe fallback)
242
525
  // so turn execution remains consistent and non-fatal.
526
+ let structuredSystemPrompt;
243
527
  if (channel) {
244
528
  try {
245
- const refreshed = await (0, prompt_1.buildSystem)(channel, options, currentContext);
246
- upsertSystemPrompt(messages, refreshed);
529
+ const buildSystemOptions = {
530
+ ...options,
531
+ providerCapabilities: providerRuntime.capabilities,
532
+ supportedReasoningEfforts: providerRuntime.supportedReasoningEfforts,
533
+ };
534
+ const refreshed = await (0, prompt_1.buildSystem)(channel, buildSystemOptions, currentContext);
535
+ structuredSystemPrompt = refreshed;
536
+ upsertSystemPrompt(messages, (0, prompt_1.flattenSystemPrompt)(refreshed));
247
537
  }
248
538
  catch (error) {
249
539
  const hadExistingSystemPrompt = messages[0]?.role === "system" && typeof messages[0].content === "string";
@@ -264,32 +554,134 @@ async function runAgent(messages, callbacks, channel, signal, options) {
264
554
  });
265
555
  }
266
556
  }
267
- await (0, associative_recall_1.injectAssociativeRecall)(messages);
268
- // kickCount and lastKickReason preserved but unused while kick detection is disabled.
269
- // let kickCount = 0;
270
- // let lastKickReason: KickReason | null = null;
557
+ if (channel && options?.skipKeptNotes !== true) {
558
+ await (0, kept_notes_1.injectKeptNotes)(messages, {
559
+ channel,
560
+ friend: currentContext?.friend,
561
+ judge: async (input) => (0, kept_notes_1.createKeptNotesJudge)(await getProviderRuntime("agent"), signal)(input),
562
+ signal,
563
+ traceId,
564
+ });
565
+ }
271
566
  let done = false;
272
567
  let lastUsage;
273
568
  let overflowRetried = false;
274
- let retryCount = 0;
569
+ let outcome = "settled";
570
+ let completion;
571
+ let terminalError;
572
+ let terminalErrorClassification;
573
+ let sawSteeringFollowUp = false;
574
+ let mustResolveBeforeHandoffActive = options?.mustResolveBeforeHandoff === true;
575
+ let currentReasoningEffort = "medium";
576
+ let sawSendMessageSelf = false;
577
+ let sawPonder = false;
578
+ let sawQuerySession = false;
579
+ let sawBridgeManage = false;
580
+ let sawExternalStateQuery = false;
581
+ // Once-per-turn flag for the fresh-work rest gate. Without this, an agent
582
+ // that called rest, was told "fresh work arrived", processed the items,
583
+ // and called rest again would get the same message forever — the gate
584
+ // condition is read from the turn-start snapshot of pendingMessages,
585
+ // which doesn't update mid-turn. The agent only needs to be told once;
586
+ // after that, repeated rest attempts mean they've acknowledged.
587
+ let freshWorkGateFired = false;
588
+ // Counter for "no tool call returned despite tool_choice=required" violations.
589
+ // MiniMax reasoning models occasionally emit only a <think>...</think>
590
+ // block and stop, without any tool call — even when tool_choice is set to
591
+ // "required". This is a provider-level violation; the harness retries with
592
+ // a corrective nudge up to a small cap rather than silently accepting an
593
+ // empty turn.
594
+ let noToolCallRetries = 0;
595
+ const NO_TOOL_CALL_MAX_RETRIES = 2;
596
+ const toolLoopState = (0, tool_loop_1.createToolLoopState)();
597
+ const toolFrictionLedger = (0, tool_friction_1.createToolFrictionLedger)();
598
+ const finishTerminalProviderError = (error, classification) => {
599
+ terminalError = error;
600
+ terminalErrorClassification = classification;
601
+ /* v8 ignore start — auth-failure guidance: tested via provider error classification tests @preserve */
602
+ if (terminalErrorClassification === "auth-failure") {
603
+ const agentName = (0, identity_2.getAgentName)();
604
+ const currentProvider = providerRuntime.id;
605
+ callbacks.onError(new Error(buildAuthFailureGuidance(currentProvider, providerRuntime.model, agentName, terminalError.message)), "terminal");
606
+ }
607
+ else {
608
+ callbacks.onError(terminalError, "terminal");
609
+ }
610
+ /* v8 ignore stop */
611
+ const errorDetails = (0, error_classification_1.extractProviderErrorDetails)(terminalError);
612
+ (0, runtime_1.emitNervesEvent)({
613
+ level: "error",
614
+ event: "engine.error",
615
+ trace_id: traceId,
616
+ component: "engine",
617
+ message: terminalError.message,
618
+ meta: {
619
+ provider: providerRuntime.id,
620
+ model: providerRuntime.model,
621
+ errorClassification: terminalErrorClassification,
622
+ ...(errorDetails.status !== undefined ? { httpStatus: errorDetails.status } : {}),
623
+ ...(errorDetails.bodyExcerpt ? { bodyExcerpt: errorDetails.bodyExcerpt } : {}),
624
+ summary: (0, error_classification_1.summarizeProviderError)(terminalError, terminalErrorClassification, providerRuntime.id, providerRuntime.model),
625
+ },
626
+ });
627
+ stripLastToolCalls(messages);
628
+ outcome = "errored";
629
+ done = true;
630
+ };
275
631
  // Prevent MaxListenersExceeded warning — each iteration adds a listener
276
632
  try {
277
633
  require("events").setMaxListeners(50, signal);
278
634
  }
279
635
  catch { /* unsupported */ }
280
636
  const toolPreferences = currentContext?.friend?.toolPreferences;
281
- const baseTools = (0, tools_1.getToolsForChannel)(channel ? (0, channel_1.getChannelCapabilities)(channel) : undefined, toolPreferences && Object.keys(toolPreferences).length > 0 ? toolPreferences : undefined);
637
+ 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);
638
+ // Augment tool context with reasoning effort controls from provider
639
+ const augmentedToolContext = options?.toolContext
640
+ ? {
641
+ ...options.toolContext,
642
+ supportedReasoningEfforts: providerRuntime.supportedReasoningEfforts,
643
+ setReasoningEffort: (level) => { currentReasoningEffort = level; },
644
+ activeWorkFrame: options?.activeWorkFrame,
645
+ }
646
+ : undefined;
282
647
  // Rebase provider-owned turn state from canonical messages at user-turn start.
283
648
  // This prevents stale provider caches from replaying prior-turn context.
284
649
  providerRuntime.resetTurnState(messages);
285
650
  while (!done) {
286
- // When toolChoiceRequired is true (the default), include final_answer
287
- // so the model can signal completion. With tool_choice: required, the
288
- // model must call a tool every turn final_answer is how it exits.
289
- // Overridable via options.toolChoiceRequired = false (e.g. CLI).
290
- const activeTools = toolChoiceRequired ? [...baseTools, tools_1.finalAnswerTool] : baseTools;
651
+ // Channel-based tool filtering:
652
+ // - Inner dialog: exclude send_message (delivery via surface), observe (no one to observe)
653
+ // - All outward channels (1:1, group, reaction): observe available
654
+ //
655
+ // ponder, settle/rest, surface, and observe are always assembled based on channel context.
656
+ // ponder is available in ALL channels (outer: think privately, inner: keep turning).
657
+ // Inner dialog gets restTool instead of settleTool (rest = end turn, gated by attention queue).
658
+ // toolChoiceRequired only controls whether tool_choice: "required" is set in the API call.
659
+ const isInnerDialog = channel === "inner";
660
+ const filteredBaseTools = isInnerDialog
661
+ ? baseTools.filter((t) => t.function.name !== "send_message")
662
+ : baseTools;
663
+ const activeTools = [
664
+ ...filteredBaseTools,
665
+ tools_1.ponderTool,
666
+ ...(isInnerDialog ? [tools_2.surfaceToolDef, tools_1.restTool] : []),
667
+ ...(!isInnerDialog ? [tools_1.observeTool] : []),
668
+ ...(!isInnerDialog ? [tools_1.settleTool] : []),
669
+ ...(isChatStyleChannel(channel ?? "") ? [tools_1.speakTool] : []),
670
+ ];
291
671
  const steeringFollowUps = options?.drainSteeringFollowUps?.() ?? [];
292
672
  if (steeringFollowUps.length > 0) {
673
+ const hasSupersedingFollowUp = steeringFollowUps.some((followUp) => followUp.effect === "clear_and_supersede");
674
+ if (hasSupersedingFollowUp) {
675
+ mustResolveBeforeHandoffActive = false;
676
+ options?.setMustResolveBeforeHandoff?.(false);
677
+ outcome = "superseded";
678
+ break;
679
+ }
680
+ if (steeringFollowUps.some((followUp) => followUp.effect === "set_no_handoff")) {
681
+ mustResolveBeforeHandoffActive = true;
682
+ options?.setMustResolveBeforeHandoff?.(true);
683
+ }
684
+ sawSteeringFollowUp = true;
293
685
  for (const followUp of steeringFollowUps) {
294
686
  messages.push({ role: "user", content: followUp.text });
295
687
  }
@@ -297,28 +689,129 @@ async function runAgent(messages, callbacks, channel, signal, options) {
297
689
  }
298
690
  // Yield so pending I/O (stdin Ctrl-C) can be processed between iterations
299
691
  await new Promise((r) => setImmediate(r));
300
- if (signal?.aborted)
692
+ if (signal?.aborted) {
693
+ outcome = "aborted";
301
694
  break;
695
+ }
302
696
  try {
303
- callbacks.onModelStart();
304
- const result = await providerRuntime.streamTurn({
305
- messages,
306
- activeTools,
307
- callbacks,
308
- signal,
309
- traceId,
310
- toolChoiceRequired,
697
+ const callProviderTurn = async () => {
698
+ callbacks.onModelStart();
699
+ try {
700
+ return await providerRuntime.streamTurn({
701
+ messages,
702
+ activeTools,
703
+ callbacks,
704
+ signal,
705
+ traceId,
706
+ toolChoiceRequired,
707
+ reasoningEffort: currentReasoningEffort,
708
+ eagerSettleStreaming: true,
709
+ systemPrompt: structuredSystemPrompt,
710
+ });
711
+ }
712
+ catch (error) {
713
+ if (signal?.aborted)
714
+ throw new provider_attempt_1.ProviderAttemptAbortError();
715
+ throw error;
716
+ }
717
+ };
718
+ const callProviderTurnWithOverflowRecovery = async () => {
719
+ try {
720
+ return await callProviderTurn();
721
+ }
722
+ catch (error) {
723
+ if (error instanceof provider_attempt_1.ProviderAttemptAbortError)
724
+ throw error;
725
+ if (isContextOverflow(error) && !overflowRetried) {
726
+ overflowRetried = true;
727
+ stripLastToolCalls(messages);
728
+ const { maxTokens, contextMargin } = (0, config_1.getContextConfig)();
729
+ const trimmed = (0, context_1.trimMessages)(messages, maxTokens, contextMargin, maxTokens * 2);
730
+ messages.splice(0, messages.length, ...trimmed);
731
+ providerRuntime.resetTurnState(messages);
732
+ callbacks.onError(new Error("context trimmed, retrying..."), "transient");
733
+ return callProviderTurn();
734
+ }
735
+ throw error;
736
+ }
737
+ };
738
+ const attempt = await (0, provider_attempt_1.runProviderAttempt)({
739
+ operation: "turn",
740
+ provider: providerRuntime.id,
741
+ model: providerRuntime.model,
742
+ run: callProviderTurnWithOverflowRecovery,
743
+ classifyError: (error) => providerRuntime.classifyError(error),
744
+ onRetry: async (record, maxAttempts) => {
745
+ const delayMs = record.delayMs;
746
+ const seconds = delayMs / 1000;
747
+ const cause = RETRY_LABELS[record.classification];
748
+ try {
749
+ await (0, provider_credentials_1.refreshProviderCredentialPool)((0, identity_2.getAgentName)(), {
750
+ preserveCachedOnFailure: true,
751
+ providers: [record.provider],
752
+ });
753
+ _providerRuntimes[facing] = null;
754
+ providerRuntime = await getProviderRuntime(facing);
755
+ providerRuntime.resetTurnState(messages);
756
+ }
757
+ catch (refreshError) {
758
+ (0, runtime_1.emitNervesEvent)({
759
+ level: "warn",
760
+ component: "engine",
761
+ event: "engine.provider_retry_refresh_failed",
762
+ message: "provider credential refresh failed during retry",
763
+ meta: { provider: record.provider, model: record.model, reason: refreshError instanceof Error ? refreshError.message : String(refreshError) },
764
+ });
765
+ }
766
+ callbacks.onError(new Error(`${cause}, retrying in ${seconds}s (${record.attempt}/${maxAttempts})...`), "transient");
767
+ },
768
+ sleep: async (delayMs) => {
769
+ await waitForProviderRetry(delayMs, signal);
770
+ providerRuntime.resetTurnState(messages);
771
+ },
311
772
  });
773
+ if (!attempt.ok) {
774
+ finishTerminalProviderError(attempt.error, attempt.classification);
775
+ continue;
776
+ }
777
+ const result = attempt.value;
312
778
  // Track usage from the latest API call
313
779
  if (result.usage)
314
780
  lastUsage = result.usage;
315
- retryCount = 0; // reset on success
316
781
  // SHARED: build CC-format assistant message from TurnResult
317
782
  const msg = {
318
783
  role: "assistant",
319
784
  };
320
- if (result.content)
321
- msg.content = result.content;
785
+ // Persist assistant content WITHOUT inline <think>...</think> blocks.
786
+ // Reasoning content already routed through onReasoningChunk for live
787
+ // surfacing and persisted separately as `_reasoning_items` for
788
+ // providers that support a reasoning channel; saving it inline AND
789
+ // alongside tool_calls causes MiniMax to reject the replayed turn
790
+ // with "tool result's tool id not found" (error code 2013) because
791
+ // it can't reconcile reasoning-with-tools in the same assistant
792
+ // message. Strip aggressively at persist so the next replay is
793
+ // clean; preserve the original reasoning trace on the message via
794
+ // `_inline_reasoning` so debug/audit paths can still see it.
795
+ if (result.content) {
796
+ const stripped = stripThinkBlocksForViolationCheck(result.content);
797
+ if (stripped.length > 0)
798
+ msg.content = stripped;
799
+ if (stripped.length !== result.content.length) {
800
+ msg._inline_reasoning = result.content;
801
+ (0, runtime_1.emitNervesEvent)({
802
+ level: "info",
803
+ component: "engine",
804
+ event: "engine.inline_reasoning_stripped",
805
+ message: "stripped inline <think> blocks from persisted assistant message; preserved on _inline_reasoning",
806
+ meta: {
807
+ provider: providerRuntime.id,
808
+ model: providerRuntime.model,
809
+ originalLength: result.content.length,
810
+ strippedLength: stripped.length,
811
+ },
812
+ });
813
+ }
814
+ }
322
815
  if (result.toolCalls.length)
323
816
  msg.tool_calls = result.toolCalls.map((tc) => ({
324
817
  id: tc.id,
@@ -331,52 +824,115 @@ async function runAgent(messages, callbacks, channel, signal, options) {
331
824
  if (reasoningItems.length > 0) {
332
825
  msg._reasoning_items = reasoningItems;
333
826
  }
827
+ // Store thinking blocks (Anthropic) on the assistant message for round-tripping
828
+ const thinkingItems = result.outputItems.filter((item) => "type" in item && (item.type === "thinking" || item.type === "redacted_thinking"));
829
+ if (thinkingItems.length > 0) {
830
+ msg._thinking_blocks = thinkingItems;
831
+ }
832
+ // Phase annotation for Codex provider
833
+ const hasPhaseAnnotation = providerRuntime.capabilities.has("phase-annotation");
834
+ const isSoleSettle = result.toolCalls.length === 1 && result.toolCalls[0].name === "settle";
835
+ if (hasPhaseAnnotation) {
836
+ msg.phase = isSoleSettle ? "settle" : "commentary";
837
+ }
838
+ // Detect the MiniMax "only-thinking, no tool call" violation: no tool
839
+ // calls returned, and the content is empty after stripping
840
+ // <think>...</think> blocks. This is a narrow check — legitimate
841
+ // content-only responses (text without think tags, or text outside
842
+ // think tags) still flow through the original "no tool calls →
843
+ // accept as-is" path so existing channels and tests are unaffected.
844
+ const onlyThinkContent = !result.toolCalls.length
845
+ && typeof result.content === "string"
846
+ && stripThinkBlocksForViolationCheck(result.content).length === 0
847
+ && result.content.length > 0;
334
848
  if (!result.toolCalls.length) {
335
- // Kick detection is disabled while tool_choice: required + final_answer
336
- // is the primary loop control mechanism. The model should never reach
337
- // this path (tool_choice: required forces a tool call), but if it does,
338
- // accept the response as-is rather than risk false-positive kicks.
339
- //
340
- // Preserved for future use re-enable by uncommenting:
341
- // const kick = detectKick(result.content, options);
342
- // if (kick) {
343
- // kickCount++;
344
- // lastKickReason = kick.reason;
345
- // callbacks.onKick?.();
346
- // const kickContent = result.content
347
- // ? result.content + "\n\n" + kick.message
348
- // : kick.message;
349
- // messages.push({ role: "assistant", content: kickContent });
350
- // providerRuntime.resetTurnState(messages);
351
- // continue;
352
- // }
849
+ if (onlyThinkContent && toolChoiceRequired && noToolCallRetries < NO_TOOL_CALL_MAX_RETRIES) {
850
+ // Provider-level violation: tool_choice was required, model emitted
851
+ // only a <think>...</think> block (or empty content) with no tool
852
+ // call. Retry with a corrective nudge up to NO_TOOL_CALL_MAX_RETRIES
853
+ // times. After cap, accept as-is (the readback path strips think
854
+ // tags and surfaces a clear diagnostic).
855
+ noToolCallRetries++;
856
+ (0, runtime_1.emitNervesEvent)({
857
+ level: "warn",
858
+ component: "engine",
859
+ event: "engine.no_tool_call_retry",
860
+ message: "model returned only <think> content with no tool call despite tool_choice=required; retrying with corrective nudge",
861
+ meta: {
862
+ attempt: noToolCallRetries,
863
+ cap: NO_TOOL_CALL_MAX_RETRIES,
864
+ provider: providerRuntime.id,
865
+ model: providerRuntime.model,
866
+ contentLength: result.content.length,
867
+ },
868
+ });
869
+ messages.push(msg);
870
+ messages.push({
871
+ role: "user",
872
+ content: isInnerDialog
873
+ ? "no tool was called this turn. you must end every turn by calling rest (or surface, ponder, observe). emit the tool call now."
874
+ : "no tool was called this turn. you must end every turn by calling settle with your answer (or ponder/observe). emit the tool call now.",
875
+ });
876
+ continue;
877
+ }
878
+ // Legitimate text-only response, or cap reached — accept as-is.
353
879
  messages.push(msg);
354
880
  done = true;
355
881
  }
356
882
  else {
357
- // Check for final_answer sole call: intercept before tool execution
358
- const isSoleFinalAnswer = result.toolCalls.length === 1 && result.toolCalls[0].name === "final_answer";
359
- if (isSoleFinalAnswer) {
360
- // Extract answer from the tool call arguments.
361
- // Supports: {"answer":"text"}, "text" (JSON string), retry on failure.
362
- let answer;
363
- try {
364
- const parsed = JSON.parse(result.toolCalls[0].arguments);
365
- if (typeof parsed === "string") {
366
- answer = parsed;
367
- }
368
- else if (parsed.answer != null) {
369
- answer = parsed.answer;
370
- }
371
- // else: valid JSON but no answer field — answer stays undefined (retry)
883
+ // Reset the retry counter on any successful tool call.
884
+ noToolCallRetries = 0;
885
+ // Check for settle sole call: intercept before tool execution
886
+ if (isSoleSettle) {
887
+ /* v8 ignore next -- defensive: JSON.parse catch for malformed settle args @preserve */
888
+ const settleArgs = (() => { try {
889
+ return JSON.parse(result.toolCalls[0].arguments);
372
890
  }
373
891
  catch {
374
- // JSON parsing failed (e.g. truncated output) — answer stays undefined (retry)
892
+ return {};
893
+ } })();
894
+ callbacks.onToolStart("settle", settleArgs);
895
+ // Inner dialog attention queue gate: reject settle if items remain
896
+ const attentionQueue = (augmentedToolContext ?? options?.toolContext)?.delegatedOrigins;
897
+ if (isInnerDialog && attentionQueue && attentionQueue.length > 0) {
898
+ callbacks.onToolEnd("settle", (0, tools_1.summarizeArgs)("settle", settleArgs), false);
899
+ callbacks.onClearText?.();
900
+ messages.push(msg);
901
+ const gateMessage = "you're holding thoughts someone is waiting for — surface them before you settle.";
902
+ messages.push({ role: "tool", tool_call_id: result.toolCalls[0].id, content: gateMessage });
903
+ providerRuntime.appendToolOutput(result.toolCalls[0].id, gateMessage);
904
+ continue;
375
905
  }
376
- if (answer != null) {
377
- if (result.finalAnswerStreamed) {
906
+ // Extract answer from the tool call arguments.
907
+ // Supports: {"answer":"text","intent":"..."} or "text" (JSON string).
908
+ const { answer, intent } = parseSettlePayload(result.toolCalls[0].arguments);
909
+ // Inner dialog settle: no CompletionMetadata, "(settled)" ack
910
+ if (isInnerDialog) {
911
+ callbacks.onToolEnd("settle", (0, tools_1.summarizeArgs)("settle", settleArgs), true);
912
+ messages.push(msg);
913
+ const settled = "(settled)";
914
+ messages.push({ role: "tool", tool_call_id: result.toolCalls[0].id, content: settled });
915
+ providerRuntime.appendToolOutput(result.toolCalls[0].id, settled);
916
+ outcome = "settled";
917
+ done = true;
918
+ continue;
919
+ }
920
+ const retryError = getSettleRetryError(mustResolveBeforeHandoffActive, intent, sawSteeringFollowUp, options?.delegationDecision, sawSendMessageSelf, sawPonder, sawQuerySession, options?.currentObligation ?? null, options?.activeWorkFrame?.inner?.job, sawExternalStateQuery);
921
+ const deliveredAnswer = answer;
922
+ const validDirectReply = mustResolveBeforeHandoffActive && intent === "direct_reply" && sawSteeringFollowUp;
923
+ const validTerminalIntent = intent === "complete" || intent === "blocked";
924
+ const validClosure = deliveredAnswer != null
925
+ && !retryError
926
+ && (!mustResolveBeforeHandoffActive || validDirectReply || validTerminalIntent);
927
+ if (validClosure) {
928
+ callbacks.onToolEnd("settle", (0, tools_1.summarizeArgs)("settle", settleArgs), true);
929
+ completion = {
930
+ answer: deliveredAnswer,
931
+ intent: validDirectReply ? "direct_reply" : intent === "blocked" ? "blocked" : "complete",
932
+ };
933
+ if (result.settleStreamed) {
378
934
  // The streaming layer already parsed and emitted the answer
379
- // progressively via FinalAnswerParser. Skip clearing and
935
+ // progressively via SettleParser. Skip clearing and
380
936
  // re-emitting to avoid double-delivery.
381
937
  }
382
938
  else {
@@ -384,37 +940,126 @@ async function runAgent(messages, callbacks, channel, signal, options) {
384
940
  callbacks.onClearText?.();
385
941
  // Emit the answer through the callback pipeline so channels receive it.
386
942
  // Never truncate -- channel adapters handle splitting long messages.
387
- callbacks.onTextChunk(answer);
943
+ callbacks.onTextChunk(deliveredAnswer);
388
944
  }
389
- // Keep the full assistant message (with tool_calls) for debuggability,
390
- // plus a synthetic tool response so the conversation stays valid on resume.
391
945
  messages.push(msg);
392
- messages.push({ role: "tool", tool_call_id: result.toolCalls[0].id, content: "(delivered)" });
393
- providerRuntime.appendToolOutput(result.toolCalls[0].id, "(delivered)");
394
- done = true;
946
+ if (validDirectReply) {
947
+ const resumeWork = "direct reply delivered. resume the unresolved obligation now and keep working until you can finish or clearly report that you are blocked.";
948
+ messages.push({ role: "tool", tool_call_id: result.toolCalls[0].id, content: resumeWork });
949
+ providerRuntime.appendToolOutput(result.toolCalls[0].id, resumeWork);
950
+ }
951
+ else {
952
+ const delivered = "(delivered)";
953
+ messages.push({ role: "tool", tool_call_id: result.toolCalls[0].id, content: delivered });
954
+ providerRuntime.appendToolOutput(result.toolCalls[0].id, delivered);
955
+ outcome = intent === "blocked" ? "blocked" : "settled";
956
+ done = true;
957
+ }
395
958
  }
396
959
  else {
397
- // Answer is undefined -- the model's final_answer was incomplete or
960
+ // Answer is undefined -- the model's settle was incomplete or
398
961
  // malformed. Clear any partial streamed text or noise, then push the
399
962
  // assistant msg + error tool result and let the model try again.
963
+ callbacks.onToolEnd("settle", (0, tools_1.summarizeArgs)("settle", settleArgs), false);
400
964
  callbacks.onClearText?.();
401
- const retryError = "your final_answer was incomplete or malformed. call final_answer again with your complete response.";
402
965
  messages.push(msg);
403
- messages.push({ role: "tool", tool_call_id: result.toolCalls[0].id, content: retryError });
404
- providerRuntime.appendToolOutput(result.toolCalls[0].id, retryError);
966
+ const toolRetryMessage = retryError
967
+ ?? "your settle was incomplete or malformed. call settle again with your complete response.";
968
+ messages.push({ role: "tool", tool_call_id: result.toolCalls[0].id, content: toolRetryMessage });
969
+ providerRuntime.appendToolOutput(result.toolCalls[0].id, toolRetryMessage);
970
+ }
971
+ continue;
972
+ }
973
+ // Check for observe sole call: intercept before tool execution
974
+ const isSoleObserve = result.toolCalls.length === 1 && result.toolCalls[0].name === "observe";
975
+ if (isSoleObserve) {
976
+ /* v8 ignore next -- defensive: JSON.parse catch for malformed observe args @preserve */
977
+ const observeArgs = (() => { try {
978
+ return JSON.parse(result.toolCalls[0].arguments);
405
979
  }
980
+ catch {
981
+ return {};
982
+ } })();
983
+ let reason;
984
+ if (typeof observeArgs?.reason === "string")
985
+ reason = observeArgs.reason;
986
+ callbacks.onToolStart("observe", observeArgs);
987
+ (0, runtime_1.emitNervesEvent)({
988
+ component: "engine",
989
+ event: "engine.observe",
990
+ message: "agent observed without responding",
991
+ meta: { ...(reason ? { reason } : {}) },
992
+ });
993
+ callbacks.onToolEnd("observe", (0, tools_1.summarizeArgs)("observe", observeArgs), true);
994
+ messages.push(msg);
995
+ const silenced = "(silenced)";
996
+ messages.push({ role: "tool", tool_call_id: result.toolCalls[0].id, content: silenced });
997
+ providerRuntime.appendToolOutput(result.toolCalls[0].id, silenced);
998
+ outcome = "observed";
999
+ done = true;
1000
+ continue;
1001
+ }
1002
+ // Check for rest sole call: intercept before tool execution
1003
+ const isSoleRest = result.toolCalls.length === 1 && result.toolCalls[0].name === "rest";
1004
+ if (isSoleRest) {
1005
+ const restArgs = (() => { try {
1006
+ return JSON.parse(result.toolCalls[0].arguments);
1007
+ }
1008
+ catch {
1009
+ return {};
1010
+ } })();
1011
+ callbacks.onToolStart("rest", restArgs);
1012
+ // Attention queue gate: reject rest if items remain
1013
+ const attentionQueue = (augmentedToolContext ?? options?.toolContext)?.delegatedOrigins;
1014
+ if (attentionQueue && attentionQueue.length > 0) {
1015
+ callbacks.onToolEnd("rest", (0, tools_1.summarizeArgs)("rest", restArgs), false);
1016
+ messages.push(msg);
1017
+ const gateMessage = "you're holding thoughts someone is waiting for — surface them before you rest.";
1018
+ messages.push({ role: "tool", tool_call_id: result.toolCalls[0].id, content: gateMessage });
1019
+ providerRuntime.appendToolOutput(result.toolCalls[0].id, gateMessage);
1020
+ continue;
1021
+ }
1022
+ if (hasFreshPendingWork(options) && !freshWorkGateFired) {
1023
+ freshWorkGateFired = true;
1024
+ callbacks.onToolEnd("rest", (0, tools_1.summarizeArgs)("rest", restArgs), false);
1025
+ messages.push(msg);
1026
+ const gateMessage = "fresh work arrived for me this turn — inspect the pending messages above and take the next concrete action before you rest.";
1027
+ messages.push({ role: "tool", tool_call_id: result.toolCalls[0].id, content: gateMessage });
1028
+ providerRuntime.appendToolOutput(result.toolCalls[0].id, gateMessage);
1029
+ (0, runtime_1.emitNervesEvent)({
1030
+ level: "info",
1031
+ component: "engine",
1032
+ event: "engine.fresh_work_gate_fired",
1033
+ message: "rest deferred once because pending work arrived this turn; agent has been notified",
1034
+ meta: { pendingCount: options.pendingMessages.length },
1035
+ });
1036
+ continue;
1037
+ }
1038
+ callbacks.onToolEnd("rest", (0, tools_1.summarizeArgs)("rest", restArgs), true);
1039
+ messages.push(msg);
1040
+ const ack = "(resting)";
1041
+ messages.push({ role: "tool", tool_call_id: result.toolCalls[0].id, content: ack });
1042
+ providerRuntime.appendToolOutput(result.toolCalls[0].id, ack);
1043
+ (0, runtime_1.emitNervesEvent)({
1044
+ component: "engine",
1045
+ event: "engine.rested",
1046
+ message: "resting until next heartbeat",
1047
+ meta: { ...(typeof restArgs?.status === "string" ? { status: restArgs.status } : {}) },
1048
+ });
1049
+ outcome = "rested";
1050
+ done = true;
406
1051
  continue;
407
1052
  }
408
1053
  messages.push(msg);
409
- // SHARED: execute tools (final_answer in mixed calls is rejected inline)
1054
+ // Execute tools (sole-call tools in mixed calls are rejected inline)
410
1055
  for (const tc of result.toolCalls) {
411
1056
  if (signal?.aborted)
412
1057
  break;
413
- // Intercept final_answer in mixed call: reject it
414
- if (tc.name === "final_answer") {
415
- const rejection = "rejected: final_answer must be the only tool call. Finish your work first, then call final_answer alone.";
416
- messages.push({ role: "tool", tool_call_id: tc.id, content: rejection });
417
- providerRuntime.appendToolOutput(tc.id, rejection);
1058
+ // Reject sole-call tools when mixed with other tool calls
1059
+ const soleCallRejection = SOLE_CALL_REJECTION[tc.name];
1060
+ if (soleCallRejection) {
1061
+ messages.push({ role: "tool", tool_call_id: tc.id, content: soleCallRejection });
1062
+ providerRuntime.appendToolOutput(tc.id, soleCallRejection);
418
1063
  continue;
419
1064
  }
420
1065
  let args = {};
@@ -424,93 +1069,252 @@ async function runAgent(messages, callbacks, channel, signal, options) {
424
1069
  catch {
425
1070
  /* ignore */
426
1071
  }
427
- const argSummary = (0, tools_1.summarizeArgs)(tc.name, args);
428
- // Confirmation check for mutate tools
429
- if ((0, tools_1.isConfirmationRequired)(tc.name) && !options?.skipConfirmation) {
430
- let decision = "denied";
431
- if (callbacks.onConfirmAction) {
432
- decision = await callbacks.onConfirmAction(tc.name, args);
1072
+ if (tc.name === "send_message" && args.friendId === "self") {
1073
+ sawSendMessageSelf = true;
1074
+ }
1075
+ if (tc.name === "speak") {
1076
+ let speakArgs = {};
1077
+ try {
1078
+ speakArgs = JSON.parse(tc.arguments);
433
1079
  }
434
- if (decision !== "confirmed") {
435
- const cancelled = "Action cancelled by user.";
436
- callbacks.onToolStart(tc.name, args);
437
- callbacks.onToolEnd(tc.name, argSummary, false);
438
- messages.push({ role: "tool", tool_call_id: tc.id, content: cancelled });
439
- providerRuntime.appendToolOutput(tc.id, cancelled);
1080
+ catch { /* malformed */ }
1081
+ const speakMessage = typeof speakArgs.message === "string" ? speakArgs.message : "";
1082
+ const argSummary = (0, tools_1.summarizeArgs)("speak", { message: speakMessage });
1083
+ callbacks.onToolStart("speak", { message: speakMessage });
1084
+ if (speakMessage.trim().length === 0) {
1085
+ const err = "speak requires a non-empty `message` string.";
1086
+ callbacks.onToolEnd("speak", argSummary, false);
1087
+ messages.push({ role: "tool", tool_call_id: tc.id, content: err });
1088
+ providerRuntime.appendToolOutput(tc.id, err);
1089
+ (0, runtime_1.emitNervesEvent)({
1090
+ level: "warn",
1091
+ component: "engine",
1092
+ event: "engine.speak_invalid",
1093
+ message: "speak rejected: missing or empty message",
1094
+ meta: {},
1095
+ });
440
1096
  continue;
441
1097
  }
1098
+ callbacks.onTextChunk(speakMessage);
1099
+ let speakDeliveryError = null;
1100
+ try {
1101
+ await callbacks.flushNow?.();
1102
+ }
1103
+ catch (err) {
1104
+ speakDeliveryError = err instanceof Error ? err : new Error(String(err));
1105
+ }
1106
+ if (speakDeliveryError) {
1107
+ callbacks.onToolEnd("speak", argSummary, false);
1108
+ const failMsg = `speak delivery failed: ${speakDeliveryError.message}. the message did not reach your friend; do not assume they saw it.`;
1109
+ messages.push({ role: "tool", tool_call_id: tc.id, content: failMsg });
1110
+ providerRuntime.appendToolOutput(tc.id, failMsg);
1111
+ (0, runtime_1.emitNervesEvent)({
1112
+ level: "error",
1113
+ component: "engine",
1114
+ event: "engine.speak_delivery_failed",
1115
+ message: "speak delivery failed",
1116
+ meta: { error: speakDeliveryError.message, messageLength: speakMessage.length },
1117
+ });
1118
+ continue;
1119
+ }
1120
+ callbacks.onToolEnd("speak", argSummary, true);
1121
+ const ack = "(spoken)";
1122
+ messages.push({ role: "tool", tool_call_id: tc.id, content: ack });
1123
+ providerRuntime.appendToolOutput(tc.id, ack);
1124
+ (0, runtime_1.emitNervesEvent)({
1125
+ component: "engine",
1126
+ event: "engine.speak",
1127
+ message: "agent spoke mid-turn",
1128
+ meta: { messageLength: speakMessage.length },
1129
+ });
1130
+ continue;
1131
+ }
1132
+ if (tc.name === "ponder") {
1133
+ const parsedArgs = normalizeLegacyPonderArgs(parsePonderPayload(tc.arguments));
1134
+ const argSummary = (0, tools_1.summarizeArgs)(tc.name, parsedArgs);
1135
+ callbacks.onToolStart(tc.name, parsedArgs);
1136
+ let toolResult;
1137
+ let success = false;
1138
+ try {
1139
+ const action = parsedArgs.action ?? "create";
1140
+ const currentSession = (augmentedToolContext ?? options?.toolContext)?.currentSession;
1141
+ const currentOrigin = currentSession
1142
+ ? { friendId: currentSession.friendId, channel: currentSession.channel, key: currentSession.key }
1143
+ : undefined;
1144
+ const isInnerChannel = currentOrigin?.friendId === "self" && currentOrigin?.channel === "inner";
1145
+ const successCriteria = parseSuccessCriteria(parsedArgs.success_criteria);
1146
+ const payload = parsePacketPayload(parsedArgs.payload_json);
1147
+ let packet;
1148
+ let returnObligationId = null;
1149
+ let resultAction = "created";
1150
+ if (action === "create") {
1151
+ const kind = parsedArgs.kind;
1152
+ const objective = typeof parsedArgs.objective === "string" ? parsedArgs.objective.trim() : "";
1153
+ const summary = typeof parsedArgs.summary === "string" ? parsedArgs.summary.trim() : "";
1154
+ if (!kind || !objective || !successCriteria || !payload) {
1155
+ throw new Error("ponder create requires kind, objective, success_criteria, and valid payload_json.");
1156
+ }
1157
+ const agentRoot = (0, identity_2.getAgentRoot)();
1158
+ let relatedObligationId;
1159
+ if (currentOrigin && !isInnerChannel) {
1160
+ try {
1161
+ const obligation = (0, obligations_1.createObligation)(agentRoot, {
1162
+ origin: currentOrigin,
1163
+ content: objective,
1164
+ });
1165
+ relatedObligationId = obligation.id;
1166
+ }
1167
+ catch {
1168
+ relatedObligationId = undefined;
1169
+ }
1170
+ }
1171
+ const frictionSignature = kind === "harness_friction" && typeof payload.frictionSignature === "string"
1172
+ ? payload.frictionSignature
1173
+ : null;
1174
+ const existing = frictionSignature && currentOrigin
1175
+ ? (0, packets_1.findHarnessFrictionPacket)(agentRoot, currentOrigin, frictionSignature)
1176
+ : null;
1177
+ if (existing) {
1178
+ resultAction = "revised";
1179
+ returnObligationId = existing.relatedReturnObligationId ?? null;
1180
+ packet = existing.status === "drafting"
1181
+ ? (0, packets_1.revisePonderPacket)(agentRoot, existing.id, {
1182
+ kind,
1183
+ objective,
1184
+ summary,
1185
+ successCriteria,
1186
+ payload,
1187
+ })
1188
+ : existing;
1189
+ }
1190
+ else {
1191
+ returnObligationId = (0, obligations_1.generateObligationId)(Date.now());
1192
+ packet = (0, packets_1.createPonderPacket)(agentRoot, {
1193
+ kind,
1194
+ objective,
1195
+ summary,
1196
+ successCriteria,
1197
+ ...(currentOrigin ? { origin: currentOrigin } : {}),
1198
+ ...(relatedObligationId ? { relatedObligationId } : {}),
1199
+ relatedReturnObligationId: returnObligationId,
1200
+ ...(parsedArgs.follows_packet_id ? { followsPacketId: parsedArgs.follows_packet_id } : {}),
1201
+ payload,
1202
+ });
1203
+ (0, obligations_1.createReturnObligation)((0, identity_2.getAgentName)(), {
1204
+ id: returnObligationId,
1205
+ origin: currentOrigin ?? { friendId: "self", channel: "inner", key: "dialog" },
1206
+ status: "queued",
1207
+ delegatedContent: (summary || objective).length > 120 ? `${(summary || objective).slice(0, 117)}...` : (summary || objective),
1208
+ packetId: packet.id,
1209
+ createdAt: Date.now(),
1210
+ });
1211
+ }
1212
+ }
1213
+ else if (action === "revise") {
1214
+ const packetId = typeof parsedArgs.packet_id === "string" ? parsedArgs.packet_id.trim() : "";
1215
+ const kind = parsedArgs.kind;
1216
+ const objective = typeof parsedArgs.objective === "string" ? parsedArgs.objective.trim() : "";
1217
+ const summary = typeof parsedArgs.summary === "string" ? parsedArgs.summary.trim() : "";
1218
+ if (!packetId || !kind || !objective || !successCriteria || !payload) {
1219
+ throw new Error("ponder revise requires packet_id, kind, objective, success_criteria, and valid payload_json.");
1220
+ }
1221
+ packet = (0, packets_1.revisePonderPacket)((0, identity_2.getAgentRoot)(), packetId, {
1222
+ kind,
1223
+ objective,
1224
+ summary,
1225
+ successCriteria,
1226
+ payload,
1227
+ });
1228
+ returnObligationId = packet.relatedReturnObligationId ?? null;
1229
+ resultAction = "revised";
1230
+ }
1231
+ else {
1232
+ throw new Error("ponder requires action=create or revise.");
1233
+ }
1234
+ try {
1235
+ await (0, socket_client_1.requestInnerWake)((0, identity_2.getAgentName)());
1236
+ }
1237
+ catch { /* daemon may not be running */ }
1238
+ sawPonder = true;
1239
+ toolResult = buildPonderResult(packet, resultAction, returnObligationId);
1240
+ success = true;
1241
+ (0, runtime_1.emitNervesEvent)({
1242
+ component: "engine",
1243
+ event: "engine.ponder_packet",
1244
+ message: "ponder packet touched",
1245
+ meta: {
1246
+ action: resultAction,
1247
+ packetId: packet.id,
1248
+ kind: packet.kind,
1249
+ status: packet.status,
1250
+ },
1251
+ });
1252
+ }
1253
+ catch (error) {
1254
+ toolResult = error instanceof Error ? error.message : String(error);
1255
+ }
1256
+ callbacks.onToolEnd(tc.name, argSummary, success);
1257
+ messages.push({ role: "tool", tool_call_id: tc.id, content: toolResult });
1258
+ providerRuntime.appendToolOutput(tc.id, toolResult);
1259
+ continue;
1260
+ }
1261
+ /* v8 ignore next -- flag tested via truth-check integration tests @preserve */
1262
+ if (tc.name === "query_session")
1263
+ sawQuerySession = true;
1264
+ /* v8 ignore next -- flag tested via truth-check integration tests @preserve */
1265
+ if (tc.name === "bridge_manage")
1266
+ sawBridgeManage = true;
1267
+ /* v8 ignore next -- flag tested via truth-check integration tests @preserve */
1268
+ if (isExternalStateQuery(tc.name, args))
1269
+ sawExternalStateQuery = true;
1270
+ const argSummary = (0, tools_1.summarizeArgs)(tc.name, args);
1271
+ const toolLoop = (0, tool_loop_1.detectToolLoop)(toolLoopState, tc.name, args);
1272
+ if (toolLoop.stuck) {
1273
+ const rejection = `loop guard: ${toolLoop.message}`;
1274
+ callbacks.onToolStart(tc.name, args);
1275
+ callbacks.onToolEnd(tc.name, argSummary, false);
1276
+ messages.push({ role: "tool", tool_call_id: tc.id, content: rejection });
1277
+ providerRuntime.appendToolOutput(tc.id, rejection);
1278
+ continue;
442
1279
  }
443
1280
  callbacks.onToolStart(tc.name, args);
444
1281
  let toolResult;
445
1282
  let success;
446
1283
  try {
447
- toolResult = await (0, tools_1.execTool)(tc.name, args, options?.toolContext);
1284
+ const execToolFn = options?.execTool ?? tools_1.execTool;
1285
+ toolResult = await execToolFn(tc.name, args, augmentedToolContext ?? options?.toolContext);
448
1286
  success = true;
449
1287
  }
450
1288
  catch (e) {
451
1289
  toolResult = `error: ${e}`;
452
1290
  success = false;
453
1291
  }
454
- callbacks.onToolEnd(tc.name, argSummary, success);
1292
+ toolResult = (0, tool_friction_1.rewriteToolResultForModel)(tc.name, toolResult, toolFrictionLedger);
1293
+ (0, tool_loop_1.recordToolOutcome)(toolLoopState, tc.name, args, toolResult, success);
1294
+ callbacks.onToolEnd(tc.name, (0, tools_1.buildToolResultSummary)(tc.name, args, toolResult, success), success);
455
1295
  messages.push({ role: "tool", tool_call_id: tc.id, content: toolResult });
456
1296
  providerRuntime.appendToolOutput(tc.id, toolResult);
1297
+ callbacks.onToolResult?.(messages);
457
1298
  }
458
1299
  }
459
1300
  }
460
1301
  catch (e) {
461
1302
  // Abort is not an error — just stop cleanly
462
- if (signal?.aborted) {
1303
+ if (e instanceof provider_attempt_1.ProviderAttemptAbortError || signal?.aborted) {
463
1304
  stripLastToolCalls(messages);
1305
+ outcome = "aborted";
464
1306
  break;
465
1307
  }
466
- // Context overflow: trim aggressively and retry once
467
- if (isContextOverflow(e) && !overflowRetried) {
468
- overflowRetried = true;
469
- stripLastToolCalls(messages);
470
- const { maxTokens, contextMargin } = (0, config_1.getContextConfig)();
471
- const trimmed = (0, context_1.trimMessages)(messages, maxTokens, contextMargin, maxTokens * 2);
472
- messages.splice(0, messages.length, ...trimmed);
473
- providerRuntime.resetTurnState(messages);
474
- callbacks.onError(new Error("context trimmed, retrying..."), "transient");
475
- continue;
1308
+ const errorForClassification = e instanceof Error ? e : /* v8 ignore next -- defensive @preserve */ new Error(String(e));
1309
+ let providerClassification;
1310
+ try {
1311
+ providerClassification = providerRuntime.classifyError(errorForClassification);
476
1312
  }
477
- // Transient errors: retry with exponential backoff
478
- if (isTransientError(e) && retryCount < MAX_RETRIES) {
479
- retryCount++;
480
- const delay = RETRY_BASE_MS * Math.pow(2, retryCount - 1);
481
- const cause = classifyTransientError(e);
482
- callbacks.onError(new Error(`${cause}, retrying in ${delay / 1000}s (${retryCount}/${MAX_RETRIES})...`), "transient");
483
- // Wait with abort support
484
- const aborted = await new Promise((resolve) => {
485
- const timer = setTimeout(() => resolve(false), delay);
486
- if (signal) {
487
- const onAbort = () => { clearTimeout(timer); resolve(true); };
488
- if (signal.aborted) {
489
- clearTimeout(timer);
490
- resolve(true);
491
- return;
492
- }
493
- signal.addEventListener("abort", onAbort, { once: true });
494
- }
495
- });
496
- if (aborted) {
497
- stripLastToolCalls(messages);
498
- break;
499
- }
500
- providerRuntime.resetTurnState(messages);
501
- continue;
1313
+ catch {
1314
+ /* v8 ignore next -- defensive: classifyError should not throw @preserve */
1315
+ providerClassification = "unknown";
502
1316
  }
503
- callbacks.onError(e instanceof Error ? e : new Error(String(e)), "terminal");
504
- (0, runtime_1.emitNervesEvent)({
505
- level: "error",
506
- event: "engine.error",
507
- trace_id: traceId,
508
- component: "engine",
509
- message: e instanceof Error ? e.message : String(e),
510
- meta: {},
511
- });
512
- stripLastToolCalls(messages);
513
- done = true;
1317
+ finishTerminalProviderError(errorForClassification, providerClassification);
514
1318
  }
515
1319
  }
516
1320
  (0, runtime_1.emitNervesEvent)({
@@ -518,7 +1322,12 @@ async function runAgent(messages, callbacks, channel, signal, options) {
518
1322
  trace_id: traceId,
519
1323
  component: "engine",
520
1324
  message: "runAgent turn completed",
521
- meta: { done },
1325
+ meta: { done, sawPonder, sawQuerySession, sawBridgeManage },
522
1326
  });
523
- return { usage: lastUsage };
1327
+ return {
1328
+ usage: lastUsage,
1329
+ outcome,
1330
+ completion,
1331
+ ...(terminalError ? { error: terminalError, errorClassification: terminalErrorClassification } : {}),
1332
+ };
524
1333
  }