Coverage for src / quber / playground / tracing.py: 62%
13 statements
« prev ^ index » next coverage.py v7.14.0, created at 2026-09-23 22:14 -0400
« prev ^ index » next coverage.py v7.14.0, created at 2026-09-23 22:14 -0400
1"""The LangSmith tracer the playground's agents share.
3The extraction agents are classes and each holds its own `LangSmithTracer`.
4The playground's agents are module-level functions with no instance to hold
5one, so this module owns the single tracer they all open runs on. Each call
6site wraps its own `Agent.run` the way the extraction agents do: open the run
7with the prompt as messages, set the assistant message and usage on exit.
9Document identity is not an argument the answer functions take, so the route
10that knows it sets `document_key` for the request and each run copies it into
11its metadata. Same no-op contract as the extraction tracer: with
12`TRACE_TO_LANGSMITH` unset nothing here reaches the network.
13"""
15from __future__ import annotations
17from contextvars import ContextVar
18from typing import Any, Dict, Optional
20from quber.agents.langsmith_tracer import LangSmithTracer
22# The document the request in flight is asking about. Set by the route, read
23# by the runs; a contextvar so concurrent requests never see each other's.
24document_key: ContextVar[Optional[str]] = ContextVar("document_key", default=None)
26_tracer: Optional[LangSmithTracer] = None
29def tracer() -> LangSmithTracer:
30 """The process-wide tracer, built on first use so settings are final."""
31 global _tracer
32 if _tracer is None:
33 _tracer = LangSmithTracer(run_name="quber-playground")
34 return _tracer
37def run_metadata() -> Dict[str, Any]:
38 """The document key of the request in flight, or nothing."""
39 doc = document_key.get()
40 return {"doc_key": doc} if doc else {}