The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): preserve system service user during migration
KoNit-K18a45464f305
diff --git a/hermes_cli/gateway_migrate.py b/hermes_cli/gateway_migrate.pyindex 16684da4cf1..5e88e7a4853 100644--- a/hermes_cli/gateway_migrate.py+++ b/hermes_cli/gateway_migrate.py@@ -37,6 +37,7 @@ class ProfileGateway:home: Pathpid: Optional[int] = Noneservice: Optional[tuple[str, bool]] = None # ("systemd", system) | ("launchd", False)+ run_as_user: Optional[str] = None # User= recorded by a system-scope systemd unituid: Optional[int] = None # owner of the gateway process/unit; None = unknown (never "different")runtime_home: Optional[Path] = None # HERMES_HOME the installed unit pins, when it differs from ``home``@@ -58,6 +59,7 @@ class ProfileGateway:return {"profile": self.name, "home": str(self.home), "pid": self.pid,"service": None if self.service is None else {"kind": self.service[0], "system": self.service[1]},+ "run_as_user": self.run_as_user,"uid": self.uid, "runtime_home": None if self.runtime_home is None else str(self.runtime_home),}@@ -98,6 +100,12 @@ class MigrationPlan:return self.default.servicereturn next((p.service for p in self.secondaries if p.service is not None), None)+ def target_run_as_user(self) -> Optional[str]:+ """Preserve the system unit identity that the default unit replaces."""+ if self.default.run_as_user:+ return self.default.run_as_user+ return next((p.run_as_user for p in self.secondaries if p.run_as_user), None)+def to_dict(self) -> dict:return {"default_home": str(self.default_home),@@ -183,7 +191,16 @@ def _installed_service(home: Path) -> Optional[tuple[str, bool]]:return None-def _service_op(kind: str, system: bool, verb: str, home: Path) -> None:+def _systemd_service_user(home: Path, service: Optional[tuple[str, bool]]) -> Optional[str]:+ """Read ``User=`` before migration removes a system-scope unit."""+ if service != ("systemd", True):+ return None+ from hermes_cli import gateway as gw+ with _home_env(home):+ return gw._read_systemd_user_from_unit(gw.get_systemd_unit_path(system=True))+++def _service_op(kind: str, system: bool, verb: str, home: Path, *, run_as_user: Optional[str] = None) -> None:"""``stop`` / ``uninstall`` / ``start`` / ``restart`` / ``install`` on ``home``'s service."""from hermes_cli import gateway as gwwith _home_env(home):@@ -191,7 +208,7 @@ def _service_op(kind: str, system: bool, verb: str, home: Path) -> None:if kind == "launchd":gw.launchd_install()else:- gw.systemd_install(system=system, non_interactive=True)+ gw.systemd_install(system=system, run_as_user=run_as_user, non_interactive=True)returngw._service_call(kind, verb, system)@@ -401,7 +418,8 @@ def build_migration_plan() -> MigrationPlan:for name, home in _profile_homes():pid, service = _live_gateway_pid(home), _installed_service(home)uid, runtime_home = _gateway_identity(home, pid, service)- profiles.append(ProfileGateway(name=name, home=home, pid=pid, service=service, uid=uid,+ profiles.append(ProfileGateway(name=name, home=home, pid=pid, service=service,+ run_as_user=_systemd_service_user(home, service), uid=uid,runtime_home=None if runtime_home == home else runtime_home))plan = MigrationPlan(default_home=default_home, profiles=profiles,@@ -587,7 +605,13 @@ def _wait_for_served(default_home: Path, expected: set[str], timeout: float) ->return served-def _restart_default(plan_default: ProfileGateway, target: Optional[tuple[str, bool]], default_home: Path) -> str:+def _restart_default(+ plan_default: ProfileGateway,+ target: Optional[tuple[str, bool]],+ default_home: Path,+ *,+ run_as_user: Optional[str] = None,+) -> str:"""Bring the default gateway up on the new flag value; returns a one-line description."""if plan_default.service is not None:kind, system = plan_default.service@@ -595,7 +619,7 @@ def _restart_default(plan_default: ProfileGateway, target: Optional[tuple[str, breturn f"restarted the default gateway via {kind}"if target is not None:kind, system = target- _service_op(kind, system, "install", default_home)+ _service_op(kind, system, "install", default_home, run_as_user=run_as_user)_service_op(kind, system, "start", default_home)return f"installed and started the default gateway via {kind}"verb = "restarted" if plan_default.pid is not None else "started"@@ -635,7 +659,7 @@ def apply_migration(plan: MigrationPlan, *, served_wait: float = _SERVED_WAIT_SEprint(f" ✓ {p.name}: stopped standalone gateway (pid {p.pid})")_write_multiplex_flag(plan.default_home, True)print(f" ✓ default: gateway.multiplex_profiles: true ({plan.default_home / 'config.yaml'})")- print(f" ✓ {_restart_default(plan.default, plan.target_service_kind(), plan.default_home)}")+ print(f" ✓ {_restart_default(plan.default, plan.target_service_kind(), plan.default_home, run_as_user=plan.target_run_as_user())}")expected = {p.name for p in plan.profiles}served = _wait_for_served(plan.default_home, expected, served_wait)@@ -694,7 +718,9 @@ def rollback_migration(default_home: Optional[Path] = None) -> bool:service = _secondary_service(rec)if service is not None:kind, system = service- _service_op(kind, system, "install", home)+ run_as_user = rec.get("run_as_user")+ _service_op(kind, system, "install", home,+ run_as_user=run_as_user if isinstance(run_as_user, str) else None)_service_op(kind, system, "start", home)print(f" ✓ {name}: reinstalled and started its {kind} service")elif rec.get("pid"):diff --git a/tests/hermes_cli/test_gateway_migrate_multiplex.py b/tests/hermes_cli/test_gateway_migrate_multiplex.pyindex 1d1b4f6f914..55e9e65ce7c 100644--- a/tests/hermes_cli/test_gateway_migrate_multiplex.py+++ b/tests/hermes_cli/test_gateway_migrate_multiplex.py@@ -42,7 +42,7 @@ def fleet(tmp_path, monkeypatch):refused_at_start={},)- def _service_op(kind, system, verb, home):+ def _service_op(kind, system, verb, home, *, run_as_user=None):name = _name(home)state.ops.append((name, verb))if verb == "start" and name != "default":@@ -148,15 +148,45 @@ def test_apply_records_manifest_flips_flag_and_rollback_restores(fleet, capsys):assert not (fleet.root / gm.MANIFEST_NAME).exists()+def test_migration_preserves_root_system_service_user_for_default_install(fleet, monkeypatch):+ """A root-owned secondary system unit must be replaced with an explicit root unit."""+ fleet.services["coder"] = ("systemd", True)+ monkeypatch.setattr(+ gm,+ "_systemd_service_user",+ lambda home, service: "root" if _name(home) == "coder" and service == ("systemd", True) else None,+ )+ plan = gm.build_migration_plan()+ root_secondary = next(p for p in plan.standalone_secondaries if p.name == "coder")+ assert root_secondary.run_as_user == "root"+ installs = []++ def _service_op(kind, system, verb, home, *, run_as_user=None):+ if _name(home) == "default" and verb == "install":+ installs.append((kind, system, run_as_user))+ fleet.services.pop(_name(home), None) if verb == "uninstall" else None+ if verb == "install":+ fleet.services[_name(home)] = (kind, system)+ if verb in ("start", "restart") and _name(home) == "default":+ (fleet.root / "gateway_state.json").write_text(json.dumps({+ "served_profiles": ["default", "coder", "ops"]}))++ monkeypatch.setattr(gm, "_service_op", _service_op)+ monkeypatch.setattr(gm, "_wait_for_served", lambda *args: ["default", "coder", "ops"])++ assert gm.apply_migration(plan, served_wait=0.1) is True+ assert installs == [("systemd", True, "root")]++def test_rollback_with_failed_secondary_still_restarts_default_and_keeps_manifest(fleet, monkeypatch):assert gm.apply_migration(gm.build_migration_plan(), served_wait=5.0) is Truefleet.ops.clear()real_op = gm._service_op- def _flaky(kind, system, verb, home):+ def _flaky(kind, system, verb, home, *, run_as_user=None):if verb == "start" and _name(home) == "coder":raise RuntimeError("systemctl start failed")- real_op(kind, system, verb, home)+ real_op(kind, system, verb, home, run_as_user=run_as_user)monkeypatch.setattr(gm, "_service_op", _flaky)assert gm.rollback_migration(fleet.root) is False