@ouro.bot/cli 0.1.0-alpha.51 → 0.1.0-alpha.511

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