conversation-session-bash-read-state.test.ts 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { mkdir, readFile, rm, writeFile } from "node:fs/promises";
  2. import { join } from "node:path";
  3. import { clearAppData, DEFAULT_WORKSPACE } from "../../../helpers/desktop-app.js";
  4. import {
  5. E2E_REPLY_TOKEN,
  6. countUpstreamRequests,
  7. listToolCallBlocks,
  8. prepareConversationE2E,
  9. sendPrompt,
  10. waitForAssistantMessageContaining,
  11. waitForChatState,
  12. waitForComposerText,
  13. waitForUpstreamRequestContaining,
  14. waitForToolCallBlockByToolName,
  15. waitForUserMessageContaining,
  16. } from "../../../helpers/conversation-session.js";
  17. import { ensureToolCrossProductFullAccessMode } from "../../../helpers/conversation-session-tool-cross-product.js";
  18. const CASE_ROOT = join(DEFAULT_WORKSPACE, "bash-read-state-e2e");
  19. const TARGET_RELATIVE_PATH = "bash-read-state-e2e/target.txt";
  20. const FORMATTER_RELATIVE_PATH = "bash-read-state-e2e/overwrite-target.mjs";
  21. const TARGET_PATH = join(DEFAULT_WORKSPACE, TARGET_RELATIVE_PATH);
  22. const FORMATTER_PATH = join(DEFAULT_WORKSPACE, FORMATTER_RELATIVE_PATH);
  23. const INITIAL_CONTENT = "alpha\nbeta\n";
  24. const FORMATTED_CONTENT = "delta\nbeta\n";
  25. const STALE_HINT = "Call Read before editing";
  26. const NOT_READ_ERROR = "File has not been read yet. Read it first before writing to it.";
  27. describe("会话区 Bash read-state E2E", () => {
  28. afterEach(async () => {
  29. await rm(CASE_ROOT, { recursive: true, force: true });
  30. });
  31. after(async () => {
  32. await browser.electron.restoreAllMocks();
  33. await rm(CASE_ROOT, { recursive: true, force: true });
  34. await clearAppData();
  35. });
  36. it("Bash cat 应允许后续 Edit,并在 formatter Bash 后向模型暴露 stale hint", async function () {
  37. this.timeout(180000);
  38. const runId = Date.now();
  39. const marker = `E2E_BASH_READ_STATE_${runId}`;
  40. await writeFixtures();
  41. await prepareConversationE2E();
  42. await ensureToolCrossProductFullAccessMode();
  43. const prompt = buildPrompt(marker);
  44. await sendPrompt(prompt);
  45. await waitForComposerText("", "Bash read-state 首发后输入框没有清空");
  46. await waitForUserMessageContaining(marker);
  47. await waitForUpstreamRequestContaining(marker, 60000);
  48. await waitForToolCallBlockByToolName("Bash", 90000);
  49. await waitForToolCallBlockByToolName("Edit", 90000);
  50. await waitForAssistantMessageContaining(E2E_REPLY_TOKEN);
  51. await waitForChatState(
  52. (snapshot) => snapshot.state === "idle" && snapshot.queueCount === 0,
  53. "Bash read-state 工具链没有回到 idle",
  54. 120000,
  55. );
  56. const toolBlocks = await listToolCallBlocks();
  57. expect(toolBlocks.filter((block) => block.toolName === "Bash").length).toBeGreaterThanOrEqual(2);
  58. expect(toolBlocks.some((block) => block.toolName === "Edit")).toBe(true);
  59. expect(await readFile(TARGET_PATH, "utf-8")).toBe(FORMATTED_CONTENT);
  60. expect(await countUpstreamRequests({ includes: [STALE_HINT] })).toBeGreaterThan(0);
  61. expect(await countUpstreamRequests({ includes: [NOT_READ_ERROR] })).toBe(0);
  62. });
  63. });
  64. function buildPrompt(marker: string) {
  65. return [
  66. `${marker}: Follow these tool steps exactly.`,
  67. `1. Use Bash with exactly this command: cat ${TARGET_RELATIVE_PATH}`,
  68. `2. Use Edit on ${TARGET_PATH}; replace exactly "alpha" with "gamma".`,
  69. `3. Use Bash with exactly this command: node ${FORMATTER_RELATIVE_PATH} --fix`,
  70. `4. Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`,
  71. "Do not use the Read tool in this case.",
  72. ].join(" ");
  73. }
  74. async function writeFixtures() {
  75. await mkdir(CASE_ROOT, { recursive: true });
  76. await writeFile(TARGET_PATH, INITIAL_CONTENT, "utf-8");
  77. await writeFile(
  78. FORMATTER_PATH,
  79. [
  80. "import { writeFile } from 'node:fs/promises';",
  81. "await new Promise((resolve) => setTimeout(resolve, 30));",
  82. `await writeFile(${JSON.stringify(TARGET_PATH)}, ${JSON.stringify(FORMATTED_CONTENT)});`,
  83. "console.log('formatted target');",
  84. "",
  85. ].join("\n"),
  86. "utf-8",
  87. );
  88. }