import type { ReviewDecision, ReviewOptions } from '@enclave-vm/core/worker'; import { PublicMcpError, type FrontMcpLogger } from '@frontmcp/sdk'; import type { CodeCallBudget } from '../codecall.types'; import type { JournaledCall, ReviewCallerContext } from '../review/create-run-review'; import type { CodeCallApprovalReference, ReviewApprovals } from '../review/review-approvals'; import { NO_APPROVAL_CHANNEL } from '../review/review-rules'; import { needsOwnScript, type PendingApproval } from '../review/run-review'; import { isBlockedSelfReference } from '../security/self-reference-guard'; import type { CallReservation, CodeCallBudgetStore, ExceededBudget } from './budget-store'; export const BUDGET_EXCEEDED_RULE = 'budget-exceeded'; export const BUDGET_UNAVAILABLE_RULE = 'budget-unavailable'; /** The code of a call stopped as it entered its tool, which a script sees as `RATE_LIMITED`. */ const STOPPED_AT_DISPATCH_CODE = 'RATE_LIMIT_EXCEEDED'; const UNIT_BY_COUNT: Record = { toolCalls: 'tool calls', recordsChanged: 'records changed', }; const SUGGESTION_BY_DECISION: Record = { needs_approval: 'A person can approve this call; otherwise send it again once the window resets', denied: 'Once the window resets, send this call and the ones after it in a new script, not the calls that already ran', }; /** What a suggestion adds where the run can pause at the call: resuming it stands in for a new script. */ const IF_PAUSED = '; if the result has "paused", codecall:resume the run then instead of sending a script'; /** A script can name any string as a tool; a finding shows at most this much of it. */ const MAX_TOOL_NAME_CHARS = 128; /** How many tools that already ran a finding names. */ const MAX_NAMED_TOOLS = 10; const UNAVAILABLE_REASON = 'The budgets could not be checked, so the call did not run; a window reset does not fix this'; const REFUSED_BY_PERSON = 'a person refused to approve it'; /** What budgets answer: they never ask for changes. */ export type BudgetDecision = Extract; const ALLOW: BudgetDecision = { decision: 'allow' }; /** A call as the budgets see it: its tool, its arguments and its place in the run. */ export interface BudgetCall { toolName: string; args: unknown; callIndex?: number; } /** The budgets' answer for a call, with the approval a `needs_approval` waits for. */ export interface BudgetVerdict { decision: ReviewDecision; pending?: PendingApproval; } /** What a refusal says: its reason, or its first finding's message, with that finding's suggestion. */ export function messageOf(decision: Exclude): string { const [finding] = decision.findings ?? []; const said = 'reason' in decision ? decision.reason : (finding?.message ?? ''); return finding?.suggestion ? `${said} (${finding.suggestion})` : said; } /** Who budgets count for: the signed-in user or API key, else the verified session, else one shared anonymous bucket. */ export function budgetCallerOf(context: ReviewCallerContext): string { const { auth } = context; if (!auth.isAnonymous && auth.user.sub) return `subject:${auth.user.sub}`; const session = context.tryGetContext()?.verifiedSessionId; return session ? `session:${session}` : 'anonymous'; } function exceededDecision( { budget, resetsInSeconds }: ExceededBudget, toolName: string, deniedBecause: string | undefined, pausable: boolean, ): BudgetDecision { const shownName = toolName.slice(0, MAX_TOOL_NAME_CHARS); const decision = deniedBecause ? 'denied' : budget.onExceeded; const reason = `Budget "${budget.name}" is used up: this caller reached ${budget.limit} ${UNIT_BY_COUNT[budget.counts]} ` + `in its ${budget.windowSeconds}-second window, which resets in ${resetsInSeconds} seconds, ` + `so the call to ${shownName} did not run${deniedBecause ? `; ${deniedBecause}` : ''}`; const suggestion = `${SUGGESTION_BY_DECISION[decision]}${pausable ? IF_PAUSED : ''}`; return { decision, reason, findings: [{ rule: BUDGET_EXCEEDED_RULE, message: reason, suggestion, toolName: shownName }], }; } /** Once the store answers, only the calls that did not run may be sent again: the ones before may have had effects. */ function unavailableDecision(toolName: string, ranBefore: readonly string[], pausable: boolean): BudgetDecision { const ran = ranBefore.length > 0 ? `${ranBefore.slice(0, MAX_NAMED_TOOLS).join(', ')} already ran in this script` : ''; const before = ran || 'Calls before this one in the script may already have run'; return { decision: 'denied', reason: UNAVAILABLE_REASON, findings: [ { rule: BUDGET_UNAVAILABLE_RULE, message: UNAVAILABLE_REASON, suggestion: `${before}; once the budget store answers, send only the calls that did not run, in a new script${pausable ? IF_PAUSED : ''}`, toolName: toolName.slice(0, MAX_TOOL_NAME_CHARS), }, ], }; } /** How a person approves a call over a budget, for one run. */ export interface BudgetApprovals { approvals: ReviewApprovals; /** Approval ids a person approved during this request; a budget's is spent by the one call it lets through. */ grantedByPerson: Set; /** Whether a person can approve: the Approval plugin is installed, or the client can answer an elicitation. */ canApprove: boolean; /** The tools with effects that already ran in this run, which approving a later call would run again. */ effectsSoFar?: (starting?: JournaledCall) => readonly string[]; /** Whether the run can pause at the call at `callIndex`, so `codecall:resume` runs it without the calls before it. */ canPause?: (callIndex?: number) => boolean; } /** Gives a spent grant back. */ type Release = () => Promise; /** Spends an unspent grant for one call: its release, or undefined when another call spent it first. */ type GrantClaim = () => Promise; /** One call's use of the budgets: admitted as it enters its tool, and its records counted when it ends. */ export interface BudgetedCall { /** The members of the call's dispatch context the tool flow calls. */ readonly dispatch: { /** Throws on a count that is not a non-negative integer; a report after the call ended is counted at once. */ onRecordsChanged(count: number): void; /** As execute() starts: checks again, spends the grants the call needs and counts it, or throws to stop it. */ beforeExecute(): Promise; }; /** The budgets' answer when they stopped the call as it entered its tool. */ refusal?: BudgetVerdict; /** Counts the records the call reported, once. */ end(): Promise; } /** The budgets of one caller for one run: looked at before each call, spent and counted as it enters its tool. */ export class BudgetMeter { /** A count that could not be kept closes the rest of the run, since the stored counts are now short. */ private countLost = false; private readonly pendingByCallIndex = new Map(); constructor( private readonly store: CodeCallBudgetStore, private readonly caller: string, private readonly approval: BudgetApprovals, private readonly logger?: FrontMcpLogger, ) {} /** The approvals this run's calls wait for. */ get pendingApprovals(): Iterable { return this.pendingByCallIndex.values(); } /** The approval the call at `callIndex` waits for, when the budgets stopped it. */ pendingApprovalAt(callIndex: number | undefined): PendingApproval | undefined { return this.pendingByCallIndex.get(callIndex); } /** Early, to stop fast: allow, or a budget's answer when the call has no unspent grant over it. Spends nothing. */ async check(call: BudgetCall): Promise { if (isBlockedSelfReference(call.toolName)) return { decision: ALLOW }; if (this.countLost) return this.unavailable(call, false); try { return await this.verdictFor(call, await this.store.exceededOf(this.caller)); } catch (error: unknown) { this.warn('codecall: could not check the budgets', error); return this.unavailable(call, false); } } startCall(call: BudgetCall): BudgetedCall { let recordsChanged = 0; let ended = false; const budgetedCall: BudgetedCall = { dispatch: { onRecordsChanged: (count) => { if (!Number.isSafeInteger(count) || count < 0) { throw new RangeError(`reportRecordsChanged needs a non-negative integer, not ${String(count)}`); } if (ended) void this.countRecords(count); else recordsChanged += count; }, beforeExecute: async () => { const verdict = await this.admit(call); const { decision } = verdict; if (decision.decision === 'allow') return; budgetedCall.refusal = verdict; throw new PublicMcpError(messageOf(decision), STOPPED_AT_DISPATCH_CODE); }, }, end: async () => { if (ended) return; ended = true; if (recordsChanged > 0) await this.countRecords(recordsChanged); }, }; return budgetedCall; } /** As the call enters its tool: counted at once, then kept only with grants for every budget that leaves it over. */ private async admit(call: BudgetCall): Promise { if (this.countLost) return this.unavailable(call, true); const claimed: Release[] = []; let reservation: CallReservation | undefined; let admitted = false; try { reservation = await this.store.reserveCall(this.caller); const verdict = await this.verdictFor(call, reservation.exceeded, claimed); if (this.countLost) return this.unavailable(call, true); admitted = verdict.decision.decision === 'allow'; return verdict; } catch (error: unknown) { this.countLost = true; this.warn('codecall: could not admit a call against the budgets', error); return this.unavailable(call, true); } finally { if (!admitted) await this.giveBack([...claimed, () => reservation?.release() ?? Promise.resolve()]); } } /** The budgets' answer for a call over `exceeded`; given `claimed`, it also claims the grants it needs into it. */ private async verdictFor(call: BudgetCall, exceeded: ExceededBudget[], claimed?: Release[]): Promise { this.pendingByCallIndex.delete(call.callIndex); if (exceeded.length === 0) return { decision: ALLOW }; const pausable = this.canPauseAt(call); const denying = exceeded.find(({ budget }) => budget.onExceeded === 'denied'); if (denying) return { decision: exceededDecision(denying, call.toolName, undefined, pausable) }; if (!this.approval.canApprove) { return { decision: exceededDecision(exceeded[0], call.toolName, NO_APPROVAL_CHANNEL, pausable) }; } const grants: Array<{ over: ExceededBudget; reference: CodeCallApprovalReference; claim: GrantClaim }> = []; for (const over of exceeded) { const reference = this.approval.approvals.budgetReferenceFor(call, over.budget.name); const claim = await this.unspentGrantOf(reference); if (claim === 'denied') return { decision: exceededDecision(over, call.toolName, REFUSED_BY_PERSON, pausable) }; if (!claim) return this.waitForApproval(over, reference, call, claimed !== undefined, pausable); grants.push({ over, reference, claim }); } if (!claimed) return { decision: ALLOW }; for (const { over, reference, claim } of grants) { const release = await claim(); if (!release) return this.waitForApproval(over, reference, call, true, pausable); claimed.push(release); } return { decision: ALLOW }; } /** * A `needs_approval`, unless tools with effects already ran in a run that cannot pause there: approving would run * them again, as review answers. A run that can pause resumes from the call once a person approves it. */ private waitForApproval( over: ExceededBudget, reference: CodeCallApprovalReference, call: BudgetCall, atDispatch: boolean, pausable: boolean, ): BudgetVerdict { const repeated = pausable ? [] : (this.approval.effectsSoFar?.(atDispatch ? call : undefined) ?? []); if (repeated.length > 0) return { decision: needsOwnScript(call.toolName, repeated) }; const pending = { reference, call: { toolName: call.toolName, args: call.args } }; this.pendingByCallIndex.set(call.callIndex, pending); return { decision: exceededDecision(over, call.toolName, undefined, pausable), pending }; } private canPauseAt(call: BudgetCall): boolean { return this.approval.canPause?.(call.callIndex) === true; } /** A claim on a grant of the reference no call spent yet, `denied` when a person refused it, else undefined. */ private async unspentGrantOf(reference: CodeCallApprovalReference): Promise { const { approvals, grantedByPerson } = this.approval; if (grantedByPerson.has(reference.id)) { return async () => { if (!grantedByPerson.delete(reference.id)) return undefined; return async () => void grantedByPerson.add(reference.id); }; } const recorded = await approvals.recordedGrantsOf(reference); if (recorded === 'denied') return 'denied'; for (const { grantKey, countsUntil } of recorded) { if (await this.store.isSpent(grantKey)) continue; return async () => { const claimant = await this.store.claim(grantKey, countsUntil); return claimant === undefined ? undefined : () => this.store.release(grantKey, claimant); }; } return undefined; } /** The answer when the budgets cannot be checked, naming the tools with effects that already ran in the run. */ private unavailable(call: BudgetCall, atDispatch: boolean): BudgetVerdict { const ranBefore = this.approval.effectsSoFar?.(atDispatch ? call : undefined) ?? []; return { decision: unavailableDecision(call.toolName, ranBefore, this.canPauseAt(call)) }; } /** Gives back what an admission took; one that cannot be given back closes the rest of the run. */ private async giveBack(releases: Release[]): Promise { const results = await Promise.allSettled(releases.map((release) => release())); const failed = results.find((result) => result.status === 'rejected'); if (!failed) return; this.countLost = true; this.warn('codecall: could not give back what a refused call took from the budgets', failed.reason); } private async countRecords(count: number): Promise { try { await this.store.addRecords(this.caller, count); } catch (error: unknown) { this.countLost = true; this.warn('codecall: could not count records against the budgets', error); } } private warn(message: string, error: unknown): void { this.logger?.warn(message, { error: error instanceof Error ? error.message : String(error) }); } } /** The budgets of the caller a CodeCall tool serves, or undefined without `budgets`. */ export function budgetMeterOf( store: CodeCallBudgetStore | undefined, caller: ReviewCallerContext, approval: BudgetApprovals, logger?: FrontMcpLogger, ): BudgetMeter | undefined { return store && new BudgetMeter(store, budgetCallerOf(caller), approval, logger); } /** Enclave's review hooks with the budgets looked at first at each call, whether or not `review` is on. */ export function withBudgets( hooks: ReviewOptions | undefined, budgets: BudgetMeter | undefined, ): ReviewOptions | undefined { if (!budgets) return hooks; const reviewPre = hooks?.pre; return { ...hooks, pre: async (input) => { if (input.phase === 'call') { const { toolName, args, callIndex } = input; const { decision } = await budgets.check({ toolName, args, callIndex }); if (decision.decision !== 'allow') return decision; } return reviewPre ? reviewPre(input) : ALLOW; }, }; }