Skip to content

feat(backend): cleanup expired PlatformLinkTokens - #13022

Merged
Bentlybro merged 2 commits into
Significant-Gravitas:devfrom
devareddy05:fix/cleanup-expired-platform-link-tokens
May 12, 2026
Merged

feat(backend): cleanup expired PlatformLinkTokens#13022
Bentlybro merged 2 commits into
Significant-Gravitas:devfrom
devareddy05:fix/cleanup-expired-platform-link-tokens

Conversation

@devareddy05

Copy link
Copy Markdown
Contributor

Closes #12833.

Why

PR #12615 added PlatformLinkToken rows on every /setup and DM-link click but no cleanup job. The table grows unbounded; CodeRabbit + the autogpt-pr-reviewer bot flagged it as a should-fix follow-up.

What

  • New cleanup_expired_platform_link_tokens() in backend/platform_linking/db.py — deletes rows where expiresAt < now - 24h (retention window keeps recently-expired rows for short-term debugging).
  • Exposed on DatabaseManager + DatabaseManagerAsyncClient.
  • Scheduler job cleanup_platform_link_tokens registered alongside cleanup_oauth_tokens, gated by a new platform_link_token_cleanup_interval_hours setting (default 6h, same as OAuth).

How

Mirrors the existing cleanup_expired_oauth_tokens pattern exactly — same delete shape, same scheduler hook, same config-driven cadence. Relies on the existing @@index([expiresAt]) on the model. Adds two unit tests (cutoff position + zero-row case).

@devareddy05
devareddy05 requested a review from a team as a code owner May 6, 2026 17:37
@devareddy05
devareddy05 requested review from 0ubbe and ntindle and removed request for a team May 6, 2026 17:37
@github-project-automation github-project-automation Bot moved this to 🆕 Needs initial review in AutoGPT development kanban May 6, 2026
@github-actions github-actions Bot added the platform/backend AutoGPT Platform - Back end label May 6, 2026
@coderabbitai

coderabbitai Bot commented May 6, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack
No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 2551e3d9-3adb-4793-982e-15a81c2c33d1

📥 Commits

Reviewing files that changed from the base of the PR and between 864d473 and 6c5683d.

📒 Files selected for processing (2)
  • autogpt_platform/backend/backend/data/db_manager.py
  • autogpt_platform/backend/backend/util/settings.py
🚧 Files skipped from review as they are similar to previous changes (2)
  • autogpt_platform/backend/backend/util/settings.py
  • autogpt_platform/backend/backend/data/db_manager.py
📜 Recent review details
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (11)
  • GitHub Check: check API types
  • GitHub Check: test (3.13)
  • GitHub Check: test (3.11)
  • GitHub Check: type-check (3.13)
  • GitHub Check: test (3.12)
  • GitHub Check: type-check (3.12)
  • GitHub Check: type-check (3.11)
  • GitHub Check: end-to-end tests
  • GitHub Check: Analyze (python)
  • GitHub Check: Analyze (typescript)
  • GitHub Check: Check PR Status

Walkthrough

This PR adds a periodic cleanup job for expired PlatformLinkToken rows. It introduces a 24-hour retention window, implements cleanup logic in the platform linking database module, exposes it via DatabaseManager's RPC interface, registers a configurable scheduler job, and provides unit tests.

Changes

Platform Link Token Cleanup Job

Layer / File(s) Summary
Configuration & Scheduling Parameters
autogpt_platform/backend/backend/util/settings.py
Add platform_link_token_cleanup_interval_hours config field (default: 6, range: 1–24) to control cleanup job frequency.
Cleanup Logic & Retention Window
autogpt_platform/backend/backend/platform_linking/db.py
Define LINK_TOKEN_RETENTION_HOURS = 24 and implement cleanup_expired_platform_link_tokens() to delete expired token rows beyond the retention window, log the count, and return rows deleted.
RPC Exposure
autogpt_platform/backend/backend/data/db_manager.py
Add cleanup_expired_platform_link_tokens() method aliases in DatabaseManager and DatabaseManagerAsyncClient to expose the cleanup function through the RPC interface.
Scheduler Job Registration
autogpt_platform/backend/backend/executor/scheduler.py
Define cleanup_platform_link_tokens() async wrapper and register it as an interval-based job in Scheduler.run_service using the configured cleanup interval from settings.
Tests
autogpt_platform/backend/backend/platform_linking/db_test.py
Import retention constant and cleanup function; add TestCleanupExpired suite with tests verifying deletion counts within the retention window and zero-count returns when no tokens are expired.

Sequence Diagram(s)

sequenceDiagram
  participant Scheduler
  participant AsyncRunner
  participant DatabaseManager
  participant PlatformLinkingDB
  participant Database
  Scheduler->>AsyncRunner: trigger cleanup_platform_link_tokens (interval)
  AsyncRunner->>DatabaseManager: call cleanup_expired_platform_link_tokens()
  DatabaseManager->>PlatformLinkingDB: invoke cleanup_expired_platform_link_tokens()
  PlatformLinkingDB->>Database: delete expired rows older than cutoff
  PlatformLinkingDB-->>DatabaseManager: return deleted_count
  DatabaseManager-->>AsyncRunner: return result
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐇 I nibble old tokens, hop through the night,
Cutoff at sunrise, everything tidy and bright.
Scheduler hums softly, the DB breathes free,
Twenty-four hours kept for a quick memory.
Clean rows, clean fields — a happy rabbit's decree.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 40.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: adding a cleanup mechanism for expired platform link tokens.
Description check ✅ Passed The description is comprehensive and well-related to the changeset, explaining the problem, solution approach, and implementation pattern.
Linked Issues check ✅ Passed All PR objectives directly address the linked issue #12833 requirements: cleanup function added, exposed via DatabaseManager, scheduled job registered, and unit tests included.
Out of Scope Changes check ✅ Passed All changes are directly scoped to the cleanup mechanism requirements: db function, manager aliases, scheduler job, config setting, and tests—no extraneous modifications.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov

codecov Bot commented May 6, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 85.71429% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 70.51%. Comparing base (2624b6f) to head (6c5683d).
⚠️ Report is 9 commits behind head on dev.

Additional details and impacted files
@@            Coverage Diff             @@
##              dev   #13022      +/-   ##
==========================================
- Coverage   70.59%   70.51%   -0.09%     
==========================================
  Files        2193     2193              
  Lines      164774   164809      +35     
  Branches    16822    16823       +1     
==========================================
- Hits       116325   116208     -117     
- Misses      45093    45242     +149     
- Partials     3356     3359       +3     
Flag Coverage Δ
platform-backend 79.54% <85.71%> (-0.05%) ⬇️
platform-frontend-e2e 31.12% <ø> (-0.91%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
Platform Backend 79.54% <85.71%> (-0.05%) ⬇️
Platform Frontend 37.63% <ø> (-0.30%) ⬇️
AutoGPT Libs ∅ <ø> (∅)
Classic AutoGPT 28.43% <ø> (ø)
🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Bentlybro

Bentlybro commented May 8, 2026

Copy link
Copy Markdown
Member

Thanks for picking this up, it was on my TODO and you nailed it, I will approve 🙏

@CLAassistant

CLAassistant commented May 9, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@devareddy05
devareddy05 force-pushed the fix/cleanup-expired-platform-link-tokens branch from 65e0ec0 to 864d473 Compare May 9, 2026 20:34
@devareddy05

Copy link
Copy Markdown
Contributor Author

Hi @Bentlybro / @ntindle , thanks for the approval!

The PR is currently blocked by the Vercel check, which is failing with a vercel.com/git/authorize?... URL instead of an actual deployment log. That makes it look like the Vercel ↔ GitHub integration has lost access to the repo (or this PR branch), rather than there being a real build failure.

I tried pushing an empty commit to retrigger the check, but it failed the same way on the new SHA, so it doesn’t seem fixable from my side. Could one of you please re-authorize / rerun the Vercel integration, or mark the check as non-blocking for this PR if this is a known issue?

The branch is also currently showing as behind dev , I’ll update it from base shortly.

Thanks!

@ntindle

ntindle commented May 9, 2026

Copy link
Copy Markdown
Member

You don't need vercel to pass for your PRs:)

@devareddy05

Copy link
Copy Markdown
Contributor Author

You don't need vercel to pass for your PRs:)

Got it, thanks! Branch is now up-to-date with dev

@Bentlybro
Bentlybro added this pull request to the merge queue May 12, 2026
Merged via the queue into Significant-Gravitas:dev with commit c7210ba May 12, 2026
38 of 39 checks passed
@github-project-automation github-project-automation Bot moved this from 👍🏼 Mergeable to ✅ Done in AutoGPT development kanban May 12, 2026
majdyz pushed a commit that referenced this pull request May 12, 2026
Closes #12833.

## Why
PR #12615 added `PlatformLinkToken` rows on every `/setup` and DM-link
click but no cleanup job. The table grows unbounded; CodeRabbit + the
autogpt-pr-reviewer bot flagged it as a should-fix follow-up.

## What
- New `cleanup_expired_platform_link_tokens()` in
`backend/platform_linking/db.py` — deletes rows where `expiresAt < now -
24h` (retention window keeps recently-expired rows for short-term
debugging).
- Exposed on `DatabaseManager` + `DatabaseManagerAsyncClient`.
- Scheduler job `cleanup_platform_link_tokens` registered alongside
`cleanup_oauth_tokens`, gated by a new
`platform_link_token_cleanup_interval_hours` setting (default 6h, same
as OAuth).

## How
Mirrors the existing `cleanup_expired_oauth_tokens` pattern exactly —
same delete shape, same scheduler hook, same config-driven cadence.
Relies on the existing `@@index([expiresAt])` on the model. Adds two
unit tests (cutoff position + zero-row case).

Co-authored-by: devareddy05 <187963739+devareddy05@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

platform/backend AutoGPT Platform - Back end size/m

Projects

Status: ✅ Done

Development

Successfully merging this pull request may close these issues.

4 participants