/** Budget counts per caller in fixed windows of shared storage: looked at before each call, spent and counted at dispatch. */ import type { PreReviewInput, ReviewDecision } from '@enclave-vm/core/worker'; import { createRootStorage, MemoryStorageAdapter, type NamespacedStorage } from '@frontmcp/utils'; import { BUDGET_EXCEEDED_RULE, BUDGET_UNAVAILABLE_RULE, budgetCallerOf, BudgetMeter, withBudgets, type BudgetCall, } from '../budgets/budget-meter'; import { CodeCallBudgetStore } from '../budgets/budget-store'; import { codeCallBudgetsOptionsSchema, type CodeCallBudget } from '../codecall.types'; import { createToolCallError, TOOL_CALL_ERROR_CODES, type ToolCallErrorCode } from '../errors'; import { effectsSoFarOf, type RunReviewCaller } from '../review/create-run-review'; import { ReviewApprovals, type ApprovalRecordView } from '../review/review-approvals'; import { RunJournal } from '../runs/run-journal'; import { pausableSoFar } from '../runs/run-pause'; import { RunReplay } from '../runs/run-replay'; const HOUR = 3600; const MAX_AGE_MS = 3_600_000; const RECORDS_PER_HOUR: CodeCallBudget = { name: 'records-per-hour', counts: 'recordsChanged', limit: 200, windowSeconds: HOUR, onExceeded: 'needs_approval', }; const CALLS_PER_MINUTE: CodeCallBudget = { name: 'calls-per-minute', counts: 'toolCalls', limit: 2, windowSeconds: 60, onExceeded: 'denied', }; const CALLS_PER_HOUR: CodeCallBudget = { ...CALLS_PER_MINUTE, name: 'calls-per-hour', onExceeded: 'needs_approval' }; const ARCHIVE: BudgetCall = { toolName: 'users.archive', args: { ids: ['t1'] }, callIndex: 2 }; async function sharedStorage(): Promise { const adapter = new MemoryStorageAdapter(); await adapter.connect(); return createRootStorage(adapter).namespace('codecall-budgets'); } function callerContext(subject: string | undefined, session?: string) { return { auth: { isAnonymous: subject === undefined, user: { sub: subject ?? '' } }, tryGetContext: () => (session ? { verifiedSessionId: session } : undefined), }; } /** The approvals of one script, with the states a person gave approval ids in the Approval plugin at `grantedAt`. */ function approvalsWith(states: Record, grantedAt: number) { const queryApprovals = async ({ toolId }: { toolId: string }): Promise => { const state = states[toolId]; return state ? [{ state, scope: 'session', grantedAt, metadata: { maxCalls: 10 } }] : []; }; return new ReviewApprovals({ scriptHash: 'script-hash', policyVersion: 'policy', maxAgeMs: MAX_AGE_MS, service: { queryApprovals }, granted: new Set(), }); } function meterFor( store: CodeCallBudgetStore, caller: string, { approvals = approvalsWith({}, Date.now()), grantedByPerson = new Set(), canApprove = true } = {}, ): BudgetMeter { return new BudgetMeter(store, caller, { approvals, grantedByPerson, canApprove }); } const referenceOf = (budget: string, call: BudgetCall = ARCHIVE) => approvalsWith({}, Date.now()).budgetReferenceFor(call, budget); async function decisionOf(meter: BudgetMeter, call: BudgetCall = ARCHIVE): Promise { return (await meter.check(call)).decision; } /** Calls that ran: each admitted as its tool starts, then ended with the records it reported. */ async function useCalls(meter: BudgetMeter, records: number, calls = 1): Promise { for (let index = 0; index < calls; index++) { const call = meter.startCall({ toolName: 'users.archive', args: { index }, callIndex: index }); await call.dispatch.beforeExecute(); call.dispatch.onRecordsChanged(records); await call.end(); } } const callInput = (toolName: string): PreReviewInput => ({ phase: 'call', toolName, args: {}, callIndex: 0, analysis: {} }) as unknown as PreReviewInput; describe('budgets options', () => { it('answers needs_approval by default and keeps counts in their own namespace', () => { const parsed = codeCallBudgetsOptionsSchema.parse({ limits: [{ name: 'records-per-hour', counts: 'recordsChanged', limit: 200, windowSeconds: HOUR }], }); expect(parsed.limits[0].onExceeded).toBe('needs_approval'); expect(parsed.namespace).toBe('codecall-budgets'); expect(parsed.storage).toEqual({ type: 'auto' }); }); it.each([ ['a repeated name', [RECORDS_PER_HOUR, RECORDS_PER_HOUR]], ['a name with a colon', [{ ...RECORDS_PER_HOUR, name: 'records:hour' }]], ['a limit of 0', [{ ...RECORDS_PER_HOUR, limit: 0 }]], ['a fractional window', [{ ...RECORDS_PER_HOUR, windowSeconds: 1.5 }]], ['another count', [{ ...RECORDS_PER_HOUR, counts: 'cost' }]], ['no budget', []], ])('refuses %s', (_, limits) => { expect(codeCallBudgetsOptionsSchema.safeParse({ limits }).success).toBe(false); }); }); describe('budgetCallerOf', () => { it('counts a signed-in user or an API key by its subject, whatever the session', () => { expect(budgetCallerOf(callerContext('user-1', 's1'))).toBe(budgetCallerOf(callerContext('user-1', 's2'))); expect(budgetCallerOf(callerContext('static:3f2a9c'))).toBe('subject:static:3f2a9c'); }); it('counts an anonymous caller by its verified session, and all anonymous callers without one together', () => { expect(budgetCallerOf(callerContext(undefined, 's1'))).toBe('session:s1'); expect(budgetCallerOf(callerContext(undefined))).toBe('anonymous'); }); }); describe('budget counts', () => { let now: number; let storage: NamespacedStorage; const clock = () => now; beforeEach(async () => { now = Date.UTC(2026, 10, 25, 10, 0, 0); storage = await sharedStorage(); }); it('stops the call after the one that crossed a recordsChanged limit, for that caller only', async () => { const store = new CodeCallBudgetStore(storage, [RECORDS_PER_HOUR], clock); const ada = meterFor(store, 'subject:ada'); await useCalls(ada, 150); expect(await decisionOf(ada)).toEqual({ decision: 'allow' }); await useCalls(ada, 80); const decision = await decisionOf(ada); expect(decision).toMatchObject({ decision: 'needs_approval', reason: expect.stringContaining('records-per-hour') }); expect(decision.findings).toEqual([ expect.objectContaining({ rule: BUDGET_EXCEEDED_RULE, toolName: 'users.archive' }), ]); expect(await decisionOf(meterFor(store, 'subject:bob'))).toEqual({ decision: 'allow' }); }); it('holds the counts across two instances sharing one storage adapter', async () => { const first = meterFor(new CodeCallBudgetStore(storage, [CALLS_PER_MINUTE], clock), 'subject:ada'); const second = meterFor(new CodeCallBudgetStore(storage, [CALLS_PER_MINUTE], clock), 'subject:ada'); await useCalls(first, 0); await useCalls(second, 0); expect(await decisionOf(first)).toMatchObject({ decision: 'denied' }); expect(await decisionOf(second)).toMatchObject({ decision: 'denied' }); }); it('starts again in the next window, and keeps a count for at least its window', async () => { const store = new CodeCallBudgetStore(storage, [CALLS_PER_MINUTE], clock); const ada = meterFor(store, 'subject:ada'); await useCalls(ada, 0, 2); now += 30_000; expect(await decisionOf(ada)).toMatchObject({ decision: 'denied', findings: [expect.objectContaining({ message: expect.stringContaining('resets in 30 seconds') })], }); const [key] = await storage.keys('*'); expect(await storage.ttl(key)).toBeGreaterThan(55); now += 30_000; expect(await decisionOf(ada)).toEqual({ decision: 'allow' }); }); it('counts a call as a tool call only once its tool started, and its records when it ends', async () => { const store = new CodeCallBudgetStore(storage, [CALLS_PER_MINUTE], clock); const ada = meterFor(store, 'subject:ada'); await ada.startCall(ARCHIVE).end(); await ada.startCall(ARCHIVE).end(); expect(await decisionOf(ada)).toEqual({ decision: 'allow' }); await useCalls(ada, 0, 2); expect(await decisionOf(ada)).toMatchObject({ decision: 'denied' }); }); it('stops a call as it enters its tool when the window filled up after the call was looked at', async () => { const store = new CodeCallBudgetStore(storage, [CALLS_PER_MINUTE], clock); const ada = meterFor(store, 'subject:ada'); await useCalls(ada, 0); expect(await decisionOf(ada)).toEqual({ decision: 'allow' }); const call = ada.startCall(ARCHIVE); await useCalls(meterFor(store, 'subject:ada'), 0); await expect(call.dispatch.beforeExecute()).rejects.toMatchObject({ code: 'RATE_LIMIT_EXCEEDED' }); expect(call.refusal?.decision).toMatchObject({ decision: 'denied' }); }); it('counts the records a call reported once when it ends, and a report after it ended as it comes', async () => { const store = new CodeCallBudgetStore(storage, [RECORDS_PER_HOUR], clock); const ada = meterFor(store, 'subject:ada'); const call = ada.startCall(ARCHIVE); call.dispatch.onRecordsChanged(150); await call.end(); await call.end(); expect(await decisionOf(ada)).toEqual({ decision: 'allow' }); call.dispatch.onRecordsChanged(60); await new Promise((resolve) => setImmediate(resolve)); expect(await decisionOf(ada)).toMatchObject({ decision: 'needs_approval' }); }); it('admits one of two calls that start together against a limit of one, and takes the other off the count', async () => { const store = new CodeCallBudgetStore(storage, [{ ...CALLS_PER_MINUTE, limit: 1 }], clock); const [first, second] = [meterFor(store, 'subject:ada'), meterFor(store, 'subject:ada')]; const admissions = await Promise.allSettled([ first.startCall(ARCHIVE).dispatch.beforeExecute(), second.startCall(ARCHIVE).dispatch.beforeExecute(), ]); expect(admissions.map(({ status }) => status).sort()).toEqual(['fulfilled', 'rejected']); const [key] = await storage.keys('calls-per-minute:*'); expect(await storage.get(key)).toBe('1'); }); it('asks for changes instead of an approval once tools with effects ran in the run, as review does', async () => { const store = new CodeCallBudgetStore(storage, [RECORDS_PER_HOUR], clock); await useCalls(meterFor(store, 'subject:ada'), 200); const ada = new BudgetMeter(store, 'subject:ada', { approvals: approvalsWith({}, Date.now()), grantedByPerson: new Set(), canApprove: true, effectsSoFar: () => ['tickets.archive'], }); const verdict = await ada.check(ARCHIVE); expect(verdict.decision).toMatchObject({ decision: 'needs_changes', findings: [expect.objectContaining({ rule: 'approval-after-effects', toolName: ARCHIVE.toolName })], }); expect(verdict.pending).toBeUndefined(); }); it.each([[-1], [1.5], [Number.NaN]])( 'refuses a report of %p records, so the call fails and no count drops', async (count) => { const store = new CodeCallBudgetStore(storage, [RECORDS_PER_HOUR], clock); const ada = meterFor(store, 'subject:ada'); await useCalls(ada, 200); const call = ada.startCall(ARCHIVE); expect(() => call.dispatch.onRecordsChanged(count)).toThrow(RangeError); await call.end(); expect(await decisionOf(ada)).toMatchObject({ decision: 'needs_approval' }); }, ); it("never stops CodeCall's reserved names, such as keep and recall", async () => { const store = new CodeCallBudgetStore(storage, [CALLS_PER_MINUTE], clock); const ada = meterFor(store, 'subject:ada'); await useCalls(ada, 0, 2); expect(await decisionOf(ada, { toolName: 'codecall:recall', args: {} })).toEqual({ decision: 'allow' }); expect(await decisionOf(ada, { toolName: 'codecall:keep', args: {} })).toEqual({ decision: 'allow' }); }); it('treats a stored count that is not a number as reached', async () => { const store = new CodeCallBudgetStore(storage, [CALLS_PER_MINUTE], clock); const ada = meterFor(store, 'subject:ada'); await useCalls(ada, 0); const [key] = await storage.keys('*'); await storage.set(key, 'not-a-count'); expect(await decisionOf(ada)).toMatchObject({ decision: 'denied' }); }); it('denies the call when the counts cannot be read', async () => { const store = new CodeCallBudgetStore(storage, [RECORDS_PER_HOUR], clock); jest.spyOn(storage, 'mget').mockRejectedValueOnce(new Error('connection refused')); expect(await decisionOf(meterFor(store, 'subject:ada'))).toMatchObject({ decision: 'denied', findings: [{ rule: BUDGET_UNAVAILABLE_RULE }], }); }); it('stops a call when the run lost a count while the call was being admitted, giving back what it took', async () => { const store = new CodeCallBudgetStore(storage, [RECORDS_PER_HOUR, CALLS_PER_HOUR], clock); const ada = meterFor(store, 'subject:ada'); const finished = ada.startCall(ARCHIVE); await finished.end(); jest.spyOn(store, 'addRecords').mockRejectedValueOnce(new Error('connection refused')); const realReserve = store.reserveCall.bind(store); jest.spyOn(store, 'reserveCall').mockImplementationOnce(async (caller) => { const reservation = await realReserve(caller); finished.dispatch.onRecordsChanged(5); await new Promise((resolve) => setImmediate(resolve)); return reservation; }); const call = ada.startCall(ARCHIVE); await expect(call.dispatch.beforeExecute()).rejects.toMatchObject({ code: 'RATE_LIMIT_EXCEEDED' }); expect(call.refusal?.decision.findings).toEqual([expect.objectContaining({ rule: BUDGET_UNAVAILABLE_RULE })]); const [callsKey] = await storage.keys('calls-per-hour:*'); expect(await storage.get(callsKey)).toBe('0'); jest.restoreAllMocks(); }); it('touches no later budget once a call is over one that denies, so two calls never push each other over', async () => { const hourly: CodeCallBudget = { ...CALLS_PER_MINUTE, name: 'calls-per-hour', limit: 1 }; const daily: CodeCallBudget = { ...CALLS_PER_MINUTE, name: 'calls-per-day', limit: 1, windowSeconds: 86400 }; const store = new CodeCallBudgetStore(storage, [hourly, daily], clock); const realIncrBy = storage.incrBy.bind(storage); let secondReserved!: () => void; const afterSecond = new Promise((resolve) => (secondReserved = resolve)); let dailyIncrements = 0; jest.spyOn(storage, 'incrBy').mockImplementation(async (key, amount) => { if (key.startsWith('calls-per-day:') && amount > 0 && dailyIncrements++ === 0) await afterSecond; return realIncrBy(key, amount); }); const first = store.reserveCall('subject:ada'); await new Promise((resolve) => setImmediate(resolve)); const second = await store.reserveCall('subject:ada'); secondReserved(); expect((await first).exceeded).toEqual([]); expect(second.exceeded.map(({ budget }) => budget.name)).toEqual(['calls-per-hour']); const [hourlyKey] = await storage.keys('calls-per-hour:*'); const [dailyKey] = await storage.keys('calls-per-day:*'); expect([await storage.get(hourlyKey), await storage.get(dailyKey)]).toEqual(['1', '1']); jest.restoreAllMocks(); }); it('counts nothing for a call a records budget that denies already stops', async () => { const records: CodeCallBudget = { ...RECORDS_PER_HOUR, onExceeded: 'denied' }; const store = new CodeCallBudgetStore(storage, [CALLS_PER_HOUR, records], clock); await useCalls(meterFor(store, 'subject:ada'), 200); const incrBy = jest.spyOn(storage, 'incrBy'); const reservation = await store.reserveCall('subject:ada'); expect(reservation.exceeded.map(({ budget }) => budget.name)).toEqual(['records-per-hour']); expect(incrBy).not.toHaveBeenCalled(); jest.restoreAllMocks(); }); it('takes back every increment a reservation made, and rejects a release that could not take one back', async () => { const daily: CodeCallBudget = { ...CALLS_PER_HOUR, name: 'calls-per-day', windowSeconds: 86400 }; const store = new CodeCallBudgetStore(storage, [CALLS_PER_HOUR, daily], clock); await (await store.reserveCall('subject:ada')).release(); const failing = await store.reserveCall('subject:ada'); jest.spyOn(storage, 'incrBy').mockRejectedValueOnce(new Error('connection refused')); await expect(failing.release()).rejects.toThrow('connection refused'); jest.restoreAllMocks(); const keys = [...(await storage.keys('calls-per-hour:*')), ...(await storage.keys('calls-per-day:*'))]; expect(await Promise.all(keys.map((key) => storage.get(key)))).toEqual(['1', '0']); }); it('closes the rest of the run when a refused call cannot be taken back off the count', async () => { const store = new CodeCallBudgetStore(storage, [CALLS_PER_HOUR], clock); const ada = meterFor(store, 'subject:ada'); await useCalls(ada, 0, 2); const realIncrBy = storage.incrBy.bind(storage); jest.spyOn(storage, 'incrBy').mockImplementation(async (key, amount) => { if (amount < 0) throw new Error('connection refused'); return realIncrBy(key, amount); }); await expect(ada.startCall(ARCHIVE).dispatch.beforeExecute()).rejects.toMatchObject({ code: 'RATE_LIMIT_EXCEEDED', }); jest.restoreAllMocks(); expect(await decisionOf(ada)).toMatchObject({ decision: 'denied', findings: [{ rule: BUDGET_UNAVAILABLE_RULE }] }); }); it('denies the rest of the run once a count could not be kept', async () => { const store = new CodeCallBudgetStore(storage, [RECORDS_PER_HOUR], clock); const ada = meterFor(store, 'subject:ada'); jest.spyOn(storage, 'incrBy').mockRejectedValueOnce(new Error('connection refused')); await useCalls(ada, 10); expect(await decisionOf(ada)).toMatchObject({ decision: 'denied' }); }); }); describe('budget approvals', () => { let grantedAt: number; let storage: NamespacedStorage; let store: CodeCallBudgetStore; const recordsReference = () => referenceOf('records-per-hour'); const approvedAs = (id: string) => approvalsWith({ [id]: 'approved' }, grantedAt); beforeEach(async () => { grantedAt = Date.now() - 600_000; storage = await sharedStorage(); store = new CodeCallBudgetStore(storage, [RECORDS_PER_HOUR]); await useCalls(meterFor(store, 'subject:ada'), 200); }); it('asks for the call over the budget with a reference shaped as a review approval, for that call only', async () => { const ada = meterFor(store, 'subject:ada'); const verdict = await ada.check(ARCHIVE); expect(verdict.decision).toMatchObject({ decision: 'needs_approval' }); expect(verdict.pending?.reference).toEqual({ id: expect.stringMatching(/^codecall:approval:[0-9a-f]{64}$/), toolName: ARCHIVE.toolName, argumentsHash: approvalsWith({}, grantedAt).referenceFor(ARCHIVE).argumentsHash, }); expect(verdict.pending?.call).toEqual({ toolName: ARCHIVE.toolName, args: ARCHIVE.args }); expect(ada.pendingApprovalAt(ARCHIVE.callIndex)).toBe(verdict.pending); expect(verdict.pending?.reference.id).not.toBe(approvalsWith({}, grantedAt).referenceFor(ARCHIVE).id); }); it('binds an approval to the call at its place in the run', async () => { const grantedByPerson = new Set([recordsReference().id]); const ada = meterFor(store, 'subject:ada', { grantedByPerson }); const firstIdenticalCall = await ada.check({ ...ARCHIVE, callIndex: 0 }); expect(firstIdenticalCall.decision).toMatchObject({ decision: 'needs_approval' }); expect(firstIdenticalCall.pending?.reference.id).not.toBe(recordsReference().id); expect(await decisionOf(ada)).toEqual({ decision: 'allow' }); }); it('looks at a grant before the call and spends it only as the call enters its tool, counting the call', async () => { const grantedByPerson = new Set([recordsReference().id]); const ada = meterFor(store, 'subject:ada', { grantedByPerson }); expect(await decisionOf(ada)).toEqual({ decision: 'allow' }); expect(grantedByPerson.size).toBe(1); await ada.startCall(ARCHIVE).dispatch.beforeExecute(); expect(grantedByPerson.size).toBe(0); expect(await decisionOf(ada)).toMatchObject({ decision: 'needs_approval' }); }); it('spends an Approval plugin grant once, never in a later run, and takes a new grant as new', async () => { const approvals = approvedAs(recordsReference().id); await meterFor(store, 'subject:ada', { approvals }).startCall(ARCHIVE).dispatch.beforeExecute(); expect(await decisionOf(meterFor(store, 'subject:ada', { approvals }))).toMatchObject({ decision: 'needs_approval', }); const regranted = approvalsWith({ [recordsReference().id]: 'approved' }, grantedAt + 1); expect(await decisionOf(meterFor(store, 'subject:ada', { approvals: regranted }))).toEqual({ decision: 'allow' }); }); it('lets only one of two concurrent runs on instances sharing a store spend one grant', async () => { const second = new CodeCallBudgetStore(storage, [RECORDS_PER_HOUR]); const approvals = approvedAs(recordsReference().id); const admissions = await Promise.allSettled( [store, second].map((instance) => meterFor(instance, 'subject:ada', { approvals }).startCall(ARCHIVE).dispatch.beforeExecute(), ), ); expect(admissions.map(({ status }) => status).sort()).toEqual(['fulfilled', 'rejected']); }); describe('a call over two budgets, with an Approval plugin grant for each', () => { let both: CodeCallBudgetStore; let bob: BudgetMeter; let recordsGrant: string; let callsGrant: string; const callsId = () => referenceOf('calls-per-hour').id; beforeEach(async () => { both = new CodeCallBudgetStore(storage, [RECORDS_PER_HOUR, CALLS_PER_HOUR]); await useCalls(meterFor(both, 'subject:bob'), 100, 2); const approvals = approvalsWith({ [recordsReference().id]: 'approved', [callsId()]: 'approved' }, grantedAt); bob = meterFor(both, 'subject:bob', { approvals }); recordsGrant = `${recordsReference().id}:${grantedAt}`; callsGrant = `${callsId()}:${grantedAt}`; }); afterEach(() => { jest.restoreAllMocks(); }); it('gives back the grant it claimed when another call wins the next one', async () => { const realClaim = both.claim.bind(both); const claim = jest.spyOn(both, 'claim').mockImplementationOnce(realClaim).mockResolvedValueOnce(undefined); const call = bob.startCall(ARCHIVE); await expect(call.dispatch.beforeExecute()).rejects.toMatchObject({ code: 'RATE_LIMIT_EXCEEDED' }); expect(claim).toHaveBeenCalledTimes(2); expect(call.refusal?.pending?.reference.id).toBe(callsId()); expect(await both.isSpent(recordsGrant)).toBe(false); }); it('gives back the grant it claimed when the store fails on the next claim', async () => { const realClaim = both.claim.bind(both); const claim = jest .spyOn(both, 'claim') .mockImplementationOnce(realClaim) .mockRejectedValueOnce(new Error('connection refused')); const call = bob.startCall(ARCHIVE); await expect(call.dispatch.beforeExecute()).rejects.toMatchObject({ code: 'RATE_LIMIT_EXCEEDED' }); expect(claim).toHaveBeenCalledTimes(2); expect(call.refusal?.decision.findings).toEqual([expect.objectContaining({ rule: BUDGET_UNAVAILABLE_RULE })]); expect(await both.isSpent(recordsGrant)).toBe(false); }); it('takes the call back off the count and spends no grant when the store fails while counting it', async () => { jest.spyOn(storage, 'extendTtl').mockRejectedValueOnce(new Error('connection refused')); const call = bob.startCall(ARCHIVE); await expect(call.dispatch.beforeExecute()).rejects.toMatchObject({ code: 'RATE_LIMIT_EXCEEDED' }); const [callsKey] = await storage.keys('calls-per-hour:*'); expect(await storage.get(callsKey)).toBe('2'); expect(await both.isSpent(recordsGrant)).toBe(false); expect(await both.isSpent(callsGrant)).toBe(false); }); it('takes the call back off the count when it is refused for a grant it lacks', async () => { jest.spyOn(both, 'claim').mockResolvedValue(undefined); const call = bob.startCall(ARCHIVE); await expect(call.dispatch.beforeExecute()).rejects.toMatchObject({ code: 'RATE_LIMIT_EXCEEDED' }); const [callsKey] = await storage.keys('calls-per-hour:*'); expect(await storage.get(callsKey)).toBe('2'); }); it('spends both grants and counts the call when it can', async () => { await bob.startCall(ARCHIVE).dispatch.beforeExecute(); expect(await both.isSpent(recordsGrant)).toBe(true); expect(await both.isSpent(callsGrant)).toBe(true); }); }); it('spends no grant of a person until it has one for each budget the call is over', async () => { const both = new CodeCallBudgetStore(storage, [RECORDS_PER_HOUR, CALLS_PER_HOUR]); const recordsId = recordsReference().id; const callsId = referenceOf('calls-per-hour').id; const grantedByPerson = new Set([recordsId]); await useCalls(meterFor(both, 'subject:bob'), 100, 2); const bob = meterFor(both, 'subject:bob', { grantedByPerson }); const verdict = await bob.check(ARCHIVE); expect(verdict.decision).toMatchObject({ decision: 'needs_approval', reason: expect.stringContaining('calls-per'), }); expect(verdict.pending?.reference.id).toBe(callsId); grantedByPerson.add(callsId); await bob.startCall(ARCHIVE).dispatch.beforeExecute(); expect(grantedByPerson.size).toBe(0); }); it("refuses a claim after the grant's deadline, and marks a spent grant no longer than it counts", async () => { const fixed = new CodeCallBudgetStore(storage, [RECORDS_PER_HOUR], () => grantedAt + 600_000); expect(await fixed.claim('expired-grant', grantedAt + 600_000)).toBeUndefined(); const claims = await Promise.all([ fixed.claim('grant-1', grantedAt + MAX_AGE_MS), fixed.claim('grant-1', grantedAt + MAX_AGE_MS), ]); expect(claims.filter((claimant) => claimant !== undefined)).toHaveLength(1); const [marker] = await storage.keys('grant.spent:*'); expect(await storage.ttl(marker)).toBeLessThanOrEqual(3000); expect(await storage.ttl(marker)).toBeGreaterThanOrEqual(2999); }); it('never lifts a budget with a destructive-call approval, nor approves a destructive call with a budget one', async () => { const reviewReference = approvalsWith({}, grantedAt).referenceFor(ARCHIVE); const approvals = approvalsWith( { [reviewReference.id]: 'approved', [reviewReference.anyArgumentsId as string]: 'approved' }, grantedAt, ); expect(await decisionOf(meterFor(store, 'subject:ada', { approvals }))).toMatchObject({ decision: 'needs_approval', }); expect(await approvedAs(recordsReference().id).stateOf(reviewReference, false)).toBe('missing'); }); it('denies a call a person refused to approve over the budget', async () => { const approvals = approvalsWith({ [recordsReference().id]: 'denied' }, grantedAt); expect(await decisionOf(meterFor(store, 'subject:ada', { approvals }))).toMatchObject({ decision: 'denied', reason: expect.stringContaining('a person refused to approve it'), }); }); it('denies a call over the budget when nobody can approve it', async () => { const verdict = await meterFor(store, 'subject:ada', { canApprove: false }).check(ARCHIVE); expect(verdict.decision).toMatchObject({ decision: 'denied', reason: expect.stringContaining('nobody can approve it here'), }); expect(verdict.pending).toBeUndefined(); }); }); describe('a call stopped as it starts, with the run journal', () => { let store: CodeCallBudgetStore; let journal: RunJournal; beforeEach(async () => { store = new CodeCallBudgetStore(await sharedStorage(), [CALLS_PER_HOUR]); await useCalls(meterFor(store, 'subject:ada'), 0, 2); journal = new RunJournal(); }); const effectsSoFar = () => effectsSoFarOf({ journal } as unknown as RunReviewCaller, () => false); const meterWithJournal = () => new BudgetMeter(store, 'subject:ada', { approvals: approvalsWith({}, Date.now()), grantedByPerson: new Set(), canApprove: true, effectsSoFar: effectsSoFar(), }); it("asks for an approval for the run's first call, whose own unfinished entry is not an earlier effect", async () => { journal.begin(ARCHIVE.toolName, ARCHIVE.args); const call = meterWithJournal().startCall(ARCHIVE); await expect(call.dispatch.beforeExecute()).rejects.toMatchObject({ code: 'RATE_LIMIT_EXCEEDED' }); expect(call.refusal?.decision).toMatchObject({ decision: 'needs_approval' }); expect(call.refusal?.pending?.reference.toolName).toBe(ARCHIVE.toolName); }); it('does not take a call stopped as it starts for pausable once a later call of a parallel() ran with effects', async () => { const replay = new RunReplay(); replay.appliedByRuntime(true); const pausable = (laterCallRan: boolean) => { journal = new RunJournal({ keepResults: true, replay }); journal.begin(ARCHIVE.toolName, ARCHIVE.args, 0); if (laterCallRan) journal.begin('tickets.delete', { id: 1 }, 1).succeed({ deleted: 1 }); return new BudgetMeter(store, 'subject:ada', { approvals: approvalsWith({}, Date.now()), grantedByPerson: new Set(), canApprove: true, effectsSoFar: effectsSoFar(), canPause: (callIndex) => pausableSoFar(journal, callIndex), }).startCall({ ...ARCHIVE, callIndex: 0 }); }; const alone = pausable(false); const beside = pausable(true); await expect(alone.dispatch.beforeExecute()).rejects.toMatchObject({ code: 'RATE_LIMIT_EXCEEDED' }); await expect(beside.dispatch.beforeExecute()).rejects.toMatchObject({ code: 'RATE_LIMIT_EXCEEDED' }); expect(alone.refusal?.decision).toMatchObject({ decision: 'needs_approval' }); expect(beside.refusal?.decision).toMatchObject({ decision: 'needs_changes', findings: [ expect.objectContaining({ rule: 'approval-after-effects', message: expect.stringContaining('tickets.delete') }), ], }); }); it('asks for changes when a tool with effects already reached its tool earlier in the run', async () => { journal.begin('tickets.delete', { id: 1 }).succeed({ deleted: 1 }); journal.begin(ARCHIVE.toolName, ARCHIVE.args); const call = meterWithJournal().startCall(ARCHIVE); await expect(call.dispatch.beforeExecute()).rejects.toMatchObject({ code: 'RATE_LIMIT_EXCEEDED' }); expect(call.refusal?.decision).toMatchObject({ decision: 'needs_changes', findings: [ expect.objectContaining({ rule: 'approval-after-effects', message: expect.stringContaining('tickets.delete') }), ], }); expect(call.refusal?.pending).toBeUndefined(); }); it('names the tools with effects that ran when the store fails as a call starts, and says to send only the rest', async () => { journal.begin('tickets.delete', { id: 1 }).succeed({ deleted: 1 }); journal.begin(ARCHIVE.toolName, ARCHIVE.args); jest.spyOn(store, 'reserveCall').mockRejectedValueOnce(new Error('connection refused')); const call = meterWithJournal().startCall(ARCHIVE); const failure = await call.dispatch.beforeExecute().catch((error: Error) => error); const suggestion = 'tickets.delete already ran in this script; once the budget store answers, send only the calls that did not run, in a new script'; expect(call.refusal?.decision.findings).toEqual([ expect.objectContaining({ rule: BUDGET_UNAVAILABLE_RULE, suggestion }), ]); expect(failure).toMatchObject({ code: 'RATE_LIMIT_EXCEEDED', message: expect.stringContaining(suggestion) }); jest.restoreAllMocks(); }); it('names earlier effects at the early check too, and says calls before may have run when none are known', async () => { journal.begin('tickets.delete', { id: 1 }).succeed({ deleted: 1 }); jest.spyOn(store, 'exceededOf').mockRejectedValueOnce(new Error('connection refused')); const named = await meterWithJournal().check(ARCHIVE); journal = new RunJournal(); jest.spyOn(store, 'exceededOf').mockRejectedValueOnce(new Error('connection refused')); const unnamed = await meterWithJournal().check(ARCHIVE); expect(named.decision.findings?.[0]?.suggestion).toMatch(/^tickets\.delete already ran in this script; /); expect(unnamed.decision.findings?.[0]?.suggestion).toBe( 'Calls before this one in the script may already have run; once the budget store answers, send only the calls that did not run, in a new script', ); jest.restoreAllMocks(); }); it('leaves out only the starting call, not an identical call running beside it', () => { journal.begin(ARCHIVE.toolName, ARCHIVE.args); journal.begin(ARCHIVE.toolName, ARCHIVE.args); expect(effectsSoFar()(ARCHIVE)).toEqual([ARCHIVE.toolName]); journal.close(); expect(effectsSoFar()()).toEqual([ARCHIVE.toolName]); }); it('counts a tool whose result was refused once it ran, and not one a rate limit stopped', () => { const failed = (tool: string, code: ToolCallErrorCode) => journal.begin(tool, {}).fail(createToolCallError(code, tool)); failed('tickets.export', TOOL_CALL_ERROR_CODES.RESULT_TOO_LARGE); failed('tickets.close', TOOL_CALL_ERROR_CODES.RATE_LIMITED); expect(effectsSoFar()()).toEqual(['tickets.export']); }); }); describe('withBudgets', () => { it('asks the budgets before the review at each call, and only the review for the script', async () => { const store = new CodeCallBudgetStore(await sharedStorage(), [{ ...CALLS_PER_MINUTE, limit: 1 }]); const ada = meterFor(store, 'subject:ada'); const reviewPre = jest.fn(async (): Promise => ({ decision: 'allow' })); const hooks = withBudgets({ pre: reviewPre, timeoutMs: 5000 }, ada); expect(hooks?.timeoutMs).toBe(5000); expect(await hooks?.pre?.(callInput('users.get'))).toEqual({ decision: 'allow' }); await useCalls(ada, 0); expect(await hooks?.pre?.(callInput('users.get'))).toMatchObject({ decision: 'denied' }); expect(reviewPre).toHaveBeenCalledTimes(1); }); it('is the budgets alone without review, and the review alone without budgets', async () => { const review = { pre: async (): Promise => ({ decision: 'allow' }) }; const ada = meterFor(new CodeCallBudgetStore(await sharedStorage(), [CALLS_PER_MINUTE]), 'subject:ada'); expect(await withBudgets(undefined, ada)?.pre?.(callInput('users.get'))).toEqual({ decision: 'allow' }); expect(withBudgets(review, undefined)).toBe(review); expect(withBudgets(undefined, undefined)).toBeUndefined(); }); });