Skip to content

Delete AI chat sessions when a site is deleted - #4132

Merged
bcotrim merged 14 commits into
trunkfrom
archive-chats-on-site-delete
Jul 21, 2026
Merged

Delete AI chat sessions when a site is deleted#4132
bcotrim merged 14 commits into
trunkfrom
archive-chats-on-site-delete

Conversation

@bcotrim

@bcotrim bcotrim commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Related issues

  • Related to STU-1985 (follow-up: chat sessions were orphaned when their site was deleted)

How AI was used in this PR

Implemented with Claude Code: mapped the delete flows, wrote the helper, hook, and tests. Verified with lint, typecheck, unit tests, and a CLI smoke test (built CLI against a seeded fake $HOME). Human-reviewed before opening.

Proposed Changes

  • Deleting a site currently leaves its AI chat sessions orphaned: their site group no longer exists, so they pile up under Unassigned in the sidebar while the session files and placements silently linger.
  • With this change, deleting a site — from the desktop app, the CLI, or the local API server — deletes its chat sessions outright: the transcript JSONL (plus sidecars), the site placement, and any star/archive flags. Per review discussion, deletion (rather than archiving) is the right call because the sessions folder is hidden and there's no way to restore archived chats, unlike site files.
  • Sessions are matched to the deleted site by site id or site path, so sessions with a stale placement field are still caught (and this stays correct alongside the in-flight switch of sidebar grouping from path- to id-keyed).
  • Session cleanup is best-effort: a failure is logged but never blocks the site deletion itself.

Testing Instructions

  1. Create a site and start a couple of AI chat sessions bound to it.
  2. Delete the site (agentic UI, desktop delete dialog, or studio site delete --path <site>).
  3. Verify the chats disappear from the sidebar immediately — neither under the (now gone) site group nor under Unassigned — including when the delete was run from a terminal while the app is open.
  4. Verify the session JSONL files are removed from disk (~/Library/Application Support/Studio/sessions on macOS) and ~/.studio/shared.json / app.json no longer reference those session ids.
  5. Verify sessions belonging to other sites are untouched (files, placements, and starred flags).
  6. Unit tests: npm test -- packages/common/ai/sessions/tests/delete-sessions-for-site.test.ts apps/cli/commands/site/tests/delete.test.ts

Pre-merge Checklist

  • Have you checked for TypeScript, React or other console errors?

🤖 Generated with Claude Code

@bcotrim
bcotrim marked this pull request as ready for review July 9, 2026 20:05
@bcotrim

bcotrim commented Jul 9, 2026

Copy link
Copy Markdown
Contributor Author

@shaunandrews @youknowriad What do you think about deleting chat sessions when their site is deleted? For now I only implemented archiving, but I think there's a case to be made that a session for a site that no longer exists is not very useful. A middle ground could be tying it to the "Delete site files" checkbox: keep the transcripts unless the user also chose to delete the site's files.

@youknowriad

Copy link
Copy Markdown
Contributor

I feel like we should just delete the sessions when a site is deleted, the reason is that the folder where they live is hidden and there's no way to restore them, unlike the site files.

@wpmobilebot

wpmobilebot commented Jul 9, 2026

Copy link
Copy Markdown
Collaborator

📊 Performance Test Results

Comparing 7bc7bcc vs trunk

app-size

Metric trunk 7bc7bcc Diff Change
App Size (Mac) 1354.38 MB 1354.40 MB +0.02 MB ⚪ 0.0%

site-editor

Metric trunk 7bc7bcc Diff Change
load 1105 ms 1088 ms 17 ms ⚪ 0.0%

site-startup

Metric trunk 7bc7bcc Diff Change
siteCreation 7017 ms 6994 ms 23 ms ⚪ 0.0%
siteStartup 2356 ms 2365 ms +9 ms ⚪ 0.0%

Results are median values from multiple test runs.

Legend: 🟢 Improvement (faster) | 🔴 Regression (slower) | ⚪ No change (<50ms diff)

@bcotrim bcotrim changed the title Archive AI chat sessions when a site is deleted Jul 9, 2026
@shaunandrews

Copy link
Copy Markdown
Contributor

Makes sense to delete them.

@katinthehatsite katinthehatsite left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I tested this and it mostly works fine but I noticed that sometimes I hit the Session not found screen. For example, I click on Delete site for the site that has sessions and stay on the open session for that site and after a couple of seconds, I can see the message Session not found:

Screen.Recording.2026-07-13.at.3.13.55.PM.mov

I think we can switch to the next available site once the deletion completed or started to avoid hitting that screen. What do you think?

@bcotrim

bcotrim commented Jul 13, 2026

Copy link
Copy Markdown
Contributor Author

I think we can switch to the next available site once the deletion completed or started to avoid hitting that screen. What do you think?

Done in bcd84ea
Can you please take another look? 🙇

@bcotrim
bcotrim requested a review from katinthehatsite July 13, 2026 15:03
@katinthehatsite

Copy link
Copy Markdown
Contributor

Done in bcd84ea
Can you please take another look? 🙇

Thanks for making the changes! I don't see the error related to the sessions not found anymore but I do see the brief flash of error when I remain on the chat session of the site that is being deleted:

Screen.Recording.2026-07-14.at.9.31.31.AM.mov

Is that happening on your end as well?

bcotrim added 4 commits July 14, 2026 09:55
…e-delete

# Conflicts:
#	apps/ui/src/components/site-list/index.test.tsx
#	apps/ui/src/components/site-list/index.tsx
#	apps/ui/src/ui-classic/components/session-view/style.module.css
@bcotrim

bcotrim commented Jul 14, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for making the changes! I don't see the error related to the sessions not found anymore but I do see the brief flash of error when I remain on the chat session of the site that is being deleted:

Is that happening on your end as well?

It was! Nice catch! Should be fixed now, can you take another look please?

…e-delete

# Conflicts:
#	apps/ui/src/components/site-list/index.test.tsx
#	apps/ui/src/components/site-list/index.tsx
…e-delete

# Conflicts:
#	apps/ui/src/data/core/connectors/ipc/index.ts
#	apps/ui/src/ui-classic/router/layout-root/index.tsx

@sejas sejas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Works as expected. I noticed my second site was selected instead of the first one, so I created a small PR against this one to fix that and fix the scroll to the top. #4287

Otherwise looks good. Great work Bernardo!

Comment on lines +97 to +98
await deleteSharedSession( sessionId );
await deleteAiSessionPlacement( sessionId );

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We could consider moving these isnide the try/catch to avoid an error to prevent running the rest of the loop.

@bcotrim
bcotrim merged commit dafd601 into trunk Jul 21, 2026
15 checks passed
@bcotrim
bcotrim deleted the archive-chats-on-site-delete branch July 21, 2026 19:15
bcotrim added a commit that referenced this pull request Jul 22, 2026
…te (#4292)

## Related issues

<!--
Link a related issue to this PR. If the PR does not immediately resolve
the issue,
for example, it requires a separate deployment to production, avoid
using the "Fixes" keyword and use "Related to" instead.
-->

- Related to #4132
- Addresses @sejas's review suggestion from that PR

## How AI was used in this PR

<!--
Help reviewers understand what to look for and verify that you've
reviewed the code yourself.
-->

AI was used to implement the focused robustness change, add the
regression test, and run validation.

## Proposed Changes

<!--
Explain the intent of this PR:
- What problem does it solve, or what need does it address?
- How does it affect the user (new capability, fix, performance,
accessibility, etc.)?
- Note any user-visible behavior changes or trade-offs.

Focus on the "why" and the user impact. Avoid listing modified files or
describing implementation mechanics — the diff already shows that.
-->

- Ensure deleting a site continues removing its remaining AI chat
sessions when one session's metadata cleanup fails.
- Preserve best-effort metadata cleanup when a session file is already
missing.

## Testing Instructions

<!--
Add as many details as possible to help others reproduce the issue and
test the fix.
"Before / After" screenshots can also be very helpful when the change is
visual.
-->

- Run `npx eslint --fix packages/common/ai/sessions/manage.ts
packages/common/ai/sessions/tests/delete-sessions-for-site.test.ts`.
- Run `npm run typecheck`.
- Run `npm test --
packages/common/ai/sessions/tests/delete-sessions-for-site.test.ts` and
confirm all three tests pass.

## Pre-merge Checklist

<!--
Complete applicable items on this checklist **before** merging into
trunk. Inapplicable items can be left unchecked.

Both the PR author and reviewer are responsible for ensuring the
checklist is completed.
-->

- [x] Have you checked for TypeScript, React or other console errors?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

6 participants