zcodeProtocolV4Telemetry.test.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. conversationTelemetryFactSchema,
  4. commandAckSchema,
  5. parseCommandEnvelope,
  6. } from "../src/zcode-protocol-v4/index.js";
  7. const base = {
  8. version: 1 as const,
  9. eventId: "event-1",
  10. eventSeq: 1,
  11. occurredAt: 1_700_000_000_000,
  12. sessionId: "session-1",
  13. };
  14. describe("ConversationTelemetryFact schema", () => {
  15. it("Memory 元数据保持 boolean 且兼容旧 ACK/fact", () => {
  16. const ack = { commandId: "command-1", status: "accepted", revisionAtDecision: 0 };
  17. const fact = { ...base, kind: "turn.started" };
  18. for (const [schema, value] of [
  19. [commandAckSchema, ack],
  20. [conversationTelemetryFactSchema, fact],
  21. ] as const) {
  22. expect(schema.parse(value)).not.toHaveProperty("memoryEnabled");
  23. for (const memoryEnabled of [true, false]) {
  24. expect(schema.parse({ ...value, memoryEnabled })).toMatchObject({ memoryEnabled });
  25. }
  26. expect(schema.safeParse({ ...value, memoryEnabled: "false" }).success).toBe(false);
  27. }
  28. });
  29. it("只允许定时任务 admission 与 CronCreate 关联 ID,不接收任务正文", () => {
  30. const started = conversationTelemetryFactSchema.parse({
  31. ...base,
  32. kind: "turn.started",
  33. sourceCommandId: "automation-1:1700000000000",
  34. automationId: "automation-1",
  35. taskTrigger: "schedule",
  36. scheduledAt: 1_700_000_000_000,
  37. });
  38. expect(started).toMatchObject({ automationId: "automation-1", taskTrigger: "schedule" });
  39. expect(() =>
  40. conversationTelemetryFactSchema.parse({ ...started, prompt: "private" }),
  41. ).toThrow();
  42. const tool = conversationTelemetryFactSchema.parse({
  43. ...base,
  44. kind: "tool.lifecycle",
  45. phase: "completed",
  46. toolCallId: "cron-create-1",
  47. toolName: "CronCreate",
  48. automationId: "automation-1",
  49. });
  50. expect(tool).toMatchObject({ automationId: "automation-1" });
  51. expect(() => conversationTelemetryFactSchema.parse({ ...tool, output: "private" })).toThrow();
  52. });
  53. it("Off-Peak admission 只接收稳定 task ID,且与 automation 互斥", () => {
  54. const started = conversationTelemetryFactSchema.parse({
  55. ...base,
  56. kind: "turn.started",
  57. sourceCommandId: "message-run-1",
  58. offPeakTaskId: "offpeak-1",
  59. offPeakRunType: "init",
  60. });
  61. expect(started).toMatchObject({ offPeakTaskId: "offpeak-1", offPeakRunType: "init" });
  62. expect(() =>
  63. conversationTelemetryFactSchema.parse({
  64. ...base,
  65. kind: "turn.started",
  66. sourceCommandId: "message-run-2",
  67. offPeakRunType: "resume",
  68. }),
  69. ).toThrow();
  70. expect(() =>
  71. conversationTelemetryFactSchema.parse({
  72. ...started,
  73. automationId: "automation-1",
  74. }),
  75. ).toThrow();
  76. for (const forbidden of [
  77. { prompt: "private prompt" },
  78. { workspacePath: "/private/workspace" },
  79. { ticketId: "private-ticket" },
  80. { responseBody: "private response" },
  81. ]) {
  82. expect(() => conversationTelemetryFactSchema.parse({ ...started, ...forbidden })).toThrow();
  83. }
  84. });
  85. it("只接受 provider hostname,不允许 URL、header、prompt 或工具正文穿透", () => {
  86. const valid = conversationTelemetryFactSchema.parse({
  87. ...base,
  88. kind: "model.request.status",
  89. requestId: "request-1",
  90. status: "model_request_started",
  91. providerId: "zcode-plan",
  92. modelId: "model-1",
  93. providerHostname: "example.com",
  94. transport: "sse",
  95. attempt: 1,
  96. maxAttempts: 3,
  97. });
  98. expect(valid).toMatchObject({ providerHostname: "example.com" });
  99. for (const forbidden of [
  100. { baseURL: "https://example.com/private?q=token" },
  101. { requestHeaders: { authorization: "secret" } },
  102. { requestHeaderCount: 3 },
  103. { responseHeaderCount: 4 },
  104. { prompt: "private prompt" },
  105. { responseBody: "private response" },
  106. { message: "upstream response body" },
  107. { finishReason: "provider-private-reason" },
  108. { providerHostname: "https://example.com/private?q=token" },
  109. { providerHostname: "example.com:443" },
  110. { providerHostname: "user@example.com" },
  111. { providerHostname: "example.com/path" },
  112. { providerHostname: "example.com query" },
  113. ]) {
  114. expect(() => conversationTelemetryFactSchema.parse({ ...valid, ...forbidden })).toThrow();
  115. }
  116. expect(() =>
  117. conversationTelemetryFactSchema.parse({
  118. ...valid,
  119. providerHostname: "[2001:db8::1]",
  120. }),
  121. ).not.toThrow();
  122. });
  123. it("tool fact 只接收 allowlist perf,拒绝 input/output", () => {
  124. const valid = conversationTelemetryFactSchema.parse({
  125. ...base,
  126. kind: "tool.lifecycle",
  127. phase: "completed",
  128. toolCallId: "tool-1",
  129. toolName: "Read",
  130. performance: {
  131. totalMs: 20,
  132. fileCount: 1,
  133. workspaceKind: "local",
  134. commandName: "git",
  135. commandCount: 1,
  136. commandStatus: "completed",
  137. },
  138. });
  139. expect(valid.kind).toBe("tool.lifecycle");
  140. expect(() =>
  141. conversationTelemetryFactSchema.parse({
  142. ...valid,
  143. input: { path: "/secret" },
  144. }),
  145. ).toThrow();
  146. expect(() =>
  147. conversationTelemetryFactSchema.parse({
  148. ...valid,
  149. performance: { totalMs: 20, rawOutput: "secret" },
  150. }),
  151. ).toThrow();
  152. });
  153. it("subagent fact 只接收父子会话关联,不允许正文", () => {
  154. const valid = conversationTelemetryFactSchema.parse({
  155. ...base,
  156. kind: "subagent.lifecycle",
  157. phase: "spawned",
  158. agentId: "agent-1",
  159. agentType: "explore",
  160. childSessionId: "child-session-1",
  161. parentToolCallId: "parent-tool-1",
  162. background: false,
  163. status: "running",
  164. });
  165. expect(valid).toMatchObject({
  166. agentId: "agent-1",
  167. childSessionId: "child-session-1",
  168. background: false,
  169. });
  170. expect(() =>
  171. conversationTelemetryFactSchema.parse({
  172. ...valid,
  173. prompt: "private",
  174. }),
  175. ).toThrow();
  176. });
  177. it("stream fact 只保留 parent id,不允许正文;usage 不接收 V4 文件摘要", () => {
  178. const stream = conversationTelemetryFactSchema.parse({
  179. ...base,
  180. kind: "stream.chunk",
  181. channel: "text",
  182. chunkLength: 20,
  183. firstChunk: true,
  184. parentToolCallId: "parent-agent-tool",
  185. });
  186. expect(stream).toMatchObject({ parentToolCallId: "parent-agent-tool" });
  187. expect(() =>
  188. conversationTelemetryFactSchema.parse({ ...stream, content: "private" }),
  189. ).toThrow();
  190. const usage = conversationTelemetryFactSchema.parse({
  191. ...base,
  192. kind: "usage.delta",
  193. inputTokens: 1,
  194. outputTokens: 2,
  195. totalTokens: 3,
  196. reasoningTokens: 0,
  197. cacheReadTokens: 0,
  198. cacheWriteTokens: 0,
  199. });
  200. expect(() =>
  201. conversationTelemetryFactSchema.parse({
  202. ...usage,
  203. fileAdditions: 10,
  204. fileCount: 1,
  205. }),
  206. ).toThrow();
  207. });
  208. it("workflow.lifecycle 只带 run / 子代理 id 与状态;phase 决定必填", () => {
  209. const spawned = conversationTelemetryFactSchema.parse({
  210. ...base,
  211. kind: "workflow.lifecycle",
  212. phase: "actor-spawned",
  213. runId: "dwfrun-1",
  214. toolCallId: "tool-1",
  215. agentId: "agent#1@1",
  216. childSessionId: "sess_dwf-1",
  217. sourceCommandId: "launch-input-1",
  218. });
  219. expect(spawned).toMatchObject({ phase: "actor-spawned", childSessionId: "sess_dwf-1" });
  220. const settled = conversationTelemetryFactSchema.parse({
  221. ...base,
  222. kind: "workflow.lifecycle",
  223. phase: "run-settled",
  224. runId: "dwfrun-1",
  225. status: "stopped",
  226. stopReason: "user",
  227. errorMessage: "Workflow run stopped (user)",
  228. });
  229. expect(settled).toMatchObject({ status: "stopped", stopReason: "user" });
  230. // actor-spawned 缺子会话 id 不成立;run-settled 缺状态不成立;正文字段一律拒绝。
  231. expect(() =>
  232. conversationTelemetryFactSchema.parse({
  233. ...base,
  234. kind: "workflow.lifecycle",
  235. phase: "actor-spawned",
  236. runId: "dwfrun-1",
  237. agentId: "agent#1@1",
  238. }),
  239. ).toThrow();
  240. expect(() =>
  241. conversationTelemetryFactSchema.parse({
  242. ...base,
  243. kind: "workflow.lifecycle",
  244. phase: "run-settled",
  245. runId: "dwfrun-1",
  246. }),
  247. ).toThrow();
  248. expect(() =>
  249. conversationTelemetryFactSchema.parse({ ...spawned, scriptText: "export const x = 1" }),
  250. ).toThrow();
  251. });
  252. it("turn.started 接受 workflow 唤起来源;turn.terminal 接受 workflowResultConsumed", () => {
  253. expect(
  254. conversationTelemetryFactSchema.parse({
  255. ...base,
  256. kind: "turn.started",
  257. inputSource: "background_task",
  258. backgroundSource: "workflow",
  259. }),
  260. ).toMatchObject({ backgroundSource: "workflow" });
  261. expect(
  262. conversationTelemetryFactSchema.parse({
  263. ...base,
  264. kind: "turn.terminal",
  265. status: "success",
  266. workflowResultConsumed: true,
  267. }),
  268. ).toMatchObject({ workflowResultConsumed: true });
  269. });
  270. });
  271. describe("setAssistantFeedback command schema", () => {
  272. it("复用 row target CAS,feedback null 表示清除", () => {
  273. const parsed = parseCommandEnvelope({
  274. commandId: "command-1",
  275. clientId: "desktop-1",
  276. sessionId: "session-1",
  277. baseRevision: 3,
  278. baseLogEpoch: "epoch-1",
  279. type: "setAssistantFeedback",
  280. payload: {
  281. target: { rowId: 7, entityId: "assistant-message-1" },
  282. feedback: null,
  283. },
  284. issuedAt: 1_700_000_000_000,
  285. });
  286. expect(parsed.ok).toBe(true);
  287. });
  288. it("缺少 revision/logEpoch 或伪造 target 字段时拒绝", () => {
  289. const envelope = {
  290. commandId: "command-1",
  291. clientId: "desktop-1",
  292. sessionId: "session-1",
  293. type: "setAssistantFeedback" as const,
  294. payload: {
  295. target: { rowId: 7, entityId: "assistant-message-1" },
  296. feedback: "like",
  297. },
  298. issuedAt: 1_700_000_000_000,
  299. };
  300. expect(parseCommandEnvelope(envelope).ok).toBe(false);
  301. expect(
  302. parseCommandEnvelope({
  303. ...envelope,
  304. baseRevision: 3,
  305. baseLogEpoch: "epoch-1",
  306. payload: {
  307. ...envelope.payload,
  308. target: { ...envelope.payload.target, messageId: "x" },
  309. },
  310. }).ok,
  311. ).toBe(false);
  312. });
  313. });