mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-11-16 02:46:38 +03:00
5cad79d797
The guard that turns registrars into no-ops after a plugin's load times out wrapped only the names defined directly on PluginContext. register_idle_task lives on a mixin, so a plugin whose register() overran the load timeout could still register an idle task from its abandoned worker after the failure path had swept its other registrations, and the host would later run it. The guard now walks PluginContext's MRO, so every register_*, subscribe and on_unload that PluginContext defines or inherits is a no-op once the load is abandoned. A contract test calls every registrar on an abandoned context and expects nothing to run. The guard lives in hermes_cli.plugins_load_guard, so the hermes_cli.plugins facade does not grow with it; plugins.py applies it to PluginContext once, after its last registrar is defined. Co-authored-by: xue xinglong <257216957+chaos-xxl@users.noreply.github.com>
44 lines
1.9 KiB
Python
44 lines
1.9 KiB
Python
"""The abandoned-load guard behind :class:`hermes_cli.plugins.PluginContext`.
|
|
|
|
A plugin's ``register()`` runs on a worker bounded by ``plugins.load_timeout_seconds``. When it
|
|
overruns, the loader marks the context abandoned (``PluginContext._abandon_load``) and sweeps what it
|
|
registered, but the worker thread may still be executing ``register()``; every registrar it reaches
|
|
afterwards must be a no-op, or the late registration lands in registries the failure path already
|
|
swept (#108139). ``hermes_cli.plugins`` applies :func:`ignore_registrars_after_abandoned_load` to
|
|
``PluginContext`` once, after its last registrar is defined.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
from functools import wraps
|
|
|
|
logger = logging.getLogger("hermes_cli.plugins")
|
|
|
|
|
|
def _ignore_after_abandoned_load(method):
|
|
"""Turn one registrar into a no-op once the context's load timed out."""
|
|
@wraps(method)
|
|
def wrapped(self, *args, **kwargs):
|
|
if getattr(self, "_load_abandoned", False):
|
|
logger.warning(
|
|
"Plugin '%s' called %s() after its load timed out; ignored", self.manifest.name,
|
|
method.__name__,
|
|
)
|
|
return None
|
|
return method(self, *args, **kwargs)
|
|
|
|
return wrapped
|
|
|
|
|
|
def ignore_registrars_after_abandoned_load(cls: type) -> None:
|
|
"""Guard every mutating entry point plugins reach through ``ctx`` during register(): each
|
|
``register_*``, ``subscribe`` and ``on_unload`` that ``cls`` defines or inherits from a mixin.
|
|
Applied by name over the MRO, so the guard cannot drift from the surface as registrars or
|
|
mixins are added."""
|
|
names = {name for klass in cls.__mro__ for name in vars(klass)
|
|
if name.startswith("register_") or name in {"subscribe", "on_unload"}}
|
|
for name in sorted(names):
|
|
if callable(method := getattr(cls, name)):
|
|
setattr(cls, name, _ignore_after_abandoned_load(method))
|