runtime-external-child-session-events.test.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. createSessionEvent,
  4. createSessionId,
  5. createTurnId,
  6. SessionEventType,
  7. type SessionEvent,
  8. } from "@zcode/contracts";
  9. import { AgentRuntime } from "../src/runtime.js";
  10. import { createTestSessionEventStore } from "./test-event-store.js";
  11. /**
  12. * 外部子 runtime 的两个窄接缝(dwf actor / legacy script workflow 用它们做 transcript 直播)。
  13. * 子 runtime 在 class 外构造,所以既拿不到 private eventStore 也调不到 private notifyEventSinks。
  14. */
  15. describe("agent runtime external child session seams", () => {
  16. const parentSessionId = createSessionId("parent-session");
  17. const childSessionId = createSessionId("child-session");
  18. function childEvent(): SessionEvent {
  19. return createSessionEvent(
  20. SessionEventType.SessionTitleUpdated,
  21. childSessionId,
  22. { previousTitle: "", source: "first_input", title: "workflow actor a#1@1" },
  23. { sequenceNumber: 1, traceId: "trace-child", turnId: createTurnId("turn-child") },
  24. );
  25. }
  26. it("交出的是本 runtime 自己的 event store(供子 runtime 共享)", () => {
  27. const eventStore = createTestSessionEventStore();
  28. const runtime = new AgentRuntime(parentSessionId, {}, { eventStore });
  29. expect(runtime.getSessionEventStore()).toBe(eventStore);
  30. });
  31. it("外部子会话事件扇出到外部 sinks,保留子 sessionId", async () => {
  32. const eventStore = createTestSessionEventStore();
  33. const constructorSinkEvents: SessionEvent[] = [];
  34. const subscribedSinkEvents: SessionEvent[] = [];
  35. const runtime = new AgentRuntime(
  36. parentSessionId,
  37. {},
  38. {
  39. eventStore,
  40. eventSink: {
  41. onSessionEvent: (event) => {
  42. constructorSinkEvents.push(event);
  43. },
  44. },
  45. },
  46. );
  47. runtime.subscribeEvents({
  48. onSessionEvent: (event) => {
  49. subscribedSinkEvents.push(event);
  50. },
  51. });
  52. const event = childEvent();
  53. await runtime.notifyExternalChildSessionEvent({ childSessionId, event });
  54. // raw child event,一字不改:协议层按 event.sessionId 路由到 detached live session。
  55. expect(constructorSinkEvents).toEqual([event]);
  56. expect(subscribedSinkEvents).toEqual([event]);
  57. expect(constructorSinkEvents[0]!.sessionId).toBe(childSessionId);
  58. expect(constructorSinkEvents[0]!.sequenceNumber).toBe(1);
  59. });
  60. it("只通知不 append:父会话与子会话在 store 里都没有多出事件", async () => {
  61. const eventStore = createTestSessionEventStore();
  62. const runtime = new AgentRuntime(parentSessionId, {}, { eventStore });
  63. await runtime.notifyExternalChildSessionEvent({ childSessionId, event: childEvent() });
  64. // 子 runtime 已经按自己的 sessionId 落过库;父侧再 append 就是同一事件的第二份。
  65. expect(await eventStore.getEvents(parentSessionId)).toHaveLength(0);
  66. expect(await eventStore.getEvents(childSessionId)).toHaveLength(0);
  67. });
  68. it("单个 sink 抛错不影响其余 sink(沿用 notifyEventSinks 的隔离)", async () => {
  69. const eventStore = createTestSessionEventStore();
  70. const received: SessionEvent[] = [];
  71. const runtime = new AgentRuntime(
  72. parentSessionId,
  73. {},
  74. {
  75. eventStore,
  76. eventSink: {
  77. onSessionEvent: () => {
  78. throw new Error("sink boom");
  79. },
  80. },
  81. },
  82. );
  83. runtime.subscribeEvents({
  84. onSessionEvent: (event) => {
  85. received.push(event);
  86. },
  87. });
  88. await expect(
  89. runtime.notifyExternalChildSessionEvent({ childSessionId, event: childEvent() }),
  90. ).resolves.toBeUndefined();
  91. expect(received).toHaveLength(1);
  92. });
  93. });