Skip to content

fix(connections): keep retrying when Google does not confirm a revoked grant - #22669

Open
cryppadotta wants to merge 15 commits into
masterfrom
fix/retry-google-revocation
Open

cryppadotta wants to merge 15 commits into
masterfrom
fix/retry-google-revocation

Conversation

@cryppadotta

Copy link
Copy Markdown
Contributor

Thinking Path

  • Paperclip is the open source app people use to manage AI agents for work.
  • Paperclip Cloud can manage the Google sign-in for a Google Workspace connection. The app keeps the refresh token. Cloud keeps no token.
  • When a user removes the connection, the app asks Cloud to end the access at Google (feat(connections): end Google access when a Paperclip-managed Google connection is removed #22065).
  • Before this change, the app deleted the token after that call, whatever Cloud answered. If Google or Cloud could not be reached, the access stayed open at Google, and no retry was possible.
  • The owner decided on 2026-11-23: the app keeps the token in a pending record and retries until Google answers. Cloud still never stores a provider token.
  • This pull request adds that pending record and a background retry. It also covers company deletion and a Cloud-removed instance.
  • The benefit is that a removed Google connection does not keep access at Google because of a short outage, and the user does not need to do anything.

Linked Issues or Issue Description

Refs #22065

What happened?
A managed Google connection was removed while Paperclip Cloud or Google was unavailable. The removal result was not_ended, and the refresh token was deleted. Paperclip could not try again, so Google access stayed open.

What did you expect?
Paperclip keeps the token only for the retry, and retries automatically until Google confirms or gives a final answer. The user sees that Paperclip keeps trying, and does not need to act.

Steps to reproduce

  1. Connect Gmail through Paperclip Cloud.
  2. Make the broker revoke fail (Cloud unavailable, or Google 5xx).
  3. Remove the connection. The token is deleted and nothing retries.

What Changed

  • New table google_pending_revocations (migration 0502, additive, CREATE TABLE only). It has company_id with no foreign key, so a row outlives a deleted company. It stores the token as the same local_encrypted material as the connection secret version. It never stores plaintext. It is not a company secret: it is not listed, an agent cannot bind it, and no route reads it.
  • google-access-removal.ts: before the broker call, the app copies the sent token version into a pending row (lease 2 minutes, so a restart during the call resumes). A final outcome deletes the row. A retryable outcome (provider_revocation_failed, revocation_in_progress, broker_unavailable, broker_error, unexpected_error) keeps it, and the result is the new retrying. If the connection secret is still live after the conditional delete (for example, the user signed in again during the call), the row is deleted.
  • New worker on the google-pending-revocation delivery queue (registerDeadlineWork; it registers the real next deadline and has no deadline when the table is empty). Each attempt takes a lease on the row and calls the broker with a 15-second timeout. Backoff is 1, 2, 4 ... minutes, up to 6 hours. The hard stop is 30 days. The worker stops without a call if a live company secret holds the same token.
  • When a retry ends, the worker writes a connection.google_access_retry audit event and a tool_connection.google_access_ended or tool_connection.google_access_not_ended activity entry (labels added to the activity feed). For a deleted company, it writes only a log line.
  • Company deletion queues every managed Google token of the company inside the delete transaction, before the secrets are deleted (reason = company_deleted).
  • When the Cloud connection status check finds the instance removed, the app queues every managed Google token (reason = instance_removed), then deletes each connection secret.
  • ManagedGoogleAccessRemoval.result gets retrying (shared type and OpenAPI schema). The removal message for retrying: "Google didn't confirm that Paperclip's access has ended yet. Paperclip will keep trying automatically." It has no link and asks for no action.
  • Docs: doc/connections/GOOGLE-WORKSPACE.md ("Pending revocation"), docs/api/tool-connections.md, the help page docs/guides/board-operator/deleting-connection-data.md, and a dated decision row in doc/memory/google-connector-memory.md.
  • tests/runner-e2e/source-sdk-public-inputs.json refreshed for the changed shared type, as pnpm verify asks.

Verification

  • npx vitest run src/__tests__/google-managed-connection-removal.test.ts (embedded Postgres): 46 pass. Without the fix, 15 fail (red), with the fix all pass (green). New cases:
    • an unconfirmed token is kept only in the pending row, encrypted, not in the secrets list; the worker retries and deletes the row when Google confirms; audit and activity are written;
    • the row exists during the broker call, so a fresh worker after a restart revokes it;
    • binding_not_known, other_connections_remain and provider_revocation_disabled stop the retry;
    • backoff 1 minute to 6 hours, and give-up at 30 days with a final audit and activity entry;
    • a live secret with the same token stops the retry without a call;
    • an older broker's 403 INSTANCE_SUSPENDED is retried;
    • company deletion queues the token and the worker revokes it after the company is gone;
    • a removed instance queues every token, drops the secrets, does not queue again, and the worker revokes.
  • npx vitest run src/__tests__/paperclip-cloud-connector-status.test.ts: the removed-instance hook runs only for removed, and a failure does not change the status.
  • ui: npx vitest run src/features/connections/googleAccessRemoval.test.ts (7 pass, the new retrying case is red without the change).
  • Also passed: tool-connection-content-deletion, its canary, delivery-work-coordinator, idle-local-work, packages/db tests; server tsc --noEmit.
  • pnpm verify exits 2 (global doc input changed), so required CI supplies the broad coverage.
  • Not verified: any call to a real broker or to Google. No request was sent to staging, production or Google.

Risks

  • Migration is additive (CREATE TABLE and indexes on the new table): online_compatible.
  • Paperclip now holds an encrypted token for at most 30 days after removal when Google did not confirm. The token is never usable for tool calls: no grant or connection refers to the row.
  • Instance removal deletes the managed Google connection secrets. Those connections then need a reconnect after a new enrollment. They could not refresh anyway, because Cloud refuses a removed instance.
  • Deploy order: either order is safe. With the current production broker, a removed instance's revoke gets 403 and the app retries until the hard stop. The Cloud broker change that accepts it is a separate PR in the private Cloud repository.

Model Used

  • Claude Opus 5.5 (claude-opus-5-5) through Claude Code, with tool use (shell, file edits, embedded Postgres tests), standard reasoning.

Checklist

  • I have included a thinking path that traces from project context to this change
  • I have specified the model used (with version and capability details)
  • I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work
  • I have searched GitHub for duplicate or related PRs and linked them above
  • I have either (a) linked existing issues with Fixes: # / Closes # / Refs # OR (b) described the issue in-PR following the relevant issue template
  • I have not referenced internal/instance-local Paperclip issues or links (only public GitHub #NNN / github.com/paperclipai/paperclip URLs)
  • My branch name describes the change (e.g. docs/..., fix/...) and contains no internal Paperclip ticket id or instance-derived details
  • I have run tests locally and they pass
  • I have added or updated tests where applicable
  • I have updated relevant documentation to reflect my changes
  • 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
  • I will address all Greptile and reviewer comments before requesting merge

🤖 Generated with Claude Code

cryppadotta and others added 3 commits November 23, 2026 10:41
…d grant

Owner decision 2026-11-23: when Google access must end and Google cannot
be reached or confirm, the app keeps the refresh token in a pending
revocation record and retries until Google answers. Paperclip Cloud still
never stores provider tokens.

Before this change, removing a Paperclip-managed Google connection or
grant deleted the refresh token whatever Paperclip Cloud answered, so a
failed or unconfirmed revoke could never be retried and Google access
stayed open.

- google_pending_revocations holds the token's local_encrypted material
  (copied from the connection secret version, never plaintext), keyed by
  the token hash, with company_id and no foreign key.
- Removal queues the token before the broker call, so a restart during
  the call still resumes; a final answer deletes the row, a retryable one
  keeps it and the removal reports "retrying".
- The google-pending-revocation delivery queue retries with a lease,
  1 minute to 6 hours backoff and a 30-day hard stop, and writes an audit
  event and an activity entry when it ends.
- Company deletion queues every managed Google token inside the delete
  transaction; a Cloud-removed instance queues them, then drops the
  connection secrets.
- The removal message says Paperclip keeps trying, with no action needed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…l result type

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…mber

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@feynman-nuclear-review

feynman-nuclear-review Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Feynman Nuclear Review — 5/5

Re-review of 21204be

The PR's revocation code is unchanged from the 5/5 head 45b0015: the pending-revocation worker, the access-removal flow, the admin-only durable sweep, version pinning, and the lifecycle wiring and tests all carry the same diff. The branch has since merged upstream master, which refactored createPaperclipCloudConnector.getInstanceStatus into an internal instanceStatus() returning { status, confirmed } and routed both public methods through it.

The PR's adapter is correct against that refactor. A 401 (or a transient error with a remembered removed) yields confirmed: false, so getConfirmedInstanceStatus() returns unknown_instance, while a signed removed answer returns removed. The existing connector test still matches that contract, and the reconciler only deletes instance tokens for a confirmed removed, so an unconfirmed answer cannot trigger the destructive path.

Residual notes, not defects: the merged instanceStatus() still calls settle("removed") on a 401, which clears cached capabilities and records last-known removed before the caller sees unknown_instance; that is inherited upstream behavior and does not delete secrets. The google_access_not_ended final-answer copy, the awaited enrollment sweep, and the snapshot drift remain as previously noted.

Verdict: no actionable correctness, security, or reliability defects found within the stated coverage. Advisory only.

Score 5 because the PR's own code is unchanged from the previously published 5/5 head and its adaptation to the merged connector refactor is correct. Evidence: the compare delta lists 54 files, but only 7 are in the PR's 31-file set; the rest are base/merge drift. Only server/src/services/paperclip-cloud-connector.ts changed materially within the PR diff (20+/14 to 42+/34): I read the new instanceStatus() and confirmed 401 and transient error return confirmed: false, getConfirmedInstanceStatus maps an unconfirmed removed to unknown_instance, and a signed removed stays removed; getInstanceStatus keeps its prior 401-as-removed behavior for other callers. All other PR-file diffs are unchanged from 45b0015. Remaining risk and limitations: the inherited 401 settle("removed") capability-cache clearing; the pre-existing copy nuance; the awaited sweep; snapshot drift; no checkout, so tests/typecheck were not run; no CI/check read tool, so CI is unknown and not claimed; no live broker or Google call was made.

Reviewed commit: 21204beafbc81d7df9e6d0a78e39c638d1cbc065

Coverage: 31 files.
No repository checkout, so tests and typecheck were not run locally.
No check/CI read tool is available for this task; CI status is unknown and not claimed.
No live Paperclip Cloud broker or Google request was made.
The generated Drizzle snapshot was checked structurally rather than line by line.
The connector status refactor arrived via the merged base branch; its broader upstream tests were not re-read.

Task: PAP-6439 · Run · Review history

@ponytail-paperclip-review

ponytail-paperclip-review Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Ponytail Paperclip Review — 4/5

What this change does: A managed Google connection removal keeps the encrypted refresh token in google_pending_revocations when Cloud or Google does not confirm the revoke, and a deadline worker retries for up to 30 days. The current head (21204bea) merges current master and refactors the connector status read into instanceStatus, so getConfirmedInstanceStatus returns unknown_instance unless a removed answer is signed; the base's new misconfigured/unreachable statuses and the removed argument on the Google removal notice are retained.

Prior findings: the earlier reliability, tenant, live-use, instance-removal, pinned-version, transactional-sweep, admin-gate, and activity-feed fixes remain. The refactor preserves the 401 and signed-removal behavior, and the connector tests still assert both; the PR diff against the new base is still the same 31 files.

Nice to have

  1. source_secret_id is still write-only (packages/db/src/schema/google_pending_revocations.ts:10)

    • What this is: The column is inserted but has no production reader; only a test asserts it.
    • Problem: The new table carries a field nothing reads.
    • Fix: Remove it from the new migration, schema, insert, and test assertion, or use it in the retry audit.
  2. A broker 401 is reported as "not configured" (server/src/services/paperclip-cloud-connector-status.ts:23)

    • What this is: unknown_instance falls through to not_configured.
    • Problem: A broker auth failure is an unverified answer; the UI then shows "Enable Paperclip-managed sign-in". The new base has unverified and unreachable, so not_configured is now more clearly the wrong bucket.
    • Fix: Return unverified for unknown_instance before the fallthrough and assert it in the status test.

Verdict: ship; the two items above are optional.

Not checked: tests and typecheck were not run (no dependencies or database); CI/check status was unavailable; no live broker or Google call was made; the merge and refactor were inspected but not executed.

Score 4: the current head merges master and refactors the status read without changing the PR's contract. instanceStatus() now centralizes the broker call, preserves the base's transient/cached behavior, and getConfirmedInstanceStatus() maps an unconfirmed removed answer to unknown_instance; the 401 and signed-removal tests still cover both. I read the base-vs-head diff for paperclip-cloud-connector.ts, the current paperclip-cloud-connector-status.ts, the route, the schema/migration, and the activity-feed files, and confirmed the new base still ends at migration 0501 with the same snapshot id, so the PR's 0502 remains additive. The earlier reliability, tenant, live-use, instance-removal, pinned-version, transactional-sweep, admin-gate, and activity-feed fixes remain. Remaining risk: the write-only source_secret_id and the 401-to-not_configured mapping are unchanged; the base now has misconfigured/unreachable statuses, which makes the 401 mapping more visibly inconsistent. The enrollment GET still awaits the full sweep, though the transaction saves the work before deletes and the path is admin-only. No CI or test run was available.

Reviewed commit: 21204beafbc81d7df9e6d0a78e39c638d1cbc065

Coverage: 31 files.
No repository checkout with dependencies/database, so tests and typecheck were not run locally.
No CI/check read tool is available; check status is unknown (mergeable_state was previously blocked, not evidence of a specific failure).
No live Paperclip Cloud broker or Google request was made.
The master merge and the connector instanceStatus refactor were inspected in source; the combined tree was not executed.
The enrollment GET still awaits the instance-wide sweep, so a large instance can block that admin request; I did not measure the latency.

Task: PAP-6440 · Run · Review history

@greptile-apps

greptile-apps Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Critical impact] No new blocking issue was established in this review.

Summary

Adds saved, encrypted Google revocations and a background worker that retries unconfirmed removal for up to 30 days.

  • Paperclip retries Google access removal when Cloud cannot confirm it.
  • People can see when Google access removal is still being retried.

Reviews (7) · Last reviewed commit: "chore(runner-e2e): refresh pinned source..." · Reviewed by Greptile

Comment thread server/src/routes/tool-access.ts Resolved
cryppadotta and others added 3 commits November 23, 2026 11:18
…d, and act only on a confirmed instance removal

A restart between the first broker call and the secret delete left the
removed grant's own secret live, and the worker read it as another live
holder and dropped the retry. The pending row now records its source
secret and the live-holder check skips it.

A 401 from Paperclip Cloud's status check no longer starts the instance
removal cleanup; only a signed status: removed answer does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…vocation

* origin/master:
  fix(antigravity): offer the cloud candidate in the agent picker for linux-x64 environments (#22655)
  fix(cloud): name every durable-data blocker in the online pass result (#22641)
  fix(workspaces): move a workspace with only a task warning online, as offline already does (#22650)
…dge live use by references

The pending row is now unique per company and token, and settling or
dropping it is filtered by company, so one company's removal can never
settle another company's retry. A live holder is now a secret that a
live grant or connection refers to; a stored but unused copy, or a
secret whose delete failed, no longer cancels the retry. The removed
instance cleanup no longer runs inside the status request.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@cryppadotta

Copy link
Copy Markdown
Contributor Author

@greptileai review

Comment thread server/src/routes/tool-access.ts Resolved

@superagent-security superagent-security Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superagent found 1 security concern(s).

Comment thread server/src/routes/tool-access.ts Resolved
cryppadotta and others added 2 commits November 23, 2026 12:01
…nd limit instance cleanup to instance admins

The live-use check now counts any secret with the same token that a live
grant or connection uses, including the row's source secret, so a grant
signed in again before the retry is never revoked under it. Rows queued
for a removed instance skip the check. Only an instance admin's status
check starts the removed-instance cleanup, and only one sweep runs at a
time.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…vocation

* origin/master:
  test(native): give the repeated-bytes provider pack tests a 60s timeout (#22663)
@cryppadotta

Copy link
Copy Markdown
Contributor Author

@greptileai review

Comment thread server/src/routes/tool-access.ts Resolved
Comment thread server/src/routes/tool-access.ts Resolved
…removed-instance sweep atomically

A grant that pins an older refresh-token version sends that version, so
the retry now keeps the same version instead of the latest one. The
removed-instance cleanup queues every company's tokens in one
transaction before it drops any secret, and the status request waits
for that save; the in-process single-flight guard is gone because the
queue is idempotent.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@cryppadotta

Copy link
Copy Markdown
Contributor Author

@greptileai review

Comment thread server/src/routes/tool-access.ts Resolved
cryppadotta and others added 2 commits November 23, 2026 12:40
…nce cleanup

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…vocation

* origin/master:
  fix(ci): make unchanged GitHub Actions polls free with conditional requests (#22677)
  chore(lockfile): refresh pnpm lockfiles (#22678)
  feat(telemetry): report custom connector domains and aggregator app requests (#22665)
cryppadotta and others added 2 commits November 23, 2026 13:01
…nection's Activity tab

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…nt types

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@cryppadotta

Copy link
Copy Markdown
Contributor Author

@greptileai review

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant