fix(gateway): anchor service WorkingDirectory at HERMES_HOME, not the source checkout#34805
Merged
Conversation
… source checkout The systemd unit (and launchd plist) pinned WorkingDirectory to PROJECT_ROOT (the checkout the unit was generated from). When that checkout is transient — a git worktree, or a clone hermes update later relocates/removes — the path rots. systemd then fails the start at the CHDIR step (status=200/CHDIR) BEFORE Python loads, so the on-boot refresh_systemd_unit_if_needed() self-heal never runs and Restart=always crash-loops forever on a dead directory. Observed in the wild: a gateway that crash-looped 153 times overnight, bot offline until a manual 'hermes gateway restart' regenerated the unit. Anchor cwd at HERMES_HOME instead — it never moves, always exists, and the gateway never needed cwd to be the checkout (ExecStart uses an absolute python + -m hermes_cli.main). Existing broken units now differ from the generated unit and self-heal on the next start/restart/update.
test_system_unit_has_no_root_paths asserted the system unit's WorkingDirectory was the remapped *checkout* path (/home/alice/.hermes/hermes-agent). That is the brittle pin this PR fixes — the system unit now anchors cwd at the target user's HERMES_HOME (/home/alice/.hermes). The test's intent (no root-home leak, target-user paths present) is unchanged and still holds.
This was referenced Jun 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Gateway services no longer crash-loop forever when their source checkout moves or is deleted.
The systemd unit (and launchd plist) pinned
WorkingDirectorytoPROJECT_ROOT— the checkout the unit was generated from. When that checkout is transient (a git worktree, or a clone thathermes updatelater relocates/removes), the path rots. systemd then fails the start at the CHDIR step (status=200/CHDIR, "Changing to the requested working directory failed") before Python loads, so the on-bootrefresh_systemd_unit_if_needed()self-heal never runs andRestart=alwayscrash-loops forever on a dead directory.Observed in the wild: a gateway that crash-looped 153 times overnight, Discord/Telegram/WhatsApp all offline, recovering only when a human ran
hermes gateway restart(which regenerated the unit as a side effect).Root cause
WorkingDirectoryis vestigial for the gateway —ExecStartuses an absolute python interpreter +-m hermes_cli.main, so module resolution never depended on cwd. Pinning it to the volatile checkout was pure downside.Changes
hermes_cli/gateway.py: new_stable_service_working_dir()→ anchorsWorkingDirectoryatHERMES_HOME(stable, always exists), falling back toPROJECT_ROOTonly if HERMES_HOME can't be resolved.tests/hermes_cli/test_gateway_service.py:TestServiceWorkingDirIsStable— HERMES_HOME anchor, PROJECT_ROOT fallback, user-unit asserts no/.worktrees/path, launchd parity.Why this prevents recurrence
status=200) becomes impossible — cwd is always a real directory.refresh_systemd_unit_if_needed()rewrites it on the nextstart/restart/update(the on-boot refresh already runs there).No speculative
ExecStartPrehook: with cwd stable, the only remaining failure is a deleted venv (status=203/EXEC), which no unit rewrite can fix — it needs a reinstall.Validation
WorkingDirectory(user unit)<checkout>(can vanish)HERMES_HOME(stable)