Skip to content

The agent that grows with you

fix(i18n): a freshly started gateway serves installed language packs

kshitijk4poor7af86aa2ddfa
diff --git a/tests/tui_gateway/test_i18n_methods.py b/tests/tui_gateway/test_i18n_methods.py
index 45d181fe12d..3b22c013e87 100644
--- a/tests/tui_gateway/test_i18n_methods.py
+++ b/tests/tui_gateway/test_i18n_methods.py
@@ -75,3 +75,94 @@ def test_catalog_answers_for_the_requested_profile_home(tmp_path, monkeypatch, c
assert _call(server, "i18n.catalog", {"lang": "de", "profile": "named"})["result"]["messages"] == {"approval.denied": "Nur named"}
assert _call(server, "i18n.catalog", {"lang": "de"})["result"]["messages"] == {}
+
+
+def test_cold_gateway_serves_an_installed_pack_before_any_agent_exists(tmp_path, monkeypatch, clean_layers):
+ """The Desktop asks for ``i18n.languages`` / ``i18n.catalog`` right after connecting, before anything
+ in the gateway has discovered plugins. An installed, enabled pack must already be in both answers."""
+ from tui_gateway import server
+ from hermes_cli import plugins as plugins_mod
+
+ home = tmp_path / "home"
+ plugin = home / "plugins" / "hermes-lang-ko"
+ (plugin / "locales").mkdir(parents=True)
+ empty_bundled = tmp_path / "bundled"
+ empty_bundled.mkdir()
+ (plugin / "plugin.yaml").write_text(yaml.safe_dump({
+ "name": "hermes-lang-ko", "version": "1.0.0", "description": "Korean pack",
+ "provides_locales": [{"id": "ko", "endonym": "한국어"}]}, allow_unicode=True), encoding="utf-8")
+ (plugin / "locales" / "ko.yaml").write_text("{}\n", encoding="utf-8")
+ (plugin / "locales" / "ko.desktop.yaml").write_text(
+ yaml.safe_dump({"settings": {"appearance": {"modelPricingTitle": "모델 가격"}}}, allow_unicode=True),
+ encoding="utf-8")
+ (home / "config.yaml").write_text(yaml.safe_dump({"plugins": {"enabled": ["hermes-lang-ko"]}}), encoding="utf-8")
+ monkeypatch.setenv("HOME", str(tmp_path / "os-home"))
+ monkeypatch.setenv("HERMES_HOME", str(home))
+ monkeypatch.setattr(server, "_hermes_home", home)
+ monkeypatch.setattr(plugins_mod, "get_bundled_plugins_dir", lambda: empty_bundled)
+ i18n.reset_language_cache()
+ manager = plugins_mod.get_plugin_manager()
+ assert "ko" not in i18n_layers.pack_languages() # nothing has loaded plugins in this home yet
+ try:
+ ko = next(row for row in _call(server, "i18n.languages", {})["result"]["languages"] if row["id"] == "ko")
+ assert ko["source"] == "plugin:hermes-lang-ko"
+ desktop = _call(server, "i18n.catalog", {"lang": "ko", "surface": "desktop"})["result"]
+ assert desktop["messages"] == {"settings.appearance.modelPricingTitle": "모델 가격"}
+ finally:
+ manager.unload()
+
+
+def test_failed_plugin_discovery_still_answers_with_bundled_languages(tmp_path, monkeypatch, clean_layers):
+ from tui_gateway import server
+ from hermes_cli import plugins as plugins_mod
+
+ def boom(force=False):
+ raise RuntimeError("plugin loader broke")
+
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path))
+ monkeypatch.setattr(server, "_hermes_home", tmp_path)
+ monkeypatch.setattr(plugins_mod, "discover_plugins", boom)
+ i18n.reset_language_cache()
+
+ languages = _call(server, "i18n.languages", {})["result"]["languages"]
+ assert {"id": "ko", "endonym": "한국어", "rtl": False, "source": "bundled"} in languages
+ assert _call(server, "i18n.catalog", {"lang": "ko", "surface": "desktop"})["result"]["messages"] == {}
+
+
[email protected]("method", ["i18n.languages", "i18n.catalog"])
+def test_slow_plugin_discovery_does_not_hold_up_other_requests(method, tmp_path, monkeypatch, clean_layers):
+ """Cold discovery loads every enabled plugin, so it runs on the RPC pool: the reader keeps serving."""
+ import queue
+ import threading
+
+ from hermes_cli import plugins as plugins_mod
+ from tui_gateway import server
+
+ class Transport:
+ def __init__(self):
+ self.frames = queue.Queue()
+
+ def write(self, frame):
+ self.frames.put(frame)
+ return True
+
+ entered, release = threading.Event(), threading.Event()
+
+ def slow_discovery(force=False):
+ entered.set()
+ assert release.wait(10)
+
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path))
+ monkeypatch.setattr(server, "_hermes_home", tmp_path)
+ monkeypatch.setattr(plugins_mod, "discover_plugins", slow_discovery)
+ transport = Transport()
+ try:
+ params = {"lang": "ko", "surface": "desktop"} if method == "i18n.catalog" else {}
+ assert server.dispatch({"jsonrpc": "2.0", "id": "slow", "method": method, "params": params}, transport) is None
+ assert entered.wait(10)
+ pong = server.dispatch({"jsonrpc": "2.0", "id": "ping", "method": "ping", "params": {}}, transport)
+ assert pong["id"] == "ping" and "result" in pong
+ finally:
+ release.set()
+ reply = transport.frames.get(timeout=10)
+ assert reply["id"] == "slow" and "result" in reply
diff --git a/tui_gateway/methods_i18n.py b/tui_gateway/methods_i18n.py
index bec0a7b60c2..1b23e29554c 100644
--- a/tui_gateway/methods_i18n.py
+++ b/tui_gateway/methods_i18n.py
@@ -10,6 +10,8 @@ Bodies are rebound onto server.py's globals (method_ctx.bind_module) and referen
from __future__ import annotations
+import logging
+
from .method_ctx import HandlerRegistry, bind_module
_registry = HandlerRegistry()
@@ -19,11 +21,24 @@ _profile_scoped = _registry.profile_scoped
_I18N_ERR = 5400
+def _load_language_packs() -> None:
+ """Plugin packs register during plugin discovery, which nothing in this process may have run yet: the
+ Desktop asks for its catalog as soon as it connects, before any agent exists. Idempotent. A failed
+ discovery still answers with the bundled and overlay layers."""
+ from hermes_cli.plugins import discover_plugins
+ try:
+ discover_plugins()
+ except Exception:
+ logging.getLogger(__name__).warning("i18n: plugin discovery failed; serving languages without plugin packs",
+ exc_info=True)
+
+
@method("i18n.languages")
@_profile_scoped
def _(rid, params: dict) -> dict:
from agent.i18n import language_options
try:
+ _load_language_packs()
return _ok(rid, {"languages": language_options()})
except Exception as e:
return _err(rid, _I18N_ERR, str(e))
@@ -37,8 +52,9 @@ def _(rid, params: dict) -> dict:
surface = str(params.get("surface") or CORE_SURFACE)
if surface not in SURFACES:
return _err(rid, 4002, f"surface must be one of {', '.join(SURFACES)}")
- lang = resolve_language_id(params.get("lang", "")) or "en"
try:
+ _load_language_packs()
+ lang = resolve_language_id(params.get("lang", "")) or "en"
return _ok(rid, {"lang": lang, "surface": surface, "messages": surface_catalog(lang, surface)})
except Exception as e:
return _err(rid, _I18N_ERR, str(e))
@@ -46,3 +62,5 @@ def _(rid, params: dict) -> dict:
def register(server) -> None:
bind_module(globals(), server, skip=("_",))
+ # A cold call runs plugin discovery, which loads every enabled plugin: keep it off the request reader.
+ server._LONG_HANDLERS = server._LONG_HANDLERS | _registry.names()