The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(logging): flag a skipped write instead of reading it from a closed stream
kshitijk4poor17553bd6f8c7
diff --git a/hermes_logging.py b/hermes_logging.pyindex 7e2995d9f9f..1bce39c95fc 100644--- a/hermes_logging.py+++ b/hermes_logging.py@@ -450,6 +450,7 @@ class _ManagedRotatingFileHandler(RotatingFileHandler):# Set by _ProfileRoutingFileHandler: a vanished logs/ dir means the profile was deleted,# so skip the write (the router re-routes the record) instead of a FileNotFoundError traceback.self._skip_write_when_dir_missing = False+ self.skipped_missing_dir = Falsesuper().__init__(*args, **kwargs)self._record_stream_stat()@@ -503,12 +504,18 @@ class _ManagedRotatingFileHandler(RotatingFileHandler):self._reopen_stream(st)def emit(self, record: logging.LogRecord) -> None:+ self.skipped_missing_dir = False+ had_stream = self.stream is not None# The kernel caches inode metadata, so this stat is sub-microsecond on a hot file.- if self.stream is not None or os.path.exists(self.baseFilename):+ file_exists = had_stream or os.path.exists(self.baseFilename)+ if file_exists:self._reopen_if_externally_rotated()- # Only reached with no stream (reopen failed), so a healthy emit pays no extra stat.- if (self.stream is None and self._skip_write_when_dir_missing+ # Only when the file is gone (or its reopen just failed), so a healthy emit pays no extra+ # stat. ``stream is None`` alone is not a signal: on Windows concurrent-log-handler closes+ # the stream after every write.+ if (self.stream is None and (had_stream or not file_exists) and self._skip_write_when_dir_missingand not os.path.isdir(os.path.dirname(self.baseFilename))):+ self.skipped_missing_dir = Truereturnsuper().emit(record)# A record actually reached the file: only now has the destination recovered. Resetting@@ -681,9 +688,9 @@ class _ProfileRoutingFileHandler(logging.Handler):handler.handle(record)finally:reset_hermes_home_override(token)- # The write lost its stream (an rmtree inside the liveness interval skips it, see- # _ManagedRotatingFileHandler.emit): re-check now so this record is not dropped.- if handler.stream is None and self._release_if_deleted(home):+ # The write was skipped because logs/ is gone (an rmtree inside the liveness interval,+ # see _ManagedRotatingFileHandler.emit): re-check now so this record is not dropped.+ if handler.skipped_missing_dir and self._release_if_deleted(home):self._handler_for_home(self._default_home).handle(record)except Exception:self.handleError(record)diff --git a/tests/test_hermes_logging.py b/tests/test_hermes_logging.pyindex 4b675bc4038..45e384b28ff 100644--- a/tests/test_hermes_logging.py+++ b/tests/test_hermes_logging.py@@ -71,10 +71,10 @@ def liveness_clock(monkeypatch):"""Drive the router's liveness-check clock so tests can cross the recheck intervalwithout sleeping. Returns ``advance()``, which jumps past the interval."""now = [1000.0]- monkeypatch.setattr(hermes_logging, "_monotonic", lambda: now[0], raising=False)+ monkeypatch.setattr(hermes_logging, "_monotonic", lambda: now[0])def advance():- now[0] += getattr(hermes_logging, "_PROFILE_LIVENESS_RECHECK_S", 2.0)+ now[0] += hermes_logging._PROFILE_LIVENESS_RECHECK_Sreturn advance@@ -395,10 +395,28 @@ class TestSetupLogging: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_live_profile_liveness_checked_at_most_once_per_interval(self, hermes_home, liveness_clock):- """A live routed profile must not pay the delete check (two stats) on every record."""+ @pytest.mark.parametrize("stream_closed_after_write", [False, True], ids=["kept-open", "closed-per-write"])+ def test_live_profile_liveness_checked_at_most_once_per_interval(+ self, hermes_home, liveness_clock, monkeypatch, stream_closed_after_write+ ):+ """A live routed profile must not pay the delete check (two stats) on every record.++ ``closed-per-write`` is how concurrent-log-handler behaves on Windows: the stream is+ closed after every write, which must not read as a skipped write.+ """from hermes_constants import reset_hermes_home_override, set_hermes_home_override+ if stream_closed_after_write:+ real_emit = hermes_logging._ManagedRotatingFileHandler.emit++ def emit_then_close(handler, record):+ real_emit(handler, record)+ if handler.stream is not None:+ handler.stream.close()+ handler.stream = None++ monkeypatch.setattr(hermes_logging._ManagedRotatingFileHandler, "emit", emit_then_close)+profile_home = (hermes_home / "profiles" / "worker").resolve()profile_home.mkdir(parents=True)hermes_logging.setup_logging(hermes_home=hermes_home)