@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,603 @@
1
+ /**
2
+ * Composition-root tests for `piPermissionSystemExtension(pi)`.
3
+ *
4
+ * These run the real factory via the `makeFakePi()` harness and assert the
5
+ * wiring contracts that unit tests cannot see: handler-registration
6
+ * completeness, shared-instance contracts across factory invocations, teardown,
7
+ * service↔gate registry sharing, and `ready`-after-publish ordering.
8
+ *
9
+ * Every test runs the factory, which mutates two process-global `Symbol.for()`
10
+ * slots and reads `PI_CODING_AGENT_DIR`. The shared `beforeEach`/`afterEach`
11
+ * isolate the agent dir to a tmpdir and clear both global slots so factory runs
12
+ * do not leak across tests.
13
+ */
14
+ import {
15
+ mkdirSync,
16
+ mkdtempSync,
17
+ readdirSync,
18
+ readFileSync,
19
+ rmSync,
20
+ writeFileSync,
21
+ } from "node:fs";
22
+ import { tmpdir } from "node:os";
23
+ import { dirname, join } from "node:path";
24
+
25
+ import {
26
+ createEventBus,
27
+ type ExtensionAPI,
28
+ } from "@earendil-works/pi-coding-agent";
29
+ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
30
+
31
+ import { getGlobalConfigPath } from "#src/config-paths";
32
+ import { DEFAULT_EXTENSION_CONFIG } from "#src/extension-config";
33
+ import piPermissionSystemExtension from "#src/index";
34
+ import {
35
+ PERMISSIONS_READY_CHANNEL,
36
+ PERMISSIONS_RPC_CHECK_CHANNEL,
37
+ } from "#src/permission-events";
38
+ import {
39
+ createPermissionForwardingLocation,
40
+ type ForwardedPermissionRequest,
41
+ } from "#src/permission-forwarding";
42
+ import { getPermissionsService } from "#src/service";
43
+ import { SUBAGENT_CHILD_SESSION_CREATED } from "#src/subagent-lifecycle-events";
44
+ import { getSubagentSessionRegistry } from "#src/subagent-registry";
45
+ import { makeFakePi } from "#test/helpers/make-fake-pi";
46
+
47
+ const SERVICE_KEY = Symbol.for("@gotgenes/pi-permission-system:service");
48
+ const SUBAGENT_REGISTRY_KEY = Symbol.for(
49
+ "@gotgenes/pi-permission-system:subagent-registry",
50
+ );
51
+
52
+ /** The six events the factory must register a handler for. */
53
+ const EXPECTED_HANDLERS = [
54
+ "before_agent_start",
55
+ "input",
56
+ "resources_discover",
57
+ "session_shutdown",
58
+ "session_start",
59
+ "tool_call",
60
+ ];
61
+
62
+ let agentDir: string;
63
+
64
+ beforeEach(() => {
65
+ agentDir = mkdtempSync(join(tmpdir(), "pi-perm-comp-root-"));
66
+ vi.stubEnv("PI_CODING_AGENT_DIR", agentDir);
67
+ });
68
+
69
+ afterEach(() => {
70
+ // Drop both process-global slots so factory runs do not leak across tests.
71
+ const store = globalThis as Record<symbol, unknown>;
72
+ // eslint-disable-next-line @typescript-eslint/no-dynamic-delete -- Symbol-keyed global property
73
+ delete store[SERVICE_KEY];
74
+ // eslint-disable-next-line @typescript-eslint/no-dynamic-delete -- Symbol-keyed global property
75
+ delete store[SUBAGENT_REGISTRY_KEY];
76
+ vi.unstubAllEnvs();
77
+ rmSync(agentDir, { recursive: true, force: true });
78
+ });
79
+
80
+ // ── Shared helpers ──────────────────────────────────────────────────────────
81
+
82
+ /** Write the global config file under the stubbed agent dir. */
83
+ function writeGlobalConfig(config: Record<string, unknown>): void {
84
+ const globalConfigPath = getGlobalConfigPath(agentDir);
85
+ mkdirSync(dirname(globalConfigPath), { recursive: true });
86
+ writeFileSync(
87
+ globalConfigPath,
88
+ `${JSON.stringify({ ...DEFAULT_EXTENSION_CONFIG, ...config }, null, 2)}\n`,
89
+ "utf8",
90
+ );
91
+ }
92
+
93
+ /** Build a minimal subagent `ctx` (no UI) for driving tool-call gates. */
94
+ function makeChildCtx(cwd: string, sessionId: string): unknown {
95
+ return {
96
+ cwd,
97
+ hasUI: false,
98
+ sessionManager: {
99
+ getEntries: (): unknown[] => [],
100
+ getSessionId: (): string => sessionId,
101
+ getSessionDir: (): string => cwd,
102
+ },
103
+ ui: {
104
+ notify: (): void => {},
105
+ setStatus: (): void => {},
106
+ select: async (): Promise<string | undefined> => undefined,
107
+ input: async (): Promise<string | undefined> => undefined,
108
+ },
109
+ };
110
+ }
111
+
112
+ /**
113
+ * Build a UI-present `ctx` that records the titles passed to `ui.select`, and
114
+ * approves every prompt. The ask-prompt message (which embeds the tool-input
115
+ * preview) is the first line of the select title.
116
+ */
117
+ function makeUiCtx(cwd: string, capturedTitles: string[]): { ctx: unknown } {
118
+ const ctx = {
119
+ cwd,
120
+ hasUI: true,
121
+ sessionManager: {
122
+ getEntries: (): unknown[] => [],
123
+ getSessionId: (): string => "ui-session",
124
+ getSessionDir: (): string => cwd,
125
+ },
126
+ ui: {
127
+ notify: (): void => {},
128
+ setStatus: (): void => {},
129
+ select: async (title: string): Promise<string | undefined> => {
130
+ capturedTitles.push(title);
131
+ return "Yes";
132
+ },
133
+ input: async (): Promise<string | undefined> => undefined,
134
+ },
135
+ };
136
+ return { ctx };
137
+ }
138
+
139
+ const sleep = (ms: number): Promise<void> =>
140
+ new Promise((resolve) => setTimeout(resolve, ms));
141
+
142
+ /** Drive the registered `session_start` handler with a ctx. */
143
+ function fireSessionStart(
144
+ pi: ReturnType<typeof makeFakePi>,
145
+ ctx: unknown,
146
+ ): Promise<unknown> {
147
+ return pi.fire("session_start", { reason: "start" }, ctx);
148
+ }
149
+
150
+ /**
151
+ * Simulate the parent UI session responding to a forwarded permission request.
152
+ *
153
+ * Polls the parent's requests directory for the child's request file, then
154
+ * writes an approval response so the child's forwarding poll resolves quickly
155
+ * instead of waiting out the 10-minute timeout.
156
+ */
157
+ async function approveForwardedRequest(
158
+ forwardingDir: string,
159
+ parentSessionId: string,
160
+ ): Promise<ForwardedPermissionRequest> {
161
+ const location = createPermissionForwardingLocation(
162
+ forwardingDir,
163
+ parentSessionId,
164
+ );
165
+ const deadline = Date.now() + 2000;
166
+ while (Date.now() < deadline) {
167
+ let files: string[] = [];
168
+ try {
169
+ files = readdirSync(location.requestsDir).filter((f) =>
170
+ f.endsWith(".json"),
171
+ );
172
+ } catch {
173
+ files = [];
174
+ }
175
+ const requestFile = files[0];
176
+ if (requestFile) {
177
+ const request = JSON.parse(
178
+ readFileSync(join(location.requestsDir, requestFile), "utf8"),
179
+ ) as ForwardedPermissionRequest;
180
+ mkdirSync(location.responsesDir, { recursive: true });
181
+ writeFileSync(
182
+ join(location.responsesDir, `${request.id}.json`),
183
+ JSON.stringify({
184
+ approved: true,
185
+ state: "approved",
186
+ responderSessionId: parentSessionId,
187
+ respondedAt: Date.now(),
188
+ }),
189
+ "utf8",
190
+ );
191
+ return request;
192
+ }
193
+ await sleep(5);
194
+ }
195
+ throw new Error("Timed out waiting for the forwarded permission request");
196
+ }
197
+
198
+ describe("event-handler registration completeness", () => {
199
+ it("registers a handler for every required event exactly once", () => {
200
+ const pi = makeFakePi();
201
+ piPermissionSystemExtension(pi as unknown as ExtensionAPI);
202
+
203
+ expect([...pi.handlers.keys()].sort()).toEqual(EXPECTED_HANDLERS);
204
+ });
205
+ });
206
+
207
+ describe("subagent registry sharing across factory instances", () => {
208
+ // The #296 regression class: two factory invocations on *different* event
209
+ // buses must still resolve the same process-global SubagentSessionRegistry,
210
+ // so a child registered via the parent's bus detects itself as a subagent and
211
+ // forwards (rather than blocking) an external-directory `ask`.
212
+ it("lets a child instance forward an ask it received via the parent's bus", async () => {
213
+ writeGlobalConfig({
214
+ permission: { "*": "allow", external_directory: "ask" },
215
+ });
216
+
217
+ const childCwd = mkdtempSync(join(tmpdir(), "pi-perm-child-cwd-"));
218
+ const externalDir = mkdtempSync(join(tmpdir(), "pi-perm-external-"));
219
+ const forwardingDir = join(agentDir, "sessions", "permission-forwarding");
220
+ const parentSessionId = "parent-session-1";
221
+ const childSessionId = "child-session-1";
222
+
223
+ // Two factory instances, each wired to its own event bus (as in production:
224
+ // every session's ResourceLoader creates a separate bus).
225
+ const parentBus = createEventBus();
226
+ const childBus = createEventBus();
227
+ piPermissionSystemExtension(
228
+ makeFakePi({ events: parentBus }) as unknown as ExtensionAPI,
229
+ );
230
+ const childPi = makeFakePi({
231
+ events: childBus,
232
+ toolNames: ["read"],
233
+ });
234
+ piPermissionSystemExtension(childPi as unknown as ExtensionAPI);
235
+
236
+ // The child session is announced on the *parent's* bus only; the parent's
237
+ // lifecycle subscription writes it into the shared global registry.
238
+ parentBus.emit(SUBAGENT_CHILD_SESSION_CREATED, {
239
+ sessionId: childSessionId,
240
+ parentSessionId,
241
+ });
242
+
243
+ // The child fires an external-directory read with no UI. With the shared
244
+ // registry it detects itself as a subagent and forwards; the simulated
245
+ // parent approves.
246
+ const firePromise = childPi.fire(
247
+ "tool_call",
248
+ {
249
+ toolName: "read",
250
+ toolCallId: "child-external-read",
251
+ input: { path: join(externalDir, "secret.txt") },
252
+ },
253
+ makeChildCtx(childCwd, childSessionId),
254
+ );
255
+
256
+ const request = await approveForwardedRequest(
257
+ forwardingDir,
258
+ parentSessionId,
259
+ );
260
+ expect(request.targetSessionId).toBe(parentSessionId);
261
+ expect(request.requesterSessionId).toBe(childSessionId);
262
+ // The child persists the original display fields so the parent emits a
263
+ // non-degraded `permissions:ui_prompt` event (forwarded non-degradation).
264
+ expect(request.source).toBe("tool_call");
265
+ expect(request.surface).toBe("read");
266
+ expect(request.value).toBe(join(externalDir, "secret.txt"));
267
+
268
+ const result = (await firePromise) as { block?: true };
269
+ expect(result.block).toBeUndefined();
270
+
271
+ rmSync(childCwd, { recursive: true, force: true });
272
+ rmSync(externalDir, { recursive: true, force: true });
273
+ });
274
+ });
275
+
276
+ describe("shutdown teardown chain", () => {
277
+ it("unpublishes the service and unsubscribes the lifecycle on shutdown", async () => {
278
+ const cwd = mkdtempSync(join(tmpdir(), "pi-perm-teardown-cwd-"));
279
+ const pi = makeFakePi();
280
+ piPermissionSystemExtension(pi as unknown as ExtensionAPI);
281
+
282
+ // The service is published at session_start, not at factory init.
283
+ await fireSessionStart(pi, makeChildCtx(cwd, "top-session"));
284
+ expect(getPermissionsService()).toBeDefined();
285
+
286
+ await pi.fire("session_shutdown");
287
+
288
+ // Service slot cleared.
289
+ expect(getPermissionsService()).toBeUndefined();
290
+
291
+ // Lifecycle unsubscribed: a post-shutdown session-created must not register.
292
+ pi.events.emit(SUBAGENT_CHILD_SESSION_CREATED, {
293
+ sessionId: "late-child",
294
+ parentSessionId: "p-late",
295
+ });
296
+ expect(getSubagentSessionRegistry().has("late-child")).toBe(false);
297
+
298
+ rmSync(cwd, { recursive: true, force: true });
299
+ });
300
+ });
301
+
302
+ describe("service and gate share one formatter registry", () => {
303
+ // A formatter registered through the published service must be consulted by
304
+ // the live gate handler — proving both reference the same
305
+ // ToolInputFormatterRegistry instance the factory created once.
306
+ it("surfaces a service-registered formatter in the gate's ask prompt", async () => {
307
+ writeGlobalConfig({
308
+ permission: { "*": "allow", demo: "ask" },
309
+ });
310
+
311
+ const cwd = mkdtempSync(join(tmpdir(), "pi-perm-ui-cwd-"));
312
+ const pi = makeFakePi({ toolNames: ["demo"] });
313
+ piPermissionSystemExtension(pi as unknown as ExtensionAPI);
314
+
315
+ const capturedTitles: string[] = [];
316
+ const { ctx } = makeUiCtx(cwd, capturedTitles);
317
+ // The service is published at session_start; publish before resolving it.
318
+ await fireSessionStart(pi, ctx);
319
+
320
+ const previewMarker = "PREVIEW::shared-registry-proof";
321
+ getPermissionsService()!.registerToolInputFormatter(
322
+ "demo",
323
+ () => previewMarker,
324
+ );
325
+ const result = (await pi.fire(
326
+ "tool_call",
327
+ { toolName: "demo", toolCallId: "demo-ask", input: { foo: "bar" } },
328
+ ctx,
329
+ )) as { block?: true };
330
+
331
+ // The gate prompted (not blocked) and the prompt embedded the formatter's
332
+ // preview — so the gate consulted the same registry the service wrote to.
333
+ expect(result.block).toBeUndefined();
334
+ expect(capturedTitles.some((t) => t.includes(previewMarker))).toBe(true);
335
+
336
+ rmSync(cwd, { recursive: true, force: true });
337
+ });
338
+ });
339
+
340
+ describe("service and gate share one access extractor registry", () => {
341
+ // An extractor registered through the published service must be consulted by
342
+ // the live gate handler — proving both reference the same
343
+ // ToolAccessExtractorRegistry instance the factory created once (#352).
344
+ it("path-gates a custom-shaped tool via a service-registered extractor", async () => {
345
+ writeGlobalConfig({
346
+ permission: { "*": "allow", path: { "*.env": "deny" } },
347
+ });
348
+
349
+ const cwd = mkdtempSync(join(tmpdir(), "pi-perm-ext-cwd-"));
350
+ const pi = makeFakePi({ toolNames: ["ffgrep"] });
351
+ piPermissionSystemExtension(pi as unknown as ExtensionAPI);
352
+
353
+ const { ctx } = makeUiCtx(cwd, []);
354
+ await fireSessionStart(pi, ctx);
355
+
356
+ // ffgrep carries its path under a non-standard key; without the extractor
357
+ // the default input.path convention would miss it.
358
+ getPermissionsService()!.registerToolAccessExtractor("ffgrep", (input) =>
359
+ typeof input.target === "string" ? input.target : undefined,
360
+ );
361
+
362
+ const result = (await pi.fire(
363
+ "tool_call",
364
+ { toolName: "ffgrep", toolCallId: "ff-1", input: { target: ".env" } },
365
+ ctx,
366
+ )) as { block?: true };
367
+
368
+ // The path deny fired — so the gate extracted ffgrep's path through the
369
+ // same registry the service wrote to.
370
+ expect(result.block).toBe(true);
371
+
372
+ rmSync(cwd, { recursive: true, force: true });
373
+ });
374
+ });
375
+
376
+ describe("ready emitted after service publication", () => {
377
+ // Ordering contracts exist only at the composition root: a consumer reacting
378
+ // to permissions:ready must be able to resolve the service immediately. The
379
+ // service is published and ready fires at session_start (not factory init).
380
+ it("publishes the service before emitting permissions:ready", async () => {
381
+ const cwd = mkdtempSync(join(tmpdir(), "pi-perm-ready-cwd-"));
382
+ const seen: string[] = [];
383
+ const pi = makeFakePi();
384
+ pi.events.on(PERMISSIONS_READY_CHANNEL, () => {
385
+ seen.push(getPermissionsService() ? "present" : "missing");
386
+ });
387
+
388
+ piPermissionSystemExtension(pi as unknown as ExtensionAPI);
389
+
390
+ // ready is not emitted at load; only after session_start publishes.
391
+ expect(seen).toEqual([]);
392
+
393
+ await fireSessionStart(pi, makeChildCtx(cwd, "top-session"));
394
+
395
+ expect(seen).toEqual(["present"]);
396
+
397
+ rmSync(cwd, { recursive: true, force: true });
398
+ });
399
+ });
400
+
401
+ describe("single source of truth for session state", () => {
402
+ // Regression guard for the split-brain bug: before the fix, the gate path
403
+ // recorded session approvals into a private SessionRules instance that the
404
+ // RPC check and the service never saw. After the fix, both readers use the
405
+ // same SessionRules the gate writes into.
406
+ it("gate session-approval is visible to the RPC check and the service", async () => {
407
+ writeGlobalConfig({
408
+ permission: { "*": "allow", demo: "ask" },
409
+ });
410
+
411
+ const cwd = mkdtempSync(join(tmpdir(), "pi-perm-sot-cwd-"));
412
+ const pi = makeFakePi({ toolNames: ["demo"] });
413
+ piPermissionSystemExtension(pi as unknown as ExtensionAPI);
414
+
415
+ // UI ctx that approves the gate prompt for this session (options[1]).
416
+ const ctx = {
417
+ cwd,
418
+ hasUI: true,
419
+ sessionManager: {
420
+ getEntries: (): unknown[] => [],
421
+ getSessionId: (): string => "sot-session",
422
+ getSessionDir: (): string => cwd,
423
+ },
424
+ ui: {
425
+ notify: (): void => {},
426
+ setStatus: (): void => {},
427
+ // Return the second option label-agnostically — always the
428
+ // "for this session" choice regardless of the exact label text.
429
+ select: async (
430
+ _title: string,
431
+ options: string[],
432
+ ): Promise<string | undefined> => options[1],
433
+ input: async (): Promise<string | undefined> => undefined,
434
+ },
435
+ };
436
+
437
+ await fireSessionStart(pi, ctx);
438
+
439
+ // Drive a tool_call on "demo"; the gate prompts and the mock selects
440
+ // options[1], recording a session-scoped approval.
441
+ await pi.fire(
442
+ "tool_call",
443
+ {
444
+ toolName: "demo",
445
+ toolCallId: "demo-for-session",
446
+ input: { foo: "bar" },
447
+ },
448
+ ctx,
449
+ );
450
+
451
+ // RPC check — the deprecated channel must now reflect the session approval.
452
+ // eslint-disable-next-line @typescript-eslint/no-deprecated -- intentionally testing the deprecated RPC channel's session-rules visibility
453
+ const rpcCheckChannel: string = PERMISSIONS_RPC_CHECK_CHANNEL;
454
+ const requestId = "sot-rpc-1";
455
+ const replyPromise = new Promise<unknown>((resolve) => {
456
+ const unsub = pi.events.on(
457
+ `${rpcCheckChannel}:reply:${requestId}`,
458
+ (data) => {
459
+ unsub();
460
+ resolve(data);
461
+ },
462
+ );
463
+ });
464
+ pi.events.emit(rpcCheckChannel, { requestId, surface: "demo" });
465
+ const reply = (await replyPromise) as {
466
+ success: boolean;
467
+ data?: { result: string };
468
+ };
469
+
470
+ expect(reply.success).toBe(true);
471
+ // Before the fix this was "ask" — the RPC channel read an empty SessionRules.
472
+ expect(reply.data?.result).toBe("allow");
473
+
474
+ // Service accessor must also see the session approval.
475
+ const serviceResult = getPermissionsService()!.checkPermission("demo");
476
+ expect(serviceResult.state).toBe("allow");
477
+
478
+ rmSync(cwd, { recursive: true, force: true });
479
+ });
480
+ });
481
+
482
+ describe("multi-instance global service interplay", () => {
483
+ // The fix (#302) scopes the process-global service slot to the publishing
484
+ // instance. The parent publishes at its session_start; an in-process child
485
+ // (registered by session id) skips publishing, and its identity-scoped
486
+ // teardown is a no-op — so the parent's service is the one that resolves
487
+ // throughout the child's lifecycle and survives the child's shutdown.
488
+ it("keeps the parent's service published across the child's lifecycle", async () => {
489
+ const parentCwd = mkdtempSync(join(tmpdir(), "pi-perm-parent-cwd-"));
490
+ const childCwd = mkdtempSync(join(tmpdir(), "pi-perm-child-cwd-"));
491
+ const childSessionId = "child-session-mi";
492
+
493
+ const parentPi = makeFakePi({ events: createEventBus() });
494
+ piPermissionSystemExtension(parentPi as unknown as ExtensionAPI);
495
+ const childPi = makeFakePi({ events: createEventBus() });
496
+ piPermissionSystemExtension(childPi as unknown as ExtensionAPI);
497
+
498
+ // The parent is not a registered child, so it publishes its service.
499
+ await fireSessionStart(
500
+ parentPi,
501
+ makeChildCtx(parentCwd, "parent-session-mi"),
502
+ );
503
+ const parentService = getPermissionsService();
504
+ expect(parentService).toBeDefined();
505
+
506
+ // The child is registered in the shared global registry before its own
507
+ // session_start, so it detects itself and skips publishing.
508
+ getSubagentSessionRegistry().register(childSessionId, {
509
+ parentSessionId: "parent-session-mi",
510
+ });
511
+ await fireSessionStart(childPi, makeChildCtx(childCwd, childSessionId));
512
+
513
+ // Mid-run: the slot resolves the parent's service, never the child's.
514
+ expect(getPermissionsService()).toBe(parentService);
515
+
516
+ // The child's shutdown is a no-op for the slot it never owned.
517
+ await childPi.fire("session_shutdown");
518
+ expect(getPermissionsService()).toBe(parentService);
519
+
520
+ rmSync(parentCwd, { recursive: true, force: true });
521
+ rmSync(childCwd, { recursive: true, force: true });
522
+ });
523
+ });
524
+
525
+ describe("session approvals do not leak across same-cwd session switches", () => {
526
+ // Pi caches the extension *import* (the jiti module, factory function) for
527
+ // same-cwd `/new` / `/resume` / `/fork` / `/import` switches
528
+ // (earendil-works/pi#5905). The factory is still re-invoked per switch, and
529
+ // `session_shutdown` still fires — so a session-scoped "allow for this
530
+ // session" grant must not survive into the next session.
531
+ //
532
+ // Two factory invocations against the same cwd model the cached-import
533
+ // switch: invocation #1 records an approval and shuts down; invocation #2 is
534
+ // the re-invoked cached factory. The new session must start with an empty
535
+ // SessionRules. Two independent mechanisms keep it empty, and the grant only
536
+ // leaks if *both* break together: `session_shutdown` clears the first
537
+ // instance's rules, and the re-invoked factory builds a fresh SessionRules
538
+ // (no module-scoped state bridges the switch — the per-session reset the
539
+ // fresh-jiti load used to provide is gone once the import is cached).
540
+
541
+ /** A UI ctx that approves the gate's "for this session" option (options[1]). */
542
+ function makeSessionApprovingCtx(cwd: string, sessionId: string): unknown {
543
+ return {
544
+ cwd,
545
+ hasUI: true,
546
+ sessionManager: {
547
+ getEntries: (): unknown[] => [],
548
+ getSessionId: (): string => sessionId,
549
+ getSessionDir: (): string => cwd,
550
+ },
551
+ ui: {
552
+ notify: (): void => {},
553
+ setStatus: (): void => {},
554
+ select: async (
555
+ _title: string,
556
+ options: string[],
557
+ ): Promise<string | undefined> => options[1],
558
+ input: async (): Promise<string | undefined> => undefined,
559
+ },
560
+ };
561
+ }
562
+
563
+ it("starts the next same-cwd session with an empty session ruleset", async () => {
564
+ writeGlobalConfig({
565
+ permission: { "*": "allow", demo: "ask" },
566
+ });
567
+
568
+ const cwd = mkdtempSync(join(tmpdir(), "pi-perm-switch-cwd-"));
569
+
570
+ // ── Session #1: approve `demo` for the session, then shut down ──────────
571
+ const firstPi = makeFakePi({ toolNames: ["demo"] });
572
+ piPermissionSystemExtension(firstPi as unknown as ExtensionAPI);
573
+
574
+ const firstCtx = makeSessionApprovingCtx(cwd, "switch-session-1");
575
+ await fireSessionStart(firstPi, firstCtx);
576
+
577
+ // The gate prompts and the mock selects options[1], recording a
578
+ // session-scoped approval the service can read back.
579
+ await firstPi.fire(
580
+ "tool_call",
581
+ { toolName: "demo", toolCallId: "demo-approve", input: { foo: "bar" } },
582
+ firstCtx,
583
+ );
584
+ expect(getPermissionsService()!.checkPermission("demo").state).toBe(
585
+ "allow",
586
+ );
587
+
588
+ // The switch tears down the old session before the new one starts.
589
+ await firstPi.fire("session_shutdown");
590
+
591
+ // ── Session #2: the re-invoked cached factory, same cwd ────────────────
592
+ const secondPi = makeFakePi({ toolNames: ["demo"] });
593
+ piPermissionSystemExtension(secondPi as unknown as ExtensionAPI);
594
+
595
+ await fireSessionStart(secondPi, makeChildCtx(cwd, "switch-session-2"));
596
+
597
+ // The previous session's approval must not be visible: `demo` is back to
598
+ // its configured `ask`, not the carried-over `allow`.
599
+ expect(getPermissionsService()!.checkPermission("demo").state).toBe("ask");
600
+
601
+ rmSync(cwd, { recursive: true, force: true });
602
+ });
603
+ });