@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,192 +0,0 @@
1
- import { beforeEach, describe, expect, it, type Mock, vi } from "vitest";
2
- import type { AccessIntent } from "#src/access-intent/access-intent";
3
- import { PathNormalizer } from "#src/path-normalizer";
4
- import { LocalPermissionsService } from "#src/permissions-service";
5
- import type { ToolAccessExtractorRegistrar } from "#src/tool-access-extractor-registry";
6
- import type {
7
- ToolInputFormatter,
8
- ToolInputFormatterRegistrar,
9
- } from "#src/tool-input-formatter-registry";
10
- import type { PermissionCheckResult, PermissionState } from "#src/types";
11
-
12
- import { makeCheckResult } from "#test/helpers/handler-fixtures";
13
-
14
- // Mock node:fs so realpathSync (the canonical alias) is controllable.
15
- const realpathSync = vi.hoisted(() =>
16
- vi.fn<(path: string) => string>((p) => p),
17
- );
18
- vi.mock("node:fs", () => ({
19
- realpathSync,
20
- default: { realpathSync },
21
- }));
22
-
23
- // ── helpers ────────────────────────────────────────────────────────────────
24
-
25
- interface FakeResolver {
26
- resolve: Mock<(intent: AccessIntent) => PermissionCheckResult>;
27
- getToolPermission: Mock<
28
- (toolName: string, agentName?: string) => PermissionState
29
- >;
30
- }
31
-
32
- function makeResolver(): FakeResolver {
33
- return {
34
- resolve: vi
35
- .fn<(intent: AccessIntent) => PermissionCheckResult>()
36
- .mockReturnValue(makeCheckResult()),
37
- getToolPermission: vi
38
- .fn<(toolName: string, agentName?: string) => PermissionState>()
39
- .mockReturnValue("ask"),
40
- };
41
- }
42
-
43
- function makeFormatterRegistry(): ToolInputFormatterRegistrar {
44
- return {
45
- register: vi
46
- .fn<ToolInputFormatterRegistrar["register"]>()
47
- .mockReturnValue(vi.fn()),
48
- };
49
- }
50
-
51
- function makeAccessExtractorRegistry(): ToolAccessExtractorRegistrar {
52
- return {
53
- register: vi
54
- .fn<ToolAccessExtractorRegistrar["register"]>()
55
- .mockReturnValue(vi.fn()),
56
- };
57
- }
58
-
59
- function makeService(overrides?: {
60
- resolver?: FakeResolver;
61
- formatterRegistry?: ToolInputFormatterRegistrar;
62
- accessExtractorRegistry?: ToolAccessExtractorRegistrar;
63
- }) {
64
- const resolver = overrides?.resolver ?? makeResolver();
65
- // The published service always answers against the parent session's cwd.
66
- const session = { getPathNormalizer: () => normalizer };
67
- const formatterRegistry =
68
- overrides?.formatterRegistry ?? makeFormatterRegistry();
69
- const accessExtractorRegistry =
70
- overrides?.accessExtractorRegistry ?? makeAccessExtractorRegistry();
71
- const service = new LocalPermissionsService(
72
- resolver,
73
- session,
74
- formatterRegistry,
75
- accessExtractorRegistry,
76
- );
77
- return { service, resolver, formatterRegistry, accessExtractorRegistry };
78
- }
79
-
80
- const normalizer = new PathNormalizer("linux", "/test/project");
81
-
82
- // ── tests ──────────────────────────────────────────────────────────────────
83
-
84
- beforeEach(() => {
85
- realpathSync.mockReset();
86
- realpathSync.mockImplementation((p: string) => p);
87
- });
88
-
89
- describe("checkPermission", () => {
90
- it("resolves a non-path surface through a tool intent", () => {
91
- const { service, resolver } = makeService();
92
- service.checkPermission("bash", "echo hi", "my-agent");
93
- expect(resolver.resolve).toHaveBeenCalledWith({
94
- kind: "tool",
95
- surface: "bash",
96
- input: { command: "echo hi" },
97
- agentName: "my-agent",
98
- });
99
- });
100
-
101
- it("resolves an external_directory path query through an access-path intent matching the canonical alias", () => {
102
- realpathSync.mockImplementation((p: string) =>
103
- p === "/test/project/link" ? "/test/project/real" : p,
104
- );
105
- const { service, resolver } = makeService();
106
- service.checkPermission("external_directory", "link");
107
- expect(resolver.resolve).toHaveBeenCalledTimes(1);
108
- const intent = resolver.resolve.mock.calls[0][0];
109
- expect(intent.kind).toBe("access-path");
110
- if (intent.kind === "access-path") {
111
- expect(intent.surface).toBe("external_directory");
112
- expect(intent.path.matchValues()).toContain("/test/project/real");
113
- }
114
- });
115
-
116
- it("resolves a path-bearing tool query (read) through an access-path intent", () => {
117
- const { service, resolver } = makeService();
118
- service.checkPermission("read", "/test/project/.env");
119
- const intent = resolver.resolve.mock.calls[0][0];
120
- expect(intent.kind).toBe("access-path");
121
- if (intent.kind === "access-path") {
122
- expect(intent.surface).toBe("read");
123
- expect(intent.path.value()).toBe("/test/project/.env");
124
- }
125
- });
126
-
127
- it("falls back to a tool intent for a value-less path query", () => {
128
- const { service, resolver } = makeService();
129
- service.checkPermission("path");
130
- const intent = resolver.resolve.mock.calls[0][0];
131
- expect(intent.kind).toBe("tool");
132
- });
133
-
134
- it("returns the result from resolver.resolve", () => {
135
- const expected = makeCheckResult({ state: "deny", toolName: "bash" });
136
- const resolver = makeResolver();
137
- resolver.resolve.mockReturnValue(expected);
138
- const { service } = makeService({ resolver });
139
- const result = service.checkPermission("bash", "rm -rf /");
140
- expect(result).toBe(expected);
141
- });
142
- });
143
-
144
- describe("getToolPermission", () => {
145
- it("delegates to resolver.getToolPermission", () => {
146
- const resolver = makeResolver();
147
- resolver.getToolPermission.mockReturnValue("deny");
148
- const { service } = makeService({ resolver });
149
- const result = service.getToolPermission("write", "my-agent");
150
- expect(resolver.getToolPermission).toHaveBeenCalledWith(
151
- "write",
152
- "my-agent",
153
- );
154
- expect(result).toBe("deny");
155
- });
156
-
157
- it("omits agentName when not provided", () => {
158
- const { service, resolver } = makeService();
159
- service.getToolPermission("read");
160
- expect(resolver.getToolPermission).toHaveBeenCalledWith("read", undefined);
161
- });
162
- });
163
-
164
- describe("registerToolInputFormatter", () => {
165
- it("delegates to formatterRegistry.register and returns the unsubscribe function", () => {
166
- const unsub = vi.fn();
167
- const { service, formatterRegistry } = makeService();
168
- vi.mocked(formatterRegistry.register).mockReturnValue(unsub);
169
- const formatter: ToolInputFormatter = vi.fn();
170
- const result = service.registerToolInputFormatter("my-tool", formatter);
171
- expect(formatterRegistry.register).toHaveBeenCalledWith(
172
- "my-tool",
173
- formatter,
174
- );
175
- expect(result).toBe(unsub);
176
- });
177
- });
178
-
179
- describe("registerToolAccessExtractor", () => {
180
- it("delegates to accessExtractorRegistry.register and returns the unsubscribe function", () => {
181
- const unsub = vi.fn();
182
- const { service, accessExtractorRegistry } = makeService();
183
- vi.mocked(accessExtractorRegistry.register).mockReturnValue(unsub);
184
- const extractor = vi.fn();
185
- const result = service.registerToolAccessExtractor("ffgrep", extractor);
186
- expect(accessExtractorRegistry.register).toHaveBeenCalledWith(
187
- "ffgrep",
188
- extractor,
189
- );
190
- expect(result).toBe(unsub);
191
- });
192
- });
@@ -1,432 +0,0 @@
1
- import { homedir } from "node:os";
2
- import { join } from "node:path";
3
- import { beforeEach, describe, expect, test, vi } from "vitest";
4
-
5
- // Hoisted stub so the vi.mock factory can reference it.
6
- const { mockSpawnSync } = vi.hoisted(() => ({
7
- mockSpawnSync: vi.fn(),
8
- }));
9
-
10
- // Mock node:child_process so tests that exercise the subprocess fallback path
11
- // don't actually invoke npm. Default: subprocess fails (non-zero exit), so
12
- // tests focused on the walk-up strategy continue to expect null.
13
- vi.mock("node:child_process", () => ({
14
- spawnSync: mockSpawnSync,
15
- default: { spawnSync: mockSpawnSync },
16
- }));
17
-
18
- import { discoverGlobalNodeModulesRoot } from "#src/node-modules-discovery";
19
- import { isPiInfrastructureRead } from "#src/pi-infrastructure-read";
20
-
21
- // ── discoverGlobalNodeModulesRoot ──────────────────────────────────────────
22
-
23
- describe("discoverGlobalNodeModulesRoot", () => {
24
- beforeEach(() => {
25
- // Default: subprocess fails, so walk-up-focused tests see null for URLs
26
- // with no node_modules ancestor.
27
- mockSpawnSync.mockReset();
28
- mockSpawnSync.mockReturnValue({ status: 1, stdout: "" });
29
- });
30
-
31
- test("returns the node_modules dir when the file is inside one", () => {
32
- const url =
33
- "file:///opt/homebrew/lib/node_modules/pi-permission-system/dist/external-directory.js";
34
- expect(discoverGlobalNodeModulesRoot(url)).toBe(
35
- "/opt/homebrew/lib/node_modules",
36
- );
37
- });
38
-
39
- test("returns node_modules for a deeply nested file", () => {
40
- const url =
41
- "file:///home/user/.nvm/versions/node/v20/lib/node_modules/pi-permission-system/src/external-directory.js";
42
- expect(discoverGlobalNodeModulesRoot(url)).toBe(
43
- "/home/user/.nvm/versions/node/v20/lib/node_modules",
44
- );
45
- });
46
-
47
- test("returns node_modules for a bun global install path", () => {
48
- const url =
49
- "file:///home/user/.bun/install/global/node_modules/pi-permission-system/dist/external-directory.js";
50
- expect(discoverGlobalNodeModulesRoot(url)).toBe(
51
- "/home/user/.bun/install/global/node_modules",
52
- );
53
- });
54
-
55
- test("returns the innermost (closest-to-file) node_modules ancestor", () => {
56
- // The walk-up algorithm stops at the first node_modules dir it encounters,
57
- // which is the innermost one when the file is inside a nested install.
58
- // In practice this never happens for a real global install — the extension
59
- // is always directly at <global_root>/node_modules/pi-permission-system/…
60
- const url =
61
- "file:///opt/lib/node_modules/some-pkg/node_modules/pi-permission-system/dist/index.js";
62
- expect(discoverGlobalNodeModulesRoot(url)).toBe(
63
- "/opt/lib/node_modules/some-pkg/node_modules",
64
- );
65
- });
66
-
67
- test("returns null when the file is not inside any node_modules directory", () => {
68
- const url =
69
- "file:///home/user/development/pi-permission-system/dist/external-directory.js";
70
- expect(discoverGlobalNodeModulesRoot(url)).toBeNull();
71
- });
72
-
73
- test("returns null for a root-level file", () => {
74
- const url = "file:///external-directory.js";
75
- expect(discoverGlobalNodeModulesRoot(url)).toBeNull();
76
- });
77
-
78
- test("returns null for an invalid URL", () => {
79
- expect(discoverGlobalNodeModulesRoot("not-a-url")).toBeNull();
80
- });
81
-
82
- test("works with the real import.meta.url of this extension (smoke test)", () => {
83
- // The extension IS installed inside a node_modules tree when running in CI
84
- // or global install. In a local dev checkout the result may be null — that's
85
- // the documented graceful-degradation path.
86
- const result = discoverGlobalNodeModulesRoot();
87
- expect(result === null || result.endsWith("node_modules")).toBe(true);
88
- });
89
-
90
- test("the discovered path includes the pi-permission-system package directory", () => {
91
- const url =
92
- "file:///opt/homebrew/lib/node_modules/pi-permission-system/dist/external-directory.js";
93
- const root = discoverGlobalNodeModulesRoot(url);
94
- expect(root).not.toBeNull();
95
- expect(join(root!, "pi-permission-system")).toBe(
96
- "/opt/homebrew/lib/node_modules/pi-permission-system",
97
- );
98
- });
99
- });
100
-
101
- // ── isPiInfrastructureRead ─────────────────────────────────────────────────
102
-
103
- const INFRA_DIRS = [
104
- "/home/user/.pi/agent",
105
- "/home/user/.pi/agent/git",
106
- "/opt/homebrew/lib/node_modules",
107
- ];
108
- const CWD = "/home/user/project";
109
-
110
- describe("isPiInfrastructureRead", () => {
111
- // ── read tools allowed for infra paths ──────────────────────────────────
112
-
113
- test("allows 'read' tool for a file inside agentDir", () => {
114
- expect(
115
- isPiInfrastructureRead(
116
- "read",
117
- "/home/user/.pi/agent/extensions/pi-permission-system/config.json",
118
- INFRA_DIRS,
119
- CWD,
120
- "linux",
121
- ),
122
- ).toBe(true);
123
- });
124
-
125
- test("allows 'find' tool for a path inside node_modules infra dir", () => {
126
- expect(
127
- isPiInfrastructureRead(
128
- "find",
129
- "/opt/homebrew/lib/node_modules/pi-ask-user/skills",
130
- INFRA_DIRS,
131
- CWD,
132
- "linux",
133
- ),
134
- ).toBe(true);
135
- });
136
-
137
- test("allows 'grep' tool for a path inside agentDir/git", () => {
138
- expect(
139
- isPiInfrastructureRead(
140
- "grep",
141
- "/home/user/.pi/agent/git/some-package/README.md",
142
- INFRA_DIRS,
143
- CWD,
144
- "linux",
145
- ),
146
- ).toBe(true);
147
- });
148
-
149
- test("allows 'ls' tool for a path inside node_modules infra dir", () => {
150
- expect(
151
- isPiInfrastructureRead(
152
- "ls",
153
- "/opt/homebrew/lib/node_modules/pi-permission-system",
154
- INFRA_DIRS,
155
- CWD,
156
- "linux",
157
- ),
158
- ).toBe(true);
159
- });
160
-
161
- // ── write tools never allowed even for infra paths ───────────────────────
162
-
163
- test("blocks 'write' tool for a file inside agentDir", () => {
164
- expect(
165
- isPiInfrastructureRead(
166
- "write",
167
- "/home/user/.pi/agent/extensions/pi-permission-system/config.json",
168
- INFRA_DIRS,
169
- CWD,
170
- "linux",
171
- ),
172
- ).toBe(false);
173
- });
174
-
175
- test("blocks 'edit' tool for a file inside node_modules", () => {
176
- expect(
177
- isPiInfrastructureRead(
178
- "edit",
179
- "/opt/homebrew/lib/node_modules/pi-ask-user/skills/ask-user/SKILL.md",
180
- INFRA_DIRS,
181
- CWD,
182
- "linux",
183
- ),
184
- ).toBe(false);
185
- });
186
-
187
- test("blocks 'bash' tool regardless of path", () => {
188
- expect(
189
- isPiInfrastructureRead(
190
- "bash",
191
- "/opt/homebrew/lib/node_modules/pi-ask-user/SKILL.md",
192
- INFRA_DIRS,
193
- CWD,
194
- "linux",
195
- ),
196
- ).toBe(false);
197
- });
198
-
199
- // ── non-infra paths not allowed ──────────────────────────────────────────
200
-
201
- test("does not allow 'read' for a path outside all infra dirs", () => {
202
- expect(
203
- isPiInfrastructureRead("read", "/etc/passwd", INFRA_DIRS, CWD, "linux"),
204
- ).toBe(false);
205
- });
206
-
207
- test("does not allow 'read' for a path only partially matching an infra dir prefix", () => {
208
- // /home/user/.pi/agent-other should not match /home/user/.pi/agent
209
- expect(
210
- isPiInfrastructureRead(
211
- "read",
212
- "/home/user/.pi/agent-other/config.json",
213
- INFRA_DIRS,
214
- CWD,
215
- "linux",
216
- ),
217
- ).toBe(false);
218
- });
219
-
220
- // ── project-local Pi packages (.pi/npm, .pi/git) ─────────────────────────
221
-
222
- test("allows 'read' for a path inside project-local .pi/npm/", () => {
223
- expect(
224
- isPiInfrastructureRead(
225
- "read",
226
- `${CWD}/.pi/npm/node_modules/some-skill/SKILL.md`,
227
- INFRA_DIRS,
228
- CWD,
229
- "linux",
230
- ),
231
- ).toBe(true);
232
- });
233
-
234
- test("allows 'read' for a path inside project-local .pi/git/", () => {
235
- expect(
236
- isPiInfrastructureRead(
237
- "read",
238
- `${CWD}/.pi/git/github.com/org/skill-repo/SKILL.md`,
239
- INFRA_DIRS,
240
- CWD,
241
- "linux",
242
- ),
243
- ).toBe(true);
244
- });
245
-
246
- test("blocks 'write' for a path inside project-local .pi/npm/", () => {
247
- expect(
248
- isPiInfrastructureRead(
249
- "write",
250
- `${CWD}/.pi/npm/node_modules/some-skill/SKILL.md`,
251
- INFRA_DIRS,
252
- CWD,
253
- "linux",
254
- ),
255
- ).toBe(false);
256
- });
257
-
258
- // ── empty / edge cases ───────────────────────────────────────────────────
259
-
260
- test("returns false when infrastructureDirs is empty and path is not project-local", () => {
261
- expect(
262
- isPiInfrastructureRead("read", "/etc/passwd", [], CWD, "linux"),
263
- ).toBe(false);
264
- });
265
-
266
- test("returns false when infrastructureDirs is empty but path IS project-local .pi/npm", () => {
267
- // Project-local paths are checked separately from the dirs array.
268
- expect(
269
- isPiInfrastructureRead(
270
- "read",
271
- `${CWD}/.pi/npm/node_modules/x/SKILL.md`,
272
- [],
273
- CWD,
274
- "linux",
275
- ),
276
- ).toBe(true);
277
- });
278
- });
279
-
280
- // ── isPiInfrastructureRead — glob patterns ─────────────────────────────────
281
-
282
- describe("isPiInfrastructureRead with glob patterns", () => {
283
- test("glob entry matches a versioned nested path", () => {
284
- expect(
285
- isPiInfrastructureRead(
286
- "read",
287
- "/opt/homebrew/Cellar/pi-coding-agent/0.74.0/libexec/lib/node_modules/@earendil-works/pi-coding-agent/SKILL.md",
288
- ["/opt/homebrew/*/@earendil-works/pi-coding-agent/*"],
289
- CWD,
290
- "linux",
291
- ),
292
- ).toBe(true);
293
- });
294
-
295
- test("** behaves the same as * (matches across path separators)", () => {
296
- expect(
297
- isPiInfrastructureRead(
298
- "read",
299
- "/opt/homebrew/Cellar/pi-coding-agent/0.74.0/libexec/lib/node_modules/@earendil-works/pi-coding-agent/SKILL.md",
300
- ["/opt/homebrew/**/@earendil-works/pi-coding-agent/**"],
301
- CWD,
302
- "linux",
303
- ),
304
- ).toBe(true);
305
- });
306
-
307
- test("glob entry does not match an unrelated path", () => {
308
- expect(
309
- isPiInfrastructureRead(
310
- "read",
311
- "/etc/passwd",
312
- ["/opt/homebrew/*/@earendil-works/pi-coding-agent/*"],
313
- CWD,
314
- "linux",
315
- ),
316
- ).toBe(false);
317
- });
318
-
319
- test("? matches exactly one character", () => {
320
- expect(
321
- isPiInfrastructureRead(
322
- "read",
323
- "/opt/homebrew/X/file.md",
324
- ["/opt/homebrew/?/file.md"],
325
- CWD,
326
- "linux",
327
- ),
328
- ).toBe(true);
329
- });
330
-
331
- test("? does not match multiple characters", () => {
332
- expect(
333
- isPiInfrastructureRead(
334
- "read",
335
- "/opt/homebrew/abc/file.md",
336
- ["/opt/homebrew/?/file.md"],
337
- CWD,
338
- "linux",
339
- ),
340
- ).toBe(false);
341
- });
342
-
343
- test("mixed array of plain dirs and glob patterns — both branches work", () => {
344
- const dirs = [
345
- "/home/user/.pi/agent",
346
- "/opt/homebrew/*/@earendil-works/pi-coding-agent/*",
347
- ];
348
- expect(
349
- isPiInfrastructureRead(
350
- "read",
351
- "/home/user/.pi/agent/config.json",
352
- dirs,
353
- CWD,
354
- "linux",
355
- ),
356
- ).toBe(true);
357
- expect(
358
- isPiInfrastructureRead(
359
- "read",
360
- "/opt/homebrew/Cellar/pi-coding-agent/0.74.0/libexec/lib/node_modules/@earendil-works/pi-coding-agent/SKILL.md",
361
- dirs,
362
- CWD,
363
- "linux",
364
- ),
365
- ).toBe(true);
366
- });
367
-
368
- test("plain entry with ~ prefix matches after home expansion", () => {
369
- const home = homedir();
370
- expect(
371
- isPiInfrastructureRead(
372
- "read",
373
- `${home}/.pi/agent/config.json`,
374
- ["~/.pi/agent"],
375
- CWD,
376
- "linux",
377
- ),
378
- ).toBe(true);
379
- });
380
-
381
- test("write tool with a glob-matching path is still rejected", () => {
382
- expect(
383
- isPiInfrastructureRead(
384
- "write",
385
- "/opt/homebrew/Cellar/pi-coding-agent/0.74.0/libexec/lib/node_modules/@earendil-works/pi-coding-agent/SKILL.md",
386
- ["/opt/homebrew/**/@earendil-works/pi-coding-agent/**"],
387
- CWD,
388
- "linux",
389
- ),
390
- ).toBe(false);
391
- });
392
- });
393
-
394
- // ── isPiInfrastructureRead — win32 case-insensitive matching ───────────────
395
-
396
- describe("isPiInfrastructureRead on win32", () => {
397
- test("plain infra dir matches a case-different path", () => {
398
- expect(
399
- isPiInfrastructureRead(
400
- "read",
401
- "c:\\users\\foo\\.pi\\agent\\config.json",
402
- ["C:\\Users\\Foo\\.pi\\agent"],
403
- "C:\\proj",
404
- "win32",
405
- ),
406
- ).toBe(true);
407
- });
408
-
409
- test("glob infra dir matches case-insensitively", () => {
410
- expect(
411
- isPiInfrastructureRead(
412
- "read",
413
- "c:\\users\\foo\\npm\\node_modules\\@earendil-works\\pi-coding-agent\\skill.md",
414
- ["C:\\Users\\Foo\\**\\pi-coding-agent\\**"],
415
- "C:\\proj",
416
- "win32",
417
- ),
418
- ).toBe(true);
419
- });
420
-
421
- test("rejects a path outside every infra dir", () => {
422
- expect(
423
- isPiInfrastructureRead(
424
- "read",
425
- "c:\\windows\\system32\\drivers\\etc\\hosts",
426
- ["C:\\Users\\Foo\\.pi\\agent"],
427
- "C:\\proj",
428
- "win32",
429
- ),
430
- ).toBe(false);
431
- });
432
- });