|
| 1 | +import { describe, expect, test } from "bun:test" |
| 2 | +import path from "path" |
| 3 | +import { tmpdir } from "../fixture/fixture" |
| 4 | + |
| 5 | +const entry = path.resolve(import.meta.dir, "../../src/index.ts") |
| 6 | +const command = process.env.AICTRL_TEST_BINARY |
| 7 | + ? [process.env.AICTRL_TEST_BINARY] |
| 8 | + : ["bun", "run", "--conditions=browser", entry] |
| 9 | + |
| 10 | +type Rule = { permission: string; pattern: string; action: string } |
| 11 | + |
| 12 | +type RunInput = { config: Record<string, unknown>; args?: string[] } |
| 13 | + |
| 14 | +/** Runs `aictrl run --format json` against a fixture provider that answers with text, and returns its NDJSON events. */ |
| 15 | +async function runEvents(input: RunInput) { |
| 16 | + await using tmp = await tmpdir({ git: true }) |
| 17 | + const server = Bun.serve({ |
| 18 | + hostname: "127.0.0.1", |
| 19 | + port: 0, |
| 20 | + idleTimeout: 0, |
| 21 | + fetch() { |
| 22 | + const chunks = [ |
| 23 | + { delta: { role: "assistant", content: "Fixture complete" }, finish_reason: null }, |
| 24 | + { delta: {}, finish_reason: "stop" }, |
| 25 | + ].map( |
| 26 | + (choice) => |
| 27 | + `data: ${JSON.stringify({ id: "fixture", object: "chat.completion.chunk", created: 1, model: "fixture", choices: [{ index: 0, ...choice }] })}\n\n`, |
| 28 | + ) |
| 29 | + return new Response(chunks.join("") + "data: [DONE]\n\n", { |
| 30 | + headers: { "content-type": "text/event-stream" }, |
| 31 | + }) |
| 32 | + }, |
| 33 | + }) |
| 34 | + const config = { |
| 35 | + provider: { |
| 36 | + fixture: { |
| 37 | + npm: "@ai-sdk/openai-compatible", |
| 38 | + options: { apiKey: "fixture", baseURL: `http://127.0.0.1:${server.port}/v1` }, |
| 39 | + models: { fixture: { name: "fixture", tool_call: true, limit: { context: 100000, output: 1000 } } }, |
| 40 | + }, |
| 41 | + }, |
| 42 | + ...input.config, |
| 43 | + } |
| 44 | + const home = path.join(tmp.path, "home") |
| 45 | + const proc = Bun.spawn( |
| 46 | + [...command, "run", "--format", "json", "--model", "fixture/fixture", ...(input.args ?? []), "Say done."], |
| 47 | + { |
| 48 | + cwd: tmp.path, |
| 49 | + env: { |
| 50 | + ...process.env, |
| 51 | + AICTRL_CONFIG_CONTENT: JSON.stringify(config), |
| 52 | + AICTRL_DISABLE_DEFAULT_PLUGINS: "true", |
| 53 | + AICTRL_DISABLE_AUTOCOMPACT: "true", |
| 54 | + HOME: home, |
| 55 | + AICTRL_TEST_HOME: home, |
| 56 | + AICTRL_TEST_MANAGED_CONFIG_DIR: path.join(tmp.path, "managed"), |
| 57 | + XDG_DATA_HOME: path.join(tmp.path, "data"), |
| 58 | + XDG_CONFIG_HOME: path.join(tmp.path, "config"), |
| 59 | + XDG_STATE_HOME: path.join(tmp.path, "state"), |
| 60 | + XDG_CACHE_HOME: path.join(tmp.path, "cache"), |
| 61 | + }, |
| 62 | + stdout: "pipe", |
| 63 | + stderr: "pipe", |
| 64 | + }, |
| 65 | + ) |
| 66 | + const timeout = setTimeout(() => proc.kill("SIGKILL"), 15000) |
| 67 | + try { |
| 68 | + const [stdout, stderr] = await Promise.all([new Response(proc.stdout).text(), new Response(proc.stderr).text()]) |
| 69 | + const events: { type: string }[] = stdout |
| 70 | + .split("\n") |
| 71 | + .filter((line) => line.startsWith("{")) |
| 72 | + .map((line) => JSON.parse(line)) |
| 73 | + if (!events.some((event) => event.type === "session_start")) |
| 74 | + throw new Error(`no session_start event\n${stderr}\n${stdout}`) |
| 75 | + return events |
| 76 | + } finally { |
| 77 | + clearTimeout(timeout) |
| 78 | + proc.kill("SIGKILL") |
| 79 | + server.stop(true) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +type SessionStart = { type: "session_start"; agent?: string; permissions: Rule[] } |
| 84 | + |
| 85 | +function findSessionStart(events: { type: string }[]) { |
| 86 | + return events.find((event): event is SessionStart => event.type === "session_start")! |
| 87 | +} |
| 88 | + |
| 89 | +async function sessionStart(input: RunInput) { |
| 90 | + return findSessionStart(await runEvents(input)) |
| 91 | +} |
| 92 | + |
| 93 | +/** The rule a tool call is decided by: the last rule whose permission and pattern match (exact match is enough here). */ |
| 94 | +function decisive(rules: Rule[], permission: string, pattern: string) { |
| 95 | + return rules.findLast( |
| 96 | + (rule) => |
| 97 | + (rule.permission === "*" || rule.permission === permission) && (rule.pattern === "*" || rule.pattern === pattern), |
| 98 | + ) |
| 99 | +} |
| 100 | + |
| 101 | +describe("session_start.permissions (#212)", () => { |
| 102 | + test("lists config rules for the default agent, in evaluation order", async () => { |
| 103 | + const event = await sessionStart({ |
| 104 | + config: { agent: { title: { disable: true } }, permission: { "*": "allow", bash: { "rm *": "deny" } } }, |
| 105 | + }) |
| 106 | + |
| 107 | + expect(event.permissions).toContainEqual({ permission: "bash", pattern: "rm *", action: "deny" }) |
| 108 | + expect(decisive(event.permissions, "bash", "rm *")?.action).toBe("deny") |
| 109 | + expect(decisive(event.permissions, "bash", "ls")?.action).toBe("allow") |
| 110 | + }) |
| 111 | + |
| 112 | + test("places a selected agent's own rules after the config rules", async () => { |
| 113 | + const event = await sessionStart({ |
| 114 | + args: ["--agent", "reviewer"], |
| 115 | + config: { |
| 116 | + agent: { title: { disable: true }, reviewer: { mode: "primary", permission: { bash: { "rm *": "allow" } } } }, |
| 117 | + permission: { "*": "allow", bash: { "rm *": "deny" } }, |
| 118 | + }, |
| 119 | + }) |
| 120 | + |
| 121 | + expect(event.agent).toBe("reviewer") |
| 122 | + expect(decisive(event.permissions, "bash", "rm *")?.action).toBe("allow") |
| 123 | + }) |
| 124 | + |
| 125 | + test("uses a --command's own agent, which the command's tool calls run under", async () => { |
| 126 | + const config = { |
| 127 | + agent: { title: { disable: true }, reviewer: { mode: "primary", permission: { bash: { "rm *": "allow" } } } }, |
| 128 | + permission: { "*": "allow", bash: { "rm *": "deny" } }, |
| 129 | + } |
| 130 | + const inline = await sessionStart({ |
| 131 | + args: ["--command", "review"], |
| 132 | + config: { ...config, command: { review: { template: "Review $ARGUMENTS", agent: "reviewer" } } }, |
| 133 | + }) |
| 134 | + // A subtask command runs in a child session; the primary session keeps the default agent. |
| 135 | + const subtask = await sessionStart({ |
| 136 | + args: ["--command", "review"], |
| 137 | + config: { ...config, command: { review: { template: "Review $ARGUMENTS", agent: "reviewer", subtask: true } } }, |
| 138 | + }) |
| 139 | + |
| 140 | + expect(decisive(inline.permissions, "bash", "rm *")?.action).toBe("allow") |
| 141 | + expect(decisive(subtask.permissions, "bash", "rm *")?.action).toBe("deny") |
| 142 | + }) |
| 143 | + |
| 144 | + test("is still emitted when the default agent cannot be resolved", async () => { |
| 145 | + // The prompt reports the unknown agent as session_error; session_start must not be lost. |
| 146 | + const event = await sessionStart({ config: { default_agent: "missing", agent: { title: { disable: true } } } }) |
| 147 | + |
| 148 | + expect(event.permissions).toEqual([]) |
| 149 | + }) |
| 150 | + |
| 151 | + test("is empty when a --command's agent cannot be resolved, and the run fails", async () => { |
| 152 | + // SessionPrompt.command runs the command under its own agent, so an unknown one fails the run |
| 153 | + // instead of falling back to the default agent, whose rules would then never be evaluated. |
| 154 | + const events = await runEvents({ |
| 155 | + args: ["--command", "review"], |
| 156 | + config: { |
| 157 | + agent: { title: { disable: true } }, |
| 158 | + permission: { "*": "allow" }, |
| 159 | + command: { review: { template: "Review $ARGUMENTS", agent: "missing" } }, |
| 160 | + }, |
| 161 | + }) |
| 162 | + |
| 163 | + expect(findSessionStart(events).permissions).toEqual([]) |
| 164 | + expect(events.map((event) => event.type)).toContain("session_error") |
| 165 | + }) |
| 166 | +}) |
0 commit comments