Under plugins.isolation: host, _import_hosted_plugin_dir had no "already imported" guard, unlike the in-process path (_exec_plugin_module checks sys.modules). Every rescan of the home layer -- an unsettled plugin-dir stamp (the dir was created within the 2 s mtime tick) once the 1 s stamp TTL lapses, or another plugin dir landing while the process runs -- ran the whole hosted import again: another host extraction, fresh proxy objects replacing the ones callers already hold, and the in-process warning for a create_client profile logged again. main's test_create_client_profile_loads_in_process_and_its_sibling_stays_hosted hit it on the Windows arm64 runner (assert 3 == 1): the plugin host's first start there took over a second, so the next lookups landed past the TTL with the stamps still unsettled and rescanned. The layer now records the module names it holds as hosted proxies and skips them on a rescan; a failed host extraction or in-process import is not recorded, so the next scan retries it as before.
93 lines
4.1 KiB
Python
93 lines
4.1 KiB
Python
"""plugins.isolation: host — a profile that cannot cross the host boundary never takes its siblings down."""
|
|
|
|
import logging
|
|
import os
|
|
import sys
|
|
|
|
import hermes_yaml as yaml
|
|
import pytest
|
|
|
|
from hermes_cli import plugins as plugins_mod
|
|
|
|
TWO_PROFILE_PLUGIN = '''
|
|
import os
|
|
from providers import register_provider
|
|
from providers.base import ProviderProfile
|
|
|
|
class Hosted(ProviderProfile):
|
|
def build_extra_body(self, *, session_id=None, **context):
|
|
return {"pid": os.getpid()}
|
|
|
|
class Local(ProviderProfile):
|
|
def create_client(self, **kwargs):
|
|
return {"pid": os.getpid()}
|
|
|
|
register_provider(Hosted(name="mixhosted", base_url="https://mixhosted.example/v1"))
|
|
register_provider(Local(name="mixlocal", base_url="acp://mixlocal"))
|
|
'''
|
|
|
|
|
|
@pytest.mark.platforms("any") # the host is a child process: its env/home resolution is per-OS
|
|
def test_create_client_profile_loads_in_process_and_its_sibling_stays_hosted(tmp_path, monkeypatch, caplog):
|
|
import providers
|
|
|
|
home = tmp_path / ".hermes"
|
|
plugin_dir = home / "plugins" / "model-providers" / "mixpair"
|
|
plugin_dir.mkdir(parents=True)
|
|
(plugin_dir / "plugin.yaml").write_text("name: mixpair\nkind: model-provider\n", encoding="utf-8")
|
|
(plugin_dir / "__init__.py").write_text(TWO_PROFILE_PLUGIN, encoding="utf-8")
|
|
(home / "config.yaml").write_text(yaml.safe_dump({"plugins": {"isolation": "host"}}), encoding="utf-8")
|
|
monkeypatch.setenv("HOME", str(tmp_path / "os-home"))
|
|
monkeypatch.setenv("HERMES_HOME", str(home))
|
|
|
|
with caplog.at_level(logging.WARNING, logger="providers"):
|
|
hosted = providers.get_provider_profile("mixhosted")
|
|
local = providers.get_provider_profile("mixlocal")
|
|
try:
|
|
assert hosted is not None and local is not None
|
|
assert local.create_client()["pid"] == os.getpid()
|
|
assert hosted.build_extra_body(session_id="s")["pid"] != os.getpid()
|
|
# The in-process import registered ONLY the profile that needs it.
|
|
assert type(hosted).__module__ == "hermes_cli.plugin_host_profiles"
|
|
notices = [r.getMessage() for r in caplog.records if "mixlocal" in r.getMessage()]
|
|
assert len(notices) == 1 and "create_client" in notices[0]
|
|
assert not any("mixhosted" in r.getMessage() for r in caplog.records)
|
|
finally:
|
|
host = getattr(plugins_mod.get_plugin_manager(), "_plugin_host_instance", None)
|
|
if host is not None:
|
|
host.shutdown()
|
|
for name in [m for m in sys.modules if m.endswith("_mixpair")]:
|
|
sys.modules.pop(name, None)
|
|
|
|
|
|
@pytest.mark.platforms("any")
|
|
def test_a_rescan_keeps_the_hosted_plugin_it_already_loaded(tmp_path, monkeypatch, caplog):
|
|
"""Another plugin dir landing while the process runs rescans the home: the plugin already loaded
|
|
is not imported again (no second warning, the same hosted profile object)."""
|
|
import providers
|
|
|
|
home = tmp_path / ".hermes"
|
|
plugin_dir = home / "plugins" / "model-providers" / "mixpair"
|
|
plugin_dir.mkdir(parents=True)
|
|
(plugin_dir / "plugin.yaml").write_text("name: mixpair\nkind: model-provider\n", encoding="utf-8")
|
|
(plugin_dir / "__init__.py").write_text(TWO_PROFILE_PLUGIN, encoding="utf-8")
|
|
(home / "config.yaml").write_text(yaml.safe_dump({"plugins": {"isolation": "host"}}), encoding="utf-8")
|
|
monkeypatch.setenv("HOME", str(tmp_path / "os-home"))
|
|
monkeypatch.setenv("HERMES_HOME", str(home))
|
|
|
|
try:
|
|
with caplog.at_level(logging.WARNING, logger="providers"):
|
|
first = providers.get_provider_profile("mixhosted")
|
|
assert providers.get_provider_profile("mixlocal") is not None
|
|
(home / "plugins" / "model-providers" / "other").mkdir()
|
|
providers.refresh_bound_home()
|
|
again = providers.get_provider_profile("mixhosted")
|
|
assert first is not None and again is first
|
|
assert len([r for r in caplog.records if "mixlocal" in r.getMessage()]) == 1
|
|
finally:
|
|
host = getattr(plugins_mod.get_plugin_manager(), "_plugin_host_instance", None)
|
|
if host is not None:
|
|
host.shutdown()
|
|
for name in [m for m in sys.modules if m.endswith("_mixpair")]:
|
|
sys.modules.pop(name, None)
|