Skip to content

The agent that grows with you

fix(cli): expand HERMES_HOME once at process entry; sweep raw readers

teknium1fc94ff56e1e5
diff --git a/hermes_cli/_startup_fast.py b/hermes_cli/_startup_fast.py
index 883386ed1b6..e72c5672292 100644
--- a/hermes_cli/_startup_fast.py
+++ b/hermes_cli/_startup_fast.py
@@ -12,7 +12,7 @@ import os
import sys
__all__ = [
- "project_root_str", "ensure_project_root_on_path", "is_termux_env",
+ "project_root_str", "normalize_hermes_home_env", "ensure_project_root_on_path", "is_termux_env",
"is_termux_fast_version_argv", "is_global_fast_version_argv",
"is_container_startup_environment", "active_profile_may_override_home",
"container_mode_may_be_active", "read_openai_version", "read_install_method",
@@ -34,6 +34,25 @@ def project_root_str() -> str:
return os.path.realpath(os.path.join(os.path.dirname(__file__), os.pardir))
+def normalize_hermes_home_env() -> None:
+ """Expand ``~``/``$VAR`` in ``HERMES_HOME`` once, at process entry, and write it back.
+
+ fish does not expand ``~`` inside ``VAR=~/...`` and every shell passes a quoted value
+ through verbatim, so a literal tilde reaches the process. ``Path("~/.hermes")`` is
+ *relative*: the many raw ``os.environ["HERMES_HOME"]`` readers (this fast path, the
+ active_profile probe, profile re-home, the dotenv loader) would each resolve it against
+ cwd and scaffold a full home under ``<cwd>/~/.hermes``. One expansion here gives every
+ reader the same absolute spelling; ``hermes_constants`` expands as well for non-CLI
+ entry points. A relative value that is not tilde/variable-shaped is left alone.
+ """
+ raw = os.environ.get("HERMES_HOME", "")
+ if not raw.strip():
+ return
+ expanded = os.path.expanduser(os.path.expandvars(raw.strip()))
+ if expanded != raw:
+ os.environ["HERMES_HOME"] = expanded
+
+
def ensure_project_root_on_path() -> None:
"""Put the project root at sys.path[0], deduping realpath-equivalents."""
project_root = project_root_str()
diff --git a/hermes_cli/main.py b/hermes_cli/main.py
index bb14dd90e8b..54eff5e9095 100644
--- a/hermes_cli/main.py
+++ b/hermes_cli/main.py
@@ -35,6 +35,10 @@ if _bootstrap_root not in sys.path:
sys.path.insert(0, _bootstrap_root)
from hermes_cli import _startup_fast # noqa: E402
+# A literal ``~``/``$VAR`` in HERMES_HOME (fish, or any quoted value) must become absolute
+# before the first reader — otherwise it resolves against cwd and scaffolds <cwd>/~/.hermes.
+_startup_fast.normalize_hermes_home_env()
+
# Early venv self-heal — MUST run before any third-party import below. A prior
# ``hermes update`` may have left a recovery marker with a core package wiped;
# the hermes_cli.config/env_loader imports further down would then crash before
diff --git a/hermes_constants.py b/hermes_constants.py
index 09d5f131bf9..df35569ac1d 100644
--- a/hermes_constants.py
+++ b/hermes_constants.py
@@ -875,7 +875,7 @@ def _profile_home_path(env: dict[str, str] | None = None) -> str | None:
hermes_home = get_hermes_home_override() or (env or {}).get("HERMES_HOME") or os.getenv("HERMES_HOME")
if not hermes_home:
return None
- profile_home = os.path.join(hermes_home, "home")
+ profile_home = str(_expand_hermes_home(hermes_home) / "home")
return profile_home if os.path.isdir(profile_home) else None
diff --git a/tests/hermes_cli/test_startup_fast_guards.py b/tests/hermes_cli/test_startup_fast_guards.py
index 4628b8fabd8..53948cda3a7 100644
--- a/tests/hermes_cli/test_startup_fast_guards.py
+++ b/tests/hermes_cli/test_startup_fast_guards.py
@@ -104,3 +104,44 @@ def test_fast_version_reports_install_method_stamp(tmp_path):
result = _run_version({"HERMES_HOME": str(home), "TERMUX_VERSION": ""})
assert result.returncode == 0, result.stderr
assert "Install method: git" in result.stdout
+
+
+def test_literal_tilde_hermes_home_expands_before_any_reader(tmp_path):
+ """A literal ``~`` in HERMES_HOME (fish, or any quoted value) is expanded at process entry.
+
+ Before the fix ``Path("~/.x")`` was relative, so the real CLI resolved it against cwd and
+ scaffolded a full home under ``<cwd>/~/.x``. The negative assertion on cwd is the
+ reporter's own acceptance criterion.
+ """
+ fake_home = tmp_path / "home"
+ cwd = tmp_path / "project"
+ fake_home.mkdir()
+ cwd.mkdir()
+ env = {**os.environ, "HOME": str(fake_home), "USERPROFILE": str(fake_home), "HERMES_HOME": "~/.x"}
+ env.pop("HERMES_DEV", None)
+ env["PYTHONPATH"] = str(REPO_ROOT)
+ result = subprocess.run(
+ [sys.executable, "-m", "hermes_cli.main", "config", "path"],
+ capture_output=True, text=True, timeout=120, cwd=cwd, env=env,
+ )
+ assert result.returncode == 0, result.stderr
+ assert result.stdout.strip() == str(fake_home / ".x" / "config.yaml")
+ assert not (cwd / "~").exists(), sorted(p.name for p in cwd.iterdir())
+
+
+def test_normalize_hermes_home_env_rewrites_tilde_and_leaves_absolute_alone(tmp_path, monkeypatch):
+ from hermes_cli import _startup_fast
+
+ monkeypatch.setenv("HOME", str(tmp_path))
+ monkeypatch.setenv("USERPROFILE", str(tmp_path))
+ monkeypatch.setenv("HERMES_HOME", "~/.x")
+ _startup_fast.normalize_hermes_home_env()
+ assert os.environ["HERMES_HOME"] == str(tmp_path / ".x")
+
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path / "abs"))
+ _startup_fast.normalize_hermes_home_env()
+ assert os.environ["HERMES_HOME"] == str(tmp_path / "abs")
+
+ monkeypatch.delenv("HERMES_HOME")
+ _startup_fast.normalize_hermes_home_env()
+ assert "HERMES_HOME" not in os.environ
diff --git a/website/docs/reference/environment-variables.md b/website/docs/reference/environment-variables.md
index e3c63083758..33a1b95648f 100644
--- a/website/docs/reference/environment-variables.md
+++ b/website/docs/reference/environment-variables.md
@@ -123,7 +123,7 @@ Hermes reads environment variables from the process environment and, for user-ma
| `VOICE_TOOLS_OPENAI_KEY` | Preferred OpenAI key for OpenAI speech-to-text and text-to-speech providers |
| `HERMES_LOCAL_STT_COMMAND` | Optional local speech-to-text command template. Supports `{input_path}`, `{output_dir}`, `{language}`, and `{model}` placeholders |
| `HERMES_LOCAL_STT_LANGUAGE` | Default language hint for STT. Used by the `local` (faster-whisper) provider, `HERMES_LOCAL_STT_COMMAND`, the local `whisper` CLI fallback (default: `en`), Groq, and xAI when no per-provider `language` is set in `config.yaml` |
-| `HERMES_HOME` | Override Hermes config directory (default: `~/.hermes`). Also scopes the gateway PID file and systemd service name, so multiple installations can run concurrently |
+| `HERMES_HOME` | Override Hermes config directory (default: `~/.hermes`). A literal `~` or `$VAR` in the value is expanded (fish does not expand `~` inside `VAR=~/…`), so it never resolves relative to the current directory. Also scopes the gateway PID file and systemd service name, so multiple installations can run concurrently |
| `HERMES_GIT_BASH_PATH` | **Windows only.** Override `bash.exe` discovery for the terminal tool. Points at any bash — full Git-for-Windows install, WSL bash via symlink, MSYS2, Cygwin. The installer sets this automatically to the PortableGit it provisioned. See the [Windows (Native) Guide](../user-guide/windows-native.md#how-hermes-runs-shell-commands-on-windows) |
| `HERMES_DISABLE_WINDOWS_UTF8` | **Windows only.** Set to `1` to disable the UTF-8 stdio shim (`configure_windows_stdio()`) and fall back to the console's locale code page. Useful for bisecting encoding bugs; rarely the right setting in normal operation |
| `HERMES_KANBAN_HOME` | Override the shared Hermes root that anchors the kanban board (db + workspaces + worker logs). Falls back to `get_default_hermes_root()` (the parent of any active profile). Useful for tests and unusual deployments |