@gotgenes/pi-permission-system 18.1.0 → 18.1.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.
Files changed (132) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/docs/assets/logo.png +0 -0
  3. package/docs/assets/logo.svg +213 -0
  4. package/docs/configuration.md +700 -0
  5. package/docs/cross-extension-api.md +525 -0
  6. package/docs/guides/permission-frontmatter-for-subagent-extensions.md +201 -0
  7. package/docs/guides/upstream-issue-template.md +113 -0
  8. package/docs/migration/legacy-to-flat.md +365 -0
  9. package/docs/opencode-compatibility.md +213 -0
  10. package/docs/session-approvals.md +68 -0
  11. package/docs/subagent-integration.md +102 -0
  12. package/docs/troubleshooting.md +53 -0
  13. package/package.json +5 -2
  14. package/src/access-intent/access-path.ts +29 -4
  15. package/src/access-intent/bash/bash-path-resolver.ts +39 -18
  16. package/src/access-intent/bash/msys-bash-tokens.ts +64 -0
  17. package/src/path-normalizer.ts +105 -2
  18. package/test/access-intent/access-path.test.ts +0 -277
  19. package/test/access-intent/bash/node-text.test.ts +0 -148
  20. package/test/access-intent/bash/parser.test.ts +0 -19
  21. package/test/access-intent/bash/program.test.ts +0 -673
  22. package/test/access-intent/bash/token-classification.test.ts +0 -363
  23. package/test/access-intent/bash/token-collection.test.ts +0 -300
  24. package/test/active-agent.test.ts +0 -155
  25. package/test/async-cache.test.ts +0 -48
  26. package/test/bash-arity.test.ts +0 -144
  27. package/test/bash-external-directory.test.ts +0 -1022
  28. package/test/builtin-tool-input-formatters.test.ts +0 -109
  29. package/test/canonicalize-path.test.ts +0 -119
  30. package/test/composition-root.test.ts +0 -698
  31. package/test/config-loader.test.ts +0 -740
  32. package/test/config-modal.test.ts +0 -320
  33. package/test/config-paths.test.ts +0 -83
  34. package/test/config-pipeline.test.ts +0 -90
  35. package/test/config-reporter.test.ts +0 -147
  36. package/test/config-store.test.ts +0 -466
  37. package/test/decision-audit.test.ts +0 -72
  38. package/test/decision-reporter.test.ts +0 -112
  39. package/test/denial-messages.test.ts +0 -714
  40. package/test/detect-permissive-bash-fallback.test.ts +0 -56
  41. package/test/expand-home.test.ts +0 -93
  42. package/test/extension-config.test.ts +0 -129
  43. package/test/extension-paths.test.ts +0 -108
  44. package/test/forwarded-permissions/io.test.ts +0 -251
  45. package/test/forwarding-manager.test.ts +0 -200
  46. package/test/handlers/before-agent-start.test.ts +0 -314
  47. package/test/handlers/external-directory-integration.test.ts +0 -515
  48. package/test/handlers/external-directory-session-dedup.test.ts +0 -175
  49. package/test/handlers/external-directory-symlink-acceptance.test.ts +0 -167
  50. package/test/handlers/gates/bash-command-metamorphic.test.ts +0 -88
  51. package/test/handlers/gates/bash-command.test.ts +0 -257
  52. package/test/handlers/gates/bash-external-directory.test.ts +0 -268
  53. package/test/handlers/gates/bash-path.test.ts +0 -346
  54. package/test/handlers/gates/candidate-check.test.ts +0 -52
  55. package/test/handlers/gates/external-directory-messages.test.ts +0 -85
  56. package/test/handlers/gates/external-directory-policy.test.ts +0 -134
  57. package/test/handlers/gates/external-directory.test.ts +0 -267
  58. package/test/handlers/gates/helpers.test.ts +0 -165
  59. package/test/handlers/gates/path.test.ts +0 -369
  60. package/test/handlers/gates/runner.test.ts +0 -408
  61. package/test/handlers/gates/skill-input-gate-pipeline.test.ts +0 -176
  62. package/test/handlers/gates/skill-input.test.ts +0 -128
  63. package/test/handlers/gates/skill-read.test.ts +0 -161
  64. package/test/handlers/gates/tool-call-gate-pipeline.test.ts +0 -356
  65. package/test/handlers/gates/tool.test.ts +0 -244
  66. package/test/handlers/input-events.test.ts +0 -168
  67. package/test/handlers/input.test.ts +0 -199
  68. package/test/handlers/lifecycle.test.ts +0 -221
  69. package/test/handlers/tool-call-boundary.test.ts +0 -145
  70. package/test/handlers/tool-call-events.test.ts +0 -277
  71. package/test/handlers/tool-call.test.ts +0 -395
  72. package/test/handlers/validate-requested-tool.test.ts +0 -92
  73. package/test/helpers/external-directory-fixtures.ts +0 -269
  74. package/test/helpers/gate-fixtures.ts +0 -316
  75. package/test/helpers/handler-fixtures.ts +0 -335
  76. package/test/helpers/make-fake-pi.ts +0 -100
  77. package/test/helpers/manager-harness.ts +0 -112
  78. package/test/helpers/session-fixtures.ts +0 -199
  79. package/test/input-normalizer.test.ts +0 -325
  80. package/test/logging.test.ts +0 -51
  81. package/test/mcp-targets.test.ts +0 -233
  82. package/test/node-modules-discovery.test.ts +0 -97
  83. package/test/normalize.test.ts +0 -247
  84. package/test/path-containment.test.ts +0 -161
  85. package/test/path-normalization.test.ts +0 -233
  86. package/test/path-normalizer.test.ts +0 -196
  87. package/test/path-surfaces.test.ts +0 -55
  88. package/test/pattern-suggest.test.ts +0 -248
  89. package/test/permission-dialog.test.ts +0 -205
  90. package/test/permission-event-rpc.test.ts +0 -560
  91. package/test/permission-events.test.ts +0 -400
  92. package/test/permission-forwarder.test.ts +0 -370
  93. package/test/permission-forwarding.test.ts +0 -315
  94. package/test/permission-gate.test.ts +0 -269
  95. package/test/permission-manager-unified.test.ts +0 -3714
  96. package/test/permission-merge.test.ts +0 -61
  97. package/test/permission-prompter.test.ts +0 -518
  98. package/test/permission-prompts.test.ts +0 -363
  99. package/test/permission-resolver.test.ts +0 -277
  100. package/test/permission-session.test.ts +0 -404
  101. package/test/permission-ui-prompt.test.ts +0 -146
  102. package/test/permissions-service.test.ts +0 -192
  103. package/test/pi-infrastructure-read.test.ts +0 -432
  104. package/test/policy-loader.test.ts +0 -561
  105. package/test/prompting-gateway.test.ts +0 -231
  106. package/test/rule.test.ts +0 -650
  107. package/test/safe-system-paths.test.ts +0 -46
  108. package/test/scope-merge.test.ts +0 -116
  109. package/test/service-lifecycle.test.ts +0 -163
  110. package/test/service.test.ts +0 -261
  111. package/test/session-approval.test.ts +0 -75
  112. package/test/session-logger.test.ts +0 -200
  113. package/test/session-rules.test.ts +0 -321
  114. package/test/session-start.test.ts +0 -112
  115. package/test/skill-prompt-sanitizer.test.ts +0 -418
  116. package/test/status.test.ts +0 -10
  117. package/test/subagent-context.test.ts +0 -372
  118. package/test/subagent-lifecycle-events.test.ts +0 -132
  119. package/test/subagent-registry.test.ts +0 -145
  120. package/test/synthesize.test.ts +0 -302
  121. package/test/system-prompt-sanitizer.test.ts +0 -382
  122. package/test/tool-access-extractor-registry.test.ts +0 -77
  123. package/test/tool-input-formatter-registry.test.ts +0 -75
  124. package/test/tool-input-path.test.ts +0 -84
  125. package/test/tool-input-preview.test.ts +0 -129
  126. package/test/tool-input-prompt-formatters.test.ts +0 -115
  127. package/test/tool-preview-formatter.test.ts +0 -458
  128. package/test/tool-registry.test.ts +0 -197
  129. package/test/value-guards.test.ts +0 -193
  130. package/test/wildcard-matcher.test.ts +0 -424
  131. package/test/yaml-frontmatter.test.ts +0 -91
  132. package/test/yolo-mode.test.ts +0 -188
@@ -1,395 +0,0 @@
1
- import { describe, expect, it, vi } from "vitest";
2
-
3
- import { getEventInput } from "#src/handlers/permission-gate-handler";
4
-
5
- import {
6
- makeBashCommandCheck,
7
- makeCheckResult,
8
- makeCtx,
9
- makeHandler,
10
- makeSurfaceCheck,
11
- makeToolCallEvent,
12
- } from "#test/helpers/handler-fixtures";
13
-
14
- // ── SDK stubs ──────────────────────────────────────────────────────────────
15
- vi.mock("@earendil-works/pi-coding-agent", async (importOriginal) => {
16
- const original =
17
- await importOriginal<typeof import("@earendil-works/pi-coding-agent")>();
18
- return { ...original };
19
- });
20
-
21
- // ── getEventInput ──────────────────────────────────────────────────────────
22
-
23
- describe("getEventInput", () => {
24
- it("returns the input field when present", () => {
25
- expect(getEventInput({ input: { path: "/foo" } })).toEqual({
26
- path: "/foo",
27
- });
28
- });
29
-
30
- it("returns the arguments field when input is absent", () => {
31
- expect(getEventInput({ arguments: { command: "ls" } })).toEqual({
32
- command: "ls",
33
- });
34
- });
35
-
36
- it("returns empty object when neither field is present", () => {
37
- expect(getEventInput({ type: "tool_call" })).toEqual({});
38
- });
39
-
40
- it("prefers input over arguments when both are present", () => {
41
- expect(getEventInput({ input: { a: 1 }, arguments: { b: 2 } })).toEqual({
42
- a: 1,
43
- });
44
- });
45
- });
46
-
47
- // ── handleToolCall ─────────────────────────────────────────────────────────
48
-
49
- describe("handleToolCall", () => {
50
- it("activates session with ctx", async () => {
51
- const ctx = makeCtx();
52
- const { handler, forwarding } = makeHandler();
53
- await handler.handleToolCall(makeToolCallEvent("read"), ctx);
54
- // session.activate(ctx) calls forwarding.start(ctx) on the real session
55
- expect(forwarding.start).toHaveBeenCalledWith(ctx);
56
- });
57
-
58
- it("blocks when tool name cannot be resolved", async () => {
59
- const { handler } = makeHandler();
60
- const result = await handler.handleToolCall(
61
- { type: "tool_call" },
62
- makeCtx(),
63
- );
64
- expect(result).toEqual({
65
- action: "block",
66
- reason: expect.stringContaining("tool"),
67
- });
68
- });
69
-
70
- it("blocks when tool is not registered", async () => {
71
- const { handler } = makeHandler({ tools: ["read"] });
72
- const result = await handler.handleToolCall(
73
- makeToolCallEvent("unknown-tool"),
74
- makeCtx(),
75
- );
76
- expect(result).toMatchObject({ action: "block" });
77
- });
78
-
79
- it("returns empty object when tool is allowed", async () => {
80
- const { handler } = makeHandler();
81
- const result = await handler.handleToolCall(
82
- makeToolCallEvent("read"),
83
- makeCtx(),
84
- );
85
- expect(result).toEqual({ action: "allow" });
86
- });
87
-
88
- it("blocks when tool is denied by policy", async () => {
89
- const { handler } = makeHandler({
90
- session: {
91
- checkPermission: vi
92
- .fn()
93
- .mockReturnValue(makeCheckResult({ state: "deny" })),
94
- },
95
- });
96
- const result = await handler.handleToolCall(
97
- makeToolCallEvent("read"),
98
- makeCtx(),
99
- );
100
- expect(result).toMatchObject({ action: "block" });
101
- });
102
- });
103
-
104
- // ── skill-read gate ────────────────────────────────────────────────────────
105
-
106
- describe("handleToolCall — skill-read gate", () => {
107
- it("blocks a read of a denied skill path", async () => {
108
- const skillEntry = {
109
- name: "librarian",
110
- description: "Research skills",
111
- location: "/skills/librarian/SKILL.md",
112
- state: "deny" as const,
113
- normalizedLocation: "/skills/librarian/SKILL.md",
114
- normalizedBaseDir: "/skills/librarian",
115
- };
116
- const { handler } = makeHandler({
117
- session: {
118
- getActiveSkillEntries: vi.fn().mockReturnValue([skillEntry]),
119
- },
120
- toolRegistry: {
121
- getAll: vi.fn().mockReturnValue([{ toolName: "read" }]),
122
- },
123
- });
124
- const event = {
125
- type: "tool_call",
126
- toolCallId: "tc-skill",
127
- toolName: "read",
128
- input: { path: "/skills/librarian/SKILL.md" },
129
- };
130
- const result = await handler.handleToolCall(event, makeCtx());
131
- expect(result).toMatchObject({ action: "block" });
132
- });
133
-
134
- it("allows a read of a non-skill path even when skill entries are present", async () => {
135
- const skillEntry = {
136
- name: "librarian",
137
- description: "Research skills",
138
- location: "/skills/librarian/SKILL.md",
139
- state: "deny" as const,
140
- normalizedLocation: "/skills/librarian/SKILL.md",
141
- normalizedBaseDir: "/skills/librarian",
142
- };
143
- const { handler } = makeHandler({
144
- session: {
145
- getActiveSkillEntries: vi.fn().mockReturnValue([skillEntry]),
146
- },
147
- toolRegistry: {
148
- getAll: vi.fn().mockReturnValue([{ toolName: "read" }]),
149
- },
150
- });
151
- const event = {
152
- type: "tool_call",
153
- toolCallId: "tc-ok",
154
- toolName: "read",
155
- input: { path: "/test/project/src/index.ts" },
156
- };
157
- const result = await handler.handleToolCall(event, makeCtx());
158
- expect(result).toEqual({ action: "allow" });
159
- });
160
- });
161
-
162
- // ── external-directory gate ────────────────────────────────────────────────
163
-
164
- describe("handleToolCall — external-directory gate", () => {
165
- it("blocks a read of a path outside cwd when policy is deny", async () => {
166
- const { handler } = makeHandler({
167
- session: {
168
- checkPermission: vi
169
- .fn()
170
- .mockReturnValue(makeCheckResult({ state: "deny" })),
171
- },
172
- tools: ["read"],
173
- });
174
- const event = makeToolCallEvent("read", {
175
- input: { path: "/outside/project/file.ts" },
176
- });
177
- const result = await handler.handleToolCall(event, makeCtx());
178
- expect(result).toMatchObject({ action: "block" });
179
- });
180
- });
181
-
182
- // ── bash external-directory gate ──────────────────────────────────────────
183
-
184
- describe("handleToolCall — bash external-directory gate", () => {
185
- it("blocks a bash command referencing an external path when policy is deny", async () => {
186
- const { handler } = makeHandler({
187
- session: {
188
- checkPermission: vi
189
- .fn()
190
- .mockReturnValue(makeCheckResult({ state: "deny" })),
191
- },
192
- tools: ["bash"],
193
- });
194
- const event = makeToolCallEvent("bash", {
195
- input: { command: "cat /outside/project/file.ts" },
196
- });
197
- const result = await handler.handleToolCall(event, makeCtx());
198
- expect(result).toMatchObject({ action: "block" });
199
- });
200
- });
201
-
202
- // ── path gate (tools) ─────────────────────────────────────────────────────
203
-
204
- describe("handleToolCall — path gate (tools)", () => {
205
- it("blocks a read of .env when path surface denies *.env", async () => {
206
- const { handler } = makeHandler({
207
- session: {
208
- checkPermission: makeSurfaceCheck({
209
- path: { state: "deny", matchedPattern: "*.env" },
210
- }),
211
- },
212
- tools: ["read"],
213
- });
214
- const event = makeToolCallEvent("read", { input: { path: ".env" } });
215
- const result = await handler.handleToolCall(event, makeCtx());
216
- expect(result).toMatchObject({ action: "block" });
217
- });
218
-
219
- it("allows a read when path surface allows", async () => {
220
- const { handler } = makeHandler({ tools: ["read"] });
221
- const event = makeToolCallEvent("read", {
222
- input: { path: "src/index.ts" },
223
- });
224
- const result = await handler.handleToolCall(event, makeCtx());
225
- expect(result).toEqual({ action: "allow" });
226
- });
227
- });
228
-
229
- // ── bash path gate ────────────────────────────────────────────────────────
230
-
231
- describe("handleToolCall — bash path gate", () => {
232
- it("blocks a bash command accessing .env when path surface denies", async () => {
233
- const { handler } = makeHandler({
234
- session: {
235
- checkPermission: makeSurfaceCheck({
236
- path: { state: "deny", matchedPattern: "*.env" },
237
- }),
238
- },
239
- tools: ["bash"],
240
- });
241
- const event = makeToolCallEvent("bash", { input: { command: "cat .env" } });
242
- const result = await handler.handleToolCall(event, makeCtx());
243
- expect(result).toMatchObject({ action: "block" });
244
- });
245
- });
246
-
247
- // ── bash command chain gate ───────────────────────────────────────────────
248
-
249
- describe("handleToolCall — bash command chain gate", () => {
250
- it("blocks a chain when a later sub-command is denied (#301)", async () => {
251
- const { handler } = makeHandler({
252
- session: {
253
- checkPermission: makeBashCommandCheck({
254
- deny: /^npm\b/,
255
- denyMatched: "npm *",
256
- allowMatched: "echo *",
257
- }),
258
- },
259
- tools: ["bash"],
260
- });
261
- const event = makeToolCallEvent("bash", {
262
- input: { command: "echo start && npm install compromised-package" },
263
- });
264
- const result = await handler.handleToolCall(event, makeCtx());
265
- expect(result).toMatchObject({ action: "block" });
266
- });
267
-
268
- it("blocks a command nested inside command substitution (#306)", async () => {
269
- const { handler } = makeHandler({
270
- session: {
271
- checkPermission: makeBashCommandCheck({
272
- deny: /^rm\b/,
273
- denyMatched: "rm *",
274
- allowMatched: "echo *",
275
- }),
276
- },
277
- tools: ["bash"],
278
- });
279
- const event = makeToolCallEvent("bash", {
280
- input: { command: "echo $(rm -rf foo)" },
281
- });
282
- const result = await handler.handleToolCall(event, makeCtx());
283
- expect(result).toMatchObject({ action: "block" });
284
- });
285
-
286
- it("allows a single non-chained bash command", async () => {
287
- const { handler } = makeHandler({ tools: ["bash"] });
288
- const event = makeToolCallEvent("bash", { input: { command: "echo hi" } });
289
- const result = await handler.handleToolCall(event, makeCtx());
290
- expect(result).toEqual({ action: "allow" });
291
- });
292
- });
293
-
294
- // ---------------------------------------------------------------------------
295
- // Moved from permission-system.test.ts catch-all (#342)
296
- // ---------------------------------------------------------------------------
297
-
298
- describe("handleToolCall — bash external-directory policy states", () => {
299
- it("allows bash command with only internal paths when external_directory is denied", async () => {
300
- const { handler } = makeHandler({ tools: ["bash"] });
301
- const event = makeToolCallEvent("bash", {
302
- input: { command: "cat src/index.ts" },
303
- });
304
- const result = await handler.handleToolCall(event, makeCtx());
305
- expect(result).toEqual({ action: "allow" });
306
- });
307
-
308
- it("blocks bash command with external path when external_directory is ask and no UI", async () => {
309
- const { handler } = makeHandler({
310
- session: {
311
- checkPermission: makeSurfaceCheck({
312
- external_directory: { state: "ask", source: "special" },
313
- }),
314
- },
315
- tools: ["bash"],
316
- prompter: {
317
- canConfirm: vi.fn().mockReturnValue(false),
318
- prompt: vi.fn().mockResolvedValue({ approved: false, state: "denied" }),
319
- },
320
- });
321
- const event = makeToolCallEvent("bash", {
322
- input: { command: "cat /etc/hosts" },
323
- });
324
- const result = await handler.handleToolCall(
325
- event,
326
- makeCtx({ hasUI: false }),
327
- );
328
- expect(result).toMatchObject({ action: "block" });
329
- expect(String((result as { reason?: unknown }).reason)).toMatch(
330
- /no interactive UI/i,
331
- );
332
- });
333
-
334
- it("allows bash command with external path when external_directory is allow", async () => {
335
- const { handler } = makeHandler({
336
- session: {
337
- checkPermission: makeSurfaceCheck({
338
- external_directory: { state: "allow", source: "special" },
339
- }),
340
- },
341
- tools: ["bash"],
342
- });
343
- const event = makeToolCallEvent("bash", {
344
- input: { command: "cat /etc/hosts" },
345
- });
346
- const result = await handler.handleToolCall(event, makeCtx());
347
- expect(result).toEqual({ action: "allow" });
348
- });
349
-
350
- it("applies bash pattern deny after external_directory allow", async () => {
351
- const { handler } = makeHandler({
352
- session: {
353
- checkPermission: makeSurfaceCheck(
354
- {
355
- external_directory: { state: "allow", source: "special" },
356
- bash: { state: "deny", source: "bash" },
357
- },
358
- { state: "allow" },
359
- ),
360
- },
361
- tools: ["bash"],
362
- });
363
- const event = makeToolCallEvent("bash", {
364
- input: { command: "cat /etc/hosts" },
365
- });
366
- const result = await handler.handleToolCall(event, makeCtx());
367
- expect(result).toMatchObject({ action: "block" });
368
- });
369
- });
370
-
371
- describe("handleToolCall — generic ask prompt content", () => {
372
- it("ask prompt includes serialized tool input for informed approval", async () => {
373
- const { handler, prompter } = makeHandler({
374
- session: {
375
- checkPermission: makeSurfaceCheck({
376
- weather_lookup: { state: "ask" },
377
- }),
378
- },
379
- tools: ["weather_lookup"],
380
- prompter: {
381
- canConfirm: vi.fn().mockReturnValue(true),
382
- prompt: vi.fn().mockResolvedValue({ approved: false, state: "denied" }),
383
- },
384
- });
385
- const event = makeToolCallEvent("weather_lookup", {
386
- input: { city: "Chicago", units: "metric" },
387
- });
388
- await handler.handleToolCall(event, makeCtx());
389
- expect(vi.mocked(prompter.prompt)).toHaveBeenCalledTimes(1);
390
- const promptDetails = vi.mocked(prompter.prompt).mock.calls[0][0];
391
- expect(promptDetails.message).toMatch(
392
- /\{"city":"Chicago","units":"metric"\}/,
393
- );
394
- });
395
- });
@@ -1,92 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
-
3
- import {
4
- type RequestedToolValidation,
5
- validateRequestedTool,
6
- } from "#src/handlers/permission-gate-handler";
7
-
8
- // ── helpers ────────────────────────────────────────────────────────────────
9
-
10
- function makeTools(names: string[]): { name: string }[] {
11
- return names.map((name) => ({ name }));
12
- }
13
-
14
- const TOOLS = makeTools(["read", "bash", "edit"]);
15
-
16
- // ── validateRequestedTool ──────────────────────────────────────────────────
17
-
18
- describe("validateRequestedTool", () => {
19
- describe("missing / unresolvable tool name", () => {
20
- it("blocks when event has no name field", () => {
21
- const result = validateRequestedTool({ type: "tool_call" }, TOOLS);
22
- expect(result.status).toBe("block");
23
- expect(
24
- (result as Extract<RequestedToolValidation, { status: "block" }>)
25
- .reason,
26
- ).toBeTruthy();
27
- });
28
-
29
- it("blocks when name field is an empty string", () => {
30
- const result = validateRequestedTool({ name: "" }, TOOLS);
31
- expect(result.status).toBe("block");
32
- });
33
-
34
- it("blocks when name field is null", () => {
35
- const result = validateRequestedTool({ name: null }, TOOLS);
36
- expect(result.status).toBe("block");
37
- });
38
-
39
- it("blocks when event is a primitive", () => {
40
- const result = validateRequestedTool("not-an-object", TOOLS);
41
- expect(result.status).toBe("block");
42
- });
43
- });
44
-
45
- describe("unregistered tool", () => {
46
- it("blocks when the tool name is not in the registered list", () => {
47
- const result = validateRequestedTool({ name: "unknown-tool" }, TOOLS);
48
- expect(result.status).toBe("block");
49
- });
50
-
51
- it("includes available tool names in the block reason", () => {
52
- const result = validateRequestedTool({ name: "unknown-tool" }, TOOLS);
53
- expect(result.status).toBe("block");
54
- const { reason } = result as Extract<
55
- RequestedToolValidation,
56
- { status: "block" }
57
- >;
58
- expect(reason).toContain("read");
59
- expect(reason).toContain("bash");
60
- expect(reason).toContain("edit");
61
- });
62
-
63
- it("blocks with empty available list when no tools are registered", () => {
64
- const result = validateRequestedTool({ name: "anything" }, []);
65
- expect(result.status).toBe("block");
66
- });
67
- });
68
-
69
- describe("registered tool (ok path)", () => {
70
- it("returns ok with the raw tool name for a known tool", () => {
71
- const result = validateRequestedTool({ name: "read" }, TOOLS);
72
- expect(result).toEqual({ status: "ok", toolName: "read" });
73
- });
74
-
75
- it("returns the raw name as it appeared in the event (not normalised)", () => {
76
- // If an alias mechanism were to normalise "Read" → "read",
77
- // validateRequestedTool still returns the raw value from the event.
78
- // Without aliases the raw name and registered name are the same; this
79
- // asserts the contract that toolName comes from the event, not from the
80
- // registration lookup's normalizedToolName field.
81
- const result = validateRequestedTool({ name: "bash" }, TOOLS);
82
- expect(result).toEqual({ status: "ok", toolName: "bash" });
83
- });
84
-
85
- it("resolves tool name via the `arguments` field naming convention", () => {
86
- // getToolNameFromValue reads `.name` then falls back to other fields;
87
- // a plain `{ name: "edit" }` event is sufficient here.
88
- const result = validateRequestedTool({ name: "edit" }, TOOLS);
89
- expect(result).toEqual({ status: "ok", toolName: "edit" });
90
- });
91
- });
92
- });