diff --git a/nanobot/utils/rotating_output.py b/nanobot/utils/rotating_output.py index b45c069d4..be732c258 100644 --- a/nanobot/utils/rotating_output.py +++ b/nanobot/utils/rotating_output.py @@ -20,7 +20,7 @@ DEFAULT_BACKUP_COUNT = 5 class RotatingTextOutput(io.TextIOBase): - """A UTF-8 text stream that rotates a file before it exceeds its size cap.""" + """A UTF-8 text stream that keeps each retained file within its size cap.""" def __init__(self, path: Path, *, max_bytes: int, backup_count: int) -> None: super().__init__() @@ -57,15 +57,54 @@ class RotatingTextOutput(io.TextIOBase): data = text.encode(self.encoding, errors=self.errors) with self._lock: self._ensure_open() - if self._size and self._size + len(data) > self._max_bytes: + if len(data) <= self._max_bytes: + if self._size and self._size + len(data) > self._max_bytes: + self._rotate() + self._ensure_open() + assert self._handle is not None + self._handle.write(data) + self._handle.flush() + self._size += len(data) + return len(text) + + if self._size: self._rotate() + offset = 0 + while offset < len(data): self._ensure_open() - assert self._handle is not None - self._handle.write(data) - self._handle.flush() - self._size += len(data) + capacity = self._max_bytes - self._size + if capacity <= 0: + self._rotate() + continue + + chunk_size = self._utf8_chunk_size(data[offset:], capacity) + if chunk_size == 0 and self._size: + self._rotate() + continue + # A configured cap smaller than one UTF-8 code point cannot + # satisfy both constraints. Keep the byte cap in that degenerate + # case; normal caps split only at code-point boundaries. + chunk_size = chunk_size or min(capacity, len(data) - offset) + chunk = data[offset : offset + chunk_size] + assert self._handle is not None + self._handle.write(chunk) + self._handle.flush() + self._size += chunk_size + offset += chunk_size + if offset < len(data): + self._rotate() return len(text) + @staticmethod + def _utf8_chunk_size(data: bytes, capacity: int) -> int: + """Return the largest prefix within capacity that ends on a UTF-8 boundary.""" + end = min(len(data), capacity) + if end == len(data): + return end + while end > 0 and data[end] & 0xC0 == 0x80: + end -= 1 + return end + def flush(self) -> None: with self._lock: if self._handle is not None: diff --git a/tests/utils/test_rotating_output.py b/tests/utils/test_rotating_output.py index edbab7812..2a0f203c9 100644 --- a/tests/utils/test_rotating_output.py +++ b/tests/utils/test_rotating_output.py @@ -38,6 +38,21 @@ def test_rotating_output_rotates_oversized_existing_file_before_append(tmp_path: assert (tmp_path / "gateway.log.1").read_text(encoding="utf-8") == "existing output" +def test_rotating_output_splits_one_oversized_write_at_utf8_boundaries( + tmp_path: Path, +) -> None: + log_path = tmp_path / "gateway.log" + output = RotatingTextOutput(log_path, max_bytes=8, backup_count=2) + text = "A世界BC🙂DEF界G" + + assert output.write(text) == len(text) + output.close() + + paths = [tmp_path / "gateway.log.2", tmp_path / "gateway.log.1", log_path] + assert all(path.stat().st_size <= 8 for path in paths) + assert "".join(path.read_text(encoding="utf-8") for path in paths) == text + + def test_rotating_output_falls_back_when_active_log_cannot_be_renamed( tmp_path: Path, monkeypatch,