zcodeProtocolV4WorkflowWorkspace.test.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // 工作区 transcript 的协议面(docs/dynamic-workflow/transcript-and-notifications.md「Two read queries」):
  2. // 两条只读 query 的参数 / 结果形状与上界。
  3. import { describe, expect, it } from "vitest";
  4. import {
  5. V4_METHODS,
  6. WORKFLOW_WORKSPACE_LIMITS,
  7. v4ConversationWorkflowRunNodeResultParamsSchema,
  8. v4ConversationWorkflowRunNodeResultResultSchema,
  9. v4ConversationWorkflowRunWorkspaceParamsSchema,
  10. v4ConversationWorkflowRunWorkspaceResultSchema,
  11. workflowRunWorkspaceNodeSchema,
  12. } from "../src/zcode-protocol-v4/index.js";
  13. function node(overrides: Record<string, unknown> = {}) {
  14. return {
  15. siteId: "world-read#3",
  16. ordinal: 2,
  17. kind: "world-run" as const,
  18. op: "run",
  19. args: ["pnpm", ["vitest", "run"], { timeoutMs: 60_000 }],
  20. status: "completed" as const,
  21. summary: { resultBytes: 4_212, exitCode: 1, stdoutBytes: 4_100, stderrBytes: 80 },
  22. createdAt: 1_700_000_000_000,
  23. updatedAt: 1_700_000_001_300,
  24. ...overrides,
  25. };
  26. }
  27. describe("v4 conversation workflowRunWorkspace query", () => {
  28. it("声明两个只读、无状态、超时重发安全的 query,与产物的 ①/③ 拆法同族", () => {
  29. expect(V4_METHODS.conversationWorkflowRunWorkspace).toBe(
  30. "v4/conversation/workflowRunWorkspace",
  31. );
  32. expect(V4_METHODS.conversationWorkflowRunNodeResult).toBe(
  33. "v4/conversation/workflowRunNodeResult",
  34. );
  35. const params = { sessionId: "s", runId: "r" };
  36. expect(v4ConversationWorkflowRunWorkspaceParamsSchema.parse(params)).toEqual(params);
  37. // 不带 atSeq / atLogEpoch:读的是 journal,没有陈旧可防。
  38. expect(
  39. v4ConversationWorkflowRunWorkspaceParamsSchema.safeParse({ ...params, atSeq: 1 }).success,
  40. ).toBe(false);
  41. });
  42. it("清单行带 op / args / 状态 / 摘要 / 时刻,**不带正文**;升级前的历史行两者缺席仍合法", () => {
  43. const full = node();
  44. expect(workflowRunWorkspaceNodeSchema.parse(full)).toEqual(full);
  45. const legacy = node({ op: undefined, args: undefined, summary: undefined });
  46. expect(workflowRunWorkspaceNodeSchema.parse(legacy)).toEqual({
  47. siteId: "world-read#3",
  48. ordinal: 2,
  49. kind: "world-run",
  50. status: "completed",
  51. createdAt: 1_700_000_000_000,
  52. updatedAt: 1_700_000_001_300,
  53. });
  54. // 正文永不进清单:多一个 result 键整行拒收。
  55. expect(workflowRunWorkspaceNodeSchema.safeParse(node({ result: "x" })).success).toBe(false);
  56. // `inputTruncated` 只有 true 一个值:false 就该缺席。
  57. expect(workflowRunWorkspaceNodeSchema.safeParse(node({ inputTruncated: false })).success).toBe(
  58. false,
  59. );
  60. expect(
  61. workflowRunWorkspaceNodeSchema.parse(node({ inputTruncated: true })).inputTruncated,
  62. ).toBe(true);
  63. });
  64. it("失败行带 code + message;message 有展示上界", () => {
  65. const failed = node({
  66. status: "failed",
  67. summary: undefined,
  68. error: { code: "DriverError", message: "timed out after 300000ms" },
  69. });
  70. expect(workflowRunWorkspaceNodeSchema.parse(failed)).toEqual(failed);
  71. expect(
  72. workflowRunWorkspaceNodeSchema.safeParse(
  73. node({
  74. status: "failed",
  75. summary: undefined,
  76. error: {
  77. code: "DriverError",
  78. message: "x".repeat(WORKFLOW_WORKSPACE_LIMITS.maxErrorMessageLength + 1),
  79. },
  80. }),
  81. ).success,
  82. ).toBe(false);
  83. });
  84. it("结果包裹 nodes 数组(有界)+ 可选的 truncated", () => {
  85. const result = { nodes: [node()], truncated: true };
  86. expect(v4ConversationWorkflowRunWorkspaceResultSchema.parse(result)).toEqual(result);
  87. expect(v4ConversationWorkflowRunWorkspaceResultSchema.parse({ nodes: [] })).toEqual({
  88. nodes: [],
  89. });
  90. expect(
  91. v4ConversationWorkflowRunWorkspaceResultSchema.safeParse({
  92. nodes: Array.from({ length: WORKFLOW_WORKSPACE_LIMITS.maxNodes + 1 }, () => node()),
  93. }).success,
  94. ).toBe(false);
  95. });
  96. });
  97. describe("v4 conversation workflowRunNodeResult query", () => {
  98. it("按 (run, 站点, 序号) 取一个节点的正文;maxBytes 可选且钳在 resultMaxBytes", () => {
  99. const params = { sessionId: "s", runId: "r", siteId: "world-read#3", ordinal: 2 };
  100. expect(v4ConversationWorkflowRunNodeResultParamsSchema.parse(params)).toEqual(params);
  101. expect(
  102. v4ConversationWorkflowRunNodeResultParamsSchema.parse({ ...params, maxBytes: 1024 }),
  103. ).toEqual({ ...params, maxBytes: 1024 });
  104. expect(
  105. v4ConversationWorkflowRunNodeResultParamsSchema.safeParse({
  106. ...params,
  107. maxBytes: WORKFLOW_WORKSPACE_LIMITS.resultMaxBytes + 1,
  108. }).success,
  109. ).toBe(false);
  110. const { ordinal: _dropped, ...withoutOrdinal } = params;
  111. expect(v4ConversationWorkflowRunNodeResultParamsSchema.safeParse(withoutOrdinal).success).toBe(
  112. false,
  113. );
  114. });
  115. it("结果带状态、保形有界化后的正文、截断标记与截断前字节数;running 行没有正文", () => {
  116. const completed = {
  117. status: "completed" as const,
  118. result: { exitCode: 0, stdout: "ok\n", stderr: "" },
  119. truncated: false,
  120. totalBytes: 41,
  121. };
  122. expect(v4ConversationWorkflowRunNodeResultResultSchema.parse(completed)).toEqual(completed);
  123. const running = { status: "running" as const, truncated: false, totalBytes: 0 };
  124. expect(v4ConversationWorkflowRunNodeResultResultSchema.parse(running)).toEqual(running);
  125. const failed = {
  126. status: "failed" as const,
  127. error: { code: "WorldReadCapExceeded", message: "stdout over 256 KiB" },
  128. truncated: false,
  129. totalBytes: 0,
  130. };
  131. expect(v4ConversationWorkflowRunNodeResultResultSchema.parse(failed)).toEqual(failed);
  132. // truncated 必填:调用方要据此在页脚说「只显示前 …」。
  133. const { truncated: _t, ...withoutTruncated } = completed;
  134. expect(
  135. v4ConversationWorkflowRunNodeResultResultSchema.safeParse(withoutTruncated).success,
  136. ).toBe(false);
  137. });
  138. });