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

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