|
1 | 1 | import { describe, expect, spyOn, test } from "bun:test" |
| 2 | +import { Identifier } from "../../src/id/id" |
| 3 | +import type { PermissionNext } from "../../src/permission/next" |
| 4 | +import { Instance } from "../../src/project/instance" |
| 5 | +import { Session } from "../../src/session" |
| 6 | +import { LLM } from "../../src/session/llm" |
2 | 7 | import { MessageV2 } from "../../src/session/message-v2" |
3 | 8 | import { Plugin } from "../../src/plugin" |
4 | | -import { completeTask, taskResultText } from "../../src/tool/task" |
| 9 | +import { TaskTool, completeTask, taskResultText } from "../../src/tool/task" |
| 10 | +import { tmpdir } from "../fixture/fixture" |
5 | 11 |
|
6 | 12 | function result(input: { error?: MessageV2.Assistant["error"]; parts?: MessageV2.Part[] }): MessageV2.WithParts { |
7 | 13 | return { |
@@ -127,3 +133,88 @@ describe("task tool child result", () => { |
127 | 133 | ).toBe("done") |
128 | 134 | }) |
129 | 135 | }) |
| 136 | + |
| 137 | +describe("task tool subagent session", () => { |
| 138 | + test("applies the caller session's rules to the subagent, beneath the subagent denies", async () => { |
| 139 | + await using tmp = await tmpdir({ |
| 140 | + git: true, |
| 141 | + config: { |
| 142 | + provider: { |
| 143 | + fixture: { |
| 144 | + npm: "@ai-sdk/google", |
| 145 | + options: { apiKey: "fixture" }, |
| 146 | + models: { "gemini-fixture": { name: "fixture", limit: { context: 100000, output: 1000 } } }, |
| 147 | + }, |
| 148 | + }, |
| 149 | + experimental: { primary_tools: ["websearch"] }, |
| 150 | + }, |
| 151 | + }) |
| 152 | + await Instance.provide({ |
| 153 | + directory: tmp.path, |
| 154 | + fn: async () => { |
| 155 | + const inherited: PermissionNext.Ruleset = [ |
| 156 | + { permission: "webfetch", action: "deny", pattern: "*" }, |
| 157 | + { permission: "task", action: "allow", pattern: "*" }, |
| 158 | + ] |
| 159 | + const parent = await Session.create({ title: "Caller", permission: inherited }) |
| 160 | + const caller = await Session.updateMessage({ |
| 161 | + id: Identifier.ascending("message"), |
| 162 | + parentID: Identifier.ascending("message"), |
| 163 | + role: "assistant", |
| 164 | + mode: "build", |
| 165 | + agent: "build", |
| 166 | + path: { cwd: tmp.path, root: tmp.path }, |
| 167 | + cost: 0, |
| 168 | + tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } }, |
| 169 | + modelID: "gemini-fixture", |
| 170 | + providerID: "fixture", |
| 171 | + time: { created: Date.now() }, |
| 172 | + sessionID: parent.id, |
| 173 | + }) |
| 174 | + const requests: LLM.StreamInput[] = [] |
| 175 | + const stream = spyOn(LLM, "stream").mockImplementation(async (input) => { |
| 176 | + requests.push(input) |
| 177 | + return { |
| 178 | + fullStream: (async function* () { |
| 179 | + yield { type: "start-step" } |
| 180 | + yield { type: "text-start" } |
| 181 | + yield { type: "text-delta", text: "done" } |
| 182 | + yield { type: "text-end" } |
| 183 | + yield { type: "finish-step", finishReason: "stop", usage: { inputTokens: 1, outputTokens: 1 } } |
| 184 | + })(), |
| 185 | + } as unknown as Awaited<ReturnType<typeof LLM.stream>> |
| 186 | + }) |
| 187 | + try { |
| 188 | + const tool = await TaskTool.init() |
| 189 | + const result = await tool.execute( |
| 190 | + { description: "Explore", prompt: "Look around", subagent_type: "general" }, |
| 191 | + { |
| 192 | + sessionID: parent.id, |
| 193 | + messageID: caller.id, |
| 194 | + agent: "build", |
| 195 | + abort: new AbortController().signal, |
| 196 | + extra: { bypassAgentCheck: true }, |
| 197 | + messages: [], |
| 198 | + metadata() {}, |
| 199 | + async ask() {}, |
| 200 | + }, |
| 201 | + ) |
| 202 | + const child = await Session.get(result.metadata.sessionId) |
| 203 | + const subagent = requests.filter((request) => request.sessionID === child.id) |
| 204 | + expect(subagent).toHaveLength(1) |
| 205 | + expect(subagent[0].permission).toEqual([ |
| 206 | + ...inherited, |
| 207 | + { permission: "todowrite", action: "deny", pattern: "*" }, |
| 208 | + { permission: "todoread", action: "deny", pattern: "*" }, |
| 209 | + { permission: "task", action: "deny", pattern: "*" }, |
| 210 | + // primary-only tools stay unavailable to subagents |
| 211 | + { permission: "websearch", action: "deny", pattern: "*" }, |
| 212 | + ]) |
| 213 | + } finally { |
| 214 | + stream.mockRestore() |
| 215 | + await Session.remove(parent.id) |
| 216 | + } |
| 217 | + }, |
| 218 | + }) |
| 219 | + }) |
| 220 | +}) |
0 commit comments