@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.
Files changed (203) hide show
  1. package/CHANGELOG.md +2234 -0
  2. package/LICENSE +21 -0
  3. package/README.md +158 -0
  4. package/config/config.example.json +39 -0
  5. package/package.json +82 -0
  6. package/schemas/permissions.schema.json +158 -0
  7. package/src/active-agent.ts +72 -0
  8. package/src/async-cache.ts +21 -0
  9. package/src/bash-arity.ts +210 -0
  10. package/src/builtin-tool-input-formatters.ts +82 -0
  11. package/src/canonicalize-path.ts +30 -0
  12. package/src/common.ts +121 -0
  13. package/src/config-loader.ts +432 -0
  14. package/src/config-modal.ts +259 -0
  15. package/src/config-paths.ts +47 -0
  16. package/src/config-reporter.ts +34 -0
  17. package/src/config-store.ts +222 -0
  18. package/src/decision-audit.ts +75 -0
  19. package/src/decision-reporter.ts +41 -0
  20. package/src/denial-messages.ts +232 -0
  21. package/src/expand-home.ts +28 -0
  22. package/src/extension-config.ts +79 -0
  23. package/src/extension-paths.ts +66 -0
  24. package/src/forwarded-permissions/io.ts +404 -0
  25. package/src/forwarded-permissions/permission-forwarder.ts +580 -0
  26. package/src/forwarding-manager.ts +74 -0
  27. package/src/gate-prompter.ts +12 -0
  28. package/src/handlers/before-agent-start.ts +94 -0
  29. package/src/handlers/gates/bash-command.ts +75 -0
  30. package/src/handlers/gates/bash-external-directory.ts +127 -0
  31. package/src/handlers/gates/bash-path-extractor.ts +15 -0
  32. package/src/handlers/gates/bash-path.ts +152 -0
  33. package/src/handlers/gates/bash-program.ts +1143 -0
  34. package/src/handlers/gates/bash-token-classification.ts +105 -0
  35. package/src/handlers/gates/candidate-check.ts +32 -0
  36. package/src/handlers/gates/descriptor.ts +81 -0
  37. package/src/handlers/gates/external-directory-messages.ts +20 -0
  38. package/src/handlers/gates/external-directory.ts +133 -0
  39. package/src/handlers/gates/helpers.ts +76 -0
  40. package/src/handlers/gates/path.ts +91 -0
  41. package/src/handlers/gates/runner.ts +186 -0
  42. package/src/handlers/gates/skill-input-gate-pipeline.ts +104 -0
  43. package/src/handlers/gates/skill-input.ts +46 -0
  44. package/src/handlers/gates/skill-read.ts +87 -0
  45. package/src/handlers/gates/tool-call-gate-pipeline.ts +129 -0
  46. package/src/handlers/gates/tool.ts +102 -0
  47. package/src/handlers/gates/types.ts +13 -0
  48. package/src/handlers/index.ts +3 -0
  49. package/src/handlers/lifecycle.ts +95 -0
  50. package/src/handlers/permission-gate-handler.ts +190 -0
  51. package/src/handlers/tool-call-boundary.ts +91 -0
  52. package/src/index.ts +225 -0
  53. package/src/input-normalizer.ts +157 -0
  54. package/src/logging.ts +113 -0
  55. package/src/mcp-targets.ts +170 -0
  56. package/src/node-modules-discovery.ts +76 -0
  57. package/src/normalize.ts +43 -0
  58. package/src/path-utils.ts +355 -0
  59. package/src/pattern-suggest.ts +132 -0
  60. package/src/permission-dialog.ts +138 -0
  61. package/src/permission-event-rpc.ts +223 -0
  62. package/src/permission-events.ts +266 -0
  63. package/src/permission-forwarding.ts +188 -0
  64. package/src/permission-gate.ts +94 -0
  65. package/src/permission-manager.ts +392 -0
  66. package/src/permission-merge.ts +32 -0
  67. package/src/permission-prompter.ts +142 -0
  68. package/src/permission-prompts.ts +93 -0
  69. package/src/permission-resolver.ts +109 -0
  70. package/src/permission-session.ts +189 -0
  71. package/src/permission-ui-prompt.ts +127 -0
  72. package/src/permissions-service.ts +63 -0
  73. package/src/persistent-approval-recorder.ts +139 -0
  74. package/src/policy-loader.ts +350 -0
  75. package/src/prompting-gateway.ts +104 -0
  76. package/src/rule.ts +188 -0
  77. package/src/scope-merge.ts +72 -0
  78. package/src/service-lifecycle.ts +49 -0
  79. package/src/service.ts +163 -0
  80. package/src/session-approval-recorder.ts +6 -0
  81. package/src/session-approval.ts +43 -0
  82. package/src/session-logger.ts +91 -0
  83. package/src/session-rules.ts +79 -0
  84. package/src/skill-prompt-sanitizer.ts +292 -0
  85. package/src/status.ts +35 -0
  86. package/src/subagent-context.ts +104 -0
  87. package/src/subagent-lifecycle-events.ts +72 -0
  88. package/src/subagent-registry.ts +105 -0
  89. package/src/synthesize.ts +92 -0
  90. package/src/system-prompt-sanitizer.ts +274 -0
  91. package/src/tool-access-extractor-registry.ts +68 -0
  92. package/src/tool-input-formatter-registry.ts +67 -0
  93. package/src/tool-input-preview.ts +34 -0
  94. package/src/tool-input-prompt-formatters.ts +63 -0
  95. package/src/tool-preview-formatter.ts +207 -0
  96. package/src/tool-registry.ts +148 -0
  97. package/src/types.ts +64 -0
  98. package/src/wildcard-matcher.ts +120 -0
  99. package/src/yolo-mode.ts +30 -0
  100. package/test/active-agent.test.ts +155 -0
  101. package/test/async-cache.test.ts +48 -0
  102. package/test/bash-arity.test.ts +144 -0
  103. package/test/bash-external-directory.test.ts +956 -0
  104. package/test/builtin-tool-input-formatters.test.ts +109 -0
  105. package/test/canonicalize-path.test.ts +93 -0
  106. package/test/common.test.ts +287 -0
  107. package/test/composition-root.test.ts +603 -0
  108. package/test/config-loader.test.ts +740 -0
  109. package/test/config-modal.test.ts +320 -0
  110. package/test/config-paths.test.ts +83 -0
  111. package/test/config-pipeline.test.ts +90 -0
  112. package/test/config-reporter.test.ts +147 -0
  113. package/test/config-store.test.ts +466 -0
  114. package/test/decision-audit.test.ts +72 -0
  115. package/test/decision-reporter.test.ts +112 -0
  116. package/test/denial-messages.test.ts +656 -0
  117. package/test/detect-permissive-bash-fallback.test.ts +56 -0
  118. package/test/expand-home.test.ts +93 -0
  119. package/test/extension-config.test.ts +129 -0
  120. package/test/extension-paths.test.ts +108 -0
  121. package/test/forwarded-permissions/io.test.ts +251 -0
  122. package/test/forwarding-manager.test.ts +194 -0
  123. package/test/handlers/before-agent-start.test.ts +317 -0
  124. package/test/handlers/external-directory-integration.test.ts +623 -0
  125. package/test/handlers/external-directory-session-dedup.test.ts +430 -0
  126. package/test/handlers/external-directory-symlink-acceptance.test.ts +149 -0
  127. package/test/handlers/gates/bash-command-metamorphic.test.ts +83 -0
  128. package/test/handlers/gates/bash-command.test.ts +191 -0
  129. package/test/handlers/gates/bash-external-directory.test.ts +269 -0
  130. package/test/handlers/gates/bash-path.test.ts +337 -0
  131. package/test/handlers/gates/bash-program.test.ts +410 -0
  132. package/test/handlers/gates/bash-token-classification.test.ts +241 -0
  133. package/test/handlers/gates/candidate-check.test.ts +52 -0
  134. package/test/handlers/gates/external-directory-messages.test.ts +61 -0
  135. package/test/handlers/gates/external-directory.test.ts +259 -0
  136. package/test/handlers/gates/helpers.test.ts +177 -0
  137. package/test/handlers/gates/path.test.ts +294 -0
  138. package/test/handlers/gates/runner.test.ts +447 -0
  139. package/test/handlers/gates/skill-input-gate-pipeline.test.ts +176 -0
  140. package/test/handlers/gates/skill-input.test.ts +131 -0
  141. package/test/handlers/gates/skill-read.test.ts +158 -0
  142. package/test/handlers/gates/tool-call-gate-pipeline.test.ts +252 -0
  143. package/test/handlers/gates/tool.test.ts +223 -0
  144. package/test/handlers/input-events.test.ts +168 -0
  145. package/test/handlers/input.test.ts +199 -0
  146. package/test/handlers/lifecycle.test.ts +221 -0
  147. package/test/handlers/tool-call-boundary.test.ts +145 -0
  148. package/test/handlers/tool-call-events.test.ts +277 -0
  149. package/test/handlers/tool-call.test.ts +395 -0
  150. package/test/handlers/validate-requested-tool.test.ts +92 -0
  151. package/test/helpers/gate-fixtures.ts +323 -0
  152. package/test/helpers/handler-fixtures.ts +335 -0
  153. package/test/helpers/make-fake-pi.ts +100 -0
  154. package/test/helpers/manager-harness.ts +112 -0
  155. package/test/helpers/session-fixtures.ts +204 -0
  156. package/test/input-normalizer.test.ts +367 -0
  157. package/test/logging.test.ts +51 -0
  158. package/test/mcp-targets.test.ts +233 -0
  159. package/test/node-modules-discovery.test.ts +97 -0
  160. package/test/normalize.test.ts +247 -0
  161. package/test/path-utils.test.ts +650 -0
  162. package/test/pattern-suggest.test.ts +248 -0
  163. package/test/permission-dialog.test.ts +241 -0
  164. package/test/permission-event-rpc.test.ts +541 -0
  165. package/test/permission-events.test.ts +402 -0
  166. package/test/permission-forwarder.test.ts +369 -0
  167. package/test/permission-forwarding.test.ts +315 -0
  168. package/test/permission-gate.test.ts +305 -0
  169. package/test/permission-manager-unified.test.ts +3368 -0
  170. package/test/permission-merge.test.ts +61 -0
  171. package/test/permission-prompter.test.ts +518 -0
  172. package/test/permission-prompts.test.ts +363 -0
  173. package/test/permission-resolver.test.ts +265 -0
  174. package/test/permission-session.test.ts +363 -0
  175. package/test/permission-ui-prompt.test.ts +146 -0
  176. package/test/permissions-service.test.ts +177 -0
  177. package/test/persistent-approval-recorder.test.ts +133 -0
  178. package/test/pi-infrastructure-read.test.ts +369 -0
  179. package/test/policy-loader.test.ts +561 -0
  180. package/test/prompting-gateway.test.ts +230 -0
  181. package/test/rule.test.ts +604 -0
  182. package/test/scope-merge.test.ts +116 -0
  183. package/test/service-lifecycle.test.ts +163 -0
  184. package/test/service.test.ts +308 -0
  185. package/test/session-approval.test.ts +75 -0
  186. package/test/session-logger.test.ts +200 -0
  187. package/test/session-rules.test.ts +304 -0
  188. package/test/session-start.test.ts +112 -0
  189. package/test/skill-prompt-sanitizer.test.ts +374 -0
  190. package/test/status.test.ts +10 -0
  191. package/test/subagent-context.test.ts +326 -0
  192. package/test/subagent-lifecycle-events.test.ts +132 -0
  193. package/test/subagent-registry.test.ts +145 -0
  194. package/test/synthesize.test.ts +300 -0
  195. package/test/system-prompt-sanitizer.test.ts +382 -0
  196. package/test/tool-access-extractor-registry.test.ts +77 -0
  197. package/test/tool-input-formatter-registry.test.ts +75 -0
  198. package/test/tool-input-preview.test.ts +129 -0
  199. package/test/tool-input-prompt-formatters.test.ts +115 -0
  200. package/test/tool-preview-formatter.test.ts +458 -0
  201. package/test/tool-registry.test.ts +197 -0
  202. package/test/wildcard-matcher.test.ts +424 -0
  203. package/test/yolo-mode.test.ts +188 -0
@@ -0,0 +1,194 @@
1
+ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2
+
3
+ import { ForwardingManager } from "#src/forwarding-manager";
4
+
5
+ // ── Mocks ─────────────────────────────────────────────────────────────────
6
+
7
+ const mockProcessInbox = vi.hoisted(() =>
8
+ vi.fn((): Promise<void> => Promise.resolve()),
9
+ );
10
+ const mockIsSubagentExecutionContext = vi.hoisted(() => vi.fn());
11
+
12
+ vi.mock("../src/subagent-context", () => ({
13
+ isSubagentExecutionContext: mockIsSubagentExecutionContext,
14
+ }));
15
+
16
+ // ── Helpers ───────────────────────────────────────────────────────────────
17
+
18
+ function makeCtx(overrides: { hasUI?: boolean; sessionId?: string } = {}) {
19
+ return {
20
+ hasUI: overrides.hasUI ?? true,
21
+ sessionManager: {
22
+ getSessionId: vi.fn().mockReturnValue(overrides.sessionId ?? "sess-1"),
23
+ },
24
+ cwd: "/project",
25
+ } as unknown as import("@earendil-works/pi-coding-agent").ExtensionContext;
26
+ }
27
+
28
+ function makeForwarder() {
29
+ return { processInbox: mockProcessInbox };
30
+ }
31
+
32
+ function makeManager() {
33
+ return new ForwardingManager("/agent/subagent-sessions", makeForwarder());
34
+ }
35
+
36
+ // ── Tests ─────────────────────────────────────────────────────────────────
37
+
38
+ describe("ForwardingManager", () => {
39
+ beforeEach(() => {
40
+ vi.useFakeTimers();
41
+ mockIsSubagentExecutionContext.mockReset();
42
+ mockIsSubagentExecutionContext.mockReturnValue(false);
43
+ mockProcessInbox.mockReset();
44
+ mockProcessInbox.mockResolvedValue(undefined);
45
+ });
46
+
47
+ afterEach(() => {
48
+ vi.useRealTimers();
49
+ });
50
+
51
+ describe("stop()", () => {
52
+ it("is a no-op when not started", () => {
53
+ const manager = makeManager();
54
+ expect(() => manager.stop()).not.toThrow();
55
+ });
56
+
57
+ it("clears the timer and processing state after start()", async () => {
58
+ const manager = makeManager();
59
+ const ctx = makeCtx();
60
+ manager.start(ctx);
61
+ manager.stop();
62
+
63
+ // After stop, the timer fires no more callbacks.
64
+ mockProcessInbox.mockClear();
65
+ await vi.advanceTimersByTimeAsync(500);
66
+ expect(mockProcessInbox).not.toHaveBeenCalled();
67
+ });
68
+ });
69
+
70
+ describe("start()", () => {
71
+ it("does not start polling when hasUI is false", async () => {
72
+ const manager = makeManager();
73
+ const ctx = makeCtx({ hasUI: false });
74
+ manager.start(ctx);
75
+
76
+ await vi.advanceTimersByTimeAsync(500);
77
+ expect(mockProcessInbox).not.toHaveBeenCalled();
78
+ });
79
+
80
+ it("stops any existing poll and does not start a new one when hasUI is false", async () => {
81
+ const manager = makeManager();
82
+ const uiCtx = makeCtx({ hasUI: true });
83
+ const noUiCtx = makeCtx({ hasUI: false });
84
+
85
+ manager.start(uiCtx);
86
+ // Now stop the polling by calling start() with no-UI ctx.
87
+ manager.start(noUiCtx);
88
+
89
+ mockProcessInbox.mockClear();
90
+ await vi.advanceTimersByTimeAsync(500);
91
+ expect(mockProcessInbox).not.toHaveBeenCalled();
92
+ });
93
+
94
+ it("does not start polling when isSubagentExecutionContext returns true", async () => {
95
+ mockIsSubagentExecutionContext.mockReturnValue(true);
96
+ const manager = makeManager();
97
+ const ctx = makeCtx();
98
+ manager.start(ctx);
99
+
100
+ await vi.advanceTimersByTimeAsync(500);
101
+ expect(mockProcessInbox).not.toHaveBeenCalled();
102
+ });
103
+
104
+ it("stops any existing poll when called with a subagent context", async () => {
105
+ mockIsSubagentExecutionContext.mockReturnValueOnce(false);
106
+ const manager = makeManager();
107
+ const ctx1 = makeCtx();
108
+ manager.start(ctx1);
109
+
110
+ // Second call with a subagent context.
111
+ mockIsSubagentExecutionContext.mockReturnValue(true);
112
+ const ctx2 = makeCtx();
113
+ manager.start(ctx2);
114
+
115
+ mockProcessInbox.mockClear();
116
+ await vi.advanceTimersByTimeAsync(500);
117
+ expect(mockProcessInbox).not.toHaveBeenCalled();
118
+ });
119
+
120
+ it("starts polling and calls processInbox on tick", async () => {
121
+ const manager = makeManager();
122
+ const ctx = makeCtx();
123
+ manager.start(ctx);
124
+
125
+ await vi.advanceTimersByTimeAsync(250);
126
+ expect(mockProcessInbox).toHaveBeenCalledWith(ctx);
127
+ });
128
+
129
+ it("is idempotent — calling start() twice does not create a second timer", async () => {
130
+ const manager = makeManager();
131
+ const ctx = makeCtx();
132
+ manager.start(ctx);
133
+ manager.start(ctx);
134
+
135
+ await vi.advanceTimersByTimeAsync(250);
136
+ // Only one tick should fire per interval, not two.
137
+ expect(mockProcessInbox).toHaveBeenCalledTimes(1);
138
+ });
139
+
140
+ it("updates the context when called again while already running", async () => {
141
+ const manager = makeManager();
142
+ const ctx1 = makeCtx({ sessionId: "sess-1" });
143
+ const ctx2 = makeCtx({ sessionId: "sess-2" });
144
+ manager.start(ctx1);
145
+ manager.start(ctx2);
146
+
147
+ await vi.advanceTimersByTimeAsync(250);
148
+ // The process call should use the newer context.
149
+ expect(mockProcessInbox).toHaveBeenCalledWith(ctx2);
150
+ });
151
+
152
+ it("skips a tick while processing is in progress", async () => {
153
+ // Make processInbox hang so processing=true persists.
154
+ let resolveProcess: () => void;
155
+ mockProcessInbox.mockReturnValue(
156
+ new Promise<void>((resolve) => {
157
+ resolveProcess = resolve;
158
+ }),
159
+ );
160
+
161
+ const manager = makeManager();
162
+ const ctx = makeCtx();
163
+ manager.start(ctx);
164
+
165
+ // First tick starts processing.
166
+ await vi.advanceTimersByTimeAsync(250);
167
+ expect(mockProcessInbox).toHaveBeenCalledTimes(1);
168
+
169
+ // Second tick is skipped because processing flag is still true.
170
+ await vi.advanceTimersByTimeAsync(250);
171
+ expect(mockProcessInbox).toHaveBeenCalledTimes(1);
172
+
173
+ // Resolve and a third tick should fire.
174
+ resolveProcess!();
175
+ await vi.advanceTimersByTimeAsync(250);
176
+ expect(mockProcessInbox).toHaveBeenCalledTimes(2);
177
+ });
178
+
179
+ it("passes subagentSessionsDir from the constructor to isSubagentExecutionContext", () => {
180
+ const manager = new ForwardingManager(
181
+ "/custom/subagent-dir",
182
+ makeForwarder(),
183
+ );
184
+ const ctx = makeCtx();
185
+ manager.start(ctx);
186
+
187
+ expect(mockIsSubagentExecutionContext).toHaveBeenCalledWith(
188
+ ctx,
189
+ "/custom/subagent-dir",
190
+ undefined,
191
+ );
192
+ });
193
+ });
194
+ });
@@ -0,0 +1,317 @@
1
+ import { describe, expect, it, vi } from "vitest";
2
+
3
+ import {
4
+ AgentPrepHandler,
5
+ shouldExposeTool,
6
+ } from "#src/handlers/before-agent-start";
7
+ import type { ToolRegistry } from "#src/tool-registry";
8
+
9
+ import { makeCheckResult, makeCtx } from "#test/helpers/handler-fixtures";
10
+ import {
11
+ makeRealResolver,
12
+ makeRealSession,
13
+ } from "#test/helpers/session-fixtures";
14
+
15
+ // ── SDK stubs ──────────────────────────────────────────────────────────────
16
+ vi.mock("@earendil-works/pi-coding-agent", async (importOriginal) => {
17
+ const original =
18
+ await importOriginal<typeof import("@earendil-works/pi-coding-agent")>();
19
+ return {
20
+ ...original,
21
+ isToolCallEventType: vi.fn().mockReturnValue(false),
22
+ };
23
+ });
24
+
25
+ // ── helpers ────────────────────────────────────────────────────────────────
26
+
27
+ function makeEvent(systemPrompt = "You are an assistant.") {
28
+ return { systemPrompt };
29
+ }
30
+
31
+ function makeToolRegistry(overrides: Partial<ToolRegistry> = {}): ToolRegistry {
32
+ return {
33
+ getAll: vi.fn().mockReturnValue([]),
34
+ getActive: vi.fn().mockReturnValue([]),
35
+ setActive: vi.fn(),
36
+ ...overrides,
37
+ };
38
+ }
39
+
40
+ function makeSetup(opts?: {
41
+ toolPermission?: "allow" | "deny" | "ask";
42
+ toolRegistry?: Partial<ToolRegistry>;
43
+ }) {
44
+ const { session, permissionManager, sessionRules, configStore, forwarding } =
45
+ makeRealSession();
46
+ const { resolver } = makeRealResolver(permissionManager, sessionRules);
47
+ if (opts?.toolPermission !== undefined) {
48
+ vi.mocked(permissionManager.getToolPermission).mockReturnValue(
49
+ opts.toolPermission,
50
+ );
51
+ }
52
+ // Default checkPermission returns allow (for skill-prompt sanitizer)
53
+ vi.mocked(permissionManager.checkPermission).mockReturnValue(
54
+ makeCheckResult(),
55
+ );
56
+ const toolRegistry = makeToolRegistry(opts?.toolRegistry);
57
+ const handler = new AgentPrepHandler(session, resolver, toolRegistry);
58
+ return {
59
+ handler,
60
+ session,
61
+ resolver,
62
+ permissionManager,
63
+ configStore,
64
+ forwarding,
65
+ toolRegistry,
66
+ };
67
+ }
68
+
69
+ // ── shouldExposeTool (pure helper) ─────────────────────────────────────────
70
+
71
+ describe("shouldExposeTool", () => {
72
+ it("returns true when tool permission is allow", () => {
73
+ const getter = vi.fn().mockReturnValue("allow");
74
+ expect(shouldExposeTool("read", null, getter)).toBe(true);
75
+ });
76
+
77
+ it("returns true when tool permission is ask", () => {
78
+ const getter = vi.fn().mockReturnValue("ask");
79
+ expect(shouldExposeTool("bash", "agent-x", getter)).toBe(true);
80
+ });
81
+
82
+ it("returns false when tool permission is deny", () => {
83
+ const getter = vi.fn().mockReturnValue("deny");
84
+ expect(shouldExposeTool("write", null, getter)).toBe(false);
85
+ });
86
+
87
+ it("passes agentName through to getToolPermission", () => {
88
+ const getter = vi.fn().mockReturnValue("allow");
89
+ shouldExposeTool("read", "my-agent", getter);
90
+ expect(getter).toHaveBeenCalledWith("read", "my-agent");
91
+ });
92
+
93
+ it("converts null agentName to undefined for getToolPermission", () => {
94
+ const getter = vi.fn().mockReturnValue("allow");
95
+ shouldExposeTool("read", null, getter);
96
+ expect(getter).toHaveBeenCalledWith("read", undefined);
97
+ });
98
+ });
99
+
100
+ // ── AgentPrepHandler.handle ────────────────────────────────────────────────
101
+
102
+ describe("AgentPrepHandler.handle", () => {
103
+ it("activates the session with ctx", async () => {
104
+ const ctx = makeCtx();
105
+ const { handler, forwarding } = makeSetup();
106
+ await handler.handle(makeEvent(), ctx);
107
+ // Real session.activate calls forwarding.start
108
+ expect(forwarding.start).toHaveBeenCalledWith(ctx);
109
+ });
110
+
111
+ it("refreshes config with ctx", async () => {
112
+ const ctx = makeCtx();
113
+ const { handler, configStore } = makeSetup();
114
+ await handler.handle(makeEvent(), ctx);
115
+ expect(configStore.refresh).toHaveBeenCalledWith(ctx);
116
+ });
117
+
118
+ it("resolves agent name using systemPrompt", async () => {
119
+ const ctx = makeCtx();
120
+ const { handler, session } = makeSetup();
121
+ const spy = vi.spyOn(session, "resolveAgentName");
122
+ await handler.handle(makeEvent("<active_agent name='x'>"), ctx);
123
+ expect(spy).toHaveBeenCalledWith(ctx, "<active_agent name='x'>");
124
+ });
125
+
126
+ it("filters out denied tools from allowed list", async () => {
127
+ const { handler, toolRegistry } = makeSetup({
128
+ toolPermission: "deny",
129
+ toolRegistry: {
130
+ getActive: vi.fn().mockReturnValue(["write", "read"]),
131
+ },
132
+ });
133
+ await handler.handle(makeEvent(), makeCtx());
134
+ expect(toolRegistry.setActive).toHaveBeenCalledWith([]);
135
+ });
136
+
137
+ it("includes allowed and ask tools in the active list", async () => {
138
+ const { handler, toolRegistry } = makeSetup({
139
+ toolRegistry: {
140
+ getActive: vi.fn().mockReturnValue(["read", "write"]),
141
+ },
142
+ });
143
+ await handler.handle(makeEvent(), makeCtx());
144
+ expect(toolRegistry.setActive).toHaveBeenCalledWith(["read", "write"]);
145
+ });
146
+
147
+ it("does not activate registered tools pi left inactive (find/grep/ls)", async () => {
148
+ // Regression for #385: the active set is the base, not the full registry.
149
+ const { handler, toolRegistry } = makeSetup({
150
+ toolRegistry: {
151
+ getActive: vi.fn().mockReturnValue(["read", "bash", "edit", "write"]),
152
+ getAll: vi
153
+ .fn()
154
+ .mockReturnValue([
155
+ { name: "read" },
156
+ { name: "bash" },
157
+ { name: "edit" },
158
+ { name: "write" },
159
+ { name: "find" },
160
+ { name: "grep" },
161
+ { name: "ls" },
162
+ ]),
163
+ },
164
+ });
165
+ await handler.handle(makeEvent(), makeCtx());
166
+ expect(toolRegistry.setActive).toHaveBeenCalledWith([
167
+ "read",
168
+ "bash",
169
+ "edit",
170
+ "write",
171
+ ]);
172
+ });
173
+
174
+ it("calls setActive on every turn (no dedup gate)", async () => {
175
+ const { handler, toolRegistry } = makeSetup({
176
+ toolRegistry: {
177
+ getActive: vi.fn().mockReturnValue(["read"]),
178
+ },
179
+ });
180
+ await handler.handle(makeEvent(), makeCtx());
181
+ await handler.handle(makeEvent(), makeCtx());
182
+ expect(toolRegistry.setActive).toHaveBeenCalledTimes(2);
183
+ });
184
+
185
+ it("filters a denied skill from the systemPrompt on every turn, not just the first", async () => {
186
+ const systemPrompt = [
187
+ "You are an assistant.",
188
+ "",
189
+ "<available_skills>",
190
+ " <skill>",
191
+ " <name>secret</name>",
192
+ " <description>A denied skill</description>",
193
+ " <location>/skills/secret/SKILL.md</location>",
194
+ " </skill>",
195
+ "</available_skills>",
196
+ ].join("\n");
197
+ const { handler, permissionManager } = makeSetup();
198
+ vi.mocked(permissionManager.checkPermission).mockImplementation(
199
+ (surface) =>
200
+ surface === "skill"
201
+ ? makeCheckResult({ state: "deny" })
202
+ : makeCheckResult(),
203
+ );
204
+
205
+ const first = await handler.handle(makeEvent(systemPrompt), makeCtx());
206
+ const second = await handler.handle(makeEvent(systemPrompt), makeCtx());
207
+
208
+ expect(first).toHaveProperty("systemPrompt");
209
+ expect((first as { systemPrompt: string }).systemPrompt).not.toContain(
210
+ "secret",
211
+ );
212
+ expect(second).toHaveProperty("systemPrompt");
213
+ expect((second as { systemPrompt: string }).systemPrompt).not.toContain(
214
+ "secret",
215
+ );
216
+ });
217
+
218
+ it("returns empty object on repeated calls with unchanged inputs", async () => {
219
+ const { handler } = makeSetup();
220
+ await handler.handle(makeEvent(), makeCtx());
221
+ const result = await handler.handle(makeEvent(), makeCtx());
222
+ expect(result).toEqual({});
223
+ });
224
+
225
+ it("stores resolved skill entries on the session", async () => {
226
+ const { handler, session } = makeSetup();
227
+ const spy = vi.spyOn(session, "setActiveSkillEntries");
228
+ await handler.handle(makeEvent(), makeCtx());
229
+ expect(spy).toHaveBeenCalledWith(expect.any(Array));
230
+ });
231
+
232
+ it("returns modified systemPrompt when prompt changes", async () => {
233
+ const systemPrompt = `You are an assistant.\n\nAvailable tools:\n- read\n- write\n`;
234
+ const { handler } = makeSetup();
235
+ const result = await handler.handle(makeEvent(systemPrompt), makeCtx());
236
+ expect(result).toHaveProperty("systemPrompt");
237
+ });
238
+
239
+ it("returns empty object when systemPrompt is unchanged", async () => {
240
+ const prompt = "No tools section here.";
241
+ const { handler } = makeSetup();
242
+ const result = await handler.handle(makeEvent(prompt), makeCtx());
243
+ expect(result).toEqual({});
244
+ });
245
+
246
+ it("narrows a denied tool out of the Available tools listing without removing the section", async () => {
247
+ const systemPrompt = [
248
+ "Available tools:",
249
+ "- read: Read file contents",
250
+ "- bash: Run shell commands",
251
+ ].join("\n");
252
+ const { handler, permissionManager } = makeSetup({
253
+ toolRegistry: {
254
+ getActive: vi.fn().mockReturnValue(["read", "bash"]),
255
+ },
256
+ });
257
+ vi.mocked(permissionManager.getToolPermission).mockImplementation((tool) =>
258
+ tool === "bash" ? "deny" : "allow",
259
+ );
260
+
261
+ const result = await handler.handle(makeEvent(systemPrompt), makeCtx());
262
+
263
+ expect(result.systemPrompt).toBeDefined();
264
+ const out = result.systemPrompt ?? "";
265
+ expect(out).toContain("Available tools:");
266
+ expect(out).toContain("- read: Read file contents");
267
+ expect(out).not.toContain("- bash");
268
+ });
269
+
270
+ it("keeps the wire system prompt byte-stable across the tool-listing drift between turns", async () => {
271
+ const fullProse = [
272
+ "You are an assistant.",
273
+ "",
274
+ "Available tools:",
275
+ "- bash: Run shell commands",
276
+ "- read: Read file contents",
277
+ "- edit: Edit a file",
278
+ "- write: Write a file",
279
+ "",
280
+ "Guidelines:",
281
+ "- use bash for file operations like ls, rg, find",
282
+ "- use read to examine files instead of cat or sed.",
283
+ "- Be concise in your responses",
284
+ ].join("\n");
285
+ const narrowedProse = [
286
+ "You are an assistant.",
287
+ "",
288
+ "Available tools:",
289
+ "- read: Read file contents",
290
+ "- edit: Edit a file",
291
+ "- write: Write a file",
292
+ "",
293
+ "Guidelines:",
294
+ "- use read to examine files instead of cat or sed.",
295
+ "- Be concise in your responses",
296
+ ].join("\n");
297
+ const { handler, permissionManager } = makeSetup({
298
+ toolRegistry: {
299
+ getActive: vi.fn().mockReturnValue(["bash", "read", "edit", "write"]),
300
+ },
301
+ });
302
+ vi.mocked(permissionManager.getToolPermission).mockImplementation((tool) =>
303
+ tool === "bash" ? "deny" : "allow",
304
+ );
305
+
306
+ // Turn 1: Pi feeds the full default listing.
307
+ const first = await handler.handle(makeEvent(fullProse), makeCtx());
308
+ // Turn 2: Pi's setActive rebuild means the event now carries the narrowed
309
+ // listing, so the override the handler returns must still match turn 1.
310
+ const second = await handler.handle(makeEvent(narrowedProse), makeCtx());
311
+
312
+ const wire1 = first.systemPrompt ?? fullProse;
313
+ const wire2 = second.systemPrompt ?? narrowedProse;
314
+ expect(wire1).toBe(narrowedProse);
315
+ expect(wire2).toBe(narrowedProse);
316
+ });
317
+ });