Skip to content

fix(env): preserve desktop session token despite pinned .env value - #38586

Closed
liuhao1024 wants to merge 1 commit into
NousResearch:mainfrom
liuhao1024:fix/desktop-session-token-dotenv-override
Closed

fix(env): preserve desktop session token despite pinned .env value#38586
liuhao1024 wants to merge 1 commit into
NousResearch:mainfrom
liuhao1024:fix/desktop-session-token-dotenv-override

Conversation

@liuhao1024

Copy link
Copy Markdown
Contributor

What does this PR do?

Preserves the Desktop-spawned process-level HERMES_DASHBOARD_SESSION_TOKEN when ~/.hermes/.env also pins a value for the same variable. Without this fix, a pinned token in .env causes the local-mode Desktop app to enter an infinite SIGTERM boot loop because the backend enforces the pinned token while the renderer presents the auto-generated one.

Related Issue

Fixes #38575

Type of Change

  • 🐛 Bug fix (non-breaking change that fixes an issue)

Changes Made

  • hermes_cli/env_loader.py: In load_hermes_dotenv(), detect the Desktop app's presence via HERMES_DASHBOARD_TUI=1 (set by main.cjs line 3363) and save the process-level HERMES_DASHBOARD_SESSION_TOKEN before override=True runs, then restore it afterward if dotenv overwrote it with the pinned .env value.
  • tests/hermes_cli/test_env_loader.py: Added two tests — test_desktop_token_preserved_despite_pinned_dotenv (desktop token survives .env override) and test_pinned_token_applied_when_not_desktop_mode (pinned token still applies in remote-backend use case).

How to Test

  1. Set HERMES_DASHBOARD_SESSION_TOKEN=pinned-value in ~/.hermes/.env
  2. Launch Hermes Desktop in local mode (Settings → Gateway → Remote gateway OFF)
  3. Before this fix: observe infinite boot loop in ~/.hermes/logs/desktop.log (SIGTERM + restart cycle)
  4. After this fix: Desktop starts normally — the backend uses the Desktop's auto-generated token, ignoring the pinned value
  5. Verify remote mode still works: hermes dashboard (without Desktop) should still honor the pinned token from .env

Checklist

Code

  • I've read the Contributing Guide
  • My commit messages follow Conventional Commits (fix(scope):, feat(scope):, etc.)
  • I searched for existing PRs to make sure this isn't a duplicate
  • My PR contains only changes related to this fix/feature (no unrelated commits)
  • I've run pytest tests/hermes_cli/test_env_loader.py -v and all tests pass (8/8)
  • I've added tests for my changes (required for bug fixes, strongly encouraged for features)
  • I've tested on my platform: macOS

Documentation & Housekeeping

  • I've updated relevant documentation (README, docs/, docstrings) — or N/A
  • I've updated cli-config.yaml.example if I added/changed config keys — or N/A
  • I've updated CONTRIBUTING.md or AGENTS.md if I changed architecture or workflows — or N/A
  • I've considered cross-platform impact (Windows, macOS) per the compatibility guide — or N/A (Desktop app is macOS-only; env_loader.py is cross-platform)
  • I've updated tool descriptions/schemas if I changed tool behavior — or N/A

Code Intelligence

  • Analyzed: hermes_cli/env_loader.py:load_hermes_dotenv() — called from hermes_cli/main.py, gateway/run.py, cron/scheduler.py, hermes_cli/doctor.py, hermes_cli/dump.py (6 call sites)
  • Blast radius: LOW — adds a guard that only activates when HERMES_DASHBOARD_TUI=1 (Desktop mode); all non-Desktop code paths are unaffected
  • Related patterns: main.cjs line 3362 sets HERMES_DASHBOARD_SESSION_TOKEN in the spawn env; line 3363 sets HERMES_DASHBOARD_TUI=1; web_server.py line 135 reads the token at import time

When the Desktop app spawns the backend in local mode, it injects an
auto-generated HERMES_DASHBOARD_SESSION_TOKEN via the process environment
(main.cjs line 3362).  load_hermes_dotenv() then runs with override=True,
which clobbers the desktop token with the pinned value from ~/.hermes/.env.
The token mismatch causes an infinite SIGTERM boot loop: the local backend
enforces the pinned token while the renderer presents the auto-generated one.

Fix: in load_hermes_dotenv(), detect the desktop's presence via
HERMES_DASHBOARD_TUI=1 (set by main.cjs) and preserve the process-level
HERMES_DASHBOARD_SESSION_TOKEN before override=True runs, then restore it
afterward if dotenv overwrote it.

Fixes NousResearch#38575
@alt-glitch alt-glitch added type/bug Something isn't working area/config Config system, migrations, profiles comp/cli CLI entry point, hermes_cli/, setup wizard P3 Low — cosmetic, nice to have labels Jun 4, 2026

@tonydwb tonydwb left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review Summary

Verdict: Approved

Changes

In load_hermes_dotenv(), before the .env override pass clobbers process-level values, the desktop-generated HERMES_DASHBOARD_SESSION_TOKEN is preserved (when HERMES_DASHBOARD_TUI=1). After loading, the desktop token is restored if it was overwritten.

Reasoning

Fixes issue #38575 boot loop caused by a pinned .env token overriding the Desktop app's auto-generated one. The fix correctly distinguishes the two contexts:

  • Desktop (HERMES_DASHBOARD_TUI=1): desktop token wins (session is local, pinned token is for remote mode only)
  • Remote mode: pinned .env token applies normally

Testing

Two tests cover both code paths cleanly.


Reviewed by Hermes Agent

@1RB

1RB commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

Superseded by #39652. This PR gates preservation on HERMES_DASHBOARD_TUI == "1", which is never set in production (only in test monkeypatches). The migration approach in #39652 removes the stale HERMES_DASHBOARD_SESSION_TOKEN from .env during config version 27 upgrade, fixing the root cause instead of adding runtime workarounds.

@liuhao1024

Copy link
Copy Markdown
Contributor Author

Superseded by #39652 by @1RB, which takes a more fundamental approach:

#39652's migration approach is cleaner — it removes the stale config once rather than working around it on every boot. Closing in favor of #39652.

@liuhao1024 liuhao1024 closed this Jun 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/config Config system, migrations, profiles comp/cli CLI entry point, hermes_cli/, setup wizard P3 Low — cosmetic, nice to have type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Pinned HERMES_DASHBOARD_SESSION_TOKEN in .env breaks Desktop LOCAL mode into SIGTERM boot loop

4 participants