UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

126 lines • 5.78 kB
import { DeniedError } from "./types.js"; const authorizers = new WeakMap(); // The legacy loop passes this validated object unchanged from preflight to execution; the prepared executor runs a // frozen copy instead and moves the approval to it with transferDispatchApproval (senpi#5795). // Identical calls, including calls with duplicate provider IDs, get distinct objects. const approvals = new WeakMap(); /** Retirement cannot remove the authorizer installed by a newer extension load. */ export function registerDispatchAuthorizer(session, authorizer) { const previous = authorizers.get(session); const registration = { authorizer, retired: new AbortController() }; authorizers.set(session, registration); previous?.retired.abort(new Error("Permission authorizer was retired")); return () => { // Retain the retired registration so required authority cannot become // indistinguishable from a session where enforcement was never loaded. registration.retired.abort(new Error("Permission authorizer was retired")); }; } export function hasDispatchAuthorizer(session) { return authorizers.has(session); } /** Keep the policy and operation that were actually presented, not their post-reply replacements. */ export function prepareDispatchApproval(session, request, policy) { const registration = authorizers.get(session); if (!registration) throw new Error("Permission authorizer is unavailable"); registration.retired.signal.throwIfAborted(); const approval = { registration, toolCallId: request.toolCallId, toolName: request.toolName, input: JSON.stringify(request.input), identity: { ...request.identity }, policy: { ...policy }, }; return () => { registration.retired.signal.throwIfAborted(); if (authorizers.get(session) !== registration) throw new Error("Permission authorizer was retired"); approvals.set(request.input, approval); }; } /** * Move the approval recorded for the preflight's arguments object onto the frozen copy the tool will execute. The * approval leaves `from` in every case (that object never executes); it lands on `to` only while `to` is exactly * the input that was approved and its authorizer is not retired, so a later in-place change asks again (senpi#5795). * Never throws: anything that is not an object is a no-op. */ export function transferDispatchApproval(from, to) { if (typeof from !== "object" || from === null || typeof to !== "object" || to === null) return; const approval = approvals.get(from); if (approval === undefined) return; approvals.delete(from); if (approval.registration.retired.signal.aborted) return; if (approval.input !== JSON.stringify(to)) return; approvals.set(to, approval); } export function forgetDispatchApproval(input) { approvals.delete(input); } function matches(approval, registration, request, policy) { return (approval?.registration === registration && approval.toolCallId === request.toolCallId && approval.toolName === request.toolName && approval.input === JSON.stringify(request.input) && approval.identity.owner === request.identity.owner && approval.identity.metadata === request.identity.metadata && approval.policy.action === policy.action && approval.policy.fingerprint === policy.fingerprint); } /** Await only this caller's approval; aborting it does not retire another caller's authority. */ async function awaitApproval(pending, signal) { if (!signal) return pending; let onAbort = () => { }; const aborted = new Promise((_resolve, reject) => { onAbort = () => reject(signal.reason); signal.addEventListener("abort", onAbort, { once: true }); if (signal.aborted) onAbort(); }); try { await Promise.race([pending, aborted]); } finally { signal.removeEventListener("abort", onAbort); } } /** * Resolve live permission after MCP readiness. The returned synchronous fence must * pass immediately before dispatch; a false result requires preparation again. */ export async function authorizeToolDispatch(session, request, ctx, signal) { signal?.throwIfAborted(); const registration = authorizers.get(session); if (!registration) throw new Error("Permission authorizer is unavailable"); registration.retired.signal.throwIfAborted(); const policy = registration.authorizer.policy(request, ctx); if (policy.action === "deny") throw new DeniedError(["*"]); if (policy.action === "ask" && !matches(approvals.get(request.input), registration, request, policy)) { const approve = prepareDispatchApproval(session, request, policy); const waitSignal = signal === undefined ? registration.retired.signal : AbortSignal.any([signal, registration.retired.signal]); waitSignal.throwIfAborted(); await awaitApproval(registration.authorizer.ask(request, ctx, waitSignal), waitSignal); signal?.throwIfAborted(); approve(); } const approvedInput = JSON.stringify(request.input); return () => { if (signal?.aborted || registration.retired.signal.aborted || authorizers.get(session) !== registration) return false; if (JSON.stringify(request.input) !== approvedInput) return false; const current = registration.authorizer.policy(request, ctx); if (current.action === "deny") return false; return current.action === "allow" || matches(approvals.get(request.input), registration, request, current); }; } //# sourceMappingURL=dispatch.js.map