@gotgenes/pi-permission-system 18.1.0 → 18.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (132) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/docs/assets/logo.png +0 -0
  3. package/docs/assets/logo.svg +213 -0
  4. package/docs/configuration.md +700 -0
  5. package/docs/cross-extension-api.md +525 -0
  6. package/docs/guides/permission-frontmatter-for-subagent-extensions.md +201 -0
  7. package/docs/guides/upstream-issue-template.md +113 -0
  8. package/docs/migration/legacy-to-flat.md +365 -0
  9. package/docs/opencode-compatibility.md +213 -0
  10. package/docs/session-approvals.md +68 -0
  11. package/docs/subagent-integration.md +102 -0
  12. package/docs/troubleshooting.md +53 -0
  13. package/package.json +5 -2
  14. package/src/access-intent/access-path.ts +29 -4
  15. package/src/access-intent/bash/bash-path-resolver.ts +39 -18
  16. package/src/access-intent/bash/msys-bash-tokens.ts +64 -0
  17. package/src/path-normalizer.ts +105 -2
  18. package/test/access-intent/access-path.test.ts +0 -277
  19. package/test/access-intent/bash/node-text.test.ts +0 -148
  20. package/test/access-intent/bash/parser.test.ts +0 -19
  21. package/test/access-intent/bash/program.test.ts +0 -673
  22. package/test/access-intent/bash/token-classification.test.ts +0 -363
  23. package/test/access-intent/bash/token-collection.test.ts +0 -300
  24. package/test/active-agent.test.ts +0 -155
  25. package/test/async-cache.test.ts +0 -48
  26. package/test/bash-arity.test.ts +0 -144
  27. package/test/bash-external-directory.test.ts +0 -1022
  28. package/test/builtin-tool-input-formatters.test.ts +0 -109
  29. package/test/canonicalize-path.test.ts +0 -119
  30. package/test/composition-root.test.ts +0 -698
  31. package/test/config-loader.test.ts +0 -740
  32. package/test/config-modal.test.ts +0 -320
  33. package/test/config-paths.test.ts +0 -83
  34. package/test/config-pipeline.test.ts +0 -90
  35. package/test/config-reporter.test.ts +0 -147
  36. package/test/config-store.test.ts +0 -466
  37. package/test/decision-audit.test.ts +0 -72
  38. package/test/decision-reporter.test.ts +0 -112
  39. package/test/denial-messages.test.ts +0 -714
  40. package/test/detect-permissive-bash-fallback.test.ts +0 -56
  41. package/test/expand-home.test.ts +0 -93
  42. package/test/extension-config.test.ts +0 -129
  43. package/test/extension-paths.test.ts +0 -108
  44. package/test/forwarded-permissions/io.test.ts +0 -251
  45. package/test/forwarding-manager.test.ts +0 -200
  46. package/test/handlers/before-agent-start.test.ts +0 -314
  47. package/test/handlers/external-directory-integration.test.ts +0 -515
  48. package/test/handlers/external-directory-session-dedup.test.ts +0 -175
  49. package/test/handlers/external-directory-symlink-acceptance.test.ts +0 -167
  50. package/test/handlers/gates/bash-command-metamorphic.test.ts +0 -88
  51. package/test/handlers/gates/bash-command.test.ts +0 -257
  52. package/test/handlers/gates/bash-external-directory.test.ts +0 -268
  53. package/test/handlers/gates/bash-path.test.ts +0 -346
  54. package/test/handlers/gates/candidate-check.test.ts +0 -52
  55. package/test/handlers/gates/external-directory-messages.test.ts +0 -85
  56. package/test/handlers/gates/external-directory-policy.test.ts +0 -134
  57. package/test/handlers/gates/external-directory.test.ts +0 -267
  58. package/test/handlers/gates/helpers.test.ts +0 -165
  59. package/test/handlers/gates/path.test.ts +0 -369
  60. package/test/handlers/gates/runner.test.ts +0 -408
  61. package/test/handlers/gates/skill-input-gate-pipeline.test.ts +0 -176
  62. package/test/handlers/gates/skill-input.test.ts +0 -128
  63. package/test/handlers/gates/skill-read.test.ts +0 -161
  64. package/test/handlers/gates/tool-call-gate-pipeline.test.ts +0 -356
  65. package/test/handlers/gates/tool.test.ts +0 -244
  66. package/test/handlers/input-events.test.ts +0 -168
  67. package/test/handlers/input.test.ts +0 -199
  68. package/test/handlers/lifecycle.test.ts +0 -221
  69. package/test/handlers/tool-call-boundary.test.ts +0 -145
  70. package/test/handlers/tool-call-events.test.ts +0 -277
  71. package/test/handlers/tool-call.test.ts +0 -395
  72. package/test/handlers/validate-requested-tool.test.ts +0 -92
  73. package/test/helpers/external-directory-fixtures.ts +0 -269
  74. package/test/helpers/gate-fixtures.ts +0 -316
  75. package/test/helpers/handler-fixtures.ts +0 -335
  76. package/test/helpers/make-fake-pi.ts +0 -100
  77. package/test/helpers/manager-harness.ts +0 -112
  78. package/test/helpers/session-fixtures.ts +0 -199
  79. package/test/input-normalizer.test.ts +0 -325
  80. package/test/logging.test.ts +0 -51
  81. package/test/mcp-targets.test.ts +0 -233
  82. package/test/node-modules-discovery.test.ts +0 -97
  83. package/test/normalize.test.ts +0 -247
  84. package/test/path-containment.test.ts +0 -161
  85. package/test/path-normalization.test.ts +0 -233
  86. package/test/path-normalizer.test.ts +0 -196
  87. package/test/path-surfaces.test.ts +0 -55
  88. package/test/pattern-suggest.test.ts +0 -248
  89. package/test/permission-dialog.test.ts +0 -205
  90. package/test/permission-event-rpc.test.ts +0 -560
  91. package/test/permission-events.test.ts +0 -400
  92. package/test/permission-forwarder.test.ts +0 -370
  93. package/test/permission-forwarding.test.ts +0 -315
  94. package/test/permission-gate.test.ts +0 -269
  95. package/test/permission-manager-unified.test.ts +0 -3714
  96. package/test/permission-merge.test.ts +0 -61
  97. package/test/permission-prompter.test.ts +0 -518
  98. package/test/permission-prompts.test.ts +0 -363
  99. package/test/permission-resolver.test.ts +0 -277
  100. package/test/permission-session.test.ts +0 -404
  101. package/test/permission-ui-prompt.test.ts +0 -146
  102. package/test/permissions-service.test.ts +0 -192
  103. package/test/pi-infrastructure-read.test.ts +0 -432
  104. package/test/policy-loader.test.ts +0 -561
  105. package/test/prompting-gateway.test.ts +0 -231
  106. package/test/rule.test.ts +0 -650
  107. package/test/safe-system-paths.test.ts +0 -46
  108. package/test/scope-merge.test.ts +0 -116
  109. package/test/service-lifecycle.test.ts +0 -163
  110. package/test/service.test.ts +0 -261
  111. package/test/session-approval.test.ts +0 -75
  112. package/test/session-logger.test.ts +0 -200
  113. package/test/session-rules.test.ts +0 -321
  114. package/test/session-start.test.ts +0 -112
  115. package/test/skill-prompt-sanitizer.test.ts +0 -418
  116. package/test/status.test.ts +0 -10
  117. package/test/subagent-context.test.ts +0 -372
  118. package/test/subagent-lifecycle-events.test.ts +0 -132
  119. package/test/subagent-registry.test.ts +0 -145
  120. package/test/synthesize.test.ts +0 -302
  121. package/test/system-prompt-sanitizer.test.ts +0 -382
  122. package/test/tool-access-extractor-registry.test.ts +0 -77
  123. package/test/tool-input-formatter-registry.test.ts +0 -75
  124. package/test/tool-input-path.test.ts +0 -84
  125. package/test/tool-input-preview.test.ts +0 -129
  126. package/test/tool-input-prompt-formatters.test.ts +0 -115
  127. package/test/tool-preview-formatter.test.ts +0 -458
  128. package/test/tool-registry.test.ts +0 -197
  129. package/test/value-guards.test.ts +0 -193
  130. package/test/wildcard-matcher.test.ts +0 -424
  131. package/test/yaml-frontmatter.test.ts +0 -91
  132. package/test/yolo-mode.test.ts +0 -188
@@ -1,233 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
- import {
3
- createMcpPermissionTargets,
4
- McpTargetList,
5
- parseQualifiedMcpToolName,
6
- } from "#src/mcp-targets";
7
-
8
- describe("parseQualifiedMcpToolName", () => {
9
- it("returns server and tool for a valid qualified name", () => {
10
- expect(parseQualifiedMcpToolName("exa:search")).toEqual({
11
- server: "exa",
12
- tool: "search",
13
- });
14
- });
15
-
16
- it("returns server and tool with surrounding whitespace trimmed", () => {
17
- expect(parseQualifiedMcpToolName(" exa : search ")).toEqual({
18
- server: "exa",
19
- tool: "search",
20
- });
21
- });
22
-
23
- it("returns null for empty string", () => {
24
- expect(parseQualifiedMcpToolName("")).toBeNull();
25
- });
26
-
27
- it("returns null for whitespace-only string", () => {
28
- expect(parseQualifiedMcpToolName(" ")).toBeNull();
29
- });
30
-
31
- it("returns null when colon is the first character", () => {
32
- expect(parseQualifiedMcpToolName(":search")).toBeNull();
33
- });
34
-
35
- it("returns null when colon is the last character", () => {
36
- expect(parseQualifiedMcpToolName("exa:")).toBeNull();
37
- });
38
-
39
- it("returns null for a plain tool name with no colon", () => {
40
- expect(parseQualifiedMcpToolName("exa_search")).toBeNull();
41
- });
42
-
43
- it("returns null when server part is empty after trimming", () => {
44
- expect(parseQualifiedMcpToolName(" :search")).toBeNull();
45
- });
46
-
47
- it("returns null when tool part is empty after trimming", () => {
48
- expect(parseQualifiedMcpToolName("exa: ")).toBeNull();
49
- });
50
- });
51
-
52
- describe("createMcpPermissionTargets", () => {
53
- describe("tool call (input.tool)", () => {
54
- it("produces targets for a bare tool name with no configured servers", () => {
55
- const targets = createMcpPermissionTargets({ tool: "exa_search" }, []);
56
- expect(targets).toContain("exa_search");
57
- expect(targets).toContain("mcp_call");
58
- });
59
-
60
- it("produces targets for a qualified tool name (server:tool)", () => {
61
- const targets = createMcpPermissionTargets({ tool: "exa:search" }, []);
62
- expect(targets).toContain("exa_search");
63
- expect(targets).toContain("exa:search");
64
- expect(targets).toContain("exa");
65
- expect(targets).toContain("mcp_call");
66
- });
67
-
68
- it("produces targets for a tool call with explicit server field", () => {
69
- const targets = createMcpPermissionTargets(
70
- { tool: "search", server: "exa" },
71
- [],
72
- );
73
- expect(targets).toContain("exa_search");
74
- expect(targets).toContain("exa:search");
75
- expect(targets).toContain("exa");
76
- expect(targets).toContain("mcp_call");
77
- });
78
-
79
- it("derives server targets from configured server names when tool name ends with _<server>", () => {
80
- const targets = createMcpPermissionTargets({ tool: "exa_search" }, [
81
- "exa",
82
- ]);
83
- // exa_search ends with _exa? No — it ends with _search. This tool name
84
- // does NOT trigger server derivation because it does not end with _exa.
85
- expect(targets).toContain("exa_search");
86
- });
87
-
88
- it("does not include duplicate entries", () => {
89
- const targets = createMcpPermissionTargets({ tool: "exa:search" }, [
90
- "exa",
91
- ]);
92
- const unique = [...new Set(targets)];
93
- expect(targets).toEqual(unique);
94
- });
95
- });
96
-
97
- describe("connect call (input.connect)", () => {
98
- it("produces targets for a connect operation", () => {
99
- const targets = createMcpPermissionTargets({ connect: "exa" }, []);
100
- expect(targets).toContain("mcp_connect_exa");
101
- expect(targets).toContain("exa");
102
- expect(targets).toContain("mcp_connect");
103
- });
104
-
105
- it("does not include mcp_call for connect operations", () => {
106
- const targets = createMcpPermissionTargets({ connect: "exa" }, []);
107
- expect(targets).not.toContain("mcp_call");
108
- });
109
- });
110
-
111
- describe("describe operation (input.describe)", () => {
112
- it("produces targets for a describe operation on a qualified tool", () => {
113
- const targets = createMcpPermissionTargets(
114
- { describe: "exa:search" },
115
- [],
116
- );
117
- expect(targets).toContain("exa_search");
118
- expect(targets).toContain("exa:search");
119
- expect(targets).toContain("exa");
120
- expect(targets).toContain("mcp_describe");
121
- });
122
- });
123
-
124
- describe("search operation (input.search)", () => {
125
- it("produces mcp_search and the search string as targets", () => {
126
- const targets = createMcpPermissionTargets({ search: "weather" }, []);
127
- expect(targets).toContain("weather");
128
- expect(targets).toContain("mcp_search");
129
- });
130
-
131
- it("includes server targets when server is provided alongside search", () => {
132
- const targets = createMcpPermissionTargets(
133
- { search: "weather", server: "exa" },
134
- [],
135
- );
136
- expect(targets).toContain("mcp_server_exa");
137
- expect(targets).toContain("exa");
138
- expect(targets).toContain("mcp_search");
139
- });
140
- });
141
-
142
- describe("server listing (input.server only)", () => {
143
- it("produces mcp_list and server-specific targets", () => {
144
- const targets = createMcpPermissionTargets({ server: "exa" }, []);
145
- expect(targets).toContain("mcp_server_exa");
146
- expect(targets).toContain("exa");
147
- expect(targets).toContain("mcp_list");
148
- });
149
- });
150
-
151
- describe("status (no meaningful input)", () => {
152
- it("produces mcp_status for empty input", () => {
153
- const targets = createMcpPermissionTargets({}, []);
154
- expect(targets).toContain("mcp_status");
155
- });
156
-
157
- it("produces mcp_status for null input", () => {
158
- const targets = createMcpPermissionTargets(null, []);
159
- expect(targets).toContain("mcp_status");
160
- });
161
-
162
- it("produces mcp_status when no server/tool/connect/describe/search present", () => {
163
- const targets = createMcpPermissionTargets({ unrelated: "value" }, [
164
- "exa",
165
- ]);
166
- expect(targets).toContain("mcp_status");
167
- });
168
- });
169
-
170
- describe("priority ordering", () => {
171
- it("tool targets appear before mcp_call", () => {
172
- const targets = createMcpPermissionTargets({ tool: "exa:search" }, []);
173
- const mcpCallIdx = targets.indexOf("mcp_call");
174
- const exaSearchIdx = targets.indexOf("exa_search");
175
- expect(exaSearchIdx).toBeGreaterThanOrEqual(0);
176
- expect(mcpCallIdx).toBeGreaterThan(exaSearchIdx);
177
- });
178
- });
179
- });
180
-
181
- describe("McpTargetList", () => {
182
- describe("add", () => {
183
- it("ignores null", () => {
184
- const list = new McpTargetList();
185
- list.add(null);
186
- expect(list.toArray()).toEqual([]);
187
- });
188
-
189
- it("ignores empty string", () => {
190
- const list = new McpTargetList();
191
- list.add("");
192
- expect(list.toArray()).toEqual([]);
193
- });
194
-
195
- it("appends a new value", () => {
196
- const list = new McpTargetList();
197
- list.add("exa");
198
- expect(list.toArray()).toEqual(["exa"]);
199
- });
200
-
201
- it("dedups repeated values", () => {
202
- const list = new McpTargetList();
203
- list.add("exa");
204
- list.add("exa");
205
- expect(list.toArray()).toEqual(["exa"]);
206
- });
207
-
208
- it("preserves first-insertion order across a mix of values", () => {
209
- const list = new McpTargetList();
210
- list.add("exa_search");
211
- list.add("exa:search");
212
- list.add("exa");
213
- list.add("exa_search"); // duplicate — must not change order
214
- list.add("mcp_call");
215
- expect(list.toArray()).toEqual([
216
- "exa_search",
217
- "exa:search",
218
- "exa",
219
- "mcp_call",
220
- ]);
221
- });
222
- });
223
-
224
- describe("toArray", () => {
225
- it("returns an independent copy that does not mutate the list", () => {
226
- const list = new McpTargetList();
227
- list.add("exa");
228
- const first = list.toArray();
229
- first.push("mutated");
230
- expect(list.toArray()).toEqual(["exa"]);
231
- });
232
- });
233
- });
@@ -1,97 +0,0 @@
1
- import { beforeEach, describe, expect, test, vi } from "vitest";
2
-
3
- // Hoisted stubs for mocks that reference them in vi.mock factories.
4
- const { mockSpawnSync, mockExistsSync } = vi.hoisted(() => ({
5
- mockSpawnSync: vi.fn(),
6
- mockExistsSync: vi.fn(),
7
- }));
8
-
9
- // Mock node:child_process so tests don't spawn real subprocesses.
10
- vi.mock("node:child_process", () => ({
11
- spawnSync: mockSpawnSync,
12
- default: { spawnSync: mockSpawnSync },
13
- }));
14
-
15
- // Mock node:fs so existsSync is controllable.
16
- vi.mock("node:fs", () => ({
17
- existsSync: mockExistsSync,
18
- default: { existsSync: mockExistsSync },
19
- }));
20
-
21
- import { discoverGlobalNodeModulesRoot } from "#src/node-modules-discovery";
22
-
23
- describe("discoverGlobalNodeModulesRoot", () => {
24
- beforeEach(() => {
25
- mockSpawnSync.mockReset();
26
- mockExistsSync.mockReset();
27
- });
28
-
29
- test("returns node_modules root when URL is inside a node_modules tree", () => {
30
- const fakeUrl =
31
- "file:///opt/homebrew/lib/node_modules/@gotgenes/pi-permission-system/dist/external-directory.js";
32
- const result = discoverGlobalNodeModulesRoot(fakeUrl);
33
- expect(result).toBe("/opt/homebrew/lib/node_modules");
34
- expect(mockSpawnSync).not.toHaveBeenCalled();
35
- });
36
-
37
- test("calls npm root -g as fallback when walk-up finds no node_modules ancestor", () => {
38
- const npmRootPath = "/opt/homebrew/lib/node_modules";
39
- mockSpawnSync.mockReturnValue({
40
- status: 0,
41
- stdout: `${npmRootPath}\n`,
42
- });
43
- mockExistsSync.mockReturnValue(true);
44
-
45
- const fakeUrl = "file:///Users/dev/my-project/src/external-directory.ts";
46
- const result = discoverGlobalNodeModulesRoot(fakeUrl);
47
-
48
- expect(mockSpawnSync).toHaveBeenCalledWith(
49
- "npm",
50
- ["root", "-g"],
51
- expect.objectContaining({ encoding: "utf-8" }),
52
- );
53
- expect(result).toBe(npmRootPath);
54
- });
55
-
56
- test("returns null when walk-up fails and npm root -g returns non-zero exit", () => {
57
- mockSpawnSync.mockReturnValue({ status: 1, stdout: "" });
58
-
59
- const fakeUrl = "file:///Users/dev/my-project/src/external-directory.ts";
60
- const result = discoverGlobalNodeModulesRoot(fakeUrl);
61
-
62
- expect(result).toBeNull();
63
- });
64
-
65
- test("returns null when walk-up fails and spawnSync throws", () => {
66
- mockSpawnSync.mockImplementation(() => {
67
- throw new Error("ENOENT");
68
- });
69
-
70
- const fakeUrl = "file:///Users/dev/my-project/src/external-directory.ts";
71
- const result = discoverGlobalNodeModulesRoot(fakeUrl);
72
-
73
- expect(result).toBeNull();
74
- });
75
-
76
- test("returns null when walk-up fails and npm root -g returns non-existent path", () => {
77
- mockSpawnSync.mockReturnValue({
78
- status: 0,
79
- stdout: "/some/nonexistent/node_modules\n",
80
- });
81
- mockExistsSync.mockReturnValue(false);
82
-
83
- const fakeUrl = "file:///Users/dev/my-project/src/external-directory.ts";
84
- const result = discoverGlobalNodeModulesRoot(fakeUrl);
85
-
86
- expect(result).toBeNull();
87
- });
88
-
89
- test("returns null when walk-up fails and npm root -g returns empty stdout", () => {
90
- mockSpawnSync.mockReturnValue({ status: 0, stdout: " " });
91
-
92
- const fakeUrl = "file:///Users/dev/my-project/src/external-directory.ts";
93
- const result = discoverGlobalNodeModulesRoot(fakeUrl);
94
-
95
- expect(result).toBeNull();
96
- });
97
- });
@@ -1,247 +0,0 @@
1
- import { describe, expect, test } from "vitest";
2
- import { normalizeFlatConfig } from "#src/normalize";
3
-
4
- describe("normalizeFlatConfig", () => {
5
- describe("string shorthand", () => {
6
- test("string value produces a single catch-all rule for the surface", () => {
7
- const result = normalizeFlatConfig({ read: "allow" });
8
- expect(result).toEqual([
9
- { surface: "read", pattern: "*", action: "allow", origin: "builtin" },
10
- ]);
11
- });
12
-
13
- test("string shorthand works for multiple surfaces", () => {
14
- const result = normalizeFlatConfig({ read: "allow", write: "deny" });
15
- expect(result).toEqual([
16
- { surface: "read", pattern: "*", action: "allow", origin: "builtin" },
17
- { surface: "write", pattern: "*", action: "deny", origin: "builtin" },
18
- ]);
19
- });
20
-
21
- test("universal fallback '*' becomes a catch-all rule with surface '*'", () => {
22
- const result = normalizeFlatConfig({ "*": "ask" });
23
- expect(result).toEqual([
24
- { surface: "*", pattern: "*", action: "ask", origin: "builtin" },
25
- ]);
26
- });
27
-
28
- test("external_directory string shorthand maps directly to its surface", () => {
29
- const result = normalizeFlatConfig({ external_directory: "ask" });
30
- expect(result).toEqual([
31
- {
32
- surface: "external_directory",
33
- pattern: "*",
34
- action: "ask",
35
- origin: "builtin",
36
- },
37
- ]);
38
- });
39
-
40
- test("invalid string values (non-PermissionState) are ignored", () => {
41
- const result = normalizeFlatConfig({
42
- read: "allow",
43
- write: "invalid" as never,
44
- });
45
- expect(result).toEqual([
46
- { surface: "read", pattern: "*", action: "allow", origin: "builtin" },
47
- ]);
48
- });
49
- });
50
-
51
- describe("object pattern map", () => {
52
- test("object value produces one rule per pattern", () => {
53
- const result = normalizeFlatConfig({
54
- bash: { "*": "ask", "git *": "allow" },
55
- });
56
- expect(result).toEqual([
57
- { surface: "bash", pattern: "*", action: "ask", origin: "builtin" },
58
- {
59
- surface: "bash",
60
- pattern: "git *",
61
- action: "allow",
62
- origin: "builtin",
63
- },
64
- ]);
65
- });
66
-
67
- test("mcp object map produces rules with surface 'mcp'", () => {
68
- const result = normalizeFlatConfig({
69
- mcp: { "*": "ask", mcp_status: "allow" },
70
- });
71
- expect(result).toEqual([
72
- { surface: "mcp", pattern: "*", action: "ask", origin: "builtin" },
73
- {
74
- surface: "mcp",
75
- pattern: "mcp_status",
76
- action: "allow",
77
- origin: "builtin",
78
- },
79
- ]);
80
- });
81
-
82
- test("skill object map produces rules with surface 'skill'", () => {
83
- const result = normalizeFlatConfig({
84
- skill: { "*": "ask", librarian: "allow" },
85
- });
86
- expect(result).toEqual([
87
- { surface: "skill", pattern: "*", action: "ask", origin: "builtin" },
88
- {
89
- surface: "skill",
90
- pattern: "librarian",
91
- action: "allow",
92
- origin: "builtin",
93
- },
94
- ]);
95
- });
96
-
97
- test("invalid action values in object map are ignored", () => {
98
- const result = normalizeFlatConfig({
99
- bash: { "git *": "allow", "rm -rf *": "bad" as never },
100
- });
101
- expect(result).toEqual([
102
- {
103
- surface: "bash",
104
- pattern: "git *",
105
- action: "allow",
106
- origin: "builtin",
107
- },
108
- ]);
109
- });
110
- });
111
-
112
- describe("mixed surfaces", () => {
113
- test("full mixed config produces rules in insertion order", () => {
114
- const result = normalizeFlatConfig({
115
- "*": "ask",
116
- read: "allow",
117
- write: "deny",
118
- bash: { "*": "ask", "git *": "allow" },
119
- mcp: { mcp_status: "allow" },
120
- skill: { "*": "ask" },
121
- external_directory: "ask",
122
- });
123
- expect(result).toEqual([
124
- { surface: "*", pattern: "*", action: "ask", origin: "builtin" },
125
- { surface: "read", pattern: "*", action: "allow", origin: "builtin" },
126
- { surface: "write", pattern: "*", action: "deny", origin: "builtin" },
127
- { surface: "bash", pattern: "*", action: "ask", origin: "builtin" },
128
- {
129
- surface: "bash",
130
- pattern: "git *",
131
- action: "allow",
132
- origin: "builtin",
133
- },
134
- {
135
- surface: "mcp",
136
- pattern: "mcp_status",
137
- action: "allow",
138
- origin: "builtin",
139
- },
140
- { surface: "skill", pattern: "*", action: "ask", origin: "builtin" },
141
- {
142
- surface: "external_directory",
143
- pattern: "*",
144
- action: "ask",
145
- origin: "builtin",
146
- },
147
- ]);
148
- });
149
- });
150
-
151
- describe("empty and edge cases", () => {
152
- test("empty permission object produces empty ruleset", () => {
153
- expect(normalizeFlatConfig({})).toEqual([]);
154
- });
155
-
156
- test("non-object values (null, array) nested in map are skipped", () => {
157
- const result = normalizeFlatConfig({
158
- bash: null as never,
159
- read: "allow",
160
- });
161
- expect(result).toEqual([
162
- { surface: "read", pattern: "*", action: "allow", origin: "builtin" },
163
- ]);
164
- });
165
- });
166
-
167
- describe("deny with reason", () => {
168
- test("{ action: 'deny', reason } produces a deny rule carrying the reason", () => {
169
- const result = normalizeFlatConfig({
170
- bash: { "npm *": { action: "deny", reason: "Use pnpm instead" } },
171
- });
172
- expect(result).toEqual([
173
- {
174
- surface: "bash",
175
- pattern: "npm *",
176
- action: "deny",
177
- reason: "Use pnpm instead",
178
- origin: "builtin",
179
- },
180
- ]);
181
- });
182
-
183
- test("{ action: 'deny' } without a reason produces a deny rule without reason", () => {
184
- const result = normalizeFlatConfig({
185
- bash: { "rm -rf *": { action: "deny" } },
186
- });
187
- expect(result).toEqual([
188
- {
189
- surface: "bash",
190
- pattern: "rm -rf *",
191
- action: "deny",
192
- origin: "builtin",
193
- },
194
- ]);
195
- });
196
-
197
- test("deny-with-reason and plain strings coexist in the same surface", () => {
198
- const result = normalizeFlatConfig({
199
- bash: {
200
- "git *": "allow",
201
- "npm *": { action: "deny", reason: "Use pnpm" },
202
- "*": "ask",
203
- },
204
- });
205
- expect(result).toEqual([
206
- {
207
- surface: "bash",
208
- pattern: "git *",
209
- action: "allow",
210
- origin: "builtin",
211
- },
212
- {
213
- surface: "bash",
214
- pattern: "npm *",
215
- action: "deny",
216
- reason: "Use pnpm",
217
- origin: "builtin",
218
- },
219
- { surface: "bash", pattern: "*", action: "ask", origin: "builtin" },
220
- ]);
221
- });
222
-
223
- test("top-level deny-with-reason object is treated as a pattern map", () => {
224
- // At the surface level, { action: "deny", reason: "..." } is parsed as a
225
- // pattern→action map: "action" is a pattern key with action "deny", and
226
- // "reason" maps to a non-PermissionState string that is dropped.
227
- const result = normalizeFlatConfig({
228
- bash: { action: "deny", reason: "Not allowed" } as never,
229
- });
230
- expect(result).toEqual([
231
- {
232
- surface: "bash",
233
- pattern: "action",
234
- action: "deny",
235
- origin: "builtin",
236
- },
237
- ]);
238
- });
239
-
240
- test("non-string reason is rejected (malformed config)", () => {
241
- const result = normalizeFlatConfig({
242
- bash: { "npm *": { action: "deny", reason: 42 } as never },
243
- });
244
- expect(result).toEqual([]);
245
- });
246
- });
247
- });