The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(update): one definition of "first-party module"
kshitij15cb86eba3fe
diff --git a/hermes_cli/update_cmd.py b/hermes_cli/update_cmd.pyindex 42c3382352d..bf0415ce0b0 100644--- a/hermes_cli/update_cmd.py+++ b/hermes_cli/update_cmd.py@@ -195,6 +195,8 @@ def _validate_critical_modules_import(root) -> tuple[bool, str | None, str | NonReturns ``(ok, failing_module, error_message)``."""+ from hermes_constants import FIRST_PARTY_MODULE_ROOTS+probe = ("import importlib, sys\n""for name in %r:\n"@@ -202,12 +204,11 @@ def _validate_critical_modules_import(root) -> tuple[bool, str | None, str | Non" importlib.import_module(name)\n"" except ModuleNotFoundError as exc:\n"# A missing *third-party* module means dependencies aren't installed- # yet (this guard can run before the dependency sync on the git path),- # not a skewed checkout. Only treat it as breakage when the missing- # module is one of ours.+ # yet, not a skewed checkout. Only our own packages count as breakage.+ # The root set is injected from hermes_constants so this can't drift+ # from the hint the user is shown (they disagreed once already)." missing = (getattr(exc, 'name', '') or '').split('.')[0]\n"- " if missing in ('tools', 'agent', 'gateway', 'plugins', 'providers') \\\n"- " or missing.startswith('hermes'):\n"+ " if missing in %r or missing.startswith('hermes_'):\n"" sys.stdout.write(name + '\\n' + str(exc))\n"" raise SystemExit(3)\n"" except ImportError as exc:\n"@@ -215,7 +216,8 @@ def _validate_critical_modules_import(root) -> tuple[bool, str | None, str | Non" raise SystemExit(3)\n"" except Exception:\n"" pass\n" # non-import errors (config/env) aren't update breakage- "raise SystemExit(0)\n" % (_UPDATE_CRITICAL_MODULES,)+ "raise SystemExit(0)\n"+ % (_UPDATE_CRITICAL_MODULES, tuple(sorted(FIRST_PARTY_MODULE_ROOTS))))try:interpreter = sys.executablediff --git a/hermes_constants.py b/hermes_constants.pyindex 2f86ce88949..9ad478f9e27 100644--- a/hermes_constants.py+++ b/hermes_constants.py@@ -1252,6 +1252,43 @@ AI_GATEWAY_BASE_URL = "https://ai-gateway.vercel.sh/v1"# ─── Partial-update diagnostics ──────────────────────────────────────────────+# Top-level packages/modules that ship as part of Hermes itself. An ImportError+# naming one of these means our own tree is inconsistent; anything else is a+# third-party problem with different remediation. Single source of truth —+# `hermes_cli.update_cmd`'s post-update probe consumes this same set so the+# guard that BLOCKS and the hint that EXPLAINS can never disagree.+FIRST_PARTY_MODULE_ROOTS = frozenset(+ {+ "agent",+ "acp_adapter",+ "cli",+ "cron",+ "gateway",+ "model_tools",+ "plugins",+ "providers",+ "tools",+ "toolsets",+ "run_agent",+ "tui_gateway",+ "utils",+ }+)+++def is_first_party_module(name: str | None) -> bool:+ """True when *name* is a module that ships with Hermes.++ Matches on the first dotted segment against an exact set — a substring or+ ``startswith`` test would also claim third-party ``agents``, ``agentops``,+ and ``toolsets_x``.+ """+ root = str(name).split(".")[0] if name else ""+ if not root:+ return False+ return root in FIRST_PARTY_MODULE_ROOTS or root.startswith("hermes_")++def partial_update_hint(exc: BaseException) -> list[str]:"""Return recovery guidance lines when *exc* looks like a half-updated tree.@@ -1276,13 +1313,7 @@ def partial_update_hint(exc: BaseException) -> list[str]:if isinstance(exc, ModuleNotFoundError):return []name = getattr(exc, "name", None)- # Compare the first dotted segment against an exact set: a bare- # ``startswith`` would also match third-party ``agents``, ``agentops``,- # ``toolsets``, etc. and blame our updater for their import errors.- root = str(name).split(".")[0] if name else ""- if root not in {"tools", "agent", "gateway", "plugins", "providers"} and not (- root == "cli" or root.startswith("hermes_") or root == "hermes"- ):+ if not is_first_party_module(name):return []return ["",diff --git a/tests/hermes_cli/test_update_import_guard.py b/tests/hermes_cli/test_update_import_guard.pyindex fbd14223e14..3e581ea3374 100644--- a/tests/hermes_cli/test_update_import_guard.py+++ b/tests/hermes_cli/test_update_import_guard.py@@ -193,3 +193,45 @@ def test_hint_fires_for_each_first_party_root(modname):exc = ImportError("cannot import name 'X'")exc.name = modnameassert partial_update_hint(exc), f"expected guidance for {modname}"+++def test_probe_and_hint_share_one_first_party_definition():+ """The guard that BLOCKS and the hint that EXPLAINS must never disagree.++ These started as two hand-maintained lists and immediately diverged:+ `cli` was first-party to the hint but not the probe, and `hermesx`+ (third-party) matched the probe's loose `startswith("hermes")`. A user+ could get a rollback with no explanation, or an explanation with no+ detection. Both now derive from FIRST_PARTY_MODULE_ROOTS; this test+ fails if either grows a private copy.+ """+ from hermes_constants import FIRST_PARTY_MODULE_ROOTS, is_first_party_module++ captured = {}++ class _Result:+ returncode = 0+ stdout = ""+ stderr = ""++ def capture(cmd, **_kw):+ captured["probe"] = cmd[-1]+ return _Result()++ real_run = update_cmd.subprocess.run+ update_cmd.subprocess.run = capture+ try:+ update_cmd._validate_critical_modules_import("/tmp")+ finally:+ update_cmd.subprocess.run = real_run++ probe_src = captured["probe"]+ # Every first-party root must appear in the probe's injected tuple.+ for root in FIRST_PARTY_MODULE_ROOTS:+ assert repr(root) in probe_src, f"{root} missing from the probe's root set"+ # And the hint must agree on each of them.+ for root in FIRST_PARTY_MODULE_ROOTS:+ assert is_first_party_module(f"{root}.anything")+ # Lookalikes stay out of both.+ for lookalike in ("agents", "agentops", "toolsets_x", "hermesx"):+ assert not is_first_party_module(lookalike)