Files
PaperClipAI/server/src/__tests__
Devin FoleyandPaperclip 1bbf734ce6 fix(connections): avoid timeouts when saving agent access (#23790)
## Thinking Path

> - Paperclip manages agents and the connections they can use.
> - A connection's Permissions page saves its actions and agent audience
together.
> - The save checks the saved profile, its agent targets, and related
accounts.
> - Those checks repeat database reads for each target and action. They
also expand the same profile for each related account.
> - On a large company, the request can time out before it reaches the
save transaction.
> - This change bounds that work and keeps the existing permission
decisions and reauthorization boundaries.

## Linked Issues or Issue Description

**What happened?**

Adding an agent to an existing connection leaves the page at “Saving…”
until the request fails. Refreshing shows the old audience. The browser
reproduction ended with HTTP 502 at the five-minute request deadline.

**Expected behavior**

Save an authorized agent-audience change and return the saved
permissions without repeated checks across the full company and catalog.

**Steps to reproduce**

Use a company with many agents, several accounts under one application,
and an existing connection profile with many catalog entries. Add an
agent on the connection's Permissions page. The new regression covers 40
additional agents, three accounts, and 50 actions through the assembled
server and real PostgreSQL.

GitHub searches found no duplicate fix.

## What Changed

- Share membership and grant reads inside one authorization check.
Retain the existing two-second age limit. Close the scope before the
handler runs and start a fresh scope for each reauthorization.
- Resolve catalog references and canonical agent target IDs in batches.
Keep the ordinary target lookup outside the check scope and for other
UUID spellings. Continue to decide permissions for every target.
- Keep the already checked profile in each related account's permission
scope without expanding that profile again. Continue to check changed
profiles, gateway bindings, and the referenced accounts.
- Add a save regression with SQL-count bounds and a subsequent
grant-revocation refusal. Cover nested, completed, and failed
authorization-check scopes.

## Verification

- `cd server && pnpm run typecheck` — passed, including the test-tree
layers. Repeated successfully after rebasing onto current `master`.
- `cd server && pnpm exec vitest run
src/__tests__/connection-management.integration.test.ts
src/__tests__/connection-management-delegated-policy.integration.test.ts
src/__tests__/connection-management-gateway-rules.integration.test.ts
src/__tests__/connection-management-delegated-tests.integration.test.ts
src/__tests__/connection-management-providers.integration.test.ts
src/__tests__/connection-management-gateways.integration.test.ts
src/__tests__/authorization-request-reads.test.ts` — 7 files and 110
tests passed.
- After rebase, repeated both
`connection-management.integration.test.ts` and
`authorization-request-reads.test.ts` on head `fc8b3ecf16cc`: 2 files
and 31 tests passed. The 110-test run above preceded the rebase.
- Red/green: the new HTTP regression first failed on 2,218 membership
reads. It now saves the requested audience, retains all 50 actions,
meets the query bounds, and refuses the next request after its grant
scope is restricted.
- Read-only replay of the affected authorization path completed with the
fix. No tenant database rows were changed by the replay.
- `node scripts/check-test-budget.mjs --files
server/src/__tests__/connection-management.integration.test.ts
server/src/__tests__/authorization-request-reads.test.ts` — passed.
- `git diff --check` and Gitleaks scan of the diff — passed. The diff
was also checked for private identifiers and personal data.
- `pnpm verify` selected 2,008 test files through the shared
authorization dependencies. That broad local run was stopped; it is not
a pass. The focused suites and server typecheck above passed. Required
CI must supply the broad checks on this revision.
- Final head `fc8b3ecf16cc`: [CI
run](https://github.com/paperclipai/paperclip/actions/runs/47445309988)
passed, including typecheck, build, all general and serialized server
shards, native Runner checks, and all end-to-end shards. [Greptile
review](https://github.com/paperclipai/paperclip/pull/23790#issuecomment-7426956976)
scored 5/5 with no actionable issues or unresolved threads. GitHub
reports the branch mergeable with a clean merge state.
- The fix has not been deployed. A live browser save and refresh on the
new release remain to be verified after deployment.

## Risks

No schema, API, or deployment-policy change. Shared authorization code
is affected. Reuse is limited to one check with the existing age limit;
each pre-write reauthorization reads fresh facts. The related-account
checks still include profile IDs in their permission scopes. Existing
delegated, scoped-grant, company-isolation, and gateway tests pass.
Revert this commit to roll back.

## Model Used

OpenAI GPT-6.1 Sol (`gpt-6.1-sol`) through Codex in T3 Code, with xhigh
reasoning and tool use.

## 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 (the
public contract is unchanged; the internal boundaries and validation are
recorded above)
- [x] I have considered and documented any risks above
- [x] All Paperclip CI gates are green
- [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-12-04 15:35:22 -08:00
..
…

Server Tests

Server tests that need a real PostgreSQL process must use ./helpers/embedded-postgres.ts instead of constructing embedded-postgres directly.

The shared helper creates a throwaway data directory and a reserved-safe loopback port for each test database. This protects the live Paperclip control-plane Postgres from server vitest runs; see PAP-2033 for the incident that introduced this guard.