@vellumai/assistant 0.11.4-staging.2 → 0.11.4-staging.4

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 (209) hide show
  1. package/AGENTS.md +8 -2
  2. package/ARCHITECTURE.md +2 -0
  3. package/docs/architecture/memory.md +15 -0
  4. package/docs/browser-use-architecture-phase2.md +128 -56
  5. package/docs/flux-turn-detection-spike.md +11 -6
  6. package/docs/guardian-request-flow.md +35 -0
  7. package/knip.json +3 -0
  8. package/node_modules/@vellumai/ces-client/node_modules/@vellumai/service-contracts/src/remote-web-pairing.ts +60 -0
  9. package/node_modules/@vellumai/gateway-client/node_modules/@vellumai/service-contracts/src/remote-web-pairing.ts +60 -0
  10. package/node_modules/@vellumai/service-contracts/src/remote-web-pairing.ts +60 -0
  11. package/openapi.yaml +136 -76
  12. package/package.json +1 -1
  13. package/scripts/write-plugin-api-shim.ts +10 -0
  14. package/src/__tests__/app-control-flow.test.ts +1 -0
  15. package/src/__tests__/approval-routes-http.test.ts +2 -2
  16. package/src/__tests__/assistant-feature-flag-guard.test.ts +25 -3
  17. package/src/__tests__/channel-setup-panel-ack.test.ts +1 -1
  18. package/src/__tests__/compaction-events.test.ts +8 -10
  19. package/src/__tests__/conversation-agent-loop.test.ts +4 -1
  20. package/src/__tests__/conversation-confirmation-signals.test.ts +112 -0
  21. package/src/__tests__/conversation-load-history-repair.test.ts +209 -0
  22. package/src/__tests__/conversation-notifiers-provenance.test.ts +1 -1
  23. package/src/__tests__/conversation-queue.test.ts +39 -62
  24. package/src/__tests__/conversation-routes-disk-view.test.ts +1 -1
  25. package/src/__tests__/conversation-routes-enabled-plugins.test.ts +1 -1
  26. package/src/__tests__/conversation-routes-guardian-reply.test.ts +9 -9
  27. package/src/__tests__/conversation-routes-hidden-queue.test.ts +1 -1
  28. package/src/__tests__/conversation-routes-slash-commands.test.ts +1 -1
  29. package/src/__tests__/conversation-runtime-assembly.test.ts +53 -0
  30. package/src/__tests__/conversation-slash-queue.test.ts +3 -0
  31. package/src/__tests__/conversation-surfaces-action-delivery.test.ts +1 -0
  32. package/src/__tests__/conversation-surfaces-activation-emit.test.ts +1 -0
  33. package/src/__tests__/conversation-surfaces-app-control.test.ts +1 -0
  34. package/src/__tests__/conversation-surfaces-app-open.test.ts +1 -1
  35. package/src/__tests__/conversation-surfaces-data-persist.test.ts +1 -1
  36. package/src/__tests__/conversation-surfaces-history-restored-completion.test.ts +21 -14
  37. package/src/__tests__/conversation-surfaces-queued-emit.test.ts +1 -0
  38. package/src/__tests__/conversation-surfaces-standalone-payloads.test.ts +1 -0
  39. package/src/__tests__/conversation-surfaces-standalone.test.ts +1 -0
  40. package/src/__tests__/conversation-surfaces-state-update.test.ts +1 -1
  41. package/src/__tests__/conversation-surfaces-table-action.test.ts +1 -1
  42. package/src/__tests__/conversation-surfaces-task-progress.test.ts +1 -1
  43. package/src/__tests__/conversation-tool-setup-app-refresh.test.ts +1 -1
  44. package/src/__tests__/conversation-tool-setup-attribution.test.ts +1 -1
  45. package/src/__tests__/cu-unified-flow.test.ts +1 -0
  46. package/src/__tests__/document-sync-tags.test.ts +0 -75
  47. package/src/__tests__/file-ops-service.test.ts +163 -30
  48. package/src/__tests__/filesystem-tools.test.ts +23 -24
  49. package/src/__tests__/gateway-only-guard.test.ts +2 -5
  50. package/src/__tests__/host-file-read-tool.test.ts +16 -19
  51. package/src/__tests__/http-user-message-parity.test.ts +1 -1
  52. package/src/__tests__/init-feature-flag-overrides.test.ts +49 -0
  53. package/src/__tests__/managed-skill-lifecycle.test.ts +7 -0
  54. package/src/__tests__/media-generate-image.test.ts +131 -21
  55. package/src/__tests__/memory-retrieval-hook.test.ts +94 -2
  56. package/src/__tests__/plugin-api-webhook-url.test.ts +10 -7
  57. package/src/__tests__/plugin-import-boundary-reverse-guard.test.ts +7 -3
  58. package/src/__tests__/proxy-approval-callback.test.ts +1 -0
  59. package/src/__tests__/qdrant-manager.test.ts +14 -1
  60. package/src/__tests__/run-due-schedules.test.ts +21 -0
  61. package/src/__tests__/scaffold-managed-skill-tool.test.ts +187 -18
  62. package/src/__tests__/schedule-routes.test.ts +23 -0
  63. package/src/__tests__/schedule-store.test.ts +17 -0
  64. package/src/__tests__/secret-ingress-http.test.ts +1 -1
  65. package/src/__tests__/send-endpoint-busy.test.ts +3 -3
  66. package/src/__tests__/starter-task-flow.test.ts +5 -4
  67. package/src/__tests__/subagent-fork-prompt-role.test.ts +1 -1
  68. package/src/__tests__/subagent-spawn-and-await.test.ts +4 -7
  69. package/src/__tests__/subagent-tool-gate-mode.test.ts +1 -1
  70. package/src/__tests__/subagent-tools.test.ts +81 -101
  71. package/src/__tests__/surface-completion-in-flight-snapshot.test.ts +1 -0
  72. package/src/__tests__/tool-executor.test.ts +5 -1
  73. package/src/__tests__/ui-choice-copy-surfaces.test.ts +1 -1
  74. package/src/__tests__/ui-visual-surface.test.ts +1 -1
  75. package/src/__tests__/ui-voice-picker-surface.test.ts +1 -1
  76. package/src/__tests__/ui-work-result-surface.test.ts +1 -1
  77. package/src/__tests__/voice-scoped-grant-consumer.test.ts +5 -3
  78. package/src/__tests__/voice-session-bridge.test.ts +85 -29
  79. package/src/acp/session-manager.ts +8 -1
  80. package/src/api/events/host-file.ts +2 -2
  81. package/src/api/surfaces.ts +5 -0
  82. package/src/calls/__tests__/voice-session-bridge.test.ts +21 -10
  83. package/src/calls/__tests__/voice-triage-escalate.test.ts +8 -0
  84. package/src/calls/voice-session-bridge.ts +44 -25
  85. package/src/calls/voice-triage-escalate.ts +1 -0
  86. package/src/cli/bundled-modules.ts +29 -0
  87. package/src/cli/commands/db/repair.ts +4 -8
  88. package/src/cli/commands/domain.ts +6 -3
  89. package/src/cli/commands/email.ts +6 -3
  90. package/src/cli/commands/keys.ts +8 -3
  91. package/src/cli/commands/plugins.ts +85 -36
  92. package/src/cli/commands/schedules.ts +35 -1
  93. package/src/cli/lib/bundled-marketplace.json +1 -1
  94. package/src/config/__tests__/balanced-model-experiment.test.ts +278 -0
  95. package/src/config/assistant-feature-flags.ts +36 -15
  96. package/src/config/balanced-model-experiment.ts +35 -0
  97. package/src/config/bundled-skills/image-studio/SKILL.md +5 -4
  98. package/src/config/bundled-skills/image-studio/TOOLS.json +1 -1
  99. package/src/config/bundled-skills/image-studio/tools/media-generate-image.ts +101 -0
  100. package/src/config/bundled-skills/skill-management/TOOLS.json +9 -3
  101. package/src/config/bundled-skills/subagent/SKILL.md +17 -12
  102. package/src/config/bundled-skills/subagent/TOOLS.json +4 -4
  103. package/src/config/call-site-defaults.ts +7 -0
  104. package/src/config/default-profile-catalog.ts +96 -4
  105. package/src/config/feature-flag-registry.json +11 -11
  106. package/src/config/skills.ts +9 -2
  107. package/src/daemon/__tests__/conversation-surfaces-launch.test.ts +1 -1
  108. package/src/daemon/conversation-agent-loop.ts +14 -13
  109. package/src/daemon/conversation-notifiers.ts +11 -9
  110. package/src/daemon/conversation-process.ts +0 -27
  111. package/src/daemon/conversation-runtime-assembly.ts +9 -2
  112. package/src/daemon/conversation-store.ts +4 -4
  113. package/src/daemon/conversation-surfaces.ts +27 -13
  114. package/src/daemon/conversation-tool-setup.ts +2 -4
  115. package/src/daemon/conversation.ts +104 -51
  116. package/src/daemon/doordash-steps.ts +2 -2
  117. package/src/daemon/lifecycle.ts +14 -1
  118. package/src/daemon/process-message.ts +0 -13
  119. package/src/daemon/windows-compiled-entry.ts +4 -0
  120. package/src/documents/document-store.ts +5 -235
  121. package/src/hooks/types.ts +5 -0
  122. package/src/ipc/gateway-flag-listener.ts +17 -3
  123. package/src/live-voice/__tests__/live-voice-flux-turn-end.test.ts +118 -0
  124. package/src/live-voice/live-voice-manager.ts +16 -3
  125. package/src/live-voice/live-voice-session.ts +63 -9
  126. package/src/live-voice/windows-compiled-live-voice.ts +4 -0
  127. package/src/monitoring/control.ts +1 -0
  128. package/src/monitoring/db-integrity-sample.ts +4 -5
  129. package/src/notifications/AGENTS.md +2 -0
  130. package/src/notifications/approval-card-data.ts +33 -0
  131. package/src/permissions/prompter.ts +1 -5
  132. package/src/persistence/conversation-queries.ts +66 -16
  133. package/src/persistence/embeddings/qdrant-manager.ts +84 -49
  134. package/src/persistence/migrations/360-add-document-workspace-path.ts +5 -14
  135. package/src/persistence/schema/documents.ts +4 -4
  136. package/src/plugin-api/constants.ts +8 -0
  137. package/src/plugin-api/index.ts +5 -1
  138. package/src/plugin-api/webhook-url.ts +13 -11
  139. package/src/plugins/defaults/main.ts +6 -7
  140. package/src/plugins/defaults/memory/graph/__tests__/conversation-graph-memory-v2-routing.test.ts +77 -0
  141. package/src/plugins/defaults/memory/graph/conversation-graph-memory.ts +24 -6
  142. package/src/plugins/defaults/memory/hooks/user-prompt-submit.ts +53 -5
  143. package/src/plugins/defaults/memory/memory-retrospective-job.ts +4 -4
  144. package/src/plugins/defaults/memory/v3/__tests__/injection.test.ts +18 -0
  145. package/src/plugins/defaults/memory/v3/__tests__/shadow-plugin.test.ts +66 -1
  146. package/src/plugins/defaults/memory/v3/injector.ts +8 -0
  147. package/src/plugins/defaults/memory/v3/shadow-plugin.ts +23 -9
  148. package/src/plugins/defaults/memory/worker-control.ts +1 -0
  149. package/src/plugins/defaults/worker-entrypoints.ts +3 -0
  150. package/src/plugins/mtime-cache.ts +17 -0
  151. package/src/prompts/templates/system-sections.ts +0 -7
  152. package/src/providers/__tests__/context-overflow-error.test.ts +24 -0
  153. package/src/providers/__tests__/retry-callsite.test.ts +20 -0
  154. package/src/providers/openai/chat-completions-provider.ts +11 -1
  155. package/src/providers/speech-to-text/__tests__/deepgram-flux-realtime.test.ts +11 -6
  156. package/src/providers/speech-to-text/deepgram-flux-realtime.ts +9 -49
  157. package/src/routes/control.ts +1 -0
  158. package/src/routes/route-host-client.ts +1 -0
  159. package/src/runtime/AGENTS.md +16 -17
  160. package/src/runtime/agent-wake.ts +15 -12
  161. package/src/runtime/routes/__tests__/conversation-list-routes.test.ts +170 -1
  162. package/src/runtime/routes/__tests__/schedule-routes-disarm-reason.test.ts +215 -0
  163. package/src/runtime/routes/conversation-list-routes.ts +54 -22
  164. package/src/runtime/routes/conversation-management-routes.ts +2 -3
  165. package/src/runtime/routes/conversation-routes.ts +11 -13
  166. package/src/runtime/routes/documents-routes.ts +3 -222
  167. package/src/runtime/routes/playground/__tests__/inject-failures.test.ts +2 -0
  168. package/src/runtime/routes/playground/__tests__/reset-circuit.test.ts +3 -0
  169. package/src/runtime/routes/playground/inject-failures.ts +2 -2
  170. package/src/runtime/routes/playground/reset-circuit.ts +1 -1
  171. package/src/runtime/routes/schedule-routes.ts +94 -7
  172. package/src/runtime/routes/workspace-routes.ts +0 -9
  173. package/src/runtime/routes/workspace-utils.ts +3 -13
  174. package/src/runtime/services/conversation-serializer.ts +7 -2
  175. package/src/schedule/__tests__/plugin-schedule-declarations.test.ts +68 -5
  176. package/src/schedule/__tests__/plugin-schedule-reconciler.test.ts +81 -0
  177. package/src/schedule/plugin-schedule-availability.ts +58 -0
  178. package/src/schedule/plugin-schedule-declarations.ts +23 -27
  179. package/src/schedule/plugin-schedule-reconciler.ts +12 -3
  180. package/src/schedule/schedule-store.ts +5 -1
  181. package/src/schedule/scheduler.ts +9 -4
  182. package/src/schedule/worker-control.ts +1 -0
  183. package/src/subagent/__tests__/consult-prompt.test.ts +26 -15
  184. package/src/subagent/consult-context.ts +11 -11
  185. package/src/subagent/consult-prompt.ts +26 -35
  186. package/src/subagent/manager.ts +20 -37
  187. package/src/subagent/notify.ts +7 -1
  188. package/src/subagent/types.ts +15 -13
  189. package/src/tools/__tests__/tool-input-schemas.test.ts +7 -7
  190. package/src/tools/acp/spawn.ts +6 -4
  191. package/src/tools/filesystem/read.ts +27 -10
  192. package/src/tools/host-filesystem/read.ts +27 -15
  193. package/src/tools/shared/filesystem/file-ops-service.ts +63 -35
  194. package/src/tools/shared/filesystem/legacy-read-args.ts +22 -0
  195. package/src/tools/shared/filesystem/types.ts +5 -5
  196. package/src/tools/skills/scaffold-managed.ts +25 -7
  197. package/src/tools/subagent/spawn.ts +28 -88
  198. package/src/tools/ui-surface/surface-shape-docs.ts +1 -1
  199. package/src/util/__tests__/worker-process-command.test.ts +37 -0
  200. package/src/util/logger.ts +16 -0
  201. package/src/util/worker-process.ts +37 -4
  202. package/src/windows-compiled-cli.ts +32 -0
  203. package/src/windows-compiled-entry.ts +4 -0
  204. package/src/windows-compiled-logger.ts +6 -0
  205. package/src/windows-compiled-worker-entry.ts +29 -0
  206. package/src/__tests__/document-workspace-file.test.ts +0 -467
  207. package/src/daemon/interactive-turn-sender.ts +0 -59
  208. package/src/subagent/__tests__/consult-transcript.test.ts +0 -184
  209. package/src/subagent/consult-transcript.ts +0 -90
@@ -431,3 +431,115 @@ describe("sendToClient receives state signals", () => {
431
431
  });
432
432
  });
433
433
  });
434
+
435
+ describe("the sink is fixed for the conversation's life", () => {
436
+ test("a cold instance delivers activity state with no turn ever having run", () => {
437
+ // The summarize-up-to route resolves its conversation outside the send
438
+ // path and emits a thinking indicator before any turn: it must reach the
439
+ // sink the conversation was built with, not depend on a send having wired
440
+ // one (LUM-2987).
441
+ const clientMsgs: AssistantEvent[] = [];
442
+ const conversation = makeConversation((msg) => clientMsgs.push(msg));
443
+
444
+ conversation.emitActivityState("thinking", "context_compacting", {
445
+ statusText: "Summarizing conversation",
446
+ });
447
+
448
+ const activity = clientMsgs.filter(
449
+ (m) => m.type === "assistant_activity_state",
450
+ );
451
+ expect(activity).toHaveLength(1);
452
+ expect((activity[0] as { statusText?: string }).statusText).toBe(
453
+ "Summarizing conversation",
454
+ );
455
+ });
456
+
457
+ test("presence is a property of the turn, not of delivery", () => {
458
+ // Nothing is in flight, so no human is asserted present, even though the
459
+ // sink is live and delivering.
460
+ const clientMsgs: AssistantEvent[] = [];
461
+ const conversation = makeConversation((msg) => clientMsgs.push(msg));
462
+ expect(conversation.hasNoClient).toBe(true);
463
+ conversation.emitActivityState("thinking", "message_dequeued");
464
+ expect(clientMsgs).toHaveLength(1);
465
+ expect(conversation.hasNoClient).toBe(true);
466
+ });
467
+ });
468
+
469
+ describe("event observers", () => {
470
+ test("an observer sees each event after the sink delivered it, and detaches", () => {
471
+ const order: string[] = [];
472
+ const conversation = makeConversation((msg) => {
473
+ order.push(`sink:${msg.type}`);
474
+ });
475
+ const detach = conversation.addEventObserver((msg) => {
476
+ order.push(`observer:${msg.type}`);
477
+ });
478
+
479
+ conversation.emitActivityState("thinking", "message_dequeued");
480
+ expect(order).toEqual([
481
+ "sink:assistant_activity_state",
482
+ "observer:assistant_activity_state",
483
+ ]);
484
+
485
+ detach();
486
+ conversation.emitActivityState("idle", "message_complete");
487
+ expect(order).toEqual([
488
+ "sink:assistant_activity_state",
489
+ "observer:assistant_activity_state",
490
+ "sink:assistant_activity_state",
491
+ ]);
492
+ });
493
+
494
+ test("a throwing observer neither blocks the sink nor its siblings", () => {
495
+ const seen: string[] = [];
496
+ const conversation = makeConversation((msg) => {
497
+ seen.push(`sink:${msg.type}`);
498
+ });
499
+ conversation.addEventObserver(() => {
500
+ throw new Error("observer boom");
501
+ });
502
+ conversation.addEventObserver((msg) => {
503
+ seen.push(`second:${msg.type}`);
504
+ });
505
+
506
+ conversation.emitActivityState("thinking", "message_dequeued");
507
+ expect(seen).toEqual([
508
+ "sink:assistant_activity_state",
509
+ "second:assistant_activity_state",
510
+ ]);
511
+ });
512
+
513
+ test("the prompter's confirmation_request reaches observers after the sink", () => {
514
+ // Voice auto-resolves approval prompts it has no UI for; it must observe
515
+ // the request only once the sink has put it on the wire, so its resolution
516
+ // lands after the request it answers.
517
+ const order: string[] = [];
518
+ const conversation = makeConversation((msg) => {
519
+ order.push(`sink:${msg.type}`);
520
+ });
521
+ conversation.addEventObserver((msg) => {
522
+ if (msg.type === "confirmation_request") {
523
+ order.push("observer:confirmation_request");
524
+ conversation.handleConfirmationResponse(msg.requestId, "deny", {
525
+ emissionContext: { source: "system" },
526
+ });
527
+ }
528
+ });
529
+
530
+ void conversation.prompter.prompt(
531
+ "bash",
532
+ { command: "rm -rf /tmp/x" },
533
+ "high",
534
+ [],
535
+ [],
536
+ undefined,
537
+ conversation.conversationId,
538
+ "sandbox",
539
+ false,
540
+ );
541
+
542
+ expect(order[0]).toBe("sink:confirmation_request");
543
+ expect(order[1]).toBe("observer:confirmation_request");
544
+ });
545
+ });
@@ -713,3 +713,212 @@ describe("loadFromDb tool-result truncation", () => {
713
713
  ).toBe(preStubbed);
714
714
  });
715
715
  });
716
+
717
+ /**
718
+ * A guardian card is written into the conversation the request came from by
719
+ * the notification pairing layer, straight to the DB, while that
720
+ * conversation's turn is parked awaiting the approval. On the next load it
721
+ * lands between the parked turn's `tool_use` and its `tool_result`.
722
+ *
723
+ * The card is recognized by its own `ui_surface` id rather than a stored
724
+ * marker, so rows written before this rule existed are dropped on the same
725
+ * terms as new ones.
726
+ */
727
+ describe("guardian card rows are not replayed as conversation history", () => {
728
+ const baseConversation = {
729
+ id: "conv-1",
730
+ contextSummary: null,
731
+ contextCompactedMessageCount: 0,
732
+ totalInputTokens: 0,
733
+ totalOutputTokens: 0,
734
+ totalEstimatedCost: 0,
735
+ };
736
+
737
+ /** The parked turn: an approval-gated `tool_use` and the result it got. */
738
+ const toolUseRow = {
739
+ id: "m-use",
740
+ role: "assistant",
741
+ content: [
742
+ { type: "text", text: "Running that now." },
743
+ { type: "tool_use", id: "tu_1", name: "bash", input: { cmd: "ls" } },
744
+ ],
745
+ };
746
+ const toolResultRow = {
747
+ id: "m-result",
748
+ role: "user",
749
+ content: [
750
+ { type: "tool_result", tool_use_id: "tu_1", content: "REAL RESULT" },
751
+ ],
752
+ };
753
+
754
+ /** What `buildApprovalCardBlocks` persists: the surface, then its text. */
755
+ function cardRow(surfaceId: string) {
756
+ return {
757
+ id: "m-card",
758
+ role: "assistant",
759
+ content: [
760
+ {
761
+ type: "ui_surface",
762
+ surfaceId,
763
+ surfaceType: "card",
764
+ title: "Tool Approval",
765
+ data: {},
766
+ display: "inline",
767
+ },
768
+ { type: "text", text: "Tool Approval\nbash - requested by Alice" },
769
+ ],
770
+ };
771
+ }
772
+
773
+ function allBlocks(messages: Message[]) {
774
+ return messages.flatMap((m) => m.content);
775
+ }
776
+
777
+ function toolResultsFor(messages: Message[], toolUseId: string) {
778
+ return allBlocks(messages).filter(
779
+ (b) => b.type === "tool_result" && b.tool_use_id === toolUseId,
780
+ );
781
+ }
782
+
783
+ function serialized(value: unknown): string {
784
+ return JSON.stringify(value);
785
+ }
786
+
787
+ beforeEach(() => {
788
+ nextMockMessageId = 1;
789
+ mockConversation = { ...baseConversation };
790
+ });
791
+
792
+ // Sensitivity check for the drop below: an ordinary assistant turn in the
793
+ // same position still corrupts the pair. If the rule ever widened past
794
+ // guardian cards, this test fails first and says so.
795
+ test("an ordinary assistant turn between a tool_use and its result still corrupts it", async () => {
796
+ mockDbMessages = [
797
+ toolUseRow,
798
+ {
799
+ id: "m-card",
800
+ role: "assistant",
801
+ content: [{ type: "text", text: "Still working on it." }],
802
+ },
803
+ toolResultRow,
804
+ ];
805
+
806
+ const conversation = makeConversation();
807
+ await conversation.loadFromDb();
808
+ const messages = conversation.getMessages();
809
+
810
+ expect(serialized(messages)).toContain("tool result missing from history");
811
+ expect(serialized(messages)).toContain("[orphaned tool_result for tu_1]");
812
+ expect(serialized(toolResultsFor(messages, "tu_1"))).not.toContain(
813
+ "REAL RESULT",
814
+ );
815
+ });
816
+
817
+ test("a guardian card is dropped and the real tool_result survives", async () => {
818
+ mockDbMessages = [
819
+ toolUseRow,
820
+ cardRow("tool-approval-req-1"),
821
+ toolResultRow,
822
+ ];
823
+
824
+ const conversation = makeConversation();
825
+ await conversation.loadFromDb();
826
+ const messages = conversation.getMessages();
827
+
828
+ // The pair is adjacent and intact: no stub, no downgrade, and exactly one
829
+ // tool_result for tu_1 carrying the tool's own output.
830
+ expect(serialized(messages)).not.toContain(
831
+ "tool result missing from history",
832
+ );
833
+ expect(serialized(messages)).not.toContain("[orphaned tool_result");
834
+ const results = toolResultsFor(messages, "tu_1");
835
+ expect(results).toHaveLength(1);
836
+ expect(results[0].type === "tool_result" && results[0].content).toBe(
837
+ "REAL RESULT",
838
+ );
839
+ expect(serialized(messages)).not.toContain("Tool Approval");
840
+ expect(messages).toHaveLength(2);
841
+ });
842
+
843
+ test("an access-request card is dropped on the same terms", async () => {
844
+ mockDbMessages = [
845
+ toolUseRow,
846
+ cardRow("access-request-req-9"),
847
+ toolResultRow,
848
+ ];
849
+
850
+ const conversation = makeConversation();
851
+ await conversation.loadFromDb();
852
+ const messages = conversation.getMessages();
853
+
854
+ expect(serialized(messages)).not.toContain("[orphaned tool_result");
855
+ expect(messages).toHaveLength(2);
856
+ });
857
+
858
+ test("a card as the tail row leaves history ending on a user message", async () => {
859
+ // The prefill shape: the approval was delivered and the turn never
860
+ // resumed, so the card is the last row. Replayed, it makes the history
861
+ // end on an assistant turn, which extended-thinking models reject with
862
+ // "does not support assistant message prefill".
863
+ mockDbMessages = [toolUseRow, cardRow("tool-approval-req-1")];
864
+
865
+ const conversation = makeConversation();
866
+ await conversation.loadFromDb();
867
+ const messages = conversation.getMessages();
868
+
869
+ expect(messages[messages.length - 1].role).toBe("user");
870
+ // The tail is repair's fill for the still-unanswered tool_use, which is
871
+ // correct here: the tool genuinely never ran.
872
+ expect(toolResultsFor(messages, "tu_1")).toHaveLength(1);
873
+ });
874
+
875
+ test("a dropped card still restores its ui_surface for action routing", async () => {
876
+ // Surface lifecycle and model context are different questions. The card is
877
+ // absent from history, but its Approve/Reject buttons must keep routing
878
+ // after a restart, which `findConversationBySurfaceId` resolves through
879
+ // surfaceState rebuilt at load.
880
+ mockDbMessages = [toolUseRow, cardRow("tool-approval-req-1")];
881
+
882
+ const conversation = makeConversation();
883
+ await conversation.loadFromDb();
884
+
885
+ expect(serialized(conversation.getMessages())).not.toContain(
886
+ "tool-approval-req-1",
887
+ );
888
+ expect(
889
+ (
890
+ conversation as unknown as { surfaceState: Map<string, unknown> }
891
+ ).surfaceState.has("tool-approval-req-1"),
892
+ ).toBe(true);
893
+ });
894
+
895
+ test("an assistant turn carrying a non-approval surface stays in history", async () => {
896
+ // A wake card is unshifted onto a real turn's own content. Its surface id
897
+ // uses a different prefix, and dropping that row would discard speech.
898
+ mockDbMessages = [
899
+ { id: "m-ask", role: "user", content: [{ type: "text", text: "hi" }] },
900
+ {
901
+ id: "m-wake",
902
+ role: "assistant",
903
+ content: [
904
+ {
905
+ type: "ui_surface",
906
+ surfaceId: "wake-conv-1-1700000000",
907
+ surfaceType: "card",
908
+ title: "Conversation Woke",
909
+ data: {},
910
+ display: "inline",
911
+ },
912
+ { type: "text", text: "Picking this back up." },
913
+ ],
914
+ },
915
+ ];
916
+
917
+ const conversation = makeConversation();
918
+ await conversation.loadFromDb();
919
+
920
+ expect(serialized(conversation.getMessages())).toContain(
921
+ "Picking this back up.",
922
+ );
923
+ });
924
+ });
@@ -35,7 +35,7 @@ describe("live-call question provenance", () => {
35
35
 
36
36
  test("a row persisted after the turn settled carries the owner, not the stale turn actor", async () => {
37
37
  registerConversationNotifiers("conv-n1", {
38
- sendToClient: () => {},
38
+ emit: () => {},
39
39
  messages: [],
40
40
  // Voice cleanup restored the owner; the per-turn field still holds the
41
41
  // finished voice turn's contact.
@@ -11,7 +11,6 @@ import type {
11
11
  } from "../agent/loop.js";
12
12
  import type { AssistantEvent } from "../api/index.js";
13
13
  import type { Message, ProviderResponse } from "../providers/types.js";
14
- import { broadcastMessage } from "../runtime/assistant-event-hub.js";
15
14
  import { stampAndBuffer } from "../runtime/assistant-stream-state.js";
16
15
  import { createAbortReason } from "../util/abort-reasons.js";
17
16
  import { setConfig } from "./helpers/set-config.js";
@@ -2989,6 +2988,9 @@ describe("Conversation host attachment directives", () => {
2989
2988
  attachments: [],
2990
2989
  onEvent: (e) => events.push(e),
2991
2990
  requestId: "req-1",
2991
+ // A host attachment read prompts only when a human is present to
2992
+ // answer; presence is declared per turn.
2993
+ isInteractive: true,
2992
2994
  });
2993
2995
  await waitForPendingRun(1);
2994
2996
 
@@ -3059,6 +3061,9 @@ describe("Conversation host attachment directives", () => {
3059
3061
  attachments: [],
3060
3062
  onEvent: (e) => events.push(e),
3061
3063
  requestId: "req-1",
3064
+ // A host attachment read prompts only when a human is present to
3065
+ // answer; presence is declared per turn.
3066
+ isInteractive: true,
3062
3067
  });
3063
3068
  await waitForPendingRun(1);
3064
3069
 
@@ -3925,13 +3930,20 @@ describe("subagent notification user_message_echo suppression", () => {
3925
3930
  });
3926
3931
  });
3927
3932
 
3928
- describe("interactive drain sender rebind", () => {
3933
+ describe("drained turns deliver through the conversation sink", () => {
3929
3934
  beforeEach(() => {
3930
3935
  pendingRuns = [];
3931
3936
  });
3932
3937
 
3933
- test("a drained interactive message rebinds the hub sender for its turn, then restores the pre-drain state", async () => {
3934
- const conversation = makeConversation();
3938
+ test("a confirmation prompt raised in a drained interactive turn reaches the sink", async () => {
3939
+ // The sink is fixed for the conversation's life, so an interactive turn
3940
+ // drained from the queue delivers exactly like a live one: no per-turn
3941
+ // rebinding, nothing for the previous turn's cleanup to clobber. This is
3942
+ // the invariant behind the acp_spawn permission-prompt timeouts (a queued
3943
+ // interactive turn's confirmation_request used to be emitted into a
3944
+ // reset no-op sender and hang until the permission timeout).
3945
+ const sink: AssistantEvent[] = [];
3946
+ const conversation = makeConversation((msg) => sink.push(msg));
3935
3947
  await conversation.loadFromDb();
3936
3948
 
3937
3949
  const p1 = conversation.processMessage({
@@ -3942,78 +3954,46 @@ describe("interactive drain sender rebind", () => {
3942
3954
  });
3943
3955
  await waitForPendingRun(1);
3944
3956
 
3945
- // Queue an interactive item the way the /v1/messages route does when the
3946
- // conversation is busy.
3947
- const result = conversation.enqueueMessage({
3957
+ conversation.enqueueMessage({
3948
3958
  content: "msg-2",
3949
3959
  requestId: "req-2",
3950
3960
  onEvent: () => {},
3951
3961
  isInteractive: true,
3952
3962
  });
3953
- expect(result.queued).toBe(true);
3954
-
3955
- // The interactive turn that finishes ahead of the drain resets the
3956
- // conversation-level sender to a no-op (process-message.ts contract).
3957
- // The drained turn must not inherit that dead sender: the
3958
- // PermissionPrompter reads the conversation-level sender, so a
3959
- // confirmation_request raised during the drained turn would otherwise
3960
- // reach no client and hang until the permission timeout.
3961
- conversation.updateClient(() => {}, true);
3962
3963
 
3963
3964
  await resolveRun(0);
3964
3965
  await p1;
3965
3966
  await waitForPendingRun(2);
3966
3967
 
3967
- expect(conversation.getCurrentSender()).toBe(broadcastMessage);
3968
+ // The drained turn is in flight and interactive: presence reads a human.
3968
3969
  expect(conversation.hasNoClient).toBe(false);
3969
3970
 
3970
- await resolveRun(1);
3971
- await waitForCondition(
3972
- () => conversation.getCurrentSender() !== broadcastMessage,
3971
+ // A prompt raised now, mid-drained-turn, must land on the sink.
3972
+ void conversation.prompter.prompt(
3973
+ "acp_spawn",
3974
+ { agent: "claude", task: "t" },
3975
+ "high",
3976
+ [],
3977
+ [],
3978
+ undefined,
3979
+ conversation.conversationId,
3980
+ "host",
3981
+ false,
3982
+ undefined,
3983
+ "tool-use-1",
3973
3984
  );
3974
- expect(conversation.hasNoClient).toBe(true);
3975
- });
3976
-
3977
- test("a drained interactive message restores a live pre-drain binding instead of clearing it", async () => {
3978
- const conversation = makeConversation();
3979
- await conversation.loadFromDb();
3980
-
3981
- const p1 = conversation.processMessage({
3982
- content: "msg-1",
3983
- attachments: [],
3984
- onEvent: () => {},
3985
- requestId: "req-1",
3986
- });
3987
- await waitForPendingRun(1);
3988
-
3989
- conversation.enqueueMessage({
3990
- content: "msg-2",
3991
- requestId: "req-2",
3992
- onEvent: () => {},
3993
- isInteractive: true,
3994
- });
3995
-
3996
- // A live binding predates the drained turn (the send route installs one
3997
- // when a message is queued behind a non-interactive turn, which performs
3998
- // no restore). The drain's restore must preserve it for out-of-turn
3999
- // producers (call transcript and completion notifiers), not clear it.
4000
- conversation.updateClient(broadcastMessage, false);
4001
-
4002
- await resolveRun(0);
4003
- await p1;
4004
- await waitForPendingRun(2);
4005
-
4006
- expect(conversation.getCurrentSender()).toBe(broadcastMessage);
4007
- expect(conversation.hasNoClient).toBe(false);
3985
+ const prompt = sink.find((e) => e.type === "confirmation_request");
3986
+ expect(prompt).toBeDefined();
3987
+ expect((prompt as { toolName: string }).toolName).toBe("acp_spawn");
3988
+ // Settle it so the prompt's timer does not outlive the test.
3989
+ conversation.prompter.denyAllPending();
4008
3990
 
4009
3991
  await resolveRun(1);
4010
- // The drained turn has ended; the pre-drain live binding must survive.
4011
- await waitForCondition(() => !conversation.isProcessing());
4012
- expect(conversation.getCurrentSender()).toBe(broadcastMessage);
4013
- expect(conversation.hasNoClient).toBe(false);
3992
+ // Turn over: nothing is in flight, so no human is asserted present.
3993
+ await waitForCondition(() => conversation.hasNoClient);
4014
3994
  });
4015
3995
 
4016
- test("a drained non-interactive message leaves the no-op sender in place", async () => {
3996
+ test("a drained non-interactive turn asserts no human present", async () => {
4017
3997
  const conversation = makeConversation();
4018
3998
  await conversation.loadFromDb();
4019
3999
 
@@ -4032,13 +4012,10 @@ describe("interactive drain sender rebind", () => {
4032
4012
  isInteractive: false,
4033
4013
  });
4034
4014
 
4035
- conversation.updateClient(() => {}, true);
4036
-
4037
4015
  await resolveRun(0);
4038
4016
  await p1;
4039
4017
  await waitForPendingRun(2);
4040
4018
 
4041
- expect(conversation.getCurrentSender()).not.toBe(broadcastMessage);
4042
4019
  expect(conversation.hasNoClient).toBe(true);
4043
4020
 
4044
4021
  await resolveRun(1);
@@ -149,7 +149,7 @@ function createFakeConversation(conversationId: string): Conversation {
149
149
  return this.turnInterfaceContext;
150
150
  },
151
151
  ensureActorScopedHistory: async () => {},
152
- updateClient: () => {},
152
+ replayActivityState: () => {},
153
153
 
154
154
  setHostCuProxy(this: { hostCuProxy: unknown }, proxy: unknown) {
155
155
  this.hostCuProxy = proxy;
@@ -158,7 +158,7 @@ function makeConversation() {
158
158
  currentRequestId: undefined,
159
159
  queue: { length: 0 },
160
160
  setTrustContext: () => {},
161
- updateClient: () => {},
161
+ replayActivityState: () => {},
162
162
  emitConfirmationStateChanged: () => {},
163
163
  emitActivityState: () => {},
164
164
  setTurnChannelContext: () => {},
@@ -163,7 +163,7 @@ describe("handleSendMessage guardian reply interception", () => {
163
163
  const runAgentLoop = mock(async () => undefined);
164
164
  const session = {
165
165
  setTrustContext: () => {},
166
- updateClient: () => {},
166
+ replayActivityState: () => {},
167
167
  emitConfirmationStateChanged: () => {},
168
168
  emitActivityState: () => {},
169
169
  setTurnChannelContext: () => {},
@@ -253,7 +253,7 @@ describe("handleSendMessage guardian reply interception", () => {
253
253
  const runAgentLoop = mock(async () => undefined);
254
254
  const session = {
255
255
  setTrustContext: () => {},
256
- updateClient: () => {},
256
+ replayActivityState: () => {},
257
257
  emitConfirmationStateChanged: () => {},
258
258
  emitActivityState: () => {},
259
259
  setTurnChannelContext: () => {},
@@ -331,7 +331,7 @@ describe("handleSendMessage guardian reply interception", () => {
331
331
  const runAgentLoop = mock(async () => undefined);
332
332
  const session = {
333
333
  setTrustContext: () => {},
334
- updateClient: () => {},
334
+ replayActivityState: () => {},
335
335
  emitConfirmationStateChanged: () => {},
336
336
  emitActivityState: () => {},
337
337
  setTurnChannelContext: () => {},
@@ -419,7 +419,7 @@ describe("handleSendMessage guardian reply interception", () => {
419
419
  const runAgentLoop = mock(async () => undefined);
420
420
  const session = {
421
421
  setTrustContext: () => {},
422
- updateClient: () => {},
422
+ replayActivityState: () => {},
423
423
  emitConfirmationStateChanged: () => {},
424
424
  emitActivityState: () => {},
425
425
  setTurnChannelContext: () => {},
@@ -507,7 +507,7 @@ describe("handleSendMessage guardian reply interception", () => {
507
507
  const runAgentLoop = mock(async () => undefined);
508
508
  const session = {
509
509
  setTrustContext: () => {},
510
- updateClient: () => {},
510
+ replayActivityState: () => {},
511
511
  emitConfirmationStateChanged: () => {},
512
512
  emitActivityState: () => {},
513
513
  setTurnChannelContext: () => {},
@@ -590,7 +590,7 @@ describe("handleSendMessage guardian reply interception", () => {
590
590
  const runAgentLoop = mock(async () => undefined);
591
591
  const session = {
592
592
  setTrustContext: () => {},
593
- updateClient: () => {},
593
+ replayActivityState: () => {},
594
594
  emitConfirmationStateChanged: () => {},
595
595
  emitActivityState: () => {},
596
596
  setTurnChannelContext: () => {},
@@ -667,7 +667,7 @@ describe("handleSendMessage guardian reply interception", () => {
667
667
  const runAgentLoop = mock(async () => undefined);
668
668
  const session = {
669
669
  setTrustContext: () => {},
670
- updateClient: () => {},
670
+ replayActivityState: () => {},
671
671
  emitConfirmationStateChanged: () => {},
672
672
  emitActivityState: () => {},
673
673
  setTurnChannelContext: () => {},
@@ -746,7 +746,7 @@ describe("handleSendMessage guardian reply interception", () => {
746
746
  const runAgentLoop = mock(async () => undefined);
747
747
  const session = {
748
748
  setTrustContext: () => {},
749
- updateClient: () => {},
749
+ replayActivityState: () => {},
750
750
  emitConfirmationStateChanged: () => {},
751
751
  emitActivityState: () => {},
752
752
  setTurnChannelContext: () => {},
@@ -826,7 +826,7 @@ describe("handleSendMessage guardian reply interception", () => {
826
826
  const runAgentLoop = mock(async () => undefined);
827
827
  const session = {
828
828
  setTrustContext: () => {},
829
- updateClient: () => {},
829
+ replayActivityState: () => {},
830
830
  emitConfirmationStateChanged: () => {},
831
831
  emitActivityState: () => {},
832
832
  setTurnChannelContext: () => {},
@@ -169,7 +169,7 @@ function makeConversationWithPendingConfirmation(
169
169
  setTrustContext(this: { trustContext: unknown }, ctx: unknown) {
170
170
  this.trustContext = ctx;
171
171
  },
172
- updateClient: () => {},
172
+ replayActivityState: () => {},
173
173
  emitConfirmationStateChanged: () => {},
174
174
  emitActivityState: () => {},
175
175
  setTurnChannelContext: () => {},
@@ -233,7 +233,7 @@ function makeConversation() {
233
233
  currentRequestId: undefined,
234
234
  queue: { length: 0 },
235
235
  setTrustContext: () => {},
236
- updateClient: (_fn: unknown, _b: boolean) => {},
236
+ replayActivityState: () => {},
237
237
  emitConfirmationStateChanged: () => {},
238
238
  emitActivityState: () => {},
239
239
  setTurnChannelContext: () => {},