guarded-mode.test.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. CronAutomationSchema,
  4. inheritPermissionMode,
  5. type CollaborationMode,
  6. } from "@zcode/contracts";
  7. import { PermissionService } from "../src/permission/service.js";
  8. import { bashToolEntry } from "../src/tool/handlers/bash.js";
  9. describe("Guarded is an opt-in mode, not a global gate", () => {
  10. it("independent Plan keeps its restriction over guarded dangerous commands", () => {
  11. const service = new PermissionService();
  12. const context = {
  13. toolName: "Bash",
  14. input: { command: "rm -rf build" },
  15. mode: "guarded" as const,
  16. riskLevel: "high" as const,
  17. };
  18. const capability = {
  19. ...bashToolEntry.metadata,
  20. userApprovalRule: "safety.bash.remove-critical-path",
  21. };
  22. expect(service.checkPermission({ ...context, planEnabled: true }, capability)).toMatchObject({
  23. decision: "deny",
  24. ruleId: "mode.plan.nonReadOnly",
  25. });
  26. expect(service.checkPermission({ ...context, planEnabled: false }, capability)).toMatchObject({
  27. decision: "ask",
  28. approvalMode: "user-once",
  29. });
  30. });
  31. it("guarded retains workflow alwaysAsk and ordinary session grants", () => {
  32. const service = new PermissionService();
  33. const context = {
  34. toolName: "CreateWorkflow",
  35. input: {},
  36. mode: "guarded" as const,
  37. riskLevel: "high" as const,
  38. };
  39. const capability = { alwaysAsk: true };
  40. expect(service.checkPermission(context, capability).decision).toBe("ask");
  41. service.grantSessionPermission([
  42. { type: "addRules", behavior: "allow", rules: [{ toolName: "CreateWorkflow" }] },
  43. ]);
  44. expect(service.checkPermission(context, capability).decision).toBe("allow");
  45. });
  46. it("automation records preserve explicit guarded/yolo and do not migrate absence", () => {
  47. const record = {
  48. automationId: "guarded-fixture",
  49. title: "fixture",
  50. cronExpr: "0 * * * *",
  51. prompt: "fixture",
  52. enabled: true,
  53. lifecycleStatus: "active",
  54. runCount: 0,
  55. recurring: true,
  56. };
  57. for (const mode of ["guarded", "yolo", undefined]) {
  58. const parsed = CronAutomationSchema.parse({ ...record, ...(mode ? { mode } : {}) });
  59. expect(parsed.mode).toBe(mode);
  60. }
  61. });
  62. it.each(["guarded", "yolo", "build", "edit", "plan", "auto"] as CollaborationMode[])(
  63. "inheritance from %s only transforms YOLO children",
  64. (parent) => {
  65. for (const child of [
  66. "yolo",
  67. "build",
  68. "edit",
  69. "plan",
  70. "auto",
  71. "guarded",
  72. ] as CollaborationMode[]) {
  73. expect(inheritPermissionMode(parent, child)).toBe(
  74. parent === "guarded" && child === "yolo" ? "guarded" : child,
  75. );
  76. }
  77. },
  78. );
  79. it("requires once approval only in guarded and preserves YOLO baseline for nonmatches", () => {
  80. const service = new PermissionService();
  81. for (const mode of ["guarded", "yolo"] as const) {
  82. for (const command of ["rm -rf build", "echo hello", "bash -c 'rm -rf build'"]) {
  83. const input = { command };
  84. const capability = bashToolEntry.resolvePermissionCapability?.(input, {
  85. mode,
  86. bashShellSelection: {
  87. dialect: "posix",
  88. source: "auto-detected",
  89. display: { name: "bash" },
  90. },
  91. });
  92. const decision = service.checkPermission(
  93. { toolName: "Bash", input, mode, riskLevel: "high" },
  94. { ...bashToolEntry.metadata, ...capability },
  95. );
  96. expect(decision.decision).toBe(
  97. mode === "guarded" && command === "rm -rf build" ? "ask" : "allow",
  98. );
  99. expect(decision.approvalMode).toBe(
  100. mode === "guarded" && command === "rm -rf build" ? "user-once" : undefined,
  101. );
  102. }
  103. }
  104. });
  105. // 2026-09-17 review:selection 缺失是基础设施状态,不能让 guarded 静默退回 YOLO。
  106. it("matches with POSIX grammar when the session shell selection is not yet available", () => {
  107. const input = { command: "rm -rf build" };
  108. const capability = bashToolEntry.resolvePermissionCapability?.(input, {
  109. mode: "guarded",
  110. });
  111. expect(capability).toEqual({
  112. userApprovalRule: "safety.bash.remove-critical-path",
  113. });
  114. const legacy = bashToolEntry.resolvePermissionCapability?.(input, {
  115. mode: "guarded",
  116. bashShellSelection: {
  117. dialect: "legacy-shell",
  118. source: "legacy-fallback",
  119. display: { name: "system shell" },
  120. },
  121. });
  122. expect(legacy).toBeUndefined();
  123. });
  124. });