@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,561 +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 { describe, expect, it } from "vitest";
5
- import { FilePolicyLoader } from "#src/policy-loader";
6
-
7
- // ---------------------------------------------------------------------------
8
- // Helpers
9
- // ---------------------------------------------------------------------------
10
-
11
- function makeTempDir(): string {
12
- return mkdtempSync(join(tmpdir(), "policy-loader-test-"));
13
- }
14
-
15
- function makeLoader(
16
- baseDir: string,
17
- options: {
18
- globalConfig?: Record<string, unknown>;
19
- mcpServerNames?: readonly string[];
20
- } = {},
21
- ) {
22
- const agentsDir = join(baseDir, "agents");
23
- mkdirSync(agentsDir, { recursive: true });
24
-
25
- const globalConfigPath = join(baseDir, "config.json");
26
- writeFileSync(
27
- globalConfigPath,
28
- JSON.stringify(options.globalConfig ?? {}, null, 2),
29
- );
30
-
31
- return new FilePolicyLoader({
32
- globalConfigPath,
33
- agentsDir,
34
- mcpServerNames: options.mcpServerNames
35
- ? [...options.mcpServerNames]
36
- : undefined,
37
- });
38
- }
39
-
40
- // ---------------------------------------------------------------------------
41
- // loadGlobalConfig
42
- // ---------------------------------------------------------------------------
43
-
44
- describe("FilePolicyLoader.loadGlobalConfig", () => {
45
- it("returns ScopeConfig with permission from a valid config file", () => {
46
- const baseDir = makeTempDir();
47
- try {
48
- const loader = makeLoader(baseDir, {
49
- globalConfig: { permission: { "*": "allow", read: "ask" } },
50
- });
51
- const config = loader.loadGlobalConfig();
52
- expect(config.permission).toEqual({ "*": "allow", read: "ask" });
53
- } finally {
54
- rmSync(baseDir, { recursive: true, force: true });
55
- }
56
- });
57
-
58
- it("returns empty ScopeConfig when config file is missing", () => {
59
- const loader = new FilePolicyLoader({
60
- globalConfigPath: "/nonexistent/config.json",
61
- agentsDir: "/nonexistent/agents",
62
- });
63
- const config = loader.loadGlobalConfig();
64
- expect(config.permission).toBeUndefined();
65
- });
66
-
67
- it("returns empty ScopeConfig when config file has no permission key", () => {
68
- const baseDir = makeTempDir();
69
- try {
70
- const loader = makeLoader(baseDir, {
71
- globalConfig: { debugLog: true },
72
- });
73
- const config = loader.loadGlobalConfig();
74
- expect(config.permission).toBeUndefined();
75
- } finally {
76
- rmSync(baseDir, { recursive: true, force: true });
77
- }
78
- });
79
- });
80
-
81
- // ---------------------------------------------------------------------------
82
- // loadProjectConfig
83
- // ---------------------------------------------------------------------------
84
-
85
- describe("FilePolicyLoader.loadProjectConfig", () => {
86
- it("returns empty ScopeConfig when no project path is configured", () => {
87
- const loader = new FilePolicyLoader({
88
- globalConfigPath: "/nonexistent/config.json",
89
- agentsDir: "/nonexistent/agents",
90
- });
91
- const config = loader.loadProjectConfig();
92
- expect(config).toEqual({});
93
- });
94
-
95
- it("returns ScopeConfig from a project config file", () => {
96
- const baseDir = makeTempDir();
97
- try {
98
- const projectConfigPath = join(baseDir, "project-config.json");
99
- writeFileSync(
100
- projectConfigPath,
101
- JSON.stringify({ permission: { bash: "allow" } }),
102
- );
103
- const loader = new FilePolicyLoader({
104
- globalConfigPath: "/nonexistent/config.json",
105
- agentsDir: "/nonexistent/agents",
106
- projectGlobalConfigPath: projectConfigPath,
107
- });
108
- const config = loader.loadProjectConfig();
109
- expect(config.permission).toEqual({ bash: "allow" });
110
- } finally {
111
- rmSync(baseDir, { recursive: true, force: true });
112
- }
113
- });
114
- });
115
-
116
- // ---------------------------------------------------------------------------
117
- // loadAgentConfig / loadProjectAgentConfig
118
- // ---------------------------------------------------------------------------
119
-
120
- describe("FilePolicyLoader.loadAgentConfig", () => {
121
- it("returns empty ScopeConfig when agentName is undefined", () => {
122
- const baseDir = makeTempDir();
123
- try {
124
- const loader = makeLoader(baseDir);
125
- expect(loader.loadAgentConfig()).toEqual({});
126
- } finally {
127
- rmSync(baseDir, { recursive: true, force: true });
128
- }
129
- });
130
-
131
- it("returns ScopeConfig from agent frontmatter", () => {
132
- const baseDir = makeTempDir();
133
- try {
134
- const agentsDir = join(baseDir, "agents");
135
- mkdirSync(agentsDir, { recursive: true });
136
- writeFileSync(
137
- join(agentsDir, "coder.md"),
138
- `---\npermission:\n bash: allow\n---\n# Coder agent\n`,
139
- );
140
- const loader = new FilePolicyLoader({
141
- globalConfigPath: join(baseDir, "config.json"),
142
- agentsDir,
143
- });
144
- writeFileSync(join(baseDir, "config.json"), "{}");
145
- const config = loader.loadAgentConfig("coder");
146
- expect(config.permission).toEqual({ bash: "allow" });
147
- } finally {
148
- rmSync(baseDir, { recursive: true, force: true });
149
- }
150
- });
151
-
152
- it("returns empty ScopeConfig when agent file does not exist", () => {
153
- const baseDir = makeTempDir();
154
- try {
155
- const loader = makeLoader(baseDir);
156
- expect(loader.loadAgentConfig("nonexistent")).toEqual({});
157
- } finally {
158
- rmSync(baseDir, { recursive: true, force: true });
159
- }
160
- });
161
- });
162
-
163
- describe("FilePolicyLoader.loadProjectAgentConfig", () => {
164
- it("returns empty ScopeConfig when no projectAgentsDir is configured", () => {
165
- const baseDir = makeTempDir();
166
- try {
167
- const loader = makeLoader(baseDir);
168
- expect(loader.loadProjectAgentConfig("coder")).toEqual({});
169
- } finally {
170
- rmSync(baseDir, { recursive: true, force: true });
171
- }
172
- });
173
- });
174
-
175
- // ---------------------------------------------------------------------------
176
- // getConfigIssues
177
- // ---------------------------------------------------------------------------
178
-
179
- describe("FilePolicyLoader.getConfigIssues", () => {
180
- it("returns empty array before any loads", () => {
181
- const loader = new FilePolicyLoader({
182
- globalConfigPath: "/nonexistent/config.json",
183
- agentsDir: "/nonexistent/agents",
184
- });
185
- expect(loader.getConfigIssues()).toEqual([]);
186
- });
187
-
188
- it("returns empty array for valid config", () => {
189
- const baseDir = makeTempDir();
190
- try {
191
- const loader = makeLoader(baseDir, {
192
- globalConfig: { permission: { "*": "ask" } },
193
- });
194
- loader.loadGlobalConfig();
195
- expect(loader.getConfigIssues()).toEqual([]);
196
- } finally {
197
- rmSync(baseDir, { recursive: true, force: true });
198
- }
199
- });
200
- });
201
-
202
- // ---------------------------------------------------------------------------
203
- // getResolvedPolicyPaths
204
- // ---------------------------------------------------------------------------
205
-
206
- describe("FilePolicyLoader.getResolvedPolicyPaths", () => {
207
- it("returns correct paths and existence when files exist", () => {
208
- const baseDir = makeTempDir();
209
- try {
210
- const globalConfigPath = join(baseDir, "config.json");
211
- const agentsDir = join(baseDir, "agents");
212
- writeFileSync(globalConfigPath, "{}");
213
- mkdirSync(agentsDir, { recursive: true });
214
-
215
- const loader = new FilePolicyLoader({ globalConfigPath, agentsDir });
216
- const paths = loader.getResolvedPolicyPaths();
217
-
218
- expect(paths.globalConfigPath).toBe(globalConfigPath);
219
- expect(paths.globalConfigExists).toBe(true);
220
- expect(paths.agentsDir).toBe(agentsDir);
221
- expect(paths.agentsDirExists).toBe(true);
222
- expect(paths.projectConfigPath).toBeNull();
223
- expect(paths.projectConfigExists).toBe(false);
224
- expect(paths.projectAgentsDir).toBeNull();
225
- expect(paths.projectAgentsDirExists).toBe(false);
226
- } finally {
227
- rmSync(baseDir, { recursive: true, force: true });
228
- }
229
- });
230
- });
231
-
232
- // ---------------------------------------------------------------------------
233
- // getCacheStamp
234
- // ---------------------------------------------------------------------------
235
-
236
- describe("FilePolicyLoader.getCacheStamp", () => {
237
- it("returns a string stamp", () => {
238
- const loader = new FilePolicyLoader({
239
- globalConfigPath: "/nonexistent/config.json",
240
- agentsDir: "/nonexistent/agents",
241
- });
242
- const stamp = loader.getCacheStamp();
243
- expect(typeof stamp).toBe("string");
244
- expect(stamp.length).toBeGreaterThan(0);
245
- });
246
-
247
- it("changes when the global config file changes", () => {
248
- const baseDir = makeTempDir();
249
- try {
250
- const globalConfigPath = join(baseDir, "config.json");
251
- writeFileSync(globalConfigPath, "{}");
252
- const agentsDir = join(baseDir, "agents");
253
- mkdirSync(agentsDir, { recursive: true });
254
-
255
- const loader = new FilePolicyLoader({ globalConfigPath, agentsDir });
256
- const stamp1 = loader.getCacheStamp();
257
-
258
- // Wait a tick so mtime changes
259
- const now = Date.now();
260
- while (Date.now() - now < 50) {
261
- // busy-wait for mtime resolution
262
- }
263
- writeFileSync(globalConfigPath, '{"permission": {}}');
264
- const stamp2 = loader.getCacheStamp();
265
-
266
- expect(stamp1).not.toBe(stamp2);
267
- } finally {
268
- rmSync(baseDir, { recursive: true, force: true });
269
- }
270
- });
271
-
272
- it("includes agent stamp when agentName is provided and agent file exists", () => {
273
- const baseDir = makeTempDir();
274
- try {
275
- const agentsDir = join(baseDir, "agents");
276
- mkdirSync(agentsDir, { recursive: true });
277
- writeFileSync(join(baseDir, "config.json"), "{}");
278
- writeFileSync(
279
- join(agentsDir, "coder.md"),
280
- "---\npermission:\n read: allow\n---\n",
281
- );
282
-
283
- const loader = new FilePolicyLoader({
284
- globalConfigPath: join(baseDir, "config.json"),
285
- agentsDir,
286
- });
287
-
288
- const stampWithout = loader.getCacheStamp();
289
- const stampWith = loader.getCacheStamp("coder");
290
- // Agent file exists, so the stamp differs from the no-agent case.
291
- expect(stampWithout).not.toBe(stampWith);
292
- } finally {
293
- rmSync(baseDir, { recursive: true, force: true });
294
- }
295
- });
296
- });
297
-
298
- // ---------------------------------------------------------------------------
299
- // Mtime cache invalidation
300
- // ---------------------------------------------------------------------------
301
-
302
- describe("FilePolicyLoader mtime caching", () => {
303
- it("returns cached value on second call with unchanged file", () => {
304
- const baseDir = makeTempDir();
305
- try {
306
- const loader = makeLoader(baseDir, {
307
- globalConfig: { permission: { "*": "allow" } },
308
- });
309
- const first = loader.loadGlobalConfig();
310
- const second = loader.loadGlobalConfig();
311
- // Same reference — cache hit
312
- expect(second).toBe(first);
313
- } finally {
314
- rmSync(baseDir, { recursive: true, force: true });
315
- }
316
- });
317
-
318
- it("reloads when file mtime changes", () => {
319
- const baseDir = makeTempDir();
320
- try {
321
- const globalConfigPath = join(baseDir, "config.json");
322
- const agentsDir = join(baseDir, "agents");
323
- mkdirSync(agentsDir, { recursive: true });
324
- writeFileSync(
325
- globalConfigPath,
326
- JSON.stringify({ permission: { "*": "allow" } }),
327
- );
328
-
329
- const loader = new FilePolicyLoader({ globalConfigPath, agentsDir });
330
- const first = loader.loadGlobalConfig();
331
- expect(first.permission?.["*"]).toBe("allow");
332
-
333
- // busy-wait for mtime resolution
334
- const now = Date.now();
335
- while (Date.now() - now < 50) {
336
- /* spin */
337
- }
338
-
339
- writeFileSync(
340
- globalConfigPath,
341
- JSON.stringify({ permission: { "*": "deny" } }),
342
- );
343
- const second = loader.loadGlobalConfig();
344
- expect(second.permission?.["*"]).toBe("deny");
345
- expect(second).not.toBe(first);
346
- } finally {
347
- rmSync(baseDir, { recursive: true, force: true });
348
- }
349
- });
350
- });
351
-
352
- // ---------------------------------------------------------------------------
353
- // Agent frontmatter loading
354
- // ---------------------------------------------------------------------------
355
-
356
- describe("FilePolicyLoader agent frontmatter", () => {
357
- it("loads permission from agent frontmatter with pattern map", () => {
358
- const baseDir = makeTempDir();
359
- try {
360
- const agentsDir = join(baseDir, "agents");
361
- mkdirSync(agentsDir, { recursive: true });
362
- writeFileSync(join(baseDir, "config.json"), "{}");
363
- writeFileSync(
364
- join(agentsDir, "coder.md"),
365
- [
366
- "---",
367
- "permission:",
368
- " bash:",
369
- ' "git *": allow',
370
- ' "rm *": deny',
371
- "---",
372
- "# Coder",
373
- ].join("\n"),
374
- );
375
-
376
- const loader = new FilePolicyLoader({
377
- globalConfigPath: join(baseDir, "config.json"),
378
- agentsDir,
379
- });
380
- const config = loader.loadAgentConfig("coder");
381
- expect(config.permission).toBeDefined();
382
- expect(config.permission?.bash).toEqual({
383
- "git *": "allow",
384
- "rm *": "deny",
385
- });
386
- } finally {
387
- rmSync(baseDir, { recursive: true, force: true });
388
- }
389
- });
390
-
391
- it("returns empty config for agent file without frontmatter", () => {
392
- const baseDir = makeTempDir();
393
- try {
394
- const agentsDir = join(baseDir, "agents");
395
- mkdirSync(agentsDir, { recursive: true });
396
- writeFileSync(join(baseDir, "config.json"), "{}");
397
- writeFileSync(join(agentsDir, "plain.md"), "# No frontmatter\n");
398
-
399
- const loader = new FilePolicyLoader({
400
- globalConfigPath: join(baseDir, "config.json"),
401
- agentsDir,
402
- });
403
- expect(loader.loadAgentConfig("plain")).toEqual({});
404
- } finally {
405
- rmSync(baseDir, { recursive: true, force: true });
406
- }
407
- });
408
-
409
- it("project agent config loads from projectAgentsDir", () => {
410
- const baseDir = makeTempDir();
411
- try {
412
- const agentsDir = join(baseDir, "agents");
413
- const projectAgentsDir = join(baseDir, "project-agents");
414
- mkdirSync(agentsDir, { recursive: true });
415
- mkdirSync(projectAgentsDir, { recursive: true });
416
- writeFileSync(join(baseDir, "config.json"), "{}");
417
- writeFileSync(
418
- join(projectAgentsDir, "coder.md"),
419
- "---\npermission:\n write: allow\n---\n# Coder\n",
420
- );
421
-
422
- const loader = new FilePolicyLoader({
423
- globalConfigPath: join(baseDir, "config.json"),
424
- agentsDir,
425
- projectAgentsDir,
426
- });
427
- const config = loader.loadProjectAgentConfig("coder");
428
- expect(config.permission).toEqual({ write: "allow" });
429
- } finally {
430
- rmSync(baseDir, { recursive: true, force: true });
431
- }
432
- });
433
- });
434
-
435
- // ---------------------------------------------------------------------------
436
- // MCP server name reading
437
- // ---------------------------------------------------------------------------
438
-
439
- describe("FilePolicyLoader.getConfiguredMcpServerNames", () => {
440
- it("returns override names when provided", () => {
441
- const loader = new FilePolicyLoader({
442
- globalConfigPath: "/nonexistent/config.json",
443
- agentsDir: "/nonexistent/agents",
444
- mcpServerNames: ["exa", "research"],
445
- });
446
- expect(loader.getConfiguredMcpServerNames()).toEqual(
447
- expect.arrayContaining(["exa", "research"]),
448
- );
449
- });
450
-
451
- it("deduplicates and trims override names", () => {
452
- const loader = new FilePolicyLoader({
453
- globalConfigPath: "/nonexistent/config.json",
454
- agentsDir: "/nonexistent/agents",
455
- mcpServerNames: [" exa ", "exa", "research"],
456
- });
457
- const names = loader.getConfiguredMcpServerNames();
458
- expect(names.filter((n) => n === "exa")).toHaveLength(1);
459
- });
460
-
461
- it("reads server names from mcp.json on disk", () => {
462
- const baseDir = makeTempDir();
463
- try {
464
- const mcpConfigPath = join(baseDir, "mcp.json");
465
- writeFileSync(
466
- mcpConfigPath,
467
- JSON.stringify({ mcpServers: { exa: {}, research: {} } }),
468
- );
469
-
470
- const loader = new FilePolicyLoader({
471
- globalConfigPath: "/nonexistent/config.json",
472
- agentsDir: "/nonexistent/agents",
473
- globalMcpConfigPath: mcpConfigPath,
474
- });
475
- const names = loader.getConfiguredMcpServerNames();
476
- expect(names).toEqual(expect.arrayContaining(["exa", "research"]));
477
- } finally {
478
- rmSync(baseDir, { recursive: true, force: true });
479
- }
480
- });
481
-
482
- it("returns empty array when mcp.json is missing", () => {
483
- const loader = new FilePolicyLoader({
484
- globalConfigPath: "/nonexistent/config.json",
485
- agentsDir: "/nonexistent/agents",
486
- globalMcpConfigPath: "/nonexistent/mcp.json",
487
- });
488
- expect(loader.getConfiguredMcpServerNames()).toEqual([]);
489
- });
490
-
491
- it("caches MCP server names across calls", () => {
492
- const baseDir = makeTempDir();
493
- try {
494
- const mcpConfigPath = join(baseDir, "mcp.json");
495
- writeFileSync(mcpConfigPath, JSON.stringify({ mcpServers: { exa: {} } }));
496
-
497
- const loader = new FilePolicyLoader({
498
- globalConfigPath: "/nonexistent/config.json",
499
- agentsDir: "/nonexistent/agents",
500
- globalMcpConfigPath: mcpConfigPath,
501
- });
502
- const first = loader.getConfiguredMcpServerNames();
503
- const second = loader.getConfiguredMcpServerNames();
504
- // Same reference — cache hit
505
- expect(second).toBe(first);
506
- } finally {
507
- rmSync(baseDir, { recursive: true, force: true });
508
- }
509
- });
510
- });
511
-
512
- // ---------------------------------------------------------------------------
513
- // Config issue accumulation
514
- // ---------------------------------------------------------------------------
515
-
516
- describe("FilePolicyLoader config issue accumulation", () => {
517
- it("accumulates issues from malformed config files", () => {
518
- const baseDir = makeTempDir();
519
- try {
520
- // Write invalid JSON to trigger a parse error issue
521
- const globalConfigPath = join(baseDir, "config.json");
522
- const agentsDir = join(baseDir, "agents");
523
- mkdirSync(agentsDir, { recursive: true });
524
- writeFileSync(globalConfigPath, "{ INVALID JSON");
525
-
526
- const loader = new FilePolicyLoader({ globalConfigPath, agentsDir });
527
- loader.loadGlobalConfig();
528
- const issues = loader.getConfigIssues();
529
- expect(issues.length).toBeGreaterThanOrEqual(1);
530
- expect(issues[0]).toContain("Failed to read config");
531
- } finally {
532
- rmSync(baseDir, { recursive: true, force: true });
533
- }
534
- });
535
-
536
- it("does not duplicate issues on repeated loads", () => {
537
- const baseDir = makeTempDir();
538
- try {
539
- const globalConfigPath = join(baseDir, "config.json");
540
- const agentsDir = join(baseDir, "agents");
541
- mkdirSync(agentsDir, { recursive: true });
542
- writeFileSync(globalConfigPath, "{ INVALID JSON");
543
-
544
- const loader = new FilePolicyLoader({ globalConfigPath, agentsDir });
545
- loader.loadGlobalConfig();
546
- const issuesBefore = loader.getConfigIssues();
547
-
548
- // Bust cache by waiting for mtime change
549
- const now = Date.now();
550
- while (Date.now() - now < 50) {
551
- /* spin */
552
- }
553
- writeFileSync(globalConfigPath, "{ INVALID JSON");
554
- loader.loadGlobalConfig();
555
- const issuesAfter = loader.getConfigIssues();
556
- expect(issuesAfter.length).toBe(issuesBefore.length);
557
- } finally {
558
- rmSync(baseDir, { recursive: true, force: true });
559
- }
560
- });
561
- });