@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,363 @@
|
|
|
1
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
3
|
+
|
|
4
|
+
// ── Module mocks (hoisted) ─────────────────────────────────────────────────
|
|
5
|
+
|
|
6
|
+
const { mockGetActiveAgentName, mockGetActiveAgentNameFromSystemPrompt } =
|
|
7
|
+
vi.hoisted(() => ({
|
|
8
|
+
mockGetActiveAgentName: vi.fn<(ctx: ExtensionContext) => string | null>(),
|
|
9
|
+
mockGetActiveAgentNameFromSystemPrompt:
|
|
10
|
+
vi.fn<(systemPrompt?: string) => string | null>(),
|
|
11
|
+
}));
|
|
12
|
+
|
|
13
|
+
vi.mock("../src/active-agent", () => ({
|
|
14
|
+
getActiveAgentName: mockGetActiveAgentName,
|
|
15
|
+
getActiveAgentNameFromSystemPrompt: mockGetActiveAgentNameFromSystemPrompt,
|
|
16
|
+
}));
|
|
17
|
+
|
|
18
|
+
// ── Test helpers ───────────────────────────────────────────────────────────
|
|
19
|
+
|
|
20
|
+
import type { DEFAULT_EXTENSION_CONFIG } from "#src/extension-config";
|
|
21
|
+
import { SessionApproval } from "#src/session-approval";
|
|
22
|
+
import type { SkillPromptEntry } from "#src/skill-prompt-sanitizer";
|
|
23
|
+
import { makeCtx } from "#test/helpers/handler-fixtures";
|
|
24
|
+
import {
|
|
25
|
+
makeConfigStore,
|
|
26
|
+
makeFakePermissionManager,
|
|
27
|
+
makeRealSession,
|
|
28
|
+
} from "#test/helpers/session-fixtures";
|
|
29
|
+
|
|
30
|
+
// Alias so the existing tests read naturally.
|
|
31
|
+
const createSession = makeRealSession;
|
|
32
|
+
const makePermissionManager = makeFakePermissionManager;
|
|
33
|
+
|
|
34
|
+
function makeSkillEntry(
|
|
35
|
+
name: string,
|
|
36
|
+
overrides: Partial<SkillPromptEntry> = {},
|
|
37
|
+
): SkillPromptEntry {
|
|
38
|
+
return {
|
|
39
|
+
name,
|
|
40
|
+
description: `${name} skill`,
|
|
41
|
+
location: `/${name}/SKILL.md`,
|
|
42
|
+
state: "allow",
|
|
43
|
+
normalizedLocation: `/${name}/SKILL.md`,
|
|
44
|
+
normalizedBaseDir: `/${name}`,
|
|
45
|
+
...overrides,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// ── Tests ──────────────────────────────────────────────────────────────────
|
|
50
|
+
|
|
51
|
+
beforeEach(() => {
|
|
52
|
+
mockGetActiveAgentName.mockReset();
|
|
53
|
+
mockGetActiveAgentNameFromSystemPrompt.mockReset();
|
|
54
|
+
mockGetActiveAgentName.mockReturnValue(null);
|
|
55
|
+
mockGetActiveAgentNameFromSystemPrompt.mockReturnValue(null);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
describe("PermissionSession", () => {
|
|
59
|
+
describe("activate and deactivate", () => {
|
|
60
|
+
it("stores the context on activate", () => {
|
|
61
|
+
const { session, forwarding } = createSession();
|
|
62
|
+
const ctx = makeCtx();
|
|
63
|
+
|
|
64
|
+
session.activate(ctx);
|
|
65
|
+
|
|
66
|
+
expect(forwarding.start).toHaveBeenCalledWith(ctx);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("clears context on deactivate", () => {
|
|
70
|
+
const { session, forwarding } = createSession();
|
|
71
|
+
session.activate(makeCtx());
|
|
72
|
+
session.deactivate();
|
|
73
|
+
|
|
74
|
+
expect(forwarding.stop).toHaveBeenCalled();
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("forwards activate to the gateway", () => {
|
|
78
|
+
const { session, gateway } = createSession();
|
|
79
|
+
const ctx = makeCtx();
|
|
80
|
+
|
|
81
|
+
session.activate(ctx);
|
|
82
|
+
|
|
83
|
+
expect(gateway.activate).toHaveBeenCalledWith(ctx);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("forwards deactivate to the gateway", () => {
|
|
87
|
+
const { session, gateway } = createSession();
|
|
88
|
+
session.activate(makeCtx());
|
|
89
|
+
session.deactivate();
|
|
90
|
+
|
|
91
|
+
expect(gateway.deactivate).toHaveBeenCalled();
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
describe("resetForNewSession", () => {
|
|
96
|
+
it("configures the injected PermissionManager for the context cwd", () => {
|
|
97
|
+
const pm = makePermissionManager();
|
|
98
|
+
const { session } = createSession({ permissionManager: pm });
|
|
99
|
+
const ctx = makeCtx({ cwd: "/new/project" });
|
|
100
|
+
|
|
101
|
+
session.resetForNewSession(ctx);
|
|
102
|
+
|
|
103
|
+
expect(pm.configureForCwd).toHaveBeenCalledWith("/new/project");
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("clears skill entries", () => {
|
|
107
|
+
const { session } = createSession();
|
|
108
|
+
session.setActiveSkillEntries([makeSkillEntry("test")]);
|
|
109
|
+
expect(session.getActiveSkillEntries()).toHaveLength(1);
|
|
110
|
+
|
|
111
|
+
session.resetForNewSession(makeCtx());
|
|
112
|
+
|
|
113
|
+
expect(session.getActiveSkillEntries()).toEqual([]);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it("starts forwarding with the new context", () => {
|
|
117
|
+
const { session, forwarding } = createSession();
|
|
118
|
+
const ctx = makeCtx();
|
|
119
|
+
|
|
120
|
+
session.resetForNewSession(ctx);
|
|
121
|
+
|
|
122
|
+
expect(forwarding.start).toHaveBeenCalledWith(ctx);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it("activates the new context", () => {
|
|
126
|
+
const { session } = createSession();
|
|
127
|
+
const ctx = makeCtx();
|
|
128
|
+
|
|
129
|
+
session.resetForNewSession(ctx);
|
|
130
|
+
|
|
131
|
+
// Verify context is stored by calling resolveAgentName which needs it
|
|
132
|
+
mockGetActiveAgentName.mockReturnValue("test-agent");
|
|
133
|
+
const name = session.resolveAgentName(ctx);
|
|
134
|
+
expect(name).toBe("test-agent");
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
describe("shutdown", () => {
|
|
139
|
+
it("clears session rules", () => {
|
|
140
|
+
const { session, sessionRules } = createSession();
|
|
141
|
+
sessionRules.recordSessionApproval(SessionApproval.single("bash", "*"));
|
|
142
|
+
expect(sessionRules.getRuleset()).toHaveLength(1);
|
|
143
|
+
|
|
144
|
+
session.shutdown();
|
|
145
|
+
|
|
146
|
+
expect(sessionRules.getRuleset()).toEqual([]);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it("clears skill entries", () => {
|
|
150
|
+
const { session } = createSession();
|
|
151
|
+
session.setActiveSkillEntries([makeSkillEntry("s")]);
|
|
152
|
+
|
|
153
|
+
session.shutdown();
|
|
154
|
+
|
|
155
|
+
expect(session.getActiveSkillEntries()).toEqual([]);
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it("stops forwarding and deactivates context", () => {
|
|
159
|
+
const { session, forwarding } = createSession();
|
|
160
|
+
session.activate(makeCtx());
|
|
161
|
+
|
|
162
|
+
session.shutdown();
|
|
163
|
+
|
|
164
|
+
expect(forwarding.stop).toHaveBeenCalled();
|
|
165
|
+
});
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
describe("skill entries", () => {
|
|
169
|
+
it("get/set skill entries", () => {
|
|
170
|
+
const { session } = createSession();
|
|
171
|
+
const entries = [makeSkillEntry("a"), makeSkillEntry("b")];
|
|
172
|
+
session.setActiveSkillEntries(entries);
|
|
173
|
+
expect(session.getActiveSkillEntries()).toEqual(entries);
|
|
174
|
+
});
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
describe("resolveAgentName", () => {
|
|
178
|
+
it("returns name from session context", () => {
|
|
179
|
+
mockGetActiveAgentName.mockReturnValue("ctx-agent");
|
|
180
|
+
const { session } = createSession();
|
|
181
|
+
const ctx = makeCtx();
|
|
182
|
+
|
|
183
|
+
expect(session.resolveAgentName(ctx)).toBe("ctx-agent");
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it("falls back to system prompt", () => {
|
|
187
|
+
mockGetActiveAgentName.mockReturnValue(null);
|
|
188
|
+
mockGetActiveAgentNameFromSystemPrompt.mockReturnValue("prompt-agent");
|
|
189
|
+
const { session } = createSession();
|
|
190
|
+
const ctx = makeCtx();
|
|
191
|
+
|
|
192
|
+
expect(session.resolveAgentName(ctx, "system prompt")).toBe(
|
|
193
|
+
"prompt-agent",
|
|
194
|
+
);
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it("falls back to last known name", () => {
|
|
198
|
+
const { session } = createSession();
|
|
199
|
+
const ctx = makeCtx();
|
|
200
|
+
|
|
201
|
+
// First call sets name
|
|
202
|
+
mockGetActiveAgentName.mockReturnValue("first-agent");
|
|
203
|
+
session.resolveAgentName(ctx);
|
|
204
|
+
|
|
205
|
+
// Second call with no name resolves to last known
|
|
206
|
+
mockGetActiveAgentName.mockReturnValue(null);
|
|
207
|
+
mockGetActiveAgentNameFromSystemPrompt.mockReturnValue(null);
|
|
208
|
+
expect(session.resolveAgentName(ctx)).toBe("first-agent");
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
it("exposes lastKnownActiveAgentName", () => {
|
|
212
|
+
const { session } = createSession();
|
|
213
|
+
expect(session.lastKnownActiveAgentName).toBeNull();
|
|
214
|
+
|
|
215
|
+
mockGetActiveAgentName.mockReturnValue("named");
|
|
216
|
+
session.resolveAgentName(makeCtx());
|
|
217
|
+
expect(session.lastKnownActiveAgentName).toBe("named");
|
|
218
|
+
});
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
describe("infrastructure paths", () => {
|
|
222
|
+
it("getInfrastructureReadDirs combines piInfrastructureDirs and piInfrastructureReadPaths", () => {
|
|
223
|
+
const configStore = makeConfigStore({
|
|
224
|
+
current: vi.fn().mockReturnValue({
|
|
225
|
+
piInfrastructureReadPaths: ["/extra/path"],
|
|
226
|
+
}),
|
|
227
|
+
});
|
|
228
|
+
const { session } = createSession({ configStore });
|
|
229
|
+
expect(session.getInfrastructureReadDirs()).toEqual([
|
|
230
|
+
"/test/agent",
|
|
231
|
+
"/test/agent/git",
|
|
232
|
+
"/extra/path",
|
|
233
|
+
]);
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
it("getInfrastructureReadDirs returns only piInfrastructureDirs when config omits the field", () => {
|
|
237
|
+
const { session } = createSession();
|
|
238
|
+
expect(session.getInfrastructureReadDirs()).toEqual([
|
|
239
|
+
"/test/agent",
|
|
240
|
+
"/test/agent/git",
|
|
241
|
+
]);
|
|
242
|
+
});
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
describe("config delegation", () => {
|
|
246
|
+
it("refreshConfig delegates to configStore.refresh", () => {
|
|
247
|
+
const { session, configStore } = createSession();
|
|
248
|
+
const ctx = makeCtx();
|
|
249
|
+
session.refreshConfig(ctx);
|
|
250
|
+
expect(configStore.refresh).toHaveBeenCalledWith(ctx);
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
it("logResolvedConfigPaths delegates to configStore.logResolvedPaths", () => {
|
|
254
|
+
const { session, configStore } = createSession();
|
|
255
|
+
session.logResolvedConfigPaths();
|
|
256
|
+
expect(configStore.logResolvedPaths).toHaveBeenCalled();
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
it("config getter delegates to configStore.current()", () => {
|
|
260
|
+
const fakeConfig = { debugLog: true } as typeof DEFAULT_EXTENSION_CONFIG;
|
|
261
|
+
const configStore = makeConfigStore({
|
|
262
|
+
current: vi.fn().mockReturnValue(fakeConfig),
|
|
263
|
+
});
|
|
264
|
+
const { session } = createSession({ configStore });
|
|
265
|
+
expect(session.config).toBe(fakeConfig);
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
it("getToolPreviewLimits returns resolved preview limits from config", () => {
|
|
269
|
+
const configStore = makeConfigStore({
|
|
270
|
+
current: vi.fn().mockReturnValue({
|
|
271
|
+
toolInputPreviewMaxLength: 400,
|
|
272
|
+
toolTextSummaryMaxLength: 120,
|
|
273
|
+
}),
|
|
274
|
+
});
|
|
275
|
+
const { session } = createSession({ configStore });
|
|
276
|
+
const limits = session.getToolPreviewLimits();
|
|
277
|
+
expect(limits.toolInputPreviewMaxLength).toBe(400);
|
|
278
|
+
expect(limits.toolTextSummaryMaxLength).toBe(120);
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
it("getToolPreviewLimits falls back to built-in defaults when config omits fields", () => {
|
|
282
|
+
const { session } = createSession();
|
|
283
|
+
const limits = session.getToolPreviewLimits();
|
|
284
|
+
expect(limits.toolInputPreviewMaxLength).toBeGreaterThan(0);
|
|
285
|
+
expect(limits.toolTextSummaryMaxLength).toBeGreaterThan(0);
|
|
286
|
+
expect(limits.toolInputLogPreviewMaxLength).toBeGreaterThan(0);
|
|
287
|
+
});
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
describe("reload", () => {
|
|
291
|
+
it("configures PermissionManager for current context cwd", () => {
|
|
292
|
+
const pm = makePermissionManager();
|
|
293
|
+
const { session } = createSession({ permissionManager: pm });
|
|
294
|
+
const ctx = makeCtx({ cwd: "/project" });
|
|
295
|
+
session.activate(ctx);
|
|
296
|
+
|
|
297
|
+
session.reload();
|
|
298
|
+
|
|
299
|
+
expect(pm.configureForCwd).toHaveBeenCalledWith("/project");
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
it("clears skill entries", () => {
|
|
303
|
+
const { session } = createSession();
|
|
304
|
+
session.setActiveSkillEntries([makeSkillEntry("s")]);
|
|
305
|
+
|
|
306
|
+
session.reload();
|
|
307
|
+
|
|
308
|
+
expect(session.getActiveSkillEntries()).toEqual([]);
|
|
309
|
+
});
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
describe("getRuntimeContext", () => {
|
|
313
|
+
it("returns null before activation", () => {
|
|
314
|
+
const { session } = createSession();
|
|
315
|
+
expect(session.getRuntimeContext()).toBeNull();
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
it("returns context after activation", () => {
|
|
319
|
+
const { session } = createSession();
|
|
320
|
+
const ctx = makeCtx();
|
|
321
|
+
session.activate(ctx);
|
|
322
|
+
expect(session.getRuntimeContext()).toBe(ctx);
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
it("returns null after deactivation", () => {
|
|
326
|
+
const { session } = createSession();
|
|
327
|
+
session.activate(makeCtx());
|
|
328
|
+
session.deactivate();
|
|
329
|
+
expect(session.getRuntimeContext()).toBeNull();
|
|
330
|
+
});
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
describe("notify", () => {
|
|
334
|
+
it("forwards the message to ctx.ui.notify with 'warning' severity after activation", () => {
|
|
335
|
+
const { session } = createSession();
|
|
336
|
+
const ctx = makeCtx();
|
|
337
|
+
session.activate(ctx);
|
|
338
|
+
|
|
339
|
+
session.notify("something went wrong");
|
|
340
|
+
|
|
341
|
+
expect(ctx.ui.notify).toHaveBeenCalledOnce();
|
|
342
|
+
expect(ctx.ui.notify).toHaveBeenCalledWith(
|
|
343
|
+
"something went wrong",
|
|
344
|
+
"warning",
|
|
345
|
+
);
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
it("is a no-op and does not throw before activation", () => {
|
|
349
|
+
const { session } = createSession();
|
|
350
|
+
|
|
351
|
+
expect(() => session.notify("msg")).not.toThrow();
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
it("is a no-op and does not throw after deactivation", () => {
|
|
355
|
+
const { session } = createSession();
|
|
356
|
+
const ctx = makeCtx();
|
|
357
|
+
session.activate(ctx);
|
|
358
|
+
session.deactivate();
|
|
359
|
+
|
|
360
|
+
expect(() => session.notify("msg")).not.toThrow();
|
|
361
|
+
});
|
|
362
|
+
});
|
|
363
|
+
});
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
buildDirectUiPrompt,
|
|
5
|
+
buildForwardedUiPrompt,
|
|
6
|
+
buildRpcUiPrompt,
|
|
7
|
+
} from "#src/permission-ui-prompt";
|
|
8
|
+
|
|
9
|
+
describe("buildDirectUiPrompt", () => {
|
|
10
|
+
it("maps a tool_call prompt to the tool surface and command value", () => {
|
|
11
|
+
expect(
|
|
12
|
+
buildDirectUiPrompt({
|
|
13
|
+
requestId: "req-1",
|
|
14
|
+
source: "tool_call",
|
|
15
|
+
agentName: "Explore",
|
|
16
|
+
message: "Allow git push?",
|
|
17
|
+
toolName: "bash",
|
|
18
|
+
command: "git push",
|
|
19
|
+
}),
|
|
20
|
+
).toEqual({
|
|
21
|
+
requestId: "req-1",
|
|
22
|
+
source: "tool_call",
|
|
23
|
+
surface: "bash",
|
|
24
|
+
value: "git push",
|
|
25
|
+
agentName: "Explore",
|
|
26
|
+
message: "Allow git push?",
|
|
27
|
+
forwarding: null,
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("normalizes a skill prompt to the skill surface and skill-name value", () => {
|
|
32
|
+
expect(
|
|
33
|
+
buildDirectUiPrompt({
|
|
34
|
+
requestId: "req-2",
|
|
35
|
+
source: "skill_input",
|
|
36
|
+
agentName: null,
|
|
37
|
+
message: "Allow skill?",
|
|
38
|
+
skillName: "deploy-helper",
|
|
39
|
+
}),
|
|
40
|
+
).toEqual({
|
|
41
|
+
requestId: "req-2",
|
|
42
|
+
source: "skill_input",
|
|
43
|
+
surface: "skill",
|
|
44
|
+
value: "deploy-helper",
|
|
45
|
+
agentName: null,
|
|
46
|
+
message: "Allow skill?",
|
|
47
|
+
forwarding: null,
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("derives value with command > path > target > skillName > toolName precedence", () => {
|
|
52
|
+
expect(
|
|
53
|
+
buildDirectUiPrompt({
|
|
54
|
+
requestId: "req-3",
|
|
55
|
+
source: "tool_call",
|
|
56
|
+
agentName: null,
|
|
57
|
+
message: "m",
|
|
58
|
+
toolName: "read",
|
|
59
|
+
path: "/etc/hosts",
|
|
60
|
+
target: "ignored",
|
|
61
|
+
}).value,
|
|
62
|
+
).toBe("/etc/hosts");
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
describe("buildRpcUiPrompt", () => {
|
|
67
|
+
it("maps an RPC prompt to the rpc_prompt source with passthrough surface/value", () => {
|
|
68
|
+
expect(
|
|
69
|
+
buildRpcUiPrompt({
|
|
70
|
+
requestId: "req-rpc",
|
|
71
|
+
surface: "bash",
|
|
72
|
+
value: "git push",
|
|
73
|
+
agentName: "Worker",
|
|
74
|
+
message: "Allow git push?",
|
|
75
|
+
}),
|
|
76
|
+
).toEqual({
|
|
77
|
+
requestId: "req-rpc",
|
|
78
|
+
source: "rpc_prompt",
|
|
79
|
+
surface: "bash",
|
|
80
|
+
value: "git push",
|
|
81
|
+
agentName: "Worker",
|
|
82
|
+
message: "Allow git push?",
|
|
83
|
+
forwarding: null,
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it("defaults missing surface, value, and agentName to null", () => {
|
|
88
|
+
expect(
|
|
89
|
+
buildRpcUiPrompt({ requestId: "req-rpc-2", message: "Allow?" }),
|
|
90
|
+
).toEqual({
|
|
91
|
+
requestId: "req-rpc-2",
|
|
92
|
+
source: "rpc_prompt",
|
|
93
|
+
surface: null,
|
|
94
|
+
value: null,
|
|
95
|
+
agentName: null,
|
|
96
|
+
message: "Allow?",
|
|
97
|
+
forwarding: null,
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
describe("buildForwardedUiPrompt", () => {
|
|
103
|
+
it("populates forwarding context and carries the original source/surface/value", () => {
|
|
104
|
+
expect(
|
|
105
|
+
buildForwardedUiPrompt({
|
|
106
|
+
requestId: "req-fwd",
|
|
107
|
+
message: "Subagent 'Explore' requested permission.\n\nAllow git push?",
|
|
108
|
+
requesterAgentName: "Explore",
|
|
109
|
+
requesterSessionId: "child-session",
|
|
110
|
+
source: "tool_call",
|
|
111
|
+
surface: "bash",
|
|
112
|
+
value: "git push",
|
|
113
|
+
}),
|
|
114
|
+
).toEqual({
|
|
115
|
+
requestId: "req-fwd",
|
|
116
|
+
source: "tool_call",
|
|
117
|
+
surface: "bash",
|
|
118
|
+
value: "git push",
|
|
119
|
+
agentName: "Explore",
|
|
120
|
+
message: "Subagent 'Explore' requested permission.\n\nAllow git push?",
|
|
121
|
+
forwarding: {
|
|
122
|
+
requesterAgentName: "Explore",
|
|
123
|
+
requesterSessionId: "child-session",
|
|
124
|
+
},
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it("falls back to source tool_call with null surface/value when the request omits them", () => {
|
|
129
|
+
expect(
|
|
130
|
+
buildForwardedUiPrompt({
|
|
131
|
+
requestId: "req-fwd-old",
|
|
132
|
+
message: "Allow?",
|
|
133
|
+
requesterAgentName: null,
|
|
134
|
+
requesterSessionId: null,
|
|
135
|
+
}),
|
|
136
|
+
).toEqual({
|
|
137
|
+
requestId: "req-fwd-old",
|
|
138
|
+
source: "tool_call",
|
|
139
|
+
surface: null,
|
|
140
|
+
value: null,
|
|
141
|
+
agentName: null,
|
|
142
|
+
message: "Allow?",
|
|
143
|
+
forwarding: { requesterAgentName: null, requesterSessionId: null },
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
});
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import type { ScopedPermissionManager } from "#src/permission-manager";
|
|
3
|
+
import { LocalPermissionsService } from "#src/permissions-service";
|
|
4
|
+
import type { Ruleset } from "#src/rule";
|
|
5
|
+
import type { SessionRules } from "#src/session-rules";
|
|
6
|
+
import type { ToolAccessExtractorRegistrar } from "#src/tool-access-extractor-registry";
|
|
7
|
+
import type {
|
|
8
|
+
ToolInputFormatter,
|
|
9
|
+
ToolInputFormatterRegistrar,
|
|
10
|
+
} from "#src/tool-input-formatter-registry";
|
|
11
|
+
|
|
12
|
+
import { makeCheckResult } from "#test/helpers/handler-fixtures";
|
|
13
|
+
import { makeFakePermissionManager } from "#test/helpers/session-fixtures";
|
|
14
|
+
|
|
15
|
+
// ── input-normalizer stub ──────────────────────────────────────────────────
|
|
16
|
+
|
|
17
|
+
const mockBuildInputForSurface = vi.hoisted(() =>
|
|
18
|
+
vi.fn<(surface: string, value?: string) => unknown>(),
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
vi.mock("#src/input-normalizer", () => ({
|
|
22
|
+
buildInputForSurface: mockBuildInputForSurface,
|
|
23
|
+
}));
|
|
24
|
+
|
|
25
|
+
// ── helpers ────────────────────────────────────────────────────────────────
|
|
26
|
+
|
|
27
|
+
function makeSessionRules(
|
|
28
|
+
rules: Ruleset = [],
|
|
29
|
+
): Pick<SessionRules, "getRuleset"> {
|
|
30
|
+
return {
|
|
31
|
+
getRuleset: vi.fn<SessionRules["getRuleset"]>().mockReturnValue(rules),
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function makeFormatterRegistry(): ToolInputFormatterRegistrar {
|
|
36
|
+
return {
|
|
37
|
+
register: vi
|
|
38
|
+
.fn<ToolInputFormatterRegistrar["register"]>()
|
|
39
|
+
.mockReturnValue(vi.fn()),
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function makeAccessExtractorRegistry(): ToolAccessExtractorRegistrar {
|
|
44
|
+
return {
|
|
45
|
+
register: vi
|
|
46
|
+
.fn<ToolAccessExtractorRegistrar["register"]>()
|
|
47
|
+
.mockReturnValue(vi.fn()),
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function makeService(overrides?: {
|
|
52
|
+
permissionManager?: ScopedPermissionManager;
|
|
53
|
+
sessionRules?: Pick<SessionRules, "getRuleset">;
|
|
54
|
+
formatterRegistry?: ToolInputFormatterRegistrar;
|
|
55
|
+
accessExtractorRegistry?: ToolAccessExtractorRegistrar;
|
|
56
|
+
}) {
|
|
57
|
+
const permissionManager =
|
|
58
|
+
overrides?.permissionManager ?? makeFakePermissionManager();
|
|
59
|
+
const sessionRules = overrides?.sessionRules ?? makeSessionRules();
|
|
60
|
+
const formatterRegistry =
|
|
61
|
+
overrides?.formatterRegistry ?? makeFormatterRegistry();
|
|
62
|
+
const accessExtractorRegistry =
|
|
63
|
+
overrides?.accessExtractorRegistry ?? makeAccessExtractorRegistry();
|
|
64
|
+
const service = new LocalPermissionsService(
|
|
65
|
+
permissionManager,
|
|
66
|
+
sessionRules,
|
|
67
|
+
formatterRegistry,
|
|
68
|
+
accessExtractorRegistry,
|
|
69
|
+
);
|
|
70
|
+
return {
|
|
71
|
+
service,
|
|
72
|
+
permissionManager,
|
|
73
|
+
sessionRules,
|
|
74
|
+
formatterRegistry,
|
|
75
|
+
accessExtractorRegistry,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// ── tests ──────────────────────────────────────────────────────────────────
|
|
80
|
+
|
|
81
|
+
beforeEach(() => {
|
|
82
|
+
mockBuildInputForSurface.mockReset();
|
|
83
|
+
mockBuildInputForSurface.mockReturnValue({ type: "tool-input" });
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
describe("checkPermission", () => {
|
|
87
|
+
it("builds the surface input from surface and value", () => {
|
|
88
|
+
const { service } = makeService();
|
|
89
|
+
service.checkPermission("bash", "echo hi");
|
|
90
|
+
expect(mockBuildInputForSurface).toHaveBeenCalledWith("bash", "echo hi");
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("builds the surface input with undefined value when value is omitted", () => {
|
|
94
|
+
const { service } = makeService();
|
|
95
|
+
service.checkPermission("read");
|
|
96
|
+
expect(mockBuildInputForSurface).toHaveBeenCalledWith("read", undefined);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it("calls permissionManager.checkPermission with surface, built input, agentName, and current ruleset", () => {
|
|
100
|
+
const ruleset: Ruleset = [
|
|
101
|
+
{ surface: "bash", pattern: "*", action: "allow", origin: "global" },
|
|
102
|
+
];
|
|
103
|
+
const builtInput = { type: "bash-input" };
|
|
104
|
+
mockBuildInputForSurface.mockReturnValue(builtInput);
|
|
105
|
+
const { service, permissionManager, sessionRules } = makeService({
|
|
106
|
+
sessionRules: makeSessionRules(ruleset),
|
|
107
|
+
});
|
|
108
|
+
service.checkPermission("bash", "echo hi", "my-agent");
|
|
109
|
+
expect(permissionManager.checkPermission).toHaveBeenCalledWith(
|
|
110
|
+
"bash",
|
|
111
|
+
builtInput,
|
|
112
|
+
"my-agent",
|
|
113
|
+
ruleset,
|
|
114
|
+
);
|
|
115
|
+
void sessionRules; // used indirectly
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it("returns the result from permissionManager.checkPermission", () => {
|
|
119
|
+
const expected = makeCheckResult({ state: "deny", toolName: "bash" });
|
|
120
|
+
const { service, permissionManager } = makeService();
|
|
121
|
+
vi.mocked(permissionManager.checkPermission).mockReturnValue(expected);
|
|
122
|
+
const result = service.checkPermission("bash", "rm -rf /");
|
|
123
|
+
expect(result).toBe(expected);
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
describe("getToolPermission", () => {
|
|
128
|
+
it("delegates to permissionManager.getToolPermission", () => {
|
|
129
|
+
const { service, permissionManager } = makeService();
|
|
130
|
+
vi.mocked(permissionManager.getToolPermission).mockReturnValue("deny");
|
|
131
|
+
const result = service.getToolPermission("write", "my-agent");
|
|
132
|
+
expect(permissionManager.getToolPermission).toHaveBeenCalledWith(
|
|
133
|
+
"write",
|
|
134
|
+
"my-agent",
|
|
135
|
+
);
|
|
136
|
+
expect(result).toBe("deny");
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it("omits agentName when not provided", () => {
|
|
140
|
+
const { service, permissionManager } = makeService();
|
|
141
|
+
service.getToolPermission("read");
|
|
142
|
+
expect(permissionManager.getToolPermission).toHaveBeenCalledWith(
|
|
143
|
+
"read",
|
|
144
|
+
undefined,
|
|
145
|
+
);
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
describe("registerToolInputFormatter", () => {
|
|
150
|
+
it("delegates to formatterRegistry.register and returns the unsubscribe function", () => {
|
|
151
|
+
const unsub = vi.fn();
|
|
152
|
+
const { service, formatterRegistry } = makeService();
|
|
153
|
+
vi.mocked(formatterRegistry.register).mockReturnValue(unsub);
|
|
154
|
+
const formatter: ToolInputFormatter = vi.fn();
|
|
155
|
+
const result = service.registerToolInputFormatter("my-tool", formatter);
|
|
156
|
+
expect(formatterRegistry.register).toHaveBeenCalledWith(
|
|
157
|
+
"my-tool",
|
|
158
|
+
formatter,
|
|
159
|
+
);
|
|
160
|
+
expect(result).toBe(unsub);
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
describe("registerToolAccessExtractor", () => {
|
|
165
|
+
it("delegates to accessExtractorRegistry.register and returns the unsubscribe function", () => {
|
|
166
|
+
const unsub = vi.fn();
|
|
167
|
+
const { service, accessExtractorRegistry } = makeService();
|
|
168
|
+
vi.mocked(accessExtractorRegistry.register).mockReturnValue(unsub);
|
|
169
|
+
const extractor = vi.fn();
|
|
170
|
+
const result = service.registerToolAccessExtractor("ffgrep", extractor);
|
|
171
|
+
expect(accessExtractorRegistry.register).toHaveBeenCalledWith(
|
|
172
|
+
"ffgrep",
|
|
173
|
+
extractor,
|
|
174
|
+
);
|
|
175
|
+
expect(result).toBe(unsub);
|
|
176
|
+
});
|
|
177
|
+
});
|