@vellumai/assistant 0.12.4 → 0.12.5-dev.202609251017.e467a6c

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 (281) hide show
  1. package/docs/architecture/memory.md +9 -1
  2. package/docs/architecture/scheduling.md +13 -5
  3. package/docs/desktop-browser-cli.md +3 -2
  4. package/docs/guardian-request-flow.md +1 -1
  5. package/node_modules/@vellumai/avatar-manifest/src/__tests__/notification-avatar.test.ts +48 -4
  6. package/node_modules/@vellumai/avatar-manifest/src/notification-avatar.ts +12 -8
  7. package/node_modules/@vellumai/ces-client/node_modules/@vellumai/service-contracts/src/__tests__/guardian-requests.test.ts +26 -0
  8. package/node_modules/@vellumai/ces-client/node_modules/@vellumai/service-contracts/src/guardian-requests.ts +77 -3
  9. package/node_modules/@vellumai/gateway-client/node_modules/@vellumai/service-contracts/src/__tests__/guardian-requests.test.ts +26 -0
  10. package/node_modules/@vellumai/gateway-client/node_modules/@vellumai/service-contracts/src/guardian-requests.ts +77 -3
  11. package/node_modules/@vellumai/gateway-client/src/outbound-contract.ts +2 -4
  12. package/node_modules/@vellumai/service-contracts/src/__tests__/guardian-requests.test.ts +26 -0
  13. package/node_modules/@vellumai/service-contracts/src/guardian-requests.ts +77 -3
  14. package/openapi.yaml +152 -3
  15. package/package.json +1 -1
  16. package/src/__tests__/agent-loop.test.ts +76 -0
  17. package/src/__tests__/anthropic-error-formatting.test.ts +9 -0
  18. package/src/__tests__/background-shell-bash.test.ts +34 -0
  19. package/src/__tests__/background-shell-host-bash.test.ts +98 -1
  20. package/src/__tests__/background-tool-registry.test.ts +39 -0
  21. package/src/__tests__/channel-readiness-discord.test.ts +8 -0
  22. package/src/__tests__/channel-readiness-slack-remote.test.ts +8 -0
  23. package/src/__tests__/client-os-metadata-persistence.test.ts +4 -20
  24. package/src/__tests__/conversation-agent-loop.test.ts +529 -1
  25. package/src/__tests__/conversation-error.test.ts +18 -0
  26. package/src/__tests__/conversation-pairing.test.ts +69 -0
  27. package/src/__tests__/conversation-queue.test.ts +257 -4
  28. package/src/__tests__/conversation-runtime-assembly.test.ts +27 -0
  29. package/src/__tests__/emit-signal-routing-intent.test.ts +62 -0
  30. package/src/__tests__/evict-conversations-for-reload.test.ts +21 -0
  31. package/src/__tests__/gemini-provider.test.ts +9 -0
  32. package/src/__tests__/host-cu-proxy.test.ts +79 -0
  33. package/src/__tests__/host-cu-routes-targeted.test.ts +14 -5
  34. package/src/__tests__/introduction-card-resolver.test.ts +53 -8
  35. package/src/__tests__/job-handler-registration-guard.test.ts +1 -0
  36. package/src/__tests__/non-member-access-request.test.ts +48 -0
  37. package/src/__tests__/notification-events-source-context-probe.test.ts +48 -1
  38. package/src/__tests__/notification-platform-adapter.test.ts +97 -0
  39. package/src/__tests__/notification-vellum-adapter.test.ts +103 -3
  40. package/src/__tests__/platform-callback-registration.test.ts +46 -1
  41. package/src/__tests__/plugin-import-boundary-guard.test.ts +3 -0
  42. package/src/__tests__/scaffold-managed-skill-tool.test.ts +116 -66
  43. package/src/__tests__/scheduler-disk-pressure.test.ts +1 -0
  44. package/src/__tests__/scheduler-result-notification.test.ts +521 -7
  45. package/src/__tests__/scheduler-wake.test.ts +1 -0
  46. package/src/__tests__/skill-load-inline-command.test.ts +35 -0
  47. package/src/__tests__/subagent-cron-run-attribution.test.ts +41 -2
  48. package/src/__tests__/subagent-manager-notify.test.ts +48 -0
  49. package/src/__tests__/trusted-contact-inline-approval-integration.test.ts +6 -6
  50. package/src/__tests__/usage-attribution.test.ts +31 -0
  51. package/src/__tests__/wake-schedule-escalation.test.ts +1 -0
  52. package/src/agent/loop.ts +23 -0
  53. package/src/api/ask-question-tool.ts +50 -0
  54. package/src/api/events/generation-handoff.ts +4 -0
  55. package/src/api/events/message-complete.ts +4 -0
  56. package/src/api/events/tool-result.ts +11 -0
  57. package/src/api/index.ts +11 -0
  58. package/src/api/requests/guardian-actions.test.ts +45 -0
  59. package/src/api/requests/guardian-actions.ts +20 -0
  60. package/src/api/responses/conversation-message.ts +4 -0
  61. package/src/api/responses/home.ts +27 -0
  62. package/src/approvals/guardian-decision-primitive.ts +6 -4
  63. package/src/approvals/guardian-request-resolvers.ts +35 -30
  64. package/src/avatar/notification-avatar.test.ts +42 -34
  65. package/src/avatar/notification-avatar.ts +1 -0
  66. package/src/calls/__tests__/call-controller.test.ts +18 -0
  67. package/src/calls/__tests__/voice-control-protocol.test.ts +6 -1
  68. package/src/calls/__tests__/voice-leg-coordinator.test.ts +70 -0
  69. package/src/calls/__tests__/voice-session-bridge.test.ts +116 -4
  70. package/src/calls/__tests__/voice-triage-escalate.test.ts +77 -11
  71. package/src/calls/call-controller.ts +3 -1
  72. package/src/calls/voice-control-protocol.ts +12 -4
  73. package/src/calls/voice-escalation-judge.ts +2 -2
  74. package/src/calls/voice-leg-coordinator.ts +23 -5
  75. package/src/calls/voice-screen-annotation.ts +44 -0
  76. package/src/calls/voice-session-bridge.ts +63 -20
  77. package/src/calls/voice-triage-escalate.ts +82 -27
  78. package/src/cli/commands/__tests__/conversations-clear.test.ts +156 -0
  79. package/src/cli/commands/conversations.help.ts +7 -2
  80. package/src/cli/commands/conversations.ts +36 -30
  81. package/src/cli/commands/oauth/index.help.ts +8 -2
  82. package/src/cli/commands/oauth/request.test.ts +32 -0
  83. package/src/cli/commands/platform/index.help.ts +2 -1
  84. package/src/cli/commands/platform/index.ts +8 -2
  85. package/src/cli/lib/__tests__/plugin-catalog-cache.test.ts +60 -0
  86. package/src/cli/lib/__tests__/plugin-catalog-platform.test.ts +66 -2
  87. package/src/cli/lib/bundled-marketplace.json +35 -0
  88. package/src/cli/lib/bundled-plugin-packages.json +21 -0
  89. package/src/cli/lib/plugin-catalog-cache.ts +17 -2
  90. package/src/cli/lib/plugin-catalog-platform.ts +48 -9
  91. package/src/config/__tests__/default-profile-catalog.test.ts +28 -0
  92. package/src/config/__tests__/platform-identity.test.ts +59 -0
  93. package/src/config/__tests__/profile-order.test.ts +19 -0
  94. package/src/config/__tests__/sync-gated-profiles.test.ts +71 -0
  95. package/src/config/bundled-skills/computer-use/SKILL.md +20 -0
  96. package/src/config/bundled-skills/computer-use/TOOLS.json +11 -11
  97. package/src/config/bundled-skills/screen-annotation/SKILL.md +41 -29
  98. package/src/config/bundled-skills/screen-annotation/TOOLS.json +5 -5
  99. package/src/config/call-site-defaults.ts +6 -0
  100. package/src/config/default-profile-catalog.ts +30 -1
  101. package/src/config/default-profile-names.ts +36 -2
  102. package/src/config/feature-flag-registry.json +9 -1
  103. package/src/config/platform-identity.ts +22 -5
  104. package/src/config/profile-order.ts +7 -1
  105. package/src/config/schemas/call-site-catalog.ts +7 -0
  106. package/src/config/schemas/llm.ts +21 -5
  107. package/src/config/sync-gated-profiles.ts +96 -44
  108. package/src/daemon/__tests__/abort-null-controller.test.ts +135 -1
  109. package/src/daemon/__tests__/auto-profile-router.test.ts +380 -0
  110. package/src/daemon/__tests__/turn-tail-assistant-reply-notify.test.ts +16 -0
  111. package/src/daemon/activity-trail.test.ts +206 -0
  112. package/src/daemon/activity-trail.ts +190 -0
  113. package/src/daemon/auto-profile-router.ts +431 -0
  114. package/src/daemon/computer-use-observation.test.ts +81 -0
  115. package/src/daemon/computer-use-observation.ts +23 -0
  116. package/src/daemon/conversation-agent-loop-handlers.ts +11 -0
  117. package/src/daemon/conversation-agent-loop.ts +157 -32
  118. package/src/daemon/conversation-error.ts +19 -0
  119. package/src/daemon/conversation-lifecycle.ts +70 -7
  120. package/src/daemon/conversation-media-retry.ts +1 -2
  121. package/src/daemon/conversation-messaging.ts +31 -2
  122. package/src/daemon/conversation-process.ts +107 -16
  123. package/src/daemon/conversation-queue-manager.ts +7 -0
  124. package/src/daemon/conversation-runtime-assembly.ts +1 -0
  125. package/src/daemon/conversation-turn-finalize.ts +14 -0
  126. package/src/daemon/conversation.ts +25 -6
  127. package/src/daemon/event-loop-watchdog.test.ts +201 -2
  128. package/src/daemon/event-loop-watchdog.ts +182 -6
  129. package/src/daemon/host-cu-proxy.ts +34 -3
  130. package/src/daemon/pkb-reminder-builder.test.ts +10 -3
  131. package/src/daemon/pkb-reminder-builder.ts +1 -1
  132. package/src/daemon/wake-conversation-ops.ts +4 -2
  133. package/src/desktop/desktop-computer-use.test.ts +14 -4
  134. package/src/home/feed-types.ts +2 -0
  135. package/src/inbound/platform-callback-registration.ts +19 -6
  136. package/src/live-voice/__tests__/live-voice-agent-turn.test.ts +27 -0
  137. package/src/live-voice/__tests__/live-voice-flux-turn-end.test.ts +83 -0
  138. package/src/live-voice/__tests__/live-voice-triage-escalate.test.ts +48 -1
  139. package/src/live-voice/__tests__/live-voice-vad.test.ts +34 -2
  140. package/src/live-voice/__tests__/protocol.test.ts +22 -0
  141. package/src/live-voice/continuation-judge.ts +1 -1
  142. package/src/live-voice/live-voice-session.ts +25 -19
  143. package/src/live-voice/protocol.ts +16 -5
  144. package/src/messaging/providers/discord/withdraw.ts +4 -5
  145. package/src/messaging/providers/gmail/client.ts +4 -0
  146. package/src/messaging/providers/slack/withdraw.ts +5 -5
  147. package/src/messaging/providers/telegram-bot/withdraw.ts +4 -5
  148. package/src/monitoring/__tests__/proc-fixtures.ts +28 -0
  149. package/src/monitoring/__tests__/proc-wait-state.test.ts +232 -0
  150. package/src/monitoring/__tests__/process-usage.test.ts +243 -0
  151. package/src/monitoring/proc-wait-state.ts +366 -0
  152. package/src/monitoring/process-memory.ts +4 -3
  153. package/src/monitoring/process-usage.ts +215 -0
  154. package/src/monitoring/resource-sample-types.ts +8 -0
  155. package/src/monitoring/resource-sampler.ts +17 -2
  156. package/src/monitoring/stall-capture.ts +12 -0
  157. package/src/notifications/README.md +88 -9
  158. package/src/notifications/__tests__/assistant-reply-producer.test.ts +390 -119
  159. package/src/notifications/__tests__/background-result-producer.test.ts +1272 -0
  160. package/src/notifications/__tests__/broadcaster.test.ts +292 -1
  161. package/src/notifications/__tests__/completion-policy.test.ts +243 -0
  162. package/src/notifications/__tests__/decision-engine.test.ts +51 -4
  163. package/src/notifications/__tests__/has-pending-background-work.test.ts +281 -0
  164. package/src/notifications/__tests__/home-feed-side-effect.test.ts +161 -3
  165. package/src/notifications/__tests__/resolve-visible-in-source.test.ts +110 -1
  166. package/src/notifications/__tests__/schedule-result-producer.test.ts +11 -4
  167. package/src/notifications/adapters/macos.ts +24 -12
  168. package/src/notifications/adapters/platform.ts +16 -5
  169. package/src/notifications/assistant-reply-producer.ts +96 -71
  170. package/src/notifications/background-result-producer.ts +369 -0
  171. package/src/notifications/broadcaster.ts +28 -8
  172. package/src/notifications/completion-policy.ts +115 -0
  173. package/src/notifications/completion-work.ts +46 -0
  174. package/src/notifications/conversation-pairing.ts +32 -13
  175. package/src/notifications/decision-engine.ts +18 -24
  176. package/src/notifications/emit-signal.ts +16 -1
  177. package/src/notifications/events-store.ts +4 -0
  178. package/src/notifications/has-pending-background-work.ts +91 -0
  179. package/src/notifications/home-feed-mirror.ts +44 -0
  180. package/src/notifications/home-feed-side-effect.ts +54 -28
  181. package/src/notifications/resolve-visible-in-source.ts +56 -2
  182. package/src/notifications/result-output.ts +154 -0
  183. package/src/notifications/schedule-result-producer.ts +6 -187
  184. package/src/notifications/types.ts +2 -0
  185. package/src/oauth/AGENTS.md +9 -1
  186. package/src/oauth/byo-connection.test.ts +28 -0
  187. package/src/persistence/__tests__/recent-conversation-messages.test.ts +41 -0
  188. package/src/persistence/conversation-crud.ts +26 -0
  189. package/src/persistence/conversation-types.ts +15 -37
  190. package/src/persistence/embeddings/__tests__/embedding-local-lifecycle.test.ts +85 -14
  191. package/src/persistence/jobs-store.ts +229 -0
  192. package/src/persistence/migrations/383-normalize-opencode-host-connections.ts +44 -0
  193. package/src/persistence/migrations/__tests__/383-normalize-opencode-host-connections.test.ts +102 -0
  194. package/src/persistence/steps.ts +12 -0
  195. package/src/platform/sync-avatar.test.ts +29 -0
  196. package/src/plugins/defaults/memory/AGENTS.md +5 -3
  197. package/src/plugins/defaults/memory/__tests__/skill-update-receipt-boundary-guard.test.ts +45 -0
  198. package/src/plugins/defaults/memory/__tests__/skill-update-receipt-job.test.ts +580 -0
  199. package/src/plugins/defaults/memory/__tests__/turn-retrieval.test.ts +21 -0
  200. package/src/plugins/defaults/memory/graph/tools.ts +9 -5
  201. package/src/plugins/defaults/memory/hooks/conversation-deleted.ts +4 -0
  202. package/src/plugins/defaults/memory/hooks/user-prompt-submit.ts +7 -3
  203. package/src/plugins/defaults/memory/job-handlers.ts +8 -2
  204. package/src/plugins/defaults/memory/skill-update-receipt-job.ts +334 -0
  205. package/src/plugins/defaults/memory/tools.test.ts +17 -0
  206. package/src/plugins/defaults/memory/turn-retrieval.ts +8 -0
  207. package/src/plugins/defaults/memory/v3/__tests__/injection.test.ts +24 -0
  208. package/src/plugins/defaults/memory/v3/injector.ts +4 -4
  209. package/src/plugins/types.ts +2 -0
  210. package/src/providers/__tests__/search-provider-catalog.test.ts +8 -0
  211. package/src/providers/anthropic/client.ts +4 -0
  212. package/src/providers/gemini/client.ts +7 -1
  213. package/src/providers/inference/auth.ts +25 -0
  214. package/src/providers/inference/profile-probe.ts +1 -0
  215. package/src/{calls/__tests__/typesafe-noul.test.ts → providers/jev/ask.test.ts} +4 -4
  216. package/src/{calls/typesafe-noul.ts → providers/jev/ask.ts} +67 -34
  217. package/src/providers/openai/__tests__/api-error-normalization.test.ts +12 -0
  218. package/src/providers/openai/api-error-normalization.ts +4 -0
  219. package/src/providers/retry.ts +2 -0
  220. package/src/providers/search-provider-catalog.ts +10 -0
  221. package/src/providers/types.ts +7 -0
  222. package/src/providers/usage-tracking.ts +5 -3
  223. package/src/runtime/__tests__/agent-wake.test.ts +308 -0
  224. package/src/runtime/access-request-helper.ts +16 -9
  225. package/src/runtime/agent-wake-queue.ts +52 -0
  226. package/src/runtime/agent-wake.ts +111 -18
  227. package/src/runtime/channel-approval-types.ts +6 -38
  228. package/src/runtime/channel-approvals.ts +4 -5
  229. package/src/runtime/http-server.ts +4 -1
  230. package/src/runtime/introduction-policy.test.ts +17 -1
  231. package/src/runtime/introduction-policy.ts +25 -7
  232. package/src/runtime/migrations/vbundle-builder.ts +9 -0
  233. package/src/runtime/routes/__tests__/inference-profiles-routes.test.ts +21 -1
  234. package/src/runtime/routes/__tests__/inference-provider-connection-routes.test.ts +60 -0
  235. package/src/runtime/routes/__tests__/oauth-proxy-routes.test.ts +61 -3
  236. package/src/runtime/routes/auto-profile-routes.ts +83 -0
  237. package/src/runtime/routes/conversation-management-routes.ts +1 -3
  238. package/src/runtime/routes/conversation-routes.ts +9 -0
  239. package/src/runtime/routes/guardian-action-routes.ts +8 -23
  240. package/src/runtime/routes/host-cu-routes.ts +3 -1
  241. package/src/runtime/routes/index.ts +2 -0
  242. package/src/runtime/routes/inference-profiles-routes.ts +5 -2
  243. package/src/runtime/routes/inference-provider-connection-routes.ts +25 -8
  244. package/src/runtime/routes/migration-routes.ts +8 -2
  245. package/src/runtime/routes/oauth-proxy-passthrough.test.ts +32 -0
  246. package/src/runtime/routes/platform-routes.ts +6 -1
  247. package/src/runtime/routes/user-route-dispatcher.ts +1 -2
  248. package/src/runtime/web-presence.ts +3 -4
  249. package/src/schedule/schedule-store.ts +10 -0
  250. package/src/schedule/scheduler.ts +92 -24
  251. package/src/subagent/manager.ts +32 -12
  252. package/src/telegram/__tests__/webhook-health.test.ts +8 -0
  253. package/src/telemetry/telemetry-wire-source.json +1 -1
  254. package/src/telemetry/telemetry-wire.generated.ts +1 -0
  255. package/src/tools/background-tool-registry.ts +30 -3
  256. package/src/tools/browser/cdp-client/local-cdp-client.ts +1 -2
  257. package/src/tools/host-terminal/host-shell.ts +7 -0
  258. package/src/tools/network/__tests__/web-fetch-firecrawl.test.ts +28 -1
  259. package/src/tools/network/__tests__/web-fetch-metadata.test.ts +34 -0
  260. package/src/tools/network/__tests__/web-fetch-tinyfish.test.ts +35 -1
  261. package/src/tools/network/__tests__/web-search.test.ts +96 -0
  262. package/src/tools/network/web-fetch.ts +30 -9
  263. package/src/tools/network/web-search.ts +235 -4
  264. package/src/tools/skills/load.ts +13 -17
  265. package/src/tools/skills/scaffold-managed.ts +86 -71
  266. package/src/tools/terminal/shell-launch.test.ts +27 -1
  267. package/src/tools/terminal/shell.ts +4 -0
  268. package/src/tools/workspace-tools/loader.ts +1 -2
  269. package/src/usage/attribution.ts +18 -1
  270. package/src/util/abort-reasons.ts +2 -0
  271. package/src/util/errors.ts +1 -0
  272. package/src/util/oauth-request-body.test.ts +5 -1
  273. package/src/util/provider-error-patterns.ts +11 -0
  274. package/src/util/single-flight.test.ts +6 -0
  275. package/src/util/single-flight.ts +2 -0
  276. package/src/util/worker-pipe.test.ts +56 -10
  277. package/src/util/worker-pipe.ts +9 -4
  278. package/src/workspace/git-service.ts +6 -1
  279. package/src/workspace/migrations/158-repoint-opencode-host-provider-fragments.ts +163 -0
  280. package/src/workspace/migrations/__tests__/158-repoint-opencode-host-provider-fragments.test.ts +188 -0
  281. package/src/workspace/migrations/registry.ts +2 -0
@@ -31,6 +31,25 @@ let assistantRow: MessageRow | null = null;
31
31
  let initiatingRow: MessageRow | null = null;
32
32
  let attentionState: AttentionState | null = null;
33
33
  let getConversationShouldThrow = false;
34
+ let pendingBackgroundWork = false;
35
+ let pendingWorkStartedAt: number | undefined;
36
+ let firstAssistantRow: MessageRow | null = null;
37
+ let persistedRows: MessageRow[] | undefined;
38
+ const recentHistoryPages: Array<string | undefined> = [];
39
+ const pendingWorkArgs: unknown[][] = [];
40
+
41
+ mock.module("../has-pending-background-work.js", () => ({
42
+ hasPendingBackgroundWork: (...args: unknown[]) => {
43
+ pendingWorkArgs.push(args);
44
+ if (pendingWorkStartedAt !== undefined) {
45
+ return (
46
+ pendingWorkStartedAt >=
47
+ (args[1] as { startedAfter: number }).startedAfter
48
+ );
49
+ }
50
+ return pendingBackgroundWork;
51
+ },
52
+ }));
34
53
 
35
54
  mock.module("../emit-signal.js", () => ({
36
55
  emitNotificationSignal: async (params: any) => {
@@ -60,8 +79,31 @@ mock.module("../../persistence/conversation-crud.js", () => ({
60
79
  },
61
80
  getMessageById: (messageId: string) => {
62
81
  messageLookups.push(messageId);
82
+ if (firstAssistantRow?.id === messageId) {
83
+ return firstAssistantRow;
84
+ }
85
+ const persisted = persistedRows?.find((row) => row.id === messageId);
86
+ if (persisted) {
87
+ return persisted;
88
+ }
63
89
  return messageId === ASSISTANT_MESSAGE_ID ? assistantRow : initiatingRow;
64
90
  },
91
+ getRecentConversationMessages: (
92
+ _conversationId: string,
93
+ limit: number,
94
+ beforeMessageId?: string,
95
+ ) => {
96
+ recentHistoryPages.push(beforeMessageId);
97
+ const history =
98
+ persistedRows ??
99
+ [initiatingRow, firstAssistantRow, assistantRow].filter(
100
+ (row): row is MessageRow => row !== null,
101
+ );
102
+ const end = beforeMessageId
103
+ ? history.findIndex((row) => row.id === beforeMessageId)
104
+ : history.length;
105
+ return history.slice(Math.max(0, end - limit), end);
106
+ },
65
107
  }));
66
108
 
67
109
  // Attachments the assistant row carries. Linked by the agent loop before the
@@ -78,6 +120,30 @@ mock.module("../../persistence/attachments-store.js", () => ({
78
120
  },
79
121
  }));
80
122
 
123
+ let guardianPrincipalId: string | undefined = "guardian-1";
124
+ let guardianLookupGate: Promise<void> | undefined;
125
+ let onGuardianLookup = () => {};
126
+ const realGuardianDelivery =
127
+ await import("../../contacts/guardian-delivery-reader.js");
128
+ mock.module("../../contacts/guardian-delivery-reader.js", () => ({
129
+ ...realGuardianDelivery,
130
+ getGuardianDelivery: async () => {
131
+ onGuardianLookup();
132
+ if (guardianLookupGate) {
133
+ await guardianLookupGate;
134
+ }
135
+ return guardianPrincipalId
136
+ ? [
137
+ {
138
+ channelType: "vellum",
139
+ status: "active",
140
+ principalId: guardianPrincipalId,
141
+ },
142
+ ]
143
+ : null;
144
+ },
145
+ }));
146
+
81
147
  // Defaults to unattended, so every other case in this file exercises the
82
148
  // unsuppressed path.
83
149
  let desktopAttended = false;
@@ -290,10 +356,19 @@ async function run(
290
356
  }
291
357
 
292
358
  beforeEach(() => {
359
+ pendingBackgroundWork = false;
360
+ pendingWorkStartedAt = undefined;
361
+ firstAssistantRow = null;
362
+ persistedRows = undefined;
363
+ recentHistoryPages.length = 0;
364
+ pendingWorkArgs.length = 0;
293
365
  emitCalls.length = 0;
294
366
  warnCalls.length = 0;
295
367
  messageLookups.length = 0;
296
368
  attachmentLookups.length = 0;
369
+ guardianPrincipalId = "guardian-1";
370
+ guardianLookupGate = undefined;
371
+ onGuardianLookup = () => {};
297
372
  desktopPresenceArgs.length = 0;
298
373
  webPresenceArgs.length = 0;
299
374
  assistantAttachments = [];
@@ -316,6 +391,281 @@ beforeEach(() => {
316
391
  // ── Tests ──────────────────────────────────────────────────────────────
317
392
 
318
393
  describe("emitAssistantReplyNotification", () => {
394
+ function appendCompletedContinuation(): void {
395
+ persistedRows = [
396
+ ...(persistedRows ?? [initiatingRow!, assistantRow!]),
397
+ makeMessage({
398
+ id: "msg-completed-trigger",
399
+ createdAt: assistantRow!.createdAt,
400
+ metadata: JSON.stringify({
401
+ backgroundEventSource: "background-tool",
402
+ backgroundToolCompletion: {
403
+ id: "tool-completed",
404
+ toolName: "bash",
405
+ conversationId: CONVERSATION_ID,
406
+ command: "example-command",
407
+ startedAt: assistantRow!.createdAt,
408
+ completedAt: assistantRow!.createdAt,
409
+ status: "completed",
410
+ exitCode: 0,
411
+ output: "completed",
412
+ },
413
+ }),
414
+ }),
415
+ makeMessage({
416
+ id: "msg-completed-result",
417
+ role: "assistant",
418
+ createdAt: assistantRow!.createdAt,
419
+ content: [{ type: "text", text: "The requested work is finished." }],
420
+ }),
421
+ ];
422
+ }
423
+
424
+ test.each([false, true])(
425
+ "suppresses a stale kickoff after fast completion with projected=%s and timestamp ties",
426
+ async (projected) => {
427
+ assistantRow!.content = [{ type: "text", text: "The work is started." }];
428
+ appendCompletedContinuation();
429
+ if (projected) {
430
+ attentionState!.latestAssistantMessageId = "msg-completed-result";
431
+ }
432
+
433
+ await run();
434
+
435
+ expect(pendingBackgroundWork).toBe(false);
436
+ expect(emitCalls).toHaveLength(0);
437
+ },
438
+ );
439
+
440
+ test.each(["new continuation", "pending continuation", "reply seen"])(
441
+ "rechecks freshness after presence lookup when there is a %s",
442
+ async (change) => {
443
+ const { promise: lookupStarted, resolve: markLookupStarted } =
444
+ Promise.withResolvers<void>();
445
+ const { promise: lookupReady, resolve: releaseLookup } =
446
+ Promise.withResolvers<void>();
447
+ onGuardianLookup = markLookupStarted;
448
+ guardianLookupGate = lookupReady;
449
+ const notification = run();
450
+ await lookupStarted;
451
+ if (change === "new continuation") {
452
+ appendCompletedContinuation();
453
+ } else if (change === "pending continuation") {
454
+ pendingBackgroundWork = true;
455
+ } else {
456
+ attentionState!.lastSeenAssistantMessageAt = assistantRow!.createdAt;
457
+ }
458
+ releaseLookup();
459
+ await notification;
460
+
461
+ expect(emitCalls).toHaveLength(0);
462
+ },
463
+ );
464
+
465
+ test.each(["text", "media"])(
466
+ "keeps an unseen public %s reply with a same-turn private wrap-up across history pages",
467
+ async (replyType) => {
468
+ if (replyType === "media") {
469
+ assistantRow!.content = [];
470
+ assistantAttachments = [{ originalFilename: "report.pdf" }];
471
+ }
472
+ persistedRows = [initiatingRow!, assistantRow!];
473
+ for (let index = 0; index < 110; index++) {
474
+ persistedRows.push(
475
+ makeMessage({
476
+ id: `msg-private-${index}`,
477
+ role: "assistant",
478
+ content: [
479
+ {
480
+ type: "tool_use",
481
+ id: `tool-${index}`,
482
+ name: "bash",
483
+ input: {},
484
+ },
485
+ ],
486
+ metadata: JSON.stringify({ assistantTextVisibility: "private" }),
487
+ createdAt: assistantRow!.createdAt + index + 1,
488
+ }),
489
+ makeMessage({
490
+ id: `msg-tool-result-${index}`,
491
+ content: [
492
+ {
493
+ type: "tool_result",
494
+ tool_use_id: `tool-${index}`,
495
+ content: "ok",
496
+ },
497
+ ],
498
+ createdAt: assistantRow!.createdAt + index + 1,
499
+ }),
500
+ );
501
+ }
502
+ const wrapUp = makeMessage({
503
+ id: "msg-private-wrap-up",
504
+ role: "assistant",
505
+ content: [{ type: "text", text: "Private working notes." }],
506
+ metadata: JSON.stringify({ assistantTextVisibility: "private" }),
507
+ createdAt: assistantRow!.createdAt + 200,
508
+ });
509
+ persistedRows.push(wrapUp);
510
+ attentionState!.latestAssistantMessageId = wrapUp.id;
511
+ attentionState!.latestAssistantMessageAt = wrapUp.createdAt;
512
+
513
+ await run();
514
+
515
+ expect(recentHistoryPages).toContain(
516
+ persistedRows[persistedRows.length - 200].id,
517
+ );
518
+ expect(emitCalls).toHaveLength(1);
519
+ expect(emitCalls[0].contextPayload.requestedMessage).toBe(
520
+ replyType === "media" ? "Sent report.pdf" : "Sure, here is the plan.",
521
+ );
522
+ },
523
+ );
524
+
525
+ test("does not reuse a seen public reply because a private wrap-up is unseen", async () => {
526
+ const wrapUp = makeMessage({
527
+ id: "msg-private-wrap-up",
528
+ role: "assistant",
529
+ metadata: JSON.stringify({ assistantTextVisibility: "private" }),
530
+ createdAt: assistantRow!.createdAt + 1,
531
+ });
532
+ persistedRows = [initiatingRow!, assistantRow!, wrapUp];
533
+ attentionState!.latestAssistantMessageId = wrapUp.id;
534
+ attentionState!.latestAssistantMessageAt = wrapUp.createdAt;
535
+ attentionState!.lastSeenAssistantMessageAt = assistantRow!.createdAt;
536
+
537
+ await run();
538
+
539
+ expect(emitCalls).toHaveLength(0);
540
+ });
541
+
542
+ test.each([false, true])(
543
+ "keeps a newer reply eligible when older work starts its continuation, completed=%s",
544
+ async (completed) => {
545
+ appendCompletedContinuation();
546
+ const trigger = persistedRows![2];
547
+ const metadata = JSON.parse(trigger.metadata!);
548
+ metadata.backgroundToolCompletion.startedAt =
549
+ assistantRow!.createdAt - 1000;
550
+ trigger.metadata = JSON.stringify(metadata);
551
+ if (!completed) {
552
+ persistedRows!.pop();
553
+ }
554
+ await run();
555
+ expect(emitCalls).toHaveLength(1);
556
+ },
557
+ );
558
+
559
+ test("keeps a later human reply eligible after old background completion", async () => {
560
+ appendCompletedContinuation();
561
+ persistedRows = persistedRows!.map((row) => ({
562
+ ...row,
563
+ id: `old-${row.id}`,
564
+ createdAt: row.createdAt - 1000,
565
+ }));
566
+ persistedRows!.push(initiatingRow!, assistantRow!);
567
+
568
+ await run();
569
+
570
+ expect(emitCalls).toHaveLength(1);
571
+ });
572
+
573
+ test.each([false, true])(
574
+ "preserves a mixed human/hidden batch unless a later continuation exists=%s",
575
+ async (laterContinuation) => {
576
+ initiatingRow!.metadata = JSON.stringify({
577
+ turnOutcome: "batched",
578
+ turnBatchedInto: "msg-hidden-batch-tail",
579
+ });
580
+ const hiddenTail = makeMessage({
581
+ id: "msg-hidden-batch-tail",
582
+ metadata: JSON.stringify({ hidden: true }),
583
+ });
584
+ persistedRows = [initiatingRow!, hiddenTail, assistantRow!];
585
+ if (laterContinuation) {
586
+ appendCompletedContinuation();
587
+ }
588
+
589
+ await run();
590
+
591
+ expect(emitCalls).toHaveLength(laterContinuation ? 0 : 1);
592
+ },
593
+ );
594
+
595
+ test.each([undefined, "", 123, "msg-missing-target"])(
596
+ "rejects a malformed or missing batch target %s",
597
+ async (turnBatchedInto) => {
598
+ initiatingRow!.metadata = JSON.stringify({
599
+ turnOutcome: "batched",
600
+ turnBatchedInto,
601
+ });
602
+
603
+ await run();
604
+
605
+ expect(emitCalls).toHaveLength(0);
606
+ },
607
+ );
608
+
609
+ test("scopes unfinished work to this reply's turn, including its tool-call rows", async () => {
610
+ firstAssistantRow = makeMessage({
611
+ id: "msg-first-assistant",
612
+ role: "assistant",
613
+ createdAt: 1700000000100,
614
+ });
615
+
616
+ await run();
617
+
618
+ expect(emitCalls).toHaveLength(1);
619
+ expect(pendingWorkArgs.length).toBeGreaterThan(0);
620
+ for (const args of pendingWorkArgs) {
621
+ expect(args).toEqual([
622
+ CONVERSATION_ID,
623
+ { startedAfter: firstAssistantRow.createdAt },
624
+ ]);
625
+ }
626
+ });
627
+
628
+ test("long turns retain the pending work cutoff from their first assistant row", async () => {
629
+ const first = makeMessage({
630
+ id: "msg-first-assistant",
631
+ role: "assistant",
632
+ createdAt: initiatingRow!.createdAt + 1,
633
+ });
634
+ pendingWorkStartedAt = first.createdAt + 1;
635
+ persistedRows = [initiatingRow!, first];
636
+ for (let index = 0; index < 220; index++) {
637
+ persistedRows.push(
638
+ makeMessage({
639
+ id: `msg-long-turn-${index}`,
640
+ role: "assistant",
641
+ createdAt: first.createdAt + index + 2,
642
+ }),
643
+ );
644
+ }
645
+ assistantRow!.createdAt = first.createdAt + 300;
646
+ attentionState!.latestAssistantMessageAt = assistantRow!.createdAt;
647
+ persistedRows.push(assistantRow!);
648
+
649
+ await run();
650
+
651
+ expect(emitCalls).toHaveLength(0);
652
+ expect(pendingWorkArgs[0]).toEqual([
653
+ CONVERSATION_ID,
654
+ { startedAfter: first.createdAt },
655
+ ]);
656
+ });
657
+
658
+ test("does not announce completion while delegated work is pending", async () => {
659
+ pendingBackgroundWork = true;
660
+ assistantRow = makeAssistantRow([
661
+ { type: "text", text: "I have started the requested work." },
662
+ ]);
663
+
664
+ await run();
665
+
666
+ expect(emitCalls).toHaveLength(0);
667
+ });
668
+
319
669
  test("emits one well-formed signal for an unseen user-conversation reply", async () => {
320
670
  await run();
321
671
 
@@ -336,7 +686,7 @@ describe("emitAssistantReplyNotification", () => {
336
686
  },
337
687
  dedupeKey: `chat.assistant_reply:${CONVERSATION_ID}:${ASSISTANT_MESSAGE_ID}`,
338
688
  });
339
- // No conversation-creation fields: the platform channel is push-only.
689
+ // Completion alerts link to the existing conversation.
340
690
  expect("requiresConversation" in emitCalls[0]).toBe(false);
341
691
  expect("conversationAffinityHint" in emitCalls[0]).toBe(false);
342
692
  expect("routingIntent" in emitCalls[0]).toBe(false);
@@ -355,139 +705,34 @@ describe("emitAssistantReplyNotification", () => {
355
705
  // so the producer's contract here is the hint it emits, not the silence.
356
706
  describe("desktop presence", () => {
357
707
  beforeEach(() => {
358
- initiatingRow = makeMacOriginatedMessage();
359
708
  desktopAttended = true;
360
709
  });
361
710
 
362
- test("marks the signal source-active while a desktop reports itself attended", async () => {
363
- await run();
364
-
365
- expect(emitCalls).toHaveLength(1);
366
- expect(emitCalls[0].attentionHints.visibleInSourceNow).toBe(true);
367
- // Unscoped by design: the push targets the assistant owner, and a pod
368
- // has exactly one owner, so no principal filter belongs here.
369
- expect(desktopPresenceArgs).toEqual([[]]);
370
- });
371
-
372
- test("marks a Windows-originated signal source-active while attended", async () => {
373
- initiatingRow = makeWindowsOriginatedMessage();
374
-
375
- await run();
376
-
377
- expect(emitCalls).toHaveLength(1);
378
- expect(emitCalls[0].attentionHints.visibleInSourceNow).toBe(true);
379
- });
380
-
381
- test("leaves the signal live when no desktop is attended", async () => {
382
- desktopAttended = false;
383
-
384
- await run();
385
-
386
- expect(emitCalls).toHaveLength(1);
387
- expect(emitCalls[0].attentionHints.visibleInSourceNow).toBe(false);
388
- });
389
-
390
- test("leaves the signal live when the presence read throws", async () => {
391
- desktopPresenceShouldThrow = true;
392
-
393
- await run();
394
-
395
- expect(emitCalls).toHaveLength(1);
396
- expect(emitCalls[0].attentionHints.visibleInSourceNow).toBe(false);
397
- expect(warnCalls).toHaveLength(1);
398
- });
399
-
400
- // An attended desktop only speaks for a turn that desktop itself opened.
401
- // The user can send from the phone minutes after last touching the keyboard,
402
- // is exactly the reply this producer exists to push.
403
- const NON_DESKTOP_ORIGIN_CASES: Array<{
404
- name: string;
405
- metadata: unknown;
406
- }> = [
407
- {
408
- name: "the iOS app",
409
- metadata: { client: { os: "ios" }, clientOsFromRequest: true },
410
- },
411
- {
412
- name: "a browser",
413
- metadata: { client: { os: "web" }, clientOsFromRequest: true },
414
- },
415
- {
416
- name: "a client that reports no OS",
417
- metadata: { client: {} },
418
- },
419
- { name: "a row with no client bag", metadata: {} },
420
- {
421
- name: "a client reporting an unknown OS",
422
- metadata: { client: { os: "bsd" }, clientOsFromRequest: true },
423
- },
424
- // The fail-closed shape this gate exists to refuse: a surface action
425
- // tapped on the phone carries no transport, so persistence stamps the
426
- // `macos` the conversation's live client state kept from an earlier
427
- // desktop send. Without the marker that OS names an earlier turn, not
428
- // this one, and the push the user is waiting for on their phone would
429
- // be dropped by the attended desktop.
430
- {
431
- name: "a row whose macOS OS was inherited, not reported",
432
- metadata: { userMessageInterface: "web", client: { os: "macos" } },
433
- },
434
- // A marker without a matching OS is not evidence of anything.
435
- {
436
- name: "a row marked request-reported with no client bag",
437
- metadata: { clientOsFromRequest: true },
438
- },
439
- ];
440
-
441
- for (const { name, metadata } of NON_DESKTOP_ORIGIN_CASES) {
442
- test(`leaves the signal live for a turn opened from ${name}`, async () => {
443
- initiatingRow = makeMessage({ metadata: JSON.stringify(metadata) });
444
-
711
+ for (const [name, makeRow] of [
712
+ ["macOS", makeMacOriginatedMessage],
713
+ ["Windows", makeWindowsOriginatedMessage],
714
+ ] as const) {
715
+ test(`notifies an unseen ${name} reply while the user is in another app`, async () => {
716
+ initiatingRow = makeRow();
445
717
  await run();
446
718
 
447
719
  expect(emitCalls).toHaveLength(1);
448
720
  expect(emitCalls[0].attentionHints.visibleInSourceNow).toBe(false);
449
- // The origin check runs first, so a turn no desktop opened never
450
- // reaches the presence read.
451
721
  expect(desktopPresenceArgs).toEqual([]);
452
722
  });
453
723
  }
454
724
 
455
- // The transport interface is "web" for the macOS app, the iOS app, and a
456
- // desktop browser alike, so it cannot stand in for the client OS.
457
- test("leaves the signal live for a web-interface turn with no client OS", async () => {
458
- initiatingRow = makeMessage({
459
- metadata: JSON.stringify({
460
- userMessageChannel: "vellum",
461
- userMessageInterface: "web",
462
- }),
463
- });
464
-
725
+ test("ignores whole-computer presence even when its read would fail", async () => {
726
+ initiatingRow = makeMacOriginatedMessage();
727
+ desktopPresenceShouldThrow = true;
465
728
  await run();
466
729
 
467
- expect(emitCalls).toHaveLength(1);
468
730
  expect(emitCalls[0].attentionHints.visibleInSourceNow).toBe(false);
469
- });
470
-
471
- // An unrecognized channel fails the whole metadata schema, so the origin
472
- // read has to answer off the permissive fallback too.
473
- test("marks the signal source-active when only the channel is unrecognized", async () => {
474
- initiatingRow = makeMessage({
475
- metadata: JSON.stringify({
476
- userMessageChannel: "not-a-channel",
477
- client: { os: "macos" },
478
- clientOsFromRequest: true,
479
- }),
480
- });
481
-
482
- await run();
483
-
484
- expect(emitCalls).toHaveLength(1);
485
- expect(emitCalls[0].attentionHints.visibleInSourceNow).toBe(true);
731
+ expect(desktopPresenceArgs).toEqual([]);
732
+ expect(warnCalls).toHaveLength(0);
486
733
  });
487
734
  });
488
735
 
489
- // Same pre-gate as "desktop presence" above, scoped to a browser tab
490
- // instead of the whole app.
491
736
  describe("web presence", () => {
492
737
  beforeEach(() => {
493
738
  initiatingRow = makeWebOriginatedMessage();
@@ -501,7 +746,18 @@ describe("emitAssistantReplyNotification", () => {
501
746
  expect(emitCalls[0].attentionHints.visibleInSourceNow).toBe(true);
502
747
  // Scoped to this conversation, unlike desktop attendance: a focused tab
503
748
  // only speaks for the conversation it is actually looking at.
504
- expect(webPresenceArgs).toEqual([[CONVERSATION_ID]]);
749
+ expect(webPresenceArgs).toEqual([
750
+ [CONVERSATION_ID, { actorPrincipalId: "guardian-1" }],
751
+ ]);
752
+ });
753
+
754
+ test("does not suppress without a resolved recipient", async () => {
755
+ guardianPrincipalId = undefined;
756
+ await run();
757
+
758
+ expect(emitCalls).toHaveLength(1);
759
+ expect(emitCalls[0].attentionHints.visibleInSourceNow).toBe(false);
760
+ expect(webPresenceArgs).toEqual([]);
505
761
  });
506
762
 
507
763
  test("leaves the signal live when no web tab is focused on this conversation", async () => {
@@ -540,7 +796,9 @@ describe("emitAssistantReplyNotification", () => {
540
796
 
541
797
  expect(emitCalls).toHaveLength(1);
542
798
  expect(emitCalls[0].attentionHints.visibleInSourceNow).toBe(true);
543
- expect(webPresenceArgs).toEqual([[CONVERSATION_ID]]);
799
+ expect(webPresenceArgs).toEqual([
800
+ [CONVERSATION_ID, { actorPrincipalId: "guardian-1" }],
801
+ ]);
544
802
  });
545
803
  });
546
804
 
@@ -957,6 +1215,19 @@ describe("emitAssistantReplyNotification", () => {
957
1215
  });
958
1216
  }
959
1217
 
1218
+ test("pushes the reply to a hidden voice continuation result", async () => {
1219
+ initiatingRow = makeMessage({
1220
+ metadata: JSON.stringify({
1221
+ hidden: true,
1222
+ voiceContinuationResult: true,
1223
+ }),
1224
+ });
1225
+
1226
+ await run();
1227
+
1228
+ expect(emitCalls).toHaveLength(1);
1229
+ });
1230
+
960
1231
  // A pointer turn fact-checks the rows it generated only after the turn ends
961
1232
  // and deletes them when validation fails, so a push at turn end would have
962
1233
  // already carried a call outcome the deterministic fallback then replaces.