conversation-session-plan-mode-capabilities.test.ts 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. import { readFile, rm } from "node:fs/promises";
  2. import { clearAppData } from "../../../helpers/desktop-app.js";
  3. import { resolveE2EToolPath } from "../../../helpers/e2e-runtime-paths.js";
  4. import {
  5. E2E_READONLY_TOOL_FILE_CONTENT,
  6. E2E_REPLY_TOKEN,
  7. countUpstreamRequests,
  8. getUpstreamRequestRecordCount,
  9. getMessages,
  10. prepareConversationE2E,
  11. sendPrompt,
  12. waitForAssistantMessageContaining,
  13. waitForChatState,
  14. waitForComposerText,
  15. waitForUpstreamRequest,
  16. waitForToolCallBlockByToolCallId,
  17. waitForToolCallBlockByToolName,
  18. waitForUserMessageContaining,
  19. } from "../../../helpers/conversation-session.js";
  20. const DENY_PROMPT_MARKER = "E2E_PLAN_MODE_DENY_STOPS_TURN";
  21. const DENY_ENTER_MARKER = "E2E_PLAN_MODE_DENY_ENTER_PLAN";
  22. const DENY_EXIT_MARKER = "E2E_PLAN_MODE_DENY_EXIT_PLAN";
  23. const DENY_NEXT_MARKER = "E2E_PLAN_MODE_DENY_NEXT_INPUT";
  24. const DENY_EXIT_TOOL_CALL_ID = "toolu_e2e_plan_mode_deny_exit_plan_mode";
  25. const TOOL_PROMPT_MARKER = "E2E_PLAN_MODE_TOOL_BOUNDARY";
  26. const TOOL_ENTER_MARKER = "E2E_PLAN_MODE_TOOL_BOUNDARY_ENTER";
  27. const TOOL_MIXED_MARKER = "E2E_PLAN_MODE_TOOL_BOUNDARY_MIXED";
  28. const TOOL_WRITE_CALL_ID = "toolu_e2e_plan_mode_boundary_write";
  29. const TOOL_READ_CALL_ID = "toolu_e2e_plan_mode_boundary_read";
  30. const DENIED_WRITE_FILE_NAME = "zcode-e2e-plan-mode-denied-write.txt";
  31. const DENIED_WRITE_FILE_PATH = resolveE2EToolPath(
  32. "conversation-session-plan-mode-capabilities",
  33. DENIED_WRITE_FILE_NAME,
  34. );
  35. const TITLE_REQUEST_SENTINEL = "Generate a concise title";
  36. const COMPACT_REQUEST_SENTINEL = "CRITICAL: Respond with TEXT ONLY";
  37. const PLAN_MODE_REMINDER = "Plan mode is active";
  38. const PLAN_MODE_NO_EDIT_REMINDER = "MUST NOT make any edits";
  39. const PLAN_MODE_DENIAL_REASON =
  40. "Plan mode only allows read-only, non-destructive tools";
  41. describe("conversation session plan mode capabilities", () => {
  42. after(async () => {
  43. await browser.electron.restoreAllMocks();
  44. await clearAppData();
  45. });
  46. afterEach(async () => {
  47. await rm(DENIED_WRITE_FILE_PATH, { force: true });
  48. });
  49. it("stops the current turn after ExitPlanMode is declined without feedback", async () => {
  50. await prepareConversationE2E();
  51. const runId = Date.now();
  52. const prompt = [
  53. `${DENY_PROMPT_MARKER}_${runId}: enter plan mode and request approval.`,
  54. `${DENY_ENTER_MARKER}: call EnterPlanMode before drafting.`,
  55. `${DENY_EXIT_MARKER}: call ExitPlanMode with the proposed plan.`,
  56. ].join(" ");
  57. await sendPrompt(prompt);
  58. await waitForComposerText("", "plan mode deny prompt 发送后输入框没有清空");
  59. await waitForUserMessageContaining(DENY_PROMPT_MARKER);
  60. await waitForUpstreamRequest(
  61. {
  62. includes: [DENY_PROMPT_MARKER, DENY_ENTER_MARKER, DENY_EXIT_MARKER],
  63. excludes: [TITLE_REQUEST_SENTINEL, COMPACT_REQUEST_SENTINEL],
  64. },
  65. "plan mode deny initial request",
  66. 60000,
  67. );
  68. await waitForToolCallBlockByToolName("EnterPlanMode", 60000);
  69. await waitForUpstreamRequest(
  70. {
  71. includes: [
  72. DENY_EXIT_MARKER,
  73. "toolu_e2e_plan_mode_deny_enter_plan_mode",
  74. ],
  75. excludes: [TITLE_REQUEST_SENTINEL, COMPACT_REQUEST_SENTINEL],
  76. },
  77. "plan mode deny exit request",
  78. 60000,
  79. );
  80. await waitForToolCallBlockByToolName("ExitPlanMode", 60000);
  81. await waitForElicitationDialogContaining("Approve", 60000);
  82. const requestCountBeforeDecline = await getUpstreamRequestRecordCount();
  83. await dismissPlanApprovalWithoutFeedback();
  84. await waitForIdle("plan mode deny without feedback 没有回到 idle");
  85. await expectNoNewUpstreamRequestContaining(
  86. [DENY_EXIT_TOOL_CALL_ID],
  87. requestCountBeforeDecline,
  88. 2500,
  89. );
  90. const nextPrompt = `${DENY_NEXT_MARKER}_${runId}: reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  91. const nextRequestStart = await getUpstreamRequestRecordCount();
  92. await sendPrompt(nextPrompt);
  93. await waitForComposerText("", "plan mode deny 后续输入发送后输入框没有清空");
  94. await waitForUpstreamRequest(
  95. {
  96. includes: [
  97. DENY_NEXT_MARKER,
  98. PLAN_MODE_REMINDER,
  99. PLAN_MODE_NO_EDIT_REMINDER,
  100. ],
  101. excludes: [TITLE_REQUEST_SENTINEL, COMPACT_REQUEST_SENTINEL],
  102. },
  103. "plan mode deny follow-up request should remain in plan mode",
  104. 60000,
  105. afterRecordCount(nextRequestStart),
  106. );
  107. await waitForAssistantMessageContaining(E2E_REPLY_TOKEN);
  108. await waitForIdle("plan mode deny 后续请求没有结束");
  109. });
  110. it("blocks mutating tools but still runs later read-only tools in plan mode", async () => {
  111. await rm(DENIED_WRITE_FILE_PATH, { force: true });
  112. await prepareConversationE2E();
  113. const runId = Date.now();
  114. const prompt = [
  115. `${TOOL_PROMPT_MARKER}_${runId}: enter plan mode and inspect without edits.`,
  116. `${TOOL_ENTER_MARKER}: call EnterPlanMode first.`,
  117. `${TOOL_MIXED_MARKER}: then call Write and Read in the same assistant message.`,
  118. ].join(" ");
  119. await sendPrompt(prompt);
  120. await waitForComposerText("", "plan mode tool boundary prompt 发送后输入框没有清空");
  121. await waitForUserMessageContaining(TOOL_PROMPT_MARKER);
  122. await waitForUpstreamRequest(
  123. {
  124. includes: [TOOL_PROMPT_MARKER, TOOL_ENTER_MARKER, TOOL_MIXED_MARKER],
  125. excludes: [TITLE_REQUEST_SENTINEL, COMPACT_REQUEST_SENTINEL],
  126. },
  127. "plan mode tool boundary initial request",
  128. 60000,
  129. );
  130. await waitForToolCallBlockByToolName("EnterPlanMode", 60000);
  131. await waitForUpstreamRequest(
  132. {
  133. includes: [
  134. TOOL_MIXED_MARKER,
  135. "toolu_e2e_plan_mode_boundary_enter_plan_mode",
  136. PLAN_MODE_REMINDER,
  137. PLAN_MODE_NO_EDIT_REMINDER,
  138. ],
  139. excludes: [TITLE_REQUEST_SENTINEL, COMPACT_REQUEST_SENTINEL],
  140. },
  141. "plan mode tool boundary mixed tool request",
  142. 60000,
  143. );
  144. await waitForToolCallBlockByToolCallId(TOOL_WRITE_CALL_ID, 60000);
  145. await waitForToolCallBlockByToolCallId(TOOL_READ_CALL_ID, 60000);
  146. await waitForUpstreamRequest(
  147. {
  148. includes: [
  149. TOOL_MIXED_MARKER,
  150. TOOL_WRITE_CALL_ID,
  151. TOOL_READ_CALL_ID,
  152. PLAN_MODE_DENIAL_REASON,
  153. E2E_READONLY_TOOL_FILE_CONTENT,
  154. ],
  155. excludes: [TITLE_REQUEST_SENTINEL, COMPACT_REQUEST_SENTINEL],
  156. },
  157. "plan mode tool boundary continuation request",
  158. 60000,
  159. );
  160. expect(await readFileIfExists(DENIED_WRITE_FILE_PATH)).toBe(null);
  161. await waitForAssistantMessageContaining(E2E_REPLY_TOKEN);
  162. await expectAssistantReplyWithoutDeniedChangeSummary();
  163. await waitForIdle("plan mode tool boundary 没有结束");
  164. });
  165. });
  166. async function waitForElicitationDialogContaining(
  167. text: string,
  168. timeoutMs: number,
  169. ) {
  170. const expectedTexts = expandElicitationDialogTextAlternatives(text);
  171. let latestDialogText = "";
  172. await browser.waitUntil(
  173. async () => {
  174. // 修复原因:ExitPlanMode 的批准/忽略选项渲染在 footer,body 只包含计划正文。
  175. // 旧等待只查 body,会在真实弹窗已出现时继续等到超时。
  176. latestDialogText = await browser.execute(() => {
  177. const body = document.querySelector<HTMLElement>(
  178. "[data-elicitation-dialog-body='true']",
  179. );
  180. const footer = document.querySelector<HTMLElement>(
  181. "[data-elicitation-dialog-footer='true']",
  182. );
  183. return [body?.innerText, footer?.innerText].filter(Boolean).join("\n");
  184. });
  185. return expectedTexts.some((expectedText) =>
  186. latestDialogText.includes(expectedText),
  187. );
  188. },
  189. {
  190. timeout: timeoutMs,
  191. timeoutMsg: `Timed out waiting for elicitation dialog containing ${expectedTexts.join(
  192. " / ",
  193. )}; latest=${latestDialogText}`,
  194. },
  195. );
  196. }
  197. function expandElicitationDialogTextAlternatives(text: string) {
  198. if (text === "Approve") {
  199. return ["Approve", "批准"];
  200. }
  201. if (text === "Dismiss") {
  202. return ["Dismiss", "忽略"];
  203. }
  204. return [text];
  205. }
  206. async function dismissPlanApprovalWithoutFeedback() {
  207. const dismissButton = await $(
  208. "//button[contains(., 'Dismiss') or contains(., '忽略')]",
  209. );
  210. await dismissButton.waitForEnabled({ timeout: 30000 });
  211. await dismissButton.click();
  212. }
  213. async function expectNoNewUpstreamRequestContaining(
  214. includes: string[],
  215. afterRecordCountValue: number,
  216. durationMs: number,
  217. ) {
  218. await browser.pause(durationMs);
  219. const count = await countUpstreamRequests(
  220. {
  221. includes,
  222. excludes: [TITLE_REQUEST_SENTINEL, COMPACT_REQUEST_SENTINEL],
  223. },
  224. afterRecordCount(afterRecordCountValue),
  225. );
  226. expect(count).toBe(0);
  227. }
  228. async function waitForIdle(timeoutMsg: string) {
  229. await waitForChatState(
  230. (snapshot) => snapshot.state === "idle" && snapshot.queueCount === 0,
  231. timeoutMsg,
  232. 60000,
  233. );
  234. }
  235. async function expectAssistantReplyWithoutDeniedChangeSummary() {
  236. const replies = (await getMessages("assistant")).filter((message) =>
  237. message.text.includes(E2E_REPLY_TOKEN),
  238. );
  239. expect(replies.length).toBeGreaterThan(0);
  240. for (const reply of replies) {
  241. expect(reply.text).not.toContain("file changed");
  242. expect(reply.text).not.toContain("文件已更改");
  243. }
  244. }
  245. async function readFileIfExists(path: string) {
  246. try {
  247. return await readFile(path, "utf8");
  248. } catch (error) {
  249. if (error instanceof Error && "code" in error && error.code === "ENOENT") {
  250. return null;
  251. }
  252. throw error;
  253. }
  254. }
  255. function afterRecordCount(recordCount: number) {
  256. return recordCount > 0 ? { afterIndex: recordCount - 1 } : {};
  257. }