Files
PaperClipAI/server
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
..