import { readFile } from "node:fs/promises"; import { createServer, type Server } from "node:http"; import type { AddressInfo } from "node:net"; import { TID_BROWSER_ADDRESS_INPUT, TID_BROWSER_WEBVIEW } from "@zcode/shared/test-ids"; import { clearAppData } from "#e2e/helpers/desktop-app"; import type { E2ENetworkCaptureArtifact } from "#e2e/helpers/network-capture-proxy"; import { prepareV4ConversationE2E, sendV4Prompt, waitForV4AssistantMessageContaining, waitForV4ConversationState, waitForV4UserMessageContaining, } from "#e2e/helpers/v4-conversation"; import { waitForUpstreamRequest } from "#e2e/helpers/conversation-session-network"; import { waitForToolCallBlockByToolName } from "#e2e/helpers/conversation-session-tool"; import { ensureToolCrossProductFullAccessMode } from "#e2e/helpers/conversation-session-tool-cross-product"; const PARENT_MARKER = "E2E_BROWSER_SUBAGENT_BOUNDARY_PARENT"; const CHILD_MARKER = "E2E_BROWSER_SUBAGENT_BOUNDARY_CHILD"; const PARENT_INIT_TOOL_ID = "toolu_e2e_browser_subagent_parent_init"; const AGENT_TOOL_ID = "toolu_e2e_browser_subagent_agent"; const CHILD_CHECK_TOOL_ID = "toolu_e2e_browser_subagent_child_check"; const PARENT_RESUME_TOOL_ID = "toolu_e2e_browser_subagent_parent_resume"; const PARENT_INIT_OK = "E2E_BROWSER_SUBAGENT_PARENT_INIT_OK"; const CHILD_CHECKS_OK = "E2E_BROWSER_SUBAGENT_BOUNDARY_CHECKS_OK"; const CHILD_DONE = "E2E_BROWSER_SUBAGENT_BOUNDARY_CHILD_DONE"; const PARENT_RESUME_OK = "E2E_BROWSER_SUBAGENT_PARENT_RESUME_OK"; const PARENT_DONE = "E2E_BROWSER_SUBAGENT_BOUNDARY_DONE"; interface ParentBrowserSurface { address: string; id: number; } let fixtureServer: Server | null = null; let fixtureRootUrl = ""; describe("Browser Use subagent 会话 E2E", () => { before(async () => { fixtureServer = createFixtureServer(); fixtureRootUrl = await listen(fixtureServer); }); after(async () => { await closeServer(fixtureServer); fixtureServer = null; await browser.electron.restoreAllMocks(); await clearAppData(); }); it("BAB01/BU-E2E-012: child 与对话共用 Tab,新开的 Tab 在面板展开且结束后保留", async function () { this.timeout(300000); await prepareV4ConversationE2E(); await ensureToolCrossProductFullAccessMode(); const runId = Date.now(); const prompt = [ `${PARENT_MARKER}_${runId}: cache one Browser and Tab, then delegate a browser check to a general-purpose child.`, `E2E_BROWSER_FIXTURE_URL:${fixtureRootUrl}`, `The child marker is ${CHILD_MARKER}; after it returns, reuse the original parent tab and reply ${PARENT_DONE}.`, ].join(" "); await sendV4Prompt(prompt); await waitForV4UserMessageContaining(`${PARENT_MARKER}_${runId}`); await waitForUpstreamRequest( { includes: [PARENT_MARKER, PARENT_INIT_TOOL_ID, PARENT_INIT_OK], excludes: ["Generate a concise title", "CRITICAL: Respond with TEXT ONLY"], }, "主 Agent 初始化 Browser 后没有进入 Agent delegation continuation", 90000, ); const beforeChild = await waitForParentBrowserSurface(); await waitForToolCallBlockByToolName("Agent", 90000); await waitForUpstreamRequest( { includes: [CHILD_MARKER, CHILD_CHECK_TOOL_ID, CHILD_CHECKS_OK, "ordinaryValue", "42"], excludes: [PARENT_MARKER, "Generate a concise title", "CRITICAL: Respond with TEXT ONLY"], }, "child 普通 Node REPL 与自有 Browser Tab 检查没有完成", 90000, ); const childRequestText = JSON.stringify( await waitForCapturedRequest([CHILD_MARKER, CHILD_CHECK_TOOL_ID, CHILD_CHECKS_OK]), ); expect(childRequestText).not.toContain("Browser is not available in subagent"); // child 开的 Tab 归当前对话:面板直接展示它,而不是挂成看不见的孤儿 shell。 await waitForBrowserSurface((surface) => surface.address.includes("child=1")); await waitForUpstreamRequest( { includes: [PARENT_MARKER, AGENT_TOOL_ID, CHILD_DONE], excludes: ["Generate a concise title", "CRITICAL: Respond with TEXT ONLY"], }, "child 完成后结果没有回到父 Agent continuation", 90000, ); await waitForUpstreamRequest( { includes: [PARENT_MARKER, PARENT_RESUME_TOOL_ID, PARENT_RESUME_OK], excludes: ["Generate a concise title", "CRITICAL: Respond with TEXT ONLY"], }, "父 Agent 返回后没有复用缓存 Tab 完成写入", 90000, ); await waitForV4AssistantMessageContaining(PARENT_DONE); await waitForV4ConversationState( (snapshot) => snapshot.state === "idle" && snapshot.queueCount === 0, "subagent Browser case 完成后没有回到 idle", 90000, ); expect(beforeChild.id).toBeGreaterThan(0); }); }); function createFixtureServer(): Server { return createServer((_request, response) => { response.setHeader("content-type", "text/html; charset=utf-8"); response.end(`
empty
`); }); } async function listen(server: Server): Promise