Skip to content

fix: clean up orphaned messages in delete_branch and propagate add_items metadata errors#3498

Closed
kimnamu wants to merge 1 commit into
openai:mainfrom
kimnamu:fix/advanced-sqlite-session-bugs
Closed

fix: clean up orphaned messages in delete_branch and propagate add_items metadata errors#3498
kimnamu wants to merge 1 commit into
openai:mainfrom
kimnamu:fix/advanced-sqlite-session-bugs

Conversation

@kimnamu
Copy link
Copy Markdown

@kimnamu kimnamu commented May 25, 2026

Summary

Two correctness fixes for AdvancedSQLiteSession, each with a regression test.

#3346delete_branch() removes message_structure rows but never cleans up the
actual messages, leaking storage indefinitely. The fix reuses the existing
_cleanup_orphaned_messages_sync() (which add_items() already calls on failure)
so there's no new deletion logic — just a missing call site.

#3348add_items() catches _insert_structure_metadata() failures, runs cleanup,
but then returns normally. Callers cannot distinguish success from partial failure.
The fix adds a raise RuntimeError(...) from e after the existing cleanup block (3 lines).

Approach

Both fixes reuse existing infrastructure rather than introducing new code:

Fix Approach Why
#3346 Call self._cleanup_orphaned_messages_sync(conn) after structure deletion This method already exists at L491 and uses the same LEFT JOIN ... IS NULL pattern — no new query logic needed
#3348 Add raise RuntimeError(...) from e after the existing except cleanup block The cleanup + logging already exists; only the re-raise was missing

Before / After

Scenario Before (main) After (this PR)
delete_branch("feat") with branch-only messages message_structure deleted, orphan rows stay in messages_table forever Orphans removed via existing _cleanup_orphaned_messages_sync()
add_items([...]) when metadata fails Logs error, returns None — caller assumes success Raises RuntimeError with cause chain; caller can handle or retry
Run Test image image
Existing test suite 36 pass 36 pass (zero regressions) + 2 new

Test plan

  • All 36 pre-existing tests pass unchanged (zero regressions)
  • test_delete_branch_cleans_up_orphaned_messages: creates branch-only messages, deletes branch, asserts messages_table is clean
  • test_add_items_raises_on_structure_metadata_failure: monkeypatches metadata insertion to fail, asserts RuntimeError and verifies orphan cleanup ran
$ uv run pytest tests/extensions/memory/test_advanced_sqlite_session.py -v
...
38 passed in 0.22s
image

Issue number

Closes #3346
Closes #3348

Checks

  • I've added new tests (if relevant)
  • I've added/updated the relevant documentation
  • I've run make lint and make format
  • I've made sure tests pass

… error propagation

- delete_branch() now removes messages that are no longer referenced by
  any branch after structure metadata deletion (openai#3346)
- add_items() now raises RuntimeError when structure metadata insertion
  fails instead of silently swallowing the error (openai#3348)

Closes openai#3346
Closes openai#3348
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 659f368812

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/agents/extensions/memory/advanced_sqlite_session.py
@kimnamu
Copy link
Copy Markdown
Author

kimnamu commented May 26, 2026

@codex review

@chatgpt-codex-connector
Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@kimnamu
Copy link
Copy Markdown
Author

kimnamu commented May 29, 2026

Closing in favor of two focused, single-issue PRs to make review easier:

Both reuse the session's existing helpers and each ships with its own regression test. The AdvancedSQLiteSession internals (especially the shared _cleanup_orphaned_messages_sync helper) made these fixes very clean to slot in — thanks for the well-structured code. Happy to adjust anything on the new PRs.

@kimnamu kimnamu closed this May 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

2 participants