Files
PaperClipAI/server/src/__tests__/budget-enforcement-scoping.test.ts
T
HisarWebandClaude Opus 5.5 2b8ae371d3 perf(budgets): read only the needed run columns under the run lock (#20130)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - Before a run is dispatched, `reserveRunBudget` reserves budget under
the company accounting lock, the same lock that charges and policy
changes use
> - Inside that transaction it read the full `heartbeat_runs` row `FOR
UPDATE`, including `context_snapshot` and the log columns
> - Admission and native Stop recovery read only a few of those columns
> - On one instance 5 to 8 waiters queued on the company lock while this
query took up to 1.3 s; `context_snapshot` was 28 kB on average and up
to 2.8 MB
> - This pull request selects only the columns that the reservation path
reads, with the same row lock
> - The benefit is that the large columns are not detoasted or sent
while the company lock is held, so the lock is held for a shorter time

## Linked Issues or Issue Description

No issue exists. Related, but a different part of budget admission:
#17966 (skip the accounting lock for admission that cannot block) and
#18928 (queue accounting transactions).

**What happened?**
Run dispatch waited on the company accounting lock. The lock holder was
the `SELECT * ... FOR UPDATE` on `heartbeat_runs` in `reserveRunBudget`,
which read large `context_snapshot` values.

**What did you expect?**
The reservation reads only the columns it needs while it holds the
company lock.

**Steps to reproduce**
1. Create a company with a budget policy and runs with large context
snapshots.
2. Start many runs at the same time.
3. Look at `pg_stat_activity` or the query log. The `heartbeat_runs` row
read in `reserveRunBudget` returns all columns while other transactions
wait on the company lock.

**Paperclip version**
`master` at the time of this PR.

## What Changed

- `server/src/services/budget-reservations.ts`: a new
`RESERVATION_RUN_COLUMNS` with `id`, `companyId`, `agentId`,
`nativeIssueId`, `status`, `costAccountedAt`, `resultJson`,
`runtimeMode` and `usageJson`. `reserveRunBudget` selects these columns
`FOR UPDATE` instead of the full row. These are the fields that
admission and `readClaimedNativeStopRecovery` read.
- `server/src/__tests__/budget-enforcement-scoping.test.ts`: a new test
checks that the locked query selects only these columns.

## Verification

- `cd server && npx vitest run
src/__tests__/budget-enforcement-scoping.test.ts`: passes, including the
new test.
- `pnpm --filter @paperclipai/server typecheck`: passes. The type of
`run` is now the narrow select, so a later use of a column that is not
selected fails the typecheck.

## Risks

- Low risk. The row lock, the filter and the logic do not change. If a
future change in this function needs another column, it has to be added
to `RESERVATION_RUN_COLUMNS`; the typecheck shows this.

## Model Used

- Claude Opus 5.5 (`claude-opus-5-5`) in Claude Code, with tool use
(shell, git, test runs). Extended reasoning on.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [ ] All Paperclip CI gates are green
- [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 19:41:25 -06:00

287 lines
19 KiB
TypeScript

import { randomUUID } from "node:crypto";
import { and, eq, sql } from "drizzle-orm";
import { drizzle } from "drizzle-orm/postgres-js";
import { afterAll, afterEach, beforeAll, describe, expect, it } from "vitest";
import { agents, budgetPolicies, budgetReservations, companies, costEvents, createDb, heartbeatRuns, projects, type Db } from "@paperclipai/db";
import { getEmbeddedPostgresTestSupport, startEmbeddedPostgresTestDatabase } from "./helpers/embedded-postgres.js";
import { budgetService, computeObservedSpend, flushBudgetEvaluationWork } from "../services/budgets.js";
import { reserveRunBudget } from "../services/budget-reservations.js";
import { accountRunCost as accountRun } from "../services/run-cost-accounting.js";
import { persistUsageReceipt as persistReceipt, type UsageReceiptEnvelope } from "../services/usage-receipts.js";
import { configureAgentLifecycle } from "../services/agent-lifecycle.js";
const support = await getEmbeddedPostgresTestSupport();
(support.supported ? describe : describe.skip)("scoped live budget enforcement", () => {
let database: Awaited<ReturnType<typeof startEmbeddedPostgresTestDatabase>>;
let db: Db;
beforeAll(async () => {
database = await startEmbeddedPostgresTestDatabase("budget-live-scoping-");
db = createDb(database.connectionString);
}, 60_000);
afterEach(async () => { await db.transaction(async tx => {
await tx.execute(sql`SET LOCAL client_min_messages = warning`);
await tx.execute(sql`TRUNCATE companies CASCADE`);
}); });
afterAll(async () => { await database?.cleanup(); });
// The explicit worker turn separates receipt commit from budget transitions.
async function persistUsageReceipt(...args: Parameters<typeof persistReceipt>) {
const result = await persistReceipt(...args);
await flushBudgetEvaluationWork(db);
return result;
}
async function accountRunCost(...args: Parameters<typeof accountRun>) {
const result = await accountRun(...args);
await flushBudgetEvaluationWork(db);
return result;
}
async function fixture(options: { scope?: "company" | "agent" | "project"; amount?: number; reservationCents?: string } = {}) {
const companyId = randomUUID(), agentId = randomUUID(), otherAgentId = randomUUID();
const projectId = randomUUID(), runId = randomUUID(), sourceId = randomUUID();
await db.insert(companies).values({ id: companyId, name: "Budget live", issuePrefix: "BLIVE" });
await db.insert(agents).values([
{ id: agentId, companyId, name: "Running", role: "engineer", status: "active", adapterType: "process" },
{ id: otherAgentId, companyId, name: "Unrelated idle", role: "engineer", status: "idle", adapterType: "process" },
]);
await db.insert(projects).values({ id: projectId, companyId, name: "Scoped project", status: "in_progress" });
await db.insert(heartbeatRuns).values({ id: runId, companyId, agentId, invocationSource: "on_demand", status: "running",
costAccountingPending: true, usageJson: { accountingReceiptSourceId: sourceId, accountingReceiptSequence: 0,
accountingReceiptReady: false, ledgerScope: { projectId } } });
const scopeType = options.scope ?? "agent";
const [policy] = await db.insert(budgetPolicies).values({ companyId, scopeType,
scopeId: scopeType === "company" ? companyId : scopeType === "project" ? projectId : agentId,
metric: "billed_cents", windowKind: "lifetime", amount: options.amount ?? 100,
reservationCents: options.reservationCents ?? "0", notifyEnabled: false }).returning();
const readRun = async () => (await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.id, runId)))[0];
const exposure = () => computeObservedSpend(db, policy);
const receipt = (sequence: number, costUsd?: number, extra: Partial<UsageReceiptEnvelope["receipt"]> = {}): UsageReceiptEnvelope => ({
schema: "paperclip/accounting-receipt/v1", id: randomUUID(), companyId, runId, sourceId, sequence,
receivedAt: new Date().toISOString(), adapterType: "process", receipt: {
complete: false, usageBasis: "per_run", provider: "test", biller: "test", model: "budget-fixture",
billingType: "metered_api", costStatus: "unpriced", ...(costUsd === undefined ? {} : { costUsd }), ...extra,
},
});
const queuedRun = async (id = randomUUID()) => {
await db.insert(heartbeatRuns).values({ id, companyId, agentId, invocationSource: "on_demand", status: "queued" });
return id;
};
return { companyId, agentId, otherAgentId, projectId, runId, sourceId, policy, readRun, exposure, receipt, queuedRun };
}
it("blocks a known streaming overrun without charging checkpoints or touching unrelated agents", async () => {
const f = await fixture();
const [before] = await db.select().from(agents).where(eq(agents.id, f.otherAgentId));
await persistUsageReceipt(db, f.receipt(1, 0.6));
expect((await f.exposure()).totalExact).toBe("60.0000000");
expect(await budgetService(db).getInvocationBlock(f.companyId, f.agentId, { projectId: f.projectId })).toBeNull();
await persistUsageReceipt(db, f.receipt(2, 1.1));
const [agent] = await db.select().from(agents).where(eq(agents.id, f.agentId));
const [policy] = await db.select().from(budgetPolicies).where(eq(budgetPolicies.id, f.policy.id));
const [other] = await db.select().from(agents).where(eq(agents.id, f.otherAgentId));
expect(agent.lifecycleHolds).toContain("budget");
expect(policy.enforcementVersion).toBeGreaterThan(policy.enforcementDeliveredVersion);
expect(other).toEqual(before);
expect(await db.select().from(costEvents)).toHaveLength(0);
expect(await budgetService(db).getInvocationBlock(f.companyId, f.agentId, { projectId: f.projectId })).not.toBeNull();
expect((await f.exposure()).liveObservedCents).toBe("110.0000000");
});
it("replaces cumulative observations and preserves known exposure across missing, stale, and lower partial costs", async () => {
const f = await fixture({ amount: 1000 });
const first = f.receipt(1, 0.4);
await persistUsageReceipt(db, first);
await persistUsageReceipt(db, f.receipt(2, 0.7));
await persistUsageReceipt(db, first);
await persistUsageReceipt(db, f.receipt(3));
await persistUsageReceipt(db, { ...f.receipt(1, 20), sourceId: randomUUID() });
await persistUsageReceipt(db, f.receipt(4, 0.5));
expect((await f.exposure()).liveObservedCents).toBe("70.0000000");
expect((await f.readRun()).usageJson?.budgetExposure).toMatchObject({ sourceId: f.sourceId, sequence: 4, costCents: "70.0000000" });
expect(await db.select().from(costEvents)).toHaveLength(0);
});
it("counts the maximum of a reservation and live spend once at final dispatch", async () => {
const f = await fixture({ reservationCents: "40" });
await db.insert(budgetReservations).values({ companyId: f.companyId, runId: f.runId, agentId: f.agentId,
projectId: f.projectId, amountCents: "50", providerStartedAt: new Date() });
await persistUsageReceipt(db, f.receipt(1, 0.6));
expect((await f.exposure()).effectiveHeldCents).toBe("60.0000000");
const nextRunId = await f.queuedRun();
expect((await reserveRunBudget(db, f.companyId, nextRunId, f.projectId)).amountCents).toBe("40.0000000");
expect((await f.exposure()).effectiveHeldCents).toBe("100.0000000");
expect(await budgetService(db).getInvocationBlock(f.companyId, f.agentId, { projectId: f.projectId })).toBeNull();
await expect(reserveRunBudget(db, f.companyId, await f.queuedRun(), f.projectId)).rejects.toThrow("reserved");
});
it("retains each best-effort competing dispatch reservation", async () => {
const f = await fixture({ reservationCents: "60" });
const first = await f.queuedRun(), second = await f.queuedRun();
const outcomes = await Promise.allSettled([
reserveRunBudget(db, f.companyId, first, f.projectId), reserveRunBudget(db, f.companyId, second, f.projectId),
]);
const admitted = outcomes.filter(result => result.status === "fulfilled").length;
expect(admitted).toBeGreaterThanOrEqual(1);
expect((await f.exposure()).effectiveHeldCents).toBe(`${admitted * 60}.0000000`);
});
it("locks the run row without reading its large snapshot or log columns", async () => {
const f = await fixture({ reservationCents: "10" });
const runId = await f.queuedRun();
await db.update(heartbeatRuns).set({ contextSnapshot: { issueId: randomUUID(), payload: "x".repeat(65536) } })
.where(eq(heartbeatRuns.id, runId));
const queries: string[] = [];
const recorded = drizzle(db.$client, { schema: db._.fullSchema, logger: { logQuery: query => queries.push(query) } }) as unknown as Db;
await expect(reserveRunBudget(recorded, f.companyId, runId, f.projectId)).resolves.toMatchObject({ reused: false });
const runLock = queries.filter(query => /from "heartbeat_runs"/.test(query) && /for update/.test(query));
expect(runLock).toHaveLength(1);
const selected = runLock[0].slice(0, runLock[0].indexOf(' from "heartbeat_runs"')).replaceAll('"heartbeat_runs".', "");
expect([...selected.matchAll(/"(\w+)"/g)].map(match => match[1]).sort())
.toEqual(["agent_id", "company_id", "cost_accounted_at", "id", "native_issue_id", "result_json", "runtime_mode", "status", "usage_json"]);
});
it("commits a budget release while dispatch waits for asynchronous lifecycle readiness", async () => {
const f = await fixture();
const worker = configureAgentLifecycle(db, {
requiredPluginIds: async () => [], runHost: async () => "complete", runPlugin: async () => "complete",
});
try {
await persistUsageReceipt(db, f.receipt(1, 1.1));
await worker.process(f.agentId);
expect((await db.select().from(agents).where(eq(agents.id, f.agentId)))[0].lifecycleState).toBe("paused");
await db.update(budgetPolicies).set({ amount: 200 }).where(eq(budgetPolicies.id, f.policy.id));
await budgetService(db).evaluateAffectedScopes(f.companyId, f.agentId, f.projectId);
await flushBudgetEvaluationWork(db);
const nextRunId = await f.queuedRun();
await expect(reserveRunBudget(db, f.companyId, nextRunId, f.projectId)).rejects.toThrow("lifecycle");
const [resuming] = await db.select().from(agents).where(eq(agents.id, f.agentId));
expect(resuming.lifecycleHolds).not.toContain("budget");
expect(resuming.lifecycleState).toBe("resuming");
expect(await db.select().from(budgetReservations).where(eq(budgetReservations.runId, nextRunId))).toHaveLength(0);
await worker.process(f.agentId);
expect((await db.select().from(agents).where(eq(agents.id, f.agentId)))[0].lifecycleState).toBe("ready");
await expect(reserveRunBudget(db, f.companyId, nextRunId, f.projectId)).resolves.toMatchObject({ reused: false });
} finally { await worker.stop(); }
});
it("atomically replaces live exposure and its hold with one final ledger charge", async () => {
const f = await fixture({ amount: 1000 });
await db.insert(budgetReservations).values({ companyId: f.companyId, runId: f.runId, agentId: f.agentId,
projectId: f.projectId, amountCents: "50", providerStartedAt: new Date() });
await persistUsageReceipt(db, f.receipt(1, 0.6));
await persistUsageReceipt(db, f.receipt(2, 0.8, { complete: true, costStatus: "reported" }));
await db.update(heartbeatRuns).set({ status: "succeeded", finishedAt: new Date() }).where(eq(heartbeatRuns.id, f.runId));
expect(await accountRunCost(db, f.runId)).toBe(true);
expect(await accountRunCost(db, f.runId)).toBe(false);
expect(await f.exposure()).toMatchObject({ ledgerCents: "80.0000000", liveObservedCents: "0.0000000",
effectiveHeldCents: "0.0000000", totalExact: "80.0000000", pendingTerminalRunCount: 0 });
expect(await db.select().from(costEvents).where(eq(costEvents.companyId, f.companyId))).toHaveLength(1);
const [hold] = await db.select().from(budgetReservations).where(eq(budgetReservations.runId, f.runId));
expect(hold.state).toBe("settled");
});
it("keeps an over-budget hold stable while replacing live usage with a multipart final receipt", async () => {
const f = await fixture();
await persistUsageReceipt(db, f.receipt(1, 1.1));
await persistUsageReceipt(db, f.receipt(2, 1.2, { complete: true, costStatus: "reported", usage: { inputTokens: 2, outputTokens: 2 },
usageByModel: [
{ model: "first", costUsd: 0.3, usage: { inputTokens: 1, outputTokens: 1 } },
{ model: "second", costUsd: 0.9, usage: { inputTokens: 1, outputTokens: 1 } },
] }));
await db.update(heartbeatRuns).set({ status: "succeeded", finishedAt: new Date() }).where(eq(heartbeatRuns.id, f.runId));
const [before] = await db.select().from(agents).where(eq(agents.id, f.agentId));
expect(before.lifecycleHolds).toContain("budget");
await accountRunCost(db, f.runId);
const [after] = await db.select().from(agents).where(eq(agents.id, f.agentId));
expect(after.lifecycleHolds).toContain("budget");
expect(after.lifecycleVersion).toBe(before.lifecycleVersion);
expect(after.lifecycleOperation).toEqual(before.lifecycleOperation);
expect(await f.exposure()).toMatchObject({ ledgerCents: "120.0000000", liveObservedCents: "0.0000000" });
expect(await db.select().from(costEvents).where(eq(costEvents.companyId, f.companyId))).toHaveLength(2);
});
it("recognizes legacy active-run usage without a projection and excludes subscription charges", async () => {
const f = await fixture();
const [run] = await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.id, f.runId));
await db.update(heartbeatRuns).set({ usageJson: { ...run.usageJson, costUsd: 0.75, usageSource: "per_run", billingType: "metered_api" } })
.where(eq(heartbeatRuns.id, f.runId));
expect((await f.exposure()).liveObservedCents).toBe("75.0000000");
await persistUsageReceipt(db, f.receipt(1, 10, { billingType: "subscription_included" }));
expect((await f.exposure()).liveObservedCents).toBe("0.0000000");
expect(await budgetService(db).getInvocationBlock(f.companyId, f.agentId, { projectId: f.projectId })).toBeNull();
});
it("retains established per-run exposure when a later receipt has an unreconciled session basis", async () => {
const f = await fixture();
await persistUsageReceipt(db, f.receipt(1, 0.7));
await persistUsageReceipt(db, f.receipt(2, 99, { usageBasis: "session_cumulative" }));
expect((await f.exposure()).liveObservedCents).toBe("70.0000000");
expect((await f.readRun()).usageJson?.budgetExposure).toMatchObject({ costCents: "70.0000000" });
});
it("ignores out-of-range legacy numeric text without losing a usable fallback price", async () => {
const f = await fixture();
const run = await f.readRun();
await db.update(heartbeatRuns).set({ usageJson: { ...run.usageJson, usageSource: "per_run", billingType: "metered_api",
cacheAdjustedCostUsd: "1e999", costUsdExact: "1e999", costUsd: 0.25 } }).where(eq(heartbeatRuns.id, f.runId));
expect((await f.exposure()).liveObservedCents).toBe("25.0000000");
});
it("rounds each legacy run to nanodollars before aggregating its exposure", async () => {
const f = await fixture();
const secondRunId = randomUUID();
const usage = { usageSource: "per_run", billingType: "metered_api", costUsdExact: "0.0000000004" };
await db.update(heartbeatRuns).set({ usageJson: usage }).where(eq(heartbeatRuns.id, f.runId));
await db.insert(heartbeatRuns).values({ id: secondRunId, companyId: f.companyId, agentId: f.agentId,
invocationSource: "on_demand", status: "running", costAccountingPending: true, usageJson: usage });
expect((await f.exposure()).liveObservedCents).toBe("0.0000000");
await db.update(heartbeatRuns).set({ usageJson: { ...usage, costUsdExact: "0.0000000005" } })
.where(eq(heartbeatRuns.companyId, f.companyId));
expect((await f.exposure()).liveObservedCents).toBe("0.0000002");
});
it("limits project enforcement to that project while the same agent can serve another", async () => {
const f = await fixture({ scope: "project" });
const [otherProject] = await db.insert(projects).values({ companyId: f.companyId, name: "Other project", status: "in_progress" }).returning();
await persistUsageReceipt(db, f.receipt(1, 1.1));
const [agent] = await db.select().from(agents).where(eq(agents.id, f.agentId));
const [project] = await db.select().from(projects).where(eq(projects.id, f.projectId));
expect(project.pauseReason).toBe("budget");
expect(agent.lifecycleHolds).not.toContain("budget");
expect(await budgetService(db).getInvocationBlock(f.companyId, f.agentId, { projectId: f.projectId })).not.toBeNull();
expect(await budgetService(db).getInvocationBlock(f.companyId, f.agentId, { projectId: otherProject.id })).toBeNull();
await expect(reserveRunBudget(db, f.companyId, await f.queuedRun(), otherProject.id)).resolves.toMatchObject({ reused: false });
});
// Invariant 3: the missing price stays pending accounting debt, but it is
// exposure, never an admission gate.
it("keeps missing-price terminal accounting pending without holding new work", async () => {
const f = await fixture();
await db.update(heartbeatRuns).set({ status: "cancelled", finishedAt: new Date() }).where(eq(heartbeatRuns.id, f.runId));
expect(await budgetService(db).getInvocationBlock(f.companyId, f.agentId, { projectId: f.projectId })).toBeNull();
expect(await f.exposure()).toMatchObject({ pendingTerminalRunCount: 1, ledgerCents: "0.0000000", liveObservedCents: "0.0000000" });
expect(await accountRunCost(db, f.runId)).toBe(false);
expect((await f.readRun()).costAccountingPending).toBe(true);
expect(await db.select().from(costEvents).where(and(eq(costEvents.companyId, f.companyId), eq(costEvents.heartbeatRunId, f.runId)))).toHaveLength(0);
});
// Pending accounting never places a budget hold or an admission gate.
it.each(["pre-provider release", "stale pending marker"])("settles pending accounting after %s without a hold", async kind => {
const f = await fixture();
await db.update(heartbeatRuns).set({ status: "cancelled", finishedAt: new Date() }).where(eq(heartbeatRuns.id, f.runId));
expect(await budgetService(db).getInvocationBlock(f.companyId, f.agentId, { projectId: f.projectId })).toBeNull();
await budgetService(db).evaluateAffectedScopes(f.companyId, f.agentId, f.projectId);
await flushBudgetEvaluationWork(db);
const [held] = await db.select().from(agents).where(eq(agents.id, f.agentId));
expect(held.lifecycleHolds).not.toContain("budget");
await db.update(heartbeatRuns).set(kind === "pre-provider release"
? { resultJson: { executionRecovery: { providerWorkStarted: false } } }
: { costAccountedAt: new Date() }).where(eq(heartbeatRuns.id, f.runId));
expect(await accountRunCost(db, f.runId)).toBe(kind === "pre-provider release");
const [released] = await db.select().from(agents).where(eq(agents.id, f.agentId));
expect(released.lifecycleHolds).not.toContain("budget");
expect(await budgetService(db).getInvocationBlock(f.companyId, f.agentId, { projectId: f.projectId })).toBeNull();
expect((await f.readRun()).costAccountingPending).toBe(false);
expect(await db.select().from(costEvents).where(eq(costEvents.companyId, f.companyId))).toHaveLength(0);
});
});