@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,402 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-deprecated -- tests the deprecated RPC channel implementation */
|
|
2
|
+
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { dirname, join } from "node:path";
|
|
5
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
6
|
+
|
|
7
|
+
import { getGlobalConfigPath } from "#src/config-paths";
|
|
8
|
+
import piPermissionSystemExtension from "#src/index";
|
|
9
|
+
import type {
|
|
10
|
+
PermissionDecisionEvent,
|
|
11
|
+
PermissionsCheckReplyData,
|
|
12
|
+
PermissionsCheckRequest,
|
|
13
|
+
PermissionsPromptReplyData,
|
|
14
|
+
PermissionsPromptRequest,
|
|
15
|
+
PermissionsReadyEvent,
|
|
16
|
+
PermissionsRpcReply,
|
|
17
|
+
PermissionUiPromptEvent,
|
|
18
|
+
} from "#src/permission-events";
|
|
19
|
+
import {
|
|
20
|
+
emitDecisionEvent,
|
|
21
|
+
emitReadyEvent,
|
|
22
|
+
emitUiPromptEvent,
|
|
23
|
+
PERMISSIONS_DECISION_CHANNEL,
|
|
24
|
+
PERMISSIONS_PROTOCOL_VERSION,
|
|
25
|
+
PERMISSIONS_READY_CHANNEL,
|
|
26
|
+
PERMISSIONS_RPC_CHECK_CHANNEL,
|
|
27
|
+
PERMISSIONS_RPC_PROMPT_CHANNEL,
|
|
28
|
+
PERMISSIONS_UI_PROMPT_CHANNEL,
|
|
29
|
+
} from "#src/permission-events";
|
|
30
|
+
|
|
31
|
+
// ── Minimal EventBus stub ──────────────────────────────────────────────────
|
|
32
|
+
|
|
33
|
+
function makeEventBus() {
|
|
34
|
+
return {
|
|
35
|
+
emit: vi.fn(),
|
|
36
|
+
on: vi.fn().mockReturnValue(() => undefined),
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// ── Constants ──────────────────────────────────────────────────────────────
|
|
41
|
+
|
|
42
|
+
describe("constants", () => {
|
|
43
|
+
it("PERMISSIONS_PROTOCOL_VERSION is 1", () => {
|
|
44
|
+
expect(PERMISSIONS_PROTOCOL_VERSION).toBe(1);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("channel names have the correct values", () => {
|
|
48
|
+
expect(PERMISSIONS_READY_CHANNEL).toBe("permissions:ready");
|
|
49
|
+
expect(PERMISSIONS_UI_PROMPT_CHANNEL).toBe("permissions:ui_prompt");
|
|
50
|
+
expect(PERMISSIONS_DECISION_CHANNEL).toBe("permissions:decision");
|
|
51
|
+
expect(PERMISSIONS_RPC_CHECK_CHANNEL).toBe("permissions:rpc:check");
|
|
52
|
+
expect(PERMISSIONS_RPC_PROMPT_CHANNEL).toBe("permissions:rpc:prompt");
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
// ── emitReadyEvent ─────────────────────────────────────────────────────────
|
|
57
|
+
|
|
58
|
+
describe("emitReadyEvent", () => {
|
|
59
|
+
it("emits an empty payload on the permissions:ready channel", () => {
|
|
60
|
+
const bus = makeEventBus();
|
|
61
|
+
emitReadyEvent(bus);
|
|
62
|
+
expect(bus.emit).toHaveBeenCalledOnce();
|
|
63
|
+
expect(bus.emit).toHaveBeenCalledWith("permissions:ready", {});
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("carries no protocolVersion (version lives in the RPC envelope)", () => {
|
|
67
|
+
const bus = makeEventBus();
|
|
68
|
+
emitReadyEvent(bus);
|
|
69
|
+
const payload = bus.emit.mock.calls[0][1] as PermissionsReadyEvent;
|
|
70
|
+
expect(payload).not.toHaveProperty("protocolVersion");
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("swallows event bus errors because broadcasts are best-effort", () => {
|
|
74
|
+
const bus = {
|
|
75
|
+
emit: vi.fn(() => {
|
|
76
|
+
throw new Error("listener failed");
|
|
77
|
+
}),
|
|
78
|
+
on: vi.fn().mockReturnValue(() => undefined),
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
expect(() => emitReadyEvent(bus)).not.toThrow();
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
// ── emitUiPromptEvent ──────────────────────────────────────────────────────
|
|
86
|
+
|
|
87
|
+
describe("emitUiPromptEvent", () => {
|
|
88
|
+
function makeUiPromptEvent(
|
|
89
|
+
overrides: Partial<PermissionUiPromptEvent> = {},
|
|
90
|
+
): PermissionUiPromptEvent {
|
|
91
|
+
return {
|
|
92
|
+
requestId: "req-123",
|
|
93
|
+
source: "tool_call",
|
|
94
|
+
surface: "bash",
|
|
95
|
+
value: "git status",
|
|
96
|
+
agentName: "Explore",
|
|
97
|
+
message: "Allow git status?",
|
|
98
|
+
forwarding: null,
|
|
99
|
+
...overrides,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
it("emits on the permissions:ui_prompt channel", () => {
|
|
104
|
+
const bus = makeEventBus();
|
|
105
|
+
emitUiPromptEvent(bus, makeUiPromptEvent());
|
|
106
|
+
expect(bus.emit).toHaveBeenCalledOnce();
|
|
107
|
+
expect(bus.emit.mock.calls[0][0]).toBe("permissions:ui_prompt");
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("forwards the full payload unchanged", () => {
|
|
111
|
+
const bus = makeEventBus();
|
|
112
|
+
const event = makeUiPromptEvent({
|
|
113
|
+
forwarding: { requesterAgentName: "Worker", requesterSessionId: "child" },
|
|
114
|
+
});
|
|
115
|
+
emitUiPromptEvent(bus, event);
|
|
116
|
+
expect(bus.emit.mock.calls[0][1]).toEqual(event);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it("swallows event bus errors because UI prompt broadcasts are observational", () => {
|
|
120
|
+
const bus = {
|
|
121
|
+
emit: vi.fn(() => {
|
|
122
|
+
throw new Error("listener failed");
|
|
123
|
+
}),
|
|
124
|
+
on: vi.fn().mockReturnValue(() => undefined),
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
expect(() => emitUiPromptEvent(bus, makeUiPromptEvent())).not.toThrow();
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
// ── emitDecisionEvent ──────────────────────────────────────────────────────
|
|
132
|
+
|
|
133
|
+
describe("emitDecisionEvent", () => {
|
|
134
|
+
function makeDecisionEvent(
|
|
135
|
+
overrides: Partial<PermissionDecisionEvent> = {},
|
|
136
|
+
): PermissionDecisionEvent {
|
|
137
|
+
return {
|
|
138
|
+
surface: "bash",
|
|
139
|
+
value: "git status",
|
|
140
|
+
result: "allow",
|
|
141
|
+
resolution: "policy_allow",
|
|
142
|
+
origin: "global",
|
|
143
|
+
agentName: null,
|
|
144
|
+
matchedPattern: "*",
|
|
145
|
+
...overrides,
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
it("emits on the permissions:decision channel", () => {
|
|
150
|
+
const bus = makeEventBus();
|
|
151
|
+
emitDecisionEvent(bus, makeDecisionEvent());
|
|
152
|
+
expect(bus.emit).toHaveBeenCalledOnce();
|
|
153
|
+
expect(bus.emit.mock.calls[0][0]).toBe("permissions:decision");
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it("forwards the full payload unchanged", () => {
|
|
157
|
+
const bus = makeEventBus();
|
|
158
|
+
const event = makeDecisionEvent({
|
|
159
|
+
surface: "mcp",
|
|
160
|
+
value: "exa:search",
|
|
161
|
+
result: "deny",
|
|
162
|
+
resolution: "policy_deny",
|
|
163
|
+
origin: "project",
|
|
164
|
+
agentName: "Worker",
|
|
165
|
+
matchedPattern: "exa:*",
|
|
166
|
+
});
|
|
167
|
+
emitDecisionEvent(bus, event);
|
|
168
|
+
expect(bus.emit.mock.calls[0][1]).toEqual(event);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it("accepts all defined resolution values", () => {
|
|
172
|
+
const resolutions: PermissionDecisionEvent["resolution"][] = [
|
|
173
|
+
"policy_allow",
|
|
174
|
+
"policy_deny",
|
|
175
|
+
"session_approved",
|
|
176
|
+
"infrastructure_auto_allowed",
|
|
177
|
+
"user_approved",
|
|
178
|
+
"user_approved_for_session",
|
|
179
|
+
"user_approved_for_project",
|
|
180
|
+
"user_approved_globally",
|
|
181
|
+
"user_denied",
|
|
182
|
+
"auto_approved",
|
|
183
|
+
"confirmation_unavailable",
|
|
184
|
+
];
|
|
185
|
+
const bus = makeEventBus();
|
|
186
|
+
for (const resolution of resolutions) {
|
|
187
|
+
emitDecisionEvent(bus, makeDecisionEvent({ resolution }));
|
|
188
|
+
}
|
|
189
|
+
expect(bus.emit).toHaveBeenCalledTimes(resolutions.length);
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
it("accepts null for optional fields", () => {
|
|
193
|
+
const bus = makeEventBus();
|
|
194
|
+
emitDecisionEvent(
|
|
195
|
+
bus,
|
|
196
|
+
makeDecisionEvent({
|
|
197
|
+
origin: null,
|
|
198
|
+
agentName: null,
|
|
199
|
+
matchedPattern: null,
|
|
200
|
+
}),
|
|
201
|
+
);
|
|
202
|
+
const payload = bus.emit.mock.calls[0][1] as PermissionDecisionEvent;
|
|
203
|
+
expect(payload.origin).toBeNull();
|
|
204
|
+
expect(payload.agentName).toBeNull();
|
|
205
|
+
expect(payload.matchedPattern).toBeNull();
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
it("swallows event bus errors because broadcasts are best-effort", () => {
|
|
209
|
+
const bus = {
|
|
210
|
+
emit: vi.fn(() => {
|
|
211
|
+
throw new Error("listener failed");
|
|
212
|
+
}),
|
|
213
|
+
on: vi.fn().mockReturnValue(() => undefined),
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
expect(() => emitDecisionEvent(bus, makeDecisionEvent())).not.toThrow();
|
|
217
|
+
});
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
// ── Type-shape compile-time checks (runtime assertions on literal values) ──
|
|
221
|
+
|
|
222
|
+
describe("type shapes (PermissionsRpcReply)", () => {
|
|
223
|
+
it("success reply has success=true and protocolVersion", () => {
|
|
224
|
+
const reply: PermissionsRpcReply<{ result: "allow" }> = {
|
|
225
|
+
success: true,
|
|
226
|
+
protocolVersion: PERMISSIONS_PROTOCOL_VERSION,
|
|
227
|
+
data: { result: "allow" },
|
|
228
|
+
};
|
|
229
|
+
expect(reply.success).toBe(true);
|
|
230
|
+
expect(reply.protocolVersion).toBe(1);
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
it("error reply has success=false and error string", () => {
|
|
234
|
+
const reply: PermissionsRpcReply = {
|
|
235
|
+
success: false,
|
|
236
|
+
protocolVersion: PERMISSIONS_PROTOCOL_VERSION,
|
|
237
|
+
error: "no_ui",
|
|
238
|
+
};
|
|
239
|
+
expect(reply.success).toBe(false);
|
|
240
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- narrowing on discriminated union
|
|
241
|
+
if (!reply.success) {
|
|
242
|
+
expect(reply.error).toBe("no_ui");
|
|
243
|
+
}
|
|
244
|
+
});
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
describe("type shapes (PermissionsCheckRequest)", () => {
|
|
248
|
+
it("minimal request requires requestId and surface", () => {
|
|
249
|
+
const req: PermissionsCheckRequest = {
|
|
250
|
+
requestId: "abc-123",
|
|
251
|
+
surface: "bash",
|
|
252
|
+
};
|
|
253
|
+
expect(req.requestId).toBe("abc-123");
|
|
254
|
+
expect(req.surface).toBe("bash");
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
it("optional fields are accepted", () => {
|
|
258
|
+
const req: PermissionsCheckRequest = {
|
|
259
|
+
requestId: "abc-123",
|
|
260
|
+
surface: "bash",
|
|
261
|
+
value: "git status",
|
|
262
|
+
agentName: "Worker",
|
|
263
|
+
};
|
|
264
|
+
expect(req.value).toBe("git status");
|
|
265
|
+
expect(req.agentName).toBe("Worker");
|
|
266
|
+
});
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
describe("type shapes (PermissionsCheckReplyData)", () => {
|
|
270
|
+
it("has result, matchedPattern, origin", () => {
|
|
271
|
+
const data: PermissionsCheckReplyData = {
|
|
272
|
+
result: "ask",
|
|
273
|
+
matchedPattern: null,
|
|
274
|
+
origin: "builtin",
|
|
275
|
+
};
|
|
276
|
+
expect(data.result).toBe("ask");
|
|
277
|
+
});
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
describe("type shapes (PermissionsPromptRequest)", () => {
|
|
281
|
+
it("minimal request requires requestId, surface, value, message", () => {
|
|
282
|
+
const req: PermissionsPromptRequest = {
|
|
283
|
+
requestId: "def-456",
|
|
284
|
+
surface: "bash",
|
|
285
|
+
value: "rm -rf /tmp",
|
|
286
|
+
message: "Allow rm -rf /tmp?",
|
|
287
|
+
};
|
|
288
|
+
expect(req.requestId).toBe("def-456");
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
it("optional agentName and sessionLabel are accepted", () => {
|
|
292
|
+
const req: PermissionsPromptRequest = {
|
|
293
|
+
requestId: "def-456",
|
|
294
|
+
surface: "bash",
|
|
295
|
+
value: "rm -rf /tmp",
|
|
296
|
+
message: "Allow rm -rf /tmp?",
|
|
297
|
+
agentName: "Explore",
|
|
298
|
+
sessionLabel: "Allow rm *",
|
|
299
|
+
};
|
|
300
|
+
expect(req.agentName).toBe("Explore");
|
|
301
|
+
expect(req.sessionLabel).toBe("Allow rm *");
|
|
302
|
+
});
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
describe("type shapes (PermissionsPromptReplyData)", () => {
|
|
306
|
+
it("approved reply has approved=true and state", () => {
|
|
307
|
+
const data: PermissionsPromptReplyData = {
|
|
308
|
+
approved: true,
|
|
309
|
+
state: "approved_for_session",
|
|
310
|
+
};
|
|
311
|
+
expect(data.approved).toBe(true);
|
|
312
|
+
expect(data.state).toBe("approved_for_session");
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
it("denied reply may include denialReason", () => {
|
|
316
|
+
const data: PermissionsPromptReplyData = {
|
|
317
|
+
approved: false,
|
|
318
|
+
state: "denied_with_reason",
|
|
319
|
+
denialReason: "Too risky",
|
|
320
|
+
};
|
|
321
|
+
expect(data.denialReason).toBe("Too risky");
|
|
322
|
+
});
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
// ── piPermissionSystemExtension emits permissions:ready ────────────────────
|
|
326
|
+
|
|
327
|
+
describe("piPermissionSystemExtension ready event wiring", () => {
|
|
328
|
+
let baseDir: string;
|
|
329
|
+
let originalAgentDir: string | undefined;
|
|
330
|
+
|
|
331
|
+
beforeEach(() => {
|
|
332
|
+
baseDir = mkdtempSync(join(tmpdir(), "pi-perm-events-test-"));
|
|
333
|
+
originalAgentDir = process.env.PI_CODING_AGENT_DIR;
|
|
334
|
+
const globalConfigPath = getGlobalConfigPath(baseDir);
|
|
335
|
+
mkdirSync(dirname(globalConfigPath), { recursive: true });
|
|
336
|
+
mkdirSync(join(baseDir, "agents"), { recursive: true });
|
|
337
|
+
writeFileSync(
|
|
338
|
+
globalConfigPath,
|
|
339
|
+
`${JSON.stringify({ permission: { "*": "ask" } })}\n`,
|
|
340
|
+
"utf8",
|
|
341
|
+
);
|
|
342
|
+
process.env.PI_CODING_AGENT_DIR = baseDir;
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
afterEach(() => {
|
|
346
|
+
if (originalAgentDir === undefined) {
|
|
347
|
+
delete process.env.PI_CODING_AGENT_DIR;
|
|
348
|
+
} else {
|
|
349
|
+
process.env.PI_CODING_AGENT_DIR = originalAgentDir;
|
|
350
|
+
}
|
|
351
|
+
rmSync(baseDir, { recursive: true, force: true });
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
it("emits permissions:ready at session_start", async () => {
|
|
355
|
+
const emitSpy = vi.fn();
|
|
356
|
+
const handlers = new Map<
|
|
357
|
+
string,
|
|
358
|
+
(event: unknown, ctx: unknown) => unknown
|
|
359
|
+
>();
|
|
360
|
+
piPermissionSystemExtension({
|
|
361
|
+
on: vi.fn(
|
|
362
|
+
(event: string, handler: (e: unknown, c: unknown) => unknown) => {
|
|
363
|
+
handlers.set(event, handler);
|
|
364
|
+
},
|
|
365
|
+
),
|
|
366
|
+
registerCommand: vi.fn(),
|
|
367
|
+
getAllTools: vi.fn().mockReturnValue([]),
|
|
368
|
+
getActiveTools: vi.fn().mockReturnValue([]),
|
|
369
|
+
setActiveTools: vi.fn(),
|
|
370
|
+
registerProvider: vi.fn(),
|
|
371
|
+
events: { emit: emitSpy, on: vi.fn().mockReturnValue(() => undefined) },
|
|
372
|
+
} as never);
|
|
373
|
+
|
|
374
|
+
// ready is not emitted at load — only after session_start publishes.
|
|
375
|
+
expect(
|
|
376
|
+
emitSpy.mock.calls.filter(([c]) => c === PERMISSIONS_READY_CHANNEL),
|
|
377
|
+
).toHaveLength(0);
|
|
378
|
+
|
|
379
|
+
const ctx = {
|
|
380
|
+
cwd: baseDir,
|
|
381
|
+
hasUI: false,
|
|
382
|
+
sessionManager: {
|
|
383
|
+
getEntries: (): unknown[] => [],
|
|
384
|
+
getSessionId: (): string => "top-session",
|
|
385
|
+
getSessionDir: (): string => baseDir,
|
|
386
|
+
},
|
|
387
|
+
ui: {
|
|
388
|
+
notify: (): void => {},
|
|
389
|
+
setStatus: (): void => {},
|
|
390
|
+
select: async (): Promise<string | undefined> => undefined,
|
|
391
|
+
input: async (): Promise<string | undefined> => undefined,
|
|
392
|
+
},
|
|
393
|
+
};
|
|
394
|
+
await handlers.get("session_start")?.({ reason: "start" }, ctx);
|
|
395
|
+
|
|
396
|
+
const readyCalls = emitSpy.mock.calls.filter(
|
|
397
|
+
([channel]) => channel === PERMISSIONS_READY_CHANNEL,
|
|
398
|
+
);
|
|
399
|
+
expect(readyCalls).toHaveLength(1);
|
|
400
|
+
expect(readyCalls[0][1]).toEqual({});
|
|
401
|
+
});
|
|
402
|
+
});
|