Files
DeepTutor/deeptutor/services/memory/trace.py
Antonio-Faure 63ef9b7c2c chore: remove dead Python modules and helpers (#2253)
* chore: remove dead Python modules and helpers

Delete unreferenced P0 code identified by the dead-code audit:
- services/llm/registry.py and its test-only coverage
- logging/adapters package (LlamaIndex log forwarding)
- textbook_struct.chapter_rebuild.detect_frames
- load_config_with_main_async and its async file helper
- memory.trace.iter_by_ids / latest_ts
- TokenTracker.format_summary
- WeixinChannel._api_get
- ArxivSearchTool.extract_arxiv_id_from_url
- FileLibraryStore migration scaffolding (_run_migrations et al.)
- eight small dead helpers in services, api/utils, and knowledge
- redundant runtime C2CMessage import in the QQ channel

* chore: drop the prompt-language shim and a dead storage helper

The book agents were the last in-tree users of blocks/_language.py, which
only re-exported services.prompt.language. Import the real module and
delete the shim. storage._storage_path_from_version_entry had no caller.

Verified: ruff F401/F811/F841/F821 on changed files; py_compile.

---------

Co-authored-by: Antonio-Faure <209759495+Antonio-Faure@users.noreply.github.com>
2026-12-02 10:50:16 +08:00

117 lines
3.5 KiB
Python

"""L1 raw event trace: append-only JSONL files, one per surface per UTC day.
Trace capture must never break the producing surface — every append is
wrapped and failures are logged-and-swallowed. Writes are serialized
per-surface with an asyncio lock so multiple turns in the same process
don't interleave JSON lines.
"""
from __future__ import annotations
import asyncio
from dataclasses import asdict, dataclass
from datetime import datetime, timezone
import json
import logging
from pathlib import Path
from typing import Any, Iterator
from deeptutor.services.memory.ids import new_trace_id
from deeptutor.services.memory.paths import Surface, trace_dir, trace_file
logger = logging.getLogger(__name__)
_locks: dict[str, asyncio.Lock] = {}
def _lock_for(surface: Surface) -> asyncio.Lock:
lock = _locks.get(surface)
if lock is None:
lock = asyncio.Lock()
_locks[surface] = lock
return lock
@dataclass
class TraceEvent:
id: str
ts: str
surface: Surface
kind: str
payload: dict[str, Any]
session_id: str | None = None
turn_id: str | None = None
@classmethod
def new(
cls,
surface: Surface,
kind: str,
payload: dict[str, Any],
*,
session_id: str | None = None,
turn_id: str | None = None,
) -> "TraceEvent":
return cls(
id=new_trace_id(surface),
ts=datetime.now(tz=timezone.utc).isoformat(),
surface=surface,
kind=kind,
payload=payload,
session_id=session_id,
turn_id=turn_id,
)
async def append(event: TraceEvent) -> None:
"""Append one event to today's surface trace file. Never raises."""
try:
path = trace_file(event.surface, datetime.now(tz=timezone.utc).date())
line = json.dumps(asdict(event), ensure_ascii=False, separators=(",", ":"))
async with _lock_for(event.surface):
await asyncio.to_thread(_append_line, path, line)
except Exception:
logger.warning(
"memory trace append failed surface=%s kind=%s",
event.surface,
event.kind,
exc_info=True,
)
def _append_line(path: Path, line: str) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
with path.open("a", encoding="utf-8") as fh:
fh.write(line)
fh.write("\n")
def iter_since(surface: Surface, since: datetime | None = None) -> Iterator[TraceEvent]:
"""Yield events for ``surface`` in chronological order, optionally
filtering to events with ``ts >= since`` (UTC)."""
files = sorted(trace_dir(surface).glob("*.jsonl"))
cutoff_iso = since.isoformat() if since else ""
cutoff_date_iso = since.date().isoformat() if since else ""
for path in files:
if cutoff_date_iso and path.stem < cutoff_date_iso:
continue
try:
with path.open("r", encoding="utf-8") as fh:
for raw in fh:
raw = raw.strip()
if not raw:
continue
try:
obj = json.loads(raw)
except json.JSONDecodeError:
continue
if cutoff_iso and obj.get("ts", "") < cutoff_iso:
continue
yield TraceEvent(**obj)
except OSError:
continue
def count_since(surface: Surface, since: datetime | None = None) -> int:
return sum(1 for _ in iter_since(surface, since))