Files
PaperClipAI/packages
DottaandClaude Opus 5.5 b908678ea3 fix(connections): leave no connection behind when a first sign-in is cancelled (#22714)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - Agents use apps through connections. Most app connections use an
OAuth sign-in.
> - Paperclip saves a draft connection before it opens the provider
sign-in page.
> - When the user cancels or abandons that sign-in, the draft stays. The
Apps list counts it as a connection that "needs attention".
> - The user did not ask for that connection, and the user must not
clean it up by hand.
> - This pull request hides drafts that never finished their first
sign-in and removes the draft when the user clicks Cancel.
> - The benefit is that a cancelled sign-in leaves no visible
connection, and a retry makes exactly one connection.

## Linked Issues or Issue Description

Refs #22634

**What happened?**

A workspace has one working "Google Workspace for the organization"
connection. The user clicks "Add account", chooses services, and clicks
Continue. The page shows "Opening Google Workspace…". The user clicks
Cancel before finishing at Google. The Apps page then shows "2
connections · 1 needs attention".

**What did you expect to happen?**

A cancelled sign-in leaves no visible connection. The same applies when
the user closes the page, reloads it, closes the Google tab, or refuses
consent at Google. A retry after a cancel makes exactly one connection.
A cancelled reconnect of a working connection does not change that
connection.

**Steps to reproduce**

1. Connect one Google Workspace account.
2. Open Apps, then Google Workspace, then "Add account".
3. Click "Continue to sign in", choose services, and click Continue.
4. On the "Opening Google Workspace" page, click Cancel.
5. Look at the Google Workspace card on the Apps page.

## What Changed

- Root cause: `connectApp` saves an OAuth draft (`status: "draft"`)
before sign-in (`server/src/services/tool-access.ts`, the connection
insert in the connect path). Cancel only navigated away. The Apps list
counted every draft as "needs attention"
(`ui/src/pages/apps/Browse.tsx`, `connectionState` and the attention
count).
- New module
`server/src/services/tool-access/connections/first-sign-in.ts`. It
decides if an OAuth draft never held a credential. It checks for OAuth
tokens on the connection and its grants, legacy inline tokens (the
removal module's list), a provider consent time, an external credential,
and the markers a real connection keeps when it needs a new sign-in. If
any check is not clear, the connection counts as real.
- The connection list endpoint sets `firstSignInPending: true` on those
drafts. The shared `ToolConnection` type has the new field.
- New route `POST /tool-connections/:connectionId/sign-in/cancel`. It
checks company access and configure permission. It removes the draft
with the normal removal teardown (grants, OAuth states, bindings) only
if the draft never held a credential. For any other connection it
changes nothing and returns `abandoned: false`. A removal writes the
activity entry `tool_connection.sign_in_cancelled`. The route is in the
OpenAPI registry.
- The Apps list does not show connections with `firstSignInPending`, and
it does not show an app whose only connections are such drafts. The
check is one pass with `Set` lookups.
- Cancel works in one transaction: it deletes the pending sign-in
states, locks the connection, checks it again, and parks it as
`disabled` with the marker `oauth_sign_in_cancelled`. If the connection
must stay, the transaction rolls back. Then the normal removal archives
the parked draft. Setup never reuses a parked draft, so a retry during
the removal gets its own row.
- One predicate, `connectionRemovedOrSignInCancelled`, guards
authorization start, access finish and machine sign-in. The direct,
Cloud and Vercel Connect callbacks and the access finish step lock the
connection before they write a grant or activate it, and return 409
`oauth_sign_in_cancelled` for a parked draft.
- `docs/api/tool-connections.md` documents the cancel route and the list
flag.
- The setup page calls the cancel route when the user clicks Cancel on
the sign-in screen. It does this only for a draft that this page made.
It does not do this for a resume, a reconnect, or a task-linked setup.
- Design for abandoned drafts (tab closed, reload, consent refused):
they are not listed, and the next attempt reuses them through the
existing draft reuse in setup. No background cleanup job is added.

## Verification

-
`server/src/__tests__/tool-access-cancelled-first-sign-in.integration.test.ts`
runs the real routes against embedded Postgres and a local OAuth
provider. It covers: a draft is marked and hidden; a provider token
exchange held open across Cancel ends in 409; Cancel and a retry at the
same time leave exactly one active connection; a parked draft is hidden
and refuses a new sign-in start; a legacy draft with an inline token is
not cancelled; Cancel removes it, deletes its OAuth state, logs
activity, and a late callback fails; a retry after Cancel gives exactly
one live connection (organization and personal); Cancel does not change
an active connection or a connection that needs a new sign-in; a draft
refused at the provider stays hidden and Cancel removes it; another
company cannot cancel the draft.
- `ui/src/pages/apps/Browse.test.tsx`: a pending draft is not listed and
does not add "needs attention".
- `ui/src/pages/apps/AppsConnect.test.tsx`: Google Workspace setup,
Continue, then Cancel calls the cancel route for the new draft.
- Red/green: the new Browse and setup tests fail without the UI change.
The server test fails on master because the route and flag do not exist.
- Local runs: the new server file, `openapi-routes.test.ts`,
`tool-access-oauth-protocol.integration.test.ts`, `Browse.test.tsx`,
`AppsConnect.test.tsx`, `activity-format.test.ts`, and
`src/features/connections` pass. Server typecheck passes. `pnpm verify`
exits 2 because of the pinned inputs file, so CI supplies broad
coverage.
- Not done: a real browser test against Google.

## Risks

- Low risk. The cancel route fails toward no change: it removes nothing
that shows any sign of a credential.
- Cancel and a late provider callback agree under the connection row
lock. If the callback commits first, Cancel changes nothing. If Cancel
commits first, the callback returns 409 and saves nothing. This covers
the direct, Cloud and Vercel Connect callbacks and the access finish
step.
- If the removal after Cancel fails, the draft stays parked and hidden,
and a second Cancel finishes it.
- A connection that was removed before and is now added again keeps its
old consent record. If that new sign-in is cancelled, it stays as a
visible draft, as it does today.

## Model Used

- Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, used in Claude Code
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>
2026-11-23 18:16:28 -06:00
..