Skip to content

fix: #3346 clean up orphaned messages in delete_branch#3526

Open
kimnamu wants to merge 1 commit into
openai:mainfrom
kimnamu:fix/3346-delete-branch-orphan-cleanup
Open

fix: #3346 clean up orphaned messages in delete_branch#3526
kimnamu wants to merge 1 commit into
openai:mainfrom
kimnamu:fix/3346-delete-branch-orphan-cleanup

Conversation

@kimnamu
Copy link
Copy Markdown

@kimnamu kimnamu commented May 29, 2026

Summary

Fixes #3346.

delete_branch() removes the branch's message_structure rows but never cleans up the underlying messages, so messages that were only referenced by the deleted branch stay in the messages table indefinitely.

The session already has a well-factored _cleanup_orphaned_messages_sync() helper (the same one add_items() relies on for its failure path), so this fix simply calls it after the structure rows are deleted — no new deletion logic, just the missing call site. The log line now also reports how many orphaned messages were removed.

Before / After

Scenario Before (main) After (this PR)
delete_branch("feature") with branch-only messages message_structure rows deleted, but the messages stay in messages_table forever Orphaned messages removed via the existing _cleanup_orphaned_messages_sync() helper
Shared messages (referenced by another branch) Retained Retained (unchanged)
Log output Deleted branch 'feature': N message entries, M usage entries Deleted branch 'feature': N structure entries, M usage entries, K orphaned messages removed
New deletion logic introduced None — reuses existing helper

Test

Adds test_delete_branch_cleans_up_orphaned_messages, a regression test that creates a branch-only message, deletes the branch, and asserts the orphaned message is gone while the shared message remains.

$ uv run pytest tests/extensions/memory/test_advanced_sqlite_session.py::test_delete_branch_cleans_up_orphaned_messages -v
============================= test session starts ==============================
tests/extensions/memory/test_advanced_sqlite_session.py::test_delete_branch_cleans_up_orphaned_messages PASSED [100%]
============================== 1 passed in 0.26s ===============================

Note: this is one half of #3498, split into a single-issue PR as suggested for easier review. The other half (#3348) is in #3527.

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

Labels

duplicate This issue or pull request already exists feature:sessions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AdvancedSQLiteSession.delete_branch() leaves branch-only messages in the base table

2 participants