The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(logging): release an out-of-band deleted profile from the log router
kshitijk4poor2d57cd755500
diff --git a/hermes_logging.py b/hermes_logging.pyindex 5cd3454b930..0419ae919e8 100644--- a/hermes_logging.py+++ b/hermes_logging.py@@ -19,7 +19,9 @@ from logging.handlers import QueueHandler, QueueListenerfrom pathlib import Pathfrom typing import Optional, Sequence-from hermes_constants import get_config_path, get_hermes_home, mkdir_under_hermes_home+from hermes_constants import (+ get_config_path, get_hermes_home, mkdir_under_hermes_home, named_profile_is_deleted,+)# setup_logging() is idempotent: a second call is a no-op unless ``force=True``._logging_initialized = False@@ -609,18 +611,17 @@ class _ProfileRoutingFileHandler(logging.Handler):candidate = Path(raw_home).expanduser().resolve()except (TypeError, ValueError, OSError):candidate = self._default_home- if (- candidate != self._default_home- and candidate in self._profile_homes- and not candidate.is_dir()- ):- # The profile was deleted underneath this long-lived process — the- # startup ``_profile_homes`` snapshot is static, so its handler would- # retry (and stderr-spam) the vanished logs/ path on every record- # (#103777). Fall back to the default home; mirrors the cron- # scheduler's ``_existing_profile_homes()`` is_dir() filter.+ if candidate == self._default_home or candidate not in self._profile_homes:+ return self._default_home+ if not candidate.is_dir() or named_profile_is_deleted(candidate):+ # Deleted out of band (CLI ``hermes profile delete`` while this process runs; it+ # tombstones before rmtree). The startup snapshot still names it, so its handler would+ # retry the vanished logs/ path on every record (#103777). Release it the way an+ # in-process delete does: closes the stale fd and drops it from the routing set, so+ # later records for it skip this check and fall back to the default home.+ self.release_profile(candidate)return self._default_home- return candidate if candidate in self._profile_homes else self._default_home+ return candidatedef _handler_for_home(self, home: Path) -> _ManagedRotatingFileHandler:with self._profile_handlers_lock:diff --git a/tests/test_hermes_logging.py b/tests/test_hermes_logging.pyindex 95f7fa08200..7234d1f90ed 100644--- a/tests/test_hermes_logging.py+++ b/tests/test_hermes_logging.py@@ -335,6 +335,48 @@ class TestSetupLogging:# No "--- Logging error ---" FileNotFoundError loop on stderr.assert "FileNotFoundError" not in capsys.readouterr().err+ @pytest.mark.parametrize("removal", ["tombstone", "rmtree"])+ def test_out_of_band_profile_delete_releases_routed_handler(self, hermes_home, capsys, removal):+ """A CLI delete tombstones then removes the home behind a running serve: the router must+ stop writing there at either step and close the stale file, like an in-process delete."""+ import shutil++ from hermes_constants import (+ mark_named_profile_deleted, reset_hermes_home_override, set_hermes_home_override,+ )++ profile_home = (hermes_home / "profiles" / "worker").resolve()+ profile_home.mkdir(parents=True)+ hermes_logging.setup_logging(hermes_home=hermes_home)+ assert hermes_logging.enable_profile_log_routing([hermes_home, profile_home]) is True+ logger = logging.getLogger("cron.scheduler.out-of-band-delete-test")++ def emit(message):+ token = set_hermes_home_override(profile_home)+ try:+ logger.warning(message)+ finally:+ reset_hermes_home_override(token)+ hermes_logging.flush_log_queue()++ emit("before delete")+ routed = [h._profile_handlers[profile_home] for h in hermes_logging._queued_file_handlers+ if isinstance(h, hermes_logging._ProfileRoutingFileHandler)]+ assert routed+ mark_named_profile_deleted(profile_home)+ if removal == "rmtree":+ shutil.rmtree(profile_home)+ capsys.readouterr()+ for i in range(3):+ emit(f"after delete {i}")++ assert "Logging error" not in capsys.readouterr().err+ assert all(h.stream is None or h.stream.closed for h in routed), "stale profile log fd kept open"+ default_log = (hermes_home / "logs" / "agent.log").read_text(encoding="utf-8-sig")+ assert all(f"after delete {i}" in default_log for i in range(3))+ profile_log = profile_home / "logs" / "agent.log"+ assert not profile_log.exists() or "after delete" not in profile_log.read_text(encoding="utf-8-sig")+def test_explicit_params_override_config(self, hermes_home):"""Explicit function params take precedence over config.yaml."""import hermes_yaml as yaml