@vellumai/assistant 0.11.9 → 0.11.10-dev.202609091114.7b6342c

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 (433) hide show
  1. package/ARCHITECTURE.md +2 -2
  2. package/Dockerfile +6 -12
  3. package/docs/architecture/memory.md +17 -9
  4. package/docs/guardian-request-flow.md +7 -4
  5. package/docs/runbook-trusted-contacts.md +13 -2
  6. package/node_modules/@vellumai/ces-client/node_modules/@vellumai/service-contracts/package.json +3 -1
  7. package/node_modules/@vellumai/ces-client/node_modules/@vellumai/service-contracts/src/__tests__/plan-credit.test.ts +51 -0
  8. package/node_modules/@vellumai/ces-client/node_modules/@vellumai/service-contracts/src/__tests__/secret-detection.test.ts +2 -0
  9. package/node_modules/@vellumai/ces-client/node_modules/@vellumai/service-contracts/src/desktop-presence.ts +11 -0
  10. package/node_modules/@vellumai/ces-client/node_modules/@vellumai/service-contracts/src/index.ts +1 -0
  11. package/node_modules/@vellumai/ces-client/node_modules/@vellumai/service-contracts/src/plan-credit.ts +66 -0
  12. package/node_modules/@vellumai/ces-client/node_modules/@vellumai/service-contracts/src/secret-detection.ts +7 -0
  13. package/node_modules/@vellumai/gateway-client/node_modules/@vellumai/service-contracts/package.json +3 -1
  14. package/node_modules/@vellumai/gateway-client/node_modules/@vellumai/service-contracts/src/__tests__/plan-credit.test.ts +51 -0
  15. package/node_modules/@vellumai/gateway-client/node_modules/@vellumai/service-contracts/src/__tests__/secret-detection.test.ts +2 -0
  16. package/node_modules/@vellumai/gateway-client/node_modules/@vellumai/service-contracts/src/desktop-presence.ts +11 -0
  17. package/node_modules/@vellumai/gateway-client/node_modules/@vellumai/service-contracts/src/index.ts +1 -0
  18. package/node_modules/@vellumai/gateway-client/node_modules/@vellumai/service-contracts/src/plan-credit.ts +66 -0
  19. package/node_modules/@vellumai/gateway-client/node_modules/@vellumai/service-contracts/src/secret-detection.ts +7 -0
  20. package/node_modules/@vellumai/gateway-client/src/gateway-ipc-contracts.ts +72 -0
  21. package/node_modules/@vellumai/service-contracts/package.json +3 -1
  22. package/node_modules/@vellumai/service-contracts/src/__tests__/plan-credit.test.ts +51 -0
  23. package/node_modules/@vellumai/service-contracts/src/__tests__/secret-detection.test.ts +2 -0
  24. package/node_modules/@vellumai/service-contracts/src/desktop-presence.ts +11 -0
  25. package/node_modules/@vellumai/service-contracts/src/index.ts +1 -0
  26. package/node_modules/@vellumai/service-contracts/src/plan-credit.ts +66 -0
  27. package/node_modules/@vellumai/service-contracts/src/secret-detection.ts +7 -0
  28. package/openapi.yaml +729 -63
  29. package/package.json +1 -1
  30. package/src/__tests__/agent-loop-mutable-latest-user-message.test.ts +19 -21
  31. package/src/__tests__/assistant-event-hub-machine-name.test.ts +40 -0
  32. package/src/__tests__/assistant-event-hub.test.ts +45 -0
  33. package/src/__tests__/cli-memory-v2-reembed-skills.test.ts +0 -1
  34. package/src/__tests__/compactor-assistant-tail-preview.test.ts +111 -0
  35. package/src/__tests__/compactor-tail-resolution.test.ts +116 -0
  36. package/src/__tests__/computer-use-native-history.test.ts +30 -0
  37. package/src/__tests__/computer-use-window-schema.test.ts +24 -0
  38. package/src/__tests__/config-get-vision-flag.test.ts +36 -0
  39. package/src/__tests__/config-loader-validation-reset-notice.test.ts +47 -15
  40. package/src/__tests__/context-search-conversations-source.test.ts +27 -5
  41. package/src/__tests__/conversation-agent-loop-overflow.test.ts +189 -195
  42. package/src/__tests__/conversation-agent-loop.test.ts +690 -50
  43. package/src/__tests__/conversation-fork-crud.test.ts +132 -45
  44. package/src/__tests__/conversation-fork-retrospective.test.ts +9 -7
  45. package/src/__tests__/conversation-lifecycle.test.ts +684 -10
  46. package/src/__tests__/conversation-pairing.test.ts +10 -9
  47. package/src/__tests__/conversation-runtime-assembly.test.ts +288 -1
  48. package/src/__tests__/conversation-surfaces-history-restored-completion.test.ts +21 -4
  49. package/src/__tests__/conversation-surfaces-point-at-budget.test.ts +150 -0
  50. package/src/__tests__/conversation-surfaces-point-at-capability.test.ts +161 -0
  51. package/src/__tests__/copy-composer-tc-templates.test.ts +2 -2
  52. package/src/__tests__/credential-security-invariants.test.ts +1 -0
  53. package/src/__tests__/delivered-post-record.test.ts +107 -0
  54. package/src/__tests__/guardian-action-service-scope.test.ts +53 -0
  55. package/src/__tests__/guardian-question-mode.test.ts +88 -138
  56. package/src/__tests__/headless-browser-mode.test.ts +63 -13
  57. package/src/__tests__/heartbeat-disk-pressure.test.ts +2 -0
  58. package/src/__tests__/heartbeat-service.test.ts +118 -27
  59. package/src/__tests__/host-cu-proxy.test.ts +159 -1
  60. package/src/__tests__/host-proxy-interface.test.ts +15 -0
  61. package/src/__tests__/host-proxy-preactivation.test.ts +61 -0
  62. package/src/__tests__/injector-chain.test.ts +3 -3
  63. package/src/__tests__/injector-registry-order-guard.test.ts +1 -1
  64. package/src/__tests__/injector-v3-suppression.test.ts +587 -45
  65. package/src/__tests__/llm-schema.test.ts +70 -2
  66. package/src/__tests__/logger.test.ts +33 -32
  67. package/src/__tests__/mcp-config-secret-boundary.test.ts +2 -0
  68. package/src/__tests__/memory-config-file-parity.test.ts +18 -0
  69. package/src/__tests__/memory-retrieval-hook.test.ts +53 -3
  70. package/src/__tests__/messages-read-boundary-guard.test.ts +0 -1
  71. package/src/__tests__/messaging-send-tool.test.ts +251 -128
  72. package/src/__tests__/messaging-shared.test.ts +19 -0
  73. package/src/__tests__/notification-decision-fallback.test.ts +98 -76
  74. package/src/__tests__/notification-decision-strategy.test.ts +10 -38
  75. package/src/__tests__/notification-events-source-context-probe.test.ts +211 -0
  76. package/src/__tests__/openai-responses-prompt-cache.test.ts +7 -2
  77. package/src/__tests__/openai-responses-provider.test.ts +42 -0
  78. package/src/__tests__/outlook-messaging-provider.test.ts +9 -0
  79. package/src/__tests__/platform-callback-registration.test.ts +162 -3
  80. package/src/__tests__/plugin-api-webhook-url.test.ts +66 -4
  81. package/src/__tests__/plugin-import-boundary-guard.test.ts +1 -0
  82. package/src/__tests__/pricing.test.ts +29 -0
  83. package/src/__tests__/prompt-cache-cross-turn-stability.test.ts +63 -3
  84. package/src/__tests__/scheduler-result-notification.test.ts +209 -0
  85. package/src/__tests__/strip-memory-injections.test.ts +75 -27
  86. package/src/__tests__/tool-approval-seed-content-blocks.test.ts +4 -13
  87. package/src/acp/auto-install.test.ts +35 -1
  88. package/src/acp/resolve-agent.test.ts +3 -3
  89. package/src/agent/loop.ts +116 -45
  90. package/src/api/constants/personality-sliders.test.ts +21 -0
  91. package/src/api/constants/personality-sliders.ts +64 -0
  92. package/src/api/events/contact-record-request.ts +16 -4
  93. package/src/api/events/contact-request.ts +10 -1
  94. package/src/api/index.ts +12 -0
  95. package/src/api/responses/memory-v3-selection-log.ts +64 -13
  96. package/src/api/surfaces.ts +6 -5
  97. package/src/background-wake/next-wake.test.ts +13 -0
  98. package/src/background-wake/next-wake.ts +4 -2
  99. package/src/calls/__tests__/voice-session-bridge.test.ts +166 -0
  100. package/src/calls/__tests__/voice-turn-sight-hold.test.ts +301 -0
  101. package/src/calls/voice-session-bridge.ts +162 -1
  102. package/src/calls/voice-triage-escalate.ts +1 -1
  103. package/src/channels/types.ts +14 -0
  104. package/src/cli/__tests__/catalog-search-help.test.ts +1 -0
  105. package/src/cli/__tests__/contacts-address-prompt-cli.test.ts +701 -0
  106. package/src/cli/__tests__/contacts-record-prompt-cli.test.ts +276 -15
  107. package/src/cli/__tests__/roadmap-cli.test.ts +313 -0
  108. package/src/cli/__tests__/run-assistant-command.ts +59 -20
  109. package/src/cli/commands/__tests__/conversations-slack.test.ts +12 -0
  110. package/src/cli/commands/__tests__/plugins-search.test.ts +81 -0
  111. package/src/cli/commands/config.help.ts +3 -1
  112. package/src/cli/commands/contacts.help.ts +179 -12
  113. package/src/cli/commands/contacts.ts +442 -47
  114. package/src/cli/commands/memory/worker.ts +1 -1
  115. package/src/cli/commands/platform/__tests__/credits-format.test.ts +152 -0
  116. package/src/cli/commands/platform/__tests__/credits.test.ts +20 -0
  117. package/src/cli/commands/platform/credits-format.ts +108 -0
  118. package/src/cli/commands/platform/index.help.ts +33 -11
  119. package/src/cli/commands/platform/index.ts +7 -18
  120. package/src/cli/commands/plugins.help.ts +4 -1
  121. package/src/cli/commands/plugins.ts +2 -1
  122. package/src/cli/commands/roadmap.help.ts +171 -0
  123. package/src/cli/commands/roadmap.ts +358 -0
  124. package/src/cli/index.help.ts +2 -0
  125. package/src/cli/lib/__tests__/cli-output.test.ts +5 -0
  126. package/src/cli/lib/cli-output.ts +6 -6
  127. package/src/cli/program.ts +2 -0
  128. package/src/config/__tests__/webhook-routing.test.ts +42 -0
  129. package/src/config/acp-defaults.test.ts +3 -3
  130. package/src/config/acp-defaults.ts +5 -5
  131. package/src/config/bundled-skills/acp/SKILL.md +8 -11
  132. package/src/config/bundled-skills/computer-use/SKILL.md +21 -0
  133. package/src/config/bundled-skills/computer-use/TOOLS.json +6 -0
  134. package/src/config/bundled-skills/contacts/SKILL.md +61 -11
  135. package/src/config/bundled-skills/messaging/SKILL.md +2 -0
  136. package/src/config/bundled-skills/messaging/TOOLS.json +4 -4
  137. package/src/config/bundled-skills/messaging/tools/messaging-send.ts +160 -36
  138. package/src/config/bundled-skills/messaging/tools/shared.ts +6 -0
  139. package/src/config/bundled-skills/schedule/SKILL.md +10 -2
  140. package/src/config/bundled-skills/screen-annotation/SKILL.md +80 -0
  141. package/src/config/bundled-skills/screen-annotation/TOOLS.json +73 -0
  142. package/src/config/bundled-skills/screen-annotation/tools/screen-annotation.test.ts +74 -0
  143. package/src/config/bundled-skills/screen-annotation/tools/screen-clear-marks.ts +33 -0
  144. package/src/config/bundled-skills/screen-annotation/tools/screen-point-at.ts +25 -0
  145. package/src/config/env-registry.ts +2 -0
  146. package/src/config/feature-flag-registry.json +39 -7
  147. package/src/config/input-modalities.test.ts +71 -0
  148. package/src/config/input-modalities.ts +95 -0
  149. package/src/config/loader.ts +2 -0
  150. package/src/config/schemas/__tests__/memory-v3.test.ts +58 -15
  151. package/src/config/schemas/heartbeat.ts +3 -3
  152. package/src/config/schemas/llm.ts +47 -10
  153. package/src/config/schemas/memory-storage.test.ts +39 -0
  154. package/src/config/schemas/memory-storage.ts +21 -0
  155. package/src/config/schemas/memory-v3.ts +73 -46
  156. package/src/config/schemas/services.ts +18 -0
  157. package/src/config/webhook-routing.ts +12 -2
  158. package/src/context/compactor.ts +12 -13
  159. package/src/context/strip-injections.ts +29 -25
  160. package/src/context/token-estimator.ts +2 -2
  161. package/src/daemon/__tests__/embedding-reconcile.test.ts +77 -0
  162. package/src/daemon/__tests__/lifecycle-embedding-reconcile.test.ts +3 -0
  163. package/src/daemon/__tests__/turn-actor.test.ts +68 -0
  164. package/src/daemon/conversation-agent-loop-handlers.ts +152 -22
  165. package/src/daemon/conversation-agent-loop.ts +19 -3
  166. package/src/daemon/conversation-messaging.ts +3 -2
  167. package/src/daemon/conversation-runtime-assembly.ts +191 -69
  168. package/src/daemon/conversation-surfaces.ts +32 -62
  169. package/src/daemon/conversation.ts +153 -46
  170. package/src/daemon/embedding-reconcile.ts +64 -1
  171. package/src/daemon/handlers/config-embeddings.ts +31 -0
  172. package/src/daemon/host-cu-proxy.ts +66 -50
  173. package/src/daemon/host-cu-target.ts +139 -0
  174. package/src/daemon/host-proxy-preactivation.ts +29 -1
  175. package/src/daemon/mcp-reload-service.ts +6 -0
  176. package/src/daemon/providers-setup.ts +5 -26
  177. package/src/daemon/trust-context.ts +1 -1
  178. package/src/daemon/turn-actor.ts +46 -0
  179. package/src/heartbeat/heartbeat-service.ts +72 -13
  180. package/src/inbound/platform-callback-registration.ts +77 -11
  181. package/src/inbound/pod-webhook-claim.ts +35 -0
  182. package/src/inbound/velay-webhooks-gate.ts +20 -0
  183. package/src/ipc/gateway-client.ts +91 -0
  184. package/src/live-voice/__tests__/live-voice-photo.test.ts +174 -0
  185. package/src/live-voice/__tests__/live-voice-session-guardian.test.ts +127 -0
  186. package/src/live-voice/__tests__/live-voice-session-telemetry.test.ts +42 -2
  187. package/src/live-voice/__tests__/live-voice-sight-frame.test.ts +81 -0
  188. package/src/live-voice/__tests__/protocol.test.ts +67 -0
  189. package/src/live-voice/live-voice-connection.ts +14 -1
  190. package/src/live-voice/live-voice-photo.ts +109 -4
  191. package/src/live-voice/live-voice-session-manager.ts +10 -1
  192. package/src/live-voice/live-voice-session.ts +196 -31
  193. package/src/live-voice/protocol.ts +134 -1
  194. package/src/mcp/__tests__/reload-signal-emission.test.ts +54 -0
  195. package/src/mcp/__tests__/reload-signal.test.ts +57 -0
  196. package/src/mcp/__tests__/startup.test.ts +150 -0
  197. package/src/mcp/reload-signal.ts +50 -0
  198. package/src/mcp/startup.ts +101 -0
  199. package/src/messaging/providers/outlook/adapter.ts +2 -13
  200. package/src/messaging/providers/outlook/client.ts +13 -1
  201. package/src/messaging/providers/outlook/types.ts +3 -0
  202. package/src/messaging/providers/telegram-bot/adapter.ts +9 -2
  203. package/src/messaging/providers/telegram-bot/client.ts +8 -2
  204. package/src/messaging/providers/whatsapp/adapter.ts +9 -2
  205. package/src/messaging/providers/whatsapp/client.ts +8 -2
  206. package/src/notifications/AGENTS.md +5 -0
  207. package/src/notifications/README.md +21 -7
  208. package/src/notifications/__tests__/assistant-reply-producer.test.ts +3 -10
  209. package/src/notifications/__tests__/background-failure-signal.test.ts +96 -0
  210. package/src/notifications/__tests__/broadcaster.test.ts +171 -7
  211. package/src/notifications/__tests__/decision-engine.test.ts +79 -6
  212. package/src/notifications/__tests__/edit-notification.test.ts +59 -0
  213. package/src/notifications/__tests__/resolve-visible-in-source.test.ts +96 -0
  214. package/src/notifications/__tests__/schedule-result-producer.test.ts +397 -0
  215. package/src/notifications/access-request-copy.ts +5 -5
  216. package/src/notifications/approval-card-data.ts +29 -13
  217. package/src/notifications/assistant-reply-producer.ts +0 -4
  218. package/src/notifications/background-failure-signal.ts +13 -1
  219. package/src/notifications/broadcaster.ts +65 -2
  220. package/src/notifications/conversation-pairing.ts +263 -155
  221. package/src/notifications/copy-composer.ts +38 -22
  222. package/src/notifications/decision-engine.ts +89 -61
  223. package/src/notifications/delivered-post-record.ts +81 -0
  224. package/src/notifications/deliveries-store.ts +8 -1
  225. package/src/notifications/edit-notification.ts +16 -0
  226. package/src/notifications/events-store.ts +68 -2
  227. package/src/notifications/guardian-question-mode.ts +24 -232
  228. package/src/notifications/home-feed-side-effect.ts +1 -0
  229. package/src/notifications/notification-utils.ts +13 -5
  230. package/src/notifications/resolve-visible-in-source.ts +56 -0
  231. package/src/notifications/schedule-result-producer.ts +275 -0
  232. package/src/notifications/signal.ts +5 -0
  233. package/src/oauth/__tests__/seed-providers-managed.test.ts +82 -0
  234. package/src/oauth/seed-providers.ts +201 -3
  235. package/src/onboarding/onboarding-events-store.ts +14 -8
  236. package/src/persistence/conversation-crud.ts +68 -19
  237. package/src/persistence/embeddings/embedding-backend.test.ts +162 -0
  238. package/src/persistence/embeddings/embedding-backend.ts +148 -11
  239. package/src/persistence/embeddings/embedding-gemini.test.ts +328 -9
  240. package/src/persistence/embeddings/embedding-gemini.ts +211 -27
  241. package/src/persistence/embeddings/embedding-openai.test.ts +113 -0
  242. package/src/persistence/embeddings/embedding-openai.ts +55 -4
  243. package/src/persistence/embeddings/embedding-types.test.ts +24 -0
  244. package/src/persistence/embeddings/embedding-types.ts +25 -1
  245. package/src/persistence/job-utils.ts +6 -2
  246. package/src/persistence/message-metadata.ts +20 -0
  247. package/src/persistence/migrations/283-memory-v3-selections-message-id-and-sections.ts +2 -1
  248. package/src/persistence/migrations/376-notification-deliveries-canonical-message-id.ts +37 -0
  249. package/src/persistence/schema/memory-injection.ts +31 -9
  250. package/src/persistence/schema/notifications.ts +8 -0
  251. package/src/persistence/steps.ts +2 -0
  252. package/src/plugin-api/index.ts +2 -1
  253. package/src/plugin-api/vision-support.test.ts +81 -0
  254. package/src/plugin-api/vision-support.ts +57 -10
  255. package/src/plugin-api/webhook-url.ts +18 -4
  256. package/src/plugins/AGENTS.md +1 -1
  257. package/src/plugins/defaults/channel/injectors.ts +3 -1
  258. package/src/plugins/defaults/injector-order.ts +2 -2
  259. package/src/plugins/defaults/memory/AGENTS.md +263 -30
  260. package/src/plugins/defaults/memory/__tests__/conversation-memory-orphan-sweep.test.ts +4 -0
  261. package/src/plugins/defaults/memory/__tests__/conversation-memory-purge.test.ts +6 -0
  262. package/src/plugins/defaults/memory/__tests__/db-memory-attach.test.ts +9 -3
  263. package/src/plugins/defaults/memory/__tests__/memory-log-stores-degraded.test.ts +3 -7
  264. package/src/plugins/defaults/memory/__tests__/relocated-memory-test-rows.ts +18 -0
  265. package/src/plugins/defaults/memory/context-search/sources/conversations.ts +16 -2
  266. package/src/plugins/defaults/memory/conversation-memory-purge.ts +2 -0
  267. package/src/plugins/defaults/memory/fork-conversation-memory.ts +13 -13
  268. package/src/plugins/defaults/memory/graph/__tests__/conversation-graph-memory-v2-routing.test.ts +19 -12
  269. package/src/plugins/defaults/memory/graph/conversation-graph-memory.ts +17 -15
  270. package/src/plugins/defaults/memory/graph-topology/build-memory-graph.ts +1 -1
  271. package/src/plugins/defaults/memory/hooks/__tests__/post-compact.test.ts +121 -0
  272. package/src/plugins/defaults/memory/hooks/init.ts +10 -0
  273. package/src/plugins/defaults/memory/hooks/injection-metadata.ts +100 -0
  274. package/src/plugins/defaults/memory/hooks/post-compact.ts +19 -7
  275. package/src/plugins/defaults/memory/hooks/user-prompt-submit.ts +11 -83
  276. package/src/plugins/defaults/memory/injectors.ts +4 -4
  277. package/src/plugins/defaults/memory/job-handlers.ts +4 -1
  278. package/src/plugins/defaults/memory/jobs/__tests__/embed-concept-page.test.ts +1 -0
  279. package/src/plugins/defaults/memory/jobs/embed-concept-page.ts +11 -3
  280. package/src/plugins/defaults/memory/memory-db.ts +4 -0
  281. package/src/plugins/defaults/memory/memory-marker.ts +28 -17
  282. package/src/plugins/defaults/memory/src/__tests__/memory-v2-simulate-route.test.ts +0 -1
  283. package/src/plugins/defaults/memory/src/__tests__/memory-worker-routes.test.ts +20 -0
  284. package/src/plugins/defaults/memory/src/memory-item-routes.test.ts +0 -1
  285. package/src/plugins/defaults/memory/src/memory-worker-routes.ts +3 -1
  286. package/src/plugins/defaults/memory/substrate/__tests__/cli-command-store.test.ts +1 -1
  287. package/src/plugins/defaults/memory/substrate/__tests__/consolidation-job.test.ts +66 -0
  288. package/src/plugins/defaults/memory/substrate/__tests__/injected-block-slugs.test.ts +345 -0
  289. package/src/plugins/defaults/memory/substrate/__tests__/page-index.test.ts +0 -1
  290. package/src/plugins/defaults/memory/substrate/__tests__/prompts-consolidation.test.ts +170 -1
  291. package/src/plugins/defaults/memory/substrate/__tests__/reembed-job.test.ts +1 -0
  292. package/src/plugins/defaults/memory/substrate/capability-slugs.ts +53 -0
  293. package/src/plugins/defaults/memory/substrate/cli-command-store.ts +4 -18
  294. package/src/plugins/defaults/memory/substrate/consolidation-job.ts +40 -1
  295. package/src/plugins/defaults/memory/substrate/ingest.ts +11 -9
  296. package/src/plugins/defaults/memory/substrate/injected-block-slugs.ts +426 -42
  297. package/src/plugins/defaults/memory/substrate/page-index.ts +5 -4
  298. package/src/plugins/defaults/memory/substrate/prompts/consolidation.ts +127 -14
  299. package/src/plugins/defaults/memory/substrate/skill-store.ts +1 -18
  300. package/src/plugins/defaults/memory/v2/__tests__/backfill-jobs.test.ts +1 -0
  301. package/src/plugins/defaults/memory/v2/__tests__/injection.test.ts +2 -8
  302. package/src/plugins/defaults/memory/v2/__tests__/router.test.ts +0 -1
  303. package/src/plugins/defaults/memory/v2/injection.ts +4 -4
  304. package/src/plugins/defaults/memory/v3/__tests__/capabilities.test.ts +16 -0
  305. package/src/plugins/defaults/memory/v3/__tests__/carry-integration.test.ts +1004 -319
  306. package/src/plugins/defaults/memory/v3/__tests__/dense.test.ts +227 -4
  307. package/src/plugins/defaults/memory/v3/__tests__/injection.test.ts +836 -263
  308. package/src/plugins/defaults/memory/v3/__tests__/live-integration.test.ts +65 -38
  309. package/src/plugins/defaults/memory/v3/__tests__/maintain-job.test.ts +354 -3
  310. package/src/plugins/defaults/memory/v3/__tests__/orchestrate.test.ts +688 -104
  311. package/src/plugins/defaults/memory/v3/__tests__/overlong-sections.test.ts +109 -0
  312. package/src/plugins/defaults/memory/v3/__tests__/page-content.test.ts +144 -0
  313. package/src/plugins/defaults/memory/v3/__tests__/plugin-schema.test.ts +534 -0
  314. package/src/plugins/defaults/memory/v3/__tests__/pool-log-store.test.ts +449 -0
  315. package/src/plugins/defaults/memory/v3/__tests__/pool-select.test.ts +265 -27
  316. package/src/plugins/defaults/memory/v3/__tests__/render-injection.test.ts +34 -117
  317. package/src/plugins/defaults/memory/v3/__tests__/section-dense-store.test.ts +491 -4
  318. package/src/plugins/defaults/memory/v3/__tests__/section-needle.test.ts +124 -1
  319. package/src/plugins/defaults/memory/v3/__tests__/sections.test.ts +264 -4
  320. package/src/plugins/defaults/memory/v3/__tests__/selection-log-store.test.ts +560 -22
  321. package/src/plugins/defaults/memory/v3/__tests__/shadow-integration.test.ts +40 -33
  322. package/src/plugins/defaults/memory/v3/__tests__/shadow-plugin.test.ts +718 -63
  323. package/src/plugins/defaults/memory/v3/__tests__/types.test.ts +83 -2
  324. package/src/plugins/defaults/memory/v3/candidate-match.ts +1 -1
  325. package/src/plugins/defaults/memory/v3/capabilities.ts +90 -14
  326. package/src/plugins/defaults/memory/v3/card.test.ts +15 -16
  327. package/src/plugins/defaults/memory/v3/card.ts +16 -15
  328. package/src/plugins/defaults/memory/v3/dense.ts +10 -0
  329. package/src/plugins/defaults/memory/v3/ever-injected-store.test.ts +643 -140
  330. package/src/plugins/defaults/memory/v3/ever-injected-store.ts +462 -212
  331. package/src/plugins/defaults/memory/v3/hot-set.test.ts +2 -2
  332. package/src/plugins/defaults/memory/v3/injector.ts +366 -278
  333. package/src/plugins/defaults/memory/v3/learned-edges.test.ts +2 -2
  334. package/src/plugins/defaults/memory/v3/maintain-job.ts +299 -73
  335. package/src/plugins/defaults/memory/v3/orchestrate.ts +327 -214
  336. package/src/plugins/defaults/memory/v3/overlong-sections.ts +83 -0
  337. package/src/plugins/defaults/memory/v3/page-content.ts +72 -92
  338. package/src/plugins/defaults/memory/v3/plugin-schema.ts +346 -0
  339. package/src/plugins/defaults/memory/v3/pool-log-store.ts +291 -0
  340. package/src/plugins/defaults/memory/v3/pool-select.test.ts +2 -2
  341. package/src/plugins/defaults/memory/v3/pool-select.ts +160 -60
  342. package/src/plugins/defaults/memory/v3/prune.test.ts +1042 -329
  343. package/src/plugins/defaults/memory/v3/prune.ts +433 -270
  344. package/src/plugins/defaults/memory/v3/rare-term-lane.test.ts +269 -0
  345. package/src/plugins/defaults/memory/v3/rare-term-lane.ts +141 -0
  346. package/src/plugins/defaults/memory/v3/render-injection.ts +43 -82
  347. package/src/plugins/defaults/memory/v3/section-dense-store.ts +316 -28
  348. package/src/plugins/defaults/memory/v3/section-needle.ts +145 -11
  349. package/src/plugins/defaults/memory/v3/sections.ts +103 -16
  350. package/src/plugins/defaults/memory/v3/selection-log-store.ts +243 -78
  351. package/src/plugins/defaults/memory/v3/shadow-plugin.ts +189 -68
  352. package/src/plugins/defaults/memory/v3/tuning-profile.ts +6 -0
  353. package/src/plugins/defaults/memory/v3/types.ts +169 -23
  354. package/src/plugins/defaults/memory/v3-eval/__tests__/eval-packets.test.ts +21 -4
  355. package/src/plugins/defaults/memory/v3-eval/eval-packets.ts +77 -26
  356. package/src/plugins/types.ts +14 -0
  357. package/src/providers/__tests__/connection-model-compat.test.ts +1 -0
  358. package/src/providers/__tests__/provider-secret-catalog.test.ts +4 -0
  359. package/src/providers/__tests__/xml-tool-call-salvage.test.ts +172 -0
  360. package/src/providers/inline-audio-support.test.ts +34 -0
  361. package/src/providers/inline-audio-support.ts +27 -0
  362. package/src/providers/model-catalog.ts +102 -0
  363. package/src/providers/openai/__tests__/chat-completions-extra-body.test.ts +51 -0
  364. package/src/providers/openai/__tests__/chat-completions-provider-xml-salvage.test.ts +291 -0
  365. package/src/providers/openai/chat-completions-provider.ts +114 -14
  366. package/src/providers/openai/codex-models.ts +1 -0
  367. package/src/providers/openai/responses-provider.ts +65 -15
  368. package/src/providers/opencode/client.test.ts +24 -0
  369. package/src/providers/opencode/client.ts +6 -1
  370. package/src/providers/provider-secret-catalog.ts +11 -1
  371. package/src/providers/retry.ts +6 -0
  372. package/src/providers/vellum/client.ts +14 -1
  373. package/src/providers/vellum/personality-directions.test.ts +163 -0
  374. package/src/providers/vellum/personality-directions.ts +120 -0
  375. package/src/providers/xml-tool-call-salvage.ts +212 -0
  376. package/src/runtime/AGENTS.md +7 -2
  377. package/src/runtime/__tests__/background-job-runner.test.ts +115 -0
  378. package/src/runtime/__tests__/guardian-form-registry.test.ts +0 -32
  379. package/src/runtime/__tests__/host-observe.test.ts +44 -0
  380. package/src/runtime/assistant-event-hub.ts +14 -9
  381. package/src/runtime/auth/__tests__/guard-tests.test.ts +1 -4
  382. package/src/runtime/auth/__tests__/route-policy.test.ts +65 -0
  383. package/src/runtime/auth/same-actor.ts +4 -3
  384. package/src/runtime/background-job-runner.ts +10 -0
  385. package/src/runtime/channel-reply-delivery.ts +1 -162
  386. package/src/runtime/guardian-action-service.ts +20 -1
  387. package/src/runtime/host-observe.ts +56 -6
  388. package/src/runtime/http-server.ts +35 -0
  389. package/src/runtime/outbound-post-reconciliation.ts +183 -0
  390. package/src/runtime/routes/__tests__/contact-address-prompt-route.test.ts +231 -0
  391. package/src/runtime/routes/__tests__/contact-record-prompt-route.test.ts +59 -3
  392. package/src/runtime/routes/__tests__/guardian-form-routes.test.ts +85 -0
  393. package/src/runtime/routes/__tests__/heartbeat-routes.test.ts +24 -0
  394. package/src/runtime/routes/__tests__/platform-credits-routes.test.ts +239 -0
  395. package/src/runtime/routes/__tests__/roadmap-routes.test.ts +469 -0
  396. package/src/runtime/routes/__tests__/watch-routes.test.ts +70 -1
  397. package/src/runtime/routes/__tests__/webhook-routes.test.ts +83 -0
  398. package/src/runtime/routes/contact-prompt-routes.ts +177 -28
  399. package/src/runtime/routes/conversation-query-routes.ts +53 -11
  400. package/src/runtime/routes/events-routes.ts +13 -3
  401. package/src/runtime/routes/guardian-action-routes.ts +7 -0
  402. package/src/runtime/routes/guardian-form-routes.ts +6 -4
  403. package/src/runtime/routes/heartbeat-routes.ts +41 -53
  404. package/src/runtime/routes/index.ts +2 -2
  405. package/src/runtime/routes/integrations/a2a.ts +12 -3
  406. package/src/runtime/routes/integrations/vercel.ts +13 -3
  407. package/src/runtime/routes/platform-routes.ts +175 -23
  408. package/src/runtime/routes/roadmap-routes.ts +744 -0
  409. package/src/runtime/routes/watch-routes.ts +50 -0
  410. package/src/runtime/routes/webhook-routes.ts +24 -2
  411. package/src/runtime/web-presence.ts +13 -9
  412. package/src/schedule/__tests__/schedule-timezone.test.ts +21 -3
  413. package/src/schedule/__tests__/worker-mcp-tools.test.ts +88 -0
  414. package/src/schedule/schedule-timezone.ts +25 -0
  415. package/src/schedule/scheduler.ts +19 -0
  416. package/src/schedule/worker.ts +119 -2
  417. package/src/telemetry/__tests__/live-voice-funnel.test.ts +32 -0
  418. package/src/telemetry/live-voice-funnel.ts +33 -0
  419. package/src/tools/acp/list-agents.test.ts +1 -1
  420. package/src/tools/browser/__tests__/browser-status.test.ts +3 -0
  421. package/src/tools/browser/browser-execution.ts +117 -53
  422. package/src/tools/browser/browser-status-constants.ts +6 -6
  423. package/src/tools/capability-offer.test.ts +87 -0
  424. package/src/tools/capability-offer.ts +93 -0
  425. package/src/tools/computer-use/definitions.ts +6 -0
  426. package/src/tools/computer-use/skill-proxy-bridge.ts +10 -0
  427. package/src/tools/terminal/safe-env.ts +2 -0
  428. package/src/watch/__tests__/watch-retro.test.ts +11 -10
  429. package/src/watch/__tests__/watch-session-manager.test.ts +75 -1
  430. package/src/watch/watch-retro.ts +10 -9
  431. package/src/watch/watch-session-manager.ts +17 -0
  432. package/src/runtime/routes/__tests__/sight-frame-routes.test.ts +0 -487
  433. package/src/runtime/routes/sight-frame-routes.ts +0 -136
@@ -1,64 +1,64 @@
1
1
  /**
2
- * The memory-v3 {@link Injector}s: frozen net-new cards + per-turn spotlight.
2
+ * The memory-v3 {@link Injector}s: frozen net-new sections + per-turn pointer.
3
3
  *
4
4
  * Two injectors share one orchestration result per turn (memoized via
5
- * {@link observeTurnOnce} so re-entry assemblies — overflow convergence,
6
- * post-compaction re-injection — reuse the turn's selections instead of
5
+ * {@link observeTurnOnce}, so re-entry assemblies, overflow convergence and
6
+ * post-compaction re-injection, reuse the turn's selections instead of
7
7
  * re-running the selector):
8
8
  *
9
9
  * - {@link memoryV3Injector} (id `memory-v3`, `after-memory-prefix`): the
10
- * PERSISTENT layer. Renders only this turn's NET-NEW selections — selections
11
- * not already in the everInjected store — as compact cards inside one
12
- * `<memory>` block and returns the block. The everInjected store write
13
- * (`recordInjected`) and the prune-valve schedule are DEFERRED to a commit
14
- * callback the block carries (`meta[MEMORY_V3_COMMIT_META_KEY]`): runtime
15
- * assembly invokes it only when the turn's tail is a user message — the
16
- * same gate as metadata capture — so a turn whose block silently fails to
17
- * attach never claims its cards in the store (which would suppress them
18
- * until compaction). Runtime assembly splices the block onto the current
19
- * user message and the user-prompt-submit hook persists the unwrapped inner
20
- * text under `metadata.memoryV3InjectedBlock`; `conversation.ts` rehydrates
21
- * it on load. The block is FROZEN thereafter: prior turns' card blocks stay
22
- * byte-identical in history, so they ride the provider's cached prefix and
23
- * survive restarts — mirroring v2's `memoryInjectedBlock` mechanism. An
24
- * all-repeat turn returns an EMPTY-TEXT block: assembly attaches nothing,
25
- * but the block's presence still keys v2 suppression (v3 ran and owns the
26
- * `<memory>` layer this turn). A `null` return (failure / empty selection /
27
- * every net-new card rendered empty) attaches no v3 block: under
28
- * `memory-v3-live` the user-prompt-submit hook skips v2 retrieval entirely,
29
- * so a null return leaves the turn with no NEW injected memory (prior turns'
30
- * frozen cards still ride history).
10
+ * PERSISTENT layer. The injection unit is the SECTION: each selected page
11
+ * contributes every section selected for it, or its lead when it was
12
+ * selected with none (a capability slug contributes its whole capability
13
+ * content). It renders only this turn's NET-NEW units, the pairs
14
+ * `(slug, section key)` not already active in the section store, inside
15
+ * one `<memory>` block. The resident pairs are stamped with the turn's
16
+ * selection time (`touchSelected`, the prune valve's recency); the
17
+ * net-new record (`recordInjected`) and the valve schedule are DEFERRED
18
+ * to a commit callback the block carries
19
+ * (`meta[MEMORY_V3_COMMIT_META_KEY]`), which runtime assembly invokes
20
+ * only when the block attached to a user tail, so a block that fails to
21
+ * attach never claims its sections (which would suppress them until
22
+ * compaction). The user-prompt-submit hook persists the unwrapped text
23
+ * under `metadata.memoryV3InjectedBlock` and `conversation.ts` rehydrates
24
+ * it on load. The block is FROZEN thereafter: prior turns' blocks stay
25
+ * byte-identical in history, ride the provider's cached prefix, and
26
+ * survive restarts, mirroring v2's `memoryInjectedBlock`. An all-repeat
27
+ * turn returns an EMPTY-TEXT block: assembly attaches nothing, but its
28
+ * presence keys v2 suppression. A `null` return (failure, empty
29
+ * selection, every net-new section rendered empty) attaches no v3 block,
30
+ * and since the hook skips v2 retrieval under `memory-v3-live` the turn
31
+ * gets no NEW injected memory (prior turns' frozen sections still ride
32
+ * history).
31
33
  *
32
- * - {@link memoryV3SpotlightInjector} (id `memory-v3-spotlight`,
33
- * `after-memory-prefix`): the per-turn spotlight layer. Renders the top
34
- * `spotlight.n` selected finder hits' matched sections, plus the previous
35
- * `spotlight.windowTurns` turns' entries from an in-memory per-conversation
36
- * ring (a daemon restart simply re-warms it), as a `<memory_spotlight>`
37
- * block. Runtime assembly splices the block onto the current user message
38
- * immediately after any frozen `<memory>` cards. The user-prompt-submit
39
- * hook persists the wrapped text under `metadata.memoryV3SpotlightBlock`;
40
- * `conversation.ts` rehydrates it on load. Historical user messages keep
41
- * the spotlight they were sent with, so the provider prefix through those
42
- * messages stays byte-identical. Mid-turn re-entry and post-compact
43
- * tail-strip the current tail before splicing a fresh spotlight so the
44
- * block does not double-stack.
34
+ * - {@link memoryV3PointerInjector} (id `memory-v3-pointer`,
35
+ * `after-memory-prefix`): the per-turn pointer layer. Lists this turn's
36
+ * selected sections that are ALREADY resident in history as a
37
+ * `<memory_pointer>` block of `memory/concepts/<slug>.md § <key>` lines,
38
+ * no bodies, so the model knows which frozen sections the turn is about.
39
+ * The entries are re-validated against the section store at render time:
40
+ * a pair the valve tombstoned since classification is dropped (it
41
+ * re-injects on its next selection), and with nothing left no block is
42
+ * emitted. Runtime assembly splices the block right after any frozen
43
+ * `<memory>` sections; the hook persists the wrapped text under
44
+ * `metadata.memoryV3PointerBlock` and `conversation.ts` rehydrates it.
45
+ * Historical user messages keep the pointer they were sent with, so the
46
+ * prefix through them stays byte-identical; re-entry and post-compaction
47
+ * assemblies tail-strip the current pointer before splicing a fresh one.
45
48
  *
46
49
  * Gating: `memory.v3.live` (config) runs orchestration and attaches blocks;
47
- * with it off, no orchestration runs and nothing is attached.
50
+ * with it off, nothing runs and nothing is attached. Both injectors apply
51
+ * the personal-memory trust gate ({@link isPersonalMemoryAllowed}): an
52
+ * untrusted remote actor's turn produces, records, and persists nothing,
53
+ * since v3 blocks are persisted to message metadata and rehydrated forever.
48
54
  *
49
- * Both injectors apply the same personal-memory trust gate as v2
50
- * ({@link isPersonalMemoryAllowed}): an untrusted remote actor's turn
51
- * produces nothing — no orchestration, no cards, no spotlight, and nothing
52
- * recorded or persisted. Memory pages, skill/CLI capability cards, and
53
- * matched-section spotlights all surface private user content — and because
54
- * v3 cards are persisted to message metadata and rehydrated forever, the gate
55
- * must also keep an untrusted turn from recording or persisting anything.
56
- *
57
- * Known mirror-of-v2 limitation: cards attached by mid-turn re-entry
58
- * assemblies (post-compaction re-injection) live only in memory — metadata is
59
- * persisted at the first-call site only — so a restart drops them while the
60
- * store still claims them, exactly as v2's `lastInjectedBlock` reinject does.
61
- * The next compaction clears the store and resets both layers.
55
+ * A re-entry assembly (the post-compaction hook, which also serves the
56
+ * overflow ladder's rungs) attaches its blocks in memory only: metadata is
57
+ * persisted at the first-call site alone, and every message a re-entry
58
+ * attaches to predates a compaction's `historyStrippedAt` marker, which
59
+ * keeps `loadFromDb` from rehydrating it. How a re-entry, and an assembly
60
+ * whose run messages a transcript replaces, classify the turn's units is
61
+ * documented at the partition in {@link memoryV3Injector}.
62
62
  */
63
63
 
64
64
  import { getConfig } from "../../../../config/loader.js";
@@ -70,6 +70,7 @@ import {
70
70
  type PendingConversationNotice,
71
71
  queueConversationNotice,
72
72
  } from "../../../../daemon/conversation-notices.js";
73
+ import { findConversationOrSubagent } from "../../../../daemon/conversation-registry.js";
73
74
  import { isPersonalMemoryAllowed } from "../../../../daemon/trust-context.js";
74
75
  import {
75
76
  type InjectionBlock,
@@ -77,63 +78,105 @@ import {
77
78
  type TurnContext,
78
79
  } from "../../../types.js";
79
80
  import { getLogger } from "../logging.js";
80
- import { wrapMemoryBlock, wrapMemorySpotlightBlock } from "../memory-marker.js";
81
- import { isCapabilitySlug } from "./capabilities.js";
82
- import { cardBytes } from "./card.js";
83
- import { getActiveSlugs, recordInjected } from "./ever-injected-store.js";
81
+ import { wrapMemoryBlock, wrapMemoryPointerBlock } from "../memory-marker.js";
82
+ import { renderedBytes } from "../substrate/injected-block-slugs.js";
83
+ import {
84
+ type InjectionUnit,
85
+ injectionUnits,
86
+ isCapabilitySlug,
87
+ } from "./capabilities.js";
88
+ import {
89
+ getActiveSections,
90
+ getPrunedSections,
91
+ recordInjected,
92
+ sectionRefSetHas,
93
+ touchSelected,
94
+ } from "./ever-injected-store.js";
84
95
  import type { OrchestrateResult } from "./orchestrate.js";
85
- import { renderV3CardContent } from "./page-content.js";
96
+ import { renderV3InjectionEntry } from "./page-content.js";
86
97
  import { MemoryV3RetrievalUnavailableError } from "./pool-select.js";
87
98
  import { schedulePruneValve } from "./prune.js";
88
99
  import {
89
- renderCardsBlockInner,
90
- renderSpotlightInner,
91
- type SpotlightEntry,
100
+ renderInjectionBlockInner,
101
+ renderPointerInner,
92
102
  } from "./render-injection.js";
93
103
  import { observeTurn } from "./shadow-plugin.js";
94
104
  import {
95
105
  MEMORY_V3_BLOCK_ID,
96
106
  MEMORY_V3_COMMIT_META_KEY,
97
- MEMORY_V3_SPOTLIGHT_BLOCK_ID,
107
+ MEMORY_V3_POINTER_BLOCK_ID,
108
+ type SectionRef,
98
109
  type Slug,
99
110
  } from "./types.js";
100
111
 
101
112
  const log = getLogger("memory-v3-shadow");
102
113
 
103
114
  /**
104
- * Cap on the per-conversation maps below. Least-recently-touched entries are
105
- * evicted first; an evicted conversation simply re-warms (spotlight) or
106
- * re-runs orchestration (memo) on its next turn.
115
+ * Cap on the per-conversation memo below, counted over IDLE conversations:
116
+ * the least-recently-touched idle entry is evicted first, and an evicted
117
+ * conversation simply re-runs orchestration on its next turn. A memo whose
118
+ * turn is still running is never evicted ({@link turnInFlight}), whatever
119
+ * the number of other conversations touching the process: a later re-entry
120
+ * of that turn re-emits the memo's rendered entries, and without them it
121
+ * would read the turn's committed sections as resident and emit pointers
122
+ * for bodies the re-injection strip cleared. The map can therefore exceed
123
+ * the cap by the number of turns in flight, never by idle entries, and the
124
+ * touches that follow such a burst, inserts and refreshes alike, evict idle
125
+ * entries until it fits again.
107
126
  */
108
127
  const MAX_TRACKED_CONVERSATIONS = 256;
128
+ let trackedConversationsCap = MAX_TRACKED_CONVERSATIONS;
129
+
130
+ /**
131
+ * Whether the conversation's turn is running: the live conversation's
132
+ * processing flag, set at turn start and cleared in the agent loop's
133
+ * `finally`, which is the window every re-entry assembly runs in. A
134
+ * conversation the registry does not hold, or a registry double without the
135
+ * method, reads as idle.
136
+ */
137
+ function turnInFlight(conversationId: string): boolean {
138
+ const conversation = findConversationOrSubagent(conversationId);
139
+ return (
140
+ typeof conversation?.isProcessing === "function" &&
141
+ conversation.isProcessing()
142
+ );
143
+ }
109
144
 
110
145
  /**
111
146
  * LRU-set `key` on `map`: delete-then-set so a re-touched key moves to the
112
147
  * back of the Map's insertion order (a plain `set` on an existing key keeps
113
148
  * its original position, which would evict the most long-lived ACTIVE
114
- * conversation first). Eviction only fires when inserting a genuinely new
115
- * key at the cap.
149
+ * conversation first). Eviction fires on any touch, an insert or a refresh
150
+ * of a tracked key, that finds the rest of the map at the cap, and takes
151
+ * the least-recently-touched entries whose turn is not in flight.
116
152
  */
117
153
  function lruSet<V>(map: Map<string, V>, key: string, value: V): void {
118
- if (map.has(key)) {
119
- map.delete(key);
120
- } else if (map.size >= MAX_TRACKED_CONVERSATIONS) {
121
- const oldest = map.keys().next().value;
122
- if (oldest !== undefined) {
123
- map.delete(oldest);
154
+ map.delete(key);
155
+ // Evict idle entries, least recently touched first, until the entry fits
156
+ // the cap: a burst of turns in flight can carry the map past it, and the
157
+ // touches that follow bring it back once they finish. A refresh evicts
158
+ // like an insert, since after a burst the only conversations still
159
+ // active may all be tracked already.
160
+ for (const candidate of map.keys()) {
161
+ if (map.size < trackedConversationsCap) {
162
+ break;
163
+ }
164
+ if (!turnInFlight(candidate)) {
165
+ map.delete(candidate);
124
166
  }
125
167
  }
126
168
  map.set(key, value);
127
169
  }
128
170
 
171
+ /** Test-only: the memo's current size. */
172
+ export function memoryV3TurnMemoSizeForTests(): number {
173
+ return observedTurns.size;
174
+ }
175
+
129
176
  function queueMemoryV3ConversationNotice(
130
177
  err: MemoryV3RetrievalUnavailableError,
131
178
  ctx: TurnContext,
132
- live: boolean,
133
179
  ): void {
134
- if (!live) {
135
- return;
136
- }
137
180
  const notice: PendingConversationNotice = err.conversationNotice ?? {
138
181
  source: "memory_v3",
139
182
  code: "UNKNOWN",
@@ -148,11 +191,38 @@ function queueMemoryV3ConversationNotice(
148
191
  );
149
192
  }
150
193
 
194
+ /**
195
+ * The gates both injectors share: memory on, v3 live, a trusted actor, and
196
+ * not the voice front door (which keeps carried sections from history but
197
+ * defers current-turn retrieval to the escalated leg so memory cannot delay
198
+ * its first token).
199
+ */
200
+ function turnIsEligible(ctx: TurnContext): boolean {
201
+ const config = getConfig();
202
+ if (!isMemoryEnabled(config) || !isMemoryV3Live(config)) {
203
+ return false;
204
+ }
205
+ if (!isPersonalMemoryAllowed(ctx.trust)) {
206
+ return false;
207
+ }
208
+ return ctx.callSite !== "voiceFrontDoor";
209
+ }
210
+
151
211
  // ─── shared per-turn orchestration memo ─────────────────────────────────────
152
212
 
153
213
  interface ObservedTurn {
154
214
  turnIndex: number;
155
215
  result: Promise<OrchestrateResult | null>;
216
+ /** The resident sections this turn's latest sections-produce pointed at,
217
+ * rendered by the pointer injector (which runs after it). */
218
+ pointer?: SectionRef[];
219
+ /** The entries the turn's FIRST produce rendered net-new (the ones its
220
+ * commit recorded), by slug and section key. A re-entry assembly within
221
+ * the turn re-emits them byte for byte instead of partitioning them
222
+ * against the store, which counts them active although their only copy
223
+ * rode the tail the re-injection strip cleared. Set even when empty, so
224
+ * a re-entry is recognised as one. */
225
+ rendered?: ReadonlyMap<Slug, ReadonlyMap<string, string>>;
156
226
  }
157
227
 
158
228
  /** Latest observed turn per conversation (both injectors + re-entry sites
@@ -161,7 +231,7 @@ const observedTurns = new Map<string, ObservedTurn>();
161
231
 
162
232
  /**
163
233
  * Run {@link observeTurn} once per (conversation, turn) and memoize the
164
- * promise. The cards and spotlight injectors both consume the result, and
234
+ * promise. The sections and pointer injectors both consume the result, and
165
235
  * re-entry assemblies within the same turn (overflow convergence,
166
236
  * post-compaction re-injection) reuse the turn's selections rather than
167
237
  * paying a second selector call. A new `turnIndex` replaces the entry, so the
@@ -180,97 +250,64 @@ function observeTurnOnce(
180
250
  return result;
181
251
  }
182
252
 
183
- // ─── ephemeral spotlight ring ────────────────────────────────────────────────
184
-
185
- interface SpotlightTurn {
186
- turnIndex: number;
187
- entries: SpotlightEntry[];
253
+ function observedTurn(
254
+ conversationId: string,
255
+ turnIndex: number,
256
+ ): ObservedTurn | undefined {
257
+ const cached = observedTurns.get(conversationId);
258
+ return cached && cached.turnIndex === turnIndex ? cached : undefined;
188
259
  }
189
260
 
190
- /** Recent turns' spotlight entries per conversation, oldest → newest. */
191
- const spotlightRings = new Map<string, SpotlightTurn[]>();
192
-
193
- /**
194
- * Compute this turn's spotlight entries: the top `n` SELECTED finder hits'
195
- * matched sections, in finder surfacing order (needle → dense → edge
196
- * precedence). A finder hit on a stable-prefix page keeps its matched-section
197
- * ref in the orchestrate result, so core/hot pages with current relevance
198
- * spotlight too.
199
- */
200
- function computeSpotlightEntries(
201
- result: OrchestrateResult,
202
- n: number,
203
- ): SpotlightEntry[] {
204
- const selected = new Set<Slug>(result.selections.map((s) => s.slug));
205
- const entries: SpotlightEntry[] = [];
206
- for (const candidate of result.lanes.finder) {
207
- if (entries.length >= n) {
208
- break;
209
- }
210
- if (!selected.has(candidate.slug)) {
211
- continue;
212
- }
213
- // Capability slugs never spotlight: their card already carries the whole
214
- // injection form, and their index "sections" are slices of the full-help
215
- // body the injection layer deliberately keeps out of context.
216
- if (isCapabilitySlug(candidate.slug)) {
217
- continue;
218
- }
219
- const section = result.matchedSections.get(candidate.slug);
220
- if (!section) {
221
- continue;
222
- }
223
- entries.push({
224
- slug: candidate.slug,
225
- title: section.title,
226
- text: section.text,
227
- });
261
+ function rememberPointerEntries(
262
+ conversationId: string,
263
+ turnIndex: number,
264
+ entries: SectionRef[],
265
+ ): void {
266
+ const cached = observedTurn(conversationId, turnIndex);
267
+ if (cached) {
268
+ cached.pointer = entries;
228
269
  }
229
- return entries;
230
270
  }
231
271
 
232
- /**
233
- * Fold this turn's entries into the conversation's ring and return the
234
- * rendered window: current turn first, then previous turns newest-first,
235
- * deduped by slug § heading, bounded by `n × (windowTurns + 1)` entries by
236
- * construction. Re-observing the SAME turn (re-entry assembly) replaces that
237
- * turn's entry rather than accumulating.
238
- */
239
- function updateSpotlightWindow(
272
+ function rememberRendered(
240
273
  conversationId: string,
241
274
  turnIndex: number,
242
- entries: SpotlightEntry[],
243
- windowTurns: number,
244
- ): SpotlightEntry[] {
245
- const prior = (spotlightRings.get(conversationId) ?? []).filter(
246
- (t) => t.turnIndex < turnIndex && t.turnIndex >= turnIndex - windowTurns,
247
- );
248
- lruSet(spotlightRings, conversationId, [...prior, { turnIndex, entries }]);
249
-
250
- const window: SpotlightEntry[] = [];
251
- const seen = new Set<string>();
252
- // Newest first: this turn's entries, then prior turns newest → oldest.
253
- for (const turn of [{ turnIndex, entries }, ...prior.reverse()]) {
254
- for (const entry of turn.entries) {
255
- const key = `${entry.slug}§${entry.title}`;
256
- if (seen.has(key)) {
257
- continue;
258
- }
259
- seen.add(key);
260
- window.push(entry);
275
+ entries: ReadonlyArray<SectionRef & { text: string }>,
276
+ ): void {
277
+ const cached = observedTurn(conversationId, turnIndex);
278
+ if (!cached) {
279
+ return;
280
+ }
281
+ const rendered = new Map<Slug, Map<string, string>>();
282
+ for (const { slug, key, text } of entries) {
283
+ let keys = rendered.get(slug);
284
+ if (!keys) {
285
+ keys = new Map();
286
+ rendered.set(slug, keys);
261
287
  }
288
+ keys.set(key, text);
262
289
  }
263
- return window;
290
+ cached.rendered = rendered;
264
291
  }
265
292
 
266
- /** Test-only reset for the per-turn memo and the spotlight rings. */
267
- export function resetMemoryV3InjectorStateForTests(): void {
293
+ /** Test-only reset for the per-turn memo and its cap; a `capacity` shrinks
294
+ * the cap so eviction is reachable with a handful of conversations. */
295
+ export function resetMemoryV3InjectorStateForTests(
296
+ capacity: number = MAX_TRACKED_CONVERSATIONS,
297
+ ): void {
268
298
  observedTurns.clear();
269
- spotlightRings.clear();
299
+ trackedConversationsCap = capacity;
270
300
  }
271
301
 
272
302
  // ─── injectors ───────────────────────────────────────────────────────────────
273
303
 
304
+ /** One unit this assembly's block carries, in selection order: `text` is
305
+ * the first produce's entry when re-emitted by a re-entry, and is rendered
306
+ * here otherwise. */
307
+ interface BlockSlot extends InjectionUnit {
308
+ text: string | undefined;
309
+ }
310
+
274
311
  export const memoryV3Injector: Injector = {
275
312
  name: "memory-v3-shadow",
276
313
  // High order so it sorts last; the live `<memory>` block uses the
@@ -278,20 +315,7 @@ export const memoryV3Injector: Injector = {
278
315
  // of this sort key, which only orders content-producing injectors.
279
316
  order: 1000,
280
317
  async produce(ctx: TurnContext): Promise<InjectionBlock | null> {
281
- const config = getConfig();
282
- if (!isMemoryEnabled(config)) {
283
- return null;
284
- }
285
- const live = isMemoryV3Live(config);
286
- if (!live) {
287
- return null;
288
- }
289
- if (!isPersonalMemoryAllowed(ctx.trust)) {
290
- return null;
291
- }
292
- // The front door keeps carried cards from history but defers current-turn
293
- // retrieval to the escalated leg so memory cannot delay its first token.
294
- if (ctx.callSite === "voiceFrontDoor") {
318
+ if (!turnIsEligible(ctx)) {
295
319
  return null;
296
320
  }
297
321
 
@@ -300,7 +324,7 @@ export const memoryV3Injector: Injector = {
300
324
  observed = await observeTurnOnce(ctx.conversationId, ctx.turnIndex);
301
325
  } catch (err) {
302
326
  if (err instanceof MemoryV3RetrievalUnavailableError) {
303
- queueMemoryV3ConversationNotice(err, ctx, live);
327
+ queueMemoryV3ConversationNotice(err, ctx);
304
328
  log.error(
305
329
  {
306
330
  err: err.message,
@@ -314,8 +338,8 @@ export const memoryV3Injector: Injector = {
314
338
  }
315
339
  // Empty selection → return null (attach nothing). The user-prompt-submit
316
340
  // hook skipped v2 retrieval under live, so a turn with nothing selected
317
- // simply gets no v3 `<memory>` block (prior turns' frozen cards still ride
318
- // history).
341
+ // simply gets no v3 `<memory>` block (prior turns' frozen sections still
342
+ // ride history).
319
343
  if (!observed || observed.selections.length === 0) {
320
344
  return null;
321
345
  }
@@ -324,72 +348,169 @@ export const memoryV3Injector: Injector = {
324
348
  const result = observed;
325
349
 
326
350
  try {
327
- const active = getActiveSlugs(ctx.conversationId);
328
- const netNew = result.selections
329
- .map((s) => s.slug)
330
- .filter((slug) => !active.has(slug));
351
+ // Partition this turn's injection units: each selected section under
352
+ // its own key, a page selected with none under `""` (its lead;
353
+ // capability content injects whole under `""` too).
354
+ // - A pair tombstoned since the turn's first produce is skipped: a
355
+ // re-entry never revives what the valve pruned.
356
+ // - On a re-entry assembly (`rendered` set by the first produce) an
357
+ // entry the first produce rendered is re-emitted from the memo byte
358
+ // for byte: the store counts it active, but its only copy rode the
359
+ // tail the re-injection strip cleared, so the store alone would
360
+ // read it as resident and drop it for the rest of the turn. A
361
+ // re-entry block carries no commit (and runtime assembly withholds
362
+ // one on a `reinjection` assembly besides), so a turn's sections
363
+ // are recorded once, at the first-call site; after a compaction they
364
+ // stay unclaimed until the next turn injects them net-new onto its
365
+ // own persisted message, and the newest-copy rule
366
+ // (`stripPrunedSectionsFromMessages`) retires the re-entry copy.
367
+ // - A pair active in the store is resident: a pointer entry, stamped
368
+ // with the turn's selection time below. Capability slugs are
369
+ // stamped too but left out of the pointer (no `memory/concepts/`
370
+ // path to point at).
371
+ // - Every other pair renders net-new, including one the first produce
372
+ // saw resident whose copy a compaction's store reset has since
373
+ // unclaimed (neither active nor tombstoned), in memory only.
374
+ // Under a run-messages replacement (`ctx.replacesRunMessages`: the
375
+ // Slack chronological transcript, rendered from persisted rows, so it
376
+ // carries no earlier turn's block) residency means nothing. The
377
+ // store's active set is not consulted, every pair renders or
378
+ // re-emits, none is pointed at, and the block carries no commit, so
379
+ // nothing is recorded or scheduled and runtime assembly attaches the
380
+ // block in memory only as the prompt's single copy. A Slack
381
+ // conversation whose transcript injector is absent is not replaced and
382
+ // injects as an ordinary committed turn.
383
+ const rendered = observedTurn(
384
+ ctx.conversationId,
385
+ ctx.turnIndex,
386
+ )?.rendered;
387
+ const firstProduce = rendered === undefined;
388
+ const replaced = ctx.replacesRunMessages === true;
389
+ const active = replaced ? null : getActiveSections(ctx.conversationId);
390
+ const pruned = firstProduce
391
+ ? undefined
392
+ : getPrunedSections(ctx.conversationId);
393
+ const resident: SectionRef[] = [];
394
+ const slots: BlockSlot[] = [];
395
+ for (const { slug, key, matched } of injectionUnits(result.selections)) {
396
+ if (pruned && sectionRefSetHas(pruned, slug, key)) {
397
+ continue;
398
+ }
399
+ const reemitted = rendered?.get(slug)?.get(key);
400
+ if (reemitted !== undefined) {
401
+ slots.push({ slug, key, matched, text: reemitted });
402
+ continue;
403
+ }
404
+ if (active && sectionRefSetHas(active, slug, key)) {
405
+ resident.push({ slug, key });
406
+ continue;
407
+ }
408
+ slots.push({ slug, key, matched, text: undefined });
409
+ }
410
+ rememberPointerEntries(
411
+ ctx.conversationId,
412
+ ctx.turnIndex,
413
+ resident.filter(({ slug }) => !isCapabilitySlug(slug)),
414
+ );
415
+ // The turn's selection time, stamped on the resident pairs NOW, in the
416
+ // same synchronous segment as the classification above: the page reads
417
+ // below yield to the event loop, and a prune valve queued by an earlier
418
+ // turn's commit fires on a timer, so it can run while they are awaited
419
+ // and would otherwise rank a pair this turn is about by its stale
420
+ // stamp and evict it. The stamp is a recency bump, not a claim, so a
421
+ // turn whose block never attaches (or that returns null below because
422
+ // every net-new pair rendered empty) bumps harmlessly. Only the first
423
+ // produce stamps: a re-entry re-emits the turn's selections, and a
424
+ // run-messages replacement claims nothing (`resident` is empty there).
425
+ // The net-new pairs take the same time with their record in the commit.
426
+ const selectedAt = Date.now();
427
+ if (firstProduce) {
428
+ touchSelected(ctx.conversationId, resident, selectedAt);
429
+ }
331
430
 
332
- // Render net-new cards, skipping slugs that resolve to no content
333
- // (deleted pages, unresolvable capabilities) — nothing is attached for
431
+ // Render net-new sections (each an independent page read, so in
432
+ // parallel), skipping pairs that resolve to no content (deleted pages,
433
+ // unresolvable capabilities, empty sections): nothing is attached for
334
434
  // them, so nothing is recorded either.
335
- const cards: Array<{ slug: Slug; card: string }> = [];
336
- for (const slug of netNew) {
337
- const card = await renderV3CardContent(slug);
338
- if (card.trim().length > 0) {
339
- cards.push({ slug, card });
435
+ const netNew = slots.filter((slot) => slot.text === undefined);
436
+ const renderedNow = await Promise.all(
437
+ netNew.map(({ slug, matched }) =>
438
+ renderV3InjectionEntry(slug, matched),
439
+ ),
440
+ );
441
+ for (const [i, slot] of netNew.entries()) {
442
+ slot.text = renderedNow[i]!;
443
+ }
444
+ const entries: Array<SectionRef & { text: string }> = [];
445
+ for (const { slug, key, text } of slots) {
446
+ if (text !== undefined && text.trim().length > 0) {
447
+ entries.push({ slug, key, text });
340
448
  }
341
449
  }
342
- // Every net-new card rendered empty: return null rather than an
450
+ // Every net-new section rendered empty: return null rather than an
343
451
  // empty-text block. Under live there is no v2 block, so the turn simply
344
- // gets no new memory. Distinct from the all-repeat case (empty `netNew`),
345
- // where the empty block correctly keeps v2 suppressed because the cards
346
- // already ride history.
347
- if (netNew.length > 0 && cards.length === 0) {
452
+ // gets no new memory. Distinct from the all-repeat case (empty
453
+ // `netNew`), where the empty block correctly keeps v2 suppressed
454
+ // because the sections already ride history.
455
+ if (netNew.length > 0 && entries.length === 0) {
348
456
  return null;
349
457
  }
350
- const entries = cards.map(({ slug, card }) => ({
351
- slug,
352
- // Capability cards (skills / CLI commands) render with their own
353
- // `# Skill:` / `# CLI command:` headers, which the prune valve's
354
- // `# memory/concepts/<slug>.md` section grammar can never locate to
355
- // free. Record them at zero bytes so they never inflate the freeable
356
- // resident accounting (the valve would otherwise loop-fire on bytes
357
- // it cannot free).
358
- bytes: isCapabilitySlug(slug) ? 0 : cardBytes(card),
359
- }));
360
-
361
- // The everInjected store write and the prune-valve schedule are
362
- // DEFERRED to this commit callback, invoked by runtime assembly at the
363
- // point where attachment is guaranteed (the turn's tail is a user
364
- // message — the same gate as metadata capture). Recording here in
365
- // `produce()` would let a never-attached turn (non-user tail) claim
366
- // cards in the store, suppressing them until compaction. The valve is
367
- // scheduled after `recordInjected` so the resident accounting includes
368
- // this turn's cards; stable-prefix lane members (core/hot/fresh) are
369
- // exempt — the selector's stable prefix must never be pruned out from
370
- // under it.
371
- const commit = (): void => {
372
- recordInjected(ctx.conversationId, entries);
373
- schedulePruneValve(ctx.conversationId, {
374
- exemptSlugs: new Set<Slug>([
375
- ...result.lanes.core,
376
- ...result.lanes.hot,
377
- ...result.lanes.fresh,
378
- ]),
379
- });
380
- };
381
458
 
382
459
  // Empty net-new → empty-text block: assembly attaches no content
383
460
  // (`applyInjectionBlock` no-ops empty text) but the block's presence
384
461
  // still marks v3 as this turn's `<memory>` source for v2 suppression.
385
- const inner = renderCardsBlockInner(cards.map((c) => c.card));
386
- return {
462
+ const inner = renderInjectionBlockInner(entries.map((e) => e.text));
463
+ const block: InjectionBlock = {
387
464
  id: MEMORY_V3_BLOCK_ID,
388
465
  text: inner.length === 0 ? "" : wrapMemoryBlock(inner),
389
466
  // Mirror v2's dynamic `<memory>` block placement.
390
467
  placement: "after-memory-prefix",
391
- meta: { [MEMORY_V3_COMMIT_META_KEY]: commit },
392
468
  };
469
+ if (!firstProduce) {
470
+ return block;
471
+ }
472
+
473
+ rememberRendered(ctx.conversationId, ctx.turnIndex, entries);
474
+ // A block rendered for a run-messages replacement rides that prompt
475
+ // only: no copy of it persists, so the store must not claim its
476
+ // sections and the valve has nothing new to account for.
477
+ if (replaced) {
478
+ return block;
479
+ }
480
+ // The net-new record and the prune-valve schedule are DEFERRED to this
481
+ // commit callback, invoked by runtime assembly at the point where
482
+ // attachment is guaranteed (the turn's tail is a user message, the
483
+ // same gate as metadata capture). Recording here in `produce()` would
484
+ // let a never-attached turn (non-user tail) claim sections in the
485
+ // store, suppressing them until compaction. Only the turn's first
486
+ // produce carries it: a re-entry block re-emits what this one rendered
487
+ // and is never persisted, so it must not record anything. The valve is
488
+ // scheduled after the record so the resident accounting, and the
489
+ // recency it ranks by, include this turn's sections (the resident
490
+ // pairs carry their stamp from the classification above); it evicts by
491
+ // recency with no lane exemptions. It runs on a timer, so this turn's
492
+ // block may not have folded back into the live history when it strips;
493
+ // a section it prunes from this very turn is stripped by assembly Step
494
+ // 0 on the next turn, which applies the store's full tombstone set
495
+ // every turn.
496
+ const commit = (): void => {
497
+ recordInjected(
498
+ ctx.conversationId,
499
+ entries.map(({ slug, key, text }) => ({
500
+ slug,
501
+ key,
502
+ // Capability content (skills / CLI commands) renders with its own
503
+ // `# Skill:` / `# CLI command:` header, which the prune valve's
504
+ // section grammar can never locate to free. Record it at zero
505
+ // bytes so it never inflates the freeable resident accounting
506
+ // (the valve would otherwise loop-fire on bytes it cannot free).
507
+ bytes: isCapabilitySlug(slug) ? 0 : renderedBytes(text),
508
+ })),
509
+ selectedAt,
510
+ );
511
+ schedulePruneValve(ctx.conversationId);
512
+ };
513
+ return { ...block, meta: { [MEMORY_V3_COMMIT_META_KEY]: commit } };
393
514
  } catch (err) {
394
515
  log.warn(
395
516
  {
@@ -403,73 +524,40 @@ export const memoryV3Injector: Injector = {
403
524
  },
404
525
  };
405
526
 
406
- export const memoryV3SpotlightInjector: Injector = {
407
- name: "memory-v3-spotlight",
408
- // After the cards injector so the shared memo is (usually) already primed.
527
+ export const memoryV3PointerInjector: Injector = {
528
+ name: "memory-v3-pointer",
529
+ // After the sections injector, whose produce records the resident
530
+ // selections of this assembly on the shared memo (a re-entry recomputes
531
+ // them, so the pointer follows what its own block re-emitted or rendered).
409
532
  order: 1001,
410
533
  async produce(ctx: TurnContext): Promise<InjectionBlock | null> {
411
- const config = getConfig();
412
- if (!isMemoryEnabled(config)) {
534
+ if (!turnIsEligible(ctx)) {
413
535
  return null;
414
536
  }
415
- // The spotlight rides the live `<memory>` layer; with `memory.v3.live` off
416
- // it produces nothing and keeps no ring state.
417
- if (!isMemoryV3Live(config)) {
537
+ const remembered = observedTurn(ctx.conversationId, ctx.turnIndex)?.pointer;
538
+ if (!remembered || remembered.length === 0) {
418
539
  return null;
419
540
  }
420
- if (!isPersonalMemoryAllowed(ctx.trust)) {
421
- return null;
422
- }
423
- if (ctx.callSite === "voiceFrontDoor") {
424
- return null;
425
- }
426
-
427
- try {
428
- const result = await observeTurnOnce(ctx.conversationId, ctx.turnIndex);
429
- if (!result || result.selections.length === 0) {
430
- return null;
431
- }
432
-
433
- const { n, windowTurns } = config.memory.v3.spotlight;
434
- const current = computeSpotlightEntries(result, n);
435
- const window = updateSpotlightWindow(
436
- ctx.conversationId,
437
- ctx.turnIndex,
438
- current,
439
- windowTurns,
440
- );
441
- if (window.length === 0) {
442
- return null;
443
- }
444
-
445
- return {
446
- id: MEMORY_V3_SPOTLIGHT_BLOCK_ID,
447
- text: wrapMemorySpotlightBlock(renderSpotlightInner(window)),
448
- // Immediately after frozen `<memory>` cards. `countMemoryPrefixBlocks`
449
- // counts `<memory>` but not `<memory_spotlight>`, so this lands
450
- // between the cards and NOW.md / user text.
451
- placement: "after-memory-prefix",
452
- };
453
- } catch (err) {
454
- if (err instanceof MemoryV3RetrievalUnavailableError) {
455
- queueMemoryV3ConversationNotice(err, ctx, true);
456
- log.error(
457
- {
458
- err: err.message,
459
- conversationId: ctx.conversationId,
460
- },
461
- "memory-v3 spotlight selection failed; skipping spotlight",
462
- );
463
- return null;
464
- }
465
- log.warn(
466
- {
467
- err: err instanceof Error ? err.message : String(err),
468
- conversationId: ctx.conversationId,
469
- },
470
- "memory-v3 spotlight render failed (non-fatal) — skipping spotlight",
471
- );
541
+ // Re-validate against the store at render time: the sections injector
542
+ // classified these pairs as resident before awaiting its page reads, and
543
+ // a prune valve queued by an earlier turn's commit can fire in that
544
+ // window and tombstone one of them, stripping its body from the live
545
+ // history. A pair no longer active is dropped rather than pointed at; it
546
+ // is simply absent this turn and re-injects on its next selection.
547
+ const active = getActiveSections(ctx.conversationId);
548
+ const entries = remembered.filter(({ slug, key }) =>
549
+ sectionRefSetHas(active, slug, key),
550
+ );
551
+ if (entries.length === 0) {
472
552
  return null;
473
553
  }
554
+ return {
555
+ id: MEMORY_V3_POINTER_BLOCK_ID,
556
+ text: wrapMemoryPointerBlock(renderPointerInner(entries)),
557
+ // Immediately after the frozen `<memory>` blocks: `countMemoryPrefixBlocks`
558
+ // counts `<memory>` but not `<memory_pointer>`, so this lands between
559
+ // the sections and NOW.md / user text.
560
+ placement: "after-memory-prefix",
561
+ };
474
562
  },
475
563
  };