Skip to content

Commit ad26368

Browse files
committed
feat(opencode): wire permission.ask plugin hook
The `permission.ask` hook was declared in the Hooks interface but never fired from the permission engine. Plugins implementing it were silently ignored. After built-in rules evaluate to "ask", `Plugin.trigger("permission.ask")` now runs inside `Effect.ensuring` (so pending cleanup always fires). If the plugin sets status to "allow" or "deny", the request short-circuits before prompting the user. Errors fall back to normal prompt flow. Fixes anomalyco#19469 Made-with: Cursor
1 parent 7715252 commit ad26368

2 files changed

Lines changed: 131 additions & 1 deletion

File tree

‎packages/opencode/src/permission/index.ts‎

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { Database, eq } from "@/storage/db"
1111
import { Log } from "@/util/log"
1212
import { Wildcard } from "@/util/wildcard"
1313
import { Deferred, Effect, Layer, Schema, ServiceMap } from "effect"
14+
import type { Permission as PluginPermission } from "@opencode-ai/sdk"
1415
import os from "os"
1516
import z from "zod"
1617
import { evaluate as evalRule } from "./evaluate"
@@ -130,6 +131,20 @@ export namespace Permission {
130131
approved: Ruleset
131132
}
132133

134+
function toPluginPermission(info: Request): PluginPermission {
135+
return {
136+
id: String(info.id),
137+
type: info.permission,
138+
pattern: info.patterns.length <= 1 ? info.patterns[0] : info.patterns,
139+
sessionID: String(info.sessionID),
140+
messageID: info.tool?.messageID ? String(info.tool.messageID) : MessageID.make("message_permission_hook"),
141+
callID: info.tool?.callID,
142+
title: info.permission,
143+
metadata: info.metadata,
144+
time: { created: Date.now() },
145+
}
146+
}
147+
133148
export function evaluate(permission: string, pattern: string, ...rulesets: Ruleset[]): Rule {
134149
log.info("evaluate", { permission, pattern, ruleset: rulesets.flat() })
135150
return evalRule(permission, pattern, ...rulesets)
@@ -192,8 +207,30 @@ export namespace Permission {
192207
const deferred = yield* Deferred.make<void, RejectedError | CorrectedError>()
193208
pending.set(id, { info, deferred })
194209
void Bus.publish(Event.Asked, info)
210+
195211
return yield* Effect.ensuring(
196-
Deferred.await(deferred),
212+
Effect.gen(function* () {
213+
const pluginOutput = { status: "ask" as "ask" | "allow" | "deny" }
214+
yield* Effect.tryPromise(() =>
215+
import("../plugin/index").then(({ Plugin }) =>
216+
Plugin.trigger("permission.ask", toPluginPermission(info), pluginOutput),
217+
),
218+
).pipe(
219+
Effect.catch((error) => {
220+
log.warn("permission.ask plugin hook failed", { error })
221+
return Effect.void
222+
}),
223+
)
224+
225+
if (pluginOutput.status === "allow") return
226+
if (pluginOutput.status === "deny") {
227+
return yield* new DeniedError({
228+
ruleset: input.ruleset.filter((rule) => Wildcard.match(request.permission, rule.permission)),
229+
})
230+
}
231+
232+
return yield* Deferred.await(deferred)
233+
}),
197234
Effect.sync(() => {
198235
pending.delete(id)
199236
}),

‎packages/opencode/test/permission/next.test.ts‎

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { afterEach, test, expect } from "bun:test"
22
import os from "os"
3+
import path from "path"
34
import { Bus } from "../../src/bus"
45
import { Permission } from "../../src/permission"
56
import { PermissionID } from "../../src/permission/schema"
@@ -1146,3 +1147,95 @@ test("ask - abort should clear pending request", async () => {
11461147
},
11471148
})
11481149
})
1150+
1151+
// plugin permission.ask hook tests
1152+
1153+
async function writePermissionPlugin(dir: string, mode: "allow" | "deny" | "throw") {
1154+
const code = `
1155+
export default async function plugin() {
1156+
return {
1157+
"permission.ask": async (_input, output) => {
1158+
${mode === "throw" ? 'throw new Error("plugin boom")' : `output.status = "${mode}"`}
1159+
},
1160+
}
1161+
}
1162+
`
1163+
const pluginPath = path.join(dir, "test-plugin.mjs")
1164+
await Bun.write(pluginPath, code)
1165+
return pluginPath
1166+
}
1167+
1168+
test("ask - plugin permission.ask can allow without prompting", async () => {
1169+
await using tmp = await tmpdir({ git: true })
1170+
const pluginPath = await writePermissionPlugin(tmp.path, "allow")
1171+
await Bun.write(
1172+
path.join(tmp.path, "opencode.json"),
1173+
JSON.stringify({ $schema: "https://opencode.ai/config.json", plugin: [`file://${pluginPath}`] }),
1174+
)
1175+
await Instance.provide({
1176+
directory: tmp.path,
1177+
fn: async () => {
1178+
const result = await Permission.ask({
1179+
sessionID: SessionID.make("session_plugin_allow"),
1180+
permission: "bash",
1181+
patterns: ["ls"],
1182+
metadata: {},
1183+
always: [],
1184+
ruleset: [],
1185+
})
1186+
expect(result).toBeUndefined()
1187+
expect(await Permission.list()).toHaveLength(0)
1188+
},
1189+
})
1190+
})
1191+
1192+
test("ask - plugin permission.ask can deny before prompting", async () => {
1193+
await using tmp = await tmpdir({ git: true })
1194+
const pluginPath = await writePermissionPlugin(tmp.path, "deny")
1195+
await Bun.write(
1196+
path.join(tmp.path, "opencode.json"),
1197+
JSON.stringify({ $schema: "https://opencode.ai/config.json", plugin: [`file://${pluginPath}`] }),
1198+
)
1199+
await Instance.provide({
1200+
directory: tmp.path,
1201+
fn: async () => {
1202+
await expect(
1203+
Permission.ask({
1204+
sessionID: SessionID.make("session_plugin_deny"),
1205+
permission: "bash",
1206+
patterns: ["ls"],
1207+
metadata: {},
1208+
always: [],
1209+
ruleset: [],
1210+
}),
1211+
).rejects.toBeInstanceOf(Permission.DeniedError)
1212+
expect(await Permission.list()).toHaveLength(0)
1213+
},
1214+
})
1215+
})
1216+
1217+
test("ask - plugin permission.ask errors fall back to prompt flow", async () => {
1218+
await using tmp = await tmpdir({ git: true })
1219+
const pluginPath = await writePermissionPlugin(tmp.path, "throw")
1220+
await Bun.write(
1221+
path.join(tmp.path, "opencode.json"),
1222+
JSON.stringify({ $schema: "https://opencode.ai/config.json", plugin: [`file://${pluginPath}`] }),
1223+
)
1224+
await Instance.provide({
1225+
directory: tmp.path,
1226+
fn: async () => {
1227+
const ask = Permission.ask({
1228+
sessionID: SessionID.make("session_plugin_throw"),
1229+
permission: "bash",
1230+
patterns: ["ls"],
1231+
metadata: {},
1232+
always: [],
1233+
ruleset: [],
1234+
})
1235+
const pending = await waitForPending(1)
1236+
expect(pending).toHaveLength(1)
1237+
await rejectAll()
1238+
await ask.catch(() => {})
1239+
},
1240+
})
1241+
})

0 commit comments

Comments
 (0)