"""Where the time of one answer goes: steps and model calls (local log only). Every /answer writes one JSON line to data/logs/answer_timings.jsonl: the total time, the time of the ZP Wiki and CRZP parts and the number and time of model calls. The question itself is stored only as a short hash, so the log holds no user text. ANSWER_TIMING_LOG=0 turns it off. """ from __future__ import annotations import contextvars import hashlib import json import os import threading import time _current: contextvars.ContextVar[dict | None] = contextvars.ContextVar("answer_timing", default=None) _write_lock = threading.Lock() def start(question: str, engine: str) -> contextvars.Token: record = {"q": hashlib.sha256(question.encode()).hexdigest()[:12], "engine": engine, "started": time.monotonic(), "steps": {}, "model_calls": 0, "model_seconds": 0.0, "_lock": threading.Lock()} return _current.set(record) def step(name: str, seconds: float) -> None: record = _current.get() if record is not None: with record["_lock"]: record["steps"][name] = round(record["steps"].get(name, 0.0) + seconds, 2) def model_call(seconds: float, kind: str = "", prompt_chars: int = 0) -> None: record = _current.get() if record is not None: with record["_lock"]: record["model_calls"] += 1 record["model_seconds"] = round(record["model_seconds"] + seconds, 2) record.setdefault("calls", []).append({"s": round(seconds, 2), "kind": kind[:60], "chars": prompt_chars}) def finish(token: contextvars.Token, status: str | None) -> dict | None: record = _current.get() _current.reset(token) if record is None: return None out = {k: v for k, v in record.items() if not k.startswith("_") and k != "started"} out.update(total=round(time.monotonic() - record["started"], 2), status=status, at=time.strftime("%Y-%m-%dT%H:%M:%S")) if os.getenv("ANSWER_TIMING_LOG", "1") != "0": try: from tools.rag.common import DB_FILE path = DB_FILE.parent / "logs" / "answer_timings.jsonl" path.parent.mkdir(parents=True, exist_ok=True) with _write_lock, path.open("a", encoding="utf-8") as handle: handle.write(json.dumps(out, ensure_ascii=False) + "\n") except OSError: pass return out