Skip to content

The agent that grows with you

fix(openviking): read .env BOM-tolerantly when rewriting credentials

Drexuxuxdde7075d6c87
diff --git a/plugins/memory/openviking/__init__.py b/plugins/memory/openviking/__init__.py
index e1235f04783..4bfa72a2fb8 100644
--- a/plugins/memory/openviking/__init__.py
+++ b/plugins/memory/openviking/__init__.py
@@ -1217,7 +1217,16 @@ def _env_line_safe(value: Any) -> str:
def _write_env_vars(env_path: Path, env_writes: dict, remove_keys: tuple[str, ...] = ()) -> None:
env_path.parent.mkdir(parents=True, exist_ok=True)
remove_set = set(remove_keys) - set(env_writes)
- existing_lines = env_path.read_text(encoding="utf-8").splitlines() if env_path.exists() else []
+ # Read exactly like the canonical .env reader in hermes_cli/config.py
+ # (save_env_value). A Windows editor can leave a UTF-8 BOM, which fails
+ # the key match on the first line so that key gets duplicated on write,
+ # or save the file as cp1252, which makes a strict utf-8 read raise and
+ # abort setup. Copying existing lines through must never do either.
+ existing_lines = (
+ env_path.read_text(encoding="utf-8-sig", errors="replace").splitlines()
+ if env_path.exists()
+ else []
+ )
updated_keys = set()
new_lines = []
for line in existing_lines:
diff --git a/tests/plugins/memory/test_openviking_provider.py b/tests/plugins/memory/test_openviking_provider.py
index 358ecaa8829..54d971c294e 100644
--- a/tests/plugins/memory/test_openviking_provider.py
+++ b/tests/plugins/memory/test_openviking_provider.py
@@ -1729,3 +1729,50 @@ def test_is_available_false_without_any_endpoint(monkeypatch):
openviking_module, "_load_hermes_openviking_config", lambda: {}
)
assert OpenVikingMemoryProvider().is_available() is False
+
+
+class TestOpenVikingEnvWriter:
+ """``_write_env_vars`` copies existing .env lines through on every update,
+ so how it *reads* them decides whether a credential update lands.
+
+ f1ea4a56c ("cover the remaining setup-time .env reads with utf-8-sig")
+ swept this class; this writer was missed.
+ """
+
+ def test_bom_prefixed_env_updates_in_place(self, tmp_path):
+ from plugins.memory.openviking import _write_env_vars
+
+ env = tmp_path / ".env"
+ env.write_bytes(b"\xef\xbb\xbfOPENAI_API_KEY=old\nOTHER=1\n")
+
+ _write_env_vars(env, {"OPENAI_API_KEY": "new"})
+
+ lines = [l for l in env.read_text(encoding="utf-8-sig").splitlines() if l]
+ # The stale value must be gone, not shadowed by an appended duplicate:
+ # .env loaders keep the first occurrence, so a duplicate silently wins.
+ assert lines.count("OPENAI_API_KEY=new") == 1
+ assert not any(l.endswith("=old") for l in lines)
+ assert "OTHER=1" in lines
+
+ def test_non_utf8_env_does_not_abort_setup(self, tmp_path):
+ from plugins.memory.openviking import _write_env_vars
+
+ env = tmp_path / ".env"
+ env.write_bytes(b"NAME=caf\xe9\nOPENAI_API_KEY=old\n")
+
+ _write_env_vars(env, {"OPENAI_API_KEY": "new"})
+
+ lines = env.read_text(encoding="utf-8-sig").splitlines()
+ assert "OPENAI_API_KEY=new" in lines
+
+ def test_plain_env_is_unchanged_apart_from_the_write(self, tmp_path):
+ from plugins.memory.openviking import _write_env_vars
+
+ env = tmp_path / ".env"
+ env.write_text("A=1\nOPENAI_API_KEY=old\nB=2\n", encoding="utf-8")
+
+ _write_env_vars(env, {"OPENAI_API_KEY": "new"})
+
+ assert env.read_text(encoding="utf-8").splitlines() == [
+ "A=1", "OPENAI_API_KEY=new", "B=2",
+ ]