Skip to content

The agent that grows with you

fix(env): self-referential .env values resolve once per process instead of growing on every reload

Austin Pickett369fb8ef2577
diff --git a/hermes_cli/env_loader.py b/hermes_cli/env_loader.py
index 2f2f895eb54..6a1529fb8cc 100644
--- a/hermes_cli/env_loader.py
+++ b/hermes_cli/env_loader.py
@@ -4,13 +4,15 @@ from __future__ import annotations
import codecs
import io
+import itertools
import logging
import os
import sys
import threading
from pathlib import Path
-from dotenv import load_dotenv
+from dotenv.main import DotEnv
+from dotenv.variables import parse_variables
from utils import atomic_replace, fast_safe_load
logger = logging.getLogger(__name__)
@@ -36,6 +38,18 @@ _SECRET_SOURCE_VALUES_BY_HOME: dict[str, dict[str, str]] = {}
_APPLIED_HOMES: set[str] = set()
_SECRET_SOURCE_CACHE_LOCK = threading.RLock()
+# What THIS process has published from dotenv files, per variable: (value before our first publish, or
+# None if absent; last value we published; load pass that published it). Reloads run per gateway turn and
+# per cron fire, and a line like ``PATH=/x:${PATH}`` interpolated against an environ that already holds the
+# previous reload's output grows by ``/x:`` every time until child spawns fail with E2BIG (#109902). A new
+# pass resolves against the baseline instead — but only where the environ still holds exactly what we
+# published, so a value the shell, config bridge, or an external secret source changed since is not frozen.
+# One process-wide record (not per home/project scope): the environ is process-wide, so alternating
+# callers (gateway with a project .env, cron without; home A then B) must peel each other's output too.
+_DOTENV_PUBLISHED: dict[str, tuple[str | None, str, int]] = {}
+_DOTENV_PASSES = itertools.count()
+_DOTENV_LOCK = threading.RLock()
+
# Behavioral routing keys a parent Hermes process injects into child env that silently redirect a profile
# onto the wrong provider path; these — and ONLY these — are scrubbed at startup when absent from the
# profile's .env. Credentials are excluded: shell exports are a documented way to supply them, and
@@ -234,16 +248,50 @@ def _sanitize_loaded_credentials() -> None:
)
-def _load_dotenv_with_fallback(path: Path, *, override: bool) -> None:
+def _load_dotenv_with_fallback(path: Path, *, override: bool, load_pass: int | None = None) -> None:
+ """Load one dotenv file into ``os.environ`` like ``dotenv.load_dotenv`` — same parser, same
+ ``${VAR}`` / ``${VAR:-default}`` / precedence rules — except that ``${VAR}`` resolves against the value
+ VAR had before this process's earlier passes published it (see ``_DOTENV_PUBLISHED``).
+
+ ``load_pass`` groups the layered files of one ``load_hermes_dotenv`` call: within a pass a later layer
+ (project, managed) still sees the earlier layer's output, as it always did; only OTHER passes' output
+ is peeled. A bare call (``hermes send``'s direct reload) is its own pass."""
+ raw = path.read_bytes()
try:
# utf-8-sig strips a leading BOM (PowerShell 5.1 / Notepad); plain utf-8 would keep U+FEFF on the
# first key name and silently drop it from os.environ under its canonical name.
- load_dotenv(dotenv_path=path, override=override, encoding="utf-8-sig")
+ text = raw.decode("utf-8-sig")
except UnicodeDecodeError:
- raw = path.read_bytes() # strip the BOM by hand: utf-8-sig can't once we decode latin-1
- if raw.startswith(codecs.BOM_UTF8):
+ if raw.startswith(codecs.BOM_UTF8): # strip the BOM by hand: utf-8-sig can't once we decode latin-1
raw = raw[len(codecs.BOM_UTF8) :]
- load_dotenv(stream=io.StringIO(raw.decode("latin-1")), override=override)
+ text = raw.decode("latin-1")
+ assignments = list(DotEnv(dotenv_path=None, stream=io.StringIO(text), interpolate=False).parse())
+
+ with _DOTENV_LOCK:
+ if load_pass is None:
+ load_pass = next(_DOTENV_PASSES)
+ lookup_env: dict[str, str | None] = dict(os.environ)
+ for name, (baseline, published, published_pass) in _DOTENV_PUBLISHED.items():
+ if published_pass != load_pass and lookup_env.get(name) == published:
+ if baseline is None:
+ del lookup_env[name] # absent, so ``${VAR:-default}`` takes the default again
+ else:
+ lookup_env[name] = baseline
+ resolved: dict[str, str | None] = {}
+ for name, value in assignments:
+ if value is not None: # mirrors dotenv.main.resolve_variables, minus the live os.environ
+ lookup = {**lookup_env, **resolved} if override else {**resolved, **lookup_env}
+ value = "".join(atom.resolve(lookup) for atom in parse_variables(value))
+ resolved[name] = value
+ for name, value in resolved.items():
+ if value is None or (not override and name in os.environ):
+ continue
+ current = os.environ.get(name)
+ record = _DOTENV_PUBLISHED.get(name)
+ # Ours and untouched since → keep the original baseline; anything else is a newer outside value.
+ baseline = record[0] if record is not None and current == record[1] else current
+ os.environ[name] = value
+ _DOTENV_PUBLISHED[name] = (baseline, value, load_pass)
_sanitize_loaded_credentials() # httpx encodes headers as ASCII
@@ -353,6 +401,7 @@ def load_hermes_dotenv(
loaded: list[Path] = []
user_env = home_path / ".env"
project_env_path = Path(project_env) if project_env else None
+ load_pass = next(_DOTENV_PASSES) # one pass: later layers below see the earlier layers' output
if user_env.exists(): # normalize formatting / strip NULs before parsing
_sanitize_env_file_if_needed(user_env)
@@ -360,7 +409,7 @@ def load_hermes_dotenv(
_sanitize_env_file_if_needed(project_env_path)
if user_env.exists():
- _load_dotenv_with_fallback(user_env, override=True)
+ _load_dotenv_with_fallback(user_env, override=True, load_pass=load_pass)
loaded.append(user_env)
_clear_known_keys_missing_from_dotenv(user_env) # mirrors reload_env(): inherited keys must not leak
@@ -369,10 +418,10 @@ def load_hermes_dotenv(
# the committed .env. override=False lets a systemd `EnvironmentFile=-…/.op.env` token win.
op_env = home_path / ".op.env"
if op_env.exists() and not os.environ.get("OP_SERVICE_ACCOUNT_TOKEN"):
- _load_dotenv_with_fallback(op_env, override=False)
+ _load_dotenv_with_fallback(op_env, override=False, load_pass=load_pass)
if project_env_path and project_env_path.exists():
- _load_dotenv_with_fallback(project_env_path, override=not loaded)
+ _load_dotenv_with_fallback(project_env_path, override=not loaded, load_pass=load_pass)
loaded.append(project_env_path)
# External sources are skipped for the updater (dotenv + managed env still load): ``update`` must not
@@ -390,7 +439,7 @@ def load_hermes_dotenv(
# managed env still load in both cases; only external source resolution is unnecessary for the updater.
if load_external_secrets and not _early_recovery._should_skip_external_secret_sources():
_apply_external_secret_sources(home_path)
- _apply_managed_env()
+ _apply_managed_env(load_pass=load_pass)
# config.yaml owns terminal.*, but the override=True loads above let a stale TERMINAL_ENV=docker in
# ~/.hermes/.env win on every reload and flip the backend mid-session in long-lived processes.
@@ -420,7 +469,7 @@ def _reapply_terminal_config_bridge(home_path: Path) -> None:
pass
-def _apply_managed_env() -> None:
+def _apply_managed_env(*, load_pass: int | None = None) -> None:
"""Apply the managed-scope .env last, with override, so it beats user/shell. Does NOT stop the agent
from later mutating os.environ (v1 relies on filesystem permissions). Fail-open: never blocks startup."""
try:
@@ -435,7 +484,7 @@ def _apply_managed_env() -> None:
if not managed_env.exists():
return
_sanitize_env_file_if_needed(managed_env)
- _load_dotenv_with_fallback(managed_env, override=True)
+ _load_dotenv_with_fallback(managed_env, override=True, load_pass=load_pass)
def _apply_external_secret_sources(home_path: Path) -> None:
diff --git a/tests/hermes_cli/test_env_loader_self_reference.py b/tests/hermes_cli/test_env_loader_self_reference.py
new file mode 100644
index 00000000000..388ee208a28
--- /dev/null
+++ b/tests/hermes_cli/test_env_loader_self_reference.py
@@ -0,0 +1,102 @@
+"""Self-referential .env lines (``PATH=/x:${PATH}``) must resolve once per process, not grow on every reload
+(#109902: gateway per-turn and cron per-fire reloads grew PATH until child spawns died with E2BIG)."""
+
+import os
+
+import pytest
+
+from hermes_cli.env_loader import load_hermes_dotenv
+
+BASE_PATH = "/usr/bin:/bin"
+
+
+def home(tmp_path, monkeypatch):
+ monkeypatch.setenv("PATH", BASE_PATH)
+ monkeypatch.setenv("HERMES_MULTIPLEX_PROFILES", "0")
+ h = tmp_path / "hermes"
+ h.mkdir()
+ return h
+
+
+def test_same_file_reloaded_three_times_leaves_path_unchanged(home):
+ (home / ".env").write_text("PATH=/x:${PATH}\nPREV=${PATH}\nCHAIN=/c:${PREV}\n")
+
+ seen = []
+ for _ in range(3):
+ load_hermes_dotenv(hermes_home=home, load_external_secrets=False)
+ seen.append((os.environ["PATH"], os.environ["CHAIN"]))
+
+ assert seen == [(f"/x:{BASE_PATH}", f"/c:/x:{BASE_PATH}")] * 3
+
+
+def test_alternating_project_env_and_none_stays_stable(home, tmp_path):
+ """The gateway reloads with a project .env, cron without: both call forms share one process."""
+ (home / ".env").write_text("PATH=/x:${PATH}\n")
+ project_env = tmp_path / "project.env"
+ project_env.write_text("UNRELATED=1\n")
+
+ counts = []
+ for i in range(6):
+ load_hermes_dotenv(
+ hermes_home=home, project_env=project_env if i % 2 else None, load_external_secrets=False
+ )
+ counts.append(os.environ["PATH"].count("/x:"))
+
+ assert counts == [1] * 6
+
+
+def test_alternating_homes_each_resolve_against_boot_path(tmp_path, monkeypatch):
+ monkeypatch.setenv("PATH", BASE_PATH)
+ monkeypatch.setenv("HERMES_MULTIPLEX_PROFILES", "0")
+ home_a = tmp_path / "a"
+ home_b = tmp_path / "b"
+ for h, tag in ((home_a, "a"), (home_b, "b")):
+ h.mkdir()
+ (h / ".env").write_text(f"PATH=/{tag}:${{PATH}}\n")
+
+ seen = []
+ for h in (home_a, home_b, home_a, home_b, home_a):
+ load_hermes_dotenv(hermes_home=h, load_external_secrets=False)
+ seen.append(os.environ["PATH"])
+
+ assert seen == [f"/a:{BASE_PATH}", f"/b:{BASE_PATH}", f"/a:{BASE_PATH}", f"/b:{BASE_PATH}", f"/a:{BASE_PATH}"]
+
+
+def test_variable_unset_at_boot_and_default_syntax_resolve_once(home, monkeypatch):
+ monkeypatch.delenv("FOO", raising=False)
+ monkeypatch.delenv("DEF", raising=False)
+ (home / ".env").write_text("FOO=/f:${FOO}\nDEF=/d:${DEF:-seed}\n")
+
+ for _ in range(3):
+ load_hermes_dotenv(hermes_home=home, load_external_secrets=False)
+
+ assert os.environ["FOO"] == "/f:"
+ assert os.environ["DEF"] == "/d:seed"
+
+
+def test_value_changed_outside_the_loader_is_not_frozen(home):
+ """Only this process's own dotenv output is peeled; a newer outside value (shell, config bridge,
+ secret source) becomes the new baseline instead of being reverted."""
+ (home / ".env").write_text("PATH=/x:${PATH}\n")
+
+ load_hermes_dotenv(hermes_home=home, load_external_secrets=False)
+ os.environ["PATH"] = "/opt/new:/bin"
+ load_hermes_dotenv(hermes_home=home, load_external_secrets=False)
+ load_hermes_dotenv(hermes_home=home, load_external_secrets=False)
+
+ assert os.environ["PATH"] == "/x:/opt/new:/bin"
+
+
+def test_later_layer_in_the_same_load_still_sees_the_earlier_layer(home, tmp_path):
+ """Within one load_hermes_dotenv the project layer builds on the user layer, as before; the
+ combination is what must not grow across reloads."""
+ (home / ".env").write_text("XPATH=/x:${PATH}\n")
+ project_env = tmp_path / "project.env"
+ project_env.write_text("YPATH=/p:${XPATH}\n")
+
+ for _ in range(3):
+ load_hermes_dotenv(hermes_home=home, project_env=project_env, load_external_secrets=False)
+
+ assert os.environ["XPATH"] == f"/x:{BASE_PATH}"
+ assert os.environ["YPATH"] == f"/p:/x:{BASE_PATH}"