Skip to content

feat(chat): soft-delete sidebar chats with restore from Recently Deleted#5830

Merged
waleedlatif1 merged 8 commits into
stagingfrom
feat/chat-soft-delete-recently-deleted
Jul 22, 2026
Merged

feat(chat): soft-delete sidebar chats with restore from Recently Deleted#5830
waleedlatif1 merged 8 commits into
stagingfrom
feat/chat-soft-delete-recently-deleted

Conversation

@waleedlatif1

Copy link
Copy Markdown
Collaborator

Summary

  • Deleting a chat from the sidebar is now a soft delete: sets copilot_chats.deleted_at instead of dropping the row, so chats show up in Settings → Recently Deleted and can be restored (Marcus's archive request)
  • New POST /api/mothership/chats/[chatId]/restore route + contract, owner-scoped like delete; list endpoint gains scope=active|archived
  • Recently Deleted gets a Chats tab; chat restore is not gated on workspace edit permission since chats are per-user (any member can delete/restore their own)
  • Deleted chats are excluded everywhere: lifecycle access helpers (shared ownedLiveChatWhere predicate), fork, legacy copilot chat lists, chat resources routes, copilot VFS tasks listing
  • Soft-deleted chats join the retention purge in cleanup-soft-deletes via the existing prepareChatCleanup teardown, matching every other Recently Deleted resource
  • Migration 0264: nullable deleted_at + partial index (user_id, workspace_id) WHERE deleted_at IS NOT NULL, built CONCURRENTLY; expand-only, backward-compatible with the deployed app
  • Delete-confirmation copy updated: chats are archived and restorable, not "permanently removed"

Type of Change

  • New feature

Testing

Unit tests updated/added for the soft-delete DELETE, scope-filtered list, and lifecycle exclusion; migration applied and verified against a local Postgres; full affected suites green (496 tests)

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

@waleedlatif1
waleedlatif1 requested a review from a team as a code owner July 22, 2026 00:48
@vercel

vercel Bot commented Jul 22, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
docs Skipped Skipped Jul 22, 2026 2:49am

Request Review

@cursor

cursor Bot commented Jul 22, 2026

Copy link
Copy Markdown

PR Summary

Medium Risk
Touches chat lifecycle, retention cleanup, and many list/mutation paths; race-safe cleanup reduces data-loss risk on restore, but behavior changes are broad across copilot/mothership APIs.

Overview
Sidebar mothership chats are soft-deleted (copilot_chats.deleted_at) instead of hard-deleted, so they appear in Settings → Recently Deleted and can be brought back. Restore is exposed as POST /api/mothership/chats/[chatId]/restore; listing adds scope=active|archived, with React Query keys split so active and archived lists stay in sync on delete/restore.

Active reads and mutations now require deleted_at IS NULL (shared ownedLiveChatWhere, list queries, resource routes, fork). The legacy /api/copilot/chat/delete path returns 400 for mothership chats so it cannot bypass the soft-delete flow.

Recently Deleted gains a Chats tab and useRestoreMothershipChat; chat restore is allowed without workspace edit permission (per-user ownership). Delete modal copy treats chats as archivable/restorable like workflows.

Retention jobs hard-delete expired soft-deleted chats (and fold them into prepareChatCleanup); cleanup re-checks row existence / cutoffs so restores mid-job do not purge S3 or copilot backend data. Migration 0264 adds deleted_at and a partial index for archived listings.

Reviewed by Cursor Bugbot for commit 742ad5b. Configure here.

@greptile-apps

greptile-apps Bot commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR adds soft deletion and restoration for sidebar chats. The main changes are:

  • Archives deleted chats and lists them under Recently Deleted.
  • Adds owner-scoped chat restore and active-versus-archived list APIs.
  • Excludes archived chats from active chat operations and related data surfaces.
  • Permanently purges expired archived chats through retention cleanup.
  • Adds the database column, partial index, UI, hooks, contracts, and tests.

Confidence Score: 5/5

This looks safe to merge.

  • Permanent deletion rechecks the retention condition, so a restored chat or workflow survives a concurrent cleanup.
  • External chat cleanup skips rows that still exist after the database deletion phase.
  • Restoring a workspace chat requires both ownership and current workspace access.
  • No blocking issues remain in the changed code.

Important Files Changed

Filename Overview
apps/sim/background/cleanup-soft-deletes.ts Adds expired-chat cleanup and rechecks chat and workflow eligibility before permanent deletion.
apps/sim/lib/cleanup/chat-cleanup.ts Rechecks chat survival before removing backend data and attributed files.
apps/sim/background/cleanup-tasks.ts Rechecks chat age during retention deletion and relies on chat-row cascades for owned child records.
apps/sim/app/api/mothership/chats/[chatId]/restore/route.ts Adds owner-scoped restoration with current workspace access validation and refreshed activity timestamps.
apps/sim/hooks/queries/mothership-chats.ts Adds scoped chat-list queries, restoration, and cache invalidation for active and archived lists.
packages/db/migrations/0264_fat_ikaris.sql Adds nullable chat deletion timestamps and an index for archived-chat lookup.

Reviews (10): Last reviewed commit: "fix(cleanup): recheck workflow archive c..." | Re-trigger Greptile

Comment thread apps/sim/background/cleanup-soft-deletes.ts Outdated
Comment thread apps/sim/app/api/mothership/chats/[chatId]/restore/route.ts
Comment thread apps/sim/hooks/queries/mothership-chats.ts
Comment thread apps/sim/app/api/mothership/chats/[chatId]/route.ts
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cursor review

Comment thread apps/sim/app/api/mothership/chats/[chatId]/route.ts
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cursor review

Comment thread apps/sim/app/api/mothership/chats/[chatId]/restore/route.ts
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cursor review

Comment thread apps/sim/background/cleanup-tasks.ts
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cursor review

@cursor cursor 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.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit a9d1cde. Configure here.

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cursor review

@cursor cursor 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.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 971d6a2. Configure here.

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cursor review

Comment thread apps/sim/background/cleanup-soft-deletes.ts
Comment thread apps/sim/lib/data-drains/sources/copilot-chats.ts
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cursor review

Comment thread apps/sim/background/cleanup-tasks.ts
Comment thread apps/sim/app/api/mothership/chats/[chatId]/restore/route.ts
Comment thread packages/db/migrations/0264_fat_ikaris.sql
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

1 similar comment
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cursor review

@cursor cursor 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.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 742ad5b. Configure here.

@waleedlatif1
waleedlatif1 merged commit 93fbf58 into staging Jul 22, 2026
20 checks passed
@waleedlatif1
waleedlatif1 deleted the feat/chat-soft-delete-recently-deleted branch July 22, 2026 03:10
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