Skip to content

Commit c1bca11

Browse files
byapparovclaude
andcommitted
fix(session): drop session-denied tools from model requests (#147)
The tool list sent to the model was filtered only by the agent's permission, while tool calls are checked against the agent ruleset merged with the session's. Headless runs deny question, plan_enter and plan_exit at session level, so the build agent still advertised question and plan_enter on every step and the calls were only rejected at execution. Pass the session ruleset into LLM.stream and merge it with the agent's before computing disabled tools, so any tool the session denies with pattern "*" is removed from the request. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent cd736e2 commit c1bca11

4 files changed

Lines changed: 147 additions & 4 deletions

File tree

‎packages/cli/src/session/llm.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ export namespace LLM {
3535
sessionID: string
3636
model: Provider.Model
3737
agent: Agent.Info
38+
// session rules layered over the agent's, e.g. the denies `aictrl run` creates its session with
39+
permission?: PermissionNext.Ruleset
3840
system: string[]
3941
abort: AbortSignal
4042
messages: ModelMessage[]
@@ -275,8 +277,10 @@ export namespace LLM {
275277
})
276278
}
277279

278-
async function resolveTools(input: Pick<StreamInput, "tools" | "agent" | "user">) {
279-
const disabled = PermissionNext.disabled(Object.keys(input.tools), input.agent.permission)
280+
async function resolveTools(input: Pick<StreamInput, "tools" | "agent" | "permission" | "user">) {
281+
// same ruleset tool calls are checked against, so a denied tool is never offered to the model
282+
const ruleset = PermissionNext.merge(input.agent.permission, input.permission ?? [])
283+
const disabled = PermissionNext.disabled(Object.keys(input.tools), ruleset)
280284
for (const tool of Object.keys(input.tools)) {
281285
if (input.user.tools?.[tool] === false || disabled.has(tool)) {
282286
delete input.tools[tool]

‎packages/cli/src/session/prompt.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,7 @@ export namespace SessionPrompt {
790790
const result = await processor.process({
791791
user: lastUser,
792792
agent,
793+
permission: session.permission,
793794
abort,
794795
sessionID,
795796
system,

‎packages/cli/test/session/llm.test.ts‎

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import { ProviderTransform } from "../../src/provider/transform"
99
import { ModelsDev } from "../../src/provider/models"
1010
import { Filesystem } from "../../src/util/filesystem"
1111
import { tmpdir } from "../fixture/fixture"
12-
import type { Agent } from "../../src/agent/agent"
12+
import { Agent } from "../../src/agent/agent"
13+
import type { PermissionNext } from "../../src/permission/next"
1314
import type { MessageV2 } from "../../src/session/message-v2"
1415

1516
describe("session.llm.hasToolCalls", () => {
@@ -801,4 +802,91 @@ describe("session.llm.stream", () => {
801802
},
802803
})
803804
})
805+
806+
test("omits tools the session denies from the request while the agent allows them", async () => {
807+
const server = state.server
808+
if (!server) {
809+
throw new Error("Server not initialized")
810+
}
811+
812+
const providerID = "alibaba"
813+
const modelID = "qwen-plus"
814+
const fixture = await loadFixture(providerID, modelID)
815+
816+
await using tmp = await tmpdir({
817+
init: async (dir) => {
818+
await Bun.write(
819+
path.join(dir, "aictrl.json"),
820+
JSON.stringify({
821+
$schema: "https://aictrl.ai/config.json",
822+
enabled_providers: [providerID],
823+
provider: {
824+
[providerID]: {
825+
options: {
826+
apiKey: "test-key",
827+
baseURL: `${server.url.origin}/v1`,
828+
},
829+
},
830+
},
831+
}),
832+
)
833+
},
834+
})
835+
836+
await Instance.provide({
837+
directory: tmp.path,
838+
fn: async () => {
839+
const resolved = await Provider.getModel(providerID, fixture.model.id)
840+
// the build agent allows question and plan_enter (and already denies plan_exit)
841+
const agent = await Agent.get("build")
842+
// the session rules `aictrl run` creates headless sessions with
843+
const headless: PermissionNext.Ruleset = [
844+
{ permission: "question", action: "deny", pattern: "*" },
845+
{ permission: "plan_enter", action: "deny", pattern: "*" },
846+
{ permission: "plan_exit", action: "deny", pattern: "*" },
847+
]
848+
849+
async function requestTools(sessionID: string, permission?: PermissionNext.Ruleset) {
850+
const request = waitRequest(
851+
"/chat/completions",
852+
new Response(createChatStream("Hello"), {
853+
status: 200,
854+
headers: { "Content-Type": "text/event-stream" },
855+
}),
856+
)
857+
const stub = () =>
858+
tool({
859+
description: "stub",
860+
inputSchema: jsonSchema({ type: "object", properties: {} }),
861+
execute: async () => "ok",
862+
})
863+
const stream = await LLM.stream({
864+
user: {
865+
id: `user-${sessionID}`,
866+
sessionID,
867+
role: "user",
868+
time: { created: Date.now() },
869+
agent: agent.name,
870+
model: { providerID, modelID: resolved.id },
871+
},
872+
sessionID,
873+
model: resolved,
874+
agent,
875+
permission,
876+
system: ["You are a helpful assistant."],
877+
abort: new AbortController().signal,
878+
messages: [{ role: "user", content: "Hello" }],
879+
tools: { question: stub(), plan_enter: stub(), plan_exit: stub(), read: stub() },
880+
})
881+
for await (const _ of stream.fullStream) {
882+
}
883+
const body = (await request).body
884+
return (body.tools as Array<{ function: { name: string } }>).map((item) => item.function.name).sort()
885+
}
886+
887+
expect(await requestTools("session-interactive")).toEqual(["plan_enter", "question", "read"])
888+
expect(await requestTools("session-headless", headless)).toEqual(["read"])
889+
},
890+
})
891+
})
804892
})

‎packages/cli/test/session/prompt.test.ts‎

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import path from "path"
2-
import { describe, expect, test } from "bun:test"
2+
import { describe, expect, spyOn, test } from "bun:test"
33
import { fileURLToPath } from "url"
4+
import type { PermissionNext } from "../../src/permission/next"
45
import { Instance } from "../../src/project/instance"
56
import { Session } from "../../src/session"
7+
import { LLM } from "../../src/session/llm"
68
import { MessageV2 } from "../../src/session/message-v2"
79
import { SessionPrompt } from "../../src/session/prompt"
810
import { Log } from "../../src/util/log"
@@ -209,3 +211,51 @@ describe("session.prompt agent variant", () => {
209211
}
210212
})
211213
})
214+
215+
describe("session.prompt session permission", () => {
216+
test("passes the session's permission rules to the model request", async () => {
217+
await using tmp = await tmpdir({
218+
git: true,
219+
config: {
220+
provider: {
221+
fixture: {
222+
npm: "@ai-sdk/google",
223+
options: { apiKey: "fixture" },
224+
models: { "gemini-fixture": { name: "fixture", limit: { context: 100000, output: 1000 } } },
225+
},
226+
},
227+
},
228+
})
229+
await Instance.provide({
230+
directory: tmp.path,
231+
fn: async () => {
232+
const permission: PermissionNext.Ruleset = [{ permission: "question", action: "deny", pattern: "*" }]
233+
const session = await Session.create({ title: "Session permission", permission })
234+
const requests: LLM.StreamInput[] = []
235+
const stream = spyOn(LLM, "stream").mockImplementation(async (input) => {
236+
requests.push(input)
237+
return {
238+
fullStream: (async function* () {
239+
yield { type: "start-step" }
240+
yield { type: "finish-step", finishReason: "stop", usage: { inputTokens: 1, outputTokens: 1 } }
241+
})(),
242+
} as unknown as Awaited<ReturnType<typeof LLM.stream>>
243+
})
244+
try {
245+
await SessionPrompt.prompt({
246+
sessionID: session.id,
247+
agent: "build",
248+
model: { providerID: "fixture", modelID: "gemini-fixture" },
249+
parts: [{ type: "text", text: "Finish the task" }],
250+
})
251+
const turns = requests.filter((request) => request.agent.name === "build")
252+
expect(turns).toHaveLength(1)
253+
expect(turns[0].permission).toEqual(permission)
254+
} finally {
255+
stream.mockRestore()
256+
await Session.remove(session.id)
257+
}
258+
},
259+
})
260+
})
261+
})

0 commit comments

Comments
 (0)