Files
PaperClipAI/server
Michael NguyenandClaude Opus 5.5 f3d9f141ba fix(tool-gateway): read policy context once per tool listing
A tools/list request decided access for each tool in the company with a
separate policy decision. Each decision read about 13 tables with
select *, including the full heartbeat run row and its large context
snapshot. With 900 tools, one listing made about 11,800 queries and
used about 2 GB of heap. Several listings at run start crashed the
server with out-of-memory errors.

- loadContext selects only the columns that a decision uses. It reads
  the run issue, project and routine ids from context_snapshot in SQL.
- decide accepts an optional cache for one batch of decisions. A
  listing reads the shared inputs one time: agent, run, profiles,
  profile entries, policies, grants, connections, applications and the
  company catalog. Rate-limit counters are not cached, and a decision
  that consumes a rate limit does not use the cache.
- The gateway makes one cache for each listing and runs a maximum of
  16 decisions at the same time.
- connectedMcpToolsForCompany reads each connection row one time, not
  one time for each tool. Its two reads share one read-only snapshot.

Access decisions do not change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-01 09:57:24 -07:00
..