From 728b2b60528aa09c43f97c3d15f4cc937a5eb99f Mon Sep 17 00:00:00 2001 From: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Date: Sat, 19 Sep 2026 12:59:09 -0500 Subject: [PATCH] fix(core): honor session permissions in skill and MCP discovery (#50015) --- packages/core/src/mcp/instructions.ts | 14 +++--- packages/core/src/session/context.ts | 8 ++-- packages/core/src/skill.ts | 5 +- packages/core/src/skill/instructions.ts | 11 ++--- packages/core/test/mcp-instructions.test.ts | 48 +++++++++---------- packages/core/test/session-runner.test.ts | 34 ++++++++----- packages/core/test/skill.test.ts | 2 +- packages/core/test/skill/instructions.test.ts | 22 ++++----- 8 files changed, 75 insertions(+), 69 deletions(-) diff --git a/packages/core/src/mcp/instructions.ts b/packages/core/src/mcp/instructions.ts index b582bf65884..3517b74ce9d 100644 --- a/packages/core/src/mcp/instructions.ts +++ b/packages/core/src/mcp/instructions.ts @@ -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, current: ReadonlyArray Effect.Effect + /** Lists server instructions reachable under the given ruleset; callers pass the merged agent and Session permissions. */ + readonly load: (permissions: Permission.Ruleset) => Effect.Effect } export class Service extends Context.Service()("@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 | Instructions.Removed) => Instructions.make>({ 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 [] diff --git a/packages/core/src/session/context.ts b/packages/core/src/session/context.ts index a697dd2cae3..24c71d8fe60 100644 --- a/packages/core/src/session/context.ts +++ b/packages/core/src/session/context.ts @@ -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" }, diff --git a/packages/core/src/skill.ts b/packages/core/src/skill.ts index ac502f671f9..493d1aa2923 100644 --- a/packages/core/src/skill.ts +++ b/packages/core/src/skill.ts @@ -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, agent: Agent.Info) => - skills.filter((skill) => Permission.evaluate("skill", skill.id, agent.permissions).effect !== "deny") +export const available = (skills: ReadonlyArray, permissions: Permission.Ruleset) => + skills.filter((skill) => Permission.evaluate("skill", skill.id, permissions).effect !== "deny") export const toModelOutput = (skill: Info, files: ReadonlyArray) => { const directory = path.dirname(skill.path) diff --git a/packages/core/src/skill/instructions.ts b/packages/core/src/skill/instructions.ts index 241e8f80150..14811dace4f 100644 --- a/packages/core/src/skill/instructions.ts +++ b/packages/core/src/skill/instructions.ts @@ -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, current: ReadonlyArray Effect.Effect + /** Lists skills the given ruleset does not deny; callers pass the merged agent and Session permissions. */ + readonly load: (permissions: Permission.Ruleset) => Effect.Effect } export class Service extends Context.Service()("@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 ? [] diff --git a/packages/core/test/mcp-instructions.test.ts b/packages/core/test/mcp-instructions.test.ts index 63da31156a3..906a6d78ca8 100644 --- a/packages/core/test/mcp-instructions.test.ts +++ b/packages/core/test/mcp-instructions.test.ts @@ -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( diff --git a/packages/core/test/session-runner.test.ts b/packages/core/test/session-runner.test.ts index 24dd448af91..66f1af14af0 100644 --- a/packages/core/test/session-runner.test.ts +++ b/packages/core/test/session-runner.test.ts @@ -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: [] }) diff --git a/packages/core/test/skill.test.ts b/packages/core/test/skill.test.ts index 9998bab07a3..51b1ae34b8a 100644 --- a/packages/core/test/skill.test.ts +++ b/packages/core/test/skill.test.ts @@ -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([]) }), ) }) diff --git a/packages/core/test/skill/instructions.test.ts b/packages/core/test/skill/instructions.test.ts index d13fc771fff..6af55cf3d0a 100644 --- a/packages/core/test/skill/instructions.test.ts +++ b/packages/core/test/skill/instructions.test.ts @@ -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("Effect") }).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]))) }) })