Skip to content

The agent that grows with you

fix(openviking): preserve non-UTF-8 env bytes on update

ehz0ah38175b8c2263
diff --git a/plugins/memory/openviking/__init__.py b/plugins/memory/openviking/__init__.py
index 4bfa72a2fb8..f9ea8372985 100644
--- a/plugins/memory/openviking/__init__.py
+++ b/plugins/memory/openviking/__init__.py
@@ -1217,13 +1217,12 @@ 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)
- # 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.
+ # A Windows editor can leave a UTF-8 BOM, which prevents the first key
+ # from matching, or save the file as cp1252, which makes a strict UTF-8
+ # read fail. Strip the BOM and round-trip undecodable bytes unchanged so
+ # updating one credential cannot corrupt an unrelated value.
existing_lines = (
- env_path.read_text(encoding="utf-8-sig", errors="replace").splitlines()
+ env_path.read_text(encoding="utf-8-sig", errors="surrogateescape").splitlines()
if env_path.exists()
else []
)
@@ -1243,7 +1242,11 @@ def _write_env_vars(env_path: Path, env_writes: dict, remove_keys: tuple[str, ..
new_lines.append(f"{key}={_env_line_safe(val)}")
# Pre-create with 0600 so secrets are never briefly world-readable.
_precreate_secret_file(env_path)
- env_path.write_text("\n".join(new_lines) + ("\n" if new_lines else ""), encoding="utf-8")
+ env_path.write_text(
+ "\n".join(new_lines) + ("\n" if new_lines else ""),
+ encoding="utf-8",
+ errors="surrogateescape",
+ )
_restrict_secret_file_permissions(env_path)
diff --git a/tests/plugins/memory/test_openviking_provider.py b/tests/plugins/memory/test_openviking_provider.py
index 54d971c294e..ad1a96d47e9 100644
--- a/tests/plugins/memory/test_openviking_provider.py
+++ b/tests/plugins/memory/test_openviking_provider.py
@@ -1754,7 +1754,7 @@ class TestOpenVikingEnvWriter:
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):
+ def test_non_utf8_env_preserves_unrelated_bytes(self, tmp_path):
from plugins.memory.openviking import _write_env_vars
env = tmp_path / ".env"
@@ -1762,8 +1762,7 @@ class TestOpenVikingEnvWriter:
_write_env_vars(env, {"OPENAI_API_KEY": "new"})
- lines = env.read_text(encoding="utf-8-sig").splitlines()
- assert "OPENAI_API_KEY=new" in lines
+ assert env.read_bytes() == b"NAME=caf\xe9\nOPENAI_API_KEY=new\n"
def test_plain_env_is_unchanged_apart_from_the_write(self, tmp_path):
from plugins.memory.openviking import _write_env_vars