zcodeProtocolV4WorkflowObservationDisplay.test.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // GetWorkflowRun 工具卡载荷的**线上** schema(docs/dynamic-workflow/launch.md「Their cards」)。
  2. //
  3. // 这一侧与 CLI 的 getWorkflowRunToolResultDisplayPayloadSchema 是两份独立的 zod,却必须逐字段
  4. // 同步:两侧都是 strict,少一个字段整条 display 校验失败、工具卡退化成纯文本;枚举多一个值,
  5. // 手机远控的 bundle 会拒收整帧(2026-09-04 曾因此让一条订阅永久失效)。所以这里钉的是
  6. // 「CLI 真的会发出来的那个形状,这一侧收得下」。
  7. import { describe, expect, it } from "vitest";
  8. // toolCall 与 toolOutput 两条 union 共用同一个成员 schema,所以从导出的那一条进就够了。
  9. import { toolCallDisplaySchema } from "../src/zcode-protocol-v4/index.js";
  10. /** CLI 侧 createGetWorkflowRunDisplay 在一个情势完整的 run 上会发出的载荷。 */
  11. const CARD = {
  12. kind: "get_workflow_run" as const,
  13. runId: "dwfrun-7f3a",
  14. label: "nightly triage",
  15. status: "running" as const,
  16. summary:
  17. "Running for 5m 40s, in phase 2 of 4 (judge). 5 of 7 dispatched steps settled, 2 running.",
  18. generatedAt: 1_755_000_000_000,
  19. usage: {
  20. spentTokens: 18_420,
  21. nodesObserved: 7,
  22. nodesRunning: 2,
  23. nodesCompleted: 5,
  24. nodesFailed: 0,
  25. },
  26. phases: [
  27. {
  28. name: "collect",
  29. state: "done" as const,
  30. rounds: 1,
  31. nodesSettled: 2,
  32. nodesRunning: 0,
  33. enteredAt: 1,
  34. exitedAt: 2,
  35. },
  36. {
  37. name: "judge",
  38. state: "current" as const,
  39. rounds: 1,
  40. nodesSettled: 3,
  41. nodesRunning: 2,
  42. enteredAt: 2,
  43. },
  44. ],
  45. subagents: [
  46. {
  47. siteId: "agent#2",
  48. ordinal: 2,
  49. name: "judge",
  50. state: "executing" as const,
  51. phaseName: "judge",
  52. instructionsHead: "Judge specs 1-7 for flakiness",
  53. startedAt: 3,
  54. turn: 4,
  55. toolCalls: 7,
  56. lastTool: { name: "Read", target: "packages/net/retry.spec.ts", at: 4 },
  57. stepsSettled: 1,
  58. stepsFailed: 0,
  59. tokens: 5_100,
  60. lastProgressAt: 4,
  61. },
  62. {
  63. siteId: "agent#3",
  64. ordinal: 1,
  65. state: "waiting" as const,
  66. waitCause: "backoff" as const,
  67. retryAfterMs: 20_000,
  68. waitSince: 5,
  69. parkedOn: "q-01",
  70. stepsSettled: 1,
  71. stepsFailed: 0,
  72. tokens: 2_130,
  73. },
  74. ],
  75. health: {
  76. lastProgressAt: 6,
  77. stalledSince: 7,
  78. concurrency: { effective: 2, cap: 3, reason: "rate_limited", since: 8 },
  79. consecutiveFailures: 0,
  80. cachedSteps: 0,
  81. leftoverRunning: 2,
  82. pendingQuestionsKnown: false,
  83. },
  84. actors: [{ siteId: "agent#2", ordinal: 2, name: "judge" }],
  85. logTail: [{ sequence: 19, message: "judge#1 finished" }],
  86. };
  87. describe("v4 get_workflow_run display payload", () => {
  88. it("accepts the situation report the CLI sends", () => {
  89. expect(toolCallDisplaySchema.parse(CARD)).toEqual(CARD);
  90. });
  91. it("takes an absent phase table and an empty roster", () => {
  92. const { phases: _phases, ...withoutPhases } = CARD;
  93. expect(toolCallDisplaySchema.parse({ ...withoutPhases, subagents: [] })).toBeTruthy();
  94. });
  95. // 情势上线**之前**持久化的载荷:没有 summary / generatedAt / subagents / health。本 schema
  96. // 是 strict 的,把这五件设成必填等于让升级后打开的每一条历史会话里这张卡整块被剥、
  97. // 退化成纯文本——而 spec 承诺的文本兜底只针对完全没有载荷的行。
  98. it("still accepts a payload persisted before the situation report existed", () => {
  99. const legacy = {
  100. kind: "get_workflow_run" as const,
  101. runId: "dwfrun-old",
  102. label: "yesterday's run",
  103. status: "completed" as const,
  104. usage: {
  105. spentTokens: 10,
  106. nodesObserved: 2,
  107. nodesRunning: 0,
  108. nodesCompleted: 2,
  109. nodesFailed: 0,
  110. },
  111. actors: [{ siteId: "agent#1", ordinal: 1 }],
  112. logTail: [{ sequence: 1, message: "done" }],
  113. result: "shipped",
  114. };
  115. expect(toolCallDisplaySchema.parse(legacy)).toEqual(legacy);
  116. });
  117. // 日志行的落库时刻是可选键:新载荷带它(卡上据它算年龄),旧载荷没有它,两者都要过。
  118. it("takes a log entry with or without its journal time", () => {
  119. const withTime = {
  120. ...CARD,
  121. logTail: [{ sequence: 19, message: "judge#1 finished", at: 1_700_000_000_000 }],
  122. };
  123. expect(toolCallDisplaySchema.parse(withTime)).toEqual(withTime);
  124. });
  125. // 枚举闭集:一个未知的相位词会让这一侧拒收整帧,所以两侧的词表永远一起改。
  126. it("keeps the subagent state, phase state and wait cause closed", () => {
  127. for (const patch of [
  128. { subagents: [{ ...CARD.subagents[1], state: "thinking" }] },
  129. { subagents: [{ ...CARD.subagents[1], waitCause: "quota" }] },
  130. { phases: [{ ...CARD.phases[1], state: "skipped" }] },
  131. ]) {
  132. expect(toolCallDisplaySchema.safeParse({ ...CARD, ...patch }).success).toBe(false);
  133. }
  134. });
  135. it("stays strict about unknown keys inside the roster", () => {
  136. expect(
  137. toolCallDisplaySchema.safeParse({
  138. ...CARD,
  139. subagents: [{ ...CARD.subagents[1], persona: "you are…" }],
  140. }).success,
  141. ).toBe(false);
  142. });
  143. it("bounds the roster at 64 rows and the instructions head at 240 chars", () => {
  144. expect(
  145. toolCallDisplaySchema.safeParse({
  146. ...CARD,
  147. subagents: Array.from({ length: 65 }, () => CARD.subagents[1]),
  148. }).success,
  149. ).toBe(false);
  150. expect(
  151. toolCallDisplaySchema.safeParse({
  152. ...CARD,
  153. subagents: [{ ...CARD.subagents[0], instructionsHead: "x".repeat(241) }],
  154. }).success,
  155. ).toBe(false);
  156. });
  157. });