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

1"""The LangSmith tracer the playground's agents share. 

2 

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. 

8 

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""" 

14 

15from __future__ import annotations 

16 

17from contextvars import ContextVar 

18from typing import Any, Dict, Optional 

19 

20from quber.agents.langsmith_tracer import LangSmithTracer 

21 

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) 

25 

26_tracer: Optional[LangSmithTracer] = None 

27 

28 

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 

35 

36 

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 {}