mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-11-16 19:36:54 +00:00
MAJOR: `hermes -p X gateway restart --all` with no installed service re-entered
`run_gateway(replace=True)` IN the CLI process. `_home_env` swapped only HERMES_HOME
while os.environ already held X's dotenv (loaded at CLI startup) and gateway/run.py's
root .env load does not clear inherited keys, so the default gateway came up with X's
TELEGRAM_BOT_TOKEN / OPENAI_API_KEY. And any child spawned inside the swap (Windows
`start --all`, the launchd/detached fallback, migrate's secondary spawns) read the
swapped home as the launch profile, so `served_profile_child_env` neither stripped nor
scrubbed X's env.
- `_home_env` pins the process's launch home for the swap (`pin_process_hermes_home`),
so routed-home decisions inside it keep the real identity; `strip_launch_profile_env`
reads the residue list from that pinned home too.
- `_restart_all_as_host` spawns the root detached (`_spawn_detached_gateway`, scrubbed
base env + the root's own secret scope, `gateway run --replace`) when the CLI runs
under a named profile; the default profile keeps its foreground run. The Desktop
update hand-off's `gateway start --all` under a named active profile is still served.
- test: `test_named_profile_restart_all_with_host_down_restarts_the_default_root` now
asserts the child env carries none of the named profile's keys and `run_gateway` is
never entered (red on the PR head: "the root ran IN the named profile's process").
MINORS:
- (a) `gateway_migrate._service_op('enable')` raises on a non-zero `systemctl enable`;
a fresh apply treats it as a preflight refusal before the flag/manifest write, a
resume reports it and converges. New `test_migrate_refuses_when_the_survivor_cannot_
be_enabled` (red on head: exit 0 with the ⚠ line).
- (b) `systemd_install` already-current branch warns when `systemctl enable` fails.
- (c) `remove_system_systemd_unit` reports a failed `systemctl stop` instead of ✓.
64 lines
3.3 KiB
Python
64 lines
3.3 KiB
Python
"""Profile delete/rename: remove the OLD name's supervisor registrations that live outside
|
|
the user systemd / launchd arms in ``profiles._cleanup_gateway_service``.
|
|
|
|
Every registration is keyed on the profile name and starts ``--profile <old>`` with
|
|
``HERMES_HOME`` pinned to a directory that the delete removed or the rename moved, so a
|
|
survivor crash-loops (KeepAlive / Restart= / Task Scheduler retry) at the next boot or login.
|
|
Callers bind the profile's home (``set_hermes_home_override``) before calling so the
|
|
name-deriving helpers resolve THAT profile, never the ambient one.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import subprocess
|
|
|
|
|
|
def remove_system_systemd_unit() -> bool:
|
|
"""Remove ``/etc/systemd/system/hermes-gateway-<profile>.service`` when root; otherwise
|
|
name the leftover and the exact command, because the profile dir the ``gateway uninstall
|
|
--system`` verb would need to resolve the name from is about to vanish."""
|
|
from hermes_cli import gateway
|
|
svc_name = gateway.get_service_name()
|
|
unit = gateway._SYSTEM_UNIT_DIR / f"{svc_name}.service"
|
|
if not unit.exists():
|
|
return False
|
|
if os.geteuid() != 0: # windows-footgun: ok — called only from the Linux arm of profiles._cleanup_gateway_service
|
|
print(f"⚠ System service {svc_name} remains at {unit}; it will restart the removed profile at boot.")
|
|
print(f" Remove it with: sudo systemctl disable --now {svc_name} && sudo rm {unit} && sudo systemctl daemon-reload")
|
|
return False
|
|
subprocess.run(["systemctl", "disable", svc_name], capture_output=True, check=False, timeout=30)
|
|
stopped = subprocess.run(["systemctl", "stop", svc_name], capture_output=True, check=False, timeout=30)
|
|
unit.unlink(missing_ok=True)
|
|
subprocess.run(["systemctl", "daemon-reload"], capture_output=True, check=False, timeout=30)
|
|
if getattr(stopped, "returncode", 0) != 0:
|
|
# The unit file is gone (it must not resurrect the removed profile at boot), but the
|
|
# gateway it supervised may still be running under the old name.
|
|
print(f"⚠ System service {svc_name} unit removed, but `systemctl stop` exited {stopped.returncode}; "
|
|
f"its gateway may still be running. Stop it with: sudo systemctl stop {svc_name}")
|
|
return True
|
|
print(f"✓ System service {svc_name} removed")
|
|
return True
|
|
|
|
|
|
def remove_windows_task() -> bool:
|
|
"""Delete the profile's Scheduled Task and Startup-folder login item. Direct ``schtasks
|
|
/Delete`` rather than ``gateway_windows.uninstall()``: that verb may open a UAC prompt,
|
|
which a dashboard DELETE or a scripted rename must never block on."""
|
|
from hermes_cli import gateway_windows as gw
|
|
task_name = gw.get_task_name()
|
|
removed = False
|
|
if gw.is_task_registered():
|
|
code, _out, err = gw._exec_schtasks(["/Delete", "/F", "/TN", task_name])
|
|
if code == 0:
|
|
removed = True
|
|
print(f"✓ Removed Scheduled Task {task_name!r}")
|
|
else:
|
|
print(f"⚠ Scheduled Task {task_name!r} remains (schtasks code {code}): {err.strip()}")
|
|
for path in (gw.get_startup_entry_path(), gw._legacy_startup_entry_path()):
|
|
try:
|
|
path.unlink()
|
|
except FileNotFoundError:
|
|
continue
|
|
removed = True
|
|
print(f"✓ Removed Windows login item: {path}")
|
|
return removed
|