Skip to content

The agent that grows with you

fix(left-core): singularity_in_use expands dotenv references as load_hermes_dotenv does

Teknium4de668036865
diff --git a/hermes_cli/env_loader.py b/hermes_cli/env_loader.py
index 3a0a6ae29e8..d2055780a40 100644
--- a/hermes_cli/env_loader.py
+++ b/hermes_cli/env_loader.py
@@ -325,16 +325,8 @@ def _sanitize_loaded_credentials() -> None:
)
-def _load_dotenv_with_fallback(
- path: Path, *, override: bool, load_pass: int | None = None, managed: bool = False,
-) -> 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."""
+def _parse_dotenv_file(path: Path) -> list[tuple[str, str | None]]:
+ """``(name, raw value)`` assignments of one dotenv file, uninterpolated, in file order."""
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
@@ -346,26 +338,78 @@ def _load_dotenv_with_fallback(
# Imported here, not at module level: gateway tests stub ``sys.modules["dotenv"]`` with a bare module
# exposing only ``load_dotenv``, and ``gateway.run`` imports this module at import time.
from dotenv.main import DotEnv
+
+ return list(DotEnv(dotenv_path=None, stream=io.StringIO(text), interpolate=False).parse())
+
+
+def _dotenv_lookup_env(load_pass: int | None) -> dict[str, str]:
+ """``os.environ`` with every value an earlier pass (any pass, for ``None``) published peeled back to
+ its baseline — what ``${VAR}`` resolves against (see ``_DOTENV_PUBLISHED``). Caller holds the lock."""
+ lookup_env = 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
+ return lookup_env
+
+
+def _resolve_dotenv_assignments(
+ assignments: list[tuple[str, str | None]], lookup_env: dict[str, str], *, override: bool,
+) -> dict[str, str | None]:
+ """Expand ``${VAR}`` / ``${VAR:-default}`` like ``dotenv.main.resolve_variables``, minus the live
+ ``os.environ``: a line sees *lookup_env* and the file's earlier lines (earlier lines win under
+ *override*, the environment otherwise)."""
from dotenv.variables import parse_variables
- assignments = list(DotEnv(dotenv_path=None, stream=io.StringIO(text), interpolate=False).parse())
+ resolved: dict[str, str | None] = {}
+ for name, value in assignments:
+ if value is not None:
+ lookup = {**lookup_env, **resolved} if override else {**resolved, **lookup_env}
+ value = "".join(atom.resolve(lookup) for atom in parse_variables(value))
+ resolved[name] = value
+ return resolved
+
+def resolve_home_dotenv(home: Path) -> dict[str, str]:
+ """What ``load_hermes_dotenv`` would publish from *home*'s dotenv layers, read-only: the home's
+ ``.env`` then the administrator-managed ``.env``, both with override and ``${VAR}`` expanded by the
+ same rules against the process's own environment (this process's dotenv output peeled back), each
+ layer seeing the one before. External secret sources and the project ``.env`` are not included."""
+ from hermes_cli import managed_scope
+
+ managed_dir = managed_scope.get_managed_dir()
+ layers = [home / ".env", *([managed_dir / ".env"] if managed_dir is not None else [])]
+ with _DOTENV_LOCK:
+ lookup_env = _dotenv_lookup_env(None)
+ out: dict[str, str] = {}
+ for path in layers:
+ try:
+ assignments = _parse_dotenv_file(path)
+ except OSError:
+ continue
+ for name, value in _resolve_dotenv_assignments(assignments, lookup_env, override=True).items():
+ if value is not None:
+ lookup_env[name] = out[name] = value
+ return out
+
+
+def _load_dotenv_with_fallback(
+ path: Path, *, override: bool, load_pass: int | None = None, managed: bool = False,
+) -> 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."""
+ assignments = _parse_dotenv_file(path)
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
+ resolved = _resolve_dotenv_assignments(assignments, _dotenv_lookup_env(load_pass), override=override)
for name, value in resolved.items():
if value is None or (not override and name in os.environ):
continue
diff --git a/hermes_cli/left_core_migration.py b/hermes_cli/left_core_migration.py
index 9c4eeaaf544..e7344482547 100644
--- a/hermes_cli/left_core_migration.py
+++ b/hermes_cli/left_core_migration.py
@@ -151,8 +151,9 @@ def langfuse_in_use(home: Path, *, process_env: bool = False) -> bool:
def singularity_in_use(home: Path, *, process_env: bool = False) -> bool:
"""What made core run its Singularity backend for *home*: ``terminal.backend`` (or the legacy
``terminal.env_type``) set to ``singularity`` in its config (managed overlay applied, as the bridge
- reads it), else ``TERMINAL_ENV=singularity`` from its dotenv layers in ``load_hermes_dotenv``'s order
- (the home's ``.env``, then the administrator-managed ``.env`` with override). config.yaml wins over
+ reads it), else ``TERMINAL_ENV=singularity`` from its dotenv layers as ``load_hermes_dotenv`` resolves
+ them (the home's ``.env``, then the administrator-managed ``.env`` with override, ``${VAR}``
+ references expanded: :func:`~hermes_cli.env_loader.resolve_home_dotenv`). config.yaml wins over
both, as in the bridge. *process_env* (startup) also counts a ``TERMINAL_ENV`` the process received
from its environment (systemd unit, Docker, shell export), for the launch home only: ``TERMINAL_*``
is process-global (``get_secret`` never scopes it), so for any other served home it is the launch
@@ -166,10 +167,8 @@ def singularity_in_use(home: Path, *, process_env: bool = False) -> bool:
terminal = terminal if isinstance(terminal, dict) else {}
configured = str(terminal.get("backend") or terminal.get("env_type") or "").strip()
if not configured:
- from agent.secret_scope import load_env_file
- from hermes_cli.managed_scope import load_managed_env
- dotenv = {**load_env_file(home / ".env"), **load_managed_env()}
- configured = str(dotenv.get("TERMINAL_ENV") or "").strip()
+ from hermes_cli.env_loader import resolve_home_dotenv
+ configured = resolve_home_dotenv(home).get("TERMINAL_ENV", "").strip()
if not configured and process_env:
from hermes_constants import get_routing_process_hermes_home, hermes_home_key
if hermes_home_key(home) == hermes_home_key(get_routing_process_hermes_home()):
diff --git a/tests/hermes_cli/test_left_core_singularity.py b/tests/hermes_cli/test_left_core_singularity.py
index 44e39fea118..2a15b3055a0 100644
--- a/tests/hermes_cli/test_left_core_singularity.py
+++ b/tests/hermes_cli/test_left_core_singularity.py
@@ -59,6 +59,19 @@ def test_the_managed_env_beats_the_homes_env_as_in_load_hermes_dotenv(tmp_path,
assert lcm.singularity_in_use(_home(tmp_path, env=home_env)) is in_use
+ "HPC_BACKEND=singularity\nTERMINAL_ENV=${HPC_BACKEND}\n",
+ "TERMINAL_ENV=${SITE_BACKEND}\n", # the process environment supplies the value
+ "TERMINAL_ENV=${UNSET_BACKEND:-singularity}\n",
+])
+def test_dotenv_references_resolve_as_in_load_hermes_dotenv(tmp_path, monkeypatch, env):
+ # load_hermes_dotenv expands ${VAR} / ${VAR:-default} against the file's earlier lines and the
+ # process environment before TERMINAL_ENV picks the backend.
+ monkeypatch.setenv("SITE_BACKEND", "singularity")
+ monkeypatch.delenv("UNSET_BACKEND", raising=False)
+ assert lcm.singularity_in_use(_home(tmp_path, env=env)) is True
+
+
def test_process_env_backend_counts_only_for_the_launch_home(tmp_path, monkeypatch):
launch = _home(tmp_path)
secondary = tmp_path / "profiles" / "work"