From b67f2021842e3f39f814e294b74e55daa60e514c Mon Sep 17 00:00:00 2001 From: Tachi Date: Sat, 1 Aug 2026 14:11:34 +0400 Subject: [PATCH] fix(update): honor lazy install opt-out during restore --- .../test_lazy_refresh_venv_repair.py | 68 +++++++++++++++++++ tests/tools/test_lazy_deps.py | 25 ++++--- tools/lazy_deps.py | 27 ++------ 3 files changed, 84 insertions(+), 36 deletions(-) diff --git a/tests/hermes_cli/test_lazy_refresh_venv_repair.py b/tests/hermes_cli/test_lazy_refresh_venv_repair.py index fef990aebd..aef31c2848 100644 --- a/tests/hermes_cli/test_lazy_refresh_venv_repair.py +++ b/tests/hermes_cli/test_lazy_refresh_venv_repair.py @@ -4,6 +4,7 @@ from __future__ import annotations import textwrap from pathlib import Path +from types import SimpleNamespace from unittest.mock import MagicMock, patch import hermes_cli.main as m @@ -133,6 +134,73 @@ def test_refresh_uses_pre_rebuild_snapshot_when_provided(monkeypatch): assert restored == [["platform.telegram"]] +def test_cmd_update_captures_and_propagates_pre_rebuild_snapshot( + tmp_path, monkeypatch +): + """The updater must carry pre-rebuild state into its repair refresh.""" + from hermes_cli import managed_uv, update_cmd + + (tmp_path / ".git").mkdir() + snapshot = ["platform.telegram"] + refresh_calls = [] + + class RefreshReached(Exception): + pass + + def fake_run(cmd, **kwargs): + if "rev-parse" in cmd: + return SimpleNamespace(returncode=0, stdout="main\n", stderr="") + if "rev-list" in cmd: + return SimpleNamespace(returncode=0, stdout="0\n", stderr="") + return SimpleNamespace(returncode=0, stdout="", stderr="") + + def fake_refresh(prefix, *, env=None, features=None): + refresh_calls.append((prefix, env, features)) + raise RefreshReached + + monkeypatch.setattr(m, "PROJECT_ROOT", tmp_path) + monkeypatch.setattr(m, "_capture_active_lazy_features", lambda: snapshot.copy()) + monkeypatch.setattr(m, "_is_windows", lambda: False) + monkeypatch.setattr(m, "_run_pre_update_backup", lambda args: None) + monkeypatch.setattr(m, "_pause_windows_gateways_for_update", lambda: None) + monkeypatch.setattr(m, "_resume_windows_gateways_after_update", lambda state: None) + monkeypatch.setattr(update_cmd, "_discard_lockfile_churn", lambda *args: None) + monkeypatch.setattr(m, "_get_origin_url", lambda *args: "https://github.com/NousResearch/hermes-agent.git") + monkeypatch.setattr(m, "_resolve_update_branch", lambda args: "main") + monkeypatch.setattr(m, "_stash_local_changes_if_needed", lambda *args: None) + monkeypatch.setattr(update_cmd, "_invalidate_update_cache", lambda: None) + monkeypatch.setattr( + update_cmd, "_venv_core_imports_healthy", lambda: (False, "broken") + ) + monkeypatch.setattr(update_cmd, "_write_update_incomplete_marker", lambda: None) + monkeypatch.setattr( + m, "_install_python_dependencies_with_optional_fallback", lambda *a, **k: None + ) + monkeypatch.setattr(m, "_refresh_active_lazy_features", fake_refresh) + monkeypatch.setattr(m.subprocess, "run", fake_run) + monkeypatch.setattr(managed_uv, "update_managed_uv", lambda **kwargs: None) + monkeypatch.setattr(managed_uv, "ensure_uv", lambda **kwargs: "uv") + + args = SimpleNamespace( + yes=True, + force=False, + force_venv=False, + no_backup=True, + backup=False, + branch=None, + ) + with pytest.raises(RefreshReached): + m._cmd_update_impl(args, gateway_mode=False) + + assert refresh_calls == [ + ( + ["uv", "pip"], + {**m.os.environ, "VIRTUAL_ENV": str(tmp_path / "venv")}, + snapshot, + ) + ] + + diff --git a/tests/tools/test_lazy_deps.py b/tests/tools/test_lazy_deps.py index fd8dbfffdc..74838a69a3 100644 --- a/tests/tools/test_lazy_deps.py +++ b/tests/tools/test_lazy_deps.py @@ -358,29 +358,28 @@ class TestRefreshActiveFeatures: ld._unsupported_feature_reason("platform.matrix") or "" ) - def test_restore_snapshot_reinstalls_telegram_with_lazy_installs_disabled( + def test_restore_snapshot_skips_telegram_with_lazy_installs_disabled( self, monkeypatch ): - """An update may restore a captured feature without opening runtime installs.""" + """The security opt-out also blocks updater-driven restoration.""" monkeypatch.setattr(ld, "_allow_lazy_installs", lambda: False) - satisfied = iter([False, True]) - monkeypatch.setattr(ld, "_is_satisfied", lambda spec: next(satisfied)) - installs = [] + monkeypatch.setattr(ld, "_is_satisfied", lambda spec: False) monkeypatch.setattr( ld, "_venv_pip_install", - lambda specs, **kw: installs.append(specs) - or ld._InstallResult(True, "", ""), + lambda *args, **kwargs: pytest.fail( + "pip must not run when lazy installs are disabled" + ), ) result = ld.restore_features(["platform.telegram"]) - assert result == {"platform.telegram": "restored"} - assert installs == [("python-telegram-bot[webhooks]==22.6",)] - assert ld._allow_lazy_installs() is False - monkeypatch.setattr(ld, "_is_satisfied", lambda spec: False) - with pytest.raises(ld.FeatureUnavailable, match="lazy installs disabled"): - ld.ensure("platform.telegram", prompt=False) + assert result == { + "platform.telegram": ( + "skipped: lazy installs disabled " + "(security.allow_lazy_installs=false)" + ) + } def test_restore_snapshot_does_not_install_never_activated_features( self, monkeypatch diff --git a/tools/lazy_deps.py b/tools/lazy_deps.py index 855f786a96..88cf6e78b5 100644 --- a/tools/lazy_deps.py +++ b/tools/lazy_deps.py @@ -1139,14 +1139,9 @@ def refresh_active_features(*, prompt: bool = False) -> dict[str, str]: def restore_features(features: list[str]) -> dict[str, str]: """Restore features captured before an explicit managed-runtime rebuild. - ``security.allow_lazy_installs`` gates installs initiated at feature-use - time. A runtime rebuild is different: the updater is deliberately - recreating the environment and may restore only features that were - already present before that rebuild. Feature names are still checked - against :data:`LAZY_DEPS`, so this never accepts arbitrary package specs. - - This does not change the security setting. Subsequent runtime calls to - :func:`ensure` remain subject to the normal lazy-install gate. + Feature names are checked against :data:`LAZY_DEPS`, and installs remain + subject to ``security.allow_lazy_installs``. An explicit opt-out therefore + leaves the captured feature absent and reports it as skipped. """ return _refresh_features(features, prompt=False, restoring=True) @@ -1171,21 +1166,7 @@ def _refresh_features( try: if restoring: - result = _venv_pip_install(missing) - if not result.success: - snippet = (result.stderr or result.stdout or "").strip() - raise FeatureUnavailable( - feature, - missing, - f"pip install failed: {snippet or 'no error output'}", - ) - if feature_missing(feature): - raise FeatureUnavailable( - feature, - missing, - "install reported success but packages are still missing " - "(may require Python restart)", - ) + ensure(feature, prompt=False) results[feature] = "restored" else: ensure(feature, prompt=prompt)