@ouro.bot/cli 0.1.0-alpha.62 → 0.1.0-alpha.636

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 (430) hide show
  1. package/README.md +127 -23
  2. package/RepairGuide.ouro/agent.json +5 -0
  3. package/RepairGuide.ouro/psyche/IDENTITY.md +19 -0
  4. package/RepairGuide.ouro/psyche/SOUL.md +55 -0
  5. package/RepairGuide.ouro/skills/diagnose-broken-remote.md +63 -0
  6. package/RepairGuide.ouro/skills/diagnose-stacked-typed-issues.md +35 -0
  7. package/RepairGuide.ouro/skills/diagnose-sync-blocked.md +54 -0
  8. package/RepairGuide.ouro/skills/diagnose-vault-expired.md +60 -0
  9. package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/agent.json +4 -2
  10. package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/SOUL.md +2 -2
  11. package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/the-serpent.md +1 -1
  12. package/changelog.json +4070 -13
  13. package/dist/arc/attention-types.js +8 -0
  14. package/dist/arc/cares.js +144 -0
  15. package/dist/arc/episodes.js +118 -0
  16. package/dist/arc/intentions.js +134 -0
  17. package/dist/arc/json-store.js +117 -0
  18. package/dist/arc/obligations.js +266 -0
  19. package/dist/arc/packets.js +194 -0
  20. package/dist/arc/presence.js +185 -0
  21. package/dist/arc/task-lifecycle.js +57 -0
  22. package/dist/heart/active-work.js +831 -43
  23. package/dist/heart/agent-entry.js +69 -3
  24. package/dist/heart/attachments/image-normalize.js +194 -0
  25. package/dist/heart/attachments/materialize.js +97 -0
  26. package/dist/heart/attachments/originals.js +88 -0
  27. package/dist/heart/attachments/render.js +29 -0
  28. package/dist/heart/attachments/sources/bluebubbles.js +156 -0
  29. package/dist/heart/attachments/sources/cli-local-file.js +78 -0
  30. package/dist/heart/attachments/sources/index.js +16 -0
  31. package/dist/heart/attachments/store.js +103 -0
  32. package/dist/heart/attachments/types.js +93 -0
  33. package/dist/heart/auth/auth-flow.js +479 -0
  34. package/dist/heart/awaiting/await-alert.js +146 -0
  35. package/dist/heart/awaiting/await-expiry.js +108 -0
  36. package/dist/heart/awaiting/await-loader.js +91 -0
  37. package/dist/heart/awaiting/await-parser.js +141 -0
  38. package/dist/heart/awaiting/await-runtime-state.js +100 -0
  39. package/dist/heart/awaiting/await-scheduler.js +377 -0
  40. package/dist/heart/background-operations.js +281 -0
  41. package/dist/heart/bridges/manager.js +137 -17
  42. package/dist/heart/bridges/store.js +14 -2
  43. package/dist/heart/bundle-state.js +168 -0
  44. package/dist/heart/commitments.js +135 -0
  45. package/dist/heart/config-registry.js +322 -0
  46. package/dist/heart/config.js +114 -119
  47. package/dist/heart/core.js +914 -248
  48. package/dist/heart/cross-chat-delivery.js +3 -18
  49. package/dist/heart/daemon/agent-config-check.js +419 -0
  50. package/dist/heart/daemon/agent-discovery.js +102 -3
  51. package/dist/heart/daemon/agent-service.js +522 -0
  52. package/dist/heart/daemon/agentic-repair.js +547 -0
  53. package/dist/heart/daemon/bluebubbles-health-diagnostics.js +122 -0
  54. package/dist/heart/daemon/boot-sync-probe.js +197 -0
  55. package/dist/heart/daemon/cadence.js +70 -0
  56. package/dist/heart/daemon/cli-defaults.js +776 -0
  57. package/dist/heart/daemon/cli-desk.js +322 -0
  58. package/dist/heart/daemon/cli-exec.js +7468 -0
  59. package/dist/heart/daemon/cli-help.js +505 -0
  60. package/dist/heart/daemon/cli-parse.js +1554 -0
  61. package/dist/heart/daemon/cli-render-doctor.js +57 -0
  62. package/dist/heart/daemon/cli-render.js +763 -0
  63. package/dist/heart/daemon/cli-types.js +8 -0
  64. package/dist/heart/daemon/connect-bay.js +323 -0
  65. package/dist/heart/daemon/daemon-cli.js +29 -1700
  66. package/dist/heart/daemon/daemon-entry.js +485 -2
  67. package/dist/heart/daemon/daemon-health.js +176 -0
  68. package/dist/heart/daemon/daemon-rollup.js +57 -0
  69. package/dist/heart/daemon/daemon-runtime-sync.js +88 -13
  70. package/dist/heart/daemon/daemon-tombstone.js +236 -0
  71. package/dist/heart/daemon/daemon.js +905 -71
  72. package/dist/heart/daemon/dns-workflow.js +394 -0
  73. package/dist/heart/daemon/doctor-types.js +8 -0
  74. package/dist/heart/daemon/doctor.js +873 -0
  75. package/dist/heart/daemon/health-monitor.js +122 -1
  76. package/dist/heart/daemon/hooks/agent-config-v2.js +33 -0
  77. package/dist/heart/daemon/hooks/bundle-meta.js +115 -1
  78. package/dist/heart/daemon/http-health-probe.js +80 -0
  79. package/dist/heart/daemon/human-command-screens.js +234 -0
  80. package/dist/heart/daemon/human-readiness.js +114 -0
  81. package/dist/heart/daemon/inner-status.js +89 -0
  82. package/dist/heart/daemon/interactive-repair.js +394 -0
  83. package/dist/heart/daemon/launchd.js +37 -8
  84. package/dist/heart/daemon/log-tailer.js +79 -10
  85. package/dist/heart/daemon/logs-prune.js +110 -0
  86. package/dist/heart/daemon/mcp-canary.js +297 -0
  87. package/dist/heart/daemon/migrate-to-desk.js +848 -0
  88. package/dist/heart/daemon/os-cron-deps.js +135 -0
  89. package/dist/heart/daemon/os-cron.js +14 -12
  90. package/dist/heart/daemon/ouro-bot-entry.js +4 -2
  91. package/dist/heart/daemon/ouro-entry.js +3 -1
  92. package/dist/heart/daemon/plugin-cli.js +432 -0
  93. package/dist/heart/daemon/process-manager.js +463 -34
  94. package/dist/heart/daemon/provider-discovery.js +137 -0
  95. package/dist/heart/daemon/provider-ping-progress.js +83 -0
  96. package/dist/heart/daemon/pulse.js +475 -0
  97. package/dist/heart/daemon/readiness-repair.js +365 -0
  98. package/dist/heart/daemon/run-hooks.js +2 -0
  99. package/dist/heart/daemon/runtime-logging.js +11 -3
  100. package/dist/heart/daemon/runtime-metadata.js +2 -30
  101. package/dist/heart/daemon/safe-mode.js +161 -0
  102. package/dist/heart/daemon/sense-manager.js +493 -38
  103. package/dist/heart/daemon/session-id-resolver.js +131 -0
  104. package/dist/heart/daemon/skill-management-installer.js +22 -9
  105. package/dist/heart/daemon/socket-client.js +158 -11
  106. package/dist/heart/daemon/stale-bundle-prune.js +96 -0
  107. package/dist/heart/daemon/startup-tui.js +330 -0
  108. package/dist/heart/daemon/task-scheduler.js +117 -39
  109. package/dist/heart/daemon/terminal-ui.js +499 -0
  110. package/dist/heart/daemon/thoughts.js +229 -17
  111. package/dist/heart/daemon/up-progress.js +366 -0
  112. package/dist/heart/daemon/vault-items.js +56 -0
  113. package/dist/heart/delegation.js +1 -4
  114. package/dist/heart/habits/habit-migration.js +189 -0
  115. package/dist/heart/habits/habit-parser.js +140 -0
  116. package/dist/heart/habits/habit-runtime-state.js +100 -0
  117. package/dist/heart/habits/habit-scheduler.js +372 -0
  118. package/dist/heart/{daemon → hatch}/hatch-flow.js +32 -56
  119. package/dist/heart/{daemon → hatch}/hatch-specialist.js +6 -8
  120. package/dist/heart/{daemon → hatch}/specialist-prompt.js +12 -9
  121. package/dist/heart/{daemon → hatch}/specialist-tools.js +37 -14
  122. package/dist/heart/identity.js +168 -57
  123. package/dist/heart/kept-notes.js +357 -0
  124. package/dist/heart/kicks.js +1 -1
  125. package/dist/heart/machine-identity.js +161 -0
  126. package/dist/heart/mail-import-discovery.js +353 -0
  127. package/dist/heart/mailbox/mailbox-http-hooks.js +66 -0
  128. package/dist/heart/mailbox/mailbox-http-response.js +7 -0
  129. package/dist/heart/mailbox/mailbox-http-routes.js +246 -0
  130. package/dist/heart/mailbox/mailbox-http-static.js +103 -0
  131. package/dist/heart/mailbox/mailbox-http-transport.js +116 -0
  132. package/dist/heart/mailbox/mailbox-http.js +99 -0
  133. package/dist/heart/mailbox/mailbox-read.js +31 -0
  134. package/dist/heart/mailbox/mailbox-types.js +27 -0
  135. package/dist/heart/mailbox/mailbox-view.js +197 -0
  136. package/dist/heart/mailbox/readers/agent-machine.js +418 -0
  137. package/dist/heart/mailbox/readers/continuity-readers.js +319 -0
  138. package/dist/heart/mailbox/readers/mail.js +375 -0
  139. package/dist/heart/mailbox/readers/runtime-readers.js +756 -0
  140. package/dist/heart/mailbox/readers/sessions.js +232 -0
  141. package/dist/heart/mailbox/readers/shared.js +111 -0
  142. package/dist/heart/mcp/mcp-server.js +656 -0
  143. package/dist/heart/migrate-config.js +100 -0
  144. package/dist/heart/model-capabilities.js +19 -0
  145. package/dist/heart/orientation-frame.js +217 -0
  146. package/dist/heart/platform.js +81 -0
  147. package/dist/heart/provider-attempt.js +134 -0
  148. package/dist/heart/provider-binding-resolver.js +272 -0
  149. package/dist/heart/provider-credentials.js +425 -0
  150. package/dist/heart/provider-failover.js +301 -0
  151. package/dist/heart/provider-models.js +81 -0
  152. package/dist/heart/provider-ping.js +262 -0
  153. package/dist/heart/provider-readiness-cache.js +40 -0
  154. package/dist/heart/provider-visibility.js +188 -0
  155. package/dist/heart/providers/anthropic-token.js +131 -0
  156. package/dist/heart/providers/anthropic.js +139 -52
  157. package/dist/heart/providers/azure.js +23 -11
  158. package/dist/heart/providers/error-classification.js +127 -0
  159. package/dist/heart/providers/github-copilot.js +145 -0
  160. package/dist/heart/providers/minimax-vlm.js +189 -0
  161. package/dist/heart/providers/minimax.js +26 -8
  162. package/dist/heart/providers/openai-codex.js +55 -40
  163. package/dist/heart/runtime-capability-check.js +170 -0
  164. package/dist/heart/runtime-credentials.js +367 -0
  165. package/dist/heart/runtime-cwd.js +87 -0
  166. package/dist/heart/sense-truth.js +13 -4
  167. package/dist/heart/session-activity.js +48 -24
  168. package/dist/heart/session-events.js +1163 -0
  169. package/dist/heart/session-playback-cli-main.js +5 -0
  170. package/dist/heart/session-playback-cli.js +36 -0
  171. package/dist/heart/session-playback.js +231 -0
  172. package/dist/heart/session-stats-cli-main.js +5 -0
  173. package/dist/heart/session-stats.js +182 -0
  174. package/dist/heart/session-transcript.js +133 -0
  175. package/dist/heart/start-of-turn-packet.js +345 -0
  176. package/dist/heart/streaming.js +44 -27
  177. package/dist/heart/structured-output.js +196 -0
  178. package/dist/heart/sync-classification.js +176 -0
  179. package/dist/heart/sync.js +449 -0
  180. package/dist/heart/target-resolution.js +9 -5
  181. package/dist/heart/tempo.js +93 -0
  182. package/dist/heart/temporal-view.js +41 -0
  183. package/dist/heart/timeouts.js +101 -0
  184. package/dist/heart/tool-activity-callbacks.js +59 -0
  185. package/dist/heart/tool-description.js +143 -0
  186. package/dist/heart/tool-friction.js +55 -0
  187. package/dist/heart/tool-loop.js +200 -0
  188. package/dist/heart/turn-context.js +389 -0
  189. package/dist/heart/{daemon → versioning}/ouro-bot-global-installer.js +6 -5
  190. package/dist/heart/{daemon → versioning}/ouro-bot-wrapper.js +1 -1
  191. package/dist/heart/versioning/ouro-path-installer.js +426 -0
  192. package/dist/heart/versioning/ouro-version-manager.js +295 -0
  193. package/dist/heart/{daemon → versioning}/staged-restart.js +40 -8
  194. package/dist/heart/{daemon → versioning}/update-checker.js +6 -1
  195. package/dist/heart/{daemon → versioning}/update-hooks.js +63 -59
  196. package/dist/mailbox-ui/assets/index-9-AxCxuB.js +61 -0
  197. package/dist/mailbox-ui/assets/index-CWzt267f.css +1 -0
  198. package/dist/mailbox-ui/index.html +15 -0
  199. package/dist/mailroom/attention.js +167 -0
  200. package/dist/mailroom/autonomy.js +209 -0
  201. package/dist/mailroom/blob-store.js +715 -0
  202. package/dist/mailroom/body-cache.js +61 -0
  203. package/dist/mailroom/core.js +788 -0
  204. package/dist/mailroom/entry.js +160 -0
  205. package/dist/mailroom/file-store.js +568 -0
  206. package/dist/mailroom/mbox-import.js +393 -0
  207. package/dist/mailroom/migration.js +164 -0
  208. package/dist/mailroom/outbound.js +380 -0
  209. package/dist/mailroom/policy.js +263 -0
  210. package/dist/mailroom/reader.js +233 -0
  211. package/dist/mailroom/search-cache.js +334 -0
  212. package/dist/mailroom/search-relevance.js +319 -0
  213. package/dist/mailroom/smtp-ingress.js +176 -0
  214. package/dist/mailroom/source-state.js +176 -0
  215. package/dist/mailroom/thread.js +109 -0
  216. package/dist/mailroom/travel-extract.js +89 -0
  217. package/dist/mind/bundle-manifest.js +14 -1
  218. package/dist/mind/context.js +251 -101
  219. package/dist/mind/desk-section.js +310 -0
  220. package/dist/mind/diary-integrity.js +60 -0
  221. package/dist/mind/{memory.js → diary.js} +68 -76
  222. package/dist/mind/embedding-provider.js +60 -0
  223. package/dist/mind/file-state.js +179 -0
  224. package/dist/mind/friends/channel.js +39 -0
  225. package/dist/mind/friends/resolver.js +54 -2
  226. package/dist/mind/friends/store-file.js +48 -4
  227. package/dist/mind/friends/types.js +2 -2
  228. package/dist/mind/journal-index.js +162 -0
  229. package/dist/mind/note-search.js +268 -0
  230. package/dist/mind/obligation-steering.js +221 -0
  231. package/dist/mind/pending.js +6 -1
  232. package/dist/mind/prompt-refresh.js +3 -2
  233. package/dist/mind/prompt.js +1058 -146
  234. package/dist/mind/provenance-trust.js +26 -0
  235. package/dist/mind/scrutiny.js +173 -0
  236. package/dist/nerves/cli-logging.js +7 -1
  237. package/dist/nerves/coverage/audit-rules.js +15 -6
  238. package/dist/nerves/coverage/audit.js +28 -2
  239. package/dist/nerves/coverage/cli.js +1 -1
  240. package/dist/nerves/coverage/contract.js +5 -5
  241. package/dist/nerves/coverage/file-completeness.js +139 -5
  242. package/dist/nerves/event-buffer.js +111 -0
  243. package/dist/nerves/index.js +224 -4
  244. package/dist/nerves/observation.js +20 -0
  245. package/dist/nerves/redact.js +79 -0
  246. package/dist/nerves/review/cli-main.js +5 -0
  247. package/dist/nerves/review/cli.js +156 -0
  248. package/dist/nerves/review/core.js +152 -0
  249. package/dist/nerves/runtime.js +5 -1
  250. package/dist/repertoire/ado-client.js +15 -56
  251. package/dist/repertoire/ado-semantic.js +16 -10
  252. package/dist/repertoire/api-client.js +97 -0
  253. package/dist/repertoire/bitwarden-store.js +997 -0
  254. package/dist/repertoire/bundle-templates.js +72 -0
  255. package/dist/repertoire/bw-installer.js +180 -0
  256. package/dist/repertoire/coding/codex-jsonl.js +64 -0
  257. package/dist/repertoire/coding/context-pack.js +331 -0
  258. package/dist/repertoire/coding/feedback.js +197 -30
  259. package/dist/repertoire/coding/manager.js +163 -10
  260. package/dist/repertoire/coding/spawner.js +55 -9
  261. package/dist/repertoire/coding/tools.js +177 -7
  262. package/dist/repertoire/commerce-errors.js +109 -0
  263. package/dist/repertoire/commerce-self-test.js +156 -0
  264. package/dist/repertoire/credential-access.js +178 -0
  265. package/dist/repertoire/desk/classifier.js +362 -0
  266. package/dist/repertoire/duffel-client.js +185 -0
  267. package/dist/repertoire/github-client.js +14 -55
  268. package/dist/repertoire/graph-client.js +11 -52
  269. package/dist/repertoire/guardrails.js +385 -0
  270. package/dist/repertoire/mcp-client.js +295 -0
  271. package/dist/repertoire/mcp-manager.js +403 -0
  272. package/dist/repertoire/mcp-tools.js +83 -0
  273. package/dist/repertoire/plugin-mcp.js +175 -0
  274. package/dist/repertoire/plugins.js +253 -0
  275. package/dist/repertoire/shell-sessions.js +133 -0
  276. package/dist/repertoire/skills.js +48 -4
  277. package/dist/repertoire/stripe-client.js +131 -0
  278. package/dist/repertoire/tool-results.js +29 -0
  279. package/dist/repertoire/tools-attachments.js +317 -0
  280. package/dist/repertoire/tools-awaiting.js +372 -0
  281. package/dist/repertoire/tools-base.js +57 -1082
  282. package/dist/repertoire/tools-bluebubbles.js +2 -0
  283. package/dist/repertoire/tools-bridge.js +144 -0
  284. package/dist/repertoire/tools-bundle.js +993 -0
  285. package/dist/repertoire/tools-config.js +186 -0
  286. package/dist/repertoire/tools-continuity.js +252 -0
  287. package/dist/repertoire/tools-credential.js +383 -0
  288. package/dist/repertoire/tools-files.js +344 -0
  289. package/dist/repertoire/tools-flight.js +227 -0
  290. package/dist/repertoire/tools-flow.js +119 -0
  291. package/dist/repertoire/tools-github.js +3 -8
  292. package/dist/repertoire/tools-mail.js +1975 -0
  293. package/dist/repertoire/tools-notes.js +438 -0
  294. package/dist/repertoire/tools-obligations.js +143 -0
  295. package/dist/repertoire/tools-orientation.js +31 -0
  296. package/dist/repertoire/tools-record.js +464 -0
  297. package/dist/repertoire/tools-runtime.js +150 -0
  298. package/dist/repertoire/tools-session.js +756 -0
  299. package/dist/repertoire/tools-shell.js +120 -0
  300. package/dist/repertoire/tools-stripe.js +182 -0
  301. package/dist/repertoire/tools-surface.js +316 -0
  302. package/dist/repertoire/tools-teams.js +12 -39
  303. package/dist/repertoire/tools-travel.js +125 -0
  304. package/dist/repertoire/tools-trip.js +982 -0
  305. package/dist/repertoire/tools-user-profile.js +146 -0
  306. package/dist/repertoire/tools-vault.js +40 -0
  307. package/dist/repertoire/tools-voice.js +145 -0
  308. package/dist/repertoire/tools.js +215 -103
  309. package/dist/repertoire/travel-api-client.js +360 -0
  310. package/dist/repertoire/user-profile.js +131 -0
  311. package/dist/repertoire/vault-setup.js +246 -0
  312. package/dist/repertoire/vault-unlock.js +594 -0
  313. package/dist/scripts/claude-code-hook.js +41 -0
  314. package/dist/scripts/claude-code-stop-hook.js +47 -0
  315. package/dist/senses/attention-queue.js +116 -0
  316. package/dist/senses/await-turn-message.js +58 -0
  317. package/dist/senses/bluebubbles/active-turns.js +216 -0
  318. package/dist/senses/bluebubbles/attachment-cache.js +53 -0
  319. package/dist/senses/bluebubbles/attachment-download.js +137 -0
  320. package/dist/senses/{bluebubbles-client.js → bluebubbles/client.js} +219 -18
  321. package/dist/senses/bluebubbles/entry.js +77 -0
  322. package/dist/senses/{bluebubbles-inbound-log.js → bluebubbles/inbound-log.js} +20 -3
  323. package/dist/senses/bluebubbles/index.js +2599 -0
  324. package/dist/senses/{bluebubbles-media.js → bluebubbles/media.js} +121 -71
  325. package/dist/senses/{bluebubbles-model.js → bluebubbles/model.js} +33 -12
  326. package/dist/senses/{bluebubbles-mutation-log.js → bluebubbles/mutation-log.js} +3 -3
  327. package/dist/senses/bluebubbles/processed-log.js +133 -0
  328. package/dist/senses/bluebubbles/replay.js +137 -0
  329. package/dist/senses/{bluebubbles-runtime-state.js → bluebubbles/runtime-state.js} +30 -2
  330. package/dist/senses/{bluebubbles-session-cleanup.js → bluebubbles/session-cleanup.js} +1 -1
  331. package/dist/senses/bluebubbles-meta-guard.js +40 -0
  332. package/dist/senses/cli/bracketed-paste.js +82 -0
  333. package/dist/senses/cli/image-paste.js +287 -0
  334. package/dist/senses/cli/image-ref-navigation.js +75 -0
  335. package/dist/senses/cli/ink-app.js +156 -0
  336. package/dist/senses/cli/inline-diff.js +64 -0
  337. package/dist/senses/cli/input-keys.js +174 -0
  338. package/dist/senses/cli/kill-ring.js +86 -0
  339. package/dist/senses/cli/message-list.js +51 -0
  340. package/dist/senses/cli/ouro-tui.js +607 -0
  341. package/dist/senses/cli/spinner-imperative.js +135 -0
  342. package/dist/senses/cli/spinner.js +101 -0
  343. package/dist/senses/cli/status-line.js +60 -0
  344. package/dist/senses/cli/streaming-markdown.js +526 -0
  345. package/dist/senses/cli/tool-display.js +85 -0
  346. package/dist/senses/cli/tool-render.js +85 -0
  347. package/dist/senses/cli/tui-store.js +240 -0
  348. package/dist/senses/cli/virtual-list.js +35 -0
  349. package/dist/senses/cli-entry.js +60 -8
  350. package/dist/senses/cli-layout.js +100 -0
  351. package/dist/senses/cli.js +517 -204
  352. package/dist/senses/commands.js +66 -3
  353. package/dist/senses/habit-turn-message.js +108 -0
  354. package/dist/senses/inner-dialog-worker.js +254 -22
  355. package/dist/senses/inner-dialog.js +488 -39
  356. package/dist/senses/mail-entry.js +66 -0
  357. package/dist/senses/mail.js +379 -0
  358. package/dist/senses/pipeline.js +666 -181
  359. package/dist/senses/proactive-content-guard.js +51 -0
  360. package/dist/senses/shared-turn.js +393 -0
  361. package/dist/senses/surface-tool.js +70 -0
  362. package/dist/senses/teams-entry.js +60 -8
  363. package/dist/senses/teams.js +388 -98
  364. package/dist/senses/trust-gate.js +100 -5
  365. package/dist/senses/voice/audio-playback.js +237 -0
  366. package/dist/senses/voice/audio-routing.js +119 -0
  367. package/dist/senses/voice/elevenlabs.js +202 -0
  368. package/dist/senses/voice/floor-control.js +431 -0
  369. package/dist/senses/voice/floor-controller.js +115 -0
  370. package/dist/senses/voice/golden-path.js +116 -0
  371. package/dist/senses/voice/index.js +29 -0
  372. package/dist/senses/voice/meeting.js +113 -0
  373. package/dist/senses/voice/outbound.js +190 -0
  374. package/dist/senses/voice/phone.js +33 -0
  375. package/dist/senses/voice/playback.js +139 -0
  376. package/dist/senses/voice/realtime-eval.js +496 -0
  377. package/dist/senses/voice/realtime-trace.js +531 -0
  378. package/dist/senses/voice/transcript.js +70 -0
  379. package/dist/senses/voice/turn.js +191 -0
  380. package/dist/senses/voice/twilio-phone-runtime.js +807 -0
  381. package/dist/senses/voice/twilio-phone.js +5079 -0
  382. package/dist/senses/voice/types.js +2 -0
  383. package/dist/senses/voice/whisper.js +161 -0
  384. package/dist/senses/voice-entry.js +81 -0
  385. package/dist/senses/voice-realtime-eval-command.js +99 -0
  386. package/dist/senses/voice-realtime-eval-entry.js +21 -0
  387. package/dist/senses/voice-twilio-entry.js +87 -0
  388. package/dist/trips/core.js +138 -0
  389. package/dist/trips/store.js +265 -0
  390. package/dist/util/frontmatter.js +53 -0
  391. package/package.json +42 -8
  392. package/skills/agent-commerce.md +106 -0
  393. package/skills/browser-navigation.md +117 -0
  394. package/skills/commerce-setup-guide.md +116 -0
  395. package/skills/commerce-setup.md +84 -0
  396. package/skills/configure-dev-tools.md +99 -0
  397. package/skills/travel-planning.md +138 -0
  398. package/dist/heart/daemon/auth-flow.js +0 -351
  399. package/dist/heart/daemon/ouro-path-installer.js +0 -178
  400. package/dist/heart/safe-workspace.js +0 -228
  401. package/dist/heart/session-recall.js +0 -116
  402. package/dist/mind/associative-recall.js +0 -209
  403. package/dist/repertoire/tasks/board.js +0 -134
  404. package/dist/repertoire/tasks/index.js +0 -224
  405. package/dist/repertoire/tasks/lifecycle.js +0 -80
  406. package/dist/repertoire/tasks/middleware.js +0 -65
  407. package/dist/repertoire/tasks/parser.js +0 -173
  408. package/dist/repertoire/tasks/scanner.js +0 -132
  409. package/dist/repertoire/tasks/transitions.js +0 -144
  410. package/dist/senses/bluebubbles-entry.js +0 -13
  411. package/dist/senses/bluebubbles.js +0 -1177
  412. package/dist/senses/debug-activity.js +0 -148
  413. package/subagents/README.md +0 -7
  414. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/basilisk.md +0 -0
  415. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/jafar.md +0 -0
  416. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/jormungandr.md +0 -0
  417. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/kaa.md +0 -0
  418. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/medusa.md +0 -0
  419. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/monty.md +0 -0
  420. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/nagini.md +0 -0
  421. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/ouroboros.md +0 -0
  422. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/python.md +0 -0
  423. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/quetzalcoatl.md +0 -0
  424. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/sir-hiss.md +0 -0
  425. /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/the-snake.md +0 -0
  426. /package/dist/{repertoire/tasks/types.js → heart/attachments/sources/adapter.js} +0 -0
  427. /package/dist/heart/{daemon → hatch}/hatch-animation.js +0 -0
  428. /package/dist/heart/{daemon → hatch}/specialist-orchestrator.js +0 -0
  429. /package/dist/heart/{daemon → versioning}/ouro-uti.js +0 -0
  430. /package/dist/heart/{daemon → versioning}/wrapper-publish-guard.js +0 -0
@@ -0,0 +1,1163 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.EVENT_CONTENT_MAX_CHARS = void 0;
37
+ exports.truncateLargeEventContent = truncateLargeEventContent;
38
+ exports.capStructuredRecordString = capStructuredRecordString;
39
+ exports.capStructuredRecordStringArray = capStructuredRecordStringArray;
40
+ exports.capStructuredRecordStringLeaves = capStructuredRecordStringLeaves;
41
+ exports.normalizeContinuityState = normalizeContinuityState;
42
+ exports.validateSessionMessages = validateSessionMessages;
43
+ exports.detectDuplicateToolCallIds = detectDuplicateToolCallIds;
44
+ exports.repairSessionMessages = repairSessionMessages;
45
+ exports.migrateToolNames = migrateToolNames;
46
+ exports.sanitizeProviderMessages = sanitizeProviderMessages;
47
+ exports.stampIngressTime = stampIngressTime;
48
+ exports.getIngressTime = getIngressTime;
49
+ exports.projectProviderMessages = projectProviderMessages;
50
+ exports.annotateMessageTimestamps = annotateMessageTimestamps;
51
+ exports.bestEventTimestamp = bestEventTimestamp;
52
+ exports.formatSessionEventTimestamp = formatSessionEventTimestamp;
53
+ exports.extractEventText = extractEventText;
54
+ exports.deriveSessionChronology = deriveSessionChronology;
55
+ exports.describeCurrentSessionTiming = describeCurrentSessionTiming;
56
+ exports.migrateLegacySessionEnvelope = migrateLegacySessionEnvelope;
57
+ exports.parseSessionEnvelope = parseSessionEnvelope;
58
+ exports.loadSessionEnvelopeFile = loadSessionEnvelopeFile;
59
+ exports.buildCanonicalSessionEnvelope = buildCanonicalSessionEnvelope;
60
+ exports.appendEvictedToArchive = appendEvictedToArchive;
61
+ exports.appendSyntheticAssistantEvent = appendSyntheticAssistantEvent;
62
+ const fs = __importStar(require("fs"));
63
+ const runtime_1 = require("../nerves/runtime");
64
+ const structured_output_1 = require("./structured-output");
65
+ let archiveDisabledEmitted = false;
66
+ exports.EVENT_CONTENT_MAX_CHARS = 256 * 1024;
67
+ function truncateLargeEventContent(content, maxChars) {
68
+ if (typeof content !== "string") {
69
+ return { content, truncated: false, originalLength: 0 };
70
+ }
71
+ if (content.length <= maxChars) {
72
+ return { content, truncated: false, originalLength: content.length };
73
+ }
74
+ const marker = `[truncated — event content exceeded ${maxChars} chars; original length ${content.length} chars]`;
75
+ const remainingBudget = Math.max(0, maxChars - marker.length);
76
+ const headLength = Math.ceil(remainingBudget * 0.75);
77
+ const tailLength = Math.max(0, remainingBudget - headLength);
78
+ return {
79
+ content: `${content.slice(0, headLength)}${marker}${tailLength > 0 ? content.slice(-tailLength) : ""}`,
80
+ truncated: true,
81
+ originalLength: content.length,
82
+ };
83
+ }
84
+ function capStructuredRecordString(value) {
85
+ return truncateLargeEventContent(value, exports.EVENT_CONTENT_MAX_CHARS).content;
86
+ }
87
+ function capStructuredRecordStringArray(values) {
88
+ return values.map((value) => capStructuredRecordString(value));
89
+ }
90
+ function capStructuredRecordStringLeaves(value) {
91
+ if (typeof value === "string") {
92
+ return capStructuredRecordString(value);
93
+ }
94
+ if (Array.isArray(value)) {
95
+ return value.map((item) => capStructuredRecordStringLeaves(item));
96
+ }
97
+ if (!value || typeof value !== "object") {
98
+ return value;
99
+ }
100
+ const capped = {};
101
+ for (const [key, child] of Object.entries(value)) {
102
+ capped[key] = capStructuredRecordStringLeaves(child);
103
+ }
104
+ return capped;
105
+ }
106
+ function formatElapsed(ms) {
107
+ const minutes = Math.max(0, Math.floor(ms / 60000));
108
+ if (minutes < 60)
109
+ return `${minutes}m ago`;
110
+ const hours = Math.floor(minutes / 60);
111
+ if (hours < 24)
112
+ return `${hours}h ago`;
113
+ const days = Math.floor(hours / 24);
114
+ return `${days}d ago`;
115
+ }
116
+ const LEGACY_WRITTEN_NOTE_PREFIX = "mem" + "ory";
117
+ const TOOL_NAME_MIGRATIONS = {
118
+ final_answer: "settle",
119
+ no_response: "observe",
120
+ go_inward: "ponder",
121
+ descend: "ponder",
122
+ [`${LEGACY_WRITTEN_NOTE_PREFIX}_save`]: "diary_write",
123
+ [`${LEGACY_WRITTEN_NOTE_PREFIX}_search`]: "search_notes",
124
+ };
125
+ function normalizeUsage(usage) {
126
+ if (!usage || typeof usage !== "object")
127
+ return null;
128
+ const record = usage;
129
+ if (typeof record.input_tokens !== "number"
130
+ || typeof record.output_tokens !== "number"
131
+ || typeof record.reasoning_tokens !== "number"
132
+ || typeof record.total_tokens !== "number") {
133
+ return null;
134
+ }
135
+ return {
136
+ input_tokens: record.input_tokens,
137
+ output_tokens: record.output_tokens,
138
+ reasoning_tokens: record.reasoning_tokens,
139
+ total_tokens: record.total_tokens,
140
+ };
141
+ }
142
+ function normalizeContinuityState(state) {
143
+ const record = state && typeof state === "object"
144
+ ? state
145
+ : null;
146
+ return {
147
+ mustResolveBeforeHandoff: record?.mustResolveBeforeHandoff === true,
148
+ lastFriendActivityAt: typeof record?.lastFriendActivityAt === "string" ? record.lastFriendActivityAt : null,
149
+ };
150
+ }
151
+ function normalizeContent(content) {
152
+ if (content == null)
153
+ return null;
154
+ if (typeof content === "string")
155
+ return content;
156
+ if (!Array.isArray(content))
157
+ return null;
158
+ return content
159
+ .filter((part) => part != null && typeof part === "object")
160
+ .map((part) => ({ ...part }));
161
+ }
162
+ const SYNTHETIC_TIMESTAMP_PREFIX_RE = /^(?:(?:\[(?:just now|-\d+[mhd])\])\s*)+/i;
163
+ function stripSyntheticTimestampPrefix(text) {
164
+ return text.replace(SYNTHETIC_TIMESTAMP_PREFIX_RE, "");
165
+ }
166
+ function sanitizeConversationContent(role, content) {
167
+ if (role !== "user" && role !== "assistant")
168
+ return content;
169
+ if (typeof content === "string")
170
+ return stripSyntheticTimestampPrefix(content);
171
+ if (!Array.isArray(content))
172
+ return content;
173
+ return content.map((part) => {
174
+ if (part.type === "text" && typeof part.text === "string") {
175
+ return { ...part, text: stripSyntheticTimestampPrefix(part.text) };
176
+ }
177
+ return part;
178
+ });
179
+ }
180
+ function normalizeToolCalls(rawToolCalls) {
181
+ if (!Array.isArray(rawToolCalls))
182
+ return [];
183
+ return rawToolCalls
184
+ .filter((call) => call != null && typeof call === "object")
185
+ .map((call) => {
186
+ const fn = call.function;
187
+ const originalName = typeof fn?.name === "string" ? fn.name : "unknown";
188
+ const migratedName = TOOL_NAME_MIGRATIONS[originalName] ?? originalName;
189
+ return {
190
+ id: typeof call.id === "string" ? call.id : "",
191
+ type: typeof call.type === "string" ? call.type : "function",
192
+ function: {
193
+ name: migratedName,
194
+ arguments: typeof fn?.arguments === "string" ? fn.arguments : JSON.stringify(fn?.arguments ?? ""),
195
+ },
196
+ };
197
+ });
198
+ }
199
+ function normalizeRole(role) {
200
+ if (role === "developer")
201
+ return "system";
202
+ return role === "system" || role === "user" || role === "assistant" || role === "tool"
203
+ ? role
204
+ : "user";
205
+ }
206
+ function normalizeMessage(message) {
207
+ const record = message;
208
+ const role = normalizeRole(record.role);
209
+ const normalizedContent = sanitizeConversationContent(role, normalizeContent(record.content));
210
+ if (role === "assistant") {
211
+ return {
212
+ role,
213
+ content: normalizedContent,
214
+ name: typeof record.name === "string" ? record.name : null,
215
+ toolCallId: null,
216
+ toolCalls: normalizeToolCalls(record.tool_calls),
217
+ hadToolCallsField: Array.isArray(record.tool_calls),
218
+ };
219
+ }
220
+ if (role === "tool") {
221
+ return {
222
+ role,
223
+ content: typeof record.content === "string" ? record.content : "",
224
+ name: null,
225
+ toolCallId: typeof record.tool_call_id === "string" ? record.tool_call_id : null,
226
+ toolCalls: [],
227
+ hadToolCallsField: false,
228
+ };
229
+ }
230
+ return {
231
+ role,
232
+ content: normalizedContent ?? "",
233
+ name: typeof record.name === "string" ? record.name : null,
234
+ toolCallId: null,
235
+ toolCalls: [],
236
+ hadToolCallsField: false,
237
+ };
238
+ }
239
+ function contentText(content) {
240
+ if (typeof content === "string")
241
+ return content;
242
+ if (!Array.isArray(content))
243
+ return "";
244
+ return content
245
+ .map((part) => (part.type === "text" && typeof part.text === "string"
246
+ ? part.text
247
+ : ""))
248
+ .filter((text) => text.length > 0)
249
+ .join("");
250
+ }
251
+ function toProviderMessage(message) {
252
+ if (message.role === "assistant") {
253
+ const assistant = {
254
+ role: "assistant",
255
+ content: message.content,
256
+ };
257
+ if (message.name)
258
+ assistant.name = message.name;
259
+ if (message.hadToolCallsField || message.toolCalls.length > 0) {
260
+ assistant.tool_calls = message.toolCalls.map((call) => ({
261
+ id: call.id,
262
+ type: call.type,
263
+ function: {
264
+ name: call.function.name,
265
+ arguments: call.function.arguments,
266
+ },
267
+ }));
268
+ }
269
+ return assistant;
270
+ }
271
+ if (message.role === "tool") {
272
+ return {
273
+ role: "tool",
274
+ content: typeof message.content === "string" ? message.content : contentText(message.content),
275
+ tool_call_id: message.toolCallId ?? "",
276
+ };
277
+ }
278
+ if (message.role === "system") {
279
+ return {
280
+ role: "system",
281
+ content: typeof message.content === "string" ? message.content : contentText(message.content),
282
+ ...(message.name ? { name: message.name } : {}),
283
+ };
284
+ }
285
+ return {
286
+ role: "user",
287
+ content: (typeof message.content === "string" || Array.isArray(message.content) ? message.content : ""),
288
+ ...(message.name ? { name: message.name } : {}),
289
+ };
290
+ }
291
+ function messageFingerprint(message) {
292
+ const normalized = normalizeMessage(message);
293
+ return JSON.stringify({
294
+ role: normalized.role,
295
+ content: normalized.content,
296
+ name: normalized.name,
297
+ tool_call_id: normalized.toolCallId,
298
+ tool_calls: normalized.toolCalls,
299
+ });
300
+ }
301
+ function makeEventId(sequence) {
302
+ return `evt-${String(sequence).padStart(6, "0")}`;
303
+ }
304
+ /**
305
+ * Collapse duplicate event ids to a single entry, last-occurrence-wins.
306
+ *
307
+ * Concurrent writers in older versions of postTurnPersist could each load the
308
+ * envelope, compute `events.length + 1` for the next sequence, and both write
309
+ * an event with the same id. The duplicates would persist in the saved JSON
310
+ * and confuse downstream replay (the same outbound message could appear to
311
+ * have been sent twice from the agent's perspective without the agent knowing
312
+ * it sent it). We dedupe defensively on every load so corrupted sessions
313
+ * self-heal on the next save and so any future race produces a consistent
314
+ * view.
315
+ */
316
+ function dedupeEventsByIdLastWins(events) {
317
+ // Index id → last position so we can preserve original order while
318
+ // collapsing duplicates to their final occurrence.
319
+ const lastIndexById = new Map();
320
+ for (let i = 0; i < events.length; i++) {
321
+ lastIndexById.set(events[i].id, i);
322
+ }
323
+ return events.filter((event, index) => lastIndexById.get(event.id) === index);
324
+ }
325
+ /**
326
+ * The next sequence to assign for a freshly-built event. Uses max(existing
327
+ * sequences) + 1 rather than `events.length + 1` so that gaps from earlier
328
+ * pruning, archive replay, or self-heal dedup never produce a colliding id.
329
+ */
330
+ function nextEventSequence(existing) {
331
+ return existing.reduce((max, event) => Math.max(max, event.sequence), 0) + 1;
332
+ }
333
+ function validateSessionMessages(messages) {
334
+ const violations = [];
335
+ let prevNonToolRole = null;
336
+ let prevAssistantHadToolCalls = false;
337
+ let sawToolResultSincePrevAssistant = false;
338
+ for (let i = 0; i < messages.length; i++) {
339
+ const msg = normalizeMessage(messages[i]);
340
+ if (msg.role === "system")
341
+ continue;
342
+ if (msg.role === "tool") {
343
+ sawToolResultSincePrevAssistant = true;
344
+ continue;
345
+ }
346
+ if (msg.role === "assistant" && prevNonToolRole === "assistant") {
347
+ if (!(prevAssistantHadToolCalls && sawToolResultSincePrevAssistant)) {
348
+ violations.push(`back-to-back assistant at index ${i}`);
349
+ }
350
+ }
351
+ prevAssistantHadToolCalls = msg.role === "assistant" && msg.toolCalls.length > 0;
352
+ sawToolResultSincePrevAssistant = false;
353
+ prevNonToolRole = msg.role;
354
+ }
355
+ for (const collision of detectDuplicateToolCallIds(messages)) {
356
+ violations.push(`duplicate tool_call_id '${collision.id}' across assistant messages at indices ${collision.indices.join(", ")} — provider may reject (MiniMax canonicalizes call_function_<hash>_<n> across turns)`);
357
+ }
358
+ return violations;
359
+ }
360
+ /**
361
+ * Detect tool_call_ids that appear in more than one assistant message
362
+ * within the conversation. MiniMax-M2.7 in particular emits canonical
363
+ * ids of the form `call_function_<hash>_<n>` and reuses the same id
364
+ * across turns when the same function is called — which causes provider
365
+ * rejections on replay because tool_call_id is supposed to be unique
366
+ * per request. We don't (yet) rewrite these here; this function exists
367
+ * so the sanitize pipeline can surface the collision through nerves
368
+ * (`mind.session_invariant_violation`) and operators can decide.
369
+ *
370
+ * Same-message duplicates (one assistant calling the same id twice)
371
+ * are not collisions — they're a legitimate parallel call shape and
372
+ * would be handled by the assistant's own emit logic. We only flag
373
+ * cross-message reuse.
374
+ */
375
+ function detectDuplicateToolCallIds(messages) {
376
+ const idsByFirstIndex = new Map();
377
+ for (let i = 0; i < messages.length; i++) {
378
+ const msg = normalizeMessage(messages[i]);
379
+ if (msg.role !== "assistant")
380
+ continue;
381
+ const seenInThisMessage = new Set();
382
+ for (const call of msg.toolCalls) {
383
+ if (!call.id || seenInThisMessage.has(call.id))
384
+ continue;
385
+ seenInThisMessage.add(call.id);
386
+ const indices = idsByFirstIndex.get(call.id) ?? [];
387
+ indices.push(i);
388
+ idsByFirstIndex.set(call.id, indices);
389
+ }
390
+ }
391
+ const collisions = [];
392
+ for (const [id, indices] of idsByFirstIndex) {
393
+ if (indices.length > 1)
394
+ collisions.push({ id, indices });
395
+ }
396
+ return collisions;
397
+ }
398
+ function repairSessionMessages(messages) {
399
+ const normalized = messages.map(normalizeMessage);
400
+ const violations = validateSessionMessages(messages);
401
+ if (violations.length === 0)
402
+ return normalized.map(toProviderMessage);
403
+ const result = [];
404
+ let duplicateAssistantsDropped = 0;
405
+ for (const msg of normalized) {
406
+ if (msg.role === "assistant" && result.length > 0) {
407
+ const prev = result[result.length - 1];
408
+ if (prev.role === "assistant" && prev.toolCalls.length === 0) {
409
+ const prevContent = contentText(prev.content);
410
+ const curContent = contentText(msg.content);
411
+ // Drop the second of two consecutive assistants when the content is
412
+ // byte-identical (after trim) — that's a retry/double-persist artifact,
413
+ // not legitimate continuation. Concatenating them produced visible
414
+ // duplicate text in surfaces. Empty strings still concatenate (could
415
+ // be "" + real content).
416
+ if (prevContent.trim().length > 0 && prevContent.trim() === curContent.trim() && msg.toolCalls.length === 0) {
417
+ duplicateAssistantsDropped += 1;
418
+ continue;
419
+ }
420
+ prev.content = `${prevContent}\n\n${curContent}`;
421
+ continue;
422
+ }
423
+ }
424
+ result.push(msg);
425
+ }
426
+ if (duplicateAssistantsDropped > 0) {
427
+ (0, runtime_1.emitNervesEvent)({
428
+ level: "info",
429
+ event: "mind.session_duplicate_assistant_dropped",
430
+ component: "mind",
431
+ message: "dropped consecutive assistant messages with identical content (retry/double-persist artifact)",
432
+ meta: { count: duplicateAssistantsDropped },
433
+ });
434
+ }
435
+ (0, runtime_1.emitNervesEvent)({
436
+ level: "info",
437
+ event: "mind.session_invariant_repair",
438
+ component: "mind",
439
+ message: "repaired session invariant violations",
440
+ meta: { violations },
441
+ });
442
+ return result.map(toProviderMessage);
443
+ }
444
+ function repairToolCallSequences(messages, inlineReasoningStrippedCallIds = new Set()) {
445
+ const normalized = messages.map(normalizeMessage);
446
+ // Position-aware orphan detection. A tool result is orphaned if there is
447
+ // no preceding assistant message in the array whose tool_calls contain the
448
+ // matching id. (The previous logic checked all assistant messages
449
+ // globally, which kept tool results that appeared BEFORE their matching
450
+ // assistant — invalid order — and triggered MiniMax error 2013 on replay.)
451
+ let removed = 0;
452
+ const seenCallIds = new Set();
453
+ const repaired = normalized.filter((msg) => {
454
+ if (msg.role === "assistant") {
455
+ for (const tc of msg.toolCalls)
456
+ seenCallIds.add(tc.id);
457
+ return true;
458
+ }
459
+ if (msg.role !== "tool")
460
+ return true;
461
+ const keep = msg.toolCallId !== null && seenCallIds.has(msg.toolCallId);
462
+ if (!keep)
463
+ removed++;
464
+ return keep;
465
+ });
466
+ if (removed > 0) {
467
+ (0, runtime_1.emitNervesEvent)({
468
+ level: "info",
469
+ event: "mind.session_orphan_tool_result_repair",
470
+ component: "mind",
471
+ message: "removed orphaned tool results from session history",
472
+ meta: { removed },
473
+ });
474
+ }
475
+ let injected = 0;
476
+ for (let i = 0; i < repaired.length; i++) {
477
+ const msg = repaired[i];
478
+ if (msg.role !== "assistant" || msg.toolCalls.length === 0)
479
+ continue;
480
+ const resultIds = new Set();
481
+ for (let j = i + 1; j < repaired.length; j++) {
482
+ const following = repaired[j];
483
+ if (following.role === "tool" && following.toolCallId !== null) {
484
+ resultIds.add(following.toolCallId);
485
+ continue;
486
+ }
487
+ if (following.role === "assistant" || following.role === "user")
488
+ break;
489
+ }
490
+ const missing = msg.toolCalls.filter((toolCall) => !resultIds.has(toolCall.id));
491
+ if (missing.length === 0)
492
+ continue;
493
+ const syntheticResults = missing.map((toolCall) => ({
494
+ role: "tool",
495
+ content: buildSyntheticToolResultMessage(toolCall.id, inlineReasoningStrippedCallIds),
496
+ name: null,
497
+ toolCallId: toolCall.id,
498
+ toolCalls: [],
499
+ hadToolCallsField: false,
500
+ }));
501
+ let insertAt = i + 1;
502
+ while (insertAt < repaired.length && repaired[insertAt].role === "tool")
503
+ insertAt++;
504
+ repaired.splice(insertAt, 0, ...syntheticResults);
505
+ injected += syntheticResults.length;
506
+ }
507
+ if (injected > 0) {
508
+ (0, runtime_1.emitNervesEvent)({
509
+ level: "info",
510
+ event: "mind.session_orphan_tool_call_repair",
511
+ component: "mind",
512
+ message: "injected synthetic tool results for orphaned tool calls",
513
+ meta: { injected },
514
+ });
515
+ }
516
+ return repaired.map(toProviderMessage);
517
+ }
518
+ function canonicalizeSystemMessageSequence(messages) {
519
+ const normalized = messages.map(normalizeMessage);
520
+ const firstSystemIndex = normalized.findIndex((msg) => msg.role === "system");
521
+ if (firstSystemIndex === -1)
522
+ return normalized.map(toProviderMessage);
523
+ const extraSystemCount = normalized.filter((msg) => msg.role === "system").length - 1;
524
+ if (firstSystemIndex === 0 && extraSystemCount === 0) {
525
+ return normalized.map(toProviderMessage);
526
+ }
527
+ const primarySystem = normalized[firstSystemIndex];
528
+ const nonSystemMessages = normalized.filter((msg) => msg.role !== "system");
529
+ const repaired = [primarySystem, ...nonSystemMessages].map(toProviderMessage);
530
+ (0, runtime_1.emitNervesEvent)({
531
+ level: "info",
532
+ event: "mind.session_system_prompt_repair",
533
+ component: "mind",
534
+ message: "canonicalized session system prompt sequence",
535
+ meta: {
536
+ firstSystemIndex,
537
+ extraSystemCount,
538
+ finalMessageCount: repaired.length,
539
+ },
540
+ });
541
+ return repaired;
542
+ }
543
+ function migrateToolNames(messages) {
544
+ const safeMessages = messages.filter((message) => Boolean(message) && typeof message === "object");
545
+ let migrated = 0;
546
+ for (const message of safeMessages) {
547
+ const record = message;
548
+ if (record.role !== "assistant" || !Array.isArray(record.tool_calls))
549
+ continue;
550
+ for (const toolCall of record.tool_calls) {
551
+ if (!toolCall || typeof toolCall !== "object")
552
+ continue;
553
+ const toolRecord = toolCall;
554
+ if (toolRecord.type !== "function")
555
+ continue;
556
+ const originalName = toolRecord.function?.name;
557
+ if (typeof originalName !== "string")
558
+ continue;
559
+ if (TOOL_NAME_MIGRATIONS[originalName])
560
+ migrated += 1;
561
+ }
562
+ }
563
+ if (migrated > 0) {
564
+ (0, runtime_1.emitNervesEvent)({
565
+ level: "info",
566
+ event: "mind.session_tool_name_migration",
567
+ component: "mind",
568
+ message: "migrated deprecated tool names in session history",
569
+ meta: { migrated },
570
+ });
571
+ }
572
+ return safeMessages.map(normalizeMessage).map(toProviderMessage);
573
+ }
574
+ /**
575
+ * Strip inline `<think>...</think>` blocks from a string. Mirrors the
576
+ * helper at senses/shared-turn.ts (operator-facing) and core.ts
577
+ * (live-turn) — kept inline here because session-events.ts is the load-
578
+ * time repair path and needs its own copy to avoid sense/heart import
579
+ * cycles. If the close tag is missing, drops everything from the open
580
+ * tag onward.
581
+ */
582
+ function stripInlineThinkBlocks(input) {
583
+ let out = input;
584
+ for (;;) {
585
+ const open = out.indexOf("<think>");
586
+ if (open === -1)
587
+ break;
588
+ const close = out.indexOf("</think>", open + "<think>".length);
589
+ if (close === -1) {
590
+ out = out.slice(0, open);
591
+ break;
592
+ }
593
+ out = out.slice(0, open) + out.slice(close + "</think>".length);
594
+ }
595
+ return out.trim();
596
+ }
597
+ /**
598
+ * Strip inline `<think>` content from any assistant message that ALSO has
599
+ * tool_calls. MiniMax-style models persist think-content + tool_calls on
600
+ * the same assistant turn; replaying that combination triggers MiniMax
601
+ * error 2013 ("tool result's tool id not found") and stalls the session.
602
+ *
603
+ * AX requirement: the agent MUST see that this happened. We don't silently
604
+ * paper over their previous turn — we strip for replay correctness AND
605
+ * collect the affected tool_call_ids in `inlineReasoningStrippedCallIds`
606
+ * so the downstream synthetic-tool-result repair can produce an
607
+ * explanatory message addressed to those specific calls. The agent sees:
608
+ * "your previous tool call's result was lost because the assistant message
609
+ * had inline reasoning blocks the provider couldn't replay — here's what
610
+ * happened, retry if needed." Full awareness, no silent corrections.
611
+ *
612
+ * This load-time repair self-heals existing sessions that were saved
613
+ * before the persist-time strip in core.ts landed.
614
+ */
615
+ function repairInlineReasoningOnReplay(messages, inlineReasoningStrippedCallIds) {
616
+ let repaired = 0;
617
+ const result = messages.map((msg) => {
618
+ if (msg.role !== "assistant")
619
+ return msg;
620
+ const a = msg;
621
+ if (!a.tool_calls || a.tool_calls.length === 0)
622
+ return msg;
623
+ if (typeof a.content !== "string")
624
+ return msg;
625
+ if (!a.content.includes("<think>"))
626
+ return msg;
627
+ const stripped = stripInlineThinkBlocks(a.content);
628
+ repaired++;
629
+ for (const tc of a.tool_calls)
630
+ inlineReasoningStrippedCallIds.add(tc.id);
631
+ return { ...a, content: stripped.length > 0 ? stripped : null };
632
+ });
633
+ if (repaired > 0) {
634
+ (0, runtime_1.emitNervesEvent)({
635
+ level: "info",
636
+ event: "mind.session_inline_reasoning_repair",
637
+ component: "mind",
638
+ message: "stripped inline <think> blocks from assistant messages with tool_calls so replay is valid; agent will see explanatory tool-result messages",
639
+ meta: { repaired, affectedCallIds: inlineReasoningStrippedCallIds.size },
640
+ });
641
+ }
642
+ return result;
643
+ }
644
+ /**
645
+ * Compose the synthetic tool-result message the agent sees when their
646
+ * previous turn's tool call has no matching tool result. The default
647
+ * message tells the agent what happened (turn ended early, result lost)
648
+ * and what to do (retry if the work isn't done). When the parent
649
+ * assistant message had inline `<think>` reasoning that the provider
650
+ * rejected, the message is more specific so the agent can adjust.
651
+ *
652
+ * AX rule: every repair must produce a message the agent can read and
653
+ * act on. Silent strips are never OK.
654
+ */
655
+ function buildSyntheticToolResultMessage(toolCallId, inlineReasoningStrippedCallIds) {
656
+ if (inlineReasoningStrippedCallIds.has(toolCallId)) {
657
+ return [
658
+ "error: this tool call's result was lost.",
659
+ "your previous assistant turn included inline `<think>...</think>` reasoning alongside tool_calls,",
660
+ "and the provider (likely MiniMax) rejects that combination on replay (error 2013).",
661
+ "the harness has stripped the inline reasoning from the persisted content so the next replay is valid;",
662
+ "your reasoning trace itself is preserved out-of-band and not lost.",
663
+ "if the underlying work still needs to be done, retry the tool call now —",
664
+ "the call may not have run, or it ran but the result didn't reach you.",
665
+ ].join(" ");
666
+ }
667
+ return [
668
+ "error: this tool call's result was lost — the previous turn ended before the tool finished",
669
+ "(provider rejection, daemon interrupt, or the tool itself errored).",
670
+ "if the work needs to be done, retry the tool call now.",
671
+ ].join(" ");
672
+ }
673
+ function sanitizeProviderMessages(messages) {
674
+ const safeMessages = messages.filter((message) => Boolean(message) && typeof message === "object");
675
+ const normalized = safeMessages.map(normalizeMessage);
676
+ const violations = validateSessionMessages(safeMessages);
677
+ if (violations.length > 0) {
678
+ (0, runtime_1.emitNervesEvent)({
679
+ level: "info",
680
+ event: "mind.session_invariant_violation",
681
+ component: "mind",
682
+ message: "session invariant violated",
683
+ meta: { violations },
684
+ });
685
+ }
686
+ // Track which tool_call_ids belonged to assistant messages whose inline
687
+ // reasoning we just stripped. The synthetic-tool-result repair downstream
688
+ // uses this set to produce an explanatory message for those calls so the
689
+ // agent has full awareness of what happened.
690
+ const inlineReasoningStrippedCallIds = new Set();
691
+ return canonicalizeSystemMessageSequence(migrateToolNames(repairToolCallSequences(repairInlineReasoningOnReplay(repairSessionMessages(normalized.map(toProviderMessage)), inlineReasoningStrippedCallIds), inlineReasoningStrippedCallIds)));
692
+ }
693
+ function stampIngressTime(msg) {
694
+ msg._ingressAt = new Date().toISOString();
695
+ }
696
+ function getIngressTime(msg) {
697
+ const value = msg._ingressAt;
698
+ return typeof value === "string" ? value : null;
699
+ }
700
+ function createEventTime(role, recordedAt, captureKind, ingressAt) {
701
+ if (captureKind === "migration") {
702
+ return {
703
+ authoredAt: null,
704
+ authoredAtSource: "migration",
705
+ observedAt: null,
706
+ observedAtSource: "migration",
707
+ recordedAt,
708
+ recordedAtSource: "migration",
709
+ };
710
+ }
711
+ if (role === "user") {
712
+ return {
713
+ authoredAt: null,
714
+ authoredAtSource: "unknown",
715
+ observedAt: ingressAt ?? recordedAt,
716
+ observedAtSource: "ingest",
717
+ recordedAt,
718
+ recordedAtSource: "save",
719
+ };
720
+ }
721
+ return {
722
+ authoredAt: recordedAt,
723
+ authoredAtSource: "local",
724
+ observedAt: recordedAt,
725
+ observedAtSource: "local",
726
+ recordedAt,
727
+ recordedAtSource: "save",
728
+ };
729
+ }
730
+ function buildEventFromMessage(message, sequence, recordedAt, captureKind, sourceMessageIndex, legacyVersion, ingressAt) {
731
+ const normalized = normalizeMessage(message);
732
+ const role = normalized.role;
733
+ const cappedContent = truncateLargeEventContent(normalized.content, exports.EVENT_CONTENT_MAX_CHARS).content;
734
+ return {
735
+ id: makeEventId(sequence),
736
+ sequence,
737
+ role,
738
+ content: cappedContent,
739
+ name: normalized.name,
740
+ toolCallId: role === "tool" ? normalized.toolCallId : null,
741
+ toolCalls: role === "assistant" ? normalized.toolCalls : [],
742
+ attachments: [],
743
+ time: createEventTime(role, recordedAt, captureKind, ingressAt),
744
+ relations: {
745
+ replyToEventId: null,
746
+ threadRootEventId: null,
747
+ references: [],
748
+ toolCallId: role === "tool" ? normalized.toolCallId : null,
749
+ supersedesEventId: null,
750
+ redactsEventId: null,
751
+ },
752
+ provenance: {
753
+ captureKind,
754
+ legacyVersion,
755
+ sourceMessageIndex,
756
+ },
757
+ };
758
+ }
759
+ function projectProviderMessages(envelope) {
760
+ const eventIds = envelope.projection.eventIds.length > 0
761
+ ? envelope.projection.eventIds
762
+ : envelope.events.map((event) => event.id);
763
+ const byId = new Map(envelope.events.map((event) => [event.id, event]));
764
+ return eventIds
765
+ .map((id) => byId.get(id))
766
+ .filter((event) => Boolean(event))
767
+ .map((event) => toProviderMessage({
768
+ role: event.role,
769
+ content: event.content,
770
+ name: event.name,
771
+ toolCallId: event.toolCallId,
772
+ toolCalls: event.toolCalls,
773
+ hadToolCallsField: event.toolCalls.length > 0,
774
+ }));
775
+ }
776
+ /**
777
+ * Annotate user and assistant messages with a relative time offset tag.
778
+ * System and tool messages are untouched.
779
+ */
780
+ function annotateMessageTimestamps(envelope, messages, nowMs = Date.now()) {
781
+ const eventIds = envelope.projection.eventIds.length > 0
782
+ ? envelope.projection.eventIds
783
+ : envelope.events.map((event) => event.id);
784
+ const byId = new Map(envelope.events.map((event) => [event.id, event]));
785
+ const events = eventIds
786
+ .map((id) => byId.get(id))
787
+ .filter((event) => Boolean(event));
788
+ return messages.map((msg, i) => {
789
+ const event = events[i];
790
+ if (!event)
791
+ return msg;
792
+ if (event.role !== "user" && event.role !== "assistant")
793
+ return msg;
794
+ const ts = bestEventTimestamp(event);
795
+ const elapsed = nowMs - Date.parse(ts);
796
+ if (elapsed < 0)
797
+ return msg;
798
+ const tag = elapsed < 60000 ? "[just now]" : `[-${formatElapsedCompact(elapsed)}]`;
799
+ if (typeof msg.content === "string" && msg.content.length > 0) {
800
+ return { ...msg, content: `${tag} ${msg.content}` };
801
+ }
802
+ return msg;
803
+ });
804
+ }
805
+ /** Compact elapsed format for message annotations: "3m", "2h", "1d". */
806
+ function formatElapsedCompact(ms) {
807
+ const minutes = Math.max(1, Math.floor(ms / 60000));
808
+ if (minutes < 60)
809
+ return `${minutes}m`;
810
+ const hours = Math.floor(minutes / 60);
811
+ if (hours < 24)
812
+ return `${hours}h`;
813
+ const days = Math.floor(hours / 24);
814
+ return `${days}d`;
815
+ }
816
+ function bestEventTimestamp(event) {
817
+ return event.time.authoredAt ?? event.time.observedAt ?? event.time.recordedAt;
818
+ }
819
+ function formatSessionEventTimestamp(event) {
820
+ const iso = bestEventTimestamp(event);
821
+ const date = new Date(iso);
822
+ const year = date.getUTCFullYear();
823
+ const month = String(date.getUTCMonth() + 1).padStart(2, "0");
824
+ const day = String(date.getUTCDate()).padStart(2, "0");
825
+ const hour = String(date.getUTCHours()).padStart(2, "0");
826
+ const minute = String(date.getUTCMinutes()).padStart(2, "0");
827
+ return `${year}-${month}-${day} ${hour}:${minute}`;
828
+ }
829
+ function extractEventText(event) {
830
+ return contentText(event.content);
831
+ }
832
+ function deriveSessionChronology(events) {
833
+ let lastInboundAt = null;
834
+ let lastOutboundAt = null;
835
+ let lastActivityAt = null;
836
+ let lastAssistantSequence = -1;
837
+ for (const event of events) {
838
+ if (event.role === "system")
839
+ continue;
840
+ const at = bestEventTimestamp(event);
841
+ lastActivityAt = at;
842
+ if (event.role === "user") {
843
+ lastInboundAt = at;
844
+ }
845
+ if (event.role === "assistant") {
846
+ lastOutboundAt = at;
847
+ lastAssistantSequence = event.sequence;
848
+ }
849
+ }
850
+ const unansweredInboundCount = events.filter((event) => event.role === "user" && event.sequence > lastAssistantSequence).length;
851
+ return {
852
+ lastInboundAt,
853
+ lastOutboundAt,
854
+ lastActivityAt,
855
+ unansweredInboundCount,
856
+ };
857
+ }
858
+ function describeCurrentSessionTiming(events, nowMs = Date.now()) {
859
+ const chronology = deriveSessionChronology(events);
860
+ const parts = [];
861
+ if (chronology.lastInboundAt) {
862
+ parts.push(`last inbound ${formatElapsed(nowMs - Date.parse(chronology.lastInboundAt))}`);
863
+ }
864
+ if (chronology.lastOutboundAt) {
865
+ parts.push(`i last replied ${formatElapsed(nowMs - Date.parse(chronology.lastOutboundAt))}`);
866
+ }
867
+ if (chronology.unansweredInboundCount > 0) {
868
+ const count = chronology.unansweredInboundCount;
869
+ parts.push(`${count} unanswered inbound message${count === 1 ? "" : "s"}`);
870
+ }
871
+ return parts.length > 0 ? `current thread: ${parts.join("; ")}` : "";
872
+ }
873
+ function migrateLegacySessionEnvelope(raw, options) {
874
+ if (!raw || typeof raw !== "object")
875
+ return null;
876
+ const legacy = raw;
877
+ const looksLegacy = legacy.version === 1
878
+ || (legacy.version == null && ("messages" in legacy || "lastUsage" in legacy || "state" in legacy));
879
+ if (!looksLegacy)
880
+ return null;
881
+ const messages = Array.isArray(legacy.messages)
882
+ ? sanitizeProviderMessages(legacy.messages)
883
+ : [];
884
+ const recordedAt = options.fileMtimeAt ?? options.recordedAt;
885
+ const events = messages.map((message, index) => buildEventFromMessage(message, index + 1, recordedAt, "migration", index, 1));
886
+ return {
887
+ version: 2,
888
+ events,
889
+ projection: {
890
+ eventIds: events.map((event) => event.id),
891
+ trimmed: false,
892
+ maxTokens: null,
893
+ contextMargin: null,
894
+ inputTokens: null,
895
+ projectedAt: recordedAt,
896
+ },
897
+ structuredOutputs: (0, structured_output_1.extractStructuredOutputsFromEvents)(events, { emitTelemetry: false }),
898
+ lastUsage: normalizeUsage(legacy.lastUsage),
899
+ state: normalizeContinuityState(legacy.state),
900
+ };
901
+ }
902
+ function parseSessionEnvelope(raw, options = {}) {
903
+ const recordedAt = options.recordedAt ?? new Date().toISOString();
904
+ const fileMtimeAt = options.fileMtimeAt ?? null;
905
+ const migrated = migrateLegacySessionEnvelope(raw, { recordedAt, fileMtimeAt });
906
+ if (migrated)
907
+ return migrated;
908
+ if (!raw || typeof raw !== "object")
909
+ return null;
910
+ const record = raw;
911
+ if (record.version !== 2 || !Array.isArray(record.events) || !record.projection || typeof record.projection !== "object") {
912
+ return null;
913
+ }
914
+ const rawEvents = record.events
915
+ .filter((event) => event != null && typeof event === "object")
916
+ .map((event, index) => {
917
+ const role = normalizeRole(event.role);
918
+ const time = event.time;
919
+ const relations = event.relations;
920
+ const provenance = event.provenance;
921
+ const content = sanitizeConversationContent(role, normalizeContent(event.content));
922
+ return {
923
+ id: typeof event.id === "string" ? event.id : makeEventId(index + 1),
924
+ sequence: typeof event.sequence === "number" ? event.sequence : index + 1,
925
+ role,
926
+ content,
927
+ name: typeof event.name === "string" ? event.name : null,
928
+ toolCallId: typeof event.toolCallId === "string" ? event.toolCallId : null,
929
+ toolCalls: normalizeToolCalls(event.toolCalls),
930
+ attachments: Array.isArray(event.attachments) ? event.attachments.filter((item) => typeof item === "string") : [],
931
+ time: {
932
+ authoredAt: typeof time?.authoredAt === "string" ? time.authoredAt : null,
933
+ authoredAtSource: typeof time?.authoredAtSource === "string" ? time.authoredAtSource : "unknown",
934
+ observedAt: typeof time?.observedAt === "string" ? time.observedAt : null,
935
+ observedAtSource: typeof time?.observedAtSource === "string" ? time.observedAtSource : "unknown",
936
+ recordedAt: typeof time?.recordedAt === "string" ? time.recordedAt : recordedAt,
937
+ recordedAtSource: typeof time?.recordedAtSource === "string" ? time.recordedAtSource : "save",
938
+ },
939
+ relations: {
940
+ replyToEventId: typeof relations?.replyToEventId === "string" ? relations.replyToEventId : null,
941
+ threadRootEventId: typeof relations?.threadRootEventId === "string" ? relations.threadRootEventId : null,
942
+ references: Array.isArray(relations?.references) ? relations.references.filter((item) => typeof item === "string") : [],
943
+ toolCallId: typeof relations?.toolCallId === "string" ? relations.toolCallId : null,
944
+ supersedesEventId: typeof relations?.supersedesEventId === "string" ? relations.supersedesEventId : null,
945
+ redactsEventId: typeof relations?.redactsEventId === "string" ? relations.redactsEventId : null,
946
+ },
947
+ provenance: {
948
+ captureKind: typeof provenance?.captureKind === "string" ? provenance.captureKind : "live",
949
+ legacyVersion: typeof provenance?.legacyVersion === "number" ? provenance.legacyVersion : null,
950
+ sourceMessageIndex: typeof provenance?.sourceMessageIndex === "number" ? provenance.sourceMessageIndex : null,
951
+ },
952
+ };
953
+ });
954
+ // Self-heal duplicate event ids that may have been written by concurrent
955
+ // writers in older harness versions. Last-occurrence-wins by id (later
956
+ // entries in the persisted file are the more recent state for that id).
957
+ // We preserve the original document order otherwise, so projection.eventIds
958
+ // still resolves predictably.
959
+ const events = dedupeEventsByIdLastWins(rawEvents);
960
+ const projection = record.projection;
961
+ return {
962
+ version: 2,
963
+ events,
964
+ projection: {
965
+ eventIds: Array.isArray(projection.eventIds) ? projection.eventIds.filter((item) => typeof item === "string") : [],
966
+ trimmed: projection.trimmed === true,
967
+ maxTokens: typeof projection.maxTokens === "number" ? projection.maxTokens : null,
968
+ contextMargin: typeof projection.contextMargin === "number" ? projection.contextMargin : null,
969
+ inputTokens: typeof projection.inputTokens === "number" ? projection.inputTokens : null,
970
+ projectedAt: typeof projection.projectedAt === "string" ? projection.projectedAt : null,
971
+ },
972
+ structuredOutputs: record.structuredOutputs === undefined
973
+ ? (0, structured_output_1.extractStructuredOutputsFromEvents)(events, { emitTelemetry: false })
974
+ : (0, structured_output_1.normalizeStructuredOutputs)(record.structuredOutputs),
975
+ lastUsage: normalizeUsage(record.lastUsage),
976
+ state: normalizeContinuityState(record.state),
977
+ };
978
+ }
979
+ function loadSessionEnvelopeFile(filePath) {
980
+ try {
981
+ const raw = fs.readFileSync(filePath, "utf-8");
982
+ let mtime;
983
+ try {
984
+ mtime = fs.statSync(filePath).mtime.toISOString();
985
+ }
986
+ catch {
987
+ mtime = new Date().toISOString();
988
+ }
989
+ return parseSessionEnvelope(JSON.parse(raw), {
990
+ recordedAt: mtime,
991
+ fileMtimeAt: mtime,
992
+ });
993
+ }
994
+ catch {
995
+ return null;
996
+ }
997
+ }
998
+ function messageRole(msg) {
999
+ return normalizeRole(msg.role);
1000
+ }
1001
+ function filterNonSystem(messages) {
1002
+ return messages.filter((msg) => messageRole(msg) !== "system");
1003
+ }
1004
+ /**
1005
+ * Compare two message arrays by their non-system messages only.
1006
+ * Returns the number of matching non-system messages from the start.
1007
+ * System messages (whose content changes every turn due to live world-state)
1008
+ * are excluded so that prefix matching is not defeated by system prompt updates.
1009
+ */
1010
+ function findCommonPrefixLength(a, b) {
1011
+ const aNonSys = filterNonSystem(a);
1012
+ const bNonSys = filterNonSystem(b);
1013
+ const max = Math.min(aNonSys.length, bNonSys.length);
1014
+ for (let i = 0; i < max; i++) {
1015
+ if (messageFingerprint(aNonSys[i]) !== messageFingerprint(bNonSys[i]))
1016
+ return i;
1017
+ }
1018
+ return max;
1019
+ }
1020
+ function selectProjectedEventIds(currentMessages, currentEventIds, trimmedMessages) {
1021
+ if (trimmedMessages.length === 0)
1022
+ return [];
1023
+ const result = [];
1024
+ let needle = 0;
1025
+ let trimmedFingerprint = null;
1026
+ for (let i = 0; i < currentMessages.length && needle < trimmedMessages.length; i++) {
1027
+ const currentMessage = currentMessages[i];
1028
+ const trimmedMessage = trimmedMessages[needle];
1029
+ if (currentMessage !== trimmedMessage) {
1030
+ trimmedFingerprint ??= messageFingerprint(trimmedMessage);
1031
+ if (messageFingerprint(currentMessage) !== trimmedFingerprint)
1032
+ continue;
1033
+ }
1034
+ result.push(currentEventIds[i]);
1035
+ needle++;
1036
+ trimmedFingerprint = null;
1037
+ }
1038
+ return result;
1039
+ }
1040
+ function buildCanonicalSessionEnvelope(options) {
1041
+ const existing = options.existing;
1042
+ // Callers pass pre-sanitized messages + pre-captured ingress times.
1043
+ const currentIngressTimes = options.currentIngressTimes ?? options.currentMessages.map(getIngressTime);
1044
+ const previousMessages = options.previousMessages;
1045
+ const currentMessages = options.currentMessages;
1046
+ const trimmedMessages = options.trimmedMessages;
1047
+ const previousProjectionIds = existing?.projection.eventIds.length
1048
+ ? [...existing.projection.eventIds]
1049
+ : existing?.events.map((event) => event.id) ?? [];
1050
+ // Compare only non-system messages to find the common prefix.
1051
+ // System messages change every turn (live world-state in system prompt)
1052
+ // and must not defeat prefix matching of the actual conversation.
1053
+ const nonSystemPrefix = findCommonPrefixLength(previousMessages, currentMessages);
1054
+ // Build a lookup of non-system previous projection IDs.
1055
+ const prevNonSystemIds = [];
1056
+ for (let i = 0; i < previousMessages.length; i++) {
1057
+ if (messageRole(previousMessages[i]) !== "system") {
1058
+ prevNonSystemIds.push(previousProjectionIds[i]);
1059
+ }
1060
+ }
1061
+ // Walk currentMessages and build currentEventIds + new events.
1062
+ // Non-system messages within the prefix reuse old event IDs.
1063
+ // System messages and post-prefix messages get new events.
1064
+ const events = [...(existing?.events ?? [])];
1065
+ const currentEventIds = [];
1066
+ let nonSystemSeen = 0;
1067
+ for (let i = 0; i < currentMessages.length; i++) {
1068
+ const role = messageRole(currentMessages[i]);
1069
+ const isSystem = role === "system";
1070
+ const inPrefix = !isSystem && nonSystemSeen < nonSystemPrefix;
1071
+ if (inPrefix) {
1072
+ // Reuse existing event ID for this matched non-system message
1073
+ currentEventIds.push(prevNonSystemIds[nonSystemSeen]);
1074
+ nonSystemSeen++;
1075
+ }
1076
+ else if (isSystem && i < previousMessages.length
1077
+ && messageRole(previousMessages[i]) === "system"
1078
+ && messageFingerprint(currentMessages[i]) === messageFingerprint(previousMessages[i])) {
1079
+ // System message at same position with identical content -- reuse event ID
1080
+ currentEventIds.push(previousProjectionIds[i]);
1081
+ }
1082
+ else {
1083
+ if (!isSystem)
1084
+ nonSystemSeen++;
1085
+ // Create a new event. Use nextEventSequence(events) instead of
1086
+ // `events.length + 1` so that any gap (from pruning, archive replay,
1087
+ // or self-heal dedup) cannot collide with an existing id.
1088
+ const event = buildEventFromMessage(currentMessages[i], nextEventSequence(events), options.recordedAt, "live", null, null, currentIngressTimes[i]);
1089
+ events.push(event);
1090
+ currentEventIds.push(event.id);
1091
+ }
1092
+ }
1093
+ const projectionEventIds = selectProjectedEventIds(currentMessages, currentEventIds, trimmedMessages);
1094
+ // Prune events: only keep events whose IDs are in the projection.
1095
+ // Events not in projection are returned as evicted for archiving.
1096
+ const projectionIdSet = new Set(projectionEventIds);
1097
+ const prunedEvents = events.filter((event) => projectionIdSet.has(event.id));
1098
+ const evictedEvents = events.filter((event) => !projectionIdSet.has(event.id));
1099
+ return {
1100
+ envelope: {
1101
+ version: 2,
1102
+ events: prunedEvents,
1103
+ projection: {
1104
+ eventIds: projectionEventIds,
1105
+ trimmed: projectionEventIds.length < currentEventIds.length,
1106
+ maxTokens: options.projectionBasis.maxTokens,
1107
+ contextMargin: options.projectionBasis.contextMargin,
1108
+ inputTokens: options.projectionBasis.inputTokens,
1109
+ projectedAt: options.recordedAt,
1110
+ },
1111
+ structuredOutputs: (0, structured_output_1.extractStructuredOutputsFromEvents)(prunedEvents),
1112
+ lastUsage: normalizeUsage(options.lastUsage),
1113
+ state: normalizeContinuityState(options.state),
1114
+ },
1115
+ evictedEvents,
1116
+ };
1117
+ }
1118
+ function agentFromSessionPath(sessPath) {
1119
+ const match = sessPath.match(/(?:^|[/\\])AgentBundles[/\\]([^/\\]+)\.ouro(?:[/\\]|$)/);
1120
+ return match?.[1] ?? "unknown";
1121
+ }
1122
+ /**
1123
+ * Archive writes are intentionally disabled. The session envelope remains the
1124
+ * bounded working-memory record; evicted events are no longer persisted to an
1125
+ * unbounded sidecar.
1126
+ */
1127
+ function appendEvictedToArchive(sessPath, evictedEvents) {
1128
+ if (evictedEvents.length === 0)
1129
+ return;
1130
+ if (!archiveDisabledEmitted) {
1131
+ archiveDisabledEmitted = true;
1132
+ (0, runtime_1.emitNervesEvent)({
1133
+ component: "heart",
1134
+ event: "heart.session_archive_disabled",
1135
+ message: "session archive append disabled",
1136
+ meta: {
1137
+ type: "session_archive_disabled",
1138
+ agent: agentFromSessionPath(sessPath),
1139
+ sessionPath: sessPath,
1140
+ evictedCount: evictedEvents.length,
1141
+ ts: new Date().toISOString(),
1142
+ },
1143
+ });
1144
+ }
1145
+ }
1146
+ function appendSyntheticAssistantEvent(envelope, content, recordedAt) {
1147
+ // Use nextEventSequence(events) instead of `events.length + 1` so any gap
1148
+ // (from pruning, archive replay, or self-heal dedup) cannot collide with
1149
+ // an existing event id. Same fix pattern as line 1046.
1150
+ const sequence = nextEventSequence(envelope.events);
1151
+ const event = buildEventFromMessage({ role: "assistant", content }, sequence, recordedAt, "synthetic", null, null);
1152
+ return {
1153
+ ...envelope,
1154
+ events: [...envelope.events, event],
1155
+ structuredOutputs: (0, structured_output_1.extractStructuredOutputsFromEvents)([...envelope.events, event]),
1156
+ projection: {
1157
+ ...envelope.projection,
1158
+ eventIds: [...envelope.projection.eventIds, event.id],
1159
+ projectedAt: recordedAt,
1160
+ trimmed: false,
1161
+ },
1162
+ };
1163
+ }