Skip to content

The agent that grows with you

test(profiles): prove delete_profile releases routed log handlers end to end

teknium139469496460c
diff --git a/tests/hermes_cli/test_profile_delete_log_handlers.py b/tests/hermes_cli/test_profile_delete_log_handlers.py
new file mode 100644
index 00000000000..69ab7291679
--- /dev/null
+++ b/tests/hermes_cli/test_profile_delete_log_handlers.py
@@ -0,0 +1,92 @@
+"""Profile deletion must release the profile-routed log handlers this process holds (#112538).
+
+A long-lived multiplex process (the Desktop's ``hermes serve`` backend) routes records to every
+served profile's ``logs/*.log`` through ``_ProfileRoutingFileHandler``, which lazily opens one
+rotating handler per home. On Windows that handler is ``ConcurrentRotatingFileHandler`` and keeps
+``logs/.__<name>.lock`` open, so ``delete_profile`` run inside that process fails ``rmtree`` with
+``[WinError 32]``: the REST call returns 500, the Desktop drops the bot, the directory stays.
+The routing seam itself is platform-neutral, so the release is asserted on every OS and the
+directory removal on Windows.
+"""
+
+import logging
+from pathlib import Path
+
+import pytest
+
+import hermes_logging
+from hermes_cli import profiles
+from hermes_constants import reset_hermes_home_override, set_hermes_home_override
+
+
+def routed_profile(tmp_path, monkeypatch):
+ """A default home logging in-process plus a created profile; host services are not touched."""
+ monkeypatch.setattr(Path, "home", lambda: tmp_path)
+ home = tmp_path / ".hermes"
+ home.mkdir()
+ monkeypatch.setenv("HERMES_HOME", str(home))
+ monkeypatch.setattr(profiles, "_cleanup_gateway_service", lambda *_: None)
+ monkeypatch.setattr(profiles, "_maybe_unregister_gateway_service", lambda *_: None)
+ monkeypatch.setattr(profiles, "_stop_profile_backends", lambda *_: None)
+ monkeypatch.setattr(profiles, "_notify_multiplexer", lambda *_: None)
+ profile = profiles.create_profile("routed-log-delete", no_alias=True)
+ hermes_logging.setup_logging(hermes_home=home, force=True)
+ try:
+ yield home, profile
+ finally:
+ # Keep a pre-fix failure from leaking open handles into tempdir cleanup.
+ hermes_logging._reset_queued_handlers()
+ hermes_logging._logging_initialized = False
+
+
+def _log_into(profile: Path, *, adopt: bool) -> None:
+ """Route one record to *profile*: explicitly (Desktop cron startup) or through the
+ ``setup_logging(hermes_home=<profile>)`` adoption path ``agent_init`` takes."""
+ logger = logging.getLogger("agent.tests.profile-delete")
+ token = set_hermes_home_override(profile)
+ try:
+ if adopt:
+ hermes_logging.setup_logging(hermes_home=profile)
+ else:
+ assert hermes_logging.enable_profile_log_routing([Path(profile).parent.parent, profile]) is True
+ logger.error("routed record before deletion")
+ finally:
+ reset_hermes_home_override(token)
+ hermes_logging.flush_log_queue()
+ assert "routed record before deletion" in (profile / "logs" / "agent.log").read_text(encoding="utf-8")
+
+
+def _routers() -> list:
+ return [h for h in hermes_logging._queued_file_handlers
+ if isinstance(h, hermes_logging._ProfileRoutingFileHandler)]
+
+
[email protected]("adopt", [False, True], ids=["explicit-routing", "setup_logging-adoption"])
+def test_delete_profile_releases_every_routed_handler_for_that_home(routed_profile, adopt):
+ _home, profile = routed_profile
+ _log_into(profile, adopt=adopt)
+ routers = _routers()
+ resolved = profile.resolve()
+ held = {Path(r.baseFilename).name: r._profile_handlers[resolved] for r in routers}
+ assert set(held) == {"agent.log", "errors.log"}
+
+ profiles.delete_profile("routed-log-delete", yes=True)
+
+ for router in routers:
+ assert resolved not in router._profile_handlers
+ assert resolved not in router._profile_homes
+ assert all(handler.stream is None for handler in held.values()), "streams still open into the deleted profile"
+ assert not profile.exists()
+
+
+def test_delete_profile_removes_directory_despite_concurrent_log_locks(routed_profile):
+ """The WinError 32 path: ``.__agent.lock`` / ``.__errors.lock`` are held by the routed
+ ConcurrentRotatingFileHandler until released; rmtree must succeed afterwards."""
+ _home, profile = routed_profile
+ _log_into(profile, adopt=True)
+
+ profiles.delete_profile("routed-log-delete", yes=True)
+
+ assert not profile.exists()
diff --git a/tests/hermes_cli/test_profiles.py b/tests/hermes_cli/test_profiles.py
index 7dae31bd5f5..4cfa4e11b91 100644
--- a/tests/hermes_cli/test_profiles.py
+++ b/tests/hermes_cli/test_profiles.py
@@ -374,25 +374,6 @@ class TestBackfillProfileEnvs:
class TestDeleteProfile:
"""Tests for delete_profile()."""
- def test_releases_routed_log_handlers_before_removing_profile(self, profile_env, monkeypatch):
- """Deleting a Desktop profile releases this process's profile-scoped log handlers."""
- profile_dir = create_profile("coder", no_alias=True)
- released: list[Path] = []
-
- monkeypatch.setattr(profiles, "_cleanup_gateway_service", lambda *_: None)
- monkeypatch.setattr(profiles, "_maybe_unregister_gateway_service", lambda *_: None)
- monkeypatch.setattr(profiles, "_stop_profile_backends", lambda *_: None)
- monkeypatch.setattr(profiles, "_notify_multiplexer", lambda *_: None)
- monkeypatch.setattr(
- "hermes_logging.release_profile_log_handlers",
- lambda home: released.append(home) or 2,
- )
-
- delete_profile("coder", yes=True)
-
- assert released == [profile_dir]
- assert not profile_dir.exists()
-
def test_rmtree_failure_raises(self, profile_env):
profile_dir = create_profile("coder", no_alias=True)