The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(update): refuse gateway ancestor tree-kill on Windows
Ayush Nangiac923b53913e5
diff --git a/hermes_cli/update_cmd.py b/hermes_cli/update_cmd.pyindex 4c19f69bd19..adedb4e8f03 100644--- a/hermes_cli/update_cmd.py+++ b/hermes_cli/update_cmd.py@@ -5473,6 +5473,55 @@ def _leftover_pausable_gateway_pids(return pids+def _refuse_gateway_ancestor_tree_kill(+ pids: list[int], *, gateway_mode: bool+) -> bool:+ """Refuse a plain Windows update that would kill its own process tree.++ A chat agent can launch plain ``hermes update`` through its terminal tool.+ In that topology the updater is a child of the gateway. The leftover+ holder recovery below uses ``taskkill /T /F`` on Windows, so force-stopping+ that gateway also kills the updater before it can mutate the checkout+ (#98814).++ ``/update`` uses the supported ``--gateway`` hand-off and is deliberately+ exempt: it detaches the updater and provides file-based progress/result+ delivery. For every other invocation, refuse only when a nominated+ gateway is positively identified as this process's ancestor. If ancestry+ cannot be established, preserve the existing holder recovery behavior.+ """+ if gateway_mode or not pids:+ return False++ try:+ from hermes_cli.gateway import _is_pid_ancestor_of_current_process++ ancestors = [+ int(pid)+ for pid in pids+ if _is_pid_ancestor_of_current_process(int(pid))+ ]+ except Exception as exc:+ logger.debug("Could not inspect gateway ancestry before tree-kill: %s", exc)+ return False++ if not ancestors:+ return False++ rendered = ", ".join(str(pid) for pid in ancestors)+ print(+ "✗ Refusing to stop the gateway process tree because this updater "+ f"is running inside it (gateway PID(s): {rendered})."+ )+ print(+ " On Windows, taskkill /T would terminate the updater before the "+ "update can run."+ )+ print(" From a chat platform, use `/update` instead.")+ print(" Otherwise, run `hermes update` from a separate terminal.")+ return True++def _ledger_manual_serve_holders(matches: list[tuple[int, str, str]],) -> list[dict]:@@ -7801,6 +7850,13 @@ def _cmd_update_impl(args, gateway_mode: bool):if _venv_holders:_gateway_holders = _m()._leftover_pausable_gateway_pids(_venv_holders)if _gateway_holders is not None:+ if _refuse_gateway_ancestor_tree_kill(+ _gateway_holders, gateway_mode=gateway_mode+ ):+ _m()._resume_windows_gateways_after_update(+ _windows_gateway_resume+ )+ sys.exit(2)# Every remaining holder is a gateway the pause machinery# already owns — respawned by its supervisor inside the# pause→guard window, or up through a spawn path discoverydiff --git a/tests/hermes_cli/test_update_concurrent_quarantine.py b/tests/hermes_cli/test_update_concurrent_quarantine.pyindex 631e9102a83..dca6ffae957 100644--- a/tests/hermes_cli/test_update_concurrent_quarantine.py+++ b/tests/hermes_cli/test_update_concurrent_quarantine.py@@ -757,6 +757,52 @@ def test_leftover_holders_that_are_all_gateways_are_nominated(monkeypatch):assert cli_main._leftover_pausable_gateway_pids(matches) == [300, 301]+def test_plain_update_refuses_to_tree_kill_its_gateway_ancestor(+ monkeypatch, capsys+):+ """#98814: terminal-launched update must survive to report the refusal."""+ import hermes_cli.gateway as gateway_cli+ import hermes_cli.update_cmd as update_cmd++ monkeypatch.setattr(+ gateway_cli,+ "_is_pid_ancestor_of_current_process",+ lambda pid: pid == 300,+ )++ refused = update_cmd._refuse_gateway_ancestor_tree_kill(+ [300, 301], gateway_mode=False+ )++ assert refused is True+ output = capsys.readouterr().out+ assert "taskkill /T" in output+ assert "`/update`" in output+ assert "separate terminal" in output+++def test_gateway_handoff_keeps_leftover_gateway_recovery(monkeypatch, capsys):+ """The detached `/update` hand-off still owns leftover gateway cleanup."""+ import hermes_cli.gateway as gateway_cli+ import hermes_cli.update_cmd as update_cmd++ ancestry_checks = []+ monkeypatch.setattr(+ gateway_cli,+ "_is_pid_ancestor_of_current_process",+ lambda pid: ancestry_checks.append(pid) or True,+ )++ assert (+ update_cmd._refuse_gateway_ancestor_tree_kill(+ [300], gateway_mode=True+ )+ is False+ )+ assert ancestry_checks == []+ assert capsys.readouterr().out == ""++def test_one_non_gateway_holder_keeps_the_hard_refusal(monkeypatch):"""A REPL/backend holder means the guard must abort exactly as before."""monkeypatch.setitem(@@ -1012,6 +1058,51 @@ def test_update_gate_still_aborts_on_non_gateway_concurrent(assert "--force" in captured[email protected](cli_main, "_is_windows", return_value=True)+def test_update_impl_refuses_before_terminating_gateway_ancestor(+ _winp, monkeypatch, capsys+):+ """#98814: the live holder path must gate the destructive call itself."""+ import gateway.status as status_mod+ import hermes_cli.gateway as gateway_cli++ holder = (+ 300,+ "python.exe",+ r"C:\x\venv\Scripts\python.exe -m hermes_cli.main gateway run",+ )+ monkeypatch.setattr(+ gateway_cli,+ "_is_pid_ancestor_of_current_process",+ lambda pid: pid == 300,+ )++ with patch.object(+ cli_main, "_venv_scripts_dir", return_value=None+ ), patch.object(+ cli_main, "_run_pre_update_backup", return_value=None+ ), patch.object(+ cli_main, "_pause_windows_gateways_for_update", return_value=None+ ), patch.object(+ cli_main, "_detect_venv_python_processes", return_value=[holder]+ ), patch.object(+ cli_main, "_leftover_pausable_gateway_pids", return_value=[300]+ ), patch.object(+ cli_main, "_resume_windows_gateways_after_update"+ ) as resume, patch.object(+ status_mod, "terminate_pid"+ ) as terminate:+ with pytest.raises(SystemExit) as excinfo:+ cli_main._cmd_update_impl(_update_args(), gateway_mode=False)++ assert excinfo.value.code == 2+ terminate.assert_not_called()+ resume.assert_called_once_with(None)+ output = capsys.readouterr().out+ assert "taskkill /T" in output+ assert "`/update`" in output++def test_stop_service_refuses_pid_reuse_before_sc_stop(monkeypatch):import hermes_cli.update_cmd as update_cmd@@ -1033,4 +1124,3 @@ def test_stop_service_refuses_pid_reuse_before_sc_stop(monkeypatch):assert calls == []-