The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): route multiplexed profile logs to their own logs/ dir
Teknium87ad7aa0b072
diff --git a/gateway/run.py b/gateway/run.pyindex c328ff20e20..912afa6f2cb 100644--- a/gateway/run.py+++ b/gateway/run.py@@ -2479,6 +2479,30 @@ def _multiplex_profile_homes(config: object) -> list[tuple[str, "Path"]]:)+def _enable_multiplex_log_routing(config: object) -> bool:+ """Route agent.log/errors.log/gateway.log records to their owning profile.++ ``setup_logging(mode="gateway")`` binds the queued file handlers to the+ launch home, so under ``multiplex_profiles`` every secondary profile's+ records (emitted inside ``_profile_runtime_scope``) land in the default+ profile's log files (#82936). Swap the static handlers for the+ profile routers from #99440 — the same primitive the Desktop cron ticker+ uses — once the served-profile set is known. Inert for single-profile+ gateways (``enable_profile_log_routing`` is a no-op below two homes).+ """+ if not getattr(config, "multiplex_profiles", False):+ return False+ try:+ from hermes_logging import enable_profile_log_routing++ return enable_profile_log_routing(+ [home for _name, home in _multiplex_profile_homes(config)]+ )+ except Exception:+ logger.debug("could not enable per-profile log routing", exc_info=True)+ return False++def _handoff_watch_scopes(runner: object) -> list:"""``(profile_name, home)`` pairs whose ``state.db`` the watcher must poll.@@ -33627,6 +33651,10 @@ async def start_gateway(config: Optional[GatewayConfig] = None, replace: bool =logging.getLogger().setLevel(_stderr_level)runner = GatewayRunner(config)+ # Multiplex: swap the launch-home file handlers for per-profile routers so+ # each profile's records land in its own logs/ (#82936). Must run after+ # the runner resolved the (possibly None) config and after setup_logging.+ _enable_multiplex_log_routing(runner.config)# ``--replace`` is explicit startup authority, not a durable reconnect# policy. GatewayRunner scopes this bit to cold adapter connects and clears# it before the background reconnect watcher starts.diff --git a/tests/gateway/test_multiplex_log_routing.py b/tests/gateway/test_multiplex_log_routing.pynew file mode 100644index 00000000000..529018b51a9--- /dev/null+++ b/tests/gateway/test_multiplex_log_routing.py@@ -0,0 +1,67 @@+"""Multiplex gateway log routing (#82936, salvage of #84954).++``setup_logging(mode="gateway")`` binds agent.log/errors.log/gateway.log to+the launch home. Under ``multiplex_profiles`` every secondary profile's+records — emitted inside ``_profile_runtime_scope`` — used to fan out into+the DEFAULT profile's files. The gateway now enables the #99440 profile+routers at startup so each record lands in its owner's ``logs/``.+"""++import logging+import types+from pathlib import Path++import pytest++import hermes_logging+from gateway import run+++def clean_logging():+ hermes_logging._reset_queued_handlers()+ hermes_logging._logging_initialized = False+ yield+ hermes_logging._reset_queued_handlers()+ hermes_logging._logging_initialized = False+++def _emit_under(home: Path, name: str, level: int, msg: str) -> None:+ from hermes_constants import reset_hermes_home_override, set_hermes_home_override++ token = set_hermes_home_override(home)+ try:+ logging.getLogger(name).log(level, msg)+ finally:+ reset_hermes_home_override(token)+++def _contains(home: Path, filename: str, needle: str) -> bool:+ path = home / "logs" / filename+ return path.exists() and needle in path.read_text()+++def test_multiplex_gateway_routes_profile_records_to_their_own_logs(+ tmp_path, monkeypatch, clean_logging+):+ default_home = tmp_path / "default"+ beta_home = tmp_path / "default" / "profiles" / "beta"+ beta_home.mkdir(parents=True)+ homes = [("default", default_home), ("beta", beta_home)]+ monkeypatch.setattr(run, "_multiplex_profile_homes", lambda _cfg: homes)++ hermes_logging.setup_logging(hermes_home=default_home, mode="gateway")++ # Single-profile gateway: wiring is inert and handlers stay static.+ assert run._enable_multiplex_log_routing(types.SimpleNamespace(multiplex_profiles=False)) is False+ assert run._enable_multiplex_log_routing(types.SimpleNamespace(multiplex_profiles=True)) is True++ _emit_under(beta_home, "gateway.run", logging.WARNING, "BETA-GATEWAY-WARN")+ _emit_under(default_home, "gateway.run", logging.INFO, "DEFAULT-GATEWAY-INFO")+ hermes_logging.flush_log_queue()++ for filename in ("agent.log", "errors.log", "gateway.log"):+ assert _contains(beta_home, filename, "BETA-GATEWAY-WARN"), filename+ assert not _contains(default_home, filename, "BETA-GATEWAY-WARN"), filename+ assert _contains(default_home, "gateway.log", "DEFAULT-GATEWAY-INFO")+ assert not _contains(beta_home, "gateway.log", "DEFAULT-GATEWAY-INFO")