Skip to content

Support deleting chat messages (turn-level cascade delete) #778

Description

@haiphucnguyen

Add the ability to delete a chat message. Since branching/regenerate is not yet supported, delete operates at turn granularity: a turn is one user message + its following assistant response.

  1. Deleting a user message
    Soft-deletes the user message and cascades to soft-delete its immediately following assistant response (same turn).
    Rationale: a user message and its AI response are one logical turn. Leaving an orphaned AI response with no visible originating prompt is confusing and semantically meaningless.

  2. Deleting an AI response directly
    Not allowed. No delete action is exposed on assistant messages in the UI.
    Rationale: an assistant message without its paired user message is a dangling answer with no question — it breaks conversation readability and context. Regeneration (replacing a response) is a separate concern already handled by isOutdated, not by delete.

  3. Storage approach
    Use soft delete: add deletedAt: Instant? = null to ChatMessage.
    All read paths (message list, export, context-window building for the LLM) must filter WHERE deletedAt IS NULL.

  4. In-memory LLM context sync
    Deleting a turn must also invalidate/rebuild the session's TokenAwareSummarizingMemory (ChatSessionService) so the next LLM call doesn't include deleted messages. Simplest approach: evict the cached session context on delete so it's rehydrated from (filtered) DB messages on next use.

Out of scope

Branching / multiple assistant responses per user message (not supported yet)
Session-level token-usage accounting impact (tracked in a separate issue)

Activity

  1. added
    help wanted
    on Oct 22, 2026
  2. rcpeken commented on Oct 24, 2026

    @rcpeken
    Contributor

    Hi @haiphucnguyen, I'd like to work on this one if it's still open.

    I read through the current code first. A few things I'd plan around:

    LLM memory: I don't think evicting the cached context is enough on its own. Eviction from memoryCache triggers triggerAsyncSummarization(), and the summary is persisted through SessionMemoryRepository, so the deleted turn could survive inside the summary. I'd reuse the pattern from ChatSessionService.markMessagesAsOutdatedAfter: sessionMemoryRepository.deleteBySessionId, reload the recent non-deleted messages, then loadFromFilteredMemory.

    Read paths: a deleted_at column via a new migration (65.sqm), and the filter added next to every is_outdated = 0 in ChatMessages.sq, plus the search, bookmark and export queries that don't filter today.

    Turn pairing: the "following assistant response" would be the next ASSISTANT message by created_at in the same session, skipping outdated ones. Tests for: normal turn, user message with a failed/no response, and a bookmarked message.

    Two questions before I start:

    Sync: selectUnsynced / updateMessageFromServer don't know about deletion. Should a delete be synced to the server (so deleted_at goes out like any other change), or stay local for now?
    If the deleted message is bookmarked, should the bookmark be removed, or just hidden with it?

  3. haiphucnguyen commented on Oct 25, 2026

    @haiphucnguyen
    CollaboratorAuthor

    Thank you @rcpeken for thoughtful analysis.

    LLM memory: I don't think evicting the cached context is enough on its own. Eviction from memoryCache triggers triggerAsyncSummarization(), and the summary is persisted through SessionMemoryRepository, so the deleted turn could survive inside the summary. I'd reuse the pattern from ChatSessionService.markMessagesAsOutdatedAfter: sessionMemoryRepository.deleteBySessionId, reload the recent non-deleted messages, then loadFromFilteredMemory.

    Exactly like what you said, this is the right pattern.

    Read paths: a deleted_at column via a new migration (65.sqm), and the filter added next to every is_outdated = 0 in ChatMessages.sq, plus the search, bookmark and export queries that don't filter today.

    Yes, it is. I am having the open branch, and I may merge my change with the 65.sqm, but you get the point of database migration script

    Two questions before I start:
    Sync: selectUnsynced / updateMessageFromServer don't know about deletion. Should a delete be synced to the server (so deleted_at goes out like any other change), or stay local for now?

    You can ignore the sync now, the community does not have feature releate to sync. Let it stay local

    If the deleted message is bookmarked, should the bookmark be removed, or just hidden with it?
    We should only hide the deleted bookmark message

    In the chatview, we should hide the delete user message and the next ai response message. It is different than outdated message we still display in ChatView, the deleted message is hidden.

  4. rcpeken commented on Oct 26, 2026

    @rcpeken
    Contributor

    Thanks for the assignment and the clear answers! I'll keep delete local-only, hide (not remove) bookmarks of deleted messages, and hide the deleted turn completely in ChatView, unlike outdated messages. I'll start with 65.sqm and renumber if your branch lands first.

  5. haiphucnguyen commented on Oct 26, 2026

    @haiphucnguyen
    CollaboratorAuthor

    Awesome, thank you @rcpeken . I am looking forward to your PR!

  6. rcpeken commented on Oct 27, 2026

    @rcpeken
    Contributor

    PR is up: #784

    As mentioned, your 65.sqm landed first, so the migration is now 66.sqm with AfterVersion(66). I also tested the upgrade path from an existing database in the running app. Happy to adjust anything in review.

  7. added a commit that references this issue on Oct 29, 2026
    ee73f36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

help wanted

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions