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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 "#e2e/helpers/desktop-app";
  7. import { countUpstreamRequestsContaining } from "#e2e/helpers/conversation-session-network";
  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 "#e2e/helpers/v4-conversation";
  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("V4_REWIND_FILE_SAFE_ORIGINAL_REPLY", 90000);
  43. await waitForFileContent(SAFE_PATH, "safe-after\n");
  44. const originalSessionId = (await getV4PaneSnapshot()).sessionId;
  45. await waitForV4Edit();
  46. const rowId = await beginFirstV4UserQueryEdit("E2E_V4_REWIND_FILE_SAFE_EDITED 文件恢复后重发");
  47. await clickV4EditWorkspaceReset(rowId);
  48. const rewindAck = await waitForLatestV4CommandAck("editUserQuery", 30000).catch(
  49. async (error) => {
  50. const fileState = await readFile(SAFE_PATH, "utf8").catch(() => "<missing>");
  51. throw new Error(
  52. `${error instanceof Error ? error.message : String(error)}; safe.txt=${JSON.stringify(fileState)}`,
  53. );
  54. },
  55. );
  56. expect(rewindAck.status).toBe("accepted");
  57. await waitForV4TimelineContaining("V4_REWIND_FILE_SAFE_EDITED_REPLY", 90000);
  58. await waitForV4Pane((snapshot) => !snapshot.canStop, "safe 组合 rewind 没有回到 idle", 60000);
  59. expect(await readFile(SAFE_PATH, "utf8")).toBe("safe-before\n");
  60. const finalSnapshot = await getV4PaneSnapshot();
  61. expect(finalSnapshot.sessionId).toBe(originalSessionId);
  62. expect(finalSnapshot.timelineText).not.toContain("V4_REWIND_FILE_SAFE_ORIGINAL_REPLY");
  63. expect(finalSnapshot.timelineText).not.toContain("Conversation rewind applied");
  64. });
  65. it("PV4-22 external modified 先 blocked,新 commandId 降级后只裁对话", async function () {
  66. this.timeout(150000);
  67. await prepareFileCase(CONFLICT_PATH);
  68. await sendV4Prompt("E2E_V4_REWIND_FILE_CONFLICT 请更新 conflict.txt");
  69. await approvePendingFileWriteIfRequired();
  70. await waitForV4TimelineContaining("V4_REWIND_FILE_CONFLICT_ORIGINAL_REPLY", 90000);
  71. await waitForFileContent(CONFLICT_PATH, "conflict-after\n");
  72. await writeFile(CONFLICT_PATH, "external-wins\n", "utf8");
  73. const editedMarker = "E2E_V4_REWIND_FILE_CONFLICT_EDITED";
  74. const requestsBefore = await countUpstreamRequestsContaining(editedMarker);
  75. await waitForV4Edit();
  76. const rowId = await beginFirstV4UserQueryEdit(`${editedMarker} 只重置对话`);
  77. await clickV4EditWorkspaceReset(rowId);
  78. await waitForV4EditWorkspaceConflict(60000);
  79. expect(await readFile(CONFLICT_PATH, "utf8")).toBe("external-wins\n");
  80. expect((await getV4PaneSnapshot()).timelineText).toContain(
  81. "V4_REWIND_FILE_CONFLICT_ORIGINAL_REPLY",
  82. );
  83. expect(await countUpstreamRequestsContaining(editedMarker)).toBe(requestsBefore);
  84. await clickV4EditConflictConversationOnly();
  85. await waitForV4TimelineContaining("V4_REWIND_FILE_CONFLICT_EDITED_REPLY", 90000);
  86. expect(await readFile(CONFLICT_PATH, "utf8")).toBe("external-wins\n");
  87. const finalSnapshot = await getV4PaneSnapshot();
  88. expect(finalSnapshot.timelineText).not.toContain("V4_REWIND_FILE_CONFLICT_ORIGINAL_REPLY");
  89. expect(await countUpstreamRequestsContaining(editedMarker)).toBe(requestsBefore + 1);
  90. });
  91. });
  92. async function prepareFileCase(filePath: string) {
  93. await rm(CASE_ROOT, { force: true, recursive: true });
  94. await mkdir(CASE_ROOT, { recursive: true });
  95. await writeFile(filePath, filePath === SAFE_PATH ? "safe-before\n" : "conflict-before\n", "utf8");
  96. await prepareV4ConversationE2E();
  97. await switchV4Mode("yolo");
  98. }
  99. async function waitForFileContent(filePath: string, expected: string) {
  100. await browser.waitUntil(
  101. async () => (await readFile(filePath, "utf8").catch(() => "")) === expected,
  102. { timeout: 60000, timeoutMsg: `${filePath} 没有收敛到期望内容` },
  103. );
  104. }
  105. async function approvePendingFileWriteIfRequired() {
  106. try {
  107. await waitForV4PermissionDialog(15000);
  108. expect(await approveV4Permission()).toBe(true);
  109. } catch {
  110. // yolo 模式会直接执行;不同 permission registry 状态下也可能显式请求一次授权。
  111. }
  112. }