Conversation
… switch (MemPalace#1676) The kill switch added in MemPalace#1305 (hooks_cli._palace_root_exists()) was silently defeated by module-level _WAL_DIR.mkdir(parents=True, exist_ok=True) in mcp_server.py. Importing the MCP server unconditionally recreated ~/.mempalace/, defeating the documented kill-switch gesture of removing that directory. This change moves WAL setup out of module scope into a lazy initializer called from each write path. Module import no longer touches disk; the kill-switch contract is preserved without mcp_server.py needing to know about it. Regression test asserts no top-level disk side effect when ~/.mempalace is absent and the MCP server module is imported. Closes MemPalace#1676
There was a problem hiding this comment.
Pull request overview
This PR fixes a regression where importing mempalace.mcp_server could recreate ~/.mempalace/, inadvertently defeating the documented hooks kill switch (hooks_cli._palace_root_exists()), by moving WAL directory/file setup from module import time to a lazy initializer invoked on first WAL write.
Changes:
- Reworked WAL initialization in
mempalace/mcp_server.pyto be lazy (_ensure_wal()), preventing import-time filesystem side effects. - Added a regression test ensuring
import mempalace.mcp_serverdoes not create~/.mempalace/whenHOMEpoints to an empty temp directory.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
mempalace/mcp_server.py |
Removes import-time WAL directory/file creation; initializes WAL on first _wal_log() call. |
tests/test_mcp_server.py |
Adds subprocess regression test ensuring MCP server import does not recreate the palace root directory. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Catch OSError/NotImplementedError around _ensure_wal's mkdir so a read-only $HOME or permission failure doesn't propagate out of _wal_log and break the actual write call. Pre-MemPalace#1676 module-level setup didn't catch this either, but that ran at import time where a crash is visible; runtime fallthrough into _wal_log is worse. Mark initialized on failure so we don't retry every write. - Test: pass HOME via subprocess env (set before interpreter startup, matching test_lazy_init_no_import_side_effect for KG cache) instead of setting it from inside the -c snippet. Add timeout=30 to prevent a hang from stalling the suite. Both catches from copilot-pull-request-reviewer on #1. Tests: 2278 passed, 3 skipped.
Owner
Author
|
Closing in favor of MemPalace#1678 — same lazy-init fix, with broader test coverage. Tracked from MemPalace#1676. |
ggettert
pushed a commit
that referenced
this pull request
Jul 2, 2026
Adds _try_gemini_json parser to normalize.py for three layouts:
1. Gemini API contents format (~/.gemini/sessions/*.json):
{"contents": [{"role": "user", "parts": [{"text": "..."}]}, ...]}
2. Messages-wrapper variant:
{"messages": [{"role": "user", ...}, {"role": "model", ...}]}
3. Flat top-level list with role="model".
This complements the existing _try_gemini_jsonl parser (which handles
~/.gemini/tmp/<hash>/chats/session-*.jsonl with session_metadata
sentinel) — JSONL covers Gemini CLI runtime sessions, JSON covers
exported / Studio-saved transcripts.
## Review feedback addressed (PR MemPalace#204)
bgauryy review:
- #1 Parser-precedence bug: _try_gemini_json runs *before*
_try_claude_ai_json so the {"messages":[..., role=model, ...]}
layout is no longer silently claimed by the Claude parser. The
Gemini parser's has_model_role guard prevents false-positives
against Claude / ChatGPT data.
- MemPalace#2 Layout 2a coverage: TestGeminiJson.test_messages_wrapper_format
+ test_messages_wrapper_does_not_get_claimed_by_claude pin the
fix in place.
- MemPalace#3 Test conflicts with current main: rebased onto develop;
tests restructured into TestGeminiJson class.
- MemPalace#4 tempfile/os.unlink → pytest tmp_path everywhere.
- MemPalace#5 elif not text → else (the elif branch was dead).
- MemPalace#6 Module docstring updated to mention Google AI Studio.
Tests: 9 new cases in TestGeminiJson covering all three layouts,
multi-part text joining, non-text part skipping, has_model_role
disambiguation, dispatch-chain regression for review #1.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes MemPalace#1676. The kill switch added in MemPalace#1305 (
hooks_cli._palace_root_exists()) was silently defeated by module-level_WAL_DIR.mkdir(parents=True, exist_ok=True)inmcp_server.py. Importing the MCP server unconditionally recreated~/.mempalace/, so any user who removed the directory as a kill-switch gesture got it back as soon as the MCP server ran.This PR moves the WAL setup (directory creation, chmod, file open) out of module scope into a lazy initializer (
_ensure_wal()) called from_wal_log()— the single write path. Module import no longer touches disk. The kill-switch contract is preserved withoutmcp_server.pyneeding to know about it.What changed
mempalace/mcp_server.py(+30 / -13): module-levelmkdir/chmod/ WAL file open removed. Replaced with_ensure_wal()lazy initializer +_wal_initializedguard, called from the start of_wal_log(). Behavior is identical on first WAL write; subsequent writes short-circuit via the guard.tests/test_mcp_server.py(+47): new regression test asserting~/.mempalace/stays absent afterimport mempalace.mcp_serverwhenHOMEpoints at an empty temp dir.Real behavior proof
Pre-fix reproduction (against develop HEAD, before this PR)
Kill switch defeated by the import.
Post-fix reproduction (this branch)
Kill switch holds across the import.
Test suite
No regressions. The new test passes. WAL is exercised via the existing
_wal_logtest paths in the rest of the suite (which create the WAL dir on demand, as designed by this PR).Not tested
mkdir(exist_ok=True)+O_CREAT|O_WRONLYon the file, so two processes racing the first write would both succeed and converge — same race semantics as before this PR (themkdir(parents=True, exist_ok=True)at module load was already serialised through filesystem atomicity, not application-level locking). No change in this PR's footprint.~/.mempalaceexists as a regular file or broken symlink. Same as before:_WAL_DIR.mkdirwould raise; the existing_palace_root_exists()already usesis_dir()to defend against that case in hooks;_wal_logis unprotected against it both before and after this PR. Out of scope.Why this shape (and not a broader refactor)
Option (2) in the issue (shared
kill_switch.pymodule + cross-module test) is the right long-term answer if the kill switch needs to be enforced across more code paths in future. Keeping this PR minimal:_wal_logIf maintainers prefer the shared-module approach, happy to extend.
Related
_palace_root_exists()on the hooks side)~/.mempalaceauto-creation gotcha, fixed in the test layer)Closes MemPalace#1676.