Skip to content

The agent that grows with you

fix(update): a proxy the user's .env sets survives the post-update restore

Teknium008b6a374abb
diff --git a/hermes_cli/env_loader.py b/hermes_cli/env_loader.py
index 89e1522a982..36c0e694dfb 100644
--- a/hermes_cli/env_loader.py
+++ b/hermes_cli/env_loader.py
@@ -155,6 +155,14 @@ def launch_dotenv_keys() -> frozenset[str]:
return frozenset(_LOADED_DOTENV_KEYS)
+def dotenv_published_values(name: str) -> frozenset[str]:
+ """The values dotenv loads in this process last wrote to ``name``, its name matched case-insensitively:
+ on Windows ``https_proxy`` in a .env and an inherited ``HTTPS_PROXY`` are one environment variable."""
+ folded = name.upper()
+ with _DOTENV_LOCK:
+ return frozenset(value for key, (_base, value, _pass) in _DOTENV_PUBLISHED.items() if key.upper() == folded)
+
+
def managed_dotenv_keys() -> frozenset[str]:
"""KEY names the administrator-managed ``.env`` loaded (see ``_MANAGED_DOTENV_KEYS``). Policy for
every profile: never stripped from a routed child, and re-applied over the routed scope."""
diff --git a/hermes_cli/update_proxy_env.py b/hermes_cli/update_proxy_env.py
index 15cc73b31d5..3185d354ccc 100644
--- a/hermes_cli/update_proxy_env.py
+++ b/hermes_cli/update_proxy_env.py
@@ -8,7 +8,12 @@ the user's setting, and every host the PAC sends direct (LAN model servers,
internal names) would go through the proxy. ``HERMES_UPDATE_SYSTEM_PROXY`` names
the variables the Desktop added and ``HERMES_UPDATE_USER_<KEY>`` holds a user's
own NO_PROXY it extended; a long-lived process restores its env from them at
-start. Same algorithm as restoreUserProxyEnv, posix.sh and windows.ps1.
+start. Same algorithm as restoreUserProxyEnv, posix.sh and windows.ps1, plus one
+rule only a Hermes process needs: a value the user's ``.env`` set wins. ``hermes
+update`` and the restarted process load ``~/.hermes/.env`` with override before
+this runs, so a marked key holding the value a dotenv load published is the
+user's own setting (same value as the Desktop's or not) and is kept. The Desktop
+and the hand-off scripts never load ``.env``, so their envs never hit that case.
"""
from __future__ import annotations
@@ -23,12 +28,18 @@ _NO_PROXY_KEYS = ("NO_PROXY", "no_proxy")
def restore_user_proxy_env(environ: MutableMapping[str, str] | None = None) -> None:
"""Drop the proxy variables the Desktop added and restore the user's own NO_PROXY."""
+ from hermes_cli.env_loader import dotenv_published_values
+
env = os.environ if environ is None else environ
+
+ def users_dotenv_value(key: str) -> bool:
+ return key in env and env[key] in dotenv_published_values(key)
+
for key in env.get(MARKER, "").split(","):
- if key in _ADDED_KEYS:
+ if key in _ADDED_KEYS and not users_dotenv_value(key):
env.pop(key, None)
for key in _NO_PROXY_KEYS:
saved = env.pop(SAVED_PREFIX + key, None)
- if saved is not None:
+ if saved is not None and not users_dotenv_value(key):
env[key] = saved
env.pop(MARKER, None)
diff --git a/tests/hermes_cli/test_update_proxy_env.py b/tests/hermes_cli/test_update_proxy_env.py
index 2a6ea44591a..eece23d9659 100644
--- a/tests/hermes_cli/test_update_proxy_env.py
+++ b/tests/hermes_cli/test_update_proxy_env.py
@@ -17,3 +17,29 @@ def test_marked_update_proxy_env_is_undone_and_the_users_own_env_kept():
restore_user_proxy_env(unmarked)
assert unmarked == {"HTTPS_PROXY": "http://mine:3128", "NO_PROXY": "lan"}
assert "HERMES_UPDATE_SYSTEM_PROXY" not in os.environ
+
+
+
+def test_a_proxy_the_users_dotenv_sets_survives_the_restore(tmp_path, monkeypatch):
+ """`hermes update` restarts a gateway/backend with the Desktop's marked env; that process loads the
+ user's .env (override) at import, then restores. The .env value is the user's own. The .env spells the
+ proxy lower-case: on Windows that is the same variable as the inherited upper-case one."""
+ from hermes_cli import env_loader
+
+ monkeypatch.setattr(env_loader, "_DOTENV_PUBLISHED", {})
+ monkeypatch.setattr(env_loader, "_LOADED_DOTENV_KEYS", set())
+ monkeypatch.setenv("HTTPS_PROXY", "http://10.0.0.7:7890")
+ monkeypatch.setenv("HTTP_PROXY", "http://10.0.0.7:7890")
+ monkeypatch.setenv("NO_PROXY", "corp.internal,localhost,10.0.0.0/8")
+ monkeypatch.setenv("HERMES_UPDATE_SYSTEM_PROXY", "HTTPS_PROXY,https_proxy,HTTP_PROXY,http_proxy")
+ monkeypatch.setenv("HERMES_UPDATE_USER_NO_PROXY", "corp.internal")
+ home = tmp_path / "hermes-home"
+ home.mkdir()
+ (home / ".env").write_text("https_proxy=http://user:[email protected]:3128\nNO_PROXY=lan.example\n", encoding="utf-8")
+
+ env_loader.load_hermes_dotenv(hermes_home=home, load_external_secrets=False)
+ restore_user_proxy_env()
+
+ proxy_env = {key: os.environ.get(key) for key in ("https_proxy", "HTTP_PROXY", "NO_PROXY", "HERMES_UPDATE_USER_NO_PROXY")}
+ assert proxy_env == {"https_proxy": "http://user:[email protected]:3128", "HTTP_PROXY": None, "NO_PROXY": "lan.example",
+ "HERMES_UPDATE_USER_NO_PROXY": None}