incoming-message-producers.test.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import { describe, expect, it, vi } from "vitest";
  2. import { MessageHistoryImpl } from "../src/agent/message-history.js";
  3. import { createSubagentMessageSink } from "../src/subagent/message-steering.js";
  4. import { persistSubagentMessageCommand } from "../src/runtime/methods/subagent-messages.js";
  5. import { persistBackgroundTaskNotificationBatch } from "../src/runtime/methods/background-notifications.js";
  6. import { formatTaskNotification } from "../src/runtime-task/notification.js";
  7. function runtime() {
  8. return {
  9. sessionId: "parent",
  10. ensureContextInitialized: vi.fn(),
  11. messageHistory: new MessageHistoryImpl(),
  12. persistSyntheticUserNoticeForSession: vi.fn(),
  13. sessionStore: undefined,
  14. };
  15. }
  16. describe("incoming message trusted producers", () => {
  17. it.each([undefined, " summary "])(
  18. "marks coordinator messages independently of summary=%s",
  19. async (summary) => {
  20. const steerTurn = vi.fn(async () => ({
  21. kind: "queued" as const,
  22. pendingInputId: "pending",
  23. queueLength: 1,
  24. turnId: "turn" as never,
  25. }));
  26. const sink = createSubagentMessageSink({ steerTurn }, { traceContext: {} as never });
  27. await sink.send({ id: "message", message: "raw body", summary } as never);
  28. expect(steerTurn).toHaveBeenCalledWith(
  29. expect.objectContaining({
  30. inputPresentation: "coordinator_steer",
  31. // summary 只用于 UI 预览,子 agent 只收到 message。
  32. input: "raw body",
  33. }),
  34. );
  35. },
  36. );
  37. it.each([false, true])("persists peer consumption timing midTurn=%s", async (midTurn) => {
  38. const target = runtime();
  39. await persistSubagentMessageCommand.call(
  40. target as never,
  41. {
  42. text: "<subagent-message>raw</subagent-message>",
  43. traceContext: {},
  44. id: "command",
  45. agentId: "child",
  46. } as never,
  47. midTurn,
  48. );
  49. const marker = midTurn ? "subagent_reply_steer" : "subagent_reply";
  50. expect(target.messageHistory.toRuntimeEntries()[0]?.metadata).toEqual({
  51. source: "legacy_synthetic",
  52. inputPresentation: marker,
  53. });
  54. expect(target.persistSyntheticUserNoticeForSession).toHaveBeenCalledWith(
  55. expect.objectContaining({
  56. text: "<subagent-message>raw</subagent-message>",
  57. metadata: expect.objectContaining({ inputPresentation: marker }),
  58. }),
  59. );
  60. });
  61. // 空闲整批的每条通知存为同一条消息的一个文本块,边界不靠正文猜测;这一轮的输入文本仍以 "\n\n" 相连。
  62. // 轮中通知每条单独一个 entry,生产中不会成批,保持原来的连接字符串。
  63. it.each([false, true])(
  64. "keeps a background batch raw with one marker midTurn=%s",
  65. async (midTurn) => {
  66. const target = runtime();
  67. const persisted = await persistBackgroundTaskNotificationBatch.call(
  68. target as never,
  69. [
  70. { id: "1", text: "one", traceContext: {} },
  71. { id: "2", text: "two", traceContext: {} },
  72. ] as never,
  73. midTurn,
  74. );
  75. const content = midTurn
  76. ? "one\n\ntwo"
  77. : [
  78. { type: "text", text: "one" },
  79. { type: "text", text: "two" },
  80. ];
  81. expect(target.messageHistory.toRuntimeEntries()).toEqual([
  82. {
  83. message: { role: "user", content },
  84. metadata: {
  85. source: "legacy_synthetic",
  86. inputPresentation: midTurn ? "task_notification_steer" : "task_notification",
  87. },
  88. },
  89. ]);
  90. expect(target.persistSyntheticUserNoticeForSession).toHaveBeenCalledOnce();
  91. expect(target.persistSyntheticUserNoticeForSession).toHaveBeenCalledWith(
  92. expect.objectContaining({ text: midTurn ? "one\n\ntwo" : ["one", "two"] }),
  93. );
  94. expect(persisted.text).toBe("one\n\ntwo");
  95. },
  96. );
  97. it.each(["local_agent", "local_bash", "local_workflow", "monitor_mcp"] as const)(
  98. "stores only raw notification payload for %s",
  99. (taskType) => {
  100. const text = formatTaskNotification({
  101. taskId: "task",
  102. taskType,
  103. status: "completed",
  104. summary: "done",
  105. });
  106. expect(text).toMatch(/^<task-notification>/);
  107. expect(text).not.toContain("[SYSTEM NOTIFICATION");
  108. },
  109. );
  110. });