cli(session): defer module-level _STATE_DIR expansion to runtime (#137)#146
Conversation
|
I have read the CLA Document and I hereby sign the CLA on this PR |
|
All contributors have signed the CLA. ✍️ ✅ |
…tomem#137) Replace module-level constant _STATE_DIR = Path('~').expanduser() with a lazy _state_dir() -> Path helper that calls Path.home() at each invocation. This allows test isolation via monkeypatch.setenv(). - _STATE_DIR usages at mkdir() call site updated to _state_dir() - _STATE_FILE still computed at module level (harmless: only used for path construction, not for existence/mkdir) mypy: 0 errors (no new type:ignore)
f7f7fac to
f4bd68a
Compare
|
I have read the CLA Document and I hereby sign the CLA on this PR |
|
recheck |
|
I have read the CLA Document and I hereby sign the CLA |
|
recheck |
memtomem
left a comment
There was a problem hiding this comment.
Thanks for identifying the frozen-at-import issue — the fix direction is right, but there's an inconsistency that undermines the test-isolation goal:
`_STATE_FILE` is still resolved at import time:
def _state_dir() -> Path:
return Path.home() / ".memtomem"
_STATE_FILE = _state_dir() / ".current_session" # ← called once at importAfter this PR, `_write_current_session` does:
_state_dir().mkdir(...) # resolves HOME dynamically ✓
_STATE_FILE.write_text() # uses the import-time path ✗If a test patches `HOME`, the `mkdir` creates the directory under the new home, but `write_text` / `read_text` / `unlink` still target the original home. Same issue in `_read_current_session` and `_clear_current_session`.
Suggested fix — make both lazy:
def _state_dir() -> Path:
return Path.home() / ".memtomem"
def _state_file() -> Path:
return _state_dir() / ".current_session"Then replace the 4 `_STATE_FILE` references (lines 20, 28, 33) with `_state_file()`. This keeps the same production behavior while actually making test isolation work end-to-end.
CLA note: please re-sign the CLA per the comment on #145 — the earlier signature from #130 didn't persist due to a now-fixed infrastructure issue (#143).
|
Thanks for tackling #137! Flagging a subtle issue before merge:
The new _STATE_FILE = _state_dir() / ".current_session" # Path.home() called once, result frozen
Suggested fix — mirror def _state_file() -> Path:
return _state_dir() / ".current_session"Then replace Testing suggestion — add a round-trip test to lock the behavior in: def test_session_state_respects_home_override(tmp_path, monkeypatch):
monkeypatch.setenv("HOME", str(tmp_path))
_write_current_session("abc-123")
assert _state_file().exists()
assert _read_current_session() == "abc-123"This proves the fix end-to-end (not just |
memtomem
left a comment
There was a problem hiding this comment.
Nice idea — deferring HOME resolution is the right fix for test isolation.
One issue: _STATE_FILE is still computed at module level:
_STATE_FILE = _state_dir() / ".current_session"This calls _state_dir() (and therefore Path.home()) at import time, so _STATE_FILE is frozen. _read_current_session and _clear_current_session both use _STATE_FILE directly, so monkeypatching HOME after import won't affect them.
Suggestion — make _STATE_FILE lazy too:
def _state_file() -> Path:
return _state_dir() / ".current_session"Then replace _STATE_FILE references in _read_current_session, _write_current_session, and _clear_current_session with _state_file(). That completes the deferral.
Per @memtomem's review: _STATE_FILE was still computed at module level via _state_dir() / '.current_session', which calls Path.home() at import time. This defeats the test-isolation goal of memtomem#137. Changed to a _state_file() -> Path function so every call site resolves HOME at call time, not at import time. Replaces: _STATE_FILE (module-level) → _state_file() (lazy)
|
Thanks @memtomem — great catch! I made Fixed (pushed): - _STATE_FILE = _state_dir() / ".current_session"
+ def _state_file() -> Path:
+ return _state_dir() / ".current_session"All 4 call sites updated ( |
memtomem
left a comment
There was a problem hiding this comment.
Looks good — both _state_dir() and _state_file() are now lazy, completing the HOME deferral. Thanks for the quick fix!
Summary
Fixes #137 — Replace module-level
_STATE_DIR = Path("~/.memtomem").expanduser()with a lazy_state_dir() -> Pathhelper that resolvesPath.home()at each call.Problem
The old code captured
~expansion once at import time:Any subsequent
monkeypatch.setenv("HOME", ...)in test code cannot relocate it, limiting test isolation for the session CLI.Solution
_STATE_FILEstill computed at module level (harmless — path construction only)_STATE_DIR.mkdir(...)call site updated to_state_dir().mkdir(...)Test Plan
from memtomem.cli.session_cmd import _state_dir; _state_dir()returns valid Pathuv run mypy packages/memtomem/src/memtomem/cli/session_cmd.py— 0 errorsuv run pytest -m "not ollama"— 1272/1272 OK# type: ignorelinesAI Usage Disclosure
None — human-authored.
Files Changed (1 file, +6/-3)
packages/memtomem/src/memtomem/cli/session_cmd.py