@komarspn/pi-permission-system 16.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +2234 -0
- package/LICENSE +21 -0
- package/README.md +158 -0
- package/config/config.example.json +39 -0
- package/package.json +82 -0
- package/schemas/permissions.schema.json +158 -0
- package/src/active-agent.ts +72 -0
- package/src/async-cache.ts +21 -0
- package/src/bash-arity.ts +210 -0
- package/src/builtin-tool-input-formatters.ts +82 -0
- package/src/canonicalize-path.ts +30 -0
- package/src/common.ts +121 -0
- package/src/config-loader.ts +432 -0
- package/src/config-modal.ts +259 -0
- package/src/config-paths.ts +47 -0
- package/src/config-reporter.ts +34 -0
- package/src/config-store.ts +222 -0
- package/src/decision-audit.ts +75 -0
- package/src/decision-reporter.ts +41 -0
- package/src/denial-messages.ts +232 -0
- package/src/expand-home.ts +28 -0
- package/src/extension-config.ts +79 -0
- package/src/extension-paths.ts +66 -0
- package/src/forwarded-permissions/io.ts +404 -0
- package/src/forwarded-permissions/permission-forwarder.ts +580 -0
- package/src/forwarding-manager.ts +74 -0
- package/src/gate-prompter.ts +12 -0
- package/src/handlers/before-agent-start.ts +94 -0
- package/src/handlers/gates/bash-command.ts +75 -0
- package/src/handlers/gates/bash-external-directory.ts +127 -0
- package/src/handlers/gates/bash-path-extractor.ts +15 -0
- package/src/handlers/gates/bash-path.ts +152 -0
- package/src/handlers/gates/bash-program.ts +1143 -0
- package/src/handlers/gates/bash-token-classification.ts +105 -0
- package/src/handlers/gates/candidate-check.ts +32 -0
- package/src/handlers/gates/descriptor.ts +81 -0
- package/src/handlers/gates/external-directory-messages.ts +20 -0
- package/src/handlers/gates/external-directory.ts +133 -0
- package/src/handlers/gates/helpers.ts +76 -0
- package/src/handlers/gates/path.ts +91 -0
- package/src/handlers/gates/runner.ts +186 -0
- package/src/handlers/gates/skill-input-gate-pipeline.ts +104 -0
- package/src/handlers/gates/skill-input.ts +46 -0
- package/src/handlers/gates/skill-read.ts +87 -0
- package/src/handlers/gates/tool-call-gate-pipeline.ts +129 -0
- package/src/handlers/gates/tool.ts +102 -0
- package/src/handlers/gates/types.ts +13 -0
- package/src/handlers/index.ts +3 -0
- package/src/handlers/lifecycle.ts +95 -0
- package/src/handlers/permission-gate-handler.ts +190 -0
- package/src/handlers/tool-call-boundary.ts +91 -0
- package/src/index.ts +225 -0
- package/src/input-normalizer.ts +157 -0
- package/src/logging.ts +113 -0
- package/src/mcp-targets.ts +170 -0
- package/src/node-modules-discovery.ts +76 -0
- package/src/normalize.ts +43 -0
- package/src/path-utils.ts +355 -0
- package/src/pattern-suggest.ts +132 -0
- package/src/permission-dialog.ts +138 -0
- package/src/permission-event-rpc.ts +223 -0
- package/src/permission-events.ts +266 -0
- package/src/permission-forwarding.ts +188 -0
- package/src/permission-gate.ts +94 -0
- package/src/permission-manager.ts +392 -0
- package/src/permission-merge.ts +32 -0
- package/src/permission-prompter.ts +142 -0
- package/src/permission-prompts.ts +93 -0
- package/src/permission-resolver.ts +109 -0
- package/src/permission-session.ts +189 -0
- package/src/permission-ui-prompt.ts +127 -0
- package/src/permissions-service.ts +63 -0
- package/src/persistent-approval-recorder.ts +139 -0
- package/src/policy-loader.ts +350 -0
- package/src/prompting-gateway.ts +104 -0
- package/src/rule.ts +188 -0
- package/src/scope-merge.ts +72 -0
- package/src/service-lifecycle.ts +49 -0
- package/src/service.ts +163 -0
- package/src/session-approval-recorder.ts +6 -0
- package/src/session-approval.ts +43 -0
- package/src/session-logger.ts +91 -0
- package/src/session-rules.ts +79 -0
- package/src/skill-prompt-sanitizer.ts +292 -0
- package/src/status.ts +35 -0
- package/src/subagent-context.ts +104 -0
- package/src/subagent-lifecycle-events.ts +72 -0
- package/src/subagent-registry.ts +105 -0
- package/src/synthesize.ts +92 -0
- package/src/system-prompt-sanitizer.ts +274 -0
- package/src/tool-access-extractor-registry.ts +68 -0
- package/src/tool-input-formatter-registry.ts +67 -0
- package/src/tool-input-preview.ts +34 -0
- package/src/tool-input-prompt-formatters.ts +63 -0
- package/src/tool-preview-formatter.ts +207 -0
- package/src/tool-registry.ts +148 -0
- package/src/types.ts +64 -0
- package/src/wildcard-matcher.ts +120 -0
- package/src/yolo-mode.ts +30 -0
- package/test/active-agent.test.ts +155 -0
- package/test/async-cache.test.ts +48 -0
- package/test/bash-arity.test.ts +144 -0
- package/test/bash-external-directory.test.ts +956 -0
- package/test/builtin-tool-input-formatters.test.ts +109 -0
- package/test/canonicalize-path.test.ts +93 -0
- package/test/common.test.ts +287 -0
- package/test/composition-root.test.ts +603 -0
- package/test/config-loader.test.ts +740 -0
- package/test/config-modal.test.ts +320 -0
- package/test/config-paths.test.ts +83 -0
- package/test/config-pipeline.test.ts +90 -0
- package/test/config-reporter.test.ts +147 -0
- package/test/config-store.test.ts +466 -0
- package/test/decision-audit.test.ts +72 -0
- package/test/decision-reporter.test.ts +112 -0
- package/test/denial-messages.test.ts +656 -0
- package/test/detect-permissive-bash-fallback.test.ts +56 -0
- package/test/expand-home.test.ts +93 -0
- package/test/extension-config.test.ts +129 -0
- package/test/extension-paths.test.ts +108 -0
- package/test/forwarded-permissions/io.test.ts +251 -0
- package/test/forwarding-manager.test.ts +194 -0
- package/test/handlers/before-agent-start.test.ts +317 -0
- package/test/handlers/external-directory-integration.test.ts +623 -0
- package/test/handlers/external-directory-session-dedup.test.ts +430 -0
- package/test/handlers/external-directory-symlink-acceptance.test.ts +149 -0
- package/test/handlers/gates/bash-command-metamorphic.test.ts +83 -0
- package/test/handlers/gates/bash-command.test.ts +191 -0
- package/test/handlers/gates/bash-external-directory.test.ts +269 -0
- package/test/handlers/gates/bash-path.test.ts +337 -0
- package/test/handlers/gates/bash-program.test.ts +410 -0
- package/test/handlers/gates/bash-token-classification.test.ts +241 -0
- package/test/handlers/gates/candidate-check.test.ts +52 -0
- package/test/handlers/gates/external-directory-messages.test.ts +61 -0
- package/test/handlers/gates/external-directory.test.ts +259 -0
- package/test/handlers/gates/helpers.test.ts +177 -0
- package/test/handlers/gates/path.test.ts +294 -0
- package/test/handlers/gates/runner.test.ts +447 -0
- package/test/handlers/gates/skill-input-gate-pipeline.test.ts +176 -0
- package/test/handlers/gates/skill-input.test.ts +131 -0
- package/test/handlers/gates/skill-read.test.ts +158 -0
- package/test/handlers/gates/tool-call-gate-pipeline.test.ts +252 -0
- package/test/handlers/gates/tool.test.ts +223 -0
- package/test/handlers/input-events.test.ts +168 -0
- package/test/handlers/input.test.ts +199 -0
- package/test/handlers/lifecycle.test.ts +221 -0
- package/test/handlers/tool-call-boundary.test.ts +145 -0
- package/test/handlers/tool-call-events.test.ts +277 -0
- package/test/handlers/tool-call.test.ts +395 -0
- package/test/handlers/validate-requested-tool.test.ts +92 -0
- package/test/helpers/gate-fixtures.ts +323 -0
- package/test/helpers/handler-fixtures.ts +335 -0
- package/test/helpers/make-fake-pi.ts +100 -0
- package/test/helpers/manager-harness.ts +112 -0
- package/test/helpers/session-fixtures.ts +204 -0
- package/test/input-normalizer.test.ts +367 -0
- package/test/logging.test.ts +51 -0
- package/test/mcp-targets.test.ts +233 -0
- package/test/node-modules-discovery.test.ts +97 -0
- package/test/normalize.test.ts +247 -0
- package/test/path-utils.test.ts +650 -0
- package/test/pattern-suggest.test.ts +248 -0
- package/test/permission-dialog.test.ts +241 -0
- package/test/permission-event-rpc.test.ts +541 -0
- package/test/permission-events.test.ts +402 -0
- package/test/permission-forwarder.test.ts +369 -0
- package/test/permission-forwarding.test.ts +315 -0
- package/test/permission-gate.test.ts +305 -0
- package/test/permission-manager-unified.test.ts +3368 -0
- package/test/permission-merge.test.ts +61 -0
- package/test/permission-prompter.test.ts +518 -0
- package/test/permission-prompts.test.ts +363 -0
- package/test/permission-resolver.test.ts +265 -0
- package/test/permission-session.test.ts +363 -0
- package/test/permission-ui-prompt.test.ts +146 -0
- package/test/permissions-service.test.ts +177 -0
- package/test/persistent-approval-recorder.test.ts +133 -0
- package/test/pi-infrastructure-read.test.ts +369 -0
- package/test/policy-loader.test.ts +561 -0
- package/test/prompting-gateway.test.ts +230 -0
- package/test/rule.test.ts +604 -0
- package/test/scope-merge.test.ts +116 -0
- package/test/service-lifecycle.test.ts +163 -0
- package/test/service.test.ts +308 -0
- package/test/session-approval.test.ts +75 -0
- package/test/session-logger.test.ts +200 -0
- package/test/session-rules.test.ts +304 -0
- package/test/session-start.test.ts +112 -0
- package/test/skill-prompt-sanitizer.test.ts +374 -0
- package/test/status.test.ts +10 -0
- package/test/subagent-context.test.ts +326 -0
- package/test/subagent-lifecycle-events.test.ts +132 -0
- package/test/subagent-registry.test.ts +145 -0
- package/test/synthesize.test.ts +300 -0
- package/test/system-prompt-sanitizer.test.ts +382 -0
- package/test/tool-access-extractor-registry.test.ts +77 -0
- package/test/tool-input-formatter-registry.test.ts +75 -0
- package/test/tool-input-preview.test.ts +129 -0
- package/test/tool-input-prompt-formatters.test.ts +115 -0
- package/test/tool-preview-formatter.test.ts +458 -0
- package/test/tool-registry.test.ts +197 -0
- package/test/wildcard-matcher.test.ts +424 -0
- package/test/yolo-mode.test.ts +188 -0
|
@@ -0,0 +1,200 @@
|
|
|
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
|
+
});
|
|
@@ -0,0 +1,304 @@
|
|
|
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
|
+
);
|
|
135
|
+
expect(result.action).toBe("allow");
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it("returns ask (default) for a path outside approved directories", () => {
|
|
139
|
+
const session = new SessionRules();
|
|
140
|
+
session.approve("external_directory", "/other/project/*");
|
|
141
|
+
const result = evaluate(
|
|
142
|
+
"external_directory",
|
|
143
|
+
"/other/unrelated/file.ts",
|
|
144
|
+
session.getRuleset(),
|
|
145
|
+
);
|
|
146
|
+
// No rule matches — evaluate returns synthetic rule with default action "ask"
|
|
147
|
+
expect(result.action).toBe("ask");
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it("does not match a sibling directory that shares a string prefix", () => {
|
|
151
|
+
const session = new SessionRules();
|
|
152
|
+
session.approve("external_directory", "/other/project/*");
|
|
153
|
+
const result = evaluate(
|
|
154
|
+
"external_directory",
|
|
155
|
+
"/other/project-b/foo.ts",
|
|
156
|
+
session.getRuleset(),
|
|
157
|
+
);
|
|
158
|
+
expect(result.action).toBe("ask");
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it("matches the directory itself (trailing slash)", () => {
|
|
162
|
+
const session = new SessionRules();
|
|
163
|
+
session.approve("external_directory", "/other/project/src/*");
|
|
164
|
+
// The * in wildcardMatch maps to .* which matches zero chars — so /src/ is covered.
|
|
165
|
+
const result = evaluate(
|
|
166
|
+
"external_directory",
|
|
167
|
+
"/other/project/src/",
|
|
168
|
+
session.getRuleset(),
|
|
169
|
+
);
|
|
170
|
+
expect(result.action).toBe("allow");
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it("handles multiple approved directories", () => {
|
|
174
|
+
const session = new SessionRules();
|
|
175
|
+
session.approve("external_directory", "/project-a/*");
|
|
176
|
+
session.approve("external_directory", "/project-b/*");
|
|
177
|
+
expect(
|
|
178
|
+
evaluate(
|
|
179
|
+
"external_directory",
|
|
180
|
+
"/project-a/foo.ts",
|
|
181
|
+
session.getRuleset(),
|
|
182
|
+
).action,
|
|
183
|
+
).toBe("allow");
|
|
184
|
+
expect(
|
|
185
|
+
evaluate(
|
|
186
|
+
"external_directory",
|
|
187
|
+
"/project-b/bar.ts",
|
|
188
|
+
session.getRuleset(),
|
|
189
|
+
).action,
|
|
190
|
+
).toBe("allow");
|
|
191
|
+
expect(
|
|
192
|
+
evaluate(
|
|
193
|
+
"external_directory",
|
|
194
|
+
"/project-c/baz.ts",
|
|
195
|
+
session.getRuleset(),
|
|
196
|
+
).action,
|
|
197
|
+
).toBe("ask");
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it("does not match a different surface", () => {
|
|
201
|
+
const session = new SessionRules();
|
|
202
|
+
session.approve("external_directory", "/other/project/*");
|
|
203
|
+
const result = evaluate(
|
|
204
|
+
"bash",
|
|
205
|
+
"/other/project/foo.ts",
|
|
206
|
+
session.getRuleset(),
|
|
207
|
+
);
|
|
208
|
+
expect(result.action).toBe("ask");
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
it("returns allow after clearing and re-approving", () => {
|
|
212
|
+
const session = new SessionRules();
|
|
213
|
+
session.approve("external_directory", "/old/project/*");
|
|
214
|
+
session.clear();
|
|
215
|
+
session.approve("external_directory", "/new/project/*");
|
|
216
|
+
expect(
|
|
217
|
+
evaluate(
|
|
218
|
+
"external_directory",
|
|
219
|
+
"/old/project/file.ts",
|
|
220
|
+
session.getRuleset(),
|
|
221
|
+
).action,
|
|
222
|
+
).toBe("ask");
|
|
223
|
+
expect(
|
|
224
|
+
evaluate(
|
|
225
|
+
"external_directory",
|
|
226
|
+
"/new/project/file.ts",
|
|
227
|
+
session.getRuleset(),
|
|
228
|
+
).action,
|
|
229
|
+
).toBe("allow");
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
// ── deriveApprovalPattern ──────────────────────────────────────────────────
|
|
235
|
+
|
|
236
|
+
describe("deriveApprovalPattern", () => {
|
|
237
|
+
it("returns parent directory glob for a file path", () => {
|
|
238
|
+
expect(deriveApprovalPattern("/other/project/src/foo.ts")).toBe(
|
|
239
|
+
"/other/project/src/*",
|
|
240
|
+
);
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
it("returns directory glob when path already ends with separator", () => {
|
|
244
|
+
expect(deriveApprovalPattern("/other/project/src/")).toBe(
|
|
245
|
+
"/other/project/src/*",
|
|
246
|
+
);
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
it("returns parent directory glob for a directory-like path without trailing separator", () => {
|
|
250
|
+
// Cannot distinguish dir from file — dirname is the safe choice
|
|
251
|
+
expect(deriveApprovalPattern("/other/project/src")).toBe(
|
|
252
|
+
"/other/project/*",
|
|
253
|
+
);
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
it("handles root path", () => {
|
|
257
|
+
expect(deriveApprovalPattern("/")).toBe("/*");
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
it("handles single-level path", () => {
|
|
261
|
+
expect(deriveApprovalPattern("/foo")).toBe("/*");
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
it("produces a pattern that matches paths under the approved directory", () => {
|
|
265
|
+
const pattern = deriveApprovalPattern("/other/project/src/foo.ts");
|
|
266
|
+
const session = new SessionRules();
|
|
267
|
+
session.approve("external_directory", pattern);
|
|
268
|
+
expect(
|
|
269
|
+
evaluate(
|
|
270
|
+
"external_directory",
|
|
271
|
+
"/other/project/src/bar.ts",
|
|
272
|
+
session.getRuleset(),
|
|
273
|
+
).action,
|
|
274
|
+
).toBe("allow");
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
it("produces a pattern that does not match sibling directories", () => {
|
|
278
|
+
const pattern = deriveApprovalPattern("/other/project/src/foo.ts");
|
|
279
|
+
const session = new SessionRules();
|
|
280
|
+
session.approve("external_directory", pattern);
|
|
281
|
+
expect(
|
|
282
|
+
evaluate(
|
|
283
|
+
"external_directory",
|
|
284
|
+
"/other/project/lib/bar.ts",
|
|
285
|
+
session.getRuleset(),
|
|
286
|
+
).action,
|
|
287
|
+
).toBe("ask");
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
it("binds a current-directory file to the cwd subtree once resolved", () => {
|
|
291
|
+
// Callers resolve the path to its canonical absolute form before deriving;
|
|
292
|
+
// a current-directory file then yields the cwd glob and excludes siblings.
|
|
293
|
+
const pattern = deriveApprovalPattern("/test/project/index.html");
|
|
294
|
+
expect(pattern).toBe("/test/project/*");
|
|
295
|
+
const session = new SessionRules();
|
|
296
|
+
session.approve("edit", pattern);
|
|
297
|
+
expect(
|
|
298
|
+
evaluate("edit", "/test/project/index.html", session.getRuleset()).action,
|
|
299
|
+
).toBe("allow");
|
|
300
|
+
expect(evaluate("edit", "/etc/passwd", session.getRuleset()).action).toBe(
|
|
301
|
+
"ask",
|
|
302
|
+
);
|
|
303
|
+
});
|
|
304
|
+
});
|
|
@@ -0,0 +1,112 @@
|
|
|
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
|
+
});
|