subagent-browser-session.test.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { describe, expect, it, vi } from "vitest";
  2. import { createSessionId, type BrowserControlPort } from "@zcode/contracts";
  3. import { createTestAgentRuntime } from "./test-agent-runtime.js";
  4. import { createTestModelFactory } from "./test-runtime-model.js";
  5. import { createTestSessionEventStore } from "./test-event-store.js";
  6. import { createRecordingSubagentSessionStore } from "./subagent-test-store.js";
  7. import { done, initial, spawn, usage } from "./subagent-profile-scenario.js";
  8. function createPorts() {
  9. const childPort: BrowserControlPort = {
  10. list: vi.fn(async () => []),
  11. execute: vi.fn(async () => ({ ok: true as const, elapsedMs: 0 })),
  12. turnEnded: vi.fn(async () => {}),
  13. closeSession: vi.fn(async () => {}),
  14. };
  15. const parentPort: BrowserControlPort = {
  16. list: vi.fn(async () => []),
  17. execute: vi.fn(async () => ({ ok: true as const, elapsedMs: 0 })),
  18. turnEnded: vi.fn(async () => {}),
  19. closeSession: vi.fn(async () => {}),
  20. forChildSession: vi.fn(() => childPort),
  21. };
  22. return { childPort, parentPort };
  23. }
  24. async function runParentWithChild(browserControlPort: BrowserControlPort) {
  25. const sessionId = createSessionId();
  26. const childSessionIds: string[] = [];
  27. let issued = false;
  28. const runtime = createTestAgentRuntime(
  29. sessionId,
  30. { mode: "yolo", compact: { enabled: false }, titleGeneration: { enabled: false } },
  31. {
  32. browserControlPort,
  33. sessionStore: createRecordingSubagentSessionStore(),
  34. eventStore: createTestSessionEventStore(),
  35. loadAgentDefinitions: vi.fn(async () => ({ activeAgents: [initial()] })),
  36. modelFactory: createTestModelFactory({
  37. async generateText(_request, observation) {
  38. if (observation.invocationContext?.metadata?.querySource === "subagent") {
  39. childSessionIds.push(observation.invocationContext.traceContext!.sessionId!);
  40. return done();
  41. }
  42. if (issued) return done();
  43. issued = true;
  44. return { finishReason: "tool-calls", text: "", usage, toolCalls: [spawn("BROWSE")] };
  45. },
  46. }),
  47. },
  48. );
  49. try {
  50. await runtime.executeTurn("START");
  51. } finally {
  52. runtime.beginShutdown();
  53. }
  54. return { sessionId, childSessionId: childSessionIds[0]! };
  55. }
  56. describe("subagent Browser Use session", () => {
  57. it("以父会话为 tab 归属登记子会话,子代理结束时经子端口撤销登记", async () => {
  58. const { childPort, parentPort } = createPorts();
  59. const { sessionId, childSessionId } = await runParentWithChild(parentPort);
  60. expect(childSessionId).toBeDefined();
  61. expect(childSessionId).not.toBe(sessionId);
  62. expect(parentPort.forChildSession).toHaveBeenCalledWith({
  63. childSessionId,
  64. parentSessionId: sessionId,
  65. tabOwner: "parent",
  66. });
  67. expect(childPort.closeSession).toHaveBeenCalledWith(
  68. expect.objectContaining({ sessionId: childSessionId }),
  69. );
  70. expect(parentPort.closeSession).not.toHaveBeenCalledWith(
  71. expect.objectContaining({ sessionId: childSessionId }),
  72. );
  73. });
  74. it("端口没有 forChildSession 时直接用父端口关闭子会话自己的 session", async () => {
  75. const { parentPort } = createPorts();
  76. delete parentPort.forChildSession;
  77. const { childSessionId } = await runParentWithChild(parentPort);
  78. expect(parentPort.closeSession).toHaveBeenCalledWith(
  79. expect.objectContaining({ sessionId: childSessionId }),
  80. );
  81. });
  82. });