fix: defer KnowledgeGraph init to first use in MCP server - #167
Conversation
f70baa7 to
953232b
Compare
PR Review: fix: defer KnowledgeGraph init to first use in MCP serverExecutive Summary
Affected Areas: Business Impact: None at runtime. Prevents unwanted Flow Changes: Ratings
PR Health
What ChangedBefore:
After: All 5 call sites updated:
No missed references — remaining Low Priority Issues⚡ #1: Lazy getter is not thread-safeLocation: The check-then-set pattern ( Mitigating factors: MCP servers run on a single # If thread safety ever becomes needed:
import threading
_kg_lock = threading.Lock()
def _get_kg():
global _kg
if _kg is None:
with _kg_lock:
if _kg is None:
_kg = KnowledgeGraph()
return _kgVerdictClean, well-motivated fix. The lazy initialization pattern is standard Python, all call sites are covered, and the change eliminates a real side effect that impacts testability. No functional risk. Created by Octocode MCP https://octocode.ai 🔍🐙 |
Replace module-level _kg = KnowledgeGraph() with lazy _get_kg() that creates the instance on first call. Respects --palace flag for custom db_path when provided. Importing mcp_server no longer creates ~/.mempalace/knowledge_graph.sqlite3 as a side effect, fixing test isolation and spurious database creation.
953232b to
053fca2
Compare
|
@bgauryy Recreated this branch from current main to resolve merge conflicts — upstream added |
guybary-wix
left a comment
There was a problem hiding this comment.
Checked the diff — confirms your claim. Only _get_kg() + the 5 call-site replacements, no stale upstream noise. --palace path logic carried over correctly. Approving.
guybary-wix
left a comment
There was a problem hiding this comment.
Previous approval was submitted in error by an automated tool. Withdrawing approval.
|
@guybary-wix any concerns? |
web3guru888
left a comment
There was a problem hiding this comment.
🔧 Review of #167 — fix: defer KnowledgeGraph init to first use in MCP server
Scope: +18/−9 · 1 file(s) · touches core
⚠️ mempalace/mcp_server.py(modified: +18/−9)
Issues
⚠️ Touchesmempalace/mcp_server.py— Core MCP server — maintainer guards this closely
🟡 Needs attention — touches guarded files and has items to address.
🏛️ Reviewed by MemPalace-AGI · Autonomous research system with perfect memory · Showcase: Truth Palace of Atlantis
TestKGLazyCache covers the scenarios behind the lazy per-path refactor: - test_lazy_init_no_import_side_effect: a fresh subprocess import does not create ~/.mempalace/knowledge_graph.sqlite3 (what closed PR MemPalace#167 was aiming at). - test_get_kg_returns_same_instance: two _get_kg() calls under the same resolved path return the same object, cache has one entry. - test_get_kg_different_paths_different_instances: rotating env var produces distinct KGs. - test_multi_tenant_env_switch: the exact scenario from MemPalace#1136 — write under path A, query under path B returns empty, switching back to A sees the fact. - test_cache_thread_safe: 16 threads racing _get_kg() end up with one shared instance and one cache entry.
TestKGLazyCache covers the scenarios behind the lazy per-path refactor: - test_lazy_init_no_import_side_effect: a fresh subprocess import does not create ~/.mempalace/knowledge_graph.sqlite3 (what closed PR MemPalace#167 was aiming at). - test_get_kg_returns_same_instance: two _get_kg() calls under the same resolved path return the same object, cache has one entry. - test_get_kg_different_paths_different_instances: rotating env var produces distinct KGs. - test_multi_tenant_env_switch: the exact scenario from MemPalace#1136 — write under path A, query under path B returns empty, switching back to A sees the fact. - test_cache_thread_safe: 16 threads racing _get_kg() end up with one shared instance and one cache entry.
TestKGLazyCache covers the scenarios behind the lazy per-path refactor: - test_lazy_init_no_import_side_effect: a fresh subprocess import does not create ~/.mempalace/knowledge_graph.sqlite3 (what closed PR MemPalace#167 was aiming at). - test_get_kg_returns_same_instance: two _get_kg() calls under the same resolved path return the same object, cache has one entry. - test_get_kg_different_paths_different_instances: rotating env var produces distinct KGs. - test_multi_tenant_env_switch: the exact scenario from MemPalace#1136 — write under path A, query under path B returns empty, switching back to A sees the fact. - test_cache_thread_safe: 16 threads racing _get_kg() end up with one shared instance and one cache entry.
TestKGLazyCache covers the scenarios behind the lazy per-path refactor: - test_lazy_init_no_import_side_effect: a fresh subprocess import does not create ~/.mempalace/knowledge_graph.sqlite3 (what closed PR MemPalace#167 was aiming at). - test_get_kg_returns_same_instance: two _get_kg() calls under the same resolved path return the same object, cache has one entry. - test_get_kg_different_paths_different_instances: rotating env var produces distinct KGs. - test_multi_tenant_env_switch: the exact scenario from MemPalace#1136 — write under path A, query under path B returns empty, switching back to A sees the fact. - test_cache_thread_safe: 16 threads racing _get_kg() end up with one shared instance and one cache entry.
Summary
mcp_server.pyline 34 runs_kg = KnowledgeGraph()at module level.KnowledgeGraph.__init__creates the parent directory and opens/creates a SQLite database. This means importing the MCP server module — even to inspect its tools list or in tests — creates~/.mempalace/knowledge_graph.sqlite3as a side effect.Fix
Replace
_kg = KnowledgeGraph()with_kg = Noneand a lazy_get_kg()getter that creates the instance on first call. All 5 call sites updated from_kg.method()to_get_kg().method().Changes
1 file changed (
mempalace/mcp_server.py), 14 insertions, 6 deletions.Test plan
ruff check+ruff format --checkpasspython3 -m py_compilecompiles OK_kg.call sites updated to_get_kg().Refs: #159 (point 8)