@gotgenes/pi-permission-system 18.1.1 → 18.2.0

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 (133) hide show
  1. package/CHANGELOG.md +25 -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/extension-config.ts +7 -0
  8. package/src/forwarded-permissions/permission-forwarder.ts +6 -3
  9. package/src/handlers/gates/helpers.ts +1 -1
  10. package/src/handlers/gates/runner.ts +22 -0
  11. package/src/index.ts +10 -6
  12. package/src/path-normalizer.ts +105 -2
  13. package/src/permission-manager.ts +21 -1
  14. package/src/permission-prompter.ts +10 -16
  15. package/src/prompting-gateway.ts +13 -17
  16. package/src/rule.ts +21 -1
  17. package/src/status.ts +1 -1
  18. package/src/yolo-mode.ts +0 -30
  19. package/test/access-intent/access-path.test.ts +0 -277
  20. package/test/access-intent/bash/node-text.test.ts +0 -148
  21. package/test/access-intent/bash/parser.test.ts +0 -19
  22. package/test/access-intent/bash/program.test.ts +0 -673
  23. package/test/access-intent/bash/token-classification.test.ts +0 -363
  24. package/test/access-intent/bash/token-collection.test.ts +0 -300
  25. package/test/active-agent.test.ts +0 -155
  26. package/test/async-cache.test.ts +0 -48
  27. package/test/bash-arity.test.ts +0 -144
  28. package/test/bash-external-directory.test.ts +0 -1022
  29. package/test/builtin-tool-input-formatters.test.ts +0 -109
  30. package/test/canonicalize-path.test.ts +0 -119
  31. package/test/composition-root.test.ts +0 -698
  32. package/test/config-loader.test.ts +0 -740
  33. package/test/config-modal.test.ts +0 -320
  34. package/test/config-paths.test.ts +0 -83
  35. package/test/config-pipeline.test.ts +0 -90
  36. package/test/config-reporter.test.ts +0 -147
  37. package/test/config-store.test.ts +0 -466
  38. package/test/decision-audit.test.ts +0 -72
  39. package/test/decision-reporter.test.ts +0 -112
  40. package/test/denial-messages.test.ts +0 -714
  41. package/test/detect-permissive-bash-fallback.test.ts +0 -56
  42. package/test/expand-home.test.ts +0 -93
  43. package/test/extension-config.test.ts +0 -129
  44. package/test/extension-paths.test.ts +0 -108
  45. package/test/forwarded-permissions/io.test.ts +0 -251
  46. package/test/forwarding-manager.test.ts +0 -200
  47. package/test/handlers/before-agent-start.test.ts +0 -314
  48. package/test/handlers/external-directory-integration.test.ts +0 -515
  49. package/test/handlers/external-directory-session-dedup.test.ts +0 -175
  50. package/test/handlers/external-directory-symlink-acceptance.test.ts +0 -167
  51. package/test/handlers/gates/bash-command-metamorphic.test.ts +0 -88
  52. package/test/handlers/gates/bash-command.test.ts +0 -257
  53. package/test/handlers/gates/bash-external-directory.test.ts +0 -268
  54. package/test/handlers/gates/bash-path.test.ts +0 -346
  55. package/test/handlers/gates/candidate-check.test.ts +0 -52
  56. package/test/handlers/gates/external-directory-messages.test.ts +0 -85
  57. package/test/handlers/gates/external-directory-policy.test.ts +0 -134
  58. package/test/handlers/gates/external-directory.test.ts +0 -267
  59. package/test/handlers/gates/helpers.test.ts +0 -165
  60. package/test/handlers/gates/path.test.ts +0 -369
  61. package/test/handlers/gates/runner.test.ts +0 -408
  62. package/test/handlers/gates/skill-input-gate-pipeline.test.ts +0 -176
  63. package/test/handlers/gates/skill-input.test.ts +0 -128
  64. package/test/handlers/gates/skill-read.test.ts +0 -161
  65. package/test/handlers/gates/tool-call-gate-pipeline.test.ts +0 -356
  66. package/test/handlers/gates/tool.test.ts +0 -244
  67. package/test/handlers/input-events.test.ts +0 -168
  68. package/test/handlers/input.test.ts +0 -199
  69. package/test/handlers/lifecycle.test.ts +0 -221
  70. package/test/handlers/tool-call-boundary.test.ts +0 -145
  71. package/test/handlers/tool-call-events.test.ts +0 -277
  72. package/test/handlers/tool-call.test.ts +0 -395
  73. package/test/handlers/validate-requested-tool.test.ts +0 -92
  74. package/test/helpers/external-directory-fixtures.ts +0 -269
  75. package/test/helpers/gate-fixtures.ts +0 -316
  76. package/test/helpers/handler-fixtures.ts +0 -335
  77. package/test/helpers/make-fake-pi.ts +0 -100
  78. package/test/helpers/manager-harness.ts +0 -112
  79. package/test/helpers/session-fixtures.ts +0 -199
  80. package/test/input-normalizer.test.ts +0 -325
  81. package/test/logging.test.ts +0 -51
  82. package/test/mcp-targets.test.ts +0 -233
  83. package/test/node-modules-discovery.test.ts +0 -97
  84. package/test/normalize.test.ts +0 -247
  85. package/test/path-containment.test.ts +0 -161
  86. package/test/path-normalization.test.ts +0 -233
  87. package/test/path-normalizer.test.ts +0 -196
  88. package/test/path-surfaces.test.ts +0 -55
  89. package/test/pattern-suggest.test.ts +0 -248
  90. package/test/permission-dialog.test.ts +0 -205
  91. package/test/permission-event-rpc.test.ts +0 -560
  92. package/test/permission-events.test.ts +0 -400
  93. package/test/permission-forwarder.test.ts +0 -370
  94. package/test/permission-forwarding.test.ts +0 -315
  95. package/test/permission-gate.test.ts +0 -269
  96. package/test/permission-manager-unified.test.ts +0 -3714
  97. package/test/permission-merge.test.ts +0 -61
  98. package/test/permission-prompter.test.ts +0 -518
  99. package/test/permission-prompts.test.ts +0 -363
  100. package/test/permission-resolver.test.ts +0 -277
  101. package/test/permission-session.test.ts +0 -404
  102. package/test/permission-ui-prompt.test.ts +0 -146
  103. package/test/permissions-service.test.ts +0 -192
  104. package/test/pi-infrastructure-read.test.ts +0 -432
  105. package/test/policy-loader.test.ts +0 -561
  106. package/test/prompting-gateway.test.ts +0 -231
  107. package/test/rule.test.ts +0 -650
  108. package/test/safe-system-paths.test.ts +0 -46
  109. package/test/scope-merge.test.ts +0 -116
  110. package/test/service-lifecycle.test.ts +0 -163
  111. package/test/service.test.ts +0 -261
  112. package/test/session-approval.test.ts +0 -75
  113. package/test/session-logger.test.ts +0 -200
  114. package/test/session-rules.test.ts +0 -321
  115. package/test/session-start.test.ts +0 -112
  116. package/test/skill-prompt-sanitizer.test.ts +0 -418
  117. package/test/status.test.ts +0 -10
  118. package/test/subagent-context.test.ts +0 -372
  119. package/test/subagent-lifecycle-events.test.ts +0 -132
  120. package/test/subagent-registry.test.ts +0 -145
  121. package/test/synthesize.test.ts +0 -302
  122. package/test/system-prompt-sanitizer.test.ts +0 -382
  123. package/test/tool-access-extractor-registry.test.ts +0 -77
  124. package/test/tool-input-formatter-registry.test.ts +0 -75
  125. package/test/tool-input-path.test.ts +0 -84
  126. package/test/tool-input-preview.test.ts +0 -129
  127. package/test/tool-input-prompt-formatters.test.ts +0 -115
  128. package/test/tool-preview-formatter.test.ts +0 -458
  129. package/test/tool-registry.test.ts +0 -197
  130. package/test/value-guards.test.ts +0 -193
  131. package/test/wildcard-matcher.test.ts +0 -424
  132. package/test/yaml-frontmatter.test.ts +0 -91
  133. package/test/yolo-mode.test.ts +0 -188
@@ -1,244 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
-
3
- import { describeToolGate } from "#src/handlers/gates/tool";
4
- import type { ToolCallContext } from "#src/handlers/gates/types";
5
- import { PathNormalizer } from "#src/path-normalizer";
6
- import {
7
- TOOL_INPUT_LOG_PREVIEW_MAX_LENGTH,
8
- TOOL_INPUT_PREVIEW_MAX_LENGTH,
9
- TOOL_TEXT_SUMMARY_MAX_LENGTH,
10
- } from "#src/tool-input-preview";
11
- import { ToolPreviewFormatter } from "#src/tool-preview-formatter";
12
- import type { PermissionCheckResult } from "#src/types";
13
-
14
- // ── helpers ────────────────────────────────────────────────────────────────
15
-
16
- function makeFormatter(): ToolPreviewFormatter {
17
- return new ToolPreviewFormatter({
18
- toolInputPreviewMaxLength: TOOL_INPUT_PREVIEW_MAX_LENGTH,
19
- toolTextSummaryMaxLength: TOOL_TEXT_SUMMARY_MAX_LENGTH,
20
- toolInputLogPreviewMaxLength: TOOL_INPUT_LOG_PREVIEW_MAX_LENGTH,
21
- });
22
- }
23
-
24
- function makeTcc(overrides: Partial<ToolCallContext> = {}): ToolCallContext {
25
- return {
26
- toolName: "read",
27
- agentName: null,
28
- input: {},
29
- toolCallId: "tc-1",
30
- cwd: "/test/project",
31
- ...overrides,
32
- };
33
- }
34
-
35
- function makeCheckResult(
36
- state: "allow" | "deny" | "ask",
37
- overrides: Partial<PermissionCheckResult> = {},
38
- ): PermissionCheckResult {
39
- return {
40
- state,
41
- toolName: "read",
42
- source: "tool",
43
- origin: "builtin",
44
- matchedPattern: "*",
45
- ...overrides,
46
- };
47
- }
48
-
49
- // The per-tool gate now receives the AccessPath the pipeline builds, bound to
50
- // the makeTcc default cwd; approval values derive from `accessPath.value()`.
51
- const normalizer = new PathNormalizer("linux", "/test/project");
52
-
53
- // ── tests ──────────────────────────────────────────────────────────────────
54
-
55
- describe("describeToolGate", () => {
56
- it("returns descriptor with tool name as surface for standard tools", () => {
57
- const desc = describeToolGate(
58
- makeTcc({ toolName: "read" }),
59
- makeCheckResult("ask"),
60
- makeFormatter(),
61
- );
62
- expect(desc.surface).toBe("read");
63
- expect(desc.decision.surface).toBe("read");
64
- });
65
-
66
- it("returns descriptor with tool name as decision value for standard tools", () => {
67
- const desc = describeToolGate(
68
- makeTcc({ toolName: "write" }),
69
- makeCheckResult("ask"),
70
- makeFormatter(),
71
- );
72
- expect(desc.decision.value).toBe("write");
73
- });
74
-
75
- it("returns bash surface with command in decision.value for bash tools", () => {
76
- const check = makeCheckResult("ask", {
77
- toolName: "bash",
78
- command: "git status",
79
- });
80
- const desc = describeToolGate(
81
- makeTcc({ toolName: "bash", input: { command: "git status" } }),
82
- check,
83
- makeFormatter(),
84
- );
85
- expect(desc.surface).toBe("bash");
86
- expect(desc.decision.surface).toBe("bash");
87
- expect(desc.decision.value).toBe("git status");
88
- });
89
-
90
- it("returns mcp surface with target in decision.value for MCP tools", () => {
91
- const check = makeCheckResult("ask", {
92
- toolName: "mcp",
93
- target: "server:tool",
94
- });
95
- const desc = describeToolGate(
96
- makeTcc({ toolName: "mcp", input: { tool: "server:tool" } }),
97
- check,
98
- makeFormatter(),
99
- );
100
- expect(desc.surface).toBe("mcp");
101
- expect(desc.decision.surface).toBe("mcp");
102
- expect(desc.decision.value).toBe("server:tool");
103
- });
104
-
105
- it("populates denialContext with kind 'tool' and check result", () => {
106
- const check = makeCheckResult("deny", { toolName: "read" });
107
- const desc = describeToolGate(makeTcc(), check, makeFormatter());
108
- expect(desc.denialContext).toEqual({
109
- kind: "tool",
110
- check,
111
- agentName: undefined,
112
- input: {},
113
- });
114
- });
115
-
116
- it("populates denialContext with agent name when provided", () => {
117
- const check = makeCheckResult("ask", { toolName: "read" });
118
- const desc = describeToolGate(
119
- makeTcc({ agentName: "my-agent" }),
120
- check,
121
- makeFormatter(),
122
- );
123
- expect(desc.denialContext.agentName).toBe("my-agent");
124
- });
125
-
126
- it("populates denialContext with input for tool context", () => {
127
- const check = makeCheckResult("ask", { toolName: "bash", command: "ls" });
128
- const desc = describeToolGate(
129
- makeTcc({ toolName: "bash", input: { command: "ls" } }),
130
- check,
131
- makeFormatter(),
132
- );
133
- expect(desc.denialContext).toMatchObject({
134
- kind: "tool",
135
- input: { command: "ls" },
136
- });
137
- });
138
-
139
- it("populates sessionApproval via suggestSessionPattern", () => {
140
- const check = makeCheckResult("ask", {
141
- toolName: "bash",
142
- command: "git status",
143
- });
144
- const desc = describeToolGate(
145
- makeTcc({ toolName: "bash", input: { command: "git status" } }),
146
- check,
147
- makeFormatter(),
148
- );
149
- expect(desc.sessionApproval).toBeDefined();
150
- expect(desc.sessionApproval?.surface).toBe("bash");
151
- expect(desc.sessionApproval?.representativePattern).toBeDefined();
152
- });
153
-
154
- it("binds a current-directory file's session approval to the cwd subtree", () => {
155
- const check = makeCheckResult("ask", { toolName: "edit" });
156
- const desc = describeToolGate(
157
- makeTcc({
158
- toolName: "edit",
159
- input: { path: "index.html" },
160
- cwd: "/test/project",
161
- }),
162
- check,
163
- makeFormatter(),
164
- normalizer.forPath("index.html"),
165
- );
166
- expect(desc.sessionApproval?.surface).toBe("edit");
167
- expect(desc.sessionApproval?.representativePattern).toBe("/test/project/*");
168
- });
169
-
170
- it("resolves a sub-directory file's session approval to an absolute pattern", () => {
171
- // The approval value derives from the AccessPath's lexical absolute form
172
- // (`value()`), so sub-directory approvals are absolute too — the deliberate
173
- // tradeoff that keeps the pattern aligned with the policy values it is
174
- // matched against.
175
- const check = makeCheckResult("ask", { toolName: "edit" });
176
- const desc = describeToolGate(
177
- makeTcc({
178
- toolName: "edit",
179
- input: { path: "src/foo.ts" },
180
- cwd: "/test/project",
181
- }),
182
- check,
183
- makeFormatter(),
184
- normalizer.forPath("src/foo.ts"),
185
- );
186
- expect(desc.sessionApproval?.representativePattern).toBe(
187
- "/test/project/src/*",
188
- );
189
- });
190
-
191
- it("falls back to a wildcard session approval when no AccessPath is given", () => {
192
- // A path-bearing tool with no `input.path` keeps the `tool` intent and gets
193
- // no AccessPath, so the suggestion collapses to the catch-all.
194
- const desc = describeToolGate(
195
- makeTcc({ toolName: "read", input: {} }),
196
- makeCheckResult("ask"),
197
- makeFormatter(),
198
- );
199
- expect(desc.sessionApproval?.surface).toBe("read");
200
- expect(desc.sessionApproval?.representativePattern).toBe("*");
201
- });
202
-
203
- it("populates promptDetails with correct fields", () => {
204
- const check = makeCheckResult("ask");
205
- const desc = describeToolGate(
206
- makeTcc({ toolName: "read", agentName: "my-agent", toolCallId: "tc-42" }),
207
- check,
208
- makeFormatter(),
209
- );
210
- expect(desc.promptDetails).toMatchObject({
211
- source: "tool_call",
212
- agentName: "my-agent",
213
- toolCallId: "tc-42",
214
- toolName: "read",
215
- });
216
- expect(desc.promptDetails.message).toBeDefined();
217
- expect(desc.promptDetails.sessionLabel).toBeDefined();
218
- });
219
-
220
- it("populates logContext with tool input preview fields", () => {
221
- const check = makeCheckResult("ask", { toolName: "bash", command: "ls" });
222
- const desc = describeToolGate(
223
- makeTcc({ toolName: "bash", input: { command: "ls" } }),
224
- check,
225
- makeFormatter(),
226
- );
227
- expect(desc.logContext).toMatchObject({
228
- source: "tool_call",
229
- toolName: "bash",
230
- });
231
- expect(desc.logContext.command).toBe("ls");
232
- });
233
-
234
- it("uses toolName as input for checkPermission surface", () => {
235
- const desc = describeToolGate(
236
- makeTcc({ toolName: "edit", input: { path: "/a.ts" } }),
237
- makeCheckResult("ask", { toolName: "edit" }),
238
- makeFormatter(),
239
- normalizer.forPath("/a.ts"),
240
- );
241
- expect(desc.surface).toBe("edit");
242
- expect(desc.input).toEqual({ path: "/a.ts" });
243
- });
244
- });
@@ -1,168 +0,0 @@
1
- /**
2
- * Tests that handleInput emits permissions:decision events for skill input gates.
3
- */
4
- import { describe, expect, it, vi } from "vitest";
5
-
6
- import type { GatePrompter } from "#src/gate-prompter";
7
- import {
8
- getDecisionEvents,
9
- makeCheckResult,
10
- makeCtx,
11
- makeHandler,
12
- } from "#test/helpers/handler-fixtures";
13
-
14
- // ── helpers ────────────────────────────────────────────────────────────────
15
-
16
- /** Build a checkPermission mock returning a skill-surface result. */
17
- function makeSkillCheckPermission(state: "allow" | "deny" | "ask") {
18
- return vi.fn().mockReturnValue(
19
- makeCheckResult({
20
- state,
21
- toolName: "skill",
22
- source: "skill",
23
- origin: "global",
24
- matchedPattern: "*",
25
- }),
26
- );
27
- }
28
-
29
- // ── tests ──────────────────────────────────────────────────────────────────
30
-
31
- describe("handleInput decision events — skill gate", () => {
32
- it("does not emit when input is not a skill invocation", async () => {
33
- const { handler, events } = makeHandler();
34
- await handler.handleInput({ text: "hello world" }, makeCtx());
35
- expect(getDecisionEvents(events)).toHaveLength(0);
36
- });
37
-
38
- it("emits allow with policy_allow for an allowed skill", async () => {
39
- const { handler, events } = makeHandler({
40
- session: { checkPermission: makeSkillCheckPermission("allow") },
41
- });
42
- await handler.handleInput({ text: "/skill:librarian" }, makeCtx());
43
-
44
- const decisions = getDecisionEvents(events);
45
- expect(decisions).toHaveLength(1);
46
- expect(decisions[0]).toMatchObject({
47
- surface: "skill",
48
- value: "librarian",
49
- result: "allow",
50
- resolution: "policy_allow",
51
- });
52
- });
53
-
54
- it("emits deny with policy_deny for a denied skill", async () => {
55
- const { handler, events } = makeHandler({
56
- session: { checkPermission: makeSkillCheckPermission("deny") },
57
- });
58
- await handler.handleInput({ text: "/skill:restricted" }, makeCtx());
59
-
60
- const decisions = getDecisionEvents(events);
61
- expect(decisions).toHaveLength(1);
62
- expect(decisions[0]).toMatchObject({
63
- surface: "skill",
64
- value: "restricted",
65
- result: "deny",
66
- resolution: "policy_deny",
67
- });
68
- });
69
-
70
- it("emits allow with user_approved when state=ask and user approves", async () => {
71
- const { handler, events } = makeHandler({
72
- session: {
73
- checkPermission: makeSkillCheckPermission("ask"),
74
- },
75
- prompter: {
76
- canConfirm: vi.fn().mockReturnValue(true),
77
- prompt: vi
78
- .fn<GatePrompter["prompt"]>()
79
- .mockResolvedValue({ approved: true, state: "approved" }),
80
- },
81
- });
82
- await handler.handleInput({ text: "/skill:explorer" }, makeCtx());
83
-
84
- const decisions = getDecisionEvents(events);
85
- expect(decisions).toHaveLength(1);
86
- expect(decisions[0]).toMatchObject({
87
- surface: "skill",
88
- value: "explorer",
89
- result: "allow",
90
- resolution: "user_approved",
91
- });
92
- });
93
-
94
- it("emits deny with user_denied when state=ask and user denies", async () => {
95
- const { handler, events } = makeHandler({
96
- session: {
97
- checkPermission: makeSkillCheckPermission("ask"),
98
- },
99
- prompter: {
100
- canConfirm: vi.fn().mockReturnValue(true),
101
- prompt: vi
102
- .fn<GatePrompter["prompt"]>()
103
- .mockResolvedValue({ approved: false, state: "denied" }),
104
- },
105
- });
106
- await handler.handleInput({ text: "/skill:explorer" }, makeCtx());
107
-
108
- const decisions = getDecisionEvents(events);
109
- expect(decisions).toHaveLength(1);
110
- expect(decisions[0]).toMatchObject({
111
- surface: "skill",
112
- value: "explorer",
113
- result: "deny",
114
- resolution: "user_denied",
115
- });
116
- });
117
-
118
- it("emits deny with confirmation_unavailable when state=ask but no UI", async () => {
119
- const { handler, events } = makeHandler({
120
- session: {
121
- checkPermission: makeSkillCheckPermission("ask"),
122
- },
123
- prompter: {
124
- canConfirm: vi.fn().mockReturnValue(false),
125
- prompt: vi.fn<GatePrompter["prompt"]>(),
126
- },
127
- });
128
- await handler.handleInput(
129
- { text: "/skill:explorer" },
130
- makeCtx({ hasUI: false }),
131
- );
132
-
133
- const decisions = getDecisionEvents(events);
134
- expect(decisions).toHaveLength(1);
135
- expect(decisions[0]).toMatchObject({
136
- surface: "skill",
137
- value: "explorer",
138
- result: "deny",
139
- resolution: "confirmation_unavailable",
140
- });
141
- });
142
-
143
- it("emits allow with auto_approved when prompt returns autoApproved:true", async () => {
144
- const { handler, events } = makeHandler({
145
- session: {
146
- checkPermission: makeSkillCheckPermission("ask"),
147
- },
148
- prompter: {
149
- canConfirm: vi.fn().mockReturnValue(true),
150
- prompt: vi.fn<GatePrompter["prompt"]>().mockResolvedValue({
151
- approved: true,
152
- state: "approved",
153
- autoApproved: true,
154
- }),
155
- },
156
- });
157
- await handler.handleInput({ text: "/skill:explorer" }, makeCtx());
158
-
159
- const decisions = getDecisionEvents(events);
160
- expect(decisions).toHaveLength(1);
161
- expect(decisions[0]).toMatchObject({
162
- surface: "skill",
163
- value: "explorer",
164
- result: "allow",
165
- resolution: "auto_approved",
166
- });
167
- });
168
- });
@@ -1,199 +0,0 @@
1
- import { describe, expect, it, vi } from "vitest";
2
- import type { GatePrompter } from "#src/gate-prompter";
3
- import { extractSkillNameFromInput } from "#src/handlers/permission-gate-handler";
4
-
5
- import { makeCtx, makeHandler } from "#test/helpers/handler-fixtures";
6
-
7
- // ── helpers ────────────────────────────────────────────────────────────────
8
-
9
- function makeInputEvent(text: string) {
10
- return { text };
11
- }
12
-
13
- // ── extractSkillNameFromInput ──────────────────────────────────────────────
14
-
15
- describe("extractSkillNameFromInput", () => {
16
- it("returns null for plain text", () => {
17
- expect(extractSkillNameFromInput("hello world")).toBeNull();
18
- });
19
-
20
- it("returns null for empty string", () => {
21
- expect(extractSkillNameFromInput("")).toBeNull();
22
- });
23
-
24
- it("returns null for bare /skill: with no name", () => {
25
- expect(extractSkillNameFromInput("/skill:")).toBeNull();
26
- });
27
-
28
- it("extracts skill name from /skill:<name>", () => {
29
- expect(extractSkillNameFromInput("/skill:librarian")).toBe("librarian");
30
- });
31
-
32
- it("extracts skill name stopping at whitespace", () => {
33
- expect(extractSkillNameFromInput("/skill:librarian some extra")).toBe(
34
- "librarian",
35
- );
36
- });
37
-
38
- it("trims leading whitespace before the prefix", () => {
39
- expect(extractSkillNameFromInput(" /skill:my-skill")).toBe("my-skill");
40
- });
41
-
42
- it("returns null when the skill name after trimming is empty", () => {
43
- expect(extractSkillNameFromInput("/skill: ")).toBeNull();
44
- });
45
- });
46
-
47
- // ── handleInput ───────────────────────────────────────────────────────────
48
-
49
- describe("handleInput", () => {
50
- it("activates session with ctx", async () => {
51
- const ctx = makeCtx();
52
- const { handler, forwarding } = makeHandler();
53
- await handler.handleInput(makeInputEvent("hello"), ctx);
54
- // session.activate(ctx) calls forwarding.start(ctx) on the real session
55
- expect(forwarding.start).toHaveBeenCalledWith(ctx);
56
- });
57
-
58
- it("returns continue for non-skill input", async () => {
59
- const { handler } = makeHandler();
60
- const result = await handler.handleInput(
61
- makeInputEvent("just a message"),
62
- makeCtx(),
63
- );
64
- expect(result).toEqual({ action: "continue" });
65
- });
66
-
67
- it("does not check permissions for non-skill input", async () => {
68
- const { handler, permissionManager } = makeHandler();
69
- await handler.handleInput(makeInputEvent("just a message"), makeCtx());
70
- expect(permissionManager.check).not.toHaveBeenCalled();
71
- });
72
-
73
- it("returns continue when skill is allowed", async () => {
74
- const { handler } = makeHandler();
75
- const result = await handler.handleInput(
76
- makeInputEvent("/skill:librarian"),
77
- makeCtx(),
78
- );
79
- expect(result).toEqual({ action: "continue" });
80
- });
81
-
82
- it("returns handled when skill is denied", async () => {
83
- const { handler } = makeHandler({
84
- session: {
85
- checkPermission: vi.fn().mockReturnValue({ state: "deny" }),
86
- },
87
- });
88
- const result = await handler.handleInput(
89
- makeInputEvent("/skill:librarian"),
90
- makeCtx(),
91
- );
92
- expect(result).toEqual({ action: "handled" });
93
- });
94
-
95
- it("shows a warning notification when skill is denied and UI is available", async () => {
96
- const ctx = makeCtx({ hasUI: true });
97
- const { handler } = makeHandler({
98
- session: {
99
- checkPermission: vi.fn().mockReturnValue({ state: "deny" }),
100
- },
101
- });
102
- await handler.handleInput(makeInputEvent("/skill:librarian"), ctx);
103
- expect(ctx.ui.notify).toHaveBeenCalledWith(
104
- expect.stringContaining("librarian"),
105
- "warning",
106
- );
107
- });
108
-
109
- it("does not show a warning notification when skill is denied and UI is absent", async () => {
110
- const ctx = makeCtx({ hasUI: false });
111
- const { handler } = makeHandler({
112
- session: {
113
- checkPermission: vi.fn().mockReturnValue({ state: "deny" }),
114
- },
115
- });
116
- await handler.handleInput(makeInputEvent("/skill:librarian"), ctx);
117
- expect(ctx.ui.notify).not.toHaveBeenCalled();
118
- });
119
-
120
- it("returns handled when skill requires approval but no UI is available", async () => {
121
- const { handler } = makeHandler({
122
- session: {
123
- checkPermission: vi.fn().mockReturnValue({ state: "ask" }),
124
- },
125
- prompter: {
126
- canConfirm: vi.fn().mockReturnValue(false),
127
- prompt: vi.fn<GatePrompter["prompt"]>(),
128
- },
129
- });
130
- const result = await handler.handleInput(
131
- makeInputEvent("/skill:librarian"),
132
- makeCtx(),
133
- );
134
- expect(result).toEqual({ action: "handled" });
135
- });
136
-
137
- it("prompts and returns continue when skill ask is approved", async () => {
138
- const approvePrompt = vi
139
- .fn<GatePrompter["prompt"]>()
140
- .mockResolvedValue({ approved: true, state: "approved" });
141
- const { handler, prompter } = makeHandler({
142
- session: {
143
- checkPermission: vi.fn().mockReturnValue({ state: "ask" }),
144
- },
145
- prompter: {
146
- canConfirm: vi.fn().mockReturnValue(true),
147
- prompt: approvePrompt,
148
- },
149
- });
150
- const result = await handler.handleInput(
151
- makeInputEvent("/skill:librarian"),
152
- makeCtx(),
153
- );
154
- expect(result).toEqual({ action: "continue" });
155
- expect(prompter.prompt).toHaveBeenCalledOnce();
156
- });
157
-
158
- it("returns handled when skill ask is denied by user", async () => {
159
- const { handler } = makeHandler({
160
- session: {
161
- checkPermission: vi.fn().mockReturnValue({ state: "ask" }),
162
- },
163
- prompter: {
164
- canConfirm: vi.fn().mockReturnValue(true),
165
- prompt: vi
166
- .fn<GatePrompter["prompt"]>()
167
- .mockResolvedValue({ approved: false, state: "denied" }),
168
- },
169
- });
170
- const result = await handler.handleInput(
171
- makeInputEvent("/skill:librarian"),
172
- makeCtx(),
173
- );
174
- expect(result).toEqual({ action: "handled" });
175
- });
176
-
177
- it("passes agentName in the prompt permission request", async () => {
178
- const approvePrompt = vi
179
- .fn<GatePrompter["prompt"]>()
180
- .mockResolvedValue({ approved: true, state: "approved" });
181
- const { handler, prompter } = makeHandler({
182
- session: {
183
- checkPermission: vi.fn().mockReturnValue({ state: "ask" }),
184
- resolveAgentName: vi.fn().mockReturnValue("code-agent"),
185
- },
186
- prompter: {
187
- canConfirm: vi.fn().mockReturnValue(true),
188
- prompt: approvePrompt,
189
- },
190
- });
191
- await handler.handleInput(makeInputEvent("/skill:librarian"), makeCtx());
192
- expect(prompter.prompt).toHaveBeenCalledWith(
193
- expect.objectContaining({
194
- agentName: "code-agent",
195
- skillName: "librarian",
196
- }),
197
- );
198
- });
199
- });