Files
hermes-agent/hermes_cli/web_deps.py
Teknium 2776813df3 compat(plugins): temporary import-path shims for external plugins — ONE commit, revert on schedule
The Sep 2026 decomposition (PR #102117) makes internal import paths a non-API: names now live in
the focused modules that define them. This commit is the ONLY thing keeping the old paths alive,
so external plugins have time to update. It is deliberately a single, unsquashed commit:

    git revert <this sha>

removes every shim, stub and manifest at once on the announced date. Nothing in-tree may depend on
these pointers: scripts/check_compat_pointers.py (wired into lint.yml) fails CI if it does.

What it adds (see COMPAT_MANIFEST.md, compat_manifest.json):
- 332 facade modules get one delimited `PLUGIN-COMPAT` block appended at the end of the file
- 1,172 moved names resolved lazily via a module `__getattr__` (PEP 562) — never a top-level import,
  so no import cycles; facades that already had `__getattr__` get a chained one
- 592 third-party/stdlib names the old modules used to expose, with their original import statements
- 266 public definitions that had been deleted as unused, restored byte-for-byte from the pre-decomposition
  tree (+40 private helpers and 16 imports pulled in only because a restored definition needs them)
- 3 deleted modules recreated as re-export stubs (gateway/startup_watchdog, hermes_cli/observability/
  relay_runtime, tools/environments/modal_utils)
- private names (`_x`) get no pointer: they were never API (3,792 skipped)

Verified: all 335 touched modules import under a fresh HERMES_HOME and every manifest name resolves;
the lint reports zero in-tree uses; ruff clean; targeted suites unchanged.
2026-09-03 17:13:22 -07:00

133 lines
4.3 KiB
Python

"""Late-binding dependency seam for extracted dashboard routers.
``web_server`` owns the dashboard app and its process state; routers under
``web_routers/`` cannot import it at import time (web_server imports them to mount them —
a cycle) and must not copy its state (tests ``monkeypatch.setattr(web_server, ...)`` and
expect that to win). ``late(name)`` / ``LateState(name)`` resolve ``<module>.<name>`` *at
call time*; ``module`` defaults to ``web_server`` and may name a ``web_server_<concern>``
module whose helper tests monkeypatch there.
"""
from __future__ import annotations
import importlib
import sys
from typing import Any
WEB_SERVER = "hermes_cli.web_server"
def _server(module: str = WEB_SERVER):
"""Return the live ``module`` (imported on demand)."""
mod = sys.modules.get(module)
if mod is None: # pragma: no cover - routers are only mounted by web_server
mod = importlib.import_module(module)
return mod
def late(name: str, module: str = WEB_SERVER):
"""Late-binding proxy for a callable defined on ``module``."""
def _proxy(*args: Any, **kwargs: Any):
return getattr(_server(module), name)(*args, **kwargs)
_proxy.__name__ = name
_proxy.__qualname__ = name
return _proxy
class LateState:
"""Live proxy for module-level state owned by ``web_server``.
Forwards attribute/item access, iteration, membership, len/truthiness, ``with`` (locks)
and rich comparisons to ``web_server.<name>`` resolved at operation time — some state is
defined *after* the router's ``include_router`` point, so a late import would miss it.
"""
__slots__ = ("_name", "_module")
def __init__(self, name: str, module: str = WEB_SERVER) -> None:
object.__setattr__(self, "_name", name)
object.__setattr__(self, "_module", module)
def _target(self) -> Any:
return getattr(_server(object.__getattribute__(self, "_module")),
object.__getattribute__(self, "_name"))
def __getattr__(self, attr: str) -> Any:
return getattr(self._target(), attr)
def __getitem__(self, key: Any) -> Any:
return self._target()[key]
def __setitem__(self, key: Any, value: Any) -> None:
self._target()[key] = value
def __delitem__(self, key: Any) -> None:
del self._target()[key]
def __contains__(self, item: Any) -> bool:
return item in self._target()
def __iter__(self):
return iter(self._target())
def __len__(self) -> int:
return len(self._target())
def __bool__(self) -> bool:
return bool(self._target())
def __enter__(self):
return self._target().__enter__()
def __exit__(self, *exc):
return self._target().__exit__(*exc)
def __eq__(self, other: Any) -> bool:
return self._target() == other
def __ne__(self, other: Any) -> bool:
return self._target() != other
def __lt__(self, other: Any) -> bool:
return self._target() < other
def __le__(self, other: Any) -> bool:
return self._target() <= other
def __gt__(self, other: Any) -> bool:
return self._target() > other
def __ge__(self, other: Any) -> bool:
return self._target() >= other
def __hash__(self) -> int:
return hash(self._target())
def __repr__(self) -> str: # pragma: no cover - debugging aid
return f"<LateState {object.__getattribute__(self, '_name')} -> {self._target()!r}>"
# ---- BEGIN PLUGIN-COMPAT (revert-scheduled; see COMPAT_MANIFEST.md) ----
# Names external plugins imported from this module before the Sep 2026 decomposition.
# Internal code MUST NOT use these (scripts/check_compat_pointers.py fails CI if it does).
# The whole block is removed by reverting the commit that added it.
def get_dashboard_health():
"""The ``DASHBOARD_HEALTH`` singleton owned by web_server."""
return _server().DASHBOARD_HEALTH
def get_session_token() -> str:
"""Current dashboard session token (``web_server._SESSION_TOKEN``)."""
return _server()._SESSION_TOKEN
def has_valid_session_token(request) -> bool:
"""Late-bound alias for ``web_server._has_valid_session_token``."""
return _server()._has_valid_session_token(request)
def late_attr(name: str) -> Any:
"""Read ``web_server.<name>`` right now (for non-callable state reads)."""
return getattr(_server(), name)
# ---- END PLUGIN-COMPAT ----