conversation-session-v4-permission.test.ts 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // M4 门禁:v4 前向命令 resolveInteraction 收口反向权限请求(catalog Q 组 / permission)。
  2. // 证据层 L3:Write 工具在默认模式触发权限弹窗 → V4InteractionDialogs 渲染 →
  3. // 批准 → resolveInteraction 命令经 v4-bridge → interaction-broker race deferred 收口 →
  4. // 工具执行 → tool_result 回灌 → 终态文本。证明反向 RPC 与前向命令的汇合点打通。
  5. import { access, mkdir, rm } from "node:fs/promises";
  6. import { clearAppData } from "../helpers/desktop-app.js";
  7. import { resolveE2ERuntimePath, resolveE2EToolPath } from "../helpers/e2e-runtime-paths.js";
  8. import {
  9. approveV4Permission,
  10. denyV4PermissionWithFeedback,
  11. prepareV4ConversationE2E,
  12. sendV4Prompt,
  13. waitForV4PermissionDialogInComposerDock,
  14. waitForV4TimelineContaining,
  15. } from "../helpers/v4-conversation.js";
  16. import { getLatestUpstreamToolResultByToolCallId } from "../helpers/conversation-session-network.js";
  17. const V4_PERMISSION_CASE = "conversation-session-v4-permission";
  18. const V4_PERMISSION_DIR = resolveE2ERuntimePath(V4_PERMISSION_CASE);
  19. const V4_PERMISSION_OUTPUT_PATH = resolveE2EToolPath(V4_PERMISSION_CASE, "write-output.txt");
  20. const V4_PERMISSION_ALLOW_FEEDBACK_OUTPUT_PATH = resolveE2EToolPath(
  21. V4_PERMISSION_CASE,
  22. "allow-feedback-output.txt",
  23. );
  24. const V4_PERMISSION_DENY_OUTPUT_PATH = resolveE2EToolPath(V4_PERMISSION_CASE, "deny-output.txt");
  25. const V4_PERMISSION_DENY_BLANK_OUTPUT_PATH = resolveE2EToolPath(
  26. V4_PERMISSION_CASE,
  27. "deny-blank-output.txt",
  28. );
  29. const V4_PERMISSION_ALLOW_FEEDBACK = "This note must be ignored when allowing the tool.";
  30. const V4_PERMISSION_DENY_FEEDBACK =
  31. "Do not write the file; explain that the requested change needs clarification first. " +
  32. "Additional context: " +
  33. "x".repeat(600);
  34. const V4_PERMISSION_DENY_BASE_CONTENT =
  35. "The user doesn't want to proceed with this tool use. The tool use was rejected (eg. if it was a file edit, the new_string was NOT written to the file). STOP what you are doing and wait for the user to tell you how to proceed.";
  36. describe("v4 M4 门禁:resolveInteraction 收口权限请求", () => {
  37. before(async () => {
  38. await rm(V4_PERMISSION_DIR, { recursive: true, force: true });
  39. await mkdir(V4_PERMISSION_DIR, { recursive: true });
  40. });
  41. after(async () => {
  42. await browser.electron.restoreAllMocks();
  43. await clearAppData();
  44. await rm(V4_PERMISSION_DIR, { recursive: true, force: true });
  45. });
  46. it("默认模式 Write 工具触发权限弹窗,批准后工具执行并出终态文本", async () => {
  47. await prepareV4ConversationE2E();
  48. await sendV4Prompt(`E2E_V4_PERMISSION_WRITE 请写入文件 ${V4_PERMISSION_OUTPUT_PATH}`);
  49. // 反向权限请求 → V4InteractionDialogs 渲染 PermissionDialog。
  50. // Bugfix: v4 权限等待态是 bottom dock 阻塞交互,不应退回 SessionPane 外层全宽底部卡片。
  51. const dockState = await waitForV4PermissionDialogInComposerDock();
  52. expect(dockState.inDock).toBe(true);
  53. expect(dockState.composerHidden).toBe(true);
  54. expect(dockState.interactionBeforeComposer).toBe(true);
  55. expect(await readPermissionFeedbackTextareaSemantics()).toEqual({
  56. tagName: "TEXTAREA",
  57. rows: "1",
  58. autoSizing: true,
  59. maxRows: true,
  60. index: "5.",
  61. indexCenteredOnFirstLine: true,
  62. rowGaps: [4, 4, 4, 4],
  63. });
  64. // 前向 resolveInteraction 命令收口
  65. const dismissed = await approveV4Permission();
  66. expect(dismissed).toBe(true);
  67. // 批准后工具执行 → tool_result 回灌 → 终态文本
  68. await waitForV4TimelineContaining("V4_PERMISSION_DONE", 60000);
  69. });
  70. it("批准时填写的反馈被忽略,工具仍按 Allow once 执行", async () => {
  71. await prepareV4ConversationE2E();
  72. await sendV4Prompt(
  73. `E2E_V4_PERMISSION_ALLOW_FEEDBACK 请写入文件 ${V4_PERMISSION_ALLOW_FEEDBACK_OUTPUT_PATH}`,
  74. );
  75. await waitForV4PermissionDialogInComposerDock();
  76. expect(await approveV4Permission(V4_PERMISSION_ALLOW_FEEDBACK)).toBe(true);
  77. await waitForV4TimelineContaining("V4_PERMISSION_ALLOW_FEEDBACK_DONE", 60000);
  78. const allowedResult = await getLatestUpstreamToolResultByToolCallId(
  79. "toolu_v4_permission_allow_feedback",
  80. );
  81. expect(allowedResult?.isError).toBe(false);
  82. expect(allowedResult?.content).not.toContain(V4_PERMISSION_ALLOW_FEEDBACK);
  83. await expectFileExists(V4_PERMISSION_ALLOW_FEEDBACK_OUTPUT_PATH);
  84. });
  85. for (const submitWithEnter of [true, false]) {
  86. it(`第 5 行填写反馈并${submitWithEnter ? "回车" : "确认"},反馈进入同一个 provider-visible tool_result 且工具不执行`, async () => {
  87. await prepareV4ConversationE2E();
  88. await sendV4Prompt(
  89. `E2E_V4_PERMISSION_DENY_FEEDBACK 请写入文件 ${V4_PERMISSION_DENY_OUTPUT_PATH}`,
  90. );
  91. await waitForV4PermissionDialogInComposerDock();
  92. const input = await $(
  93. 'textarea[aria-label="Optional feedback for the model when denying"],textarea[aria-label="拒绝时给模型的可选反馈"]',
  94. );
  95. const allow = await $('[data-permission-option-kind="allowOnce"]');
  96. const deny = await $('[data-permission-option-kind^="reject"]');
  97. await expect(allow).toBeFocused();
  98. await browser.keys("ArrowUp");
  99. await expect(input).toBeFocused();
  100. expect((await readPermissionFeedbackTextareaSemantics()).index).toBe("5.");
  101. expect(await deny.getAttribute("aria-selected")).toBe("false");
  102. await browser.keys("ArrowUp");
  103. await expect(deny).toBeFocused();
  104. expect(
  105. await browser.execute(() =>
  106. document
  107. .querySelector(
  108. 'textarea[aria-label="Optional feedback for the model when denying"],textarea[aria-label="拒绝时给模型的可选反馈"]',
  109. )
  110. ?.parentElement?.classList.contains("bg-selected"),
  111. ),
  112. ).toBe(false);
  113. await browser.keys("Tab");
  114. await expect(input).toBeFocused();
  115. await browser.keys(["Shift", "Tab"]);
  116. await expect(deny).toBeFocused();
  117. await browser.keys("ArrowDown");
  118. await expect(input).toBeFocused();
  119. expect(await denyV4PermissionWithFeedback(V4_PERMISSION_DENY_FEEDBACK, submitWithEnter)).toBe(
  120. true,
  121. );
  122. await waitForV4TimelineContaining("V4_PERMISSION_DENY_DONE", 60000);
  123. const deniedResult = await getLatestUpstreamToolResultByToolCallId(
  124. "toolu_v4_permission_deny_feedback",
  125. );
  126. expect(deniedResult?.isError).toBe(true);
  127. expect(deniedResult?.content).toBe(
  128. V4_PERMISSION_DENY_BASE_CONTENT +
  129. " To tell you how to proceed, the user said:\n" +
  130. V4_PERMISSION_DENY_FEEDBACK,
  131. );
  132. await expectFileMissing(V4_PERMISSION_DENY_OUTPUT_PATH);
  133. });
  134. }
  135. it("拒绝时只有空白反馈,provider-visible tool_result 保持基础 Deny 文案", async () => {
  136. await prepareV4ConversationE2E();
  137. await sendV4Prompt(
  138. `E2E_V4_PERMISSION_DENY_BLANK 请写入文件 ${V4_PERMISSION_DENY_BLANK_OUTPUT_PATH}`,
  139. );
  140. await waitForV4PermissionDialogInComposerDock();
  141. expect(await denyV4PermissionWithFeedback(" ")).toBe(true);
  142. await waitForV4TimelineContaining("V4_PERMISSION_DENY_BLANK_DONE", 60000);
  143. const deniedResult = await getLatestUpstreamToolResultByToolCallId(
  144. "toolu_v4_permission_deny_blank",
  145. );
  146. expect(deniedResult?.isError).toBe(true);
  147. expect(deniedResult?.content).toBe(V4_PERMISSION_DENY_BASE_CONTENT);
  148. expect(deniedResult?.content).not.toContain("To tell you how to proceed");
  149. await expectFileMissing(V4_PERMISSION_DENY_BLANK_OUTPUT_PATH);
  150. });
  151. });
  152. async function expectFileExists(path: string) {
  153. const exists = await access(path)
  154. .then(() => true)
  155. .catch(() => false);
  156. expect(exists).toBe(true);
  157. }
  158. async function expectFileMissing(path: string) {
  159. const exists = await access(path)
  160. .then(() => true)
  161. .catch(() => false);
  162. expect(exists).toBe(false);
  163. }
  164. async function readPermissionFeedbackTextareaSemantics() {
  165. return browser.execute(() => {
  166. const textarea = document.querySelector<HTMLTextAreaElement>(
  167. 'textarea[aria-label="Optional feedback for the model when denying"],textarea[aria-label="拒绝时给模型的可选反馈"]',
  168. );
  169. const style = textarea ? getComputedStyle(textarea) : null;
  170. const indexRect = textarea?.previousElementSibling?.getBoundingClientRect();
  171. // 原因:反馈行曾落在外层 12px 分组间距中;校验实际位置,避免只匹配样式名。
  172. const feedbackRow = textarea?.parentElement;
  173. const rowRects = [
  174. ...Array.from(
  175. feedbackRow?.previousElementSibling?.querySelectorAll("[data-permission-option-kind]") ??
  176. [],
  177. ),
  178. ...(feedbackRow ? [feedbackRow] : []),
  179. ].map((row) => row.getBoundingClientRect());
  180. return {
  181. tagName: textarea?.tagName ?? null,
  182. rows: textarea?.getAttribute("rows") ?? null,
  183. autoSizing: textarea?.classList.contains("field-sizing-content") ?? false,
  184. maxRows: textarea?.classList.contains("max-h-[5lh]") ?? false,
  185. index: textarea?.previousElementSibling?.textContent ?? null,
  186. rowGaps: rowRects.slice(1).map((rect, index) => rect.top - rowRects[index]!.bottom),
  187. indexCenteredOnFirstLine: Boolean(
  188. textarea &&
  189. style &&
  190. indexRect &&
  191. Math.abs(
  192. indexRect.top +
  193. indexRect.height / 2 -
  194. (textarea.getBoundingClientRect().top +
  195. parseFloat(style.borderTopWidth) +
  196. parseFloat(style.paddingTop) +
  197. parseFloat(style.lineHeight) / 2),
  198. ) <= 0.5,
  199. ),
  200. };
  201. });
  202. }