"""Thread-safe log buffer for streaming subprocess output to Gradio.""" from __future__ import annotations import threading from collections import deque class LogBuffer: """Append-only, thread-safe log buffer with a maximum line count.""" def __init__(self, max_lines: int = 5000) -> None: self._lines: deque[str] = deque(maxlen=max_lines) self._lock = threading.Lock() def append(self, line: str) -> None: with self._lock: self._lines.append(line) def get_text(self) -> str: with self._lock: return "\n".join(self._lines) def clear(self) -> None: with self._lock: self._lines.clear() def __len__(self) -> int: with self._lock: return len(self._lines)