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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import {
  2. expandAssistantHistoriesWithContent,
  3. expandVisibleSummaryGroups,
  4. } from "#e2e/helpers/conversation-session-tool-diagnostics";
  5. import { startDraftFromProject } from "#e2e/helpers/project-sidebar";
  6. import { mkdir, readFile, rm, writeFile } from "node:fs/promises";
  7. import { join } from "node:path";
  8. import { clearAppData, DEFAULT_WORKSPACE } from "#e2e/helpers/desktop-app";
  9. import {
  10. E2E_REPLY_TOKEN,
  11. prepareV4ConversationE2E,
  12. sendV4Prompt,
  13. waitForV4AssistantMessageContaining,
  14. waitForV4ConversationState,
  15. waitForV4ComposerText,
  16. waitForV4UserMessageContaining,
  17. } from "#e2e/helpers/v4-conversation";
  18. import {
  19. countUpstreamRequests,
  20. waitForUpstreamRequestContaining,
  21. } from "#e2e/helpers/conversation-session-network";
  22. import {
  23. listToolCallBlocks,
  24. waitForToolCallBlockByToolName,
  25. } from "#e2e/helpers/conversation-session-tool";
  26. import { ensureToolCrossProductFullAccessMode } from "#e2e/helpers/conversation-session-tool-cross-product";
  27. const CASE_ROOT = join(DEFAULT_WORKSPACE, "bash-read-state-e2e");
  28. const TARGET_RELATIVE_PATH = "bash-read-state-e2e/target.txt";
  29. const FORMATTER_RELATIVE_PATH = "bash-read-state-e2e/overwrite-target.mjs";
  30. const TARGET_PATH = join(DEFAULT_WORKSPACE, TARGET_RELATIVE_PATH);
  31. const FORMATTER_PATH = join(DEFAULT_WORKSPACE, FORMATTER_RELATIVE_PATH);
  32. const INITIAL_CONTENT = "alpha\nbeta\n";
  33. const FORMATTED_CONTENT = "delta\nbeta\n";
  34. const STALE_HINT = "Call Read before editing";
  35. const NOT_READ_ERROR = "File has not been read yet. Read it first before writing to it.";
  36. describe("会话区 Bash read-state E2E", () => {
  37. afterEach(async () => {
  38. await rm(CASE_ROOT, { recursive: true, force: true });
  39. });
  40. after(async () => {
  41. await browser.electron.restoreAllMocks();
  42. await rm(CASE_ROOT, { recursive: true, force: true });
  43. await clearAppData();
  44. });
  45. it("Bash cat 应允许后续 Edit,并在 formatter Bash 后向模型暴露 stale hint", async function () {
  46. this.timeout(180000);
  47. const runId = Date.now();
  48. const marker = `E2E_BASH_READ_STATE_${runId}`;
  49. await writeFixtures();
  50. await prepareV4ConversationE2E();
  51. await startDraftFromProject(DEFAULT_WORKSPACE);
  52. await ensureToolCrossProductFullAccessMode();
  53. const prompt = buildPrompt(marker);
  54. await sendV4Prompt(prompt);
  55. await waitForV4ComposerText("", "Bash read-state 首发后输入框没有清空");
  56. await waitForV4UserMessageContaining(marker);
  57. await waitForUpstreamRequestContaining(marker, 60000);
  58. await waitForToolCallBlockByToolName("Bash", 90000);
  59. await waitForToolCallBlockByToolName("Edit", 90000);
  60. await waitForV4AssistantMessageContaining(E2E_REPLY_TOKEN);
  61. await waitForV4ConversationState(
  62. (snapshot) => snapshot.state === "idle" && snapshot.queueCount === 0,
  63. "Bash read-state 工具链没有回到 idle",
  64. 120000,
  65. );
  66. // 完成后历史和工具组默认收起;先展开真实内容,再逐条检查 Bash/Edit。
  67. await expandAssistantHistoriesWithContent();
  68. await browser.waitUntil(
  69. async () => {
  70. await expandVisibleSummaryGroups();
  71. return (
  72. (await listToolCallBlocks()).filter((block) => block.toolName === "Bash").length === 2
  73. );
  74. },
  75. { timeout: 15000, timeoutMsg: "Bash 历史未展开" },
  76. );
  77. const toolBlocks = await listToolCallBlocks();
  78. expect(toolBlocks.filter((block) => block.toolName === "Bash").length).toBeGreaterThanOrEqual(
  79. 2,
  80. );
  81. expect(toolBlocks.some((block) => block.toolName === "Edit")).toBe(true);
  82. expect(await readFile(TARGET_PATH, "utf-8")).toBe(FORMATTED_CONTENT);
  83. expect(await countUpstreamRequests({ includes: [STALE_HINT] })).toBeGreaterThan(0);
  84. expect(await countUpstreamRequests({ includes: [NOT_READ_ERROR] })).toBe(0);
  85. });
  86. });
  87. function buildPrompt(marker: string) {
  88. return [
  89. `${marker}: Follow these tool steps exactly.`,
  90. `1. Use Bash with exactly this command: cat ${TARGET_RELATIVE_PATH}`,
  91. `2. Use Edit on ${TARGET_PATH}; replace exactly "alpha" with "gamma".`,
  92. `3. Use Bash with exactly this command: node ${FORMATTER_RELATIVE_PATH} --fix`,
  93. `4. Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`,
  94. "Do not use the Read tool in this case.",
  95. ].join(" ");
  96. }
  97. async function writeFixtures() {
  98. await mkdir(CASE_ROOT, { recursive: true });
  99. await writeFile(TARGET_PATH, INITIAL_CONTENT, "utf-8");
  100. await writeFile(
  101. FORMATTER_PATH,
  102. [
  103. "import { writeFile } from 'node:fs/promises';",
  104. "await new Promise((resolve) => setTimeout(resolve, 30));",
  105. `await writeFile(${JSON.stringify(TARGET_PATH)}, ${JSON.stringify(FORMATTED_CONTENT)});`,
  106. "console.log('formatted target');",
  107. "",
  108. ].join("\n"),
  109. "utf-8",
  110. );
  111. }