@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,560 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-deprecated -- tests the deprecated RPC channel implementation */
2
- import { createEventBus } from "@earendil-works/pi-coding-agent";
3
- import { beforeEach, describe, expect, it, vi } from "vitest";
4
- import { PathNormalizer } from "#src/path-normalizer";
5
- import {
6
- type PermissionRpcDeps,
7
- registerPermissionRpcHandlers,
8
- } from "#src/permission-event-rpc";
9
- import type {
10
- PermissionsCheckReplyData,
11
- PermissionsRpcReply,
12
- } from "#src/permission-events";
13
- import {
14
- PERMISSIONS_PROTOCOL_VERSION,
15
- PERMISSIONS_RPC_CHECK_CHANNEL,
16
- PERMISSIONS_RPC_PROMPT_CHANNEL,
17
- PERMISSIONS_UI_PROMPT_CHANNEL,
18
- } from "#src/permission-events";
19
-
20
- // Mock node:fs so realpathSync (the canonical alias) is controllable.
21
- const realpathSync = vi.hoisted(() =>
22
- vi.fn<(path: string) => string>((p) => p),
23
- );
24
- vi.mock("node:fs", () => ({
25
- realpathSync,
26
- default: { realpathSync },
27
- }));
28
-
29
- // ── Helpers ────────────────────────────────────────────────────────────────
30
-
31
- function makeCheckResult(
32
- state: "allow" | "deny" | "ask",
33
- overrides: Record<string, unknown> = {},
34
- ) {
35
- return {
36
- toolName: "bash",
37
- state,
38
- matchedPattern: "*",
39
- source: "bash" as const,
40
- origin: "global" as const,
41
- ...overrides,
42
- };
43
- }
44
-
45
- function makeDeps(
46
- overrides: Partial<PermissionRpcDeps> = {},
47
- ): PermissionRpcDeps {
48
- return {
49
- resolver: {
50
- resolve: vi.fn().mockReturnValue(makeCheckResult("allow")),
51
- },
52
- session: {
53
- getRuntimeContext: vi.fn().mockReturnValue(null),
54
- getPathNormalizer: vi
55
- .fn()
56
- .mockReturnValue(new PathNormalizer("linux", "/test/project")),
57
- },
58
- requestPermissionDecisionFromUi: vi.fn(),
59
- logger: { review: vi.fn() },
60
- ...overrides,
61
- };
62
- }
63
-
64
- /** Build the narrow RPC session view from a runtime context. */
65
- function makeSession(ctx: unknown): PermissionRpcDeps["session"] {
66
- return {
67
- getRuntimeContext: vi.fn().mockReturnValue(ctx),
68
- getPathNormalizer: vi
69
- .fn()
70
- .mockReturnValue(new PathNormalizer("linux", "/test/project")),
71
- };
72
- }
73
-
74
- /** Wait for a single event on the bus reply channel. */
75
- function waitForReply<T>(
76
- bus: ReturnType<typeof createEventBus>,
77
- channel: string,
78
- ): Promise<T> {
79
- return new Promise((resolve) => {
80
- const unsub = bus.on(channel, (data) => {
81
- unsub();
82
- resolve(data as T);
83
- });
84
- });
85
- }
86
-
87
- beforeEach(() => {
88
- realpathSync.mockReset();
89
- realpathSync.mockImplementation((p: string) => p);
90
- });
91
-
92
- // ── registerPermissionRpcHandlers — check RPC ──────────────────────────────
93
-
94
- describe("registerPermissionRpcHandlers — permissions:rpc:check", () => {
95
- it("returns unsubscribe handles", () => {
96
- const bus = createEventBus();
97
- const handles = registerPermissionRpcHandlers(bus, makeDeps());
98
- expect(typeof handles.unsubCheck).toBe("function");
99
- expect(typeof handles.unsubPrompt).toBe("function");
100
- });
101
-
102
- it("replies allow for an allowed surface/value", async () => {
103
- const bus = createEventBus();
104
- const deps = makeDeps({
105
- resolver: {
106
- resolve: vi.fn().mockReturnValue(makeCheckResult("allow")),
107
- },
108
- });
109
- registerPermissionRpcHandlers(bus, deps);
110
-
111
- const replyPromise = waitForReply<
112
- PermissionsRpcReply<PermissionsCheckReplyData>
113
- >(bus, `${PERMISSIONS_RPC_CHECK_CHANNEL}:reply:req-allow`);
114
- bus.emit(PERMISSIONS_RPC_CHECK_CHANNEL, {
115
- requestId: "req-allow",
116
- surface: "bash",
117
- value: "git status",
118
- });
119
-
120
- const reply = await replyPromise;
121
- expect(reply.success).toBe(true);
122
- expect(reply.protocolVersion).toBe(PERMISSIONS_PROTOCOL_VERSION);
123
- if (reply.success) {
124
- expect(reply.data?.result).toBe("allow");
125
- expect(reply.data?.origin).toBe("global");
126
- }
127
- });
128
-
129
- it("replies deny for a denied surface/value", async () => {
130
- const bus = createEventBus();
131
- const deps = makeDeps({
132
- resolver: {
133
- resolve: vi.fn().mockReturnValue(
134
- makeCheckResult("deny", {
135
- origin: "project",
136
- matchedPattern: "rm *",
137
- }),
138
- ),
139
- },
140
- });
141
- registerPermissionRpcHandlers(bus, deps);
142
-
143
- const replyPromise = waitForReply<
144
- PermissionsRpcReply<PermissionsCheckReplyData>
145
- >(bus, `${PERMISSIONS_RPC_CHECK_CHANNEL}:reply:req-deny`);
146
- bus.emit(PERMISSIONS_RPC_CHECK_CHANNEL, {
147
- requestId: "req-deny",
148
- surface: "bash",
149
- value: "rm -rf /tmp",
150
- });
151
-
152
- const reply = await replyPromise;
153
- expect(reply.success).toBe(true);
154
- if (reply.success) {
155
- expect(reply.data?.result).toBe("deny");
156
- expect(reply.data?.matchedPattern).toBe("rm *");
157
- }
158
- });
159
-
160
- it("replies ask for an ask surface/value", async () => {
161
- const bus = createEventBus();
162
- const deps = makeDeps({
163
- resolver: {
164
- resolve: vi
165
- .fn()
166
- .mockReturnValue(
167
- makeCheckResult("ask", { matchedPattern: undefined }),
168
- ),
169
- },
170
- });
171
- registerPermissionRpcHandlers(bus, deps);
172
-
173
- const replyPromise = waitForReply<
174
- PermissionsRpcReply<PermissionsCheckReplyData>
175
- >(bus, `${PERMISSIONS_RPC_CHECK_CHANNEL}:reply:req-ask`);
176
- bus.emit(PERMISSIONS_RPC_CHECK_CHANNEL, {
177
- requestId: "req-ask",
178
- surface: "mcp",
179
- value: "exa:search",
180
- });
181
-
182
- const reply = await replyPromise;
183
- expect(reply.success).toBe(true);
184
- if (reply.success) {
185
- expect(reply.data?.result).toBe("ask");
186
- }
187
- });
188
-
189
- it("passes agentName through to the resolver when provided", async () => {
190
- const resolve = vi.fn().mockReturnValue(makeCheckResult("allow"));
191
- const bus = createEventBus();
192
- const deps = makeDeps({
193
- resolver: { resolve },
194
- });
195
- registerPermissionRpcHandlers(bus, deps);
196
-
197
- const replyPromise = waitForReply(
198
- bus,
199
- `${PERMISSIONS_RPC_CHECK_CHANNEL}:reply:req-agent`,
200
- );
201
- bus.emit(PERMISSIONS_RPC_CHECK_CHANNEL, {
202
- requestId: "req-agent",
203
- surface: "bash",
204
- value: "git push",
205
- agentName: "Worker",
206
- });
207
- await replyPromise;
208
-
209
- expect(resolve).toHaveBeenCalledWith({
210
- kind: "tool",
211
- surface: "bash",
212
- input: { command: "git push" },
213
- agentName: "Worker",
214
- });
215
- });
216
-
217
- it("resolves a path-surface query through an access-path intent matching the canonical alias", async () => {
218
- realpathSync.mockImplementation((p: string) =>
219
- p === "/test/project/link" ? "/test/project/real" : p,
220
- );
221
- const resolve = vi.fn().mockReturnValue(makeCheckResult("deny"));
222
- const bus = createEventBus();
223
- registerPermissionRpcHandlers(bus, makeDeps({ resolver: { resolve } }));
224
-
225
- const replyPromise = waitForReply(
226
- bus,
227
- `${PERMISSIONS_RPC_CHECK_CHANNEL}:reply:req-path`,
228
- );
229
- bus.emit(PERMISSIONS_RPC_CHECK_CHANNEL, {
230
- requestId: "req-path",
231
- surface: "external_directory",
232
- value: "link",
233
- });
234
- await replyPromise;
235
-
236
- expect(resolve).toHaveBeenCalledTimes(1);
237
- const intent = resolve.mock.calls[0][0] as {
238
- kind: string;
239
- surface: string;
240
- path: { matchValues(): string[] };
241
- };
242
- expect(intent.kind).toBe("access-path");
243
- expect(intent.surface).toBe("external_directory");
244
- expect(intent.path.matchValues()).toContain("/test/project/real");
245
- });
246
-
247
- it("replies with error envelope when requestId is missing", async () => {
248
- const bus = createEventBus();
249
- registerPermissionRpcHandlers(bus, makeDeps());
250
-
251
- // No reply channel to wait on — emit without requestId and confirm
252
- // no throw / crash. We check indirectly via a timeout-free approach:
253
- // emit an immediately-followable good request and ensure both succeed.
254
- const replyPromise = waitForReply<PermissionsRpcReply>(
255
- bus,
256
- `${PERMISSIONS_RPC_CHECK_CHANNEL}:reply:req-good`,
257
- );
258
- bus.emit(PERMISSIONS_RPC_CHECK_CHANNEL, {}); // missing requestId — should not crash
259
- bus.emit(PERMISSIONS_RPC_CHECK_CHANNEL, {
260
- requestId: "req-good",
261
- surface: "bash",
262
- });
263
-
264
- const reply = await replyPromise;
265
- expect(reply.success).toBe(true); // good request still handled
266
- });
267
-
268
- it("unsubCheck stops the handler from firing", async () => {
269
- const resolve = vi.fn().mockReturnValue(makeCheckResult("allow"));
270
- const bus = createEventBus();
271
- const deps = makeDeps({
272
- resolver: { resolve },
273
- });
274
- const handles = registerPermissionRpcHandlers(bus, deps);
275
- handles.unsubCheck();
276
-
277
- bus.emit(PERMISSIONS_RPC_CHECK_CHANNEL, {
278
- requestId: "req-unsub",
279
- surface: "bash",
280
- });
281
-
282
- // Give async handlers a chance to fire
283
- await new Promise((r) => setTimeout(r, 10));
284
- expect(resolve).not.toHaveBeenCalled();
285
- });
286
- });
287
-
288
- // ── registerPermissionRpcHandlers — prompt RPC ──────────────────────────
289
-
290
- describe("registerPermissionRpcHandlers — permissions:rpc:prompt", () => {
291
- function makeUi() {
292
- return {
293
- select: vi.fn(),
294
- input: vi.fn(),
295
- notify: vi.fn(),
296
- setStatus: vi.fn(),
297
- };
298
- }
299
-
300
- function makeCtxWithUi() {
301
- return {
302
- hasUI: true,
303
- ui: makeUi(),
304
- cwd: "/test/project",
305
- sessionManager: {
306
- getSessionDir: vi.fn().mockReturnValue("/sessions/test"),
307
- },
308
- };
309
- }
310
-
311
- it("replies with approval when user approves", async () => {
312
- const bus = createEventBus();
313
- const ctx = makeCtxWithUi();
314
- const approvedDecision = { approved: true, state: "approved" as const };
315
- const deps = makeDeps({
316
- session: makeSession(ctx),
317
- requestPermissionDecisionFromUi: vi
318
- .fn()
319
- .mockResolvedValue(approvedDecision),
320
- });
321
- registerPermissionRpcHandlers(bus, deps);
322
-
323
- const replyPromise = waitForReply<
324
- PermissionsRpcReply<
325
- import("../src/permission-events").PermissionsPromptReplyData
326
- >
327
- >(bus, `${PERMISSIONS_RPC_PROMPT_CHANNEL}:reply:req-prompt-1`);
328
- bus.emit(PERMISSIONS_RPC_PROMPT_CHANNEL, {
329
- requestId: "req-prompt-1",
330
- surface: "bash",
331
- value: "rm -rf /tmp",
332
- message: "Allow rm -rf /tmp?",
333
- });
334
-
335
- const reply = await replyPromise;
336
- expect(reply.success).toBe(true);
337
- expect(reply.protocolVersion).toBe(PERMISSIONS_PROTOCOL_VERSION);
338
- if (reply.success) {
339
- expect(reply.data?.approved).toBe(true);
340
- expect(reply.data?.state).toBe("approved");
341
- }
342
- });
343
-
344
- it("emits a UI prompt broadcast before awaiting the UI decision", async () => {
345
- const bus = createEventBus();
346
- const ctx = makeCtxWithUi();
347
- const requestUi = vi
348
- .fn()
349
- .mockResolvedValue({ approved: true, state: "approved" as const });
350
- const deps = makeDeps({
351
- session: makeSession(ctx),
352
- requestPermissionDecisionFromUi: requestUi,
353
- });
354
- registerPermissionRpcHandlers(bus, deps);
355
-
356
- const promptPromise = waitForReply(bus, PERMISSIONS_UI_PROMPT_CHANNEL);
357
- const replyPromise = waitForReply(
358
- bus,
359
- `${PERMISSIONS_RPC_PROMPT_CHANNEL}:reply:req-prompt-broadcast`,
360
- );
361
- bus.emit(PERMISSIONS_RPC_PROMPT_CHANNEL, {
362
- requestId: "req-prompt-broadcast",
363
- surface: "bash",
364
- value: "git push",
365
- message: "Allow git push?",
366
- agentName: "Worker",
367
- sessionLabel: "Allow git *",
368
- });
369
-
370
- await expect(promptPromise).resolves.toEqual({
371
- requestId: "req-prompt-broadcast",
372
- source: "rpc_prompt",
373
- surface: "bash",
374
- value: "git push",
375
- agentName: "Worker",
376
- message: "Allow git push?",
377
- forwarding: null,
378
- });
379
- await replyPromise;
380
- });
381
-
382
- it("passes the message to requestPermissionDecisionFromUi", async () => {
383
- const bus = createEventBus();
384
- const ctx = makeCtxWithUi();
385
- const requestUi = vi
386
- .fn()
387
- .mockResolvedValue({ approved: true, state: "approved" as const });
388
- const deps = makeDeps({
389
- session: makeSession(ctx),
390
- requestPermissionDecisionFromUi: requestUi,
391
- });
392
- registerPermissionRpcHandlers(bus, deps);
393
-
394
- const replyPromise = waitForReply(
395
- bus,
396
- `${PERMISSIONS_RPC_PROMPT_CHANNEL}:reply:req-prompt-2`,
397
- );
398
- bus.emit(PERMISSIONS_RPC_PROMPT_CHANNEL, {
399
- requestId: "req-prompt-2",
400
- surface: "bash",
401
- value: "git push",
402
- message: "Allow git push?",
403
- agentName: "Worker",
404
- sessionLabel: "Allow git *",
405
- });
406
- await replyPromise;
407
-
408
- expect(requestUi).toHaveBeenCalledWith(
409
- ctx.ui,
410
- expect.stringContaining("Worker"),
411
- "Allow git push?",
412
- { sessionLabel: "Allow git *" },
413
- );
414
- });
415
-
416
- it("replies with denied when user denies", async () => {
417
- const bus = createEventBus();
418
- const ctx = makeCtxWithUi();
419
- const deniedDecision = {
420
- approved: false,
421
- state: "denied_with_reason" as const,
422
- denialReason: "Too risky",
423
- };
424
- const deps = makeDeps({
425
- session: makeSession(ctx),
426
- requestPermissionDecisionFromUi: vi
427
- .fn()
428
- .mockResolvedValue(deniedDecision),
429
- });
430
- registerPermissionRpcHandlers(bus, deps);
431
-
432
- const replyPromise = waitForReply<
433
- PermissionsRpcReply<
434
- import("../src/permission-events").PermissionsPromptReplyData
435
- >
436
- >(bus, `${PERMISSIONS_RPC_PROMPT_CHANNEL}:reply:req-denied`);
437
- bus.emit(PERMISSIONS_RPC_PROMPT_CHANNEL, {
438
- requestId: "req-denied",
439
- surface: "bash",
440
- value: "rm -rf /",
441
- message: "Allow rm -rf /?",
442
- });
443
-
444
- const reply = await replyPromise;
445
- expect(reply.success).toBe(true);
446
- if (reply.success) {
447
- expect(reply.data?.approved).toBe(false);
448
- expect(reply.data?.state).toBe("denied_with_reason");
449
- expect(reply.data?.denialReason).toBe("Too risky");
450
- }
451
- });
452
-
453
- it("replies with no_ui error when context has no UI", async () => {
454
- const bus = createEventBus();
455
- const deps = makeDeps({
456
- session: makeSession(null),
457
- });
458
- registerPermissionRpcHandlers(bus, deps);
459
-
460
- const replyPromise = waitForReply<PermissionsRpcReply>(
461
- bus,
462
- `${PERMISSIONS_RPC_PROMPT_CHANNEL}:reply:req-no-ui`,
463
- );
464
- bus.emit(PERMISSIONS_RPC_PROMPT_CHANNEL, {
465
- requestId: "req-no-ui",
466
- surface: "bash",
467
- value: "git push",
468
- message: "Allow git push?",
469
- });
470
-
471
- const reply = await replyPromise;
472
- expect(reply.success).toBe(false);
473
- expect((reply as { success: false; error: string }).error).toBe("no_ui");
474
- });
475
-
476
- it("replies with no_ui error when context hasUI is false", async () => {
477
- const bus = createEventBus();
478
- const deps = makeDeps({
479
- session: makeSession({ hasUI: false, ui: makeUi() }),
480
- });
481
- registerPermissionRpcHandlers(bus, deps);
482
-
483
- const replyPromise = waitForReply<PermissionsRpcReply>(
484
- bus,
485
- `${PERMISSIONS_RPC_PROMPT_CHANNEL}:reply:req-headless`,
486
- );
487
- bus.emit(PERMISSIONS_RPC_PROMPT_CHANNEL, {
488
- requestId: "req-headless",
489
- surface: "bash",
490
- value: "git push",
491
- message: "Allow git push?",
492
- });
493
-
494
- const reply = await replyPromise;
495
- expect(reply.success).toBe(false);
496
- expect((reply as { success: false; error: string }).error).toBe("no_ui");
497
- });
498
-
499
- it("writes to the review log after a prompt decision", async () => {
500
- const bus = createEventBus();
501
- const ctx = makeCtxWithUi();
502
- const logger = { review: vi.fn() };
503
- const deps = makeDeps({
504
- session: makeSession(ctx),
505
- requestPermissionDecisionFromUi: vi
506
- .fn()
507
- .mockResolvedValue({ approved: true, state: "approved" as const }),
508
- logger,
509
- });
510
- registerPermissionRpcHandlers(bus, deps);
511
-
512
- const replyPromise = waitForReply(
513
- bus,
514
- `${PERMISSIONS_RPC_PROMPT_CHANNEL}:reply:req-log`,
515
- );
516
- bus.emit(PERMISSIONS_RPC_PROMPT_CHANNEL, {
517
- requestId: "req-log",
518
- surface: "bash",
519
- value: "git push",
520
- message: "Allow git push?",
521
- agentName: "Worker",
522
- });
523
- await replyPromise;
524
-
525
- expect(logger.review).toHaveBeenCalledWith(
526
- "permission_request.rpc_prompt",
527
- expect.objectContaining({
528
- requestId: "req-log",
529
- surface: "bash",
530
- value: "git push",
531
- agentName: "Worker",
532
- approved: true,
533
- }),
534
- );
535
- });
536
-
537
- it("unsubPrompt stops the handler from firing", async () => {
538
- const requestUi = vi
539
- .fn()
540
- .mockResolvedValue({ approved: true, state: "approved" as const });
541
- const bus = createEventBus();
542
- const ctx = makeCtxWithUi();
543
- const deps = makeDeps({
544
- session: makeSession(ctx),
545
- requestPermissionDecisionFromUi: requestUi,
546
- });
547
- const handles = registerPermissionRpcHandlers(bus, deps);
548
- handles.unsubPrompt();
549
-
550
- bus.emit(PERMISSIONS_RPC_PROMPT_CHANNEL, {
551
- requestId: "req-unsub-prompt",
552
- surface: "bash",
553
- value: "git push",
554
- message: "Allow?",
555
- });
556
-
557
- await new Promise((resolve) => setTimeout(resolve, 10));
558
- expect(requestUi).not.toHaveBeenCalled();
559
- });
560
- });