Files
PaperClipAI/doc/connection-review-counts.md
T
DottaandPaperclip 58308a3e63 perf(apps): scope sidebar review counts to one connection (#18688)
## Thinking Path

> - Paperclip helps people manage AI agents and their governed app
access.
> - The Apps sidebar displays a Review badge for one connection.
> - It polls the full company attention service every 30 seconds to get
two counts.
> - That service also loads every profile, rule and active catalog
entry.
> - This pull request adds an authorized connection read for only those
counts.
> - The same badge now avoids unrelated profile evaluation and database
result decoding.

## Linked Issues or Issue Description

**Current behavior**

The contextual sidebar polls company-wide attention. It needs only one
connection's signed pending action count and whether any catalog entries
are quarantined.

**Proposed behavior**

Both sidebar variants poll a connection-scoped count endpoint with
existing connection read authority. The full attention API stays
unchanged.

**Reason and benefit**

The retained 200-profile fixture performs seven service queries and
returns 80,115 rows for this badge. The new reader performs one query
and returns one row. This reduces local service CPU and avoids
unnecessary decoded profile/rule data. Related merged evaluator fixes
are already in the baseline: Refs #18673, Refs #18681. A search found no
existing PR for this scoped count reader. ROADMAP.md contains no
duplicate planned work.

## What Changed

- Add the narrow shared DTO, typed client, guarded REST/OpenAPI route
and catalog-owned reader.
- Count independent aggregates with both company boundaries and exact
signed-argument semantics. Keep archived counts at zero.
- Update both contextual sidebar variants. Keep the exact badge, 30
second interval and existing attention-prefix invalidation.
- Add PostgreSQL differential tests, authenticated HTTP tests, sidebar
tests, a repeatable benchmark and access documentation.

## Verification

- Baseline: `3f8ebfff8aa9febd0733c6f65b07485dcadfd0e2`, including both
merged evaluator fixes. Service treatment: `0827f7d0b3`; route/fixture
refinement: `230c42ee15`; final local existence gate: `390a218254`.
- Passed: focused PostgreSQL differential test; assembled HTTP test with
board session and agent JWT; 14 sidebar component tests; token gates;
map checks; diff check.
- Fixture: 200 profiles, 76,000 rules, 1,900 active tools, 695 removed
references, 12 agents, a sibling connection and 2,000 unrelated
historical invocations. Complete normalized two-count results are equal
on both paths.
- Quiet local measurement: seven alternating warm pairs after two warmup
pairs, Node 24.22.2, macOS, real disposable PostgreSQL. Legacy/new
service CPU median: 213.891/1.680 ms; ranges:
209.329–228.552/0.515–6.137 ms. Wall median: 156.867/1.248 ms; ranges:
151.739–162.289/1.115–1.386 ms. Queries: 7/1. Returned rows: 80,115/1.
- Run `cd server && node --expose-gc --import
./node_modules/tsx/dist/loader.mjs
scripts/benchmark-connection-review-counts.ts`. See
`doc/connection-review-counts.md` for scope and failure history.
Byte-volume accounting runs outside timing; no HTTP payload reduction is
claimed.
- Initial raw scalar SQL failed with ambiguous columns. Typed subqueries
fixed it and passed real PostgreSQL equality. Initial fixture agents
lacked permissions and correctly received 403. The final fixture grants
explicit authority and tests revocation. An initial benchmark included
byte serialization in timing; the reported run excludes it.
- Passed required local `pnpm -r typecheck` and `pnpm build`. The
initial sandbox typecheck could not open the tsx IPC socket; the
escalated rerun passed. Full monolithic `pnpm test:run` was not run
locally: repeating a 15–25 minute suite serially across both performance
PRs adds no distinct evidence. Broad suite acceptance uses final-head
CI, which runs the same general/serialized test partitions and the
native Runner lane. Final-head CI is in progress.
- Passed built-static Chromium journey on an owned local-trusted server
and disposable PostgreSQL: Review 3 appears, only scoped counts are
requested by this sidebar, resolving requests/clearing quarantines then
reloading removes the badge, and archived counts are zero. The first ad
hoc launcher omitted required decision-service options and failed at
startup; using the normal no-op wake callback fixed that harness.
- Greptile identified an invalid resolved-request fixture label.
Corrected `denied` to the declared `rejected`, then reran the real
PostgreSQL differential test. The count SQL and non-pending exclusion
are unchanged.
- The first final-head CI failed the static cross-tenant
existence-oracle guard because the new explicit resource company
assertion had no nearby `hasCompanyAccess` gate. Existing management
admission already rejects foreign resources with 404. The route now also
follows the standard local two-step gate. The unchanged guard plus
authenticated HTTP negative cases passed (3 tests). No allowlist or test
was weakened.
- These local service measurements do not prove hosted performance. No
deployment is included.

## Risks

Low risk. New read admission reuses existing current-authority checks.
The old attention API is unchanged. Counts still require database work
over relevant pending/catalog rows. No cache, schema migration or
telemetry is added. Roll back this PR if the badge differs or scoped
reads deny previously permitted users.

## Model Used

OpenAI `gpt-6.1-sol` (Sol 6.1), with reasoning, tool use and code
execution. The host does not expose the configured context-window size.

## 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

---------

Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-11-02 17:12:22 -06:00

5.8 KiB
Raw Blame History

Connection review counts

The contextual App sidebar uses GET /api/tool-connections/:connectionId/review-counts for its Review badge. The response contains pendingActionRequestCount and quarantinedCatalogEntryCount. The badge adds the pending count and one when the quarantine count is positive. Both sidebar variants keep the 30 second cadence. The key is a child of the existing company attention prefix so current mutations and events still invalidate it.

The route uses existing connection read admission. It resolves UUIDs and UIDs, checks company and current actor/responsible-user authority, and enforces personal account restrictions before reading counts. An authorized archived connection returns zero counts, as its absent legacy attention item did. Missing and hidden connections retain normal admission errors. This read adds no execution authority.

The catalog owner reads two independent scalar aggregates in one SQL statement. Pending counts join requests to invocations by invocation ID and constrain both company IDs. SQL NULL signed arguments are excluded; empty signed text counts. Multiple requests for one invocation count separately. No profile, rule or active catalog projection runs for this read. The company-wide attention API is unchanged.

Local performance proof (2026-11-02)

Baseline: 3f8ebfff8aa9febd0733c6f65b07485dcadfd0e2 on current master, including both prior evaluator fixes (#18673 and #18681). Treatment is the count reader in this change. Node v24.22.2, macOS, disposable real embedded PostgreSQL, one database connection, no cross-request cache. The comparison calls the actual legacy attention service and selects one connection's two counts, then calls the new reader. All normalized results equal {pendingActionRequestCount: 2, quarantinedCatalogEntryCount: 3}.

Fixture: 200 profiles, 76,000 rules, 1,900 active tools, 695 removed references, 12 agents, one sibling connection, six quarantine entries, signed/unsigned/resolved requests and 2,000 unrelated historical invocations. Reproduction:

cd server
node --expose-gc --import ./node_modules/tsx/dist/loader.mjs scripts/benchmark-connection-review-counts.ts

After two warmup pairs, seven alternating measured pairs in a coordinated quiet window produced:

Service measurement Legacy attention → selected counts Scoped counts
Queries 7 1
Returned application rows 80,115 1
Process CPU median (range), ms 213.891 (209.329–228.552) 1.680 (0.515–6.137)
Wall median (range), ms 156.867 (151.739–162.289) 1.248 (1.115–1.386)

Separate work-volume instrumentation serialized database result values to JSON: 32,978,153 versus 66 bytes. This measures decoded application values, not network bytes or HTTP response size. The legacy HTTP response never contains the rule arrays. Work-volume serialization is disabled during every timed sample.

These are local source-service measurements, not deployed latency or capacity claims. They exclude HTTP admission, network transport and serialization. They do not measure PostgreSQL CPU. Scoped SQL still scans relevant pending/catalog data using existing indexes; no migration or new cache was needed.

Correctness and preserved failures

PostgreSQL differential tests cover sibling/company boundaries, multiple requests, NULL and empty signed text, unsigned/resolved requests, null invocation connections, cross-company historical references, unhealthy-only connections, archive/restore, and changed rows between reads. Assembled HTTP tests use actual human sessions and agent JWT authentication with explicit permissions. They exercise UID lookup, missing/foreign resources, personal grant revocation and membership revocation. Both sidebar variants have badge, disabled-before-identity and prefix invalidation component tests.

The first SQL attempt embedded column references directly into scalar raw SQL. Real PostgreSQL rejected the resulting ambiguous unqualified columns. Typed Drizzle subqueries fixed that failure; the passing differential test exercises the corrected query. An initial benchmark serialized result values inside its timed instrumentation and inflated CPU. That run was discarded; the results above come from a fresh alternating run with byte accounting outside timing. Initial HTTP fixtures lacked agent membership/management grants and correctly received 403; the accepted fixture supplies both and retains revocation denial checks.

The built static UI was also exercised in Chromium against an owned local-trusted server and disposable PostgreSQL. The connection Permissions page showed Review 3, issued the scoped count request, and made no company attention request. After resolving requests and clearing quarantines, reload removed the badge. An archived connection returned zero counts. This browser path uses board authority; separate authenticated HTTP fixtures prove delegated agent authority. The first ad hoc browser launcher omitted required decision-service options and failed at startup; adding the standard no-op wake callback allowed the same journey to pass. No product behavior was changed to recover that harness setup failure.

Required local recursive typecheck and full build passed. The first sandboxed typecheck could not open the tsx IPC socket; the escalated rerun passed. Greptile identified an invalid resolved-request fixture label (denied); it was corrected to the declared rejected status. This preserves the non-pending exclusion that the differential test verifies.

The first final-head CI run flagged the new explicit resource company assertion because the route lacked a local hasCompanyAccess existence gate. Management admission already rejects foreign resources with 404, but the route now also follows the repository's two-step gate pattern. The static existence-oracle guard and actual authenticated HTTP negative cases verify that pattern together.