session-mapper-turn-payload.test.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Legacy v3 边界:TurnStarted 除独立遥测归因外、TurnSteerQueued 的兼容字段仍透传;
  2. // strict schema 漏声明会让 Host 丢弃整条事件(Bot 收不到 task_run_started,上一轮正文串进下一轮)。
  3. import { describe, expect, it } from "vitest";
  4. import {
  5. SessionEventType,
  6. type EventId,
  7. type SessionEvent,
  8. type SessionId,
  9. type TraceId,
  10. type TurnId,
  11. type TurnStartedPayloadBase,
  12. } from "@zcode/contracts";
  13. import { zcodeSessionEventSchema } from "@zcode/shared";
  14. import { mapSessionEvent } from "../src/zcode-protocol/session-mapper.js";
  15. function turnEvent(type: SessionEventType, payload: Record<string, unknown>): SessionEvent {
  16. return {
  17. id: `legacy-${type}` as EventId,
  18. sessionId: "legacy-session" as SessionId,
  19. turnId: "turn-1" as TurnId,
  20. type,
  21. timestamp: new Date(1_700_000_000_000),
  22. traceId: "legacy-trace" as TraceId,
  23. sequenceNumber: 1,
  24. payload,
  25. };
  26. }
  27. describe("legacy turn payload 映射", () => {
  28. it("turn.started 保留 inputOrigin、workflowLaunch 与 epilogueStart", () => {
  29. const mapped = mapSessionEvent(
  30. turnEvent(SessionEventType.TurnStarted, {
  31. turnNumber: 1,
  32. input: "hello",
  33. inputId: "input-1",
  34. inputOrigin: "mobile",
  35. epilogueStart: 3,
  36. workflowLaunch: { runId: "run-1", toolCallId: "launch-1", name: "Daily" },
  37. }),
  38. );
  39. expect(zcodeSessionEventSchema.safeParse(mapped).error?.issues ?? []).toEqual([]);
  40. });
  41. // 用 Required<> 列出 CLI 声明的全部字段:CLI 新增字段时这里先编译失败,必须同步 App schema
  42. // (漏声明会让 strict 校验丢弃整条 turn.started,已发生过四次)。isFirstMessage 由 mapper 移除,
  43. // 见 session-mapper-legacy-strip.test.ts;App 对它的兼容由下一条直接解析线协议的用例覆盖。
  44. it("turn.started 映射后的 CLI 全部字段都被 App strict schema 接受", () => {
  45. const payload: Required<TurnStartedPayloadBase> = {
  46. isFirstMessage: true,
  47. inputOrigin: "desktop",
  48. executionStartedAt: 1_700_000_000_000.5,
  49. turnNumber: 2,
  50. input: "hello",
  51. messageId: "message-1" as TurnStartedPayloadBase["messageId"] & string,
  52. inputId: "input-1",
  53. foregroundExecutionId: "execution-1",
  54. queryId: "query-1" as TurnStartedPayloadBase["queryId"] & string,
  55. inputSource: "background_task",
  56. inputVisibility: "user-visible",
  57. workflowLaunch: { runId: "run-1", toolCallId: "launch-1", name: "Daily" },
  58. executionKind: "agent",
  59. epilogueStart: 3,
  60. originMeta: { backgroundSource: "subagent", workId: "work-1", title: "Agent" },
  61. backgroundSource: "subagent",
  62. targetId: "target-1",
  63. attachments: [{ fileName: "a.png", mime: "image/png", bytes: 1 }],
  64. intent: { planEnabled: true },
  65. };
  66. const mapped = mapSessionEvent(turnEvent(SessionEventType.TurnStarted, { ...payload }));
  67. expect(zcodeSessionEventSchema.safeParse(mapped).error?.issues ?? []).toEqual([]);
  68. });
  69. // 回归(2026-11-17 mac E2E,评审 SG-01):8172e49580 之后、148827e6dc 之前的 CLI 会把 isFirstMessage
  70. // 原样发到 turn.started,App strict schema 未声明时整条事件被丢弃(Bot 串轮、运行中摘要 ZodError)。
  71. // 当前 CLI 已不发送(mapper 移除);App 仍须接受旧发送方。这里绕过 mapper 直接解析线协议事件,
  72. // schema 删掉该字段时 true/false 两例会失败。
  73. it.each([true, false, undefined])("App strict schema 接受旧 CLI 发送的 isFirstMessage=%s", (value) => {
  74. const wire = mapSessionEvent(
  75. turnEvent(SessionEventType.TurnStarted, { turnNumber: 1, input: "hello" }),
  76. ) as { payload: Record<string, unknown> };
  77. const legacySenderEvent = {
  78. ...wire,
  79. payload: { ...wire.payload, ...(value === undefined ? {} : { isFirstMessage: value }) },
  80. };
  81. expect(zcodeSessionEventSchema.safeParse(legacySenderEvent).error?.issues ?? []).toEqual([]);
  82. });
  83. it("turn.steerQueued 保留 inputPresentation 与 automationId", () => {
  84. const mapped = mapSessionEvent(
  85. turnEvent(SessionEventType.TurnSteerQueued, {
  86. pendingInputId: "pending-1",
  87. input: "next",
  88. inputPreview: "next",
  89. inputSize: 4,
  90. inputPresentation: "user_steer",
  91. automationId: "automation-1",
  92. targetTurnId: "turn-1",
  93. queueLength: 1,
  94. }),
  95. );
  96. expect(zcodeSessionEventSchema.safeParse(mapped).error?.issues ?? []).toEqual([]);
  97. });
  98. });