diff --git a/packages/core/src/permission.ts b/packages/core/src/permission.ts index d2682097cb6..2bb84cea901 100644 --- a/packages/core/src/permission.ts +++ b/packages/core/src/permission.ts @@ -16,7 +16,7 @@ import { PluginHooks } from "./plugin/hooks.js" const PermissionEffect = Permission.Effect export { PermissionEffect as Effect } -export { Rule, Ruleset } from "@opencode/schema/permission" +export { Rule, Ruleset, deniesAll } from "@opencode/schema/permission" const missingAgentPermissions: Permission.Ruleset = [{ action: "*", resource: "*", effect: "deny" }] export const ID = Permission.ID @@ -107,6 +107,24 @@ export function merge(...rulesets: Permission.Ruleset[]): Permission.Ruleset { return rulesets.flat() } +/** + * The rules for `agent` in `session`: the agent's, then the Session's, then any that `experimental.rules` hooks append. + * Requests are evaluated against them and steps list tools, skills and MCP servers by them, so what an agent is + * offered and what it may call come from one ruleset. + */ +export const rules = Effect.fn("Permission.rules")(function* ( + hooks: PluginHooks.Interface, + input: { readonly session: SessionSchema.Info; readonly agent: Agent.Selection }, +) { + const event = yield* hooks.trigger("permission", "experimental.rules", { + sessionID: input.session.id, + agent: input.agent.id, + session: input.session.permissions ?? [], + rules: [...(input.agent.info?.permissions ?? missingAgentPermissions), ...(input.session.permissions ?? [])], + }) + return event.rules +}) + export interface Interface { readonly close: Effect.Effect readonly ask: (input: AssertInput) => Effect.Effect @@ -169,8 +187,7 @@ const layer = Layer.effect( const configured = Effect.fnUntraced(function* (sessionID: SessionSchema.ID, agentID?: Agent.ID) { const session = yield* sessions.get(sessionID) if (!session) return yield* new SessionErrors.NotFoundError({ sessionID }) - const agent = yield* agents.resolve(agentID ?? session.agent) - return merge(agent?.permissions ?? missingAgentPermissions, session.permissions ?? []) + return yield* rules(hooks, { session, agent: yield* agents.select(agentID ?? session.agent) }) }) function denied(input: Pick, rules: Permission.Ruleset) { diff --git a/packages/core/src/schedule/check.ts b/packages/core/src/schedule/check.ts index 816422d13de..754eadd4aa4 100644 --- a/packages/core/src/schedule/check.ts +++ b/packages/core/src/schedule/check.ts @@ -13,6 +13,7 @@ import { FileAccess } from "../file-access.js" import { Location } from "../location.js" import { Permission } from "../permission.js" import { Plugin } from "../plugin.js" +import { PluginHooks } from "../plugin/hooks.js" import type { SessionMessage } from "../session/message.js" import type { SessionSchema } from "../session/schema.js" import { ShellParse } from "../shell/parse.js" @@ -255,6 +256,7 @@ const runCode = Effect.fnUntraced(function* ( ) { const permission = yield* Permission.Service const agents = yield* Agent.Service + const hooks = yield* PluginHooks.Service const agent = yield* agents.select(session.agent) if (!agent.info) return { ok: false as const, error: `Agent not found: ${agent.id}` } @@ -298,7 +300,7 @@ const runCode = Effect.fnUntraced(function* ( code: condition.code, tools: paths, bindings, - permissions: Permission.merge(agent.info.permissions, session.permissions ?? []), + permissions: yield* Permission.rules(hooks, { session, agent }), limits: CODE_LIMITS, context: { sessionID: session.id, diff --git a/packages/core/src/session/context.ts b/packages/core/src/session/context.ts index eea20c76fb2..8f87859ac46 100644 --- a/packages/core/src/session/context.ts +++ b/packages/core/src/session/context.ts @@ -14,6 +14,7 @@ import { InstructionBuiltIns } from "../instructions/builtins.js" import { Location } from "../location.js" import { McpInstructions } from "../mcp/instructions.js" import { McpTool } from "../tool/mcp.js" +import { PluginHooks } from "../plugin/hooks.js" import { ReferenceInstructions } from "../reference/instructions.js" import { SkillInstructions } from "../skill/instructions.js" import { Tool } from "../tool.js" @@ -81,6 +82,7 @@ const layer = Layer.effect( const db = (yield* Database.Service).db const discovery = yield* InstructionDiscovery.Service const entries = yield* InstructionEntry.Service + const hooks = yield* PluginHooks.Service const location = yield* Location.Service const mcpInstructions = yield* McpInstructions.Service const mcpTools = yield* McpTool.Service @@ -142,8 +144,8 @@ 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 ?? []) + // The rules this agent's calls are evaluated against also decide which tools, skills and MCP servers it lists. + const permissions = yield* Permission.rules(hooks, { session, agent }) const loaded = yield* Effect.all( { tools: registry.snapshot(permissions), @@ -225,6 +227,7 @@ export const node = makeLocationNode({ Location.node, McpInstructions.node, McpTool.node, + PluginHooks.node, ReferenceInstructions.node, SessionRunnerModel.node, SessionModelRequest.node, diff --git a/packages/core/src/tool.ts b/packages/core/src/tool.ts index 30d071beb3c..91ad1246de5 100644 --- a/packages/core/src/tool.ts +++ b/packages/core/src/tool.ts @@ -17,7 +17,6 @@ import { SessionMessage } from "./session/message.js" import { SessionSchema } from "./session/schema.js" import { State } from "./state.js" import { definition, effectiveName, execute, normalizedName, normalizeContent } from "./tool/runtime.js" -import { Wildcard } from "./util/wildcard.js" export class RegistrationError extends Schema.TaggedError()("Tool.RegistrationError", { name: Schema.String, @@ -382,10 +381,8 @@ function commit(editor: Data, id: string, input: Tool.Info & { readonly id: stri editor.definitions.set(id, registered.success) } -const whollyDisabled = (action: string, rules: Permission.Ruleset) => { - const rule = rules.findLast((rule) => Wildcard.match(action, rule.action)) - return rule?.resource === "*" && rule.effect === "deny" -} +const whollyDisabled = (action: string, rules: Permission.Ruleset) => + Permission.deniesAll(action, rules, process.platform === "win32") const formatSchemaIssue = SchemaIssue.makeFormatterDefault() diff --git a/packages/plugin/src/effect/permission.ts b/packages/plugin/src/effect/permission.ts index 4b7bb71948a..20b13788f62 100644 --- a/packages/plugin/src/effect/permission.ts +++ b/packages/plugin/src/effect/permission.ts @@ -29,8 +29,23 @@ export interface PermissionEvaluation { } } +/** + * The rules for one agent in one Session. They decide its permission requests and what its steps list: a tool whose + * permission they deny outright is not listed, and neither is a skill or MCP server they deny. Rules a hook appends + * are evaluated after the Session's. Experimental: may change without compatibility guarantees. + */ +export interface PermissionRules { + readonly sessionID: Session.ID + readonly agent: Agent.ID + /** The Session's own rules, which follow the agent's in `rules`. */ + readonly session: ReadonlyArray + /** The agent's rules, then the Session's. */ + rules: Array +} + export interface PermissionHooks { readonly evaluate: PermissionEvaluation + readonly "experimental.rules": PermissionRules } export type PermissionDomain = Pick, "list" | "get" | "reply"> & { diff --git a/packages/plugin/src/promise/permission.ts b/packages/plugin/src/promise/permission.ts index a5099b243b0..dcfd4f4d915 100644 --- a/packages/plugin/src/promise/permission.ts +++ b/packages/plugin/src/promise/permission.ts @@ -27,8 +27,23 @@ export interface PermissionEvaluation { } } +/** + * The rules for one agent in one Session. They decide its permission requests and what its steps list: a tool whose + * permission they deny outright is not listed, and neither is a skill or MCP server they deny. Rules a hook appends + * are evaluated after the Session's. Experimental: may change without compatibility guarantees. + */ +export interface PermissionRules { + readonly sessionID: Session.ID + readonly agent: Agent.ID + /** The Session's own rules, which follow the agent's in `rules`. */ + readonly session: ReadonlyArray + /** The agent's rules, then the Session's. */ + rules: Array +} + export interface PermissionHooks { readonly evaluate: PermissionEvaluation + readonly "experimental.rules": PermissionRules } export type PermissionDomain = Pick & { diff --git a/packages/schema/src/permission.ts b/packages/schema/src/permission.ts index 41887ed9ea6..832f2318bc0 100644 --- a/packages/schema/src/permission.ts +++ b/packages/schema/src/permission.ts @@ -6,6 +6,7 @@ import { ephemeral, inventory } from "./event.js" import { ascending } from "./identifier.js" import { SessionID } from "./session-id.js" import { statics } from "./schema.js" +import { Wildcard } from "./wildcard.js" export const ID = Schema.String.check(Schema.isStartingWith("per")).pipe( Schema.brand("Permission.ID"), @@ -65,3 +66,12 @@ export const Rule = Schema.Struct({ export const Ruleset = Schema.Array(Rule).annotate({ identifier: "Permission.Ruleset" }) export type Ruleset = typeof Ruleset.Type + +/** + * Whether `rules` deny `action` whatever the resource: the last rule for the action denies `*`. A tool with this + * permission is not listed. A narrower deny, or an allow after it, leaves the tool listed for its calls to decide. + */ +export function deniesAll(action: string, rules: ReadonlyArray, ignoreCase: boolean) { + const rule = rules.findLast((rule) => Wildcard.match(action, rule.action, ignoreCase)) + return rule?.resource === "*" && rule.effect === "deny" +}