The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(memory): read/write .env as UTF-8 in mem0 and hindsight setup
solyanviktor-star75afc47baa52
diff --git a/plugins/memory/mem0/_setup.py b/plugins/memory/mem0/_setup.pyindex e84308a5fb0..df0b32c1153 100644--- a/plugins/memory/mem0/_setup.py+++ b/plugins/memory/mem0/_setup.py@@ -193,7 +193,14 @@ def _write_env(env_path: Path, env_writes: dict[str, str]) -> None:env_path.parent.mkdir(parents=True, exist_ok=True)existing_lines: list[str] = []if env_path.exists():- existing_lines = env_path.read_text().splitlines()+ # Read as UTF-8 (BOM-tolerant), matching the canonical .env readers in+ # hermes_cli/config.py. read_text() with no encoding falls back to the+ # system locale (cp1252/GBK on Windows): it mangles or crashes on+ # non-ASCII values while copying existing lines through, and a BOM'd+ # first line would fail the key match and get duplicated.+ existing_lines = env_path.read_text(+ encoding="utf-8-sig"+ ).splitlines()updated_keys: set[str] = set()new_lines: list[str] = []@@ -208,7 +215,7 @@ def _write_env(env_path: Path, env_writes: dict[str, str]) -> None:if k not in updated_keys:new_lines.append(f"{k}={v}")- env_path.write_text("\n".join(new_lines) + "\n")+ env_path.write_text("\n".join(new_lines) + "\n", encoding="utf-8")def _save_mem0_json(hermes_home: str, data: dict) -> None:diff --git a/tests/plugins/memory/test_mem0_setup.py b/tests/plugins/memory/test_mem0_setup.pyindex cbe8353a48b..eeebe4d9126 100644--- a/tests/plugins/memory/test_mem0_setup.py+++ b/tests/plugins/memory/test_mem0_setup.py@@ -181,6 +181,26 @@ class TestWriteEnv:assert "OTHER=keep" in contentassert "old" not in content+ def test_preserves_non_ascii_existing_lines(self, tmp_path):+ """Existing non-ASCII .env content must survive the read-modify-write+ as UTF-8 (the locale codec would crash/mangle it on Windows)."""+ env_path = tmp_path / ".env"+ env_path.write_bytes("PROXY_NOTE=café-zürich-完了\n".encode("utf-8"))+ _write_env(env_path, {"OPENAI_API_KEY": "sk-test"})+ content = env_path.read_text(encoding="utf-8")+ assert "PROXY_NOTE=café-zürich-完了" in content+ assert "OPENAI_API_KEY=sk-test" in content++ def test_updates_first_key_with_bom(self, tmp_path):+ """A Notepad-edited .env carries a BOM; the first key must still be+ matched/updated in place, not duplicated."""+ env_path = tmp_path / ".env"+ env_path.write_bytes("OPENAI_API_KEY=old\n".encode("utf-8"))+ _write_env(env_path, {"OPENAI_API_KEY": "new"})+ content = env_path.read_text(encoding="utf-8")+ assert content.count("OPENAI_API_KEY=") == 1+ assert "OPENAI_API_KEY=new" in content+class TestPostSetup: