Spaces:
Sleeping
Sleeping
File size: 5,846 Bytes
61bb677 2415446 61bb677 2415446 61bb677 2415446 61bb677 2415446 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | """Structured DEBUG traces for end-to-end request / CLI / provider logging.
Emitted lines are merged into JSON log rows by ``config.logging_config``.
Conversation and Claude Code prompts are logged verbatim unless values live under
sanitized credential keys (e.g. ``api_key``, ``authorization``). The default
INFO log level excludes these detailed request traces.
"""
import asyncio
import sys
from collections.abc import AsyncGenerator, AsyncIterator, Mapping
from typing import Any
from loguru import logger
from free_claude_code.core.async_iterators import try_close_async_iterator
TRACE_PAYLOAD_BINDING = "trace_payload"
_SECRET_VALUE_KEYS = frozenset(
k.lower()
for k in (
"authorization",
"x-api-key",
"anthropic-auth-token",
"api_key",
"password",
"secret",
"token",
"bearer_token",
"openapi_token",
"nvidia-api-key",
)
)
def sanitize_trace_value(obj: Any) -> Any:
"""Recursively copy JSON-like structures redacting credential-shaped keys."""
if isinstance(obj, Mapping):
out: dict[str, Any] = {}
for k, v in obj.items():
if str(k).lower() in _SECRET_VALUE_KEYS:
out[str(k)] = "<redacted>"
else:
out[str(k)] = sanitize_trace_value(v)
return out
if isinstance(obj, tuple | list):
return [sanitize_trace_value(x) for x in obj]
return obj
def trace_event(*, stage: str, event: str, source: str, **fields: Any) -> None:
"""Emit one structured DEBUG trace row merged into JSON by the log sink."""
payload = sanitize_trace_value(
{
"stage": stage,
"event": event,
"source": source,
**fields,
},
)
logger.bind(trace_payload=payload).debug("TRACE {}", event)
async def close_stream_input(
iterator: object,
*,
owner: str,
source: str,
preserved_error: BaseException | None,
) -> None:
"""Close one transform input and observe cleanup failure without raising it."""
close_error = await try_close_async_iterator(iterator)
if close_error is None:
return
trace_event(
stage="lifecycle",
event="stream.input.close_failed",
source=source,
owner=owner,
close_exc_type=type(close_error).__name__,
preserved_exc_type=(
type(preserved_error).__name__ if preserved_error is not None else None
),
)
def extract_claude_session_id_from_headers(headers: Mapping[str, str]) -> str | None:
"""Best-effort session id forwarded by Claude Code / SDK via HTTP."""
lowered = {str(k).lower(): v for k, v in headers.items() if isinstance(v, str)}
for key in (
"anthropic-session-id",
"x-anthropic-session-id",
"claude-session-id",
"x-claude-session-id",
):
candidate = lowered.get(key)
if candidate:
return candidate
return None
async def traced_async_stream(
agen: AsyncIterator[str],
*,
stage: str,
source: str,
complete_event: str,
interrupted_event: str,
chunk_event: str | None = None,
chunk_interval: int = 250,
extra: Mapping[str, Any] | None = None,
) -> AsyncGenerator[str]:
"""Emit TRACE rows when a text stream completes, fails, cancels, or periodically."""
common = dict(extra or {})
count = 0
nbytes = 0
interrupted = False
try:
async for chunk in agen:
count += 1
nbytes += len(chunk.encode("utf-8", errors="replace"))
if chunk_event and chunk_interval > 0 and count % chunk_interval == 0:
trace_event(
stage=stage,
event=chunk_event,
source=source,
stream_chunks_so_far=count,
stream_bytes_so_far=nbytes,
**common,
)
yield chunk
except GeneratorExit:
raise
except asyncio.CancelledError:
interrupted = True
trace_event(
stage=stage,
event=interrupted_event,
source=source,
stream_chunks=count,
stream_bytes=nbytes,
outcome="cancelled",
**common,
)
raise
except BaseExceptionGroup as grp:
interrupted = True
trace_event(
stage=stage,
event=interrupted_event,
source=source,
stream_chunks=count,
stream_bytes=nbytes,
outcome="exception_group",
note=str(grp),
**common,
)
raise
except Exception as exc:
interrupted = True
trace_event(
stage=stage,
event=interrupted_event,
source=source,
stream_chunks=count,
stream_bytes=nbytes,
outcome="error",
exc_type=type(exc).__name__,
**common,
)
raise
finally:
await close_stream_input(
agen,
owner="traced_async_stream",
source=source,
preserved_error=sys.exception(),
)
if not interrupted:
trace_event(
stage=stage,
event=complete_event,
source=source,
stream_chunks=count,
stream_bytes=nbytes,
outcome="ok",
**common,
)
def provider_chat_body_snapshot(body: Mapping[str, Any]) -> dict[str, Any]:
"""Sanitized OpenAI-compat chat body subset for traces (conversation text verbatim)."""
keys = ("model", "messages", "tools", "tool_choice", "temperature", "max_tokens")
snap = {k: body[k] for k in keys if k in body and body[k] is not None}
return sanitize_trace_value(snap)
|