@vellumai/assistant 0.4.42 → 0.4.43

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 (840) hide show
  1. package/.env.example +1 -6
  2. package/.prettierignore +3 -0
  3. package/ARCHITECTURE.md +131 -393
  4. package/Dockerfile +0 -1
  5. package/README.md +73 -83
  6. package/bun.lock +8 -2
  7. package/docs/architecture/integrations.md +16 -21
  8. package/docs/architecture/memory.md +1 -1
  9. package/docs/architecture/scheduling.md +63 -63
  10. package/docs/architecture/security.md +3 -3
  11. package/docs/runbook-trusted-contacts.md +11 -12
  12. package/docs/trusted-contact-access.md +39 -39
  13. package/package.json +5 -8
  14. package/src/__tests__/access-request-decision.test.ts +4 -4
  15. package/src/__tests__/active-skill-tools.test.ts +49 -34
  16. package/src/__tests__/actor-token-service.test.ts +55 -85
  17. package/src/__tests__/amazon-cdp-integration.test.ts +14 -26
  18. package/src/__tests__/app-bundler.test.ts +14 -368
  19. package/src/__tests__/app-compiler.test.ts +0 -1
  20. package/src/__tests__/app-executors.test.ts +10 -1
  21. package/src/__tests__/approval-hardcoded-copy-guard.test.ts +1 -1
  22. package/src/__tests__/approval-primitive.test.ts +2 -4
  23. package/src/__tests__/approval-routes-http.test.ts +1 -1
  24. package/src/__tests__/asset-materialize-tool.test.ts +1 -4
  25. package/src/__tests__/asset-search-tool.test.ts +1 -4
  26. package/src/__tests__/assistant-attachments.test.ts +23 -0
  27. package/src/__tests__/assistant-feature-flags-integration.test.ts +4 -8
  28. package/src/__tests__/assistant-id-boundary-guard.test.ts +5 -5
  29. package/src/__tests__/attachments-store.test.ts +1 -4
  30. package/src/__tests__/avatar-e2e.test.ts +43 -23
  31. package/src/__tests__/browser-fill-credential.test.ts +1 -1
  32. package/src/__tests__/bundled-skill-retrieval-guard.test.ts +2 -9
  33. package/src/__tests__/call-controller.test.ts +4 -8
  34. package/src/__tests__/call-conversation-messages.test.ts +1 -1
  35. package/src/__tests__/call-domain.test.ts +250 -8
  36. package/src/__tests__/call-pointer-message-composer.test.ts +14 -14
  37. package/src/__tests__/call-pointer-messages.test.ts +7 -11
  38. package/src/__tests__/call-recovery.test.ts +47 -0
  39. package/src/__tests__/call-routes-http.test.ts +13 -0
  40. package/src/__tests__/call-start-guardian-guard.test.ts +1 -1
  41. package/src/__tests__/callback-handoff-copy.test.ts +5 -5
  42. package/src/__tests__/canonical-guardian-store.test.ts +3 -3
  43. package/src/__tests__/channel-approval-routes.test.ts +101 -134
  44. package/src/__tests__/channel-approval.test.ts +0 -201
  45. package/src/__tests__/channel-approvals.test.ts +2 -2
  46. package/src/__tests__/channel-delivery-store.test.ts +16 -24
  47. package/src/__tests__/channel-guardian.test.ts +641 -740
  48. package/src/__tests__/channel-invite-transport.test.ts +1 -2
  49. package/src/__tests__/channel-policy.test.ts +9 -12
  50. package/src/__tests__/channel-readiness-service.test.ts +156 -45
  51. package/src/__tests__/channel-reply-delivery.test.ts +3 -3
  52. package/src/__tests__/channel-retry-sweep.test.ts +7 -7
  53. package/src/__tests__/checker.test.ts +10 -7
  54. package/src/__tests__/chrome-cdp.test.ts +57 -17
  55. package/src/__tests__/cli-help-reference-sync.test.ts +26 -0
  56. package/src/__tests__/compaction.benchmark.test.ts +25 -5
  57. package/src/__tests__/computer-use-session-lifecycle.test.ts +1 -1
  58. package/src/__tests__/computer-use-session-working-dir.test.ts +2 -6
  59. package/src/__tests__/computer-use-skill-lifecycle-cleanup.test.ts +1 -1
  60. package/src/__tests__/config-loader-backfill.test.ts +310 -0
  61. package/src/__tests__/config-watcher.test.ts +1 -5
  62. package/src/__tests__/confirmation-request-guardian-bridge.test.ts +3 -5
  63. package/src/__tests__/connection-policy.test.ts +3 -62
  64. package/src/__tests__/contacts-tools.test.ts +0 -2
  65. package/src/__tests__/context-memory-e2e.test.ts +11 -7
  66. package/src/__tests__/context-overflow-policy.test.ts +2 -2
  67. package/src/__tests__/context-window-manager.test.ts +220 -61
  68. package/src/__tests__/conversation-attention-store.test.ts +178 -2
  69. package/src/__tests__/conversation-attention-telegram.test.ts +8 -11
  70. package/src/__tests__/conversation-pairing.test.ts +14 -14
  71. package/src/__tests__/conversation-routes-guardian-reply.test.ts +1 -1
  72. package/src/__tests__/conversation-store.test.ts +2 -2
  73. package/src/__tests__/conversation-unread-route.test.ts +155 -0
  74. package/src/__tests__/credential-metadata-store.test.ts +0 -2
  75. package/src/__tests__/credential-security-invariants.test.ts +9 -16
  76. package/src/__tests__/credentials-cli.test.ts +49 -5
  77. package/src/__tests__/daemon-assistant-events.test.ts +4 -22
  78. package/src/__tests__/db-migration-rollback.test.ts +2 -2
  79. package/src/__tests__/deterministic-verification-control-plane.test.ts +19 -19
  80. package/src/__tests__/dictation-mode-detection.test.ts +1 -1
  81. package/src/__tests__/dynamic-page-surface.test.ts +2 -2
  82. package/src/__tests__/dynamic-skill-workflow-prompt.test.ts +2 -6
  83. package/src/__tests__/email-cli.test.ts +12 -12
  84. package/src/__tests__/email-service-config-fallback.test.ts +1 -1
  85. package/src/__tests__/emit-signal-routing-intent.test.ts +3 -18
  86. package/src/__tests__/event-bus.test.ts +0 -1
  87. package/src/__tests__/followup-tools.test.ts +0 -2
  88. package/src/__tests__/gateway-client-managed-outbound.test.ts +6 -6
  89. package/src/__tests__/gateway-only-enforcement.test.ts +13 -77
  90. package/src/__tests__/gateway-only-guard.test.ts +5 -0
  91. package/src/__tests__/guardian-action-conversation-turn.test.ts +3 -3
  92. package/src/__tests__/guardian-action-followup-executor.test.ts +29 -94
  93. package/src/__tests__/guardian-action-followup-store.test.ts +2 -12
  94. package/src/__tests__/guardian-action-grant-mint-consume.test.ts +48 -194
  95. package/src/__tests__/guardian-action-late-reply.test.ts +12 -12
  96. package/src/__tests__/guardian-action-store.test.ts +2 -2
  97. package/src/__tests__/guardian-action-sweep.test.ts +5 -5
  98. package/src/__tests__/guardian-decision-primitive-canonical.test.ts +1 -3
  99. package/src/__tests__/guardian-dispatch.test.ts +5 -46
  100. package/src/__tests__/guardian-grant-minting.test.ts +5 -44
  101. package/src/__tests__/guardian-outbound-http.test.ts +95 -114
  102. package/src/__tests__/guardian-question-mode.test.ts +1 -4
  103. package/src/__tests__/guardian-routing-invariants.test.ts +5 -13
  104. package/src/__tests__/guardian-routing-state.test.ts +3 -3
  105. package/src/__tests__/guardian-verification-voice-binding.test.ts +64 -7
  106. package/src/__tests__/guardian-verify-setup-skill-regression.test.ts +2 -2
  107. package/src/__tests__/handle-user-message-secret-resume.test.ts +3 -5
  108. package/src/__tests__/handlers-user-message-approval-consumption.test.ts +16 -34
  109. package/src/__tests__/headless-browser-interactions.test.ts +1 -1
  110. package/src/__tests__/headless-browser-navigate.test.ts +1 -1
  111. package/src/__tests__/headless-browser-read-tools.test.ts +1 -1
  112. package/src/__tests__/headless-browser-snapshot.test.ts +1 -1
  113. package/src/__tests__/heartbeat-service.test.ts +1 -1
  114. package/src/__tests__/home-base-bootstrap.test.ts +0 -2
  115. package/src/__tests__/host-shell-tool.test.ts +3 -12
  116. package/src/__tests__/inbound-invite-redemption.test.ts +2 -2
  117. package/src/__tests__/ingress-url-consistency.test.ts +0 -64
  118. package/src/__tests__/integration-status.test.ts +8 -8
  119. package/src/__tests__/intent-routing.test.ts +9 -13
  120. package/src/__tests__/invite-redemption-service.test.ts +4 -4
  121. package/src/__tests__/invite-routes-http.test.ts +10 -10
  122. package/src/__tests__/llm-usage-store.test.ts +45 -9
  123. package/src/__tests__/local-gateway-health.test.ts +209 -0
  124. package/src/__tests__/managed-avatar-client.test.ts +23 -12
  125. package/src/__tests__/managed-skill-lifecycle.test.ts +1 -2
  126. package/src/__tests__/managed-store.test.ts +29 -12
  127. package/src/__tests__/managed-twitter-guardrails.test.ts +353 -0
  128. package/src/__tests__/mcp-cli.test.ts +1 -1
  129. package/src/__tests__/mcp-health-check.test.ts +1 -1
  130. package/src/__tests__/media-generate-image.test.ts +1 -1
  131. package/src/__tests__/media-reuse-story.e2e.test.ts +1 -4
  132. package/src/__tests__/memory-context-benchmark.benchmark.test.ts +9 -6
  133. package/src/__tests__/memory-regressions.test.ts +1 -166
  134. package/src/__tests__/messaging-send-tool.test.ts +8 -4
  135. package/src/__tests__/migration-export-http.test.ts +2 -2
  136. package/src/__tests__/migration-transport.test.ts +44 -0
  137. package/src/__tests__/non-member-access-request.test.ts +49 -36
  138. package/src/__tests__/notification-broadcaster.test.ts +15 -15
  139. package/src/__tests__/notification-decision-fallback.test.ts +2 -2
  140. package/src/__tests__/notification-decision-strategy.test.ts +4 -4
  141. package/src/__tests__/notification-deep-link.test.ts +3 -3
  142. package/src/__tests__/notification-guardian-path.test.ts +6 -44
  143. package/src/__tests__/notification-routing-intent.test.ts +11 -7
  144. package/src/__tests__/oauth-cli.test.ts +1 -1
  145. package/src/__tests__/onboarding-starter-tasks.test.ts +2 -6
  146. package/src/__tests__/onboarding-template-contract.test.ts +2 -2
  147. package/src/__tests__/platform.test.ts +168 -5
  148. package/src/__tests__/playbook-execution.test.ts +0 -2
  149. package/src/__tests__/playbook-tools.test.ts +0 -2
  150. package/src/__tests__/pricing.test.ts +125 -0
  151. package/src/__tests__/provider-error-scenarios.test.ts +9 -3
  152. package/src/__tests__/recording-handler.test.ts +46 -80
  153. package/src/__tests__/recording-state-machine.test.ts +112 -183
  154. package/src/__tests__/registry.test.ts +1 -1
  155. package/src/__tests__/relay-server.test.ts +69 -71
  156. package/src/__tests__/reminder-store.test.ts +3 -3
  157. package/src/__tests__/request-file-tool.test.ts +2 -2
  158. package/src/__tests__/ride-shotgun-handler.test.ts +2 -33
  159. package/src/__tests__/runtime-attachment-metadata.test.ts +3 -3
  160. package/src/__tests__/runtime-events-sse-parity.test.ts +1 -1
  161. package/src/__tests__/scaffold-managed-skill-tool.test.ts +4 -4
  162. package/src/__tests__/schedule-store.test.ts +13 -4
  163. package/src/__tests__/schedule-tools.test.ts +0 -2
  164. package/src/__tests__/scheduler-recurrence.test.ts +3 -4
  165. package/src/__tests__/scoped-approval-grants.test.ts +3 -5
  166. package/src/__tests__/scoped-grant-security-matrix.test.ts +6 -8
  167. package/src/__tests__/secret-prompt-log-hygiene.test.ts +1 -1
  168. package/src/__tests__/secret-response-routing.test.ts +1 -1
  169. package/src/__tests__/send-endpoint-busy.test.ts +1 -1
  170. package/src/__tests__/sequence-store.test.ts +0 -2
  171. package/src/__tests__/server-history-render.test.ts +2 -199
  172. package/src/__tests__/session-abort-tool-results.test.ts +9 -3
  173. package/src/__tests__/session-agent-loop.test.ts +107 -3
  174. package/src/__tests__/session-confirmation-signals.test.ts +10 -4
  175. package/src/__tests__/session-conflict-gate.test.ts +9 -3
  176. package/src/__tests__/session-init.benchmark.test.ts +22 -13
  177. package/src/__tests__/session-load-history-repair.test.ts +6 -3
  178. package/src/__tests__/session-pre-run-repair.test.ts +9 -3
  179. package/src/__tests__/session-profile-injection.test.ts +9 -3
  180. package/src/__tests__/session-provider-retry-repair.test.ts +10 -4
  181. package/src/__tests__/session-queue.test.ts +10 -4
  182. package/src/__tests__/session-runtime-assembly.test.ts +28 -18
  183. package/src/__tests__/session-skill-tools.test.ts +2 -3
  184. package/src/__tests__/session-slash-known.test.ts +11 -4
  185. package/src/__tests__/session-slash-queue.test.ts +11 -4
  186. package/src/__tests__/session-slash-unknown.test.ts +12 -4
  187. package/src/__tests__/session-surfaces-deselection.test.ts +2 -2
  188. package/src/__tests__/session-surfaces-task-progress.test.ts +3 -3
  189. package/src/__tests__/session-tool-setup-app-refresh.test.ts +1 -1
  190. package/src/__tests__/session-tool-setup-memory-scope.test.ts +1 -1
  191. package/src/__tests__/session-tool-setup-side-effect-flag.test.ts +1 -1
  192. package/src/__tests__/session-usage.test.ts +180 -0
  193. package/src/__tests__/session-workspace-cache-state.test.ts +8 -2
  194. package/src/__tests__/session-workspace-injection.test.ts +8 -2
  195. package/src/__tests__/session-workspace-tool-tracking.test.ts +8 -2
  196. package/src/__tests__/skill-feature-flags-integration.test.ts +5 -11
  197. package/src/__tests__/skill-feature-flags.test.ts +1 -0
  198. package/src/__tests__/skill-include-graph.test.ts +1 -0
  199. package/src/__tests__/skill-load-feature-flag.test.ts +3 -9
  200. package/src/__tests__/skill-load-tool.test.ts +90 -12
  201. package/src/__tests__/skill-projection-feature-flag.test.ts +14 -15
  202. package/src/__tests__/skills-uninstall.test.ts +131 -0
  203. package/src/__tests__/skills.test.ts +32 -16
  204. package/src/__tests__/slack-block-formatting.test.ts +1 -1
  205. package/src/__tests__/slack-channel-config.test.ts +71 -12
  206. package/src/__tests__/slack-inbound-verification.test.ts +7 -7
  207. package/src/__tests__/slack-share-routes.test.ts +1 -1
  208. package/src/__tests__/slack-skill.test.ts +2 -2
  209. package/src/__tests__/slash-commands-catalog.test.ts +1 -0
  210. package/src/__tests__/slash-commands-resolver.test.ts +1 -0
  211. package/src/__tests__/starter-task-flow.test.ts +1 -1
  212. package/src/__tests__/subagent-manager-notify.test.ts +1 -1
  213. package/src/__tests__/subagent-tools.test.ts +2 -2
  214. package/src/__tests__/system-prompt.test.ts +4 -8
  215. package/src/__tests__/task-compiler.test.ts +0 -2
  216. package/src/__tests__/task-management-tools.test.ts +0 -2
  217. package/src/__tests__/task-runner.test.ts +0 -2
  218. package/src/__tests__/task-scheduler.test.ts +2 -2
  219. package/src/__tests__/telegram-bot-username-resolution.test.ts +46 -44
  220. package/src/__tests__/terminal-tools.test.ts +1 -11
  221. package/src/__tests__/thread-seed-composer.test.ts +3 -1
  222. package/src/__tests__/tool-approval-handler.test.ts +5 -7
  223. package/src/__tests__/tool-executor.test.ts +2 -2
  224. package/src/__tests__/tool-grant-request-escalation.test.ts +3 -5
  225. package/src/__tests__/tool-notification-listener.test.ts +1 -1
  226. package/src/__tests__/tool-profiling-listener.test.ts +1 -1
  227. package/src/__tests__/tool-trace-listener.test.ts +1 -2
  228. package/src/__tests__/trace-emitter.test.ts +1 -1
  229. package/src/__tests__/trust-context-guards.test.ts +1 -1
  230. package/src/__tests__/trust-store.test.ts +44 -395
  231. package/src/__tests__/trusted-contact-approval-notifier.test.ts +6 -8
  232. package/src/__tests__/trusted-contact-inline-approval-integration.test.ts +5 -7
  233. package/src/__tests__/trusted-contact-lifecycle-notifications.test.ts +6 -6
  234. package/src/__tests__/trusted-contact-multichannel.test.ts +54 -47
  235. package/src/__tests__/trusted-contact-verification.test.ts +12 -12
  236. package/src/__tests__/twilio-config.test.ts +11 -2
  237. package/src/__tests__/twilio-provider.test.ts +6 -4
  238. package/src/__tests__/twilio-routes.test.ts +408 -86
  239. package/src/__tests__/twitter-platform-proxy-client.test.ts +450 -0
  240. package/src/__tests__/update-bulletin-format.test.ts +1 -1
  241. package/src/__tests__/update-bulletin-state.test.ts +1 -1
  242. package/src/__tests__/update-bulletin.test.ts +4 -8
  243. package/src/__tests__/update-template-contract.test.ts +1 -1
  244. package/src/__tests__/usage-cache-backfill-migration.test.ts +406 -0
  245. package/src/__tests__/usage-routes.test.ts +23 -5
  246. package/src/__tests__/user-reference.test.ts +1 -1
  247. package/src/__tests__/{guardian-control-plane-policy.test.ts → verification-control-plane-policy.test.ts} +142 -170
  248. package/src/__tests__/{guardian-verification-intent-routing.test.ts → verification-session-intent-routing.test.ts} +16 -16
  249. package/src/__tests__/view-image-tool.test.ts +0 -2
  250. package/src/__tests__/voice-ingress-preflight.test.ts +36 -0
  251. package/src/__tests__/voice-invite-redemption.test.ts +18 -18
  252. package/src/__tests__/voice-scoped-grant-consumer.test.ts +7 -7
  253. package/src/__tests__/voice-session-bridge.test.ts +14 -16
  254. package/src/__tests__/workspace-policy.test.ts +1 -1
  255. package/src/approvals/AGENTS.md +4 -4
  256. package/src/approvals/approval-primitive.ts +2 -2
  257. package/src/approvals/guardian-decision-primitive.ts +1 -1
  258. package/src/approvals/guardian-request-resolvers.ts +3 -4
  259. package/src/bundler/app-bundler.ts +29 -217
  260. package/src/calls/active-call-lease.ts +207 -0
  261. package/src/calls/call-constants.ts +0 -7
  262. package/src/calls/call-controller.ts +1 -1
  263. package/src/calls/call-conversation-messages.ts +6 -6
  264. package/src/calls/call-domain.ts +73 -38
  265. package/src/calls/call-pointer-message-composer.ts +6 -6
  266. package/src/calls/call-pointer-messages.ts +14 -13
  267. package/src/calls/call-recovery.ts +2 -0
  268. package/src/calls/call-store.ts +21 -28
  269. package/src/calls/guardian-action-sweep.ts +6 -8
  270. package/src/calls/guardian-dispatch.ts +2 -6
  271. package/src/calls/relay-access-wait.ts +4 -4
  272. package/src/calls/relay-server.ts +69 -80
  273. package/src/calls/relay-setup-router.ts +16 -21
  274. package/src/calls/relay-verification.ts +27 -28
  275. package/src/calls/twilio-config.ts +28 -3
  276. package/src/calls/twilio-provider.ts +5 -5
  277. package/src/calls/twilio-rest.ts +26 -27
  278. package/src/calls/twilio-routes.ts +67 -54
  279. package/src/calls/types.ts +8 -8
  280. package/src/calls/voice-ingress-preflight.ts +110 -0
  281. package/src/calls/voice-session-bridge.ts +7 -7
  282. package/src/channels/config.ts +1 -10
  283. package/src/{config/channel-permission-profiles.ts → channels/permission-profiles.ts} +1 -1
  284. package/src/channels/types.ts +2 -13
  285. package/src/cli/__tests__/notifications.test.ts +1 -1
  286. package/src/{amazon → cli/commands/amazon}/client.ts +99 -42
  287. package/src/cli/{amazon.ts → commands/amazon/index.ts} +12 -17
  288. package/src/{amazon → cli/commands/amazon}/request-extractor.ts +39 -3
  289. package/src/cli/commands/amazon/session.ts +116 -0
  290. package/src/cli/{audit.ts → commands/audit.ts} +2 -4
  291. package/src/cli/{autonomy.ts → commands/autonomy.ts} +1 -3
  292. package/src/cli/commands/browser-relay.ts +520 -0
  293. package/src/cli/commands/channel-verification-sessions.ts +442 -0
  294. package/src/cli/{completions.ts → commands/completions.ts} +1 -3
  295. package/src/cli/{config.ts → commands/config.ts} +3 -5
  296. package/src/cli/{contacts.ts → commands/contacts.ts} +15 -17
  297. package/src/cli/{credentials.ts → commands/credentials.ts} +9 -10
  298. package/src/cli/{default-action.ts → commands/default-action.ts} +3 -3
  299. package/src/cli/{dev.ts → commands/dev.ts} +4 -6
  300. package/src/cli/{doctor.ts → commands/doctor.ts} +36 -60
  301. package/src/cli/{email.ts → commands/email.ts} +2 -2
  302. package/src/cli/{keys.ts → commands/keys.ts} +6 -6
  303. package/src/cli/{map.ts → commands/map.ts} +85 -93
  304. package/src/cli/{mcp.ts → commands/mcp.ts} +5 -7
  305. package/src/cli/{memory.ts → commands/memory.ts} +6 -7
  306. package/src/cli/{notifications.ts → commands/notifications.ts} +8 -10
  307. package/src/cli/{oauth.ts → commands/oauth.ts} +2 -2
  308. package/src/cli/commands/platform.ts +176 -0
  309. package/src/cli/{sequence.ts → commands/sequence.ts} +3 -3
  310. package/src/cli/{sessions.ts → commands/sessions.ts} +32 -52
  311. package/src/cli/commands/skills.ts +498 -0
  312. package/src/cli/{trust.ts → commands/trust.ts} +2 -4
  313. package/src/{__tests__/twitter-cli-error-shaping.test.ts → cli/commands/twitter/__tests__/cli-error-shaping.test.ts} +43 -2
  314. package/src/cli/commands/twitter/__tests__/cli-read-routing.test.ts +483 -0
  315. package/src/{__tests__/twitter-cli-routing.test.ts → cli/commands/twitter/__tests__/cli-routing.test.ts} +130 -4
  316. package/src/{__tests__/twitter-oauth-client.test.ts → cli/commands/twitter/__tests__/oauth-client.test.ts} +2 -2
  317. package/src/{twitter → cli/commands/twitter}/client.ts +17 -7
  318. package/src/cli/{twitter.ts → commands/twitter/index.ts} +322 -273
  319. package/src/cli/commands/twitter/router.ts +396 -0
  320. package/src/cli/commands/twitter/session.ts +121 -0
  321. package/src/cli/db.ts +1 -0
  322. package/src/cli/http-client.ts +87 -0
  323. package/src/cli/logger.ts +6 -0
  324. package/src/cli/main-screen.tsx +4 -3
  325. package/src/cli/output.ts +19 -0
  326. package/src/cli/program.ts +29 -27
  327. package/src/cli/reference.ts +27 -37
  328. package/src/cli.ts +452 -240
  329. package/src/config/assistant-feature-flags.ts +3 -15
  330. package/src/config/bundled-skills/_shared/CLI_RETRIEVAL_PATTERN.md +3 -6
  331. package/src/config/bundled-skills/agentmail/SKILL.md +4 -4
  332. package/src/config/bundled-skills/amazon/SKILL.md +15 -5
  333. package/src/config/bundled-skills/api-mapping/SKILL.md +4 -4
  334. package/src/config/bundled-skills/app-builder/SKILL.md +4 -3
  335. package/src/config/bundled-skills/browser/SKILL.md +4 -5
  336. package/src/config/bundled-skills/chatgpt-import/SKILL.md +4 -4
  337. package/src/config/bundled-skills/chatgpt-import/tools/chatgpt-import.ts +1 -1
  338. package/src/config/bundled-skills/claude-code/SKILL.md +4 -4
  339. package/src/config/bundled-skills/cli-discover/SKILL.md +4 -4
  340. package/src/config/bundled-skills/computer-use/SKILL.md +4 -4
  341. package/src/config/bundled-skills/contacts/SKILL.md +23 -77
  342. package/src/config/bundled-skills/deploy-fullstack-vercel/SKILL.md +4 -4
  343. package/src/config/bundled-skills/document/SKILL.md +4 -3
  344. package/src/config/bundled-skills/document-writer/SKILL.md +4 -4
  345. package/src/config/bundled-skills/doordash/SKILL.md +4 -11
  346. package/src/config/bundled-skills/doordash/__tests__/doordash-session.test.ts +8 -16
  347. package/src/config/bundled-skills/doordash/doordash-cli.ts +120 -86
  348. package/src/config/bundled-skills/doordash/lib/session.ts +1 -2
  349. package/src/config/bundled-skills/doordash/lib/shared/platform.ts +26 -9
  350. package/src/config/bundled-skills/elevenlabs-voice/SKILL.md +140 -0
  351. package/src/config/bundled-skills/email-setup/SKILL.md +4 -4
  352. package/src/config/bundled-skills/followups/SKILL.md +4 -3
  353. package/src/config/bundled-skills/frontend-design/SKILL.md +2 -0
  354. package/src/config/bundled-skills/google-calendar/SKILL.md +4 -4
  355. package/src/config/bundled-skills/google-oauth-setup/SKILL.md +4 -6
  356. package/src/config/bundled-skills/guardian-verify-setup/SKILL.md +26 -41
  357. package/src/config/bundled-skills/image-studio/SKILL.md +4 -5
  358. package/src/config/bundled-skills/image-studio/tools/media-generate-image.ts +1 -1
  359. package/src/config/bundled-skills/influencer/SKILL.md +19 -19
  360. package/src/{influencer → config/bundled-skills/influencer/scripts}/client.ts +73 -56
  361. package/src/config/bundled-skills/influencer/scripts/influencer.ts +267 -0
  362. package/src/config/bundled-skills/knowledge-graph/SKILL.md +4 -2
  363. package/src/config/bundled-skills/macos-automation/SKILL.md +4 -5
  364. package/src/config/bundled-skills/mcp-setup/SKILL.md +4 -4
  365. package/src/config/bundled-skills/media-processing/SKILL.md +3 -2
  366. package/src/config/bundled-skills/messaging/SKILL.md +6 -33
  367. package/src/config/bundled-skills/messaging/tools/messaging-send.ts +0 -5
  368. package/src/config/bundled-skills/notifications/SKILL.md +4 -4
  369. package/src/config/bundled-skills/notion/SKILL.md +4 -4
  370. package/src/config/bundled-skills/notion-oauth-setup/SKILL.md +4 -5
  371. package/src/config/bundled-skills/oauth-setup/SKILL.md +4 -5
  372. package/src/config/bundled-skills/phone-calls/SKILL.md +24 -458
  373. package/src/config/bundled-skills/phone-calls/references/CONFIG.md +83 -0
  374. package/src/config/bundled-skills/phone-calls/references/TRANSCRIPTS.md +57 -0
  375. package/src/config/bundled-skills/phone-calls/references/TROUBLESHOOTING.md +67 -0
  376. package/src/config/bundled-skills/playbooks/SKILL.md +4 -3
  377. package/src/config/bundled-skills/public-ingress/SKILL.md +65 -14
  378. package/src/config/bundled-skills/reminder/SKILL.md +4 -3
  379. package/src/config/bundled-skills/restaurant-reservation/SKILL.md +4 -6
  380. package/src/config/bundled-skills/schedule/SKILL.md +4 -3
  381. package/src/config/bundled-skills/screen-recording/SKILL.md +4 -3
  382. package/src/config/bundled-skills/self-upgrade/SKILL.md +4 -4
  383. package/src/config/bundled-skills/skills-catalog/SKILL.md +4 -4
  384. package/src/config/bundled-skills/slack/SKILL.md +4 -8
  385. package/src/config/bundled-skills/slack/tools/slack-channel-permissions.ts +1 -1
  386. package/src/config/bundled-skills/slack-app-setup/SKILL.md +66 -88
  387. package/src/config/bundled-skills/slack-digest-setup/SKILL.md +4 -5
  388. package/src/config/bundled-skills/slack-oauth-setup/SKILL.md +4 -5
  389. package/src/config/bundled-skills/start-the-day/SKILL.md +4 -4
  390. package/src/config/bundled-skills/subagent/SKILL.md +4 -3
  391. package/src/config/bundled-skills/tasks/SKILL.md +4 -3
  392. package/src/config/bundled-skills/telegram-setup/SKILL.md +63 -112
  393. package/src/config/bundled-skills/time-based-actions/SKILL.md +4 -3
  394. package/src/config/bundled-skills/transcribe/SKILL.md +4 -3
  395. package/src/config/bundled-skills/twilio-setup/SKILL.md +23 -50
  396. package/src/config/bundled-skills/twitter/SKILL.md +56 -14
  397. package/src/config/bundled-skills/typescript-eval/SKILL.md +4 -4
  398. package/src/config/bundled-skills/vercel-token-setup/SKILL.md +4 -5
  399. package/src/config/bundled-skills/voice-setup/SKILL.md +19 -45
  400. package/src/config/bundled-skills/watcher/SKILL.md +4 -3
  401. package/src/config/env-registry.ts +1 -10
  402. package/src/config/feature-flag-registry.json +0 -16
  403. package/src/config/loader.ts +78 -38
  404. package/src/config/schema.ts +143 -106
  405. package/src/config/schemas/channels.ts +80 -0
  406. package/src/config/schemas/heartbeat.ts +51 -0
  407. package/src/config/schemas/inference.ts +136 -0
  408. package/src/config/schemas/ingress.ts +81 -0
  409. package/src/config/schemas/logging.ts +21 -0
  410. package/src/config/schemas/memory-lifecycle.ts +67 -0
  411. package/src/config/schemas/memory-processing.ts +215 -0
  412. package/src/config/schemas/memory-retrieval.ts +222 -0
  413. package/src/config/schemas/memory-storage.ts +83 -0
  414. package/src/config/schemas/memory.ts +58 -0
  415. package/src/config/schemas/platform.ts +64 -0
  416. package/src/config/schemas/security.ts +54 -0
  417. package/src/config/schemas/swarm.ts +50 -0
  418. package/src/config/schemas/timeouts.ts +47 -0
  419. package/src/config/{agent-schema.ts → schemas/workspace-git.ts} +0 -97
  420. package/src/config/skill-state.ts +3 -13
  421. package/src/config/skills.ts +196 -75
  422. package/src/config/types.ts +1 -20
  423. package/src/contacts/contact-store.ts +12 -49
  424. package/src/contacts/contacts-write.ts +1 -5
  425. package/src/contacts/index.ts +0 -2
  426. package/src/contacts/types.ts +0 -8
  427. package/src/context/window-manager.ts +73 -14
  428. package/src/daemon/assistant-attachments.ts +9 -0
  429. package/src/daemon/computer-use-session.ts +3 -3
  430. package/src/daemon/connection-policy.ts +6 -21
  431. package/src/daemon/context-overflow-policy.ts +1 -1
  432. package/src/daemon/daemon-control.ts +46 -54
  433. package/src/daemon/doordash-steps.ts +1 -1
  434. package/src/daemon/handlers/config-channels.ts +407 -71
  435. package/src/daemon/handlers/config-ingress.ts +17 -85
  436. package/src/daemon/handlers/config-model.ts +145 -123
  437. package/src/daemon/handlers/config-slack-channel.ts +43 -29
  438. package/src/daemon/handlers/config-telegram.ts +32 -27
  439. package/src/daemon/handlers/config-voice.ts +1 -4
  440. package/src/daemon/handlers/dictation.ts +11 -16
  441. package/src/daemon/handlers/identity.ts +5 -6
  442. package/src/daemon/handlers/pairing.ts +5 -13
  443. package/src/daemon/handlers/recording.ts +97 -199
  444. package/src/daemon/handlers/session-history.ts +110 -96
  445. package/src/daemon/handlers/session-user-message.ts +29 -57
  446. package/src/daemon/handlers/sessions.ts +240 -137
  447. package/src/daemon/handlers/shared.ts +62 -95
  448. package/src/daemon/handlers/skills.ts +492 -543
  449. package/src/daemon/lifecycle.ts +168 -55
  450. package/src/daemon/{ipc-contract.ts → message-protocol.ts} +49 -49
  451. package/src/daemon/{ipc-contract → message-types}/computer-use.ts +0 -3
  452. package/src/daemon/{ipc-contract → message-types}/diagnostics.ts +0 -16
  453. package/src/daemon/{ipc-contract → message-types}/integrations.ts +29 -13
  454. package/src/daemon/{ipc-contract → message-types}/memory.ts +8 -0
  455. package/src/daemon/{ipc-contract → message-types}/notifications.ts +15 -1
  456. package/src/daemon/{ipc-contract → message-types}/sessions.ts +1 -0
  457. package/src/daemon/{ipc-contract → message-types}/shared.ts +0 -8
  458. package/src/daemon/{ipc-contract → message-types}/workspace.ts +2 -2
  459. package/src/daemon/providers-setup.ts +0 -5
  460. package/src/daemon/recording-executor.ts +0 -7
  461. package/src/daemon/ride-shotgun-handler.ts +9 -13
  462. package/src/daemon/server.ts +136 -510
  463. package/src/daemon/session-agent-loop-handlers.ts +22 -7
  464. package/src/daemon/session-agent-loop.ts +86 -24
  465. package/src/daemon/session-attachments.ts +1 -1
  466. package/src/daemon/session-error.ts +1 -1
  467. package/src/daemon/session-history.ts +20 -15
  468. package/src/daemon/session-lifecycle.ts +9 -7
  469. package/src/daemon/session-memory.ts +15 -1
  470. package/src/daemon/session-messaging.ts +10 -6
  471. package/src/daemon/session-notifiers.ts +10 -8
  472. package/src/daemon/session-process.ts +34 -25
  473. package/src/daemon/session-queue-manager.ts +1 -1
  474. package/src/daemon/session-runtime-assembly.ts +6 -25
  475. package/src/daemon/session-surfaces.ts +2 -2
  476. package/src/daemon/session-tool-setup.ts +1 -1
  477. package/src/daemon/session-usage.ts +119 -18
  478. package/src/daemon/session.ts +13 -9
  479. package/src/daemon/tool-side-effects.ts +6 -5
  480. package/src/daemon/trace-emitter.ts +1 -1
  481. package/src/daemon/{guardian-verification-intent.ts → verification-session-intent.ts} +16 -16
  482. package/src/daemon/watch-handler.ts +2 -5
  483. package/src/email/service.ts +8 -8
  484. package/src/events/domain-events.ts +0 -1
  485. package/src/events/tool-notification-listener.ts +1 -1
  486. package/src/followups/followup-store.ts +1 -2
  487. package/src/followups/types.ts +0 -6
  488. package/src/heartbeat/heartbeat-service.ts +1 -1
  489. package/src/inbound/platform-callback-registration.ts +1 -1
  490. package/src/inbound/public-ingress-urls.ts +0 -8
  491. package/src/index.ts +12 -0
  492. package/src/mcp/client.ts +1 -1
  493. package/src/mcp/manager.ts +1 -1
  494. package/src/memory/app-store.ts +1 -42
  495. package/src/memory/{guardian-verification.ts → channel-verification-sessions.ts} +110 -93
  496. package/src/memory/conversation-attention-store.ts +154 -0
  497. package/src/memory/conversation-bootstrap.ts +1 -1
  498. package/src/memory/conversation-crud.ts +53 -1
  499. package/src/memory/conversation-display-order-migration.ts +2 -3
  500. package/src/memory/conversation-queries.ts +1 -29
  501. package/src/memory/conversation-title-service.ts +26 -21
  502. package/src/memory/db-connection.ts +1 -8
  503. package/src/memory/db-init.ts +20 -0
  504. package/src/memory/delivery-crud.ts +4 -34
  505. package/src/memory/external-conversation-store.ts +1 -1
  506. package/src/memory/format-recall.ts +47 -0
  507. package/src/memory/guardian-action-store.ts +4 -5
  508. package/src/memory/guardian-rate-limits.ts +0 -3
  509. package/src/memory/invite-store.ts +1 -1
  510. package/src/memory/job-handlers/backfill.ts +9 -2
  511. package/src/memory/job-handlers/extraction.ts +2 -7
  512. package/src/memory/job-handlers/summarization.ts +1 -1
  513. package/src/memory/llm-usage-store.ts +11 -0
  514. package/src/memory/migrations/114-notifications.ts +12 -40
  515. package/src/memory/migrations/140-backfill-usage-cache-accounting.ts +357 -0
  516. package/src/memory/migrations/141-rename-verification-table.ts +55 -0
  517. package/src/memory/migrations/142-rename-verification-session-id-column.ts +32 -0
  518. package/src/memory/migrations/143-rename-guardian-verification-values.ts +48 -0
  519. package/src/memory/migrations/144-rename-voice-to-phone.ts +147 -0
  520. package/src/memory/migrations/index.ts +5 -0
  521. package/src/memory/migrations/registry.ts +30 -0
  522. package/src/memory/qdrant-circuit-breaker.ts +5 -0
  523. package/src/memory/retriever.test.ts +707 -0
  524. package/src/memory/retriever.ts +120 -116
  525. package/src/memory/schema/calls.ts +3 -7
  526. package/src/memory/schema/guardian.ts +2 -2
  527. package/src/memory/search/lexical.ts +4 -1
  528. package/src/memory/search/query-expansion.test.ts +70 -0
  529. package/src/memory/search/query-expansion.ts +118 -0
  530. package/src/memory/search/types.ts +18 -17
  531. package/src/messaging/providers/telegram-bot/adapter.ts +1 -1
  532. package/src/messaging/providers/whatsapp/adapter.ts +1 -4
  533. package/src/messaging/registry.ts +0 -1
  534. package/src/notifications/README.md +13 -22
  535. package/src/notifications/adapters/macos.ts +1 -1
  536. package/src/notifications/conversation-pairing.ts +2 -2
  537. package/src/notifications/copy-composer.ts +2 -2
  538. package/src/notifications/decision-engine.ts +1 -10
  539. package/src/notifications/destination-resolver.ts +2 -3
  540. package/src/notifications/emit-signal.ts +2 -8
  541. package/src/notifications/guardian-question-mode.ts +5 -8
  542. package/src/notifications/signal.ts +1 -2
  543. package/src/notifications/types.ts +1 -1
  544. package/src/oauth/token-persistence.ts +25 -1
  545. package/src/permissions/checker.ts +4 -29
  546. package/src/permissions/defaults.ts +6 -6
  547. package/src/permissions/prompter.ts +1 -1
  548. package/src/permissions/secret-prompter.ts +1 -1
  549. package/src/permissions/shell-identity.ts +1 -1
  550. package/src/permissions/trust-store.ts +13 -76
  551. package/src/permissions/workspace-policy.ts +1 -1
  552. package/src/{config → prompts}/computer-use-prompt.ts +1 -1
  553. package/src/{config → prompts}/system-prompt.ts +40 -21
  554. package/src/runtime/AGENTS.md +6 -8
  555. package/src/runtime/access-request-helper.ts +36 -55
  556. package/src/runtime/actor-trust-resolver.ts +1 -24
  557. package/src/runtime/approval-message-composer.ts +6 -2
  558. package/src/runtime/assistant-event.ts +1 -1
  559. package/src/runtime/auth/__tests__/ipc-auth-context.test.ts +1 -1
  560. package/src/runtime/auth/__tests__/subject.test.ts +32 -0
  561. package/src/runtime/auth/route-policy.ts +140 -24
  562. package/src/runtime/auth/subject.ts +9 -0
  563. package/src/runtime/auth/token-service.ts +11 -0
  564. package/src/runtime/auth/types.ts +1 -1
  565. package/src/runtime/channel-approval-types.ts +1 -1
  566. package/src/runtime/channel-approvals.ts +1 -1
  567. package/src/runtime/channel-invite-transport.ts +0 -2
  568. package/src/runtime/channel-invite-transports/slack.ts +5 -19
  569. package/src/runtime/channel-invite-transports/telegram.ts +17 -34
  570. package/src/runtime/channel-invite-transports/voice.ts +1 -1
  571. package/src/runtime/channel-readiness-service.ts +24 -159
  572. package/src/runtime/channel-readiness-types.ts +5 -1
  573. package/src/runtime/channel-reply-delivery.ts +43 -3
  574. package/src/runtime/channel-retry-sweep.ts +14 -22
  575. package/src/runtime/{channel-guardian-service.ts → channel-verification-service.ts} +50 -53
  576. package/src/runtime/confirmation-request-guardian-bridge.ts +2 -3
  577. package/src/runtime/gateway-client.ts +12 -15
  578. package/src/runtime/guardian-action-followup-executor.ts +8 -73
  579. package/src/runtime/guardian-action-grant-minter.ts +45 -61
  580. package/src/runtime/guardian-action-message-composer.ts +4 -4
  581. package/src/runtime/guardian-reply-router.ts +3 -3
  582. package/src/runtime/http-server.ts +133 -24
  583. package/src/runtime/http-types.ts +34 -1
  584. package/src/runtime/invite-instruction-generator.ts +1 -3
  585. package/src/runtime/invite-redemption-service.ts +5 -5
  586. package/src/runtime/invite-service.ts +7 -7
  587. package/src/runtime/local-actor-identity.ts +28 -2
  588. package/src/runtime/local-gateway-health.ts +275 -0
  589. package/src/runtime/middleware/twilio-validation.ts +3 -3
  590. package/src/runtime/migrations/migration-transport.ts +18 -3
  591. package/src/runtime/migrations/rebind-secrets-screen.ts +2 -2
  592. package/src/runtime/nl-approval-parser.ts +2 -3
  593. package/src/runtime/routes/access-request-decision.ts +2 -2
  594. package/src/runtime/routes/app-management-routes.ts +921 -0
  595. package/src/runtime/routes/approval-routes.ts +76 -7
  596. package/src/runtime/routes/approval-strategies/guardian-callback-strategy.ts +38 -203
  597. package/src/runtime/routes/channel-delivery-routes.ts +5 -4
  598. package/src/runtime/routes/channel-route-shared.ts +1 -3
  599. package/src/runtime/routes/channel-routes.ts +1 -4
  600. package/src/runtime/routes/channel-verification-routes.ts +257 -0
  601. package/src/runtime/routes/computer-use-routes.ts +595 -0
  602. package/src/runtime/routes/contact-routes.ts +1 -317
  603. package/src/runtime/routes/conversation-attention-routes.ts +6 -5
  604. package/src/runtime/routes/conversation-routes.ts +11 -18
  605. package/src/runtime/routes/debug-routes.ts +1 -1
  606. package/src/runtime/routes/diagnostics-routes.ts +813 -0
  607. package/src/runtime/routes/documents-routes.ts +227 -0
  608. package/src/runtime/routes/guardian-approval-interception.ts +25 -48
  609. package/src/runtime/routes/guardian-bootstrap-routes.ts +3 -3
  610. package/src/runtime/routes/guardian-expiry-sweep.ts +2 -2
  611. package/src/runtime/routes/guardian-refresh-routes.ts +11 -6
  612. package/src/runtime/routes/inbound-conversation.ts +3 -10
  613. package/src/runtime/routes/inbound-message-handler.ts +7 -6
  614. package/src/runtime/routes/inbound-stages/acl-enforcement.ts +22 -22
  615. package/src/runtime/routes/inbound-stages/background-dispatch.test.ts +44 -0
  616. package/src/runtime/routes/inbound-stages/background-dispatch.ts +140 -22
  617. package/src/runtime/routes/inbound-stages/bootstrap-intercept.ts +4 -4
  618. package/src/runtime/routes/inbound-stages/edit-intercept.ts +5 -5
  619. package/src/runtime/routes/inbound-stages/escalation-intercept.ts +3 -3
  620. package/src/runtime/routes/inbound-stages/secret-ingress-check.ts +4 -4
  621. package/src/runtime/routes/inbound-stages/verification-intercept.ts +13 -14
  622. package/src/runtime/routes/integrations/slack/channel.ts +72 -0
  623. package/src/runtime/routes/{slack-share-routes.ts → integrations/slack/share.ts} +9 -9
  624. package/src/runtime/routes/integrations/telegram.ts +111 -0
  625. package/src/runtime/routes/integrations/twilio.ts +451 -0
  626. package/src/runtime/routes/invite-routes.ts +2 -2
  627. package/src/runtime/routes/pairing-routes.ts +1 -1
  628. package/src/runtime/routes/recording-routes.ts +332 -0
  629. package/src/{daemon/handlers/config-scheduling.ts → runtime/routes/schedule-routes.ts} +91 -106
  630. package/src/runtime/routes/session-management-routes.ts +167 -0
  631. package/src/runtime/routes/session-query-routes.ts +204 -0
  632. package/src/runtime/routes/settings-routes.ts +977 -0
  633. package/src/runtime/routes/skills-routes.ts +266 -0
  634. package/src/runtime/routes/subagents-routes.ts +246 -0
  635. package/src/runtime/routes/surface-action-routes.ts +100 -10
  636. package/src/runtime/routes/surface-content-routes.ts +1 -1
  637. package/src/runtime/routes/work-items-routes.ts +809 -0
  638. package/src/runtime/routes/workspace-routes.test.ts +778 -0
  639. package/src/runtime/routes/workspace-routes.ts +410 -0
  640. package/src/runtime/routes/workspace-utils.ts +88 -0
  641. package/src/runtime/telegram-streaming-delivery.test.ts +597 -0
  642. package/src/runtime/telegram-streaming-delivery.ts +380 -0
  643. package/src/runtime/tool-grant-request-helper.ts +1 -2
  644. package/src/runtime/trust-context-resolver.ts +0 -1
  645. package/src/runtime/{guardian-outbound-actions.ts → verification-outbound-actions.ts} +23 -188
  646. package/src/runtime/verification-rate-limiter.ts +2 -2
  647. package/src/runtime/{guardian-verification-templates.ts → verification-templates.ts} +2 -28
  648. package/src/schedule/integration-status.ts +2 -2
  649. package/src/schedule/schedule-store.ts +7 -9
  650. package/src/sequence/engine.ts +1 -1
  651. package/src/skills/active-skill-tools.ts +0 -8
  652. package/src/skills/clawhub.ts +1 -10
  653. package/src/skills/managed-store.ts +14 -4
  654. package/src/skills/slash-commands.ts +1 -1
  655. package/src/subagent/manager.ts +1 -1
  656. package/src/subagent/types.ts +1 -1
  657. package/src/tasks/SPEC.md +10 -10
  658. package/src/tasks/task-scheduler.ts +1 -1
  659. package/src/telegram/bot-username.ts +13 -0
  660. package/src/tools/assets/materialize.ts +1 -1
  661. package/src/tools/assets/search.ts +1 -1
  662. package/src/tools/browser/browser-execution.ts +2 -2
  663. package/src/tools/browser/browser-manager.ts +88 -11
  664. package/src/tools/browser/browser-screencast.ts +1 -1
  665. package/src/tools/browser/headless-browser.ts +0 -17
  666. package/src/tools/browser/jit-auth.ts +1 -1
  667. package/src/tools/browser/recording-store.ts +19 -1
  668. package/src/tools/browser/runtime-check.ts +4 -2
  669. package/src/tools/calls/call-start.ts +3 -3
  670. package/src/tools/credentials/metadata-store.ts +0 -13
  671. package/src/tools/credentials/vault.ts +7 -31
  672. package/src/tools/followups/followup_create.ts +0 -8
  673. package/src/tools/mcp/mcp-tool-factory.ts +1 -1
  674. package/src/tools/memory/definitions.ts +32 -10
  675. package/src/tools/memory/handlers.test.ts +573 -0
  676. package/src/tools/memory/handlers.ts +222 -65
  677. package/src/tools/memory/register.ts +53 -24
  678. package/src/tools/network/script-proxy/session-manager.ts +1 -12
  679. package/src/tools/schedule/update.ts +0 -8
  680. package/src/tools/skills/load.ts +3 -3
  681. package/src/tools/subagent/read.ts +1 -1
  682. package/src/tools/system/voice-config.ts +2 -14
  683. package/src/tools/terminal/safe-env.ts +5 -18
  684. package/src/tools/tool-approval-handler.ts +4 -4
  685. package/src/tools/tool-manifest.ts +4 -2
  686. package/src/tools/types.ts +1 -1
  687. package/src/tools/{guardian-control-plane-policy.ts → verification-control-plane-policy.ts} +37 -39
  688. package/src/twitter/platform-proxy-client.ts +405 -0
  689. package/src/usage/types.ts +21 -0
  690. package/src/util/canonicalize-identity.ts +2 -6
  691. package/src/util/cookie-session.ts +35 -51
  692. package/src/util/platform.ts +93 -86
  693. package/src/util/pricing.ts +180 -43
  694. package/src/work-items/work-item-runner.ts +1 -1
  695. package/scripts/ipc/check-contract-inventory.ts +0 -107
  696. package/scripts/ipc/check-swift-decoder-drift.ts +0 -184
  697. package/scripts/ipc/generate-swift.ts +0 -528
  698. package/src/__tests__/__snapshots__/ipc-snapshot.test.ts.snap +0 -3043
  699. package/src/__tests__/app-migration.test.ts +0 -148
  700. package/src/__tests__/config-loader-migration.test.ts +0 -85
  701. package/src/__tests__/daemon-lifecycle.test.ts +0 -715
  702. package/src/__tests__/daemon-server-session-init.test.ts +0 -864
  703. package/src/__tests__/guardian-actions-endpoint.test.ts +0 -1452
  704. package/src/__tests__/handlers-add-trust-rule-metadata.test.ts +0 -228
  705. package/src/__tests__/handlers-cu-observation-blob.test.ts +0 -397
  706. package/src/__tests__/handlers-ipc-blob-probe.test.ts +0 -218
  707. package/src/__tests__/handlers-slack-config.test.ts +0 -140
  708. package/src/__tests__/handlers-telegram-config.test.ts +0 -1317
  709. package/src/__tests__/handlers-twitter-config.test.ts +0 -1145
  710. package/src/__tests__/ingress-reconcile.test.ts +0 -606
  711. package/src/__tests__/integrations-cli.test.ts +0 -232
  712. package/src/__tests__/ipc-blob-store.test.ts +0 -329
  713. package/src/__tests__/ipc-contract-inventory.test.ts +0 -69
  714. package/src/__tests__/ipc-contract.test.ts +0 -76
  715. package/src/__tests__/ipc-protocol.test.ts +0 -120
  716. package/src/__tests__/ipc-roundtrip.benchmark.test.ts +0 -250
  717. package/src/__tests__/ipc-snapshot.test.ts +0 -2197
  718. package/src/__tests__/ipc-validate.test.ts +0 -471
  719. package/src/__tests__/migration-cli-flows.test.ts +0 -186
  720. package/src/__tests__/migration-ordering.test.ts +0 -267
  721. package/src/__tests__/oauth-connect-handler.test.ts +0 -361
  722. package/src/__tests__/platform-move-helper.test.ts +0 -108
  723. package/src/__tests__/platform-socket-path.test.ts +0 -52
  724. package/src/__tests__/platform-workspace-migration.test.ts +0 -1051
  725. package/src/__tests__/recording-intent-handler.test.ts +0 -1155
  726. package/src/__tests__/script-proxy-profile-template-fallback.test.ts +0 -127
  727. package/src/__tests__/sms-messaging-provider.test.ts +0 -156
  728. package/src/__tests__/tool-permission-simulate-handler.test.ts +0 -367
  729. package/src/__tests__/twitter-auth-handler.test.ts +0 -561
  730. package/src/__tests__/work-item-output.test.ts +0 -150
  731. package/src/amazon/session.ts +0 -58
  732. package/src/cli/channels.ts +0 -51
  733. package/src/cli/influencer.ts +0 -319
  734. package/src/cli/integrations.ts +0 -372
  735. package/src/cli/ipc-client.ts +0 -88
  736. package/src/config/bundled-skills/configure-settings/SKILL.md +0 -86
  737. package/src/config/bundled-skills/doordash/lib/shared/ipc.ts +0 -32
  738. package/src/config/bundled-skills/sms-setup/SKILL.md +0 -210
  739. package/src/config/core-schema.ts +0 -434
  740. package/src/config/memory-schema.ts +0 -617
  741. package/src/daemon/auth-manager.ts +0 -106
  742. package/src/daemon/handlers/apps.ts +0 -783
  743. package/src/daemon/handlers/avatar.ts +0 -73
  744. package/src/daemon/handlers/browser.ts +0 -3
  745. package/src/daemon/handlers/computer-use.ts +0 -231
  746. package/src/daemon/handlers/config-dispatch.ts +0 -29
  747. package/src/daemon/handlers/config-heartbeat.ts +0 -299
  748. package/src/daemon/handlers/config-inbox.ts +0 -457
  749. package/src/daemon/handlers/config-integrations.ts +0 -409
  750. package/src/daemon/handlers/config-platform.ts +0 -77
  751. package/src/daemon/handlers/config-slack.ts +0 -41
  752. package/src/daemon/handlers/config-tools.ts +0 -226
  753. package/src/daemon/handlers/config-trust.ts +0 -135
  754. package/src/daemon/handlers/config.ts +0 -64
  755. package/src/daemon/handlers/contacts.ts +0 -193
  756. package/src/daemon/handlers/diagnostics.ts +0 -382
  757. package/src/daemon/handlers/documents.ts +0 -188
  758. package/src/daemon/handlers/guardian-actions.ts +0 -82
  759. package/src/daemon/handlers/home-base.ts +0 -82
  760. package/src/daemon/handlers/index.ts +0 -222
  761. package/src/daemon/handlers/misc.ts +0 -1139
  762. package/src/daemon/handlers/navigate-settings.ts +0 -29
  763. package/src/daemon/handlers/oauth-connect.ts +0 -202
  764. package/src/daemon/handlers/open-bundle-handler.ts +0 -88
  765. package/src/daemon/handlers/publish.ts +0 -176
  766. package/src/daemon/handlers/signing.ts +0 -56
  767. package/src/daemon/handlers/subagents.ts +0 -286
  768. package/src/daemon/handlers/twitter-auth.ts +0 -220
  769. package/src/daemon/handlers/work-items.ts +0 -796
  770. package/src/daemon/handlers/workspace-files.ts +0 -84
  771. package/src/daemon/handlers.ts +0 -16
  772. package/src/daemon/ipc-blob-store.ts +0 -246
  773. package/src/daemon/ipc-contract-inventory.json +0 -348
  774. package/src/daemon/ipc-contract-inventory.ts +0 -202
  775. package/src/daemon/ipc-handler.ts +0 -120
  776. package/src/daemon/ipc-protocol.ts +0 -85
  777. package/src/daemon/ipc-validate.ts +0 -254
  778. package/src/memory/app-migration.ts +0 -114
  779. package/src/memory/channel-delivery-store.ts +0 -40
  780. package/src/memory/channel-guardian-store.ts +0 -83
  781. package/src/memory/conversation-store.ts +0 -102
  782. package/src/memory/schema-migration.ts +0 -38
  783. package/src/messaging/providers/sms/adapter.ts +0 -232
  784. package/src/messaging/providers/sms/client.ts +0 -93
  785. package/src/messaging/providers/sms/types.ts +0 -7
  786. package/src/migrations/config-merge.ts +0 -62
  787. package/src/migrations/data-layout.ts +0 -89
  788. package/src/migrations/data-merge.ts +0 -44
  789. package/src/migrations/hooks-merge.ts +0 -118
  790. package/src/migrations/index.ts +0 -6
  791. package/src/migrations/log.ts +0 -28
  792. package/src/migrations/skills-merge.ts +0 -44
  793. package/src/migrations/workspace-layout.ts +0 -94
  794. package/src/notifications/adapters/sms.ts +0 -94
  795. package/src/runtime/channel-approval-parser.ts +0 -123
  796. package/src/runtime/channel-invite-transports/sms.ts +0 -53
  797. package/src/runtime/routes/approval-strategies/guardian-legacy-fallback-strategy.ts +0 -82
  798. package/src/runtime/routes/integration-routes.ts +0 -381
  799. package/src/runtime/routes/twilio-routes.ts +0 -1251
  800. package/src/twitter/router.ts +0 -131
  801. package/src/twitter/session.ts +0 -54
  802. package/src/watcher/providers/slack.ts +0 -282
  803. /package/src/{amazon → cli/commands/amazon}/cart.ts +0 -0
  804. /package/src/{amazon → cli/commands/amazon}/checkout.ts +0 -0
  805. /package/src/{amazon → cli/commands/amazon}/product-details.ts +0 -0
  806. /package/src/{amazon → cli/commands/amazon}/search.ts +0 -0
  807. /package/src/{twitter → cli/commands/twitter}/oauth-client.ts +0 -0
  808. /package/src/config/{calls-schema.ts → schemas/calls.ts} +0 -0
  809. /package/src/config/{elevenlabs-schema.ts → schemas/elevenlabs.ts} +0 -0
  810. /package/src/config/{mcp-schema.ts → schemas/mcp.ts} +0 -0
  811. /package/src/config/{notifications-schema.ts → schemas/notifications.ts} +0 -0
  812. /package/src/config/{sandbox-schema.ts → schemas/sandbox.ts} +0 -0
  813. /package/src/config/{skills-schema.ts → schemas/skills.ts} +0 -0
  814. /package/src/daemon/{ipc-contract → message-types}/apps.ts +0 -0
  815. /package/src/daemon/{ipc-contract → message-types}/browser.ts +0 -0
  816. /package/src/daemon/{ipc-contract → message-types}/contacts.ts +0 -0
  817. /package/src/daemon/{ipc-contract → message-types}/documents.ts +0 -0
  818. /package/src/daemon/{ipc-contract → message-types}/guardian-actions.ts +0 -0
  819. /package/src/daemon/{ipc-contract → message-types}/inbox.ts +0 -0
  820. /package/src/daemon/{ipc-contract → message-types}/messages.ts +0 -0
  821. /package/src/daemon/{ipc-contract → message-types}/pairing.ts +0 -0
  822. /package/src/daemon/{ipc-contract → message-types}/schedules.ts +0 -0
  823. /package/src/daemon/{ipc-contract → message-types}/settings.ts +0 -0
  824. /package/src/daemon/{ipc-contract → message-types}/skills.ts +0 -0
  825. /package/src/daemon/{ipc-contract → message-types}/subagents.ts +0 -0
  826. /package/src/daemon/{ipc-contract → message-types}/surfaces.ts +0 -0
  827. /package/src/daemon/{ipc-contract → message-types}/trust.ts +0 -0
  828. /package/src/daemon/{ipc-contract → message-types}/work-items.ts +0 -0
  829. /package/src/{cli/email-guardrails.ts → email/guardrails.ts} +0 -0
  830. /package/src/{config → prompts}/__tests__/build-cli-reference-section.test.ts +0 -0
  831. /package/src/{config → prompts}/templates/BOOTSTRAP.md +0 -0
  832. /package/src/{config → prompts}/templates/IDENTITY.md +0 -0
  833. /package/src/{config → prompts}/templates/SOUL.md +0 -0
  834. /package/src/{config → prompts}/templates/UPDATES.md +0 -0
  835. /package/src/{config → prompts}/templates/USER.md +0 -0
  836. /package/src/{config → prompts}/update-bulletin-format.ts +0 -0
  837. /package/src/{config → prompts}/update-bulletin-state.ts +0 -0
  838. /package/src/{config → prompts}/update-bulletin-template-path.ts +0 -0
  839. /package/src/{config → prompts}/update-bulletin.ts +0 -0
  840. /package/src/{config → prompts}/user-reference.ts +0 -0
@@ -2,7 +2,6 @@ import type { DrizzleDb } from "../db-connection.js";
2
2
  import { migrateNotificationTablesSchema } from "./019-notification-tables-schema-migration.js";
3
3
  import { migrateNotificationDeliveryPairingColumns } from "./027-notification-delivery-pairing-columns.js";
4
4
  import { migrateNotificationDeliveryClientAck } from "./028-notification-delivery-client-ack.js";
5
- import { tableHasColumn } from "./schema-introspection.js";
6
5
 
7
6
  /**
8
7
  * Notification system tables: preferences, events, decisions, and deliveries.
@@ -16,7 +15,6 @@ export function createNotificationTables(database: DrizzleDb): void {
16
15
  database.run(/*sql*/ `
17
16
  CREATE TABLE IF NOT EXISTS notification_preferences (
18
17
  id TEXT PRIMARY KEY,
19
- assistant_id TEXT NOT NULL,
20
18
  preference_text TEXT NOT NULL,
21
19
  applies_when_json TEXT NOT NULL DEFAULT '{}',
22
20
  priority INTEGER NOT NULL DEFAULT 0,
@@ -25,23 +23,13 @@ export function createNotificationTables(database: DrizzleDb): void {
25
23
  )
26
24
  `);
27
25
 
28
- if (tableHasColumn(database, "notification_preferences", "assistant_id")) {
29
- database.run(
30
- /*sql*/ `CREATE INDEX IF NOT EXISTS idx_notification_preferences_assistant_id ON notification_preferences(assistant_id)`,
31
- );
32
- database.run(
33
- /*sql*/ `CREATE INDEX IF NOT EXISTS idx_notification_preferences_assistant_priority ON notification_preferences(assistant_id, priority DESC)`,
34
- );
35
- } else {
36
- database.run(
37
- /*sql*/ `CREATE INDEX IF NOT EXISTS idx_notification_preferences_priority ON notification_preferences(priority DESC)`,
38
- );
39
- }
26
+ database.run(
27
+ /*sql*/ `CREATE INDEX IF NOT EXISTS idx_notification_preferences_priority ON notification_preferences(priority DESC)`,
28
+ );
40
29
 
41
30
  database.run(/*sql*/ `
42
31
  CREATE TABLE IF NOT EXISTS notification_events (
43
32
  id TEXT PRIMARY KEY,
44
- assistant_id TEXT NOT NULL,
45
33
  source_event_name TEXT NOT NULL,
46
34
  source_channel TEXT NOT NULL,
47
35
  source_session_id TEXT NOT NULL,
@@ -53,21 +41,12 @@ export function createNotificationTables(database: DrizzleDb): void {
53
41
  )
54
42
  `);
55
43
 
56
- if (tableHasColumn(database, "notification_events", "assistant_id")) {
57
- database.run(
58
- /*sql*/ `CREATE INDEX IF NOT EXISTS idx_notification_events_assistant_event_created ON notification_events(assistant_id, source_event_name, created_at)`,
59
- );
60
- database.run(
61
- /*sql*/ `CREATE UNIQUE INDEX IF NOT EXISTS idx_notification_events_dedupe ON notification_events(assistant_id, dedupe_key) WHERE dedupe_key IS NOT NULL`,
62
- );
63
- } else {
64
- database.run(
65
- /*sql*/ `CREATE INDEX IF NOT EXISTS idx_notification_events_event_created ON notification_events(source_event_name, created_at)`,
66
- );
67
- database.run(
68
- /*sql*/ `CREATE UNIQUE INDEX IF NOT EXISTS idx_notification_events_dedupe ON notification_events(dedupe_key) WHERE dedupe_key IS NOT NULL`,
69
- );
70
- }
44
+ database.run(
45
+ /*sql*/ `CREATE INDEX IF NOT EXISTS idx_notification_events_event_created ON notification_events(source_event_name, created_at)`,
46
+ );
47
+ database.run(
48
+ /*sql*/ `CREATE UNIQUE INDEX IF NOT EXISTS idx_notification_events_dedupe ON notification_events(dedupe_key) WHERE dedupe_key IS NOT NULL`,
49
+ );
71
50
 
72
51
  database.run(/*sql*/ `
73
52
  CREATE TABLE IF NOT EXISTS notification_decisions (
@@ -92,7 +71,6 @@ export function createNotificationTables(database: DrizzleDb): void {
92
71
  CREATE TABLE IF NOT EXISTS notification_deliveries (
93
72
  id TEXT PRIMARY KEY,
94
73
  notification_decision_id TEXT NOT NULL REFERENCES notification_decisions(id) ON DELETE CASCADE,
95
- assistant_id TEXT NOT NULL,
96
74
  channel TEXT NOT NULL,
97
75
  destination TEXT NOT NULL,
98
76
  status TEXT NOT NULL DEFAULT 'pending',
@@ -113,15 +91,9 @@ export function createNotificationTables(database: DrizzleDb): void {
113
91
  database.run(
114
92
  /*sql*/ `CREATE INDEX IF NOT EXISTS idx_notification_deliveries_decision_id ON notification_deliveries(notification_decision_id)`,
115
93
  );
116
- if (tableHasColumn(database, "notification_deliveries", "assistant_id")) {
117
- database.run(
118
- /*sql*/ `CREATE INDEX IF NOT EXISTS idx_notification_deliveries_assistant_status ON notification_deliveries(assistant_id, status)`,
119
- );
120
- } else {
121
- database.run(
122
- /*sql*/ `CREATE INDEX IF NOT EXISTS idx_notification_deliveries_status ON notification_deliveries(status)`,
123
- );
124
- }
94
+ database.run(
95
+ /*sql*/ `CREATE INDEX IF NOT EXISTS idx_notification_deliveries_status ON notification_deliveries(status)`,
96
+ );
125
97
 
126
98
  // Add conversation pairing audit columns (idempotent ALTER TABLE)
127
99
  migrateNotificationDeliveryPairingColumns(database);
@@ -0,0 +1,357 @@
1
+ import { getConfig } from "../../config/loader.js";
2
+ import type {
3
+ AnthropicCacheCreationTokenDetails,
4
+ PricingUsage,
5
+ } from "../../usage/types.js";
6
+ import { getLogger } from "../../util/logger.js";
7
+ import { resolvePricingForUsageWithOverrides } from "../../util/pricing.js";
8
+ import { type DrizzleDb, getSqliteFrom } from "../db-connection.js";
9
+ import { withCrashRecovery } from "./validate-migration-state.js";
10
+
11
+ const log = getLogger("memory-db");
12
+
13
+ const CHECKPOINT_KEY = "migration_backfill_usage_cache_accounting_v1";
14
+
15
+ interface UsageEventRow {
16
+ id: string;
17
+ conversation_id: string | null;
18
+ created_at: number;
19
+ provider: string;
20
+ model: string;
21
+ input_tokens: number;
22
+ output_tokens: number;
23
+ cache_creation_input_tokens: number | null;
24
+ cache_read_input_tokens: number | null;
25
+ }
26
+
27
+ interface RequestLogRow {
28
+ id: string;
29
+ conversation_id: string;
30
+ response_payload: string;
31
+ created_at: number;
32
+ }
33
+
34
+ interface ReconstructedUsage extends PricingUsage {
35
+ totalInputTokens: number;
36
+ }
37
+
38
+ function asRecord(value: unknown): Record<string, unknown> | null {
39
+ if (typeof value !== "object" || value == null) return null;
40
+ return value as Record<string, unknown>;
41
+ }
42
+
43
+ function parseRequiredTokenCount(value: unknown): number | null {
44
+ if (typeof value !== "number" || !Number.isFinite(value)) return null;
45
+ if (value < 0) return null;
46
+ return value;
47
+ }
48
+
49
+ function parseOptionalTokenCount(value: unknown): number {
50
+ if (typeof value !== "number" || !Number.isFinite(value)) return 0;
51
+ return Math.max(value, 0);
52
+ }
53
+
54
+ function parseResponseUsage(
55
+ responsePayload: string,
56
+ ): ReconstructedUsage | null {
57
+ let parsed: unknown;
58
+ try {
59
+ parsed = JSON.parse(responsePayload);
60
+ } catch {
61
+ return null;
62
+ }
63
+
64
+ const payloads = Array.isArray(parsed) ? parsed : [parsed];
65
+ if (payloads.length === 0) return null;
66
+
67
+ let directInputTokens = 0;
68
+ let outputTokens = 0;
69
+ let cacheReadInputTokens = 0;
70
+ let ephemeral5mInputTokens = 0;
71
+ let ephemeral1hInputTokens = 0;
72
+
73
+ for (const payload of payloads) {
74
+ const response = asRecord(payload);
75
+ const usage = asRecord(response?.usage);
76
+ if (!usage) return null;
77
+
78
+ const inputTokens = parseRequiredTokenCount(usage.input_tokens);
79
+ const responseOutputTokens = parseRequiredTokenCount(usage.output_tokens);
80
+ if (inputTokens == null || responseOutputTokens == null) {
81
+ return null;
82
+ }
83
+
84
+ const cacheReadTokens = parseOptionalTokenCount(
85
+ usage.cache_read_input_tokens,
86
+ );
87
+ const cacheCreation = asRecord(usage.cache_creation);
88
+
89
+ directInputTokens += inputTokens;
90
+ outputTokens += responseOutputTokens;
91
+ cacheReadInputTokens += cacheReadTokens;
92
+ ephemeral5mInputTokens += parseOptionalTokenCount(
93
+ cacheCreation?.ephemeral_5m_input_tokens,
94
+ );
95
+ ephemeral1hInputTokens += parseOptionalTokenCount(
96
+ cacheCreation?.ephemeral_1h_input_tokens,
97
+ );
98
+ }
99
+
100
+ const cacheCreationInputTokens =
101
+ ephemeral5mInputTokens + ephemeral1hInputTokens;
102
+ const anthropicCacheCreation: AnthropicCacheCreationTokenDetails | null =
103
+ cacheCreationInputTokens > 0
104
+ ? {
105
+ ephemeral_5m_input_tokens: ephemeral5mInputTokens,
106
+ ephemeral_1h_input_tokens: ephemeral1hInputTokens,
107
+ }
108
+ : null;
109
+
110
+ return {
111
+ directInputTokens,
112
+ outputTokens,
113
+ cacheCreationInputTokens,
114
+ cacheReadInputTokens,
115
+ anthropicCacheCreation,
116
+ totalInputTokens:
117
+ directInputTokens + cacheCreationInputTokens + cacheReadInputTokens,
118
+ };
119
+ }
120
+
121
+ function buildRequestLogMap(
122
+ rows: RequestLogRow[],
123
+ ): Map<string, RequestLogRow[]> {
124
+ const requestLogsByConversation = new Map<string, RequestLogRow[]>();
125
+ for (const row of rows) {
126
+ const conversationRows =
127
+ requestLogsByConversation.get(row.conversation_id) ?? [];
128
+ conversationRows.push(row);
129
+ requestLogsByConversation.set(row.conversation_id, conversationRows);
130
+ }
131
+ return requestLogsByConversation;
132
+ }
133
+
134
+ export function migrateBackfillUsageCacheAccounting(database: DrizzleDb): void {
135
+ withCrashRecovery(database, CHECKPOINT_KEY, () => {
136
+ const raw = getSqliteFrom(database);
137
+
138
+ const usageEventsTableExists = raw
139
+ .query(
140
+ `SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'llm_usage_events'`,
141
+ )
142
+ .get();
143
+ const requestLogsTableExists = raw
144
+ .query(
145
+ `SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'llm_request_logs'`,
146
+ )
147
+ .get();
148
+ if (!usageEventsTableExists || !requestLogsTableExists) return;
149
+
150
+ const usageRows = raw
151
+ .query(
152
+ /*sql*/ `
153
+ SELECT
154
+ id,
155
+ conversation_id,
156
+ created_at,
157
+ provider,
158
+ model,
159
+ input_tokens,
160
+ output_tokens,
161
+ cache_creation_input_tokens,
162
+ cache_read_input_tokens
163
+ FROM llm_usage_events
164
+ ORDER BY conversation_id ASC, created_at ASC, id ASC
165
+ `,
166
+ )
167
+ .all() as UsageEventRow[];
168
+
169
+ const requestLogRows = raw
170
+ .query(
171
+ /*sql*/ `
172
+ SELECT
173
+ id,
174
+ conversation_id,
175
+ response_payload,
176
+ created_at
177
+ FROM llm_request_logs
178
+ ORDER BY conversation_id ASC, created_at ASC, id ASC
179
+ `,
180
+ )
181
+ .all() as RequestLogRow[];
182
+
183
+ const requestLogsByConversation = buildRequestLogMap(requestLogRows);
184
+ const requestOffsets = new Map<string, number>();
185
+ const previousUsageEventCreatedAt = new Map<string, number>();
186
+ const pricingOverrides = getConfig().pricingOverrides;
187
+
188
+ let scannedAnthropicRows = 0;
189
+ let updatedRows = 0;
190
+ let skippedNoConversation = 0;
191
+ let skippedNoLogs = 0;
192
+ let ignoredUnusableLogs = 0;
193
+ let skippedInconsistentRows = 0;
194
+
195
+ const updateRow = raw.prepare(/*sql*/ `
196
+ UPDATE llm_usage_events
197
+ SET
198
+ input_tokens = ?,
199
+ output_tokens = ?,
200
+ cache_creation_input_tokens = ?,
201
+ cache_read_input_tokens = ?,
202
+ estimated_cost_usd = ?,
203
+ pricing_status = ?
204
+ WHERE id = ?
205
+ `);
206
+
207
+ try {
208
+ raw.exec("BEGIN");
209
+
210
+ for (const usageRow of usageRows) {
211
+ const conversationId = usageRow.conversation_id;
212
+ if (conversationId == null) {
213
+ if (usageRow.provider === "anthropic") {
214
+ scannedAnthropicRows += 1;
215
+ skippedNoConversation += 1;
216
+ }
217
+ continue;
218
+ }
219
+
220
+ const previousCreatedAt =
221
+ previousUsageEventCreatedAt.get(conversationId) ?? null;
222
+ const conversationRequestLogs =
223
+ requestLogsByConversation.get(conversationId) ?? [];
224
+ let requestOffset = requestOffsets.get(conversationId) ?? 0;
225
+ const windowLogs: RequestLogRow[] = [];
226
+
227
+ while (
228
+ requestOffset < conversationRequestLogs.length &&
229
+ conversationRequestLogs[requestOffset]!.created_at <=
230
+ usageRow.created_at
231
+ ) {
232
+ const requestLog = conversationRequestLogs[requestOffset]!;
233
+ if (
234
+ previousCreatedAt == null ||
235
+ requestLog.created_at > previousCreatedAt
236
+ ) {
237
+ windowLogs.push(requestLog);
238
+ }
239
+ requestOffset += 1;
240
+ }
241
+
242
+ requestOffsets.set(conversationId, requestOffset);
243
+ previousUsageEventCreatedAt.set(conversationId, usageRow.created_at);
244
+
245
+ if (usageRow.provider !== "anthropic") continue;
246
+
247
+ scannedAnthropicRows += 1;
248
+ if (windowLogs.length === 0) {
249
+ skippedNoLogs += 1;
250
+ continue;
251
+ }
252
+
253
+ let directInputTokens = 0;
254
+ let outputTokens = 0;
255
+ let cacheReadInputTokens = 0;
256
+ let cacheCreationInputTokens = 0;
257
+ let cacheCreation5mInputTokens = 0;
258
+ let cacheCreation1hInputTokens = 0;
259
+ let usableLogCount = 0;
260
+
261
+ for (const requestLog of windowLogs) {
262
+ const reconstructedUsage = parseResponseUsage(
263
+ requestLog.response_payload,
264
+ );
265
+ if (!reconstructedUsage) {
266
+ ignoredUnusableLogs += 1;
267
+ continue;
268
+ }
269
+ usableLogCount += 1;
270
+ directInputTokens += reconstructedUsage.directInputTokens;
271
+ outputTokens += reconstructedUsage.outputTokens;
272
+ cacheReadInputTokens += reconstructedUsage.cacheReadInputTokens;
273
+ cacheCreationInputTokens +=
274
+ reconstructedUsage.cacheCreationInputTokens;
275
+ cacheCreation5mInputTokens +=
276
+ reconstructedUsage.anthropicCacheCreation
277
+ ?.ephemeral_5m_input_tokens ?? 0;
278
+ cacheCreation1hInputTokens +=
279
+ reconstructedUsage.anthropicCacheCreation
280
+ ?.ephemeral_1h_input_tokens ?? 0;
281
+ }
282
+
283
+ if (usableLogCount === 0) {
284
+ skippedNoLogs += 1;
285
+ continue;
286
+ }
287
+
288
+ const totalInputTokens =
289
+ directInputTokens + cacheCreationInputTokens + cacheReadInputTokens;
290
+ const existingTotalInputTokens =
291
+ Math.max(usageRow.input_tokens, 0) +
292
+ Math.max(usageRow.cache_creation_input_tokens ?? 0, 0) +
293
+ Math.max(usageRow.cache_read_input_tokens ?? 0, 0);
294
+
295
+ // This lets the migration safely rewrite both flattened historical rows
296
+ // and already-correct rows, while skipping mismatched request-log windows.
297
+ if (
298
+ totalInputTokens < 0 ||
299
+ directInputTokens < 0 ||
300
+ totalInputTokens !== existingTotalInputTokens ||
301
+ outputTokens !== Math.max(usageRow.output_tokens, 0)
302
+ ) {
303
+ skippedInconsistentRows += 1;
304
+ continue;
305
+ }
306
+
307
+ const pricingUsage: PricingUsage = {
308
+ directInputTokens,
309
+ outputTokens,
310
+ cacheCreationInputTokens,
311
+ cacheReadInputTokens,
312
+ anthropicCacheCreation:
313
+ cacheCreationInputTokens > 0
314
+ ? {
315
+ ephemeral_5m_input_tokens: cacheCreation5mInputTokens,
316
+ ephemeral_1h_input_tokens: cacheCreation1hInputTokens,
317
+ }
318
+ : null,
319
+ };
320
+ const pricing = resolvePricingForUsageWithOverrides(
321
+ usageRow.provider,
322
+ usageRow.model,
323
+ pricingUsage,
324
+ pricingOverrides,
325
+ );
326
+
327
+ updateRow.run(
328
+ directInputTokens,
329
+ outputTokens,
330
+ cacheCreationInputTokens,
331
+ cacheReadInputTokens,
332
+ pricing.estimatedCostUsd,
333
+ pricing.pricingStatus,
334
+ usageRow.id,
335
+ );
336
+ updatedRows += 1;
337
+ }
338
+
339
+ raw.exec("COMMIT");
340
+ } catch (err) {
341
+ raw.exec("ROLLBACK");
342
+ throw err;
343
+ }
344
+
345
+ log.info(
346
+ {
347
+ scannedAnthropicRows,
348
+ updatedRows,
349
+ skippedNoConversation,
350
+ skippedNoLogs,
351
+ ignoredUnusableLogs,
352
+ skippedInconsistentRows,
353
+ },
354
+ "Backfilled historical Anthropic usage rows from request-log accounting",
355
+ );
356
+ });
357
+ }
@@ -0,0 +1,55 @@
1
+ import { type DrizzleDb, getSqliteFrom } from "../db-connection.js";
2
+ import { withCrashRecovery } from "./validate-migration-state.js";
3
+
4
+ /**
5
+ * One-shot migration: rename channel_guardian_verification_challenges →
6
+ * channel_verification_sessions, including all indexes that reference the
7
+ * old table name.
8
+ */
9
+ export function migrateRenameVerificationTable(database: DrizzleDb): void {
10
+ withCrashRecovery(database, "migration_rename_verification_table_v1", () => {
11
+ const raw = getSqliteFrom(database);
12
+
13
+ // Check the old table exists before attempting anything
14
+ const oldTableExists = raw
15
+ .query(
16
+ `SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'channel_guardian_verification_challenges'`,
17
+ )
18
+ .get();
19
+ if (!oldTableExists) return;
20
+
21
+ // Rename the physical table
22
+ raw.exec(
23
+ /*sql*/ `ALTER TABLE channel_guardian_verification_challenges RENAME TO channel_verification_sessions`,
24
+ );
25
+
26
+ // Drop and recreate indexes that referenced the old table name.
27
+ // SQLite automatically updates index table references on RENAME, but the
28
+ // index names still reference the old naming convention — drop and recreate
29
+ // with consistent names pointing at the new table.
30
+
31
+ raw.exec(
32
+ /*sql*/ `DROP INDEX IF EXISTS idx_channel_guardian_challenges_lookup`,
33
+ );
34
+ raw.exec(/*sql*/ `DROP INDEX IF EXISTS idx_guardian_sessions_active`);
35
+ raw.exec(/*sql*/ `DROP INDEX IF EXISTS idx_guardian_sessions_identity`);
36
+ raw.exec(/*sql*/ `DROP INDEX IF EXISTS idx_guardian_sessions_destination`);
37
+ raw.exec(/*sql*/ `DROP INDEX IF EXISTS idx_guardian_sessions_bootstrap`);
38
+
39
+ raw.exec(
40
+ /*sql*/ `CREATE INDEX IF NOT EXISTS idx_verification_sessions_lookup ON channel_verification_sessions(channel, challenge_hash, status)`,
41
+ );
42
+ raw.exec(
43
+ /*sql*/ `CREATE INDEX IF NOT EXISTS idx_verification_sessions_active ON channel_verification_sessions(channel, status)`,
44
+ );
45
+ raw.exec(
46
+ /*sql*/ `CREATE INDEX IF NOT EXISTS idx_verification_sessions_identity ON channel_verification_sessions(channel, expected_external_user_id, expected_chat_id, status)`,
47
+ );
48
+ raw.exec(
49
+ /*sql*/ `CREATE INDEX IF NOT EXISTS idx_verification_sessions_destination ON channel_verification_sessions(channel, destination_address)`,
50
+ );
51
+ raw.exec(
52
+ /*sql*/ `CREATE INDEX IF NOT EXISTS idx_verification_sessions_bootstrap ON channel_verification_sessions(channel, bootstrap_token_hash, status)`,
53
+ );
54
+ });
55
+ }
@@ -0,0 +1,32 @@
1
+ import { type DrizzleDb, getSqliteFrom } from "../db-connection.js";
2
+ import { withCrashRecovery } from "./validate-migration-state.js";
3
+
4
+ /**
5
+ * One-shot migration: rename the guardian_verification_session_id column
6
+ * in call_sessions to verification_session_id, dropping the "guardian_"
7
+ * prefix to align with the broader verification vocabulary.
8
+ */
9
+ export function migrateRenameVerificationSessionIdColumn(
10
+ database: DrizzleDb,
11
+ ): void {
12
+ withCrashRecovery(
13
+ database,
14
+ "migration_rename_verification_session_id_column_v1",
15
+ () => {
16
+ const raw = getSqliteFrom(database);
17
+
18
+ // Check the old column exists before attempting the rename
19
+ const columns = raw
20
+ .query(`PRAGMA table_info(call_sessions)`)
21
+ .all() as Array<{ name: string }>;
22
+ const hasOldColumn = columns.some(
23
+ (c) => c.name === "guardian_verification_session_id",
24
+ );
25
+ if (!hasOldColumn) return;
26
+
27
+ raw.exec(
28
+ /*sql*/ `ALTER TABLE call_sessions RENAME COLUMN guardian_verification_session_id TO verification_session_id`,
29
+ );
30
+ },
31
+ );
32
+ }
@@ -0,0 +1,48 @@
1
+ import { type DrizzleDb, getSqliteFrom } from "../db-connection.js";
2
+ import { withCrashRecovery } from "./validate-migration-state.js";
3
+
4
+ /**
5
+ * One-shot migration: rename persisted `guardian_verification` and
6
+ * `guardian_voice_verification_*` / `outbound_guardian_voice_verification_*`
7
+ * values in the call_sessions and call_events tables to drop the "guardian_"
8
+ * prefix, aligning with the broader verification vocabulary.
9
+ *
10
+ * - call_sessions.call_mode: "guardian_verification" -> "verification"
11
+ * - call_events.event_type: six guardian_voice_verification_* values renamed
12
+ */
13
+ export function migrateRenameGuardianVerificationValues(
14
+ database: DrizzleDb,
15
+ ): void {
16
+ withCrashRecovery(
17
+ database,
18
+ "migration_rename_guardian_verification_values_v1",
19
+ () => {
20
+ const raw = getSqliteFrom(database);
21
+
22
+ // Rename call_mode values
23
+ raw.exec(
24
+ /*sql*/ `UPDATE call_sessions SET call_mode = 'verification' WHERE call_mode = 'guardian_verification'`,
25
+ );
26
+
27
+ // Rename event_type values
28
+ raw.exec(
29
+ /*sql*/ `UPDATE call_events SET event_type = 'voice_verification_started' WHERE event_type = 'guardian_voice_verification_started'`,
30
+ );
31
+ raw.exec(
32
+ /*sql*/ `UPDATE call_events SET event_type = 'voice_verification_succeeded' WHERE event_type = 'guardian_voice_verification_succeeded'`,
33
+ );
34
+ raw.exec(
35
+ /*sql*/ `UPDATE call_events SET event_type = 'voice_verification_failed' WHERE event_type = 'guardian_voice_verification_failed'`,
36
+ );
37
+ raw.exec(
38
+ /*sql*/ `UPDATE call_events SET event_type = 'outbound_voice_verification_started' WHERE event_type = 'outbound_guardian_voice_verification_started'`,
39
+ );
40
+ raw.exec(
41
+ /*sql*/ `UPDATE call_events SET event_type = 'outbound_voice_verification_succeeded' WHERE event_type = 'outbound_guardian_voice_verification_succeeded'`,
42
+ );
43
+ raw.exec(
44
+ /*sql*/ `UPDATE call_events SET event_type = 'outbound_voice_verification_failed' WHERE event_type = 'outbound_guardian_voice_verification_failed'`,
45
+ );
46
+ },
47
+ );
48
+ }