conversation-session-v4-rewind-files.test.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // PV4-21/PV4-22:editUserQuery(workspaceMode=rewind) 的文件事务与冲突降级。
  2. // 证据层 L3:真实 Read/Write checkpoint + 行内三按钮 + Agent preview/apply +
  3. // same-session branch cut。冲突路径还断言 blocked 期间文件/对话/provider request 均不变。
  4. import { mkdir, readFile, rm, writeFile } from "node:fs/promises";
  5. import { join } from "node:path";
  6. import { clearAppData, DEFAULT_WORKSPACE } from "../helpers/desktop-app.js";
  7. import { countUpstreamRequestsContaining } from "../helpers/conversation-session-network.js";
  8. import {
  9. approveV4Permission,
  10. beginFirstV4UserQueryEdit,
  11. clickV4EditConflictConversationOnly,
  12. clickV4EditWorkspaceReset,
  13. getV4PaneSnapshot,
  14. prepareV4ConversationE2E,
  15. sendV4Prompt,
  16. switchV4Mode,
  17. waitForV4Edit,
  18. waitForV4EditWorkspaceConflict,
  19. waitForLatestV4CommandAck,
  20. waitForV4Pane,
  21. waitForV4PermissionDialog,
  22. waitForV4TimelineContaining,
  23. } from "../helpers/v4-conversation.js";
  24. const CASE_ROOT = join(DEFAULT_WORKSPACE, "v4-rewind-files-e2e");
  25. const SAFE_RELATIVE_PATH = "v4-rewind-files-e2e/safe.txt";
  26. const CONFLICT_RELATIVE_PATH = "v4-rewind-files-e2e/conflict.txt";
  27. const SAFE_PATH = join(DEFAULT_WORKSPACE, SAFE_RELATIVE_PATH);
  28. const CONFLICT_PATH = join(DEFAULT_WORKSPACE, CONFLICT_RELATIVE_PATH);
  29. describe("PV4-21/PV4-22 conversation + file rewind", () => {
  30. afterEach(async () => {
  31. await rm(CASE_ROOT, { force: true, recursive: true });
  32. });
  33. after(async () => {
  34. await browser.electron.restoreAllMocks();
  35. await clearAppData();
  36. });
  37. it("PV4-21 safe checkpoint 倒序恢复文件后才提交 same-session branch", async function () {
  38. this.timeout(150000);
  39. await prepareFileCase(SAFE_PATH);
  40. await sendV4Prompt("E2E_V4_REWIND_FILE_SAFE 请更新 safe.txt");
  41. await approvePendingFileWriteIfRequired();
  42. await waitForV4TimelineContaining(
  43. "V4_REWIND_FILE_SAFE_ORIGINAL_REPLY",
  44. 90000,
  45. );
  46. await waitForFileContent(SAFE_PATH, "safe-after\n");
  47. const originalSessionId = (await getV4PaneSnapshot()).sessionId;
  48. await waitForV4Edit();
  49. const rowId = await beginFirstV4UserQueryEdit(
  50. "E2E_V4_REWIND_FILE_SAFE_EDITED 文件恢复后重发",
  51. );
  52. await clickV4EditWorkspaceReset(rowId);
  53. const rewindAck = await waitForLatestV4CommandAck(
  54. "editUserQuery",
  55. 30000,
  56. ).catch(async (error) => {
  57. const fileState = await readFile(SAFE_PATH, "utf8").catch(
  58. () => "<missing>",
  59. );
  60. throw new Error(
  61. `${error instanceof Error ? error.message : String(error)}; safe.txt=${JSON.stringify(fileState)}`,
  62. );
  63. });
  64. expect(rewindAck.status).toBe("accepted");
  65. await waitForV4TimelineContaining(
  66. "V4_REWIND_FILE_SAFE_EDITED_REPLY",
  67. 90000,
  68. );
  69. await waitForV4Pane(
  70. (snapshot) => !snapshot.canStop,
  71. "safe 组合 rewind 没有回到 idle",
  72. 60000,
  73. );
  74. expect(await readFile(SAFE_PATH, "utf8")).toBe("safe-before\n");
  75. const finalSnapshot = await getV4PaneSnapshot();
  76. expect(finalSnapshot.sessionId).toBe(originalSessionId);
  77. expect(finalSnapshot.timelineText).not.toContain(
  78. "V4_REWIND_FILE_SAFE_ORIGINAL_REPLY",
  79. );
  80. expect(finalSnapshot.timelineText).not.toContain(
  81. "Conversation rewind applied",
  82. );
  83. });
  84. it("PV4-22 external modified 先 blocked,新 commandId 降级后只裁对话", async function () {
  85. this.timeout(150000);
  86. await prepareFileCase(CONFLICT_PATH);
  87. await sendV4Prompt("E2E_V4_REWIND_FILE_CONFLICT 请更新 conflict.txt");
  88. await approvePendingFileWriteIfRequired();
  89. await waitForV4TimelineContaining(
  90. "V4_REWIND_FILE_CONFLICT_ORIGINAL_REPLY",
  91. 90000,
  92. );
  93. await waitForFileContent(CONFLICT_PATH, "conflict-after\n");
  94. await writeFile(CONFLICT_PATH, "external-wins\n", "utf8");
  95. const editedMarker = "E2E_V4_REWIND_FILE_CONFLICT_EDITED";
  96. const requestsBefore = await countUpstreamRequestsContaining(editedMarker);
  97. await waitForV4Edit();
  98. const rowId = await beginFirstV4UserQueryEdit(`${editedMarker} 只重置对话`);
  99. await clickV4EditWorkspaceReset(rowId);
  100. await waitForV4EditWorkspaceConflict(60000);
  101. expect(await readFile(CONFLICT_PATH, "utf8")).toBe("external-wins\n");
  102. expect((await getV4PaneSnapshot()).timelineText).toContain(
  103. "V4_REWIND_FILE_CONFLICT_ORIGINAL_REPLY",
  104. );
  105. expect(await countUpstreamRequestsContaining(editedMarker)).toBe(
  106. requestsBefore,
  107. );
  108. await clickV4EditConflictConversationOnly();
  109. await waitForV4TimelineContaining(
  110. "V4_REWIND_FILE_CONFLICT_EDITED_REPLY",
  111. 90000,
  112. );
  113. expect(await readFile(CONFLICT_PATH, "utf8")).toBe("external-wins\n");
  114. const finalSnapshot = await getV4PaneSnapshot();
  115. expect(finalSnapshot.timelineText).not.toContain(
  116. "V4_REWIND_FILE_CONFLICT_ORIGINAL_REPLY",
  117. );
  118. expect(await countUpstreamRequestsContaining(editedMarker)).toBe(
  119. requestsBefore + 1,
  120. );
  121. });
  122. });
  123. async function prepareFileCase(filePath: string) {
  124. await rm(CASE_ROOT, { force: true, recursive: true });
  125. await mkdir(CASE_ROOT, { recursive: true });
  126. await writeFile(
  127. filePath,
  128. filePath === SAFE_PATH ? "safe-before\n" : "conflict-before\n",
  129. "utf8",
  130. );
  131. await prepareV4ConversationE2E();
  132. await switchV4Mode("yolo");
  133. }
  134. async function waitForFileContent(filePath: string, expected: string) {
  135. await browser.waitUntil(
  136. async () => (await readFile(filePath, "utf8").catch(() => "")) === expected,
  137. { timeout: 60000, timeoutMsg: `${filePath} 没有收敛到期望内容` },
  138. );
  139. }
  140. async function approvePendingFileWriteIfRequired() {
  141. try {
  142. await waitForV4PermissionDialog(15000);
  143. expect(await approveV4Permission()).toBe(true);
  144. } catch {
  145. // yolo 模式会直接执行;不同 permission registry 状态下也可能显式请求一次授权。
  146. }
  147. }