@komarspn/pi-permission-system 16.0.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 (203) hide show
  1. package/CHANGELOG.md +2234 -0
  2. package/LICENSE +21 -0
  3. package/README.md +158 -0
  4. package/config/config.example.json +39 -0
  5. package/package.json +82 -0
  6. package/schemas/permissions.schema.json +158 -0
  7. package/src/active-agent.ts +72 -0
  8. package/src/async-cache.ts +21 -0
  9. package/src/bash-arity.ts +210 -0
  10. package/src/builtin-tool-input-formatters.ts +82 -0
  11. package/src/canonicalize-path.ts +30 -0
  12. package/src/common.ts +121 -0
  13. package/src/config-loader.ts +432 -0
  14. package/src/config-modal.ts +259 -0
  15. package/src/config-paths.ts +47 -0
  16. package/src/config-reporter.ts +34 -0
  17. package/src/config-store.ts +222 -0
  18. package/src/decision-audit.ts +75 -0
  19. package/src/decision-reporter.ts +41 -0
  20. package/src/denial-messages.ts +232 -0
  21. package/src/expand-home.ts +28 -0
  22. package/src/extension-config.ts +79 -0
  23. package/src/extension-paths.ts +66 -0
  24. package/src/forwarded-permissions/io.ts +404 -0
  25. package/src/forwarded-permissions/permission-forwarder.ts +580 -0
  26. package/src/forwarding-manager.ts +74 -0
  27. package/src/gate-prompter.ts +12 -0
  28. package/src/handlers/before-agent-start.ts +94 -0
  29. package/src/handlers/gates/bash-command.ts +75 -0
  30. package/src/handlers/gates/bash-external-directory.ts +127 -0
  31. package/src/handlers/gates/bash-path-extractor.ts +15 -0
  32. package/src/handlers/gates/bash-path.ts +152 -0
  33. package/src/handlers/gates/bash-program.ts +1143 -0
  34. package/src/handlers/gates/bash-token-classification.ts +105 -0
  35. package/src/handlers/gates/candidate-check.ts +32 -0
  36. package/src/handlers/gates/descriptor.ts +81 -0
  37. package/src/handlers/gates/external-directory-messages.ts +20 -0
  38. package/src/handlers/gates/external-directory.ts +133 -0
  39. package/src/handlers/gates/helpers.ts +76 -0
  40. package/src/handlers/gates/path.ts +91 -0
  41. package/src/handlers/gates/runner.ts +186 -0
  42. package/src/handlers/gates/skill-input-gate-pipeline.ts +104 -0
  43. package/src/handlers/gates/skill-input.ts +46 -0
  44. package/src/handlers/gates/skill-read.ts +87 -0
  45. package/src/handlers/gates/tool-call-gate-pipeline.ts +129 -0
  46. package/src/handlers/gates/tool.ts +102 -0
  47. package/src/handlers/gates/types.ts +13 -0
  48. package/src/handlers/index.ts +3 -0
  49. package/src/handlers/lifecycle.ts +95 -0
  50. package/src/handlers/permission-gate-handler.ts +190 -0
  51. package/src/handlers/tool-call-boundary.ts +91 -0
  52. package/src/index.ts +225 -0
  53. package/src/input-normalizer.ts +157 -0
  54. package/src/logging.ts +113 -0
  55. package/src/mcp-targets.ts +170 -0
  56. package/src/node-modules-discovery.ts +76 -0
  57. package/src/normalize.ts +43 -0
  58. package/src/path-utils.ts +355 -0
  59. package/src/pattern-suggest.ts +132 -0
  60. package/src/permission-dialog.ts +138 -0
  61. package/src/permission-event-rpc.ts +223 -0
  62. package/src/permission-events.ts +266 -0
  63. package/src/permission-forwarding.ts +188 -0
  64. package/src/permission-gate.ts +94 -0
  65. package/src/permission-manager.ts +392 -0
  66. package/src/permission-merge.ts +32 -0
  67. package/src/permission-prompter.ts +142 -0
  68. package/src/permission-prompts.ts +93 -0
  69. package/src/permission-resolver.ts +109 -0
  70. package/src/permission-session.ts +189 -0
  71. package/src/permission-ui-prompt.ts +127 -0
  72. package/src/permissions-service.ts +63 -0
  73. package/src/persistent-approval-recorder.ts +139 -0
  74. package/src/policy-loader.ts +350 -0
  75. package/src/prompting-gateway.ts +104 -0
  76. package/src/rule.ts +188 -0
  77. package/src/scope-merge.ts +72 -0
  78. package/src/service-lifecycle.ts +49 -0
  79. package/src/service.ts +163 -0
  80. package/src/session-approval-recorder.ts +6 -0
  81. package/src/session-approval.ts +43 -0
  82. package/src/session-logger.ts +91 -0
  83. package/src/session-rules.ts +79 -0
  84. package/src/skill-prompt-sanitizer.ts +292 -0
  85. package/src/status.ts +35 -0
  86. package/src/subagent-context.ts +104 -0
  87. package/src/subagent-lifecycle-events.ts +72 -0
  88. package/src/subagent-registry.ts +105 -0
  89. package/src/synthesize.ts +92 -0
  90. package/src/system-prompt-sanitizer.ts +274 -0
  91. package/src/tool-access-extractor-registry.ts +68 -0
  92. package/src/tool-input-formatter-registry.ts +67 -0
  93. package/src/tool-input-preview.ts +34 -0
  94. package/src/tool-input-prompt-formatters.ts +63 -0
  95. package/src/tool-preview-formatter.ts +207 -0
  96. package/src/tool-registry.ts +148 -0
  97. package/src/types.ts +64 -0
  98. package/src/wildcard-matcher.ts +120 -0
  99. package/src/yolo-mode.ts +30 -0
  100. package/test/active-agent.test.ts +155 -0
  101. package/test/async-cache.test.ts +48 -0
  102. package/test/bash-arity.test.ts +144 -0
  103. package/test/bash-external-directory.test.ts +956 -0
  104. package/test/builtin-tool-input-formatters.test.ts +109 -0
  105. package/test/canonicalize-path.test.ts +93 -0
  106. package/test/common.test.ts +287 -0
  107. package/test/composition-root.test.ts +603 -0
  108. package/test/config-loader.test.ts +740 -0
  109. package/test/config-modal.test.ts +320 -0
  110. package/test/config-paths.test.ts +83 -0
  111. package/test/config-pipeline.test.ts +90 -0
  112. package/test/config-reporter.test.ts +147 -0
  113. package/test/config-store.test.ts +466 -0
  114. package/test/decision-audit.test.ts +72 -0
  115. package/test/decision-reporter.test.ts +112 -0
  116. package/test/denial-messages.test.ts +656 -0
  117. package/test/detect-permissive-bash-fallback.test.ts +56 -0
  118. package/test/expand-home.test.ts +93 -0
  119. package/test/extension-config.test.ts +129 -0
  120. package/test/extension-paths.test.ts +108 -0
  121. package/test/forwarded-permissions/io.test.ts +251 -0
  122. package/test/forwarding-manager.test.ts +194 -0
  123. package/test/handlers/before-agent-start.test.ts +317 -0
  124. package/test/handlers/external-directory-integration.test.ts +623 -0
  125. package/test/handlers/external-directory-session-dedup.test.ts +430 -0
  126. package/test/handlers/external-directory-symlink-acceptance.test.ts +149 -0
  127. package/test/handlers/gates/bash-command-metamorphic.test.ts +83 -0
  128. package/test/handlers/gates/bash-command.test.ts +191 -0
  129. package/test/handlers/gates/bash-external-directory.test.ts +269 -0
  130. package/test/handlers/gates/bash-path.test.ts +337 -0
  131. package/test/handlers/gates/bash-program.test.ts +410 -0
  132. package/test/handlers/gates/bash-token-classification.test.ts +241 -0
  133. package/test/handlers/gates/candidate-check.test.ts +52 -0
  134. package/test/handlers/gates/external-directory-messages.test.ts +61 -0
  135. package/test/handlers/gates/external-directory.test.ts +259 -0
  136. package/test/handlers/gates/helpers.test.ts +177 -0
  137. package/test/handlers/gates/path.test.ts +294 -0
  138. package/test/handlers/gates/runner.test.ts +447 -0
  139. package/test/handlers/gates/skill-input-gate-pipeline.test.ts +176 -0
  140. package/test/handlers/gates/skill-input.test.ts +131 -0
  141. package/test/handlers/gates/skill-read.test.ts +158 -0
  142. package/test/handlers/gates/tool-call-gate-pipeline.test.ts +252 -0
  143. package/test/handlers/gates/tool.test.ts +223 -0
  144. package/test/handlers/input-events.test.ts +168 -0
  145. package/test/handlers/input.test.ts +199 -0
  146. package/test/handlers/lifecycle.test.ts +221 -0
  147. package/test/handlers/tool-call-boundary.test.ts +145 -0
  148. package/test/handlers/tool-call-events.test.ts +277 -0
  149. package/test/handlers/tool-call.test.ts +395 -0
  150. package/test/handlers/validate-requested-tool.test.ts +92 -0
  151. package/test/helpers/gate-fixtures.ts +323 -0
  152. package/test/helpers/handler-fixtures.ts +335 -0
  153. package/test/helpers/make-fake-pi.ts +100 -0
  154. package/test/helpers/manager-harness.ts +112 -0
  155. package/test/helpers/session-fixtures.ts +204 -0
  156. package/test/input-normalizer.test.ts +367 -0
  157. package/test/logging.test.ts +51 -0
  158. package/test/mcp-targets.test.ts +233 -0
  159. package/test/node-modules-discovery.test.ts +97 -0
  160. package/test/normalize.test.ts +247 -0
  161. package/test/path-utils.test.ts +650 -0
  162. package/test/pattern-suggest.test.ts +248 -0
  163. package/test/permission-dialog.test.ts +241 -0
  164. package/test/permission-event-rpc.test.ts +541 -0
  165. package/test/permission-events.test.ts +402 -0
  166. package/test/permission-forwarder.test.ts +369 -0
  167. package/test/permission-forwarding.test.ts +315 -0
  168. package/test/permission-gate.test.ts +305 -0
  169. package/test/permission-manager-unified.test.ts +3368 -0
  170. package/test/permission-merge.test.ts +61 -0
  171. package/test/permission-prompter.test.ts +518 -0
  172. package/test/permission-prompts.test.ts +363 -0
  173. package/test/permission-resolver.test.ts +265 -0
  174. package/test/permission-session.test.ts +363 -0
  175. package/test/permission-ui-prompt.test.ts +146 -0
  176. package/test/permissions-service.test.ts +177 -0
  177. package/test/persistent-approval-recorder.test.ts +133 -0
  178. package/test/pi-infrastructure-read.test.ts +369 -0
  179. package/test/policy-loader.test.ts +561 -0
  180. package/test/prompting-gateway.test.ts +230 -0
  181. package/test/rule.test.ts +604 -0
  182. package/test/scope-merge.test.ts +116 -0
  183. package/test/service-lifecycle.test.ts +163 -0
  184. package/test/service.test.ts +308 -0
  185. package/test/session-approval.test.ts +75 -0
  186. package/test/session-logger.test.ts +200 -0
  187. package/test/session-rules.test.ts +304 -0
  188. package/test/session-start.test.ts +112 -0
  189. package/test/skill-prompt-sanitizer.test.ts +374 -0
  190. package/test/status.test.ts +10 -0
  191. package/test/subagent-context.test.ts +326 -0
  192. package/test/subagent-lifecycle-events.test.ts +132 -0
  193. package/test/subagent-registry.test.ts +145 -0
  194. package/test/synthesize.test.ts +300 -0
  195. package/test/system-prompt-sanitizer.test.ts +382 -0
  196. package/test/tool-access-extractor-registry.test.ts +77 -0
  197. package/test/tool-input-formatter-registry.test.ts +75 -0
  198. package/test/tool-input-preview.test.ts +129 -0
  199. package/test/tool-input-prompt-formatters.test.ts +115 -0
  200. package/test/tool-preview-formatter.test.ts +458 -0
  201. package/test/tool-registry.test.ts +197 -0
  202. package/test/wildcard-matcher.test.ts +424 -0
  203. package/test/yolo-mode.test.ts +188 -0
@@ -0,0 +1,320 @@
1
+ import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
2
+ import { tmpdir } from "node:os";
3
+ import { join } from "node:path";
4
+ import { expect, test, vi } from "vitest";
5
+ import { loadUnifiedConfig } from "#src/config-loader";
6
+ import { registerPermissionSystemCommand } from "#src/config-modal";
7
+ import type { CommandConfigStore } from "#src/config-store";
8
+ import {
9
+ DEFAULT_EXTENSION_CONFIG,
10
+ normalizePermissionSystemConfig,
11
+ type PermissionSystemExtensionConfig,
12
+ } from "#src/extension-config";
13
+ import type { Rule, Ruleset } from "#src/rule";
14
+
15
+ vi.mock("@earendil-works/pi-coding-agent", () => ({
16
+ getSettingsListTheme: () => ({}),
17
+ }));
18
+
19
+ vi.mock("@earendil-works/pi-tui", () => ({
20
+ SettingsList: class {
21
+ handleInput(): void {}
22
+ updateValue(): void {}
23
+ render(): string[] {
24
+ return [];
25
+ }
26
+ invalidate(): void {}
27
+ },
28
+ }));
29
+
30
+ type Notification = { message: string; level: "info" | "warning" | "error" };
31
+
32
+ type CommandContextStub = {
33
+ hasUI: boolean;
34
+ ui: {
35
+ notify(message: string, level: "info" | "warning" | "error"): void;
36
+ custom<T>(
37
+ renderer: (...args: unknown[]) => unknown,
38
+ options?: unknown,
39
+ ): Promise<T>;
40
+ };
41
+ };
42
+
43
+ function createCommandContext(hasUI: boolean): {
44
+ ctx: CommandContextStub;
45
+ notifications: Notification[];
46
+ getCustomCalls(): number;
47
+ } {
48
+ const notifications: Notification[] = [];
49
+ let customCalls = 0;
50
+
51
+ return {
52
+ ctx: {
53
+ hasUI,
54
+ ui: {
55
+ notify(message: string, level: "info" | "warning" | "error") {
56
+ notifications.push({ message, level });
57
+ },
58
+ async custom<T>(
59
+ _renderer: (...args: unknown[]) => unknown,
60
+ _options?: unknown,
61
+ ): Promise<T> {
62
+ customCalls += 1;
63
+ return undefined as T;
64
+ },
65
+ },
66
+ },
67
+ notifications,
68
+ getCustomCalls: () => customCalls,
69
+ };
70
+ }
71
+
72
+ function lastNotification(notifications: Notification[]): Notification {
73
+ return notifications[notifications.length - 1];
74
+ }
75
+
76
+ test("permission-system command completions expose top-level config actions", () => {
77
+ const baseDir = mkdtempSync(
78
+ join(tmpdir(), "pi-permission-system-command-completions-"),
79
+ );
80
+ const configPath = join(baseDir, "config.json");
81
+ let config: PermissionSystemExtensionConfig = { ...DEFAULT_EXTENSION_CONFIG };
82
+
83
+ try {
84
+ const configStore: CommandConfigStore = {
85
+ current: () => config,
86
+ save: (next) => {
87
+ config = next;
88
+ },
89
+ };
90
+ const controller = {
91
+ config: configStore,
92
+ configPath,
93
+ getActiveAgentConfigRules: () => [] as Ruleset,
94
+ };
95
+
96
+ let definition: {
97
+ description: string;
98
+ getArgumentCompletions?: (
99
+ argumentPrefix: string,
100
+ ) => Array<{ value: string; label: string; description?: string }> | null;
101
+ handler: (args: string, ctx: CommandContextStub) => Promise<void>;
102
+ } | null = null;
103
+
104
+ registerPermissionSystemCommand(
105
+ {
106
+ registerCommand(_name: string, nextDefinition: typeof definition) {
107
+ definition = nextDefinition;
108
+ },
109
+ } as never,
110
+ controller,
111
+ );
112
+
113
+ expect(definition!.getArgumentCompletions).toBeTypeOf("function");
114
+
115
+ const topLevel = definition!.getArgumentCompletions?.("");
116
+ expect(Array.isArray(topLevel)).toBeTruthy();
117
+ expect(topLevel?.some((item) => item.value === "show")).toBeTruthy();
118
+ expect(topLevel?.some((item) => item.value === "reset")).toBeTruthy();
119
+
120
+ const filtered = definition!.getArgumentCompletions?.("pa");
121
+ expect(filtered?.map((item) => item.value)).toEqual(["path"]);
122
+ expect(definition!.getArgumentCompletions?.("path extra")).toBe(null);
123
+ expect(definition!.getArgumentCompletions?.("zzz")).toBe(null);
124
+ } finally {
125
+ rmSync(baseDir, { recursive: true, force: true });
126
+ }
127
+ });
128
+
129
+ test("permission-system command handlers manage config summary, persistence, and modal routing", async () => {
130
+ const baseDir = mkdtempSync(join(tmpdir(), "pi-permission-system-command-"));
131
+ const configPath = join(baseDir, "config.json");
132
+ let config: PermissionSystemExtensionConfig = {
133
+ debugLog: true,
134
+ permissionReviewLog: false,
135
+ yoloMode: true,
136
+ };
137
+
138
+ try {
139
+ writeFileSync(
140
+ configPath,
141
+ `${JSON.stringify(normalizePermissionSystemConfig(config), null, 2)}\n`,
142
+ "utf-8",
143
+ );
144
+
145
+ const configStore: CommandConfigStore = {
146
+ current: () => config,
147
+ save: (next) => {
148
+ const currentConfig = normalizePermissionSystemConfig(
149
+ loadUnifiedConfig(configPath).config,
150
+ );
151
+ const normalized = normalizePermissionSystemConfig(next);
152
+ writeFileSync(
153
+ configPath,
154
+ `${JSON.stringify(normalized, null, 2)}\n`,
155
+ "utf-8",
156
+ );
157
+ config = normalizePermissionSystemConfig(
158
+ loadUnifiedConfig(configPath).config,
159
+ );
160
+ expect(config).not.toEqual(currentConfig);
161
+ },
162
+ };
163
+ const controller = {
164
+ config: configStore,
165
+ configPath,
166
+ getActiveAgentConfigRules: () => [] as Ruleset,
167
+ };
168
+
169
+ let registeredName = "";
170
+ let definition: {
171
+ description: string;
172
+ getArgumentCompletions?: (
173
+ argumentPrefix: string,
174
+ ) => Array<{ value: string; label: string; description?: string }> | null;
175
+ handler: (args: string, ctx: CommandContextStub) => Promise<void>;
176
+ } | null = null;
177
+
178
+ registerPermissionSystemCommand(
179
+ {
180
+ registerCommand(name: string, nextDefinition: typeof definition) {
181
+ registeredName = name;
182
+ definition = nextDefinition;
183
+ },
184
+ } as never,
185
+ controller,
186
+ );
187
+
188
+ expect(registeredName).toBe("permission-system");
189
+ expect(definition!.description).toContain("Configure pi-permission-system");
190
+
191
+ const infoCtx = createCommandContext(true);
192
+ await definition!.handler("show", infoCtx.ctx);
193
+ expect(lastNotification(infoCtx.notifications).message).toContain(
194
+ "yoloMode=on",
195
+ );
196
+ expect(lastNotification(infoCtx.notifications).message).toContain(
197
+ "debugLog=on",
198
+ );
199
+
200
+ await definition!.handler("path", infoCtx.ctx);
201
+ expect(lastNotification(infoCtx.notifications).message).toBe(
202
+ `permission-system config: ${configPath}`,
203
+ );
204
+
205
+ await definition!.handler("help", infoCtx.ctx);
206
+ expect(lastNotification(infoCtx.notifications).message).toContain(
207
+ "Usage: /permission-system",
208
+ );
209
+
210
+ await definition!.handler("reset", infoCtx.ctx);
211
+ expect(config).toEqual(DEFAULT_EXTENSION_CONFIG);
212
+ expect(lastNotification(infoCtx.notifications).message).toBe(
213
+ "Permission system settings reset to defaults.",
214
+ );
215
+
216
+ const persisted = JSON.parse(readFileSync(configPath, "utf8")) as Record<
217
+ string,
218
+ unknown
219
+ >;
220
+ expect(persisted).toEqual(DEFAULT_EXTENSION_CONFIG);
221
+
222
+ await definition!.handler("unknown", infoCtx.ctx);
223
+ expect(lastNotification(infoCtx.notifications).level).toBe("warning");
224
+ expect(lastNotification(infoCtx.notifications).message).toContain(
225
+ "Usage: /permission-system",
226
+ );
227
+
228
+ const headlessCtx = createCommandContext(false);
229
+ await definition!.handler("", headlessCtx.ctx);
230
+ expect(lastNotification(headlessCtx.notifications).message).toBe(
231
+ "/permission-system requires interactive TUI mode.",
232
+ );
233
+
234
+ const modalCtx = createCommandContext(true);
235
+ await definition!.handler("", modalCtx.ctx);
236
+ expect(modalCtx.getCustomCalls()).toBe(1);
237
+ } finally {
238
+ rmSync(baseDir, { recursive: true, force: true });
239
+ }
240
+ });
241
+
242
+ test("show output includes rule origins when getComposedRules is provided", async () => {
243
+ const config = { ...DEFAULT_EXTENSION_CONFIG };
244
+ const composedRules: Rule[] = [
245
+ {
246
+ surface: "read",
247
+ pattern: "*",
248
+ action: "allow",
249
+ layer: "config",
250
+ origin: "global",
251
+ },
252
+ {
253
+ surface: "bash",
254
+ pattern: "rm *",
255
+ action: "deny",
256
+ layer: "config",
257
+ origin: "project",
258
+ },
259
+ ];
260
+
261
+ const controller = {
262
+ config: { current: () => config, save: () => {} } as CommandConfigStore,
263
+ configPath: "/fake/config.json",
264
+ getActiveAgentConfigRules: () => composedRules,
265
+ };
266
+
267
+ let definition: {
268
+ handler: (args: string, ctx: CommandContextStub) => Promise<void>;
269
+ } | null = null;
270
+
271
+ registerPermissionSystemCommand(
272
+ {
273
+ registerCommand(_name: string, nextDef: typeof definition) {
274
+ definition = nextDef;
275
+ },
276
+ } as never,
277
+ controller,
278
+ );
279
+
280
+ const ctx = createCommandContext(true);
281
+ await definition!.handler("show", ctx.ctx);
282
+ const msg = lastNotification(ctx.notifications).message;
283
+
284
+ expect(msg).toContain("global");
285
+ expect(msg).toContain("project");
286
+ expect(msg).toContain("read");
287
+ expect(msg).toContain("bash");
288
+ });
289
+
290
+ test("show output omits rule summary when getComposedRules is not provided", async () => {
291
+ const config = { ...DEFAULT_EXTENSION_CONFIG, yoloMode: true };
292
+
293
+ const controller = {
294
+ config: { current: () => config, save: () => {} } as CommandConfigStore,
295
+ configPath: "/fake/config.json",
296
+ getActiveAgentConfigRules: () => [] as Ruleset,
297
+ };
298
+
299
+ let definition: {
300
+ handler: (args: string, ctx: CommandContextStub) => Promise<void>;
301
+ } | null = null;
302
+
303
+ registerPermissionSystemCommand(
304
+ {
305
+ registerCommand(_name: string, nextDef: typeof definition) {
306
+ definition = nextDef;
307
+ },
308
+ } as never,
309
+ controller,
310
+ );
311
+
312
+ const ctx = createCommandContext(true);
313
+ await definition!.handler("show", ctx.ctx);
314
+ const msg = lastNotification(ctx.notifications).message;
315
+
316
+ // Config knobs still present.
317
+ expect(msg).toContain("yoloMode=on");
318
+ // No rule annotation lines.
319
+ expect(msg).not.toContain("(global)");
320
+ });
@@ -0,0 +1,83 @@
1
+ import { join } from "node:path";
2
+ import { describe, expect, it } from "vitest";
3
+
4
+ import {
5
+ DEBUG_LOG_FILENAME,
6
+ getGlobalConfigDir,
7
+ getGlobalConfigPath,
8
+ getGlobalLogsDir,
9
+ getLegacyExtensionConfigPath,
10
+ getLegacyGlobalPolicyPath,
11
+ getLegacyProjectPolicyPath,
12
+ getProjectAgentsDir,
13
+ getProjectConfigPath,
14
+ REVIEW_LOG_FILENAME,
15
+ } from "#src/config-paths";
16
+
17
+ describe("config-paths", () => {
18
+ const agentDir = "/home/user/.pi/agent";
19
+ const cwd = "/projects/my-app";
20
+ const extensionRoot = "/opt/extensions/pi-permission-system";
21
+
22
+ describe("new layout paths", () => {
23
+ it("getGlobalConfigDir returns extensions/pi-permission-system under agentDir", () => {
24
+ expect(getGlobalConfigDir(agentDir)).toBe(
25
+ join(agentDir, "extensions", "pi-permission-system"),
26
+ );
27
+ });
28
+
29
+ it("getGlobalConfigPath returns config.json under the global config dir", () => {
30
+ expect(getGlobalConfigPath(agentDir)).toBe(
31
+ join(agentDir, "extensions", "pi-permission-system", "config.json"),
32
+ );
33
+ });
34
+
35
+ it("getGlobalLogsDir returns logs under the global config dir", () => {
36
+ expect(getGlobalLogsDir(agentDir)).toBe(
37
+ join(agentDir, "extensions", "pi-permission-system", "logs"),
38
+ );
39
+ });
40
+
41
+ it("getProjectConfigPath returns .pi/extensions/pi-permission-system/config.json under cwd", () => {
42
+ expect(getProjectConfigPath(cwd)).toBe(
43
+ join(cwd, ".pi", "extensions", "pi-permission-system", "config.json"),
44
+ );
45
+ });
46
+
47
+ it("getProjectAgentsDir returns .pi/agents under cwd", () => {
48
+ expect(getProjectAgentsDir(cwd)).toBe(join(cwd, ".pi", "agents"));
49
+ });
50
+ });
51
+
52
+ describe("legacy paths", () => {
53
+ it("getLegacyGlobalPolicyPath returns pi-permissions.jsonc under agentDir", () => {
54
+ expect(getLegacyGlobalPolicyPath(agentDir)).toBe(
55
+ join(agentDir, "pi-permissions.jsonc"),
56
+ );
57
+ });
58
+
59
+ it("getLegacyProjectPolicyPath returns .pi/agent/pi-permissions.jsonc under cwd", () => {
60
+ expect(getLegacyProjectPolicyPath(cwd)).toBe(
61
+ join(cwd, ".pi", "agent", "pi-permissions.jsonc"),
62
+ );
63
+ });
64
+
65
+ it("getLegacyExtensionConfigPath returns config.json under extensionRoot", () => {
66
+ expect(getLegacyExtensionConfigPath(extensionRoot)).toBe(
67
+ join(extensionRoot, "config.json"),
68
+ );
69
+ });
70
+ });
71
+
72
+ describe("log filenames", () => {
73
+ it("DEBUG_LOG_FILENAME is a .jsonl file", () => {
74
+ expect(DEBUG_LOG_FILENAME).toBe("pi-permission-system-debug.jsonl");
75
+ });
76
+
77
+ it("REVIEW_LOG_FILENAME is a .jsonl file", () => {
78
+ expect(REVIEW_LOG_FILENAME).toBe(
79
+ "pi-permission-system-permission-review.jsonl",
80
+ );
81
+ });
82
+ });
83
+ });
@@ -0,0 +1,90 @@
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 { loadAndMergeConfigs } from "#src/config-loader";
7
+ import { normalizePermissionSystemConfig } from "#src/extension-config";
8
+
9
+ /**
10
+ * Full-pipeline seam tests: write a temp config.json → loadAndMergeConfigs →
11
+ * normalizePermissionSystemConfig → assert values survive end to end.
12
+ *
13
+ * These tests guard the seam between the two normalizers — the class of bug
14
+ * fixed in #332, where a field declared on PermissionSystemExtensionConfig was
15
+ * silently dropped by the UnifiedPermissionConfig intermediate.
16
+ */
17
+ describe("config pipeline seam", () => {
18
+ let tempDir: string;
19
+ let agentDir: string;
20
+ let cwd: string;
21
+ let extensionRoot: string;
22
+
23
+ beforeEach(() => {
24
+ tempDir = mkdtempSync(join(tmpdir(), "config-pipeline-test-"));
25
+ agentDir = join(tempDir, "agent");
26
+ cwd = join(tempDir, "project");
27
+ extensionRoot = join(tempDir, "ext");
28
+ });
29
+
30
+ afterEach(() => {
31
+ rmSync(tempDir, { recursive: true, force: true });
32
+ });
33
+
34
+ function writeGlobal(content: Record<string, unknown>): void {
35
+ const dir = join(agentDir, "extensions", "pi-permission-system");
36
+ mkdirSync(dir, { recursive: true });
37
+ writeFileSync(join(dir, "config.json"), JSON.stringify(content));
38
+ }
39
+
40
+ it("runtime knob and preview-length field both survive the full pipeline", () => {
41
+ writeGlobal({
42
+ debugLog: true,
43
+ toolInputPreviewMaxLength: 1000,
44
+ });
45
+
46
+ const mergeResult = loadAndMergeConfigs(agentDir, cwd, extensionRoot);
47
+ const config = normalizePermissionSystemConfig(mergeResult.merged);
48
+
49
+ expect(config.debugLog).toBe(true);
50
+ expect(config.toolInputPreviewMaxLength).toBe(1000);
51
+ });
52
+
53
+ it("text summary length field survives the full pipeline", () => {
54
+ writeGlobal({
55
+ toolTextSummaryMaxLength: 250,
56
+ });
57
+
58
+ const mergeResult = loadAndMergeConfigs(agentDir, cwd, extensionRoot);
59
+ const config = normalizePermissionSystemConfig(mergeResult.merged);
60
+
61
+ expect(config.toolTextSummaryMaxLength).toBe(250);
62
+ });
63
+
64
+ it("project config overrides global preview-length field end to end", () => {
65
+ writeGlobal({ toolInputPreviewMaxLength: 200 });
66
+ const projectDir = join(cwd, ".pi", "extensions", "pi-permission-system");
67
+ mkdirSync(projectDir, { recursive: true });
68
+ writeFileSync(
69
+ join(projectDir, "config.json"),
70
+ JSON.stringify({ toolInputPreviewMaxLength: 500 }),
71
+ );
72
+
73
+ const mergeResult = loadAndMergeConfigs(agentDir, cwd, extensionRoot);
74
+ const config = normalizePermissionSystemConfig(mergeResult.merged);
75
+
76
+ expect(config.toolInputPreviewMaxLength).toBe(500);
77
+ });
78
+
79
+ it("defaults apply when config file is absent", () => {
80
+ // No config files written — agentDir and cwd directories don't exist.
81
+ const mergeResult = loadAndMergeConfigs(agentDir, cwd, extensionRoot);
82
+ const config = normalizePermissionSystemConfig(mergeResult.merged);
83
+
84
+ expect(config.debugLog).toBe(false);
85
+ expect(config.permissionReviewLog).toBe(true);
86
+ expect(config.yoloMode).toBe(false);
87
+ expect(config.toolInputPreviewMaxLength).toBeUndefined();
88
+ expect(config.toolTextSummaryMaxLength).toBeUndefined();
89
+ });
90
+ });
@@ -0,0 +1,147 @@
1
+ import {
2
+ mkdirSync,
3
+ mkdtempSync,
4
+ readFileSync,
5
+ rmSync,
6
+ writeFileSync,
7
+ } from "node:fs";
8
+ import { tmpdir } from "node:os";
9
+ import { join } from "node:path";
10
+ import { expect, test } from "vitest";
11
+ import { buildResolvedConfigLogEntry } from "#src/config-reporter";
12
+ import { createPermissionSystemLogger } from "#src/logging";
13
+ import type { ResolvedPolicyPaths } from "#src/permission-manager";
14
+ import { PermissionManager } from "#src/permission-manager";
15
+
16
+ test("buildResolvedConfigLogEntry includes policy paths and legacy detection flags", () => {
17
+ const policyPaths: ResolvedPolicyPaths = {
18
+ globalConfigPath:
19
+ "/home/user/.pi/agent/extensions/pi-permission-system/config.json",
20
+ globalConfigExists: true,
21
+ projectConfigPath:
22
+ "/projects/my-app/.pi/extensions/pi-permission-system/config.json",
23
+ projectConfigExists: false,
24
+ agentsDir: "/home/user/.pi/agent/agents",
25
+ agentsDirExists: true,
26
+ projectAgentsDir: "/projects/my-app/.pi/agent/agents",
27
+ projectAgentsDirExists: false,
28
+ };
29
+
30
+ const result = buildResolvedConfigLogEntry({ policyPaths });
31
+
32
+ expect(result.globalConfigPath).toBe(
33
+ "/home/user/.pi/agent/extensions/pi-permission-system/config.json",
34
+ );
35
+ expect(result.globalConfigExists).toBe(true);
36
+ expect(result.projectConfigPath).toBe(
37
+ "/projects/my-app/.pi/extensions/pi-permission-system/config.json",
38
+ );
39
+ expect(result.projectConfigExists).toBe(false);
40
+ expect(result.agentsDir).toBe("/home/user/.pi/agent/agents");
41
+ expect(result.agentsDirExists).toBe(true);
42
+ expect(result.projectAgentsDir).toBe("/projects/my-app/.pi/agent/agents");
43
+ expect(result.projectAgentsDirExists).toBe(false);
44
+ expect(result.legacyGlobalPolicyDetected).toBe(false);
45
+ expect(result.legacyProjectPolicyDetected).toBe(false);
46
+ expect(result.legacyExtensionConfigDetected).toBe(false);
47
+ });
48
+
49
+ test("buildResolvedConfigLogEntry handles null project paths", () => {
50
+ const policyPaths: ResolvedPolicyPaths = {
51
+ globalConfigPath:
52
+ "/home/user/.pi/agent/extensions/pi-permission-system/config.json",
53
+ globalConfigExists: false,
54
+ projectConfigPath: null,
55
+ projectConfigExists: false,
56
+ agentsDir: "/home/user/.pi/agent/agents",
57
+ agentsDirExists: false,
58
+ projectAgentsDir: null,
59
+ projectAgentsDirExists: false,
60
+ };
61
+
62
+ const result = buildResolvedConfigLogEntry({ policyPaths });
63
+
64
+ expect(result.projectConfigPath).toBe(null);
65
+ expect(result.projectConfigExists).toBe(false);
66
+ expect(result.projectAgentsDir).toBe(null);
67
+ expect(result.projectAgentsDirExists).toBe(false);
68
+ });
69
+
70
+ test("buildResolvedConfigLogEntry surfaces legacy detection flags", () => {
71
+ const policyPaths: ResolvedPolicyPaths = {
72
+ globalConfigPath:
73
+ "/home/user/.pi/agent/extensions/pi-permission-system/config.json",
74
+ globalConfigExists: true,
75
+ projectConfigPath: null,
76
+ projectConfigExists: false,
77
+ agentsDir: "/home/user/.pi/agent/agents",
78
+ agentsDirExists: false,
79
+ projectAgentsDir: null,
80
+ projectAgentsDirExists: false,
81
+ };
82
+
83
+ const result = buildResolvedConfigLogEntry({
84
+ policyPaths,
85
+ legacyGlobalPolicyDetected: true,
86
+ legacyExtensionConfigDetected: true,
87
+ });
88
+
89
+ expect(result.legacyGlobalPolicyDetected).toBe(true);
90
+ expect(result.legacyProjectPolicyDetected).toBe(false);
91
+ expect(result.legacyExtensionConfigDetected).toBe(true);
92
+ });
93
+
94
+ test("config.resolved entry appears in review log via logger", () => {
95
+ const tempDir = mkdtempSync(join(tmpdir(), "config-resolved-log-"));
96
+ try {
97
+ const logsDir = join(tempDir, "logs");
98
+ mkdirSync(logsDir, { recursive: true });
99
+ const reviewLogPath = join(logsDir, "review.jsonl");
100
+ const debugLogPath = join(logsDir, "debug.jsonl");
101
+
102
+ const globalConfigPath = join(tempDir, "pi-permissions.jsonc");
103
+ writeFileSync(globalConfigPath, "{}", "utf-8");
104
+ const agentsDir = join(tempDir, "agents");
105
+
106
+ const pm = new PermissionManager({
107
+ globalConfigPath,
108
+ agentsDir,
109
+ });
110
+
111
+ const logger = createPermissionSystemLogger({
112
+ getConfig: () => ({
113
+ debugLog: false,
114
+ permissionReviewLog: true,
115
+ yoloMode: false,
116
+ }),
117
+ debugLogPath,
118
+ reviewLogPath,
119
+ ensureLogsDirectory: () => undefined,
120
+ });
121
+
122
+ const policyPaths = pm.getResolvedPolicyPaths();
123
+ const entry = buildResolvedConfigLogEntry({ policyPaths });
124
+ logger.review(
125
+ "config.resolved",
126
+ entry as unknown as Record<string, unknown>,
127
+ );
128
+
129
+ const logContent = readFileSync(reviewLogPath, "utf-8").trim();
130
+ const parsed = JSON.parse(logContent) as Record<string, unknown>;
131
+
132
+ expect(parsed.event).toBe("config.resolved");
133
+ expect(parsed.globalConfigPath).toBe(globalConfigPath);
134
+ expect(parsed.globalConfigExists).toBe(true);
135
+ expect(parsed.agentsDir).toBe(agentsDir);
136
+ expect(parsed.agentsDirExists).toBe(false);
137
+ expect(parsed.projectConfigPath).toBe(null);
138
+ expect(parsed.projectConfigExists).toBe(false);
139
+ expect(parsed.projectAgentsDir).toBe(null);
140
+ expect(parsed.projectAgentsDirExists).toBe(false);
141
+ expect(parsed.legacyGlobalPolicyDetected).toBe(false);
142
+ expect(parsed.legacyProjectPolicyDetected).toBe(false);
143
+ expect(parsed.legacyExtensionConfigDetected).toBe(false);
144
+ } finally {
145
+ rmSync(tempDir, { recursive: true, force: true });
146
+ }
147
+ });