fix(plugins): tell a plugin its profile from the host it loads into

`PluginHost.load` computed the plugin's `ctx.profile_name` from the base
context before `ensure_started`, which is where a start now takes the
manager's current home. After a linked profile is renamed and the host
crashes, the first plugin the restart reloads was told the old profile
name while the new host, and every later load, ran under the directory.

Compute it after the host has started, in the context of that host.
This commit is contained in:
BearHuddleston
2026-11-24 03:31:32 +00:00
committed by Bear Huddleston
parent 9d41c3df90
commit 02bf2f6a48
2 changed files with 8 additions and 3 deletions

View File

@@ -250,7 +250,6 @@ class PluginHost:
"plugin_key": plugin_key, "load": self._loads[plugin_key], "plugin_id": ctx.plugin_id,
"name": manifest.name,
"path": manifest.path, "module_name": module_name, "entrypoint": entrypoint,
"profile_name": self._base_context.copy().run(lambda: ctx.profile_name),
"manifest": {k: getattr(manifest, k, None) for k in (
"name", "version", "description", "author", "source", "path", "key", "kind",
"data_namespace")},
@@ -258,6 +257,8 @@ class PluginHost:
}
try:
channel = self.ensure_started()
# After the start: a restart can move the host to another home (a renamed link's old name).
params["profile_name"] = self._base_context.copy().run(lambda: ctx.profile_name)
self._loading = plugin_key
result = channel.call("load", params)
except BaseException:

View File

@@ -443,9 +443,11 @@ def test_a_linked_profile_renamed_while_its_manager_is_kept_gets_its_host_plugin
tmp_path, monkeypatch):
"""A process keeps one manager per directory, so a linked profile renamed while it runs keeps the manager
it was opened by under the old name. The host restarted after a crash must find the profile, not the
link that is gone."""
link that is gone, and a reloaded plugin is told the profile of the home that host runs under."""
import signal
from hermes_cli.plugins_loader import _plugin_home_scope
from hermes_cli.profiles import get_active_profile_name
from tests.waiting import wait_until
from tools.registry import registry
@@ -453,7 +455,7 @@ def test_a_linked_profile_renamed_while_its_manager_is_kept_gets_its_host_plugin
"import json, os\ndef register(ctx):\n"
" ctx.register_tool(name='whereprobe_pid', toolset='whereprobe', schema={'name': 'whereprobe_pid',\n"
" 'description': 'd', 'parameters': {'type': 'object', 'properties': {}}},\n"
" handler=lambda args, **kw: json.dumps({'pid': os.getpid()}))\n")})
" handler=lambda args, **kw: json.dumps({'pid': os.getpid(), 'profile': ctx.profile_name}))\n")})
profiles = tmp_path / "root" / "profiles"
profiles.mkdir(parents=True)
(profiles / "ops").symlink_to(target, target_is_directory=True)
@@ -471,6 +473,8 @@ def test_a_linked_profile_renamed_while_its_manager_is_kept_gets_its_host_plugin
registry.dispatch("whereprobe_pid", {}, scope=manager.scope_key)), timeout=15,
what="the restarted host to serve the plugin's tool")
assert json.loads(result)["pid"] not in {first, os.getpid()}
with _plugin_home_scope(manager.owner_home):
assert json.loads(result)["profile"] == get_active_profile_name()
finally:
manager.unload()
manager._plugin_host().shutdown()