iai-personal-memory-engine icon indicating copy to clipboard operation
iai-personal-memory-engine copied to clipboard

[bug] Windows: hermetic test fixture is a no-op — the suite rewrites the operator's real crypto key and makes existing records undecryptable

Open chrismartinsoares opened this issue 3 months ago • 1 comments

Summary

tests/conftest.py's _hermetic_default_paths fixture patches only HOME. On Windows that does not redirect Path.home(), so the fixture is a no-op there and the test suite reads and writes the operator's real store.

On my machine, running pytest rewrote ~/.iai-mcp/.crypto.key. The 297 existing records had been encrypted with the previous key, no .pre-rotate copy is retained, and .crypto-key-watcher.json stores only mtime+size — so those records became permanently undecryptable. memory_recall now returns InvalidTag for all of them.

Why

os.path.expanduser on Windows resolves USERPROFILE first, then HOMEDRIVE+HOMEPATH, and falls back to HOME only when none of them is set:

HOME        = C:\...\Temp\fake-home
USERPROFILE = C:\Users\cmartin
Path.home() = C:\Users\cmartin      <-- HOME ignored

Every test resolving Path.home() / ".iai-mcp" therefore reached the live store.

Reproduce

On Windows, with an existing store that holds records:

pytest -q tests/test_cli_crypto.py tests/test_crypto.py tests/test_hippo_crypto.py

The mtime of ~/.iai-mcp/.crypto.key advances. Recall then fails with InvalidTag.

Suggested fix

Patch the Windows variables alongside HOME:

monkeypatch.setenv("HOME", str(base))
monkeypatch.setenv("USERPROFILE", str(base))
drive, tail = os.path.splitdrive(str(base))
monkeypatch.setenv("HOMEDRIVE", drive)
monkeypatch.setenv("HOMEPATH", tail or "\\")

Because the failure mode is silent, a regression guard seems worthwhile too — a test asserting the hermetic marker appears in str(Path.home()) fails loudly if isolation ever breaks again, on any platform.

Environment

Windows 11, Python 3.11.15, iai-pme 3.0.2 (cp311 win_amd64 wheel from PyPI).

Verified fixed on my side. Happy to open a PR with the fixture change plus the guard.


Distinct from #109 and #113: those cover POSIX-only APIs outside their platform guards at daemon boot. This one is the test fixture, and its consequence is destroyed user data rather than a failed start.

chrismartinsoares avatar Aug 18 '26 08:08 chrismartinsoares

Fixed in v3.0.3 — the hermetic fixture now redirects USERPROFILE/HOMEDRIVE/HOMEPATH as well as HOME, so the suite can't touch a real store on Windows. Thanks for the precise diagnosis.

CodeAbra avatar Aug 19 '26 05:08 CodeAbra