| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- import { describe, expect, it } from "vitest";
- import {
- createSessionEvent,
- createSessionId,
- createTurnId,
- SessionEventType,
- type SessionEvent,
- } from "@zcode/contracts";
- import { AgentRuntime } from "../src/runtime.js";
- import { createTestSessionEventStore } from "./test-event-store.js";
- /**
- * 外部子 runtime 的两个窄接缝(dwf actor / legacy script workflow 用它们做 transcript 直播)。
- * 子 runtime 在 class 外构造,所以既拿不到 private eventStore 也调不到 private notifyEventSinks。
- */
- describe("agent runtime external child session seams", () => {
- const parentSessionId = createSessionId("parent-session");
- const childSessionId = createSessionId("child-session");
- function childEvent(): SessionEvent {
- return createSessionEvent(
- SessionEventType.SessionTitleUpdated,
- childSessionId,
- { previousTitle: "", source: "first_input", title: "workflow actor a#1@1" },
- { sequenceNumber: 1, traceId: "trace-child", turnId: createTurnId("turn-child") },
- );
- }
- it("交出的是本 runtime 自己的 event store(供子 runtime 共享)", () => {
- const eventStore = createTestSessionEventStore();
- const runtime = new AgentRuntime(parentSessionId, {}, { eventStore });
- expect(runtime.getSessionEventStore()).toBe(eventStore);
- });
- it("外部子会话事件扇出到外部 sinks,保留子 sessionId", async () => {
- const eventStore = createTestSessionEventStore();
- const constructorSinkEvents: SessionEvent[] = [];
- const subscribedSinkEvents: SessionEvent[] = [];
- const runtime = new AgentRuntime(
- parentSessionId,
- {},
- {
- eventStore,
- eventSink: {
- onSessionEvent: (event) => {
- constructorSinkEvents.push(event);
- },
- },
- },
- );
- runtime.subscribeEvents({
- onSessionEvent: (event) => {
- subscribedSinkEvents.push(event);
- },
- });
- const event = childEvent();
- await runtime.notifyExternalChildSessionEvent({ childSessionId, event });
- // raw child event,一字不改:协议层按 event.sessionId 路由到 detached live session。
- expect(constructorSinkEvents).toEqual([event]);
- expect(subscribedSinkEvents).toEqual([event]);
- expect(constructorSinkEvents[0]!.sessionId).toBe(childSessionId);
- expect(constructorSinkEvents[0]!.sequenceNumber).toBe(1);
- });
- it("只通知不 append:父会话与子会话在 store 里都没有多出事件", async () => {
- const eventStore = createTestSessionEventStore();
- const runtime = new AgentRuntime(parentSessionId, {}, { eventStore });
- await runtime.notifyExternalChildSessionEvent({ childSessionId, event: childEvent() });
- // 子 runtime 已经按自己的 sessionId 落过库;父侧再 append 就是同一事件的第二份。
- expect(await eventStore.getEvents(parentSessionId)).toHaveLength(0);
- expect(await eventStore.getEvents(childSessionId)).toHaveLength(0);
- });
- it("单个 sink 抛错不影响其余 sink(沿用 notifyEventSinks 的隔离)", async () => {
- const eventStore = createTestSessionEventStore();
- const received: SessionEvent[] = [];
- const runtime = new AgentRuntime(
- parentSessionId,
- {},
- {
- eventStore,
- eventSink: {
- onSessionEvent: () => {
- throw new Error("sink boom");
- },
- },
- },
- );
- runtime.subscribeEvents({
- onSessionEvent: (event) => {
- received.push(event);
- },
- });
- await expect(
- runtime.notifyExternalChildSessionEvent({ childSessionId, event: childEvent() }),
- ).resolves.toBeUndefined();
- expect(received).toHaveLength(1);
- });
- });
|