conversation-session-hook-trust.test.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. import { openWorkspaceDraftFromSidebar } from "../helpers/workspace-agent-warmup.js";
  2. /**
  3. * 正式 E2E —— Workspace Hook Trust 软门禁与逐条持久信任(HK05–HK07)。
  4. * 已通过人工 review 并由 e2e:promote 从 manual-review/pending 晋级,
  5. * 随默认 spec 发现进入常规回归门禁;fixture 走 case-local provider replay。
  6. *
  7. * 证据分层:
  8. * - UI/交互:pending banner、Settings 钩子行内“信任”按钮、锁定 Switch
  9. * - Runtime:`.hook-trust-e2e/executions.jsonl` 执行证据(未信任零执行、不补跑、
  10. * 信任后自然事件执行)
  11. * - 持久化:`<E2E_HOME>/.zcode/security/workspace-hook-trust-v1.json` 精确落盘
  12. *
  13. * fixture 由 wdio.conf.ts 在 beforeSession 阶段(resetE2EHome 内)落盘,
  14. * 首个 agent 进程冷启动即加载 workspace hooks;spec 内不重复安装。
  15. */
  16. import {
  17. TID_SETTINGS_BACK_BUTTON,
  18. TID_SETTINGS_PAGE,
  19. TID_SETTINGS_SECTION_NAV,
  20. TID_TASK_SETTINGS_BUTTON,
  21. TID_V4_WORKSPACE_HOOK_PENDING_BANNER,
  22. TID_V4_WORKSPACE_HOOK_PENDING_DISMISS,
  23. TID_V4_WORKSPACE_HOOK_PENDING_REVIEW,
  24. testId,
  25. } from "@zcode/shared/test-ids";
  26. import {
  27. DEFAULT_WORKSPACE,
  28. clearAppData,
  29. clickTestIdByDom,
  30. waitForWorkspaceApp,
  31. } from "../helpers/desktop-app.js";
  32. import { sel } from "../helpers/selectors.js";
  33. import {
  34. E2E_HOOK_TRUST_LABELS,
  35. readHookTrustExecutions,
  36. readHookTrustStore,
  37. waitForHookTrustExecution,
  38. waitForHookTrustStoreRecords,
  39. } from "../helpers/hook-trust-fixture.js";
  40. import {
  41. prepareV4ConversationE2E,
  42. sendV4Prompt,
  43. waitForV4Pane,
  44. } from "../helpers/v4-conversation.js";
  45. const HOOKS_SECTION_SELECTOR = '[data-testid="hooks-settings-section"]';
  46. const HOOK_ROW_SELECTOR = '//div[@data-testid="configured-hook-row"]';
  47. // 信任按钮通过 ShieldCheck 图标定位(lucide class),不依赖 locale 文案。
  48. const TRUST_BUTTON_IN_ROW = './/button[.//*[contains(@class, "lucide-shield-check")]]';
  49. describe("Workspace Hook Trust (HK05–HK07)", () => {
  50. before(async function () {
  51. this.timeout(120000);
  52. await prepareV4ConversationE2E();
  53. });
  54. after(async () => {
  55. await browser.electron.restoreAllMocks();
  56. await clearAppData();
  57. });
  58. it("HK05: 未信任 Hook 被软门禁跳过,banner 引导进设置,行内只提供信任与锁定开关", async function () {
  59. this.timeout(180000);
  60. await sendV4Prompt("E2E_HOOK_TRUST: reply with ok");
  61. await waitForIdle("Soft-gated turn did not return to idle");
  62. // 软门禁不阻塞对话,且未信任 Hook 零执行。
  63. await $(sel(TID_V4_WORKSPACE_HOOK_PENDING_BANNER)).waitForDisplayed({
  64. timeout: 30000,
  65. });
  66. expect(await readHookTrustExecutions()).toHaveLength(0);
  67. // 从 pending banner 进入 设置 → 钩子。
  68. await clickTestIdByDom(TID_V4_WORKSPACE_HOOK_PENDING_REVIEW, {
  69. timeout: 15000,
  70. });
  71. await $(sel(TID_SETTINGS_PAGE)).waitForDisplayed({ timeout: 15000 });
  72. await clickTestIdByDom(testId(TID_SETTINGS_SECTION_NAV, "hooks"), {
  73. timeout: 15000,
  74. });
  75. await $(HOOKS_SECTION_SELECTOR).waitForDisplayed({ timeout: 15000 });
  76. // 三条 workspace Hook 声明全部未信任:只有“信任”按钮 + 锁定关闭的开关,
  77. // 没有其他审核动作(Allow once / Trust all / Keep blocked 均不存在)。
  78. await waitForHookTrustRowCount(3);
  79. for (const label of Object.values(E2E_HOOK_TRUST_LABELS)) {
  80. await assertUntrustedHookRow(label);
  81. }
  82. // 忽略 banner 是 renderer 本地行为,不产生 Trust mutation。
  83. await clickTestIdByDom(TID_SETTINGS_BACK_BUTTON, {
  84. timeout: 15000,
  85. });
  86. await waitForWorkspaceApp(DEFAULT_WORKSPACE, 30000);
  87. await clickTestIdByDom(TID_V4_WORKSPACE_HOOK_PENDING_DISMISS, {
  88. timeout: 15000,
  89. });
  90. await $(sel(TID_V4_WORKSPACE_HOOK_PENDING_BANNER)).waitForDisplayed({
  91. timeout: 15000,
  92. reverse: true,
  93. });
  94. expect(await readHookTrustStore()).toBeNull();
  95. });
  96. it("HK06: 行内信任单条声明,只对该条精确持久落盘,其余行保持可审核", async function () {
  97. this.timeout(180000);
  98. await openHooksSettings();
  99. const trustButton = await findTrustButton(E2E_HOOK_TRUST_LABELS.sessionStartA);
  100. await trustButton.waitForDisplayed({ timeout: 15000 });
  101. await trustButton.click();
  102. const records = await waitForHookTrustStoreRecords(
  103. (items) => items.length === 1,
  104. "Trusting session-start-a did not persist exactly one store record",
  105. );
  106. expect(records[0]!.decision).toBe("trusted");
  107. expect(records[0]!.eventAtGrant).toBe("SessionStart");
  108. // 被信任的行按钮消失;其余两条仍显示“信任”。
  109. await assertNoTrustButton(E2E_HOOK_TRUST_LABELS.sessionStartA);
  110. for (const label of [E2E_HOOK_TRUST_LABELS.sessionStartB, E2E_HOOK_TRUST_LABELS.promptSubmit]) {
  111. await (await findTrustButton(label)).waitForDisplayed({ timeout: 15000 });
  112. }
  113. await closeHooksSettings();
  114. });
  115. it("HK07: 信任不补跑已跳过的 SessionStart,只作用于未来自然事件", async function () {
  116. this.timeout(240000);
  117. // 第一段(同 task):信任 UserPromptSubmit 后再发一条 prompt,
  118. // 出现 prompt-submit 记录 = 信任只作用于未来自然事件;
  119. // 全程无 SessionStart 记录 = 已信任的 session-start-a 不补跑。
  120. await openHooksSettings();
  121. await clickTrustButton(E2E_HOOK_TRUST_LABELS.promptSubmit);
  122. await waitForHookTrustStoreRecords(
  123. (items) => items.length === 2,
  124. "Trusting prompt-submit did not persist a second store record",
  125. );
  126. await closeHooksSettings();
  127. await sendV4Prompt("E2E_HOOK_TRUST second prompt: reply with ok");
  128. await waitForHookTrustExecution(
  129. E2E_HOOK_TRUST_LABELS.promptSubmit,
  130. "Trusted UserPromptSubmit hook did not run on next natural prompt",
  131. );
  132. await waitForIdle("Second turn did not return to idle");
  133. const inTaskExecutions = await readHookTrustExecutions();
  134. expect(inTaskExecutions.filter((item) => item.event === "SessionStart")).toHaveLength(0);
  135. expect(inTaskExecutions.some((item) => item.label === E2E_HOOK_TRUST_LABELS.promptSubmit)).toBe(
  136. true,
  137. );
  138. // 第二段(新 task):catalog HK07 字面语义——新 task 的自然 SessionStart
  139. // 按 Trust 执行。该路径与 UserPromptSubmit 不同:覆盖新 session 冷启动时
  140. // 的配置发现、trust store 读取与 admission 评估全链路,不以"语义等价"省略。
  141. // 全局新建会分配虚拟目录;Hook、Skill 与会话候选必须继续使用夹具所属项目。
  142. await openWorkspaceDraftFromSidebar(DEFAULT_WORKSPACE);
  143. await sendV4Prompt("E2E_HOOK_TRUST new task: reply with ok");
  144. await waitForHookTrustExecution(
  145. E2E_HOOK_TRUST_LABELS.sessionStartA,
  146. "Trusted SessionStart hook did not run on the next natural session start",
  147. );
  148. await waitForIdle("New-task turn did not return to idle");
  149. const executions = await readHookTrustExecutions();
  150. // 已信任的 a 在新 task 自然 SessionStart 执行且仅执行一次;
  151. // 未信任的 b 仍被软门禁跳过。
  152. expect(
  153. executions.filter((item) => item.label === E2E_HOOK_TRUST_LABELS.sessionStartA),
  154. ).toHaveLength(1);
  155. expect(executions.some((item) => item.label === E2E_HOOK_TRUST_LABELS.sessionStartB)).toBe(
  156. false,
  157. );
  158. });
  159. });
  160. async function waitForIdle(timeoutMsg: string): Promise<void> {
  161. await waitForV4Pane(
  162. (snapshot) =>
  163. snapshot.sessionId !== null && snapshot.sessionId !== "draft" && !snapshot.canStop,
  164. timeoutMsg,
  165. 60000,
  166. );
  167. }
  168. async function openHooksSettings(): Promise<void> {
  169. await clickTestIdByDom(TID_TASK_SETTINGS_BUTTON, {
  170. timeout: 15000,
  171. timeoutMsg: "Hook trust E2E could not open Settings",
  172. });
  173. await $(sel(TID_SETTINGS_PAGE)).waitForDisplayed({ timeout: 15000 });
  174. await clickTestIdByDom(testId(TID_SETTINGS_SECTION_NAV, "hooks"), {
  175. timeout: 15000,
  176. });
  177. await $(HOOKS_SECTION_SELECTOR).waitForDisplayed({ timeout: 15000 });
  178. }
  179. async function closeHooksSettings(): Promise<void> {
  180. await clickTestIdByDom(TID_SETTINGS_BACK_BUTTON, {
  181. timeout: 15000,
  182. timeoutMsg: "Hook trust E2E could not leave Settings",
  183. });
  184. await waitForWorkspaceApp(DEFAULT_WORKSPACE, 30000);
  185. }
  186. async function waitForHookTrustRowCount(count: number): Promise<void> {
  187. await browser.waitUntil(
  188. () =>
  189. browser.execute(
  190. (expected: number) =>
  191. Array.from(
  192. document.querySelectorAll<HTMLElement>('[data-testid="configured-hook-row"]'),
  193. ).filter((row) => row.innerText.includes("hook-trust-")).length === expected,
  194. count,
  195. ),
  196. {
  197. timeout: 15000,
  198. timeoutMsg: `Hooks settings did not render ${count} workspace hook rows`,
  199. },
  200. );
  201. }
  202. function hookRowSelector(label: string): string {
  203. return `${HOOK_ROW_SELECTOR}[contains(., "${label}")]`;
  204. }
  205. async function findTrustButton(label: string) {
  206. const row = await $(hookRowSelector(label));
  207. await row.waitForDisplayed({ timeout: 15000 });
  208. return await row.$(TRUST_BUTTON_IN_ROW);
  209. }
  210. async function clickTrustButton(label: string): Promise<void> {
  211. await (await findTrustButton(label)).click();
  212. }
  213. async function assertNoTrustButton(label: string): Promise<void> {
  214. const row = await $(hookRowSelector(label));
  215. await row.waitForDisplayed({ timeout: 15000 });
  216. await (
  217. await row.$(TRUST_BUTTON_IN_ROW)
  218. ).waitForExist({
  219. timeout: 15000,
  220. reverse: true,
  221. });
  222. }
  223. async function assertUntrustedHookRow(label: string): Promise<void> {
  224. const row = await $(hookRowSelector(label));
  225. await row.waitForDisplayed({ timeout: 15000 });
  226. const buttons = await row.$$("button");
  227. // 行内动作只有“信任”按钮和 Switch 两个 button;不存在任何额外审核动作或徽章入口。
  228. expect(buttons).toHaveLength(2);
  229. const trustButton = await row.$(TRUST_BUTTON_IN_ROW);
  230. await trustButton.waitForDisplayed({ timeout: 15000 });
  231. expect(await trustButton.isEnabled()).toBe(true);
  232. // 未信任行 Switch 强制关闭并锁定。
  233. const switchControl = await row.$('button[role="switch"]');
  234. await switchControl.waitForDisplayed({ timeout: 15000 });
  235. expect(await switchControl.getAttribute("aria-checked")).toBe("false");
  236. expect(await switchControl.getAttribute("disabled")).not.toBeNull();
  237. }