@gotgenes/pi-permission-system 18.1.0 → 18.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (132) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/docs/assets/logo.png +0 -0
  3. package/docs/assets/logo.svg +213 -0
  4. package/docs/configuration.md +700 -0
  5. package/docs/cross-extension-api.md +525 -0
  6. package/docs/guides/permission-frontmatter-for-subagent-extensions.md +201 -0
  7. package/docs/guides/upstream-issue-template.md +113 -0
  8. package/docs/migration/legacy-to-flat.md +365 -0
  9. package/docs/opencode-compatibility.md +213 -0
  10. package/docs/session-approvals.md +68 -0
  11. package/docs/subagent-integration.md +102 -0
  12. package/docs/troubleshooting.md +53 -0
  13. package/package.json +5 -2
  14. package/src/access-intent/access-path.ts +29 -4
  15. package/src/access-intent/bash/bash-path-resolver.ts +39 -18
  16. package/src/access-intent/bash/msys-bash-tokens.ts +64 -0
  17. package/src/path-normalizer.ts +105 -2
  18. package/test/access-intent/access-path.test.ts +0 -277
  19. package/test/access-intent/bash/node-text.test.ts +0 -148
  20. package/test/access-intent/bash/parser.test.ts +0 -19
  21. package/test/access-intent/bash/program.test.ts +0 -673
  22. package/test/access-intent/bash/token-classification.test.ts +0 -363
  23. package/test/access-intent/bash/token-collection.test.ts +0 -300
  24. package/test/active-agent.test.ts +0 -155
  25. package/test/async-cache.test.ts +0 -48
  26. package/test/bash-arity.test.ts +0 -144
  27. package/test/bash-external-directory.test.ts +0 -1022
  28. package/test/builtin-tool-input-formatters.test.ts +0 -109
  29. package/test/canonicalize-path.test.ts +0 -119
  30. package/test/composition-root.test.ts +0 -698
  31. package/test/config-loader.test.ts +0 -740
  32. package/test/config-modal.test.ts +0 -320
  33. package/test/config-paths.test.ts +0 -83
  34. package/test/config-pipeline.test.ts +0 -90
  35. package/test/config-reporter.test.ts +0 -147
  36. package/test/config-store.test.ts +0 -466
  37. package/test/decision-audit.test.ts +0 -72
  38. package/test/decision-reporter.test.ts +0 -112
  39. package/test/denial-messages.test.ts +0 -714
  40. package/test/detect-permissive-bash-fallback.test.ts +0 -56
  41. package/test/expand-home.test.ts +0 -93
  42. package/test/extension-config.test.ts +0 -129
  43. package/test/extension-paths.test.ts +0 -108
  44. package/test/forwarded-permissions/io.test.ts +0 -251
  45. package/test/forwarding-manager.test.ts +0 -200
  46. package/test/handlers/before-agent-start.test.ts +0 -314
  47. package/test/handlers/external-directory-integration.test.ts +0 -515
  48. package/test/handlers/external-directory-session-dedup.test.ts +0 -175
  49. package/test/handlers/external-directory-symlink-acceptance.test.ts +0 -167
  50. package/test/handlers/gates/bash-command-metamorphic.test.ts +0 -88
  51. package/test/handlers/gates/bash-command.test.ts +0 -257
  52. package/test/handlers/gates/bash-external-directory.test.ts +0 -268
  53. package/test/handlers/gates/bash-path.test.ts +0 -346
  54. package/test/handlers/gates/candidate-check.test.ts +0 -52
  55. package/test/handlers/gates/external-directory-messages.test.ts +0 -85
  56. package/test/handlers/gates/external-directory-policy.test.ts +0 -134
  57. package/test/handlers/gates/external-directory.test.ts +0 -267
  58. package/test/handlers/gates/helpers.test.ts +0 -165
  59. package/test/handlers/gates/path.test.ts +0 -369
  60. package/test/handlers/gates/runner.test.ts +0 -408
  61. package/test/handlers/gates/skill-input-gate-pipeline.test.ts +0 -176
  62. package/test/handlers/gates/skill-input.test.ts +0 -128
  63. package/test/handlers/gates/skill-read.test.ts +0 -161
  64. package/test/handlers/gates/tool-call-gate-pipeline.test.ts +0 -356
  65. package/test/handlers/gates/tool.test.ts +0 -244
  66. package/test/handlers/input-events.test.ts +0 -168
  67. package/test/handlers/input.test.ts +0 -199
  68. package/test/handlers/lifecycle.test.ts +0 -221
  69. package/test/handlers/tool-call-boundary.test.ts +0 -145
  70. package/test/handlers/tool-call-events.test.ts +0 -277
  71. package/test/handlers/tool-call.test.ts +0 -395
  72. package/test/handlers/validate-requested-tool.test.ts +0 -92
  73. package/test/helpers/external-directory-fixtures.ts +0 -269
  74. package/test/helpers/gate-fixtures.ts +0 -316
  75. package/test/helpers/handler-fixtures.ts +0 -335
  76. package/test/helpers/make-fake-pi.ts +0 -100
  77. package/test/helpers/manager-harness.ts +0 -112
  78. package/test/helpers/session-fixtures.ts +0 -199
  79. package/test/input-normalizer.test.ts +0 -325
  80. package/test/logging.test.ts +0 -51
  81. package/test/mcp-targets.test.ts +0 -233
  82. package/test/node-modules-discovery.test.ts +0 -97
  83. package/test/normalize.test.ts +0 -247
  84. package/test/path-containment.test.ts +0 -161
  85. package/test/path-normalization.test.ts +0 -233
  86. package/test/path-normalizer.test.ts +0 -196
  87. package/test/path-surfaces.test.ts +0 -55
  88. package/test/pattern-suggest.test.ts +0 -248
  89. package/test/permission-dialog.test.ts +0 -205
  90. package/test/permission-event-rpc.test.ts +0 -560
  91. package/test/permission-events.test.ts +0 -400
  92. package/test/permission-forwarder.test.ts +0 -370
  93. package/test/permission-forwarding.test.ts +0 -315
  94. package/test/permission-gate.test.ts +0 -269
  95. package/test/permission-manager-unified.test.ts +0 -3714
  96. package/test/permission-merge.test.ts +0 -61
  97. package/test/permission-prompter.test.ts +0 -518
  98. package/test/permission-prompts.test.ts +0 -363
  99. package/test/permission-resolver.test.ts +0 -277
  100. package/test/permission-session.test.ts +0 -404
  101. package/test/permission-ui-prompt.test.ts +0 -146
  102. package/test/permissions-service.test.ts +0 -192
  103. package/test/pi-infrastructure-read.test.ts +0 -432
  104. package/test/policy-loader.test.ts +0 -561
  105. package/test/prompting-gateway.test.ts +0 -231
  106. package/test/rule.test.ts +0 -650
  107. package/test/safe-system-paths.test.ts +0 -46
  108. package/test/scope-merge.test.ts +0 -116
  109. package/test/service-lifecycle.test.ts +0 -163
  110. package/test/service.test.ts +0 -261
  111. package/test/session-approval.test.ts +0 -75
  112. package/test/session-logger.test.ts +0 -200
  113. package/test/session-rules.test.ts +0 -321
  114. package/test/session-start.test.ts +0 -112
  115. package/test/skill-prompt-sanitizer.test.ts +0 -418
  116. package/test/status.test.ts +0 -10
  117. package/test/subagent-context.test.ts +0 -372
  118. package/test/subagent-lifecycle-events.test.ts +0 -132
  119. package/test/subagent-registry.test.ts +0 -145
  120. package/test/synthesize.test.ts +0 -302
  121. package/test/system-prompt-sanitizer.test.ts +0 -382
  122. package/test/tool-access-extractor-registry.test.ts +0 -77
  123. package/test/tool-input-formatter-registry.test.ts +0 -75
  124. package/test/tool-input-path.test.ts +0 -84
  125. package/test/tool-input-preview.test.ts +0 -129
  126. package/test/tool-input-prompt-formatters.test.ts +0 -115
  127. package/test/tool-preview-formatter.test.ts +0 -458
  128. package/test/tool-registry.test.ts +0 -197
  129. package/test/value-guards.test.ts +0 -193
  130. package/test/wildcard-matcher.test.ts +0 -424
  131. package/test/yaml-frontmatter.test.ts +0 -91
  132. package/test/yolo-mode.test.ts +0 -188
@@ -1,199 +0,0 @@
1
- /**
2
- * Shared real-instance test fixtures for PermissionSession and
3
- * PermissionResolver.
4
- *
5
- * Use these instead of hand-rolling per-file mock intersection types.
6
- * Build a real PermissionSession from small per-collaborator fakes so tests
7
- * assert against actual behavior rather than mock contracts.
8
- *
9
- * Note: tests that exercise `resolveAgentName` must mock `active-agent` in
10
- * their own file (the vi.hoisted / vi.mock pattern from permission-session.test.ts)
11
- * since that mock is module-scoped.
12
- */
13
- import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
14
- import { vi } from "vitest";
15
-
16
- import type { ResolvedAccessIntent } from "#src/access-intent/access-intent";
17
- import type { SessionConfigStore } from "#src/config-store";
18
- import { DEFAULT_EXTENSION_CONFIG } from "#src/extension-config";
19
- import type { ExtensionPaths } from "#src/extension-paths";
20
- import type { ForwardingController } from "#src/forwarding-manager";
21
- import type { ScopedPermissionManager } from "#src/permission-manager";
22
- import { PermissionResolver } from "#src/permission-resolver";
23
- import { PermissionSession } from "#src/permission-session";
24
- import type { PromptingGatewayLifecycle } from "#src/prompting-gateway";
25
- import type { Ruleset } from "#src/rule";
26
- import type { SessionLogger } from "#src/session-logger";
27
- import { SessionRules } from "#src/session-rules";
28
- import type {
29
- PathRuleTokenMatcher,
30
- PermissionCheckResult,
31
- PermissionState,
32
- } from "#src/types";
33
-
34
- // ── Per-collaborator fake factories ────────────────────────────────────────
35
-
36
- export function makePaths(
37
- overrides: Partial<ExtensionPaths> = {},
38
- ): ExtensionPaths {
39
- return {
40
- agentDir: "/test/agent",
41
- sessionsDir: "/test/agent/sessions",
42
- subagentSessionsDir: "/test/agent/subagent-sessions",
43
- forwardingDir: "/test/agent/sessions/permission-forwarding",
44
- globalLogsDir: "/test/agent/logs",
45
- piInfrastructureDirs: ["/test/agent", "/test/agent/git"],
46
- ...overrides,
47
- };
48
- }
49
-
50
- export function makeLogger(): SessionLogger {
51
- return {
52
- debug: vi.fn(),
53
- review: vi.fn(),
54
- warn: vi.fn(),
55
- };
56
- }
57
-
58
- export function makeConfigStore(
59
- overrides: Partial<SessionConfigStore> = {},
60
- ): SessionConfigStore {
61
- return {
62
- current:
63
- overrides.current ??
64
- vi
65
- .fn<() => typeof DEFAULT_EXTENSION_CONFIG>()
66
- .mockReturnValue({ ...DEFAULT_EXTENSION_CONFIG }),
67
- refresh: overrides.refresh ?? vi.fn<(ctx?: ExtensionContext) => void>(),
68
- logResolvedPaths: overrides.logResolvedPaths ?? vi.fn<() => void>(),
69
- };
70
- }
71
-
72
- export function makeGateway(): PromptingGatewayLifecycle {
73
- return {
74
- activate: vi.fn<PromptingGatewayLifecycle["activate"]>(),
75
- deactivate: vi.fn<PromptingGatewayLifecycle["deactivate"]>(),
76
- };
77
- }
78
-
79
- export function makeForwarding(): ForwardingController {
80
- return {
81
- start: vi.fn(),
82
- stop: vi.fn(),
83
- };
84
- }
85
-
86
- /**
87
- * Fake `ScopedPermissionManager` with vi.fn() stubs.
88
- *
89
- * Return type is intentionally unannotated so callers retain full `vi.fn()`
90
- * mock access (`mock.calls`, `toHaveBeenCalledWith`, `mockReturnValue`, etc.).
91
- */
92
- export function makeFakePermissionManager() {
93
- return {
94
- configureForCwd: vi.fn<(cwd: string | undefined | null) => void>(),
95
- check: vi
96
- .fn<
97
- (
98
- intent: ResolvedAccessIntent,
99
- sessionRules?: Ruleset,
100
- ) => PermissionCheckResult
101
- >()
102
- .mockReturnValue({
103
- state: "allow",
104
- toolName: "read",
105
- source: "tool",
106
- origin: "builtin",
107
- }),
108
- getToolPermission: vi
109
- .fn<(toolName: string, agentName?: string) => PermissionState>()
110
- .mockReturnValue("allow"),
111
- getConfigIssues: vi.fn((): string[] => []),
112
- getPromotablePathTokenMatcher: vi
113
- .fn<(agentName?: string) => PathRuleTokenMatcher>()
114
- .mockReturnValue(() => false),
115
- };
116
- }
117
-
118
- // ── Real-instance factories ────────────────────────────────────────────────
119
-
120
- /**
121
- * Build a real PermissionSession from per-collaborator fakes.
122
- *
123
- * Returns the session and every collaborator so callers can destructure only
124
- * what they need and assert against collaborator spies directly.
125
- * The `permissionManager` is a `makeFakePermissionManager()` result unless
126
- * the caller passes an explicit `ScopedPermissionManager`.
127
- */
128
- export function makeRealSession(overrides?: {
129
- paths?: Partial<ExtensionPaths>;
130
- logger?: SessionLogger;
131
- forwarding?: ForwardingController;
132
- permissionManager?: ScopedPermissionManager;
133
- sessionRules?: SessionRules;
134
- configStore?: SessionConfigStore;
135
- gateway?: PromptingGatewayLifecycle;
136
- platform?: NodeJS.Platform;
137
- }): {
138
- session: PermissionSession;
139
- paths: ExtensionPaths;
140
- logger: SessionLogger;
141
- forwarding: ForwardingController;
142
- permissionManager: ReturnType<typeof makeFakePermissionManager>;
143
- sessionRules: SessionRules;
144
- configStore: SessionConfigStore;
145
- gateway: PromptingGatewayLifecycle;
146
- } {
147
- const paths = makePaths(overrides?.paths);
148
- const logger = overrides?.logger ?? makeLogger();
149
- const forwarding = overrides?.forwarding ?? makeForwarding();
150
- const permissionManager =
151
- (overrides?.permissionManager as
152
- | ReturnType<typeof makeFakePermissionManager>
153
- | undefined) ?? makeFakePermissionManager();
154
- const sessionRules = overrides?.sessionRules ?? new SessionRules();
155
- const configStore = overrides?.configStore ?? makeConfigStore();
156
- const gateway = overrides?.gateway ?? makeGateway();
157
- const platform = overrides?.platform ?? process.platform;
158
- const session = new PermissionSession(
159
- paths,
160
- forwarding,
161
- permissionManager,
162
- sessionRules,
163
- configStore,
164
- gateway,
165
- platform,
166
- );
167
- return {
168
- session,
169
- paths,
170
- logger,
171
- forwarding,
172
- permissionManager,
173
- sessionRules,
174
- configStore,
175
- gateway,
176
- };
177
- }
178
-
179
- /**
180
- * Build a real PermissionResolver from a fake manager and a SessionRules
181
- * instance.
182
- *
183
- * When called with no arguments, creates a fresh fake manager and fresh
184
- * SessionRules. Pass shared instances to connect the resolver to the same
185
- * manager/rules used by a real session.
186
- */
187
- export function makeRealResolver(
188
- manager?: ReturnType<typeof makeFakePermissionManager>,
189
- sessionRules?: SessionRules,
190
- ): {
191
- resolver: PermissionResolver;
192
- manager: ReturnType<typeof makeFakePermissionManager>;
193
- sessionRules: SessionRules;
194
- } {
195
- const resolvedManager = manager ?? makeFakePermissionManager();
196
- const resolvedRules = sessionRules ?? new SessionRules();
197
- const resolver = new PermissionResolver(resolvedManager, resolvedRules);
198
- return { resolver, manager: resolvedManager, sessionRules: resolvedRules };
199
- }
@@ -1,325 +0,0 @@
1
- import { afterEach, describe, expect, it, vi } from "vitest";
2
-
3
- const mockHomedir = vi.hoisted(() => vi.fn(() => "/mock/home"));
4
-
5
- vi.mock("node:os", () => ({
6
- homedir: mockHomedir,
7
- default: { homedir: mockHomedir },
8
- }));
9
-
10
- // Mock node:fs so realpathSync (used by the canonical alias) is controllable.
11
- // Default implementation is identity — lexical tests are unaffected.
12
- const realpathSync = vi.hoisted(() =>
13
- vi.fn<(path: string) => string>((p) => p),
14
- );
15
- vi.mock("node:fs", () => ({
16
- realpathSync,
17
- default: { realpathSync },
18
- }));
19
-
20
- import {
21
- buildAccessIntentForSurface,
22
- normalizeInput,
23
- } from "#src/input-normalizer";
24
- import { createMcpPermissionTargets } from "#src/mcp-targets";
25
- import { PathNormalizer } from "#src/path-normalizer";
26
-
27
- afterEach(() => {
28
- mockHomedir.mockClear();
29
- realpathSync.mockReset();
30
- realpathSync.mockImplementation((p: string) => p);
31
- });
32
-
33
- describe("normalizeInput — non-MCP surfaces", () => {
34
- // Path-bearing and special surfaces no longer derive path lookup values
35
- // through normalizeInput — that is now done by the access-path gate (#502)
36
- // and the service/RPC builder (#503). normalizeInput's tool branch collapses
37
- // every path-bearing or special surface to the catch-all ["*"] exactly as it
38
- // does for any unrecognised extension tool.
39
- describe("path-bearing and special surfaces collapse to '*'", () => {
40
- it("path surface ignores input.path and returns ['*']", () => {
41
- // After #504 removal: path no longer has a special branch.
42
- const result = normalizeInput("path", { path: ".env" }, []);
43
- expect(result.surface).toBe("path");
44
- expect(result.values).toEqual(["*"]);
45
- expect(result.resultExtras).toEqual({});
46
- });
47
-
48
- it("external_directory surface ignores input.path and returns ['*']", () => {
49
- const result = normalizeInput(
50
- "external_directory",
51
- { path: "/other/project" },
52
- [],
53
- );
54
- expect(result.surface).toBe("external_directory");
55
- expect(result.values).toEqual(["*"]);
56
- expect(result.resultExtras).toEqual({});
57
- });
58
-
59
- it("read surface ignores input.path and returns ['*']", () => {
60
- const result = normalizeInput("read", { path: ".env" }, []);
61
- expect(result.surface).toBe("read");
62
- expect(result.values).toEqual(["*"]);
63
- expect(result.resultExtras).toEqual({});
64
- });
65
-
66
- it("missing path also returns ['*'] (unchanged fallback)", () => {
67
- for (const surface of [
68
- "path",
69
- "external_directory",
70
- "read",
71
- "write",
72
- "edit",
73
- "grep",
74
- "find",
75
- "ls",
76
- ]) {
77
- const result = normalizeInput(surface, {}, []);
78
- expect(result.values).toEqual(["*"]);
79
- }
80
- });
81
- });
82
-
83
- describe("skill", () => {
84
- it("uses skill name from input.name", () => {
85
- const result = normalizeInput("skill", { name: "librarian" }, []);
86
- expect(result.surface).toBe("skill");
87
- expect(result.values).toEqual(["librarian"]);
88
- expect(result.resultExtras).toEqual({});
89
- });
90
-
91
- it("falls back to '*' when name is missing", () => {
92
- const result = normalizeInput("skill", {}, []);
93
- expect(result.values).toEqual(["*"]);
94
- });
95
-
96
- it("falls back to '*' when name is not a string", () => {
97
- const result = normalizeInput("skill", { name: 99 }, []);
98
- expect(result.values).toEqual(["*"]);
99
- });
100
- });
101
-
102
- describe("bash", () => {
103
- it("uses command from input.command", () => {
104
- const result = normalizeInput("bash", { command: "git status" }, []);
105
- expect(result.surface).toBe("bash");
106
- expect(result.values).toEqual(["git status"]);
107
- expect(result.resultExtras).toEqual({ command: "git status" });
108
- });
109
-
110
- it("uses empty string when command is missing", () => {
111
- const result = normalizeInput("bash", {}, []);
112
- expect(result.values).toEqual([""]);
113
- expect(result.resultExtras).toEqual({ command: "" });
114
- });
115
-
116
- it("uses empty string when command is not a string", () => {
117
- const result = normalizeInput("bash", { command: 42 }, []);
118
- expect(result.values).toEqual([""]);
119
- expect(result.resultExtras).toEqual({ command: "" });
120
- });
121
-
122
- it("strips leading comment lines from values but keeps original in resultExtras", () => {
123
- const cmd = "# Check debug logs\nfind /home -path '*debug*' -type f";
124
- const result = normalizeInput("bash", { command: cmd }, []);
125
- expect(result.values).toEqual(["find /home -path '*debug*' -type f"]);
126
- expect(result.resultExtras).toEqual({ command: cmd });
127
- });
128
-
129
- it("strips multiple comment lines", () => {
130
- const cmd = "# Step 1\n# Step 2\ngit status --short";
131
- const result = normalizeInput("bash", { command: cmd }, []);
132
- expect(result.values).toEqual(["git status --short"]);
133
- });
134
-
135
- it("preserves command when no comment lines present", () => {
136
- const result = normalizeInput(
137
- "bash",
138
- { command: "grep -rn foo src/" },
139
- [],
140
- );
141
- expect(result.values).toEqual(["grep -rn foo src/"]);
142
- });
143
-
144
- it("falls back to original when all lines are comments", () => {
145
- const cmd = "# just a comment";
146
- const result = normalizeInput("bash", { command: cmd }, []);
147
- expect(result.values).toEqual(["# just a comment"]);
148
- });
149
- });
150
-
151
- describe("extension tools (non-path-bearing)", () => {
152
- it("uses '*' as the lookup value for extension tools", () => {
153
- const result = normalizeInput("my_extension_tool", { some: "input" }, []);
154
- expect(result.surface).toBe("my_extension_tool");
155
- expect(result.values).toEqual(["*"]);
156
- expect(result.resultExtras).toEqual({});
157
- });
158
-
159
- it("uses '*' even when extension tool has a path field", () => {
160
- const result = normalizeInput(
161
- "my_extension_tool",
162
- { path: "/some/path" },
163
- [],
164
- );
165
- expect(result.values).toEqual(["*"]);
166
- });
167
- });
168
- });
169
-
170
- describe("normalizeInput — MCP surface", () => {
171
- it("surface is 'mcp'", () => {
172
- const result = normalizeInput("mcp", { tool: "exa:search" }, []);
173
- expect(result.surface).toBe("mcp");
174
- });
175
-
176
- it("values end with the catch-all 'mcp' target", () => {
177
- const result = normalizeInput("mcp", { tool: "exa:search" }, []);
178
- expect(result.values.at(-1)).toBe("mcp");
179
- });
180
-
181
- it("values include specific targets before the catch-all for a qualified tool call", () => {
182
- const result = normalizeInput("mcp", { tool: "exa:search" }, []);
183
- expect(result.values).toContain("exa_search");
184
- expect(result.values).toContain("exa:search");
185
- expect(result.values).toContain("exa");
186
- expect(result.values).toContain("mcp_call");
187
- // 'mcp' is always last
188
- expect(result.values.at(-1)).toBe("mcp");
189
- });
190
-
191
- it("matches createMcpPermissionTargets output + 'mcp' appended", () => {
192
- const rawTargets = createMcpPermissionTargets({ tool: "exa:search" }, [
193
- "exa",
194
- ]);
195
- const result = normalizeInput("mcp", { tool: "exa:search" }, ["exa"]);
196
- expect(result.values).toEqual([...rawTargets, "mcp"]);
197
- });
198
-
199
- it("resultExtras.target is the first specific target (most-specific)", () => {
200
- const result = normalizeInput("mcp", { tool: "exa:search" }, []);
201
- expect(result.resultExtras.target).toBe(result.values[0]);
202
- });
203
-
204
- it("resultExtras.target is 'mcp' when no specific targets are derived", () => {
205
- // Empty input → only mcp_status then mcp appended
206
- const result = normalizeInput("mcp", {}, []);
207
- expect(result.resultExtras.target).toBe("mcp_status");
208
- });
209
-
210
- it("values contain no duplicates", () => {
211
- const result = normalizeInput("mcp", { tool: "exa:search" }, ["exa"]);
212
- const unique = [...new Set(result.values)];
213
- expect(result.values).toEqual(unique);
214
- });
215
-
216
- it("produces mcp_status + mcp for status input", () => {
217
- const result = normalizeInput("mcp", {}, []);
218
- expect(result.values).toEqual(["mcp_status", "mcp"]);
219
- });
220
-
221
- it("produces connect targets + mcp for connect input", () => {
222
- const result = normalizeInput("mcp", { connect: "exa" }, []);
223
- expect(result.values).toContain("mcp_connect_exa");
224
- expect(result.values).toContain("mcp_connect");
225
- expect(result.values.at(-1)).toBe("mcp");
226
- });
227
- });
228
-
229
- describe("buildAccessIntentForSurface", () => {
230
- const normalizer = new PathNormalizer("linux", "/test/project");
231
-
232
- it("emits an access-path intent carrying the canonical alias for the path surface", () => {
233
- realpathSync.mockImplementation((p: string) =>
234
- p === "/test/project/link" ? "/test/project/real" : p,
235
- );
236
- const intent = buildAccessIntentForSurface(
237
- "path",
238
- "link",
239
- normalizer,
240
- undefined,
241
- );
242
- expect(intent.kind).toBe("access-path");
243
- if (intent.kind === "access-path") {
244
- expect(intent.surface).toBe("path");
245
- expect(intent.path.matchValues()).toContain("/test/project/real");
246
- expect(intent.path.value()).toBe("/test/project/link");
247
- }
248
- });
249
-
250
- it("emits an access-path intent for the external_directory surface", () => {
251
- const intent = buildAccessIntentForSurface(
252
- "external_directory",
253
- "/outside/dir",
254
- normalizer,
255
- undefined,
256
- );
257
- expect(intent.kind).toBe("access-path");
258
- if (intent.kind === "access-path") {
259
- expect(intent.surface).toBe("external_directory");
260
- expect(intent.path.value()).toBe("/outside/dir");
261
- }
262
- });
263
-
264
- it("emits an access-path intent for a path-bearing tool surface (read)", () => {
265
- const intent = buildAccessIntentForSurface(
266
- "read",
267
- "/test/project/.env",
268
- normalizer,
269
- undefined,
270
- );
271
- expect(intent.kind).toBe("access-path");
272
- if (intent.kind === "access-path") {
273
- expect(intent.surface).toBe("read");
274
- expect(intent.path.value()).toBe("/test/project/.env");
275
- }
276
- });
277
-
278
- it("emits a tool intent for a non-path surface (bash)", () => {
279
- const intent = buildAccessIntentForSurface(
280
- "bash",
281
- "echo hi",
282
- normalizer,
283
- "my-agent",
284
- );
285
- expect(intent).toEqual({
286
- kind: "tool",
287
- surface: "bash",
288
- input: { command: "echo hi" },
289
- agentName: "my-agent",
290
- });
291
- });
292
-
293
- it("passes agentName through on the access-path branch", () => {
294
- const intent = buildAccessIntentForSurface(
295
- "path",
296
- "/some/file",
297
- normalizer,
298
- "Explore",
299
- );
300
- expect(intent.agentName).toBe("Explore");
301
- });
302
-
303
- it("falls back to a tool intent for a value-less path surface", () => {
304
- const intent = buildAccessIntentForSurface(
305
- "path",
306
- undefined,
307
- normalizer,
308
- undefined,
309
- );
310
- expect(intent.kind).toBe("tool");
311
- if (intent.kind === "tool") {
312
- expect(intent.surface).toBe("path");
313
- }
314
- });
315
-
316
- it("falls back to a tool intent for a whitespace-only path value", () => {
317
- const intent = buildAccessIntentForSurface(
318
- "external_directory",
319
- " ",
320
- normalizer,
321
- undefined,
322
- );
323
- expect(intent.kind).toBe("tool");
324
- });
325
- });
@@ -1,51 +0,0 @@
1
- import {
2
- existsSync,
3
- mkdirSync,
4
- mkdtempSync,
5
- readFileSync,
6
- rmSync,
7
- } from "node:fs";
8
- import { tmpdir } from "node:os";
9
- import { join } from "node:path";
10
- import { expect, test } from "vitest";
11
- import { DEFAULT_EXTENSION_CONFIG } from "#src/extension-config";
12
- import { createPermissionSystemLogger } from "#src/logging";
13
-
14
- test("Permission-system logger respects debug toggle and keeps review log enabled by default", () => {
15
- const baseDir = mkdtempSync(join(tmpdir(), "pi-permission-system-logs-"));
16
- const logsDir = join(baseDir, "logs");
17
- const debugLogPath = join(logsDir, "debug.jsonl");
18
- const reviewLogPath = join(logsDir, "review.jsonl");
19
- const config = { ...DEFAULT_EXTENSION_CONFIG };
20
- const logger = createPermissionSystemLogger({
21
- getConfig: () => config,
22
- debugLogPath,
23
- reviewLogPath,
24
- ensureLogsDirectory: () => {
25
- mkdirSync(logsDir, { recursive: true });
26
- return undefined;
27
- },
28
- });
29
-
30
- try {
31
- const initialDebugWarning = logger.debug("debug.disabled", {
32
- sample: true,
33
- });
34
- const reviewWarning = logger.review("permission_request.waiting", {
35
- toolName: "write",
36
- });
37
-
38
- expect(initialDebugWarning).toBe(undefined);
39
- expect(reviewWarning).toBe(undefined);
40
- expect(existsSync(debugLogPath)).toBe(false);
41
- expect(existsSync(reviewLogPath)).toBe(true);
42
-
43
- config.debugLog = true;
44
- const enabledDebugWarning = logger.debug("debug.enabled", { sample: true });
45
- expect(enabledDebugWarning).toBe(undefined);
46
- expect(existsSync(debugLogPath)).toBe(true);
47
- expect(readFileSync(debugLogPath, "utf8")).toMatch(/debug\.enabled/);
48
- } finally {
49
- rmSync(baseDir, { recursive: true, force: true });
50
- }
51
- });