conversation-session-tool-history-order.test.ts 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // D19:保留更早轮的工具记录,再编辑最新用户消息,核对真实 provider 历史。
  2. // 运行时指定 ZCODE_E2E_UPSTREAM_CONTEXT_WINDOW=128000,避免待审默认小窗口插入自动压缩。
  3. import { mkdir, rm, writeFile } from "node:fs/promises";
  4. import { join } from "node:path";
  5. import { clearAppData } from "#e2e/helpers/desktop-app";
  6. import { getUpstreamRequestEvidence } from "#e2e/helpers/conversation-session-network";
  7. import { ensureToolCrossProductFullAccessMode } from "#e2e/helpers/conversation-session-tool-cross-product";
  8. import { resolveE2ERuntimePath } from "#e2e/helpers/e2e-runtime-paths";
  9. import {
  10. editFirstV4UserQuery,
  11. prepareV4ConversationE2E,
  12. sendV4Prompt,
  13. waitForV4Edit,
  14. waitForV4Pane,
  15. waitForV4TimelineContaining,
  16. } from "#e2e/helpers/v4-conversation";
  17. const fixtureRoot = resolveE2ERuntimePath("conversation-session-tool-history-order");
  18. describe("D19 tool history order after latest-user edit", () => {
  19. after(async () => {
  20. await browser.electron.restoreAllMocks();
  21. await clearAppData();
  22. await rm(fixtureRoot, { recursive: true, force: true });
  23. });
  24. it("keeps earlier Bash/Read calls and results identical after edit", async function () {
  25. this.timeout(180000);
  26. await mkdir(fixtureRoot, { recursive: true });
  27. await writeFile(join(fixtureRoot, "read.txt"), "D19_READ_RESULT\n");
  28. await prepareV4ConversationE2E();
  29. await ensureToolCrossProductFullAccessMode();
  30. await sendV4Prompt("E2E_TOOL_ORDER_SOURCE 按顺序声明 Bash 和 Read,然后回复。");
  31. await waitForV4TimelineContaining("D19_SOURCE_DONE", 60000);
  32. const initial = await waitForV4Pane(
  33. (state) => !state.canStop && Boolean(state.sessionId),
  34. "首轮未结束",
  35. );
  36. await sendV4Prompt("E2E_TOOL_ORDER_LATER 后续问题。");
  37. await waitForV4TimelineContaining("D19_LATER_DONE", 60000);
  38. await waitForV4Edit();
  39. await editFirstV4UserQuery("E2E_TOOL_ORDER_EDITED 编辑后续问题。");
  40. await waitForV4TimelineContaining("D19_EDITED_DONE", 60000);
  41. const final = await waitForV4Pane((state) => !state.canStop, "编辑重跑未结束");
  42. expect(final.sessionId).toBe(initial.sessionId);
  43. const evidence = await getUpstreamRequestEvidence({
  44. includes: ["E2E_TOOL_ORDER_SOURCE"],
  45. excludes: ["Generate a concise title"],
  46. });
  47. const historyFor = (fixtureId: string) => {
  48. const record = evidence.find((entry) => entry.fixtureId === fixtureId);
  49. expect(record).toBeDefined();
  50. expect(record!.status).toBe("complete");
  51. const body = record!.requestJson as { messages: Array<{ content: unknown }> };
  52. return (
  53. body.messages
  54. .flatMap((message) => (Array.isArray(message.content) ? message.content : []))
  55. .filter((block: { type: string; id?: string; tool_use_id?: string }) =>
  56. ["toolu_d19_bash", "toolu_d19_read"].includes(block.id ?? block.tool_use_id ?? ""),
  57. )
  58. // 缓存断点随请求尾部移动,不属于工具历史;仍逐字段比较调用 ID、参数、结果和顺序。
  59. .map(({ cache_control: _cacheControl, ...block }) => block)
  60. );
  61. };
  62. const live = historyFor("d19-followup");
  63. expect(live.map((block) => block.id ?? block.tool_use_id)).toEqual([
  64. "toolu_d19_bash",
  65. "toolu_d19_read",
  66. "toolu_d19_bash",
  67. "toolu_d19_read",
  68. ]);
  69. expect(historyFor("d19-later")).toEqual(live);
  70. expect(historyFor("d19-edited")).toEqual(live);
  71. expect(final.timelineText).toContain("D19_SOURCE_DONE");
  72. expect(final.timelineText).not.toContain("D19_LATER_DONE");
  73. });
  74. });