@gotgenes/pi-permission-system 18.1.1 → 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 (122) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/docs/configuration.md +11 -0
  3. package/package.json +1 -2
  4. package/src/access-intent/access-path.ts +29 -4
  5. package/src/access-intent/bash/bash-path-resolver.ts +39 -18
  6. package/src/access-intent/bash/msys-bash-tokens.ts +64 -0
  7. package/src/path-normalizer.ts +105 -2
  8. package/test/access-intent/access-path.test.ts +0 -277
  9. package/test/access-intent/bash/node-text.test.ts +0 -148
  10. package/test/access-intent/bash/parser.test.ts +0 -19
  11. package/test/access-intent/bash/program.test.ts +0 -673
  12. package/test/access-intent/bash/token-classification.test.ts +0 -363
  13. package/test/access-intent/bash/token-collection.test.ts +0 -300
  14. package/test/active-agent.test.ts +0 -155
  15. package/test/async-cache.test.ts +0 -48
  16. package/test/bash-arity.test.ts +0 -144
  17. package/test/bash-external-directory.test.ts +0 -1022
  18. package/test/builtin-tool-input-formatters.test.ts +0 -109
  19. package/test/canonicalize-path.test.ts +0 -119
  20. package/test/composition-root.test.ts +0 -698
  21. package/test/config-loader.test.ts +0 -740
  22. package/test/config-modal.test.ts +0 -320
  23. package/test/config-paths.test.ts +0 -83
  24. package/test/config-pipeline.test.ts +0 -90
  25. package/test/config-reporter.test.ts +0 -147
  26. package/test/config-store.test.ts +0 -466
  27. package/test/decision-audit.test.ts +0 -72
  28. package/test/decision-reporter.test.ts +0 -112
  29. package/test/denial-messages.test.ts +0 -714
  30. package/test/detect-permissive-bash-fallback.test.ts +0 -56
  31. package/test/expand-home.test.ts +0 -93
  32. package/test/extension-config.test.ts +0 -129
  33. package/test/extension-paths.test.ts +0 -108
  34. package/test/forwarded-permissions/io.test.ts +0 -251
  35. package/test/forwarding-manager.test.ts +0 -200
  36. package/test/handlers/before-agent-start.test.ts +0 -314
  37. package/test/handlers/external-directory-integration.test.ts +0 -515
  38. package/test/handlers/external-directory-session-dedup.test.ts +0 -175
  39. package/test/handlers/external-directory-symlink-acceptance.test.ts +0 -167
  40. package/test/handlers/gates/bash-command-metamorphic.test.ts +0 -88
  41. package/test/handlers/gates/bash-command.test.ts +0 -257
  42. package/test/handlers/gates/bash-external-directory.test.ts +0 -268
  43. package/test/handlers/gates/bash-path.test.ts +0 -346
  44. package/test/handlers/gates/candidate-check.test.ts +0 -52
  45. package/test/handlers/gates/external-directory-messages.test.ts +0 -85
  46. package/test/handlers/gates/external-directory-policy.test.ts +0 -134
  47. package/test/handlers/gates/external-directory.test.ts +0 -267
  48. package/test/handlers/gates/helpers.test.ts +0 -165
  49. package/test/handlers/gates/path.test.ts +0 -369
  50. package/test/handlers/gates/runner.test.ts +0 -408
  51. package/test/handlers/gates/skill-input-gate-pipeline.test.ts +0 -176
  52. package/test/handlers/gates/skill-input.test.ts +0 -128
  53. package/test/handlers/gates/skill-read.test.ts +0 -161
  54. package/test/handlers/gates/tool-call-gate-pipeline.test.ts +0 -356
  55. package/test/handlers/gates/tool.test.ts +0 -244
  56. package/test/handlers/input-events.test.ts +0 -168
  57. package/test/handlers/input.test.ts +0 -199
  58. package/test/handlers/lifecycle.test.ts +0 -221
  59. package/test/handlers/tool-call-boundary.test.ts +0 -145
  60. package/test/handlers/tool-call-events.test.ts +0 -277
  61. package/test/handlers/tool-call.test.ts +0 -395
  62. package/test/handlers/validate-requested-tool.test.ts +0 -92
  63. package/test/helpers/external-directory-fixtures.ts +0 -269
  64. package/test/helpers/gate-fixtures.ts +0 -316
  65. package/test/helpers/handler-fixtures.ts +0 -335
  66. package/test/helpers/make-fake-pi.ts +0 -100
  67. package/test/helpers/manager-harness.ts +0 -112
  68. package/test/helpers/session-fixtures.ts +0 -199
  69. package/test/input-normalizer.test.ts +0 -325
  70. package/test/logging.test.ts +0 -51
  71. package/test/mcp-targets.test.ts +0 -233
  72. package/test/node-modules-discovery.test.ts +0 -97
  73. package/test/normalize.test.ts +0 -247
  74. package/test/path-containment.test.ts +0 -161
  75. package/test/path-normalization.test.ts +0 -233
  76. package/test/path-normalizer.test.ts +0 -196
  77. package/test/path-surfaces.test.ts +0 -55
  78. package/test/pattern-suggest.test.ts +0 -248
  79. package/test/permission-dialog.test.ts +0 -205
  80. package/test/permission-event-rpc.test.ts +0 -560
  81. package/test/permission-events.test.ts +0 -400
  82. package/test/permission-forwarder.test.ts +0 -370
  83. package/test/permission-forwarding.test.ts +0 -315
  84. package/test/permission-gate.test.ts +0 -269
  85. package/test/permission-manager-unified.test.ts +0 -3714
  86. package/test/permission-merge.test.ts +0 -61
  87. package/test/permission-prompter.test.ts +0 -518
  88. package/test/permission-prompts.test.ts +0 -363
  89. package/test/permission-resolver.test.ts +0 -277
  90. package/test/permission-session.test.ts +0 -404
  91. package/test/permission-ui-prompt.test.ts +0 -146
  92. package/test/permissions-service.test.ts +0 -192
  93. package/test/pi-infrastructure-read.test.ts +0 -432
  94. package/test/policy-loader.test.ts +0 -561
  95. package/test/prompting-gateway.test.ts +0 -231
  96. package/test/rule.test.ts +0 -650
  97. package/test/safe-system-paths.test.ts +0 -46
  98. package/test/scope-merge.test.ts +0 -116
  99. package/test/service-lifecycle.test.ts +0 -163
  100. package/test/service.test.ts +0 -261
  101. package/test/session-approval.test.ts +0 -75
  102. package/test/session-logger.test.ts +0 -200
  103. package/test/session-rules.test.ts +0 -321
  104. package/test/session-start.test.ts +0 -112
  105. package/test/skill-prompt-sanitizer.test.ts +0 -418
  106. package/test/status.test.ts +0 -10
  107. package/test/subagent-context.test.ts +0 -372
  108. package/test/subagent-lifecycle-events.test.ts +0 -132
  109. package/test/subagent-registry.test.ts +0 -145
  110. package/test/synthesize.test.ts +0 -302
  111. package/test/system-prompt-sanitizer.test.ts +0 -382
  112. package/test/tool-access-extractor-registry.test.ts +0 -77
  113. package/test/tool-input-formatter-registry.test.ts +0 -75
  114. package/test/tool-input-path.test.ts +0 -84
  115. package/test/tool-input-preview.test.ts +0 -129
  116. package/test/tool-input-prompt-formatters.test.ts +0 -115
  117. package/test/tool-preview-formatter.test.ts +0 -458
  118. package/test/tool-registry.test.ts +0 -197
  119. package/test/value-guards.test.ts +0 -193
  120. package/test/wildcard-matcher.test.ts +0 -424
  121. package/test/yaml-frontmatter.test.ts +0 -91
  122. package/test/yolo-mode.test.ts +0 -188
@@ -1,408 +0,0 @@
1
- import { describe, expect, it, vi } from "vitest";
2
-
3
- import type { DenialContext } from "#src/denial-messages";
4
- import { EXTENSION_TAG } from "#src/denial-messages";
5
- import type { GateBypass } from "#src/handlers/gates/descriptor";
6
- import { SessionApproval } from "#src/session-approval";
7
- import {
8
- makeDenialDescriptor,
9
- makeDescriptor,
10
- makeGateRunner,
11
- } from "#test/helpers/gate-fixtures";
12
- import { makeCheckResult } from "#test/helpers/handler-fixtures";
13
-
14
- // ── GateRunner — descriptor path ───────────────────────────────────────────
15
-
16
- describe("GateRunner — descriptor path", () => {
17
- it("returns allow and emits policy_allow when policy is allow", async () => {
18
- const { runner, deps } = makeGateRunner();
19
- const result = await runner.run(makeDescriptor(), null, "tc-1");
20
- expect(result).toEqual({ action: "allow" });
21
- expect(deps.reporter.emitDecision).toHaveBeenCalledWith(
22
- expect.objectContaining({
23
- surface: "read",
24
- value: "read",
25
- result: "allow",
26
- resolution: "policy_allow",
27
- }),
28
- );
29
- });
30
-
31
- it("returns block and emits policy_deny when policy is deny", async () => {
32
- const { runner, deps } = makeGateRunner({
33
- resolveResult: makeCheckResult({ state: "deny", matchedPattern: "*" }),
34
- });
35
- const result = await runner.run(makeDescriptor(), null, "tc-1");
36
- expect(result).toMatchObject({ action: "block" });
37
- expect(deps.reporter.emitDecision).toHaveBeenCalledWith(
38
- expect.objectContaining({
39
- result: "deny",
40
- resolution: "policy_deny",
41
- }),
42
- );
43
- expect(deps.reporter.writeReviewLog).toHaveBeenCalledWith(
44
- "permission_request.blocked",
45
- expect.objectContaining({ resolution: "policy_denied" }),
46
- );
47
- });
48
-
49
- it("returns allow and emits session_approved on session hit", async () => {
50
- const { runner, deps } = makeGateRunner({
51
- resolveResult: makeCheckResult({
52
- source: "session",
53
- matchedPattern: "git *",
54
- }),
55
- });
56
- const result = await runner.run(
57
- makeDescriptor({
58
- surface: "bash",
59
- input: { command: "git status" },
60
- decision: { surface: "bash", value: "git status" },
61
- }),
62
- null,
63
- "tc-1",
64
- );
65
- expect(result).toEqual({ action: "allow" });
66
- expect(deps.reporter.writeReviewLog).toHaveBeenCalledWith(
67
- "permission_request.session_approved",
68
- expect.objectContaining({
69
- resolution: "session_approved",
70
- sessionApprovalPattern: "git *",
71
- }),
72
- );
73
- expect(deps.reporter.emitDecision).toHaveBeenCalledWith(
74
- expect.objectContaining({
75
- resolution: "session_approved",
76
- matchedPattern: "git *",
77
- }),
78
- );
79
- });
80
-
81
- it("returns allow and emits user_approved when ask + user approves", async () => {
82
- const { runner, deps } = makeGateRunner({
83
- resolveResult: makeCheckResult({ state: "ask", matchedPattern: "*" }),
84
- prompt: vi.fn().mockResolvedValue({ approved: true, state: "approved" }),
85
- });
86
- const result = await runner.run(makeDescriptor(), null, "tc-1");
87
- expect(result).toEqual({ action: "allow" });
88
- expect(deps.reporter.emitDecision).toHaveBeenCalledWith(
89
- expect.objectContaining({
90
- result: "allow",
91
- resolution: "user_approved",
92
- }),
93
- );
94
- });
95
-
96
- it("returns allow, emits user_approved_for_session, and records session rule on approved_for_session", async () => {
97
- const { runner, deps } = makeGateRunner({
98
- resolveResult: makeCheckResult({ state: "ask", matchedPattern: "*" }),
99
- prompt: vi
100
- .fn()
101
- .mockResolvedValue({ approved: true, state: "approved_for_session" }),
102
- });
103
- const descriptor = makeDescriptor({
104
- sessionApproval: SessionApproval.single("read", "*"),
105
- });
106
- const result = await runner.run(descriptor, null, "tc-1");
107
- expect(result).toEqual({ action: "allow" });
108
- expect(deps.reporter.emitDecision).toHaveBeenCalledWith(
109
- expect.objectContaining({
110
- resolution: "user_approved_for_session",
111
- }),
112
- );
113
- expect(deps.recordSessionApproval).toHaveBeenCalledWith(
114
- SessionApproval.single("read", "*"),
115
- );
116
- });
117
-
118
- it("calls recordSessionApproval once with the full SessionApproval when sessionApproval has multiple patterns", async () => {
119
- const { runner, deps } = makeGateRunner({
120
- resolveResult: makeCheckResult({ state: "ask", matchedPattern: "*" }),
121
- prompt: vi
122
- .fn()
123
- .mockResolvedValue({ approved: true, state: "approved_for_session" }),
124
- });
125
- const approval = SessionApproval.multiple("external_directory", [
126
- "/outside/a/*",
127
- "/outside/b/*",
128
- ]);
129
- const descriptor = makeDescriptor({ sessionApproval: approval });
130
- const result = await runner.run(descriptor, null, "tc-1");
131
- expect(result).toEqual({ action: "allow" });
132
- expect(deps.recordSessionApproval).toHaveBeenCalledTimes(1);
133
- expect(deps.recordSessionApproval).toHaveBeenCalledWith(approval);
134
- });
135
-
136
- it("returns block and emits user_denied when ask + user denies", async () => {
137
- const { runner, deps } = makeGateRunner({
138
- resolveResult: makeCheckResult({ state: "ask", matchedPattern: "*" }),
139
- prompt: vi.fn().mockResolvedValue({ approved: false, state: "denied" }),
140
- });
141
- const result = await runner.run(makeDescriptor(), null, "tc-1");
142
- expect(result).toMatchObject({ action: "block" });
143
- expect(deps.reporter.emitDecision).toHaveBeenCalledWith(
144
- expect.objectContaining({
145
- result: "deny",
146
- resolution: "user_denied",
147
- }),
148
- );
149
- });
150
-
151
- it("returns block and emits confirmation_unavailable when ask + no UI", async () => {
152
- const { runner, deps } = makeGateRunner({
153
- resolveResult: makeCheckResult({ state: "ask", matchedPattern: "*" }),
154
- canConfirm: vi.fn().mockReturnValue(false),
155
- });
156
- const result = await runner.run(makeDescriptor(), null, "tc-1");
157
- expect(result).toMatchObject({ action: "block" });
158
- expect(deps.reporter.emitDecision).toHaveBeenCalledWith(
159
- expect.objectContaining({
160
- result: "deny",
161
- resolution: "confirmation_unavailable",
162
- }),
163
- );
164
- });
165
-
166
- it("emits auto_approved resolution when decision has autoApproved flag", async () => {
167
- const { runner, deps } = makeGateRunner({
168
- resolveResult: makeCheckResult({ state: "ask", matchedPattern: "*" }),
169
- prompt: vi.fn().mockResolvedValue({
170
- approved: true,
171
- state: "approved",
172
- autoApproved: true,
173
- }),
174
- });
175
- const result = await runner.run(makeDescriptor(), null, "tc-1");
176
- expect(result).toEqual({ action: "allow" });
177
- expect(deps.reporter.emitDecision).toHaveBeenCalledWith(
178
- expect.objectContaining({
179
- resolution: "auto_approved",
180
- }),
181
- );
182
- });
183
-
184
- it("uses preResolved.state instead of calling resolve", async () => {
185
- const { runner, deps } = makeGateRunner();
186
- const descriptor = makeDescriptor({
187
- preResolved: { state: "deny" },
188
- });
189
- const result = await runner.run(descriptor, null, "tc-1");
190
- expect(result).toMatchObject({ action: "block" });
191
- expect(deps.resolve).not.toHaveBeenCalled();
192
- expect(deps.reporter.emitDecision).toHaveBeenCalledWith(
193
- expect.objectContaining({
194
- resolution: "policy_deny",
195
- }),
196
- );
197
- });
198
-
199
- it("uses preResolved.state allow without calling resolve", async () => {
200
- const { runner, deps } = makeGateRunner();
201
- const descriptor = makeDescriptor({
202
- preResolved: { state: "allow" },
203
- });
204
- const result = await runner.run(descriptor, null, "tc-1");
205
- expect(result).toEqual({ action: "allow" });
206
- expect(deps.resolve).not.toHaveBeenCalled();
207
- expect(deps.reporter.emitDecision).toHaveBeenCalledWith(
208
- expect.objectContaining({
209
- resolution: "policy_allow",
210
- }),
211
- );
212
- });
213
-
214
- it("passes agentName to resolve and decision event", async () => {
215
- const { runner, deps } = makeGateRunner();
216
- const result = await runner.run(makeDescriptor(), "test-agent", "tc-1");
217
- expect(result).toEqual({ action: "allow" });
218
- expect(deps.resolve).toHaveBeenCalledWith({
219
- kind: "tool",
220
- surface: "read",
221
- input: {},
222
- agentName: "test-agent",
223
- });
224
- expect(deps.reporter.emitDecision).toHaveBeenCalledWith(
225
- expect.objectContaining({
226
- agentName: "test-agent",
227
- }),
228
- );
229
- });
230
-
231
- it("passes requestId from toolCallId to prompt", async () => {
232
- const { runner, deps } = makeGateRunner({
233
- resolveResult: makeCheckResult({ state: "ask", matchedPattern: "*" }),
234
- });
235
- await runner.run(makeDescriptor(), null, "tc-42");
236
- expect(deps.prompt).toHaveBeenCalledWith(
237
- expect.objectContaining({ requestId: "tc-42" }),
238
- );
239
- });
240
-
241
- it("does not call recordSessionApproval when user approves once (no sessionApproval)", async () => {
242
- const { runner, deps } = makeGateRunner({
243
- resolveResult: makeCheckResult({ state: "ask", matchedPattern: "*" }),
244
- prompt: vi.fn().mockResolvedValue({ approved: true, state: "approved" }),
245
- });
246
- await runner.run(makeDescriptor(), null, "tc-1");
247
- expect(deps.recordSessionApproval).not.toHaveBeenCalled();
248
- });
249
-
250
- it("uses preCheck result directly instead of calling resolve", async () => {
251
- const { runner, deps } = makeGateRunner();
252
- const descriptor = makeDescriptor({
253
- preCheck: makeCheckResult({
254
- state: "deny",
255
- origin: "global",
256
- matchedPattern: "rm *",
257
- }),
258
- });
259
- const result = await runner.run(descriptor, null, "tc-1");
260
- expect(result).toMatchObject({ action: "block" });
261
- expect(deps.resolve).not.toHaveBeenCalled();
262
- expect(deps.reporter.emitDecision).toHaveBeenCalledWith(
263
- expect.objectContaining({
264
- resolution: "policy_deny",
265
- origin: "global",
266
- matchedPattern: "rm *",
267
- }),
268
- );
269
- });
270
-
271
- it("does not call recordSessionApproval when user approves for session but no sessionApproval on descriptor", async () => {
272
- const { runner, deps } = makeGateRunner({
273
- resolveResult: makeCheckResult({ state: "ask", matchedPattern: "*" }),
274
- prompt: vi
275
- .fn()
276
- .mockResolvedValue({ approved: true, state: "approved_for_session" }),
277
- });
278
- // No sessionApproval on descriptor
279
- await runner.run(makeDescriptor(), null, "tc-1");
280
- expect(deps.recordSessionApproval).not.toHaveBeenCalled();
281
- });
282
-
283
- describe("denialContext formatting", () => {
284
- it("uses denialContext to format denyReason with extension tag", async () => {
285
- const { runner } = makeGateRunner({
286
- resolveResult: makeCheckResult({ state: "deny", matchedPattern: "*" }),
287
- });
288
- const ctx: DenialContext = {
289
- kind: "tool",
290
- check: makeCheckResult({ state: "deny", matchedPattern: "*" }),
291
- agentName: "test-agent",
292
- };
293
- const result = await runner.run(
294
- makeDenialDescriptor(ctx),
295
- "test-agent",
296
- "tc-1",
297
- );
298
- expect(result.action).toBe("block");
299
- if (result.action === "block") {
300
- expect(result.reason).toContain(EXTENSION_TAG);
301
- expect(result.reason).not.toContain("Hard stop");
302
- }
303
- });
304
-
305
- it("uses denialContext to format unavailableReason with extension tag", async () => {
306
- const { runner } = makeGateRunner({
307
- resolveResult: makeCheckResult({ state: "ask", matchedPattern: "*" }),
308
- canConfirm: vi.fn().mockReturnValue(false),
309
- });
310
- const ctx: DenialContext = {
311
- kind: "tool",
312
- check: makeCheckResult({ state: "ask", matchedPattern: "*" }),
313
- };
314
- const result = await runner.run(makeDenialDescriptor(ctx), null, "tc-1");
315
- expect(result.action).toBe("block");
316
- if (result.action === "block") {
317
- expect(result.reason).toContain(EXTENSION_TAG);
318
- expect(result.reason).toContain("no interactive UI");
319
- }
320
- });
321
-
322
- it("uses denialContext to format userDeniedReason with extension tag", async () => {
323
- const { runner } = makeGateRunner({
324
- resolveResult: makeCheckResult({ state: "ask", matchedPattern: "*" }),
325
- prompt: vi.fn().mockResolvedValue({
326
- approved: false,
327
- state: "denied",
328
- denialReason: "too risky",
329
- }),
330
- });
331
- const ctx: DenialContext = {
332
- kind: "tool",
333
- check: makeCheckResult({ state: "ask", matchedPattern: "*" }),
334
- };
335
- const result = await runner.run(makeDenialDescriptor(ctx), null, "tc-1");
336
- expect(result.action).toBe("block");
337
- if (result.action === "block") {
338
- expect(result.reason).toContain(EXTENSION_TAG);
339
- expect(result.reason).toContain("too risky");
340
- }
341
- });
342
- });
343
- });
344
-
345
- // ── GateRunner.run — null and bypass dispatch ──────────────────────────────
346
-
347
- describe("GateRunner.run — null and bypass dispatch", () => {
348
- it("returns allow for a null gate", async () => {
349
- const { runner, deps } = makeGateRunner();
350
- const result = await runner.run(null, null, "tc-1");
351
- expect(result).toEqual({ action: "allow" });
352
- expect(deps.reporter.writeReviewLog).not.toHaveBeenCalled();
353
- expect(deps.reporter.emitDecision).not.toHaveBeenCalled();
354
- });
355
-
356
- it("returns allow for a bypass with no log or decision", async () => {
357
- const { runner, deps } = makeGateRunner();
358
- const bypass: GateBypass = { action: "allow" };
359
- const result = await runner.run(bypass, null, "tc-1");
360
- expect(result).toEqual({ action: "allow" });
361
- expect(deps.reporter.writeReviewLog).not.toHaveBeenCalled();
362
- expect(deps.reporter.emitDecision).not.toHaveBeenCalled();
363
- });
364
-
365
- it("fires writeReviewLog for a bypass with a log entry", async () => {
366
- const { runner, deps } = makeGateRunner();
367
- const bypass: GateBypass = {
368
- action: "allow",
369
- log: { event: "infra.bypass", details: { path: "/x" } },
370
- };
371
- await runner.run(bypass, null, "tc-1");
372
- expect(deps.reporter.writeReviewLog).toHaveBeenCalledWith("infra.bypass", {
373
- path: "/x",
374
- });
375
- expect(deps.reporter.emitDecision).not.toHaveBeenCalled();
376
- });
377
-
378
- it("fires emitDecision for a bypass with a decision", async () => {
379
- const { runner, deps } = makeGateRunner();
380
- const decision = {
381
- surface: "path",
382
- value: "/x",
383
- result: "allow" as const,
384
- resolution: "policy_allow" as const,
385
- origin: null,
386
- agentName: null,
387
- matchedPattern: null,
388
- };
389
- const bypass: GateBypass = { action: "allow", decision };
390
- await runner.run(bypass, null, "tc-1");
391
- expect(deps.reporter.emitDecision).toHaveBeenCalledWith(decision);
392
- expect(deps.reporter.writeReviewLog).not.toHaveBeenCalled();
393
- });
394
-
395
- it("routes a descriptor to the gate check logic and returns allow", async () => {
396
- const { runner } = makeGateRunner();
397
- const result = await runner.run(makeDescriptor(), null, "tc-1");
398
- expect(result).toEqual({ action: "allow" });
399
- });
400
-
401
- it("routes a descriptor to the gate check logic and returns block", async () => {
402
- const { runner } = makeGateRunner({
403
- resolveResult: makeCheckResult({ state: "deny", matchedPattern: "*" }),
404
- });
405
- const result = await runner.run(makeDescriptor(), null, "tc-1");
406
- expect(result).toMatchObject({ action: "block" });
407
- });
408
- });
@@ -1,176 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
-
3
- import {
4
- createSkillInputRequestId,
5
- formatSkillDenyNotice,
6
- SkillInputGatePipeline,
7
- } from "#src/handlers/gates/skill-input-gate-pipeline";
8
-
9
- import {
10
- makeGateRunner,
11
- makeNotifier,
12
- makeSkillInputInputs,
13
- } from "#test/helpers/gate-fixtures";
14
- import { makeCheckResult } from "#test/helpers/handler-fixtures";
15
-
16
- // ── createSkillInputRequestId ─────────────────────────────────────────────
17
-
18
- describe("createSkillInputRequestId", () => {
19
- it("starts with 'skill-input-'", () => {
20
- expect(createSkillInputRequestId().startsWith("skill-input-")).toBe(true);
21
- });
22
-
23
- it("returns a unique id on each call", () => {
24
- const id1 = createSkillInputRequestId();
25
- const id2 = createSkillInputRequestId();
26
- expect(id1).not.toBe(id2);
27
- });
28
- });
29
-
30
- // ── formatSkillDenyNotice ─────────────────────────────────────────────────
31
-
32
- describe("formatSkillDenyNotice", () => {
33
- it("includes the skill name in the message (no agent)", () => {
34
- const msg = formatSkillDenyNotice("librarian", null);
35
- expect(msg).toContain("librarian");
36
- });
37
-
38
- it("includes the skill name and agent name when agent is present", () => {
39
- const msg = formatSkillDenyNotice("librarian", "code-agent");
40
- expect(msg).toContain("librarian");
41
- expect(msg).toContain("code-agent");
42
- });
43
- });
44
-
45
- // ── SkillInputGatePipeline.evaluate ───────────────────────────────────────
46
-
47
- describe("SkillInputGatePipeline.evaluate", () => {
48
- // ── notifier behaviour ──────────────────────────────────────────────────
49
-
50
- it("calls notifier.warn when the skill is denied", async () => {
51
- const inputs = makeSkillInputInputs({
52
- checkPermission: () => makeCheckResult({ state: "deny" }),
53
- });
54
- const notifier = makeNotifier();
55
- const { runner } = makeGateRunner();
56
- const pipeline = new SkillInputGatePipeline(inputs);
57
-
58
- await pipeline.evaluate("librarian", null, notifier, runner);
59
-
60
- expect(notifier.warn).toHaveBeenCalledOnce();
61
- expect(notifier.warn).toHaveBeenCalledWith(
62
- expect.stringContaining("librarian"),
63
- );
64
- });
65
-
66
- it("does not call notifier.warn when the skill is allowed", async () => {
67
- const inputs = makeSkillInputInputs({
68
- checkPermission: () => makeCheckResult({ state: "allow" }),
69
- });
70
- const notifier = makeNotifier();
71
- const { runner } = makeGateRunner();
72
- const pipeline = new SkillInputGatePipeline(inputs);
73
-
74
- await pipeline.evaluate("librarian", null, notifier, runner);
75
-
76
- expect(notifier.warn).not.toHaveBeenCalled();
77
- });
78
-
79
- it("does not call notifier.warn when the skill requires approval (ask)", async () => {
80
- const inputs = makeSkillInputInputs({
81
- checkPermission: () => makeCheckResult({ state: "ask" }),
82
- });
83
- const notifier = makeNotifier();
84
- const { runner } = makeGateRunner();
85
- const pipeline = new SkillInputGatePipeline(inputs);
86
-
87
- await pipeline.evaluate("librarian", null, notifier, runner);
88
-
89
- expect(notifier.warn).not.toHaveBeenCalled();
90
- });
91
-
92
- it("includes agent name in the deny notice when agent is present", async () => {
93
- const inputs = makeSkillInputInputs({
94
- checkPermission: () => makeCheckResult({ state: "deny" }),
95
- });
96
- const notifier = makeNotifier();
97
- const { runner } = makeGateRunner();
98
- const pipeline = new SkillInputGatePipeline(inputs);
99
-
100
- await pipeline.evaluate("librarian", "code-agent", notifier, runner);
101
-
102
- expect(notifier.warn).toHaveBeenCalledWith(
103
- expect.stringContaining("code-agent"),
104
- );
105
- });
106
-
107
- // ── outcome mapping ─────────────────────────────────────────────────────
108
-
109
- it("returns allow when the gate passes", async () => {
110
- const inputs = makeSkillInputInputs({
111
- checkPermission: () => makeCheckResult({ state: "allow" }),
112
- });
113
- const { runner } = makeGateRunner();
114
- const pipeline = new SkillInputGatePipeline(inputs);
115
-
116
- const result = await pipeline.evaluate(
117
- "librarian",
118
- null,
119
- makeNotifier(),
120
- runner,
121
- );
122
-
123
- expect(result).toEqual({ action: "allow" });
124
- });
125
-
126
- it("returns block when the gate denies", async () => {
127
- const inputs = makeSkillInputInputs({
128
- checkPermission: () =>
129
- makeCheckResult({ state: "deny", matchedPattern: "*" }),
130
- });
131
- const { runner } = makeGateRunner();
132
- const pipeline = new SkillInputGatePipeline(inputs);
133
-
134
- const result = await pipeline.evaluate(
135
- "librarian",
136
- null,
137
- makeNotifier(),
138
- runner,
139
- );
140
-
141
- expect(result).toEqual({
142
- action: "block",
143
- reason: expect.stringContaining("librarian"),
144
- });
145
- });
146
-
147
- // ── checkPermission call ────────────────────────────────────────────────
148
-
149
- it("calls checkPermission with the skill surface, skill name, and agent name", async () => {
150
- const inputs = makeSkillInputInputs();
151
- const { runner } = makeGateRunner();
152
- const pipeline = new SkillInputGatePipeline(inputs);
153
-
154
- await pipeline.evaluate("explorer", "code-agent", makeNotifier(), runner);
155
-
156
- expect(inputs.checkPermission).toHaveBeenCalledWith(
157
- "skill",
158
- { name: "explorer" },
159
- "code-agent",
160
- );
161
- });
162
-
163
- it("calls checkPermission with undefined agentName when agentName is null", async () => {
164
- const inputs = makeSkillInputInputs();
165
- const { runner } = makeGateRunner();
166
- const pipeline = new SkillInputGatePipeline(inputs);
167
-
168
- await pipeline.evaluate("explorer", null, makeNotifier(), runner);
169
-
170
- expect(inputs.checkPermission).toHaveBeenCalledWith(
171
- "skill",
172
- { name: "explorer" },
173
- undefined,
174
- );
175
- });
176
- });