conversationSharePreflightCache.test.ts 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. import { describe, expect, it } from "vitest";
  2. import type {
  3. ConversationSharePreflightResult,
  4. ConversationShareTurnPreflightResult,
  5. } from "@zcode/services";
  6. import {
  7. aggregateConversationShareBlockingIssues,
  8. buildConversationSharePreflightCacheEntries,
  9. conversationSharePreflightCacheKey,
  10. conversationShareTurnFingerprint,
  11. dedupeConversationShareIssues,
  12. getMissingConversationSharePreflightTurnIds,
  13. } from "@/v4/conversationSharePreflightCache.js";
  14. function cached(productTurnId: string): ConversationShareTurnPreflightResult {
  15. return {
  16. productTurnId,
  17. blockingIssues: [],
  18. skippableWarnings: [],
  19. deferredIssues: [],
  20. };
  21. }
  22. describe("conversation share per-turn preflight cache", () => {
  23. it("turn fingerprint 随附件元数据变化而变化", () => {
  24. const rows = [
  25. {
  26. rowId: 1,
  27. turnId: "turn-1",
  28. productTurnId: "turn-1",
  29. createdAt: 1,
  30. createdAtSeq: 1,
  31. kind: "userInput" as const,
  32. text: "",
  33. origin: "realUser" as const,
  34. attachments: [
  35. {
  36. ref: "/tmp/a.txt",
  37. fileName: "a.txt",
  38. mime: "text/plain",
  39. bytes: 1,
  40. },
  41. ],
  42. },
  43. ];
  44. const first = conversationShareTurnFingerprint(rows, "turn-1");
  45. const second = conversationShareTurnFingerprint(
  46. [
  47. {
  48. ...rows[0]!,
  49. attachments: [{ ...rows[0]!.attachments[0]!, bytes: 2 }],
  50. },
  51. ],
  52. "turn-1",
  53. );
  54. expect(first).not.toBe(second);
  55. expect(conversationShareTurnFingerprint(rows, "turn-1")).toBe(first);
  56. });
  57. it("turn fingerprint 绑定分享 scope、会话水位和能力版本", () => {
  58. const rows = [
  59. {
  60. rowId: 1,
  61. turnId: "turn-1",
  62. productTurnId: "turn-1",
  63. createdAt: 1,
  64. createdAtSeq: 1,
  65. kind: "assistantText" as const,
  66. text: "生成 report.pdf",
  67. state: "complete" as const,
  68. },
  69. ];
  70. const context = {
  71. workspaceKey: "/workspace",
  72. remoteSessionId: "remote-1",
  73. sessionId: "session-1",
  74. revision: 7,
  75. logEpoch: "epoch-1",
  76. capabilitiesFingerprint: "cap-1",
  77. };
  78. const first = conversationShareTurnFingerprint(rows, "turn-1", "/workspace", context);
  79. expect(
  80. conversationShareTurnFingerprint(rows, "turn-1", "/workspace", {
  81. ...context,
  82. capabilitiesFingerprint: "cap-2",
  83. }),
  84. ).not.toBe(first);
  85. expect(
  86. conversationShareTurnFingerprint(rows, "turn-1", "/workspace", {
  87. ...context,
  88. revision: 8,
  89. }),
  90. ).not.toBe(first);
  91. expect(
  92. conversationShareTurnFingerprint(rows, "turn-1", "/workspace", {
  93. ...context,
  94. remoteSessionId: "remote-2",
  95. }),
  96. ).not.toBe(first);
  97. });
  98. it("只返回首次加入且尚未检查的 turn,取消后重新选择不重复检查", () => {
  99. const scopeKey = "workspace-a\u0000remote-a\u0000session-a";
  100. const cache = new Map<string, ConversationShareTurnPreflightResult>([
  101. [
  102. conversationSharePreflightCacheKey(scopeKey, "turn-1"),
  103. { ...cached("turn-1"), turnFingerprint: "fp-1" },
  104. ],
  105. [
  106. conversationSharePreflightCacheKey(scopeKey, "turn-2"),
  107. { ...cached("turn-2"), turnFingerprint: "fp-2" },
  108. ],
  109. ]);
  110. const fingerprints = new Map([
  111. ["turn-1", "fp-1"],
  112. ["turn-2", "fp-2"],
  113. ]);
  114. expect(
  115. getMissingConversationSharePreflightTurnIds(scopeKey, ["turn-1"], cache, fingerprints),
  116. ).toEqual([]);
  117. expect(
  118. getMissingConversationSharePreflightTurnIds(
  119. scopeKey,
  120. ["turn-1", "turn-2"],
  121. cache,
  122. fingerprints,
  123. ),
  124. ).toEqual([]);
  125. expect(
  126. getMissingConversationSharePreflightTurnIds(
  127. scopeKey,
  128. ["turn-1", "turn-2", "turn-3"],
  129. cache,
  130. fingerprints,
  131. ),
  132. ).toEqual(["turn-3"]);
  133. expect(
  134. getMissingConversationSharePreflightTurnIds(
  135. scopeKey,
  136. ["turn-1"],
  137. cache,
  138. new Map([["turn-1", "fp-changed"]]),
  139. ),
  140. ).toEqual(["turn-1"]);
  141. });
  142. it("不同 workspace/session scope 不复用同名 product turn", () => {
  143. const cache = new Map<string, ConversationShareTurnPreflightResult>([
  144. [conversationSharePreflightCacheKey("scope-a", "turn-1"), cached("turn-1")],
  145. ]);
  146. expect(getMissingConversationSharePreflightTurnIds("scope-b", ["turn-1"], cache)).toEqual([
  147. "turn-1",
  148. ]);
  149. });
  150. it("聚合时只去重没有轮次/文件定位信息的重复全局问题", () => {
  151. const globalIssue = {
  152. code: "unknown" as const,
  153. scope: "transport" as const,
  154. };
  155. expect(dedupeConversationShareIssues([globalIssue, { ...globalIssue }])).toEqual([globalIssue]);
  156. expect(
  157. dedupeConversationShareIssues([
  158. {
  159. code: "running_turn" as const,
  160. scope: "turn" as const,
  161. turnOrdinal: 1,
  162. },
  163. {
  164. code: "running_turn" as const,
  165. scope: "turn" as const,
  166. turnOrdinal: 2,
  167. },
  168. ]),
  169. ).toHaveLength(2);
  170. expect(
  171. dedupeConversationShareIssues([
  172. { code: "running_turn" as const, scope: "turn" as const },
  173. { code: "running_turn" as const, scope: "turn" as const },
  174. ]),
  175. ).toHaveLength(2);
  176. });
  177. it("host 未返回 turnResults 时回落到整体 issues,而不是抛异常", () => {
  178. const blockingIssues = [{ code: "running_turn" as const, scope: "turn" as const }];
  179. // 老版本 host(未随 services 重建重启)返回的结果里没有 turnResults 字段。
  180. const legacyResult = {
  181. revision: 3,
  182. logEpoch: "epoch-1",
  183. capabilitiesFingerprint: "fingerprint",
  184. blockingIssues,
  185. skippableWarnings: [],
  186. deferredIssues: [],
  187. supportedArtifactTypes: [],
  188. } as unknown as ConversationSharePreflightResult;
  189. expect(
  190. buildConversationSharePreflightCacheEntries(
  191. legacyResult,
  192. ["turn-1", "turn-2"],
  193. new Map([["turn-1", "fp-1"]]),
  194. ),
  195. ).toEqual([
  196. {
  197. productTurnId: "turn-1",
  198. turnFingerprint: "fp-1",
  199. blockingIssues,
  200. skippableWarnings: [],
  201. deferredIssues: [],
  202. },
  203. {
  204. productTurnId: "turn-2",
  205. turnFingerprint: undefined,
  206. blockingIssues,
  207. skippableWarnings: [],
  208. deferredIssues: [],
  209. },
  210. ]);
  211. });
  212. it("有 turnResults 时按 turn 拆分,并补上当前 fingerprint", () => {
  213. const result: ConversationSharePreflightResult = {
  214. revision: 3,
  215. logEpoch: "epoch-1",
  216. capabilitiesFingerprint: "fingerprint",
  217. blockingIssues: [{ code: "running_turn", scope: "turn", turnOrdinal: 2 }],
  218. skippableWarnings: [],
  219. deferredIssues: [],
  220. supportedArtifactTypes: [],
  221. turnResults: [
  222. {
  223. productTurnId: "turn-2",
  224. blockingIssues: [{ code: "running_turn", scope: "turn", turnOrdinal: 2 }],
  225. skippableWarnings: [],
  226. deferredIssues: [],
  227. },
  228. ],
  229. };
  230. const entries = buildConversationSharePreflightCacheEntries(
  231. result,
  232. ["turn-1", "turn-2"],
  233. new Map([
  234. ["turn-1", "fp-1"],
  235. ["turn-2", "fp-2"],
  236. ]),
  237. );
  238. // turn-1 没有明细:回落整体 issues;turn-2 用自己的明细。
  239. expect(entries[0]).toEqual({
  240. productTurnId: "turn-1",
  241. turnFingerprint: "fp-1",
  242. blockingIssues: result.blockingIssues,
  243. skippableWarnings: [],
  244. deferredIssues: [],
  245. });
  246. expect(entries[1]).toEqual({
  247. productTurnId: "turn-2",
  248. turnFingerprint: "fp-2",
  249. blockingIssues: [{ code: "running_turn", scope: "turn", turnOrdinal: 2 }],
  250. skippableWarnings: [],
  251. deferredIssues: [],
  252. });
  253. });
  254. });
  255. it("累计行数超限随选择重算,取消轮次后不残留旧错误", () => {
  256. const first = {
  257. ...cached("a"),
  258. rowBudget: { count: 3, limit: 5 },
  259. blockingIssues: [
  260. { code: "rows_limit" as const, scope: "conversation" as const, actual: 6, limit: 5 },
  261. ],
  262. };
  263. const second = { ...cached("b"), rowBudget: { count: 3, limit: 5 } };
  264. expect(aggregateConversationShareBlockingIssues([first, second])).toEqual([
  265. { code: "rows_limit", scope: "conversation", actual: 6, limit: 5 },
  266. ]);
  267. expect(aggregateConversationShareBlockingIssues([first])).toEqual([]);
  268. });
  269. it("内嵌图片变化会使轮次缓存失效", () => {
  270. const row = {
  271. kind: "toolCall" as const,
  272. rowId: 1,
  273. turnId: "t",
  274. productTurnId: "p",
  275. createdAt: 1,
  276. createdAtSeq: 1,
  277. toolCallId: "tool",
  278. toolName: "NodeRepl",
  279. status: "success" as const,
  280. display: {
  281. kind: "node_repl_images" as const,
  282. images: [{ base64: "aGVsbG8=", mimeType: "image/png" }],
  283. },
  284. };
  285. const before = conversationShareTurnFingerprint([row], "p");
  286. expect(
  287. conversationShareTurnFingerprint(
  288. [
  289. {
  290. ...row,
  291. display: { ...row.display, images: [{ base64: "d29ybGQ=", mimeType: "image/png" }] },
  292. },
  293. ],
  294. "p",
  295. ),
  296. ).not.toBe(before);
  297. });