event-normalizer.test.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. import { describe, expect, it } from "vitest";
  2. import type { EventId, SessionEvent, SessionId, TraceId, TurnId } from "@zcode/contracts";
  3. import { SessionEventType } from "@zcode/contracts";
  4. import { normalizeConversationEvent } from "../src/zcode-protocol-v4/event-normalizer.js";
  5. function event(
  6. type: SessionEvent["type"],
  7. payload: unknown,
  8. overrides: Partial<SessionEvent> = {},
  9. ): SessionEvent {
  10. return {
  11. id: "event-1" as EventId,
  12. sessionId: "session-1" as SessionId,
  13. turnId: "runtime-turn-1" as TurnId,
  14. type,
  15. timestamp: new Date(1_700_000_000_000),
  16. traceId: "trace-live" as TraceId,
  17. sequenceNumber: 1,
  18. payload,
  19. ...overrides,
  20. };
  21. }
  22. describe("Conversation Event Normalizer", () => {
  23. it("live/cold TurnStarted 以持久 messageId 得到相同的用户实体语义", () => {
  24. const payload = {
  25. turnNumber: 1,
  26. input: "hello",
  27. messageId: "message-user-1",
  28. inputId: "command-1",
  29. };
  30. const live = normalizeConversationEvent(event(SessionEventType.TurnStarted, payload));
  31. const cold = normalizeConversationEvent(
  32. event(SessionEventType.TurnStarted, payload, {
  33. id: "event-cold-1" as EventId,
  34. turnId: "hydrate-turn-1" as TurnId,
  35. traceId: "trace-v4-hydration" as TraceId,
  36. }),
  37. );
  38. expect(live.semanticKind).toBe("userIntent");
  39. expect(cold.semanticKind).toBe("userIntent");
  40. for (const fact of [live, cold]) {
  41. expect(fact).toMatchObject({
  42. entityId: "message-user-1",
  43. productTurnId: "message-user-1",
  44. transcriptMessageId: "message-user-1",
  45. visibility: "visible",
  46. origin: "realUser",
  47. placement: { lane: "trigger", relation: "withinProductTurn" },
  48. diagnostics: [],
  49. });
  50. }
  51. expect(live.runtimeTurnId).toBe("runtime-turn-1");
  52. expect(cold.runtimeTurnId).toBe("hydrate-turn-1");
  53. });
  54. it("旧 TurnStarted 缺少 messageId 时生成确定性 fallback 并留下诊断", () => {
  55. const normalized = normalizeConversationEvent(
  56. event(SessionEventType.TurnStarted, { turnNumber: 1, input: "legacy" }),
  57. );
  58. expect(normalized).toMatchObject({
  59. semanticKind: "userIntent",
  60. entityId: "legacy:user:session-1:runtime-turn-1:event-1",
  61. productTurnId: "runtime-turn-1",
  62. transcriptMessageId: null,
  63. diagnostics: [
  64. {
  65. code: "normalizer.user.missingTranscriptMessageId",
  66. eventId: "event-1",
  67. },
  68. ],
  69. });
  70. });
  71. it("subagent_message carrier 保持 mailbox canonical origin", () => {
  72. const normalized = normalizeConversationEvent(
  73. event(SessionEventType.TurnStarted, {
  74. turnNumber: 2,
  75. input: "<subagent-message>done</subagent-message>",
  76. inputVisibility: "model-only",
  77. inputSource: "subagent_message",
  78. messageId: "message-subagent-carrier",
  79. }),
  80. );
  81. expect(normalized).toMatchObject({
  82. semanticKind: "userIntent",
  83. entityId: "message-subagent-carrier",
  84. productTurnId: "message-subagent-carrier",
  85. transcriptMessageId: "message-subagent-carrier",
  86. visibility: "modelOnly",
  87. origin: "mailbox",
  88. });
  89. });
  90. it("workflow_launch 启动轮:turnHeader 与 userInput 同得 workflowLaunch origin,并带上启动元数据", () => {
  91. const workflowLaunch = {
  92. runId: "dwfrun-9",
  93. toolCallId: "launch-abc",
  94. name: "deep-research",
  95. scope: "global" as const,
  96. path: "/home/u/.zcode/workflows/deep-research.ts",
  97. args: { topic: "adaptive concurrency" },
  98. description: "Deep-dive a topic",
  99. };
  100. const normalized = normalizeConversationEvent(
  101. event(SessionEventType.TurnStarted, {
  102. turnNumber: 1,
  103. input: 'Started the saved workflow "deep-research" (global) ...',
  104. messageId: "message-launch-1",
  105. executionKind: "controlOnly",
  106. inputSource: "workflow_launch",
  107. workflowLaunch,
  108. }),
  109. );
  110. expect(normalized).toMatchObject({
  111. semanticKind: "userIntent",
  112. entityId: "message-launch-1",
  113. visibility: "visible",
  114. origin: "workflowLaunch",
  115. turnHeaderOrigin: "workflowLaunch",
  116. executionKind: "controlOnly",
  117. workflowLaunch,
  118. });
  119. });
  120. it("ModelStreaming 的 assistant 身份只由 normalizer 解释", () => {
  121. const normalized = normalizeConversationEvent(
  122. event(SessionEventType.ModelStreaming, {
  123. kind: "text_start",
  124. delta: "",
  125. done: false,
  126. assistantMessageId: "message-assistant-1",
  127. partId: "part-1",
  128. }),
  129. { productTurnId: "product-turn-1" },
  130. );
  131. expect(normalized).toMatchObject({
  132. semanticKind: "assistantSegment",
  133. entityId: "message-assistant-1",
  134. productTurnId: "product-turn-1",
  135. runtimeTurnId: "runtime-turn-1",
  136. transcriptMessageId: "message-assistant-1",
  137. visibility: "visible",
  138. origin: "assistant",
  139. placement: { lane: "assistantWork", relation: "withinProductTurn" },
  140. stream: {
  141. kind: "text_start",
  142. transcriptPartId: "part-1",
  143. },
  144. diagnostics: [],
  145. });
  146. });
  147. it("同一 text segment 的多次 delta/end 继承 open segment 身份", () => {
  148. const started = normalizeConversationEvent(
  149. event(SessionEventType.ModelStreaming, {
  150. kind: "text_start",
  151. delta: "",
  152. done: false,
  153. assistantMessageId: "message-assistant-1",
  154. partId: "part-1",
  155. }),
  156. );
  157. expect(started.semanticKind).toBe("assistantSegment");
  158. if (started.semanticKind !== "assistantSegment") throw new Error("expected assistant segment");
  159. const openTextSegment = {
  160. entityId: started.entityId,
  161. transcriptMessageId: started.transcriptMessageId,
  162. };
  163. const chunks = [
  164. { kind: "text_delta", delta: "one", done: false, partId: "part-1" },
  165. { kind: "text_end", delta: "", done: false, partId: "part-1" },
  166. ].map((payload, index) =>
  167. normalizeConversationEvent(
  168. event(SessionEventType.ModelStreaming, payload, {
  169. id: `event-${index + 2}` as EventId,
  170. sequenceNumber: index + 2,
  171. }),
  172. { openAssistantSegments: { text: openTextSegment } },
  173. ),
  174. );
  175. expect(chunks.map((fact) => fact.entityId)).toEqual([
  176. "message-assistant-1",
  177. "message-assistant-1",
  178. ]);
  179. expect(chunks.map((fact) => fact.transcriptMessageId)).toEqual([
  180. "message-assistant-1",
  181. "message-assistant-1",
  182. ]);
  183. expect(chunks.flatMap((fact) => fact.diagnostics)).toEqual([]);
  184. });
  185. it("reasoning part 与 tool call 在各自 lane 内保持稳定实体身份", () => {
  186. const reasoningStart = normalizeConversationEvent(
  187. event(SessionEventType.ModelStreaming, {
  188. kind: "reasoning_start",
  189. delta: "",
  190. done: false,
  191. partId: "reasoning-part-1",
  192. }),
  193. );
  194. const reasoningDelta = normalizeConversationEvent(
  195. event(SessionEventType.ModelStreaming, {
  196. kind: "reasoning_delta",
  197. delta: "think",
  198. done: false,
  199. }),
  200. {
  201. openAssistantSegments: {
  202. reasoning: {
  203. entityId: reasoningStart.entityId,
  204. transcriptMessageId: reasoningStart.transcriptMessageId,
  205. },
  206. },
  207. },
  208. );
  209. const toolStart = normalizeConversationEvent(
  210. event(SessionEventType.ModelStreaming, {
  211. kind: "tool_input_start",
  212. delta: "",
  213. done: false,
  214. toolCallId: "tool-call-1",
  215. toolName: "Bash",
  216. partId: "tool-part-1",
  217. }),
  218. );
  219. const toolDelta = normalizeConversationEvent(
  220. event(SessionEventType.ModelStreaming, {
  221. kind: "tool_input_delta",
  222. delta: "pwd",
  223. done: false,
  224. toolCallId: "tool-call-1",
  225. partId: "tool-part-1",
  226. }),
  227. );
  228. expect(reasoningStart.entityId).toBe("reasoning-part-1");
  229. expect(reasoningDelta.entityId).toBe("reasoning-part-1");
  230. expect(toolStart.entityId).toBe("tool-call-1");
  231. expect(toolDelta.entityId).toBe("tool-call-1");
  232. });
  233. it("compact boundary normalizes operation identity, command provenance and placement", () => {
  234. const normalized = normalizeConversationEvent(
  235. event(SessionEventType.CompactStarted, {
  236. operationId: "compact-operation-1",
  237. messageId: "compact-host-message",
  238. sourceCommandId: "command-compact-1",
  239. status: "started",
  240. trigger: "manual",
  241. }),
  242. { productTurnId: "product-turn-1" },
  243. );
  244. expect(normalized).toMatchObject({
  245. semanticKind: "passthrough",
  246. entityId: "compact-operation-1",
  247. productTurnId: "product-turn-1",
  248. transcriptMessageId: "compact-host-message",
  249. visibility: "visible",
  250. placement: { lane: "assistantWork", relation: "withinProductTurn" },
  251. sourceCommandId: "command-compact-1",
  252. });
  253. });
  254. });