@@ -121,10 +121,11 @@ export namespace PermissionNext { |
121 | 121 | const s = await state() |
122 | 122 | const { ruleset, ...request } = input |
123 | 123 | for (const pattern of request.patterns ?? []) { |
124 | | - const action = evaluate(request.permission, pattern, ruleset, s.approved) |
125 | | - log.info("evaluated", { permission: request.permission, pattern, action }) |
126 | | - if (action === "deny") throw new RejectedError() |
127 | | - if (action === "ask") { |
| 124 | + const rule = evaluate(request.permission, pattern, ruleset, s.approved) |
| 125 | + log.info("evaluated", { permission: request.permission, pattern, action: rule }) |
| 126 | + if (rule.action === "deny") |
| 127 | + throw new AutoRejectedError(ruleset.filter((r) => Wildcard.match(request.permission, r.permission))) |
| 128 | + if (rule.action === "ask") { |
128 | 129 | const id = input.id ?? Identifier.ascending("permission") |
129 | 130 | return new Promise<void>((resolve, reject) => { |
130 | 131 | const info: Request = { |
@@ -139,7 +140,7 @@ export namespace PermissionNext { |
139 | 140 | Bus.publish(Event.Asked, info) |
140 | 141 | }) |
141 | 142 | } |
142 | | - if (action === "allow") continue |
| 143 | + if (rule.action === "allow") continue |
143 | 144 | } |
144 | 145 | }, |
145 | 146 | ) |
@@ -195,7 +196,7 @@ export namespace PermissionNext { |
195 | 196 | for (const [id, pending] of Object.entries(s.pending)) { |
196 | 197 | if (pending.info.sessionID !== sessionID) continue |
197 | 198 | const ok = pending.info.patterns.every( |
198 | | - (pattern) => evaluate(pending.info.permission, pattern, s.approved) === "allow", |
| 199 | + (pattern) => evaluate(pending.info.permission, pattern, s.approved).action === "allow", |
199 | 200 | ) |
200 | 201 | if (!ok) continue |
201 | 202 | delete s.pending[id] |
@@ -215,13 +216,13 @@ export namespace PermissionNext { |
215 | 216 | }, |
216 | 217 | ) |
217 | 218 |
|
218 | | - export function evaluate(permission: string, pattern: string, ...rulesets: Ruleset[]): Action { |
| 219 | + export function evaluate(permission: string, pattern: string, ...rulesets: Ruleset[]): Rule { |
219 | 220 | const merged = merge(...rulesets) |
220 | 221 | log.info("evaluate", { permission, pattern, ruleset: merged }) |
221 | 222 | const match = merged.findLast( |
222 | 223 | (rule) => Wildcard.match(permission, rule.permission) && Wildcard.match(pattern, rule.pattern), |
223 | 224 | ) |
224 | | - return match?.action ?? "ask" |
| 225 | + return match ?? { action: "allow", permission, pattern: "*" } |
225 | 226 | } |
226 | 227 |
|
227 | 228 | const EDIT_TOOLS = ["edit", "write", "patch", "multiedit"] |
@@ -230,19 +231,23 @@ export namespace PermissionNext { |
230 | 231 | const result = new Set<string>() |
231 | 232 | for (const tool of tools) { |
232 | 233 | const permission = EDIT_TOOLS.includes(tool) ? "edit" : tool |
233 | | - if (evaluate(permission, "*", ruleset) === "deny") { |
| 234 | + if (evaluate(permission, "*", ruleset).action === "deny") { |
234 | 235 | result.add(tool) |
235 | 236 | } |
236 | 237 | } |
237 | 238 | return result |
238 | 239 | } |
239 | 240 |
|
240 | 241 | export class RejectedError extends Error { |
241 | | - constructor(public readonly reason?: string) { |
| 242 | + constructor() { |
| 243 | + super(`The user rejected permission to use this specific tool call. You may try again with different parameters.`) |
| 244 | + } |
| 245 | + } |
| 246 | + |
| 247 | + export class AutoRejectedError extends Error { |
| 248 | + constructor(public readonly ruleset: Ruleset) { |
242 | 249 | super( |
243 | | - reason !== undefined |
244 | | - ? reason |
245 | | - : `The user rejected permission to use this specific tool call. You may try again with different parameters.`, |
| 250 | + `The user has specified a rule which prevents you from using this specific tool call. Here are some of the relevant rules ${JSON.stringify(ruleset)}`, |
246 | 251 | ) |
247 | 252 | } |
248 | 253 | } |
|