Skip to content

The agent that grows with you

fix(profiles): one wrapper-candidate list for delete; doctor reads alias_path

kshitijk4poorc1196ebd0c68
diff --git a/hermes_cli/doctor_state.py b/hermes_cli/doctor_state.py
index caaa4303a06..7e34f19c3b4 100644
--- a/hermes_cli/doctor_state.py
+++ b/hermes_cli/doctor_state.py
@@ -651,7 +651,7 @@ def _check_profiles(should_fix: bool, f: Finding) -> None:
parts = [text for cond, text in (
(p.gateway_running, "gateway running"), (p.model, (p.model or "")[:30]),
(not (p.path / "config.yaml").exists(), "⚠ missing config"), (not (p.path / ".env").exists(), "no .env"),
- (not _wrapper_path(p.name).exists(), "no alias")) if cond]
+ (p.alias_path is None, "no alias")) if cond]
check_ok(f" {p.name}: {', '.join(parts) if parts else 'configured'}")
# Orphan wrappers
if wrapper_dir.is_dir():
diff --git a/hermes_cli/profiles.py b/hermes_cli/profiles.py
index 6b52218cc5e..02b3f434327 100644
--- a/hermes_cli/profiles.py
+++ b/hermes_cli/profiles.py
@@ -306,6 +306,11 @@ def _wrapper_path(alias: str) -> Path:
return _get_wrapper_dir() / (f"{alias}.bat" if sys.platform == "win32" else alias)
+def _wrapper_candidates(alias: str) -> list[Path]:
+ """Paths a wrapper for *alias* may occupy: :func:`_wrapper_path`, then a pre-``.bat`` Windows one."""
+ return [_wrapper_path(alias), _get_wrapper_dir() / alias] if sys.platform == "win32" else [_wrapper_path(alias)]
+
+
def _is_our_wrapper(path: Path) -> bool:
"""True when *path* reads as a Hermes-generated wrapper (contains ``hermes -p``)."""
try:
@@ -566,11 +571,7 @@ def remove_wrapper_script(name: str) -> bool:
except ValueError:
return False
- # Both the extensionless path (POSIX) and .bat (Windows)
- candidates = [_get_wrapper_dir() / canon]
- if sys.platform == "win32":
- candidates.insert(0, _get_wrapper_dir() / f"{canon}.bat")
- for wrapper_path in candidates:
+ for wrapper_path in _wrapper_candidates(canon):
if wrapper_path.exists() and _is_our_wrapper(wrapper_path):
with contextlib.suppress(Exception):
wrapper_path.unlink()
@@ -1831,17 +1832,8 @@ def delete_profile(name: str, yes: bool = False) -> Path:
raise ValueError("Cannot delete the default profile (~/.hermes).\nTo remove everything, use: hermes uninstall")
canon, profile_dir = _existing_profile_dir(canon)
gw_running = _check_gateway_running(profile_dir)
- wrapper_dir = _get_wrapper_dir()
- wrapper_candidates = (
- wrapper_dir / canon,
- wrapper_dir / f"{canon}.bat",
- )
- wrapper_path = next(
- (candidate for candidate in wrapper_candidates if candidate.exists()),
- wrapper_candidates[0],
- )
- has_wrapper = wrapper_path.exists()
- _print_delete_summary(canon, profile_dir, gw_running, wrapper_path if has_wrapper else None)
+ wrapper_path = next((p for p in _wrapper_candidates(canon) if p.exists()), None)
+ _print_delete_summary(canon, profile_dir, gw_running, wrapper_path)
if not yes:
print()
try:
@@ -1906,7 +1898,7 @@ def delete_profile(name: str, yes: bool = False) -> Path:
print(f"✓ Released {_released_logs} profile log handler(s) held by this process")
# 3. Remove wrapper script
- if has_wrapper and remove_wrapper_script(canon):
+ if wrapper_path and remove_wrapper_script(canon):
print(f"✓ Removed {wrapper_path}")
# 4. Remove profile directory
diff --git a/tests/hermes_cli/test_doctor.py b/tests/hermes_cli/test_doctor.py
index 0f5afab8895..a2453355abf 100644
--- a/tests/hermes_cli/test_doctor.py
+++ b/tests/hermes_cli/test_doctor.py
@@ -1634,53 +1634,29 @@ def test_cron_store_check_reports_writability_and_low_space(
assert len(finding.issues) == len(issues) and all(any(s in i for i in finding.issues) for s in issues)
-class TestCheckProfilesAlias:
- """#131753: doctor must look for the wrapper name the writer actually creates
- (``<alias>.bat`` on Windows), not the bare POSIX name on every platform."""
-
- @staticmethod
- def _run(monkeypatch, capsys, tmp_path, platform, wrappers):
- import hermes_cli.profiles as profiles_mod
- import hermes_cli.gateway_migrate as gateway_migrate
-
- wrapper_dir = tmp_path / "bin"
- wrapper_dir.mkdir()
- for name in wrappers:
- target = name[: -len(".bat")] if name.endswith(".bat") else name
- (wrapper_dir / name).write_text(
- f"@echo off\r\nhermes -p {target} %*\r\n", encoding="utf-8")
-
- home = tmp_path / "home"
- home.mkdir()
- (home / "config.yaml").write_text("model: m\n", encoding="utf-8")
- (home / ".env").write_text("K=v\n", encoding="utf-8")
-
- profiles = [
- SimpleNamespace(name="dev", path=home, is_default=False,
- gateway_running=True, model="deepseek-v4.1-flash"),
- # ops never gets a wrapper: a genuinely missing alias must stay reportable
- SimpleNamespace(name="ops", path=home, is_default=False,
- gateway_running=True, model="deepseek-v4.1-flash"),
- ]
- monkeypatch.setattr(profiles_mod, "list_profiles", lambda **kw: profiles)
- monkeypatch.setattr(profiles_mod, "_get_wrapper_dir", lambda: wrapper_dir)
- monkeypatch.setattr(profiles_mod, "profile_exists", lambda name: True)
- monkeypatch.setattr(gateway_migrate, "duplicate_credential_findings", lambda: [])
- monkeypatch.setattr(sys, "platform", platform)
-
[email protected]("platform", ["win32", "darwin"])
+def test_profile_alias_status_follows_the_wrapper_the_profile_really_has(monkeypatch, tmp_path, platform):
+ """#131753: doctor said "no alias" for every Windows profile (``.bat`` wrapper) and for any
+ profile reachable only through a custom alias name."""
+ import hermes_cli.gateway_migrate as gm
+ from hermes_cli import profiles
+
+ monkeypatch.setattr(Path, "home", lambda: tmp_path)
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path / ".hermes"))
+ monkeypatch.setattr(hermes_constants, "_default_hermes_root_memo", None)
+ monkeypatch.setattr(gm, "duplicate_credential_findings", list)
+ monkeypatch.setattr(profiles, "sys", SimpleNamespace(**{**vars(sys), "platform": platform}))
+ for name in ("dev", "ops", "work"):
+ (tmp_path / ".hermes" / "profiles" / name).mkdir(parents=True)
+ (tmp_path / ".hermes" / "profiles" / name / "config.yaml").write_text("model: m\n", encoding="utf-8")
+ profiles.create_wrapper_script("dev")
+ profiles.create_wrapper_script("w", target="work")
+
+ out = io.StringIO()
+ with contextlib.redirect_stdout(out):
doctor_state._check_profiles(False)
- return capsys.readouterr().out
-
- def test_windows_bat_wrapper_is_not_reported_missing(self, monkeypatch, capsys, tmp_path):
- out = self._run(monkeypatch, capsys, tmp_path, "win32", ["dev.bat"])
- dev_line = next(ln for ln in out.splitlines() if "dev:" in ln)
- assert "no alias" not in dev_line
- ops_line = next(ln for ln in out.splitlines() if "ops:" in ln)
- assert "no alias" in ops_line
-
- def test_posix_bare_wrapper_still_matches(self, monkeypatch, capsys, tmp_path):
- out = self._run(monkeypatch, capsys, tmp_path, "darwin", ["dev"])
- dev_line = next(ln for ln in out.splitlines() if "dev:" in ln)
- assert "no alias" not in dev_line
- ops_line = next(ln for ln in out.splitlines() if "ops:" in ln)
- assert "no alias" in ops_line
+ lines = out.getvalue().splitlines()
+ status = {name: next(ln for ln in lines if f" {name}:" in ln) for name in ("dev", "ops", "work")}
+
+ assert "no alias" not in status["dev"] and "no alias" not in status["work"]
+ assert "no alias" in status["ops"]
diff --git a/tests/hermes_cli/test_profile_delete_wrapper.py b/tests/hermes_cli/test_profile_delete_wrapper.py
new file mode 100644
index 00000000000..016894ca0c7
--- /dev/null
+++ b/tests/hermes_cli/test_profile_delete_wrapper.py
@@ -0,0 +1,40 @@
+"""`hermes profile delete` removes the profile's wrapper on Windows (#131753).
+
+Windows wrappers are ``<alias>.bat``; delete used to look only for the bare name, so the
+``.bat`` survived as an orphan alias. A wrapper written before Windows wrappers became
+``.bat`` (#34796) is bare-named and must still go.
+"""
+
+import sys
+import types
+from pathlib import Path
+
+import pytest
+
+from hermes_cli import profiles
+from hermes_cli.profiles import create_profile, create_wrapper_script, delete_profile
+
+
[email protected]("legacy", [False, True], ids=["bat", "pre-bat-bare-name"])
+def test_delete_removes_the_windows_wrapper(tmp_path, monkeypatch, legacy):
+ monkeypatch.setattr(Path, "home", lambda: tmp_path)
+ (tmp_path / ".hermes").mkdir()
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path / ".hermes"))
+ # profiles.py only: a global sys.platform patch makes modules first imported by
+ # delete_profile cache a Windows lock backend (msvcrt) for the rest of the run.
+ monkeypatch.setattr(profiles, "sys", types.SimpleNamespace(**{**vars(sys), "platform": "win32"}))
+ for stub in ("_cleanup_gateway_service", "_maybe_unregister_gateway_service", "_stop_profile_backends"):
+ monkeypatch.setattr(profiles, stub, lambda *a, **k: None)
+ profile_dir = create_profile("imported", no_alias=True)
+ if legacy:
+ wrapper = tmp_path / ".local" / "bin" / "imported"
+ wrapper.parent.mkdir(parents=True, exist_ok=True)
+ wrapper.write_text('#!/bin/sh\nexec hermes -p imported "$@"\n', encoding="utf-8")
+ else:
+ wrapper = create_wrapper_script("imported")
+ assert wrapper is not None and wrapper.name == "imported.bat"
+
+ delete_profile("imported", yes=True)
+
+ assert not profile_dir.exists()
+ assert not wrapper.exists()
diff --git a/tests/hermes_cli/test_profiles.py b/tests/hermes_cli/test_profiles.py
index b2d7bedd570..740e188d21c 100644
--- a/tests/hermes_cli/test_profiles.py
+++ b/tests/hermes_cli/test_profiles.py
@@ -544,31 +544,6 @@ class TestBackfillProfileEnvs:
class TestDeleteProfile:
"""Tests for delete_profile()."""
- def test_delete_removes_windows_bat_wrapper(self, profile_env, monkeypatch):
- """Deleting an imported/created profile removes its Windows alias."""
- monkeypatch.setattr("hermes_cli.profiles.sys.platform", "win32")
- profile_dir = create_profile("imported", no_alias=True)
- wrapper = create_wrapper_script("imported")
- assert wrapper is not None
- assert wrapper.name == "imported.bat"
- assert wrapper.exists()
-
- monkeypatch.setattr(
- "hermes_cli.profiles._cleanup_gateway_service", lambda *a, **k: None
- )
- monkeypatch.setattr(
- "hermes_cli.profiles._maybe_unregister_gateway_service",
- lambda *a, **k: None,
- )
- monkeypatch.setattr(
- "hermes_cli.profiles._stop_profile_backends", lambda *a, **k: None
- )
-
- delete_profile("imported", yes=True)
-
- assert not profile_dir.exists()
- assert not wrapper.exists()
-
def test_rmtree_failure_raises(self, profile_env):
profile_dir = create_profile("coder", no_alias=True)