| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- import { readFile } from "node:fs/promises";
- import {
- TID_CHAT_SUMMARY_PANEL,
- TID_V4_BACKGROUND_WORK_CANCEL,
- TID_V4_BACKGROUND_WORK_ITEM,
- TID_V4_SUBAGENT_OPEN_SIDE_PANE,
- } from "@zcode/shared";
- import { clearAppData } from "../helpers/desktop-app.js";
- import { waitForToolCallBlockByToolName } from "../helpers/conversation-session-tool.js";
- import {
- getUpstreamRequestRecordCount,
- waitForUpstreamRequest,
- } from "../helpers/conversation-session-network.js";
- import type { E2ENetworkCaptureArtifact } from "../helpers/network-capture-proxy.js";
- import { ensureToolCrossProductFullAccessMode } from "../helpers/conversation-session-tool-cross-product.js";
- import {
- clickV4BackgroundWorkCancel,
- getV4BackgroundWorks,
- getV4ComposerBackgroundWorkCounts,
- openV4RunningBackgroundWorks,
- prepareV4ConversationE2E,
- sendV4Prompt,
- waitForLatestV4CommandAck,
- waitForV4AssistantMessageContaining,
- waitForV4ComposerText,
- waitForV4ConversationState,
- } from "../helpers/v4-conversation.js";
- const PARENT_MARKER = "E2E_SEND_MESSAGE_RESUME_STOP_PARENT";
- const FOREGROUND_DONE = "E2E_SEND_MESSAGE_RESUME_STOP_FOREGROUND_DONE";
- const FOREGROUND_PARENT_IDLE = "E2E_SEND_MESSAGE_RESUME_STOP_FOREGROUND_PARENT_IDLE";
- const RESUME_MARKER = "E2E_SEND_MESSAGE_RESUME_STOP_RESUME";
- const CHILD_DONE = "E2E_SEND_MESSAGE_RESUME_STOP_CHILD_DONE";
- const STOP_NOTIFICATION_DONE = "E2E_SEND_MESSAGE_RESUME_STOP_NOTIFICATION_DONE";
- describe("SendMessage resume Stop", () => {
- after(async () => {
- await browser.electron.restoreAllMocks();
- await clearAppData();
- });
- it("SAT12: foreground subagent 完成后由 SendMessage resume 显示 Stop 且可停止", async function () {
- this.timeout(240000);
- await prepareV4ConversationE2E();
- await ensureToolCrossProductFullAccessMode();
- const runId = Date.now();
- const parentRunMarker = `${PARENT_MARKER}_${runId}`;
- await sendV4Prompt(
- [
- `${parentRunMarker}: call Agent exactly once with run_in_background false and subagent_type "general-purpose".`,
- `Tell the child to reply with exactly ${FOREGROUND_DONE}_${runId} and do nothing else.`,
- "After that foreground Agent completes, call no more tools and finish this turn.",
- ].join(" "),
- );
- await waitForV4ComposerText("", "foreground Agent 首发后输入框没有清空");
- await waitForToolCallBlockByToolName("Agent", 90000);
- await waitForV4AssistantMessageContaining(FOREGROUND_PARENT_IDLE, 90000);
- await waitForV4ConversationState(
- (snapshot) => snapshot.state !== "streaming" && !snapshot.stopRequested,
- "foreground Agent 完成后父 turn 没有回到 idle",
- 90000,
- );
- expect((await waitForToolCallBlockByToolName("Agent", 30000)).status).toBe("completed");
- await waitForUpstreamRequest(
- {
- includes: [parentRunMarker, FOREGROUND_DONE, "agentId: agent_"],
- excludes: ["resumed it in the background"],
- },
- "foreground Agent 完成结果没有进入父模型请求",
- 90000,
- );
- // 测试修复原因:resume 会原地更新同一 subagent projection;若等 resume 后再读
- // identity,新建 child 也可能让 row/work 自洽通过。必须在第二个 turn 前固化原身份。
- const foregroundAgentId = await readForegroundAgentId(parentRunMarker);
- const foregroundChildSessionId = await waitForSingleSubagentSummaryChildSessionId(30000);
- const resumeRunMarker = `${RESUME_MARKER}_${runId}`;
- await sendV4Prompt(
- [
- `${resumeRunMarker}: read the agentId from the completed Agent result and call SendMessage exactly once for that same agent.`,
- 'Use summary "resume stopped agent for stop check".',
- `Use this exact message verbatim: You MUST invoke the Bash tool with command "sleep 120". Do not send any text before the Bash tool finishes. After it finishes, reply with exactly ${CHILD_DONE}_${runId}.`,
- "After SendMessage returns, call no more tools and finish this turn without waiting for the resumed agent.",
- ].join(" "),
- );
- await waitForV4ComposerText("", "SendMessage resume 首发后输入框没有清空");
- await waitForToolCallBlockByToolName("SendMessage", 90000);
- await waitForV4ConversationState(
- (snapshot) => snapshot.state !== "streaming" && !snapshot.stopRequested,
- "SendMessage resume 后父 turn 没有回到 idle",
- 90000,
- );
- expect((await waitForToolCallBlockByToolName("SendMessage", 30000)).status).toBe("completed");
- expect(await waitForSingleSubagentSummaryChildSessionId(30000)).toBe(
- foregroundChildSessionId,
- );
- await openV4RunningBackgroundWorks(90000);
- const runningWork = await waitForSingleRunningBackgroundWork(30000);
- expect(runningWork.workId).toBe(foregroundAgentId);
- const control = await readRunningSubagentControl(runningWork.workId);
- expect(control).not.toBeNull();
- expect(control?.kind).toBe("agent");
- expect(control?.childSessionId).toBe(foregroundChildSessionId);
- expect(control?.stopVisible).toBe(true);
- expect((await getV4ComposerBackgroundWorkCounts())?.subagentCount).toBe(1);
- expect(await countVisibleSubagentTabs()).toBe(0);
- const requestCountBeforeStop = await getUpstreamRequestRecordCount();
- expect(await clickV4BackgroundWorkCancel(runningWork.workId)).toBe(true);
- const ack = await waitForLatestV4CommandAck("cancelBackgroundWork", 30000);
- expect(ack.status).toBe("accepted");
- // 测试修复原因:running row 会早于后台 completion notification turn 收口;只等
- // UI 消失会让晚到的 provider 失败漏检。先观察 notification request 与唯一完成文本。
- await waitForUpstreamRequest(
- {
- includes: [
- PARENT_MARKER,
- "<task-notification>",
- "<tool-use-id>toolu_e2e_send_message_resume_stop_send_message</tool-use-id>",
- "<status>stopped</status>",
- ],
- lastUserMessageIncludes: ["<task-notification>", "<status>stopped</status>"],
- },
- "Stop 后 resumed subagent completion notification 没有进入父模型请求",
- 60000,
- { afterIndex: requestCountBeforeStop - 1 },
- );
- await waitForV4AssistantMessageContaining(STOP_NOTIFICATION_DONE, 60000);
- await waitForV4ConversationState(
- (snapshot) => snapshot.state !== "streaming" && !snapshot.stopRequested,
- "Stop completion notification turn 没有回到 idle",
- 60000,
- );
- await waitForResumedSubagentStopped(runningWork.workId, 60000);
- expect(await countVisibleSubagentTabs()).toBe(0);
- });
- });
- async function readForegroundAgentId(runMarker: string): Promise<string> {
- const capturePath = process.env.E2E_PROVIDER_CAPTURE_PATH?.trim();
- if (!capturePath) {
- throw new Error("E2E_PROVIDER_CAPTURE_PATH 未配置,无法读取 foreground agentId");
- }
- const artifact = JSON.parse(
- await readFile(capturePath, "utf8"),
- ) as E2ENetworkCaptureArtifact;
- for (const record of artifact.records) {
- const requestText = JSON.stringify(record.requestJson ?? null);
- if (!requestText.includes(runMarker) || !requestText.includes(FOREGROUND_DONE)) continue;
- const agentId = /agentId:\s*(agent_[A-Za-z0-9_-]+)/u.exec(requestText)?.[1];
- if (agentId) return agentId;
- }
- throw new Error(`没有从 foreground Agent tool result 读取到 agentId: ${runMarker}`);
- }
- async function waitForSingleRunningBackgroundWork(
- timeout: number,
- ): Promise<{ workId: string; status: string | null }> {
- let running: Array<{ workId: string; status: string | null }> = [];
- await browser.waitUntil(
- async () => {
- running = (await getV4BackgroundWorks()).filter((work) => work.status === "running");
- return running.length === 1 && Boolean(running[0]?.workId);
- },
- {
- timeout,
- timeoutMsg: "没有等到唯一的 resumed subagent background work",
- },
- );
- return running[0]!;
- }
- async function waitForSingleSubagentSummaryChildSessionId(timeout: number): Promise<string> {
- let childSessionIds: string[] = [];
- await browser.waitUntil(
- async () => {
- childSessionIds = await browser.execute(
- (prefix) =>
- Array.from(document.querySelectorAll<HTMLElement>(`[data-testid^="${prefix}-"]`)).map(
- (element) => (element.getAttribute("data-testid") ?? "").slice(prefix.length + 1),
- ),
- TID_V4_SUBAGENT_OPEN_SIDE_PANE,
- );
- return childSessionIds.length === 1 && Boolean(childSessionIds[0]);
- },
- {
- timeout,
- timeoutMsg: "没有等到唯一的 foreground subagent child identity",
- },
- );
- return childSessionIds[0]!;
- }
- async function readRunningSubagentControl(workId: string): Promise<{
- kind: string | null;
- childSessionId: string | null;
- stopVisible: boolean;
- } | null> {
- return browser.execute(
- (itemPrefix, cancelPrefix, id) => {
- const row = document.querySelector<HTMLElement>(`[data-testid="${itemPrefix}-${id}"]`);
- if (!row) return null;
- const stop = row.querySelector<HTMLElement>(`[data-testid="${cancelPrefix}-${id}"]`);
- return {
- kind: row.dataset.backgroundTaskKind ?? null,
- childSessionId: row.dataset.childSessionId ?? null,
- stopVisible: Boolean(stop && getComputedStyle(stop).display !== "none"),
- };
- },
- TID_V4_BACKGROUND_WORK_ITEM,
- TID_V4_BACKGROUND_WORK_CANCEL,
- workId,
- );
- }
- async function waitForResumedSubagentStopped(workId: string, timeout: number): Promise<void> {
- await browser.waitUntil(
- async () => {
- const [works, composerCounts, runningAgentCount] = await Promise.all([
- getV4BackgroundWorks(),
- getV4ComposerBackgroundWorkCounts(),
- readStatusPanelRunningAgentCount(),
- ]);
- return (
- !works.some((work) => work.workId === workId) &&
- composerCounts === null &&
- runningAgentCount === 0
- );
- },
- {
- timeout,
- timeoutMsg: "点击 Stop 后 resumed subagent 的 work、右上角入口或 running row 没有收口",
- },
- );
- }
- async function readStatusPanelRunningAgentCount(): Promise<number> {
- return browser.execute((panelTestId) => {
- const panel = document.querySelector<HTMLElement>(`[data-testid="${panelTestId}"]`);
- return Number(panel?.dataset.runningAgentCount ?? "0");
- }, TID_CHAT_SUMMARY_PANEL);
- }
- async function countVisibleSubagentTabs(): Promise<number> {
- return browser.execute(
- () => document.querySelectorAll('[data-side-pane-tab-id^="subagent-session:"]').length,
- );
- }
|