@ouro.bot/cli 0.1.0-alpha.37 → 0.1.0-alpha.370

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 (329) hide show
  1. package/README.md +106 -14
  2. package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/agent.json +3 -2
  3. package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/SOUL.md +1 -1
  4. package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/the-serpent.md +1 -1
  5. package/changelog.json +2222 -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 +832 -0
  16. package/dist/heart/agent-entry.js +37 -2
  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 +378 -0
  28. package/dist/heart/bridges/manager.js +358 -0
  29. package/dist/heart/bridges/state-machine.js +135 -0
  30. package/dist/heart/bridges/store.js +123 -0
  31. package/dist/heart/bundle-state.js +168 -0
  32. package/dist/heart/commitments.js +111 -0
  33. package/dist/heart/config-registry.js +304 -0
  34. package/dist/heart/config.js +107 -61
  35. package/dist/heart/core.js +803 -259
  36. package/dist/heart/cross-chat-delivery.js +131 -0
  37. package/dist/heart/daemon/agent-config-check.js +382 -0
  38. package/dist/heart/daemon/agent-discovery.js +79 -3
  39. package/dist/heart/daemon/agent-service.js +360 -0
  40. package/dist/heart/daemon/agentic-repair.js +205 -0
  41. package/dist/heart/daemon/bluebubbles-health-diagnostics.js +122 -0
  42. package/dist/heart/daemon/cadence.js +70 -0
  43. package/dist/heart/daemon/cli-defaults.js +538 -0
  44. package/dist/heart/daemon/cli-exec.js +3081 -0
  45. package/dist/heart/daemon/cli-help.js +312 -0
  46. package/dist/heart/daemon/cli-parse.js +1023 -0
  47. package/dist/heart/daemon/cli-render-doctor.js +57 -0
  48. package/dist/heart/daemon/cli-render.js +560 -0
  49. package/dist/heart/daemon/cli-types.js +8 -0
  50. package/dist/heart/daemon/daemon-cli.js +29 -1171
  51. package/dist/heart/daemon/daemon-entry.js +356 -3
  52. package/dist/heart/daemon/daemon-health.js +141 -0
  53. package/dist/heart/daemon/daemon-runtime-sync.js +157 -12
  54. package/dist/heart/daemon/daemon-tombstone.js +236 -0
  55. package/dist/heart/daemon/daemon.js +757 -58
  56. package/dist/heart/daemon/doctor-types.js +8 -0
  57. package/dist/heart/daemon/doctor.js +445 -0
  58. package/dist/heart/daemon/health-monitor.js +79 -1
  59. package/dist/heart/daemon/hooks/agent-config-v2.js +33 -0
  60. package/dist/heart/daemon/hooks/bundle-meta.js +115 -1
  61. package/dist/heart/daemon/http-health-probe.js +80 -0
  62. package/dist/heart/daemon/inner-status.js +89 -0
  63. package/dist/heart/daemon/interactive-repair.js +148 -0
  64. package/dist/heart/daemon/launchd.js +46 -9
  65. package/dist/heart/daemon/log-tailer.js +82 -12
  66. package/dist/heart/daemon/logs-prune.js +105 -0
  67. package/dist/heart/daemon/message-router.js +17 -8
  68. package/dist/heart/daemon/os-cron-deps.js +134 -0
  69. package/dist/heart/daemon/ouro-bot-entry.js +1 -1
  70. package/dist/heart/daemon/process-manager.js +201 -0
  71. package/dist/heart/daemon/provider-discovery.js +113 -0
  72. package/dist/heart/daemon/pulse.js +475 -0
  73. package/dist/heart/daemon/run-hooks.js +2 -0
  74. package/dist/heart/daemon/runtime-logging.js +67 -16
  75. package/dist/heart/daemon/runtime-metadata.js +101 -0
  76. package/dist/heart/daemon/runtime-mode.js +67 -0
  77. package/dist/heart/daemon/safe-mode.js +161 -0
  78. package/dist/heart/daemon/sense-manager.js +72 -3
  79. package/dist/heart/daemon/session-id-resolver.js +131 -0
  80. package/dist/heart/daemon/skill-management-installer.js +94 -0
  81. package/dist/heart/daemon/socket-client.js +307 -0
  82. package/dist/heart/daemon/stale-bundle-prune.js +96 -0
  83. package/dist/heart/daemon/startup-tui.js +237 -0
  84. package/dist/heart/daemon/task-scheduler.js +3 -25
  85. package/dist/heart/daemon/thoughts.js +510 -0
  86. package/dist/heart/daemon/up-progress.js +135 -0
  87. package/dist/heart/delegation.js +62 -0
  88. package/dist/heart/habits/habit-migration.js +181 -0
  89. package/dist/heart/habits/habit-parser.js +140 -0
  90. package/dist/heart/habits/habit-scheduler.js +371 -0
  91. package/dist/heart/{daemon → hatch}/hatch-flow.js +55 -126
  92. package/dist/heart/{daemon → hatch}/hatch-specialist.js +3 -3
  93. package/dist/heart/{daemon → hatch}/specialist-prompt.js +11 -8
  94. package/dist/heart/{daemon → hatch}/specialist-tools.js +77 -11
  95. package/dist/heart/identity.js +154 -59
  96. package/dist/heart/kept-notes.js +357 -0
  97. package/dist/heart/kicks.js +2 -20
  98. package/dist/heart/machine-identity.js +161 -0
  99. package/dist/heart/mcp/mcp-server.js +653 -0
  100. package/dist/heart/migrate-config.js +100 -0
  101. package/dist/heart/model-capabilities.js +59 -0
  102. package/dist/heart/outlook/outlook-http-hooks.js +64 -0
  103. package/dist/heart/outlook/outlook-http-response.js +7 -0
  104. package/dist/heart/outlook/outlook-http-routes.js +232 -0
  105. package/dist/heart/outlook/outlook-http-static.js +99 -0
  106. package/dist/heart/outlook/outlook-http-transport.js +116 -0
  107. package/dist/heart/outlook/outlook-http.js +99 -0
  108. package/dist/heart/outlook/outlook-read.js +28 -0
  109. package/dist/heart/outlook/outlook-types.js +27 -0
  110. package/dist/heart/outlook/outlook-view.js +195 -0
  111. package/dist/heart/outlook/readers/agent-machine.js +359 -0
  112. package/dist/heart/outlook/readers/continuity-readers.js +332 -0
  113. package/dist/heart/outlook/readers/runtime-readers.js +660 -0
  114. package/dist/heart/outlook/readers/sessions.js +232 -0
  115. package/dist/heart/outlook/readers/shared.js +111 -0
  116. package/dist/heart/platform.js +81 -0
  117. package/dist/heart/progress-story.js +42 -0
  118. package/dist/heart/provider-attempt.js +133 -0
  119. package/dist/heart/provider-binding-resolver.js +239 -0
  120. package/dist/heart/provider-credentials.js +379 -0
  121. package/dist/heart/provider-failover.js +266 -0
  122. package/dist/heart/provider-models.js +81 -0
  123. package/dist/heart/provider-ping.js +237 -0
  124. package/dist/heart/provider-state.js +216 -0
  125. package/dist/heart/provider-visibility.js +180 -0
  126. package/dist/heart/providers/anthropic-token.js +131 -0
  127. package/dist/heart/providers/anthropic.js +193 -55
  128. package/dist/heart/providers/azure.js +103 -12
  129. package/dist/heart/providers/error-classification.js +63 -0
  130. package/dist/heart/providers/github-copilot.js +145 -0
  131. package/dist/heart/providers/minimax-vlm.js +189 -0
  132. package/dist/heart/providers/minimax.js +29 -7
  133. package/dist/heart/providers/openai-codex.js +39 -29
  134. package/dist/heart/session-activity.js +190 -0
  135. package/dist/heart/session-events.js +855 -0
  136. package/dist/heart/session-transcript.js +167 -0
  137. package/dist/heart/start-of-turn-packet.js +345 -0
  138. package/dist/heart/streaming.js +36 -27
  139. package/dist/heart/sync.js +332 -0
  140. package/dist/heart/target-resolution.js +127 -0
  141. package/dist/heart/tempo.js +93 -0
  142. package/dist/heart/temporal-view.js +41 -0
  143. package/dist/heart/tool-activity-callbacks.js +36 -0
  144. package/dist/heart/tool-description.js +135 -0
  145. package/dist/heart/tool-friction.js +55 -0
  146. package/dist/heart/tool-loop.js +200 -0
  147. package/dist/heart/turn-context.js +362 -0
  148. package/dist/heart/turn-coordinator.js +28 -0
  149. package/dist/heart/{daemon → versioning}/ouro-bot-global-installer.js +1 -1
  150. package/dist/heart/{daemon → versioning}/ouro-bot-wrapper.js +1 -1
  151. package/dist/heart/versioning/ouro-path-installer.js +301 -0
  152. package/dist/heart/versioning/ouro-version-manager.js +295 -0
  153. package/dist/heart/{daemon → versioning}/staged-restart.js +40 -8
  154. package/dist/heart/{daemon → versioning}/update-checker.js +12 -2
  155. package/dist/heart/{daemon → versioning}/update-hooks.js +63 -59
  156. package/dist/mind/bundle-manifest.js +7 -1
  157. package/dist/mind/context.js +141 -94
  158. package/dist/mind/diary-integrity.js +60 -0
  159. package/dist/mind/{memory.js → diary.js} +84 -96
  160. package/dist/mind/embedding-provider.js +60 -0
  161. package/dist/mind/file-state.js +179 -0
  162. package/dist/mind/first-impressions.js +14 -1
  163. package/dist/mind/friends/channel.js +56 -0
  164. package/dist/mind/friends/group-context.js +144 -0
  165. package/dist/mind/friends/resolver.js +38 -1
  166. package/dist/mind/friends/store-file.js +58 -3
  167. package/dist/mind/friends/trust-explanation.js +74 -0
  168. package/dist/mind/friends/types.js +9 -1
  169. package/dist/mind/journal-index.js +161 -0
  170. package/dist/mind/note-search.js +268 -0
  171. package/dist/mind/obligation-steering.js +221 -0
  172. package/dist/mind/pending.js +74 -7
  173. package/dist/mind/prompt-refresh.js +3 -2
  174. package/dist/mind/prompt.js +1030 -118
  175. package/dist/mind/provenance-trust.js +26 -0
  176. package/dist/mind/scrutiny.js +173 -0
  177. package/dist/mind/token-estimate.js +8 -12
  178. package/dist/nerves/cli-logging.js +7 -1
  179. package/dist/nerves/coverage/audit-rules.js +15 -6
  180. package/dist/nerves/coverage/audit.js +28 -2
  181. package/dist/nerves/coverage/cli.js +1 -1
  182. package/dist/nerves/coverage/file-completeness.js +83 -5
  183. package/dist/nerves/coverage/run-artifacts.js +1 -1
  184. package/dist/nerves/event-buffer.js +111 -0
  185. package/dist/nerves/index.js +224 -4
  186. package/dist/nerves/observation.js +20 -0
  187. package/dist/nerves/redact.js +79 -0
  188. package/dist/nerves/runtime.js +5 -1
  189. package/dist/outlook-ui/assets/index-LwChZTgL.css +1 -0
  190. package/dist/outlook-ui/assets/index-xTdv64BV.js +61 -0
  191. package/dist/outlook-ui/index.html +15 -0
  192. package/dist/repertoire/ado-client.js +15 -56
  193. package/dist/repertoire/ado-semantic.js +11 -10
  194. package/dist/repertoire/api-client.js +97 -0
  195. package/dist/repertoire/bitwarden-store.js +365 -0
  196. package/dist/repertoire/bundle-templates.js +72 -0
  197. package/dist/repertoire/bw-installer.js +79 -0
  198. package/dist/repertoire/coding/codex-jsonl.js +64 -0
  199. package/dist/repertoire/coding/context-pack.js +330 -0
  200. package/dist/repertoire/coding/feedback.js +197 -30
  201. package/dist/repertoire/coding/manager.js +158 -9
  202. package/dist/repertoire/coding/spawner.js +55 -9
  203. package/dist/repertoire/coding/tools.js +170 -7
  204. package/dist/repertoire/commerce-errors.js +109 -0
  205. package/dist/repertoire/commerce-self-test.js +156 -0
  206. package/dist/repertoire/credential-access.js +107 -0
  207. package/dist/repertoire/duffel-client.js +185 -0
  208. package/dist/repertoire/github-client.js +14 -55
  209. package/dist/repertoire/graph-client.js +11 -52
  210. package/dist/repertoire/guardrails.js +375 -0
  211. package/dist/repertoire/mcp-client.js +255 -0
  212. package/dist/repertoire/mcp-manager.js +305 -0
  213. package/dist/repertoire/mcp-tools.js +63 -0
  214. package/dist/repertoire/shell-sessions.js +133 -0
  215. package/dist/repertoire/skills.js +15 -24
  216. package/dist/repertoire/stripe-client.js +131 -0
  217. package/dist/repertoire/tasks/board.js +43 -5
  218. package/dist/repertoire/tasks/fix.js +182 -0
  219. package/dist/repertoire/tasks/index.js +28 -10
  220. package/dist/repertoire/tasks/lifecycle.js +2 -2
  221. package/dist/repertoire/tasks/parser.js +3 -2
  222. package/dist/repertoire/tasks/scanner.js +194 -37
  223. package/dist/repertoire/tasks/transitions.js +16 -79
  224. package/dist/repertoire/tool-results.js +29 -0
  225. package/dist/repertoire/tools-attachments.js +317 -0
  226. package/dist/repertoire/tools-base.js +45 -771
  227. package/dist/repertoire/tools-bluebubbles.js +1 -0
  228. package/dist/repertoire/tools-bridge.js +141 -0
  229. package/dist/repertoire/tools-bundle.js +984 -0
  230. package/dist/repertoire/tools-config.js +185 -0
  231. package/dist/repertoire/tools-continuity.js +248 -0
  232. package/dist/repertoire/tools-credential.js +182 -0
  233. package/dist/repertoire/tools-files.js +342 -0
  234. package/dist/repertoire/tools-flight.js +224 -0
  235. package/dist/repertoire/tools-flow.js +105 -0
  236. package/dist/repertoire/tools-github.js +1 -7
  237. package/dist/repertoire/tools-notes.js +376 -0
  238. package/dist/repertoire/tools-session.js +739 -0
  239. package/dist/repertoire/tools-shell.js +120 -0
  240. package/dist/repertoire/tools-stripe.js +180 -0
  241. package/dist/repertoire/tools-surface.js +243 -0
  242. package/dist/repertoire/tools-teams.js +12 -62
  243. package/dist/repertoire/tools-travel.js +125 -0
  244. package/dist/repertoire/tools-user-profile.js +144 -0
  245. package/dist/repertoire/tools-vault.js +40 -0
  246. package/dist/repertoire/tools.js +144 -138
  247. package/dist/repertoire/travel-api-client.js +360 -0
  248. package/dist/repertoire/user-profile.js +118 -0
  249. package/dist/repertoire/vault-setup.js +241 -0
  250. package/dist/repertoire/vault-unlock.js +359 -0
  251. package/dist/scripts/claude-code-hook.js +41 -0
  252. package/dist/scripts/claude-code-stop-hook.js +47 -0
  253. package/dist/senses/attention-queue.js +116 -0
  254. package/dist/senses/bluebubbles/attachment-cache.js +53 -0
  255. package/dist/senses/bluebubbles/attachment-download.js +137 -0
  256. package/dist/senses/{bluebubbles-client.js → bluebubbles/client.js} +260 -9
  257. package/dist/senses/bluebubbles/entry.js +13 -0
  258. package/dist/senses/bluebubbles/inbound-log.js +113 -0
  259. package/dist/senses/bluebubbles/index.js +1620 -0
  260. package/dist/senses/{bluebubbles-media.js → bluebubbles/media.js} +121 -70
  261. package/dist/senses/{bluebubbles-model.js → bluebubbles/model.js} +43 -12
  262. package/dist/senses/{bluebubbles-mutation-log.js → bluebubbles/mutation-log.js} +46 -6
  263. package/dist/senses/bluebubbles/replay.js +129 -0
  264. package/dist/senses/bluebubbles/runtime-state.js +109 -0
  265. package/dist/senses/{bluebubbles-session-cleanup.js → bluebubbles/session-cleanup.js} +1 -1
  266. package/dist/senses/cli/bracketed-paste.js +82 -0
  267. package/dist/senses/cli/image-paste.js +287 -0
  268. package/dist/senses/cli/image-ref-navigation.js +75 -0
  269. package/dist/senses/cli/ink-app.js +156 -0
  270. package/dist/senses/cli/inline-diff.js +64 -0
  271. package/dist/senses/cli/input-keys.js +174 -0
  272. package/dist/senses/cli/kill-ring.js +86 -0
  273. package/dist/senses/cli/message-list.js +51 -0
  274. package/dist/senses/cli/ouro-tui.js +605 -0
  275. package/dist/senses/cli/spinner-imperative.js +135 -0
  276. package/dist/senses/cli/spinner.js +101 -0
  277. package/dist/senses/cli/status-line.js +60 -0
  278. package/dist/senses/cli/streaming-markdown.js +526 -0
  279. package/dist/senses/cli/tool-display.js +83 -0
  280. package/dist/senses/cli/tool-render.js +85 -0
  281. package/dist/senses/cli/tui-store.js +240 -0
  282. package/dist/senses/cli/virtual-list.js +35 -0
  283. package/dist/senses/cli-entry.js +1 -1
  284. package/dist/senses/cli-layout.js +187 -0
  285. package/dist/senses/cli.js +588 -250
  286. package/dist/senses/commands.js +66 -3
  287. package/dist/senses/continuity.js +94 -0
  288. package/dist/senses/habit-turn-message.js +108 -0
  289. package/dist/senses/inner-dialog-worker.js +112 -19
  290. package/dist/senses/inner-dialog.js +636 -86
  291. package/dist/senses/pipeline.js +602 -0
  292. package/dist/senses/proactive-content-guard.js +51 -0
  293. package/dist/senses/shared-turn.js +205 -0
  294. package/dist/senses/surface-tool.js +68 -0
  295. package/dist/senses/teams.js +693 -160
  296. package/dist/senses/trust-gate.js +112 -2
  297. package/package.json +29 -7
  298. package/skills/agent-commerce.md +106 -0
  299. package/skills/browser-navigation.md +110 -0
  300. package/skills/commerce-setup-guide.md +116 -0
  301. package/skills/commerce-setup.md +84 -0
  302. package/skills/configure-dev-tools.md +101 -0
  303. package/skills/travel-planning.md +138 -0
  304. package/dist/heart/daemon/ouro-path-installer.js +0 -178
  305. package/dist/heart/daemon/subagent-installer.js +0 -134
  306. package/dist/mind/associative-recall.js +0 -197
  307. package/dist/senses/bluebubbles-entry.js +0 -11
  308. package/dist/senses/bluebubbles.js +0 -558
  309. package/dist/senses/debug-activity.js +0 -127
  310. package/subagents/README.md +0 -60
  311. package/subagents/work-doer.md +0 -235
  312. package/subagents/work-merger.md +0 -618
  313. package/subagents/work-planner.md +0 -382
  314. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/basilisk.md +0 -0
  315. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/jafar.md +0 -0
  316. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/jormungandr.md +0 -0
  317. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/kaa.md +0 -0
  318. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/medusa.md +0 -0
  319. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/monty.md +0 -0
  320. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/nagini.md +0 -0
  321. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/ouroboros.md +0 -0
  322. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/python.md +0 -0
  323. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/quetzalcoatl.md +0 -0
  324. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/sir-hiss.md +0 -0
  325. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/the-snake.md +0 -0
  326. /package/dist/heart/{daemon → hatch}/hatch-animation.js +0 -0
  327. /package/dist/heart/{daemon → hatch}/specialist-orchestrator.js +0 -0
  328. /package/dist/heart/{daemon → versioning}/ouro-uti.js +0 -0
  329. /package/dist/heart/{daemon → versioning}/wrapper-publish-guard.js +0 -0
@@ -34,18 +34,262 @@ var __importStar = (this && this.__importStar) || (function () {
34
34
  })();
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
36
  exports.OuroDaemon = void 0;
37
+ exports.parseOrphanPidsFromPs = parseOrphanPidsFromPs;
38
+ exports.filterPidfilePidsToActualOrphans = filterPidfilePidsToActualOrphans;
39
+ exports.killOrphanProcesses = killOrphanProcesses;
40
+ exports.writePidfile = writePidfile;
41
+ exports.handleAgentSenseTurn = handleAgentSenseTurn;
37
42
  const fs = __importStar(require("fs"));
38
43
  const net = __importStar(require("net"));
44
+ const os = __importStar(require("os"));
39
45
  const path = __importStar(require("path"));
40
46
  const identity_1 = require("../identity");
47
+ const agent_discovery_1 = require("./agent-discovery");
41
48
  const runtime_1 = require("../../nerves/runtime");
42
49
  const runtime_metadata_1 = require("./runtime-metadata");
43
- const update_hooks_1 = require("./update-hooks");
50
+ const runtime_mode_1 = require("./runtime-mode");
51
+ const update_hooks_1 = require("../versioning/update-hooks");
44
52
  const bundle_meta_1 = require("./hooks/bundle-meta");
53
+ const agent_config_v2_1 = require("./hooks/agent-config-v2");
45
54
  const bundle_manifest_1 = require("../../mind/bundle-manifest");
46
- const update_checker_1 = require("./update-checker");
47
- const staged_restart_1 = require("./staged-restart");
55
+ const update_checker_1 = require("../versioning/update-checker");
56
+ const staged_restart_1 = require("../versioning/staged-restart");
57
+ const ouro_version_manager_1 = require("../versioning/ouro-version-manager");
48
58
  const child_process_1 = require("child_process");
59
+ const pending_1 = require("../../mind/pending");
60
+ const agent_service_1 = require("./agent-service");
61
+ const channel_1 = require("../../mind/friends/channel");
62
+ const mcp_manager_1 = require("../../repertoire/mcp-manager");
63
+ const outlook_http_1 = require("../outlook/outlook-http");
64
+ const outlook_types_1 = require("../outlook/outlook-types");
65
+ const outlook_read_1 = require("../outlook/outlook-read");
66
+ const outlook_view_1 = require("../outlook/outlook-view");
67
+ const provider_visibility_1 = require("../provider-visibility");
68
+ const PIDFILE_PATH = path.join(os.homedir(), ".ouro-cli", "daemon.pids");
69
+ /**
70
+ * Defense-in-depth: detect if we're running under vitest. The pidfile lives
71
+ * at a hardcoded path under the user's real ~/.ouro-cli/ — there's no DI
72
+ * seam to redirect it. So when a test creates a real OuroDaemon and calls
73
+ * start(), the daemon's killOrphanProcesses() reads the REAL pidfile,
74
+ * ps-verifies the PIDs, and SIGTERMs the production daemon. We saw this
75
+ * cause an outage on 2026-04-08 (alpha.265 daemon killed 93s after startup
76
+ * by a vitest test that called daemon.start()).
77
+ *
78
+ * Both killOrphanProcesses() and writePidfile() short-circuit under vitest
79
+ * to make the production pidfile sacred. Tests that need to verify these
80
+ * functions' behavior should use the extracted pure helpers
81
+ * (parseOrphanPidsFromPs, filterPidfilePidsToActualOrphans).
82
+ */
83
+ function isVitestProcess() {
84
+ /* v8 ignore next -- defensive: process and process.argv always exist in node @preserve */
85
+ if (typeof process === "undefined" || !Array.isArray(process.argv))
86
+ return false;
87
+ return process.argv.some((arg) => typeof arg === "string" && arg.includes("vitest"));
88
+ }
89
+ /**
90
+ * Scan `ps -eo pid,ppid,command` output for daemon-owned entry points whose
91
+ * parent has died (PPID reparented to init/PID 1). Returns the list of PIDs
92
+ * that are safe to SIGTERM — true orphans, not children of live sibling
93
+ * daemons running from worktrees, test suites, or other users of the harness.
94
+ *
95
+ * Exported so unit tests can exercise the filter without shelling out.
96
+ */
97
+ function parseOrphanPidsFromPs(psOutput, selfPid) {
98
+ const orphans = [];
99
+ for (const line of psOutput.split("\n")) {
100
+ // Explicitly exclude MCP server processes — they share a harness entry
101
+ // point but are not daemon children and must never be killed.
102
+ if (line.includes("mcp-serve") || line.includes("mcp serve"))
103
+ continue;
104
+ // Match only daemon-owned JS entry points.
105
+ if (!line.includes("agent-entry.js")
106
+ && !line.includes("daemon-entry.js")
107
+ && !line.includes("bluebubbles/entry.js")
108
+ && !line.includes("teams-entry.js"))
109
+ continue;
110
+ // Parse `<pid> <ppid> <command...>`. ps pads these with leading spaces.
111
+ // Regex guarantees both groups are \d+ so parseInt can't produce NaN.
112
+ const match = line.trim().match(/^(\d+)\s+(\d+)\s/);
113
+ if (!match)
114
+ continue;
115
+ const pid = parseInt(match[1], 10);
116
+ const ppid = parseInt(match[2], 10);
117
+ if (pid === selfPid)
118
+ continue;
119
+ // CRITICAL: only kill processes whose parent is init (PID 1). A live
120
+ // PPID means the process belongs to another daemon instance (parallel
121
+ // test run, sibling worktree, another user of /tmp/ouroboros-daemon.sock).
122
+ // Killing those will crash unrelated harnesses — we saw this in B6
123
+ // when a vitest worker's daemon killed slugger's production children.
124
+ if (ppid !== 1)
125
+ continue;
126
+ orphans.push(pid);
127
+ }
128
+ return orphans;
129
+ }
130
+ /**
131
+ * Given a list of PIDs from the pidfile, return only those that are actual
132
+ * orphans (PPID reparented to init/PID 1). Protects against a polluted
133
+ * pidfile killing a PID that the OS has reassigned to an unrelated process.
134
+ *
135
+ * Implementation: shells out to `ps -p <csv> -o pid,ppid` for a batch lookup.
136
+ * Returns the empty list if ps fails — safer to skip cleanup than to
137
+ * wildcard-kill on a bad read.
138
+ *
139
+ * Exported for direct unit coverage.
140
+ */
141
+ function filterPidfilePidsToActualOrphans(candidatePids, psRunner = runPsCheck) {
142
+ if (candidatePids.length === 0)
143
+ return [];
144
+ const psOutput = psRunner(candidatePids);
145
+ if (psOutput === null)
146
+ return [];
147
+ const survivingOrphans = [];
148
+ // `ps -p x,y,z -o pid,ppid` emits a header line then one row per found PID.
149
+ // PIDs not found (already exited) are silently omitted — which is the
150
+ // correct behavior for us: we only want to kill live orphans.
151
+ for (const line of psOutput.split("\n")) {
152
+ const match = line.trim().match(/^(\d+)\s+(\d+)$/);
153
+ if (!match)
154
+ continue;
155
+ const pid = parseInt(match[1], 10);
156
+ const ppid = parseInt(match[2], 10);
157
+ if (ppid !== 1)
158
+ continue;
159
+ if (!candidatePids.includes(pid))
160
+ continue;
161
+ survivingOrphans.push(pid);
162
+ }
163
+ return survivingOrphans;
164
+ }
165
+ /* v8 ignore start -- shells out to ps; covered by filterPidfilePidsToActualOrphans unit tests via injected runner @preserve */
166
+ function runPsCheck(pids) {
167
+ try {
168
+ const csv = pids.join(",");
169
+ return (0, child_process_1.execSync)(`ps -p ${csv} -o pid=,ppid=`, { encoding: "utf-8", timeout: 5000 });
170
+ }
171
+ catch {
172
+ // ps returns non-zero when none of the requested PIDs exist. Treat as
173
+ // "no survivors" rather than an error.
174
+ return "";
175
+ }
176
+ }
177
+ /* v8 ignore stop */
178
+ /**
179
+ * Kill all ouro processes from the previous daemon instance using the pidfile.
180
+ * On startup, reads PIDs from ~/.ouro-cli/daemon.pids, kills them all, then
181
+ * deletes the file. The new daemon writes its own PIDs after spawning.
182
+ *
183
+ * Safety: pidfile contents are verified before being killed — each PID must
184
+ * be an actual orphan (PPID reparented to init/PID 1) via
185
+ * `filterPidfilePidsToActualOrphans`. Otherwise a polluted pidfile (written
186
+ * by a test, or a crashed daemon whose PIDs have since been reused by the
187
+ * OS) could SIGTERM unrelated processes.
188
+ *
189
+ * Falls back to ps-based scanning scoped to true orphans (PPID=1) if the
190
+ * pidfile doesn't exist (first run, previous daemon crashed before writing,
191
+ * manual cleanup). The scope is narrow on purpose — see parseOrphanPidsFromPs.
192
+ */
193
+ /* v8 ignore start -- process lifecycle: uses kill/ps, tested via deployment @preserve */
194
+ function killOrphanProcesses() {
195
+ if (isVitestProcess()) {
196
+ (0, runtime_1.emitNervesEvent)({
197
+ level: "warn",
198
+ component: "daemon",
199
+ event: "daemon.orphan_cleanup_test_blocked",
200
+ message: "blocked killOrphanProcesses from touching real pidfile under vitest",
201
+ meta: { pidfilePath: PIDFILE_PATH },
202
+ });
203
+ return;
204
+ }
205
+ try {
206
+ let pidsToKill = [];
207
+ // Primary: read pidfile from previous daemon
208
+ try {
209
+ const raw = fs.readFileSync(PIDFILE_PATH, "utf-8");
210
+ const candidates = raw.split("\n")
211
+ .map((s) => parseInt(s.trim(), 10))
212
+ .filter((n) => !isNaN(n) && n !== process.pid);
213
+ // Verify each candidate is an actual live orphan before killing. See
214
+ // docstring above for why this matters.
215
+ pidsToKill = filterPidfilePidsToActualOrphans(candidates);
216
+ fs.unlinkSync(PIDFILE_PATH);
217
+ }
218
+ catch {
219
+ // No pidfile — fall back to ps scan (scoped to orphans with PPID=1).
220
+ }
221
+ if (pidsToKill.length === 0) {
222
+ try {
223
+ const result = (0, child_process_1.execSync)("ps -eo pid,ppid,command", { encoding: "utf-8", timeout: 5000 });
224
+ pidsToKill = parseOrphanPidsFromPs(result, process.pid);
225
+ }
226
+ catch { /* ps failed — best effort */ }
227
+ }
228
+ if (pidsToKill.length > 0) {
229
+ for (const pid of pidsToKill) {
230
+ try {
231
+ process.kill(pid, "SIGTERM");
232
+ }
233
+ catch { /* already exited */ }
234
+ }
235
+ (0, runtime_1.emitNervesEvent)({
236
+ component: "daemon",
237
+ event: "daemon.orphan_cleanup",
238
+ message: `killed ${pidsToKill.length} orphaned ouro processes`,
239
+ meta: { pids: pidsToKill },
240
+ });
241
+ }
242
+ }
243
+ catch (error) {
244
+ (0, runtime_1.emitNervesEvent)({
245
+ level: "warn",
246
+ component: "daemon",
247
+ event: "daemon.orphan_cleanup_error",
248
+ message: "failed to clean up orphaned ouro processes",
249
+ meta: { error: error instanceof Error ? error.message : String(error) },
250
+ });
251
+ }
252
+ }
253
+ /**
254
+ * Write all managed PIDs (daemon + children) to the pidfile.
255
+ * Called after all agents and senses are spawned.
256
+ */
257
+ function writePidfile(extraPids = []) {
258
+ if (isVitestProcess()) {
259
+ (0, runtime_1.emitNervesEvent)({
260
+ level: "warn",
261
+ component: "daemon",
262
+ event: "daemon.write_pidfile_test_blocked",
263
+ message: "blocked writePidfile from clobbering real pidfile under vitest",
264
+ meta: { pidfilePath: PIDFILE_PATH, attemptedPids: extraPids.length },
265
+ });
266
+ return;
267
+ }
268
+ try {
269
+ const pids = [process.pid, ...extraPids].filter(Boolean);
270
+ fs.mkdirSync(path.dirname(PIDFILE_PATH), { recursive: true });
271
+ fs.writeFileSync(PIDFILE_PATH, pids.join("\n") + "\n", "utf-8");
272
+ }
273
+ catch { /* best effort */ }
274
+ }
275
+ function readSocketIdentity(socketPath) {
276
+ try {
277
+ const stats = fs.lstatSync(socketPath);
278
+ return {
279
+ dev: stats.dev,
280
+ ino: stats.ino,
281
+ ctimeMs: stats.ctimeMs,
282
+ };
283
+ }
284
+ catch {
285
+ return null;
286
+ }
287
+ }
288
+ function sameSocketIdentity(left, right) {
289
+ if (!left || !right)
290
+ return false;
291
+ return left.dev === right.dev && left.ino === right.ino && left.ctimeMs === right.ctimeMs;
292
+ }
49
293
  function buildWorkerRows(snapshots) {
50
294
  return snapshots.map((snapshot) => ({
51
295
  agent: snapshot.name,
@@ -54,6 +298,10 @@ function buildWorkerRows(snapshots) {
54
298
  pid: snapshot.pid,
55
299
  restartCount: snapshot.restartCount,
56
300
  startedAt: snapshot.startedAt,
301
+ lastExitCode: snapshot.lastExitCode ?? null,
302
+ lastSignal: snapshot.lastSignal ?? null,
303
+ errorReason: snapshot.errorReason ?? null,
304
+ fixHint: snapshot.fixHint ?? null,
57
305
  }));
58
306
  }
59
307
  function formatStatusSummary(payload) {
@@ -86,6 +334,35 @@ function parseIncomingCommand(raw) {
86
334
  }
87
335
  return parsed;
88
336
  }
337
+ /**
338
+ * Handle agent.senseTurn command: runs a full agent turn via the daemon process.
339
+ * Dynamic import lazy-loads shared-turn. Hot-reload works because ouro dev
340
+ * restarts the daemon process (fresh module cache).
341
+ */
342
+ async function handleAgentSenseTurn(command) {
343
+ try {
344
+ const { setAgentName } = await Promise.resolve().then(() => __importStar(require("../identity")));
345
+ setAgentName(command.agent);
346
+ const { runSenseTurn } = await Promise.resolve().then(() => __importStar(require("../../senses/shared-turn")));
347
+ const result = await runSenseTurn({
348
+ agentName: command.agent,
349
+ channel: command.channel,
350
+ sessionKey: command.sessionKey,
351
+ friendId: command.friendId,
352
+ userMessage: command.message,
353
+ });
354
+ return {
355
+ ok: true,
356
+ message: result.response,
357
+ data: { ponderDeferred: result.ponderDeferred },
358
+ };
359
+ }
360
+ catch (error) {
361
+ /* v8 ignore next -- branch: String(error) fallback only for non-Error throws @preserve */
362
+ const errorMessage = error instanceof Error ? error.message : String(error);
363
+ return { ok: false, error: `sense turn failed: ${errorMessage}` };
364
+ }
365
+ }
89
366
  class OuroDaemon {
90
367
  socketPath;
91
368
  processManager;
@@ -94,7 +371,11 @@ class OuroDaemon {
94
371
  router;
95
372
  senseManager;
96
373
  bundlesRoot;
374
+ mode;
97
375
  server = null;
376
+ outlookServer = null;
377
+ socketIdentity = null;
378
+ outlookServerFactory;
98
379
  constructor(options) {
99
380
  this.socketPath = options.socketPath;
100
381
  this.processManager = options.processManager;
@@ -103,6 +384,72 @@ class OuroDaemon {
103
384
  this.router = options.router;
104
385
  this.senseManager = options.senseManager ?? null;
105
386
  this.bundlesRoot = options.bundlesRoot ?? (0, identity_1.getAgentBundlesRoot)();
387
+ this.mode = options.mode ?? "production";
388
+ this.outlookServerFactory = options.outlookServerFactory ?? this.createDefaultOutlookServer.bind(this);
389
+ }
390
+ /* v8 ignore start -- default outlook server wiring: production-only path, tests inject outlookServerFactory stub instead. startOutlookHttpServer itself has full coverage in outlook-http.test.ts @preserve */
391
+ createDefaultOutlookServer() {
392
+ return (0, outlook_http_1.startOutlookHttpServer)({
393
+ host: "127.0.0.1",
394
+ port: outlook_types_1.OUTLOOK_DEFAULT_PORT,
395
+ bundlesRoot: this.bundlesRoot,
396
+ readMachineState: () => (0, outlook_read_1.readOutlookMachineState)({ bundlesRoot: this.bundlesRoot }),
397
+ readMachineView: ({ machine }) => {
398
+ const overview = this.buildStatusPayload().overview;
399
+ return (0, outlook_view_1.buildOutlookMachineView)({
400
+ machine,
401
+ daemon: {
402
+ status: overview.daemon,
403
+ health: overview.health,
404
+ mode: overview.mode,
405
+ socketPath: overview.socketPath,
406
+ outlookUrl: overview.outlookUrl,
407
+ entryPath: overview.entryPath,
408
+ workerCount: overview.workerCount,
409
+ senseCount: overview.senseCount,
410
+ },
411
+ });
412
+ },
413
+ readAgentState: (agentName) => (0, outlook_read_1.readOutlookAgentState)(agentName, { bundlesRoot: this.bundlesRoot }),
414
+ readAgentView: (agentName) => {
415
+ const agent = (0, outlook_read_1.readOutlookAgentState)(agentName, { bundlesRoot: this.bundlesRoot });
416
+ return (0, outlook_view_1.buildOutlookAgentView)({
417
+ agent,
418
+ viewer: { kind: "human" },
419
+ });
420
+ },
421
+ });
422
+ }
423
+ /* v8 ignore stop */
424
+ buildStatusPayload() {
425
+ const snapshots = this.processManager.listAgentSnapshots();
426
+ const workers = buildWorkerRows(snapshots);
427
+ const senses = this.senseManager?.listSenseRows() ?? [];
428
+ const repoRoot = (0, identity_1.getRepoRoot)();
429
+ const sync = (0, agent_discovery_1.listBundleSyncRows)({ bundlesRoot: this.bundlesRoot });
430
+ const agents = (0, agent_discovery_1.listAllBundleAgents)({ bundlesRoot: this.bundlesRoot });
431
+ const providers = agents.flatMap((agent) => (0, provider_visibility_1.providerVisibilityStatusRows)((0, provider_visibility_1.buildAgentProviderVisibility)({
432
+ agentName: agent.name,
433
+ agentRoot: path.join(this.bundlesRoot, `${agent.name}.ouro`),
434
+ })));
435
+ return {
436
+ overview: {
437
+ daemon: "running",
438
+ health: workers.every((worker) => worker.status === "running") ? "ok" : "warn",
439
+ socketPath: this.socketPath,
440
+ outlookUrl: this.outlookServer?.origin ?? "http://127.0.0.1:0",
441
+ ...(0, runtime_metadata_1.getRuntimeMetadata)(),
442
+ workerCount: workers.length,
443
+ senseCount: senses.length,
444
+ entryPath: path.join(repoRoot, "dist", "heart", "daemon", "daemon-entry.js"),
445
+ mode: (0, runtime_mode_1.detectRuntimeMode)(repoRoot),
446
+ },
447
+ workers,
448
+ senses,
449
+ sync,
450
+ agents,
451
+ ...(providers.length > 0 ? { providers } : {}),
452
+ };
106
453
  }
107
454
  async start() {
108
455
  if (this.server)
@@ -113,61 +460,181 @@ class OuroDaemon {
113
460
  message: "starting daemon server",
114
461
  meta: { socketPath: this.socketPath },
115
462
  });
463
+ try {
464
+ await this.startInner();
465
+ }
466
+ catch (err) {
467
+ // Emit a paired terminating event (`_error`) so the nerves audit's
468
+ // start_end_pairing rule is satisfied when startup throws mid-sequence
469
+ // and `stop()` (which emits `server_end`) is never called.
470
+ (0, runtime_1.emitNervesEvent)({
471
+ level: "error",
472
+ component: "daemon",
473
+ event: "daemon.server_error",
474
+ message: "daemon start failed",
475
+ meta: {
476
+ error: err instanceof Error ? err.message : /* v8 ignore next -- defensive: non-Error catch branch @preserve */ String(err),
477
+ },
478
+ });
479
+ throw err;
480
+ }
481
+ }
482
+ async startInner() {
116
483
  // Register update hooks and apply pending updates before starting agents
117
484
  (0, update_hooks_1.registerUpdateHook)(bundle_meta_1.bundleMetaHook);
485
+ (0, update_hooks_1.registerUpdateHook)(agent_config_v2_1.agentConfigV2Hook);
118
486
  const currentVersion = (0, bundle_manifest_1.getPackageVersion)();
119
487
  await (0, update_hooks_1.applyPendingUpdates)(this.bundlesRoot, currentVersion);
120
488
  // Start periodic update checker (polls npm registry every 30 minutes)
489
+ // Skip in dev mode — dev builds should not auto-update from npm
121
490
  const bundlesRoot = this.bundlesRoot;
122
- const daemon = this;
123
- (0, update_checker_1.startUpdateChecker)({
124
- currentVersion,
125
- deps: {
126
- distTag: "alpha",
127
- fetchRegistryJson: /* v8 ignore next -- integration: real HTTP fetch @preserve */ async () => {
128
- const res = await fetch("https://registry.npmjs.org/@ouro.bot/cli");
129
- return res.json();
130
- },
131
- },
132
- onUpdate: /* v8 ignore start -- integration: real npm install + process spawn @preserve */ async (result) => {
133
- if (!result.latestVersion)
134
- return;
135
- await (0, staged_restart_1.performStagedRestart)(result.latestVersion, {
136
- execSync: (cmd) => (0, child_process_1.execSync)(cmd, { stdio: "inherit" }),
137
- spawnSync: child_process_1.spawnSync,
138
- resolveNewCodePath: (_version) => {
139
- try {
140
- const resolved = (0, child_process_1.execSync)(`node -e "console.log(require.resolve('@ouro.bot/cli/package.json'))"`, { encoding: "utf-8" }).trim();
141
- return resolved ? path.dirname(resolved) : null;
142
- }
143
- catch {
144
- return null;
145
- }
491
+ const daemonSocketPath = this.socketPath;
492
+ if (this.mode === "dev") {
493
+ (0, runtime_1.emitNervesEvent)({
494
+ component: "daemon",
495
+ event: "daemon.update_checker_skip",
496
+ message: "skipping update checker in dev mode",
497
+ meta: { reason: "dev mode" },
498
+ });
499
+ }
500
+ else {
501
+ const daemon = this;
502
+ (0, update_checker_1.startUpdateChecker)({
503
+ currentVersion,
504
+ deps: {
505
+ distTag: "alpha",
506
+ fetchRegistryJson: /* v8 ignore next -- integration: real HTTP fetch @preserve */ async () => {
507
+ const res = await fetch("https://registry.npmjs.org/@ouro.bot/cli");
508
+ return res.json();
146
509
  },
147
- gracefulShutdown: () => daemon.stop(),
148
- nodePath: process.execPath,
149
- bundlesRoot,
150
- });
151
- },
152
- /* v8 ignore stop */
153
- });
510
+ },
511
+ onUpdate: /* v8 ignore start -- integration: real npm install + process spawn @preserve */ async (result) => {
512
+ if (!result.latestVersion)
513
+ return;
514
+ // Install via the version manager (NOT `npm install -g`). The
515
+ // global install path doesn't end up on the daemon process's
516
+ // NODE_PATH, so the previous `require.resolve('@ouro.bot/cli')`
517
+ // -based path lookup always returned null and the staged restart
518
+ // never actually completed. Verified live on 2026-04-08:
519
+ // alpha.268 daemon detected alpha.270 was available, ran the
520
+ // staged restart, and bailed at `staged_restart_path_failed` —
521
+ // meaning the daemon could never auto-update itself and required
522
+ // manual `ouro up` to pick up new versions.
523
+ //
524
+ // Switch to the version-managed layout the CLI itself uses:
525
+ // installVersion(version) puts files at
526
+ // ~/.ouro-cli/versions/{version}/node_modules/@ouro.bot/cli
527
+ // which is a known path we can compute deterministically.
528
+ // Then activateVersion(version) flips the CurrentVersion symlink
529
+ // so the next `ouro up` from the user sees the same version
530
+ // the daemon is running.
531
+ const cliHome = (0, ouro_version_manager_1.getOuroCliHome)();
532
+ await (0, staged_restart_1.performStagedRestart)(result.latestVersion, {
533
+ execSync: (cmd) => (0, child_process_1.execSync)(cmd, { stdio: "inherit" }),
534
+ spawnSync: child_process_1.spawnSync,
535
+ installNewVersion: (version) => {
536
+ (0, ouro_version_manager_1.installVersion)(version, {});
537
+ (0, ouro_version_manager_1.activateVersion)(version, {});
538
+ },
539
+ resolveNewCodePath: (version) => {
540
+ const versionPath = path.join(cliHome, "versions", version, "node_modules", "@ouro.bot", "cli");
541
+ return fs.existsSync(versionPath) ? versionPath : null;
542
+ },
543
+ gracefulShutdown: () => daemon.stop(),
544
+ spawnNewDaemon: (entryPath, sock) => {
545
+ const outFd = fs.openSync(os.devNull, "w");
546
+ const errFd = fs.openSync(os.devNull, "w");
547
+ const child = (0, child_process_1.spawn)(process.execPath, [entryPath, "--socket", sock], {
548
+ detached: true,
549
+ stdio: ["ignore", outFd, errFd],
550
+ });
551
+ child.unref();
552
+ return { pid: child.pid ?? null };
553
+ },
554
+ nodePath: process.execPath,
555
+ bundlesRoot,
556
+ socketPath: daemonSocketPath,
557
+ });
558
+ },
559
+ /* v8 ignore stop */
560
+ });
561
+ }
562
+ // MCP connections are lazily initialized per-agent during senseTurn
563
+ // (daemon manages multiple agents; agent identity must be set before loading MCP config)
564
+ /* v8 ignore start -- orphan cleanup + pidfile: calls process management functions @preserve */
565
+ killOrphanProcesses();
566
+ /* v8 ignore stop */
154
567
  await this.processManager.startAutoStartAgents();
155
568
  await this.senseManager?.startAutoStartSenses();
569
+ // Write all managed PIDs to disk so the next daemon can clean up
570
+ /* v8 ignore start -- pidfile write: collects PIDs from process managers @preserve */
571
+ const agentPids = this.processManager.listAgentSnapshots().map((s) => s.pid).filter((p) => p !== null);
572
+ const sensePids = this.senseManager?.listManagedPids?.() ?? [];
573
+ writePidfile([...agentPids, ...sensePids]);
574
+ /* v8 ignore stop */
156
575
  this.scheduler.start?.();
157
576
  await this.scheduler.reconcile?.();
158
577
  await this.drainPendingBundleMessages();
578
+ await this.drainPendingSenseMessages();
579
+ // startInner is only reachable when this.server is null (guarded in
580
+ // start()), and stop() nulls out this.outlookServer alongside this.server,
581
+ // so outlookServer is guaranteed unset here — no need for a guard.
582
+ try {
583
+ this.outlookServer = await this.outlookServerFactory();
584
+ }
585
+ catch (error) {
586
+ (0, runtime_1.emitNervesEvent)({
587
+ level: "warn",
588
+ component: "daemon",
589
+ event: "daemon.outlook_start_failed",
590
+ message: `Outlook server failed to start: ${String(error)}`,
591
+ meta: { port: outlook_types_1.OUTLOOK_DEFAULT_PORT },
592
+ });
593
+ }
159
594
  if (fs.existsSync(this.socketPath)) {
160
595
  fs.unlinkSync(this.socketPath);
161
596
  }
162
- this.server = net.createServer((connection) => {
597
+ // allowHalfOpen: true lets the server keep its writable side open after
598
+ // the client sends FIN. Without this, when a client calls `client.end()`
599
+ // after writing a command, node closes the server's writable side
600
+ // automatically — so a long-running response (like an agent.senseTurn
601
+ // LLM turn that takes 5+ seconds) never reaches the client. The
602
+ // socket-client fix in #303/#334 also removed client.end() on the
603
+ // sending side, but this option is defense in depth: even if a future
604
+ // caller half-closes, the server still writes its response correctly.
605
+ this.server = net.createServer({ allowHalfOpen: true }, (connection) => {
163
606
  let raw = "";
164
607
  let responded = false;
608
+ /* v8 ignore start — connection error handler requires real socket error @preserve */
609
+ connection.on("error", (err) => {
610
+ (0, runtime_1.emitNervesEvent)({
611
+ level: "warn",
612
+ component: "daemon",
613
+ event: "daemon.connection_error",
614
+ message: "socket connection error",
615
+ meta: { error: err.message, code: err.code ?? null },
616
+ });
617
+ });
618
+ /* v8 ignore stop */
165
619
  const flushResponse = async () => {
166
620
  if (responded)
167
621
  return;
168
622
  responded = true;
169
623
  const response = await this.handleRawPayload(raw);
170
- connection.end(response);
624
+ try {
625
+ connection.end(response);
626
+ /* v8 ignore start — EPIPE catch requires real socket disconnect @preserve */
627
+ }
628
+ catch (err) {
629
+ (0, runtime_1.emitNervesEvent)({
630
+ level: "warn",
631
+ component: "daemon",
632
+ event: "daemon.connection_end_error",
633
+ message: "failed to send response to client (EPIPE)",
634
+ meta: { error: err instanceof Error ? err.message : String(err) },
635
+ });
636
+ }
637
+ /* v8 ignore stop */
171
638
  };
172
639
  connection.on("data", (chunk) => {
173
640
  raw += chunk.toString("utf-8");
@@ -180,7 +647,23 @@ class OuroDaemon {
180
647
  const server = this.server;
181
648
  await new Promise((resolve, reject) => {
182
649
  server.once("error", reject);
183
- server.listen(this.socketPath, () => resolve());
650
+ server.listen(this.socketPath, () => {
651
+ // Replace the one-time error listener with a persistent one after successful listen
652
+ server.removeAllListeners("error");
653
+ this.socketIdentity = readSocketIdentity(this.socketPath);
654
+ /* v8 ignore start — server error after listen requires real socket race condition @preserve */
655
+ server.on("error", (err) => {
656
+ (0, runtime_1.emitNervesEvent)({
657
+ level: "error",
658
+ component: "daemon",
659
+ event: "daemon.server_error",
660
+ message: "daemon server error after listen",
661
+ meta: { error: err.message, code: err.code ?? null },
662
+ });
663
+ });
664
+ /* v8 ignore stop */
665
+ resolve();
666
+ });
184
667
  });
185
668
  }
186
669
  async drainPendingBundleMessages() {
@@ -231,26 +714,170 @@ class OuroDaemon {
231
714
  fs.writeFileSync(pendingPath, next, "utf-8");
232
715
  }
233
716
  }
717
+ /** Drains per-sense pending dirs for always-on senses across all agents. */
718
+ static ALWAYS_ON_SENSES = new Set((0, channel_1.getAlwaysOnSenseNames)());
719
+ async drainPendingSenseMessages() {
720
+ if (!fs.existsSync(this.bundlesRoot))
721
+ return;
722
+ let bundleDirs;
723
+ try {
724
+ bundleDirs = fs.readdirSync(this.bundlesRoot, { withFileTypes: true });
725
+ }
726
+ catch {
727
+ return;
728
+ }
729
+ for (const bundleDir of bundleDirs) {
730
+ if (!bundleDir.isDirectory() || !bundleDir.name.endsWith(".ouro"))
731
+ continue;
732
+ const agentName = bundleDir.name.replace(/\.ouro$/, "");
733
+ const pendingRoot = path.join(this.bundlesRoot, bundleDir.name, "state", "pending");
734
+ if (!fs.existsSync(pendingRoot))
735
+ continue;
736
+ let friendDirs;
737
+ try {
738
+ friendDirs = fs.readdirSync(pendingRoot, { withFileTypes: true });
739
+ }
740
+ catch {
741
+ continue;
742
+ }
743
+ for (const friendDir of friendDirs) {
744
+ if (!friendDir.isDirectory())
745
+ continue;
746
+ const friendPath = path.join(pendingRoot, friendDir.name);
747
+ let channelDirs;
748
+ try {
749
+ channelDirs = fs.readdirSync(friendPath, { withFileTypes: true });
750
+ }
751
+ catch {
752
+ continue;
753
+ }
754
+ for (const channelDir of channelDirs) {
755
+ if (!channelDir.isDirectory())
756
+ continue;
757
+ if (!OuroDaemon.ALWAYS_ON_SENSES.has(channelDir.name))
758
+ continue;
759
+ const channelPath = path.join(friendPath, channelDir.name);
760
+ let keyDirs;
761
+ try {
762
+ keyDirs = fs.readdirSync(channelPath, { withFileTypes: true });
763
+ }
764
+ catch {
765
+ continue;
766
+ }
767
+ for (const keyDir of keyDirs) {
768
+ if (!keyDir.isDirectory())
769
+ continue;
770
+ const leafDir = path.join(channelPath, keyDir.name);
771
+ const messages = (0, pending_1.drainPending)(leafDir);
772
+ for (const msg of messages) {
773
+ try {
774
+ await this.router.send({
775
+ from: msg.from,
776
+ to: agentName,
777
+ content: msg.content,
778
+ priority: "normal",
779
+ });
780
+ }
781
+ catch {
782
+ // Best-effort delivery — log and continue
783
+ }
784
+ }
785
+ if (messages.length > 0) {
786
+ (0, runtime_1.emitNervesEvent)({
787
+ component: "daemon",
788
+ event: "daemon.startup_sense_drain",
789
+ message: "drained pending sense messages on startup",
790
+ meta: {
791
+ agent: agentName,
792
+ channel: channelDir.name,
793
+ friendId: friendDir.name,
794
+ key: keyDir.name,
795
+ count: messages.length,
796
+ },
797
+ });
798
+ }
799
+ }
800
+ }
801
+ }
802
+ }
803
+ }
234
804
  async stop() {
805
+ // Must be named `_end` (not `_stop`) to satisfy the nerves audit's
806
+ // start/end pairing rule — see src/nerves/coverage/audit-rules.ts.
807
+ // This is the counterpart to `daemon.server_start` emitted at line 480.
235
808
  (0, runtime_1.emitNervesEvent)({
236
809
  component: "daemon",
237
- event: "daemon.server_stop",
810
+ event: "daemon.server_end",
238
811
  message: "stopping daemon server",
239
812
  meta: { socketPath: this.socketPath },
240
813
  });
241
814
  (0, update_checker_1.stopUpdateChecker)();
815
+ (0, mcp_manager_1.shutdownSharedMcpManager)();
242
816
  this.scheduler.stop?.();
243
817
  await this.processManager.stopAll();
244
818
  await this.senseManager?.stopAll();
245
819
  if (this.server) {
246
- await new Promise((resolve) => {
247
- this.server?.close(() => resolve());
248
- });
820
+ // DO NOT `await` server.close() here. server.close() resolves only
821
+ // after every open connection has closed. When stop() is invoked
822
+ // from the daemon.stop command handler, the calling client's
823
+ // connection is STILL open — its flushResponse() is currently
824
+ // awaiting THIS function. Awaiting close() creates a deadlock:
825
+ //
826
+ // client → flushResponse → handleRawPayload → daemon.stop case
827
+ // → stop() → await server.close() (waits for client's connection)
828
+ // → client's connection waits for flushResponse to call
829
+ // connection.end() → DEADLOCK
830
+ //
831
+ // Both processes sit in kevent forever. Verified live on
832
+ // 2026-04-08: alpha.268 daemon hung at `daemon.server_end` log
833
+ // line for 5+ minutes after a client sent daemon.stop, while the
834
+ // client (alpha.270 ouro up) hung waiting for the response.
835
+ //
836
+ // This regressed when #303/#334/#339 stopped half-closing the
837
+ // client socket and switched the server to allowHalfOpen: true.
838
+ // Previously, the client called .end() after writing its command,
839
+ // which (with allowHalfOpen: false) caused node to auto-tear-down
840
+ // the server's writable side — incidentally unblocking
841
+ // server.close() before the response was sent. The half-close
842
+ // breakage masked this deadlock; the fix exposed it.
843
+ //
844
+ // Solution: fire close() and let it complete asynchronously. Once
845
+ // stop() returns, the daemon.stop case returns its response,
846
+ // flushResponse() calls connection.end(response), the connection
847
+ // closes, and server.close()'s pending callback fires. The event
848
+ // loop drains and the daemon exits cleanly.
849
+ this.server.close();
249
850
  this.server = null;
250
851
  }
251
- if (fs.existsSync(this.socketPath)) {
852
+ if (this.outlookServer) {
853
+ await this.outlookServer.stop();
854
+ this.outlookServer = null;
855
+ }
856
+ const socketPathExists = fs.existsSync(this.socketPath);
857
+ const currentSocketIdentity = socketPathExists ? readSocketIdentity(this.socketPath) : null;
858
+ if (sameSocketIdentity(this.socketIdentity, currentSocketIdentity)) {
252
859
  fs.unlinkSync(this.socketPath);
253
860
  }
861
+ else if (socketPathExists) {
862
+ const expectedSocketIdentity = { dev: null, ino: null, ctimeMs: null, ...this.socketIdentity };
863
+ const actualSocketIdentity = { dev: null, ino: null, ctimeMs: null, ...currentSocketIdentity };
864
+ (0, runtime_1.emitNervesEvent)({
865
+ level: "warn",
866
+ component: "daemon",
867
+ event: "daemon.socket_cleanup_skipped",
868
+ message: "skipped daemon socket cleanup because the socket path no longer belongs to this daemon",
869
+ meta: {
870
+ socketPath: this.socketPath,
871
+ expectedDev: expectedSocketIdentity.dev,
872
+ expectedIno: expectedSocketIdentity.ino,
873
+ expectedCtimeMs: expectedSocketIdentity.ctimeMs,
874
+ actualDev: actualSocketIdentity.dev,
875
+ actualIno: actualSocketIdentity.ino,
876
+ actualCtimeMs: actualSocketIdentity.ctimeMs,
877
+ },
878
+ });
879
+ }
880
+ this.socketIdentity = null;
254
881
  }
255
882
  async handleRawPayload(raw) {
256
883
  try {
@@ -272,6 +899,27 @@ class OuroDaemon {
272
899
  message: "handling daemon command",
273
900
  meta: { kind: command.kind },
274
901
  });
902
+ try {
903
+ return await this.handleCommandInner(command);
904
+ /* v8 ignore start — command error catch tested in daemon-command-error.test; instanceof branches defensive @preserve */
905
+ }
906
+ catch (error) {
907
+ (0, runtime_1.emitNervesEvent)({
908
+ level: "error",
909
+ component: "daemon",
910
+ event: "daemon.command_error",
911
+ message: "unexpected error handling daemon command",
912
+ meta: {
913
+ kind: command.kind,
914
+ error: error instanceof Error ? error.message : String(error),
915
+ stack: error instanceof Error ? error.stack ?? null : null,
916
+ },
917
+ });
918
+ throw error;
919
+ }
920
+ /* v8 ignore stop */
921
+ }
922
+ async handleCommandInner(command) {
275
923
  switch (command.kind) {
276
924
  case "daemon.start":
277
925
  await this.start();
@@ -280,21 +928,7 @@ class OuroDaemon {
280
928
  await this.stop();
281
929
  return { ok: true, message: "daemon stopped" };
282
930
  case "daemon.status": {
283
- const snapshots = this.processManager.listAgentSnapshots();
284
- const workers = buildWorkerRows(snapshots);
285
- const senses = this.senseManager?.listSenseRows() ?? [];
286
- const data = {
287
- overview: {
288
- daemon: "running",
289
- health: workers.every((worker) => worker.status === "running") ? "ok" : "warn",
290
- socketPath: this.socketPath,
291
- ...(0, runtime_metadata_1.getRuntimeMetadata)(),
292
- workerCount: workers.length,
293
- senseCount: senses.length,
294
- },
295
- workers,
296
- senses,
297
- };
931
+ const data = this.buildStatusPayload();
298
932
  return {
299
933
  ok: true,
300
934
  summary: formatStatusSummary(data),
@@ -311,7 +945,7 @@ class OuroDaemon {
311
945
  ok: true,
312
946
  summary: "logs: use `ouro logs` to tail daemon and agent output",
313
947
  message: "log streaming available via ouro logs",
314
- data: { logDir: "~/.agentstate/daemon/logs" },
948
+ data: { logDir: "~/AgentBundles/<agent>.ouro/state/daemon/logs" },
315
949
  };
316
950
  case "agent.start":
317
951
  await this.processManager.startAgent(command.agent);
@@ -322,6 +956,35 @@ class OuroDaemon {
322
956
  case "agent.restart":
323
957
  await this.processManager.restartAgent?.(command.agent);
324
958
  return { ok: true, message: `restarted ${command.agent}` };
959
+ case "agent.ask":
960
+ return (0, agent_service_1.handleAgentAsk)(command);
961
+ case "agent.status":
962
+ return (0, agent_service_1.handleAgentStatus)(command);
963
+ case "agent.catchup":
964
+ return (0, agent_service_1.handleAgentCatchup)(command);
965
+ case "agent.delegate":
966
+ return (0, agent_service_1.handleAgentDelegate)(command);
967
+ case "agent.getContext":
968
+ return (0, agent_service_1.handleAgentGetContext)(command);
969
+ case "agent.searchNotes":
970
+ return (0, agent_service_1.handleAgentSearchNotes)(command);
971
+ case "agent.getTask":
972
+ return (0, agent_service_1.handleAgentGetTask)(command);
973
+ case "agent.checkScope":
974
+ return (0, agent_service_1.handleAgentCheckScope)(command);
975
+ case "agent.requestDecision":
976
+ return (0, agent_service_1.handleAgentRequestDecision)(command);
977
+ case "agent.checkGuidance":
978
+ return (0, agent_service_1.handleAgentCheckGuidance)(command);
979
+ case "agent.reportProgress":
980
+ return (0, agent_service_1.handleAgentReportProgress)(command);
981
+ case "agent.reportBlocker":
982
+ return (0, agent_service_1.handleAgentReportBlocker)(command);
983
+ case "agent.reportComplete":
984
+ return (0, agent_service_1.handleAgentReportComplete)(command);
985
+ case "agent.senseTurn":
986
+ return handleAgentSenseTurn(command);
987
+ /* v8 ignore stop */
325
988
  case "cron.list": {
326
989
  const jobs = this.scheduler.listJobs();
327
990
  const summary = jobs.length === 0
@@ -353,6 +1016,13 @@ class OuroDaemon {
353
1016
  data: messages,
354
1017
  };
355
1018
  }
1019
+ case "inner.wake":
1020
+ await this.processManager.startAgent(command.agent);
1021
+ this.processManager.sendToAgent?.(command.agent, { type: "message" });
1022
+ return {
1023
+ ok: true,
1024
+ message: `woke inner dialog for ${command.agent}`,
1025
+ };
356
1026
  case "chat.connect":
357
1027
  await this.processManager.startAgent(command.agent);
358
1028
  return {
@@ -375,6 +1045,35 @@ class OuroDaemon {
375
1045
  data: receipt,
376
1046
  };
377
1047
  }
1048
+ case "habit.poke": {
1049
+ this.processManager.sendToAgent?.(command.agent, { type: "habit", habitName: command.habitName });
1050
+ return {
1051
+ ok: true,
1052
+ message: `poked habit ${command.habitName} for ${command.agent}`,
1053
+ };
1054
+ }
1055
+ case "mcp.list": {
1056
+ const mcpManager = await (0, mcp_manager_1.getSharedMcpManager)();
1057
+ if (!mcpManager) {
1058
+ return { ok: true, data: [], message: "no MCP servers configured" };
1059
+ }
1060
+ return { ok: true, data: mcpManager.listAllTools() };
1061
+ }
1062
+ case "mcp.call": {
1063
+ const mcpCallManager = await (0, mcp_manager_1.getSharedMcpManager)();
1064
+ if (!mcpCallManager) {
1065
+ return { ok: false, error: "no MCP servers configured" };
1066
+ }
1067
+ try {
1068
+ const parsedArgs = command.args ? JSON.parse(command.args) : {};
1069
+ const result = await mcpCallManager.callTool(command.server, command.tool, parsedArgs);
1070
+ return { ok: true, data: result };
1071
+ }
1072
+ catch (error) {
1073
+ /* v8 ignore next -- defensive: callTool errors are always Error instances @preserve */
1074
+ return { ok: false, error: error instanceof Error ? error.message : String(error) };
1075
+ }
1076
+ }
378
1077
  case "hatch.start":
379
1078
  return {
380
1079
  ok: true,