@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,223 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
|
|
3
|
+
import { describeToolGate } from "#src/handlers/gates/tool";
|
|
4
|
+
import type { ToolCallContext } from "#src/handlers/gates/types";
|
|
5
|
+
import {
|
|
6
|
+
TOOL_INPUT_LOG_PREVIEW_MAX_LENGTH,
|
|
7
|
+
TOOL_INPUT_PREVIEW_MAX_LENGTH,
|
|
8
|
+
TOOL_TEXT_SUMMARY_MAX_LENGTH,
|
|
9
|
+
} from "#src/tool-input-preview";
|
|
10
|
+
import { ToolPreviewFormatter } from "#src/tool-preview-formatter";
|
|
11
|
+
import type { PermissionCheckResult } from "#src/types";
|
|
12
|
+
|
|
13
|
+
// ── helpers ────────────────────────────────────────────────────────────────
|
|
14
|
+
|
|
15
|
+
function makeFormatter(): ToolPreviewFormatter {
|
|
16
|
+
return new ToolPreviewFormatter({
|
|
17
|
+
toolInputPreviewMaxLength: TOOL_INPUT_PREVIEW_MAX_LENGTH,
|
|
18
|
+
toolTextSummaryMaxLength: TOOL_TEXT_SUMMARY_MAX_LENGTH,
|
|
19
|
+
toolInputLogPreviewMaxLength: TOOL_INPUT_LOG_PREVIEW_MAX_LENGTH,
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function makeTcc(overrides: Partial<ToolCallContext> = {}): ToolCallContext {
|
|
24
|
+
return {
|
|
25
|
+
toolName: "read",
|
|
26
|
+
agentName: null,
|
|
27
|
+
input: {},
|
|
28
|
+
toolCallId: "tc-1",
|
|
29
|
+
cwd: "/test/project",
|
|
30
|
+
...overrides,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function makeCheckResult(
|
|
35
|
+
state: "allow" | "deny" | "ask",
|
|
36
|
+
overrides: Partial<PermissionCheckResult> = {},
|
|
37
|
+
): PermissionCheckResult {
|
|
38
|
+
return {
|
|
39
|
+
state,
|
|
40
|
+
toolName: "read",
|
|
41
|
+
source: "tool",
|
|
42
|
+
origin: "builtin",
|
|
43
|
+
matchedPattern: "*",
|
|
44
|
+
...overrides,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// ── tests ──────────────────────────────────────────────────────────────────
|
|
49
|
+
|
|
50
|
+
describe("describeToolGate", () => {
|
|
51
|
+
it("returns descriptor with tool name as surface for standard tools", () => {
|
|
52
|
+
const desc = describeToolGate(
|
|
53
|
+
makeTcc({ toolName: "read" }),
|
|
54
|
+
makeCheckResult("ask"),
|
|
55
|
+
makeFormatter(),
|
|
56
|
+
);
|
|
57
|
+
expect(desc.surface).toBe("read");
|
|
58
|
+
expect(desc.decision.surface).toBe("read");
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("returns descriptor with tool name as decision value for standard tools", () => {
|
|
62
|
+
const desc = describeToolGate(
|
|
63
|
+
makeTcc({ toolName: "write" }),
|
|
64
|
+
makeCheckResult("ask"),
|
|
65
|
+
makeFormatter(),
|
|
66
|
+
);
|
|
67
|
+
expect(desc.decision.value).toBe("write");
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("returns bash surface with command in decision.value for bash tools", () => {
|
|
71
|
+
const check = makeCheckResult("ask", {
|
|
72
|
+
toolName: "bash",
|
|
73
|
+
command: "git status",
|
|
74
|
+
});
|
|
75
|
+
const desc = describeToolGate(
|
|
76
|
+
makeTcc({ toolName: "bash", input: { command: "git status" } }),
|
|
77
|
+
check,
|
|
78
|
+
makeFormatter(),
|
|
79
|
+
);
|
|
80
|
+
expect(desc.surface).toBe("bash");
|
|
81
|
+
expect(desc.decision.surface).toBe("bash");
|
|
82
|
+
expect(desc.decision.value).toBe("git status");
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("returns mcp surface with target in decision.value for MCP tools", () => {
|
|
86
|
+
const check = makeCheckResult("ask", {
|
|
87
|
+
toolName: "mcp",
|
|
88
|
+
target: "server:tool",
|
|
89
|
+
});
|
|
90
|
+
const desc = describeToolGate(
|
|
91
|
+
makeTcc({ toolName: "mcp", input: { tool: "server:tool" } }),
|
|
92
|
+
check,
|
|
93
|
+
makeFormatter(),
|
|
94
|
+
);
|
|
95
|
+
expect(desc.surface).toBe("mcp");
|
|
96
|
+
expect(desc.decision.surface).toBe("mcp");
|
|
97
|
+
expect(desc.decision.value).toBe("server:tool");
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it("populates denialContext with kind 'tool' and check result", () => {
|
|
101
|
+
const check = makeCheckResult("deny", { toolName: "read" });
|
|
102
|
+
const desc = describeToolGate(makeTcc(), check, makeFormatter());
|
|
103
|
+
expect(desc.denialContext).toEqual({
|
|
104
|
+
kind: "tool",
|
|
105
|
+
check,
|
|
106
|
+
agentName: undefined,
|
|
107
|
+
input: {},
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("populates denialContext with agent name when provided", () => {
|
|
112
|
+
const check = makeCheckResult("ask", { toolName: "read" });
|
|
113
|
+
const desc = describeToolGate(
|
|
114
|
+
makeTcc({ agentName: "my-agent" }),
|
|
115
|
+
check,
|
|
116
|
+
makeFormatter(),
|
|
117
|
+
);
|
|
118
|
+
expect(desc.denialContext.agentName).toBe("my-agent");
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("populates denialContext with input for tool context", () => {
|
|
122
|
+
const check = makeCheckResult("ask", { toolName: "bash", command: "ls" });
|
|
123
|
+
const desc = describeToolGate(
|
|
124
|
+
makeTcc({ toolName: "bash", input: { command: "ls" } }),
|
|
125
|
+
check,
|
|
126
|
+
makeFormatter(),
|
|
127
|
+
);
|
|
128
|
+
expect(desc.denialContext).toMatchObject({
|
|
129
|
+
kind: "tool",
|
|
130
|
+
input: { command: "ls" },
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it("populates sessionApproval via suggestSessionPattern", () => {
|
|
135
|
+
const check = makeCheckResult("ask", {
|
|
136
|
+
toolName: "bash",
|
|
137
|
+
command: "git status",
|
|
138
|
+
});
|
|
139
|
+
const desc = describeToolGate(
|
|
140
|
+
makeTcc({ toolName: "bash", input: { command: "git status" } }),
|
|
141
|
+
check,
|
|
142
|
+
makeFormatter(),
|
|
143
|
+
);
|
|
144
|
+
expect(desc.sessionApproval).toBeDefined();
|
|
145
|
+
expect(desc.sessionApproval?.surface).toBe("bash");
|
|
146
|
+
expect(desc.sessionApproval?.representativePattern).toBeDefined();
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it("binds a current-directory file's session approval to the cwd subtree", () => {
|
|
150
|
+
const check = makeCheckResult("ask", { toolName: "edit" });
|
|
151
|
+
const desc = describeToolGate(
|
|
152
|
+
makeTcc({
|
|
153
|
+
toolName: "edit",
|
|
154
|
+
input: { path: "index.html" },
|
|
155
|
+
cwd: "/test/project",
|
|
156
|
+
}),
|
|
157
|
+
check,
|
|
158
|
+
makeFormatter(),
|
|
159
|
+
);
|
|
160
|
+
expect(desc.sessionApproval?.surface).toBe("edit");
|
|
161
|
+
expect(desc.sessionApproval?.representativePattern).toBe("/test/project/*");
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it("resolves a sub-directory file's session approval to an absolute pattern", () => {
|
|
165
|
+
// Resolve-at-gate canonicalizes every path (not just the cwd-root case),
|
|
166
|
+
// so sub-directory approvals are absolute too — the deliberate tradeoff
|
|
167
|
+
// that keeps the pattern aligned with the policy values it is matched against.
|
|
168
|
+
const check = makeCheckResult("ask", { toolName: "edit" });
|
|
169
|
+
const desc = describeToolGate(
|
|
170
|
+
makeTcc({
|
|
171
|
+
toolName: "edit",
|
|
172
|
+
input: { path: "src/foo.ts" },
|
|
173
|
+
cwd: "/test/project",
|
|
174
|
+
}),
|
|
175
|
+
check,
|
|
176
|
+
makeFormatter(),
|
|
177
|
+
);
|
|
178
|
+
expect(desc.sessionApproval?.representativePattern).toBe(
|
|
179
|
+
"/test/project/src/*",
|
|
180
|
+
);
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it("populates promptDetails with correct fields", () => {
|
|
184
|
+
const check = makeCheckResult("ask");
|
|
185
|
+
const desc = describeToolGate(
|
|
186
|
+
makeTcc({ toolName: "read", agentName: "my-agent", toolCallId: "tc-42" }),
|
|
187
|
+
check,
|
|
188
|
+
makeFormatter(),
|
|
189
|
+
);
|
|
190
|
+
expect(desc.promptDetails).toMatchObject({
|
|
191
|
+
source: "tool_call",
|
|
192
|
+
agentName: "my-agent",
|
|
193
|
+
toolCallId: "tc-42",
|
|
194
|
+
toolName: "read",
|
|
195
|
+
});
|
|
196
|
+
expect(desc.promptDetails.message).toBeDefined();
|
|
197
|
+
expect(desc.promptDetails.sessionLabel).toBeDefined();
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it("populates logContext with tool input preview fields", () => {
|
|
201
|
+
const check = makeCheckResult("ask", { toolName: "bash", command: "ls" });
|
|
202
|
+
const desc = describeToolGate(
|
|
203
|
+
makeTcc({ toolName: "bash", input: { command: "ls" } }),
|
|
204
|
+
check,
|
|
205
|
+
makeFormatter(),
|
|
206
|
+
);
|
|
207
|
+
expect(desc.logContext).toMatchObject({
|
|
208
|
+
source: "tool_call",
|
|
209
|
+
toolName: "bash",
|
|
210
|
+
});
|
|
211
|
+
expect(desc.logContext.command).toBe("ls");
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
it("uses toolName as input for checkPermission surface", () => {
|
|
215
|
+
const desc = describeToolGate(
|
|
216
|
+
makeTcc({ toolName: "edit", input: { path: "/a.ts" } }),
|
|
217
|
+
makeCheckResult("ask", { toolName: "edit" }),
|
|
218
|
+
makeFormatter(),
|
|
219
|
+
);
|
|
220
|
+
expect(desc.surface).toBe("edit");
|
|
221
|
+
expect(desc.input).toEqual({ path: "/a.ts" });
|
|
222
|
+
});
|
|
223
|
+
});
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests that handleInput emits permissions:decision events for skill input gates.
|
|
3
|
+
*/
|
|
4
|
+
import { describe, expect, it, vi } from "vitest";
|
|
5
|
+
|
|
6
|
+
import type { GatePrompter } from "#src/gate-prompter";
|
|
7
|
+
import {
|
|
8
|
+
getDecisionEvents,
|
|
9
|
+
makeCheckResult,
|
|
10
|
+
makeCtx,
|
|
11
|
+
makeHandler,
|
|
12
|
+
} from "#test/helpers/handler-fixtures";
|
|
13
|
+
|
|
14
|
+
// ── helpers ────────────────────────────────────────────────────────────────
|
|
15
|
+
|
|
16
|
+
/** Build a checkPermission mock returning a skill-surface result. */
|
|
17
|
+
function makeSkillCheckPermission(state: "allow" | "deny" | "ask") {
|
|
18
|
+
return vi.fn().mockReturnValue(
|
|
19
|
+
makeCheckResult({
|
|
20
|
+
state,
|
|
21
|
+
toolName: "skill",
|
|
22
|
+
source: "skill",
|
|
23
|
+
origin: "global",
|
|
24
|
+
matchedPattern: "*",
|
|
25
|
+
}),
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// ── tests ──────────────────────────────────────────────────────────────────
|
|
30
|
+
|
|
31
|
+
describe("handleInput decision events — skill gate", () => {
|
|
32
|
+
it("does not emit when input is not a skill invocation", async () => {
|
|
33
|
+
const { handler, events } = makeHandler();
|
|
34
|
+
await handler.handleInput({ text: "hello world" }, makeCtx());
|
|
35
|
+
expect(getDecisionEvents(events)).toHaveLength(0);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("emits allow with policy_allow for an allowed skill", async () => {
|
|
39
|
+
const { handler, events } = makeHandler({
|
|
40
|
+
session: { checkPermission: makeSkillCheckPermission("allow") },
|
|
41
|
+
});
|
|
42
|
+
await handler.handleInput({ text: "/skill:librarian" }, makeCtx());
|
|
43
|
+
|
|
44
|
+
const decisions = getDecisionEvents(events);
|
|
45
|
+
expect(decisions).toHaveLength(1);
|
|
46
|
+
expect(decisions[0]).toMatchObject({
|
|
47
|
+
surface: "skill",
|
|
48
|
+
value: "librarian",
|
|
49
|
+
result: "allow",
|
|
50
|
+
resolution: "policy_allow",
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("emits deny with policy_deny for a denied skill", async () => {
|
|
55
|
+
const { handler, events } = makeHandler({
|
|
56
|
+
session: { checkPermission: makeSkillCheckPermission("deny") },
|
|
57
|
+
});
|
|
58
|
+
await handler.handleInput({ text: "/skill:restricted" }, makeCtx());
|
|
59
|
+
|
|
60
|
+
const decisions = getDecisionEvents(events);
|
|
61
|
+
expect(decisions).toHaveLength(1);
|
|
62
|
+
expect(decisions[0]).toMatchObject({
|
|
63
|
+
surface: "skill",
|
|
64
|
+
value: "restricted",
|
|
65
|
+
result: "deny",
|
|
66
|
+
resolution: "policy_deny",
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("emits allow with user_approved when state=ask and user approves", async () => {
|
|
71
|
+
const { handler, events } = makeHandler({
|
|
72
|
+
session: {
|
|
73
|
+
checkPermission: makeSkillCheckPermission("ask"),
|
|
74
|
+
},
|
|
75
|
+
prompter: {
|
|
76
|
+
canConfirm: vi.fn().mockReturnValue(true),
|
|
77
|
+
prompt: vi
|
|
78
|
+
.fn<GatePrompter["prompt"]>()
|
|
79
|
+
.mockResolvedValue({ approved: true, state: "approved" }),
|
|
80
|
+
},
|
|
81
|
+
});
|
|
82
|
+
await handler.handleInput({ text: "/skill:explorer" }, makeCtx());
|
|
83
|
+
|
|
84
|
+
const decisions = getDecisionEvents(events);
|
|
85
|
+
expect(decisions).toHaveLength(1);
|
|
86
|
+
expect(decisions[0]).toMatchObject({
|
|
87
|
+
surface: "skill",
|
|
88
|
+
value: "explorer",
|
|
89
|
+
result: "allow",
|
|
90
|
+
resolution: "user_approved",
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it("emits deny with user_denied when state=ask and user denies", async () => {
|
|
95
|
+
const { handler, events } = makeHandler({
|
|
96
|
+
session: {
|
|
97
|
+
checkPermission: makeSkillCheckPermission("ask"),
|
|
98
|
+
},
|
|
99
|
+
prompter: {
|
|
100
|
+
canConfirm: vi.fn().mockReturnValue(true),
|
|
101
|
+
prompt: vi
|
|
102
|
+
.fn<GatePrompter["prompt"]>()
|
|
103
|
+
.mockResolvedValue({ approved: false, state: "denied" }),
|
|
104
|
+
},
|
|
105
|
+
});
|
|
106
|
+
await handler.handleInput({ text: "/skill:explorer" }, makeCtx());
|
|
107
|
+
|
|
108
|
+
const decisions = getDecisionEvents(events);
|
|
109
|
+
expect(decisions).toHaveLength(1);
|
|
110
|
+
expect(decisions[0]).toMatchObject({
|
|
111
|
+
surface: "skill",
|
|
112
|
+
value: "explorer",
|
|
113
|
+
result: "deny",
|
|
114
|
+
resolution: "user_denied",
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it("emits deny with confirmation_unavailable when state=ask but no UI", async () => {
|
|
119
|
+
const { handler, events } = makeHandler({
|
|
120
|
+
session: {
|
|
121
|
+
checkPermission: makeSkillCheckPermission("ask"),
|
|
122
|
+
},
|
|
123
|
+
prompter: {
|
|
124
|
+
canConfirm: vi.fn().mockReturnValue(false),
|
|
125
|
+
prompt: vi.fn<GatePrompter["prompt"]>(),
|
|
126
|
+
},
|
|
127
|
+
});
|
|
128
|
+
await handler.handleInput(
|
|
129
|
+
{ text: "/skill:explorer" },
|
|
130
|
+
makeCtx({ hasUI: false }),
|
|
131
|
+
);
|
|
132
|
+
|
|
133
|
+
const decisions = getDecisionEvents(events);
|
|
134
|
+
expect(decisions).toHaveLength(1);
|
|
135
|
+
expect(decisions[0]).toMatchObject({
|
|
136
|
+
surface: "skill",
|
|
137
|
+
value: "explorer",
|
|
138
|
+
result: "deny",
|
|
139
|
+
resolution: "confirmation_unavailable",
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it("emits allow with auto_approved when prompt returns autoApproved:true", async () => {
|
|
144
|
+
const { handler, events } = makeHandler({
|
|
145
|
+
session: {
|
|
146
|
+
checkPermission: makeSkillCheckPermission("ask"),
|
|
147
|
+
},
|
|
148
|
+
prompter: {
|
|
149
|
+
canConfirm: vi.fn().mockReturnValue(true),
|
|
150
|
+
prompt: vi.fn<GatePrompter["prompt"]>().mockResolvedValue({
|
|
151
|
+
approved: true,
|
|
152
|
+
state: "approved",
|
|
153
|
+
autoApproved: true,
|
|
154
|
+
}),
|
|
155
|
+
},
|
|
156
|
+
});
|
|
157
|
+
await handler.handleInput({ text: "/skill:explorer" }, makeCtx());
|
|
158
|
+
|
|
159
|
+
const decisions = getDecisionEvents(events);
|
|
160
|
+
expect(decisions).toHaveLength(1);
|
|
161
|
+
expect(decisions[0]).toMatchObject({
|
|
162
|
+
surface: "skill",
|
|
163
|
+
value: "explorer",
|
|
164
|
+
result: "allow",
|
|
165
|
+
resolution: "auto_approved",
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
});
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from "vitest";
|
|
2
|
+
import type { GatePrompter } from "#src/gate-prompter";
|
|
3
|
+
import { extractSkillNameFromInput } from "#src/handlers/permission-gate-handler";
|
|
4
|
+
|
|
5
|
+
import { makeCtx, makeHandler } from "#test/helpers/handler-fixtures";
|
|
6
|
+
|
|
7
|
+
// ── helpers ────────────────────────────────────────────────────────────────
|
|
8
|
+
|
|
9
|
+
function makeInputEvent(text: string) {
|
|
10
|
+
return { text };
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// ── extractSkillNameFromInput ──────────────────────────────────────────────
|
|
14
|
+
|
|
15
|
+
describe("extractSkillNameFromInput", () => {
|
|
16
|
+
it("returns null for plain text", () => {
|
|
17
|
+
expect(extractSkillNameFromInput("hello world")).toBeNull();
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("returns null for empty string", () => {
|
|
21
|
+
expect(extractSkillNameFromInput("")).toBeNull();
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it("returns null for bare /skill: with no name", () => {
|
|
25
|
+
expect(extractSkillNameFromInput("/skill:")).toBeNull();
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("extracts skill name from /skill:<name>", () => {
|
|
29
|
+
expect(extractSkillNameFromInput("/skill:librarian")).toBe("librarian");
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("extracts skill name stopping at whitespace", () => {
|
|
33
|
+
expect(extractSkillNameFromInput("/skill:librarian some extra")).toBe(
|
|
34
|
+
"librarian",
|
|
35
|
+
);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("trims leading whitespace before the prefix", () => {
|
|
39
|
+
expect(extractSkillNameFromInput(" /skill:my-skill")).toBe("my-skill");
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("returns null when the skill name after trimming is empty", () => {
|
|
43
|
+
expect(extractSkillNameFromInput("/skill: ")).toBeNull();
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
// ── handleInput ───────────────────────────────────────────────────────────
|
|
48
|
+
|
|
49
|
+
describe("handleInput", () => {
|
|
50
|
+
it("activates session with ctx", async () => {
|
|
51
|
+
const ctx = makeCtx();
|
|
52
|
+
const { handler, forwarding } = makeHandler();
|
|
53
|
+
await handler.handleInput(makeInputEvent("hello"), ctx);
|
|
54
|
+
// session.activate(ctx) calls forwarding.start(ctx) on the real session
|
|
55
|
+
expect(forwarding.start).toHaveBeenCalledWith(ctx);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("returns continue for non-skill input", async () => {
|
|
59
|
+
const { handler } = makeHandler();
|
|
60
|
+
const result = await handler.handleInput(
|
|
61
|
+
makeInputEvent("just a message"),
|
|
62
|
+
makeCtx(),
|
|
63
|
+
);
|
|
64
|
+
expect(result).toEqual({ action: "continue" });
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("does not check permissions for non-skill input", async () => {
|
|
68
|
+
const { handler, permissionManager } = makeHandler();
|
|
69
|
+
await handler.handleInput(makeInputEvent("just a message"), makeCtx());
|
|
70
|
+
expect(permissionManager.checkPermission).not.toHaveBeenCalled();
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("returns continue when skill is allowed", async () => {
|
|
74
|
+
const { handler } = makeHandler();
|
|
75
|
+
const result = await handler.handleInput(
|
|
76
|
+
makeInputEvent("/skill:librarian"),
|
|
77
|
+
makeCtx(),
|
|
78
|
+
);
|
|
79
|
+
expect(result).toEqual({ action: "continue" });
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("returns handled when skill is denied", async () => {
|
|
83
|
+
const { handler } = makeHandler({
|
|
84
|
+
session: {
|
|
85
|
+
checkPermission: vi.fn().mockReturnValue({ state: "deny" }),
|
|
86
|
+
},
|
|
87
|
+
});
|
|
88
|
+
const result = await handler.handleInput(
|
|
89
|
+
makeInputEvent("/skill:librarian"),
|
|
90
|
+
makeCtx(),
|
|
91
|
+
);
|
|
92
|
+
expect(result).toEqual({ action: "handled" });
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it("shows a warning notification when skill is denied and UI is available", async () => {
|
|
96
|
+
const ctx = makeCtx({ hasUI: true });
|
|
97
|
+
const { handler } = makeHandler({
|
|
98
|
+
session: {
|
|
99
|
+
checkPermission: vi.fn().mockReturnValue({ state: "deny" }),
|
|
100
|
+
},
|
|
101
|
+
});
|
|
102
|
+
await handler.handleInput(makeInputEvent("/skill:librarian"), ctx);
|
|
103
|
+
expect(ctx.ui.notify).toHaveBeenCalledWith(
|
|
104
|
+
expect.stringContaining("librarian"),
|
|
105
|
+
"warning",
|
|
106
|
+
);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it("does not show a warning notification when skill is denied and UI is absent", async () => {
|
|
110
|
+
const ctx = makeCtx({ hasUI: false });
|
|
111
|
+
const { handler } = makeHandler({
|
|
112
|
+
session: {
|
|
113
|
+
checkPermission: vi.fn().mockReturnValue({ state: "deny" }),
|
|
114
|
+
},
|
|
115
|
+
});
|
|
116
|
+
await handler.handleInput(makeInputEvent("/skill:librarian"), ctx);
|
|
117
|
+
expect(ctx.ui.notify).not.toHaveBeenCalled();
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it("returns handled when skill requires approval but no UI is available", async () => {
|
|
121
|
+
const { handler } = makeHandler({
|
|
122
|
+
session: {
|
|
123
|
+
checkPermission: vi.fn().mockReturnValue({ state: "ask" }),
|
|
124
|
+
},
|
|
125
|
+
prompter: {
|
|
126
|
+
canConfirm: vi.fn().mockReturnValue(false),
|
|
127
|
+
prompt: vi.fn<GatePrompter["prompt"]>(),
|
|
128
|
+
},
|
|
129
|
+
});
|
|
130
|
+
const result = await handler.handleInput(
|
|
131
|
+
makeInputEvent("/skill:librarian"),
|
|
132
|
+
makeCtx(),
|
|
133
|
+
);
|
|
134
|
+
expect(result).toEqual({ action: "handled" });
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it("prompts and returns continue when skill ask is approved", async () => {
|
|
138
|
+
const approvePrompt = vi
|
|
139
|
+
.fn<GatePrompter["prompt"]>()
|
|
140
|
+
.mockResolvedValue({ approved: true, state: "approved" });
|
|
141
|
+
const { handler, prompter } = makeHandler({
|
|
142
|
+
session: {
|
|
143
|
+
checkPermission: vi.fn().mockReturnValue({ state: "ask" }),
|
|
144
|
+
},
|
|
145
|
+
prompter: {
|
|
146
|
+
canConfirm: vi.fn().mockReturnValue(true),
|
|
147
|
+
prompt: approvePrompt,
|
|
148
|
+
},
|
|
149
|
+
});
|
|
150
|
+
const result = await handler.handleInput(
|
|
151
|
+
makeInputEvent("/skill:librarian"),
|
|
152
|
+
makeCtx(),
|
|
153
|
+
);
|
|
154
|
+
expect(result).toEqual({ action: "continue" });
|
|
155
|
+
expect(prompter.prompt).toHaveBeenCalledOnce();
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it("returns handled when skill ask is denied by user", async () => {
|
|
159
|
+
const { handler } = makeHandler({
|
|
160
|
+
session: {
|
|
161
|
+
checkPermission: vi.fn().mockReturnValue({ state: "ask" }),
|
|
162
|
+
},
|
|
163
|
+
prompter: {
|
|
164
|
+
canConfirm: vi.fn().mockReturnValue(true),
|
|
165
|
+
prompt: vi
|
|
166
|
+
.fn<GatePrompter["prompt"]>()
|
|
167
|
+
.mockResolvedValue({ approved: false, state: "denied" }),
|
|
168
|
+
},
|
|
169
|
+
});
|
|
170
|
+
const result = await handler.handleInput(
|
|
171
|
+
makeInputEvent("/skill:librarian"),
|
|
172
|
+
makeCtx(),
|
|
173
|
+
);
|
|
174
|
+
expect(result).toEqual({ action: "handled" });
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it("passes agentName in the prompt permission request", async () => {
|
|
178
|
+
const approvePrompt = vi
|
|
179
|
+
.fn<GatePrompter["prompt"]>()
|
|
180
|
+
.mockResolvedValue({ approved: true, state: "approved" });
|
|
181
|
+
const { handler, prompter } = makeHandler({
|
|
182
|
+
session: {
|
|
183
|
+
checkPermission: vi.fn().mockReturnValue({ state: "ask" }),
|
|
184
|
+
resolveAgentName: vi.fn().mockReturnValue("code-agent"),
|
|
185
|
+
},
|
|
186
|
+
prompter: {
|
|
187
|
+
canConfirm: vi.fn().mockReturnValue(true),
|
|
188
|
+
prompt: approvePrompt,
|
|
189
|
+
},
|
|
190
|
+
});
|
|
191
|
+
await handler.handleInput(makeInputEvent("/skill:librarian"), makeCtx());
|
|
192
|
+
expect(prompter.prompt).toHaveBeenCalledWith(
|
|
193
|
+
expect.objectContaining({
|
|
194
|
+
agentName: "code-agent",
|
|
195
|
+
skillName: "librarian",
|
|
196
|
+
}),
|
|
197
|
+
);
|
|
198
|
+
});
|
|
199
|
+
});
|