@ouro.bot/cli 0.1.0-alpha.32 → 0.1.0-alpha.320

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (303) hide show
  1. package/README.md +188 -190
  2. package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/agent.json +3 -2
  3. package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/SOUL.md +1 -1
  4. package/changelog.json +1917 -0
  5. package/dist/arc/attention-types.js +8 -0
  6. package/dist/arc/cares.js +140 -0
  7. package/dist/arc/episodes.js +117 -0
  8. package/dist/arc/intentions.js +133 -0
  9. package/dist/arc/json-store.js +117 -0
  10. package/dist/arc/obligations.js +237 -0
  11. package/dist/arc/packets.js +193 -0
  12. package/dist/arc/presence.js +185 -0
  13. package/dist/arc/task-lifecycle.js +65 -0
  14. package/dist/heart/active-work.js +832 -0
  15. package/dist/heart/agent-entry.js +37 -2
  16. package/dist/heart/attachments/image-normalize.js +194 -0
  17. package/dist/heart/attachments/materialize.js +97 -0
  18. package/dist/heart/attachments/originals.js +88 -0
  19. package/dist/heart/attachments/render.js +29 -0
  20. package/dist/heart/attachments/sources/adapter.js +2 -0
  21. package/dist/heart/attachments/sources/bluebubbles.js +156 -0
  22. package/dist/heart/attachments/sources/cli-local-file.js +78 -0
  23. package/dist/heart/attachments/sources/index.js +16 -0
  24. package/dist/heart/attachments/store.js +103 -0
  25. package/dist/heart/attachments/types.js +93 -0
  26. package/dist/heart/auth/auth-flow.js +456 -0
  27. package/dist/heart/bridges/manager.js +358 -0
  28. package/dist/heart/bridges/state-machine.js +135 -0
  29. package/dist/heart/bridges/store.js +123 -0
  30. package/dist/heart/bundle-state.js +168 -0
  31. package/dist/heart/commitments.js +111 -0
  32. package/dist/heart/config-registry.js +304 -0
  33. package/dist/heart/config.js +63 -30
  34. package/dist/heart/core.js +669 -195
  35. package/dist/heart/cross-chat-delivery.js +131 -0
  36. package/dist/heart/daemon/agent-config-check.js +149 -0
  37. package/dist/heart/daemon/agent-discovery.js +79 -3
  38. package/dist/heart/daemon/agent-service.js +360 -0
  39. package/dist/heart/daemon/agentic-repair.js +170 -0
  40. package/dist/heart/daemon/cadence.js +70 -0
  41. package/dist/heart/daemon/cli-defaults.js +596 -0
  42. package/dist/heart/daemon/cli-exec.js +2238 -0
  43. package/dist/heart/daemon/cli-help.js +306 -0
  44. package/dist/heart/daemon/cli-parse.js +824 -0
  45. package/dist/heart/daemon/cli-render-doctor.js +57 -0
  46. package/dist/heart/daemon/cli-render.js +506 -0
  47. package/dist/heart/daemon/cli-types.js +8 -0
  48. package/dist/heart/daemon/daemon-cli.js +29 -1171
  49. package/dist/heart/daemon/daemon-entry.js +333 -3
  50. package/dist/heart/daemon/daemon-health.js +137 -0
  51. package/dist/heart/daemon/daemon-runtime-sync.js +153 -12
  52. package/dist/heart/daemon/daemon-tombstone.js +236 -0
  53. package/dist/heart/daemon/daemon.js +751 -58
  54. package/dist/heart/daemon/doctor-types.js +8 -0
  55. package/dist/heart/daemon/doctor.js +322 -0
  56. package/dist/heart/daemon/health-monitor.js +66 -0
  57. package/dist/heart/daemon/hooks/agent-config-v2.js +33 -0
  58. package/dist/heart/daemon/hooks/bundle-meta.js +115 -1
  59. package/dist/heart/daemon/http-health-probe.js +80 -0
  60. package/dist/heart/daemon/inner-status.js +89 -0
  61. package/dist/heart/daemon/interactive-repair.js +69 -0
  62. package/dist/heart/daemon/launchd.js +46 -9
  63. package/dist/heart/daemon/log-tailer.js +82 -12
  64. package/dist/heart/daemon/logs-prune.js +105 -0
  65. package/dist/heart/daemon/message-router.js +17 -8
  66. package/dist/heart/daemon/os-cron-deps.js +134 -0
  67. package/dist/heart/daemon/ouro-bot-entry.js +1 -1
  68. package/dist/heart/daemon/process-manager.js +201 -0
  69. package/dist/heart/daemon/provider-discovery.js +105 -0
  70. package/dist/heart/daemon/pulse.js +463 -0
  71. package/dist/heart/daemon/run-hooks.js +2 -0
  72. package/dist/heart/daemon/runtime-logging.js +67 -16
  73. package/dist/heart/daemon/runtime-metadata.js +101 -0
  74. package/dist/heart/daemon/runtime-mode.js +67 -0
  75. package/dist/heart/daemon/safe-mode.js +161 -0
  76. package/dist/heart/daemon/sense-manager.js +72 -3
  77. package/dist/heart/daemon/session-id-resolver.js +131 -0
  78. package/dist/heart/daemon/skill-management-installer.js +94 -0
  79. package/dist/heart/daemon/socket-client.js +307 -0
  80. package/dist/heart/daemon/stale-bundle-prune.js +96 -0
  81. package/dist/heart/daemon/startup-tui.js +227 -0
  82. package/dist/heart/daemon/task-scheduler.js +3 -25
  83. package/dist/heart/daemon/thoughts.js +510 -0
  84. package/dist/heart/daemon/up-progress.js +135 -0
  85. package/dist/heart/delegation.js +62 -0
  86. package/dist/heart/habits/habit-migration.js +181 -0
  87. package/dist/heart/habits/habit-parser.js +140 -0
  88. package/dist/heart/habits/habit-scheduler.js +371 -0
  89. package/dist/heart/{daemon → hatch}/hatch-flow.js +30 -120
  90. package/dist/heart/{daemon → hatch}/hatch-specialist.js +3 -3
  91. package/dist/heart/{daemon → hatch}/specialist-prompt.js +10 -7
  92. package/dist/heart/{daemon → hatch}/specialist-tools.js +49 -3
  93. package/dist/heart/identity.js +163 -60
  94. package/dist/heart/kicks.js +2 -20
  95. package/dist/heart/mcp/mcp-server.js +653 -0
  96. package/dist/heart/migrate-config.js +127 -0
  97. package/dist/heart/model-capabilities.js +59 -0
  98. package/dist/heart/outlook/outlook-http.js +439 -0
  99. package/dist/heart/outlook/outlook-read.js +1595 -0
  100. package/dist/heart/outlook/outlook-render.js +1032 -0
  101. package/dist/heart/outlook/outlook-types.js +27 -0
  102. package/dist/heart/outlook/outlook-view.js +194 -0
  103. package/dist/heart/progress-story.js +42 -0
  104. package/dist/heart/provider-failover.js +88 -0
  105. package/dist/heart/provider-ping.js +162 -0
  106. package/dist/heart/providers/anthropic-token.js +163 -0
  107. package/dist/heart/providers/anthropic.js +169 -46
  108. package/dist/heart/providers/azure.js +98 -11
  109. package/dist/heart/providers/error-classification.js +63 -0
  110. package/dist/heart/providers/github-copilot.js +136 -0
  111. package/dist/heart/providers/minimax-vlm.js +189 -0
  112. package/dist/heart/providers/minimax.js +23 -5
  113. package/dist/heart/providers/openai-codex.js +33 -22
  114. package/dist/heart/session-activity.js +190 -0
  115. package/dist/heart/session-events.js +726 -0
  116. package/dist/heart/session-recall.js +162 -0
  117. package/dist/heart/start-of-turn-packet.js +341 -0
  118. package/dist/heart/streaming.js +36 -27
  119. package/dist/heart/sync.js +332 -0
  120. package/dist/heart/target-resolution.js +127 -0
  121. package/dist/heart/tempo.js +93 -0
  122. package/dist/heart/temporal-view.js +41 -0
  123. package/dist/heart/tool-activity-callbacks.js +36 -0
  124. package/dist/heart/tool-description.js +135 -0
  125. package/dist/heart/tool-friction.js +55 -0
  126. package/dist/heart/tool-loop.js +200 -0
  127. package/dist/heart/turn-context.js +358 -0
  128. package/dist/heart/turn-coordinator.js +28 -0
  129. package/dist/heart/{daemon → versioning}/ouro-bot-global-installer.js +1 -1
  130. package/dist/heart/{daemon → versioning}/ouro-bot-wrapper.js +1 -1
  131. package/dist/heart/{daemon → versioning}/ouro-path-installer.js +78 -35
  132. package/dist/heart/versioning/ouro-version-manager.js +295 -0
  133. package/dist/heart/{daemon → versioning}/staged-restart.js +40 -8
  134. package/dist/heart/{daemon → versioning}/update-checker.js +12 -2
  135. package/dist/heart/{daemon → versioning}/update-hooks.js +63 -59
  136. package/dist/mind/associative-recall.js +137 -66
  137. package/dist/mind/bundle-manifest.js +8 -1
  138. package/dist/mind/context.js +89 -93
  139. package/dist/mind/diary-integrity.js +60 -0
  140. package/dist/mind/{memory.js → diary.js} +84 -96
  141. package/dist/mind/embedding-provider.js +60 -0
  142. package/dist/mind/file-state.js +179 -0
  143. package/dist/mind/first-impressions.js +14 -1
  144. package/dist/mind/friends/channel.js +56 -0
  145. package/dist/mind/friends/group-context.js +144 -0
  146. package/dist/mind/friends/resolver.js +37 -0
  147. package/dist/mind/friends/store-file.js +58 -3
  148. package/dist/mind/friends/trust-explanation.js +74 -0
  149. package/dist/mind/friends/types.js +8 -0
  150. package/dist/mind/journal-index.js +161 -0
  151. package/dist/mind/obligation-steering.js +221 -0
  152. package/dist/mind/pending.js +76 -9
  153. package/dist/mind/prompt.js +950 -113
  154. package/dist/mind/provenance-trust.js +26 -0
  155. package/dist/mind/scrutiny.js +173 -0
  156. package/dist/mind/token-estimate.js +8 -12
  157. package/dist/nerves/cli-logging.js +7 -1
  158. package/dist/nerves/coverage/audit.js +1 -1
  159. package/dist/nerves/coverage/file-completeness.js +76 -5
  160. package/dist/nerves/coverage/run-artifacts.js +1 -1
  161. package/dist/nerves/event-buffer.js +111 -0
  162. package/dist/nerves/index.js +224 -4
  163. package/dist/nerves/observation.js +20 -0
  164. package/dist/nerves/redact.js +79 -0
  165. package/dist/nerves/runtime.js +5 -1
  166. package/dist/outlook-ui/assets/index-IuR4F6y6.js +61 -0
  167. package/dist/outlook-ui/assets/index-LwChZTgL.css +1 -0
  168. package/dist/outlook-ui/index.html +15 -0
  169. package/dist/repertoire/ado-client.js +15 -56
  170. package/dist/repertoire/ado-semantic.js +11 -10
  171. package/dist/repertoire/api-client.js +97 -0
  172. package/dist/repertoire/bitwarden-store.js +319 -0
  173. package/dist/repertoire/bundle-templates.js +72 -0
  174. package/dist/repertoire/bw-installer.js +79 -0
  175. package/dist/repertoire/coding/codex-jsonl.js +64 -0
  176. package/dist/repertoire/coding/context-pack.js +330 -0
  177. package/dist/repertoire/coding/feedback.js +197 -30
  178. package/dist/repertoire/coding/manager.js +159 -11
  179. package/dist/repertoire/coding/spawner.js +55 -9
  180. package/dist/repertoire/coding/tools.js +170 -7
  181. package/dist/repertoire/commerce-errors.js +109 -0
  182. package/dist/repertoire/commerce-self-test.js +156 -0
  183. package/dist/repertoire/credential-access.js +527 -0
  184. package/dist/repertoire/duffel-client.js +185 -0
  185. package/dist/repertoire/github-client.js +14 -55
  186. package/dist/repertoire/graph-client.js +11 -52
  187. package/dist/repertoire/guardrails.js +375 -0
  188. package/dist/repertoire/mcp-client.js +255 -0
  189. package/dist/repertoire/mcp-manager.js +305 -0
  190. package/dist/repertoire/mcp-tools.js +63 -0
  191. package/dist/repertoire/shell-sessions.js +133 -0
  192. package/dist/repertoire/skills.js +14 -23
  193. package/dist/repertoire/stripe-client.js +131 -0
  194. package/dist/repertoire/tasks/board.js +43 -5
  195. package/dist/repertoire/tasks/fix.js +182 -0
  196. package/dist/repertoire/tasks/index.js +28 -10
  197. package/dist/repertoire/tasks/lifecycle.js +2 -2
  198. package/dist/repertoire/tasks/parser.js +3 -2
  199. package/dist/repertoire/tasks/scanner.js +194 -37
  200. package/dist/repertoire/tasks/transitions.js +16 -79
  201. package/dist/repertoire/tool-results.js +29 -0
  202. package/dist/repertoire/tools-attachments.js +316 -0
  203. package/dist/repertoire/tools-base.js +45 -771
  204. package/dist/repertoire/tools-bluebubbles.js +1 -0
  205. package/dist/repertoire/tools-bridge.js +141 -0
  206. package/dist/repertoire/tools-bundle.js +984 -0
  207. package/dist/repertoire/tools-config.js +185 -0
  208. package/dist/repertoire/tools-continuity.js +248 -0
  209. package/dist/repertoire/tools-credential.js +182 -0
  210. package/dist/repertoire/tools-files.js +342 -0
  211. package/dist/repertoire/tools-flight.js +224 -0
  212. package/dist/repertoire/tools-flow.js +105 -0
  213. package/dist/repertoire/tools-github.js +1 -7
  214. package/dist/repertoire/tools-memory.js +376 -0
  215. package/dist/repertoire/tools-session.js +739 -0
  216. package/dist/repertoire/tools-shell.js +120 -0
  217. package/dist/repertoire/tools-stripe.js +180 -0
  218. package/dist/repertoire/tools-surface.js +243 -0
  219. package/dist/repertoire/tools-teams.js +12 -62
  220. package/dist/repertoire/tools-travel.js +125 -0
  221. package/dist/repertoire/tools-user-profile.js +144 -0
  222. package/dist/repertoire/tools-vault.js +110 -0
  223. package/dist/repertoire/tools.js +144 -138
  224. package/dist/repertoire/travel-api-client.js +360 -0
  225. package/dist/repertoire/user-profile.js +118 -0
  226. package/dist/repertoire/vault-setup.js +241 -0
  227. package/dist/scripts/claude-code-hook.js +41 -0
  228. package/dist/scripts/claude-code-stop-hook.js +47 -0
  229. package/dist/senses/attention-queue.js +116 -0
  230. package/dist/senses/bluebubbles/attachment-cache.js +53 -0
  231. package/dist/senses/bluebubbles/attachment-download.js +137 -0
  232. package/dist/senses/{bluebubbles-client.js → bluebubbles/client.js} +143 -9
  233. package/dist/senses/bluebubbles/entry.js +13 -0
  234. package/dist/senses/bluebubbles/inbound-log.js +113 -0
  235. package/dist/senses/bluebubbles/index.js +1436 -0
  236. package/dist/senses/{bluebubbles-media.js → bluebubbles/media.js} +121 -70
  237. package/dist/senses/{bluebubbles-model.js → bluebubbles/model.js} +43 -12
  238. package/dist/senses/{bluebubbles-mutation-log.js → bluebubbles/mutation-log.js} +46 -6
  239. package/dist/senses/bluebubbles/replay.js +129 -0
  240. package/dist/senses/bluebubbles/runtime-state.js +109 -0
  241. package/dist/senses/{bluebubbles-session-cleanup.js → bluebubbles/session-cleanup.js} +1 -1
  242. package/dist/senses/cli/bracketed-paste.js +82 -0
  243. package/dist/senses/cli/image-paste.js +287 -0
  244. package/dist/senses/cli/image-ref-navigation.js +75 -0
  245. package/dist/senses/cli/ink-app.js +156 -0
  246. package/dist/senses/cli/inline-diff.js +64 -0
  247. package/dist/senses/cli/input-keys.js +174 -0
  248. package/dist/senses/cli/kill-ring.js +86 -0
  249. package/dist/senses/cli/message-list.js +51 -0
  250. package/dist/senses/cli/ouro-tui.js +605 -0
  251. package/dist/senses/cli/spinner-imperative.js +135 -0
  252. package/dist/senses/cli/spinner.js +101 -0
  253. package/dist/senses/cli/status-line.js +60 -0
  254. package/dist/senses/cli/streaming-markdown.js +526 -0
  255. package/dist/senses/cli/tool-display.js +83 -0
  256. package/dist/senses/cli/tool-render.js +85 -0
  257. package/dist/senses/cli/tui-store.js +240 -0
  258. package/dist/senses/cli/virtual-list.js +35 -0
  259. package/dist/senses/cli-entry.js +1 -1
  260. package/dist/senses/cli-layout.js +187 -0
  261. package/dist/senses/cli.js +595 -246
  262. package/dist/senses/commands.js +65 -1
  263. package/dist/senses/continuity.js +94 -0
  264. package/dist/senses/habit-turn-message.js +108 -0
  265. package/dist/senses/inner-dialog-worker.js +112 -19
  266. package/dist/senses/inner-dialog.js +633 -86
  267. package/dist/senses/pipeline.js +565 -0
  268. package/dist/senses/shared-turn.js +199 -0
  269. package/dist/senses/surface-tool.js +68 -0
  270. package/dist/senses/teams.js +666 -166
  271. package/dist/senses/trust-gate.js +112 -2
  272. package/package.json +27 -7
  273. package/skills/agent-commerce.md +106 -0
  274. package/skills/browser-navigation.md +110 -0
  275. package/skills/commerce-setup-guide.md +116 -0
  276. package/skills/commerce-setup.md +84 -0
  277. package/skills/configure-dev-tools.md +81 -0
  278. package/skills/travel-planning.md +138 -0
  279. package/dist/heart/daemon/subagent-installer.js +0 -134
  280. package/dist/senses/bluebubbles-entry.js +0 -11
  281. package/dist/senses/bluebubbles.js +0 -544
  282. package/dist/senses/debug-activity.js +0 -108
  283. package/subagents/README.md +0 -73
  284. package/subagents/work-doer.md +0 -235
  285. package/subagents/work-merger.md +0 -618
  286. package/subagents/work-planner.md +0 -382
  287. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/basilisk.md +0 -0
  288. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/jafar.md +0 -0
  289. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/jormungandr.md +0 -0
  290. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/kaa.md +0 -0
  291. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/medusa.md +0 -0
  292. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/monty.md +0 -0
  293. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/nagini.md +0 -0
  294. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/ouroboros.md +0 -0
  295. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/python.md +0 -0
  296. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/quetzalcoatl.md +0 -0
  297. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/sir-hiss.md +0 -0
  298. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/the-serpent.md +0 -0
  299. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/the-snake.md +0 -0
  300. /package/dist/heart/{daemon → hatch}/hatch-animation.js +0 -0
  301. /package/dist/heart/{daemon → hatch}/specialist-orchestrator.js +0 -0
  302. /package/dist/heart/{daemon → versioning}/ouro-uti.js +0 -0
  303. /package/dist/heart/{daemon → versioning}/wrapper-publish-guard.js +0 -0
@@ -0,0 +1,565 @@
1
+ "use strict";
2
+ // Shared per-turn pipeline for all senses.
3
+ // Senses are thin transport adapters; this module owns the common lifecycle:
4
+ // resolve friend -> trust gate -> load session -> drain pending -> runAgent -> postTurn -> token accumulation.
5
+ //
6
+ // Transport-level concerns (BB API calls, Teams cards, readline) stay in sense adapters.
7
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
8
+ if (k2 === undefined) k2 = k;
9
+ var desc = Object.getOwnPropertyDescriptor(m, k);
10
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
11
+ desc = { enumerable: true, get: function() { return m[k]; } };
12
+ }
13
+ Object.defineProperty(o, k2, desc);
14
+ }) : (function(o, m, k, k2) {
15
+ if (k2 === undefined) k2 = k;
16
+ o[k2] = m[k];
17
+ }));
18
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
19
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
20
+ }) : function(o, v) {
21
+ o["default"] = v;
22
+ });
23
+ var __importStar = (this && this.__importStar) || (function () {
24
+ var ownKeys = function(o) {
25
+ ownKeys = Object.getOwnPropertyNames || function (o) {
26
+ var ar = [];
27
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
28
+ return ar;
29
+ };
30
+ return ownKeys(o);
31
+ };
32
+ return function (mod) {
33
+ if (mod && mod.__esModule) return mod;
34
+ var result = {};
35
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
36
+ __setModuleDefault(result, mod);
37
+ return result;
38
+ };
39
+ })();
40
+ Object.defineProperty(exports, "__esModule", { value: true });
41
+ exports.emitObligationTransitionEpisodes = emitObligationTransitionEpisodes;
42
+ exports.handleInboundTurn = handleInboundTurn;
43
+ const fs = __importStar(require("fs"));
44
+ const path = __importStar(require("path"));
45
+ const runtime_1 = require("../nerves/runtime");
46
+ const commands_1 = require("./commands");
47
+ const continuity_1 = require("./continuity");
48
+ const manager_1 = require("../heart/bridges/manager");
49
+ const identity_1 = require("../heart/identity");
50
+ const socket_client_1 = require("../heart/daemon/socket-client");
51
+ const active_work_1 = require("../heart/active-work");
52
+ const delegation_1 = require("../heart/delegation");
53
+ const obligations_1 = require("../arc/obligations");
54
+ const provider_failover_1 = require("../heart/provider-failover");
55
+ const provider_ping_1 = require("../heart/provider-ping");
56
+ const auth_flow_1 = require("../heart/auth/auth-flow");
57
+ const tempo_1 = require("../heart/tempo");
58
+ const temporal_view_1 = require("../heart/temporal-view");
59
+ const start_of_turn_packet_1 = require("../heart/start-of-turn-packet");
60
+ const bundle_state_1 = require("../heart/bundle-state");
61
+ const sync_1 = require("../heart/sync");
62
+ const config_1 = require("../heart/config");
63
+ const session_events_1 = require("../heart/session-events");
64
+ const presence_1 = require("../arc/presence");
65
+ const episodes_1 = require("../arc/episodes");
66
+ const turn_context_1 = require("../heart/turn-context");
67
+ /**
68
+ * Emit episodes for obligation state transitions detected during a turn.
69
+ * Exported for direct testability (avoids v8 coverage merge issues in multi-file test suites).
70
+ */
71
+ function emitObligationTransitionEpisodes(agentRoot, preTurnObligationIds, postTurnObligations, preTurnObligations) {
72
+ const postTurnObligationIds = new Set(postTurnObligations.map((ob) => `${ob.id}:${ob.status}`));
73
+ for (const key of preTurnObligationIds) {
74
+ if (!postTurnObligationIds.has(key)) {
75
+ const [obId] = key.split(":");
76
+ const matchedOb = postTurnObligations.find((ob) => ob.id === obId) ?? preTurnObligations.find((ob) => ob.id === obId);
77
+ (0, episodes_1.emitEpisode)(agentRoot, {
78
+ kind: "obligation_shift",
79
+ summary: `obligation "${matchedOb?.content ?? obId}" status changed`,
80
+ whyItMattered: "obligation state transition detected during turn",
81
+ relatedEntities: [`obligation:${obId}`],
82
+ salience: "medium",
83
+ });
84
+ }
85
+ }
86
+ }
87
+ function prependTurnSections(message, sections) {
88
+ /* v8 ignore next -- defensive: only user messages with non-empty sections reach here @preserve */
89
+ if (message.role !== "user" || sections.length === 0)
90
+ return message;
91
+ const prefix = sections.join("\n\n");
92
+ /* v8 ignore start -- defensive: multipart content branch; non-string user messages are rare @preserve */
93
+ if (typeof message.content === "string") {
94
+ return {
95
+ ...message,
96
+ content: `${prefix}\n\n${message.content}`,
97
+ };
98
+ }
99
+ return {
100
+ ...message,
101
+ content: [
102
+ { type: "text", text: `${prefix}\n\n` },
103
+ ...message.content,
104
+ ],
105
+ };
106
+ /* v8 ignore stop */
107
+ }
108
+ // ── Pipeline ──────────────────────────────────────────────────────
109
+ let _lastSessionKey = null;
110
+ async function handleInboundTurn(input) {
111
+ // Reset session-scoped state when the session changes
112
+ const sessionKey = `${input.channel}/${input.sessionKey ?? "session"}`;
113
+ if (sessionKey !== _lastSessionKey) {
114
+ _lastSessionKey = sessionKey;
115
+ // Reset file-state cache and scrutiny tracking for the new session
116
+ const { fileStateCache } = await Promise.resolve().then(() => __importStar(require("../mind/file-state")));
117
+ const { resetSessionModifiedFiles } = await Promise.resolve().then(() => __importStar(require("../mind/scrutiny")));
118
+ fileStateCache.clear();
119
+ resetSessionModifiedFiles();
120
+ }
121
+ // Step 0: Check for pending failover reply
122
+ if (input.failoverState?.pending) {
123
+ const userText = input.messages
124
+ .filter((m) => m.role === "user")
125
+ .map((m) => typeof m.content === "string" ? m.content : /* v8 ignore next -- defensive: multipart content fallback @preserve */ "")
126
+ .join(" ")
127
+ .trim();
128
+ const pendingContext = input.failoverState.pending;
129
+ const failoverAction = (0, provider_failover_1.handleFailoverReply)(userText, pendingContext);
130
+ const failoverAgentName = pendingContext.agentName;
131
+ input.failoverState.pending = null; // always clear before acting
132
+ if (failoverAction.action === "switch") {
133
+ let switchSucceeded = false;
134
+ try {
135
+ (0, auth_flow_1.writeAgentProviderSelection)(failoverAgentName, "human", failoverAction.provider);
136
+ (0, auth_flow_1.writeAgentProviderSelection)(failoverAgentName, "agent", failoverAction.provider);
137
+ switchSucceeded = true;
138
+ /* v8 ignore start -- defensive: write failure during provider switch @preserve */
139
+ }
140
+ catch (switchError) {
141
+ (0, runtime_1.emitNervesEvent)({
142
+ level: "error",
143
+ component: "senses",
144
+ event: "senses.failover_switch_error",
145
+ message: `failed to switch provider to ${failoverAction.provider}`,
146
+ meta: { agentName: failoverAgentName, provider: failoverAction.provider, error: switchError instanceof Error ? switchError.message : String(switchError) },
147
+ });
148
+ }
149
+ /* v8 ignore stop */
150
+ /* v8 ignore next -- false branch: write-failure fallthrough @preserve */
151
+ if (switchSucceeded) {
152
+ (0, runtime_1.emitNervesEvent)({
153
+ component: "senses",
154
+ event: "senses.failover_switch",
155
+ message: `switched provider to ${failoverAction.provider} via failover`,
156
+ meta: { agentName: failoverAgentName, provider: failoverAction.provider },
157
+ });
158
+ // Replace "switch to <provider>" with a context message for the agent.
159
+ // The session already has the user's original question from the failed turn.
160
+ // The agent needs to know what happened so it can respond appropriately.
161
+ const newProviderSecrets = (() => {
162
+ try {
163
+ const { secrets } = (0, auth_flow_1.loadAgentSecrets)(failoverAgentName);
164
+ const cfg = secrets.providers[failoverAction.provider];
165
+ return cfg?.model ?? cfg?.modelName ?? "";
166
+ /* v8 ignore next 2 -- defensive: secrets read failure @preserve */
167
+ }
168
+ catch {
169
+ return "";
170
+ }
171
+ })();
172
+ const newProviderLabel = newProviderSecrets ? `${failoverAction.provider} (${newProviderSecrets})` : failoverAction.provider;
173
+ input.messages = [{
174
+ role: "user",
175
+ content: `[provider switch: ${pendingContext.errorSummary}. switched to ${newProviderLabel}. your conversation history is intact — respond to the user's last message.]`,
176
+ }];
177
+ input.switchedProvider = failoverAction.provider;
178
+ }
179
+ // Switch failed OR succeeded — either way, fall through to normal processing.
180
+ }
181
+ }
182
+ // Step 0b: Slash command interception (before friend resolution / agent turn)
183
+ {
184
+ const firstUserMsg = input.messages.find((m) => m.role === "user");
185
+ const userText = firstUserMsg
186
+ ? (typeof firstUserMsg.content === "string"
187
+ ? firstUserMsg.content
188
+ : Array.isArray(firstUserMsg.content)
189
+ ? firstUserMsg.content.find((p) => p.type === "text")?.text ?? ""
190
+ : /* v8 ignore next -- defensive: content is always string or array @preserve */ "")
191
+ : "";
192
+ const parsed = (0, commands_1.parseSlashCommand)(userText);
193
+ if (parsed) {
194
+ const registry = (0, commands_1.getSharedCommandRegistry)();
195
+ const dispatchResult = registry.dispatch(parsed.command, { channel: input.channel });
196
+ if (dispatchResult.handled && dispatchResult.result) {
197
+ (0, runtime_1.emitNervesEvent)({
198
+ component: "senses",
199
+ event: "senses.pipeline_command",
200
+ message: `slash command intercepted: /${parsed.command}`,
201
+ meta: { command: parsed.command, channel: input.channel },
202
+ });
203
+ if (dispatchResult.result.message) {
204
+ input.callbacks.onTextChunk(dispatchResult.result.message);
205
+ }
206
+ // Return a minimal result — no agent turn, no session write
207
+ const resolvedContext = await input.friendResolver.resolve();
208
+ return {
209
+ resolvedContext,
210
+ gateResult: { allowed: true },
211
+ turnOutcome: "command",
212
+ commandAction: dispatchResult.result.action,
213
+ };
214
+ }
215
+ }
216
+ }
217
+ // Step 1: Resolve friend
218
+ const resolvedContext = await input.friendResolver.resolve();
219
+ (0, runtime_1.emitNervesEvent)({
220
+ component: "senses",
221
+ event: "senses.pipeline_start",
222
+ message: "inbound turn pipeline started",
223
+ meta: {
224
+ channel: input.channel,
225
+ friendId: resolvedContext.friend.id,
226
+ senseType: input.capabilities.senseType,
227
+ },
228
+ });
229
+ // Step 2: Trust gate
230
+ const gateInput = {
231
+ friend: resolvedContext.friend,
232
+ provider: input.provider ?? "local",
233
+ externalId: input.externalId ?? "",
234
+ tenantId: input.tenantId,
235
+ channel: input.channel,
236
+ senseType: input.capabilities.senseType,
237
+ isGroupChat: input.isGroupChat ?? false,
238
+ groupHasFamilyMember: input.groupHasFamilyMember ?? false,
239
+ hasExistingGroupWithFamily: input.hasExistingGroupWithFamily ?? false,
240
+ };
241
+ const gateResult = input.enforceTrustGate(gateInput);
242
+ // Gate rejection: return early, no agent turn
243
+ if (!gateResult.allowed) {
244
+ (0, runtime_1.emitNervesEvent)({
245
+ component: "senses",
246
+ event: "senses.pipeline_gate_reject",
247
+ message: "trust gate rejected inbound turn",
248
+ meta: {
249
+ channel: input.channel,
250
+ friendId: resolvedContext.friend.id,
251
+ reason: gateResult.reason,
252
+ },
253
+ });
254
+ return {
255
+ resolvedContext,
256
+ gateResult,
257
+ };
258
+ }
259
+ // Step 3: Load/create session
260
+ const session = await input.sessionLoader.loadOrCreate();
261
+ const sessionMessages = session.messages;
262
+ const sessionEvents = session.events ?? [];
263
+ let mustResolveBeforeHandoff = (0, continuity_1.resolveMustResolveBeforeHandoff)(session.state?.mustResolveBeforeHandoff === true, input.continuityIngressTexts);
264
+ const lastFriendActivityAt = input.channel === "inner"
265
+ ? session.state?.lastFriendActivityAt
266
+ : new Date().toISOString();
267
+ const currentObligation = input.continuityIngressTexts
268
+ ?.map((text) => text.trim())
269
+ .filter((text) => text.length > 0)
270
+ .at(-1);
271
+ const currentSession = {
272
+ friendId: resolvedContext.friend.id,
273
+ channel: input.channel,
274
+ key: input.sessionKey ?? "session",
275
+ sessionPath: session.sessionPath,
276
+ };
277
+ const currentSessionTiming = (0, session_events_1.describeCurrentSessionTiming)(sessionEvents);
278
+ // Step 3b: Pre-turn sync pull (opt-in) — MUST happen before any continuity state reads
279
+ // so that obligations, episodes, cares, etc. reflect the latest remote state.
280
+ let syncFailure;
281
+ let syncConfig = { enabled: false, remote: "origin" };
282
+ try {
283
+ syncConfig = (0, config_1.getSyncConfig)();
284
+ }
285
+ catch { /* config not available */ }
286
+ // Wrap the turn body in try/finally so postTurnPush always runs — even on
287
+ // error or early-return failover paths.
288
+ try {
289
+ /* v8 ignore start -- sync-enabled branches tested in sync.test.ts, pipeline tests mock at module boundary @preserve */
290
+ if (syncConfig.enabled) {
291
+ const pullResult = (0, sync_1.preTurnPull)((0, identity_1.getAgentRoot)(), syncConfig);
292
+ if (!pullResult.ok) {
293
+ syncFailure = pullResult.error;
294
+ }
295
+ // Check for pending-sync from a prior failed push
296
+ if (!syncFailure) {
297
+ const pendingSyncPath = path.join((0, identity_1.getAgentRoot)(), "state", "pending-sync.json");
298
+ try {
299
+ if (fs.existsSync(pendingSyncPath)) {
300
+ const pendingSync = JSON.parse(fs.readFileSync(pendingSyncPath, "utf-8"));
301
+ syncFailure = `prior sync push failed: ${pendingSync.error ?? "unknown"}`;
302
+ fs.unlinkSync(pendingSyncPath);
303
+ }
304
+ }
305
+ catch {
306
+ // Ignore read errors for pending-sync
307
+ }
308
+ }
309
+ }
310
+ /* v8 ignore stop */
311
+ // Build the turn context snapshot — centralizes all state reads
312
+ const ctx = await (0, turn_context_1.buildTurnContext)({
313
+ currentSession,
314
+ channel: input.channel,
315
+ friendStore: input.friendStore,
316
+ });
317
+ // Propagate sync failure from pre-turn pull
318
+ ctx.syncFailure = syncFailure;
319
+ const { activeBridges, sessionActivity, pendingObligations, codingSessions, otherCodingSessions } = ctx;
320
+ const bridgeContext = (0, manager_1.formatBridgeContext)(activeBridges) || undefined;
321
+ const activeWorkFrame = (0, active_work_1.buildActiveWorkFrame)({
322
+ currentSession,
323
+ currentObligation,
324
+ mustResolveBeforeHandoff,
325
+ inner: ctx.innerWorkState,
326
+ bridges: activeBridges,
327
+ codingSessions,
328
+ otherCodingSessions,
329
+ pendingObligations,
330
+ taskBoard: ctx.taskBoard,
331
+ friendActivity: sessionActivity,
332
+ targetCandidates: ctx.targetCandidates,
333
+ innerReturnObligations: ctx.returnObligations,
334
+ });
335
+ const delegationDecision = (0, delegation_1.decideDelegation)({
336
+ channel: input.channel,
337
+ ingressTexts: input.continuityIngressTexts ?? [],
338
+ activeWork: activeWorkFrame,
339
+ mustResolveBeforeHandoff,
340
+ });
341
+ // Step 4: Drain deferred friend returns, then ordinary per-session pending.
342
+ const deferredReturns = input.channel === "inner"
343
+ ? []
344
+ : (input.drainDeferredReturns?.(resolvedContext.friend.id) ?? []);
345
+ const sessionPending = input.drainPending(input.pendingDir);
346
+ const pending = [...deferredReturns, ...sessionPending];
347
+ // Assemble messages: session messages + pending + inbound user messages
348
+ // NOTE: live world-state checkpoint and pending messages are rendered via buildSystem (system prompt sections)
349
+ const extraPrefixSections = input.onPendingDrained?.(pending) ?? [];
350
+ // extraPrefixSections from onPendingDrained still prepend to user message (e.g., inner dialog wakes)
351
+ if (extraPrefixSections.length > 0 && input.messages.length > 0) {
352
+ input.messages[0] = prependTurnSections(input.messages[0], extraPrefixSections);
353
+ }
354
+ // Append user messages from the inbound turn
355
+ for (const msg of input.messages) {
356
+ (0, session_events_1.stampIngressTime)(msg);
357
+ sessionMessages.push(msg);
358
+ }
359
+ // Step 4b: Continuity pipeline — derive tempo, build start-of-turn packet, snapshot obligations
360
+ let renderedStartOfTurnPacket;
361
+ const preTurnObligationIds = new Set(pendingObligations.map((ob) => `${ob.id}:${ob.status}`));
362
+ try {
363
+ const agentRoot = (0, identity_1.getAgentRoot)();
364
+ const agentName = (0, identity_1.getAgentName)();
365
+ const { recentEpisodes, activeCares } = ctx;
366
+ const tempoState = (0, tempo_1.deriveTempo)({
367
+ activeSessions: sessionActivity.length + 1,
368
+ openObligations: pendingObligations.length,
369
+ recentEpisodeCount: recentEpisodes.length,
370
+ lastActivityAgeMs: sessionActivity.length > 0
371
+ ? Date.now() - new Date(sessionActivity[0].lastActivityAt).getTime()
372
+ : 0,
373
+ hasBlockers: false, // obligations use specific statuses, not "blocked"
374
+ highSalienceEpisodes: recentEpisodes.filter((ep) => ep.salience === "high" || ep.salience === "critical").length,
375
+ activeCareCount: activeCares.length,
376
+ atRiskCareCount: activeCares.filter((c) => c.currentRisk != null).length,
377
+ });
378
+ const temporalView = (0, temporal_view_1.buildTemporalView)(agentRoot, {
379
+ tempo: tempoState.mode,
380
+ preloaded: {
381
+ recentEpisodes,
382
+ activeObligations: pendingObligations,
383
+ activeCares,
384
+ },
385
+ });
386
+ const startOfTurnPacket = (0, start_of_turn_packet_1.buildStartOfTurnPacket)(temporalView, {
387
+ canonicalObligations: {
388
+ primary: activeWorkFrame.primaryObligation,
389
+ all: activeWorkFrame.pendingObligations,
390
+ },
391
+ currentSessionTiming,
392
+ });
393
+ /* v8 ignore next 3 -- syncFailure propagation tested in sync.test.ts @preserve */
394
+ if (syncFailure) {
395
+ startOfTurnPacket.syncFailure = syncFailure;
396
+ }
397
+ // Structured bundle state detection — surfaces discrete issues the
398
+ // agent can remediate via the bundle_* tools. Runs independently of
399
+ // syncFailure so the two signals coexist during the transition away
400
+ // from the legacy free-form syncFailure string. Always assigned; the
401
+ // packet renderer's empty-filter handles the empty-array case without
402
+ // a separate branch here.
403
+ startOfTurnPacket.bundleState = (0, bundle_state_1.detectBundleState)(agentRoot);
404
+ const capabilities = (0, start_of_turn_packet_1.buildCapabilitiesSection)(agentRoot);
405
+ if (capabilities) {
406
+ startOfTurnPacket.capabilities = capabilities;
407
+ }
408
+ renderedStartOfTurnPacket = (0, start_of_turn_packet_1.renderStartOfTurnPacket)(startOfTurnPacket);
409
+ if (!renderedStartOfTurnPacket)
410
+ renderedStartOfTurnPacket = undefined;
411
+ // Update self-presence
412
+ const presence = (0, presence_1.derivePresence)(agentRoot, agentName, {
413
+ activeSessions: sessionActivity.length + 1,
414
+ openObligations: pendingObligations.length,
415
+ activeBridges: activeBridges.length,
416
+ codingLanes: codingSessions.length,
417
+ currentTempo: tempoState.mode,
418
+ });
419
+ (0, presence_1.writePresence)(agentRoot, agentName, presence);
420
+ }
421
+ catch (continuityError) {
422
+ (0, runtime_1.emitNervesEvent)({
423
+ level: "warn",
424
+ component: "senses",
425
+ event: "senses.continuity_error",
426
+ message: "continuity pipeline failed, continuing without start-of-turn packet",
427
+ meta: { error: continuityError instanceof Error ? continuityError.message : String(continuityError) },
428
+ });
429
+ }
430
+ // Step 5: runAgent
431
+ const existingToolContext = input.runAgentOptions?.toolContext;
432
+ const runAgentOptions = {
433
+ ...input.runAgentOptions,
434
+ bridgeContext,
435
+ activeWorkFrame,
436
+ delegationDecision,
437
+ startOfTurnPacket: renderedStartOfTurnPacket,
438
+ pendingMessages: pending.length > 0 ? pending.map((msg) => ({ from: msg.from, content: msg.content })) : undefined,
439
+ currentSessionKey: currentSession.key,
440
+ currentObligation,
441
+ mustResolveBeforeHandoff,
442
+ setMustResolveBeforeHandoff: (value) => {
443
+ mustResolveBeforeHandoff = value;
444
+ },
445
+ // Pre-read state from TurnContext for prompt assembly
446
+ daemonRunning: ctx.daemonRunning,
447
+ senseStatusLines: ctx.senseStatusLines,
448
+ bundleMeta: ctx.bundleMeta,
449
+ daemonHealth: ctx.daemonHealth,
450
+ journalFiles: ctx.journalFiles,
451
+ toolContext: {
452
+ /* v8 ignore next -- default no-op signin satisfies interface; real signin injected by sense adapter @preserve */
453
+ signin: async () => undefined,
454
+ ...existingToolContext,
455
+ context: resolvedContext,
456
+ friendStore: input.friendStore,
457
+ currentSession,
458
+ activeBridges,
459
+ },
460
+ };
461
+ const result = await input.runAgent(sessionMessages, input.callbacks, input.channel, input.signal, runAgentOptions);
462
+ // Step 5b: Failover on terminal error
463
+ if (result.outcome === "errored" && input.failoverState) {
464
+ try {
465
+ const agentName = (0, identity_1.getAgentName)();
466
+ const agentConfig = (0, identity_1.loadAgentConfig)();
467
+ const currentProvider = agentConfig.humanFacing.provider;
468
+ /* v8 ignore next -- defensive: errorClassification always set when errored @preserve */
469
+ const classification = result.errorClassification ?? "unknown";
470
+ const inventory = await (0, provider_ping_1.runHealthInventory)(agentName, currentProvider);
471
+ const { secrets } = (0, auth_flow_1.loadAgentSecrets)(agentName);
472
+ const providerModels = {};
473
+ for (const [p, cfg] of Object.entries(secrets.providers)) {
474
+ const model = cfg.model ?? cfg.modelName;
475
+ if (typeof model === "string" && model)
476
+ providerModels[p] = model;
477
+ }
478
+ // Use agent.json model (source of truth), not secrets model (may be stale)
479
+ const currentModel = agentConfig.humanFacing.model;
480
+ const failoverContext = (0, provider_failover_1.buildFailoverContext)(
481
+ /* v8 ignore next -- defensive: error always set when errored @preserve */
482
+ result.error?.message ?? "unknown error", classification, currentProvider, currentModel, agentName, inventory, providerModels);
483
+ input.failoverState.pending = failoverContext;
484
+ input.postTurn(sessionMessages, session.sessionPath, result.usage);
485
+ return {
486
+ resolvedContext,
487
+ gateResult,
488
+ usage: result.usage,
489
+ turnOutcome: result.outcome,
490
+ sessionPath: session.sessionPath,
491
+ messages: sessionMessages,
492
+ drainedPending: pending,
493
+ failoverMessage: failoverContext.userMessage,
494
+ };
495
+ /* v8 ignore start -- failover catch: tested via pipeline failover sequence throws test but v8 under-reports catch coverage @preserve */
496
+ }
497
+ catch (failoverError) {
498
+ (0, runtime_1.emitNervesEvent)({
499
+ level: "warn",
500
+ component: "senses",
501
+ event: "senses.failover_error",
502
+ message: "failover sequence failed, falling through",
503
+ meta: { error: failoverError instanceof Error ? failoverError.message : String(failoverError) },
504
+ });
505
+ }
506
+ /* v8 ignore stop */
507
+ }
508
+ // Step 5c: Emit episodes for obligation state transitions
509
+ try {
510
+ const agentRoot = (0, identity_1.getAgentRoot)();
511
+ const postTurnObligations = (0, obligations_1.readPendingObligations)(agentRoot);
512
+ emitObligationTransitionEpisodes(agentRoot, preTurnObligationIds, postTurnObligations, pendingObligations);
513
+ }
514
+ catch {
515
+ // Episode emission is non-fatal
516
+ }
517
+ // Step 6: postTurn
518
+ const continuingState = {
519
+ ...(mustResolveBeforeHandoff ? { mustResolveBeforeHandoff: true } : {}),
520
+ ...(typeof lastFriendActivityAt === "string" ? { lastFriendActivityAt } : {}),
521
+ };
522
+ const nextState = result.outcome === "settled" || result.outcome === "blocked" || result.outcome === "superseded" || result.outcome === "observed"
523
+ ? (typeof lastFriendActivityAt === "string"
524
+ ? { lastFriendActivityAt }
525
+ : undefined)
526
+ : (Object.keys(continuingState).length > 0 ? continuingState : undefined);
527
+ input.postTurn(sessionMessages, session.sessionPath, result.usage, undefined, nextState);
528
+ // Step 7: Token accumulation
529
+ await input.accumulateFriendTokens(input.friendStore, resolvedContext.friend.id, result.usage);
530
+ (0, runtime_1.emitNervesEvent)({
531
+ component: "senses",
532
+ event: "senses.pipeline_end",
533
+ message: "inbound turn pipeline completed",
534
+ meta: {
535
+ channel: input.channel,
536
+ friendId: resolvedContext.friend.id,
537
+ },
538
+ });
539
+ // DRY cross-session awareness: notify inner dialog that activity happened on another channel
540
+ // Inner dialog's next checkpoint will include this session's state
541
+ if (input.channel !== "inner") {
542
+ try {
543
+ (0, socket_client_1.requestInnerWake)((0, identity_1.getAgentName)()).catch(/* v8 ignore next */ () => { });
544
+ }
545
+ catch { /* getAgentName may fail in test environments */ }
546
+ }
547
+ return {
548
+ resolvedContext,
549
+ gateResult,
550
+ usage: result.usage,
551
+ turnOutcome: result.outcome,
552
+ completion: result.completion,
553
+ sessionPath: session.sessionPath,
554
+ messages: sessionMessages,
555
+ drainedPending: pending,
556
+ ...(input.switchedProvider ? { switchedProvider: input.switchedProvider } : {}),
557
+ };
558
+ }
559
+ finally {
560
+ // Step 6b: Post-turn sync push (opt-in, git-status-based discovery).
561
+ if (syncConfig.enabled) {
562
+ (0, sync_1.postTurnPush)((0, identity_1.getAgentRoot)(), syncConfig);
563
+ }
564
+ }
565
+ }