Skip to content

Commit 1888b67

Browse files
authored
Merge pull request #1321 from MemPalace/fix/1313-init-palace-flag
fix(cli): honor --palace flag in cmd_init (#1313)
2 parents 5380189 + a91b7ee commit 1888b67

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

mempalace/cli.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,13 @@ def cmd_init(args):
232232
from .project_scanner import discover_entities
233233
from .room_detector_local import detect_rooms_local
234234

235+
# Honor --palace (issue #1313): without this, init silently ignored the
236+
# flag and always used ~/.mempalace. Mirror the env-var pattern used by
237+
# mcp_server.py so every downstream read of ``cfg.palace_path`` (Pass 0,
238+
# cfg.init(), the post-init mine) routes to the user-specified location.
239+
if getattr(args, "palace", None):
240+
os.environ["MEMPALACE_PALACE_PATH"] = os.path.abspath(os.path.expanduser(args.palace))
241+
235242
cfg = MempalaceConfig()
236243

237244
# Resolve entity-detection languages: --lang overrides config.

tests/test_cli.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,61 @@ def test_cmd_init_normalizes_wing_name_for_topics_registry(mock_config_cls, tmp_
175175
assert mock_register.call_args.kwargs["wing"] == "my_cool_app"
176176

177177

178+
def test_cmd_init_honors_palace_flag(tmp_path, monkeypatch):
179+
"""Regression for #1313: ``cmd_init`` must honor ``--palace`` instead of
180+
silently writing to ``~/.mempalace``. Mirrors the env-var pattern used
181+
by ``cmd_mine`` / ``cmd_status`` / ``mcp_server`` so every downstream
182+
read of ``cfg.palace_path`` (Pass 0, ``cfg.init()``, post-init mine)
183+
routes to the user-specified location.
184+
"""
185+
project = tmp_path / "project"
186+
project.mkdir()
187+
palace = tmp_path / "custom_palace"
188+
189+
# Make sure no leftover env var from another test leaks in — we want to
190+
# verify that --palace ALONE drives the resolution. Prime monkeypatch's
191+
# undo list with setenv first so that the env var ``cmd_init`` writes
192+
# below is rolled back at teardown (``delenv(raising=False)`` on a
193+
# missing key registers no undo entry, which would leak into the next
194+
# test).
195+
monkeypatch.setenv("MEMPALACE_PALACE_PATH", "")
196+
monkeypatch.setenv("MEMPAL_PALACE_PATH", "")
197+
monkeypatch.delenv("MEMPALACE_PALACE_PATH")
198+
monkeypatch.delenv("MEMPAL_PALACE_PATH")
199+
200+
args = argparse.Namespace(
201+
dir=str(project),
202+
palace=str(palace),
203+
yes=True,
204+
auto_mine=False,
205+
)
206+
207+
captured = {}
208+
209+
def fake_pass_zero(project_dir, palace_dir, llm_provider):
210+
# Capture the palace_dir Pass 0 sees — this is the smoking-gun
211+
# value for the bug. Pre-fix it was always ~/.mempalace.
212+
captured["pass_zero_palace_dir"] = palace_dir
213+
return None
214+
215+
with (
216+
patch("mempalace.entity_detector.scan_for_detection", return_value=[]),
217+
patch("mempalace.room_detector_local.detect_rooms_local"),
218+
patch("mempalace.cli._run_pass_zero", side_effect=fake_pass_zero),
219+
patch("mempalace.cli._maybe_run_mine_after_init"),
220+
):
221+
cmd_init(args)
222+
223+
expected = str(palace)
224+
# Pass 0 must have been handed the --palace location, not ~/.mempalace.
225+
assert captured["pass_zero_palace_dir"] == expected
226+
# And the env var must point at the custom palace so any downstream
227+
# ``cfg.palace_path`` read in this process resolves correctly too.
228+
import os
229+
230+
assert os.environ.get("MEMPALACE_PALACE_PATH") == os.path.abspath(expected)
231+
232+
178233
@patch("mempalace.cli.MempalaceConfig")
179234
def test_cmd_init_with_entities_zero_total(mock_config_cls, tmp_path, capsys):
180235
"""When entities detected but total is 0, prints 'No entities' message."""

0 commit comments

Comments
 (0)