|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | + |
| 3 | +import { |
| 4 | + createFleetMailbox, |
| 5 | + createSpawnAgentTool, |
| 6 | + type AgentFleetDeps, |
| 7 | +} from "./agent-fleet.js"; |
| 8 | +import { unlimitedAdmissionQueue } from "./admission.js"; |
| 9 | +import { createPermissionGate } from "../permission/gate.js"; |
| 10 | +import { NOOP_TELEMETRY, type TelemetryEvent } from "../telemetry/index.js"; |
| 11 | +import type { AgentProfile } from "../agent/profiles.js"; |
| 12 | +import { createSubAgentSessionStore } from "./session-store.js"; |
| 13 | +import type { RunSubAgentParams, RunSubAgentResult } from "./types.js"; |
| 14 | + |
| 15 | +const testPermissionGate = createPermissionGate({ |
| 16 | + approvals: [], |
| 17 | + interactive: false, |
| 18 | + skipPermissions: true, |
| 19 | + reactorGated: false, |
| 20 | +}); |
| 21 | + |
| 22 | +const provider = { |
| 23 | + providerName: "test-provider", |
| 24 | + baseURL: "http://localhost", |
| 25 | + model: "test-model", |
| 26 | +}; |
| 27 | + |
| 28 | +const READ_ONLY_PROFILE: AgentProfile = { |
| 29 | + id: "read-only-worker", |
| 30 | + systemPromptRole: "You read files.", |
| 31 | + capabilities: { mode: "allow", tools: ["read_file"] }, |
| 32 | +}; |
| 33 | + |
| 34 | +function makeDeps( |
| 35 | + run: (params: RunSubAgentParams) => Promise<RunSubAgentResult>, |
| 36 | + capturedTelemetry: TelemetryEvent[], |
| 37 | +): AgentFleetDeps { |
| 38 | + const sessions = createSubAgentSessionStore(); |
| 39 | + return { |
| 40 | + permissionGate: testPermissionGate, |
| 41 | + cwd: "/tmp", |
| 42 | + getWorkdirBase: () => "/tmp/workdir", |
| 43 | + provider, |
| 44 | + run, |
| 45 | + sessions, |
| 46 | + fleetRecords: createFleetMailbox(sessions), |
| 47 | + admission: unlimitedAdmissionQueue(), |
| 48 | + profiles: [READ_ONLY_PROFILE], |
| 49 | + telemetry: { |
| 50 | + ...NOOP_TELEMETRY, |
| 51 | + capture: (event: TelemetryEvent) => { |
| 52 | + capturedTelemetry.push(event); |
| 53 | + }, |
| 54 | + }, |
| 55 | + }; |
| 56 | +} |
| 57 | + |
| 58 | +async function callSpawn( |
| 59 | + tool: ReturnType<typeof createSpawnAgentTool>, |
| 60 | + args: Record<string, unknown>, |
| 61 | +): Promise<{ content: string; isError?: boolean }> { |
| 62 | + if (tool.kind !== "full") throw new Error("expected full tool"); |
| 63 | + const result = await tool.handler( |
| 64 | + { |
| 65 | + id: `spawn-${Math.random()}`, |
| 66 | + name: "spawn_agent", |
| 67 | + arguments: args, |
| 68 | + }, |
| 69 | + new AbortController().signal, |
| 70 | + ); |
| 71 | + const content = |
| 72 | + typeof result.content === "string" |
| 73 | + ? result.content |
| 74 | + : JSON.stringify(result.content); |
| 75 | + return { |
| 76 | + content, |
| 77 | + ...(result.isError !== undefined ? { isError: result.isError } : {}), |
| 78 | + }; |
| 79 | +} |
| 80 | + |
| 81 | +describe("spawn_agent requires_tools preflight", () => { |
| 82 | + test("rejects a tool outside the allowlist with no session, telemetry, or run", async () => { |
| 83 | + const telemetry: TelemetryEvent[] = []; |
| 84 | + let runCalled = false; |
| 85 | + const deps = makeDeps(async () => { |
| 86 | + runCalled = true; |
| 87 | + return { report: "done" }; |
| 88 | + }, telemetry); |
| 89 | + const spawn = createSpawnAgentTool(deps); |
| 90 | + |
| 91 | + const result = await callSpawn(spawn, { |
| 92 | + description: "shell job", |
| 93 | + prompt: "run a command", |
| 94 | + agent: "read-only-worker", |
| 95 | + requires_tools: ["run_shell"], |
| 96 | + }); |
| 97 | + |
| 98 | + expect(result.isError).toBe(true); |
| 99 | + expect(result.content.startsWith("Error:")).toBe(true); |
| 100 | + expect(result.content).toContain("run_shell"); |
| 101 | + expect(result.content).toContain("Re-dispatch"); |
| 102 | + expect(result.content).not.toContain("continuable"); |
| 103 | + expect(runCalled).toBe(false); |
| 104 | + expect(deps.sessions.list()).toEqual([]); |
| 105 | + expect(telemetry).toEqual([]); |
| 106 | + }); |
| 107 | + |
| 108 | + test("rejects an unknown tool with a did-you-mean hint and no session", async () => { |
| 109 | + const telemetry: TelemetryEvent[] = []; |
| 110 | + let runCalled = false; |
| 111 | + const deps = makeDeps(async () => { |
| 112 | + runCalled = true; |
| 113 | + return { report: "done" }; |
| 114 | + }, telemetry); |
| 115 | + const spawn = createSpawnAgentTool(deps); |
| 116 | + |
| 117 | + const result = await callSpawn(spawn, { |
| 118 | + description: "typo job", |
| 119 | + prompt: "do it", |
| 120 | + agent: "read-only-worker", |
| 121 | + requires_tools: ["run_shel"], |
| 122 | + }); |
| 123 | + |
| 124 | + expect(result.isError).toBe(true); |
| 125 | + expect(result.content).toContain('Did you mean "run_shell"?'); |
| 126 | + expect(runCalled).toBe(false); |
| 127 | + expect(deps.sessions.list()).toEqual([]); |
| 128 | + expect(telemetry).toEqual([]); |
| 129 | + }); |
| 130 | + |
| 131 | + test("accepts a mounted tool and stamps the session record", async () => { |
| 132 | + const telemetry: TelemetryEvent[] = []; |
| 133 | + let seenRequires: readonly string[] | undefined; |
| 134 | + const deps = makeDeps(async (params) => { |
| 135 | + seenRequires = params.requiresTools; |
| 136 | + return { report: "done" }; |
| 137 | + }, telemetry); |
| 138 | + const spawn = createSpawnAgentTool(deps); |
| 139 | + |
| 140 | + const result = await callSpawn(spawn, { |
| 141 | + description: "read job", |
| 142 | + prompt: "read a file", |
| 143 | + agent: "read-only-worker", |
| 144 | + requires_tools: ["read_file"], |
| 145 | + }); |
| 146 | + |
| 147 | + expect(result.isError).not.toBe(true); |
| 148 | + const body = JSON.parse(result.content) as { |
| 149 | + agent_id: string; |
| 150 | + status: string; |
| 151 | + }; |
| 152 | + const session = deps.sessions.get(body.agent_id); |
| 153 | + expect(session?.requiresTools).toEqual(["read_file"]); |
| 154 | + expect(typeof session?.snapshotRevision).toBe("number"); |
| 155 | + expect(seenRequires).toEqual(["read_file"]); |
| 156 | + expect(telemetry).toContain("subagent_start"); |
| 157 | + }); |
| 158 | + |
| 159 | + test("aliases collapse at dispatch (shell stamps run_shell)", async () => { |
| 160 | + const telemetry: TelemetryEvent[] = []; |
| 161 | + const deps = makeDeps(async () => ({ report: "done" }), telemetry); |
| 162 | + const spawn = createSpawnAgentTool(deps); |
| 163 | + |
| 164 | + const result = await callSpawn(spawn, { |
| 165 | + description: "alias read job", |
| 166 | + prompt: "read a file", |
| 167 | + agent: "read-only-worker", |
| 168 | + requires_tools: ["read"], |
| 169 | + }); |
| 170 | + |
| 171 | + expect(result.isError).not.toBe(true); |
| 172 | + const body = JSON.parse(result.content) as { agent_id: string }; |
| 173 | + expect(deps.sessions.get(body.agent_id)?.requiresTools).toEqual([ |
| 174 | + "read_file", |
| 175 | + ]); |
| 176 | + }); |
| 177 | + |
| 178 | + test("omitted requires_tools leaves the session unstamped", async () => { |
| 179 | + const telemetry: TelemetryEvent[] = []; |
| 180 | + const deps = makeDeps(async () => ({ report: "done" }), telemetry); |
| 181 | + const spawn = createSpawnAgentTool(deps); |
| 182 | + |
| 183 | + const result = await callSpawn(spawn, { |
| 184 | + description: "plain job", |
| 185 | + prompt: "read a file", |
| 186 | + agent: "read-only-worker", |
| 187 | + }); |
| 188 | + |
| 189 | + expect(result.isError).not.toBe(true); |
| 190 | + const body = JSON.parse(result.content) as { agent_id: string }; |
| 191 | + const session = deps.sessions.get(body.agent_id); |
| 192 | + expect(session?.requiresTools).toBeUndefined(); |
| 193 | + expect(session?.snapshotRevision).toBeUndefined(); |
| 194 | + }); |
| 195 | +}); |
0 commit comments