mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-18 06:01:49 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - A Cloud instance authenticates each signed-in request from trusted Cloud headers in `resolveCloudTenantActor` > - Each such request, static files included, waits for one shared per-user sync that writes the user, company and membership rows > - The sync takes row locks even when nothing changes. When another open transaction holds the same user's rows, the sync waits for the lock. Every request of that user then waits for the same sync, with no bound > - On a hosted instance this hung all requests of one signed-in user for about 6.5 minutes. Other users, background work and requests without identity headers stayed fast > - This pull request makes a request wait for the sync only when the sync can change the actor's authority > - The benefit is that a lock held elsewhere no longer takes a signed-in user's pages down ## Linked Issues or Issue Description No public issue exists. **What happened?** For one signed-in Cloud user, every request hung for about 6.5 minutes and then all of them finished together. This included API routes, the HTML page, fonts, plugin UI files and `/api/health`. Requests without the user's identity headers (hashed `/assets/*.js`) took 6 to 18 ms in the same minutes. Background workers kept logging normally. The process, the event loop and the database stayed healthy. **Expected behavior** A user whose membership is already in place authenticates from the membership read. A lock held by another transaction on the user's rows does not hold the user's requests. **Steps to reproduce** 1. Resolve a Cloud tenant actor once, so that the user and the membership exist. 2. In another transaction, take `FOR KEY SHARE` on the user row and `FOR UPDATE` on the membership row, and keep the transaction open. 3. Resolve the same actor again from a new client (no process-local write debounce). 4. Before this change, the call does not return until step 2 commits. **Relevant logs or output** Request logs showed a p50 of about 10 s (the UI aborts at 10 s) and tails up to 252 s. Every hung request carried the same Cloud user header. All of them completed together when the lock cleared. ## What Changed - `resolveCloudTenantActor` reads the user's memberships first. When the stored primary membership is active and has the requested role, the request does not wait for the tenant write sync. The sync finishes in the background, a failure is logged, and idle and drain checks count it until it settles. - A new user, a role change, a status change or a failed membership read still waits for the sync, as before. - New PostgreSQL regression test: another transaction holds the user's rows, and an already-synced user still authenticates in under 3 s with the same companies and role. The existing grant-repair assertion now waits for the background repair. ## Verification - Red before the fix: `npx vitest run src/middleware/cloud-tenant-sync-postgres.test.ts -t "already-synced user"` timed out (`expected 'timed_out' not to be 'timed_out'`). - Green after the fix: `src/middleware/cloud-tenant-sync-postgres.test.ts` and `src/middleware/cloud-tenant-actor.test.ts` pass (50 tests). - Related suites pass: `cloud-tenant-actor-statements`, `cloud-tenant-company-provisioning`, `auth-session-route`, `company-cloud-floor` (25 tests). - `tsc --noEmit -p server/tsconfig.json` passes. ## Risks - Low. Authority still comes from the database read on each request. The change only skips waiting for writes that cannot change that authority. - A missing default grant for an unchanged membership is now repaired just after the request instead of before it. Until the repair lands, the gap fails closed (denied access), as it did before the first repair. - A sync that stays stuck now holds idle work open in the background instead of holding requests. The instance does not sleep until it settles. ## Model Used - Claude Opus 5.5 (`claude-opus-5-5`), Claude Code agent with tool use (shell, file edits, test runs). ## 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>