session-mapper-legacy-strip.test.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Legacy v1 会话事件的边界(session-mapper.ts):CLI 事件里后加的字段要在源头剥掉,不能漏进 App 的 strict schema。
  2. // 回归证据(2026-11-06 PLM-JARVIS-2 的运行时证据;示例插件 Jarvis 已迁出到市场仓库,那个 E2E 已删除,
  3. // 这里的测试守住回归):App 的 readSession(view:"summary") 经 session/events 读事件,整页 safeParse 失败——
  4. // tool.updated(started) 多了 readOnly、sideEffectScope,permission.requested 多了 fullAccessSupported。
  5. // 调用过工具、正在运行的会话因此读不到 executionId,中断不了。
  6. import { describe, expect, it } from "vitest";
  7. import {
  8. SessionEventType,
  9. type EventId,
  10. type SessionEvent,
  11. type SessionId,
  12. type ToolCallId,
  13. type TraceId,
  14. } from "@zcode/contracts";
  15. import { zcodeSessionEventSchema, type ZCodeDeliveryKind } from "@zcode/shared";
  16. import { mapSessionEvent } from "../src/zcode-protocol/session-mapper.js";
  17. function event(type: SessionEvent["type"], payload: Record<string, unknown>): SessionEvent {
  18. return {
  19. id: "legacy-strip-event" as EventId,
  20. sessionId: "legacy-strip-session" as SessionId,
  21. type,
  22. timestamp: new Date(1_700_000_000_000),
  23. traceId: "legacy-strip-trace" as TraceId,
  24. sequenceNumber: 1,
  25. payload,
  26. } as SessionEvent;
  27. }
  28. describe("legacy 会话事件剥掉后加的字段", () => {
  29. it.each([undefined, "desktop-continuous", "web-remote-replayable"] as const)(
  30. "turn.started 不向 %s 透出首条归因,内部事件保持不变",
  31. (deliveryKind: ZCodeDeliveryKind | undefined) => {
  32. for (const isFirstMessage of [undefined, true, false]) {
  33. const source = event(SessionEventType.TurnStarted, {
  34. turnNumber: 1,
  35. input: "hello",
  36. foregroundExecutionId: "execution-1",
  37. ...(isFirstMessage === undefined ? {} : { isFirstMessage }),
  38. });
  39. const mapped = mapSessionEvent(source, deliveryKind);
  40. expect(zcodeSessionEventSchema.safeParse(mapped).success).toBe(true);
  41. expect(mapped.payload).not.toHaveProperty("isFirstMessage");
  42. expect(mapped.payload).toMatchObject({ foregroundExecutionId: "execution-1" });
  43. expect(source.payload).toEqual({
  44. turnNumber: 1,
  45. input: "hello",
  46. foregroundExecutionId: "execution-1",
  47. ...(isFirstMessage === undefined ? {} : { isFirstMessage }),
  48. });
  49. }
  50. },
  51. );
  52. it("tool.updated(started) 不带 readOnly 与 sideEffectScope,整条事件过 strict schema", () => {
  53. const mapped = mapSessionEvent(
  54. event(SessionEventType.ToolCallStarted, {
  55. toolCallId: "tc-started" as ToolCallId,
  56. toolName: "ListSessions",
  57. startedAt: new Date(1_700_000_000_500),
  58. readOnly: true,
  59. sideEffectScope: "session",
  60. }),
  61. );
  62. expect(zcodeSessionEventSchema.safeParse(mapped).success).toBe(true);
  63. expect(mapped.payload).not.toHaveProperty("readOnly");
  64. expect(mapped.payload).not.toHaveProperty("sideEffectScope");
  65. expect(mapped.payload).toMatchObject({ kind: "started", toolCallId: "tc-started" });
  66. });
  67. it("permission.requested 不带 fullAccessSupported,整条事件过 strict schema", () => {
  68. const mapped = mapSessionEvent(
  69. event(SessionEventType.PermissionRequested, {
  70. requestId: "req-full-access",
  71. toolCallId: "tc-full-access" as ToolCallId,
  72. toolName: "SendToSession",
  73. riskLevel: "medium",
  74. reason: "sendToSession",
  75. input: { sessionId: "sess_target", prompt: "hi" },
  76. fullAccessSupported: true,
  77. }),
  78. );
  79. expect(zcodeSessionEventSchema.safeParse(mapped).success).toBe(true);
  80. expect(mapped.payload).not.toHaveProperty("fullAccessSupported");
  81. expect(mapped.payload).toMatchObject({
  82. toolCallId: "tc-full-access",
  83. toolName: "SendToSession",
  84. });
  85. });
  86. });