@ouro.bot/cli 0.1.0-alpha.65 → 0.1.0-alpha.650

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