@ouro.bot/cli 0.1.0-alpha.36 → 0.1.0-alpha.361
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.
- package/README.md +194 -184
- package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/agent.json +3 -2
- package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/SOUL.md +1 -1
- package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/the-serpent.md +1 -1
- package/changelog.json +2155 -0
- package/dist/arc/attention-types.js +8 -0
- package/dist/arc/cares.js +140 -0
- package/dist/arc/episodes.js +117 -0
- package/dist/arc/intentions.js +133 -0
- package/dist/arc/json-store.js +117 -0
- package/dist/arc/obligations.js +237 -0
- package/dist/arc/packets.js +193 -0
- package/dist/arc/presence.js +185 -0
- package/dist/arc/task-lifecycle.js +65 -0
- package/dist/heart/active-work.js +832 -0
- package/dist/heart/agent-entry.js +37 -2
- package/dist/heart/attachments/image-normalize.js +194 -0
- package/dist/heart/attachments/materialize.js +97 -0
- package/dist/heart/attachments/originals.js +88 -0
- package/dist/heart/attachments/render.js +29 -0
- package/dist/heart/attachments/sources/adapter.js +2 -0
- package/dist/heart/attachments/sources/bluebubbles.js +156 -0
- package/dist/heart/attachments/sources/cli-local-file.js +78 -0
- package/dist/heart/attachments/sources/index.js +16 -0
- package/dist/heart/attachments/store.js +103 -0
- package/dist/heart/attachments/types.js +93 -0
- package/dist/heart/auth/auth-flow.js +463 -0
- package/dist/heart/bridges/manager.js +358 -0
- package/dist/heart/bridges/state-machine.js +135 -0
- package/dist/heart/bridges/store.js +123 -0
- package/dist/heart/bundle-state.js +168 -0
- package/dist/heart/commitments.js +111 -0
- package/dist/heart/config-registry.js +304 -0
- package/dist/heart/config.js +53 -21
- package/dist/heart/core.js +743 -252
- package/dist/heart/cross-chat-delivery.js +131 -0
- package/dist/heart/daemon/agent-config-check.js +561 -0
- package/dist/heart/daemon/agent-discovery.js +79 -3
- package/dist/heart/daemon/agent-service.js +360 -0
- package/dist/heart/daemon/agentic-repair.js +185 -0
- package/dist/heart/daemon/bluebubbles-health-diagnostics.js +122 -0
- package/dist/heart/daemon/cadence.js +70 -0
- package/dist/heart/daemon/cli-defaults.js +591 -0
- package/dist/heart/daemon/cli-exec.js +2649 -0
- package/dist/heart/daemon/cli-help.js +306 -0
- package/dist/heart/daemon/cli-parse.js +913 -0
- package/dist/heart/daemon/cli-render-doctor.js +57 -0
- package/dist/heart/daemon/cli-render.js +560 -0
- package/dist/heart/daemon/cli-types.js +8 -0
- package/dist/heart/daemon/daemon-cli.js +30 -1171
- package/dist/heart/daemon/daemon-entry.js +358 -3
- package/dist/heart/daemon/daemon-health.js +141 -0
- package/dist/heart/daemon/daemon-runtime-sync.js +157 -12
- package/dist/heart/daemon/daemon-tombstone.js +236 -0
- package/dist/heart/daemon/daemon.js +757 -58
- package/dist/heart/daemon/doctor-types.js +8 -0
- package/dist/heart/daemon/doctor.js +465 -0
- package/dist/heart/daemon/health-monitor.js +79 -1
- package/dist/heart/daemon/hooks/agent-config-v2.js +33 -0
- package/dist/heart/daemon/hooks/bundle-meta.js +115 -1
- package/dist/heart/daemon/http-health-probe.js +80 -0
- package/dist/heart/daemon/inner-status.js +89 -0
- package/dist/heart/daemon/interactive-repair.js +91 -0
- package/dist/heart/daemon/launchd.js +46 -9
- package/dist/heart/daemon/log-tailer.js +82 -12
- package/dist/heart/daemon/logs-prune.js +105 -0
- package/dist/heart/daemon/message-router.js +17 -8
- package/dist/heart/daemon/os-cron-deps.js +134 -0
- package/dist/heart/daemon/ouro-bot-entry.js +1 -1
- package/dist/heart/daemon/process-manager.js +201 -0
- package/dist/heart/daemon/provider-discovery.js +140 -0
- package/dist/heart/daemon/pulse.js +475 -0
- package/dist/heart/daemon/run-hooks.js +2 -0
- package/dist/heart/daemon/runtime-logging.js +67 -16
- package/dist/heart/daemon/runtime-metadata.js +101 -0
- package/dist/heart/daemon/runtime-mode.js +67 -0
- package/dist/heart/daemon/safe-mode.js +161 -0
- package/dist/heart/daemon/sense-manager.js +72 -3
- package/dist/heart/daemon/session-id-resolver.js +131 -0
- package/dist/heart/daemon/skill-management-installer.js +94 -0
- package/dist/heart/daemon/socket-client.js +307 -0
- package/dist/heart/daemon/stale-bundle-prune.js +96 -0
- package/dist/heart/daemon/startup-tui.js +237 -0
- package/dist/heart/daemon/task-scheduler.js +3 -25
- package/dist/heart/daemon/thoughts.js +510 -0
- package/dist/heart/daemon/up-progress.js +135 -0
- package/dist/heart/delegation.js +62 -0
- package/dist/heart/habits/habit-migration.js +181 -0
- package/dist/heart/habits/habit-parser.js +140 -0
- package/dist/heart/habits/habit-scheduler.js +371 -0
- package/dist/heart/{daemon → hatch}/hatch-flow.js +52 -120
- package/dist/heart/{daemon → hatch}/hatch-specialist.js +3 -3
- package/dist/heart/{daemon → hatch}/specialist-prompt.js +10 -7
- package/dist/heart/{daemon → hatch}/specialist-tools.js +56 -10
- package/dist/heart/identity.js +154 -59
- package/dist/heart/kept-notes.js +357 -0
- package/dist/heart/kicks.js +2 -20
- package/dist/heart/machine-identity.js +161 -0
- package/dist/heart/mcp/mcp-server.js +653 -0
- package/dist/heart/migrate-config.js +127 -0
- package/dist/heart/model-capabilities.js +59 -0
- package/dist/heart/outlook/outlook-http-hooks.js +64 -0
- package/dist/heart/outlook/outlook-http-response.js +7 -0
- package/dist/heart/outlook/outlook-http-routes.js +232 -0
- package/dist/heart/outlook/outlook-http-static.js +99 -0
- package/dist/heart/outlook/outlook-http-transport.js +116 -0
- package/dist/heart/outlook/outlook-http.js +99 -0
- package/dist/heart/outlook/outlook-read.js +28 -0
- package/dist/heart/outlook/outlook-types.js +27 -0
- package/dist/heart/outlook/outlook-view.js +195 -0
- package/dist/heart/outlook/readers/agent-machine.js +359 -0
- package/dist/heart/outlook/readers/continuity-readers.js +332 -0
- package/dist/heart/outlook/readers/runtime-readers.js +660 -0
- package/dist/heart/outlook/readers/sessions.js +232 -0
- package/dist/heart/outlook/readers/shared.js +111 -0
- package/dist/heart/progress-story.js +42 -0
- package/dist/heart/provider-attempt.js +133 -0
- package/dist/heart/provider-binding-resolver.js +240 -0
- package/dist/heart/provider-credential-pool.js +395 -0
- package/dist/heart/provider-failover.js +274 -0
- package/dist/heart/provider-models.js +81 -0
- package/dist/heart/provider-ping.js +227 -0
- package/dist/heart/provider-state.js +208 -0
- package/dist/heart/provider-visibility.js +183 -0
- package/dist/heart/providers/anthropic-token.js +163 -0
- package/dist/heart/providers/anthropic.js +177 -50
- package/dist/heart/providers/azure.js +102 -11
- package/dist/heart/providers/error-classification.js +63 -0
- package/dist/heart/providers/github-copilot.js +145 -0
- package/dist/heart/providers/minimax-vlm.js +189 -0
- package/dist/heart/providers/minimax.js +28 -6
- package/dist/heart/providers/openai-codex.js +38 -23
- package/dist/heart/session-activity.js +190 -0
- package/dist/heart/session-events.js +855 -0
- package/dist/heart/session-transcript.js +167 -0
- package/dist/heart/start-of-turn-packet.js +345 -0
- package/dist/heart/streaming.js +36 -27
- package/dist/heart/sync.js +332 -0
- package/dist/heart/target-resolution.js +127 -0
- package/dist/heart/tempo.js +93 -0
- package/dist/heart/temporal-view.js +41 -0
- package/dist/heart/tool-activity-callbacks.js +36 -0
- package/dist/heart/tool-description.js +135 -0
- package/dist/heart/tool-friction.js +55 -0
- package/dist/heart/tool-loop.js +200 -0
- package/dist/heart/turn-context.js +362 -0
- package/dist/heart/turn-coordinator.js +28 -0
- package/dist/heart/{daemon → versioning}/ouro-bot-global-installer.js +1 -1
- package/dist/heart/{daemon → versioning}/ouro-bot-wrapper.js +1 -1
- package/dist/heart/versioning/ouro-path-installer.js +296 -0
- package/dist/heart/versioning/ouro-version-manager.js +295 -0
- package/dist/heart/{daemon → versioning}/staged-restart.js +40 -8
- package/dist/heart/{daemon → versioning}/update-checker.js +12 -2
- package/dist/heart/{daemon → versioning}/update-hooks.js +63 -59
- package/dist/mind/bundle-manifest.js +7 -1
- package/dist/mind/context.js +141 -94
- package/dist/mind/diary-integrity.js +60 -0
- package/dist/mind/{memory.js → diary.js} +84 -96
- package/dist/mind/embedding-provider.js +60 -0
- package/dist/mind/file-state.js +179 -0
- package/dist/mind/first-impressions.js +14 -1
- package/dist/mind/friends/channel.js +56 -0
- package/dist/mind/friends/group-context.js +144 -0
- package/dist/mind/friends/resolver.js +38 -1
- package/dist/mind/friends/store-file.js +58 -3
- package/dist/mind/friends/trust-explanation.js +74 -0
- package/dist/mind/friends/types.js +9 -1
- package/dist/mind/journal-index.js +161 -0
- package/dist/mind/note-search.js +268 -0
- package/dist/mind/obligation-steering.js +221 -0
- package/dist/mind/pending.js +74 -7
- package/dist/mind/prompt.js +1013 -112
- package/dist/mind/provenance-trust.js +26 -0
- package/dist/mind/scrutiny.js +173 -0
- package/dist/mind/token-estimate.js +8 -12
- package/dist/nerves/cli-logging.js +7 -1
- package/dist/nerves/coverage/audit-rules.js +15 -6
- package/dist/nerves/coverage/audit.js +28 -2
- package/dist/nerves/coverage/cli.js +1 -1
- package/dist/nerves/coverage/file-completeness.js +83 -5
- package/dist/nerves/coverage/run-artifacts.js +1 -1
- package/dist/nerves/event-buffer.js +111 -0
- package/dist/nerves/index.js +224 -4
- package/dist/nerves/observation.js +20 -0
- package/dist/nerves/redact.js +79 -0
- package/dist/nerves/runtime.js +5 -1
- package/dist/outlook-ui/assets/index-LwChZTgL.css +1 -0
- package/dist/outlook-ui/assets/index-xTdv64BV.js +61 -0
- package/dist/outlook-ui/index.html +15 -0
- package/dist/repertoire/ado-client.js +15 -56
- package/dist/repertoire/ado-semantic.js +11 -10
- package/dist/repertoire/api-client.js +97 -0
- package/dist/repertoire/bitwarden-store.js +319 -0
- package/dist/repertoire/bundle-templates.js +72 -0
- package/dist/repertoire/bw-installer.js +79 -0
- package/dist/repertoire/coding/codex-jsonl.js +64 -0
- package/dist/repertoire/coding/context-pack.js +330 -0
- package/dist/repertoire/coding/feedback.js +197 -30
- package/dist/repertoire/coding/manager.js +158 -9
- package/dist/repertoire/coding/spawner.js +55 -9
- package/dist/repertoire/coding/tools.js +170 -7
- package/dist/repertoire/commerce-errors.js +109 -0
- package/dist/repertoire/commerce-self-test.js +156 -0
- package/dist/repertoire/credential-access.js +527 -0
- package/dist/repertoire/duffel-client.js +185 -0
- package/dist/repertoire/github-client.js +14 -55
- package/dist/repertoire/graph-client.js +11 -52
- package/dist/repertoire/guardrails.js +375 -0
- package/dist/repertoire/mcp-client.js +255 -0
- package/dist/repertoire/mcp-manager.js +305 -0
- package/dist/repertoire/mcp-tools.js +63 -0
- package/dist/repertoire/shell-sessions.js +133 -0
- package/dist/repertoire/skills.js +15 -24
- package/dist/repertoire/stripe-client.js +131 -0
- package/dist/repertoire/tasks/board.js +43 -5
- package/dist/repertoire/tasks/fix.js +182 -0
- package/dist/repertoire/tasks/index.js +28 -10
- package/dist/repertoire/tasks/lifecycle.js +2 -2
- package/dist/repertoire/tasks/parser.js +3 -2
- package/dist/repertoire/tasks/scanner.js +194 -37
- package/dist/repertoire/tasks/transitions.js +16 -79
- package/dist/repertoire/tool-results.js +29 -0
- package/dist/repertoire/tools-attachments.js +316 -0
- package/dist/repertoire/tools-base.js +45 -771
- package/dist/repertoire/tools-bluebubbles.js +1 -0
- package/dist/repertoire/tools-bridge.js +141 -0
- package/dist/repertoire/tools-bundle.js +984 -0
- package/dist/repertoire/tools-config.js +185 -0
- package/dist/repertoire/tools-continuity.js +248 -0
- package/dist/repertoire/tools-credential.js +182 -0
- package/dist/repertoire/tools-files.js +342 -0
- package/dist/repertoire/tools-flight.js +224 -0
- package/dist/repertoire/tools-flow.js +105 -0
- package/dist/repertoire/tools-github.js +1 -7
- package/dist/repertoire/tools-notes.js +376 -0
- package/dist/repertoire/tools-session.js +739 -0
- package/dist/repertoire/tools-shell.js +120 -0
- package/dist/repertoire/tools-stripe.js +180 -0
- package/dist/repertoire/tools-surface.js +243 -0
- package/dist/repertoire/tools-teams.js +12 -62
- package/dist/repertoire/tools-travel.js +125 -0
- package/dist/repertoire/tools-user-profile.js +144 -0
- package/dist/repertoire/tools-vault.js +110 -0
- package/dist/repertoire/tools.js +144 -138
- package/dist/repertoire/travel-api-client.js +360 -0
- package/dist/repertoire/user-profile.js +118 -0
- package/dist/repertoire/vault-setup.js +241 -0
- package/dist/scripts/claude-code-hook.js +41 -0
- package/dist/scripts/claude-code-stop-hook.js +47 -0
- package/dist/senses/attention-queue.js +116 -0
- package/dist/senses/bluebubbles/attachment-cache.js +53 -0
- package/dist/senses/bluebubbles/attachment-download.js +137 -0
- package/dist/senses/{bluebubbles-client.js → bluebubbles/client.js} +225 -9
- package/dist/senses/bluebubbles/entry.js +13 -0
- package/dist/senses/bluebubbles/inbound-log.js +113 -0
- package/dist/senses/bluebubbles/index.js +1620 -0
- package/dist/senses/{bluebubbles-media.js → bluebubbles/media.js} +121 -70
- package/dist/senses/{bluebubbles-model.js → bluebubbles/model.js} +43 -12
- package/dist/senses/{bluebubbles-mutation-log.js → bluebubbles/mutation-log.js} +46 -6
- package/dist/senses/bluebubbles/replay.js +129 -0
- package/dist/senses/bluebubbles/runtime-state.js +109 -0
- package/dist/senses/{bluebubbles-session-cleanup.js → bluebubbles/session-cleanup.js} +1 -1
- package/dist/senses/cli/bracketed-paste.js +82 -0
- package/dist/senses/cli/image-paste.js +287 -0
- package/dist/senses/cli/image-ref-navigation.js +75 -0
- package/dist/senses/cli/ink-app.js +156 -0
- package/dist/senses/cli/inline-diff.js +64 -0
- package/dist/senses/cli/input-keys.js +174 -0
- package/dist/senses/cli/kill-ring.js +86 -0
- package/dist/senses/cli/message-list.js +51 -0
- package/dist/senses/cli/ouro-tui.js +605 -0
- package/dist/senses/cli/spinner-imperative.js +135 -0
- package/dist/senses/cli/spinner.js +101 -0
- package/dist/senses/cli/status-line.js +60 -0
- package/dist/senses/cli/streaming-markdown.js +526 -0
- package/dist/senses/cli/tool-display.js +83 -0
- package/dist/senses/cli/tool-render.js +85 -0
- package/dist/senses/cli/tui-store.js +240 -0
- package/dist/senses/cli/virtual-list.js +35 -0
- package/dist/senses/cli-entry.js +1 -1
- package/dist/senses/cli-layout.js +187 -0
- package/dist/senses/cli.js +587 -249
- package/dist/senses/commands.js +66 -3
- package/dist/senses/continuity.js +94 -0
- package/dist/senses/habit-turn-message.js +108 -0
- package/dist/senses/inner-dialog-worker.js +112 -19
- package/dist/senses/inner-dialog.js +636 -86
- package/dist/senses/pipeline.js +603 -0
- package/dist/senses/proactive-content-guard.js +51 -0
- package/dist/senses/shared-turn.js +205 -0
- package/dist/senses/surface-tool.js +68 -0
- package/dist/senses/teams.js +693 -160
- package/dist/senses/trust-gate.js +112 -2
- package/package.json +29 -7
- package/skills/agent-commerce.md +106 -0
- package/skills/browser-navigation.md +110 -0
- package/skills/commerce-setup-guide.md +116 -0
- package/skills/commerce-setup.md +84 -0
- package/skills/configure-dev-tools.md +81 -0
- package/skills/travel-planning.md +138 -0
- package/dist/heart/daemon/ouro-path-installer.js +0 -178
- package/dist/heart/daemon/subagent-installer.js +0 -134
- package/dist/mind/associative-recall.js +0 -197
- package/dist/senses/bluebubbles-entry.js +0 -11
- package/dist/senses/bluebubbles.js +0 -558
- package/dist/senses/debug-activity.js +0 -127
- package/subagents/README.md +0 -73
- package/subagents/work-doer.md +0 -235
- package/subagents/work-merger.md +0 -618
- package/subagents/work-planner.md +0 -382
- /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/basilisk.md +0 -0
- /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/jafar.md +0 -0
- /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/jormungandr.md +0 -0
- /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/kaa.md +0 -0
- /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/medusa.md +0 -0
- /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/monty.md +0 -0
- /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/nagini.md +0 -0
- /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/ouroboros.md +0 -0
- /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/python.md +0 -0
- /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/quetzalcoatl.md +0 -0
- /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/sir-hiss.md +0 -0
- /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/the-snake.md +0 -0
- /package/dist/heart/{daemon → hatch}/hatch-animation.js +0 -0
- /package/dist/heart/{daemon → hatch}/specialist-orchestrator.js +0 -0
- /package/dist/heart/{daemon → versioning}/ouro-uti.js +0 -0
- /package/dist/heart/{daemon → versioning}/wrapper-publish-guard.js +0 -0
|
@@ -0,0 +1,2649 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* CLI command execution — the core runOuroCli function and its helpers.
|
|
4
|
+
*
|
|
5
|
+
* This is the junction box: it routes parsed commands to the appropriate
|
|
6
|
+
* handler (local execution, daemon socket, or interactive flow).
|
|
7
|
+
*/
|
|
8
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
9
|
+
if (k2 === undefined) k2 = k;
|
|
10
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
11
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
12
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
13
|
+
}
|
|
14
|
+
Object.defineProperty(o, k2, desc);
|
|
15
|
+
}) : (function(o, m, k, k2) {
|
|
16
|
+
if (k2 === undefined) k2 = k;
|
|
17
|
+
o[k2] = m[k];
|
|
18
|
+
}));
|
|
19
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
20
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
21
|
+
}) : function(o, v) {
|
|
22
|
+
o["default"] = v;
|
|
23
|
+
});
|
|
24
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
25
|
+
var ownKeys = function(o) {
|
|
26
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
27
|
+
var ar = [];
|
|
28
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
29
|
+
return ar;
|
|
30
|
+
};
|
|
31
|
+
return ownKeys(o);
|
|
32
|
+
};
|
|
33
|
+
return function (mod) {
|
|
34
|
+
if (mod && mod.__esModule) return mod;
|
|
35
|
+
var result = {};
|
|
36
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
37
|
+
__setModuleDefault(result, mod);
|
|
38
|
+
return result;
|
|
39
|
+
};
|
|
40
|
+
})();
|
|
41
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
+
exports.mergeStartupStability = mergeStartupStability;
|
|
43
|
+
exports.ensureDaemonRunning = ensureDaemonRunning;
|
|
44
|
+
exports.listGithubCopilotModels = listGithubCopilotModels;
|
|
45
|
+
exports.runOuroCli = runOuroCli;
|
|
46
|
+
const child_process_1 = require("child_process");
|
|
47
|
+
const crypto_1 = require("crypto");
|
|
48
|
+
const fs = __importStar(require("fs"));
|
|
49
|
+
const os = __importStar(require("os"));
|
|
50
|
+
const path = __importStar(require("path"));
|
|
51
|
+
const semver = __importStar(require("semver"));
|
|
52
|
+
const identity_1 = require("../identity");
|
|
53
|
+
const runtime_1 = require("../../nerves/runtime");
|
|
54
|
+
const store_file_1 = require("../../mind/friends/store-file");
|
|
55
|
+
const runtime_metadata_1 = require("./runtime-metadata");
|
|
56
|
+
const runtime_mode_1 = require("./runtime-mode");
|
|
57
|
+
const daemon_runtime_sync_1 = require("./daemon-runtime-sync");
|
|
58
|
+
const update_hooks_1 = require("../versioning/update-hooks");
|
|
59
|
+
const bundle_meta_1 = require("./hooks/bundle-meta");
|
|
60
|
+
const agent_config_v2_1 = require("./hooks/agent-config-v2");
|
|
61
|
+
const bundle_manifest_1 = require("../../mind/bundle-manifest");
|
|
62
|
+
const tasks_1 = require("../../repertoire/tasks");
|
|
63
|
+
const thoughts_1 = require("./thoughts");
|
|
64
|
+
const launchd_1 = require("./launchd");
|
|
65
|
+
const auth_flow_1 = require("../auth/auth-flow");
|
|
66
|
+
const provider_credential_pool_1 = require("../provider-credential-pool");
|
|
67
|
+
const provider_binding_resolver_1 = require("../provider-binding-resolver");
|
|
68
|
+
const provider_state_1 = require("../provider-state");
|
|
69
|
+
const machine_identity_1 = require("../machine-identity");
|
|
70
|
+
const provider_models_1 = require("../provider-models");
|
|
71
|
+
const ouro_version_manager_1 = require("../versioning/ouro-version-manager");
|
|
72
|
+
const cli_parse_1 = require("./cli-parse");
|
|
73
|
+
const cli_parse_2 = require("./cli-parse");
|
|
74
|
+
const cli_help_1 = require("./cli-help");
|
|
75
|
+
const cli_render_1 = require("./cli-render");
|
|
76
|
+
const cli_defaults_1 = require("./cli-defaults");
|
|
77
|
+
const agent_config_check_1 = require("./agent-config-check");
|
|
78
|
+
const doctor_1 = require("./doctor");
|
|
79
|
+
const cli_render_doctor_1 = require("./cli-render-doctor");
|
|
80
|
+
const interactive_repair_1 = require("./interactive-repair");
|
|
81
|
+
const agentic_repair_1 = require("./agentic-repair");
|
|
82
|
+
const startup_tui_1 = require("./startup-tui");
|
|
83
|
+
const stale_bundle_prune_1 = require("./stale-bundle-prune");
|
|
84
|
+
const up_progress_1 = require("./up-progress");
|
|
85
|
+
const provider_ping_1 = require("../provider-ping");
|
|
86
|
+
// ── ensureDaemonRunning ──
|
|
87
|
+
const DEFAULT_DAEMON_STARTUP_TIMEOUT_MS = 10_000;
|
|
88
|
+
const DEFAULT_DAEMON_STARTUP_POLL_INTERVAL_MS = 500;
|
|
89
|
+
const DEFAULT_DAEMON_STARTUP_STABILITY_WINDOW_MS = 1_500;
|
|
90
|
+
const DEFAULT_DAEMON_STARTUP_RETRY_LIMIT = 1;
|
|
91
|
+
const DEFAULT_DAEMON_STARTUP_LOG_LINES = 10;
|
|
92
|
+
async function checkAlreadyRunningAgentProviders(deps) {
|
|
93
|
+
const agents = await Promise.resolve(deps.listDiscoveredAgents ? deps.listDiscoveredAgents() : (0, cli_defaults_1.defaultListDiscoveredAgents)());
|
|
94
|
+
const bundlesRoot = deps.bundlesRoot ?? (0, identity_1.getAgentBundlesRoot)();
|
|
95
|
+
const secretsRoot = deps.secretsRoot ?? path.join(os.homedir(), ".agentsecrets");
|
|
96
|
+
const degraded = [];
|
|
97
|
+
for (const agent of agents) {
|
|
98
|
+
try {
|
|
99
|
+
const result = await (0, agent_config_check_1.checkAgentConfigWithProviderHealth)(agent, bundlesRoot, secretsRoot);
|
|
100
|
+
if (result.ok)
|
|
101
|
+
continue;
|
|
102
|
+
const errorReason = result.error ?? "agent provider health check failed";
|
|
103
|
+
const fixHint = result.fix ?? "";
|
|
104
|
+
degraded.push({ agent, errorReason, fixHint });
|
|
105
|
+
(0, runtime_1.emitNervesEvent)({
|
|
106
|
+
level: "error",
|
|
107
|
+
component: "daemon",
|
|
108
|
+
event: "daemon.agent_config_invalid",
|
|
109
|
+
message: errorReason,
|
|
110
|
+
meta: { agent, fix: fixHint, source: "already-running-provider-check" },
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
catch (error) {
|
|
114
|
+
const errorReason = error instanceof Error ? error.message : String(error);
|
|
115
|
+
degraded.push({
|
|
116
|
+
agent,
|
|
117
|
+
errorReason,
|
|
118
|
+
fixHint: "Run 'ouro doctor' for diagnostics, then retry 'ouro up'.",
|
|
119
|
+
});
|
|
120
|
+
(0, runtime_1.emitNervesEvent)({
|
|
121
|
+
level: "error",
|
|
122
|
+
component: "daemon",
|
|
123
|
+
event: "daemon.agent_config_invalid",
|
|
124
|
+
message: errorReason,
|
|
125
|
+
meta: { agent, fix: "ouro doctor", source: "already-running-provider-check" },
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return degraded;
|
|
130
|
+
}
|
|
131
|
+
async function checkProviderHealthBeforeChat(agentName, deps) {
|
|
132
|
+
const bundlesRoot = deps.bundlesRoot ?? (0, identity_1.getAgentBundlesRoot)();
|
|
133
|
+
const secretsRoot = deps.secretsRoot ?? path.join(os.homedir(), ".agentsecrets");
|
|
134
|
+
const result = await (0, agent_config_check_1.checkAgentConfigWithProviderHealth)(agentName, bundlesRoot, secretsRoot);
|
|
135
|
+
if (!result.ok) {
|
|
136
|
+
const output = `${result.error}\n${result.fix ? ` fix: ${result.fix}` : ""}`;
|
|
137
|
+
deps.writeStdout(output);
|
|
138
|
+
return { ok: false, output };
|
|
139
|
+
}
|
|
140
|
+
return { ok: true };
|
|
141
|
+
}
|
|
142
|
+
function mergeStartupStability(stability, extraDegraded) {
|
|
143
|
+
if (extraDegraded.length === 0)
|
|
144
|
+
return stability;
|
|
145
|
+
const degradedByAgent = new Map();
|
|
146
|
+
for (const entry of stability?.degraded ?? [])
|
|
147
|
+
degradedByAgent.set(entry.agent, entry);
|
|
148
|
+
for (const entry of extraDegraded)
|
|
149
|
+
degradedByAgent.set(entry.agent, entry);
|
|
150
|
+
const degraded = [...degradedByAgent.values()];
|
|
151
|
+
const stable = [];
|
|
152
|
+
for (const agent of stability?.stable ?? []) {
|
|
153
|
+
if (!degradedByAgent.has(agent))
|
|
154
|
+
stable.push(agent);
|
|
155
|
+
}
|
|
156
|
+
return { stable, degraded };
|
|
157
|
+
}
|
|
158
|
+
async function ensureDaemonRunning(deps) {
|
|
159
|
+
const readLatestDaemonStartupEvent = () => {
|
|
160
|
+
try {
|
|
161
|
+
// The daemon writes structured events to daemon.ndjson in the first
|
|
162
|
+
// agent bundle's state/daemon/logs/ directory. Read the last line to
|
|
163
|
+
// surface what it's currently doing (e.g., "starting auto-start agents").
|
|
164
|
+
const bundlesRoot = (0, identity_1.getAgentBundlesRoot)();
|
|
165
|
+
if (!fs.existsSync(bundlesRoot))
|
|
166
|
+
return null;
|
|
167
|
+
const agents = fs.readdirSync(bundlesRoot).filter((d) => d.endsWith(".ouro"));
|
|
168
|
+
for (const agent of agents) {
|
|
169
|
+
const logPath = path.join(bundlesRoot, agent, "state", "daemon", "logs", "daemon.ndjson");
|
|
170
|
+
if (!fs.existsSync(logPath))
|
|
171
|
+
continue;
|
|
172
|
+
const stat = fs.statSync(logPath);
|
|
173
|
+
if (stat.size === 0)
|
|
174
|
+
continue;
|
|
175
|
+
// Only read logs from the last 30 seconds (daemon just started)
|
|
176
|
+
const mtime = stat.mtimeMs;
|
|
177
|
+
if (Date.now() - mtime > 30_000)
|
|
178
|
+
continue;
|
|
179
|
+
const buf = Buffer.alloc(4096);
|
|
180
|
+
const fd = fs.openSync(logPath, "r");
|
|
181
|
+
let bytesRead = 0;
|
|
182
|
+
try {
|
|
183
|
+
const readFrom = Math.max(0, stat.size - 4096);
|
|
184
|
+
bytesRead = fs.readSync(fd, buf, 0, 4096, readFrom);
|
|
185
|
+
}
|
|
186
|
+
finally {
|
|
187
|
+
fs.closeSync(fd);
|
|
188
|
+
}
|
|
189
|
+
const lines = buf.subarray(0, bytesRead).toString("utf-8").trim().split("\n").filter(Boolean);
|
|
190
|
+
const last = lines[lines.length - 1];
|
|
191
|
+
if (!last)
|
|
192
|
+
continue;
|
|
193
|
+
const parsed = JSON.parse(last);
|
|
194
|
+
return parsed.message ?? null;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
catch {
|
|
198
|
+
// Best effort only.
|
|
199
|
+
}
|
|
200
|
+
return null;
|
|
201
|
+
};
|
|
202
|
+
const alive = await deps.checkSocketAlive(deps.socketPath);
|
|
203
|
+
if (alive) {
|
|
204
|
+
const localRuntime = (0, runtime_metadata_1.getRuntimeMetadata)();
|
|
205
|
+
let runningRuntimePromise = null;
|
|
206
|
+
const fetchRunningRuntimeMetadata = async () => {
|
|
207
|
+
runningRuntimePromise ??= (async () => {
|
|
208
|
+
const status = await deps.sendCommand(deps.socketPath, { kind: "daemon.status" });
|
|
209
|
+
const payload = (0, cli_render_1.parseStatusPayload)(status.data);
|
|
210
|
+
return {
|
|
211
|
+
version: payload?.overview.version ?? "unknown",
|
|
212
|
+
lastUpdated: payload?.overview.lastUpdated ?? "unknown",
|
|
213
|
+
repoRoot: payload?.overview.repoRoot ?? "unknown",
|
|
214
|
+
configFingerprint: payload?.overview.configFingerprint ?? "unknown",
|
|
215
|
+
};
|
|
216
|
+
})();
|
|
217
|
+
return runningRuntimePromise;
|
|
218
|
+
};
|
|
219
|
+
const runtimeResult = await (0, daemon_runtime_sync_1.ensureCurrentDaemonRuntime)({
|
|
220
|
+
socketPath: deps.socketPath,
|
|
221
|
+
localVersion: localRuntime.version,
|
|
222
|
+
localLastUpdated: localRuntime.lastUpdated,
|
|
223
|
+
localRepoRoot: localRuntime.repoRoot,
|
|
224
|
+
localConfigFingerprint: localRuntime.configFingerprint,
|
|
225
|
+
fetchRunningVersion: async () => (await fetchRunningRuntimeMetadata()).version,
|
|
226
|
+
fetchRunningRuntimeMetadata,
|
|
227
|
+
stopDaemon: async () => {
|
|
228
|
+
await deps.sendCommand(deps.socketPath, { kind: "daemon.stop" });
|
|
229
|
+
},
|
|
230
|
+
cleanupStaleSocket: deps.cleanupStaleSocket,
|
|
231
|
+
startDaemonProcess: deps.startDaemonProcess,
|
|
232
|
+
checkSocketAlive: deps.checkSocketAlive,
|
|
233
|
+
});
|
|
234
|
+
if (!runtimeResult.verifyStartupStatus) {
|
|
235
|
+
return runtimeResult;
|
|
236
|
+
}
|
|
237
|
+
const stability = await (0, startup_tui_1.pollDaemonStartup)({
|
|
238
|
+
sendCommand: deps.sendCommand,
|
|
239
|
+
socketPath: deps.socketPath,
|
|
240
|
+
daemonPid: runtimeResult.startedPid ?? null,
|
|
241
|
+
/* v8 ignore next -- thin wrapper: raw process.stdout.write for ANSI cursor control @preserve */
|
|
242
|
+
writeRaw: (text) => process.stdout.write(text),
|
|
243
|
+
/* v8 ignore next -- thin wrapper: real stdout TTY detection injected for captured-output safety @preserve */
|
|
244
|
+
isTTY: process.stdout.isTTY === true,
|
|
245
|
+
/* v8 ignore next -- thin wrapper: real Date.now() injected for testability @preserve */
|
|
246
|
+
now: () => Date.now(),
|
|
247
|
+
/* v8 ignore next -- thin wrapper: real setTimeout injected for testability @preserve */
|
|
248
|
+
sleep: (ms) => new Promise((resolve) => setTimeout(resolve, ms)),
|
|
249
|
+
/* v8 ignore start -- daemon log tail + pid check: reads real filesystem, tested via deployment @preserve */
|
|
250
|
+
readLatestDaemonEvent: readLatestDaemonStartupEvent,
|
|
251
|
+
/* v8 ignore stop */
|
|
252
|
+
});
|
|
253
|
+
return {
|
|
254
|
+
alreadyRunning: runtimeResult.alreadyRunning,
|
|
255
|
+
message: runtimeResult.message,
|
|
256
|
+
stability,
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
const retryLimit = deps.startupRetryLimit ?? DEFAULT_DAEMON_STARTUP_RETRY_LIMIT;
|
|
260
|
+
let lastFailure = {
|
|
261
|
+
reason: "daemon failed before the startup monitor recorded a failure",
|
|
262
|
+
retryable: false,
|
|
263
|
+
};
|
|
264
|
+
let lastPid = null;
|
|
265
|
+
for (let attempt = 0; attempt <= retryLimit; attempt += 1) {
|
|
266
|
+
deps.reportDaemonStartupPhase?.("starting daemon...");
|
|
267
|
+
deps.reportDaemonStartupPhase?.("waiting for daemon socket...");
|
|
268
|
+
deps.cleanupStaleSocket(deps.socketPath);
|
|
269
|
+
const bootStartedAtMs = (deps.now ?? Date.now)();
|
|
270
|
+
const started = await deps.startDaemonProcess(deps.socketPath);
|
|
271
|
+
lastPid = started.pid ?? null;
|
|
272
|
+
const startupFailure = await waitForDaemonStartup(deps, {
|
|
273
|
+
bootStartedAtMs,
|
|
274
|
+
pid: lastPid,
|
|
275
|
+
});
|
|
276
|
+
if (!startupFailure) {
|
|
277
|
+
const stability = await (0, startup_tui_1.pollDaemonStartup)({
|
|
278
|
+
sendCommand: deps.sendCommand,
|
|
279
|
+
socketPath: deps.socketPath,
|
|
280
|
+
daemonPid: lastPid,
|
|
281
|
+
/* v8 ignore next -- thin wrapper: raw process.stdout.write for ANSI cursor control @preserve */
|
|
282
|
+
writeRaw: (text) => process.stdout.write(text),
|
|
283
|
+
/* v8 ignore next -- thin wrapper: real stdout TTY detection injected for captured-output safety @preserve */
|
|
284
|
+
isTTY: process.stdout.isTTY === true,
|
|
285
|
+
/* v8 ignore next -- thin wrapper: real Date.now() injected for testability @preserve */
|
|
286
|
+
now: () => Date.now(),
|
|
287
|
+
/* v8 ignore next -- thin wrapper: real setTimeout injected for testability @preserve */
|
|
288
|
+
sleep: (ms) => new Promise((resolve) => setTimeout(resolve, ms)),
|
|
289
|
+
/* v8 ignore start -- daemon log tail + pid check: reads real filesystem, tested via deployment @preserve */
|
|
290
|
+
readLatestDaemonEvent: readLatestDaemonStartupEvent,
|
|
291
|
+
/* v8 ignore stop */
|
|
292
|
+
});
|
|
293
|
+
return {
|
|
294
|
+
alreadyRunning: false,
|
|
295
|
+
message: `daemon started (pid ${lastPid ?? "unknown"})`,
|
|
296
|
+
stability,
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
lastFailure = startupFailure;
|
|
300
|
+
if (!startupFailure.retryable || attempt >= retryLimit) {
|
|
301
|
+
break;
|
|
302
|
+
}
|
|
303
|
+
deps.reportDaemonStartupPhase?.("daemon startup lost stability; cleaning up and retrying once...");
|
|
304
|
+
}
|
|
305
|
+
return {
|
|
306
|
+
alreadyRunning: false,
|
|
307
|
+
message: formatDaemonStartupFailureMessage(lastPid, lastFailure, deps),
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
function hasStartupHealthMonitor(deps) {
|
|
311
|
+
return !!deps.healthFilePath && !!deps.readHealthState && !!deps.readHealthUpdatedAt;
|
|
312
|
+
}
|
|
313
|
+
function hasFreshCurrentBootHealthSignal(deps, bootStartedAtMs, pid) {
|
|
314
|
+
const healthState = deps.readHealthState(deps.healthFilePath);
|
|
315
|
+
if (!healthState)
|
|
316
|
+
return false;
|
|
317
|
+
const healthUpdatedAt = deps.readHealthUpdatedAt(deps.healthFilePath);
|
|
318
|
+
if (healthUpdatedAt === null || healthUpdatedAt < bootStartedAtMs) {
|
|
319
|
+
return false;
|
|
320
|
+
}
|
|
321
|
+
const healthStartedAtMs = Date.parse(healthState.startedAt);
|
|
322
|
+
if (!Number.isFinite(healthStartedAtMs) || healthStartedAtMs < bootStartedAtMs) {
|
|
323
|
+
return false;
|
|
324
|
+
}
|
|
325
|
+
if (pid !== null && healthState.pid !== pid) {
|
|
326
|
+
return false;
|
|
327
|
+
}
|
|
328
|
+
return true;
|
|
329
|
+
}
|
|
330
|
+
function formatDaemonStartupFailureMessage(pid, failure, deps) {
|
|
331
|
+
const lines = [
|
|
332
|
+
`daemon spawned (pid ${pid ?? "unknown"}) but failed to stabilize: ${failure.reason}`,
|
|
333
|
+
];
|
|
334
|
+
const recentLogLines = deps.readRecentDaemonLogLines?.(DEFAULT_DAEMON_STARTUP_LOG_LINES) ?? [];
|
|
335
|
+
if (recentLogLines.length > 0) {
|
|
336
|
+
lines.push("recent daemon logs:");
|
|
337
|
+
lines.push(...recentLogLines.map((line) => ` ${line}`));
|
|
338
|
+
}
|
|
339
|
+
lines.push("fix hint for daemon: check daemon logs or run `ouro doctor`");
|
|
340
|
+
return lines.join("\n");
|
|
341
|
+
}
|
|
342
|
+
async function waitForDaemonStartup(deps, options) {
|
|
343
|
+
const now = deps.now ?? Date.now;
|
|
344
|
+
const sleep = deps.sleep ?? defaultSleep;
|
|
345
|
+
const timeoutMs = deps.startupTimeoutMs ?? DEFAULT_DAEMON_STARTUP_TIMEOUT_MS;
|
|
346
|
+
const pollIntervalMs = deps.startupPollIntervalMs ?? DEFAULT_DAEMON_STARTUP_POLL_INTERVAL_MS;
|
|
347
|
+
const stabilityWindowMs = deps.startupStabilityWindowMs ?? DEFAULT_DAEMON_STARTUP_STABILITY_WINDOW_MS;
|
|
348
|
+
const deadline = options.bootStartedAtMs + timeoutMs;
|
|
349
|
+
const useHealthMonitor = hasStartupHealthMonitor(deps);
|
|
350
|
+
let stableSinceMs = null;
|
|
351
|
+
let sawSocket = false;
|
|
352
|
+
if (!useHealthMonitor) {
|
|
353
|
+
const verified = await verifyDaemonAlive(deps.checkSocketAlive, deps.socketPath, timeoutMs, pollIntervalMs, sleep, now);
|
|
354
|
+
return verified
|
|
355
|
+
? null
|
|
356
|
+
: {
|
|
357
|
+
reason: `daemon failed to respond within ${Math.ceil(timeoutMs / 1000)}s`,
|
|
358
|
+
retryable: false,
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
while (now() < deadline) {
|
|
362
|
+
await sleep(pollIntervalMs);
|
|
363
|
+
const aliveNow = await deps.checkSocketAlive(deps.socketPath);
|
|
364
|
+
if (!aliveNow) {
|
|
365
|
+
if (sawSocket) {
|
|
366
|
+
return {
|
|
367
|
+
reason: "daemon socket disappeared during startup",
|
|
368
|
+
retryable: true,
|
|
369
|
+
};
|
|
370
|
+
}
|
|
371
|
+
continue;
|
|
372
|
+
}
|
|
373
|
+
if (!sawSocket) {
|
|
374
|
+
sawSocket = true;
|
|
375
|
+
stableSinceMs = now();
|
|
376
|
+
deps.reportDaemonStartupPhase?.("verifying daemon health...");
|
|
377
|
+
}
|
|
378
|
+
if (!hasFreshCurrentBootHealthSignal(deps, options.bootStartedAtMs, options.pid)) {
|
|
379
|
+
continue;
|
|
380
|
+
}
|
|
381
|
+
if (stableSinceMs !== null && now() - stableSinceMs >= stabilityWindowMs) {
|
|
382
|
+
return null;
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
return {
|
|
386
|
+
reason: sawSocket
|
|
387
|
+
? "daemon did not publish fresh health for the current boot attempt"
|
|
388
|
+
: `daemon failed to respond within ${Math.ceil(timeoutMs / 1000)}s`,
|
|
389
|
+
retryable: sawSocket,
|
|
390
|
+
};
|
|
391
|
+
}
|
|
392
|
+
async function verifyDaemonAlive(checkSocketAlive, socketPath, maxWaitMs = 10_000, pollIntervalMs = 500, sleep = defaultSleep, now = Date.now) {
|
|
393
|
+
const deadline = now() + maxWaitMs;
|
|
394
|
+
while (now() < deadline) {
|
|
395
|
+
await sleep(pollIntervalMs);
|
|
396
|
+
if (await checkSocketAlive(socketPath))
|
|
397
|
+
return true;
|
|
398
|
+
}
|
|
399
|
+
return false;
|
|
400
|
+
}
|
|
401
|
+
function defaultSleep(ms) {
|
|
402
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
403
|
+
}
|
|
404
|
+
// ── GitHub Copilot model helpers ──
|
|
405
|
+
async function listGithubCopilotModels(baseUrl, token, fetchImpl = fetch) {
|
|
406
|
+
const url = `${baseUrl.replace(/\/+$/, "")}/models`;
|
|
407
|
+
const response = await fetchImpl(url, {
|
|
408
|
+
headers: { Authorization: `Bearer ${token}` },
|
|
409
|
+
});
|
|
410
|
+
if (!response.ok) {
|
|
411
|
+
throw new Error(`model listing failed (HTTP ${response.status})`);
|
|
412
|
+
}
|
|
413
|
+
const body = await response.json();
|
|
414
|
+
/* v8 ignore start -- response shape handling: tested via config-models.test.ts @preserve */
|
|
415
|
+
const items = Array.isArray(body) ? body : (body?.data ?? []);
|
|
416
|
+
return items.map((item) => {
|
|
417
|
+
const rec = item;
|
|
418
|
+
const capabilities = Array.isArray(rec.capabilities)
|
|
419
|
+
? rec.capabilities.filter((c) => typeof c === "string")
|
|
420
|
+
: undefined;
|
|
421
|
+
return {
|
|
422
|
+
id: String(rec.id ?? rec.name ?? ""),
|
|
423
|
+
name: String(rec.name ?? rec.id ?? ""),
|
|
424
|
+
...(capabilities ? { capabilities } : {}),
|
|
425
|
+
};
|
|
426
|
+
});
|
|
427
|
+
/* v8 ignore stop */
|
|
428
|
+
}
|
|
429
|
+
// ── Provider credential verification ──
|
|
430
|
+
/* v8 ignore start -- verifyProviderCredentials: delegates to pingProvider @preserve */
|
|
431
|
+
async function verifyProviderCredentials(provider, providers) {
|
|
432
|
+
const config = providers[provider];
|
|
433
|
+
if (!config)
|
|
434
|
+
return "not configured";
|
|
435
|
+
try {
|
|
436
|
+
const { pingProvider } = await Promise.resolve().then(() => __importStar(require("../../heart/provider-ping")));
|
|
437
|
+
const result = await pingProvider(provider, config);
|
|
438
|
+
return result.ok ? "ok" : `failed (${result.message})`;
|
|
439
|
+
}
|
|
440
|
+
catch (error) {
|
|
441
|
+
return `failed (${error instanceof Error ? error.message : String(error)})`;
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
/* v8 ignore stop */
|
|
445
|
+
// ── toDaemonCommand ──
|
|
446
|
+
function toDaemonCommand(command) {
|
|
447
|
+
return command;
|
|
448
|
+
}
|
|
449
|
+
// ── Hatch input resolution ──
|
|
450
|
+
async function resolveHatchInput(command, deps) {
|
|
451
|
+
const prompt = deps.promptInput;
|
|
452
|
+
const agentName = command.agentName ?? (prompt ? await prompt("Hatchling name: ") : "");
|
|
453
|
+
const humanName = command.humanName ?? (prompt ? await prompt("Your name: ") : os.userInfo().username);
|
|
454
|
+
const providerRaw = command.provider ?? (prompt ? await prompt("Provider (azure|anthropic|minimax|openai-codex|github-copilot): ") : "");
|
|
455
|
+
if (!agentName || !humanName || !(0, cli_parse_2.isAgentProvider)(providerRaw)) {
|
|
456
|
+
throw new Error(`Usage\n${(0, cli_parse_2.usage)()}`);
|
|
457
|
+
}
|
|
458
|
+
const credentials = await (0, auth_flow_1.resolveHatchCredentials)({
|
|
459
|
+
agentName,
|
|
460
|
+
provider: providerRaw,
|
|
461
|
+
credentials: command.credentials,
|
|
462
|
+
promptInput: prompt,
|
|
463
|
+
runAuthFlow: deps.runAuthFlow,
|
|
464
|
+
});
|
|
465
|
+
return {
|
|
466
|
+
agentName,
|
|
467
|
+
humanName,
|
|
468
|
+
provider: providerRaw,
|
|
469
|
+
credentials,
|
|
470
|
+
migrationPath: command.migrationPath,
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
// ── Provider state CLI helpers ──
|
|
474
|
+
function providerCliHomeDir(deps) {
|
|
475
|
+
return (0, provider_credential_pool_1.providerCredentialHomeDirFromSecretsRoot)(deps.secretsRoot);
|
|
476
|
+
}
|
|
477
|
+
function providerCliAgentRoot(command, deps) {
|
|
478
|
+
return path.join(deps.bundlesRoot ?? (0, identity_1.getAgentBundlesRoot)(), `${command.agent}.ouro`);
|
|
479
|
+
}
|
|
480
|
+
function providerCliNow(deps) {
|
|
481
|
+
return new Date((deps.now ?? Date.now)());
|
|
482
|
+
}
|
|
483
|
+
function readOrBootstrapProviderState(agentName, deps) {
|
|
484
|
+
const agentRoot = providerCliAgentRoot({ agent: agentName }, deps);
|
|
485
|
+
const readResult = (0, provider_state_1.readProviderState)(agentRoot);
|
|
486
|
+
if (readResult.ok)
|
|
487
|
+
return { agentRoot, state: readResult.state };
|
|
488
|
+
if (readResult.reason === "invalid") {
|
|
489
|
+
throw new Error(`provider state for ${agentName} is invalid at ${readResult.statePath}: ${readResult.error}`);
|
|
490
|
+
}
|
|
491
|
+
const { config } = (0, auth_flow_1.readAgentConfigForAgent)(agentName, deps.bundlesRoot);
|
|
492
|
+
const homeDir = providerCliHomeDir(deps);
|
|
493
|
+
const machine = (0, machine_identity_1.loadOrCreateMachineIdentity)({
|
|
494
|
+
homeDir,
|
|
495
|
+
now: () => providerCliNow(deps),
|
|
496
|
+
});
|
|
497
|
+
const state = (0, provider_state_1.bootstrapProviderStateFromAgentConfig)({
|
|
498
|
+
machineId: machine.machineId,
|
|
499
|
+
now: providerCliNow(deps),
|
|
500
|
+
agentConfig: {
|
|
501
|
+
humanFacing: {
|
|
502
|
+
provider: config.humanFacing.provider,
|
|
503
|
+
model: config.humanFacing.model || (0, provider_models_1.getDefaultModelForProvider)(config.humanFacing.provider),
|
|
504
|
+
},
|
|
505
|
+
agentFacing: {
|
|
506
|
+
provider: config.agentFacing.provider,
|
|
507
|
+
model: config.agentFacing.model || (0, provider_models_1.getDefaultModelForProvider)(config.agentFacing.provider),
|
|
508
|
+
},
|
|
509
|
+
},
|
|
510
|
+
});
|
|
511
|
+
(0, provider_state_1.writeProviderState)(agentRoot, state);
|
|
512
|
+
(0, runtime_1.emitNervesEvent)({
|
|
513
|
+
component: "daemon",
|
|
514
|
+
event: "daemon.provider_state_bootstrapped",
|
|
515
|
+
message: "bootstrapped local provider state from agent config",
|
|
516
|
+
meta: { agent: agentName, agentRoot },
|
|
517
|
+
});
|
|
518
|
+
return { agentRoot, state };
|
|
519
|
+
}
|
|
520
|
+
function credentialPingConfig(record) {
|
|
521
|
+
return {
|
|
522
|
+
...record.credentials,
|
|
523
|
+
...record.config,
|
|
524
|
+
};
|
|
525
|
+
}
|
|
526
|
+
function pingAttemptCount(result) {
|
|
527
|
+
if (typeof result.attempts === "number")
|
|
528
|
+
return result.attempts;
|
|
529
|
+
if (Array.isArray(result.attempts))
|
|
530
|
+
return result.attempts.length;
|
|
531
|
+
return undefined;
|
|
532
|
+
}
|
|
533
|
+
function providerCliLegacyRecord(agent, provider, deps) {
|
|
534
|
+
try {
|
|
535
|
+
const legacyCandidates = (0, provider_credential_pool_1.readLegacyAgentProviderCredentials)({
|
|
536
|
+
homeDir: providerCliHomeDir(deps),
|
|
537
|
+
agentName: agent,
|
|
538
|
+
});
|
|
539
|
+
const candidate = legacyCandidates.find((entry) => entry.provider === provider);
|
|
540
|
+
if (candidate)
|
|
541
|
+
return { credentials: candidate.credentials, config: candidate.config };
|
|
542
|
+
}
|
|
543
|
+
catch {
|
|
544
|
+
// Fall through to the injected/secretsRoot-aware legacy reader below.
|
|
545
|
+
}
|
|
546
|
+
try {
|
|
547
|
+
const { secrets } = (0, auth_flow_1.loadAgentSecrets)(agent, { secretsRoot: deps.secretsRoot });
|
|
548
|
+
const providerSecrets = secrets.providers[provider];
|
|
549
|
+
const split = (0, provider_credential_pool_1.splitProviderCredentialFields)(provider, providerSecrets);
|
|
550
|
+
if (Object.keys(split.credentials).length === 0 && Object.keys(split.config).length === 0)
|
|
551
|
+
return null;
|
|
552
|
+
return split;
|
|
553
|
+
}
|
|
554
|
+
catch {
|
|
555
|
+
return null;
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
function readProviderCredentialRecord(agent, provider, deps) {
|
|
559
|
+
const homeDir = providerCliHomeDir(deps);
|
|
560
|
+
const poolResult = (0, provider_credential_pool_1.readProviderCredentialPool)(homeDir);
|
|
561
|
+
if (poolResult.ok) {
|
|
562
|
+
const existing = poolResult.pool.providers[provider];
|
|
563
|
+
if (existing)
|
|
564
|
+
return { ok: true, record: existing };
|
|
565
|
+
}
|
|
566
|
+
else if (poolResult.reason === "invalid") {
|
|
567
|
+
return { ok: false, reason: "invalid", poolPath: poolResult.poolPath, error: poolResult.error };
|
|
568
|
+
}
|
|
569
|
+
const legacy = providerCliLegacyRecord(agent, provider, deps);
|
|
570
|
+
if (legacy) {
|
|
571
|
+
const record = (0, provider_credential_pool_1.upsertProviderCredential)({
|
|
572
|
+
homeDir,
|
|
573
|
+
provider,
|
|
574
|
+
credentials: legacy.credentials,
|
|
575
|
+
config: legacy.config,
|
|
576
|
+
provenance: {
|
|
577
|
+
source: "legacy-agent-secrets",
|
|
578
|
+
contributedByAgent: agent,
|
|
579
|
+
},
|
|
580
|
+
now: providerCliNow(deps),
|
|
581
|
+
});
|
|
582
|
+
return { ok: true, record };
|
|
583
|
+
}
|
|
584
|
+
return {
|
|
585
|
+
ok: false,
|
|
586
|
+
reason: "missing",
|
|
587
|
+
poolPath: poolResult.poolPath,
|
|
588
|
+
error: `no credentials stored for ${provider}`,
|
|
589
|
+
};
|
|
590
|
+
}
|
|
591
|
+
function writeProviderBinding(input) {
|
|
592
|
+
const updatedAt = providerCliNow(input.deps).toISOString();
|
|
593
|
+
input.state.updatedAt = updatedAt;
|
|
594
|
+
input.state.lanes[input.lane] = {
|
|
595
|
+
provider: input.provider,
|
|
596
|
+
model: input.model,
|
|
597
|
+
source: "local",
|
|
598
|
+
updatedAt,
|
|
599
|
+
};
|
|
600
|
+
input.state.readiness[input.lane] = {
|
|
601
|
+
status: input.status,
|
|
602
|
+
provider: input.provider,
|
|
603
|
+
model: input.model,
|
|
604
|
+
checkedAt: updatedAt,
|
|
605
|
+
...(input.credentialRevision ? { credentialRevision: input.credentialRevision } : {}),
|
|
606
|
+
...(input.error ? { error: input.error } : {}),
|
|
607
|
+
...(input.attempts !== undefined ? { attempts: input.attempts } : {}),
|
|
608
|
+
};
|
|
609
|
+
(0, provider_state_1.writeProviderState)(input.agentRoot, input.state);
|
|
610
|
+
}
|
|
611
|
+
function writeProviderReadiness(input) {
|
|
612
|
+
const checkedAt = providerCliNow(input.deps).toISOString();
|
|
613
|
+
input.state.updatedAt = checkedAt;
|
|
614
|
+
input.state.readiness[input.lane] = {
|
|
615
|
+
status: input.status,
|
|
616
|
+
provider: input.provider,
|
|
617
|
+
model: input.model,
|
|
618
|
+
checkedAt,
|
|
619
|
+
credentialRevision: input.credentialRevision,
|
|
620
|
+
...(input.error ? { error: input.error } : {}),
|
|
621
|
+
...(input.attempts !== undefined ? { attempts: input.attempts } : {}),
|
|
622
|
+
};
|
|
623
|
+
(0, provider_state_1.writeProviderState)(input.agentRoot, input.state);
|
|
624
|
+
}
|
|
625
|
+
async function executeProviderUse(command, deps, options = {}) {
|
|
626
|
+
const writeMessage = (message) => {
|
|
627
|
+
if (options.writeStdout !== false)
|
|
628
|
+
deps.writeStdout(message);
|
|
629
|
+
return message;
|
|
630
|
+
};
|
|
631
|
+
const { agentRoot, state } = readOrBootstrapProviderState(command.agent, deps);
|
|
632
|
+
const credential = readProviderCredentialRecord(command.agent, command.provider, deps);
|
|
633
|
+
if (!credential.ok) {
|
|
634
|
+
if (!command.force) {
|
|
635
|
+
const message = [
|
|
636
|
+
`no credentials stored for ${command.provider}.`,
|
|
637
|
+
`Run \`ouro auth --agent ${command.agent} --provider ${command.provider}\` first.`,
|
|
638
|
+
].join("\n");
|
|
639
|
+
return writeMessage(message);
|
|
640
|
+
}
|
|
641
|
+
writeProviderBinding({
|
|
642
|
+
agentRoot,
|
|
643
|
+
state,
|
|
644
|
+
lane: command.lane,
|
|
645
|
+
provider: command.provider,
|
|
646
|
+
model: command.model,
|
|
647
|
+
deps,
|
|
648
|
+
status: "failed",
|
|
649
|
+
error: credential.error,
|
|
650
|
+
});
|
|
651
|
+
const message = `forced ${command.agent} ${command.lane} to ${command.provider} / ${command.model}: failed (${credential.error})`;
|
|
652
|
+
return writeMessage(message);
|
|
653
|
+
}
|
|
654
|
+
const pingResult = await (0, provider_ping_1.pingProvider)(command.provider, credentialPingConfig(credential.record), {
|
|
655
|
+
model: command.model,
|
|
656
|
+
attemptPolicy: { baseDelayMs: 0 },
|
|
657
|
+
sleep: deps.sleep,
|
|
658
|
+
});
|
|
659
|
+
const attempts = pingAttemptCount(pingResult);
|
|
660
|
+
if (!pingResult.ok && !command.force) {
|
|
661
|
+
const message = [
|
|
662
|
+
`${command.agent} ${command.lane} ${command.provider} / ${command.model}: failed (${pingResult.message})`,
|
|
663
|
+
`Fix credentials with \`ouro auth --agent ${command.agent} --provider ${command.provider}\` or force the local binding with \`ouro use --agent ${command.agent} --lane ${command.lane} --provider ${command.provider} --model ${command.model} --force\`.`,
|
|
664
|
+
].join("\n");
|
|
665
|
+
return writeMessage(message);
|
|
666
|
+
}
|
|
667
|
+
writeProviderBinding({
|
|
668
|
+
agentRoot,
|
|
669
|
+
state,
|
|
670
|
+
lane: command.lane,
|
|
671
|
+
provider: command.provider,
|
|
672
|
+
model: command.model,
|
|
673
|
+
deps,
|
|
674
|
+
status: pingResult.ok ? "ready" : "failed",
|
|
675
|
+
credentialRevision: credential.record.revision,
|
|
676
|
+
...(!pingResult.ok ? { error: pingResult.message } : {}),
|
|
677
|
+
...(attempts !== undefined ? { attempts } : {}),
|
|
678
|
+
});
|
|
679
|
+
const status = pingResult.ok ? "ready" : `failed (${pingResult.message})`;
|
|
680
|
+
const message = `${command.force ? "forced " : ""}${command.agent} ${command.lane} ${command.provider} / ${command.model}: ${status}`;
|
|
681
|
+
(0, runtime_1.emitNervesEvent)({
|
|
682
|
+
component: "daemon",
|
|
683
|
+
event: "daemon.provider_use_completed",
|
|
684
|
+
message: "provider use command completed",
|
|
685
|
+
meta: { agent: command.agent, lane: command.lane, provider: command.provider, model: command.model, status: pingResult.ok ? "ready" : "failed" },
|
|
686
|
+
});
|
|
687
|
+
return writeMessage(message);
|
|
688
|
+
}
|
|
689
|
+
async function executeProviderCheck(command, deps) {
|
|
690
|
+
const { agentRoot, state } = readOrBootstrapProviderState(command.agent, deps);
|
|
691
|
+
const binding = state.lanes[command.lane];
|
|
692
|
+
const credential = readProviderCredentialRecord(command.agent, binding.provider, deps);
|
|
693
|
+
if (!credential.ok) {
|
|
694
|
+
const message = [
|
|
695
|
+
`${command.agent} ${command.lane} ${binding.provider} / ${binding.model}: unknown (${credential.error})`,
|
|
696
|
+
`Run \`ouro auth --agent ${command.agent} --provider ${binding.provider}\` first.`,
|
|
697
|
+
].join("\n");
|
|
698
|
+
deps.writeStdout(message);
|
|
699
|
+
return message;
|
|
700
|
+
}
|
|
701
|
+
const pingResult = await (0, provider_ping_1.pingProvider)(binding.provider, credentialPingConfig(credential.record), {
|
|
702
|
+
model: binding.model,
|
|
703
|
+
attemptPolicy: { baseDelayMs: 0 },
|
|
704
|
+
sleep: deps.sleep,
|
|
705
|
+
});
|
|
706
|
+
const attempts = pingAttemptCount(pingResult);
|
|
707
|
+
writeProviderReadiness({
|
|
708
|
+
agentRoot,
|
|
709
|
+
state,
|
|
710
|
+
lane: command.lane,
|
|
711
|
+
provider: binding.provider,
|
|
712
|
+
model: binding.model,
|
|
713
|
+
deps,
|
|
714
|
+
status: pingResult.ok ? "ready" : "failed",
|
|
715
|
+
credentialRevision: credential.record.revision,
|
|
716
|
+
...(!pingResult.ok ? { error: pingResult.message } : {}),
|
|
717
|
+
...(attempts !== undefined ? { attempts } : {}),
|
|
718
|
+
});
|
|
719
|
+
const status = pingResult.ok ? "ready" : `failed (${pingResult.message})`;
|
|
720
|
+
const message = `${command.agent} ${command.lane} ${binding.provider} / ${binding.model}: ${status}`;
|
|
721
|
+
deps.writeStdout(message);
|
|
722
|
+
(0, runtime_1.emitNervesEvent)({
|
|
723
|
+
component: "daemon",
|
|
724
|
+
event: "daemon.provider_check_completed",
|
|
725
|
+
message: "provider check command completed",
|
|
726
|
+
meta: { agent: command.agent, lane: command.lane, provider: binding.provider, model: binding.model, status: pingResult.ok ? "ready" : "failed" },
|
|
727
|
+
});
|
|
728
|
+
return message;
|
|
729
|
+
}
|
|
730
|
+
function renderProviderCredentialLine(credential) {
|
|
731
|
+
if (credential.status === "present") {
|
|
732
|
+
const contributor = credential.contributedByAgent ? ` by ${credential.contributedByAgent}` : "";
|
|
733
|
+
const credentialFields = credential.credentialFields.length > 0 ? ` credentials: ${credential.credentialFields.join(", ")}` : " credentials: none";
|
|
734
|
+
const configFields = credential.configFields.length > 0 ? ` config: ${credential.configFields.join(", ")}` : " config: none";
|
|
735
|
+
return `credentials: present (${credential.source}${contributor}; ${credential.revision};${credentialFields};${configFields})`;
|
|
736
|
+
}
|
|
737
|
+
if (credential.status === "invalid-pool") {
|
|
738
|
+
return `credentials: invalid pool (${credential.error}); repair: ${credential.repair.command}`;
|
|
739
|
+
}
|
|
740
|
+
return `credentials: missing; repair: ${credential.repair.command}`;
|
|
741
|
+
}
|
|
742
|
+
function executeProviderStatus(command, deps) {
|
|
743
|
+
const agentRoot = providerCliAgentRoot(command, deps);
|
|
744
|
+
const homeDir = providerCliHomeDir(deps);
|
|
745
|
+
const lines = [`provider status: ${command.agent}`];
|
|
746
|
+
for (const lane of ["outward", "inner"]) {
|
|
747
|
+
const resolved = (0, provider_binding_resolver_1.resolveEffectiveProviderBinding)({
|
|
748
|
+
agentName: command.agent,
|
|
749
|
+
agentRoot,
|
|
750
|
+
homeDir,
|
|
751
|
+
lane,
|
|
752
|
+
});
|
|
753
|
+
if (!resolved.ok) {
|
|
754
|
+
lines.push(` ${lane}: unavailable`);
|
|
755
|
+
lines.push(` ${resolved.reason}: ${resolved.repair.command}`);
|
|
756
|
+
continue;
|
|
757
|
+
}
|
|
758
|
+
const binding = resolved.binding;
|
|
759
|
+
lines.push(` ${lane}: ${binding.provider} / ${binding.model} (${binding.source})`);
|
|
760
|
+
lines.push(` readiness: ${binding.readiness.status}${binding.readiness.error ? ` (${binding.readiness.error})` : ""}`);
|
|
761
|
+
lines.push(` ${renderProviderCredentialLine(binding.credential)}`);
|
|
762
|
+
for (const warning of binding.warnings) {
|
|
763
|
+
lines.push(` warning: ${warning.message}`);
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
const message = lines.join("\n");
|
|
767
|
+
deps.writeStdout(message);
|
|
768
|
+
return message;
|
|
769
|
+
}
|
|
770
|
+
async function executeLegacyAuthSwitch(command, deps) {
|
|
771
|
+
const { state } = readOrBootstrapProviderState(command.agent, deps);
|
|
772
|
+
const lanes = command.facing
|
|
773
|
+
? [command.facing === "human" ? "outward" : "inner"]
|
|
774
|
+
: ["outward", "inner"];
|
|
775
|
+
const messages = [];
|
|
776
|
+
for (const lane of lanes) {
|
|
777
|
+
const model = state.lanes[lane].model;
|
|
778
|
+
messages.push(await executeProviderUse({
|
|
779
|
+
kind: "provider.use",
|
|
780
|
+
agent: command.agent,
|
|
781
|
+
lane,
|
|
782
|
+
provider: command.provider,
|
|
783
|
+
model,
|
|
784
|
+
legacyFacing: command.facing,
|
|
785
|
+
}, deps, { writeStdout: false }));
|
|
786
|
+
}
|
|
787
|
+
const message = [
|
|
788
|
+
`deprecated: switched this machine's local provider binding. \`ouro auth switch\` no longer edits agent.json.`,
|
|
789
|
+
...messages,
|
|
790
|
+
`Use \`ouro use --agent ${command.agent} --lane <outward|inner> --provider ${command.provider} --model <model>\` for explicit provider/model selection.`,
|
|
791
|
+
].join("\n");
|
|
792
|
+
deps.writeStdout(message);
|
|
793
|
+
return message;
|
|
794
|
+
}
|
|
795
|
+
async function executeLegacyConfigModel(command, deps) {
|
|
796
|
+
const lane = command.facing === "agent" ? "inner" : "outward";
|
|
797
|
+
const { agentRoot, state } = readOrBootstrapProviderState(command.agent, deps);
|
|
798
|
+
const binding = state.lanes[lane];
|
|
799
|
+
if (binding.provider === "github-copilot") {
|
|
800
|
+
const credential = readProviderCredentialRecord(command.agent, "github-copilot", deps);
|
|
801
|
+
if (credential.ok) {
|
|
802
|
+
const ghConfig = {
|
|
803
|
+
...credential.record.config,
|
|
804
|
+
...credential.record.credentials,
|
|
805
|
+
};
|
|
806
|
+
const githubToken = ghConfig.githubToken;
|
|
807
|
+
const baseUrl = ghConfig.baseUrl;
|
|
808
|
+
if (typeof githubToken === "string" && typeof baseUrl === "string") {
|
|
809
|
+
const fetchFn = deps.fetchImpl ?? fetch;
|
|
810
|
+
try {
|
|
811
|
+
const models = await listGithubCopilotModels(baseUrl, githubToken, fetchFn);
|
|
812
|
+
const available = models.map((m) => m.id);
|
|
813
|
+
if (available.length > 0 && !available.includes(command.modelName)) {
|
|
814
|
+
const message = `model '${command.modelName}' not found. available models:\n${available.map((id) => ` ${id}`).join("\n")}`;
|
|
815
|
+
deps.writeStdout(message);
|
|
816
|
+
return message;
|
|
817
|
+
}
|
|
818
|
+
}
|
|
819
|
+
catch {
|
|
820
|
+
// Catalog validation failed; the live ping below gives the actionable result.
|
|
821
|
+
}
|
|
822
|
+
const pingResult = await (0, provider_ping_1.pingGithubCopilotModel)(baseUrl, githubToken, command.modelName, fetchFn);
|
|
823
|
+
if (!pingResult.ok) {
|
|
824
|
+
const message = `model '${command.modelName}' ping failed: ${pingResult.error}\nrun \`ouro config models --agent ${command.agent}\` to see available models.`;
|
|
825
|
+
deps.writeStdout(message);
|
|
826
|
+
return message;
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
const updatedAt = providerCliNow(deps).toISOString();
|
|
832
|
+
state.updatedAt = updatedAt;
|
|
833
|
+
state.lanes[lane] = {
|
|
834
|
+
...binding,
|
|
835
|
+
model: command.modelName,
|
|
836
|
+
source: "local",
|
|
837
|
+
updatedAt,
|
|
838
|
+
};
|
|
839
|
+
delete state.readiness[lane];
|
|
840
|
+
(0, provider_state_1.writeProviderState)(agentRoot, state);
|
|
841
|
+
const message = `deprecated: updated ${command.agent} model on ${lane}/${binding.provider}: ${binding.model} -> ${command.modelName}\nUse \`ouro use --agent ${command.agent} --lane ${lane} --provider ${binding.provider} --model ${command.modelName}\` next time.`;
|
|
842
|
+
deps.writeStdout(message);
|
|
843
|
+
return message;
|
|
844
|
+
}
|
|
845
|
+
// ── System setup ──
|
|
846
|
+
async function registerOuroBundleTypeNonBlocking(deps) {
|
|
847
|
+
const registerOuroBundleType = deps.registerOuroBundleType;
|
|
848
|
+
if (!registerOuroBundleType)
|
|
849
|
+
return;
|
|
850
|
+
try {
|
|
851
|
+
await Promise.resolve(registerOuroBundleType());
|
|
852
|
+
}
|
|
853
|
+
catch (error) {
|
|
854
|
+
(0, runtime_1.emitNervesEvent)({
|
|
855
|
+
level: "warn",
|
|
856
|
+
component: "daemon",
|
|
857
|
+
event: "daemon.ouro_uti_register_error",
|
|
858
|
+
message: "failed .ouro UTI registration from CLI flow",
|
|
859
|
+
meta: { error: error instanceof Error ? error.message : String(error) },
|
|
860
|
+
});
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
async function performSystemSetup(deps) {
|
|
864
|
+
// Install ouro command to PATH (non-blocking)
|
|
865
|
+
if (deps.installOuroCommand) {
|
|
866
|
+
try {
|
|
867
|
+
const installResult = deps.installOuroCommand();
|
|
868
|
+
/* v8 ignore next -- old-launcher repair hint: fires when stale ~/.local/bin/ouro is fixed @preserve */
|
|
869
|
+
if (installResult.repairedOldLauncher) {
|
|
870
|
+
deps.writeStdout("repaired stale ouro launcher at ~/.local/bin/ouro");
|
|
871
|
+
}
|
|
872
|
+
if (installResult.pathResolution?.status === "shadowed") {
|
|
873
|
+
deps.writeStdout(`fix ouro PATH: ${installResult.pathResolution.detail}; ` +
|
|
874
|
+
`fix: ${installResult.pathResolution.remediation}`);
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
catch (error) {
|
|
878
|
+
(0, runtime_1.emitNervesEvent)({
|
|
879
|
+
level: "warn",
|
|
880
|
+
component: "daemon",
|
|
881
|
+
event: "daemon.system_setup_ouro_cmd_error",
|
|
882
|
+
message: "failed to install ouro command to PATH",
|
|
883
|
+
meta: { error: error instanceof Error ? error.message : /* v8 ignore next -- defensive: non-Error catch branch @preserve */ String(error) },
|
|
884
|
+
});
|
|
885
|
+
}
|
|
886
|
+
}
|
|
887
|
+
// Self-healing: ensure current version is installed in ~/.ouro-cli/ layout.
|
|
888
|
+
// Handles the case where the wrapper exists but CurrentVersion is missing
|
|
889
|
+
// (e.g., first run after migration from old npx wrapper).
|
|
890
|
+
if (deps.ensureCurrentVersionInstalled) {
|
|
891
|
+
try {
|
|
892
|
+
deps.ensureCurrentVersionInstalled();
|
|
893
|
+
}
|
|
894
|
+
catch (error) {
|
|
895
|
+
(0, runtime_1.emitNervesEvent)({
|
|
896
|
+
level: "warn",
|
|
897
|
+
component: "daemon",
|
|
898
|
+
event: "daemon.system_setup_version_install_error",
|
|
899
|
+
message: "failed to ensure current version installed",
|
|
900
|
+
meta: { error: error instanceof Error ? error.message : /* v8 ignore next -- defensive @preserve */ String(error) },
|
|
901
|
+
});
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
if (deps.syncGlobalOuroBotWrapper) {
|
|
905
|
+
try {
|
|
906
|
+
await Promise.resolve(deps.syncGlobalOuroBotWrapper());
|
|
907
|
+
}
|
|
908
|
+
catch (error) {
|
|
909
|
+
(0, runtime_1.emitNervesEvent)({
|
|
910
|
+
level: "warn",
|
|
911
|
+
component: "daemon",
|
|
912
|
+
event: "daemon.system_setup_ouro_bot_wrapper_error",
|
|
913
|
+
message: "failed to sync global ouro.bot wrapper",
|
|
914
|
+
meta: { error: error instanceof Error ? error.message : /* v8 ignore next -- defensive: non-Error catch branch @preserve */ String(error) },
|
|
915
|
+
});
|
|
916
|
+
}
|
|
917
|
+
}
|
|
918
|
+
// Ensure skill-management skill is available
|
|
919
|
+
if (deps.ensureSkillManagement) {
|
|
920
|
+
try {
|
|
921
|
+
await deps.ensureSkillManagement();
|
|
922
|
+
/* v8 ignore start -- defensive: ensureSkillManagement handles its own errors internally @preserve */
|
|
923
|
+
}
|
|
924
|
+
catch (error) {
|
|
925
|
+
(0, runtime_1.emitNervesEvent)({
|
|
926
|
+
level: "warn",
|
|
927
|
+
component: "daemon",
|
|
928
|
+
event: "daemon.system_setup_skill_management_error",
|
|
929
|
+
message: "failed to ensure skill-management skill",
|
|
930
|
+
meta: { error: error instanceof Error ? error.message : String(error) },
|
|
931
|
+
});
|
|
932
|
+
}
|
|
933
|
+
/* v8 ignore stop */
|
|
934
|
+
}
|
|
935
|
+
// Register .ouro bundle type (UTI on macOS)
|
|
936
|
+
await registerOuroBundleTypeNonBlocking(deps);
|
|
937
|
+
}
|
|
938
|
+
// ── Task command execution ──
|
|
939
|
+
function executeTaskCommand(command, taskMod) {
|
|
940
|
+
if (command.kind === "task.board") {
|
|
941
|
+
if (command.status) {
|
|
942
|
+
const lines = taskMod.boardStatus(command.status);
|
|
943
|
+
return lines.length > 0 ? lines.join("\n") : "no tasks in that status";
|
|
944
|
+
}
|
|
945
|
+
const board = taskMod.getBoard();
|
|
946
|
+
return board.full || board.compact || "no tasks found";
|
|
947
|
+
}
|
|
948
|
+
if (command.kind === "task.create") {
|
|
949
|
+
try {
|
|
950
|
+
const created = taskMod.createTask({
|
|
951
|
+
title: command.title,
|
|
952
|
+
type: command.type ?? "one-shot",
|
|
953
|
+
category: "general",
|
|
954
|
+
body: "",
|
|
955
|
+
});
|
|
956
|
+
return `created: ${created}`;
|
|
957
|
+
}
|
|
958
|
+
catch (error) {
|
|
959
|
+
return `error: ${error instanceof Error ? error.message : /* v8 ignore next -- defensive: non-Error catch branch @preserve */ String(error)}`;
|
|
960
|
+
}
|
|
961
|
+
}
|
|
962
|
+
if (command.kind === "task.update") {
|
|
963
|
+
const result = taskMod.updateStatus(command.id, command.status);
|
|
964
|
+
if (!result.ok) {
|
|
965
|
+
return `error: ${result.reason ?? "status update failed"}`;
|
|
966
|
+
}
|
|
967
|
+
const archivedSuffix = result.archived && result.archived.length > 0
|
|
968
|
+
? ` | archived: ${result.archived.join(", ")}`
|
|
969
|
+
: "";
|
|
970
|
+
return `updated: ${command.id} -> ${result.to}${archivedSuffix}`;
|
|
971
|
+
}
|
|
972
|
+
if (command.kind === "task.show") {
|
|
973
|
+
const task = taskMod.getTask(command.id);
|
|
974
|
+
if (!task)
|
|
975
|
+
return `task not found: ${command.id}`;
|
|
976
|
+
return [
|
|
977
|
+
`title: ${task.title}`,
|
|
978
|
+
`type: ${task.type}`,
|
|
979
|
+
`status: ${task.status}`,
|
|
980
|
+
`category: ${task.category}`,
|
|
981
|
+
`created: ${task.created}`,
|
|
982
|
+
`updated: ${task.updated}`,
|
|
983
|
+
`path: ${task.path}`,
|
|
984
|
+
task.body ? `\n${task.body}` : "",
|
|
985
|
+
].filter(Boolean).join("\n");
|
|
986
|
+
}
|
|
987
|
+
if (command.kind === "task.actionable") {
|
|
988
|
+
const lines = taskMod.boardAction();
|
|
989
|
+
return lines.length > 0 ? lines.join("\n") : "no action required";
|
|
990
|
+
}
|
|
991
|
+
if (command.kind === "task.deps") {
|
|
992
|
+
const lines = taskMod.boardDeps();
|
|
993
|
+
return lines.length > 0 ? lines.join("\n") : "no unresolved dependencies";
|
|
994
|
+
}
|
|
995
|
+
if (command.kind === "task.fix") {
|
|
996
|
+
try {
|
|
997
|
+
const fixOptions = {
|
|
998
|
+
mode: command.mode,
|
|
999
|
+
...(command.issueId ? { issueId: command.issueId } : {}),
|
|
1000
|
+
...(command.option !== undefined ? { option: command.option } : {}),
|
|
1001
|
+
};
|
|
1002
|
+
const result = taskMod.fix(fixOptions);
|
|
1003
|
+
if (command.mode === "dry-run") {
|
|
1004
|
+
if (result.remaining.length === 0) {
|
|
1005
|
+
return `task health: clean`;
|
|
1006
|
+
}
|
|
1007
|
+
const safeIssues = result.remaining.filter((i) => i.confidence === "safe");
|
|
1008
|
+
const reviewIssues = result.remaining.filter((i) => i.confidence === "needs_review");
|
|
1009
|
+
const lines = [`${result.remaining.length} issues found`];
|
|
1010
|
+
if (safeIssues.length > 0) {
|
|
1011
|
+
lines.push("", `safe fixes (${safeIssues.length}):`);
|
|
1012
|
+
for (const issue of safeIssues) {
|
|
1013
|
+
lines.push(` ${issue.code}:${issue.target} -- ${issue.description}`);
|
|
1014
|
+
}
|
|
1015
|
+
}
|
|
1016
|
+
if (reviewIssues.length > 0) {
|
|
1017
|
+
lines.push("", `needs review (${reviewIssues.length}):`);
|
|
1018
|
+
for (const issue of reviewIssues) {
|
|
1019
|
+
lines.push(` ${issue.code}:${issue.target} -- ${issue.description}`);
|
|
1020
|
+
}
|
|
1021
|
+
}
|
|
1022
|
+
lines.push("", `task health: ${result.health}`);
|
|
1023
|
+
return lines.join("\n");
|
|
1024
|
+
}
|
|
1025
|
+
// safe, single, or --all modes: show what was done
|
|
1026
|
+
const lines = [];
|
|
1027
|
+
if (result.applied.length > 0) {
|
|
1028
|
+
lines.push(`${result.applied.length} applied:`);
|
|
1029
|
+
for (const issue of result.applied) {
|
|
1030
|
+
lines.push(` ${issue.code}:${issue.target}`);
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
1033
|
+
if (result.remaining.length > 0) {
|
|
1034
|
+
lines.push(`${result.remaining.length} remaining:`);
|
|
1035
|
+
for (const issue of result.remaining) {
|
|
1036
|
+
lines.push(` ${issue.code}:${issue.target} -- ${issue.description}`);
|
|
1037
|
+
}
|
|
1038
|
+
}
|
|
1039
|
+
if (result.applied.length === 0 && result.remaining.length === 0) {
|
|
1040
|
+
lines.push("no issues");
|
|
1041
|
+
}
|
|
1042
|
+
lines.push(`task health: ${result.health}`);
|
|
1043
|
+
return lines.join("\n");
|
|
1044
|
+
}
|
|
1045
|
+
catch (error) {
|
|
1046
|
+
return `error: ${error instanceof Error ? error.message : /* v8 ignore next -- defensive: non-Error catch branch @preserve */ String(error)}`;
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
1049
|
+
// command.kind === "task.sessions"
|
|
1050
|
+
const lines = taskMod.boardSessions();
|
|
1051
|
+
return lines.length > 0 ? lines.join("\n") : "no active sessions";
|
|
1052
|
+
}
|
|
1053
|
+
// ── Friend command execution ──
|
|
1054
|
+
const TRUST_RANK = { family: 4, friend: 3, acquaintance: 2, stranger: 1 };
|
|
1055
|
+
/* v8 ignore start -- defensive: ?? fallbacks are unreachable when inputs are valid TrustLevel values @preserve */
|
|
1056
|
+
function higherTrust(a, b) {
|
|
1057
|
+
const rankA = TRUST_RANK[a ?? "stranger"] ?? 1;
|
|
1058
|
+
const rankB = TRUST_RANK[b ?? "stranger"] ?? 1;
|
|
1059
|
+
return rankA >= rankB ? (a ?? "stranger") : (b ?? "stranger");
|
|
1060
|
+
}
|
|
1061
|
+
/* v8 ignore stop */
|
|
1062
|
+
async function executeFriendCommand(command, store) {
|
|
1063
|
+
if (command.kind === "friend.list") {
|
|
1064
|
+
const listAll = store.listAll;
|
|
1065
|
+
if (!listAll)
|
|
1066
|
+
return "friend store does not support listing";
|
|
1067
|
+
const friends = await listAll.call(store);
|
|
1068
|
+
if (friends.length === 0)
|
|
1069
|
+
return "no friends found";
|
|
1070
|
+
const lines = friends.map((f) => {
|
|
1071
|
+
const trust = f.trustLevel ?? "unknown";
|
|
1072
|
+
return `${f.id} ${f.name} ${trust}`;
|
|
1073
|
+
});
|
|
1074
|
+
return lines.join("\n");
|
|
1075
|
+
}
|
|
1076
|
+
if (command.kind === "friend.show") {
|
|
1077
|
+
const record = await store.get(command.friendId);
|
|
1078
|
+
if (!record)
|
|
1079
|
+
return `friend not found: ${command.friendId}`;
|
|
1080
|
+
return JSON.stringify(record, null, 2);
|
|
1081
|
+
}
|
|
1082
|
+
if (command.kind === "friend.create") {
|
|
1083
|
+
const now = new Date().toISOString();
|
|
1084
|
+
const id = (0, crypto_1.randomUUID)();
|
|
1085
|
+
const trustLevel = (command.trustLevel ?? "acquaintance");
|
|
1086
|
+
await store.put(id, {
|
|
1087
|
+
id,
|
|
1088
|
+
name: command.name,
|
|
1089
|
+
trustLevel,
|
|
1090
|
+
externalIds: [],
|
|
1091
|
+
tenantMemberships: [],
|
|
1092
|
+
toolPreferences: {},
|
|
1093
|
+
notes: {},
|
|
1094
|
+
totalTokens: 0,
|
|
1095
|
+
createdAt: now,
|
|
1096
|
+
updatedAt: now,
|
|
1097
|
+
schemaVersion: 1,
|
|
1098
|
+
});
|
|
1099
|
+
return `created: ${id} (${command.name}, ${trustLevel})`;
|
|
1100
|
+
}
|
|
1101
|
+
if (command.kind === "friend.update") {
|
|
1102
|
+
const current = await store.get(command.friendId);
|
|
1103
|
+
if (!current)
|
|
1104
|
+
return `friend not found: ${command.friendId}`;
|
|
1105
|
+
const now = new Date().toISOString();
|
|
1106
|
+
await store.put(command.friendId, {
|
|
1107
|
+
...current,
|
|
1108
|
+
trustLevel: command.trustLevel,
|
|
1109
|
+
role: command.trustLevel,
|
|
1110
|
+
updatedAt: now,
|
|
1111
|
+
});
|
|
1112
|
+
return `updated: ${command.friendId} → trust=${command.trustLevel}`;
|
|
1113
|
+
}
|
|
1114
|
+
if (command.kind === "friend.link") {
|
|
1115
|
+
const current = await store.get(command.friendId);
|
|
1116
|
+
if (!current)
|
|
1117
|
+
return `friend not found: ${command.friendId}`;
|
|
1118
|
+
const alreadyLinked = current.externalIds.some((ext) => ext.provider === command.provider && ext.externalId === command.externalId);
|
|
1119
|
+
if (alreadyLinked)
|
|
1120
|
+
return `identity already linked: ${command.provider}:${command.externalId}`;
|
|
1121
|
+
const now = new Date().toISOString();
|
|
1122
|
+
const newExternalIds = [
|
|
1123
|
+
...current.externalIds,
|
|
1124
|
+
{ provider: command.provider, externalId: command.externalId, linkedAt: now },
|
|
1125
|
+
];
|
|
1126
|
+
// Orphan cleanup: check if another friend has this externalId
|
|
1127
|
+
const orphan = await store.findByExternalId(command.provider, command.externalId);
|
|
1128
|
+
let mergeMessage = "";
|
|
1129
|
+
let mergedNotes = { ...current.notes };
|
|
1130
|
+
let mergedTrust = current.trustLevel;
|
|
1131
|
+
let orphanExternalIds = [];
|
|
1132
|
+
if (orphan && orphan.id !== command.friendId) {
|
|
1133
|
+
// Merge orphan's notes (target's notes take priority)
|
|
1134
|
+
mergedNotes = { ...orphan.notes, ...current.notes };
|
|
1135
|
+
// Keep higher trust level
|
|
1136
|
+
mergedTrust = higherTrust(current.trustLevel, orphan.trustLevel);
|
|
1137
|
+
// Collect orphan's other externalIds (excluding the one being linked)
|
|
1138
|
+
orphanExternalIds = orphan.externalIds.filter((ext) => !(ext.provider === command.provider && ext.externalId === command.externalId));
|
|
1139
|
+
await store.delete(orphan.id);
|
|
1140
|
+
mergeMessage = ` (merged orphan ${orphan.id})`;
|
|
1141
|
+
}
|
|
1142
|
+
await store.put(command.friendId, {
|
|
1143
|
+
...current,
|
|
1144
|
+
externalIds: [...newExternalIds, ...orphanExternalIds],
|
|
1145
|
+
notes: mergedNotes,
|
|
1146
|
+
trustLevel: mergedTrust,
|
|
1147
|
+
updatedAt: now,
|
|
1148
|
+
});
|
|
1149
|
+
return `linked ${command.provider}:${command.externalId} to ${command.friendId}${mergeMessage}`;
|
|
1150
|
+
}
|
|
1151
|
+
// command.kind === "friend.unlink"
|
|
1152
|
+
const current = await store.get(command.friendId);
|
|
1153
|
+
if (!current)
|
|
1154
|
+
return `friend not found: ${command.friendId}`;
|
|
1155
|
+
const idx = current.externalIds.findIndex((ext) => ext.provider === command.provider && ext.externalId === command.externalId);
|
|
1156
|
+
if (idx === -1)
|
|
1157
|
+
return `identity not linked: ${command.provider}:${command.externalId}`;
|
|
1158
|
+
const now = new Date().toISOString();
|
|
1159
|
+
const filtered = current.externalIds.filter((_, i) => i !== idx);
|
|
1160
|
+
await store.put(command.friendId, { ...current, externalIds: filtered, updatedAt: now });
|
|
1161
|
+
return `unlinked ${command.provider}:${command.externalId} from ${command.friendId}`;
|
|
1162
|
+
}
|
|
1163
|
+
// ── Reminder command execution ──
|
|
1164
|
+
function executeReminderCommand(command, taskMod) {
|
|
1165
|
+
try {
|
|
1166
|
+
const created = taskMod.createTask({
|
|
1167
|
+
title: command.title,
|
|
1168
|
+
type: command.cadence ? "ongoing" : "one-shot",
|
|
1169
|
+
category: command.category ?? "reminder",
|
|
1170
|
+
body: command.body,
|
|
1171
|
+
scheduledAt: command.scheduledAt,
|
|
1172
|
+
cadence: command.cadence,
|
|
1173
|
+
requester: command.requester,
|
|
1174
|
+
});
|
|
1175
|
+
return `created: ${created}`;
|
|
1176
|
+
}
|
|
1177
|
+
catch (error) {
|
|
1178
|
+
return `error: ${error instanceof Error ? error.message : /* v8 ignore next -- defensive: non-Error catch branch @preserve */ String(error)}`;
|
|
1179
|
+
}
|
|
1180
|
+
}
|
|
1181
|
+
// ── Dev mode helpers ──
|
|
1182
|
+
/* v8 ignore start -- repo resolution for ouro dev: repoPath branch tested via daemon-cli-dev; clone requires real git/npm @preserve */
|
|
1183
|
+
function getDevConfigPath() {
|
|
1184
|
+
return path.join((0, ouro_version_manager_1.getOuroCliHome)(), "dev-config.json");
|
|
1185
|
+
}
|
|
1186
|
+
function readPersistedDevPath() {
|
|
1187
|
+
try {
|
|
1188
|
+
const raw = fs.readFileSync(getDevConfigPath(), "utf-8");
|
|
1189
|
+
const config = JSON.parse(raw);
|
|
1190
|
+
if (typeof config.repoPath === "string")
|
|
1191
|
+
return config.repoPath;
|
|
1192
|
+
}
|
|
1193
|
+
catch { /* no persisted path */ }
|
|
1194
|
+
return null;
|
|
1195
|
+
}
|
|
1196
|
+
function persistDevPath(repoPath) {
|
|
1197
|
+
const configPath = getDevConfigPath();
|
|
1198
|
+
fs.mkdirSync(path.dirname(configPath), { recursive: true });
|
|
1199
|
+
fs.writeFileSync(configPath, JSON.stringify({ repoPath }), "utf-8");
|
|
1200
|
+
}
|
|
1201
|
+
function resolveDevRepoCwd(command, checkExists, deps) {
|
|
1202
|
+
if (command.repoPath) {
|
|
1203
|
+
persistDevPath(command.repoPath);
|
|
1204
|
+
return command.repoPath;
|
|
1205
|
+
}
|
|
1206
|
+
if (deps.getRepoCwd)
|
|
1207
|
+
return deps.getRepoCwd();
|
|
1208
|
+
const persisted = readPersistedDevPath();
|
|
1209
|
+
if (persisted && checkExists(path.join(persisted, ".git"))) {
|
|
1210
|
+
deps.writeStdout(`using persisted dev repo: ${persisted}`);
|
|
1211
|
+
return persisted;
|
|
1212
|
+
}
|
|
1213
|
+
return (0, identity_1.getRepoRoot)();
|
|
1214
|
+
}
|
|
1215
|
+
function resolveClonePath(options, checkExists, deps) {
|
|
1216
|
+
const cloneTarget = options.clonePath ?? path.join(os.homedir(), "Projects", "ouroboros");
|
|
1217
|
+
if (checkExists(path.join(cloneTarget, ".git"))) {
|
|
1218
|
+
// Existing repo — pull latest and rebuild
|
|
1219
|
+
deps.writeStdout(`pulling latest in ${cloneTarget}...`);
|
|
1220
|
+
(0, child_process_1.execSync)("git pull", { cwd: cloneTarget, stdio: "inherit" });
|
|
1221
|
+
deps.writeStdout(`installing dependencies in ${cloneTarget}...`);
|
|
1222
|
+
(0, child_process_1.execSync)("npm install", { cwd: cloneTarget, stdio: "inherit" });
|
|
1223
|
+
persistDevPath(cloneTarget);
|
|
1224
|
+
return cloneTarget;
|
|
1225
|
+
}
|
|
1226
|
+
// Fresh clone
|
|
1227
|
+
deps.writeStdout(`cloning ouroboros to ${cloneTarget}...`);
|
|
1228
|
+
const HARNESS_CANONICAL_REPO_URL = "https://github.com/ouroborosbot/ouroboros.git";
|
|
1229
|
+
fs.mkdirSync(path.dirname(cloneTarget), { recursive: true });
|
|
1230
|
+
(0, child_process_1.execSync)(`git clone ${HARNESS_CANONICAL_REPO_URL} "${cloneTarget}"`, { stdio: "inherit" });
|
|
1231
|
+
deps.writeStdout(`installing dependencies in ${cloneTarget}...`);
|
|
1232
|
+
(0, child_process_1.execSync)("npm install", { cwd: cloneTarget, stdio: "inherit" });
|
|
1233
|
+
deps.writeStdout(`building in ${cloneTarget}...`);
|
|
1234
|
+
try {
|
|
1235
|
+
(0, child_process_1.execSync)("npm run build", { cwd: cloneTarget, stdio: "inherit" });
|
|
1236
|
+
}
|
|
1237
|
+
catch {
|
|
1238
|
+
throw new Error(`build failed in ${cloneTarget}. check the output above.`);
|
|
1239
|
+
}
|
|
1240
|
+
return cloneTarget;
|
|
1241
|
+
}
|
|
1242
|
+
/* v8 ignore stop */
|
|
1243
|
+
// ── Main CLI execution ──
|
|
1244
|
+
async function runOuroCli(args, deps = (0, cli_defaults_1.createDefaultOuroCliDeps)()) {
|
|
1245
|
+
if (args.length === 1 && (args[0] === "--help" || args[0] === "-h")) {
|
|
1246
|
+
const text = (0, cli_help_1.getGroupedHelp)();
|
|
1247
|
+
deps.writeStdout(text);
|
|
1248
|
+
return text;
|
|
1249
|
+
}
|
|
1250
|
+
if (args.length === 1 && (args[0] === "-v" || args[0] === "--version")) {
|
|
1251
|
+
const text = (0, cli_render_1.formatVersionOutput)();
|
|
1252
|
+
deps.writeStdout(text);
|
|
1253
|
+
return text;
|
|
1254
|
+
}
|
|
1255
|
+
let command;
|
|
1256
|
+
try {
|
|
1257
|
+
command = (0, cli_parse_1.parseOuroCommand)(args);
|
|
1258
|
+
}
|
|
1259
|
+
catch (parseError) {
|
|
1260
|
+
if (deps.startChat && deps.listDiscoveredAgents && args.length === 1) {
|
|
1261
|
+
const discovered = await Promise.resolve(deps.listDiscoveredAgents());
|
|
1262
|
+
if (discovered.includes(args[0])) {
|
|
1263
|
+
await ensureDaemonRunning(deps);
|
|
1264
|
+
await deps.startChat(args[0]);
|
|
1265
|
+
return "";
|
|
1266
|
+
}
|
|
1267
|
+
}
|
|
1268
|
+
throw parseError;
|
|
1269
|
+
}
|
|
1270
|
+
if (args.length === 0) {
|
|
1271
|
+
const discovered = await Promise.resolve(deps.listDiscoveredAgents ? deps.listDiscoveredAgents() : (0, cli_defaults_1.defaultListDiscoveredAgents)());
|
|
1272
|
+
if (discovered.length === 0 && deps.runSerpentGuide) {
|
|
1273
|
+
// System setup first — ouro command, subagents, UTI — before the interactive specialist
|
|
1274
|
+
await performSystemSetup(deps);
|
|
1275
|
+
const hatchlingName = await deps.runSerpentGuide();
|
|
1276
|
+
if (!hatchlingName) {
|
|
1277
|
+
return "";
|
|
1278
|
+
}
|
|
1279
|
+
await ensureDaemonRunning(deps);
|
|
1280
|
+
if (deps.startChat) {
|
|
1281
|
+
await deps.startChat(hatchlingName);
|
|
1282
|
+
}
|
|
1283
|
+
return "";
|
|
1284
|
+
}
|
|
1285
|
+
else if (discovered.length === 0) {
|
|
1286
|
+
command = { kind: "hatch.start" };
|
|
1287
|
+
}
|
|
1288
|
+
else if (discovered.length === 1) {
|
|
1289
|
+
if (deps.startChat) {
|
|
1290
|
+
await ensureDaemonRunning(deps);
|
|
1291
|
+
const health = await checkProviderHealthBeforeChat(discovered[0], deps);
|
|
1292
|
+
if (!health.ok)
|
|
1293
|
+
return health.output;
|
|
1294
|
+
await deps.startChat(discovered[0]);
|
|
1295
|
+
return "";
|
|
1296
|
+
}
|
|
1297
|
+
command = { kind: "chat.connect", agent: discovered[0] };
|
|
1298
|
+
}
|
|
1299
|
+
else {
|
|
1300
|
+
if (deps.startChat && deps.promptInput) {
|
|
1301
|
+
const prompt = `who do you want to talk to?\n${discovered.map((a, i) => `${i + 1}. ${a}`).join("\n")}\n`;
|
|
1302
|
+
const answer = await deps.promptInput(prompt);
|
|
1303
|
+
const selected = discovered.includes(answer) ? answer : discovered[parseInt(answer, 10) - 1];
|
|
1304
|
+
if (!selected)
|
|
1305
|
+
throw new Error("Invalid selection");
|
|
1306
|
+
await ensureDaemonRunning(deps);
|
|
1307
|
+
const health = await checkProviderHealthBeforeChat(selected, deps);
|
|
1308
|
+
if (!health.ok)
|
|
1309
|
+
return health.output;
|
|
1310
|
+
await deps.startChat(selected);
|
|
1311
|
+
return "";
|
|
1312
|
+
}
|
|
1313
|
+
const message = `who do you want to talk to? ${discovered.join(", ")} (use: ouro chat <agent>)`;
|
|
1314
|
+
deps.writeStdout(message);
|
|
1315
|
+
return message;
|
|
1316
|
+
}
|
|
1317
|
+
(0, runtime_1.emitNervesEvent)({
|
|
1318
|
+
component: "daemon",
|
|
1319
|
+
event: "daemon.cli_auto_route",
|
|
1320
|
+
message: "routed bare ouro command from discovered agents",
|
|
1321
|
+
meta: { target: command.kind, count: discovered.length },
|
|
1322
|
+
});
|
|
1323
|
+
}
|
|
1324
|
+
(0, runtime_1.emitNervesEvent)({
|
|
1325
|
+
component: "daemon",
|
|
1326
|
+
event: "daemon.cli_command",
|
|
1327
|
+
message: "ouro CLI command invoked",
|
|
1328
|
+
meta: { kind: command.kind },
|
|
1329
|
+
});
|
|
1330
|
+
if (command.kind === "help") {
|
|
1331
|
+
const text = command.command
|
|
1332
|
+
? ((0, cli_help_1.getCommandHelp)(command.command) ?? `Unknown command: ${command.command}\n\n${(0, cli_help_1.getGroupedHelp)()}`)
|
|
1333
|
+
: (0, cli_help_1.getGroupedHelp)();
|
|
1334
|
+
deps.writeStdout(text);
|
|
1335
|
+
return text;
|
|
1336
|
+
}
|
|
1337
|
+
if (command.kind === "bluebubbles.replay") {
|
|
1338
|
+
const { replayBlueBubblesMessage, formatBlueBubblesReplayText } = await Promise.resolve().then(() => __importStar(require("../../senses/bluebubbles/replay")));
|
|
1339
|
+
const replay = await replayBlueBubblesMessage({
|
|
1340
|
+
agentName: command.agent,
|
|
1341
|
+
messageGuid: command.messageGuid,
|
|
1342
|
+
eventType: command.eventType,
|
|
1343
|
+
});
|
|
1344
|
+
const text = command.json
|
|
1345
|
+
? JSON.stringify(replay, null, 2)
|
|
1346
|
+
: formatBlueBubblesReplayText(replay);
|
|
1347
|
+
deps.writeStdout(text);
|
|
1348
|
+
return text;
|
|
1349
|
+
}
|
|
1350
|
+
if (command.kind === "daemon.up") {
|
|
1351
|
+
// ── dev mode cleanup: delete dev-config.json so the wrapper stops dispatching to dev repo ──
|
|
1352
|
+
/* v8 ignore start -- dev-config cleanup: requires real filesystem state @preserve */
|
|
1353
|
+
try {
|
|
1354
|
+
const devConfigPath = getDevConfigPath();
|
|
1355
|
+
if (fs.existsSync(devConfigPath)) {
|
|
1356
|
+
fs.unlinkSync(devConfigPath);
|
|
1357
|
+
}
|
|
1358
|
+
}
|
|
1359
|
+
catch { /* best effort */ }
|
|
1360
|
+
/* v8 ignore stop */
|
|
1361
|
+
// ── dev mode delegation: ouro up from a dev repo delegates to installed binary ──
|
|
1362
|
+
// Only runs when detectMode is explicitly injected (via createDefaultOuroCliDeps or tests)
|
|
1363
|
+
if (deps.detectMode) {
|
|
1364
|
+
const runtimeMode = deps.detectMode();
|
|
1365
|
+
if (runtimeMode === "dev") {
|
|
1366
|
+
/* v8 ignore next -- defensive: getInstalledBinaryPath always injected in tests @preserve */
|
|
1367
|
+
const installedBinary = deps.getInstalledBinaryPath ? deps.getInstalledBinaryPath() : null;
|
|
1368
|
+
if (installedBinary) {
|
|
1369
|
+
deps.writeStdout("delegating to installed ouro...");
|
|
1370
|
+
/* v8 ignore next 3 -- defensive: execInstalledBinary always injected; missing branch unreachable @preserve */
|
|
1371
|
+
if (deps.execInstalledBinary) {
|
|
1372
|
+
deps.execInstalledBinary(installedBinary, args);
|
|
1373
|
+
}
|
|
1374
|
+
/* v8 ignore next 2 -- unreachable after exec replaces process @preserve */
|
|
1375
|
+
return "";
|
|
1376
|
+
}
|
|
1377
|
+
const message = "no installed version found. run: npx @ouro.bot/cli@alpha";
|
|
1378
|
+
deps.writeStdout(message);
|
|
1379
|
+
return message;
|
|
1380
|
+
}
|
|
1381
|
+
}
|
|
1382
|
+
const linkedVersionBeforeUp = deps.getCurrentCliVersion?.() ?? null;
|
|
1383
|
+
const progress = new up_progress_1.UpProgress({ write: deps.writeStdout, isTTY: false });
|
|
1384
|
+
// ── versioned CLI update check ──
|
|
1385
|
+
if (deps.checkForCliUpdate) {
|
|
1386
|
+
progress.startPhase("update check");
|
|
1387
|
+
let pendingReExec = false;
|
|
1388
|
+
try {
|
|
1389
|
+
const updateResult = await deps.checkForCliUpdate();
|
|
1390
|
+
if (updateResult.available && updateResult.latestVersion) {
|
|
1391
|
+
/* v8 ignore next -- fallback: getCurrentCliVersion always injected in tests @preserve */
|
|
1392
|
+
const currentVersion = linkedVersionBeforeUp ?? "unknown";
|
|
1393
|
+
await deps.installCliVersion(updateResult.latestVersion);
|
|
1394
|
+
deps.activateCliVersion(updateResult.latestVersion);
|
|
1395
|
+
progress.completePhase("update check", `installed ${updateResult.latestVersion}`);
|
|
1396
|
+
const changelogCommand = (0, ouro_version_manager_1.buildChangelogCommand)(currentVersion, updateResult.latestVersion);
|
|
1397
|
+
/* v8 ignore next -- buildChangelogCommand is non-null when an actual newer version is installed @preserve */
|
|
1398
|
+
if (changelogCommand) {
|
|
1399
|
+
deps.writeStdout(`review changes with: ${changelogCommand}`);
|
|
1400
|
+
}
|
|
1401
|
+
pendingReExec = true;
|
|
1402
|
+
}
|
|
1403
|
+
/* v8 ignore start -- update check error: tested via daemon-cli-update-flow.test.ts @preserve */
|
|
1404
|
+
}
|
|
1405
|
+
catch (error) {
|
|
1406
|
+
(0, runtime_1.emitNervesEvent)({
|
|
1407
|
+
level: "warn",
|
|
1408
|
+
component: "daemon",
|
|
1409
|
+
event: "daemon.cli_update_check_error",
|
|
1410
|
+
message: "CLI update check failed",
|
|
1411
|
+
meta: { error: error instanceof Error ? error.message : /* v8 ignore next -- defensive: non-Error catch branch @preserve */ String(error) },
|
|
1412
|
+
});
|
|
1413
|
+
}
|
|
1414
|
+
/* v8 ignore stop */
|
|
1415
|
+
if (pendingReExec) {
|
|
1416
|
+
progress.end();
|
|
1417
|
+
deps.reExecFromNewVersion(args);
|
|
1418
|
+
}
|
|
1419
|
+
else {
|
|
1420
|
+
progress.completePhase("update check", "up to date");
|
|
1421
|
+
}
|
|
1422
|
+
}
|
|
1423
|
+
progress.startPhase("system setup");
|
|
1424
|
+
await performSystemSetup(deps);
|
|
1425
|
+
progress.completePhase("system setup");
|
|
1426
|
+
// Track whether we've already printed the "ouro updated to" message
|
|
1427
|
+
// this turn so the bundle-meta-fallback path below doesn't double-print.
|
|
1428
|
+
// There are three independent paths that can detect "the binary just
|
|
1429
|
+
// got newer":
|
|
1430
|
+
// 1. checkForCliUpdate found a newer version on npm (above) — this
|
|
1431
|
+
// path always re-execs, so the duplicate-detect runs in a
|
|
1432
|
+
// different process and the in-process tracker doesn't apply.
|
|
1433
|
+
// 2. ensureCurrentVersionInstalled (called from performSystemSetup)
|
|
1434
|
+
// flipped the CurrentVersion symlink because the running package
|
|
1435
|
+
// version is newer than what the symlink pointed at. This is the
|
|
1436
|
+
// common npx path.
|
|
1437
|
+
// 3. bundle-meta.json's stored runtime version differs from the
|
|
1438
|
+
// running version (fallback for when path 2 couldn't activate
|
|
1439
|
+
// but the binary is still newer).
|
|
1440
|
+
//
|
|
1441
|
+
// Verified live on 2026-04-08: npx download triggered both path 2 and
|
|
1442
|
+
// path 3 in the same process and the user saw the message printed twice.
|
|
1443
|
+
// Path 3's existing `linkedVersionBeforeUp !== currentVersion` guard
|
|
1444
|
+
// catches the cross-process re-exec case (path 1) but not the
|
|
1445
|
+
// in-process double-fire case (path 2 + path 3 in the same process).
|
|
1446
|
+
let printedUpdateMessage = false;
|
|
1447
|
+
const linkedVersionAfterSetup = deps.getCurrentCliVersion?.() ?? null;
|
|
1448
|
+
const runtimeVersion = (0, bundle_manifest_1.getPackageVersion)();
|
|
1449
|
+
if (linkedVersionBeforeUp && linkedVersionBeforeUp !== runtimeVersion && linkedVersionAfterSetup === runtimeVersion) {
|
|
1450
|
+
deps.writeStdout(`ouro updated to ${runtimeVersion} (was ${linkedVersionBeforeUp})`);
|
|
1451
|
+
const changelogCommand = (0, ouro_version_manager_1.buildChangelogCommand)(linkedVersionBeforeUp, runtimeVersion);
|
|
1452
|
+
if (changelogCommand) {
|
|
1453
|
+
deps.writeStdout(`review changes with: ${changelogCommand}`);
|
|
1454
|
+
}
|
|
1455
|
+
printedUpdateMessage = true;
|
|
1456
|
+
}
|
|
1457
|
+
// Run update hooks before starting daemon so user sees the output
|
|
1458
|
+
(0, update_hooks_1.registerUpdateHook)(bundle_meta_1.bundleMetaHook);
|
|
1459
|
+
(0, update_hooks_1.registerUpdateHook)(agent_config_v2_1.agentConfigV2Hook);
|
|
1460
|
+
const bundlesRoot = (0, identity_1.getAgentBundlesRoot)();
|
|
1461
|
+
const currentVersion = (0, bundle_manifest_1.getPackageVersion)();
|
|
1462
|
+
// Snapshot the previous CLI version from the first bundle-meta before
|
|
1463
|
+
// hooks overwrite it. This detects when npx downloaded a newer CLI.
|
|
1464
|
+
const previousCliVersion = (0, cli_defaults_1.readFirstBundleMetaVersion)(bundlesRoot);
|
|
1465
|
+
const updateSummary = await (0, update_hooks_1.applyPendingUpdates)(bundlesRoot, currentVersion);
|
|
1466
|
+
// Notify about CLI binary update (npx downloaded a new version).
|
|
1467
|
+
// Skip when the symlink already points to the running version — that
|
|
1468
|
+
// means path 1 (checkForCliUpdate + reExecFromNewVersion) already
|
|
1469
|
+
// printed the update message before re-exec. Also skip when path 2
|
|
1470
|
+
// (the symlink-flip detector above) already printed in this same
|
|
1471
|
+
// process — otherwise npx invocations print the message twice.
|
|
1472
|
+
/* v8 ignore start -- CLI update detection: tested via daemon-cli-version-detect.test.ts @preserve */
|
|
1473
|
+
if (!printedUpdateMessage
|
|
1474
|
+
&& previousCliVersion
|
|
1475
|
+
&& previousCliVersion !== currentVersion
|
|
1476
|
+
&& linkedVersionBeforeUp !== currentVersion) {
|
|
1477
|
+
deps.writeStdout(`ouro updated to ${currentVersion} (was ${previousCliVersion})`);
|
|
1478
|
+
const changelogCommand = (0, ouro_version_manager_1.buildChangelogCommand)(previousCliVersion, currentVersion);
|
|
1479
|
+
/* v8 ignore next -- buildChangelogCommand is non-null when previous/current runtime versions differ @preserve */
|
|
1480
|
+
if (changelogCommand) {
|
|
1481
|
+
deps.writeStdout(`review changes with: ${changelogCommand}`);
|
|
1482
|
+
}
|
|
1483
|
+
}
|
|
1484
|
+
/* v8 ignore stop */
|
|
1485
|
+
if (updateSummary.updated.length > 0) {
|
|
1486
|
+
const agents = updateSummary.updated.map((e) => e.agent);
|
|
1487
|
+
const from = updateSummary.updated[0].from;
|
|
1488
|
+
const to = updateSummary.updated[0].to;
|
|
1489
|
+
const fromStr = from ? ` (was ${from})` : "";
|
|
1490
|
+
const count = agents.length;
|
|
1491
|
+
progress.startPhase("agent updates");
|
|
1492
|
+
progress.completePhase("agent updates", `${count} agent${count === 1 ? "" : "s"} to runtime ${to}${fromStr}`);
|
|
1493
|
+
}
|
|
1494
|
+
// ── stale bundle pruning ──
|
|
1495
|
+
const prunedBundles = (0, stale_bundle_prune_1.pruneStaleEphemeralBundles)({ bundlesRoot: deps.bundlesRoot });
|
|
1496
|
+
if (prunedBundles.length > 0) {
|
|
1497
|
+
progress.startPhase("bundle cleanup");
|
|
1498
|
+
progress.completePhase("bundle cleanup", `pruned ${prunedBundles.length} stale bundle${prunedBundles.length === 1 ? "" : "s"}`);
|
|
1499
|
+
}
|
|
1500
|
+
progress.startPhase("starting daemon");
|
|
1501
|
+
const daemonResult = await ensureDaemonRunning({
|
|
1502
|
+
...deps,
|
|
1503
|
+
reportDaemonStartupPhase: (label) => {
|
|
1504
|
+
;
|
|
1505
|
+
progress.announceStep?.(label);
|
|
1506
|
+
},
|
|
1507
|
+
});
|
|
1508
|
+
if (daemonResult.alreadyRunning) {
|
|
1509
|
+
progress.startPhase("provider checks");
|
|
1510
|
+
const providerDegraded = await checkAlreadyRunningAgentProviders(deps);
|
|
1511
|
+
daemonResult.stability = mergeStartupStability(daemonResult.stability, providerDegraded);
|
|
1512
|
+
progress.completePhase("provider checks", providerDegraded.length > 0 ? `${providerDegraded.length} degraded` : "ok");
|
|
1513
|
+
}
|
|
1514
|
+
progress.end();
|
|
1515
|
+
deps.writeStdout(daemonResult.message);
|
|
1516
|
+
// Interactive repair for degraded agents (Unit 5) — skipped by --no-repair (Unit 6)
|
|
1517
|
+
if (daemonResult.stability?.degraded && daemonResult.stability.degraded.length > 0) {
|
|
1518
|
+
if (command.noRepair) {
|
|
1519
|
+
// --no-repair: write degraded summary and skip interactive repair
|
|
1520
|
+
deps.writeStdout("degraded agents:");
|
|
1521
|
+
for (const d of daemonResult.stability.degraded) {
|
|
1522
|
+
deps.writeStdout(` ${d.agent}: ${d.errorReason}`);
|
|
1523
|
+
if (d.fixHint) {
|
|
1524
|
+
deps.writeStdout(` fix: ${d.fixHint}`);
|
|
1525
|
+
}
|
|
1526
|
+
}
|
|
1527
|
+
(0, runtime_1.emitNervesEvent)({
|
|
1528
|
+
level: "warn",
|
|
1529
|
+
component: "daemon",
|
|
1530
|
+
event: "daemon.no_repair_degraded_summary",
|
|
1531
|
+
message: "degraded agents detected with --no-repair, skipping interactive repair",
|
|
1532
|
+
meta: { degradedCount: daemonResult.stability.degraded.length },
|
|
1533
|
+
});
|
|
1534
|
+
}
|
|
1535
|
+
else {
|
|
1536
|
+
await (0, agentic_repair_1.runAgenticRepair)(daemonResult.stability.degraded, {
|
|
1537
|
+
/* v8 ignore start -- production provider discovery wiring @preserve */
|
|
1538
|
+
discoverWorkingProvider: async () => {
|
|
1539
|
+
const { discoverWorkingProvider: discover } = await Promise.resolve().then(() => __importStar(require("./provider-discovery")));
|
|
1540
|
+
const { discoverExistingCredentials } = await Promise.resolve().then(() => __importStar(require("./cli-defaults")));
|
|
1541
|
+
const { pingProvider } = await Promise.resolve().then(() => __importStar(require("../provider-ping")));
|
|
1542
|
+
return discover({
|
|
1543
|
+
discoverExistingCredentials,
|
|
1544
|
+
pingProvider: pingProvider,
|
|
1545
|
+
env: process.env,
|
|
1546
|
+
secretsRoot: deps.secretsRoot ?? `${process.env["HOME"]}/.agentsecrets`,
|
|
1547
|
+
});
|
|
1548
|
+
},
|
|
1549
|
+
createProviderRuntime: agentic_repair_1.createAgenticDiagnosisProviderRuntime,
|
|
1550
|
+
readDaemonLogsTail: () => {
|
|
1551
|
+
try {
|
|
1552
|
+
const fs = require("node:fs");
|
|
1553
|
+
const path = require("node:path");
|
|
1554
|
+
const logsDir = path.join(process.env["HOME"] ?? "", ".agentstate", "daemon", "logs");
|
|
1555
|
+
const files = fs.readdirSync(logsDir).filter((f) => f.endsWith(".log")).sort();
|
|
1556
|
+
if (files.length === 0)
|
|
1557
|
+
return "(no daemon logs found)";
|
|
1558
|
+
const lastLog = fs.readFileSync(path.join(logsDir, files[files.length - 1]), "utf8");
|
|
1559
|
+
const lines = lastLog.split("\n");
|
|
1560
|
+
return lines.slice(-50).join("\n");
|
|
1561
|
+
}
|
|
1562
|
+
catch {
|
|
1563
|
+
return "(unable to read daemon logs)";
|
|
1564
|
+
}
|
|
1565
|
+
},
|
|
1566
|
+
/* v8 ignore stop */
|
|
1567
|
+
runInteractiveRepair: interactive_repair_1.runInteractiveRepair,
|
|
1568
|
+
promptInput: deps.promptInput ?? (async () => "n"),
|
|
1569
|
+
writeStdout: deps.writeStdout,
|
|
1570
|
+
runAuthFlow: async (agent, providerOverride) => {
|
|
1571
|
+
const { config } = (0, auth_flow_1.readAgentConfigForAgent)(agent, deps.bundlesRoot);
|
|
1572
|
+
const provider = providerOverride ?? config.humanFacing.provider;
|
|
1573
|
+
/* v8 ignore next -- tests always inject runAuthFlow; default is for production @preserve */
|
|
1574
|
+
const authRunner = deps.runAuthFlow ?? (await Promise.resolve().then(() => __importStar(require("../auth/auth-flow")))).runRuntimeAuthFlow;
|
|
1575
|
+
await authRunner({ agentName: agent, provider, promptInput: deps.promptInput });
|
|
1576
|
+
},
|
|
1577
|
+
});
|
|
1578
|
+
}
|
|
1579
|
+
}
|
|
1580
|
+
// Persist boot startup AFTER daemon is running — bootstrap is safe now
|
|
1581
|
+
// because the daemon socket exists, so launchd's KeepAlive registers
|
|
1582
|
+
// for crash recovery without starting a competing process.
|
|
1583
|
+
if (deps.ensureDaemonBootPersistence) {
|
|
1584
|
+
try {
|
|
1585
|
+
await Promise.resolve(deps.ensureDaemonBootPersistence(deps.socketPath));
|
|
1586
|
+
}
|
|
1587
|
+
catch (error) {
|
|
1588
|
+
(0, runtime_1.emitNervesEvent)({
|
|
1589
|
+
level: "warn",
|
|
1590
|
+
component: "daemon",
|
|
1591
|
+
event: "daemon.system_setup_launchd_error",
|
|
1592
|
+
message: "failed to persist daemon boot startup",
|
|
1593
|
+
meta: { error: error instanceof Error ? error.message : String(error), socketPath: deps.socketPath },
|
|
1594
|
+
});
|
|
1595
|
+
}
|
|
1596
|
+
}
|
|
1597
|
+
return daemonResult.message;
|
|
1598
|
+
}
|
|
1599
|
+
if (command.kind === "daemon.dev") {
|
|
1600
|
+
/* v8 ignore next -- defensive: existsSync always injected in tests @preserve */
|
|
1601
|
+
const checkExists = deps.existsSync ?? fs.existsSync;
|
|
1602
|
+
/* v8 ignore next -- repo resolution dispatched to v8-ignored helper @preserve */
|
|
1603
|
+
let repoCwd = resolveDevRepoCwd(command, checkExists, deps);
|
|
1604
|
+
let entryPath = path.join(repoCwd, "dist", "heart", "daemon", "daemon-entry.js");
|
|
1605
|
+
if (!checkExists(entryPath) || !checkExists(path.join(repoCwd, ".git"))) {
|
|
1606
|
+
if (command.repoPath) {
|
|
1607
|
+
// Explicit --repo-path didn't have a valid repo — error
|
|
1608
|
+
const message = `no harness repo found at ${repoCwd}. run npm run build first.`;
|
|
1609
|
+
deps.writeStdout(message);
|
|
1610
|
+
return message;
|
|
1611
|
+
}
|
|
1612
|
+
/* v8 ignore start -- auto-clone: interactive prompt + existing repo discovery + real git/npm @preserve */
|
|
1613
|
+
const defaultClonePath = path.join(os.homedir(), "Projects", "ouroboros");
|
|
1614
|
+
if (checkExists(path.join(defaultClonePath, ".git"))) {
|
|
1615
|
+
deps.writeStdout(`found existing repo at ${defaultClonePath}`);
|
|
1616
|
+
try {
|
|
1617
|
+
repoCwd = resolveClonePath({ clonePath: defaultClonePath }, checkExists, deps);
|
|
1618
|
+
entryPath = path.join(repoCwd, "dist", "heart", "daemon", "daemon-entry.js");
|
|
1619
|
+
}
|
|
1620
|
+
catch (err) {
|
|
1621
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
1622
|
+
deps.writeStdout(message);
|
|
1623
|
+
return message;
|
|
1624
|
+
}
|
|
1625
|
+
}
|
|
1626
|
+
else if (deps.promptInput) {
|
|
1627
|
+
deps.writeStdout("no harness repo found.");
|
|
1628
|
+
const answer = await deps.promptInput(`already have a checkout? enter its path, or press enter to clone to ${defaultClonePath}: `);
|
|
1629
|
+
const cloneTarget = answer.trim() || defaultClonePath;
|
|
1630
|
+
try {
|
|
1631
|
+
repoCwd = resolveClonePath({ clonePath: cloneTarget }, checkExists, deps);
|
|
1632
|
+
entryPath = path.join(repoCwd, "dist", "heart", "daemon", "daemon-entry.js");
|
|
1633
|
+
}
|
|
1634
|
+
catch (err) {
|
|
1635
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
1636
|
+
deps.writeStdout(message);
|
|
1637
|
+
return message;
|
|
1638
|
+
}
|
|
1639
|
+
}
|
|
1640
|
+
else {
|
|
1641
|
+
const message = `no harness repo found. run: ouro dev --repo-path /path/to/ouroboros`;
|
|
1642
|
+
deps.writeStdout(message);
|
|
1643
|
+
return message;
|
|
1644
|
+
}
|
|
1645
|
+
/* v8 ignore stop */
|
|
1646
|
+
}
|
|
1647
|
+
// Auto-build: always rebuild in dev mode so dist/ matches source
|
|
1648
|
+
/* v8 ignore start -- dev auto-build: execSync in repo cwd, tested manually @preserve */
|
|
1649
|
+
deps.writeStdout(`building from ${repoCwd}...`);
|
|
1650
|
+
try {
|
|
1651
|
+
(0, child_process_1.execSync)("npm run build", { cwd: repoCwd, stdio: "inherit" });
|
|
1652
|
+
entryPath = path.join(repoCwd, "dist", "heart", "daemon", "daemon-entry.js");
|
|
1653
|
+
}
|
|
1654
|
+
catch {
|
|
1655
|
+
const message = `build failed in ${repoCwd}. fix compilation errors and retry.`;
|
|
1656
|
+
deps.writeStdout(message);
|
|
1657
|
+
return message;
|
|
1658
|
+
}
|
|
1659
|
+
/* v8 ignore stop */
|
|
1660
|
+
/* v8 ignore start -- defensive: ensureDaemonBootPersistence always injected in tests @preserve */
|
|
1661
|
+
if (deps.ensureDaemonBootPersistence) {
|
|
1662
|
+
try {
|
|
1663
|
+
await Promise.resolve(deps.ensureDaemonBootPersistence(deps.socketPath));
|
|
1664
|
+
/* v8 ignore next -- defensive: boot persistence error should not block dev mode @preserve */
|
|
1665
|
+
}
|
|
1666
|
+
catch (error) {
|
|
1667
|
+
(0, runtime_1.emitNervesEvent)({
|
|
1668
|
+
level: "warn",
|
|
1669
|
+
component: "daemon",
|
|
1670
|
+
event: "daemon.dev_boot_persistence_error",
|
|
1671
|
+
message: "failed to persist daemon boot startup in dev mode",
|
|
1672
|
+
meta: { error: error instanceof Error ? error.message : String(error), socketPath: deps.socketPath },
|
|
1673
|
+
});
|
|
1674
|
+
}
|
|
1675
|
+
/* v8 ignore stop */
|
|
1676
|
+
}
|
|
1677
|
+
// Disable launchd KeepAlive before killing — prevents the installed daemon from respawning
|
|
1678
|
+
/* v8 ignore start -- dev launchd disable: requires real launchctl + plist on disk @preserve */
|
|
1679
|
+
const launchdDevDeps = {
|
|
1680
|
+
exec: (cmd) => { (0, child_process_1.execSync)(cmd); },
|
|
1681
|
+
existsFile: (p) => fs.existsSync(p),
|
|
1682
|
+
removeFile: (p) => { try {
|
|
1683
|
+
fs.unlinkSync(p);
|
|
1684
|
+
}
|
|
1685
|
+
catch { /* best effort */ } },
|
|
1686
|
+
homeDir: os.homedir(),
|
|
1687
|
+
userUid: process.getuid?.() ?? 0,
|
|
1688
|
+
};
|
|
1689
|
+
if ((0, launchd_1.isDaemonInstalled)(launchdDevDeps)) {
|
|
1690
|
+
(0, launchd_1.uninstallLaunchAgent)(launchdDevDeps);
|
|
1691
|
+
deps.writeStdout("disabled launchd auto-restart for dev mode");
|
|
1692
|
+
}
|
|
1693
|
+
/* v8 ignore stop */
|
|
1694
|
+
// Always force-restart in dev mode — you rebuilt, you want this code running
|
|
1695
|
+
/* v8 ignore start -- dev force-restart: socket alive/stop/spawn tested via integration; tests inject mocks @preserve */
|
|
1696
|
+
const alive = await deps.checkSocketAlive(deps.socketPath);
|
|
1697
|
+
if (alive) {
|
|
1698
|
+
try {
|
|
1699
|
+
await deps.sendCommand(deps.socketPath, { kind: "daemon.stop" });
|
|
1700
|
+
}
|
|
1701
|
+
catch { /* already stopping */ }
|
|
1702
|
+
}
|
|
1703
|
+
deps.cleanupStaleSocket(deps.socketPath);
|
|
1704
|
+
const devEntry = path.join(repoCwd, "dist", "heart", "daemon", "daemon-entry.js");
|
|
1705
|
+
const startDevDaemon = deps.startDaemonProcess === (await Promise.resolve().then(() => __importStar(require("./cli-defaults")))).defaultStartDaemonProcess
|
|
1706
|
+
? async (sp) => {
|
|
1707
|
+
const child = (0, child_process_1.spawn)("node", [devEntry, "--socket", sp], { detached: true, stdio: "ignore" });
|
|
1708
|
+
child.unref();
|
|
1709
|
+
return { pid: child.pid ?? null };
|
|
1710
|
+
}
|
|
1711
|
+
: deps.startDaemonProcess;
|
|
1712
|
+
/* v8 ignore stop */
|
|
1713
|
+
const started = await startDevDaemon(deps.socketPath);
|
|
1714
|
+
/* v8 ignore next -- defensive: pid is null only when spawn fails silently @preserve */
|
|
1715
|
+
const message = `daemon running in dev mode from ${repoCwd} (pid ${started.pid ?? "unknown"})\nrun 'ouro up' to return to production mode`;
|
|
1716
|
+
deps.writeStdout(message);
|
|
1717
|
+
return message;
|
|
1718
|
+
}
|
|
1719
|
+
// ── rollback command (local, no daemon socket needed for symlinks) ──
|
|
1720
|
+
/* v8 ignore start -- rollback/versions: tested via daemon-cli-rollback/versions tests @preserve */
|
|
1721
|
+
if (command.kind === "rollback") {
|
|
1722
|
+
const currentVersion = deps.getCurrentCliVersion?.() ?? "unknown";
|
|
1723
|
+
if (command.version) {
|
|
1724
|
+
// Rollback to a specific version
|
|
1725
|
+
const installed = deps.listCliVersions?.() ?? [];
|
|
1726
|
+
if (!installed.includes(command.version)) {
|
|
1727
|
+
try {
|
|
1728
|
+
await deps.installCliVersion(command.version);
|
|
1729
|
+
}
|
|
1730
|
+
catch (error) {
|
|
1731
|
+
const message = `failed to install version ${command.version}: ${error instanceof Error ? error.message : /* v8 ignore next -- defensive: non-Error catch branch @preserve */ String(error)}`;
|
|
1732
|
+
deps.writeStdout(message);
|
|
1733
|
+
return message;
|
|
1734
|
+
}
|
|
1735
|
+
}
|
|
1736
|
+
deps.activateCliVersion(command.version);
|
|
1737
|
+
}
|
|
1738
|
+
else {
|
|
1739
|
+
// Rollback to previous version
|
|
1740
|
+
const previousVersion = deps.getPreviousCliVersion?.();
|
|
1741
|
+
if (!previousVersion) {
|
|
1742
|
+
const message = "no previous version to roll back to";
|
|
1743
|
+
deps.writeStdout(message);
|
|
1744
|
+
return message;
|
|
1745
|
+
}
|
|
1746
|
+
deps.activateCliVersion(previousVersion);
|
|
1747
|
+
command = { ...command, version: previousVersion };
|
|
1748
|
+
}
|
|
1749
|
+
// Stop daemon (non-fatal if not running)
|
|
1750
|
+
try {
|
|
1751
|
+
await deps.sendCommand(deps.socketPath, { kind: "daemon.stop" });
|
|
1752
|
+
}
|
|
1753
|
+
catch {
|
|
1754
|
+
// Daemon may not be running — that's fine
|
|
1755
|
+
}
|
|
1756
|
+
const message = `rolled back to ${command.version} (was ${currentVersion})`;
|
|
1757
|
+
deps.writeStdout(message);
|
|
1758
|
+
return message;
|
|
1759
|
+
}
|
|
1760
|
+
// ── versions command (local install list + published update truth, no daemon socket needed) ──
|
|
1761
|
+
if (command.kind === "versions") {
|
|
1762
|
+
const versions = deps.listCliVersions?.() ?? [];
|
|
1763
|
+
const current = deps.getCurrentCliVersion?.();
|
|
1764
|
+
const previous = deps.getPreviousCliVersion?.();
|
|
1765
|
+
const localSection = versions.length === 0
|
|
1766
|
+
? "no versions installed"
|
|
1767
|
+
: versions.map((v) => {
|
|
1768
|
+
let line = v;
|
|
1769
|
+
if (v === current)
|
|
1770
|
+
line += " * current";
|
|
1771
|
+
if (v === previous)
|
|
1772
|
+
line += " (previous)";
|
|
1773
|
+
return line;
|
|
1774
|
+
}).join("\n");
|
|
1775
|
+
const sections = [localSection];
|
|
1776
|
+
if (deps.checkForCliUpdate) {
|
|
1777
|
+
try {
|
|
1778
|
+
const updateResult = await deps.checkForCliUpdate();
|
|
1779
|
+
if (updateResult.latestVersion) {
|
|
1780
|
+
sections.push(`published alpha: ${updateResult.latestVersion} (${updateResult.available ? "update available" : "up to date"})`);
|
|
1781
|
+
}
|
|
1782
|
+
else if (updateResult.error) {
|
|
1783
|
+
sections.push(`published alpha: unavailable (${updateResult.error})`);
|
|
1784
|
+
}
|
|
1785
|
+
}
|
|
1786
|
+
catch (err) {
|
|
1787
|
+
const reason = err instanceof Error ? err.message : String(err);
|
|
1788
|
+
sections.push(`published alpha: unavailable (${reason})`);
|
|
1789
|
+
}
|
|
1790
|
+
}
|
|
1791
|
+
const message = sections.join("\n\n");
|
|
1792
|
+
deps.writeStdout(message);
|
|
1793
|
+
return message;
|
|
1794
|
+
}
|
|
1795
|
+
/* v8 ignore stop */
|
|
1796
|
+
if (command.kind === "daemon.logs" && deps.tailLogs) {
|
|
1797
|
+
deps.tailLogs();
|
|
1798
|
+
return "";
|
|
1799
|
+
}
|
|
1800
|
+
if (command.kind === "daemon.logs.prune") {
|
|
1801
|
+
if (!deps.pruneDaemonLogs) {
|
|
1802
|
+
const message = "logs prune unavailable (dep not wired)";
|
|
1803
|
+
deps.writeStdout(message);
|
|
1804
|
+
return message;
|
|
1805
|
+
}
|
|
1806
|
+
const result = deps.pruneDaemonLogs();
|
|
1807
|
+
const message = `compacted ${result.filesCompacted} file${result.filesCompacted === 1 ? "" : "s"}, freed ${result.bytesFreed} bytes`;
|
|
1808
|
+
deps.writeStdout(message);
|
|
1809
|
+
return message;
|
|
1810
|
+
}
|
|
1811
|
+
if (command.kind === "outlook") {
|
|
1812
|
+
let status;
|
|
1813
|
+
try {
|
|
1814
|
+
status = await deps.sendCommand(deps.socketPath, { kind: "daemon.status" });
|
|
1815
|
+
/* v8 ignore start — error path: daemon not running */
|
|
1816
|
+
}
|
|
1817
|
+
catch {
|
|
1818
|
+
const message = "daemon unavailable — start with `ouro up` first";
|
|
1819
|
+
deps.writeStdout(message);
|
|
1820
|
+
return message;
|
|
1821
|
+
}
|
|
1822
|
+
/* v8 ignore stop */
|
|
1823
|
+
const payload = (0, cli_render_1.parseStatusPayload)(status.data);
|
|
1824
|
+
/* v8 ignore start -- ?? branch: outlookUrl always present in test fixtures */
|
|
1825
|
+
const outlookUrl = payload?.overview.outlookUrl ?? "unavailable";
|
|
1826
|
+
/* v8 ignore stop */
|
|
1827
|
+
if (!command.json) {
|
|
1828
|
+
deps.writeStdout(outlookUrl);
|
|
1829
|
+
return outlookUrl;
|
|
1830
|
+
}
|
|
1831
|
+
/* v8 ignore start — error path: outlook URL not available */
|
|
1832
|
+
if (outlookUrl === "unavailable") {
|
|
1833
|
+
deps.writeStdout(outlookUrl);
|
|
1834
|
+
return outlookUrl;
|
|
1835
|
+
}
|
|
1836
|
+
/* v8 ignore stop */
|
|
1837
|
+
/* v8 ignore start -- ?? branch: tests always inject fetchImpl */
|
|
1838
|
+
const fetchImpl = deps.fetchImpl ?? fetch;
|
|
1839
|
+
/* v8 ignore stop */
|
|
1840
|
+
const response = await fetchImpl(`${outlookUrl}/api/machine`);
|
|
1841
|
+
const data = await response.json();
|
|
1842
|
+
const text = JSON.stringify(data, null, 2);
|
|
1843
|
+
deps.writeStdout(text);
|
|
1844
|
+
return text;
|
|
1845
|
+
}
|
|
1846
|
+
// ── hook: handle Claude Code lifecycle hooks ──
|
|
1847
|
+
/* v8 ignore start -- hook handler: reads real stdin, sends to real daemon @preserve */
|
|
1848
|
+
if (command.kind === "hook") {
|
|
1849
|
+
let stdinData = "";
|
|
1850
|
+
try {
|
|
1851
|
+
stdinData = require("fs").readFileSync(0, "utf-8");
|
|
1852
|
+
}
|
|
1853
|
+
catch { /* no stdin */ }
|
|
1854
|
+
let event = {};
|
|
1855
|
+
try {
|
|
1856
|
+
event = JSON.parse(stdinData);
|
|
1857
|
+
}
|
|
1858
|
+
catch { /* malformed */ }
|
|
1859
|
+
const eventType = command.event;
|
|
1860
|
+
const sessionId = event.session_id ?? "unknown";
|
|
1861
|
+
const cwd = event.cwd ?? "";
|
|
1862
|
+
// Build notification content based on event type
|
|
1863
|
+
let content;
|
|
1864
|
+
if (eventType === "session-start") {
|
|
1865
|
+
const model = event.model ?? "";
|
|
1866
|
+
const source = event.source ?? "";
|
|
1867
|
+
content = `[Claude Code session started: ${sessionId}, cwd: ${cwd}${model ? `, model: ${model}` : ""}${source ? `, source: ${source}` : ""}]`;
|
|
1868
|
+
}
|
|
1869
|
+
else if (eventType === "stop") {
|
|
1870
|
+
const lastMsg = event.last_assistant_message ?? "";
|
|
1871
|
+
content = `[Claude Code session ended: ${sessionId}${lastMsg ? `, last: ${lastMsg.slice(0, 200)}` : ""}]`;
|
|
1872
|
+
}
|
|
1873
|
+
else if (eventType === "post-tool-use") {
|
|
1874
|
+
const toolName = event.tool_name ?? "";
|
|
1875
|
+
content = `[Claude Code used ${toolName} in session ${sessionId}]`;
|
|
1876
|
+
}
|
|
1877
|
+
else {
|
|
1878
|
+
content = `[Claude Code hook: ${eventType} in session ${sessionId}]`;
|
|
1879
|
+
}
|
|
1880
|
+
// Send to the specific agent configured for this hook. Short-circuit
|
|
1881
|
+
// when the daemon socket file doesn't exist — otherwise every
|
|
1882
|
+
// Claude Code lifecycle event during a daemon-down window logs two
|
|
1883
|
+
// ENOENT errors in ouro.ndjson (one for message.send, one for
|
|
1884
|
+
// inner.wake) which makes it hard to read the log around outages.
|
|
1885
|
+
// The hook is best-effort: dropping notifications when the daemon
|
|
1886
|
+
// is down is the correct behavior; we just don't want to log spam
|
|
1887
|
+
// about it.
|
|
1888
|
+
if (require("fs").existsSync(deps.socketPath)) {
|
|
1889
|
+
try {
|
|
1890
|
+
await deps.sendCommand(deps.socketPath, { kind: "message.send", from: `claude-code:${sessionId}`, to: command.agent, content }).catch(() => { });
|
|
1891
|
+
await deps.sendCommand(deps.socketPath, { kind: "inner.wake", agent: command.agent }).catch(() => { });
|
|
1892
|
+
}
|
|
1893
|
+
catch { /* daemon not running — silent */ }
|
|
1894
|
+
}
|
|
1895
|
+
else {
|
|
1896
|
+
(0, runtime_1.emitNervesEvent)({
|
|
1897
|
+
component: "daemon",
|
|
1898
|
+
event: "daemon.hook_skipped_no_socket",
|
|
1899
|
+
message: "claude code hook skipped — daemon socket missing",
|
|
1900
|
+
meta: { socketPath: deps.socketPath, eventType, agent: command.agent },
|
|
1901
|
+
});
|
|
1902
|
+
}
|
|
1903
|
+
// Output for Claude Code hook system
|
|
1904
|
+
deps.writeStdout(JSON.stringify({ continue: true }));
|
|
1905
|
+
return JSON.stringify({ continue: true });
|
|
1906
|
+
}
|
|
1907
|
+
/* v8 ignore stop */
|
|
1908
|
+
// ── setup: configure dev tool integration ──
|
|
1909
|
+
if (command.kind === "setup") {
|
|
1910
|
+
const { tool, agent: setupAgent } = command;
|
|
1911
|
+
const sourceRoot = (0, identity_1.getRepoRoot)();
|
|
1912
|
+
const runtimeMode = (0, runtime_mode_1.detectRuntimeMode)(sourceRoot);
|
|
1913
|
+
const mcpServeCommand = runtimeMode === "dev"
|
|
1914
|
+
? `node ${path.join(sourceRoot, "dist", "heart", "daemon", "ouro-bot-entry.js")} mcp-serve --agent ${setupAgent}`
|
|
1915
|
+
: `ouro mcp-serve --agent ${setupAgent}`;
|
|
1916
|
+
if (tool === "claude-code") {
|
|
1917
|
+
// 1. Register MCP server with Claude Code
|
|
1918
|
+
const mcpAddCmd = `claude mcp add ouro-${setupAgent} -s user -- ${mcpServeCommand}`;
|
|
1919
|
+
(0, child_process_1.execSync)(mcpAddCmd, { stdio: "pipe" });
|
|
1920
|
+
// 2. Write hooks config to ~/.claude/settings.json
|
|
1921
|
+
const settingsPath = path.join(os.homedir(), ".claude", "settings.json");
|
|
1922
|
+
let settings = {};
|
|
1923
|
+
if (fs.existsSync(settingsPath)) {
|
|
1924
|
+
try {
|
|
1925
|
+
settings = JSON.parse(fs.readFileSync(settingsPath, "utf-8"));
|
|
1926
|
+
}
|
|
1927
|
+
catch { /* start fresh */ }
|
|
1928
|
+
}
|
|
1929
|
+
// Use `ouro hook <event>` — resolves the right code based on dev vs installed mode.
|
|
1930
|
+
// Bare `ouro` works because ouro is on PATH via ~/.ouro-cli/bin/.
|
|
1931
|
+
settings.hooks = {
|
|
1932
|
+
...(settings.hooks ?? {}),
|
|
1933
|
+
SessionStart: [{ hooks: [{ type: "command", command: `ouro hook session-start --agent ${setupAgent}`, timeout: 5 }] }],
|
|
1934
|
+
Stop: [{ hooks: [{ type: "command", command: `ouro hook stop --agent ${setupAgent}`, timeout: 5 }] }],
|
|
1935
|
+
PostToolUse: [{ matcher: "Bash|Edit|Write", hooks: [{ type: "command", command: `ouro hook post-tool-use --agent ${setupAgent}`, timeout: 5 }] }],
|
|
1936
|
+
};
|
|
1937
|
+
fs.mkdirSync(path.dirname(settingsPath), { recursive: true });
|
|
1938
|
+
fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 2));
|
|
1939
|
+
(0, runtime_1.emitNervesEvent)({
|
|
1940
|
+
component: "daemon",
|
|
1941
|
+
event: "daemon.setup_complete",
|
|
1942
|
+
message: "dev tool setup complete",
|
|
1943
|
+
meta: { tool, agent: setupAgent, runtimeMode },
|
|
1944
|
+
});
|
|
1945
|
+
// 3. Write conversation formatting instructions to ~/.claude/CLAUDE.md
|
|
1946
|
+
const claudeMdPath = path.join(os.homedir(), ".claude", "CLAUDE.md");
|
|
1947
|
+
const agentInstructions = `\n## Agent conversations (ouro)\nWhen using MCP \`send_message\` to talk to an ouro agent, format the exchange clearly:\n- Before the tool call, briefly say what you're asking/telling the agent\n- After the response, quote the agent's reply in a blockquote, then add your reaction\n- Example: **Me → Agent:** "question" / > **Agent:** "response" / Your synthesis here\n`;
|
|
1948
|
+
let existingClaudeMd = "";
|
|
1949
|
+
if (fs.existsSync(claudeMdPath)) {
|
|
1950
|
+
existingClaudeMd = fs.readFileSync(claudeMdPath, "utf-8");
|
|
1951
|
+
}
|
|
1952
|
+
if (!existingClaudeMd.includes("Agent conversations (ouro)")) {
|
|
1953
|
+
fs.writeFileSync(claudeMdPath, existingClaudeMd + agentInstructions);
|
|
1954
|
+
}
|
|
1955
|
+
const message = `setup complete: claude-code + ${setupAgent}\n MCP server registered\n hooks configured\n conversation formatting instructions added`;
|
|
1956
|
+
deps.writeStdout(message);
|
|
1957
|
+
return message;
|
|
1958
|
+
}
|
|
1959
|
+
else {
|
|
1960
|
+
// tool === "codex" (parseSetupCommand validates tool, so this is the only remaining option)
|
|
1961
|
+
const mcpAddCmd = `codex mcp add ouro-${setupAgent} -- ${mcpServeCommand}`;
|
|
1962
|
+
(0, child_process_1.execSync)(mcpAddCmd, { stdio: "pipe" });
|
|
1963
|
+
(0, runtime_1.emitNervesEvent)({
|
|
1964
|
+
component: "daemon",
|
|
1965
|
+
event: "daemon.setup_complete",
|
|
1966
|
+
message: "dev tool setup complete",
|
|
1967
|
+
meta: { tool, agent: setupAgent, runtimeMode },
|
|
1968
|
+
});
|
|
1969
|
+
const message = `setup complete: codex + ${setupAgent}\n MCP server registered`;
|
|
1970
|
+
deps.writeStdout(message);
|
|
1971
|
+
return message;
|
|
1972
|
+
}
|
|
1973
|
+
}
|
|
1974
|
+
/* v8 ignore start — mcp-serve block binds to process.stdin/stdout; tested via mcp-server unit tests */
|
|
1975
|
+
// ── mcp-serve: start MCP server in-process on stdin/stdout ──
|
|
1976
|
+
if (command.kind === "mcp-serve") {
|
|
1977
|
+
const { createMcpServer } = await Promise.resolve().then(() => __importStar(require("../mcp/mcp-server")));
|
|
1978
|
+
const friendId = command.friendId ?? `local-${os.userInfo().username}`;
|
|
1979
|
+
const mcpSocketPath = command.socketOverride ?? deps.socketPath;
|
|
1980
|
+
const server = createMcpServer({
|
|
1981
|
+
agent: command.agent,
|
|
1982
|
+
friendId,
|
|
1983
|
+
socketPath: mcpSocketPath,
|
|
1984
|
+
stdin: process.stdin,
|
|
1985
|
+
stdout: process.stdout,
|
|
1986
|
+
});
|
|
1987
|
+
server.start();
|
|
1988
|
+
(0, runtime_1.emitNervesEvent)({
|
|
1989
|
+
component: "daemon",
|
|
1990
|
+
event: "daemon.mcp_serve_started",
|
|
1991
|
+
message: "MCP server started via CLI",
|
|
1992
|
+
meta: { agent: command.agent, friendId },
|
|
1993
|
+
});
|
|
1994
|
+
// Keep process alive until stdin closes
|
|
1995
|
+
await new Promise((resolve) => {
|
|
1996
|
+
process.stdin.on("end", () => {
|
|
1997
|
+
server.stop();
|
|
1998
|
+
resolve();
|
|
1999
|
+
});
|
|
2000
|
+
});
|
|
2001
|
+
return "";
|
|
2002
|
+
}
|
|
2003
|
+
/* v8 ignore stop */
|
|
2004
|
+
// ── mcp subcommands (routed through daemon socket) ──
|
|
2005
|
+
if (command.kind === "mcp.list" || command.kind === "mcp.call") {
|
|
2006
|
+
const daemonCommand = toDaemonCommand(command);
|
|
2007
|
+
let response;
|
|
2008
|
+
try {
|
|
2009
|
+
response = await deps.sendCommand(deps.socketPath, daemonCommand);
|
|
2010
|
+
}
|
|
2011
|
+
catch {
|
|
2012
|
+
const message = "daemon unavailable — start with `ouro up` first";
|
|
2013
|
+
deps.writeStdout(message);
|
|
2014
|
+
return message;
|
|
2015
|
+
}
|
|
2016
|
+
if (!response.ok) {
|
|
2017
|
+
const message = response.error ?? "unknown error";
|
|
2018
|
+
deps.writeStdout(message);
|
|
2019
|
+
return message;
|
|
2020
|
+
}
|
|
2021
|
+
const message = (0, cli_render_1.formatMcpResponse)(command, response);
|
|
2022
|
+
deps.writeStdout(message);
|
|
2023
|
+
return message;
|
|
2024
|
+
}
|
|
2025
|
+
// ── task subcommands (local, no daemon socket needed) ──
|
|
2026
|
+
if (command.kind === "task.board" || command.kind === "task.create" || command.kind === "task.update" ||
|
|
2027
|
+
command.kind === "task.show" || command.kind === "task.actionable" || command.kind === "task.deps" ||
|
|
2028
|
+
command.kind === "task.sessions" || command.kind === "task.fix") {
|
|
2029
|
+
/* v8 ignore start -- production default: requires full identity setup @preserve */
|
|
2030
|
+
const taskMod = deps.taskModule ?? (0, tasks_1.getTaskModule)();
|
|
2031
|
+
/* v8 ignore stop */
|
|
2032
|
+
const message = executeTaskCommand(command, taskMod);
|
|
2033
|
+
deps.writeStdout(message);
|
|
2034
|
+
return message;
|
|
2035
|
+
}
|
|
2036
|
+
// ── reminder subcommands (local, no daemon socket needed) ──
|
|
2037
|
+
if (command.kind === "reminder.create") {
|
|
2038
|
+
/* v8 ignore start -- production default: requires full identity setup @preserve */
|
|
2039
|
+
const taskMod = deps.taskModule ?? (0, tasks_1.getTaskModule)();
|
|
2040
|
+
/* v8 ignore stop */
|
|
2041
|
+
const message = executeReminderCommand(command, taskMod);
|
|
2042
|
+
deps.writeStdout(message);
|
|
2043
|
+
return message;
|
|
2044
|
+
}
|
|
2045
|
+
// ── habit subcommands (local, no daemon socket needed) ──
|
|
2046
|
+
if (command.kind === "habit.list" || command.kind === "habit.create") {
|
|
2047
|
+
const { parseHabitFile, renderHabitFile } = await Promise.resolve().then(() => __importStar(require("../habits/habit-parser")));
|
|
2048
|
+
/* v8 ignore start -- production default: uses real bundle root @preserve */
|
|
2049
|
+
const agentName = command.agent ?? (0, identity_1.getAgentName)();
|
|
2050
|
+
const bundleRoot = deps.agentBundleRoot ?? path.join((0, identity_1.getAgentBundlesRoot)(), `${agentName}.ouro`);
|
|
2051
|
+
/* v8 ignore stop */
|
|
2052
|
+
const habitsDir = path.join(bundleRoot, "habits");
|
|
2053
|
+
if (command.kind === "habit.list") {
|
|
2054
|
+
let files;
|
|
2055
|
+
try {
|
|
2056
|
+
files = fs.readdirSync(habitsDir).filter((f) => f.endsWith(".md") && f !== "README.md");
|
|
2057
|
+
}
|
|
2058
|
+
catch {
|
|
2059
|
+
const message = "no habits found";
|
|
2060
|
+
deps.writeStdout(message);
|
|
2061
|
+
return message;
|
|
2062
|
+
}
|
|
2063
|
+
if (files.length === 0) {
|
|
2064
|
+
const message = "no habits found";
|
|
2065
|
+
deps.writeStdout(message);
|
|
2066
|
+
return message;
|
|
2067
|
+
}
|
|
2068
|
+
const lines = [];
|
|
2069
|
+
for (const file of files) {
|
|
2070
|
+
const fileContent = fs.readFileSync(path.join(habitsDir, file), "utf-8");
|
|
2071
|
+
const habit = parseHabitFile(fileContent, path.join(habitsDir, file));
|
|
2072
|
+
const lastRunStr = habit.lastRun ?? "never";
|
|
2073
|
+
lines.push(`${habit.name} cadence=${habit.cadence ?? "none"} status=${habit.status} lastRun=${lastRunStr}`);
|
|
2074
|
+
}
|
|
2075
|
+
const message = lines.join("\n");
|
|
2076
|
+
deps.writeStdout(message);
|
|
2077
|
+
return message;
|
|
2078
|
+
}
|
|
2079
|
+
// habit.create
|
|
2080
|
+
const filePath = path.join(habitsDir, `${command.name}.md`);
|
|
2081
|
+
if (fs.existsSync(filePath)) {
|
|
2082
|
+
const message = `error: habit '${command.name}' already exists`;
|
|
2083
|
+
deps.writeStdout(message);
|
|
2084
|
+
return message;
|
|
2085
|
+
}
|
|
2086
|
+
fs.mkdirSync(habitsDir, { recursive: true });
|
|
2087
|
+
const now = new Date().toISOString();
|
|
2088
|
+
const habitContent = renderHabitFile({
|
|
2089
|
+
title: command.name,
|
|
2090
|
+
cadence: command.cadence ?? "null",
|
|
2091
|
+
status: "active",
|
|
2092
|
+
lastRun: now,
|
|
2093
|
+
created: now,
|
|
2094
|
+
}, `Habit: ${command.name}`);
|
|
2095
|
+
fs.writeFileSync(filePath, habitContent, "utf-8");
|
|
2096
|
+
const message = `created: ${filePath}`;
|
|
2097
|
+
deps.writeStdout(message);
|
|
2098
|
+
return message;
|
|
2099
|
+
}
|
|
2100
|
+
// ── friend subcommands (local, no daemon socket needed) ──
|
|
2101
|
+
if (command.kind === "friend.list" || command.kind === "friend.show" || command.kind === "friend.create" ||
|
|
2102
|
+
command.kind === "friend.update" || command.kind === "friend.link" || command.kind === "friend.unlink") {
|
|
2103
|
+
/* v8 ignore start -- production default: requires full identity setup @preserve */
|
|
2104
|
+
let store = deps.friendStore;
|
|
2105
|
+
if (!store) {
|
|
2106
|
+
// Derive agent-scoped friends dir from --agent flag or link/unlink's agent field
|
|
2107
|
+
const agentName = ("agent" in command && command.agent) ? command.agent : undefined;
|
|
2108
|
+
const friendsDir = agentName
|
|
2109
|
+
? path.join((0, identity_1.getAgentBundlesRoot)(), `${agentName}.ouro`, "friends")
|
|
2110
|
+
: path.join((0, identity_1.getAgentBundlesRoot)(), "friends");
|
|
2111
|
+
store = new store_file_1.FileFriendStore(friendsDir);
|
|
2112
|
+
}
|
|
2113
|
+
/* v8 ignore stop */
|
|
2114
|
+
const message = await executeFriendCommand(command, store);
|
|
2115
|
+
deps.writeStdout(message);
|
|
2116
|
+
return message;
|
|
2117
|
+
}
|
|
2118
|
+
// ── provider state commands (local, no daemon socket needed) ──
|
|
2119
|
+
if (command.kind === "provider.use") {
|
|
2120
|
+
return executeProviderUse(command, deps);
|
|
2121
|
+
}
|
|
2122
|
+
if (command.kind === "provider.check") {
|
|
2123
|
+
return executeProviderCheck(command, deps);
|
|
2124
|
+
}
|
|
2125
|
+
if (command.kind === "provider.status") {
|
|
2126
|
+
return executeProviderStatus(command, deps);
|
|
2127
|
+
}
|
|
2128
|
+
// ── auth (local, no daemon socket needed) ──
|
|
2129
|
+
if (command.kind === "auth.run") {
|
|
2130
|
+
const provider = command.provider ?? (0, auth_flow_1.readAgentConfigForAgent)(command.agent, deps.bundlesRoot).config.humanFacing.provider;
|
|
2131
|
+
/* v8 ignore next -- tests always inject runAuthFlow; default is for production @preserve */
|
|
2132
|
+
const authRunner = deps.runAuthFlow ?? (await Promise.resolve().then(() => __importStar(require("../auth/auth-flow")))).runRuntimeAuthFlow;
|
|
2133
|
+
const result = await authRunner({
|
|
2134
|
+
agentName: command.agent,
|
|
2135
|
+
provider,
|
|
2136
|
+
promptInput: deps.promptInput,
|
|
2137
|
+
});
|
|
2138
|
+
const credentials = (result.credentials ?? {});
|
|
2139
|
+
const split = (0, provider_credential_pool_1.splitProviderCredentialFields)(provider, credentials);
|
|
2140
|
+
if (Object.keys(split.credentials).length > 0 || Object.keys(split.config).length > 0) {
|
|
2141
|
+
(0, provider_credential_pool_1.upsertProviderCredential)({
|
|
2142
|
+
homeDir: providerCliHomeDir(deps),
|
|
2143
|
+
provider,
|
|
2144
|
+
credentials: split.credentials,
|
|
2145
|
+
config: split.config,
|
|
2146
|
+
provenance: {
|
|
2147
|
+
source: "auth-flow",
|
|
2148
|
+
contributedByAgent: command.agent,
|
|
2149
|
+
},
|
|
2150
|
+
now: providerCliNow(deps),
|
|
2151
|
+
});
|
|
2152
|
+
}
|
|
2153
|
+
// Behavior: ouro auth stores credentials only — does NOT switch provider.
|
|
2154
|
+
// Use `ouro auth switch` to change the active provider.
|
|
2155
|
+
deps.writeStdout(result.message);
|
|
2156
|
+
// Verify the credentials actually work by pinging the provider
|
|
2157
|
+
/* v8 ignore start -- integration: real API ping after auth @preserve */
|
|
2158
|
+
try {
|
|
2159
|
+
const { secrets } = (0, auth_flow_1.loadAgentSecrets)(command.agent, { secretsRoot: deps.secretsRoot });
|
|
2160
|
+
const status = await verifyProviderCredentials(provider, secrets.providers);
|
|
2161
|
+
deps.writeStdout(`${provider}: ${status}`);
|
|
2162
|
+
}
|
|
2163
|
+
catch {
|
|
2164
|
+
// Verification failure is non-blocking — credentials were saved regardless
|
|
2165
|
+
}
|
|
2166
|
+
/* v8 ignore stop */
|
|
2167
|
+
return result.message;
|
|
2168
|
+
}
|
|
2169
|
+
// ── auth verify (local, no daemon socket needed) ──
|
|
2170
|
+
/* v8 ignore start -- auth verify/switch: tested in daemon-cli.test.ts but v8 traces differ in CI @preserve */
|
|
2171
|
+
if (command.kind === "auth.verify") {
|
|
2172
|
+
const { secrets } = (0, auth_flow_1.loadAgentSecrets)(command.agent, { secretsRoot: deps.secretsRoot });
|
|
2173
|
+
const providers = secrets.providers;
|
|
2174
|
+
if (command.provider) {
|
|
2175
|
+
const status = await verifyProviderCredentials(command.provider, providers);
|
|
2176
|
+
const message = `${command.provider}: ${status}`;
|
|
2177
|
+
deps.writeStdout(message);
|
|
2178
|
+
return message;
|
|
2179
|
+
}
|
|
2180
|
+
const lines = [];
|
|
2181
|
+
for (const p of Object.keys(providers)) {
|
|
2182
|
+
const status = await verifyProviderCredentials(p, providers);
|
|
2183
|
+
lines.push(`${p}: ${status}`);
|
|
2184
|
+
}
|
|
2185
|
+
const message = lines.join("\n");
|
|
2186
|
+
deps.writeStdout(message);
|
|
2187
|
+
return message;
|
|
2188
|
+
}
|
|
2189
|
+
// ── auth switch (local, no daemon socket needed) ──
|
|
2190
|
+
if (command.kind === "auth.switch") {
|
|
2191
|
+
return executeLegacyAuthSwitch(command, deps);
|
|
2192
|
+
}
|
|
2193
|
+
/* v8 ignore stop */
|
|
2194
|
+
// ── config models (local, no daemon socket needed) ──
|
|
2195
|
+
/* v8 ignore start -- config models: tested via daemon-cli.test.ts @preserve */
|
|
2196
|
+
if (command.kind === "config.models") {
|
|
2197
|
+
const { config } = (0, auth_flow_1.readAgentConfigForAgent)(command.agent, deps.bundlesRoot);
|
|
2198
|
+
const provider = config.humanFacing.provider;
|
|
2199
|
+
if (provider !== "github-copilot") {
|
|
2200
|
+
const message = `model listing not available for ${provider} — check provider documentation.`;
|
|
2201
|
+
deps.writeStdout(message);
|
|
2202
|
+
return message;
|
|
2203
|
+
}
|
|
2204
|
+
const { secrets } = (0, auth_flow_1.loadAgentSecrets)(command.agent, { secretsRoot: deps.secretsRoot });
|
|
2205
|
+
const ghConfig = secrets.providers["github-copilot"];
|
|
2206
|
+
if (!ghConfig.githubToken || !ghConfig.baseUrl) {
|
|
2207
|
+
throw new Error(`github-copilot credentials not configured. Run \`ouro auth --agent ${command.agent} --provider github-copilot\` first.`);
|
|
2208
|
+
}
|
|
2209
|
+
const fetchFn = deps.fetchImpl ?? fetch;
|
|
2210
|
+
const models = await listGithubCopilotModels(ghConfig.baseUrl, ghConfig.githubToken, fetchFn);
|
|
2211
|
+
if (models.length === 0) {
|
|
2212
|
+
const message = "no models found";
|
|
2213
|
+
deps.writeStdout(message);
|
|
2214
|
+
return message;
|
|
2215
|
+
}
|
|
2216
|
+
const lines = ["available models:"];
|
|
2217
|
+
for (const m of models) {
|
|
2218
|
+
const caps = m.capabilities?.length ? ` (${m.capabilities.join(", ")})` : "";
|
|
2219
|
+
lines.push(` ${m.id}${caps}`);
|
|
2220
|
+
}
|
|
2221
|
+
const message = lines.join("\n");
|
|
2222
|
+
deps.writeStdout(message);
|
|
2223
|
+
return message;
|
|
2224
|
+
}
|
|
2225
|
+
/* v8 ignore stop */
|
|
2226
|
+
// ── config model (local, no daemon socket needed) ──
|
|
2227
|
+
/* v8 ignore start -- config model: tested via daemon-cli.test.ts @preserve */
|
|
2228
|
+
if (command.kind === "config.model") {
|
|
2229
|
+
return executeLegacyConfigModel(command, deps);
|
|
2230
|
+
}
|
|
2231
|
+
/* v8 ignore stop */
|
|
2232
|
+
// ── whoami (local, no daemon socket needed) ──
|
|
2233
|
+
if (command.kind === "whoami") {
|
|
2234
|
+
if (command.agent) {
|
|
2235
|
+
const agentRoot = path.join((0, identity_1.getAgentBundlesRoot)(), `${command.agent}.ouro`);
|
|
2236
|
+
const message = [
|
|
2237
|
+
`agent: ${command.agent}`,
|
|
2238
|
+
`home: ${agentRoot}`,
|
|
2239
|
+
`bones: ${(0, runtime_metadata_1.getRuntimeMetadata)().version}`,
|
|
2240
|
+
].join("\n");
|
|
2241
|
+
deps.writeStdout(message);
|
|
2242
|
+
return message;
|
|
2243
|
+
}
|
|
2244
|
+
/* v8 ignore start -- production default: requires full identity setup @preserve */
|
|
2245
|
+
try {
|
|
2246
|
+
const info = deps.whoamiInfo
|
|
2247
|
+
? deps.whoamiInfo()
|
|
2248
|
+
: {
|
|
2249
|
+
agentName: (0, identity_1.getAgentName)(),
|
|
2250
|
+
homePath: path.join((0, identity_1.getAgentBundlesRoot)(), `${(0, identity_1.getAgentName)()}.ouro`),
|
|
2251
|
+
bonesVersion: (0, runtime_metadata_1.getRuntimeMetadata)().version,
|
|
2252
|
+
};
|
|
2253
|
+
const message = [
|
|
2254
|
+
`agent: ${info.agentName}`,
|
|
2255
|
+
`home: ${info.homePath}`,
|
|
2256
|
+
`bones: ${info.bonesVersion}`,
|
|
2257
|
+
].join("\n");
|
|
2258
|
+
deps.writeStdout(message);
|
|
2259
|
+
return message;
|
|
2260
|
+
}
|
|
2261
|
+
catch {
|
|
2262
|
+
const message = "error: no agent context — use --agent <name> to specify";
|
|
2263
|
+
deps.writeStdout(message);
|
|
2264
|
+
return message;
|
|
2265
|
+
}
|
|
2266
|
+
/* v8 ignore stop */
|
|
2267
|
+
}
|
|
2268
|
+
// ── changelog (local, no daemon socket needed) ──
|
|
2269
|
+
if (command.kind === "changelog") {
|
|
2270
|
+
try {
|
|
2271
|
+
const changelogPath = deps.getChangelogPath
|
|
2272
|
+
? deps.getChangelogPath()
|
|
2273
|
+
: (0, bundle_manifest_1.getChangelogPath)();
|
|
2274
|
+
const raw = fs.readFileSync(changelogPath, "utf-8");
|
|
2275
|
+
const parsed = JSON.parse(raw);
|
|
2276
|
+
const entries = Array.isArray(parsed) ? parsed : (parsed.versions ?? []);
|
|
2277
|
+
let filtered = entries;
|
|
2278
|
+
if (command.from) {
|
|
2279
|
+
const fromVersion = command.from;
|
|
2280
|
+
filtered = entries.filter((e) => semver.valid(e.version) && semver.gt(e.version, fromVersion));
|
|
2281
|
+
}
|
|
2282
|
+
if (filtered.length === 0) {
|
|
2283
|
+
const message = "no changelog entries found.";
|
|
2284
|
+
deps.writeStdout(message);
|
|
2285
|
+
return message;
|
|
2286
|
+
}
|
|
2287
|
+
const lines = [];
|
|
2288
|
+
for (const entry of filtered) {
|
|
2289
|
+
lines.push(`## ${entry.version}${entry.date ? ` (${entry.date})` : ""}`);
|
|
2290
|
+
if (entry.changes) {
|
|
2291
|
+
for (const change of entry.changes) {
|
|
2292
|
+
lines.push(`- ${change}`);
|
|
2293
|
+
}
|
|
2294
|
+
}
|
|
2295
|
+
lines.push("");
|
|
2296
|
+
}
|
|
2297
|
+
const message = lines.join("\n").trim();
|
|
2298
|
+
deps.writeStdout(message);
|
|
2299
|
+
return message;
|
|
2300
|
+
}
|
|
2301
|
+
catch {
|
|
2302
|
+
const message = "no changelog entries found.";
|
|
2303
|
+
deps.writeStdout(message);
|
|
2304
|
+
return message;
|
|
2305
|
+
}
|
|
2306
|
+
}
|
|
2307
|
+
// ── thoughts (local, no daemon socket needed) ──
|
|
2308
|
+
if (command.kind === "thoughts") {
|
|
2309
|
+
try {
|
|
2310
|
+
const agentName = command.agent ?? (0, identity_1.getAgentName)();
|
|
2311
|
+
/* v8 ignore next -- production fallback: tests always inject bundlesRoot via createTmpBundle @preserve */
|
|
2312
|
+
const bundlesRoot = deps.bundlesRoot ?? (0, identity_1.getAgentBundlesRoot)();
|
|
2313
|
+
const agentRoot = path.join(bundlesRoot, `${agentName}.ouro`);
|
|
2314
|
+
const sessionFilePath = (0, thoughts_1.getInnerDialogSessionPath)(agentRoot);
|
|
2315
|
+
if (command.json) {
|
|
2316
|
+
try {
|
|
2317
|
+
const raw = fs.readFileSync(sessionFilePath, "utf-8");
|
|
2318
|
+
deps.writeStdout(raw);
|
|
2319
|
+
return raw;
|
|
2320
|
+
}
|
|
2321
|
+
catch {
|
|
2322
|
+
const message = "no inner dialog session found";
|
|
2323
|
+
deps.writeStdout(message);
|
|
2324
|
+
return message;
|
|
2325
|
+
}
|
|
2326
|
+
}
|
|
2327
|
+
const turns = (0, thoughts_1.parseInnerDialogSession)(sessionFilePath);
|
|
2328
|
+
const message = (0, thoughts_1.formatThoughtTurns)(turns, command.last ?? 10);
|
|
2329
|
+
deps.writeStdout(message);
|
|
2330
|
+
if (command.follow) {
|
|
2331
|
+
deps.writeStdout("\n\n--- following (ctrl+c to stop) ---\n");
|
|
2332
|
+
/* v8 ignore start -- callback tested via followThoughts unit tests @preserve */
|
|
2333
|
+
const stop = (0, thoughts_1.followThoughts)(sessionFilePath, (formatted) => {
|
|
2334
|
+
deps.writeStdout("\n" + formatted);
|
|
2335
|
+
});
|
|
2336
|
+
/* v8 ignore stop */
|
|
2337
|
+
// Block until process exit; cleanup watcher on SIGINT/SIGTERM
|
|
2338
|
+
return new Promise((resolve) => {
|
|
2339
|
+
const cleanup = () => { stop(); resolve(message); };
|
|
2340
|
+
process.once("SIGINT", cleanup);
|
|
2341
|
+
process.once("SIGTERM", cleanup);
|
|
2342
|
+
});
|
|
2343
|
+
}
|
|
2344
|
+
return message;
|
|
2345
|
+
}
|
|
2346
|
+
catch {
|
|
2347
|
+
const message = "error: no agent context — use --agent <name> to specify";
|
|
2348
|
+
deps.writeStdout(message);
|
|
2349
|
+
return message;
|
|
2350
|
+
}
|
|
2351
|
+
}
|
|
2352
|
+
// ── attention queue (local, no daemon socket needed) ──
|
|
2353
|
+
/* v8 ignore start -- CLI attention handler: requires real obligation store on disk @preserve */
|
|
2354
|
+
if (command.kind === "attention.list" || command.kind === "attention.show" || command.kind === "attention.history") {
|
|
2355
|
+
try {
|
|
2356
|
+
const agentName = command.agent ?? (0, identity_1.getAgentName)();
|
|
2357
|
+
const { listActiveReturnObligations, readReturnObligation } = await Promise.resolve().then(() => __importStar(require("../../arc/obligations")));
|
|
2358
|
+
if (command.kind === "attention.list") {
|
|
2359
|
+
const obligations = listActiveReturnObligations(agentName);
|
|
2360
|
+
if (obligations.length === 0) {
|
|
2361
|
+
const message = "nothing held — attention queue is empty";
|
|
2362
|
+
deps.writeStdout(message);
|
|
2363
|
+
return message;
|
|
2364
|
+
}
|
|
2365
|
+
const lines = obligations.map((o) => `[${o.id}] ${o.origin.friendId} via ${o.origin.channel}/${o.origin.key} — ${o.delegatedContent.slice(0, 60)}${o.delegatedContent.length > 60 ? "..." : ""} (${o.status})`);
|
|
2366
|
+
const message = lines.join("\n");
|
|
2367
|
+
deps.writeStdout(message);
|
|
2368
|
+
return message;
|
|
2369
|
+
}
|
|
2370
|
+
if (command.kind === "attention.show") {
|
|
2371
|
+
const obligation = readReturnObligation(agentName, command.id);
|
|
2372
|
+
if (!obligation) {
|
|
2373
|
+
const message = `no obligation found with id ${command.id}`;
|
|
2374
|
+
deps.writeStdout(message);
|
|
2375
|
+
return message;
|
|
2376
|
+
}
|
|
2377
|
+
const message = JSON.stringify(obligation, null, 2);
|
|
2378
|
+
deps.writeStdout(message);
|
|
2379
|
+
return message;
|
|
2380
|
+
}
|
|
2381
|
+
// attention.history: show returned obligations
|
|
2382
|
+
const { getReturnObligationsDir } = await Promise.resolve().then(() => __importStar(require("../../arc/obligations")));
|
|
2383
|
+
const obligationsDir = getReturnObligationsDir(agentName);
|
|
2384
|
+
let attEntries = [];
|
|
2385
|
+
try {
|
|
2386
|
+
attEntries = fs.readdirSync(obligationsDir);
|
|
2387
|
+
}
|
|
2388
|
+
catch { /* empty */ }
|
|
2389
|
+
const returned = attEntries
|
|
2390
|
+
.filter((e) => e.endsWith(".json"))
|
|
2391
|
+
.map((e) => { try {
|
|
2392
|
+
return JSON.parse(fs.readFileSync(path.join(obligationsDir, e), "utf-8"));
|
|
2393
|
+
}
|
|
2394
|
+
catch {
|
|
2395
|
+
return null;
|
|
2396
|
+
} })
|
|
2397
|
+
.filter((o) => o?.status === "returned")
|
|
2398
|
+
.sort((a, b) => (b.returnedAt ?? 0) - (a.returnedAt ?? 0))
|
|
2399
|
+
.slice(0, 20);
|
|
2400
|
+
if (returned.length === 0) {
|
|
2401
|
+
const message = "no surfacing history yet";
|
|
2402
|
+
deps.writeStdout(message);
|
|
2403
|
+
return message;
|
|
2404
|
+
}
|
|
2405
|
+
const lines = returned.map((o) => {
|
|
2406
|
+
const when = o.returnedAt ? new Date(o.returnedAt).toISOString() : "unknown";
|
|
2407
|
+
return `[${o.id}] → ${o.origin.friendId} via ${o.returnTarget ?? "unknown"} at ${when}`;
|
|
2408
|
+
});
|
|
2409
|
+
const message = lines.join("\n");
|
|
2410
|
+
deps.writeStdout(message);
|
|
2411
|
+
return message;
|
|
2412
|
+
}
|
|
2413
|
+
catch {
|
|
2414
|
+
const message = "error: no agent context — use --agent <name> to specify";
|
|
2415
|
+
deps.writeStdout(message);
|
|
2416
|
+
return message;
|
|
2417
|
+
}
|
|
2418
|
+
}
|
|
2419
|
+
/* v8 ignore stop */
|
|
2420
|
+
// ── inner dialog status (local, no daemon socket needed) ──
|
|
2421
|
+
/* v8 ignore start -- inner status handler: requires real agent state on disk @preserve */
|
|
2422
|
+
if (command.kind === "inner.status") {
|
|
2423
|
+
try {
|
|
2424
|
+
const agentName = command.agent ?? (0, identity_1.getAgentName)();
|
|
2425
|
+
const agentRoot = (0, identity_1.getAgentRoot)(agentName);
|
|
2426
|
+
const { buildInnerStatusOutput } = await Promise.resolve().then(() => __importStar(require("./inner-status")));
|
|
2427
|
+
const { sessionPath: getSessionPath } = await Promise.resolve().then(() => __importStar(require("../config")));
|
|
2428
|
+
const { parseCadenceToMs: parseCadenceMs, DEFAULT_CADENCE_MS } = await Promise.resolve().then(() => __importStar(require("./cadence")));
|
|
2429
|
+
const { parseFrontmatter } = await Promise.resolve().then(() => __importStar(require("../../repertoire/tasks/parser")));
|
|
2430
|
+
const { listActiveReturnObligations } = await Promise.resolve().then(() => __importStar(require("../../arc/obligations")));
|
|
2431
|
+
// Read runtime state
|
|
2432
|
+
const innerSessionPath = getSessionPath("inner-dialog", "inner", "session");
|
|
2433
|
+
const runtimeJsonPath = path.join(path.dirname(innerSessionPath), "runtime.json");
|
|
2434
|
+
let runtimeState = null;
|
|
2435
|
+
try {
|
|
2436
|
+
const raw = fs.readFileSync(runtimeJsonPath, "utf-8");
|
|
2437
|
+
runtimeState = JSON.parse(raw);
|
|
2438
|
+
}
|
|
2439
|
+
catch { /* missing or corrupt — will show "unknown" */ }
|
|
2440
|
+
// Read journal files
|
|
2441
|
+
const journalDir = path.join(agentRoot, "journal");
|
|
2442
|
+
let journalFiles = [];
|
|
2443
|
+
try {
|
|
2444
|
+
const journalEntries = fs.readdirSync(journalDir, { withFileTypes: true });
|
|
2445
|
+
journalFiles = journalEntries
|
|
2446
|
+
.filter((e) => e.isFile() && !e.name.startsWith("."))
|
|
2447
|
+
.map((e) => {
|
|
2448
|
+
const stat = fs.statSync(path.join(journalDir, e.name));
|
|
2449
|
+
return { name: e.name, mtimeMs: stat.mtimeMs };
|
|
2450
|
+
});
|
|
2451
|
+
}
|
|
2452
|
+
catch { /* missing dir — will show (empty) */ }
|
|
2453
|
+
// Read heartbeat cadence
|
|
2454
|
+
let heartbeat = null;
|
|
2455
|
+
try {
|
|
2456
|
+
const habitsDir = path.join(agentRoot, "habits");
|
|
2457
|
+
const heartbeatPath = path.join(habitsDir, "heartbeat.md");
|
|
2458
|
+
let cadenceMs = DEFAULT_CADENCE_MS;
|
|
2459
|
+
if (fs.existsSync(heartbeatPath)) {
|
|
2460
|
+
const heartbeatContent = fs.readFileSync(heartbeatPath, "utf-8");
|
|
2461
|
+
const lines = heartbeatContent.split(/\r?\n/);
|
|
2462
|
+
if (lines[0]?.trim() === "---") {
|
|
2463
|
+
const closing = lines.findIndex((line, index) => index > 0 && line.trim() === "---");
|
|
2464
|
+
if (closing !== -1) {
|
|
2465
|
+
const rawFrontmatter = lines.slice(1, closing).join("\n");
|
|
2466
|
+
const frontmatter = parseFrontmatter(rawFrontmatter);
|
|
2467
|
+
const parsedCadence = parseCadenceMs(frontmatter.cadence);
|
|
2468
|
+
if (parsedCadence !== null)
|
|
2469
|
+
cadenceMs = parsedCadence;
|
|
2470
|
+
}
|
|
2471
|
+
}
|
|
2472
|
+
}
|
|
2473
|
+
let lastCompletedAt = null;
|
|
2474
|
+
if (runtimeState?.lastCompletedAt) {
|
|
2475
|
+
const ms = new Date(runtimeState.lastCompletedAt).getTime();
|
|
2476
|
+
if (!Number.isNaN(ms))
|
|
2477
|
+
lastCompletedAt = ms;
|
|
2478
|
+
}
|
|
2479
|
+
heartbeat = { cadenceMs, lastCompletedAt };
|
|
2480
|
+
}
|
|
2481
|
+
catch { /* no habits — heartbeat unknown */ }
|
|
2482
|
+
// Attention count
|
|
2483
|
+
const activeObligations = listActiveReturnObligations(agentName);
|
|
2484
|
+
const message = buildInnerStatusOutput({
|
|
2485
|
+
agentName,
|
|
2486
|
+
runtimeState,
|
|
2487
|
+
journalFiles,
|
|
2488
|
+
heartbeat,
|
|
2489
|
+
attentionCount: activeObligations.length,
|
|
2490
|
+
now: Date.now(),
|
|
2491
|
+
});
|
|
2492
|
+
deps.writeStdout(message);
|
|
2493
|
+
return message;
|
|
2494
|
+
}
|
|
2495
|
+
catch {
|
|
2496
|
+
const message = "error: no agent context — use --agent <name> to specify";
|
|
2497
|
+
deps.writeStdout(message);
|
|
2498
|
+
return message;
|
|
2499
|
+
}
|
|
2500
|
+
}
|
|
2501
|
+
/* v8 ignore stop */
|
|
2502
|
+
// ── session list (local, no daemon socket needed) ──
|
|
2503
|
+
if (command.kind === "session.list") {
|
|
2504
|
+
/* v8 ignore start -- production default: requires full identity setup @preserve */
|
|
2505
|
+
const scanner = deps.scanSessions ?? (async () => []);
|
|
2506
|
+
/* v8 ignore stop */
|
|
2507
|
+
const sessions = await scanner();
|
|
2508
|
+
if (sessions.length === 0) {
|
|
2509
|
+
const message = "no active sessions";
|
|
2510
|
+
deps.writeStdout(message);
|
|
2511
|
+
return message;
|
|
2512
|
+
}
|
|
2513
|
+
const lines = sessions.map((s) => `${s.friendId} ${s.friendName} ${s.channel} ${s.lastActivity}`);
|
|
2514
|
+
const message = lines.join("\n");
|
|
2515
|
+
deps.writeStdout(message);
|
|
2516
|
+
return message;
|
|
2517
|
+
}
|
|
2518
|
+
if (command.kind === "chat.connect" && deps.startChat) {
|
|
2519
|
+
let agent = command.agent;
|
|
2520
|
+
// No agent specified — show selection
|
|
2521
|
+
/* v8 ignore start -- interactive agent selection: requires real promptInput + discovered agents @preserve */
|
|
2522
|
+
if (!agent) {
|
|
2523
|
+
const discovered = await Promise.resolve(deps.listDiscoveredAgents ? deps.listDiscoveredAgents() : (0, cli_defaults_1.defaultListDiscoveredAgents)());
|
|
2524
|
+
if (discovered.length === 0) {
|
|
2525
|
+
deps.writeStdout("no agents found — run `ouro` to hatch one");
|
|
2526
|
+
return "no agents found";
|
|
2527
|
+
}
|
|
2528
|
+
if (discovered.length === 1) {
|
|
2529
|
+
agent = discovered[0];
|
|
2530
|
+
}
|
|
2531
|
+
else if (deps.promptInput) {
|
|
2532
|
+
const prompt = `who do you want to talk to?\n${discovered.map((a, i) => `${i + 1}. ${a}`).join("\n")}\n`;
|
|
2533
|
+
const answer = await deps.promptInput(prompt);
|
|
2534
|
+
agent = discovered.includes(answer) ? answer : discovered[parseInt(answer, 10) - 1];
|
|
2535
|
+
if (!agent) {
|
|
2536
|
+
deps.writeStdout("invalid selection");
|
|
2537
|
+
return "invalid selection";
|
|
2538
|
+
}
|
|
2539
|
+
}
|
|
2540
|
+
else {
|
|
2541
|
+
const message = `who do you want to talk to? ${discovered.join(", ")} (use: ouro chat <agent>)`;
|
|
2542
|
+
deps.writeStdout(message);
|
|
2543
|
+
return message;
|
|
2544
|
+
}
|
|
2545
|
+
}
|
|
2546
|
+
/* v8 ignore stop */
|
|
2547
|
+
await ensureDaemonRunning(deps);
|
|
2548
|
+
// Check provider health before launching chat — fail fast with
|
|
2549
|
+
// actionable guidance instead of erroring mid-conversation.
|
|
2550
|
+
const health = await checkProviderHealthBeforeChat(agent, deps);
|
|
2551
|
+
if (!health.ok)
|
|
2552
|
+
return health.output;
|
|
2553
|
+
await deps.startChat(agent);
|
|
2554
|
+
return "";
|
|
2555
|
+
}
|
|
2556
|
+
if (command.kind === "hatch.start") {
|
|
2557
|
+
// Route through serpent guide when no explicit hatch args were provided
|
|
2558
|
+
const hasExplicitHatchArgs = !!(command.agentName || command.humanName || command.provider || command.credentials);
|
|
2559
|
+
if (deps.runSerpentGuide && !hasExplicitHatchArgs) {
|
|
2560
|
+
// System setup first — ouro command, subagents, UTI — before the interactive specialist
|
|
2561
|
+
await performSystemSetup(deps);
|
|
2562
|
+
const hatchlingName = await deps.runSerpentGuide();
|
|
2563
|
+
if (!hatchlingName) {
|
|
2564
|
+
return "";
|
|
2565
|
+
}
|
|
2566
|
+
await ensureDaemonRunning(deps);
|
|
2567
|
+
if (deps.startChat) {
|
|
2568
|
+
await deps.startChat(hatchlingName);
|
|
2569
|
+
}
|
|
2570
|
+
return "";
|
|
2571
|
+
}
|
|
2572
|
+
const hatchRunner = deps.runHatchFlow;
|
|
2573
|
+
if (!hatchRunner) {
|
|
2574
|
+
const response = await deps.sendCommand(deps.socketPath, { kind: "hatch.start" });
|
|
2575
|
+
const message = response.summary ?? response.message ?? (response.ok ? "ok" : `error: ${response.error ?? "unknown error"}`);
|
|
2576
|
+
deps.writeStdout(message);
|
|
2577
|
+
return message;
|
|
2578
|
+
}
|
|
2579
|
+
const hatchInput = await resolveHatchInput(command, deps);
|
|
2580
|
+
const result = await hatchRunner(hatchInput);
|
|
2581
|
+
await performSystemSetup(deps);
|
|
2582
|
+
const daemonResult = await ensureDaemonRunning(deps);
|
|
2583
|
+
if (deps.startChat) {
|
|
2584
|
+
await deps.startChat(hatchInput.agentName);
|
|
2585
|
+
return "";
|
|
2586
|
+
}
|
|
2587
|
+
const message = `hatched ${hatchInput.agentName} at ${result.bundleRoot} using specialist identity ${result.selectedIdentity}; ${daemonResult.message}`;
|
|
2588
|
+
deps.writeStdout(message);
|
|
2589
|
+
return message;
|
|
2590
|
+
}
|
|
2591
|
+
// ── doctor (local, no daemon socket needed) ──
|
|
2592
|
+
if (command.kind === "doctor") {
|
|
2593
|
+
const doctorDeps = {
|
|
2594
|
+
/* v8 ignore start -- thin fs wrappers tested via doctor.test.ts with injected deps @preserve */
|
|
2595
|
+
existsSync: (p) => fs.existsSync(p),
|
|
2596
|
+
readFileSync: (p) => fs.readFileSync(p, "utf-8"),
|
|
2597
|
+
readdirSync: (p) => fs.readdirSync(p),
|
|
2598
|
+
statSync: (p) => fs.statSync(p),
|
|
2599
|
+
/* v8 ignore stop */
|
|
2600
|
+
checkSocketAlive: deps.checkSocketAlive,
|
|
2601
|
+
fetchImpl: deps.fetchImpl ?? fetch,
|
|
2602
|
+
socketPath: deps.socketPath,
|
|
2603
|
+
bundlesRoot: deps.bundlesRoot ?? (0, identity_1.getAgentBundlesRoot)(),
|
|
2604
|
+
secretsRoot: deps.secretsRoot ?? path.join(os.homedir(), ".agentsecrets"),
|
|
2605
|
+
homedir: os.homedir(),
|
|
2606
|
+
envPath: process.env.PATH ?? "",
|
|
2607
|
+
};
|
|
2608
|
+
const doctorResult = await (0, doctor_1.runDoctorChecks)(doctorDeps);
|
|
2609
|
+
const output = (0, cli_render_doctor_1.formatDoctorOutput)(doctorResult);
|
|
2610
|
+
deps.writeStdout(output);
|
|
2611
|
+
(0, runtime_1.emitNervesEvent)({
|
|
2612
|
+
component: "daemon",
|
|
2613
|
+
event: "daemon.doctor_run",
|
|
2614
|
+
message: "ouro doctor completed",
|
|
2615
|
+
meta: { passed: doctorResult.summary.passed, warnings: doctorResult.summary.warnings, failed: doctorResult.summary.failed },
|
|
2616
|
+
});
|
|
2617
|
+
return output;
|
|
2618
|
+
}
|
|
2619
|
+
const daemonCommand = toDaemonCommand(command);
|
|
2620
|
+
let response;
|
|
2621
|
+
try {
|
|
2622
|
+
response = await deps.sendCommand(deps.socketPath, daemonCommand);
|
|
2623
|
+
}
|
|
2624
|
+
catch (error) {
|
|
2625
|
+
if (command.kind === "message.send") {
|
|
2626
|
+
const pendingPath = deps.fallbackPendingMessage(command);
|
|
2627
|
+
const message = `daemon unavailable; queued message fallback at ${pendingPath}`;
|
|
2628
|
+
deps.writeStdout(message);
|
|
2629
|
+
return message;
|
|
2630
|
+
}
|
|
2631
|
+
if (command.kind === "daemon.status" && (0, cli_render_1.isDaemonUnavailableError)(error)) {
|
|
2632
|
+
const message = (0, cli_render_1.daemonUnavailableStatusOutput)(deps.socketPath, deps.healthFilePath);
|
|
2633
|
+
deps.writeStdout(message);
|
|
2634
|
+
return message;
|
|
2635
|
+
}
|
|
2636
|
+
if (command.kind === "daemon.stop" && (0, cli_render_1.isDaemonUnavailableError)(error)) {
|
|
2637
|
+
const message = "daemon not running";
|
|
2638
|
+
deps.writeStdout(message);
|
|
2639
|
+
return message;
|
|
2640
|
+
}
|
|
2641
|
+
throw error;
|
|
2642
|
+
}
|
|
2643
|
+
const fallbackMessage = response.summary ?? response.message ?? (response.ok ? "ok" : `error: ${response.error ?? "unknown error"}`);
|
|
2644
|
+
const message = command.kind === "daemon.status"
|
|
2645
|
+
? (0, cli_render_1.formatDaemonStatusOutput)(response, fallbackMessage)
|
|
2646
|
+
: fallbackMessage;
|
|
2647
|
+
deps.writeStdout(message);
|
|
2648
|
+
return message;
|
|
2649
|
+
}
|