permission-full-access-projection.test.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import { describe, expect, it } from "vitest";
  2. import { SessionEventType, type SessionEvent } from "@zcode/contracts";
  3. import { ProductProjection } from "../src/zcode-protocol-v4/product-projection.js";
  4. import { conversationSnapshotSchema } from "@zcode/shared/zcode-protocol-v4";
  5. function fixture() {
  6. const projection = new ProductProjection("session", "epoch");
  7. let seq = 0;
  8. const push = (type: SessionEvent["type"], payload: unknown) =>
  9. projection.applyEvent({
  10. id: `event-${++seq}`,
  11. sessionId: "session",
  12. traceId: "trace",
  13. turnId: "turn",
  14. sequenceNumber: seq,
  15. timestamp: new Date(seq),
  16. type,
  17. payload,
  18. } as SessionEvent);
  19. push(SessionEventType.SessionCreated, { mode: "build", planEnabled: true, contextWindow: 1000 });
  20. push(SessionEventType.TurnStarted, { turnNumber: 1, input: "start" });
  21. return { projection, push };
  22. }
  23. describe("完全访问 V4 权威投影", () => {
  24. it("一次事件更新 runtime/混合 Plan 队列,暂停和普通模式切换语义不变", () => {
  25. const { projection, push } = fixture();
  26. for (const [id, planEnabled, delivery] of [
  27. ["a", true, "guide"],
  28. ["b", false, "queue"],
  29. ] as const) {
  30. push(SessionEventType.TurnSteerQueued, {
  31. pendingInputId: id,
  32. input: id,
  33. inputPreview: id,
  34. inputSize: 1,
  35. queueLength: 2,
  36. targetTurnId: "turn",
  37. delivery,
  38. intent: {
  39. sourceCommandId: id,
  40. queueItemId: id,
  41. clientId: "client",
  42. kind: "sendText",
  43. mode: "edit",
  44. planEnabled,
  45. requestedDelivery: delivery,
  46. admittedDelivery: delivery,
  47. admittedAt: 1,
  48. admissionSeq: id === "a" ? 1 : 2,
  49. attachmentRefs: [],
  50. },
  51. });
  52. }
  53. push(SessionEventType.QueueAutoDrainChanged, { autoDrain: false });
  54. const before = projection.getSnapshot().queue;
  55. push(SessionEventType.SessionModeChanged, {
  56. mode: "yolo",
  57. planEnabled: true,
  58. previousMode: "build",
  59. source: "command",
  60. permissionGrant: { interactionId: "p", queueItemIds: ["a", "b"] },
  61. });
  62. const snapshot = projection.getSnapshot();
  63. expect(snapshot.config).toMatchObject({
  64. mode: "yolo",
  65. planEnabled: true,
  66. permissionGrant: { interactionId: "p" },
  67. });
  68. expect(snapshot.queue).toEqual({
  69. ...before,
  70. items: before.items.map((item) => ({ ...item, mode: "yolo" })),
  71. });
  72. expect(conversationSnapshotSchema.safeParse(snapshot).success).toBe(true);
  73. push(SessionEventType.SessionModeChanged, {
  74. mode: "build",
  75. planEnabled: false,
  76. previousMode: "yolo",
  77. source: "command",
  78. });
  79. expect(projection.getSnapshot().queue).toEqual(snapshot.queue);
  80. expect(projection.getSnapshot().config.permissionGrant).toEqual({ interactionId: "p" });
  81. });
  82. it("新 UI 独立能力入口,旧 options 不增加错误授权;child/独立确认不投放", () => {
  83. const { projection, push } = fixture();
  84. const request = {
  85. requestId: "p",
  86. toolCallId: "t",
  87. toolName: "Bash",
  88. input: { command: "ping example.com" },
  89. reason: "approval",
  90. fullAccessSupported: true,
  91. };
  92. push(SessionEventType.PermissionRequested, request);
  93. let payload = projection.getSnapshot().pendingInteractions[0]!.payload;
  94. expect(payload.kind).toBe("permission");
  95. if (payload.kind !== "permission") throw new Error("wrong payload");
  96. expect(payload.options.map((item) => item.optionId)).toEqual([
  97. "allowOnce",
  98. "allowAlways",
  99. "deny",
  100. ]);
  101. expect(payload.fullAccessOption).toMatchObject({
  102. optionId: "fullAccess",
  103. response: { decision: "deny" },
  104. });
  105. push(SessionEventType.PermissionRequested, {
  106. ...request,
  107. requestId: "child",
  108. toolCallId: "ct",
  109. origin: {
  110. kind: "subagent",
  111. agentId: "a",
  112. agentType: "a",
  113. childSessionId: "c",
  114. parentSessionId: "session",
  115. },
  116. });
  117. payload = projection.getSnapshot().pendingInteractions.at(-1)!.payload;
  118. if (payload.kind !== "permission") throw new Error("wrong payload");
  119. expect(payload.fullAccessOption).toBeUndefined();
  120. });
  121. });