conversation-session-r3-task-control.test.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import { mkdir, writeFile } from "node:fs/promises";
  2. import { resolve } from "node:path";
  3. import { clearAppData } from "#e2e/helpers/desktop-app";
  4. import {
  5. getUpstreamRequestRecordCount,
  6. waitForUpstreamRequest,
  7. } from "#e2e/helpers/conversation-session-network";
  8. import {
  9. connectSSHWorkspaceAndSendFirstTurn,
  10. prepareSSHLifecycle,
  11. type SSHLifecycleProvider,
  12. } from "#e2e/helpers/ssh-remote-lifecycle";
  13. import { runSSHCommand } from "#e2e/helpers/ssh-remote-p0";
  14. import {
  15. approveV4Permission,
  16. clickV4PausedQueueResume,
  17. clickV4QueueItemDelete,
  18. clickV4Stop,
  19. getLatestV4CommandAck,
  20. getV4ComposerText,
  21. getV4PaneSnapshot,
  22. getV4QueueAutoDrain,
  23. getV4QueueItems,
  24. sendV4Prompt,
  25. waitForV4AssistantMessageContaining,
  26. waitForV4ConversationState,
  27. waitForV4Pane,
  28. waitForV4PausedQueueBanner,
  29. waitForV4PermissionDialogInComposerDock,
  30. waitForV4QueueCount,
  31. waitForV4TimelineContaining,
  32. } from "#e2e/helpers/v4-conversation";
  33. import {
  34. openV4QueueItemEditInComposer,
  35. waitForV4ComposerEditClosed,
  36. } from "#e2e/helpers/v4-composer-edit";
  37. const CASE_MARKER = "E2E_R3_TASK_CONTROL";
  38. const artifactDir = resolve(process.env.ZCODE_E2E_ARTIFACT_DIR ?? ".e2e-artifacts/r3-task-control");
  39. const REMOTE_PERMISSION_DIR = "/home/dev/r3-workspace-a/.zcode-e2e/r3-task-control";
  40. const REMOTE_PERMISSION_FILE = `${REMOTE_PERMISSION_DIR}/permission-output.txt`;
  41. describe(`${CASE_MARKER}: 远端 running 窗口内的 stop/queue 与权限阻塞`, () => {
  42. let provider: SSHLifecycleProvider | null = null;
  43. const facts: Record<string, unknown> = {};
  44. after(async () => {
  45. await mkdir(artifactDir, { recursive: true });
  46. await writeFile(resolve(artifactDir, "task-control.json"), JSON.stringify(facts, null, 2));
  47. await provider?.tunnel.close().catch(() => undefined);
  48. provider = null;
  49. await browser.electron.restoreAllMocks();
  50. await clearAppData();
  51. });
  52. it("R3A-TASK-02/03 busy 入队、删除/编辑、stop 保留队列、resume FIFO 消费;权限在远端阻塞并执行", async function () {
  53. this.timeout(30 * 60_000);
  54. const prepared = await prepareSSHLifecycle(CASE_MARKER, { apiType: "anthropic-messages" });
  55. provider = prepared.provider;
  56. const [workspaceA] = prepared.config.workspacePaths;
  57. await runSSHCommand(prepared.config, `rm -rf "${REMOTE_PERMISSION_DIR}"`);
  58. const taskA = await connectSSHWorkspaceAndSendFirstTurn({
  59. caseMarker: CASE_MARKER,
  60. config: prepared.config,
  61. label: "A",
  62. provider,
  63. workspacePath: workspaceA,
  64. });
  65. // 1. running 窗口由 fixture 闸门保持到 Stop,避免排队/编辑期间自然结束。
  66. const slowStart = await getUpstreamRequestRecordCount();
  67. await sendV4Prompt(`${CASE_MARKER}_SLOW 慢慢回答`);
  68. await waitForV4TimelineContaining("R3_TASK_STREAMING", 60_000);
  69. await waitForV4Pane(
  70. (s) => s.canStop && s.sessionId === taskA.taskId,
  71. "远端 turn 没有进入 running",
  72. 45_000,
  73. );
  74. await waitForUpstreamRequest(
  75. { lastUserMessageIncludes: [`${CASE_MARKER}_SLOW`] },
  76. `${CASE_MARKER}: 慢流请求没有到达回放`,
  77. 30_000,
  78. { afterIndex: slowStart - 1 },
  79. );
  80. // 2. busy 连续输入两条 → 入队 FIFO;来源 commandId 经 ACK 可见。
  81. await sendV4Prompt(`${CASE_MARKER}_Q1 第一条排队`);
  82. await waitForV4QueueCount(1, 30_000);
  83. await sendV4Prompt(`${CASE_MARKER}_Q2 第二条排队`);
  84. await waitForV4QueueCount(2, 30_000);
  85. let queue = await getV4QueueItems();
  86. expect(queue.map((item) => item.text.includes("_Q1"))).toEqual([true, false]);
  87. facts.queueAfterEnqueue = queue.map((item) => item.text.slice(0, 40));
  88. // 3. 删除 Q1 → 权威投影只剩 Q2;编辑 Q2 原位冻结,修改后原地替换 → queueItemId 不变。
  89. expect(await clickV4QueueItemDelete(queue[0]!.queueItemId)).toBe(true);
  90. await waitForV4QueueCount(1, 30_000);
  91. queue = await getV4QueueItems();
  92. expect(queue[0]!.text).toContain("_Q2");
  93. const q2Id = queue[0]!.queueItemId;
  94. // 编辑队列项:原位冻结并进入 composer 编辑态,发送走 editQueueItem 原地替换(ID 不变)。
  95. await openV4QueueItemEditInComposer(q2Id, 30_000);
  96. expect(await getV4QueueItems()).toHaveLength(1);
  97. await browser.waitUntil(async () => ((await getV4ComposerText()) ?? "").includes("_Q2"), {
  98. timeout: 15_000,
  99. timeoutMsg: `${CASE_MARKER}: 编辑态没有载入队列项原文`,
  100. });
  101. await sendV4Prompt(`${CASE_MARKER}_Q3 修改后重发`);
  102. await waitForV4ComposerEditClosed(`${CASE_MARKER}: editQueueItem 后没有退出编辑态`, 30_000);
  103. await browser.waitUntil(
  104. async () => ((await getV4QueueItems())[0]?.text ?? "").includes("_Q3"),
  105. { timeout: 30_000, timeoutMsg: `${CASE_MARKER}: 队列项没有原地替换为 Q3` },
  106. );
  107. queue = await getV4QueueItems();
  108. expect(queue).toHaveLength(1);
  109. expect(queue[0]!.queueItemId).toBe(q2Id);
  110. facts.queueAckEdit = await getLatestV4CommandAck("editQueueItem").catch(() => null);
  111. // 4. stop:当前流收口、队列原样保留且 autoDrain=false、显示暂停提示;无丢失无重复。
  112. expect((await getV4PaneSnapshot()).canStop).toBe(true);
  113. await clickV4Stop();
  114. await waitForV4Pane((s) => !s.canStop, `${CASE_MARKER}: stop 后没有收口`, 30_000);
  115. await waitForV4PausedQueueBanner(30_000);
  116. expect(await getV4QueueAutoDrain()).toBe(false);
  117. queue = await getV4QueueItems();
  118. expect(queue).toHaveLength(1);
  119. expect(queue[0]!.text).toContain("_Q3");
  120. facts.stopAck = await getLatestV4CommandAck("stop").catch(() => null);
  121. // 5. resume:按 FIFO 消费队首为新 turn,请求真实到达远端→回放;回到 idle 且队列为空。
  122. const resumeStart = await getUpstreamRequestRecordCount();
  123. await clickV4PausedQueueResume();
  124. await waitForUpstreamRequest(
  125. { lastUserMessageIncludes: [`${CASE_MARKER}_Q3`] },
  126. `${CASE_MARKER}: 队首消费请求没有到达回放`,
  127. 60_000,
  128. { afterIndex: resumeStart - 1 },
  129. );
  130. await waitForV4AssistantMessageContaining("R3_TASK_Q_REPLY", 60_000);
  131. await waitForV4QueueCount(0, 30_000);
  132. await waitForV4ConversationState(
  133. (s) => s.taskId === taskA.taskId && s.state === "idle" && s.queueCount === 0,
  134. `${CASE_MARKER}: 队列消费后没有回到 idle`,
  135. 60_000,
  136. );
  137. // 6. TASK-03:Write 工具触发远端权限阻塞;允许后工具在远端真实执行并出终态文本。
  138. const permissionStart = await getUpstreamRequestRecordCount();
  139. await sendV4Prompt(`${CASE_MARKER}_PERMISSION 请写入文件 ${REMOTE_PERMISSION_FILE}`);
  140. await waitForUpstreamRequest(
  141. { lastUserMessageIncludes: [`${CASE_MARKER}_PERMISSION`] },
  142. `${CASE_MARKER}: 权限请求 turn 没有到达回放`,
  143. 60_000,
  144. { afterIndex: permissionStart - 1 },
  145. );
  146. const dock = await waitForV4PermissionDialogInComposerDock(60_000);
  147. expect(dock.inDock).toBe(true);
  148. const beforeApprove = await runSSHCommand(
  149. prepared.config,
  150. `test -f "${REMOTE_PERMISSION_FILE}" && echo exists || echo absent`,
  151. );
  152. expect(beforeApprove.stdout.trim()).toBe("absent");
  153. expect(await approveV4Permission()).toBe(true);
  154. await waitForV4TimelineContaining("R3_TASK_PERMISSION_DONE", 90_000);
  155. const afterApprove = await runSSHCommand(prepared.config, `cat "${REMOTE_PERMISSION_FILE}"`);
  156. expect(afterApprove.stdout).toContain("R3_TASK_PERMISSION_RESULT");
  157. facts.permissionFileOnRemote = afterApprove.stdout.trim();
  158. facts.permissionAck = await getLatestV4CommandAck("resolveInteraction").catch(() => null);
  159. await waitForV4ConversationState(
  160. (s) => s.taskId === taskA.taskId && s.state === "idle",
  161. `${CASE_MARKER}: 权限 turn 没有回到 idle`,
  162. 60_000,
  163. );
  164. const snapshot = await getV4PaneSnapshot();
  165. expect(snapshot.canStop).toBe(false);
  166. });
  167. });