Skip to content

refactor(palace): migrate off _DEFAULT_BACKEND compat shim (#35) - #122

Merged
jphein merged 2 commits into
mainfrom
refactor/remove-default-backend-shim
May 22, 2026
Merged

jphein merged 2 commits into
mainfrom
refactor/remove-default-backend-shim

Conversation

@jphein

@jphein jphein commented May 22, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Migrates all 5 call sites off the fork-only _DEFAULT_BACKEND module attribute to direct get_backend("chroma") calls
  • Deletes the 14-line transitional shim and comment block from palace.py
  • Reduces fork-ahead diff by removing fork-only adapter code

Call sites migrated

File Usage
mempalace/mcp_server.py (×2) Cache invalidation (_clients.pop, _freshness.pop) + close_palace in reconnect
benchmarks/mine_bench.py Cache reset between benchmark runs
tests/test_backends.py monkeypatch.setattr target
tests/test_mcp_server.py monkeypatch.setattr target

Context

The shim was added in PR #21 (cherry-pick of upstream MemPalace#665, commit 5e90c72) as a transitional bridge. This PR completes the planned migration.

Test plan

  • Zero _DEFAULT_BACKEND references remain
  • 11 targeted tests pass
  • Full CI suite

Closes #35

🤖 Generated with Claude Code

Replace all 5 call sites that used the fork-only `_DEFAULT_BACKEND`
module attribute with direct `get_backend("chroma")` calls:

- mcp_server.py: cache invalidation (_clients.pop, _freshness.pop)
- mcp_server.py: close_palace in tool_reconnect
- benchmarks/mine_bench.py: cache reset between runs
- tests/test_backends.py: monkeypatch target
- tests/test_mcp_server.py: monkeypatch target

Delete the 14-line transitional shim and its comment block from
palace.py. The shim was added in PR #21 (cherry-pick of upstream
MemPalace#665) and is no longer needed.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings May 22, 2026 22:06
@gemini-code-assist

Copy link
Copy Markdown

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request completes the migration away from the transitional _DEFAULT_BACKEND shim. By replacing module-level attribute access with direct get_backend("chroma") calls, the codebase is now more aligned with upstream patterns and reduces unnecessary fork-specific complexity.

Highlights

  • Removal of Compatibility Shim: Deleted the _DEFAULT_BACKEND module attribute and its associated 14-line comment block from palace.py.
  • Migration of Call Sites: Updated all 5 references in mempalace/mcp_server.py, benchmarks/mine_bench.py, and test files to use get_backend("chroma") directly.
  • Code Cleanup: Reduced fork-ahead technical debt by removing transitional adapter code and updating documentation comments.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist 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.

Code Review

This pull request removes the _DEFAULT_BACKEND compatibility shim from mempalace/palace.py and migrates all call sites in the benchmarks, MCP server, and test suites to use the get_backend("chroma") abstraction. The reviewer suggested using the public close_palace() method in mempalace/mcp_server.py instead of manually manipulating internal cache attributes to ensure proper resource cleanup and better maintainability.

Comment thread mempalace/mcp_server.py
Comment on lines +373 to +377
from .palace import get_backend

_DEFAULT_BACKEND._clients.pop(_config.palace_path, None)
_DEFAULT_BACKEND._freshness.pop(_config.palace_path, None)
chroma_backend = get_backend("chroma")
chroma_backend._clients.pop(_config.palace_path, None)
chroma_backend._freshness.pop(_config.palace_path, None)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Instead of manually popping from internal attributes ._clients and ._freshness, use the public close_palace() method. This is more maintainable and ensures that _close_client() is called to properly release SQLite file locks, which is particularly important for Windows compatibility.

Suggested change
from .palace import get_backend
_DEFAULT_BACKEND._clients.pop(_config.palace_path, None)
_DEFAULT_BACKEND._freshness.pop(_config.palace_path, None)
chroma_backend = get_backend("chroma")
chroma_backend._clients.pop(_config.palace_path, None)
chroma_backend._freshness.pop(_config.palace_path, None)
from .palace import get_backend
get_backend("chroma").close_palace(_config.palace_path)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Considered this — the manual pop is intentional here. _force_chroma_cache_reset() is a targeted retry-path operation that only drops the per-palace-path cache entries (client handle + freshness timestamp) without closing the underlying client. close_palace() does more: it calls _close_client() which releases file locks and tears down the chromadb PersistentClient. The retry path wants a cache miss on the next call (so get_collection rebuilds), not a full client teardown — reopening a PersistentClient is slower and could interfere with concurrent readers.

Note that tool_reconnect (line ~2430) already uses close_palace() for the full-teardown case. The two patterns serve different purposes.

Copilot AI 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.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@jphein
jphein merged commit 6a5b861 into main May 22, 2026
7 checks passed
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.

Migrate mcp_server.py + tests off the _DEFAULT_BACKEND compat shim added in PR #21

2 participants