The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(langfuse): drop orphaned _capture/_trace modules (never imported; superseded by __init__)
Tekniumd9beaff938af
diff --git a/plugins/observability/langfuse/_capture.py b/plugins/observability/langfuse/_capture.pydeleted file mode 100644index 954b0e2d5a4..00000000000--- a/plugins/observability/langfuse/_capture.py+++ /dev/null@@ -1,461 +0,0 @@-"""Content capture, message serialization and usage/cost translation for the Langfuse plugin.--Everything here is pure data shaping; nothing touches the SDK client or trace state.-"""-from __future__ import annotations--import json-import logging-import os-import re-from typing import Any, Dict, Optional--# Same logger as the package so log records keep the plugin's name.-logger = logging.getLogger(__name__.rpartition(".")[0])--_READ_FILE_LINE_RE = re.compile(r"^\s*(\d+)\|(.*)$")-_READ_FILE_HEAD_LINES = 25-_READ_FILE_TAIL_LINES = 15--# (langfuse usage key, CanonicalUsage attribute / summary-dict key, PricingEntry attribute)-_USAGE_FIELDS = (- ("input", "input_tokens", "input_cost_per_million"),- ("output", "output_tokens", "output_cost_per_million"),- ("cache_read_input_tokens", "cache_read_tokens", "cache_read_cost_per_million"),- ("cache_creation_input_tokens", "cache_write_tokens", "cache_write_cost_per_million"),- ("reasoning_tokens", "reasoning_tokens", None),-)--_CAPTURE_MODES = ("metadata", "sanitized", "full")-_DEFAULT_CAPTURE_MODE = "sanitized"-_warned_invalid_capture = False---def _env(name: str, default: str = "") -> str:- return os.environ.get(name, default).strip()---def _debug(message: str) -> None:- if _env("HERMES_LANGFUSE_DEBUG").lower() in {"1", "true", "yes", "on"}:- logger.info("Langfuse tracing: %s", message)---# ----------------------------------------------------------------------------# Capture modes-# -----------------------------------------------------------------------------def _capture_mode() -> str:- """Resolve ``metadata | sanitized | full``.-- Read per call so tests and long-lived processes can flip modes without a- client reset. Invalid values warn once and fall back to the default rather- than silently capturing more than the operator intended.- """- global _warned_invalid_capture- value = _env("HERMES_LANGFUSE_CAPTURE").lower()- if not value:- return _DEFAULT_CAPTURE_MODE- if value in _CAPTURE_MODES:- return value- if not _warned_invalid_capture:- _warned_invalid_capture = True- logger.warning(- "Langfuse plugin: invalid HERMES_LANGFUSE_CAPTURE=%r, falling back "- "to %r (valid: %s)",- value, _DEFAULT_CAPTURE_MODE, ", ".join(_CAPTURE_MODES),- )- return _DEFAULT_CAPTURE_MODE---def _redact_secrets(value: str) -> str:- # force=True: redact even if the user disabled security.redact_secrets —- # this content is exported to an external service.- try:- from agent.redact import redact_sensitive_text- return redact_sensitive_text(value, force=True)- except Exception:- return value---def _describe_content(value: Any) -> Any:- """Metadata-mode stand-in for content: shape and size, never payload."""- if value is None or isinstance(value, bool):- return value- if isinstance(value, (int, float)):- return {"omitted": True, "type": "number"}- if isinstance(value, bytes):- return {"omitted": True, "type": "bytes", "length": len(value)}- if isinstance(value, str):- return {"omitted": True, "type": "text", "chars": len(value)}- if isinstance(value, dict):- return {"omitted": True, "type": "object", "keys": [str(k) for k in list(value.keys())[:20]]}- if isinstance(value, (list, tuple, set)):- return {"omitted": True, "type": "array", "items": len(value)}- return {"omitted": True, "type": type(value).__name__}---def _capture_content(value: Any, *, parse_json_strings: bool = False) -> Any:- """Apply the active capture mode to a CONTENT value.-- Only prompt/response text, tool arguments and tool results are content;- metadata fields (provider, model, IDs, counts) stay as-is in every mode.- """- if _capture_mode() == "metadata":- return _describe_content(value)- return _safe_value(value, parse_json_strings=parse_json_strings)---def _capture_tool_result(result: Any, *, tool_name: str, args: Any) -> Any:- """Capture a tool result: JSON strings are parsed first so a read_file- payload can be collapsed to a preview keyed by the call's ``args``."""- if _capture_mode() == "metadata":- return _describe_content(result)- value = _maybe_parse_json_string(result) if isinstance(result, str) else result- return _safe_value(_normalize_payload(value, tool_name=tool_name, args=args), parse_json_strings=True)---def _redact_data_uri(value: str) -> dict[str, Any]:- header = value.split(",", 1)[0] if "," in value else "data:"- media_type = header[5:].split(";", 1)[0] if header.startswith("data:") else ""- return {"type": "data_uri", "media_type": media_type or None, "omitted": True, "length": len(value)}---def _truncate_text(value: str, max_chars: int) -> Any:- # The SDK decodes data:*;base64 strings as media; a truncated one is- # invalid base64 and logs noisily, so redact the whole URI instead.- prefix = value[:200].lower()- if prefix.startswith("data:") and ";base64," in prefix:- return _redact_data_uri(value)- # Redact BEFORE truncating so a secret straddling the cut cannot leak.- if _capture_mode() == "sanitized":- value = _redact_secrets(value)- if len(value) <= max_chars:- return value- return value[:max_chars] + f"... [truncated {len(value) - max_chars} chars]"---def _maybe_parse_json_string(value: str) -> Any:- stripped = value.strip()- if len(stripped) < 2 or stripped[0] not in "{[":- return value- try:- parsed, idx = json.JSONDecoder().raw_decode(stripped)- except Exception:- return value- if not isinstance(parsed, (dict, list)):- return value-- trailing = stripped[idx:].strip()- if not trailing:- return parsed-- hint_key = "_hint" if trailing.startswith("[Hint:") else "_trailing_text"- if isinstance(parsed, dict):- merged = dict(parsed)- merged[hint_key if hint_key not in merged else "_trailing_text"] = trailing- return merged- return {"data": parsed, hint_key: trailing}---def _parse_read_file_lines(content: str) -> list[dict[str, Any]]:- if not isinstance(content, str) or not content:- return []- lines = []- for raw_line in content.splitlines():- match = _READ_FILE_LINE_RE.match(raw_line)- if not match:- return []- lines.append({"line": int(match.group(1)), "text": match.group(2)})- return lines---def _normalize_read_file_payload(value: dict[str, Any], *, args: Any = None) -> dict[str, Any]:- normalized: dict[str, Any] = {}- if isinstance(args, dict):- path = args.get("path")- if isinstance(path, str) and path:- normalized["path"] = path- for key in ("offset", "limit"):- if isinstance(args.get(key), int):- normalized[key] = args[key]-- lines = _parse_read_file_lines(value.get("content", ""))- if lines:- normalized["returned_lines"] = {"start": lines[0]["line"], "end": lines[-1]["line"], "count": len(lines)}- head, tail = _READ_FILE_HEAD_LINES, _READ_FILE_TAIL_LINES- if len(lines) <= head + tail:- normalized["content_preview"] = {"lines": lines}- else:- normalized["content_preview"] = {- "head": lines[:head],- "tail": lines[-tail:],- "omitted_line_count": len(lines) - head - tail,- }- elif value.get("content"):- normalized["content_preview"] = {"text": value.get("content", "")}-- for key in ("total_lines", "file_size", "truncated", "is_binary", "is_image", "hint",- "_warning", "mime_type", "dimensions", "similar_files", "error"):- if key in value:- normalized[key] = value[key]-- base64_content = value.get("base64_content")- if isinstance(base64_content, str) and base64_content:- normalized["base64_content"] = {"omitted": True, "length": len(base64_content)}- return normalized---def _normalize_payload(value: Any, *, tool_name: str = "", args: Any = None) -> Any:- """Collapse a read_file result (line-numbered content + file metadata) into a compact preview."""- is_read_file = (- isinstance(value, dict)- and isinstance(value.get("content"), str)- and all(k in value for k in ("total_lines", "file_size", "is_binary", "is_image"))- and not value.get("error")- )- if is_read_file:- return _normalize_read_file_payload(value, args=args if tool_name == "read_file" else None)- return value---def _safe_value(value: Any, *, max_chars: Optional[int] = None, depth: int = 0,- parse_json_strings: bool = False) -> Any:- max_chars = max_chars if max_chars is not None else int(_env("HERMES_LANGFUSE_MAX_CHARS", "12000") or "12000")- if depth > 4:- return "<max-depth>"- if value is None or isinstance(value, (int, float, bool)):- return value- if isinstance(value, bytes):- return {"type": "bytes", "len": len(value)}- recurse = lambda v, d: _safe_value(v, max_chars=max_chars, depth=d, parse_json_strings=parse_json_strings) # noqa: E731- if isinstance(value, str):- if parse_json_strings:- parsed = _maybe_parse_json_string(value)- if parsed is not value:- return recurse(parsed, depth)- return _truncate_text(value, max_chars)- if isinstance(value, dict):- normalized = _normalize_payload(value)- if normalized is not value:- return recurse(normalized, depth)- return {str(k): recurse(v, depth + 1) for k, v in list(value.items())[:50]}- if isinstance(value, (list, tuple, set)):- return [recurse(v, depth + 1) for v in list(value)[:50]]- if hasattr(value, "__dict__"):- return recurse(vars(value), depth + 1)- return _truncate_text(repr(value), max_chars)---# ----------------------------------------------------------------------------# Message serialization-# -----------------------------------------------------------------------------def _extract_last_user_message(messages: Any) -> Any:- if not isinstance(messages, list):- return None- for message in reversed(messages):- if isinstance(message, dict) and message.get("role") == "user":- return {"role": "user", "content": _capture_content(message.get("content"))}- return None---def _coerce_request_messages(*, request_messages: Any = None, messages: Any = None,- conversation_history: Any = None, user_message: Any = None) -> list[dict[str, Any]]:- for candidate in (request_messages, messages, conversation_history):- if isinstance(candidate, list):- return candidate- if user_message is None:- return []- return [{"role": "user", "content": user_message}]---def _serialize_system_prompt(system_prompt: Any) -> Optional[dict[str, Any]]:- """Normalize Anthropic/Bedrock ``system`` param or OpenAI-style system content."""- if isinstance(system_prompt, str):- text = system_prompt.strip()- elif isinstance(system_prompt, list):- parts: list[str] = []- for block in system_prompt:- if isinstance(block, dict):- # Anthropic: {"type": "text", "text": ...}; Bedrock Converse: {"text": ...}.- block_type = block.get("type")- if block_type == "text" or (block_type is None and "text" in block):- piece = block.get("text", "")- if isinstance(piece, str) and piece:- parts.append(piece)- elif isinstance(block, str) and block:- parts.append(block)- text = "\n\n".join(parts)- else:- return None- if not text:- return None- return {"role": "system", "content": _capture_content(text)}---def _messages_for_langfuse_input(*, request_messages: Any = None, messages: Any = None,- conversation_history: Any = None, user_message: Any = None,- system_prompt: Any = None, pre_coerced: Any = None) -> list[dict[str, Any]]:- """Generation input, prepending ``system_prompt`` when the provider split it out of messages.-- ``pre_coerced`` lets the caller pass an already-coerced list and skip a- second ``_coerce_request_messages`` per hook.- """- raw = pre_coerced if pre_coerced is not None else _coerce_request_messages(- request_messages=request_messages, messages=messages,- conversation_history=conversation_history, user_message=user_message,- )- system_msg = None if raw and raw[0].get("role") == "system" else _serialize_system_prompt(system_prompt)- serialized = _serialize_messages(raw)- return serialized if system_msg is None else [system_msg, *serialized]---def _serialize_messages(messages: Any) -> list[dict[str, Any]]:- if not isinstance(messages, list):- return []- serialized = []- for message in messages[-12:]:- if not isinstance(message, dict):- continue- role = message.get("role")- item = {"role": role, "content": _capture_content(message.get("content"), parse_json_strings=(role == "tool"))}- if role == "tool":- if message.get("tool_call_id"):- item["tool_call_id"] = message.get("tool_call_id")- if message.get("name"):- item["name"] = _safe_value(message.get("name"))- if message.get("tool_calls"):- item["tool_calls"] = _capture_content(message.get("tool_calls"), parse_json_strings=True)- serialized.append(item)- return serialized---def _serialize_tool_calls(tool_calls: Any) -> list[dict[str, Any]]:- serialized = []- for tool_call in tool_calls or ():- fn = getattr(tool_call, "function", None)- name = getattr(fn, "name", None) if fn else None- safe_arguments = _capture_content(getattr(fn, "arguments", None) if fn else None)- serialized.append({- "id": getattr(tool_call, "id", None),- "type": getattr(tool_call, "type", None) or "function",- "name": name,- "arguments": safe_arguments,- "function": {"name": name, "arguments": safe_arguments},- })- return serialized---def _serialize_assistant_message(message: Any) -> dict[str, Any]:- reasoning = None- for attr in ("reasoning", "reasoning_content", "reasoning_details"):- value = getattr(message, attr, None)- if value is not None:- reasoning = _capture_content(value)- break- return {- "content": _capture_content(getattr(message, "content", None)),- "reasoning": reasoning,- "tool_calls": _serialize_tool_calls(getattr(message, "tool_calls", None)),- }---# ----------------------------------------------------------------------------# Usage + cost-# -----------------------------------------------------------------------------def _canonical_usage_and_cost(canonical: Any, *, provider: str, model: str,- base_url: str) -> tuple[dict[str, int], dict[str, float]]:- """Translate canonical Hermes usage into Langfuse usage and cost maps."""- usage_details: Dict[str, int] = {}- for key, attr, _ in _USAGE_FIELDS:- tokens = getattr(canonical, attr)- if tokens or key in ("input", "output"):- usage_details[key] = tokens-- cost_details: Dict[str, float] = {}- try:- from agent.usage_pricing import estimate_usage_cost, resolve_billing_route-- # Subscription-included routes: Langfuse treats explicit cost_details- # (even zeros) as authoritative, so omit them and let it estimate.- route = resolve_billing_route(model, provider=provider, base_url=base_url)- if getattr(route, "billing_mode", "") == "subscription_included":- return usage_details, cost_details- cost = estimate_usage_cost(model, canonical, provider=provider, base_url=base_url, api_key="")- except Exception as exc: # pragma: no cover - fail-open- _debug(f"usage pricing failed: {exc}")- return usage_details, cost_details-- # No total (e.g. cache pricing unknown) => export no costs at all, so a- # partial component subtotal is never mistaken for the request total.- if cost.amount_usd is None:- return usage_details, cost_details-- # Langfuse only derives totals from input/output keys, so cache/custom keys- # need an explicit total (Hermes estimate also includes request pricing).- # A zero total is not exported: Langfuse would treat it as authoritative.- if cost.status != "included" and float(cost.amount_usd) > 0:- cost_details["total"] = float(cost.amount_usd)-- # Per-type breakdown for dashboards; keys mirror usage_details.- try:- from decimal import Decimal-- from agent.usage_pricing import get_pricing_entry-- entry = get_pricing_entry(model, provider=provider, base_url=base_url)- if entry:- for key, attr, rate_attr in _USAGE_FIELDS:- rate = getattr(entry, rate_attr, None) if rate_attr else None- tokens = getattr(canonical, attr)- if rate is not None and tokens:- cost_details[key] = float(Decimal(tokens) * rate / Decimal("1000000"))- except Exception: # pragma: no cover - canonical total remains usable- pass-- return usage_details, cost_details---def _usage_and_cost(response: Any, *, provider: str, api_mode: str, model: str, base_url: str) -> tuple[dict[str, int], dict[str, float]]:- raw_usage = getattr(response, "usage", None)- if not raw_usage:- return {}, {}- try:- from agent.usage_pricing import normalize_usage-- canonical = normalize_usage(raw_usage, provider=provider, api_mode=api_mode)- return _canonical_usage_and_cost(canonical, provider=provider, model=model, base_url=base_url)- except Exception as exc: # pragma: no cover - fail-open- _debug(f"usage normalization failed: {exc}")- return {}, {}---def _summary_usage_and_cost(usage: dict, *, provider: str, model: str, base_url: str) -> tuple[dict[str, int], dict[str, float]]:- """post_api_request path: usage arrives as a pre-built CanonicalUsage summary dict."""- try:- from agent.usage_pricing import CanonicalUsage-- canonical = CanonicalUsage(- input_tokens=usage.get("input_tokens", 0),- output_tokens=usage.get("output_tokens", 0) or usage.get("completion_tokens", 0),- cache_read_tokens=usage.get("cache_read_tokens", 0),- cache_write_tokens=usage.get("cache_write_tokens", 0),- reasoning_tokens=usage.get("reasoning_tokens", 0),- request_count=usage.get("request_count", 1),- )- return _canonical_usage_and_cost(canonical, provider=provider, model=model, base_url=base_url)- except Exception:- return {}, {}---def _moa_usage_and_cost(ref: dict) -> tuple[dict[str, int], dict[str, float]]:- """MoA advisor reference: usage dict keyed like CanonicalUsage plus a pre-computed ``cost_usd``."""- usage = ref.get("usage") or {}- usage_details = {}- if isinstance(usage, dict):- for key, attr, _ in _USAGE_FIELDS:- if usage.get(attr):- usage_details[key] = usage[attr]- cost_usd = ref.get("cost_usd")- return usage_details, ({"total": float(cost_usd)} if isinstance(cost_usd, (int, float)) else {})diff --git a/plugins/observability/langfuse/_trace.py b/plugins/observability/langfuse/_trace.pydeleted file mode 100644index 41b604f08c5..00000000000--- a/plugins/observability/langfuse/_trace.py+++ /dev/null@@ -1,223 +0,0 @@-"""Trace state and observation lifecycle for the Langfuse plugin.--Holds no process-global state: the live ``TraceState`` registry, its lock and-the client cache live in the package ``__init__`` (tests patch them there).-"""-from __future__ import annotations--import threading-import time-from dataclasses import dataclass, field-from typing import Any, Dict, Optional--from ._capture import _capture_mode, _debug, _extract_last_user_message--try:- from langfuse import propagate_attributes-except Exception: # pragma: no cover - fail-open when optional dep is missing- propagate_attributes = None---@dataclass-class TraceState:- trace_id: str- root_ctx: Any- root_span: Any- generations: Dict[str, Any] = field(default_factory=dict)- tools: Dict[str, Any] = field(default_factory=dict)- pending_tools_by_name: Dict[str, list] = field(default_factory=dict)- turn_tool_calls: list[dict[str, Any]] = field(default_factory=list)- # Keyed by child_session_id: subagent_stop carries no child_subagent_id.- subagents: Dict[str, Any] = field(default_factory=dict)- # Fingerprints of MoA fan-outs already recorded: the client holds its last- # fan-out until the next one, so tool-loop turns would re-emit advisors.- moa_emitted: set = field(default_factory=set)- last_updated_at: float = field(default_factory=time.time)-- def touch(self) -> None:- self.last_updated_at = time.time()-- def pop_tool(self, tool_call_id: str, tool_name: str) -> Any:- """Detach the open tool observation: by id, else FIFO by name for id-less callers."""- observation = self.tools.pop(tool_call_id, None) if tool_call_id else None- if observation is None:- queue = self.pending_tools_by_name.get(tool_name)- if queue:- observation = queue.pop(0)- if not queue:- self.pending_tools_by_name.pop(tool_name, None)- return observation-- def record_tool_output(self, tool_call_id: str, output: Any) -> None:- """Backfill the generation's tool_call record so it carries the result alongside arguments."""- for tool_call in reversed(self.turn_tool_calls):- if tool_call.get("id") == tool_call_id:- tool_call["output"] = output- function_payload = tool_call.get("function")- if isinstance(function_payload, dict):- function_payload["output"] = output- return---# ----------------------------------------------------------------------------# Trace keys-# -----------------------------------------------------------------------------def _scope_prefix(task_id: str, session_id: str) -> str:- if task_id:- return f"task:{task_id}"- if session_id:- return f"session:{session_id}"- return f"thread:{threading.get_ident()}"---def _trace_key(task_id: str, session_id: str, *, turn_id: str = "", api_request_id: str = "") -> str:- """Stable in-process trace scope key for one agent turn.-- ``turn_id``/``api_request_id`` scope state so concurrent requests sharing a- task/session never collide. ``turn_id`` wins over ``api_request_id`` so the- turn-level post_llm_call hook (no api_request_id) resolves to the same key- as request-level hooks. Legacy shape: bare ``task_id`` (no ``task:`` prefix),- kept for keys minted before turn/request scoping existed.- """- if turn_id:- return f"{_scope_prefix(task_id, session_id)}:turn:{turn_id}"- if api_request_id:- return f"{_scope_prefix(task_id, session_id)}:api:{api_request_id}"- if task_id:- return task_id- return _scope_prefix(task_id, session_id)---def _request_key(api_call_count: Any) -> str:- return str(api_call_count or 0)---# ----------------------------------------------------------------------------# Observations-# -----------------------------------------------------------------------------def _start_root_trace(task_key: str, *, task_id: str, session_id: str, platform: str, provider: str, model: str,- api_mode: str, messages: Any, client: Any,- turn_id: str = "", api_request_id: str = "") -> TraceState:- trace_id = client.create_trace_id(seed=f"{session_id or 'sessionless'}::{task_id or task_key}")- trace_input = _extract_last_user_message(messages)- metadata = {- "source": "hermes", "task_id": task_id, "turn_id": turn_id, "api_request_id": api_request_id,- "platform": platform, "provider": provider, "model": model, "api_mode": api_mode,- "capture_mode": _capture_mode(),- }- # session_id must be in trace_context for Langfuse session grouping.- trace_ctx: Dict[str, Any] = {"trace_id": trace_id}- if session_id:- trace_ctx["session_id"] = session_id-- def open_root():- ctx = client.start_as_current_observation(- trace_context=trace_ctx, name="Hermes turn", as_type="chain",- input=trace_input, metadata=metadata, end_on_exit=False,- )- return ctx, ctx.__enter__()-- root_ctx = root_span = None- if propagate_attributes is not None:- try:- with propagate_attributes(session_id=session_id or task_key, trace_name="Hermes turn",- tags=["hermes", "langfuse"]):- root_ctx, root_span = open_root()- except Exception:- root_ctx = None- if root_ctx is None:- root_ctx, root_span = open_root()-- # SDK v3 uses update_trace(); failures must never block the turn.- try:- root_span.update_trace(input=trace_input)- except Exception as exc:- _debug(f"update_trace(input) failed: {exc}")-- _debug(f"started trace {trace_id} for {task_key}")- return TraceState(trace_id=trace_id, root_ctx=root_ctx, root_span=root_span)---def _start_child_observation(state: TraceState, *, name: str, as_type: str, input_value: Any,- metadata: Optional[dict] = None, model: Optional[str] = None,- model_parameters: Optional[dict] = None) -> Any:- return state.root_span.start_observation(- name=name, as_type=as_type, input=input_value, metadata=metadata or {},- model=model, model_parameters=model_parameters,- )---def _end_observation(observation: Any, *, output: Any = None, metadata: Optional[dict] = None,- usage_details: Optional[dict] = None, cost_details: Optional[dict] = None) -> None:- if observation is None:- return- try:- update_kwargs: Dict[str, Any] = {}- if output is not None:- update_kwargs["output"] = output- for key, val in (("metadata", metadata), ("usage_details", usage_details), ("cost_details", cost_details)):- if val:- update_kwargs[key] = val- if update_kwargs:- observation.update(**update_kwargs)- observation.end()- except Exception as exc: # pragma: no cover - fail-open- _debug(f"end observation failed: {exc}")---def _end_children(state: TraceState, *, include_subagents: bool = False) -> None:- for observation in (*state.generations.values(), *state.tools.values()):- _end_observation(observation)- for queue in state.pending_tools_by_name.values():- for observation in queue:- _end_observation(observation)- if include_subagents:- for observation in state.subagents.values():- _end_observation(observation)---def _close_root(state: TraceState, *, label: str, output: Any = None,- children: bool = True, include_subagents: bool = False) -> None:- """End ``state``'s root: children first, then trace output, root ``end()``, context unwind.-- Never raises. Neither output update may prevent ``end()``, else children- export without a root. The root context manager is unwound now, while- ``opentelemetry.trace.Span`` is still a real type: a GC-driven close at- interpreter teardown raises TypeError inside ``use_span``'s isinstance check.- """- try:- if children:- _end_children(state, include_subagents=include_subagents)- if output is not None:- # update_trace sets TRACE-level I/O (SDK v3); root I/O via update().- for method, what in (("update_trace", "update_trace(output)"), ("update", "root update(output)")):- try:- getattr(state.root_span, method)(output=output)- except Exception as exc:- _debug(f"{what} failed: {exc}")- try:- state.root_span.end()- except Exception as exc:- _debug(f"root end() failed: {exc}")- if state.root_ctx is not None:- try:- state.root_ctx.__exit__(None, None, None)- except Exception: # pragma: no cover - fail-open- pass- except Exception as exc: # pragma: no cover - fail-open- _debug(f"{label} failed: {exc}")- # Last-chance end so an unexpected error still exports the root.- try:- state.root_span.end()- except Exception:- pass---def _merge_trace_output(output: Any, state: TraceState) -> Any:- if not state.turn_tool_calls:- return output- merged = dict(output) if isinstance(output, dict) else {"content": output}- merged["tool_calls"] = list(state.turn_tool_calls)- return merged