@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,370 +0,0 @@
1
- import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
2
- import { tmpdir } from "node:os";
3
- import { join } from "node:path";
4
- import { afterEach, describe, expect, test, vi } from "vitest";
5
- import { DEFAULT_EXTENSION_CONFIG } from "#src/extension-config";
6
- import {
7
- type ForwarderContext,
8
- PermissionForwarder,
9
- type PermissionForwarderDeps,
10
- } from "#src/forwarded-permissions/permission-forwarder";
11
- import { createPermissionForwardingLocation } from "#src/permission-forwarding";
12
-
13
- // ── Helpers ───────────────────────────────────────────────────────────────
14
-
15
- function makeDeps(
16
- overrides: Partial<PermissionForwarderDeps> = {},
17
- ): PermissionForwarderDeps {
18
- return {
19
- forwardingDir: "/tmp/forwarding",
20
- subagentSessionsDir: "/tmp/subagents",
21
- platform: "linux",
22
- logger: { review: vi.fn(), debug: vi.fn() },
23
- requestPermissionDecisionFromUi: vi
24
- .fn()
25
- .mockResolvedValue({ approved: true, state: "approved" as const }),
26
- config: { current: () => ({ ...DEFAULT_EXTENSION_CONFIG }) },
27
- ...overrides,
28
- };
29
- }
30
-
31
- function makeCtx(
32
- overrides: {
33
- hasUI?: boolean;
34
- ui?: ForwarderContext["ui"];
35
- sessionManager?: Partial<ForwarderContext["sessionManager"]>;
36
- } = {},
37
- ): ForwarderContext {
38
- return {
39
- hasUI: overrides.hasUI ?? false,
40
- ui: overrides.ui ?? { select: vi.fn(), input: vi.fn() },
41
- sessionManager: {
42
- getSessionId: vi.fn(() => ""),
43
- getSessionDir: vi.fn(() => ""),
44
- getEntries: vi.fn(() => []),
45
- ...overrides.sessionManager,
46
- },
47
- };
48
- }
49
-
50
- afterEach(() => {
51
- vi.unstubAllEnvs();
52
- });
53
-
54
- // ── requestApproval ───────────────────────────────────────────────────────
55
-
56
- describe("requestApproval — UI fast path", () => {
57
- test("calls requestPermissionDecisionFromUi but does not emit a UI prompt event (the prompter does)", async () => {
58
- const events = {
59
- emit: vi.fn(),
60
- on: vi.fn().mockReturnValue(() => undefined),
61
- };
62
- const requestPermissionDecisionFromUi = vi
63
- .fn()
64
- .mockResolvedValue({ approved: true, state: "approved" as const });
65
-
66
- const forwarder = new PermissionForwarder(
67
- makeDeps({ events, requestPermissionDecisionFromUi }),
68
- );
69
-
70
- await forwarder.requestApproval(
71
- makeCtx({ hasUI: true }),
72
- "Allow git push?",
73
- );
74
-
75
- expect(requestPermissionDecisionFromUi).toHaveBeenCalled();
76
- expect(events.emit).not.toHaveBeenCalledWith(
77
- "permissions:ui_prompt",
78
- expect.anything(),
79
- );
80
- });
81
- });
82
-
83
- describe("requestApproval — non-UI, non-subagent path", () => {
84
- test("returns denied without showing a dialog or emitting when there is no active UI", async () => {
85
- const events = {
86
- emit: vi.fn(),
87
- on: vi.fn().mockReturnValue(() => undefined),
88
- };
89
- const requestPermissionDecisionFromUi = vi.fn();
90
-
91
- const forwarder = new PermissionForwarder(
92
- makeDeps({ events, requestPermissionDecisionFromUi }),
93
- );
94
-
95
- const result = await forwarder.requestApproval(
96
- makeCtx({ hasUI: false }),
97
- "Allow git push?",
98
- );
99
-
100
- expect(result).toEqual({ approved: false, state: "denied" });
101
- expect(events.emit).not.toHaveBeenCalledWith(
102
- "permissions:ui_prompt",
103
- expect.anything(),
104
- );
105
- expect(requestPermissionDecisionFromUi).not.toHaveBeenCalled();
106
- });
107
- });
108
-
109
- // ── processInbox ──────────────────────────────────────────────────────────
110
-
111
- describe("processInbox", () => {
112
- test("emits a UI prompt event before showing a forwarded permission dialog", async () => {
113
- const root = mkdtempSync(join(tmpdir(), "permission-forwarding-"));
114
- try {
115
- const forwardingDir = join(root, "forwarding");
116
- const location = createPermissionForwardingLocation(
117
- forwardingDir,
118
- "parent-session",
119
- );
120
- mkdirSync(location.requestsDir, { recursive: true });
121
- mkdirSync(location.responsesDir, { recursive: true });
122
- writeFileSync(
123
- join(location.requestsDir, "req-forwarded.json"),
124
- JSON.stringify({
125
- id: "req-forwarded",
126
- createdAt: Date.now(),
127
- requesterSessionId: "child-session",
128
- targetSessionId: "parent-session",
129
- requesterAgentName: "Explore",
130
- message: "Allow git push?",
131
- }),
132
- "utf-8",
133
- );
134
-
135
- const events = {
136
- emit: vi.fn(),
137
- on: vi.fn().mockReturnValue(() => undefined),
138
- };
139
- const requestPermissionDecisionFromUi = vi
140
- .fn()
141
- .mockResolvedValue({ approved: true, state: "approved" as const });
142
-
143
- const forwarder = new PermissionForwarder(
144
- makeDeps({
145
- forwardingDir,
146
- events,
147
- requestPermissionDecisionFromUi,
148
- }),
149
- );
150
-
151
- await forwarder.processInbox(
152
- makeCtx({
153
- hasUI: true,
154
- sessionManager: {
155
- getSessionId: vi.fn(() => "parent-session"),
156
- },
157
- }),
158
- );
159
-
160
- expect(events.emit).toHaveBeenCalledWith(
161
- "permissions:ui_prompt",
162
- expect.objectContaining({
163
- requestId: "req-forwarded",
164
- source: "tool_call",
165
- surface: null,
166
- value: null,
167
- agentName: "Explore",
168
- message: expect.stringContaining("Allow git push?"),
169
- forwarding: {
170
- requesterAgentName: "Explore",
171
- requesterSessionId: "child-session",
172
- },
173
- }),
174
- );
175
- expect(requestPermissionDecisionFromUi).toHaveBeenCalled();
176
- } finally {
177
- rmSync(root, { recursive: true, force: true });
178
- }
179
- });
180
-
181
- test("emits a non-degraded UI prompt event when the request carries display fields", async () => {
182
- const root = mkdtempSync(join(tmpdir(), "permission-forwarding-"));
183
- try {
184
- const forwardingDir = join(root, "forwarding");
185
- const location = createPermissionForwardingLocation(
186
- forwardingDir,
187
- "parent-session",
188
- );
189
- mkdirSync(location.requestsDir, { recursive: true });
190
- mkdirSync(location.responsesDir, { recursive: true });
191
- writeFileSync(
192
- join(location.requestsDir, "req-forwarded-rich.json"),
193
- JSON.stringify({
194
- id: "req-forwarded-rich",
195
- createdAt: Date.now(),
196
- requesterSessionId: "child-session",
197
- targetSessionId: "parent-session",
198
- requesterAgentName: "Explore",
199
- message: "Allow git push?",
200
- source: "tool_call",
201
- surface: "bash",
202
- value: "git push",
203
- }),
204
- "utf-8",
205
- );
206
-
207
- const events = {
208
- emit: vi.fn(),
209
- on: vi.fn().mockReturnValue(() => undefined),
210
- };
211
- const requestPermissionDecisionFromUi = vi
212
- .fn()
213
- .mockResolvedValue({ approved: true, state: "approved" as const });
214
-
215
- const forwarder = new PermissionForwarder(
216
- makeDeps({
217
- forwardingDir,
218
- events,
219
- requestPermissionDecisionFromUi,
220
- }),
221
- );
222
-
223
- await forwarder.processInbox(
224
- makeCtx({
225
- hasUI: true,
226
- sessionManager: {
227
- getSessionId: vi.fn(() => "parent-session"),
228
- },
229
- }),
230
- );
231
-
232
- expect(events.emit).toHaveBeenCalledWith(
233
- "permissions:ui_prompt",
234
- expect.objectContaining({
235
- requestId: "req-forwarded-rich",
236
- source: "tool_call",
237
- surface: "bash",
238
- value: "git push",
239
- agentName: "Explore",
240
- message: expect.stringContaining("Allow git push?"),
241
- forwarding: {
242
- requesterAgentName: "Explore",
243
- requesterSessionId: "child-session",
244
- },
245
- }),
246
- );
247
- expect(requestPermissionDecisionFromUi).toHaveBeenCalled();
248
- } finally {
249
- rmSync(root, { recursive: true, force: true });
250
- }
251
- });
252
-
253
- test("does not emit a UI prompt event when forwarded permission auto-approves", async () => {
254
- const root = mkdtempSync(join(tmpdir(), "permission-forwarding-"));
255
- try {
256
- const forwardingDir = join(root, "forwarding");
257
- const location = createPermissionForwardingLocation(
258
- forwardingDir,
259
- "parent-session",
260
- );
261
- mkdirSync(location.requestsDir, { recursive: true });
262
- mkdirSync(location.responsesDir, { recursive: true });
263
- writeFileSync(
264
- join(location.requestsDir, "req-forwarded-auto.json"),
265
- JSON.stringify({
266
- id: "req-forwarded-auto",
267
- createdAt: Date.now(),
268
- requesterSessionId: "child-session",
269
- targetSessionId: "parent-session",
270
- requesterAgentName: "Explore",
271
- message: "Allow git push?",
272
- }),
273
- "utf-8",
274
- );
275
-
276
- const events = {
277
- emit: vi.fn(),
278
- on: vi.fn().mockReturnValue(() => undefined),
279
- };
280
- const requestPermissionDecisionFromUi = vi.fn();
281
-
282
- const forwarder = new PermissionForwarder(
283
- makeDeps({
284
- forwardingDir,
285
- events,
286
- requestPermissionDecisionFromUi,
287
- config: {
288
- current: () => ({ ...DEFAULT_EXTENSION_CONFIG, yoloMode: true }),
289
- },
290
- }),
291
- );
292
-
293
- await forwarder.processInbox(
294
- makeCtx({
295
- hasUI: true,
296
- sessionManager: {
297
- getSessionId: vi.fn(() => "parent-session"),
298
- },
299
- }),
300
- );
301
-
302
- expect(events.emit).not.toHaveBeenCalledWith(
303
- "permissions:ui_prompt",
304
- expect.anything(),
305
- );
306
- expect(requestPermissionDecisionFromUi).not.toHaveBeenCalled();
307
- } finally {
308
- rmSync(root, { recursive: true, force: true });
309
- }
310
- });
311
-
312
- test("recreates a missing responses/ directory and still writes the response", async () => {
313
- const root = mkdtempSync(join(tmpdir(), "permission-forwarding-"));
314
- try {
315
- const forwardingDir = join(root, "forwarding");
316
- const location = createPermissionForwardingLocation(
317
- forwardingDir,
318
- "parent-session",
319
- );
320
- // Simulate the race: requests/ exists with a pending file, but
321
- // responses/ was removed by a concurrent cleanup pass.
322
- mkdirSync(location.requestsDir, { recursive: true });
323
- // Deliberately do NOT create location.responsesDir.
324
- writeFileSync(
325
- join(location.requestsDir, "req-race.json"),
326
- JSON.stringify({
327
- id: "req-race",
328
- createdAt: Date.now(),
329
- requesterSessionId: "child-session",
330
- targetSessionId: "parent-session",
331
- requesterAgentName: "Explore",
332
- message: "Allow read?",
333
- }),
334
- "utf-8",
335
- );
336
-
337
- const logger = { review: vi.fn(), debug: vi.fn() };
338
- const requestPermissionDecisionFromUi = vi
339
- .fn()
340
- .mockResolvedValue({ approved: true, state: "approved" as const });
341
-
342
- const forwarder = new PermissionForwarder(
343
- makeDeps({
344
- forwardingDir,
345
- logger,
346
- requestPermissionDecisionFromUi,
347
- }),
348
- );
349
-
350
- await forwarder.processInbox(
351
- makeCtx({
352
- hasUI: true,
353
- sessionManager: {
354
- getSessionId: vi.fn(() => "parent-session"),
355
- },
356
- }),
357
- );
358
-
359
- // processInbox must have recreated responses/ and written a response
360
- // file — no permission_forwarding.error should have been logged.
361
- expect(logger.review).not.toHaveBeenCalledWith(
362
- "permission_forwarding.error",
363
- expect.anything(),
364
- );
365
- expect(requestPermissionDecisionFromUi).toHaveBeenCalled();
366
- } finally {
367
- rmSync(root, { recursive: true, force: true });
368
- }
369
- });
370
- });
@@ -1,315 +0,0 @@
1
- import { tmpdir } from "node:os";
2
- import { join } from "node:path";
3
- import { afterEach, describe, expect, test, vi } from "vitest";
4
- import {
5
- createPermissionForwardingLocation,
6
- isForwardedPermissionRequestForSession,
7
- resolvePermissionForwardingTargetSessionId,
8
- SUBAGENT_PARENT_SESSION_ENV_CANDIDATES,
9
- SUBAGENT_PARENT_SESSION_ENV_KEY,
10
- } from "#src/permission-forwarding";
11
- import { SubagentSessionRegistry } from "#src/subagent-registry";
12
-
13
- afterEach(() => {
14
- vi.unstubAllEnvs();
15
- });
16
-
17
- describe("SUBAGENT_PARENT_SESSION_ENV_CANDIDATES", () => {
18
- test("is an array containing PI_AGENT_ROUTER_PARENT_SESSION_ID", () => {
19
- expect(Array.isArray(SUBAGENT_PARENT_SESSION_ENV_CANDIDATES)).toBe(true);
20
- expect(SUBAGENT_PARENT_SESSION_ENV_CANDIDATES).toContain(
21
- "PI_AGENT_ROUTER_PARENT_SESSION_ID",
22
- );
23
- });
24
-
25
- test("contains PI_SUBAGENT_PARENT_SESSION for CLI-based subagent extensions", () => {
26
- expect(SUBAGENT_PARENT_SESSION_ENV_CANDIDATES).toContain(
27
- "PI_SUBAGENT_PARENT_SESSION",
28
- );
29
- });
30
-
31
- test("deprecated SUBAGENT_PARENT_SESSION_ENV_KEY equals the first candidate", () => {
32
- // eslint-disable-next-line @typescript-eslint/no-deprecated -- test verifying the deprecated alias
33
- expect(SUBAGENT_PARENT_SESSION_ENV_KEY).toBe(
34
- SUBAGENT_PARENT_SESSION_ENV_CANDIDATES[0],
35
- );
36
- });
37
- });
38
-
39
- describe("resolvePermissionForwardingTargetSessionId", () => {
40
- test("hasUI=true returns the current session ID (UI host owns forwarding)", () => {
41
- expect(
42
- resolvePermissionForwardingTargetSessionId({
43
- hasUI: true,
44
- isSubagent: false,
45
- currentSessionId: "parent-session-abc",
46
- env: {},
47
- }),
48
- ).toBe("parent-session-abc");
49
- });
50
-
51
- test("hasUI=true with isSubagent=true still returns current session ID", () => {
52
- expect(
53
- resolvePermissionForwardingTargetSessionId({
54
- hasUI: true,
55
- isSubagent: true,
56
- currentSessionId: "session-xyz",
57
- env: { PI_AGENT_ROUTER_PARENT_SESSION_ID: "other" },
58
- }),
59
- ).toBe("session-xyz");
60
- });
61
-
62
- test("hasUI=false, isSubagent=false returns null", () => {
63
- expect(
64
- resolvePermissionForwardingTargetSessionId({
65
- hasUI: false,
66
- isSubagent: false,
67
- currentSessionId: "session-xyz",
68
- env: { PI_AGENT_ROUTER_PARENT_SESSION_ID: "parent-session-abc" },
69
- }),
70
- ).toBeNull();
71
- });
72
-
73
- test("isSubagent=true, no candidates set returns null", () => {
74
- expect(
75
- resolvePermissionForwardingTargetSessionId({
76
- hasUI: false,
77
- isSubagent: true,
78
- currentSessionId: "session-xyz",
79
- env: {},
80
- }),
81
- ).toBeNull();
82
- });
83
-
84
- test("isSubagent=true, PI_AGENT_ROUTER_PARENT_SESSION_ID set returns its value", () => {
85
- expect(
86
- resolvePermissionForwardingTargetSessionId({
87
- hasUI: false,
88
- isSubagent: true,
89
- currentSessionId: "session-xyz",
90
- env: { PI_AGENT_ROUTER_PARENT_SESSION_ID: "parent-session-abc" },
91
- }),
92
- ).toBe("parent-session-abc");
93
- });
94
-
95
- test("isSubagent=true, PI_SUBAGENT_PARENT_SESSION resolves when PI_AGENT_ROUTER_PARENT_SESSION_ID is absent", () => {
96
- expect(
97
- resolvePermissionForwardingTargetSessionId({
98
- hasUI: false,
99
- isSubagent: true,
100
- currentSessionId: "session-xyz",
101
- env: {
102
- PI_SUBAGENT_PARENT_SESSION: "parent-from-convention",
103
- },
104
- }),
105
- ).toBe("parent-from-convention");
106
- });
107
-
108
- test("isSubagent=true, PI_AGENT_ROUTER_PARENT_SESSION_ID takes precedence over PI_SUBAGENT_PARENT_SESSION", () => {
109
- expect(
110
- resolvePermissionForwardingTargetSessionId({
111
- hasUI: false,
112
- isSubagent: true,
113
- currentSessionId: "session-xyz",
114
- env: {
115
- PI_AGENT_ROUTER_PARENT_SESSION_ID: "parent-from-router",
116
- PI_SUBAGENT_PARENT_SESSION: "parent-from-convention",
117
- },
118
- }),
119
- ).toBe("parent-from-router");
120
- });
121
-
122
- test("isSubagent=true, candidate value is empty string returns null", () => {
123
- expect(
124
- resolvePermissionForwardingTargetSessionId({
125
- hasUI: false,
126
- isSubagent: true,
127
- currentSessionId: "session-xyz",
128
- env: { PI_AGENT_ROUTER_PARENT_SESSION_ID: "" },
129
- }),
130
- ).toBeNull();
131
- });
132
-
133
- test("isSubagent=true, candidate value is 'unknown' returns null", () => {
134
- expect(
135
- resolvePermissionForwardingTargetSessionId({
136
- hasUI: false,
137
- isSubagent: true,
138
- currentSessionId: "session-xyz",
139
- env: { PI_AGENT_ROUTER_PARENT_SESSION_ID: "unknown" },
140
- }),
141
- ).toBeNull();
142
- });
143
-
144
- test("env defaults to process.env when omitted", () => {
145
- vi.stubEnv("PI_AGENT_ROUTER_PARENT_SESSION_ID", "env-session-abc");
146
- expect(
147
- resolvePermissionForwardingTargetSessionId({
148
- hasUI: false,
149
- isSubagent: true,
150
- }),
151
- ).toBe("env-session-abc");
152
- });
153
- });
154
-
155
- describe("resolvePermissionForwardingTargetSessionId — registry resolution", () => {
156
- const childSessionId = "child-session-abc";
157
-
158
- test("returns parentSessionId from registry when env vars are absent", () => {
159
- const registry = new SubagentSessionRegistry();
160
- registry.register(childSessionId, {
161
- parentSessionId: "parent-from-registry",
162
- });
163
-
164
- expect(
165
- resolvePermissionForwardingTargetSessionId({
166
- hasUI: false,
167
- isSubagent: true,
168
- sessionId: childSessionId,
169
- registry,
170
- env: {},
171
- }),
172
- ).toBe("parent-from-registry");
173
- });
174
-
175
- test("registry takes priority over env vars", () => {
176
- const registry = new SubagentSessionRegistry();
177
- registry.register(childSessionId, {
178
- parentSessionId: "parent-from-registry",
179
- });
180
-
181
- expect(
182
- resolvePermissionForwardingTargetSessionId({
183
- hasUI: false,
184
- isSubagent: true,
185
- sessionId: childSessionId,
186
- registry,
187
- env: { PI_AGENT_ROUTER_PARENT_SESSION_ID: "parent-from-env" },
188
- }),
189
- ).toBe("parent-from-registry");
190
- });
191
-
192
- test("falls through to env vars when registry entry has no parentSessionId", () => {
193
- const registry = new SubagentSessionRegistry();
194
- registry.register(childSessionId, {}); // no parentSessionId
195
-
196
- expect(
197
- resolvePermissionForwardingTargetSessionId({
198
- hasUI: false,
199
- isSubagent: true,
200
- sessionId: childSessionId,
201
- registry,
202
- env: { PI_AGENT_ROUTER_PARENT_SESSION_ID: "parent-from-env" },
203
- }),
204
- ).toBe("parent-from-env");
205
- });
206
-
207
- test("falls through to env vars when sessionId is not in registry", () => {
208
- const registry = new SubagentSessionRegistry(); // empty
209
-
210
- expect(
211
- resolvePermissionForwardingTargetSessionId({
212
- hasUI: false,
213
- isSubagent: true,
214
- sessionId: childSessionId,
215
- registry,
216
- env: { PI_AGENT_ROUTER_PARENT_SESSION_ID: "parent-from-env" },
217
- }),
218
- ).toBe("parent-from-env");
219
- });
220
-
221
- test("returns null when registry entry has no parentSessionId and no env vars set", () => {
222
- const registry = new SubagentSessionRegistry();
223
- registry.register(childSessionId, {}); // no parentSessionId
224
-
225
- expect(
226
- resolvePermissionForwardingTargetSessionId({
227
- hasUI: false,
228
- isSubagent: true,
229
- sessionId: childSessionId,
230
- registry,
231
- env: {},
232
- }),
233
- ).toBeNull();
234
- });
235
-
236
- test("omitting registry preserves existing behaviour", () => {
237
- expect(
238
- resolvePermissionForwardingTargetSessionId({
239
- hasUI: false,
240
- isSubagent: true,
241
- sessionId: childSessionId,
242
- env: { PI_AGENT_ROUTER_PARENT_SESSION_ID: "parent-from-env" },
243
- }),
244
- ).toBe("parent-from-env");
245
- });
246
- });
247
-
248
- // ---------------------------------------------------------------------------
249
- // Moved from permission-system.test.ts catch-all (#342)
250
- // ---------------------------------------------------------------------------
251
-
252
- test("Permission forwarding resolves the parent interactive session from subagent runtime env", () => {
253
- const targetSessionId = resolvePermissionForwardingTargetSessionId({
254
- hasUI: false,
255
- isSubagent: true,
256
- currentSessionId: "child-session",
257
- env: {
258
- PI_AGENT_ROUTER_PARENT_SESSION_ID: "parent-session",
259
- },
260
- });
261
-
262
- expect(targetSessionId).toBe("parent-session");
263
- });
264
-
265
- test("Permission forwarding does not guess a target session when subagent runtime env is missing", () => {
266
- const targetSessionId = resolvePermissionForwardingTargetSessionId({
267
- hasUI: false,
268
- isSubagent: true,
269
- currentSessionId: "child-session",
270
- env: {},
271
- });
272
-
273
- expect(targetSessionId).toBe(null);
274
- });
275
-
276
- test("Permission forwarding uses session-scoped directories per interactive session", () => {
277
- const forwardingRoot = join(tmpdir(), "pi-permission-system-forwarding-root");
278
- const sessionA = createPermissionForwardingLocation(
279
- forwardingRoot,
280
- "session-a",
281
- );
282
- const sessionB = createPermissionForwardingLocation(
283
- forwardingRoot,
284
- "session-b",
285
- );
286
-
287
- expect(sessionA.sessionRootDir).not.toBe(sessionB.sessionRootDir);
288
- expect(sessionA.requestsDir).not.toBe(sessionB.requestsDir);
289
- expect(sessionA.responsesDir).not.toBe(sessionB.responsesDir);
290
- });
291
-
292
- test("Permission forwarding request routing only matches the intended UI session", () => {
293
- expect(
294
- isForwardedPermissionRequestForSession(
295
- { targetSessionId: "session-a" },
296
- "session-a",
297
- ),
298
- ).toBe(true);
299
- expect(
300
- isForwardedPermissionRequestForSession(
301
- { targetSessionId: "session-a" },
302
- "session-b",
303
- ),
304
- ).toBe(false);
305
- });
306
-
307
- test("Permission forwarding rejects unresolved sentinel session ids", () => {
308
- const targetSessionId = resolvePermissionForwardingTargetSessionId({
309
- hasUI: true,
310
- isSubagent: false,
311
- currentSessionId: "unknown",
312
- });
313
-
314
- expect(targetSessionId).toBe(null);
315
- });