mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-29 11:42:17 +00:00
fix(core): list tools and evaluate calls from one ruleset
Before, a step listed tools, skills and MCP servers from
merge(agent, session) built in SessionContext.select, while calls were
evaluated from the same merge built separately in Permission. A plugin
that changed how calls were decided (the browser plugin's evaluate
hook) could not change what was listed, so the two disagreed.
Now Permission.rules(hooks, { session, agent }) is the only place that
builds the ruleset. Permission's evaluation (assert, ask,
assertNotDenied, allows), SessionContext.select (tool snapshot, Code
Mode catalog and search, skill and MCP instructions) and scheduled
code checks all use it.
Permission.rules triggers a new experimental plugin hook,
permission "experimental.rules". The hook gets the session ID, the
agent ID, the session's own rules (read-only), and the merged rules
(agent, then session). The hook can append rules, which are evaluated
last. Core does not restrict what a hook writes. Hooks are trusted in
the same way as permission.evaluate hooks, which can already set any
effect. With no hook registered, the result is the same as the old
merge.
Agent resolution changes from agents.resolve to agents.select. This
gives the same agent info, and the hook gets an agent ID even when the
default agent is used. An agent that cannot be resolved still gets
the deny-all rule.
The tool-hiding predicate (the last rule matching the action is a deny
on "*") moves to @opencode/schema/permission as deniesAll. Plugins can
then apply the same predicate as Tool.snapshot. Tool.snapshot and
Tool.check use it with the same matching as before (case-insensitive
on Windows).
This commit is contained in:
parent
9e20e99a4f
commit
085dee256b
7 changed files with 70 additions and 11 deletions
|
|
@ -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<void>
|
||||
readonly ask: (input: AssertInput) => Effect.Effect<AskResult, SessionErrors.NotFoundError>
|
||||
|
|
@ -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<Request, "action" | "resources">, rules: Permission.Ruleset) {
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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<RegistrationError>()("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()
|
||||
|
||||
|
|
|
|||
|
|
@ -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<Permission.Rule>
|
||||
/** The agent's rules, then the Session's. */
|
||||
rules: Array<Permission.Rule>
|
||||
}
|
||||
|
||||
export interface PermissionHooks {
|
||||
readonly evaluate: PermissionEvaluation
|
||||
readonly "experimental.rules": PermissionRules
|
||||
}
|
||||
|
||||
export type PermissionDomain = Pick<PermissionApi<unknown>, "list" | "get" | "reply"> & {
|
||||
|
|
|
|||
|
|
@ -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<Permission.Rule>
|
||||
/** The agent's rules, then the Session's. */
|
||||
rules: Array<Permission.Rule>
|
||||
}
|
||||
|
||||
export interface PermissionHooks {
|
||||
readonly evaluate: PermissionEvaluation
|
||||
readonly "experimental.rules": PermissionRules
|
||||
}
|
||||
|
||||
export type PermissionDomain = Pick<PermissionApi, "list" | "get" | "reply"> & {
|
||||
|
|
|
|||
|
|
@ -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<Rule>, ignoreCase: boolean) {
|
||||
const rule = rules.findLast((rule) => Wildcard.match(action, rule.action, ignoreCase))
|
||||
return rule?.resource === "*" && rule.effect === "deny"
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue