@ouro.bot/cli 0.1.0-alpha.4 → 0.1.0-alpha.400

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