Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

fix(hermes-plugin): preserve .env line endings during setup (#5449)

z1gonbdba5f0c6d0a
diff --git a/examples/hermes-plugin/__init__.py b/examples/hermes-plugin/__init__.py
index 488d7c4bb..78bcaf979 100644
--- a/examples/hermes-plugin/__init__.py
+++ b/examples/hermes-plugin/__init__.py
@@ -933,7 +933,17 @@ def _write_env_vars(env_path: Path, env_writes: dict, remove_keys: tuple[str, ..
# utf-8-sig + surrogateescape: a Windows editor may leave a BOM (breaks the
# first key match) or save cp1252; 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="surrogateescape").splitlines() if env_path.exists() else []
+ # newline="": universal-newline translation on read would turn every CRLF
+ # into LF, so the file's real ending could never be detected below.
+ if env_path.exists():
+ with env_path.open("r", encoding="utf-8-sig", errors="surrogateescape", newline="") as fh:
+ existing = fh.read()
+ else:
+ existing = ""
+ existing_lines = existing.splitlines()
+ # Adopt the file's own line ending instead of the platform default: writing
+ # one variable must not rewrite every untouched line from LF to CRLF.
+ eol = "\r\n" if "\r\n" in existing else "\n"
updated_keys = set()
new_lines = []
for line in existing_lines:
@@ -945,7 +955,10 @@ def _write_env_vars(env_path: Path, env_writes: dict, remove_keys: tuple[str, ..
new_lines.append(f"{key_match}={_env_line_safe(env_writes[key_match])}" if key_match in env_writes else line)
new_lines += [f"{key}={_env_line_safe(val)}" for key, val in env_writes.items() if key not in updated_keys]
_secure_secret_file(env_path, create=True)
- env_path.write_text("\n".join(new_lines) + ("\n" if new_lines else ""), encoding="utf-8", errors="surrogateescape")
+ # newline="": ``eol`` above is the only thing allowed to decide the line
+ # ending, so text mode cannot translate it on the way out.
+ with env_path.open("w", encoding="utf-8", errors="surrogateescape", newline="") as fh:
+ fh.write(eol.join(new_lines) + (eol if new_lines else ""))
_secure_secret_file(env_path)
diff --git a/examples/hermes-plugin/tests/test_env_writes.py b/examples/hermes-plugin/tests/test_env_writes.py
new file mode 100644
index 000000000..781f16b58
--- /dev/null
+++ b/examples/hermes-plugin/tests/test_env_writes.py
@@ -0,0 +1,65 @@
+"""Preserve .env line endings through the external provider's setup writer."""
+
+import os
+import stat
+
+import pytest
+
+
+def test_lf_file_keeps_lf_on_every_platform(external_provider):
+ """Writing one variable must not retype the whole file's line endings.
+
+ Text mode on Windows translates "\n" to CRLF, so an LF .env came back
+ with every untouched line rewritten.
+ """
+ home, _, module, _ = external_provider("env-lf")
+ env = home / ".env"
+ env.write_bytes(b"A=1\nOPENAI_API_KEY=old\nB=2\n")
+
+ module._write_env_vars(env, {"OPENAI_API_KEY": "new"})
+
+ assert env.read_bytes() == b"A=1\nOPENAI_API_KEY=new\nB=2\n"
+
+
+def test_crlf_file_keeps_crlf(external_provider):
+ """A file saved with CRLF keeps its own line ending."""
+ home, _, module, _ = external_provider("env-crlf")
+ env = home / ".env"
+ env.write_bytes(b"A=1\r\nOPENAI_API_KEY=old\r\nB=2\r\n")
+
+ module._write_env_vars(env, {"OPENAI_API_KEY": "new"})
+
+ assert env.read_bytes() == b"A=1\r\nOPENAI_API_KEY=new\r\nB=2\r\n"
+
+
[email protected]("eol", [b"\n", b"\r\n"], ids=["lf", "crlf"])
+def test_env_updates_preserve_bytes_and_safe_credentials(external_provider, eol):
+ home, _, module, _ = external_provider("env-credentials")
+ env = home / ".env"
+ env.write_bytes(
+ b"\xef\xbb\xbf" + eol.join([b"OPENAI_API_KEY=old", b"NAME=caf\xe9", b"REMOVE=old"])
+ + eol
+ )
+
+ module._write_env_vars(
+ env, {"OPENAI_API_KEY": "new", "ADDED": "safe\r\nvalue\x00"},
+ remove_keys=("REMOVE", "OPENAI_API_KEY"),
+ )
+
+ assert env.read_bytes() == eol.join(
+ [b"OPENAI_API_KEY=new", b"NAME=caf\xe9", b"ADDED=safevalue"]
+ ) + eol
+ if os.name == "posix":
+ assert stat.S_IMODE(env.stat().st_mode) == 0o600
+
+
+def test_new_env_file_uses_lf_and_can_be_emptied(external_provider):
+ home, _, module, _ = external_provider("env-new")
+ env = home / "nested" / ".env"
+
+ module._write_env_vars(env, {"OPENAI_API_KEY": "new"})
+ assert env.read_bytes() == b"OPENAI_API_KEY=new\n"
+ if os.name == "posix":
+ assert stat.S_IMODE(env.stat().st_mode) == 0o600
+ module._write_env_vars(env, {}, remove_keys=("OPENAI_API_KEY",))
+ assert env.read_bytes() == b""
diff --git a/examples/hermes-plugin/tests/test_setup_profiles.py b/examples/hermes-plugin/tests/test_setup_profiles.py
index 3db203240..83085f44c 100644
--- a/examples/hermes-plugin/tests/test_setup_profiles.py
+++ b/examples/hermes-plugin/tests/test_setup_profiles.py
@@ -16,7 +16,7 @@ def patch_menu(monkeypatch, select):
monkeypatch.setattr(curses_ui, "curses_radiolist", radio)
-def setup_state(external_provider, monkeypatch, *, route="local"):
+def setup_state(external_provider, monkeypatch, *, route="local", line_ending=b"\n"):
import hermes_cli.memory_setup as setup
home, provider, module, _ = external_provider("setup-profile")
@@ -26,7 +26,9 @@ def setup_state(external_provider, monkeypatch, *, route="local"):
"memory": {"provider": "openviking", "openviking": {"recall_limit": 9}},
}
(home / "config.yaml").write_text(yaml.safe_dump(config))
- (home / ".env").write_text("UNRELATED=keep\nOPENVIKING_RECALL_SCOPE=shared\n")
+ (home / ".env").write_bytes(
+ b"UNRELATED=keep" + line_ending + b"OPENVIKING_RECALL_SCOPE=shared" + line_ending
+ )
monkeypatch.setenv("OPENVIKING_RECALL_SCOPE", "shared")
values = {"endpoint": "http://127.0.0.1:1933", "user": "example"}
saved = module._default_ovcli_config_path().with_name("ovcli.conf.existing")
@@ -66,8 +68,9 @@ def select_profile(profile, route, menus, setup):
@pytest.mark.parametrize("route", ["local", "linked", "mirror"])
@pytest.mark.parametrize("profile", ["personal", "shared"])
[email protected]("line_ending", [b"\n", b"\r\n"], ids=["lf", "crlf"])
def test_setup_persists_preset_and_real_gateway_session_boundaries(
- external_provider, monkeypatch, capsys, route, profile
+ external_provider, monkeypatch, capsys, route, profile, line_ending
):
from gateway.config import Platform
from gateway.session import build_session_key
@@ -77,7 +80,9 @@ def test_setup_persists_preset_and_real_gateway_session_boundaries(
set_hermes_home_override,
)
- home, provider, module, config, setup = setup_state(external_provider, monkeypatch, route=route)
+ home, provider, module, config, setup = setup_state(
+ external_provider, monkeypatch, route=route, line_ending=line_ending
+ )
other_home, _, _, _ = external_provider("other-profile")
other_before = (other_home / "config.yaml").read_bytes()
menus = []
@@ -105,6 +110,9 @@ def test_setup_persists_preset_and_real_gateway_session_boundaries(
assert saved["thread_sessions_per_user"] is False
assert "UNRELATED=keep" in (home / ".env").read_text()
assert "OPENVIKING_RECALL_SCOPE=" not in (home / ".env").read_text()
+ env_bytes = (home / ".env").read_bytes()
+ assert env_bytes.startswith(b"UNRELATED=keep" + line_ending)
+ assert env_bytes == line_ending.join(env_bytes.splitlines()) + line_ending
assert (
module.OpenVikingMemoryProvider._setting("recall_scope", settings)
== settings["recall_scope"]