From f45a3fb2b0ea83ee0dac46267351d7505d8f5fa4 Mon Sep 17 00:00:00 2001 From: kshitij <82637225+kshitijk4poor@users.noreply.github.com> Date: Mon, 10 Aug 2026 11:18:08 +0530 Subject: [PATCH] fix(update): force-reload config modules before migration check MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit hermes update runs in the PRE-pull Python process. After git pull updates the source files on disk, sys.modules still holds the OLD hermes_cli.config and hermes_cli.config_migrations. Function-level imports return the cached module, so DEFAULT_CONFIG["_config_version"] is the OLD value and check_config_version() reports (33, 33) — "up to date" — even though the freshly-pulled code has v34 with a migration to run. The personality reset migration (#81946) was silently skipped this way: display.personality: kawaii stayed active after updates that should have reset it. Every user who updated from a pre-v34 codebase to a post-v34 codebase was affected. Fix: _run_config_check_fresh and _run_migrate_config_fresh call importlib.reload() on hermes_cli.config_defaults, hermes_cli.config, and hermes_cli.config_migrations before calling check_config_version and migrate_config. This forces the modules to be re-read from the updated source files on disk. --- hermes_cli/update_cmd.py | 77 ++++++++++++++++++++++-- tests/hermes_cli/test_cmd_update.py | 51 +++++++++++++--- tests/hermes_cli/test_update_yes_flag.py | 8 +-- 3 files changed, 119 insertions(+), 17 deletions(-) diff --git a/hermes_cli/update_cmd.py b/hermes_cli/update_cmd.py index 21918a347f..df1c1a31f3 100644 --- a/hermes_cli/update_cmd.py +++ b/hermes_cli/update_cmd.py @@ -85,6 +85,59 @@ def _reload_updated_runtime_modules() -> None: except Exception as exc: logger.debug("Could not refresh update runtime modules: %s", exc) + +def _run_config_check_fresh() -> tuple: + """Check config version using freshly-reloaded modules. + + ``hermes update`` runs in the PRE-pull Python process. After ``git pull`` + updates the source files on disk, modules already in ``sys.modules`` + still hold the OLD code. Function-level imports return the cached module, + so ``DEFAULT_CONFIG["_config_version"]`` is the OLD value and + ``check_config_version()`` reports ``(33, 33)`` — "up to date" — even + though the freshly-pulled code has v34 with a migration to run. + + This function force-reloads ``hermes_cli.config`` and + ``hermes_cli.config_migrations`` from disk so the version check reads + the UPDATED ``DEFAULT_CONFIG``. Returns ``(current_ver, latest_ver)``. + """ + import importlib + + importlib.invalidate_caches() + for mod_name in ("hermes_cli.config_defaults", "hermes_cli.config", "hermes_cli.config_migrations"): + mod = sys.modules.get(mod_name) + if mod is not None: + try: + importlib.reload(mod) + except Exception as exc: + logger.debug("Could not reload %s for fresh config check: %s", mod_name, exc) + + from hermes_cli.config import check_config_version + + return check_config_version() + + +def _run_migrate_config_fresh(*, interactive: bool = False, quiet: bool = False) -> dict: + """Run config migration using freshly-reloaded modules. + + See ``_run_config_check_fresh`` for why this is necessary. + Returns the migration results dict. + """ + import importlib + + importlib.invalidate_caches() + for mod_name in ("hermes_cli.config_defaults", "hermes_cli.config", "hermes_cli.config_migrations"): + mod = sys.modules.get(mod_name) + if mod is not None: + try: + importlib.reload(mod) + except Exception as exc: + logger.debug("Could not reload %s for fresh migration: %s", mod_name, exc) + + from hermes_cli.config import migrate_config + + return migrate_config(interactive=interactive, quiet=quiet) + + # Critical files that Hermes must be able to import immediately after an # update/install. Most are imported on every CLI startup; ``web_server.py`` # is the desktop/dashboard backend path that a fresh Windows install launches @@ -4688,20 +4741,32 @@ def _cmd_update_impl(args, gateway_mode: bool): except Exception: pass # honcho plugin not installed or not configured - # Check for config migrations + # Check for config migrations. + # + # CRITICAL: check_config_version and migrate_config must use + # freshly-reloaded modules, not the sys.modules cache. The + # ``hermes update`` process is the PRE-pull Python process — its + # ``sys.modules`` cache holds the OLD ``hermes_cli.config`` and + # ``hermes_cli.config_migrations`` from before ``git pull`` updated + # the source files. A function-level ``from hermes_cli.config import + # check_config_version`` returns the cached module, so + # ``DEFAULT_CONFIG["_config_version"]`` is the OLD value and + # ``check_config_version()`` reports ``(33, 33)`` — "up to date" — + # even though the freshly-pulled code has v34 with a migration to + # run. The personality reset migration (#81946) was silently skipped + # this way, leaving ``display.personality: kawaii`` active after + # updates that should have reset it. print() print("→ Checking configuration for new options...") from hermes_cli.config import ( get_missing_env_vars, get_missing_config_fields, - check_config_version, - migrate_config, ) missing_env = get_missing_env_vars(required_only=True) missing_config = get_missing_config_fields() - current_ver, latest_ver = check_config_version() + current_ver, latest_ver = _run_config_check_fresh() has_new_options = bool(missing_env or missing_config) version_bump_only = ( @@ -4720,7 +4785,7 @@ def _cmd_update_impl(args, gateway_mode: bool): f" ℹ Updating config format (v{current_ver} → v{latest_ver})…" ) try: - migrate_config(interactive=False, quiet=True) + _run_migrate_config_fresh(interactive=False, quiet=True) print(" ✓ Config format updated (no new settings to configure)") except Exception as _mig_err: print(f" ⚠️ Config format update failed: {_mig_err}") @@ -4806,7 +4871,7 @@ def _cmd_update_impl(args, gateway_mode: bool): interactive_migration = not ( gateway_mode or assume_yes or response == "auto" ) - results = migrate_config(interactive=interactive_migration, quiet=False) + results = _run_migrate_config_fresh(interactive=interactive_migration, quiet=False) if results["env_added"] or results["config_added"]: print() diff --git a/tests/hermes_cli/test_cmd_update.py b/tests/hermes_cli/test_cmd_update.py index e2a546da58..8d9f6b608d 100644 --- a/tests/hermes_cli/test_cmd_update.py +++ b/tests/hermes_cli/test_cmd_update.py @@ -244,10 +244,12 @@ class TestCmdUpdateBranchFallback: ), patch( "hermes_cli.config.get_missing_config_fields", return_value=[{"key": "new.option", "default": True}], - ), patch("hermes_cli.config.check_config_version", return_value=(1, 2)), patch( - "hermes_cli.config.migrate_config", + ), patch( + "hermes_cli.update_cmd._run_config_check_fresh", return_value=(1, 2) + ), patch( + "hermes_cli.update_cmd._run_migrate_config_fresh", return_value={"env_added": [], "config_added": ["new.option"]}, - ), patch("hermes_cli.main.sys") as mock_sys: + ) as migrate_config, patch("hermes_cli.main.sys") as mock_sys: mock_sys.stdin.isatty.return_value = False mock_sys.stdout.isatty.return_value = False mock_run.side_effect = _make_run_side_effect( @@ -257,8 +259,6 @@ class TestCmdUpdateBranchFallback: cmd_update(mock_args) mock_input.assert_not_called() - from hermes_cli.config import migrate_config - migrate_config.assert_called_once_with(interactive=False, quiet=False) captured = capsys.readouterr() assert "applying safe config migrations" in captured.out @@ -286,9 +286,9 @@ class TestCmdUpdateMigrationPrompt: ), patch( "hermes_cli.config.get_missing_config_fields", return_value=[] ), patch( - "hermes_cli.config.check_config_version", return_value=(5, 24) + "hermes_cli.update_cmd._run_config_check_fresh", return_value=(5, 24) ), patch( - "hermes_cli.config.migrate_config", + "hermes_cli.update_cmd._run_migrate_config_fresh", return_value={"env_added": [], "config_added": [], "warnings": []}, ) as mock_migrate: mock_run.side_effect = _make_run_side_effect( @@ -342,6 +342,43 @@ class TestCmdUpdateMigrationPrompt: assert "display.new_widget" in out +class TestConfigVersionCheckUsesFreshModules: + """Regression: config migration must use freshly-reloaded modules, not the + sys.modules cache from before git pull. + + Before the fix, ``hermes update`` ran in the PRE-pull Python process. + After ``git pull`` updated the source on disk, function-level imports + returned the OLD cached ``hermes_cli.config`` module — so + ``DEFAULT_CONFIG["_config_version"]`` was stale and + ``check_config_version()`` reported ``(33, 33)`` "up to date" even though + the freshly-pulled code had v34 with a migration to run. The personality + reset migration (#81946) was silently skipped this way. + """ + + def test_run_config_check_fresh_reloads_modules(self): + """_run_config_check_fresh must call importlib.reload on config modules.""" + import importlib + import sys + from unittest.mock import patch + + import hermes_cli.update_cmd as update_cmd + + reloaded = [] + original_reload = importlib.reload + + def tracking_reload(mod, *args, **kwargs): + reloaded.append(mod.__name__) + return original_reload(mod, *args, **kwargs) + + with patch("importlib.reload", side_effect=tracking_reload): + update_cmd._run_config_check_fresh() + + # The config modules that hold DEFAULT_CONFIG and MIGRATIONS must + # have been reloaded — this is the fix for the stale-cache bug. + assert "hermes_cli.config" in reloaded + assert "hermes_cli.config_migrations" in reloaded + + class TestCmdUpdateProfileSkillSync: """cmd_update syncs bundled skills to all profiles, including the active one. diff --git a/tests/hermes_cli/test_update_yes_flag.py b/tests/hermes_cli/test_update_yes_flag.py index 20d99e8b08..23f8ecec76 100644 --- a/tests/hermes_cli/test_update_yes_flag.py +++ b/tests/hermes_cli/test_update_yes_flag.py @@ -50,8 +50,8 @@ def _make_run_side_effect( class TestUpdateYesConfigMigration: """--yes auto-answers the config-migration prompt and skips API-key prompts.""" - @patch("hermes_cli.config.migrate_config") - @patch("hermes_cli.config.check_config_version", return_value=(1, 2)) + @patch("hermes_cli.update_cmd._run_migrate_config_fresh") + @patch("hermes_cli.update_cmd._run_config_check_fresh", return_value=(1, 2)) @patch("hermes_cli.config.get_missing_config_fields", return_value=[]) @patch("hermes_cli.config.get_missing_env_vars", return_value=["NEW_KEY"]) @patch("shutil.which", return_value=None) @@ -89,8 +89,8 @@ class TestUpdateYesConfigMigration: # The "Would you like to configure them now?" prompt text never appears. assert "Would you like to configure them now?" not in out - @patch("hermes_cli.config.migrate_config") - @patch("hermes_cli.config.check_config_version", return_value=(1, 2)) + @patch("hermes_cli.update_cmd._run_migrate_config_fresh") + @patch("hermes_cli.update_cmd._run_config_check_fresh", return_value=(1, 2)) @patch("hermes_cli.config.get_missing_config_fields", return_value=[]) @patch("hermes_cli.config.get_missing_env_vars", return_value=["NEW_KEY"]) @patch("shutil.which", return_value=None)