conversationRowSharing.test.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Conversation row 结构共享单测(docs/performance/conversation-row-structural-sharing.md)。
  2. import { describe, expect, it } from "vitest";
  3. import type { ConversationRow, ToolCallRow } from "@zcode/shared/zcode-protocol-v4";
  4. import { ConversationRowPool, shareStructure } from "@/v4/conversationRowSharing.js";
  5. /** 模拟一次反序列化:内容相同、引用全新(包括字符串)。 */
  6. function fresh<T>(value: T): T {
  7. return JSON.parse(JSON.stringify(value)) as T;
  8. }
  9. function bigToolRow(rowId: number, status: ToolCallRow["status"] = "running"): ToolCallRow {
  10. const content = `内容-${rowId}-`.repeat(2000);
  11. return {
  12. rowId,
  13. turnId: "t1",
  14. createdAt: 1,
  15. createdAtSeq: rowId,
  16. kind: "toolCall",
  17. toolCallId: `call-${rowId}`,
  18. toolName: "Write",
  19. status,
  20. input: { file_path: "/a.txt", content },
  21. inputText: JSON.stringify({ file_path: "/a.txt", content }),
  22. };
  23. }
  24. describe("shareStructure", () => {
  25. it("深相等时返回 prev 本身", () => {
  26. const prev = { a: 1, b: { c: ["x", { d: null }] } };
  27. expect(shareStructure(prev, fresh(prev))).toBe(prev);
  28. });
  29. it("部分变化时新建外壳,复用未变化的子值", () => {
  30. const prev = { keep: { deep: [1, 2] }, change: { v: 1 }, text: "长文本" };
  31. const next = { keep: { deep: [1, 2] }, change: { v: 2 }, text: "长文本" };
  32. const shared = shareStructure(prev, next);
  33. expect(shared).not.toBe(prev);
  34. expect(shared).toEqual(next);
  35. expect(shared.keep).toBe(prev.keep);
  36. expect(shared.change).toBe(next.change);
  37. });
  38. it("不修改 next,保留 next 的键顺序", () => {
  39. const prev = { a: { x: 1 }, b: 1 };
  40. const next = { b: 2, a: { x: 1 } };
  41. const snapshot = JSON.stringify(next);
  42. const nextA = next.a;
  43. const shared = shareStructure(prev, next);
  44. expect(JSON.stringify(next)).toBe(snapshot);
  45. expect(next.a).toBe(nextA);
  46. expect(Object.keys(shared)).toEqual(["b", "a"]);
  47. });
  48. it("键集合不同或类型不同时不复用 prev", () => {
  49. expect(shareStructure({ a: 1 }, { a: 1, b: undefined })).toEqual({ a: 1, b: undefined });
  50. expect(shareStructure({ a: 1, b: 2 }, { a: 1 })).toEqual({ a: 1 });
  51. expect(shareStructure([1, 2], [1, 2, 3])).toEqual([1, 2, 3]);
  52. const arr = [1];
  53. expect(shareStructure({ 0: 1 }, arr)).toBe(arr);
  54. expect(shareStructure(1, 2)).toBe(2);
  55. });
  56. it("数组元素逐个共享", () => {
  57. const prev = [{ id: 1 }, { id: 2 }];
  58. const next = [{ id: 1 }, { id: 3 }];
  59. const shared = shareStructure(prev, next);
  60. expect(shared[0]).toBe(prev[0]);
  61. expect(shared[1]).toBe(next[1]);
  62. });
  63. });
  64. describe("ConversationRowPool", () => {
  65. it("同 topic 同 rowId 的内容相同行复用旧对象", () => {
  66. const pool = new ConversationRowPool();
  67. const first = pool.shareRow("conversation/s1", bigToolRow(1));
  68. const again = pool.shareRow("conversation/s1", fresh(bigToolRow(1)));
  69. expect(again).toBe(first);
  70. });
  71. it("行状态变化时复用未变的大输入", () => {
  72. const pool = new ConversationRowPool();
  73. const first = pool.shareRow("conversation/s1", bigToolRow(1)) as ToolCallRow;
  74. const done = pool.shareRow("conversation/s1", fresh(bigToolRow(1, "success"))) as ToolCallRow;
  75. expect(done).not.toBe(first);
  76. expect(done.status).toBe("success");
  77. expect(done.input).toBe(first.input);
  78. // 字符串无法用 toBe 区分引用,这里至少确保值正确;引用复用由 heap snapshot 验证。
  79. expect(done.inputText).toBe(first.inputText);
  80. });
  81. it("不同 topic 不共享", () => {
  82. const pool = new ConversationRowPool();
  83. const a = pool.shareRow("conversation/s1", bigToolRow(1));
  84. const b = pool.shareRow("conversation/s2", fresh(bigToolRow(1)));
  85. expect(b).not.toBe(a);
  86. });
  87. it("remember 登记的行供后续共享", () => {
  88. const pool = new ConversationRowPool();
  89. const streamed = bigToolRow(1);
  90. pool.remember("conversation/s1", streamed);
  91. expect(pool.shareRow("conversation/s1", fresh(streamed))).toBe(streamed);
  92. });
  93. it("shareWindow 在所有行都未变化时复用 prev 数组", () => {
  94. const pool = new ConversationRowPool();
  95. const rows: ConversationRow[] = [bigToolRow(1), bigToolRow(2)];
  96. const window = pool.shareWindow("conversation/s1", rows);
  97. expect(window).toBe(rows);
  98. const again = pool.shareWindow("conversation/s1", fresh(rows), window);
  99. expect(again).toBe(window);
  100. const grown = pool.shareWindow("conversation/s1", fresh([...rows, bigToolRow(3)]), window);
  101. expect(grown).not.toBe(window);
  102. expect(grown[0]).toBe(window[0]);
  103. expect(grown[1]).toBe(window[1]);
  104. });
  105. });