fix(core): honor session permissions in skill and MCP discovery (#50015)

This commit is contained in:
Aiden Cline
2026-09-19 12:59:09 -05:00
committed by GitHub
Unverified
parent cc3ce20ab4
commit 728b2b6052
8 changed files with 75 additions and 69 deletions
+6 -8
View File
@@ -2,7 +2,6 @@ export * as McpInstructions from "./instructions.js"
import { makeLocationNode } from "@opencode/util/effect/app-node"
import { Context, Effect, Layer, Schema } from "effect"
import { Agent } from "../agent.js"
import { Permission } from "../permission.js"
import { McpTool } from "../tool/mcp.js"
import { Mcp } from "./index.js"
@@ -55,7 +54,8 @@ const update = (previous: ReadonlyArray<Summary>, current: ReadonlyArray<Summary
}
export interface Interface {
readonly load: (agent: Agent.Selection) => Effect.Effect<Instructions.List>
/** Lists server instructions reachable under the given ruleset; callers pass the merged agent and Session permissions. */
readonly load: (permissions: Permission.Ruleset) => Effect.Effect<Instructions.List>
}
export class Service extends Context.Service<Service, Interface>()("@opencode/McpInstructions") {}
@@ -66,9 +66,7 @@ export const layer = Layer.effect(
const mcp = yield* Mcp.Service
return Service.of({
load: Effect.fn("McpInstructions.load")(function* (selection) {
const agent = selection.info
if (!agent) return Instructions.empty
load: Effect.fn("McpInstructions.load")(function* (permissions) {
const source = (value: ReadonlyArray<Summary> | Instructions.Removed) =>
Instructions.make<ReadonlyArray<Summary>>({
key: Instructions.Key.make("core/mcp-guidance"),
@@ -83,8 +81,8 @@ export const layer = Layer.effect(
const [instructions, tools] = yield* Effect.all([mcp.instructions(), mcp.tools()], {
concurrency: "unbounded",
})
const canExecute = Permission.evaluate("execute", "*", agent.permissions).effect !== "deny"
// Instructions are useful only when this agent can reach at least one server tool.
const canExecute = Permission.evaluate("execute", "*", permissions).effect !== "deny"
// Instructions are useful only when this Session can reach at least one server tool.
const visible = instructions
.flatMap((item) => {
const owned = tools.filter((tool) => tool.server === item.server)
@@ -93,7 +91,7 @@ export const layer = Layer.effect(
if (
!owned.some(
(tool) =>
Permission.evaluate(McpTool.name(tool.server, tool.name), "*", agent.permissions).effect !== "deny",
Permission.evaluate(McpTool.name(tool.server, tool.name), "*", permissions).effect !== "deny",
)
)
return []
+5 -3
View File
@@ -127,14 +127,16 @@ const layer = Layer.effect(
yield* mcpTools.flush
const agent = yield* agents.select(session.agent)
if (!agent.info) return yield* new AgentNotFoundError({ sessionID: session.id, agent: session.agent ?? agent.id })
// Session permissions narrow discovery the same way they narrow the tool snapshot.
const permissions = Permission.merge(agent.info.permissions, session.permissions ?? [])
const loaded = yield* Effect.all(
{
tools: registry.snapshot(Permission.merge(agent.info.permissions, session.permissions ?? [])),
tools: registry.snapshot(permissions),
builtins: builtins.load(sessionID),
discovery: discovery.load(),
skills: skillInstructions.load(agent),
skills: skillInstructions.load(permissions),
references: referenceInstructions.load(),
mcp: mcpInstructions.load(agent),
mcp: mcpInstructions.load(permissions),
entries: entries.load(sessionID),
},
{ concurrency: "unbounded" },
+2 -3
View File
@@ -4,7 +4,6 @@ import { makeLocationNode } from "@opencode/util/effect/app-node"
import type { FSUtil } from "@opencode/util/fs-util"
import path from "path"
import { Context, Effect, Layer, Types } from "effect"
import type { Agent } from "@opencode/schema/agent"
import { Skill } from "@opencode/schema/skill"
import { Bus } from "./bus.js"
import { Permission } from "./permission.js"
@@ -31,8 +30,8 @@ export type Name = Skill.Name
export { Event } from "@opencode/schema/skill"
export const available = (skills: ReadonlyArray<Info>, agent: Agent.Info) =>
skills.filter((skill) => Permission.evaluate("skill", skill.id, agent.permissions).effect !== "deny")
export const available = (skills: ReadonlyArray<Info>, permissions: Permission.Ruleset) =>
skills.filter((skill) => Permission.evaluate("skill", skill.id, permissions).effect !== "deny")
export const toModelOutput = (skill: Info, files: ReadonlyArray<string>) => {
const directory = path.dirname(skill.path)
+5 -6
View File
@@ -2,7 +2,7 @@ export * as SkillInstructions from "./instructions.js"
import { makeLocationNode } from "@opencode/util/effect/app-node"
import { Context, Effect, Layer, Schema } from "effect"
import { Agent } from "../agent.js"
import { Permission } from "../permission.js"
import { Skill } from "../skill.js"
import { Instructions } from "../instructions/index.js"
@@ -58,7 +58,8 @@ const update = (previous: ReadonlyArray<Summary>, current: ReadonlyArray<Summary
}
export interface Interface {
readonly load: (agent: Agent.Selection) => Effect.Effect<Instructions.List>
/** Lists skills the given ruleset does not deny; callers pass the merged agent and Session permissions. */
readonly load: (permissions: Permission.Ruleset) => Effect.Effect<Instructions.List>
}
export class Service extends Context.Service<Service, Interface>()("@opencode/SkillInstructions") {}
@@ -69,10 +70,8 @@ const layer = Layer.effect(
const skills = yield* Skill.Service
return Service.of({
load: Effect.fn("SkillInstructions.load")(function* (selection) {
const agent = selection.info
if (!agent) return Instructions.empty
const available = Skill.available(yield* skills.list(), agent)
load: Effect.fn("SkillInstructions.load")(function* (permissions) {
const available = Skill.available(yield* skills.list(), permissions)
.flatMap((skill) =>
skill.description === undefined || skill.autoinvoke === false
? []
+22 -26
View File
@@ -1,23 +1,14 @@
import { describe, expect } from "bun:test"
import { Effect, Layer } from "effect"
import { Agent } from "@opencode/core/agent"
import { AppNodeBuilder } from "@opencode/core/effect/app-node-builder"
import { Mcp } from "@opencode/core/mcp/index"
import { McpInstructions } from "@opencode/core/mcp/instructions"
import { Permission } from "@opencode/core/permission"
import { McpTool } from "@opencode/core/tool/mcp"
import { it } from "./lib/effect"
import { readInitial, readUpdate } from "./lib/instructions"
const build = Agent.ID.make("build")
const selection = (permissions: Permission.Ruleset = []) => {
const info = Agent.Info.make({ ...Agent.Info.default(build), permissions })
return { id: info.id, info }
}
const instructions = (server: string, text: string) =>
({ server: Mcp.ServerName.make(server), instructions: text } satisfies Mcp.ServerInstructions)
({ server: Mcp.ServerName.make(server), instructions: text }) satisfies Mcp.ServerInstructions
const schema = { type: "object" as const }
const tool = (server: string, name = "search") =>
@@ -38,12 +29,10 @@ describe("McpInstructions", () => {
Effect.gen(function* () {
const service = yield* McpInstructions.Service
const generation = yield* service
.load(
selection([
{ action: McpTool.name("alpha", "restricted"), resource: "*", effect: "deny" },
{ action: McpTool.name("hidden", "search"), resource: "*", effect: "deny" },
]),
)
.load([
{ action: McpTool.name("alpha", "restricted"), resource: "*", effect: "deny" },
{ action: McpTool.name("hidden", "search"), resource: "*", effect: "deny" },
])
.pipe(Effect.flatMap(readInitial))
expect(generation.text).toBe(
@@ -80,7 +69,7 @@ describe("McpInstructions", () => {
Effect.gen(function* () {
const service = yield* McpInstructions.Service
const generation = yield* service
.load(selection([{ action: "execute", resource: "*", effect: "deny" }]))
.load([{ action: "execute", resource: "*", effect: "deny" }])
.pipe(Effect.flatMap(readInitial))
expect(generation.text).toBe("")
@@ -98,7 +87,7 @@ describe("McpInstructions", () => {
Effect.gen(function* () {
const service = yield* McpInstructions.Service
const generation = yield* service
.load(selection([{ action: "execute", resource: "*", effect: "deny" }]))
.load([{ action: "execute", resource: "*", effect: "deny" }])
.pipe(Effect.flatMap(readInitial))
expect(generation.text).toBe(
@@ -114,7 +103,14 @@ describe("McpInstructions", () => {
Effect.provide(
layer(
() => [instructions("alpha", "Alpha instructions")],
() => [({ server: Mcp.ServerName.make("alpha"), name: "search", inputSchema: schema, codemode: false }) satisfies Mcp.Tool],
() => [
{
server: Mcp.ServerName.make("alpha"),
name: "search",
inputSchema: schema,
codemode: false,
} satisfies Mcp.Tool,
],
),
),
),
@@ -124,10 +120,10 @@ describe("McpInstructions", () => {
let tools: Mcp.Tool[] = [tool("alpha")]
return Effect.gen(function* () {
const service = yield* McpInstructions.Service
const initialized = yield* service.load(selection()).pipe(Effect.flatMap(readInitial))
const initialized = yield* service.load([]).pipe(Effect.flatMap(readInitial))
tools = [{ ...tool("alpha"), codemode: false }]
const changed = yield* readUpdate(yield* service.load(selection()), initialized)
const changed = yield* readUpdate(yield* service.load([]), initialized)
expect(changed.text).toBe(
[
"The available MCP server instructions have changed. This list supersedes the previous one.",
@@ -153,10 +149,10 @@ describe("McpInstructions", () => {
const tools = [tool("alpha"), tool("beta")]
return Effect.gen(function* () {
const service = yield* McpInstructions.Service
const initialized = yield* service.load(selection()).pipe(Effect.flatMap(readInitial))
const initialized = yield* service.load([]).pipe(Effect.flatMap(readInitial))
catalog = [instructions("alpha", "Alpha instructions"), instructions("beta", "Beta instructions")]
const added = yield* readUpdate(yield* service.load(selection()), initialized)
const added = yield* readUpdate(yield* service.load([]), initialized)
expect(added.text).toBe(
[
"New MCP server instructions are available in addition to those previously listed:",
@@ -168,7 +164,7 @@ describe("McpInstructions", () => {
)
catalog = [instructions("alpha", "Updated alpha"), instructions("beta", "Beta instructions")]
const changed = yield* readUpdate(yield* service.load(selection()), added)
const changed = yield* readUpdate(yield* service.load([]), added)
expect(changed.text).toBe(
[
"The available MCP server instructions have changed. This list supersedes the previous one.",
@@ -186,11 +182,11 @@ describe("McpInstructions", () => {
)
catalog = [instructions("beta", "Beta instructions")]
const removed = yield* readUpdate(yield* service.load(selection()), changed)
const removed = yield* readUpdate(yield* service.load([]), changed)
expect(removed.text).toBe("Instructions for the following MCP servers are no longer available: alpha.")
catalog = []
expect((yield* readUpdate(yield* service.load(selection()), removed)).text).toBe(
expect((yield* readUpdate(yield* service.load([]), removed)).text).toBe(
"MCP server instructions are no longer available.",
)
}).pipe(
+23 -11
View File
@@ -362,21 +362,27 @@ const layer = Layer.unwrap(
load: () => Effect.succeed(Instructions.empty),
})
const skillInstructions = Layer.mock(SkillInstructions.Service, {
load: (agent) =>
Effect.succeed(
state.skillBaselines.has(agent.id)
? Instructions.make({
load: (permissions) => {
// `setup` tags each agent's ruleset with a `skill` rule naming that agent.
const baseline = permissions
.filter((rule) => rule.action === "skill")
.map((rule) => state.skillBaselines.get(Agent.ID.make(rule.resource)))
.findLast((text) => text !== undefined)
return Effect.succeed(
baseline === undefined
? Instructions.empty
: Instructions.make({
key: Instructions.Key.make("test/skill-guidance"),
codec: Schema.toCodecJson(Schema.String),
read: Effect.succeed(state.skillBaselines.get(agent.id)!),
read: Effect.succeed(baseline),
render: {
initial: String,
changed: (_previous, current) => current,
removed: () => "Skill guidance removed",
},
})
: Instructions.empty,
),
}),
)
},
})
const referenceInstructions = Layer.mock(ReferenceInstructions.Service, {
load: () => Effect.succeed(Instructions.empty),
@@ -526,11 +532,17 @@ const setup = Effect.gen(function* () {
})
yield* IdentityPlugin.Plugin.effect(pluginHost)
yield* NativeCompactionPlugin.Plugin.effect(pluginHost)
yield* agents.transform((editor) =>
yield* agents.transform((editor) => {
editor.update(Agent.ID.make("build"), (agent) => {
agent.mode = "primary"
}),
)
})
// Skill instructions receive only a ruleset, so tag each agent with a rule naming itself for the mock.
for (const id of ["build", "reviewer"]) {
editor.update(Agent.ID.make(id), (agent) => {
agent.permissions.push({ action: "skill", resource: id, effect: "allow" })
})
}
})
yield* db
.insert(ProjectTable)
.values({ id: Project.ID.global, worktree: AbsolutePath.make("/project"), sandboxes: [] })
+1 -1
View File
@@ -119,7 +119,7 @@ describe("Skill", () => {
}),
)
const agent = yield* agents.get(Agent.ID.make("reviewer"))
expect(Skill.available([info("deploy", "Deploy")], agent!)).toEqual([])
expect(Skill.available([info("deploy", "Deploy")], agent!.permissions)).toEqual([])
}),
)
})
+11 -11
View File
@@ -53,7 +53,7 @@ describe("SkillInstructions", () => {
let skills = [hidden, denied, manual, effect]
return Effect.gen(function* () {
const instructions = yield* SkillInstructions.Service
const initialized = yield* instructions.load({ id: agent.id, info: agent }).pipe(Effect.flatMap(readInitial))
const initialized = yield* instructions.load(agent.permissions).pipe(Effect.flatMap(readInitial))
expect(initialized.text).toBe(
[
@@ -74,7 +74,7 @@ describe("SkillInstructions", () => {
skills = []
expect(
yield* instructions
.load({ id: agent.id, info: agent })
.load(agent.permissions)
.pipe(Effect.flatMap((context) => readUpdate(context, initialized))),
).toMatchObject({ text: "Skill guidance is no longer available. Do not use any previously listed skill." })
}).pipe(Effect.provide(layer(() => skills)))
@@ -92,11 +92,11 @@ describe("SkillInstructions", () => {
let skills = [effect]
return Effect.gen(function* () {
const instructions = yield* SkillInstructions.Service
const initialized = yield* instructions.load({ id: agent.id, info: agent }).pipe(Effect.flatMap(readInitial))
const initialized = yield* instructions.load(agent.permissions).pipe(Effect.flatMap(readInitial))
skills = [effect, debugging]
const added = yield* instructions
.load({ id: agent.id, info: agent })
.load(agent.permissions)
.pipe(Effect.flatMap((context) => readUpdate(context, initialized)))
expect(added.text).toBe(
[
@@ -111,7 +111,7 @@ describe("SkillInstructions", () => {
skills = [debugging]
const removed = yield* instructions
.load({ id: agent.id, info: agent })
.load(agent.permissions)
.pipe(Effect.flatMap((context) => readUpdate(context, added)))
expect(removed.text).toBe("The following skill IDs are no longer available and must not be used: effect.")
}).pipe(Effect.provide(layer(() => skills)))
@@ -122,12 +122,12 @@ describe("SkillInstructions", () => {
let skills = [effect]
return Effect.gen(function* () {
const instructions = yield* SkillInstructions.Service
const initialized = yield* instructions.load({ id: agent.id, info: agent }).pipe(Effect.flatMap(readInitial))
const initialized = yield* instructions.load(agent.permissions).pipe(Effect.flatMap(readInitial))
skills = [Skill.Info.make({ ...effect, description: "Build applications with Effect v4" })]
expect(
yield* instructions
.load({ id: agent.id, info: agent })
.load(agent.permissions)
.pipe(Effect.flatMap((context) => readUpdate(context, initialized))),
).toMatchObject({
text: expect.stringContaining(
@@ -144,7 +144,7 @@ describe("SkillInstructions", () => {
})
return Effect.gen(function* () {
const instructions = yield* SkillInstructions.Service
expect((yield* instructions.load({ id: agent.id, info: agent }).pipe(Effect.flatMap(readInitial))).text).toBe("")
expect((yield* instructions.load(agent.permissions).pipe(Effect.flatMap(readInitial))).text).toBe("")
}).pipe(Effect.provide(layer(() => [effect])))
})
@@ -158,7 +158,7 @@ describe("SkillInstructions", () => {
})
return Effect.gen(function* () {
const instructions = yield* SkillInstructions.Service
expect((yield* instructions.load({ id: agent.id, info: agent }).pipe(Effect.flatMap(readInitial))).text).toBe("")
expect((yield* instructions.load(agent.permissions).pipe(Effect.flatMap(readInitial))).text).toBe("")
}).pipe(Effect.provide(layer(() => [effect])))
})
@@ -173,7 +173,7 @@ describe("SkillInstructions", () => {
return Effect.gen(function* () {
const instructions = yield* SkillInstructions.Service
expect(
(yield* instructions.load({ id: agent.id, info: agent }).pipe(Effect.flatMap(readInitial))).text,
(yield* instructions.load(agent.permissions).pipe(Effect.flatMap(readInitial))).text,
).toContain("<name>Effect</name>")
}).pipe(Effect.provide(layer(() => [effect])))
})
@@ -189,7 +189,7 @@ describe("SkillInstructions", () => {
})
return Effect.gen(function* () {
const instructions = yield* SkillInstructions.Service
expect((yield* instructions.load({ id: agent.id, info: agent }).pipe(Effect.flatMap(readInitial))).text).toBe("")
expect((yield* instructions.load(agent.permissions).pipe(Effect.flatMap(readInitial))).text).toBe("")
}).pipe(Effect.provide(layer(() => [effect])))
})
})