@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,458 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
|
|
2
|
+
|
|
3
|
+
import type { ToolInputFormatterLookup } from "#src/tool-input-formatter-registry";
|
|
4
|
+
|
|
5
|
+
// Mock logging collaborator before importing the module under test.
|
|
6
|
+
vi.mock("../src/logging.js", () => ({
|
|
7
|
+
safeJsonStringify: vi.fn((value: unknown) => JSON.stringify(value)),
|
|
8
|
+
}));
|
|
9
|
+
|
|
10
|
+
import { safeJsonStringify } from "#src/logging";
|
|
11
|
+
import {
|
|
12
|
+
TOOL_INPUT_LOG_PREVIEW_MAX_LENGTH,
|
|
13
|
+
TOOL_INPUT_PREVIEW_MAX_LENGTH,
|
|
14
|
+
TOOL_TEXT_SUMMARY_MAX_LENGTH,
|
|
15
|
+
} from "#src/tool-input-preview";
|
|
16
|
+
import {
|
|
17
|
+
resolveToolPreviewLimits,
|
|
18
|
+
ToolPreviewFormatter,
|
|
19
|
+
type ToolPreviewFormatterOptions,
|
|
20
|
+
} from "#src/tool-preview-formatter";
|
|
21
|
+
import type { PermissionCheckResult } from "#src/types";
|
|
22
|
+
|
|
23
|
+
const mockedStringify = vi.mocked(safeJsonStringify);
|
|
24
|
+
|
|
25
|
+
function makeFormatter(
|
|
26
|
+
overrides: Partial<ToolPreviewFormatterOptions> = {},
|
|
27
|
+
): ToolPreviewFormatter {
|
|
28
|
+
return new ToolPreviewFormatter({
|
|
29
|
+
toolInputPreviewMaxLength: TOOL_INPUT_PREVIEW_MAX_LENGTH,
|
|
30
|
+
toolTextSummaryMaxLength: TOOL_TEXT_SUMMARY_MAX_LENGTH,
|
|
31
|
+
toolInputLogPreviewMaxLength: TOOL_INPUT_LOG_PREVIEW_MAX_LENGTH,
|
|
32
|
+
...overrides,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function makeResult(
|
|
37
|
+
toolName: string,
|
|
38
|
+
overrides: Partial<PermissionCheckResult> = {},
|
|
39
|
+
): PermissionCheckResult {
|
|
40
|
+
return {
|
|
41
|
+
toolName,
|
|
42
|
+
state: "allow",
|
|
43
|
+
source: "tool",
|
|
44
|
+
origin: "builtin",
|
|
45
|
+
...overrides,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
beforeEach(() => {
|
|
50
|
+
mockedStringify.mockReset();
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
afterEach(() => {
|
|
54
|
+
vi.restoreAllMocks();
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
// ── sanitizeInlineText ────────────────────────────────────────────────────
|
|
58
|
+
|
|
59
|
+
describe("ToolPreviewFormatter.sanitizeInlineText", () => {
|
|
60
|
+
test("collapses whitespace and trims", () => {
|
|
61
|
+
const f = makeFormatter();
|
|
62
|
+
expect(f.sanitizeInlineText(" hello world ")).toBe("hello world");
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test("returns 'empty text' for blank string", () => {
|
|
66
|
+
const f = makeFormatter();
|
|
67
|
+
expect(f.sanitizeInlineText("")).toBe("empty text");
|
|
68
|
+
expect(f.sanitizeInlineText(" ")).toBe("empty text");
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test("truncates at constructor toolTextSummaryMaxLength", () => {
|
|
72
|
+
const f = makeFormatter({ toolTextSummaryMaxLength: 5 });
|
|
73
|
+
const result = f.sanitizeInlineText("hello world");
|
|
74
|
+
expect(result).toBe("hello…");
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test("explicit maxLength override takes precedence over constructor default", () => {
|
|
78
|
+
const f = makeFormatter({ toolTextSummaryMaxLength: 80 });
|
|
79
|
+
const result = f.sanitizeInlineText("hello world", 5);
|
|
80
|
+
expect(result).toBe("hello…");
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
// ── formatJsonInputForPrompt ──────────────────────────────────────────────
|
|
85
|
+
|
|
86
|
+
describe("ToolPreviewFormatter.formatJsonInputForPrompt", () => {
|
|
87
|
+
test("returns empty string when serialization yields empty", () => {
|
|
88
|
+
mockedStringify.mockReturnValue(undefined);
|
|
89
|
+
const f = makeFormatter();
|
|
90
|
+
expect(f.formatJsonInputForPrompt({})).toBe("");
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
test("returns prefixed JSON with 'with input' prefix", () => {
|
|
94
|
+
mockedStringify.mockReturnValue('{"k":"v"}');
|
|
95
|
+
const f = makeFormatter();
|
|
96
|
+
expect(f.formatJsonInputForPrompt({ k: "v" })).toBe('with input {"k":"v"}');
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
test("truncates at constructor toolInputPreviewMaxLength", () => {
|
|
100
|
+
const longJson = `"${"x".repeat(20)}"`;
|
|
101
|
+
mockedStringify.mockReturnValue(longJson);
|
|
102
|
+
const f = makeFormatter({ toolInputPreviewMaxLength: 10 });
|
|
103
|
+
const result = f.formatJsonInputForPrompt({});
|
|
104
|
+
// "with input " + 10 chars + ellipsis
|
|
105
|
+
const preview = result.slice("with input ".length);
|
|
106
|
+
expect(preview.length).toBe(11); // 10 + 1 for "…"
|
|
107
|
+
expect(preview.endsWith("…")).toBe(true);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
test("does not truncate when within toolInputPreviewMaxLength", () => {
|
|
111
|
+
mockedStringify.mockReturnValue('{"k":"v"}');
|
|
112
|
+
const f = makeFormatter({ toolInputPreviewMaxLength: 200 });
|
|
113
|
+
expect(f.formatJsonInputForPrompt({ k: "v" })).toBe('with input {"k":"v"}');
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
// ── formatSearchInputForPrompt ────────────────────────────────────────────
|
|
118
|
+
|
|
119
|
+
describe("ToolPreviewFormatter.formatSearchInputForPrompt", () => {
|
|
120
|
+
test("includes pattern and path", () => {
|
|
121
|
+
const f = makeFormatter();
|
|
122
|
+
const result = f.formatSearchInputForPrompt("grep", {
|
|
123
|
+
pattern: "TODO",
|
|
124
|
+
path: "/src",
|
|
125
|
+
});
|
|
126
|
+
expect(result).toContain("pattern 'TODO'");
|
|
127
|
+
expect(result).toContain("path '/src'");
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
test("truncates pattern at toolTextSummaryMaxLength", () => {
|
|
131
|
+
const f = makeFormatter({ toolTextSummaryMaxLength: 5 });
|
|
132
|
+
const result = f.formatSearchInputForPrompt("grep", {
|
|
133
|
+
pattern: "abcdefgh",
|
|
134
|
+
});
|
|
135
|
+
expect(result).toContain("abcde…");
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
test("uses 'current working directory' for find/grep/ls without path", () => {
|
|
139
|
+
const f = makeFormatter();
|
|
140
|
+
for (const toolName of ["find", "grep", "ls"]) {
|
|
141
|
+
const result = f.formatSearchInputForPrompt(toolName, {});
|
|
142
|
+
expect(result).toContain("current working directory");
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
test("returns empty string for unknown tool with no input", () => {
|
|
147
|
+
const f = makeFormatter();
|
|
148
|
+
expect(f.formatSearchInputForPrompt("other", {})).toBe("");
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
// ── formatToolInputForPrompt ──────────────────────────────────────────────
|
|
153
|
+
|
|
154
|
+
describe("ToolPreviewFormatter.formatToolInputForPrompt", () => {
|
|
155
|
+
test("dispatches 'edit' to standalone formatEditInputForPrompt", () => {
|
|
156
|
+
mockedStringify.mockReturnValue(undefined);
|
|
157
|
+
const f = makeFormatter();
|
|
158
|
+
const result = f.formatToolInputForPrompt("edit", {
|
|
159
|
+
path: "/foo.ts",
|
|
160
|
+
edits: [],
|
|
161
|
+
});
|
|
162
|
+
expect(result).toContain("for '/foo.ts'");
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
test("dispatches 'write' to standalone formatWriteInputForPrompt", () => {
|
|
166
|
+
const f = makeFormatter();
|
|
167
|
+
const result = f.formatToolInputForPrompt("write", {
|
|
168
|
+
path: "/out.ts",
|
|
169
|
+
content: "hi",
|
|
170
|
+
});
|
|
171
|
+
expect(result).toContain("for '/out.ts'");
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
test("dispatches 'read' to standalone formatReadInputForPrompt", () => {
|
|
175
|
+
const f = makeFormatter();
|
|
176
|
+
const result = f.formatToolInputForPrompt("read", { path: "/src/x.ts" });
|
|
177
|
+
expect(result).toContain("path '/src/x.ts'");
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
test("dispatches 'find'/'grep'/'ls' to formatSearchInputForPrompt", () => {
|
|
181
|
+
const f = makeFormatter();
|
|
182
|
+
for (const tool of ["find", "grep", "ls"]) {
|
|
183
|
+
const result = f.formatToolInputForPrompt(tool, {});
|
|
184
|
+
expect(result).toContain("current working directory");
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
test("falls back to formatJsonInputForPrompt for unknown tools", () => {
|
|
189
|
+
mockedStringify.mockReturnValue('{"x":1}');
|
|
190
|
+
const f = makeFormatter();
|
|
191
|
+
const result = f.formatToolInputForPrompt("unknown", { x: 1 });
|
|
192
|
+
expect(result).toContain('{"x":1}');
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
test("unknown tool truncates at constructor toolInputPreviewMaxLength", () => {
|
|
196
|
+
const longJson = `{"k":"${"x".repeat(50)}"}`;
|
|
197
|
+
mockedStringify.mockReturnValue(longJson);
|
|
198
|
+
const f = makeFormatter({ toolInputPreviewMaxLength: 10 });
|
|
199
|
+
const result = f.formatToolInputForPrompt("custom", {});
|
|
200
|
+
const preview = result.slice("with input ".length);
|
|
201
|
+
expect(preview.endsWith("…")).toBe(true);
|
|
202
|
+
expect(preview.length).toBe(11); // 10 + "…"
|
|
203
|
+
});
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
// ── formatToolInputForPrompt (custom formatter seam) ───────────────────────
|
|
207
|
+
|
|
208
|
+
describe("ToolPreviewFormatter.formatToolInputForPrompt — custom formatter seam", () => {
|
|
209
|
+
function makeLookup(
|
|
210
|
+
toolName: string,
|
|
211
|
+
result: string | undefined,
|
|
212
|
+
): ToolInputFormatterLookup {
|
|
213
|
+
return {
|
|
214
|
+
get: (name) => (name === toolName ? () => result : undefined),
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
test("uses a custom formatter's string result verbatim, bypassing the switch", () => {
|
|
219
|
+
const lookup = makeLookup("my-tool", "custom preview");
|
|
220
|
+
const f = new ToolPreviewFormatter(
|
|
221
|
+
{
|
|
222
|
+
toolInputPreviewMaxLength: TOOL_INPUT_PREVIEW_MAX_LENGTH,
|
|
223
|
+
toolTextSummaryMaxLength: TOOL_TEXT_SUMMARY_MAX_LENGTH,
|
|
224
|
+
toolInputLogPreviewMaxLength: TOOL_INPUT_LOG_PREVIEW_MAX_LENGTH,
|
|
225
|
+
},
|
|
226
|
+
lookup,
|
|
227
|
+
);
|
|
228
|
+
expect(f.formatToolInputForPrompt("my-tool", {})).toBe("custom preview");
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
test("falls through to the built-in switch when custom formatter returns undefined", () => {
|
|
232
|
+
mockedStringify.mockReturnValue('{"x":1}');
|
|
233
|
+
const lookup = makeLookup("unknown-tool", undefined);
|
|
234
|
+
const f = new ToolPreviewFormatter(
|
|
235
|
+
{
|
|
236
|
+
toolInputPreviewMaxLength: TOOL_INPUT_PREVIEW_MAX_LENGTH,
|
|
237
|
+
toolTextSummaryMaxLength: TOOL_TEXT_SUMMARY_MAX_LENGTH,
|
|
238
|
+
toolInputLogPreviewMaxLength: TOOL_INPUT_LOG_PREVIEW_MAX_LENGTH,
|
|
239
|
+
},
|
|
240
|
+
lookup,
|
|
241
|
+
);
|
|
242
|
+
// Falls through to JSON default for unknown tools
|
|
243
|
+
expect(f.formatToolInputForPrompt("unknown-tool", { x: 1 })).toContain(
|
|
244
|
+
'{"x":1}',
|
|
245
|
+
);
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
test("custom formatter for a built-in tool overrides the built-in preview", () => {
|
|
249
|
+
const lookup = makeLookup("read", "custom read summary");
|
|
250
|
+
const f = new ToolPreviewFormatter(
|
|
251
|
+
{
|
|
252
|
+
toolInputPreviewMaxLength: TOOL_INPUT_PREVIEW_MAX_LENGTH,
|
|
253
|
+
toolTextSummaryMaxLength: TOOL_TEXT_SUMMARY_MAX_LENGTH,
|
|
254
|
+
toolInputLogPreviewMaxLength: TOOL_INPUT_LOG_PREVIEW_MAX_LENGTH,
|
|
255
|
+
},
|
|
256
|
+
lookup,
|
|
257
|
+
);
|
|
258
|
+
// Would normally use formatReadInputForPrompt; custom overrides it
|
|
259
|
+
expect(f.formatToolInputForPrompt("read", { path: "/foo.ts" })).toBe(
|
|
260
|
+
"custom read summary",
|
|
261
|
+
);
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
test("absent lookup preserves current behaviour for all tool types", () => {
|
|
265
|
+
const f = new ToolPreviewFormatter({
|
|
266
|
+
toolInputPreviewMaxLength: TOOL_INPUT_PREVIEW_MAX_LENGTH,
|
|
267
|
+
toolTextSummaryMaxLength: TOOL_TEXT_SUMMARY_MAX_LENGTH,
|
|
268
|
+
toolInputLogPreviewMaxLength: TOOL_INPUT_LOG_PREVIEW_MAX_LENGTH,
|
|
269
|
+
});
|
|
270
|
+
// Built-in path still works
|
|
271
|
+
expect(f.formatToolInputForPrompt("read", { path: "/foo.ts" })).toContain(
|
|
272
|
+
"/foo.ts",
|
|
273
|
+
);
|
|
274
|
+
});
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
// ── formatGenericToolInputForLog ──────────────────────────────────────────
|
|
278
|
+
|
|
279
|
+
describe("ToolPreviewFormatter.formatGenericToolInputForLog", () => {
|
|
280
|
+
test("returns undefined when serialization yields empty string", () => {
|
|
281
|
+
mockedStringify.mockReturnValue(undefined);
|
|
282
|
+
const f = makeFormatter();
|
|
283
|
+
expect(f.formatGenericToolInputForLog({})).toBeUndefined();
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
test("returns prefixed input preview", () => {
|
|
287
|
+
mockedStringify.mockReturnValue('{"k":"v"}');
|
|
288
|
+
const f = makeFormatter();
|
|
289
|
+
expect(f.formatGenericToolInputForLog({ k: "v" })).toBe('input {"k":"v"}');
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
test("truncates at constructor toolInputLogPreviewMaxLength", () => {
|
|
293
|
+
const longJson = `{"k":"${"x".repeat(50)}"}`;
|
|
294
|
+
mockedStringify.mockReturnValue(longJson);
|
|
295
|
+
const f = makeFormatter({ toolInputLogPreviewMaxLength: 10 });
|
|
296
|
+
const result = f.formatGenericToolInputForLog({});
|
|
297
|
+
expect(result).toBeDefined();
|
|
298
|
+
const preview = result!.slice("input ".length);
|
|
299
|
+
expect(preview.length).toBe(11); // 10 + "…"
|
|
300
|
+
expect(preview.endsWith("…")).toBe(true);
|
|
301
|
+
});
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
// ── getToolInputPreviewForLog ─────────────────────────────────────────────
|
|
305
|
+
|
|
306
|
+
describe("ToolPreviewFormatter.getToolInputPreviewForLog", () => {
|
|
307
|
+
const pathBearingTools = new Set(["read", "write", "edit"]);
|
|
308
|
+
|
|
309
|
+
test("returns undefined for bash tool", () => {
|
|
310
|
+
const f = makeFormatter();
|
|
311
|
+
expect(
|
|
312
|
+
f.getToolInputPreviewForLog(
|
|
313
|
+
makeResult("bash"),
|
|
314
|
+
{ command: "ls" },
|
|
315
|
+
pathBearingTools,
|
|
316
|
+
),
|
|
317
|
+
).toBeUndefined();
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
test("returns undefined for mcp tool", () => {
|
|
321
|
+
const f = makeFormatter();
|
|
322
|
+
expect(
|
|
323
|
+
f.getToolInputPreviewForLog(makeResult("mcp"), {}, pathBearingTools),
|
|
324
|
+
).toBeUndefined();
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
test("returns undefined for mcp source", () => {
|
|
328
|
+
const f = makeFormatter();
|
|
329
|
+
const result = makeResult("some-server:some-tool", { source: "mcp" });
|
|
330
|
+
expect(
|
|
331
|
+
f.getToolInputPreviewForLog(result, {}, pathBearingTools),
|
|
332
|
+
).toBeUndefined();
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
test("returns path-based preview for path-bearing tools", () => {
|
|
336
|
+
const f = makeFormatter();
|
|
337
|
+
const preview = f.getToolInputPreviewForLog(
|
|
338
|
+
makeResult("read"),
|
|
339
|
+
{ path: "/src/foo.ts" },
|
|
340
|
+
pathBearingTools,
|
|
341
|
+
);
|
|
342
|
+
expect(preview).toContain("/src/foo.ts");
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
test("truncates path preview at toolInputLogPreviewMaxLength", () => {
|
|
346
|
+
const f = makeFormatter({ toolInputLogPreviewMaxLength: 15 });
|
|
347
|
+
const longPath = `/src/${"a".repeat(50)}.ts`;
|
|
348
|
+
const preview = f.getToolInputPreviewForLog(
|
|
349
|
+
makeResult("read"),
|
|
350
|
+
{ path: longPath },
|
|
351
|
+
pathBearingTools,
|
|
352
|
+
);
|
|
353
|
+
expect(preview).toBeDefined();
|
|
354
|
+
expect(preview!.length).toBeLessThanOrEqual(16); // 15 + "…"
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
test("returns generic JSON preview for non-path-bearing tools", () => {
|
|
358
|
+
mockedStringify.mockReturnValue('{"n":1}');
|
|
359
|
+
const f = makeFormatter();
|
|
360
|
+
const preview = f.getToolInputPreviewForLog(
|
|
361
|
+
makeResult("task"),
|
|
362
|
+
{ n: 1 },
|
|
363
|
+
pathBearingTools,
|
|
364
|
+
);
|
|
365
|
+
expect(preview).toContain('{"n":1}');
|
|
366
|
+
});
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
// ── getPermissionLogContext ───────────────────────────────────────────────
|
|
370
|
+
|
|
371
|
+
describe("ToolPreviewFormatter.getPermissionLogContext", () => {
|
|
372
|
+
const pathBearingTools = new Set(["read", "write", "edit"]);
|
|
373
|
+
|
|
374
|
+
test("returns command, target, toolInputPreview, and origin fields", () => {
|
|
375
|
+
const f = makeFormatter();
|
|
376
|
+
const result = makeResult("bash", { command: "ls -la" });
|
|
377
|
+
const ctx = f.getPermissionLogContext(result, {}, pathBearingTools);
|
|
378
|
+
expect(ctx.command).toBe("ls -la");
|
|
379
|
+
expect(ctx.target).toBeUndefined();
|
|
380
|
+
expect(ctx.toolInputPreview).toBeUndefined();
|
|
381
|
+
});
|
|
382
|
+
|
|
383
|
+
test("includes toolInputPreview for non-bash path-bearing tools", () => {
|
|
384
|
+
const f = makeFormatter();
|
|
385
|
+
const result = makeResult("read");
|
|
386
|
+
const ctx = f.getPermissionLogContext(
|
|
387
|
+
result,
|
|
388
|
+
{ path: "/foo.ts" },
|
|
389
|
+
pathBearingTools,
|
|
390
|
+
);
|
|
391
|
+
expect(ctx.toolInputPreview).toContain("/foo.ts");
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
test("includes origin from check result", () => {
|
|
395
|
+
const f = makeFormatter();
|
|
396
|
+
const result = makeResult("read", { origin: "project" });
|
|
397
|
+
const ctx = f.getPermissionLogContext(result, {}, pathBearingTools);
|
|
398
|
+
expect(ctx.origin).toBe("project");
|
|
399
|
+
});
|
|
400
|
+
|
|
401
|
+
test("toolInputPreview respects toolInputLogPreviewMaxLength", () => {
|
|
402
|
+
const f = makeFormatter({ toolInputLogPreviewMaxLength: 15 });
|
|
403
|
+
const longPath = `/src/${"a".repeat(50)}.ts`;
|
|
404
|
+
const ctx = f.getPermissionLogContext(
|
|
405
|
+
makeResult("read"),
|
|
406
|
+
{ path: longPath },
|
|
407
|
+
pathBearingTools,
|
|
408
|
+
);
|
|
409
|
+
expect(ctx.toolInputPreview).toBeDefined();
|
|
410
|
+
expect(ctx.toolInputPreview!.length).toBeLessThanOrEqual(16);
|
|
411
|
+
});
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
// ── resolveToolPreviewLimits ───────────────────────────────────────────────
|
|
415
|
+
|
|
416
|
+
describe("resolveToolPreviewLimits", () => {
|
|
417
|
+
test("uses configured toolInputPreviewMaxLength when provided", () => {
|
|
418
|
+
const opts = resolveToolPreviewLimits({ toolInputPreviewMaxLength: 400 });
|
|
419
|
+
expect(opts.toolInputPreviewMaxLength).toBe(400);
|
|
420
|
+
});
|
|
421
|
+
|
|
422
|
+
test("falls back to TOOL_INPUT_PREVIEW_MAX_LENGTH when toolInputPreviewMaxLength is absent", () => {
|
|
423
|
+
const opts = resolveToolPreviewLimits({});
|
|
424
|
+
expect(opts.toolInputPreviewMaxLength).toBe(TOOL_INPUT_PREVIEW_MAX_LENGTH);
|
|
425
|
+
});
|
|
426
|
+
|
|
427
|
+
test("uses configured toolTextSummaryMaxLength when provided", () => {
|
|
428
|
+
const opts = resolveToolPreviewLimits({ toolTextSummaryMaxLength: 120 });
|
|
429
|
+
expect(opts.toolTextSummaryMaxLength).toBe(120);
|
|
430
|
+
});
|
|
431
|
+
|
|
432
|
+
test("falls back to TOOL_TEXT_SUMMARY_MAX_LENGTH when toolTextSummaryMaxLength is absent", () => {
|
|
433
|
+
const opts = resolveToolPreviewLimits({});
|
|
434
|
+
expect(opts.toolTextSummaryMaxLength).toBe(TOOL_TEXT_SUMMARY_MAX_LENGTH);
|
|
435
|
+
});
|
|
436
|
+
|
|
437
|
+
test("always sets toolInputLogPreviewMaxLength to TOOL_INPUT_LOG_PREVIEW_MAX_LENGTH", () => {
|
|
438
|
+
const opts = resolveToolPreviewLimits({
|
|
439
|
+
toolInputPreviewMaxLength: 999,
|
|
440
|
+
toolTextSummaryMaxLength: 999,
|
|
441
|
+
});
|
|
442
|
+
expect(opts.toolInputLogPreviewMaxLength).toBe(
|
|
443
|
+
TOOL_INPUT_LOG_PREVIEW_MAX_LENGTH,
|
|
444
|
+
);
|
|
445
|
+
});
|
|
446
|
+
|
|
447
|
+
test("returns all three options when both fields are configured", () => {
|
|
448
|
+
const opts = resolveToolPreviewLimits({
|
|
449
|
+
toolInputPreviewMaxLength: 400,
|
|
450
|
+
toolTextSummaryMaxLength: 120,
|
|
451
|
+
});
|
|
452
|
+
expect(opts).toEqual({
|
|
453
|
+
toolInputPreviewMaxLength: 400,
|
|
454
|
+
toolTextSummaryMaxLength: 120,
|
|
455
|
+
toolInputLogPreviewMaxLength: TOOL_INPUT_LOG_PREVIEW_MAX_LENGTH,
|
|
456
|
+
});
|
|
457
|
+
});
|
|
458
|
+
});
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { afterEach, describe, expect, test, vi } from "vitest";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
checkRequestedToolRegistration,
|
|
5
|
+
getToolNameFromValue,
|
|
6
|
+
} from "#src/tool-registry";
|
|
7
|
+
|
|
8
|
+
afterEach(() => {
|
|
9
|
+
vi.restoreAllMocks();
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
describe("getToolNameFromValue", () => {
|
|
13
|
+
test("returns string value directly", () => {
|
|
14
|
+
expect(getToolNameFromValue("read")).toBe("read");
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
test("returns null for empty string", () => {
|
|
18
|
+
expect(getToolNameFromValue("")).toBeNull();
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
test("returns null for whitespace-only string", () => {
|
|
22
|
+
expect(getToolNameFromValue(" ")).toBeNull();
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test("returns null for null", () => {
|
|
26
|
+
expect(getToolNameFromValue(null)).toBeNull();
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test("returns null for undefined", () => {
|
|
30
|
+
expect(getToolNameFromValue(undefined)).toBeNull();
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
test("extracts toolName from object", () => {
|
|
34
|
+
expect(getToolNameFromValue({ toolName: "write" })).toBe("write");
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test("extracts name from object", () => {
|
|
38
|
+
expect(getToolNameFromValue({ name: "edit" })).toBe("edit");
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test("extracts tool from object", () => {
|
|
42
|
+
expect(getToolNameFromValue({ tool: "bash" })).toBe("bash");
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
test("prefers toolName over name over tool", () => {
|
|
46
|
+
expect(
|
|
47
|
+
getToolNameFromValue({
|
|
48
|
+
toolName: "first",
|
|
49
|
+
name: "second",
|
|
50
|
+
tool: "third",
|
|
51
|
+
}),
|
|
52
|
+
).toBe("first");
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test("falls back to name when toolName is empty", () => {
|
|
56
|
+
expect(getToolNameFromValue({ toolName: "", name: "edit" })).toBe("edit");
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
test("returns null for object with no recognised keys", () => {
|
|
60
|
+
expect(getToolNameFromValue({ unknown: "read" })).toBeNull();
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test("returns null for number input", () => {
|
|
64
|
+
expect(getToolNameFromValue(42)).toBeNull();
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
describe("checkRequestedToolRegistration", () => {
|
|
69
|
+
test("returns missing-tool-name for null requested name", () => {
|
|
70
|
+
const result = checkRequestedToolRegistration(null, []);
|
|
71
|
+
expect(result.status).toBe("missing-tool-name");
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test("returns missing-tool-name for whitespace-only requested name", () => {
|
|
75
|
+
const result = checkRequestedToolRegistration(" ", []);
|
|
76
|
+
expect(result.status).toBe("missing-tool-name");
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test("returns registered when tool name matches a string entry", () => {
|
|
80
|
+
const result = checkRequestedToolRegistration("read", ["read", "write"]);
|
|
81
|
+
expect(result.status).toBe("registered");
|
|
82
|
+
if (result.status === "registered") {
|
|
83
|
+
expect(result.requestedToolName).toBe("read");
|
|
84
|
+
expect(result.normalizedToolName).toBe("read");
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
test("returns registered when tool name matches an object entry by name", () => {
|
|
89
|
+
const result = checkRequestedToolRegistration("edit", [{ name: "edit" }]);
|
|
90
|
+
expect(result.status).toBe("registered");
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
test("returns registered when tool name matches an object entry by toolName", () => {
|
|
94
|
+
const result = checkRequestedToolRegistration("bash", [
|
|
95
|
+
{ toolName: "bash" },
|
|
96
|
+
]);
|
|
97
|
+
expect(result.status).toBe("registered");
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
test("returns unregistered when tool is not in the list", () => {
|
|
101
|
+
const result = checkRequestedToolRegistration("ghost", ["read", "write"]);
|
|
102
|
+
expect(result.status).toBe("unregistered");
|
|
103
|
+
if (result.status === "unregistered") {
|
|
104
|
+
expect(result.requestedToolName).toBe("ghost");
|
|
105
|
+
expect(result.availableToolNames).toContain("read");
|
|
106
|
+
expect(result.availableToolNames).toContain("write");
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
test("available tool names are sorted alphabetically", () => {
|
|
111
|
+
const result = checkRequestedToolRegistration("ghost", [
|
|
112
|
+
"write",
|
|
113
|
+
"read",
|
|
114
|
+
"edit",
|
|
115
|
+
]);
|
|
116
|
+
if (result.status === "unregistered") {
|
|
117
|
+
expect(result.availableToolNames).toEqual(["edit", "read", "write"]);
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
test("resolves alias: requested alias maps to registered canonical name", () => {
|
|
122
|
+
const aliases = { Execute: "bash" };
|
|
123
|
+
const result = checkRequestedToolRegistration("Execute", ["bash"], aliases);
|
|
124
|
+
expect(result.status).toBe("registered");
|
|
125
|
+
if (result.status === "registered") {
|
|
126
|
+
expect(result.normalizedToolName).toBe("bash");
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
test("resolves alias: registered canonical is found via reverse alias lookup", () => {
|
|
131
|
+
// "bash" is registered; alias maps "Execute" → "bash"
|
|
132
|
+
// requesting "bash" directly should still resolve via the alias table
|
|
133
|
+
const aliases = { Execute: "bash" };
|
|
134
|
+
const result = checkRequestedToolRegistration("bash", ["bash"], aliases);
|
|
135
|
+
expect(result.status).toBe("registered");
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
test("returns unregistered with empty availableToolNames for empty tool list", () => {
|
|
139
|
+
const result = checkRequestedToolRegistration("read", []);
|
|
140
|
+
expect(result.status).toBe("unregistered");
|
|
141
|
+
if (result.status === "unregistered") {
|
|
142
|
+
expect(result.availableToolNames).toEqual([]);
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
test("skips tool list entries that yield no name", () => {
|
|
147
|
+
const result = checkRequestedToolRegistration("read", [
|
|
148
|
+
null,
|
|
149
|
+
{},
|
|
150
|
+
{ unrelated: "x" },
|
|
151
|
+
"read",
|
|
152
|
+
]);
|
|
153
|
+
expect(result.status).toBe("registered");
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
// ---------------------------------------------------------------------------
|
|
158
|
+
// Moved from permission-system.test.ts catch-all (#342)
|
|
159
|
+
// ---------------------------------------------------------------------------
|
|
160
|
+
|
|
161
|
+
test("Tool registry resolves event tool names from string and object payloads", () => {
|
|
162
|
+
expect(getToolNameFromValue(" read ")).toBe("read");
|
|
163
|
+
expect(getToolNameFromValue({ toolName: "write" })).toBe("write");
|
|
164
|
+
expect(getToolNameFromValue({ name: "find" })).toBe("find");
|
|
165
|
+
expect(getToolNameFromValue({ tool: "grep" })).toBe("grep");
|
|
166
|
+
expect(getToolNameFromValue({})).toBe(null);
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
test("Tool registry blocks unregistered tools and handles aliases", () => {
|
|
170
|
+
const registeredTools = [
|
|
171
|
+
{ toolName: "mcp" },
|
|
172
|
+
{ toolName: "read" },
|
|
173
|
+
{ toolName: "bash" },
|
|
174
|
+
];
|
|
175
|
+
|
|
176
|
+
const unknownCheck = checkRequestedToolRegistration(
|
|
177
|
+
"third_party_tool",
|
|
178
|
+
registeredTools,
|
|
179
|
+
);
|
|
180
|
+
expect(unknownCheck.status).toBe("unregistered");
|
|
181
|
+
if (unknownCheck.status === "unregistered") {
|
|
182
|
+
expect(unknownCheck.availableToolNames).toEqual(["bash", "mcp", "read"]);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const aliasCheck = checkRequestedToolRegistration(
|
|
186
|
+
"legacy_read",
|
|
187
|
+
registeredTools,
|
|
188
|
+
{ legacy_read: "read" },
|
|
189
|
+
);
|
|
190
|
+
expect(aliasCheck.status).toBe("registered");
|
|
191
|
+
|
|
192
|
+
const missingNameCheck = checkRequestedToolRegistration(
|
|
193
|
+
" ",
|
|
194
|
+
registeredTools,
|
|
195
|
+
);
|
|
196
|
+
expect(missingNameCheck.status).toBe("missing-tool-name");
|
|
197
|
+
});
|