incoming-message-producers.test.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. input: summary ? "summary\n\nraw body" : "raw body",
  32. }),
  33. );
  34. },
  35. );
  36. it.each([false, true])("persists peer consumption timing midTurn=%s", async (midTurn) => {
  37. const target = runtime();
  38. await persistSubagentMessageCommand.call(
  39. target as never,
  40. {
  41. text: "<subagent-message>raw</subagent-message>",
  42. traceContext: {},
  43. id: "command",
  44. agentId: "child",
  45. } as never,
  46. midTurn,
  47. );
  48. const marker = midTurn ? "subagent_reply_steer" : "subagent_reply";
  49. expect(target.messageHistory.toRuntimeEntries()[0]?.metadata).toEqual({
  50. source: "legacy_synthetic",
  51. inputPresentation: marker,
  52. });
  53. expect(target.persistSyntheticUserNoticeForSession).toHaveBeenCalledWith(
  54. expect.objectContaining({
  55. text: "<subagent-message>raw</subagent-message>",
  56. metadata: expect.objectContaining({ inputPresentation: marker }),
  57. }),
  58. );
  59. });
  60. it.each([false, true])(
  61. "keeps a background batch raw with one marker midTurn=%s",
  62. async (midTurn) => {
  63. const target = runtime();
  64. await persistBackgroundTaskNotificationBatch.call(
  65. target as never,
  66. [
  67. { id: "1", text: "one", traceContext: {} },
  68. { id: "2", text: "two", traceContext: {} },
  69. ] as never,
  70. midTurn,
  71. );
  72. expect(target.messageHistory.toRuntimeEntries()).toEqual([
  73. {
  74. message: { role: "user", content: "one\n\ntwo" },
  75. metadata: {
  76. source: "legacy_synthetic",
  77. inputPresentation: midTurn ? "task_notification_steer" : "task_notification",
  78. },
  79. },
  80. ]);
  81. expect(target.persistSyntheticUserNoticeForSession).toHaveBeenCalledOnce();
  82. },
  83. );
  84. it.each(["local_agent", "local_bash", "local_workflow", "monitor_mcp"] as const)(
  85. "stores only raw notification payload for %s",
  86. (taskType) => {
  87. const text = formatTaskNotification({
  88. taskId: "task",
  89. taskType,
  90. status: "completed",
  91. summary: "done",
  92. });
  93. expect(text).toMatch(/^<task-notification>/);
  94. expect(text).not.toContain("[SYSTEM NOTIFICATION");
  95. },
  96. );
  97. });