Files
DeepTutor/deeptutor/logging/process_stream.py
Bingxi Zhao (Frank) df1615778a fix(logs): deliver a retrieval progress event logged off the event loop
A record logged from a worker thread reaches the handler with no running
loop in that thread. `ProcessLogHandler.emit` asked for one, caught the
`RuntimeError`, and returned — dropping the coroutine an async sink had
already created. The reader lost the event and the only trace was
`RuntimeWarning: coroutine 'RAGService._emit_tool_event' was never
awaited` (#1435). Retrieval does its heavy work off-loop, so this is
where its progress lines come from: the turn sat on a generic
"reasoning…" for a minute or two with nothing to say what stage it was
in, which is what the reporter actually felt.

`rag/service.py` already knew the answer — its own raw-log handler falls
back to `run_coroutine_threadsafe` on the loop it captured at setup. That
is the same decision stated twice, and the generic handler, which every
other caller goes through, had the wrong half of it. The rule moves into
`ProcessLogHandler` and the RAG copy becomes one of its users; the
duplicate handler class goes away.

An event with nowhere left to go is closed explicitly rather than
abandoned, so "the capture outlived its loop" no longer surfaces as a
never-awaited warning pointing at a coroutine the reader cannot place.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-13 20:38:42 +08:00

138 lines
4.4 KiB
Python

"""Process-log event capture for user-visible operational logs."""
from __future__ import annotations
import asyncio
from collections.abc import Callable, Iterator
from contextlib import contextmanager
from dataclasses import dataclass, field
import inspect
import logging
from typing import Any
from .context import LOG_CONTEXT_FIELDS
from .formatters import ContextFilter
PROCESS_LOG_PRIVATE_ATTR = "deeptutor_process_log_private"
@dataclass(frozen=True)
class ProcessLogEvent:
type: str = "process_log"
level: str = "INFO"
message: str = ""
logger: str = ""
timestamp: float = 0.0
context: dict[str, Any] = field(default_factory=dict)
@classmethod
def from_record(cls, record: logging.LogRecord) -> "ProcessLogEvent":
context = dict(getattr(record, "log_context", {}) or {})
for key in LOG_CONTEXT_FIELDS:
value = getattr(record, key, None)
if value is not None:
context[key] = value
return cls(
level=record.levelname,
message=record.getMessage(),
logger=record.name,
timestamp=record.created,
context=context,
)
def to_dict(self) -> dict[str, Any]:
return {
"type": self.type,
"level": self.level,
"message": self.message,
"logger": self.logger,
"timestamp": self.timestamp,
"context": self.context,
}
class ProcessLogHandler(logging.Handler):
"""Emit structured process-log events from matching records."""
def __init__(
self,
emit: Callable[[ProcessLogEvent], Any],
*,
task_id: str | None = None,
turn_id: str | None = None,
min_level: int = logging.INFO,
) -> None:
super().__init__(level=min_level)
self._emit = emit
self._task_id = task_id
self._turn_id = turn_id
# The loop that owns this capture, remembered while we are still on
# it. A record logged from a worker thread has no running loop of its
# own, and that is where an async ``emit`` used to be dropped (#1435).
try:
self._loop: asyncio.AbstractEventLoop | None = asyncio.get_running_loop()
except RuntimeError:
self._loop = None
self.addFilter(ContextFilter())
def _schedule(self, coro: Any) -> None:
"""Run an async ``emit`` on the loop that owns this capture.
Retrieval does its heavy work in worker threads, so its log records
arrive off-loop. ``ensure_future`` needs a loop running in *this*
thread; the owning loop is the one that can always take the call.
"""
try:
running = asyncio.get_running_loop()
except RuntimeError:
running = None
if running is not None:
asyncio.ensure_future(coro, loop=running)
return
if self._loop is not None and self._loop.is_running():
asyncio.run_coroutine_threadsafe(coro, self._loop)
return
# Nothing is left to deliver to — the capture outlived its loop.
# Close it explicitly so an undeliverable event does not also
# surface as a "never awaited" warning with no context.
coro.close()
def emit(self, record: logging.LogRecord) -> None:
try:
if getattr(record, PROCESS_LOG_PRIVATE_ATTR, False):
return
event = ProcessLogEvent.from_record(record)
if self._task_id and event.context.get("task_id") != self._task_id:
return
if self._turn_id and event.context.get("turn_id") != self._turn_id:
return
result = self._emit(event)
if inspect.isawaitable(result):
self._schedule(result)
except Exception:
self.handleError(record)
@contextmanager
def capture_process_logs(
emit: Callable[[ProcessLogEvent], Any],
*,
task_id: str | None = None,
turn_id: str | None = None,
min_level: int = logging.INFO,
) -> Iterator[ProcessLogHandler]:
"""Capture matching stdlib log records and emit ``ProcessLogEvent`` objects."""
handler = ProcessLogHandler(
emit,
task_id=task_id,
turn_id=turn_id,
min_level=min_level,
)
root = logging.getLogger()
root.addHandler(handler)
try:
yield handler
finally:
root.removeHandler(handler)
handler.close()