permission-full-access.test.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import { describe, expect, it, vi } from "vitest";
  2. import type { AgentRuntimeInternal } from "../src/runtime/internal.js";
  3. import { applyRuntimeExecutionState } from "../src/runtime/execution-state.js";
  4. import { grantPermissionFullAccess } from "../src/runtime/permission-full-access.js";
  5. function fixture() {
  6. const inputs = [
  7. {
  8. pendingInputId: "a",
  9. intent: { mode: "build", planEnabled: true, modelSelection: { modelId: "one" } },
  10. },
  11. { pendingInputId: "b", intent: { mode: "edit", planEnabled: false } },
  12. ];
  13. let receipt: unknown;
  14. const runtime = {
  15. sessionId: "session",
  16. config: { mode: "edit", planEnabled: true },
  17. rootTraceContext: {},
  18. pendingInputReservations: new Map(),
  19. activeTurn: {
  20. pendingInputs: inputs.map((item) => ({ id: item.pendingInputId, intent: item.intent })),
  21. },
  22. rebuildProjection: vi.fn(async () => ({ pendingSteerInputs: inputs })),
  23. sessionStore: {
  24. sessionEntries: vi.fn(async () =>
  25. receipt ? [{ id: "session:permission-full-access:permission-1", data: receipt }] : [],
  26. ),
  27. commitPermissionFullAccess: vi.fn(async (input) => {
  28. receipt = input.receipt.data;
  29. }),
  30. },
  31. createEvent: vi.fn((type, payload) => ({
  32. id: "grant-event",
  33. type,
  34. payload,
  35. sessionId: "session",
  36. traceId: "trace",
  37. timestamp: new Date(),
  38. sequenceNumber: 0,
  39. })),
  40. appendEvent: vi.fn(async () => {}),
  41. eventStore: { getEvents: vi.fn(async () => []) },
  42. notifyEventSinks: vi.fn(async () => {}),
  43. };
  44. return {
  45. runtime,
  46. grant: () =>
  47. grantPermissionFullAccess.call(runtime as unknown as AgentRuntimeInternal, "permission-1"),
  48. };
  49. }
  50. describe("审批框完全访问", () => {
  51. it("保留 Runtime 和每项 Plan,只更新本次目标队列权限", async () => {
  52. const { runtime, grant } = fixture();
  53. await grant();
  54. expect(runtime.config).toEqual({ mode: "yolo", planEnabled: true });
  55. expect(runtime.activeTurn.pendingInputs.map((item) => item.intent)).toEqual([
  56. { mode: "yolo", planEnabled: true, modelSelection: { modelId: "one" } },
  57. { mode: "yolo", planEnabled: false },
  58. ]);
  59. expect(runtime.sessionStore.commitPermissionFullAccess).toHaveBeenCalledOnce();
  60. expect(runtime.appendEvent).toHaveBeenCalledOnce();
  61. });
  62. it("授权重试仍严格拒绝损坏 receipt,不重新抓取队列或提交权限", async () => {
  63. const { runtime, grant } = fixture();
  64. runtime.sessionStore.sessionEntries.mockResolvedValueOnce([
  65. {
  66. id: "session:permission-full-access:permission-1",
  67. data: { interactionId: "permission-1", event: null },
  68. },
  69. ]);
  70. await expect(grant()).rejects.toThrow();
  71. expect(runtime.sessionStore.commitPermissionFullAccess).not.toHaveBeenCalled();
  72. expect(runtime.rebuildProjection).not.toHaveBeenCalled();
  73. expect(runtime.appendEvent).not.toHaveBeenCalled();
  74. expect(runtime.config.mode).toBe("edit");
  75. });
  76. it("事务失败不改内存,不发布成功", async () => {
  77. const { runtime, grant } = fixture();
  78. runtime.sessionStore.commitPermissionFullAccess.mockRejectedValueOnce(new Error("disk full"));
  79. await expect(grant()).rejects.toThrow("disk full");
  80. expect(runtime.config.mode).toBe("edit");
  81. expect(runtime.activeTurn.pendingInputs[0]?.intent.mode).toBe("build");
  82. expect(runtime.appendEvent).not.toHaveBeenCalled();
  83. });
  84. it("提交后事件失败可重试,固定目标不覆盖后来入队消息", async () => {
  85. const { runtime, grant } = fixture();
  86. runtime.appendEvent.mockRejectedValueOnce(new Error("event failed"));
  87. await expect(grant()).rejects.toThrow("event failed");
  88. runtime.activeTurn.pendingInputs.push({
  89. id: "later",
  90. intent: { mode: "edit", planEnabled: false },
  91. });
  92. await grant();
  93. expect(runtime.sessionStore.commitPermissionFullAccess).toHaveBeenCalledOnce();
  94. expect(runtime.activeTurn.pendingInputs.at(-1)?.intent.mode).toBe("edit");
  95. });
  96. it("Guide 正在消费或队列已 reserve 时拒绝提权,稍后可重试", async () => {
  97. const { runtime, grant } = fixture();
  98. Object.assign(runtime, { pendingInputDrains: 1 });
  99. await expect(grant()).rejects.toThrow("busy");
  100. Object.assign(runtime, { pendingInputDrains: 0 });
  101. runtime.pendingInputReservations.set("a", "reservation");
  102. await expect(grant()).rejects.toThrow("busy");
  103. runtime.pendingInputReservations.clear();
  104. await grant();
  105. expect(runtime.sessionStore.commitPermissionFullAccess).toHaveBeenCalledOnce();
  106. });
  107. it("发布失败后改权限,先补旧事件;迟到审批重试不覆盖较新的选择", async () => {
  108. const { runtime, grant } = fixture();
  109. const published: unknown[] = [];
  110. runtime.appendEvent.mockRejectedValueOnce(new Error("event failed"));
  111. await expect(grant()).rejects.toThrow("event failed");
  112. runtime.appendEvent.mockImplementation(async (event) => {
  113. published.push(event);
  114. });
  115. runtime.eventStore.getEvents.mockImplementation(async () => published);
  116. await applyRuntimeExecutionState(
  117. runtime as unknown as AgentRuntimeInternal,
  118. { mode: "build", planEnabled: false },
  119. { source: "command" },
  120. );
  121. expect(published.map((event: any) => event.payload.mode)).toEqual(["yolo", "build"]);
  122. await grant();
  123. expect(runtime.config).toEqual({ mode: "build", planEnabled: false });
  124. expect(published).toHaveLength(2);
  125. });
  126. });