Skip to content

fix(profiles): honour active_profile when HERMES_HOME points to hermes root#22654

Closed
wesleysimplicio wants to merge 1 commit into
NousResearch:mainfrom
wesleysimplicio:fix/ag04-profile-hermes-home-guard
Closed

fix(profiles): honour active_profile when HERMES_HOME points to hermes root#22654
wesleysimplicio wants to merge 1 commit into
NousResearch:mainfrom
wesleysimplicio:fix/ag04-profile-hermes-home-guard

Conversation

@wesleysimplicio

Copy link
Copy Markdown
Contributor

Problem

After hermes profile use NAME, the gateway (started via systemd with HERMES_HOME=/root/.hermes hardcoded) ignores the active profile and always runs as the Default profile. WebUI, Telegram, and all non-CLI platforms are affected.

Root cause

_apply_profile_override() in hermes_cli/main.py had an early-return guard:

# trust the inherited value so child processes don't re-read active_profile
if profile_name is None and os.environ.get("HERMES_HOME"):
    return

The intent was correct for child processes that inherit HERMES_HOME already pointing to a profile directory (e.g. ~/.hermes/profiles/coder). But systemd also sets HERMES_HOME — to the hermes root (/root/.hermes), not a profile directory — so the guard fired and the active_profile check was silently skipped.

Fix

Only skip the active_profile check when HERMES_HOME is already a profile directory, identified by its immediate parent being named "profiles":

hermes_home_env = os.environ.get("HERMES_HOME", "")
if profile_name is None and hermes_home_env:
    if Path(hermes_home_env).parent.name == "profiles":
        return  # already a profile dir, trust it
    # else: root dir set by systemd — still check active_profile

This preserves the child-process inheritance contract while fixing the systemd case.

Tests

New file tests/hermes_cli/test_apply_profile_override.py:

Test Scenario Result
test_hermes_home_at_root_with_active_profile_is_redirected Bug scenario: HERMES_HOME=/root/.hermes + active_profile=coder HERMES_HOME redirected to .../profiles/coder
test_hermes_home_already_profile_dir_is_trusted Child-process: HERMES_HOME=.../profiles/coder Unchanged (trusted as-is)
test_hermes_home_unset_reads_active_profile Classic path Redirected to profile dir
test_hermes_home_unset_default_profile_no_redirect active_profile=default No redirect

Stash-verified: key regression test FAILS without fix, PASSES with fix. 4/4 tests green.

Closes #22502.

…s root

Problem:
After `hermes profile use NAME`, the gateway (started via systemd with
HERMES_HOME=/root/.hermes hardcoded) ignores the active profile and
always runs as the Default profile.  WebUI, Telegram, and all non-CLI
platforms are affected.

Root cause:
_apply_profile_override() contained an early-return guard:

    if profile_name is None and os.environ.get("HERMES_HOME"):
        return   # trust the inherited value

The intent was to let child processes inherit their parent's profile via
HERMES_HOME without redundantly re-reading active_profile.  But
systemd also sets HERMES_HOME — to the hermes root (/root/.hermes),
not a profile directory — so the guard fired and silently skipped the
active_profile check.  The user's `hermes profile use NAME` write to
~/.hermes/active_profile was never seen by the gateway process.

Fix:
Only skip the active_profile check when HERMES_HOME is already a
profile directory, identified by its immediate parent directory being
named "profiles" (e.g. ~/.hermes/profiles/coder or
/opt/data/profiles/coder).  When HERMES_HOME points to a root
directory (parent name != "profiles"), continue to read active_profile.

Tests:
- test_hermes_home_at_root_with_active_profile_is_redirected: the
  bug scenario — HERMES_HOME=/root/.hermes + active_profile=coder →
  HERMES_HOME must be redirected to .../profiles/coder.
  Stash-verified: FAILS without fix, PASSES with fix.
- test_hermes_home_already_profile_dir_is_trusted: child-process
  inheritance contract unchanged — .../profiles/coder is trusted as-is.
- test_hermes_home_unset_reads_active_profile: classic path unchanged.
- test_hermes_home_unset_default_profile_no_redirect: "default" still
  produces no redirect.
4/4 tests green.

Closes NousResearch#22502.
Copilot AI review requested due to automatic review settings May 9, 2026 15:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a regression where non-CLI entrypoints (notably systemd-started gateway/WebUI/Telegram) ignore the sticky active_profile when HERMES_HOME is set to the Hermes root rather than a specific profile directory.

Changes:

  • Refines _apply_profile_override() to only “trust” an inherited HERMES_HOME when it already points to .../profiles/<name> (parent directory name is profiles).
  • Adds regression tests covering the systemd/root case vs the child-process/profile-dir inheritance case.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
hermes_cli/main.py Adjusts the early-return guard so active_profile is still honored when HERMES_HOME is set to the Hermes root.
tests/hermes_cli/test_apply_profile_override.py Adds regression tests for the updated HERMES_HOME guard behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +82 to +86
assert "profiles" in result, (
f"Expected HERMES_HOME to point into profiles/ dir, got: {result!r}"
)
assert result.endswith("coder"), (
f"Expected HERMES_HOME to end with 'coder', got: {result!r}"
Comment on lines +125 to +126
assert result is not None
assert "coder" in result
@teknium1

teknium1 commented May 9, 2026

Copy link
Copy Markdown
Contributor

Merged via salvage PR #22677. Your commit was cherry-picked onto current main with your authorship preserved in git log (rebase-merge). Thanks for the contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Profile switching fully broken in Gateway/WebUI/Telegram - HERMES_HOME guard blocks active_profile

3 participants