@gotgenes/pi-permission-system 18.1.1 → 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 (122) hide show
  1. package/CHANGELOG.md +16 -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/path-normalizer.ts +105 -2
  8. package/test/access-intent/access-path.test.ts +0 -277
  9. package/test/access-intent/bash/node-text.test.ts +0 -148
  10. package/test/access-intent/bash/parser.test.ts +0 -19
  11. package/test/access-intent/bash/program.test.ts +0 -673
  12. package/test/access-intent/bash/token-classification.test.ts +0 -363
  13. package/test/access-intent/bash/token-collection.test.ts +0 -300
  14. package/test/active-agent.test.ts +0 -155
  15. package/test/async-cache.test.ts +0 -48
  16. package/test/bash-arity.test.ts +0 -144
  17. package/test/bash-external-directory.test.ts +0 -1022
  18. package/test/builtin-tool-input-formatters.test.ts +0 -109
  19. package/test/canonicalize-path.test.ts +0 -119
  20. package/test/composition-root.test.ts +0 -698
  21. package/test/config-loader.test.ts +0 -740
  22. package/test/config-modal.test.ts +0 -320
  23. package/test/config-paths.test.ts +0 -83
  24. package/test/config-pipeline.test.ts +0 -90
  25. package/test/config-reporter.test.ts +0 -147
  26. package/test/config-store.test.ts +0 -466
  27. package/test/decision-audit.test.ts +0 -72
  28. package/test/decision-reporter.test.ts +0 -112
  29. package/test/denial-messages.test.ts +0 -714
  30. package/test/detect-permissive-bash-fallback.test.ts +0 -56
  31. package/test/expand-home.test.ts +0 -93
  32. package/test/extension-config.test.ts +0 -129
  33. package/test/extension-paths.test.ts +0 -108
  34. package/test/forwarded-permissions/io.test.ts +0 -251
  35. package/test/forwarding-manager.test.ts +0 -200
  36. package/test/handlers/before-agent-start.test.ts +0 -314
  37. package/test/handlers/external-directory-integration.test.ts +0 -515
  38. package/test/handlers/external-directory-session-dedup.test.ts +0 -175
  39. package/test/handlers/external-directory-symlink-acceptance.test.ts +0 -167
  40. package/test/handlers/gates/bash-command-metamorphic.test.ts +0 -88
  41. package/test/handlers/gates/bash-command.test.ts +0 -257
  42. package/test/handlers/gates/bash-external-directory.test.ts +0 -268
  43. package/test/handlers/gates/bash-path.test.ts +0 -346
  44. package/test/handlers/gates/candidate-check.test.ts +0 -52
  45. package/test/handlers/gates/external-directory-messages.test.ts +0 -85
  46. package/test/handlers/gates/external-directory-policy.test.ts +0 -134
  47. package/test/handlers/gates/external-directory.test.ts +0 -267
  48. package/test/handlers/gates/helpers.test.ts +0 -165
  49. package/test/handlers/gates/path.test.ts +0 -369
  50. package/test/handlers/gates/runner.test.ts +0 -408
  51. package/test/handlers/gates/skill-input-gate-pipeline.test.ts +0 -176
  52. package/test/handlers/gates/skill-input.test.ts +0 -128
  53. package/test/handlers/gates/skill-read.test.ts +0 -161
  54. package/test/handlers/gates/tool-call-gate-pipeline.test.ts +0 -356
  55. package/test/handlers/gates/tool.test.ts +0 -244
  56. package/test/handlers/input-events.test.ts +0 -168
  57. package/test/handlers/input.test.ts +0 -199
  58. package/test/handlers/lifecycle.test.ts +0 -221
  59. package/test/handlers/tool-call-boundary.test.ts +0 -145
  60. package/test/handlers/tool-call-events.test.ts +0 -277
  61. package/test/handlers/tool-call.test.ts +0 -395
  62. package/test/handlers/validate-requested-tool.test.ts +0 -92
  63. package/test/helpers/external-directory-fixtures.ts +0 -269
  64. package/test/helpers/gate-fixtures.ts +0 -316
  65. package/test/helpers/handler-fixtures.ts +0 -335
  66. package/test/helpers/make-fake-pi.ts +0 -100
  67. package/test/helpers/manager-harness.ts +0 -112
  68. package/test/helpers/session-fixtures.ts +0 -199
  69. package/test/input-normalizer.test.ts +0 -325
  70. package/test/logging.test.ts +0 -51
  71. package/test/mcp-targets.test.ts +0 -233
  72. package/test/node-modules-discovery.test.ts +0 -97
  73. package/test/normalize.test.ts +0 -247
  74. package/test/path-containment.test.ts +0 -161
  75. package/test/path-normalization.test.ts +0 -233
  76. package/test/path-normalizer.test.ts +0 -196
  77. package/test/path-surfaces.test.ts +0 -55
  78. package/test/pattern-suggest.test.ts +0 -248
  79. package/test/permission-dialog.test.ts +0 -205
  80. package/test/permission-event-rpc.test.ts +0 -560
  81. package/test/permission-events.test.ts +0 -400
  82. package/test/permission-forwarder.test.ts +0 -370
  83. package/test/permission-forwarding.test.ts +0 -315
  84. package/test/permission-gate.test.ts +0 -269
  85. package/test/permission-manager-unified.test.ts +0 -3714
  86. package/test/permission-merge.test.ts +0 -61
  87. package/test/permission-prompter.test.ts +0 -518
  88. package/test/permission-prompts.test.ts +0 -363
  89. package/test/permission-resolver.test.ts +0 -277
  90. package/test/permission-session.test.ts +0 -404
  91. package/test/permission-ui-prompt.test.ts +0 -146
  92. package/test/permissions-service.test.ts +0 -192
  93. package/test/pi-infrastructure-read.test.ts +0 -432
  94. package/test/policy-loader.test.ts +0 -561
  95. package/test/prompting-gateway.test.ts +0 -231
  96. package/test/rule.test.ts +0 -650
  97. package/test/safe-system-paths.test.ts +0 -46
  98. package/test/scope-merge.test.ts +0 -116
  99. package/test/service-lifecycle.test.ts +0 -163
  100. package/test/service.test.ts +0 -261
  101. package/test/session-approval.test.ts +0 -75
  102. package/test/session-logger.test.ts +0 -200
  103. package/test/session-rules.test.ts +0 -321
  104. package/test/session-start.test.ts +0 -112
  105. package/test/skill-prompt-sanitizer.test.ts +0 -418
  106. package/test/status.test.ts +0 -10
  107. package/test/subagent-context.test.ts +0 -372
  108. package/test/subagent-lifecycle-events.test.ts +0 -132
  109. package/test/subagent-registry.test.ts +0 -145
  110. package/test/synthesize.test.ts +0 -302
  111. package/test/system-prompt-sanitizer.test.ts +0 -382
  112. package/test/tool-access-extractor-registry.test.ts +0 -77
  113. package/test/tool-input-formatter-registry.test.ts +0 -75
  114. package/test/tool-input-path.test.ts +0 -84
  115. package/test/tool-input-preview.test.ts +0 -129
  116. package/test/tool-input-prompt-formatters.test.ts +0 -115
  117. package/test/tool-preview-formatter.test.ts +0 -458
  118. package/test/tool-registry.test.ts +0 -197
  119. package/test/value-guards.test.ts +0 -193
  120. package/test/wildcard-matcher.test.ts +0 -424
  121. package/test/yaml-frontmatter.test.ts +0 -91
  122. package/test/yolo-mode.test.ts +0 -188
@@ -1,200 +0,0 @@
1
- import { existsSync, mkdtempSync, writeFileSync } from "node:fs";
2
- import { tmpdir } from "node:os";
3
- import { join } from "node:path";
4
- import { beforeEach, describe, expect, it, vi } from "vitest";
5
- import { DEBUG_LOG_FILENAME, REVIEW_LOG_FILENAME } from "#src/config-paths";
6
- import {
7
- DEFAULT_EXTENSION_CONFIG,
8
- type PermissionSystemExtensionConfig,
9
- } from "#src/extension-config";
10
- import type { SessionLoggerDeps } from "#src/session-logger";
11
- import { PermissionSessionLogger } from "#src/session-logger";
12
-
13
- // ── helpers ────────────────────────────────────────────────────────────────
14
-
15
- let tempDir: string;
16
-
17
- beforeEach(() => {
18
- tempDir = mkdtempSync(join(tmpdir(), "ps-session-logger-"));
19
- });
20
-
21
- function makeDeps(
22
- overrides: {
23
- globalLogsDir?: string;
24
- getConfig?: () => PermissionSystemExtensionConfig;
25
- } = {},
26
- ) {
27
- return {
28
- globalLogsDir: overrides.globalLogsDir ?? tempDir,
29
- getConfig:
30
- overrides.getConfig ??
31
- ((): PermissionSystemExtensionConfig => ({
32
- ...DEFAULT_EXTENSION_CONFIG,
33
- })),
34
- notify: vi.fn<(message: string) => void>(),
35
- };
36
- }
37
-
38
- /** A `globalLogsDir` that cannot be created: a file at the parent path blocks it. */
39
- function makeBlockedLogsDir(): string {
40
- const barrier = join(tempDir, "barrier");
41
- writeFileSync(barrier, "");
42
- return join(barrier, "logs");
43
- }
44
-
45
- // ── PermissionSessionLogger ────────────────────────────────────────────────────
46
-
47
- describe("PermissionSessionLogger", () => {
48
- // ── debug ────────────────────────────────────────────────────────────────
49
-
50
- describe("debug", () => {
51
- it("writes a JSONL line to the debug log file when debugLog is true", () => {
52
- const deps = makeDeps({
53
- getConfig: () => ({ ...DEFAULT_EXTENSION_CONFIG, debugLog: true }),
54
- });
55
- const logger = new PermissionSessionLogger(deps);
56
-
57
- logger.debug("test.event", { key: "value" });
58
-
59
- expect(existsSync(join(tempDir, DEBUG_LOG_FILENAME))).toBe(true);
60
- expect(deps.notify).not.toHaveBeenCalled();
61
- });
62
-
63
- it("does not write to the debug log when debugLog is false", () => {
64
- // DEFAULT_EXTENSION_CONFIG.debugLog === false
65
- const deps = makeDeps();
66
- const logger = new PermissionSessionLogger(deps);
67
-
68
- logger.debug("test.event");
69
-
70
- expect(existsSync(join(tempDir, DEBUG_LOG_FILENAME))).toBe(false);
71
- expect(deps.notify).not.toHaveBeenCalled();
72
- });
73
-
74
- it("reads getConfig at write time — a mid-session toggle change takes effect", () => {
75
- let debugLog = true;
76
- const deps = makeDeps({
77
- getConfig: () => ({ ...DEFAULT_EXTENSION_CONFIG, debugLog }),
78
- });
79
- const logger = new PermissionSessionLogger(deps);
80
- debugLog = false;
81
-
82
- logger.debug("test.event");
83
-
84
- expect(existsSync(join(tempDir, DEBUG_LOG_FILENAME))).toBe(false);
85
- });
86
- });
87
-
88
- // ── review ───────────────────────────────────────────────────────────────
89
-
90
- describe("review", () => {
91
- it("writes a JSONL line to the review log file when permissionReviewLog is true", () => {
92
- // DEFAULT_EXTENSION_CONFIG.permissionReviewLog === true
93
- const deps = makeDeps();
94
- const logger = new PermissionSessionLogger(deps);
95
-
96
- logger.review("permission.granted", { agentName: "coder" });
97
-
98
- expect(existsSync(join(tempDir, REVIEW_LOG_FILENAME))).toBe(true);
99
- expect(deps.notify).not.toHaveBeenCalled();
100
- });
101
-
102
- it("does not write to the review log when permissionReviewLog is false", () => {
103
- const deps = makeDeps({
104
- getConfig: () => ({
105
- ...DEFAULT_EXTENSION_CONFIG,
106
- permissionReviewLog: false,
107
- }),
108
- });
109
- const logger = new PermissionSessionLogger(deps);
110
-
111
- logger.review("permission.granted");
112
-
113
- expect(existsSync(join(tempDir, REVIEW_LOG_FILENAME))).toBe(false);
114
- expect(deps.notify).not.toHaveBeenCalled();
115
- });
116
- });
117
-
118
- // ── IO-failure warnings ───────────────────────────────────────────────────
119
-
120
- describe("IO-failure warnings", () => {
121
- it("calls notify with the error message when the logs directory cannot be created", () => {
122
- const deps = makeDeps({
123
- globalLogsDir: makeBlockedLogsDir(),
124
- getConfig: () => ({ ...DEFAULT_EXTENSION_CONFIG, debugLog: true }),
125
- });
126
- const logger = new PermissionSessionLogger(deps);
127
-
128
- logger.debug("test.event");
129
-
130
- expect(deps.notify).toHaveBeenCalledOnce();
131
- expect(deps.notify).toHaveBeenCalledWith(
132
- expect.stringContaining("Failed to"),
133
- );
134
- });
135
-
136
- it("deduplicates the same IO-failure warning across multiple writes", () => {
137
- const deps = makeDeps({
138
- globalLogsDir: makeBlockedLogsDir(),
139
- getConfig: () => ({ ...DEFAULT_EXTENSION_CONFIG, debugLog: true }),
140
- });
141
- const logger = new PermissionSessionLogger(deps);
142
-
143
- logger.debug("event.one");
144
- logger.debug("event.two");
145
-
146
- expect(deps.notify).toHaveBeenCalledOnce();
147
- });
148
-
149
- it("shares the dedup set across debug and review — same message notified only once", () => {
150
- const deps = makeDeps({
151
- globalLogsDir: makeBlockedLogsDir(),
152
- getConfig: () => ({
153
- ...DEFAULT_EXTENSION_CONFIG,
154
- debugLog: true,
155
- permissionReviewLog: true,
156
- }),
157
- });
158
- const logger = new PermissionSessionLogger(deps);
159
-
160
- logger.debug("event.one"); // emits warning
161
- logger.review("event.two"); // same error message → suppressed
162
-
163
- expect(deps.notify).toHaveBeenCalledOnce();
164
- });
165
- });
166
-
167
- // ── warn ──────────────────────────────────────────────────────────────────
168
-
169
- describe("warn", () => {
170
- it("calls notify with the message directly", () => {
171
- const deps = makeDeps();
172
- const logger = new PermissionSessionLogger(deps);
173
-
174
- logger.warn("Something went wrong");
175
-
176
- expect(deps.notify).toHaveBeenCalledWith("Something went wrong");
177
- });
178
-
179
- it("calls notify for every warn — not deduplicated", () => {
180
- const deps = makeDeps();
181
- const logger = new PermissionSessionLogger(deps);
182
-
183
- logger.warn("same message");
184
- logger.warn("same message");
185
-
186
- expect(deps.notify).toHaveBeenCalledTimes(2);
187
- });
188
-
189
- it("does not throw when notify is a no-op", () => {
190
- const deps: SessionLoggerDeps = {
191
- globalLogsDir: tempDir,
192
- getConfig: () => ({ ...DEFAULT_EXTENSION_CONFIG }),
193
- notify: () => {},
194
- };
195
- const logger = new PermissionSessionLogger(deps);
196
-
197
- expect(() => logger.warn("test")).not.toThrow();
198
- });
199
- });
200
- });
@@ -1,321 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
-
3
- import { evaluate } from "#src/rule";
4
- import { SessionApproval } from "#src/session-approval";
5
- import type { SessionApprovalRecorder } from "#src/session-approval-recorder";
6
- import { deriveApprovalPattern, SessionRules } from "#src/session-rules";
7
-
8
- // ── SessionRules ───────────────────────────────────────────────────────────
9
-
10
- describe("SessionRules", () => {
11
- describe("getRuleset", () => {
12
- it("returns an empty ruleset initially", () => {
13
- const rules = new SessionRules();
14
- expect(rules.getRuleset()).toEqual([]);
15
- });
16
-
17
- it("returns a ruleset containing approved rules", () => {
18
- const rules = new SessionRules();
19
- rules.approve("external_directory", "/other/project/*");
20
- expect(rules.getRuleset()).toEqual([
21
- {
22
- surface: "external_directory",
23
- pattern: "/other/project/*",
24
- action: "allow",
25
- layer: "session",
26
- origin: "session",
27
- },
28
- ]);
29
- });
30
-
31
- it("returns a defensive copy — mutations do not affect internal state", () => {
32
- const rules = new SessionRules();
33
- rules.approve("external_directory", "/other/project/*");
34
- const copy = rules.getRuleset();
35
- copy.push({
36
- surface: "bash",
37
- pattern: "*",
38
- action: "deny",
39
- origin: "session",
40
- });
41
- expect(rules.getRuleset()).toHaveLength(1);
42
- });
43
-
44
- it("accumulates multiple approved patterns", () => {
45
- const rules = new SessionRules();
46
- rules.approve("external_directory", "/project-a/*");
47
- rules.approve("external_directory", "/project-b/*");
48
- expect(rules.getRuleset()).toHaveLength(2);
49
- });
50
- });
51
-
52
- describe("clear", () => {
53
- it("removes all session rules", () => {
54
- const rules = new SessionRules();
55
- rules.approve("external_directory", "/other/project/*");
56
- rules.approve("external_directory", "/another/path/*");
57
- rules.clear();
58
- expect(rules.getRuleset()).toEqual([]);
59
- });
60
-
61
- it("allows new approvals after clearing", () => {
62
- const rules = new SessionRules();
63
- rules.approve("external_directory", "/old/path/*");
64
- rules.clear();
65
- rules.approve("external_directory", "/new/path/*");
66
- expect(rules.getRuleset()).toHaveLength(1);
67
- expect(rules.getRuleset()[0].pattern).toBe("/new/path/*");
68
- });
69
- });
70
-
71
- describe("recordSessionApproval", () => {
72
- it("satisfies the SessionApprovalRecorder interface", () => {
73
- const rules: SessionApprovalRecorder = new SessionRules();
74
- expect(typeof rules.recordSessionApproval).toBe("function");
75
- });
76
-
77
- it("records a single-pattern approval as one rule", () => {
78
- const rules = new SessionRules();
79
- rules.recordSessionApproval(SessionApproval.single("bash", "git *"));
80
- expect(rules.getRuleset()).toEqual([
81
- {
82
- surface: "bash",
83
- pattern: "git *",
84
- action: "allow",
85
- layer: "session",
86
- origin: "session",
87
- },
88
- ]);
89
- });
90
-
91
- it("records a multi-pattern approval as one rule per pattern", () => {
92
- const rules = new SessionRules();
93
- rules.recordSessionApproval(
94
- SessionApproval.multiple("external_directory", [
95
- "/outside/a/*",
96
- "/outside/b/*",
97
- ]),
98
- );
99
- expect(rules.getRuleset()).toHaveLength(2);
100
- expect(rules.getRuleset()[0].pattern).toBe("/outside/a/*");
101
- expect(rules.getRuleset()[1].pattern).toBe("/outside/b/*");
102
- });
103
-
104
- it("records each rule with the correct surface", () => {
105
- const rules = new SessionRules();
106
- rules.recordSessionApproval(
107
- SessionApproval.multiple("external_directory", [
108
- "/outside/a/*",
109
- "/outside/b/*",
110
- ]),
111
- );
112
- for (const rule of rules.getRuleset()) {
113
- expect(rule.surface).toBe("external_directory");
114
- }
115
- });
116
-
117
- it("records nothing for an empty patterns list", () => {
118
- const rules = new SessionRules();
119
- rules.recordSessionApproval(
120
- SessionApproval.multiple("external_directory", []),
121
- );
122
- expect(rules.getRuleset()).toEqual([]);
123
- });
124
- });
125
-
126
- describe("evaluate() integration", () => {
127
- it("returns allow for a path under an approved directory", () => {
128
- const session = new SessionRules();
129
- session.approve("external_directory", "/other/project/*");
130
- const result = evaluate(
131
- "external_directory",
132
- "/other/project/src/foo.ts",
133
- session.getRuleset(),
134
- "linux",
135
- );
136
- expect(result.action).toBe("allow");
137
- });
138
-
139
- it("returns ask (default) for a path outside approved directories", () => {
140
- const session = new SessionRules();
141
- session.approve("external_directory", "/other/project/*");
142
- const result = evaluate(
143
- "external_directory",
144
- "/other/unrelated/file.ts",
145
- session.getRuleset(),
146
- "linux",
147
- );
148
- // No rule matches — evaluate returns synthetic rule with default action "ask"
149
- expect(result.action).toBe("ask");
150
- });
151
-
152
- it("does not match a sibling directory that shares a string prefix", () => {
153
- const session = new SessionRules();
154
- session.approve("external_directory", "/other/project/*");
155
- const result = evaluate(
156
- "external_directory",
157
- "/other/project-b/foo.ts",
158
- session.getRuleset(),
159
- "linux",
160
- );
161
- expect(result.action).toBe("ask");
162
- });
163
-
164
- it("matches the directory itself (trailing slash)", () => {
165
- const session = new SessionRules();
166
- session.approve("external_directory", "/other/project/src/*");
167
- // The * in wildcardMatch maps to .* which matches zero chars — so /src/ is covered.
168
- const result = evaluate(
169
- "external_directory",
170
- "/other/project/src/",
171
- session.getRuleset(),
172
- "linux",
173
- );
174
- expect(result.action).toBe("allow");
175
- });
176
-
177
- it("handles multiple approved directories", () => {
178
- const session = new SessionRules();
179
- session.approve("external_directory", "/project-a/*");
180
- session.approve("external_directory", "/project-b/*");
181
- expect(
182
- evaluate(
183
- "external_directory",
184
- "/project-a/foo.ts",
185
- session.getRuleset(),
186
- "linux",
187
- ).action,
188
- ).toBe("allow");
189
- expect(
190
- evaluate(
191
- "external_directory",
192
- "/project-b/bar.ts",
193
- session.getRuleset(),
194
- "linux",
195
- ).action,
196
- ).toBe("allow");
197
- expect(
198
- evaluate(
199
- "external_directory",
200
- "/project-c/baz.ts",
201
- session.getRuleset(),
202
- "linux",
203
- ).action,
204
- ).toBe("ask");
205
- });
206
-
207
- it("does not match a different surface", () => {
208
- const session = new SessionRules();
209
- session.approve("external_directory", "/other/project/*");
210
- const result = evaluate(
211
- "bash",
212
- "/other/project/foo.ts",
213
- session.getRuleset(),
214
- "linux",
215
- );
216
- expect(result.action).toBe("ask");
217
- });
218
-
219
- it("returns allow after clearing and re-approving", () => {
220
- const session = new SessionRules();
221
- session.approve("external_directory", "/old/project/*");
222
- session.clear();
223
- session.approve("external_directory", "/new/project/*");
224
- expect(
225
- evaluate(
226
- "external_directory",
227
- "/old/project/file.ts",
228
- session.getRuleset(),
229
- "linux",
230
- ).action,
231
- ).toBe("ask");
232
- expect(
233
- evaluate(
234
- "external_directory",
235
- "/new/project/file.ts",
236
- session.getRuleset(),
237
- "linux",
238
- ).action,
239
- ).toBe("allow");
240
- });
241
- });
242
- });
243
-
244
- // ── deriveApprovalPattern ──────────────────────────────────────────────────
245
-
246
- describe("deriveApprovalPattern", () => {
247
- it("returns parent directory glob for a file path", () => {
248
- expect(deriveApprovalPattern("/other/project/src/foo.ts")).toBe(
249
- "/other/project/src/*",
250
- );
251
- });
252
-
253
- it("returns directory glob when path already ends with separator", () => {
254
- expect(deriveApprovalPattern("/other/project/src/")).toBe(
255
- "/other/project/src/*",
256
- );
257
- });
258
-
259
- it("returns parent directory glob for a directory-like path without trailing separator", () => {
260
- // Cannot distinguish dir from file — dirname is the safe choice
261
- expect(deriveApprovalPattern("/other/project/src")).toBe(
262
- "/other/project/*",
263
- );
264
- });
265
-
266
- it("handles root path", () => {
267
- expect(deriveApprovalPattern("/")).toBe("/*");
268
- });
269
-
270
- it("handles single-level path", () => {
271
- expect(deriveApprovalPattern("/foo")).toBe("/*");
272
- });
273
-
274
- it("produces a pattern that matches paths under the approved directory", () => {
275
- const pattern = deriveApprovalPattern("/other/project/src/foo.ts");
276
- const session = new SessionRules();
277
- session.approve("external_directory", pattern);
278
- expect(
279
- evaluate(
280
- "external_directory",
281
- "/other/project/src/bar.ts",
282
- session.getRuleset(),
283
- "linux",
284
- ).action,
285
- ).toBe("allow");
286
- });
287
-
288
- it("produces a pattern that does not match sibling directories", () => {
289
- const pattern = deriveApprovalPattern("/other/project/src/foo.ts");
290
- const session = new SessionRules();
291
- session.approve("external_directory", pattern);
292
- expect(
293
- evaluate(
294
- "external_directory",
295
- "/other/project/lib/bar.ts",
296
- session.getRuleset(),
297
- "linux",
298
- ).action,
299
- ).toBe("ask");
300
- });
301
-
302
- it("binds a current-directory file to the cwd subtree once resolved", () => {
303
- // Callers resolve the path to its canonical absolute form before deriving;
304
- // a current-directory file then yields the cwd glob and excludes siblings.
305
- const pattern = deriveApprovalPattern("/test/project/index.html");
306
- expect(pattern).toBe("/test/project/*");
307
- const session = new SessionRules();
308
- session.approve("edit", pattern);
309
- expect(
310
- evaluate(
311
- "edit",
312
- "/test/project/index.html",
313
- session.getRuleset(),
314
- "linux",
315
- ).action,
316
- ).toBe("allow");
317
- expect(
318
- evaluate("edit", "/etc/passwd", session.getRuleset(), "linux").action,
319
- ).toBe("ask");
320
- });
321
- });
@@ -1,112 +0,0 @@
1
- import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
2
- import { tmpdir } from "node:os";
3
- import { dirname, join } from "node:path";
4
- import { afterEach, beforeEach, describe, expect, test } from "vitest";
5
- import { getGlobalConfigPath } from "#src/config-paths";
6
- import { DEFAULT_EXTENSION_CONFIG } from "#src/extension-config";
7
- import piPermissionSystemExtension from "#src/index";
8
- import type { ScopeConfig } from "#src/types";
9
-
10
- type MockHandler = (
11
- event: Record<string, unknown>,
12
- ctx: Record<string, unknown>,
13
- ) =>
14
- | Promise<Record<string, unknown> | undefined>
15
- | Record<string, unknown>
16
- | undefined;
17
-
18
- describe("session_start handler consolidation", () => {
19
- let baseDir: string;
20
- let originalAgentDir: string | undefined;
21
- beforeEach(() => {
22
- baseDir = mkdtempSync(join(tmpdir(), "pi-permission-session-start-"));
23
- originalAgentDir = process.env.PI_CODING_AGENT_DIR;
24
-
25
- const globalConfigPath = getGlobalConfigPath(baseDir);
26
- mkdirSync(join(baseDir, "agents"), { recursive: true });
27
- mkdirSync(dirname(globalConfigPath), { recursive: true });
28
-
29
- const config: ScopeConfig = {
30
- permission: { "*": "ask" },
31
- };
32
- writeFileSync(
33
- globalConfigPath,
34
- `${JSON.stringify({ ...DEFAULT_EXTENSION_CONFIG, ...config }, null, 2)}\n`,
35
- "utf8",
36
- );
37
-
38
- process.env.PI_CODING_AGENT_DIR = baseDir;
39
- });
40
-
41
- afterEach(() => {
42
- if (originalAgentDir === undefined) {
43
- delete process.env.PI_CODING_AGENT_DIR;
44
- } else {
45
- process.env.PI_CODING_AGENT_DIR = originalAgentDir;
46
- }
47
- rmSync(baseDir, { recursive: true, force: true });
48
- });
49
-
50
- test("registers exactly one session_start handler", () => {
51
- const registrations: Array<{ name: string; handler: MockHandler }> = [];
52
-
53
- piPermissionSystemExtension({
54
- on: (name: string, handler: MockHandler): void => {
55
- registrations.push({ name, handler });
56
- },
57
- registerCommand: (): void => {},
58
- getAllTools: (): Array<{ name: string }> => [],
59
- getActiveTools: (): string[] => [],
60
- setActiveTools: (): void => {},
61
- registerProvider: (): void => {},
62
- events: {
63
- emit: (): void => {},
64
- on: (): (() => void) => () => undefined,
65
- },
66
- } as never);
67
-
68
- const sessionStartHandlers = registrations.filter(
69
- (r) => r.name === "session_start",
70
- );
71
- expect(sessionStartHandlers).toHaveLength(1);
72
- });
73
-
74
- test("session_start handler preserves lifecycle.reload debug log", async () => {
75
- const registrations: Array<{ name: string; handler: MockHandler }> = [];
76
-
77
- piPermissionSystemExtension({
78
- on: (name: string, handler: MockHandler): void => {
79
- registrations.push({ name, handler });
80
- },
81
- registerCommand: (): void => {},
82
- getAllTools: (): Array<{ name: string }> => [],
83
- getActiveTools: (): string[] => [],
84
- setActiveTools: (): void => {},
85
- registerProvider: (): void => {},
86
- events: {
87
- emit: (): void => {},
88
- on: (): (() => void) => () => undefined,
89
- },
90
- } as never);
91
-
92
- const sessionStartHandlers = registrations.filter(
93
- (r) => r.name === "session_start",
94
- );
95
-
96
- // The single handler should accept event with reason="reload" without throwing
97
- const mockCtx = {
98
- cwd: baseDir,
99
- ui: { select: async () => "", input: async () => "" },
100
- agent: { name: "test-agent" },
101
- sessionManager: {
102
- getEntries: () => [],
103
- addEntry: () => {},
104
- },
105
- };
106
-
107
- // Should not throw when called with a reload event
108
- await expect(
109
- sessionStartHandlers[0].handler({ reason: "reload" }, mockCtx),
110
- ).resolves.not.toThrow();
111
- });
112
- });