@gotgenes/pi-permission-system 18.1.1 → 18.2.0

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 (133) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/docs/configuration.md +11 -0
  3. package/package.json +1 -2
  4. package/src/access-intent/access-path.ts +29 -4
  5. package/src/access-intent/bash/bash-path-resolver.ts +39 -18
  6. package/src/access-intent/bash/msys-bash-tokens.ts +64 -0
  7. package/src/extension-config.ts +7 -0
  8. package/src/forwarded-permissions/permission-forwarder.ts +6 -3
  9. package/src/handlers/gates/helpers.ts +1 -1
  10. package/src/handlers/gates/runner.ts +22 -0
  11. package/src/index.ts +10 -6
  12. package/src/path-normalizer.ts +105 -2
  13. package/src/permission-manager.ts +21 -1
  14. package/src/permission-prompter.ts +10 -16
  15. package/src/prompting-gateway.ts +13 -17
  16. package/src/rule.ts +21 -1
  17. package/src/status.ts +1 -1
  18. package/src/yolo-mode.ts +0 -30
  19. package/test/access-intent/access-path.test.ts +0 -277
  20. package/test/access-intent/bash/node-text.test.ts +0 -148
  21. package/test/access-intent/bash/parser.test.ts +0 -19
  22. package/test/access-intent/bash/program.test.ts +0 -673
  23. package/test/access-intent/bash/token-classification.test.ts +0 -363
  24. package/test/access-intent/bash/token-collection.test.ts +0 -300
  25. package/test/active-agent.test.ts +0 -155
  26. package/test/async-cache.test.ts +0 -48
  27. package/test/bash-arity.test.ts +0 -144
  28. package/test/bash-external-directory.test.ts +0 -1022
  29. package/test/builtin-tool-input-formatters.test.ts +0 -109
  30. package/test/canonicalize-path.test.ts +0 -119
  31. package/test/composition-root.test.ts +0 -698
  32. package/test/config-loader.test.ts +0 -740
  33. package/test/config-modal.test.ts +0 -320
  34. package/test/config-paths.test.ts +0 -83
  35. package/test/config-pipeline.test.ts +0 -90
  36. package/test/config-reporter.test.ts +0 -147
  37. package/test/config-store.test.ts +0 -466
  38. package/test/decision-audit.test.ts +0 -72
  39. package/test/decision-reporter.test.ts +0 -112
  40. package/test/denial-messages.test.ts +0 -714
  41. package/test/detect-permissive-bash-fallback.test.ts +0 -56
  42. package/test/expand-home.test.ts +0 -93
  43. package/test/extension-config.test.ts +0 -129
  44. package/test/extension-paths.test.ts +0 -108
  45. package/test/forwarded-permissions/io.test.ts +0 -251
  46. package/test/forwarding-manager.test.ts +0 -200
  47. package/test/handlers/before-agent-start.test.ts +0 -314
  48. package/test/handlers/external-directory-integration.test.ts +0 -515
  49. package/test/handlers/external-directory-session-dedup.test.ts +0 -175
  50. package/test/handlers/external-directory-symlink-acceptance.test.ts +0 -167
  51. package/test/handlers/gates/bash-command-metamorphic.test.ts +0 -88
  52. package/test/handlers/gates/bash-command.test.ts +0 -257
  53. package/test/handlers/gates/bash-external-directory.test.ts +0 -268
  54. package/test/handlers/gates/bash-path.test.ts +0 -346
  55. package/test/handlers/gates/candidate-check.test.ts +0 -52
  56. package/test/handlers/gates/external-directory-messages.test.ts +0 -85
  57. package/test/handlers/gates/external-directory-policy.test.ts +0 -134
  58. package/test/handlers/gates/external-directory.test.ts +0 -267
  59. package/test/handlers/gates/helpers.test.ts +0 -165
  60. package/test/handlers/gates/path.test.ts +0 -369
  61. package/test/handlers/gates/runner.test.ts +0 -408
  62. package/test/handlers/gates/skill-input-gate-pipeline.test.ts +0 -176
  63. package/test/handlers/gates/skill-input.test.ts +0 -128
  64. package/test/handlers/gates/skill-read.test.ts +0 -161
  65. package/test/handlers/gates/tool-call-gate-pipeline.test.ts +0 -356
  66. package/test/handlers/gates/tool.test.ts +0 -244
  67. package/test/handlers/input-events.test.ts +0 -168
  68. package/test/handlers/input.test.ts +0 -199
  69. package/test/handlers/lifecycle.test.ts +0 -221
  70. package/test/handlers/tool-call-boundary.test.ts +0 -145
  71. package/test/handlers/tool-call-events.test.ts +0 -277
  72. package/test/handlers/tool-call.test.ts +0 -395
  73. package/test/handlers/validate-requested-tool.test.ts +0 -92
  74. package/test/helpers/external-directory-fixtures.ts +0 -269
  75. package/test/helpers/gate-fixtures.ts +0 -316
  76. package/test/helpers/handler-fixtures.ts +0 -335
  77. package/test/helpers/make-fake-pi.ts +0 -100
  78. package/test/helpers/manager-harness.ts +0 -112
  79. package/test/helpers/session-fixtures.ts +0 -199
  80. package/test/input-normalizer.test.ts +0 -325
  81. package/test/logging.test.ts +0 -51
  82. package/test/mcp-targets.test.ts +0 -233
  83. package/test/node-modules-discovery.test.ts +0 -97
  84. package/test/normalize.test.ts +0 -247
  85. package/test/path-containment.test.ts +0 -161
  86. package/test/path-normalization.test.ts +0 -233
  87. package/test/path-normalizer.test.ts +0 -196
  88. package/test/path-surfaces.test.ts +0 -55
  89. package/test/pattern-suggest.test.ts +0 -248
  90. package/test/permission-dialog.test.ts +0 -205
  91. package/test/permission-event-rpc.test.ts +0 -560
  92. package/test/permission-events.test.ts +0 -400
  93. package/test/permission-forwarder.test.ts +0 -370
  94. package/test/permission-forwarding.test.ts +0 -315
  95. package/test/permission-gate.test.ts +0 -269
  96. package/test/permission-manager-unified.test.ts +0 -3714
  97. package/test/permission-merge.test.ts +0 -61
  98. package/test/permission-prompter.test.ts +0 -518
  99. package/test/permission-prompts.test.ts +0 -363
  100. package/test/permission-resolver.test.ts +0 -277
  101. package/test/permission-session.test.ts +0 -404
  102. package/test/permission-ui-prompt.test.ts +0 -146
  103. package/test/permissions-service.test.ts +0 -192
  104. package/test/pi-infrastructure-read.test.ts +0 -432
  105. package/test/policy-loader.test.ts +0 -561
  106. package/test/prompting-gateway.test.ts +0 -231
  107. package/test/rule.test.ts +0 -650
  108. package/test/safe-system-paths.test.ts +0 -46
  109. package/test/scope-merge.test.ts +0 -116
  110. package/test/service-lifecycle.test.ts +0 -163
  111. package/test/service.test.ts +0 -261
  112. package/test/session-approval.test.ts +0 -75
  113. package/test/session-logger.test.ts +0 -200
  114. package/test/session-rules.test.ts +0 -321
  115. package/test/session-start.test.ts +0 -112
  116. package/test/skill-prompt-sanitizer.test.ts +0 -418
  117. package/test/status.test.ts +0 -10
  118. package/test/subagent-context.test.ts +0 -372
  119. package/test/subagent-lifecycle-events.test.ts +0 -132
  120. package/test/subagent-registry.test.ts +0 -145
  121. package/test/synthesize.test.ts +0 -302
  122. package/test/system-prompt-sanitizer.test.ts +0 -382
  123. package/test/tool-access-extractor-registry.test.ts +0 -77
  124. package/test/tool-input-formatter-registry.test.ts +0 -75
  125. package/test/tool-input-path.test.ts +0 -84
  126. package/test/tool-input-preview.test.ts +0 -129
  127. package/test/tool-input-prompt-formatters.test.ts +0 -115
  128. package/test/tool-preview-formatter.test.ts +0 -458
  129. package/test/tool-registry.test.ts +0 -197
  130. package/test/value-guards.test.ts +0 -193
  131. package/test/wildcard-matcher.test.ts +0 -424
  132. package/test/yaml-frontmatter.test.ts +0 -91
  133. package/test/yolo-mode.test.ts +0 -188
@@ -1,740 +0,0 @@
1
- import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
2
- import { tmpdir } from "node:os";
3
- import { join } from "node:path";
4
- import { afterEach, beforeEach, describe, expect, it } from "vitest";
5
-
6
- import {
7
- loadAndMergeConfigs,
8
- loadUnifiedConfig,
9
- mergeUnifiedConfigs,
10
- stripJsonComments,
11
- } from "#src/config-loader";
12
-
13
- describe("stripJsonComments", () => {
14
- it("returns empty string for empty input", () => {
15
- expect(stripJsonComments("")).toBe("");
16
- });
17
-
18
- it("passes through plain JSON unchanged", () => {
19
- const input = '{"key": true}';
20
- expect(stripJsonComments(input)).toBe(input);
21
- });
22
-
23
- it("drops a line comment body and preserves the trailing newline", () => {
24
- // The space before // is emitted; the comment body is dropped; \n is kept.
25
- expect(stripJsonComments('{ // comment\n"k": 1}')).toBe('{ \n"k": 1}');
26
- });
27
-
28
- it("drops a line comment that runs to EOF with no trailing newline", () => {
29
- expect(stripJsonComments('{"k": 1} // trailing')).toBe('{"k": 1} ');
30
- });
31
-
32
- it("drops a block comment and nothing else", () => {
33
- expect(stripJsonComments('{ /* block */ "k": 1}')).toBe('{ "k": 1}');
34
- });
35
-
36
- it("drops an unterminated block comment to EOF", () => {
37
- expect(stripJsonComments("{ /* no close")).toBe("{ ");
38
- });
39
-
40
- it("preserves // inside a double-quoted string", () => {
41
- expect(stripJsonComments('{"url": "http://example.com"}')).toBe(
42
- '{"url": "http://example.com"}',
43
- );
44
- });
45
-
46
- it("preserves block-comment markers inside a double-quoted string", () => {
47
- expect(stripJsonComments('{"v": "a /* b */ c"}')).toBe(
48
- '{"v": "a /* b */ c"}',
49
- );
50
- });
51
-
52
- it("preserves // inside a single-quoted string", () => {
53
- expect(stripJsonComments("{'url': 'http://x.com'}")).toBe(
54
- "{'url': 'http://x.com'}",
55
- );
56
- });
57
-
58
- it("preserves block-comment markers inside a single-quoted string", () => {
59
- expect(stripJsonComments("{'v': 'a /* b */ c'}")).toBe(
60
- "{'v': 'a /* b */ c'}",
61
- );
62
- });
63
-
64
- it("honors a backslash-escaped quote so it does not close the string", () => {
65
- // The string value is: a\"b (backslash-escaped double quote)
66
- expect(stripJsonComments('{"k": "a\\"b"}')).toBe('{"k": "a\\"b"}');
67
- });
68
-
69
- it("emits an unterminated string to EOF verbatim", () => {
70
- expect(stripJsonComments('{"k": "unterminated')).toBe(
71
- '{"k": "unterminated',
72
- );
73
- });
74
-
75
- it("preserves a lone slash that is not part of // or /*", () => {
76
- expect(stripJsonComments('{"v": 1/2}')).toBe('{"v": 1/2}');
77
- });
78
-
79
- it("handles a combined JSONC document that round-trips to valid JSON", () => {
80
- const jsonc = [
81
- "{",
82
- ' "debugLog": true, // runtime knob',
83
- ' "permission": { /* the policy */ "*": "ask" }',
84
- "}",
85
- ].join("\n");
86
- const stripped = stripJsonComments(jsonc);
87
- // Must parse without throwing
88
- const parsed = JSON.parse(stripped) as Record<string, unknown>;
89
- expect(parsed.debugLog).toBe(true);
90
- expect(parsed.permission).toEqual({ "*": "ask" });
91
- });
92
- });
93
-
94
- describe("loadUnifiedConfig", () => {
95
- let tempDir: string;
96
-
97
- beforeEach(() => {
98
- tempDir = mkdtempSync(join(tmpdir(), "config-loader-test-"));
99
- });
100
-
101
- afterEach(() => {
102
- rmSync(tempDir, { recursive: true, force: true });
103
- });
104
-
105
- it("parses a valid JSON file with runtime knobs and flat permission", () => {
106
- const configPath = join(tempDir, "config.json");
107
- writeFileSync(
108
- configPath,
109
- JSON.stringify({
110
- debugLog: true,
111
- permissionReviewLog: false,
112
- yoloMode: true,
113
- permission: {
114
- "*": "ask",
115
- read: "allow",
116
- bash: { "git status": "allow" },
117
- },
118
- }),
119
- );
120
-
121
- const result = loadUnifiedConfig(configPath);
122
- expect(result.issues).toEqual([]);
123
- expect(result.config.debugLog).toBe(true);
124
- expect(result.config.permissionReviewLog).toBe(false);
125
- expect(result.config.yoloMode).toBe(true);
126
- expect(result.config.permission).toEqual({
127
- "*": "ask",
128
- read: "allow",
129
- bash: { "git status": "allow" },
130
- });
131
- });
132
-
133
- it("strips JSONC comments before parsing", () => {
134
- const configPath = join(tempDir, "config.json");
135
- writeFileSync(
136
- configPath,
137
- `{
138
- // This is a comment
139
- "debugLog": true,
140
- /* block comment */
141
- "permission": { "*": "ask" }
142
- }`,
143
- );
144
-
145
- const result = loadUnifiedConfig(configPath);
146
- expect(result.issues).toEqual([]);
147
- expect(result.config.debugLog).toBe(true);
148
- expect(result.config.permission).toEqual({ "*": "ask" });
149
- });
150
-
151
- it("ignores unknown keys without emitting issues", () => {
152
- const configPath = join(tempDir, "config.json");
153
- writeFileSync(
154
- configPath,
155
- JSON.stringify({
156
- debugLog: false,
157
- unknownField: "ignored",
158
- anotherRandom: 42,
159
- }),
160
- );
161
-
162
- const result = loadUnifiedConfig(configPath);
163
- expect(result.issues).toEqual([]);
164
- expect(result.config.debugLog).toBe(false);
165
- expect(result.config).not.toHaveProperty("unknownField");
166
- });
167
-
168
- it("returns empty config and no issues when the file does not exist", () => {
169
- const configPath = join(tempDir, "nonexistent.json");
170
- const result = loadUnifiedConfig(configPath);
171
- expect(result.issues).toEqual([]);
172
- expect(result.config.debugLog).toBeUndefined();
173
- expect(result.config.permission).toBeUndefined();
174
- });
175
-
176
- it("returns empty config and an issue when the file contains invalid JSON", () => {
177
- const configPath = join(tempDir, "config.json");
178
- writeFileSync(configPath, "not valid json {{{");
179
-
180
- const result = loadUnifiedConfig(configPath);
181
- expect(result.issues).toHaveLength(1);
182
- expect(result.issues[0]).toContain(configPath);
183
- });
184
-
185
- it("normalizes boolean fields strictly", () => {
186
- const configPath = join(tempDir, "config.json");
187
- writeFileSync(
188
- configPath,
189
- JSON.stringify({
190
- debugLog: "yes",
191
- permissionReviewLog: 1,
192
- yoloMode: null,
193
- }),
194
- );
195
-
196
- const result = loadUnifiedConfig(configPath);
197
- expect(result.config.debugLog).toBeUndefined();
198
- expect(result.config.permissionReviewLog).toBeUndefined();
199
- expect(result.config.yoloMode).toBeUndefined();
200
- });
201
-
202
- it("normalizes permission map, keeping only valid PermissionState values", () => {
203
- const configPath = join(tempDir, "config.json");
204
- writeFileSync(
205
- configPath,
206
- JSON.stringify({
207
- permission: {
208
- read: "allow",
209
- write: "invalid",
210
- bash: { "git *": "ask", "rm -rf": 42 },
211
- },
212
- }),
213
- );
214
-
215
- const result = loadUnifiedConfig(configPath);
216
- expect(result.config.permission).toEqual({
217
- read: "allow",
218
- bash: { "git *": "ask" },
219
- });
220
- });
221
-
222
- it("accepts permission as object with mixed string and object values", () => {
223
- const configPath = join(tempDir, "config.json");
224
- writeFileSync(
225
- configPath,
226
- JSON.stringify({
227
- permission: {
228
- "*": "ask",
229
- read: "allow",
230
- bash: { "*": "ask", "git *": "allow" },
231
- external_directory: "ask",
232
- },
233
- }),
234
- );
235
-
236
- const result = loadUnifiedConfig(configPath);
237
- expect(result.issues).toEqual([]);
238
- expect(result.config.permission).toEqual({
239
- "*": "ask",
240
- read: "allow",
241
- bash: { "*": "ask", "git *": "allow" },
242
- external_directory: "ask",
243
- });
244
- });
245
-
246
- it("preserves a deny-with-reason object inside a pattern map", () => {
247
- const configPath = join(tempDir, "config.json");
248
- writeFileSync(
249
- configPath,
250
- JSON.stringify({
251
- permission: {
252
- bash: {
253
- "git *": "allow",
254
- "npm *": { action: "deny", reason: "Use pnpm instead" },
255
- },
256
- },
257
- }),
258
- );
259
-
260
- const result = loadUnifiedConfig(configPath);
261
- expect(result.config.permission).toEqual({
262
- bash: {
263
- "git *": "allow",
264
- "npm *": { action: "deny", reason: "Use pnpm instead" },
265
- },
266
- });
267
- });
268
-
269
- it("strips a deny object with a non-string reason (malformed)", () => {
270
- const configPath = join(tempDir, "config.json");
271
- writeFileSync(
272
- configPath,
273
- JSON.stringify({
274
- permission: {
275
- bash: {
276
- "git *": "allow",
277
- "npm *": { action: "deny", reason: 42 },
278
- },
279
- },
280
- }),
281
- );
282
-
283
- const result = loadUnifiedConfig(configPath);
284
- expect(result.config.permission).toEqual({
285
- bash: { "git *": "allow" },
286
- });
287
- });
288
-
289
- it("returns no permission when the permission field is absent", () => {
290
- const configPath = join(tempDir, "config.json");
291
- writeFileSync(configPath, JSON.stringify({ debugLog: false }));
292
-
293
- const result = loadUnifiedConfig(configPath);
294
- expect(result.config.permission).toBeUndefined();
295
- });
296
-
297
- it("ignores a non-object permission field", () => {
298
- const configPath = join(tempDir, "config.json");
299
- writeFileSync(configPath, JSON.stringify({ permission: "allow" }));
300
-
301
- const result = loadUnifiedConfig(configPath);
302
- expect(result.config.permission).toBeUndefined();
303
- });
304
-
305
- it("parses toolInputPreviewMaxLength when a valid positive integer is present", () => {
306
- const configPath = join(tempDir, "config.json");
307
- writeFileSync(
308
- configPath,
309
- JSON.stringify({ toolInputPreviewMaxLength: 1000 }),
310
- );
311
- const result = loadUnifiedConfig(configPath);
312
- expect(result.config.toolInputPreviewMaxLength).toBe(1000);
313
- });
314
-
315
- it("parses toolTextSummaryMaxLength when a valid positive integer is present", () => {
316
- const configPath = join(tempDir, "config.json");
317
- writeFileSync(
318
- configPath,
319
- JSON.stringify({ toolTextSummaryMaxLength: 120 }),
320
- );
321
- const result = loadUnifiedConfig(configPath);
322
- expect(result.config.toolTextSummaryMaxLength).toBe(120);
323
- });
324
-
325
- it("omits toolInputPreviewMaxLength when absent", () => {
326
- const configPath = join(tempDir, "config.json");
327
- writeFileSync(configPath, JSON.stringify({ debugLog: false }));
328
- const result = loadUnifiedConfig(configPath);
329
- expect(result.config).not.toHaveProperty("toolInputPreviewMaxLength");
330
- });
331
-
332
- it("omits toolTextSummaryMaxLength when absent", () => {
333
- const configPath = join(tempDir, "config.json");
334
- writeFileSync(configPath, JSON.stringify({ debugLog: false }));
335
- const result = loadUnifiedConfig(configPath);
336
- expect(result.config).not.toHaveProperty("toolTextSummaryMaxLength");
337
- });
338
-
339
- it.each([
340
- ["zero", 0],
341
- ["negative", -1],
342
- ["float", 1.5],
343
- ["string", "200"],
344
- ["boolean", true],
345
- ] as const)("omits toolInputPreviewMaxLength for invalid value: %s", (_label, value) => {
346
- const configPath = join(tempDir, "config.json");
347
- writeFileSync(
348
- configPath,
349
- JSON.stringify({ toolInputPreviewMaxLength: value }),
350
- );
351
- const result = loadUnifiedConfig(configPath);
352
- expect(result.config).not.toHaveProperty("toolInputPreviewMaxLength");
353
- });
354
-
355
- it.each([
356
- ["zero", 0],
357
- ["negative", -1],
358
- ["float", 1.5],
359
- ["string", "80"],
360
- ["boolean", false],
361
- ] as const)("omits toolTextSummaryMaxLength for invalid value: %s", (_label, value) => {
362
- const configPath = join(tempDir, "config.json");
363
- writeFileSync(
364
- configPath,
365
- JSON.stringify({ toolTextSummaryMaxLength: value }),
366
- );
367
- const result = loadUnifiedConfig(configPath);
368
- expect(result.config).not.toHaveProperty("toolTextSummaryMaxLength");
369
- });
370
-
371
- it("parses piInfrastructureReadPaths when a valid string array is present", () => {
372
- const configPath = join(tempDir, "config.json");
373
- writeFileSync(
374
- configPath,
375
- JSON.stringify({ piInfrastructureReadPaths: ["/extra/path"] }),
376
- );
377
- const result = loadUnifiedConfig(configPath);
378
- expect(result.config.piInfrastructureReadPaths).toEqual(["/extra/path"]);
379
- });
380
-
381
- it("parses piInfrastructureReadPaths as empty array when set to []", () => {
382
- const configPath = join(tempDir, "config.json");
383
- writeFileSync(
384
- configPath,
385
- JSON.stringify({ piInfrastructureReadPaths: [] }),
386
- );
387
- const result = loadUnifiedConfig(configPath);
388
- expect(result.config.piInfrastructureReadPaths).toEqual([]);
389
- });
390
-
391
- it("omits piInfrastructureReadPaths when absent", () => {
392
- const configPath = join(tempDir, "config.json");
393
- writeFileSync(configPath, JSON.stringify({ debugLog: false }));
394
- const result = loadUnifiedConfig(configPath);
395
- expect(result.config).not.toHaveProperty("piInfrastructureReadPaths");
396
- });
397
-
398
- it.each([
399
- ["string", "not-an-array"],
400
- ["number", 42],
401
- ["mixed-type array", ["a", 1]],
402
- ["object", { a: "b" }],
403
- ] as const)("omits piInfrastructureReadPaths for invalid value: %s", (_label, value) => {
404
- const configPath = join(tempDir, "config.json");
405
- writeFileSync(
406
- configPath,
407
- JSON.stringify({ piInfrastructureReadPaths: value }),
408
- );
409
- const result = loadUnifiedConfig(configPath);
410
- expect(result.config).not.toHaveProperty("piInfrastructureReadPaths");
411
- });
412
- });
413
-
414
- describe("mergeUnifiedConfigs", () => {
415
- it("deep-merges permission objects so project overrides global per-key", () => {
416
- const merged = mergeUnifiedConfigs(
417
- {
418
- permission: {
419
- "*": "ask",
420
- read: "allow",
421
- bash: { "git status": "allow" },
422
- },
423
- },
424
- {
425
- permission: {
426
- "*": "allow",
427
- bash: { "rm -rf *": "deny" },
428
- },
429
- },
430
- );
431
-
432
- expect(merged.permission).toEqual({
433
- "*": "allow",
434
- read: "allow",
435
- bash: { "git status": "allow", "rm -rf *": "deny" },
436
- });
437
- });
438
-
439
- it("string permission value in override replaces base string for same key", () => {
440
- const merged = mergeUnifiedConfigs(
441
- { permission: { read: "ask" } },
442
- { permission: { read: "allow" } },
443
- );
444
- expect(merged.permission).toEqual({ read: "allow" });
445
- });
446
-
447
- it("object replaces string when override uses object for same surface", () => {
448
- const merged = mergeUnifiedConfigs(
449
- { permission: { bash: "ask" } },
450
- { permission: { bash: { "*": "allow", "rm -rf *": "deny" } } },
451
- );
452
- expect(merged.permission).toEqual({
453
- bash: { "*": "allow", "rm -rf *": "deny" },
454
- });
455
- });
456
-
457
- it("string replaces object when override uses string for same surface", () => {
458
- const merged = mergeUnifiedConfigs(
459
- { permission: { bash: { "git *": "allow" } } },
460
- { permission: { bash: "deny" } },
461
- );
462
- expect(merged.permission).toEqual({ bash: "deny" });
463
- });
464
-
465
- it("replaces scalar runtime knobs (project wins)", () => {
466
- const merged = mergeUnifiedConfigs(
467
- { debugLog: true, permissionReviewLog: true, yoloMode: false },
468
- { debugLog: false, yoloMode: true },
469
- );
470
-
471
- expect(merged.debugLog).toBe(false);
472
- expect(merged.permissionReviewLog).toBe(true);
473
- expect(merged.yoloMode).toBe(true);
474
- });
475
-
476
- it("returns base unchanged when override is empty", () => {
477
- const base = {
478
- debugLog: true,
479
- permission: { read: "allow" as const },
480
- };
481
- const merged = mergeUnifiedConfigs(base, {});
482
-
483
- expect(merged.debugLog).toBe(true);
484
- expect(merged.permission).toEqual({ read: "allow" });
485
- });
486
-
487
- it("returns override unchanged when base is empty", () => {
488
- const override = {
489
- yoloMode: true,
490
- permission: { bash: { "rm -rf *": "deny" as const } },
491
- };
492
- const merged = mergeUnifiedConfigs({}, override);
493
-
494
- expect(merged.yoloMode).toBe(true);
495
- expect(merged.permission).toEqual({ bash: { "rm -rf *": "deny" } });
496
- });
497
-
498
- it("does not set undefined keys in the merged result", () => {
499
- const merged = mergeUnifiedConfigs({ debugLog: true }, { yoloMode: false });
500
-
501
- expect(merged.debugLog).toBe(true);
502
- expect(merged.yoloMode).toBe(false);
503
- expect(merged).not.toHaveProperty("permissionReviewLog");
504
- expect(merged).not.toHaveProperty("permission");
505
- });
506
-
507
- it("override toolInputPreviewMaxLength replaces base value", () => {
508
- const merged = mergeUnifiedConfigs(
509
- { toolInputPreviewMaxLength: 200 },
510
- { toolInputPreviewMaxLength: 1000 },
511
- );
512
- expect(merged.toolInputPreviewMaxLength).toBe(1000);
513
- });
514
-
515
- it("base toolInputPreviewMaxLength survives when override omits it", () => {
516
- const merged = mergeUnifiedConfigs(
517
- { toolInputPreviewMaxLength: 500 },
518
- { debugLog: true },
519
- );
520
- expect(merged.toolInputPreviewMaxLength).toBe(500);
521
- });
522
-
523
- it("toolInputPreviewMaxLength is absent when both base and override omit it", () => {
524
- const merged = mergeUnifiedConfigs({ debugLog: true }, { yoloMode: false });
525
- expect(merged).not.toHaveProperty("toolInputPreviewMaxLength");
526
- });
527
-
528
- it("override toolTextSummaryMaxLength replaces base value", () => {
529
- const merged = mergeUnifiedConfigs(
530
- { toolTextSummaryMaxLength: 80 },
531
- { toolTextSummaryMaxLength: 200 },
532
- );
533
- expect(merged.toolTextSummaryMaxLength).toBe(200);
534
- });
535
-
536
- it("base toolTextSummaryMaxLength survives when override omits it", () => {
537
- const merged = mergeUnifiedConfigs(
538
- { toolTextSummaryMaxLength: 120 },
539
- { debugLog: false },
540
- );
541
- expect(merged.toolTextSummaryMaxLength).toBe(120);
542
- });
543
-
544
- it("toolTextSummaryMaxLength is absent when both base and override omit it", () => {
545
- const merged = mergeUnifiedConfigs({}, { permissionReviewLog: true });
546
- expect(merged).not.toHaveProperty("toolTextSummaryMaxLength");
547
- });
548
-
549
- it("override piInfrastructureReadPaths replaces base array", () => {
550
- const merged = mergeUnifiedConfigs(
551
- { piInfrastructureReadPaths: ["/base/path"] },
552
- { piInfrastructureReadPaths: ["/override/path"] },
553
- );
554
- expect(merged.piInfrastructureReadPaths).toEqual(["/override/path"]);
555
- });
556
-
557
- it("base piInfrastructureReadPaths survives when override omits it", () => {
558
- const merged = mergeUnifiedConfigs(
559
- { piInfrastructureReadPaths: ["/kept/path"] },
560
- { debugLog: true },
561
- );
562
- expect(merged.piInfrastructureReadPaths).toEqual(["/kept/path"]);
563
- });
564
-
565
- it("piInfrastructureReadPaths is absent when both base and override omit it", () => {
566
- const merged = mergeUnifiedConfigs({ debugLog: true }, { yoloMode: false });
567
- expect(merged).not.toHaveProperty("piInfrastructureReadPaths");
568
- });
569
-
570
- it("override piInfrastructureReadPaths as empty array replaces non-empty base", () => {
571
- const merged = mergeUnifiedConfigs(
572
- { piInfrastructureReadPaths: ["/base/path"] },
573
- { piInfrastructureReadPaths: [] },
574
- );
575
- expect(merged.piInfrastructureReadPaths).toEqual([]);
576
- });
577
- });
578
-
579
- describe("loadAndMergeConfigs", () => {
580
- let tempDir: string;
581
- let agentDir: string;
582
- let cwd: string;
583
- let extensionRoot: string;
584
-
585
- beforeEach(() => {
586
- tempDir = mkdtempSync(join(tmpdir(), "config-merge-test-"));
587
- agentDir = join(tempDir, "agent");
588
- cwd = join(tempDir, "project");
589
- extensionRoot = join(tempDir, "ext");
590
- });
591
-
592
- afterEach(() => {
593
- rmSync(tempDir, { recursive: true, force: true });
594
- });
595
-
596
- function writeGlobal(content: Record<string, unknown>): void {
597
- const dir = join(agentDir, "extensions", "pi-permission-system");
598
- mkdirSync(dir, { recursive: true });
599
- writeFileSync(join(dir, "config.json"), JSON.stringify(content));
600
- }
601
-
602
- function writeProject(content: Record<string, unknown>): void {
603
- const dir = join(cwd, ".pi", "extensions", "pi-permission-system");
604
- mkdirSync(dir, { recursive: true });
605
- writeFileSync(join(dir, "config.json"), JSON.stringify(content));
606
- }
607
-
608
- function writeLegacyGlobalPolicy(content: Record<string, unknown>): void {
609
- mkdirSync(agentDir, { recursive: true });
610
- writeFileSync(
611
- join(agentDir, "pi-permissions.jsonc"),
612
- JSON.stringify(content),
613
- );
614
- }
615
-
616
- function writeLegacyProjectPolicy(content: Record<string, unknown>): void {
617
- const dir = join(cwd, ".pi", "agent");
618
- mkdirSync(dir, { recursive: true });
619
- writeFileSync(join(dir, "pi-permissions.jsonc"), JSON.stringify(content));
620
- }
621
-
622
- function writeLegacyExtensionConfig(content: Record<string, unknown>): void {
623
- mkdirSync(extensionRoot, { recursive: true });
624
- writeFileSync(join(extensionRoot, "config.json"), JSON.stringify(content));
625
- }
626
-
627
- it("merges global and project new-layout configs", () => {
628
- writeGlobal({
629
- debugLog: true,
630
- permission: { "*": "ask", read: "allow" },
631
- });
632
- writeProject({
633
- permission: { "*": "allow", write: "deny" },
634
- });
635
-
636
- const result = loadAndMergeConfigs(agentDir, cwd, extensionRoot);
637
- // The merged config leaves a permissive top-level '*' with no bash '*' policy,
638
- // so the bash-fallback footgun warning is expected.
639
- expect(result.issues).toHaveLength(1);
640
- expect(result.issues[0]).toContain("bash");
641
- expect(result.merged.debugLog).toBe(true);
642
- expect(result.merged.permission).toEqual({
643
- "*": "allow",
644
- read: "allow",
645
- write: "deny",
646
- });
647
- });
648
-
649
- it("detects legacy global policy and emits migration issue", () => {
650
- writeLegacyGlobalPolicy({
651
- defaultPolicy: { tools: "allow" },
652
- tools: { read: "allow" },
653
- });
654
-
655
- const result = loadAndMergeConfigs(agentDir, cwd, extensionRoot);
656
- expect(result.issues).toHaveLength(1);
657
- expect(result.issues[0]).toContain("pi-permissions.jsonc");
658
- expect(result.issues[0]).toContain("extensions/pi-permission-system");
659
- // Legacy file has no flat-format permission key — no rules extracted
660
- expect(result.merged.permission).toBeUndefined();
661
- });
662
-
663
- it("detects legacy project policy and emits migration issue", () => {
664
- writeLegacyProjectPolicy({
665
- bash: { "git status": "allow" },
666
- });
667
-
668
- const result = loadAndMergeConfigs(agentDir, cwd, extensionRoot);
669
- expect(result.issues).toHaveLength(1);
670
- expect(result.issues[0]).toContain(".pi/agent/pi-permissions.jsonc");
671
- expect(result.issues[0]).toContain(".pi/extensions/pi-permission-system");
672
- // Legacy file has no flat-format permission key — no rules extracted
673
- expect(result.merged.permission).toBeUndefined();
674
- });
675
-
676
- it("detects legacy extension runtime config and emits migration issue", () => {
677
- writeLegacyExtensionConfig({
678
- debugLog: true,
679
- yoloMode: true,
680
- });
681
-
682
- const result = loadAndMergeConfigs(agentDir, cwd, extensionRoot);
683
- expect(result.issues).toHaveLength(1);
684
- expect(result.issues[0]).toContain(extensionRoot);
685
- expect(result.merged.debugLog).toBe(true);
686
- expect(result.merged.yoloMode).toBe(true);
687
- });
688
-
689
- it("does not emit legacy extension config issue when path equals new global path", () => {
690
- const newGlobalDir = join(agentDir, "extensions", "pi-permission-system");
691
- mkdirSync(newGlobalDir, { recursive: true });
692
- writeFileSync(
693
- join(newGlobalDir, "config.json"),
694
- JSON.stringify({ debugLog: true }),
695
- );
696
-
697
- const result = loadAndMergeConfigs(agentDir, cwd, newGlobalDir);
698
- expect(result.issues.filter((i) => i.includes("legacy"))).toHaveLength(0);
699
- });
700
-
701
- it("emits no issues when no legacy files exist and no new files exist", () => {
702
- const result = loadAndMergeConfigs(agentDir, cwd, extensionRoot);
703
- expect(result.issues).toEqual([]);
704
- });
705
-
706
- it("new-layout config takes precedence over legacy config at same scope", () => {
707
- writeGlobal({
708
- permission: { "*": "deny" },
709
- });
710
- writeLegacyGlobalPolicy({
711
- permission: { "*": "allow" },
712
- });
713
-
714
- const result = loadAndMergeConfigs(agentDir, cwd, extensionRoot);
715
- // New layout wins (legacy loaded first, new layout loaded second → new wins)
716
- expect(result.merged.permission).toEqual({ "*": "deny" });
717
- // But legacy still emits a migration warning
718
- expect(result.issues.some((i) => i.includes("pi-permissions.jsonc"))).toBe(
719
- true,
720
- );
721
- });
722
-
723
- it("warns when the merged config leaves bash inheriting a permissive top-level '*'", () => {
724
- writeGlobal({
725
- permission: { "*": "allow", read: "allow" },
726
- });
727
-
728
- const result = loadAndMergeConfigs(agentDir, cwd, extensionRoot);
729
- expect(result.issues.some((i) => i.includes("bash"))).toBe(true);
730
- });
731
-
732
- it("does not warn about bash fallback when bash is explicitly gated", () => {
733
- writeGlobal({
734
- permission: { "*": "allow", bash: { "*": "ask" } },
735
- });
736
-
737
- const result = loadAndMergeConfigs(agentDir, cwd, extensionRoot);
738
- expect(result.issues).toEqual([]);
739
- });
740
- });