Skip to content

The agent that grows with you

fix(profiles): hermes update's .env backfill leaves messaging channels behind

Teknium890ddad6178e
diff --git a/hermes_cli/profiles.py b/hermes_cli/profiles.py
index 7566b722dda..db27a63cf70 100644
--- a/hermes_cli/profiles.py
+++ b/hermes_cli/profiles.py
@@ -1479,18 +1479,16 @@ def seed_profile_skills(profile_dir: Path, quiet: bool = False) -> Optional[dict
def backfill_profile_envs(quiet: bool = False) -> list[str]:
- """Give every named profile predating per-profile ``.env`` one (copy of the default's, or
- the placeholder header). Never overwrites an existing profile ``.env``.
-
- Profiles created before the dashboard/CLI started seeding a ``.env`` (PR #44792) have none, so once the
- Channels/Keys endpoints became profile-scoped those profiles stopped inheriting the root install's
- credentials and showed everything as unconfigured. To avoid breaking anyone on update, copy the DEFAULT
- install's ``.env`` into each named profile that lacks one — that preserves the effective credentials
- those profiles were already running with (they previously read the root ``.env`` via the process
- environment). Users can then diverge per profile from there.
+ """Give every named profile lacking a ``.env`` one; never overwrites an existing one.
+
+ Pre-#44792 profiles read the root ``.env`` through the process environment, so on update each
+ gets a copy of the default's (else the placeholder header) to keep its model/tool credentials.
+ Messaging-channel keys are stripped like ``--clone``: one bot credential can't be consumed twice.
"""
+ from hermes_cli.profile_channels import ChannelKeyIndex, strip_channel_env_file
backfilled: list[str] = []
- default_env = _get_default_hermes_home() / ".env"
+ default_home = _get_default_hermes_home()
+ default_env, index = default_home / ".env", None
for entry in _iter_named_profile_dirs():
env_path = entry / ".env"
if env_path.exists():
@@ -1498,6 +1496,7 @@ def backfill_profile_envs(quiet: bool = False) -> list[str]:
try:
if default_env.is_file():
shutil.copy2(default_env, env_path)
+ strip_channel_env_file(env_path, index := index or ChannelKeyIndex(default_home))
else:
env_path.write_text(_PLACEHOLDER_ENV, encoding="utf-8")
os.chmod(str(env_path), 0o600)
diff --git a/tests/hermes_cli/test_profiles_backfill_channels.py b/tests/hermes_cli/test_profiles_backfill_channels.py
new file mode 100644
index 00000000000..b2e9b01707a
--- /dev/null
+++ b/tests/hermes_cli/test_profiles_backfill_channels.py
@@ -0,0 +1,35 @@
+"""``hermes update``'s .env backfill must not hand a profile the default's bot credentials.
+
+One bot token can't be consumed twice: a copied TELEGRAM_BOT_TOKEN only ever surfaces as a
+``duplicate_credential`` fatal next to the default's live adapter (Desktop: "Backend telegram down").
+"""
+
+from __future__ import annotations
+
+from pathlib import Path
+
+import pytest
+
+from hermes_cli.profiles import backfill_profile_envs, create_profile
+
+
+def profile_env(tmp_path, monkeypatch):
+ monkeypatch.setattr(Path, "home", lambda: tmp_path)
+ default_home = tmp_path / ".hermes"
+ default_home.mkdir()
+ monkeypatch.setenv("HERMES_HOME", str(default_home))
+ return tmp_path
+
+
+def test_backfill_keeps_model_keys_and_drops_messaging_channels(profile_env):
+ (profile_env / ".hermes" / ".env").write_text(
+ "OPENROUTER_API_KEY=root-key\nTELEGRAM_BOT_TOKEN=123:abc\nTELEGRAM_ALLOWED_USERS=42\n"
+ "DISCORD_BOT_TOKEN=d1\nDISCORD_HOME_CHANNEL=9\nFAL_KEY=f\n",
+ encoding="utf-8",
+ )
+ p = create_profile("legacy", no_alias=True, no_skills=True)
+ (p / ".env").unlink()
+
+ assert backfill_profile_envs(quiet=True) == ["legacy"]
+ assert (p / ".env").read_text(encoding="utf-8-sig") == "OPENROUTER_API_KEY=root-key\nFAL_KEY=f\n"