conversation-session-explore-permission-delegate.test.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import { access, rm } from "node:fs/promises";
  2. import { clearAppData } from "#e2e/helpers/desktop-app";
  3. import { resolveE2ERuntimePath } from "#e2e/helpers/e2e-runtime-paths";
  4. import {
  5. getLatestUpstreamToolResultByToolCallId,
  6. waitForUpstreamRequest,
  7. } from "#e2e/helpers/conversation-session-network";
  8. import {
  9. clickV4Stop,
  10. denyV4PermissionWithFeedback,
  11. getV4ConfigProjection,
  12. getV4PaneSnapshot,
  13. prepareV4ConversationE2E,
  14. sendV4Prompt,
  15. switchV4Mode,
  16. waitForV4AssistantMessageContaining,
  17. waitForV4Pane,
  18. waitForV4PermissionDialogInComposerDock,
  19. waitForV4TimelineContaining,
  20. } from "#e2e/helpers/v4-conversation";
  21. const CASE = "conversation-session-explore-permission-delegate";
  22. const PARENT_MARKER = "E2E_EXPLORE_PERMISSION_PARENT";
  23. const CHILD_MARKER = "E2E_EXPLORE_PERMISSION_CHILD";
  24. const SHOULD_NOT_RUN = "E2E_EXPLORE_PERMISSION_SHOULD_NOT_RUN";
  25. const DENY_FEEDBACK = "E2E_EXPLORE_PERMISSION_DENY_FEEDBACK";
  26. const CHILD_DONE = "E2E_EXPLORE_PERMISSION_CHILD_DONE";
  27. const PARENT_DONE = "E2E_EXPLORE_PERMISSION_PARENT_DONE";
  28. const AGENT_TOOL_CALL_ID = "toolu_e2e_explore_permission_agent";
  29. const BASH_TOOL_CALL_ID = "toolu_e2e_explore_permission_bash";
  30. const OUTPUT_FILE = resolveE2ERuntimePath(CASE, "output.txt");
  31. // 修复原因:内置 Explore 曾固定以 yolo 运行,只靠 prompt 约束只读,提示注入后模型可经
  32. // Agent(Explore) 在 build 父会话下免审批执行任意 Bash。现在 Explore 继承父会话模式,
  33. // 非只读 Bash 必须先在父会话审批。
  34. describe("会话区内置 Explore 权限继承 E2E", () => {
  35. afterEach(async () => {
  36. try {
  37. const snapshot = await getV4PaneSnapshot();
  38. if (snapshot.canStop) {
  39. await clickV4Stop();
  40. await waitForV4Pane(
  41. (current) => !current.canStop,
  42. "Explore 权限 case teardown 后仍处于运行态",
  43. 30_000,
  44. );
  45. }
  46. } catch {
  47. // teardown 只负责隔离下一条 case,不覆盖原始断言失败。
  48. }
  49. await rm(resolveE2ERuntimePath(CASE), { recursive: true, force: true });
  50. });
  51. after(async () => {
  52. await browser.electron.restoreAllMocks();
  53. await clearAppData();
  54. });
  55. it("T03: build 模式下 Explore 的非只读 Bash 先在父 task 审批,拒绝后不执行", async function () {
  56. this.timeout(180_000);
  57. await prepareV4ConversationE2E();
  58. await switchV4Mode("build");
  59. await browser.waitUntil(async () => (await getV4ConfigProjection()).mode === "build", {
  60. timeout: 30_000,
  61. timeoutMsg: "Explore 权限 case 没有切换到 mode=build",
  62. });
  63. const runId = Date.now();
  64. await sendV4Prompt(
  65. [
  66. `${PARENT_MARKER}_${runId}: Use the Agent tool with subagent_type "Explore".`,
  67. `Ask the child to trigger ${CHILD_MARKER} and run one Bash command.`,
  68. `After the child returns, reply with exactly ${PARENT_DONE}.`,
  69. ].join(" "),
  70. );
  71. await waitForV4TimelineContaining(`${PARENT_MARKER}_${runId}`, 30_000);
  72. const dockState = await waitForV4PermissionDialogInComposerDock(60_000);
  73. expect(dockState.inDock).toBe(true);
  74. const interaction = await readPermissionDock();
  75. expect(interaction?.originTitle).toContain("Explore");
  76. expect(interaction?.text).toContain(SHOULD_NOT_RUN);
  77. expect(await fileExists(OUTPUT_FILE)).toBe(false);
  78. expect(await denyV4PermissionWithFeedback(DENY_FEEDBACK)).toBe(true);
  79. await waitForUpstreamRequest(
  80. {
  81. includes: [CHILD_MARKER, BASH_TOOL_CALL_ID, DENY_FEEDBACK],
  82. excludes: ["Generate a concise title", "CRITICAL: Respond with TEXT ONLY"],
  83. },
  84. "Explore Bash 拒绝结果没有进入 child 下一次模型请求",
  85. 60_000,
  86. );
  87. const result = await getLatestUpstreamToolResultByToolCallId(BASH_TOOL_CALL_ID);
  88. expect(result?.isError).toBe(true);
  89. expect(result?.content).toContain(DENY_FEEDBACK);
  90. await waitForUpstreamRequest(
  91. {
  92. includes: [PARENT_MARKER, CHILD_DONE, AGENT_TOOL_CALL_ID],
  93. excludes: ["Generate a concise title", "CRITICAL: Respond with TEXT ONLY"],
  94. },
  95. "Explore 完成后父会话没有收到 Agent tool_result continuation",
  96. 60_000,
  97. );
  98. await waitForV4AssistantMessageContaining(PARENT_DONE, 60_000);
  99. await waitForV4Pane(
  100. (snapshot) => !snapshot.canStop,
  101. "Explore 权限 case 完成后没有回到 idle",
  102. 60_000,
  103. );
  104. expect(await fileExists(OUTPUT_FILE)).toBe(false);
  105. });
  106. });
  107. interface PermissionDockSnapshot {
  108. originTitle: string;
  109. text: string;
  110. }
  111. function readPermissionDock(): Promise<PermissionDockSnapshot | null> {
  112. return browser.execute(() => {
  113. const normalize = (value: string | null | undefined) =>
  114. (value ?? "").replace(/\u00a0/g, " ").trim();
  115. const listbox = Array.from(document.querySelectorAll<HTMLElement>('[role="listbox"]')).find(
  116. (element) => {
  117. const label = normalize(element.getAttribute("aria-label"));
  118. return label === "Permission required" || label === "需要权限";
  119. },
  120. );
  121. const dock = listbox?.closest<HTMLElement>('[data-v4-composer-dock="true"]');
  122. const badge = dock?.querySelector<HTMLElement>('[data-interaction-origin-badge="subagent"]');
  123. if (!dock || !badge) return null;
  124. return {
  125. originTitle: normalize(badge.getAttribute("title")),
  126. text: normalize(dock.innerText || dock.textContent),
  127. };
  128. });
  129. }
  130. async function fileExists(path: string): Promise<boolean> {
  131. try {
  132. await access(path);
  133. return true;
  134. } catch {
  135. return false;
  136. }
  137. }