@ouro.bot/cli 0.1.0-alpha.19 → 0.1.0-alpha.191

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 (202) hide show
  1. package/README.md +187 -191
  2. package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/agent.json +3 -2
  3. package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/SOUL.md +1 -1
  4. package/changelog.json +1200 -0
  5. package/dist/heart/active-work.js +832 -0
  6. package/dist/heart/bridges/manager.js +358 -0
  7. package/dist/heart/bridges/state-machine.js +135 -0
  8. package/dist/heart/bridges/store.js +123 -0
  9. package/dist/heart/cares.js +174 -0
  10. package/dist/heart/commitments.js +111 -0
  11. package/dist/heart/config.js +40 -30
  12. package/dist/heart/core.js +620 -124
  13. package/dist/heart/cross-chat-delivery.js +146 -0
  14. package/dist/heart/daemon/agent-discovery.js +81 -0
  15. package/dist/heart/daemon/agent-service.js +360 -0
  16. package/dist/heart/daemon/auth-flow.js +477 -0
  17. package/dist/heart/daemon/cadence.js +70 -0
  18. package/dist/heart/daemon/daemon-cli.js +2470 -210
  19. package/dist/heart/daemon/daemon-entry.js +157 -4
  20. package/dist/heart/daemon/daemon-health.js +136 -0
  21. package/dist/heart/daemon/daemon-runtime-sync.js +212 -0
  22. package/dist/heart/daemon/daemon-tombstone.js +123 -0
  23. package/dist/heart/daemon/daemon.js +449 -17
  24. package/dist/heart/daemon/habit-migration.js +181 -0
  25. package/dist/heart/daemon/habit-parser.js +119 -0
  26. package/dist/heart/daemon/habit-scheduler.js +369 -0
  27. package/dist/heart/daemon/hatch-flow.js +31 -112
  28. package/dist/heart/daemon/hatch-specialist.js +3 -3
  29. package/dist/heart/daemon/hooks/agent-config-v2.js +33 -0
  30. package/dist/heart/daemon/hooks/bundle-meta.js +92 -0
  31. package/dist/heart/daemon/inner-status.js +89 -0
  32. package/dist/heart/daemon/launchd.js +165 -0
  33. package/dist/heart/daemon/log-tailer.js +4 -3
  34. package/dist/heart/daemon/mcp-server.js +591 -0
  35. package/dist/heart/daemon/message-router.js +17 -8
  36. package/dist/heart/daemon/os-cron-deps.js +134 -0
  37. package/dist/heart/daemon/ouro-bot-global-installer.js +128 -0
  38. package/dist/heart/daemon/ouro-path-installer.js +78 -34
  39. package/dist/heart/daemon/ouro-version-manager.js +171 -0
  40. package/dist/heart/daemon/outlook-http.js +399 -0
  41. package/dist/heart/daemon/outlook-read.js +1331 -0
  42. package/dist/heart/daemon/outlook-render.js +1032 -0
  43. package/dist/heart/daemon/outlook-types.js +7 -0
  44. package/dist/heart/daemon/outlook-view.js +194 -0
  45. package/dist/heart/daemon/process-manager.js +75 -0
  46. package/dist/heart/daemon/run-hooks.js +39 -0
  47. package/dist/heart/daemon/runtime-logging.js +58 -15
  48. package/dist/heart/daemon/runtime-metadata.js +219 -0
  49. package/dist/heart/daemon/runtime-mode.js +67 -0
  50. package/dist/heart/daemon/safe-mode.js +161 -0
  51. package/dist/heart/daemon/sense-manager.js +50 -2
  52. package/dist/heart/daemon/session-id-resolver.js +131 -0
  53. package/dist/heart/daemon/skill-management-installer.js +94 -0
  54. package/dist/heart/daemon/socket-client.js +202 -0
  55. package/dist/heart/daemon/specialist-prompt.js +10 -7
  56. package/dist/heart/daemon/specialist-tools.js +49 -3
  57. package/dist/heart/daemon/staged-restart.js +114 -0
  58. package/dist/heart/daemon/task-scheduler.js +3 -25
  59. package/dist/heart/daemon/thoughts.js +507 -0
  60. package/dist/heart/daemon/update-checker.js +111 -0
  61. package/dist/heart/daemon/update-hooks.js +138 -0
  62. package/dist/heart/daemon/wrapper-publish-guard.js +86 -0
  63. package/dist/heart/delegation.js +62 -0
  64. package/dist/heart/identity.js +136 -55
  65. package/dist/heart/intentions.js +168 -0
  66. package/dist/heart/kicks.js +2 -20
  67. package/dist/heart/migrate-config.js +127 -0
  68. package/dist/heart/model-capabilities.js +48 -0
  69. package/dist/heart/obligations.js +220 -0
  70. package/dist/heart/presence.js +185 -0
  71. package/dist/heart/progress-story.js +42 -0
  72. package/dist/heart/provider-failover.js +88 -0
  73. package/dist/heart/provider-ping.js +172 -0
  74. package/dist/heart/providers/anthropic-token.js +163 -0
  75. package/dist/heart/providers/anthropic.js +187 -41
  76. package/dist/heart/providers/azure.js +116 -10
  77. package/dist/heart/providers/github-copilot.js +157 -0
  78. package/dist/heart/providers/minimax.js +33 -3
  79. package/dist/heart/providers/openai-codex.js +51 -16
  80. package/dist/heart/session-activity.js +172 -0
  81. package/dist/heart/session-recall.js +216 -0
  82. package/dist/heart/streaming.js +36 -27
  83. package/dist/heart/target-resolution.js +127 -0
  84. package/dist/heart/tempo.js +93 -0
  85. package/dist/heart/temporal-view.js +41 -0
  86. package/dist/heart/tool-activity-callbacks.js +36 -0
  87. package/dist/heart/tool-description.js +135 -0
  88. package/dist/heart/tool-loop.js +200 -0
  89. package/dist/heart/turn-coordinator.js +28 -0
  90. package/dist/heart/wake-packet.js +226 -0
  91. package/dist/mind/associative-recall.js +122 -32
  92. package/dist/mind/bundle-manifest.js +14 -0
  93. package/dist/mind/context.js +125 -14
  94. package/dist/mind/{memory.js → diary.js} +70 -56
  95. package/dist/mind/episodes.js +116 -0
  96. package/dist/mind/file-state.js +179 -0
  97. package/dist/mind/first-impressions.js +16 -2
  98. package/dist/mind/friends/channel.js +56 -0
  99. package/dist/mind/friends/group-context.js +144 -0
  100. package/dist/mind/friends/store-file.js +36 -0
  101. package/dist/mind/friends/trust-explanation.js +74 -0
  102. package/dist/mind/friends/types.js +8 -0
  103. package/dist/mind/journal-index.js +161 -0
  104. package/dist/mind/obligation-steering.js +221 -0
  105. package/dist/mind/obligations.js +134 -0
  106. package/dist/mind/pending.js +76 -9
  107. package/dist/mind/phrases.js +1 -0
  108. package/dist/mind/prompt.js +779 -102
  109. package/dist/mind/scrutiny.js +173 -0
  110. package/dist/mind/token-estimate.js +8 -12
  111. package/dist/nerves/coverage/audit.js +1 -1
  112. package/dist/nerves/coverage/run-artifacts.js +1 -1
  113. package/dist/nerves/event-buffer.js +111 -0
  114. package/dist/nerves/index.js +63 -3
  115. package/dist/nerves/observation.js +20 -0
  116. package/dist/nerves/runtime.js +5 -1
  117. package/dist/outlook-ui/assets/index-DN6vVYPm.css +1 -0
  118. package/dist/outlook-ui/assets/index-M1l1tDTX.js +61 -0
  119. package/dist/outlook-ui/index.html +15 -0
  120. package/dist/repertoire/ado-semantic.js +11 -5
  121. package/dist/repertoire/coding/codex-jsonl.js +64 -0
  122. package/dist/repertoire/coding/context-pack.js +330 -0
  123. package/dist/repertoire/coding/feedback.js +197 -30
  124. package/dist/repertoire/coding/manager.js +149 -2
  125. package/dist/repertoire/coding/spawner.js +55 -9
  126. package/dist/repertoire/coding/tools.js +170 -7
  127. package/dist/repertoire/guardrails.js +291 -0
  128. package/dist/repertoire/mcp-client.js +254 -0
  129. package/dist/repertoire/mcp-manager.js +251 -0
  130. package/dist/repertoire/shell-sessions.js +133 -0
  131. package/dist/repertoire/skills.js +14 -23
  132. package/dist/repertoire/tasks/board.js +43 -5
  133. package/dist/repertoire/tasks/fix.js +182 -0
  134. package/dist/repertoire/tasks/index.js +28 -10
  135. package/dist/repertoire/tasks/lifecycle.js +2 -2
  136. package/dist/repertoire/tasks/parser.js +3 -2
  137. package/dist/repertoire/tasks/scanner.js +194 -37
  138. package/dist/repertoire/tasks/transitions.js +11 -28
  139. package/dist/repertoire/tools-base.js +1345 -320
  140. package/dist/repertoire/tools-bluebubbles.js +94 -0
  141. package/dist/repertoire/tools-github.js +1 -6
  142. package/dist/repertoire/tools-teams.js +12 -59
  143. package/dist/repertoire/tools.js +318 -124
  144. package/dist/scripts/claude-code-hook.js +41 -0
  145. package/dist/scripts/claude-code-stop-hook.js +47 -0
  146. package/dist/senses/attention-queue.js +97 -0
  147. package/dist/senses/bluebubbles-client.js +102 -3
  148. package/dist/senses/bluebubbles-entry.js +2 -0
  149. package/dist/senses/bluebubbles-inbound-log.js +109 -0
  150. package/dist/senses/bluebubbles-media.js +118 -23
  151. package/dist/senses/bluebubbles-model.js +12 -4
  152. package/dist/senses/bluebubbles-mutation-log.js +45 -5
  153. package/dist/senses/bluebubbles-runtime-state.js +109 -0
  154. package/dist/senses/bluebubbles-session-cleanup.js +72 -0
  155. package/dist/senses/bluebubbles.js +1008 -102
  156. package/dist/senses/cli/adapter.js +274 -0
  157. package/dist/senses/cli/ink-app.js +156 -0
  158. package/dist/senses/cli/inline-diff.js +64 -0
  159. package/dist/senses/cli/message-list.js +51 -0
  160. package/dist/senses/cli/ouro-tui.js +349 -0
  161. package/dist/senses/cli/spinner-imperative.js +135 -0
  162. package/dist/senses/cli/spinner.js +101 -0
  163. package/dist/senses/cli/status-line.js +60 -0
  164. package/dist/senses/cli/streaming-markdown.js +526 -0
  165. package/dist/senses/cli/tool-display.js +83 -0
  166. package/dist/senses/cli/tool-render.js +85 -0
  167. package/dist/senses/cli/tui-store.js +198 -0
  168. package/dist/senses/cli/virtual-list.js +35 -0
  169. package/dist/senses/cli-entry.js +1 -1
  170. package/dist/senses/cli-layout.js +187 -0
  171. package/dist/senses/cli.js +508 -243
  172. package/dist/senses/commands.js +65 -1
  173. package/dist/senses/continuity.js +94 -0
  174. package/dist/senses/habit-turn-message.js +108 -0
  175. package/dist/senses/inner-dialog-worker.js +112 -19
  176. package/dist/senses/inner-dialog.js +592 -84
  177. package/dist/senses/pipeline.js +633 -0
  178. package/dist/senses/shared-turn.js +196 -0
  179. package/dist/senses/surface-tool.js +95 -0
  180. package/dist/senses/teams.js +667 -120
  181. package/dist/senses/trust-gate.js +112 -2
  182. package/package.json +16 -6
  183. package/skills/configure-dev-tools.md +81 -0
  184. package/subagents/README.md +4 -70
  185. package/dist/heart/daemon/subagent-installer.js +0 -134
  186. package/dist/senses/debug-activity.js +0 -107
  187. package/subagents/work-doer.md +0 -235
  188. package/subagents/work-merger.md +0 -618
  189. package/subagents/work-planner.md +0 -382
  190. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/basilisk.md +0 -0
  191. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/jafar.md +0 -0
  192. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/jormungandr.md +0 -0
  193. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/kaa.md +0 -0
  194. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/medusa.md +0 -0
  195. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/monty.md +0 -0
  196. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/nagini.md +0 -0
  197. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/ouroboros.md +0 -0
  198. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/python.md +0 -0
  199. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/quetzalcoatl.md +0 -0
  200. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/sir-hiss.md +0 -0
  201. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/the-serpent.md +0 -0
  202. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/the-snake.md +0 -0
@@ -34,11 +34,107 @@ var __importStar = (this && this.__importStar) || (function () {
34
34
  })();
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
36
  exports.OuroDaemon = void 0;
37
+ exports.killOrphanProcesses = killOrphanProcesses;
38
+ exports.writePidfile = writePidfile;
37
39
  const fs = __importStar(require("fs"));
38
40
  const net = __importStar(require("net"));
41
+ const os = __importStar(require("os"));
39
42
  const path = __importStar(require("path"));
40
43
  const identity_1 = require("../identity");
41
44
  const runtime_1 = require("../../nerves/runtime");
45
+ const runtime_metadata_1 = require("./runtime-metadata");
46
+ const runtime_mode_1 = require("./runtime-mode");
47
+ const update_hooks_1 = require("./update-hooks");
48
+ const bundle_meta_1 = require("./hooks/bundle-meta");
49
+ const agent_config_v2_1 = require("./hooks/agent-config-v2");
50
+ const bundle_manifest_1 = require("../../mind/bundle-manifest");
51
+ const update_checker_1 = require("./update-checker");
52
+ const staged_restart_1 = require("./staged-restart");
53
+ const child_process_1 = require("child_process");
54
+ const pending_1 = require("../../mind/pending");
55
+ const agent_service_1 = require("./agent-service");
56
+ const channel_1 = require("../../mind/friends/channel");
57
+ const mcp_manager_1 = require("../../repertoire/mcp-manager");
58
+ const outlook_http_1 = require("./outlook-http");
59
+ const outlook_types_1 = require("./outlook-types");
60
+ const outlook_read_1 = require("./outlook-read");
61
+ const outlook_view_1 = require("./outlook-view");
62
+ const PIDFILE_PATH = path.join(os.homedir(), ".ouro-cli", "daemon.pids");
63
+ /**
64
+ * Kill all ouro processes from the previous daemon instance using the pidfile.
65
+ * On startup, reads PIDs from ~/.ouro-cli/daemon.pids, kills them all, then
66
+ * deletes the file. The new daemon writes its own PIDs after spawning.
67
+ *
68
+ * Falls back to ps-based scanning if the pidfile doesn't exist (first run
69
+ * or manual cleanup).
70
+ */
71
+ /* v8 ignore start -- process lifecycle: uses kill/ps, tested via deployment @preserve */
72
+ function killOrphanProcesses() {
73
+ try {
74
+ let pidsToKill = [];
75
+ // Primary: read pidfile from previous daemon
76
+ try {
77
+ const raw = fs.readFileSync(PIDFILE_PATH, "utf-8");
78
+ pidsToKill = raw.split("\n").map((s) => parseInt(s.trim(), 10)).filter((n) => !isNaN(n) && n !== process.pid);
79
+ fs.unlinkSync(PIDFILE_PATH);
80
+ }
81
+ catch {
82
+ // No pidfile — fall back to ps scan
83
+ }
84
+ // Fallback: scan ps for daemon-owned processes only (not MCP servers or external tools)
85
+ if (pidsToKill.length === 0) {
86
+ try {
87
+ const result = (0, child_process_1.execSync)("ps -eo pid,command", { encoding: "utf-8", timeout: 5000 });
88
+ for (const line of result.split("\n")) {
89
+ // Only match daemon-owned entry points, NOT mcp-serve or other external processes
90
+ if (line.includes("mcp-serve") || line.includes("mcp serve"))
91
+ continue;
92
+ if (!line.includes("agent-entry.js") && !line.includes("daemon-entry.js") && !line.includes("bluebubbles-entry.js") && !line.includes("teams-entry.js"))
93
+ continue;
94
+ const pid = parseInt(line.trim(), 10);
95
+ if (!isNaN(pid) && pid !== process.pid)
96
+ pidsToKill.push(pid);
97
+ }
98
+ }
99
+ catch { /* ps failed — best effort */ }
100
+ }
101
+ if (pidsToKill.length > 0) {
102
+ for (const pid of pidsToKill) {
103
+ try {
104
+ process.kill(pid, "SIGTERM");
105
+ }
106
+ catch { /* already exited */ }
107
+ }
108
+ (0, runtime_1.emitNervesEvent)({
109
+ component: "daemon",
110
+ event: "daemon.orphan_cleanup",
111
+ message: `killed ${pidsToKill.length} orphaned ouro processes`,
112
+ meta: { pids: pidsToKill },
113
+ });
114
+ }
115
+ }
116
+ catch (error) {
117
+ (0, runtime_1.emitNervesEvent)({
118
+ level: "warn",
119
+ component: "daemon",
120
+ event: "daemon.orphan_cleanup_error",
121
+ message: "failed to clean up orphaned ouro processes",
122
+ meta: { error: error instanceof Error ? error.message : String(error) },
123
+ });
124
+ }
125
+ }
126
+ /**
127
+ * Write all managed PIDs (daemon + children) to the pidfile.
128
+ * Called after all agents and senses are spawned.
129
+ */
130
+ function writePidfile(extraPids = []) {
131
+ try {
132
+ const pids = [process.pid, ...extraPids].filter(Boolean);
133
+ fs.mkdirSync(path.dirname(PIDFILE_PATH), { recursive: true });
134
+ fs.writeFileSync(PIDFILE_PATH, pids.join("\n") + "\n", "utf-8");
135
+ }
136
+ catch { /* best effort */ }
137
+ }
42
138
  function buildWorkerRows(snapshots) {
43
139
  return snapshots.map((snapshot) => ({
44
140
  agent: snapshot.name,
@@ -47,6 +143,8 @@ function buildWorkerRows(snapshots) {
47
143
  pid: snapshot.pid,
48
144
  restartCount: snapshot.restartCount,
49
145
  startedAt: snapshot.startedAt,
146
+ lastExitCode: snapshot.lastExitCode ?? null,
147
+ lastSignal: snapshot.lastSignal ?? null,
50
148
  }));
51
149
  }
52
150
  function formatStatusSummary(payload) {
@@ -87,7 +185,9 @@ class OuroDaemon {
87
185
  router;
88
186
  senseManager;
89
187
  bundlesRoot;
188
+ mode;
90
189
  server = null;
190
+ outlookServer = null;
91
191
  constructor(options) {
92
192
  this.socketPath = options.socketPath;
93
193
  this.processManager = options.processManager;
@@ -96,6 +196,28 @@ class OuroDaemon {
96
196
  this.router = options.router;
97
197
  this.senseManager = options.senseManager ?? null;
98
198
  this.bundlesRoot = options.bundlesRoot ?? (0, identity_1.getAgentBundlesRoot)();
199
+ this.mode = options.mode ?? "production";
200
+ }
201
+ buildStatusPayload() {
202
+ const snapshots = this.processManager.listAgentSnapshots();
203
+ const workers = buildWorkerRows(snapshots);
204
+ const senses = this.senseManager?.listSenseRows() ?? [];
205
+ const repoRoot = (0, identity_1.getRepoRoot)();
206
+ return {
207
+ overview: {
208
+ daemon: "running",
209
+ health: workers.every((worker) => worker.status === "running") ? "ok" : "warn",
210
+ socketPath: this.socketPath,
211
+ outlookUrl: `${this.outlookServer?.origin ?? "http://127.0.0.1:0"}/outlook`,
212
+ ...(0, runtime_metadata_1.getRuntimeMetadata)(),
213
+ workerCount: workers.length,
214
+ senseCount: senses.length,
215
+ entryPath: path.join(repoRoot, "dist", "heart", "daemon", "daemon-entry.js"),
216
+ mode: (0, runtime_mode_1.detectRuntimeMode)(repoRoot),
217
+ },
218
+ workers,
219
+ senses,
220
+ };
99
221
  }
100
222
  async start() {
101
223
  if (this.server)
@@ -106,23 +228,155 @@ class OuroDaemon {
106
228
  message: "starting daemon server",
107
229
  meta: { socketPath: this.socketPath },
108
230
  });
231
+ // Register update hooks and apply pending updates before starting agents
232
+ (0, update_hooks_1.registerUpdateHook)(bundle_meta_1.bundleMetaHook);
233
+ (0, update_hooks_1.registerUpdateHook)(agent_config_v2_1.agentConfigV2Hook);
234
+ const currentVersion = (0, bundle_manifest_1.getPackageVersion)();
235
+ await (0, update_hooks_1.applyPendingUpdates)(this.bundlesRoot, currentVersion);
236
+ // Start periodic update checker (polls npm registry every 30 minutes)
237
+ // Skip in dev mode — dev builds should not auto-update from npm
238
+ const bundlesRoot = this.bundlesRoot;
239
+ if (this.mode === "dev") {
240
+ (0, runtime_1.emitNervesEvent)({
241
+ component: "daemon",
242
+ event: "daemon.update_checker_skip",
243
+ message: "skipping update checker in dev mode",
244
+ meta: { reason: "dev mode" },
245
+ });
246
+ }
247
+ else {
248
+ const daemon = this;
249
+ (0, update_checker_1.startUpdateChecker)({
250
+ currentVersion,
251
+ deps: {
252
+ distTag: "alpha",
253
+ fetchRegistryJson: /* v8 ignore next -- integration: real HTTP fetch @preserve */ async () => {
254
+ const res = await fetch("https://registry.npmjs.org/@ouro.bot/cli");
255
+ return res.json();
256
+ },
257
+ },
258
+ onUpdate: /* v8 ignore start -- integration: real npm install + process spawn @preserve */ async (result) => {
259
+ if (!result.latestVersion)
260
+ return;
261
+ await (0, staged_restart_1.performStagedRestart)(result.latestVersion, {
262
+ execSync: (cmd) => (0, child_process_1.execSync)(cmd, { stdio: "inherit" }),
263
+ spawnSync: child_process_1.spawnSync,
264
+ resolveNewCodePath: (_version) => {
265
+ try {
266
+ const resolved = (0, child_process_1.execSync)(`node -e "console.log(require.resolve('@ouro.bot/cli/package.json'))"`, { encoding: "utf-8" }).trim();
267
+ return resolved ? path.dirname(resolved) : null;
268
+ }
269
+ catch {
270
+ return null;
271
+ }
272
+ },
273
+ gracefulShutdown: () => daemon.stop(),
274
+ nodePath: process.execPath,
275
+ bundlesRoot,
276
+ });
277
+ },
278
+ /* v8 ignore stop */
279
+ });
280
+ }
281
+ // Pre-initialize MCP connections so they're ready for the first command (non-blocking)
282
+ /* v8 ignore next -- catch callback: getSharedMcpManager logs errors internally @preserve */
283
+ (0, mcp_manager_1.getSharedMcpManager)().catch(() => { });
284
+ /* v8 ignore start -- orphan cleanup + pidfile: calls process management functions @preserve */
285
+ killOrphanProcesses();
286
+ /* v8 ignore stop */
109
287
  await this.processManager.startAutoStartAgents();
110
288
  await this.senseManager?.startAutoStartSenses();
289
+ // Write all managed PIDs to disk so the next daemon can clean up
290
+ /* v8 ignore start -- pidfile write: collects PIDs from process managers @preserve */
291
+ const agentPids = this.processManager.listAgentSnapshots().map((s) => s.pid).filter((p) => p !== null);
292
+ const sensePids = this.senseManager?.listManagedPids?.() ?? [];
293
+ writePidfile([...agentPids, ...sensePids]);
294
+ /* v8 ignore stop */
111
295
  this.scheduler.start?.();
112
296
  await this.scheduler.reconcile?.();
113
297
  await this.drainPendingBundleMessages();
298
+ await this.drainPendingSenseMessages();
299
+ /* v8 ignore start — Outlook server startup, tested via outlook-http.test.ts */
300
+ if (!this.outlookServer) {
301
+ try {
302
+ this.outlookServer = await (0, outlook_http_1.startOutlookHttpServer)({
303
+ host: "127.0.0.1",
304
+ port: outlook_types_1.OUTLOOK_DEFAULT_PORT,
305
+ bundlesRoot: this.bundlesRoot,
306
+ readMachineState: () => (0, outlook_read_1.readOutlookMachineState)({ bundlesRoot: this.bundlesRoot }),
307
+ readMachineView: ({ machine }) => {
308
+ const overview = this.buildStatusPayload().overview;
309
+ return (0, outlook_view_1.buildOutlookMachineView)({
310
+ machine,
311
+ daemon: {
312
+ status: overview.daemon,
313
+ health: overview.health,
314
+ mode: overview.mode,
315
+ socketPath: overview.socketPath,
316
+ outlookUrl: overview.outlookUrl,
317
+ entryPath: overview.entryPath,
318
+ workerCount: overview.workerCount,
319
+ senseCount: overview.senseCount,
320
+ },
321
+ });
322
+ },
323
+ readAgentState: (agentName) => (0, outlook_read_1.readOutlookAgentState)(agentName, { bundlesRoot: this.bundlesRoot }),
324
+ readAgentView: (agentName) => {
325
+ const agent = (0, outlook_read_1.readOutlookAgentState)(agentName, { bundlesRoot: this.bundlesRoot });
326
+ return (0, outlook_view_1.buildOutlookAgentView)({
327
+ agent,
328
+ viewer: { kind: "human" },
329
+ });
330
+ },
331
+ });
332
+ }
333
+ catch (error) {
334
+ (0, runtime_1.emitNervesEvent)({
335
+ level: "warn",
336
+ component: "daemon",
337
+ event: "daemon.outlook_start_failed",
338
+ message: `Outlook server failed to start: ${String(error)}`,
339
+ meta: { port: outlook_types_1.OUTLOOK_DEFAULT_PORT },
340
+ });
341
+ }
342
+ }
343
+ /* v8 ignore stop */
114
344
  if (fs.existsSync(this.socketPath)) {
115
345
  fs.unlinkSync(this.socketPath);
116
346
  }
117
347
  this.server = net.createServer((connection) => {
118
348
  let raw = "";
119
349
  let responded = false;
350
+ /* v8 ignore start — connection error handler requires real socket error @preserve */
351
+ connection.on("error", (err) => {
352
+ (0, runtime_1.emitNervesEvent)({
353
+ level: "warn",
354
+ component: "daemon",
355
+ event: "daemon.connection_error",
356
+ message: "socket connection error",
357
+ meta: { error: err.message, code: err.code ?? null },
358
+ });
359
+ });
360
+ /* v8 ignore stop */
120
361
  const flushResponse = async () => {
121
362
  if (responded)
122
363
  return;
123
364
  responded = true;
124
365
  const response = await this.handleRawPayload(raw);
125
- connection.end(response);
366
+ try {
367
+ connection.end(response);
368
+ /* v8 ignore start — EPIPE catch requires real socket disconnect @preserve */
369
+ }
370
+ catch (err) {
371
+ (0, runtime_1.emitNervesEvent)({
372
+ level: "warn",
373
+ component: "daemon",
374
+ event: "daemon.connection_end_error",
375
+ message: "failed to send response to client (EPIPE)",
376
+ meta: { error: err instanceof Error ? err.message : String(err) },
377
+ });
378
+ }
379
+ /* v8 ignore stop */
126
380
  };
127
381
  connection.on("data", (chunk) => {
128
382
  raw += chunk.toString("utf-8");
@@ -135,7 +389,22 @@ class OuroDaemon {
135
389
  const server = this.server;
136
390
  await new Promise((resolve, reject) => {
137
391
  server.once("error", reject);
138
- server.listen(this.socketPath, () => resolve());
392
+ server.listen(this.socketPath, () => {
393
+ // Replace the one-time error listener with a persistent one after successful listen
394
+ server.removeAllListeners("error");
395
+ /* v8 ignore start — server error after listen requires real socket race condition @preserve */
396
+ server.on("error", (err) => {
397
+ (0, runtime_1.emitNervesEvent)({
398
+ level: "error",
399
+ component: "daemon",
400
+ event: "daemon.server_error",
401
+ message: "daemon server error after listen",
402
+ meta: { error: err.message, code: err.code ?? null },
403
+ });
404
+ });
405
+ /* v8 ignore stop */
406
+ resolve();
407
+ });
139
408
  });
140
409
  }
141
410
  async drainPendingBundleMessages() {
@@ -186,6 +455,93 @@ class OuroDaemon {
186
455
  fs.writeFileSync(pendingPath, next, "utf-8");
187
456
  }
188
457
  }
458
+ /** Drains per-sense pending dirs for always-on senses across all agents. */
459
+ static ALWAYS_ON_SENSES = new Set((0, channel_1.getAlwaysOnSenseNames)());
460
+ async drainPendingSenseMessages() {
461
+ if (!fs.existsSync(this.bundlesRoot))
462
+ return;
463
+ let bundleDirs;
464
+ try {
465
+ bundleDirs = fs.readdirSync(this.bundlesRoot, { withFileTypes: true });
466
+ }
467
+ catch {
468
+ return;
469
+ }
470
+ for (const bundleDir of bundleDirs) {
471
+ if (!bundleDir.isDirectory() || !bundleDir.name.endsWith(".ouro"))
472
+ continue;
473
+ const agentName = bundleDir.name.replace(/\.ouro$/, "");
474
+ const pendingRoot = path.join(this.bundlesRoot, bundleDir.name, "state", "pending");
475
+ if (!fs.existsSync(pendingRoot))
476
+ continue;
477
+ let friendDirs;
478
+ try {
479
+ friendDirs = fs.readdirSync(pendingRoot, { withFileTypes: true });
480
+ }
481
+ catch {
482
+ continue;
483
+ }
484
+ for (const friendDir of friendDirs) {
485
+ if (!friendDir.isDirectory())
486
+ continue;
487
+ const friendPath = path.join(pendingRoot, friendDir.name);
488
+ let channelDirs;
489
+ try {
490
+ channelDirs = fs.readdirSync(friendPath, { withFileTypes: true });
491
+ }
492
+ catch {
493
+ continue;
494
+ }
495
+ for (const channelDir of channelDirs) {
496
+ if (!channelDir.isDirectory())
497
+ continue;
498
+ if (!OuroDaemon.ALWAYS_ON_SENSES.has(channelDir.name))
499
+ continue;
500
+ const channelPath = path.join(friendPath, channelDir.name);
501
+ let keyDirs;
502
+ try {
503
+ keyDirs = fs.readdirSync(channelPath, { withFileTypes: true });
504
+ }
505
+ catch {
506
+ continue;
507
+ }
508
+ for (const keyDir of keyDirs) {
509
+ if (!keyDir.isDirectory())
510
+ continue;
511
+ const leafDir = path.join(channelPath, keyDir.name);
512
+ const messages = (0, pending_1.drainPending)(leafDir);
513
+ for (const msg of messages) {
514
+ try {
515
+ await this.router.send({
516
+ from: msg.from,
517
+ to: agentName,
518
+ content: msg.content,
519
+ priority: "normal",
520
+ });
521
+ }
522
+ catch {
523
+ // Best-effort delivery — log and continue
524
+ }
525
+ }
526
+ if (messages.length > 0) {
527
+ (0, runtime_1.emitNervesEvent)({
528
+ component: "daemon",
529
+ event: "daemon.startup_sense_drain",
530
+ message: "drained pending sense messages on startup",
531
+ meta: {
532
+ agent: agentName,
533
+ channel: channelDir.name,
534
+ friendId: friendDir.name,
535
+ key: keyDir.name,
536
+ count: messages.length,
537
+ },
538
+ });
539
+ }
540
+ }
541
+ }
542
+ }
543
+ }
544
+ }
189
545
  async stop() {
190
546
  (0, runtime_1.emitNervesEvent)({
191
547
  component: "daemon",
@@ -193,6 +549,8 @@ class OuroDaemon {
193
549
  message: "stopping daemon server",
194
550
  meta: { socketPath: this.socketPath },
195
551
  });
552
+ (0, update_checker_1.stopUpdateChecker)();
553
+ (0, mcp_manager_1.shutdownSharedMcpManager)();
196
554
  this.scheduler.stop?.();
197
555
  await this.processManager.stopAll();
198
556
  await this.senseManager?.stopAll();
@@ -202,6 +560,10 @@ class OuroDaemon {
202
560
  });
203
561
  this.server = null;
204
562
  }
563
+ if (this.outlookServer) {
564
+ await this.outlookServer.stop();
565
+ this.outlookServer = null;
566
+ }
205
567
  if (fs.existsSync(this.socketPath)) {
206
568
  fs.unlinkSync(this.socketPath);
207
569
  }
@@ -226,6 +588,27 @@ class OuroDaemon {
226
588
  message: "handling daemon command",
227
589
  meta: { kind: command.kind },
228
590
  });
591
+ try {
592
+ return await this.handleCommandInner(command);
593
+ /* v8 ignore start — command error catch tested in daemon-command-error.test; instanceof branches defensive @preserve */
594
+ }
595
+ catch (error) {
596
+ (0, runtime_1.emitNervesEvent)({
597
+ level: "error",
598
+ component: "daemon",
599
+ event: "daemon.command_error",
600
+ message: "unexpected error handling daemon command",
601
+ meta: {
602
+ kind: command.kind,
603
+ error: error instanceof Error ? error.message : String(error),
604
+ stack: error instanceof Error ? error.stack ?? null : null,
605
+ },
606
+ });
607
+ throw error;
608
+ }
609
+ /* v8 ignore stop */
610
+ }
611
+ async handleCommandInner(command) {
229
612
  switch (command.kind) {
230
613
  case "daemon.start":
231
614
  await this.start();
@@ -234,20 +617,7 @@ class OuroDaemon {
234
617
  await this.stop();
235
618
  return { ok: true, message: "daemon stopped" };
236
619
  case "daemon.status": {
237
- const snapshots = this.processManager.listAgentSnapshots();
238
- const workers = buildWorkerRows(snapshots);
239
- const senses = this.senseManager?.listSenseRows() ?? [];
240
- const data = {
241
- overview: {
242
- daemon: "running",
243
- health: workers.every((worker) => worker.status === "running") ? "ok" : "warn",
244
- socketPath: this.socketPath,
245
- workerCount: workers.length,
246
- senseCount: senses.length,
247
- },
248
- workers,
249
- senses,
250
- };
620
+ const data = this.buildStatusPayload();
251
621
  return {
252
622
  ok: true,
253
623
  summary: formatStatusSummary(data),
@@ -264,7 +634,7 @@ class OuroDaemon {
264
634
  ok: true,
265
635
  summary: "logs: use `ouro logs` to tail daemon and agent output",
266
636
  message: "log streaming available via ouro logs",
267
- data: { logDir: "~/.agentstate/daemon/logs" },
637
+ data: { logDir: "~/AgentBundles/<agent>.ouro/state/daemon/logs" },
268
638
  };
269
639
  case "agent.start":
270
640
  await this.processManager.startAgent(command.agent);
@@ -275,6 +645,32 @@ class OuroDaemon {
275
645
  case "agent.restart":
276
646
  await this.processManager.restartAgent?.(command.agent);
277
647
  return { ok: true, message: `restarted ${command.agent}` };
648
+ case "agent.ask":
649
+ return (0, agent_service_1.handleAgentAsk)(command);
650
+ case "agent.status":
651
+ return (0, agent_service_1.handleAgentStatus)(command);
652
+ case "agent.catchup":
653
+ return (0, agent_service_1.handleAgentCatchup)(command);
654
+ case "agent.delegate":
655
+ return (0, agent_service_1.handleAgentDelegate)(command);
656
+ case "agent.getContext":
657
+ return (0, agent_service_1.handleAgentGetContext)(command);
658
+ case "agent.searchMemory":
659
+ return (0, agent_service_1.handleAgentSearchMemory)(command);
660
+ case "agent.getTask":
661
+ return (0, agent_service_1.handleAgentGetTask)(command);
662
+ case "agent.checkScope":
663
+ return (0, agent_service_1.handleAgentCheckScope)(command);
664
+ case "agent.requestDecision":
665
+ return (0, agent_service_1.handleAgentRequestDecision)(command);
666
+ case "agent.checkGuidance":
667
+ return (0, agent_service_1.handleAgentCheckGuidance)(command);
668
+ case "agent.reportProgress":
669
+ return (0, agent_service_1.handleAgentReportProgress)(command);
670
+ case "agent.reportBlocker":
671
+ return (0, agent_service_1.handleAgentReportBlocker)(command);
672
+ case "agent.reportComplete":
673
+ return (0, agent_service_1.handleAgentReportComplete)(command);
278
674
  case "cron.list": {
279
675
  const jobs = this.scheduler.listJobs();
280
676
  const summary = jobs.length === 0
@@ -306,6 +702,13 @@ class OuroDaemon {
306
702
  data: messages,
307
703
  };
308
704
  }
705
+ case "inner.wake":
706
+ await this.processManager.startAgent(command.agent);
707
+ this.processManager.sendToAgent?.(command.agent, { type: "message" });
708
+ return {
709
+ ok: true,
710
+ message: `woke inner dialog for ${command.agent}`,
711
+ };
309
712
  case "chat.connect":
310
713
  await this.processManager.startAgent(command.agent);
311
714
  return {
@@ -328,6 +731,35 @@ class OuroDaemon {
328
731
  data: receipt,
329
732
  };
330
733
  }
734
+ case "habit.poke": {
735
+ this.processManager.sendToAgent?.(command.agent, { type: "habit", habitName: command.habitName });
736
+ return {
737
+ ok: true,
738
+ message: `poked habit ${command.habitName} for ${command.agent}`,
739
+ };
740
+ }
741
+ case "mcp.list": {
742
+ const mcpManager = await (0, mcp_manager_1.getSharedMcpManager)();
743
+ if (!mcpManager) {
744
+ return { ok: true, data: [], message: "no MCP servers configured" };
745
+ }
746
+ return { ok: true, data: mcpManager.listAllTools() };
747
+ }
748
+ case "mcp.call": {
749
+ const mcpCallManager = await (0, mcp_manager_1.getSharedMcpManager)();
750
+ if (!mcpCallManager) {
751
+ return { ok: false, error: "no MCP servers configured" };
752
+ }
753
+ try {
754
+ const parsedArgs = command.args ? JSON.parse(command.args) : {};
755
+ const result = await mcpCallManager.callTool(command.server, command.tool, parsedArgs);
756
+ return { ok: true, data: result };
757
+ }
758
+ catch (error) {
759
+ /* v8 ignore next -- defensive: callTool errors are always Error instances @preserve */
760
+ return { ok: false, error: error instanceof Error ? error.message : String(error) };
761
+ }
762
+ }
331
763
  case "hatch.start":
332
764
  return {
333
765
  ok: true,