|
| 1 | +import { expect, test } from "bun:test"; |
| 2 | +import { createPermissionGate } from "./gate.js"; |
| 3 | +import { createReactorAuthorize, createWorkerAuthorize } from "./reactor-authorize.js"; |
| 4 | +import { runWithSubAgentIdentity, getSubAgentIdentity } from "../subagent/identity-context.js"; |
| 5 | +import { gateToolCall } from "../plugins/permission-plugin.js"; |
| 6 | +import type { ToolCall } from "@intx/types/runtime"; |
| 7 | + |
| 8 | +const call: ToolCall = { |
| 9 | + id: "write-1", |
| 10 | + name: "write_file", |
| 11 | + arguments: { path: "probe.txt", content: "data" }, |
| 12 | +}; |
| 13 | +const gate = () => |
| 14 | + createPermissionGate({ |
| 15 | + cwd: process.cwd(), |
| 16 | + approvals: [], |
| 17 | + interactive: true, |
| 18 | + auto: false, |
| 19 | + skipPermissions: false, |
| 20 | + reactorGated: false, |
| 21 | + requestApproval: async () => { |
| 22 | + throw new Error("worker must never ask"); |
| 23 | + }, |
| 24 | + }); |
| 25 | + |
| 26 | +test("worker maps unresolved ask to deny while main reactor suspends", async () => { |
| 27 | + const policy = gate(); |
| 28 | + expect((await createReactorAuthorize(policy)("tool:write_file", "invoke", call)).effect).toBe( |
| 29 | + "ask", |
| 30 | + ); |
| 31 | + expect((await createWorkerAuthorize(policy)("tool:write_file", "invoke", call)).effect).toBe( |
| 32 | + "deny", |
| 33 | + ); |
| 34 | + policy.setAuto(true); |
| 35 | + expect((await createWorkerAuthorize(policy)("tool:write_file", "invoke", call)).effect).toBe( |
| 36 | + "allow", |
| 37 | + ); |
| 38 | + policy.setAuto(false); |
| 39 | + expect((await createWorkerAuthorize(policy)("tool:write_file", "invoke", call)).effect).toBe( |
| 40 | + "deny", |
| 41 | + ); |
| 42 | +}); |
| 43 | + |
| 44 | +test("worker bridge rejects malformed context, resource, and action", async () => { |
| 45 | + const authorize = createWorkerAuthorize(gate()); |
| 46 | + await expect(authorize("tool:write_file", "invoke", {})).rejects.toThrow("ToolCall"); |
| 47 | + await expect(authorize("tool:read_file", "invoke", call)).rejects.toThrow("does not match"); |
| 48 | + await expect(authorize("tool:write_file", "read", call)).rejects.toThrow("unexpected action"); |
| 49 | +}); |
| 50 | + |
| 51 | +test("worker reactor is sole owner even if parent middleware mode changes policy before runner", async () => { |
| 52 | + const policy = gate(); |
| 53 | + policy.setAuto(true); |
| 54 | + const identity = { description: "worker", cwd: process.cwd(), reactorOwnsPermissions: true }; |
| 55 | + const authorize = createWorkerAuthorize(policy); |
| 56 | + expect( |
| 57 | + (await runWithSubAgentIdentity(identity, () => authorize("tool:write_file", "invoke", call))) |
| 58 | + .effect, |
| 59 | + ).toBe("allow"); |
| 60 | + policy.setAuto(false); |
| 61 | + const result = await runWithSubAgentIdentity(identity, () => |
| 62 | + gateToolCall(policy, call, new AbortController().signal, async () => ({ |
| 63 | + callId: call.id, |
| 64 | + content: "executed", |
| 65 | + isError: false, |
| 66 | + })), |
| 67 | + ); |
| 68 | + expect(result.isError).toBe(false); |
| 69 | + expect(policy.isReactorGated()).toBe(false); |
| 70 | + expect( |
| 71 | + (await runWithSubAgentIdentity(identity, () => authorize("tool:write_file", "invoke", call))) |
| 72 | + .effect, |
| 73 | + ).toBe("deny"); |
| 74 | + expect(getSubAgentIdentity()).toBeUndefined(); |
| 75 | +}); |
| 76 | + |
| 77 | +test("concurrent authorization preserves each worker cwd across awaited policy evaluation", async () => { |
| 78 | + const policy = gate(); |
| 79 | + const seen: string[] = []; |
| 80 | + const realAuthorize = policy.authorizeCall; |
| 81 | + policy.authorizeCall = async (toolCall) => { |
| 82 | + await new Promise((resolve) => setTimeout(resolve, 2)); |
| 83 | + const identity = getSubAgentIdentity(); |
| 84 | + if (identity === undefined) throw new Error("missing worker identity"); |
| 85 | + seen.push(identity.cwd); |
| 86 | + return realAuthorize(toolCall); |
| 87 | + }; |
| 88 | + const authorize = createWorkerAuthorize(policy); |
| 89 | + await Promise.all( |
| 90 | + ["/worker-a", "/worker-b"].map((cwd) => |
| 91 | + runWithSubAgentIdentity({ description: cwd, cwd, reactorOwnsPermissions: true }, () => |
| 92 | + authorize("tool:write_file", "invoke", call), |
| 93 | + ), |
| 94 | + ), |
| 95 | + ); |
| 96 | + expect(seen.sort()).toEqual(["/worker-a", "/worker-b"]); |
| 97 | + expect(getSubAgentIdentity()).toBeUndefined(); |
| 98 | +}); |
0 commit comments