Skip to content

The agent that grows with you

fix(config): route every registered env setting through .env from config set/get/unset

teknium170e4938c07d4
diff --git a/hermes_cli/AGENTS.md b/hermes_cli/AGENTS.md
index 72aa68ac91c..087e5a207eb 100644
--- a/hermes_cli/AGENTS.md
+++ b/hermes_cli/AGENTS.md
@@ -65,7 +65,10 @@ Do not add a surface-specific goal parser. ACP has no goal command or goal loop
`{"description", "prompt", "url", "password": True, "category": provider|tool|messaging|setting}`.
Non-secret settings go in config.yaml; if internal code needs an env mirror, bridge it in code
(`gateway_timeout`; `terminal.cwd` → `TERMINAL_CWD`). `MESSAGING_CWD` is removed and `TERMINAL_CWD`
- in `.env` is deprecated — the loader warns; canonical is `terminal.cwd`.
+ in `.env` is deprecated — the loader warns; canonical is `terminal.cwd`. `hermes config
+ set/get/unset <NAME>` route any bare name registered in `OPTIONAL_ENV_VARS` / `_EXTRA_ENV_KEYS`
+ (or carrying a `setup_hidden_env` platform suffix) to `.env` via `config_env_routing.py` — the
+ file the platform setup flows write — never to the top level of config.yaml.
- **Three loaders — know which you're in:** `load_cli_config()` (CLI, `cli.py`); `load_config()`
(`hermes tools/setup`, most subcommands, `hermes_cli/config.py`, merges `DEFAULT_CONFIG`);
`hermes_cli/config_effective.py::load_user_config_effective()` (gateway runtime via
diff --git a/hermes_cli/config.py b/hermes_cli/config.py
index 5c67f10ae5c..03626fcd542 100644
--- a/hermes_cli/config.py
+++ b/hermes_cli/config.py
@@ -945,23 +945,15 @@ _ENV_CONFIG_KEYS = frozenset({
'GITHUB_TOKEN', 'HONCHO_API_KEY'})
-def _is_platform_env_config_key(key: str) -> bool:
- """Return whether a platform setup value is owned by ``.env``."""
- if "." in key:
- return False
- from hermes_cli.setup_hidden_env import is_setup_hidden_env
-
- return is_setup_hidden_env(key.upper())
-
-
def _is_env_config_key(key: str) -> bool:
- """Return whether `hermes config set` routes this key to .env."""
+ """Return whether `hermes config set` routes this credential-shaped key to .env through the
+ provider credential lifecycle. Non-secret env settings (``*_HOME_CHANNEL``, ``*_ALLOWED_USERS``)
+ are ``config_env_routing.is_env_setting_key`` and take the plain ``.env`` path."""
if "." in key:
return False
key_upper = key.upper()
return (
- _is_platform_env_config_key(key)
- or key_upper in _ENV_CONFIG_KEYS
+ key_upper in _ENV_CONFIG_KEYS
or key_upper.endswith(('_API_KEY', '_TOKEN', '_SECRET'))
or key_upper.startswith('TERMINAL_SSH'))
@@ -3527,11 +3519,6 @@ def set_config_value(key: str, value: str, force: bool = False):
"(leading, trailing, or doubled '.').")
_exit_if_key_managed(key, "set")
if _is_env_config_key(key):
- if _is_platform_env_config_key(key):
- save_env_value(key.upper(), value)
- print(f"✓ Set {key} in {get_env_path()}")
- return
- # Unified lifecycle: also rotates any config.yaml mirror of the old value.
from hermes_cli.credential_lifecycle import save_provider_env_credential
# Unified lifecycle: also rotates any config.yaml mirror of the old value so a stale
@@ -3539,6 +3526,13 @@ def set_config_value(key: str, value: str, force: bool = False):
save_provider_env_credential(key.upper(), value)
print(f"✓ Set {key} in {get_env_path()}")
return
+ from hermes_cli.config_env_routing import is_env_setting_key, save_env_setting
+
+ if is_env_setting_key(key):
+ # Same file the platform setup flows and /sethome write (#111848).
+ save_env_setting(key, value)
+ print(f"✓ Set {key} in {get_env_path()}")
+ return
# Canonicalize per-platform display keys BEFORE validation/coercion so both see the path the
# runtime reads. Unknown keys are still written (top-level scalars are bridged into os.environ
@@ -3603,9 +3597,14 @@ def get_config_value(key: str, *, as_json: bool = False, raw: bool = False):
"""Print a resolved configuration value. Credentials are masked unless ``--raw`` or
``security.redact_secrets: false``: ``print`` bypasses the log redactor, and the agent runs
this command from sessions whose transcripts persist (#84106, #110758)."""
+ from hermes_cli.config_env_routing import is_env_setting_key, read_env_setting
+
if _is_env_config_key(key):
env_value = get_env_value(key.upper())
value = _MISSING if env_value is None else env_value
+ elif is_env_setting_key(key):
+ env_value = read_env_setting(key)
+ value = _MISSING if env_value is None else env_value
else:
# Mirror set_config_value: read the canonical display.platforms path.
# See #71047.
@@ -3634,11 +3633,6 @@ def unset_config_value(key: str):
_exit_if_key_managed(key, "unset")
if _is_env_config_key(key):
- if _is_platform_env_config_key(key):
- if not remove_env_value(key.upper()):
- _exit_invalid(f"Config key not set: {key}")
- print(f"✓ Unset {key} from {get_env_path()}")
- return
# Unified lifecycle: also prunes env-seeded credential_pool entries and model-cache rows so
# the provider is fully removed instead of left resurrectable.
# See #51071.
@@ -3648,6 +3642,14 @@ def unset_config_value(key: str):
_exit_invalid(f"Config key not set: {key}")
print(f"✓ Unset {key} from {get_env_path()}")
return
+ from hermes_cli.config_env_routing import is_env_setting_key, remove_env_setting
+
+ if is_env_setting_key(key):
+ # Also drops a stale top-level config.yaml copy left by older `config set` runs (#111848).
+ if not remove_env_setting(key):
+ _exit_invalid(f"Config key not set: {key}")
+ print(f"✓ Unset {key} from {get_env_path()}")
+ return
config_path = get_config_path()
user_config = require_readable_config_before_write(config_path)
diff --git a/hermes_cli/config_env_routing.py b/hermes_cli/config_env_routing.py
new file mode 100644
index 00000000000..fbbf1b24d8d
--- /dev/null
+++ b/hermes_cli/config_env_routing.py
@@ -0,0 +1,67 @@
+"""Which ``hermes config`` keys live in ``.env`` instead of ``config.yaml``, and their lifecycle.
+
+Platform setting keys such as ``FEISHU_HOME_CHANNEL`` had two writers: the platform setup flows and
+``/sethome`` persist them to ``.env`` through ``save_env_value``, while ``hermes config set`` only
+routed credential-shaped names there and wrote every other bare name to the top level of
+``config.yaml``. The gateway bridges top-level scalars into the environment only when ``.env`` lacks
+the name and one-shot CLI readers never bridge, so the two copies diverged silently (#111848).
+Every name Hermes itself registers as an environment variable now routes to ``.env`` from
+``set``/``get``/``unset``; provider credentials keep their own rotation lifecycle in
+``hermes_cli.credential_lifecycle``.
+"""
+
+from typing import Optional
+
+
+def is_env_setting_key(key: str) -> bool:
+ """True for a bare (undotted) name Hermes documents as a ``.env`` variable: registered in
+ ``OPTIONAL_ENV_VARS`` or ``_EXTRA_ENV_KEYS``, or carrying a self-configuring platform suffix so
+ plugin adapters nobody enumerated (``IRC_HOME_CHANNEL``) get the same routing."""
+ if "." in key:
+ return False
+ from hermes_cli.config import _EXTRA_ENV_KEYS, OPTIONAL_ENV_VARS
+ from hermes_cli.setup_hidden_env import is_setup_hidden_env
+
+ name = key.upper()
+ return name in OPTIONAL_ENV_VARS or name in _EXTRA_ENV_KEYS or is_setup_hidden_env(name)
+
+
+def _drop_config_yaml_copies(key: str) -> bool:
+ """Remove same-named top-level ``config.yaml`` copies (as typed and upper-cased) so the ``.env``
+ value is the only one the gateway bridge and CLI readers can disagree about."""
+ from hermes_cli.config import _write_user_config, get_config_path, require_readable_config_before_write
+
+ config_path = get_config_path()
+ user_config = require_readable_config_before_write(config_path)
+ stale = [name for name in {key, key.upper()} if name in user_config]
+ for name in stale:
+ del user_config[name]
+ if stale:
+ _write_user_config(config_path, user_config)
+ return bool(stale)
+
+
+def save_env_setting(key: str, value: str) -> None:
+ from hermes_cli.config import save_env_value
+
+ save_env_value(key.upper(), value)
+ _drop_config_yaml_copies(key)
+
+
+def remove_env_setting(key: str) -> bool:
+ """Remove the ``.env`` entry and any stale ``config.yaml`` copy; False when neither existed."""
+ from hermes_cli.config import remove_env_value
+
+ removed = remove_env_value(key.upper())
+ return _drop_config_yaml_copies(key) or removed
+
+
+def read_env_setting(key: str) -> Optional[str]:
+ """Resolve like the gateway does: ``.env`` first, then a not-yet-converged top-level
+ ``config.yaml`` copy under the name as typed."""
+ from hermes_cli.config import get_env_value, read_raw_config_readonly
+
+ value = get_env_value(key.upper())
+ if value is None:
+ value = read_raw_config_readonly().get(key)
+ return value
diff --git a/tests/hermes_cli/test_config_platform_env_routing.py b/tests/hermes_cli/test_config_platform_env_routing.py
index 3b0f361ee5d..36b1ef72c15 100644
--- a/tests/hermes_cli/test_config_platform_env_routing.py
+++ b/tests/hermes_cli/test_config_platform_env_routing.py
@@ -1,5 +1,7 @@
-"""Regression coverage for platform setup values managed through ``.env``."""
+"""``hermes config set/get/unset`` route every name Hermes registers as an environment variable to
+``.env`` — the file the platform setup flows and ``/sethome`` already write (#111848)."""
+import pytest
import yaml
@@ -29,3 +31,30 @@ def test_platform_env_key_round_trips_without_a_config_yaml_copy(tmp_path, monke
assert yaml.safe_load(config_path.read_text(encoding="utf-8")) == {
"model": {"default": "test/model"}
}
+
+
+def test_registered_env_setting_converges_stale_config_yaml_copy(tmp_path, monkeypatch, capsys):
+ """A non-suffix adapter key (``*_ALLOWED_USERS``) routes to ``.env``; a top-level ``config.yaml``
+ copy left by an older ``config set`` is dropped on ``set`` and ``unset`` so one reader can't see a
+ value the other doesn't. Credentials keep their own ``.env`` lifecycle (control)."""
+ from hermes_cli import config as cfg
+
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path))
+ monkeypatch.setenv("HERMES_MANAGED_DIR", str(tmp_path / "managed"))
+ config_path = tmp_path / "config.yaml"
+ config_path.write_text(
+ "model:\n default: test/model\nDISCORD_ALLOWED_USERS: '111'\nWHATSAPP_MODE: web\n",
+ encoding="utf-8")
+
+ cfg.set_config_value("DISCORD_ALLOWED_USERS", "222")
+ cfg.set_config_value("TAVILY_API_KEY", "tvly-control")
+ env_text = (tmp_path / ".env").read_text(encoding="utf-8")
+ assert "DISCORD_ALLOWED_USERS=222" in env_text and "TAVILY_API_KEY=tvly-control" in env_text
+ assert yaml.safe_load(config_path.read_text(encoding="utf-8")) == {
+ "model": {"default": "test/model"}, "WHATSAPP_MODE": "web"}
+
+ cfg.unset_config_value("WHATSAPP_MODE") # only a stale yaml copy existed
+ assert yaml.safe_load(config_path.read_text(encoding="utf-8")) == {"model": {"default": "test/model"}}
+ capsys.readouterr()
+ with pytest.raises(SystemExit):
+ cfg.get_config_value("WHATSAPP_MODE")
diff --git a/website/docs/reference/environment-variables.md b/website/docs/reference/environment-variables.md
index 01b16f3db58..c46c308efa1 100644
--- a/website/docs/reference/environment-variables.md
+++ b/website/docs/reference/environment-variables.md
@@ -948,5 +948,5 @@ These go in `~/.hermes/config.yaml` under the `provider_routing` section:
| `data_collection` | `"allow"` (default) or `"deny"` to exclude data-storing providers |
:::tip
-Use `hermes config set` to set environment variables — it automatically saves them to the right file (`.env` for secrets, `config.yaml` for everything else).
+Use `hermes config set` to set environment variables — every variable documented on this page (and any platform `*_HOME_CHANNEL` / `*_ALLOWED_USERS` style setting) is saved to `.env`, the same file the setup flows write; dotted `config.yaml` settings go to `config.yaml`.
:::