@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,466 +0,0 @@
1
- import { beforeEach, describe, expect, it, vi } from "vitest";
2
-
3
- // ── Module mocks (hoisted) ─────────────────────────────────────────────────
4
-
5
- const {
6
- mockLoadAndMergeConfigs,
7
- mockLoadUnifiedConfig,
8
- mockSyncPermissionSystemStatus,
9
- mockBuildResolvedConfigLogEntry,
10
- mockExistsSync,
11
- mockMkdirSync,
12
- mockWriteFileSync,
13
- mockRenameSync,
14
- mockUnlinkSync,
15
- } = vi.hoisted(() => ({
16
- mockLoadAndMergeConfigs: vi.fn(),
17
- mockLoadUnifiedConfig: vi.fn(),
18
- mockSyncPermissionSystemStatus: vi.fn(),
19
- mockBuildResolvedConfigLogEntry: vi.fn(),
20
- mockExistsSync: vi.fn<(path: string) => boolean>(),
21
- mockMkdirSync: vi.fn(),
22
- mockWriteFileSync: vi.fn(),
23
- mockRenameSync: vi.fn(),
24
- mockUnlinkSync: vi.fn(),
25
- }));
26
-
27
- vi.mock("../src/config-loader", () => ({
28
- loadAndMergeConfigs: mockLoadAndMergeConfigs,
29
- loadUnifiedConfig: mockLoadUnifiedConfig,
30
- }));
31
-
32
- vi.mock("../src/status", () => ({
33
- syncPermissionSystemStatus: mockSyncPermissionSystemStatus,
34
- }));
35
-
36
- vi.mock("../src/config-reporter", () => ({
37
- buildResolvedConfigLogEntry: mockBuildResolvedConfigLogEntry,
38
- }));
39
-
40
- vi.mock("node:fs", () => ({
41
- existsSync: mockExistsSync,
42
- mkdirSync: mockMkdirSync,
43
- writeFileSync: mockWriteFileSync,
44
- renameSync: mockRenameSync,
45
- unlinkSync: mockUnlinkSync,
46
- default: {
47
- existsSync: mockExistsSync,
48
- mkdirSync: mockMkdirSync,
49
- writeFileSync: mockWriteFileSync,
50
- renameSync: mockRenameSync,
51
- unlinkSync: mockUnlinkSync,
52
- },
53
- }));
54
-
55
- // ── Imports ────────────────────────────────────────────────────────────────
56
-
57
- import type {
58
- ExtensionCommandContext,
59
- ExtensionContext,
60
- } from "@earendil-works/pi-coding-agent";
61
- import {
62
- ConfigStore,
63
- type ConfigStoreDeps,
64
- type ResolvedPolicyPathProvider,
65
- } from "#src/config-store";
66
- import { DEFAULT_EXTENSION_CONFIG } from "#src/extension-config";
67
- import type { ResolvedPolicyPaths } from "#src/policy-loader";
68
-
69
- // ── Helpers ────────────────────────────────────────────────────────────────
70
-
71
- function makePolicyPathProvider(
72
- paths?: Partial<ResolvedPolicyPaths>,
73
- ): ResolvedPolicyPathProvider {
74
- return {
75
- getResolvedPolicyPaths: vi.fn(
76
- (): ResolvedPolicyPaths => ({
77
- globalConfigPath: "/agent/config.json",
78
- globalConfigExists: false,
79
- projectConfigPath: null,
80
- projectConfigExists: false,
81
- agentsDir: "/agent/agents",
82
- agentsDirExists: false,
83
- projectAgentsDir: null,
84
- projectAgentsDirExists: false,
85
- ...paths,
86
- }),
87
- ),
88
- };
89
- }
90
-
91
- function makeLogger() {
92
- return {
93
- debug: vi.fn<(event: string, details?: Record<string, unknown>) => void>(),
94
- review: vi.fn<(event: string, details?: Record<string, unknown>) => void>(),
95
- };
96
- }
97
-
98
- function makeCtx(overrides: Partial<ExtensionContext> = {}): ExtensionContext {
99
- return {
100
- cwd: "/test/project",
101
- hasUI: false,
102
- ui: { notify: vi.fn(), setStatus: vi.fn() },
103
- sessionManager: { getEntries: vi.fn(), addEntry: vi.fn() },
104
- ...overrides,
105
- } as unknown as ExtensionContext;
106
- }
107
-
108
- function makeCommandCtx(
109
- overrides: Partial<ExtensionCommandContext> = {},
110
- ): ExtensionCommandContext {
111
- return {
112
- cwd: "/test/project",
113
- ui: { notify: vi.fn(), setStatus: vi.fn() },
114
- ...overrides,
115
- } as unknown as ExtensionCommandContext;
116
- }
117
-
118
- function makeStore(overrides: Partial<ConfigStoreDeps> = {}): {
119
- store: ConfigStore;
120
- logger: ReturnType<typeof makeLogger>;
121
- } {
122
- const logger = makeLogger();
123
- const deps: ConfigStoreDeps = {
124
- agentDir: "/test/agent",
125
- policyPaths: makePolicyPathProvider(),
126
- logger,
127
- ...overrides,
128
- };
129
- return { store: new ConfigStore(deps), logger };
130
- }
131
-
132
- // ── Tests ──────────────────────────────────────────────────────────────────
133
-
134
- describe("ConfigStore", () => {
135
- beforeEach(() => {
136
- mockLoadAndMergeConfigs.mockReset().mockReturnValue({
137
- merged: { ...DEFAULT_EXTENSION_CONFIG },
138
- issues: [],
139
- });
140
- mockLoadUnifiedConfig.mockReset().mockReturnValue({ config: {} });
141
- mockSyncPermissionSystemStatus.mockReset();
142
- mockBuildResolvedConfigLogEntry
143
- .mockReset()
144
- .mockReturnValue({ resolved: true });
145
- mockExistsSync.mockReset().mockReturnValue(false);
146
- mockMkdirSync.mockReset();
147
- mockWriteFileSync.mockReset();
148
- mockRenameSync.mockReset();
149
- mockUnlinkSync.mockReset();
150
- });
151
-
152
- // ── current() ─────────────────────────────────────────────────────────
153
-
154
- describe("current()", () => {
155
- it("returns DEFAULT_EXTENSION_CONFIG before any refresh", () => {
156
- const { store } = makeStore();
157
- expect(store.current()).toEqual(DEFAULT_EXTENSION_CONFIG);
158
- });
159
- });
160
-
161
- // ── refresh() ─────────────────────────────────────────────────────────
162
-
163
- describe("refresh()", () => {
164
- it("uses the passed ctx cwd for loadAndMergeConfigs", () => {
165
- const { store } = makeStore();
166
- store.refresh(makeCtx({ cwd: "/my/project" }));
167
- expect(mockLoadAndMergeConfigs).toHaveBeenCalledWith(
168
- "/test/agent",
169
- "/my/project",
170
- expect.any(String),
171
- );
172
- });
173
-
174
- it("uses empty string cwd when no ctx is provided", () => {
175
- const { store } = makeStore();
176
- store.refresh();
177
- expect(mockLoadAndMergeConfigs).toHaveBeenCalledWith(
178
- "/test/agent",
179
- "",
180
- expect.any(String),
181
- );
182
- });
183
-
184
- it("updates current() with normalized merged result", () => {
185
- const { store } = makeStore();
186
- mockLoadAndMergeConfigs.mockReturnValue({
187
- merged: { debugLog: true, permissionReviewLog: false, yoloMode: false },
188
- issues: [],
189
- });
190
- store.refresh();
191
- expect(store.current().debugLog).toBe(true);
192
- expect(store.current().permissionReviewLog).toBe(false);
193
- });
194
-
195
- it("writes config.loaded debug log", () => {
196
- const { store, logger } = makeStore();
197
- store.refresh();
198
- expect(logger.debug).toHaveBeenCalledWith(
199
- "config.loaded",
200
- expect.objectContaining({ debugLog: false }),
201
- );
202
- });
203
-
204
- it("sets warning when issues are present", () => {
205
- const { store } = makeStore();
206
- const ctx = makeCtx({ hasUI: false });
207
- mockLoadAndMergeConfigs.mockReturnValue({
208
- merged: { ...DEFAULT_EXTENSION_CONFIG },
209
- issues: ["legacy config detected"],
210
- });
211
- store.refresh(ctx);
212
- // Verify the warning is tracked (next identical call should not re-notify)
213
- const mockNotify = vi.fn();
214
- const ctx2 = makeCtx({
215
- hasUI: true,
216
- ui: { notify: mockNotify } as never,
217
- });
218
- mockLoadAndMergeConfigs.mockReturnValue({
219
- merged: { ...DEFAULT_EXTENSION_CONFIG },
220
- issues: ["legacy config detected"],
221
- });
222
- store.refresh(ctx2);
223
- // Same warning — should not re-notify
224
- expect(mockNotify).not.toHaveBeenCalled();
225
- });
226
-
227
- it("notifies UI when a new warning appears and hasUI is true", () => {
228
- const mockNotify = vi.fn();
229
- const { store } = makeStore();
230
- const ctx = makeCtx({ hasUI: true, ui: { notify: mockNotify } as never });
231
- mockLoadAndMergeConfigs.mockReturnValue({
232
- merged: { ...DEFAULT_EXTENSION_CONFIG },
233
- issues: ["new warning"],
234
- });
235
- store.refresh(ctx);
236
- expect(mockNotify).toHaveBeenCalledWith("new warning", "warning");
237
- });
238
-
239
- it("does not re-notify the same warning on subsequent calls", () => {
240
- const mockNotify = vi.fn();
241
- const { store } = makeStore();
242
- const ctx = makeCtx({ hasUI: true, ui: { notify: mockNotify } as never });
243
- mockLoadAndMergeConfigs.mockReturnValue({
244
- merged: { ...DEFAULT_EXTENSION_CONFIG },
245
- issues: ["persistent warning"],
246
- });
247
- store.refresh(ctx);
248
- store.refresh(ctx);
249
- expect(mockNotify).toHaveBeenCalledTimes(1);
250
- });
251
-
252
- it("clears warning when no issues on next refresh", () => {
253
- const mockNotify = vi.fn();
254
- const { store } = makeStore();
255
- // First call: set a warning
256
- const ctxWithUI = makeCtx({
257
- hasUI: true,
258
- ui: { notify: mockNotify } as never,
259
- });
260
- mockLoadAndMergeConfigs.mockReturnValue({
261
- merged: { ...DEFAULT_EXTENSION_CONFIG },
262
- issues: ["warning"],
263
- });
264
- store.refresh(ctxWithUI);
265
- // Second call: no issues — warning should clear
266
- mockLoadAndMergeConfigs.mockReturnValue({
267
- merged: { ...DEFAULT_EXTENSION_CONFIG },
268
- issues: [],
269
- });
270
- store.refresh();
271
- // Third call: same warning reappears — should notify again (dedup cleared)
272
- mockLoadAndMergeConfigs.mockReturnValue({
273
- merged: { ...DEFAULT_EXTENSION_CONFIG },
274
- issues: ["warning"],
275
- });
276
- store.refresh(ctxWithUI);
277
- expect(mockNotify).toHaveBeenCalledTimes(2);
278
- });
279
-
280
- it("calls syncPermissionSystemStatus when hasUI is true", () => {
281
- const { store } = makeStore();
282
- const ctx = makeCtx({ hasUI: true });
283
- store.refresh(ctx);
284
- expect(mockSyncPermissionSystemStatus).toHaveBeenCalledWith(
285
- ctx,
286
- expect.any(Object),
287
- );
288
- });
289
-
290
- it("does not call syncPermissionSystemStatus when hasUI is false", () => {
291
- const { store } = makeStore();
292
- const ctx = makeCtx({ hasUI: false });
293
- store.refresh(ctx);
294
- expect(mockSyncPermissionSystemStatus).not.toHaveBeenCalled();
295
- });
296
-
297
- it("carries piInfrastructureReadPaths from merged config into current()", () => {
298
- const { store } = makeStore();
299
- mockLoadAndMergeConfigs.mockReturnValue({
300
- merged: { piInfrastructureReadPaths: ["/extra/path"] },
301
- issues: [],
302
- });
303
- store.refresh();
304
- expect(store.current().piInfrastructureReadPaths).toEqual([
305
- "/extra/path",
306
- ]);
307
- });
308
- });
309
-
310
- // ── save() ─────────────────────────────────────────────────────────────
311
-
312
- describe("save()", () => {
313
- it("writes merged config to the global path", () => {
314
- const { store } = makeStore();
315
- mockLoadUnifiedConfig.mockReturnValue({
316
- config: { permission: { "*": "ask" } },
317
- });
318
- const next = { ...DEFAULT_EXTENSION_CONFIG, debugLog: true };
319
- const ctx = makeCommandCtx();
320
- store.save(next, ctx);
321
- expect(mockWriteFileSync).toHaveBeenCalledWith(
322
- expect.stringContaining(".tmp"),
323
- expect.stringContaining('"debugLog": true'),
324
- "utf-8",
325
- );
326
- expect(mockRenameSync).toHaveBeenCalled();
327
- });
328
-
329
- it("updates current() after a successful save", () => {
330
- const { store } = makeStore();
331
- const next = { ...DEFAULT_EXTENSION_CONFIG, debugLog: true };
332
- store.save(next, makeCommandCtx());
333
- expect(store.current().debugLog).toBe(true);
334
- });
335
-
336
- it("calls syncPermissionSystemStatus after a successful save", () => {
337
- const { store } = makeStore();
338
- const ctx = makeCommandCtx();
339
- store.save({ ...DEFAULT_EXTENSION_CONFIG }, ctx);
340
- expect(mockSyncPermissionSystemStatus).toHaveBeenCalledWith(
341
- ctx,
342
- expect.any(Object),
343
- );
344
- });
345
-
346
- it("writes config.saved debug log after a successful save", () => {
347
- const { store, logger } = makeStore();
348
- store.save({ ...DEFAULT_EXTENSION_CONFIG }, makeCommandCtx());
349
- expect(logger.debug).toHaveBeenCalledWith(
350
- "config.saved",
351
- expect.objectContaining({ debugLog: false }),
352
- );
353
- });
354
-
355
- it("notifies with error and returns early when write fails", () => {
356
- const mockNotify = vi.fn();
357
- const ctx = makeCommandCtx({ ui: { notify: mockNotify } as never });
358
- const { store, logger } = makeStore();
359
- mockMkdirSync.mockImplementation(() => {
360
- throw new Error("disk full");
361
- });
362
- store.save({ ...DEFAULT_EXTENSION_CONFIG }, ctx);
363
- expect(mockNotify).toHaveBeenCalledWith(
364
- expect.stringContaining("Failed to save"),
365
- "error",
366
- );
367
- // current() is not updated on failure
368
- expect(store.current()).toEqual(DEFAULT_EXTENSION_CONFIG);
369
- // no debug log on failure
370
- expect(logger.debug).not.toHaveBeenCalledWith(
371
- "config.saved",
372
- expect.anything(),
373
- );
374
- });
375
-
376
- it("attempts cleanup of tmp file when write fails and tmp exists", () => {
377
- const ctx = makeCommandCtx();
378
- const { store } = makeStore();
379
- mockMkdirSync.mockImplementation(() => {
380
- throw new Error("disk full");
381
- });
382
- mockExistsSync.mockReturnValue(true);
383
- store.save({ ...DEFAULT_EXTENSION_CONFIG }, ctx);
384
- expect(mockUnlinkSync).toHaveBeenCalled();
385
- });
386
-
387
- it("preserves an existing global toolInputPreviewMaxLength on save", () => {
388
- const { store } = makeStore();
389
- // Simulate a global config.json that already has the preview-length field.
390
- mockLoadUnifiedConfig.mockReturnValue({
391
- config: { toolInputPreviewMaxLength: 800 },
392
- });
393
- store.save({ ...DEFAULT_EXTENSION_CONFIG }, makeCommandCtx());
394
- expect(mockWriteFileSync).toHaveBeenCalledWith(
395
- expect.stringContaining(".tmp"),
396
- expect.stringContaining('"toolInputPreviewMaxLength": 800'),
397
- "utf-8",
398
- );
399
- });
400
-
401
- it("preserves an existing global piInfrastructureReadPaths on save", () => {
402
- const { store } = makeStore();
403
- // Simulate a global config.json that already has the infra-paths field.
404
- mockLoadUnifiedConfig.mockReturnValue({
405
- config: { piInfrastructureReadPaths: ["/extra/path"] },
406
- });
407
- store.save({ ...DEFAULT_EXTENSION_CONFIG }, makeCommandCtx());
408
- expect(mockWriteFileSync).toHaveBeenCalledWith(
409
- expect.stringContaining(".tmp"),
410
- expect.stringContaining('"piInfrastructureReadPaths"'),
411
- "utf-8",
412
- );
413
- });
414
- });
415
-
416
- // ── logResolvedPaths() ─────────────────────────────────────────────────
417
-
418
- describe("logResolvedPaths()", () => {
419
- it("writes config.resolved to both review and debug logs", () => {
420
- const { store, logger } = makeStore();
421
- store.logResolvedPaths();
422
- expect(logger.review).toHaveBeenCalledWith(
423
- "config.resolved",
424
- expect.any(Object),
425
- );
426
- expect(logger.debug).toHaveBeenCalledWith(
427
- "config.resolved",
428
- expect.any(Object),
429
- );
430
- });
431
-
432
- it("calls getResolvedPolicyPaths from the provider", () => {
433
- const mockProvider = makePolicyPathProvider();
434
- const { store } = makeStore({ policyPaths: mockProvider });
435
- store.logResolvedPaths();
436
- expect(mockProvider.getResolvedPolicyPaths).toHaveBeenCalled();
437
- });
438
-
439
- it("passes legacy detection results to buildResolvedConfigLogEntry", () => {
440
- const { store } = makeStore();
441
- // Make one legacy path exist
442
- mockExistsSync.mockImplementation((p: string) =>
443
- p.includes("policies.json"),
444
- );
445
- store.logResolvedPaths("/some/project");
446
- expect(mockBuildResolvedConfigLogEntry).toHaveBeenCalledWith(
447
- expect.objectContaining({
448
- legacyGlobalPolicyDetected: expect.any(Boolean),
449
- legacyProjectPolicyDetected: expect.any(Boolean),
450
- legacyExtensionConfigDetected: expect.any(Boolean),
451
- }),
452
- );
453
- });
454
-
455
- it("does not check project legacy path when no cwd is provided", () => {
456
- const { store } = makeStore();
457
- store.logResolvedPaths(); // no cwd
458
- // existsSync called for global and ext-config legacy paths only (not project)
459
- const calls = mockExistsSync.mock.calls.map(([p]: [string]) => p);
460
- const projectCalls = calls.filter(
461
- (p) => p.includes("/null/") || p.includes("null"),
462
- );
463
- expect(projectCalls).toHaveLength(0);
464
- });
465
- });
466
- });
@@ -1,72 +0,0 @@
1
- import { describe, expect, it, vi } from "vitest";
2
-
3
- import { DecisionAudit } from "#src/decision-audit";
4
-
5
- function makeAuditLogger() {
6
- return {
7
- debug: vi.fn<(event: string, details?: Record<string, unknown>) => void>(),
8
- warn: vi.fn<(message: string) => void>(),
9
- };
10
- }
11
-
12
- describe("DecisionAudit", () => {
13
- it("counts allowed, blocked, and error decisions in the summary", () => {
14
- const audit = new DecisionAudit();
15
- audit.recordDecision("allow");
16
- audit.recordDecision("allow");
17
- audit.recordDecision("block");
18
- audit.recordError();
19
-
20
- const logger = makeAuditLogger();
21
- audit.writeSummary(logger);
22
-
23
- expect(logger.debug).toHaveBeenCalledWith("permission.session_summary", {
24
- toolCalls: 4,
25
- allowed: 2,
26
- blocked: 1,
27
- errors: 1,
28
- });
29
- });
30
-
31
- it("emits a zeroed summary when no calls were recorded", () => {
32
- const audit = new DecisionAudit();
33
- const logger = makeAuditLogger();
34
-
35
- audit.writeSummary(logger);
36
-
37
- expect(logger.debug).toHaveBeenCalledWith("permission.session_summary", {
38
- toolCalls: 0,
39
- allowed: 0,
40
- blocked: 0,
41
- errors: 0,
42
- });
43
- });
44
-
45
- it("does not warn when the counts are consistent", () => {
46
- const audit = new DecisionAudit();
47
- audit.recordDecision("allow");
48
- audit.recordError();
49
-
50
- const logger = makeAuditLogger();
51
- audit.writeSummary(logger);
52
-
53
- expect(logger.warn).not.toHaveBeenCalled();
54
- });
55
-
56
- it("warns when the per-call invariant is violated", () => {
57
- const audit = new DecisionAudit();
58
- audit.recordDecision("allow");
59
- // Force a re-opened silent path: bump the private total without a matching
60
- // sub-total, simulating a future regression that resolves a call without
61
- // recording its terminal decision.
62
- (audit as unknown as { toolCalls: number }).toolCalls++;
63
-
64
- const logger = makeAuditLogger();
65
- audit.writeSummary(logger);
66
-
67
- expect(logger.warn).toHaveBeenCalledTimes(1);
68
- expect(logger.warn).toHaveBeenCalledWith(
69
- expect.stringContaining("invariant violated"),
70
- );
71
- });
72
- });
@@ -1,112 +0,0 @@
1
- import { describe, expect, it, vi } from "vitest";
2
-
3
- import {
4
- type DecisionReporter,
5
- GateDecisionReporter,
6
- } from "#src/decision-reporter";
7
- import {
8
- PERMISSIONS_DECISION_CHANNEL,
9
- type PermissionDecisionEvent,
10
- } from "#src/permission-events";
11
- import type { SessionLogger } from "#src/session-logger";
12
-
13
- // ── fixtures ───────────────────────────────────────────────────────────────
14
-
15
- function makeLogger(): SessionLogger {
16
- return {
17
- debug: vi.fn(),
18
- review: vi.fn(),
19
- warn: vi.fn(),
20
- };
21
- }
22
-
23
- function makeEvents() {
24
- return {
25
- emit: vi.fn(),
26
- on: vi.fn().mockReturnValue(() => undefined),
27
- };
28
- }
29
-
30
- function makeDecisionEvent(
31
- overrides: Partial<PermissionDecisionEvent> = {},
32
- ): PermissionDecisionEvent {
33
- return {
34
- surface: "read",
35
- value: "read",
36
- result: "allow",
37
- resolution: "policy_allow",
38
- origin: "global",
39
- agentName: null,
40
- matchedPattern: null,
41
- ...overrides,
42
- };
43
- }
44
-
45
- // ── tests ──────────────────────────────────────────────────────────────────
46
-
47
- describe("GateDecisionReporter", () => {
48
- it("satisfies the DecisionReporter interface", () => {
49
- const reporter: DecisionReporter = new GateDecisionReporter(
50
- makeLogger(),
51
- makeEvents(),
52
- );
53
- expect(reporter).toBeDefined();
54
- });
55
-
56
- describe("writeReviewLog", () => {
57
- it("delegates to logger.review with event and details", () => {
58
- const logger = makeLogger();
59
- const reporter = new GateDecisionReporter(logger, makeEvents());
60
- reporter.writeReviewLog("permission_request.blocked", { tool: "bash" });
61
- expect(logger.review).toHaveBeenCalledWith("permission_request.blocked", {
62
- tool: "bash",
63
- });
64
- });
65
-
66
- it("delegates with an empty details object", () => {
67
- const logger = makeLogger();
68
- const reporter = new GateDecisionReporter(logger, makeEvents());
69
- reporter.writeReviewLog("permission_request.session_approved", {});
70
- expect(logger.review).toHaveBeenCalledWith(
71
- "permission_request.session_approved",
72
- {},
73
- );
74
- });
75
-
76
- it("does not call emitDecision", () => {
77
- const events = makeEvents();
78
- const reporter = new GateDecisionReporter(makeLogger(), events);
79
- reporter.writeReviewLog("some.event", { key: "val" });
80
- expect(events.emit).not.toHaveBeenCalled();
81
- });
82
- });
83
-
84
- describe("emitDecision", () => {
85
- it("emits on the PERMISSIONS_DECISION_CHANNEL with the event", () => {
86
- const events = makeEvents();
87
- const reporter = new GateDecisionReporter(makeLogger(), events);
88
- const event = makeDecisionEvent();
89
- reporter.emitDecision(event);
90
- expect(events.emit).toHaveBeenCalledWith(
91
- PERMISSIONS_DECISION_CHANNEL,
92
- event,
93
- );
94
- });
95
-
96
- it("does not call writeReviewLog", () => {
97
- const logger = makeLogger();
98
- const reporter = new GateDecisionReporter(logger, makeEvents());
99
- reporter.emitDecision(makeDecisionEvent());
100
- expect(logger.review).not.toHaveBeenCalled();
101
- });
102
-
103
- it("does not propagate a throwing listener", () => {
104
- const events = makeEvents();
105
- events.emit.mockImplementation(() => {
106
- throw new Error("listener boom");
107
- });
108
- const reporter = new GateDecisionReporter(makeLogger(), events);
109
- expect(() => reporter.emitDecision(makeDecisionEvent())).not.toThrow();
110
- });
111
- });
112
- });