conversation-session-bash-foreground-sleep-block.test.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Z03 前台长 sleep 拦截(docs/conversation-session-case-catalog.md「Z」分组)。规格见
  2. // apps/zcode-cli/docs/design/v2/tool/04-bash.md「前台 sleep 拦截」与「Prompt 约束」末尾的描述三处文字。
  3. // 断言读上游请求体:首个请求的 Bash 描述、live continuation 与完整 CLI cold restart 后的 error tool_result。
  4. import { access } from "node:fs/promises";
  5. import { join } from "node:path";
  6. import {
  7. clearAppData,
  8. DEFAULT_WORKSPACE,
  9. waitForDefaultWorkspaceReady,
  10. } from "#e2e/helpers/desktop-app";
  11. import {
  12. getLatestUpstreamToolResultByToolCallId,
  13. getUpstreamRequestToolContract,
  14. } from "#e2e/helpers/conversation-session-network";
  15. import {
  16. expandToolCallDetails,
  17. waitForFoldedToolCallBlockByToolCallId,
  18. } from "#e2e/helpers/conversation-session-tool";
  19. import { reloadElectronSessionSafely } from "#e2e/helpers/e2e-electron-reload";
  20. import { waitForUpstreamNetworkCapture } from "#e2e/helpers/upstream-capture";
  21. import {
  22. prepareV4ConversationE2E,
  23. selectV4TaskById,
  24. sendV4Prompt,
  25. waitForV4AssistantMessageContaining,
  26. waitForV4Pane,
  27. } from "#e2e/helpers/v4-conversation";
  28. const PROMPT_MARKER = "E2E_BASH_SLEEP_BLOCK";
  29. const COLD_PROMPT_MARKER = "E2E_BASH_SLEEP_BLOCK_COLD_REPLAY";
  30. const LIVE_DONE = "E2E_BASH_SLEEP_BLOCK_LIVE_OK";
  31. const COLD_DONE = "E2E_BASH_SLEEP_BLOCK_COLD_OK";
  32. const TOOL_CALL_ID = "toolu_e2e_bash_sleep_block";
  33. const BLOCKED =
  34. "Blocked: sleep 30 followed by: echo E2E_BASH_SLEEP_BLOCK_RAN. To wait for a condition, run an until-loop with run_in_background: true (e.g. `until <check>; do sleep 2; done`); you are notified when it exits. To wait for a command you started, use run_in_background: true. Do not chain shorter sleeps to work around this block.";
  35. const TOOL_RESULT = `<tool_use_error>${BLOCKED}</tool_use_error>`;
  36. const DESCRIPTION_LINES = [
  37. "\n- `cd` carries to the next call only in the main session, if the command succeeds and ends inside the working directories. Outside them the shell returns to the start folder; otherwise it stays where it was.",
  38. "No `&` needed. Foreground `sleep` of 25 seconds or more is blocked; to wait on a condition, run an until-loop with `run_in_background`.\n",
  39. "\n- Use the `gh` CLI for GitHub operations (PRs, issues, API).\n- GitHub notifies the owner of any `@name` written in a PR, issue, or comment.",
  40. ];
  41. describe("Z03 前台长 sleep 拦截 E2E", () => {
  42. after(async () => {
  43. await browser.electron.restoreAllMocks();
  44. await clearAppData();
  45. });
  46. it("前台 sleep 30 在执行前被拒绝:工具卡显示原因,live 与 cold replay 的 error tool_result 相同", async function () {
  47. this.timeout(240000);
  48. await prepareV4ConversationE2E();
  49. const runId = Date.now();
  50. const prompt = `${PROMPT_MARKER}_${runId}: wait for the build with the sleep fixture.`;
  51. await sendV4Prompt(prompt);
  52. await waitForUpstreamNetworkCapture(prompt);
  53. // 描述三处文字随 tools 段发出;旧的 cd 行不再出现。
  54. const bash = (await getUpstreamRequestToolContract(
  55. { lastUserMessageIncludes: [`${PROMPT_MARKER}_${runId}`] },
  56. "Bash",
  57. )) as { description?: string } | null;
  58. for (const line of DESCRIPTION_LINES) expect(bash?.description).toContain(line);
  59. expect(bash?.description).not.toContain("Working directory persists between calls");
  60. const completed = await waitForV4Pane(
  61. (snapshot) =>
  62. snapshot.sessionId !== null &&
  63. snapshot.sessionId !== "draft" &&
  64. !snapshot.canStop &&
  65. snapshot.timelineText.includes(LIVE_DONE),
  66. "sleep 拦截的 continuation 没有完成",
  67. 60000,
  68. );
  69. const sessionId = completed.sessionId;
  70. if (!sessionId || sessionId === "draft") {
  71. throw new Error(`sleep 拦截 case 缺少真实 sessionId: ${sessionId ?? "null"}`);
  72. }
  73. expect(await getLatestUpstreamToolResultByToolCallId(TOOL_CALL_ID)).toEqual({
  74. content: TOOL_RESULT,
  75. isError: true,
  76. toolCallId: TOOL_CALL_ID,
  77. });
  78. // 命令没有启动:后半段写的文件不存在。
  79. await expect(access(join(DEFAULT_WORKSPACE, "sleep-block-ran.txt"))).rejects.toThrow();
  80. // 工具卡是失败状态,展开后显示不带标签的原因。
  81. const card = await waitForFoldedToolCallBlockByToolCallId(TOOL_CALL_ID, 30000);
  82. expect(card.status).toBe("failed");
  83. await expandToolCallDetails(TOOL_CALL_ID);
  84. await browser.waitUntil(
  85. async () => {
  86. const text = await browser.execute(
  87. (id: string) => document.querySelector(`[data-tool-call-id="${id}"]`)?.textContent ?? "",
  88. TOOL_CALL_ID,
  89. );
  90. return text.includes("Blocked: sleep 30 followed by: echo E2E_BASH_SLEEP_BLOCK_RAN.");
  91. },
  92. { timeout: 30000, timeoutMsg: "失败工具卡没有显示拦截原因" },
  93. );
  94. const cardText = await browser.execute(
  95. (id: string) => document.querySelector(`[data-tool-call-id="${id}"]`)?.textContent ?? "",
  96. TOOL_CALL_ID,
  97. );
  98. expect(cardText).not.toContain("<tool_use_error>");
  99. // 完整重启 Electron、Host 与 CLI:tool_result 必须来自持久化历史。
  100. await reloadElectronSessionSafely(browser);
  101. await waitForDefaultWorkspaceReady(60000);
  102. await selectV4TaskById(sessionId, 60000);
  103. await waitForV4Pane(
  104. (snapshot) => snapshot.sessionId === sessionId && !snapshot.canStop,
  105. `CLI cold restart 后没有恢复 session ${sessionId}`,
  106. 60000,
  107. );
  108. const coldPrompt = `${COLD_PROMPT_MARKER}_${runId}: reply with exactly ${COLD_DONE}.`;
  109. await sendV4Prompt(coldPrompt);
  110. const coldRequest = await waitForUpstreamNetworkCapture(coldPrompt);
  111. expect(coldRequest.statusCode).toBe(200);
  112. await waitForV4AssistantMessageContaining(COLD_DONE, 60000);
  113. expect(await getLatestUpstreamToolResultByToolCallId(TOOL_CALL_ID)).toEqual({
  114. content: TOOL_RESULT,
  115. isError: true,
  116. toolCallId: TOOL_CALL_ID,
  117. });
  118. });
  119. });