|
| 1 | +import { describe, test, expect } from "bun:test" |
| 2 | +import { PermissionNext } from "../../src/permission/next" |
| 3 | + |
| 4 | +describe("subagent permission inheritance (#12566)", () => { |
| 5 | + test("child session inherits parent agent allow-all permission", () => { |
| 6 | + // Parent agent (e.g., "build") configured with "allow": "*" |
| 7 | + const parentPermission: PermissionNext.Ruleset = [ |
| 8 | + { permission: "*", pattern: "*", action: "allow" }, |
| 9 | + ] |
| 10 | + |
| 11 | + // Parent session may have accumulated rules (e.g., from "always" replies) |
| 12 | + const parentSessionPermission: PermissionNext.Ruleset = [] |
| 13 | + |
| 14 | + // Subagent-specific overrides |
| 15 | + const childOverrides: PermissionNext.Ruleset = [ |
| 16 | + { permission: "todowrite", pattern: "*", action: "deny" }, |
| 17 | + { permission: "todoread", pattern: "*", action: "deny" }, |
| 18 | + { permission: "task", pattern: "*", action: "deny" }, |
| 19 | + ] |
| 20 | + |
| 21 | + // This is how the child session permission should be assembled: |
| 22 | + // parent agent rules + parent session rules + child overrides |
| 23 | + const childSessionPermission = PermissionNext.merge( |
| 24 | + parentPermission, |
| 25 | + parentSessionPermission, |
| 26 | + childOverrides, |
| 27 | + ) |
| 28 | + |
| 29 | + // The subagent's own permission (e.g., "general" agent defaults) |
| 30 | + const subagentPermission: PermissionNext.Ruleset = [ |
| 31 | + { permission: "bash", pattern: "*", action: "ask" }, |
| 32 | + { permission: "edit", pattern: "*", action: "ask" }, |
| 33 | + ] |
| 34 | + |
| 35 | + // In the child session, the effective ruleset is: |
| 36 | + // merge(subagent.permission, childSession.permission) |
| 37 | + const effective = PermissionNext.merge(subagentPermission, childSessionPermission) |
| 38 | + |
| 39 | + // bash should evaluate to "allow" because the parent agent's allow-all |
| 40 | + // rule is in the child session's permission and comes after the subagent's "ask" |
| 41 | + const bashRule = PermissionNext.evaluate("bash", "ls", effective) |
| 42 | + expect(bashRule.action).toBe("allow") |
| 43 | + |
| 44 | + // edit should also be allowed |
| 45 | + const editRule = PermissionNext.evaluate("edit", "src/foo.ts", effective) |
| 46 | + expect(editRule.action).toBe("allow") |
| 47 | + |
| 48 | + // But todowrite should be denied (child override takes precedence) |
| 49 | + const todoRule = PermissionNext.evaluate("todowrite", "*", effective) |
| 50 | + expect(todoRule.action).toBe("deny") |
| 51 | + |
| 52 | + // task should be denied (child override) |
| 53 | + const taskRule = PermissionNext.evaluate("task", "general", effective) |
| 54 | + expect(taskRule.action).toBe("deny") |
| 55 | + }) |
| 56 | + |
| 57 | + test("child session without parent inheritance falls back to ask", () => { |
| 58 | + // This simulates the OLD behavior (bug) where parent rules are not inherited |
| 59 | + const childOverrides: PermissionNext.Ruleset = [ |
| 60 | + { permission: "todowrite", pattern: "*", action: "deny" }, |
| 61 | + { permission: "todoread", pattern: "*", action: "deny" }, |
| 62 | + { permission: "task", pattern: "*", action: "deny" }, |
| 63 | + ] |
| 64 | + |
| 65 | + // Subagent's own permission without user config allow-all |
| 66 | + const subagentPermission: PermissionNext.Ruleset = [ |
| 67 | + { permission: "bash", pattern: "*", action: "ask" }, |
| 68 | + ] |
| 69 | + |
| 70 | + // Without parent inheritance, the merge only has subagent + child overrides |
| 71 | + const effective = PermissionNext.merge(subagentPermission, childOverrides) |
| 72 | + |
| 73 | + // bash evaluates to "ask" — this would block in unattended mode |
| 74 | + const bashRule = PermissionNext.evaluate("bash", "ls", effective) |
| 75 | + expect(bashRule.action).toBe("ask") |
| 76 | + }) |
| 77 | + |
| 78 | + test("parent session accumulated rules propagate to child", () => { |
| 79 | + // Parent agent has default ask-for-bash |
| 80 | + const parentPermission: PermissionNext.Ruleset = [ |
| 81 | + { permission: "bash", pattern: "*", action: "ask" }, |
| 82 | + ] |
| 83 | + |
| 84 | + // But the user replied "always" for bash during the parent session, |
| 85 | + // which gets stored in the parent session's permission |
| 86 | + const parentSessionPermission: PermissionNext.Ruleset = [ |
| 87 | + { permission: "bash", pattern: "*", action: "allow" }, |
| 88 | + ] |
| 89 | + |
| 90 | + const childOverrides: PermissionNext.Ruleset = [ |
| 91 | + { permission: "todowrite", pattern: "*", action: "deny" }, |
| 92 | + ] |
| 93 | + |
| 94 | + const childSessionPermission = PermissionNext.merge( |
| 95 | + parentPermission, |
| 96 | + parentSessionPermission, |
| 97 | + childOverrides, |
| 98 | + ) |
| 99 | + |
| 100 | + const subagentPermission: PermissionNext.Ruleset = [ |
| 101 | + { permission: "bash", pattern: "*", action: "ask" }, |
| 102 | + ] |
| 103 | + |
| 104 | + const effective = PermissionNext.merge(subagentPermission, childSessionPermission) |
| 105 | + |
| 106 | + // bash should be "allow" because the parent session's accumulated |
| 107 | + // "always allow" comes after the agent defaults |
| 108 | + const bashRule = PermissionNext.evaluate("bash", "ls", effective) |
| 109 | + expect(bashRule.action).toBe("allow") |
| 110 | + }) |
| 111 | + |
| 112 | + test("merge preserves last-wins semantics with findLast", () => { |
| 113 | + // PermissionNext.evaluate uses findLast, so later rules win |
| 114 | + const rules: PermissionNext.Ruleset = [ |
| 115 | + { permission: "bash", pattern: "*", action: "allow" }, |
| 116 | + { permission: "bash", pattern: "*", action: "deny" }, |
| 117 | + { permission: "bash", pattern: "*", action: "ask" }, |
| 118 | + ] |
| 119 | + const result = PermissionNext.evaluate("bash", "ls", rules) |
| 120 | + expect(result.action).toBe("ask") // last matching rule wins |
| 121 | + }) |
| 122 | + |
| 123 | + test("child deny overrides parent allow for specific permissions", () => { |
| 124 | + // Parent allows everything |
| 125 | + const parentPermission: PermissionNext.Ruleset = [ |
| 126 | + { permission: "*", pattern: "*", action: "allow" }, |
| 127 | + ] |
| 128 | + |
| 129 | + // Child explicitly denies task (subagent should not spawn sub-subagents) |
| 130 | + const childOverrides: PermissionNext.Ruleset = [ |
| 131 | + { permission: "task", pattern: "*", action: "deny" }, |
| 132 | + ] |
| 133 | + |
| 134 | + // Child session permission = parent + child overrides |
| 135 | + // The child deny comes AFTER the parent allow |
| 136 | + const childSessionPermission = PermissionNext.merge(parentPermission, childOverrides) |
| 137 | + |
| 138 | + const subagentPermission: PermissionNext.Ruleset = [] |
| 139 | + const effective = PermissionNext.merge(subagentPermission, childSessionPermission) |
| 140 | + |
| 141 | + // task should be denied because the child's deny comes last |
| 142 | + const taskRule = PermissionNext.evaluate("task", "general", effective) |
| 143 | + expect(taskRule.action).toBe("deny") |
| 144 | + |
| 145 | + // bash should be allowed via the parent's wildcard allow |
| 146 | + const bashRule = PermissionNext.evaluate("bash", "ls", effective) |
| 147 | + expect(bashRule.action).toBe("allow") |
| 148 | + }) |
| 149 | +}) |
0 commit comments