debrix 0.1.0a1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
debrix/__init__.py ADDED
@@ -0,0 +1,23 @@
1
+ """Debrix — open-source instrumentation SDK for AI agents."""
2
+
3
+ from debrix.config import configure, force_flush
4
+ from debrix.semconv import SPAN_KINDS, Attr, Event, SpanKind
5
+ from debrix.span import DebrixSpan
6
+ from debrix.tracing import get_tracer, trace_agent, trace_span, trace_tool
7
+
8
+ __version__ = "0.1.0a1"
9
+
10
+ __all__ = [
11
+ "__version__",
12
+ "SpanKind",
13
+ "Attr",
14
+ "Event",
15
+ "SPAN_KINDS",
16
+ "configure",
17
+ "force_flush",
18
+ "DebrixSpan",
19
+ "get_tracer",
20
+ "trace_agent",
21
+ "trace_tool",
22
+ "trace_span",
23
+ ]
debrix/config.py ADDED
@@ -0,0 +1,115 @@
1
+ """OTLP/HTTP exporter configuration for Debrix.
2
+
3
+ Default endpoint: ``http://127.0.0.1:4318`` (OTLP/HTTP protobuf).
4
+ Standard OpenTelemetry environment variables still apply and override defaults
5
+ when set (e.g. ``OTEL_EXPORTER_OTLP_ENDPOINT``).
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ import logging
11
+ import os
12
+ import warnings
13
+ from typing import Final
14
+
15
+ from opentelemetry import trace
16
+ from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
17
+ from opentelemetry.sdk.resources import Resource
18
+ from opentelemetry.sdk.trace import TracerProvider
19
+ from opentelemetry.sdk.trace.export import BatchSpanProcessor, SimpleSpanProcessor
20
+
21
+ from debrix.payloads import ensure_worker, flush_payloads, get_capture_mode
22
+
23
+ DEFAULT_OTLP_ENDPOINT: Final = "http://127.0.0.1:4318"
24
+
25
+ logger = logging.getLogger("debrix.config")
26
+
27
+ _configured = False
28
+ _endpoint: str = DEFAULT_OTLP_ENDPOINT
29
+
30
+
31
+ def _resolved_endpoint(endpoint: str | None = None) -> str:
32
+ if endpoint:
33
+ return endpoint.rstrip("/")
34
+ return os.environ.get(
35
+ "OTEL_EXPORTER_OTLP_ENDPOINT", DEFAULT_OTLP_ENDPOINT
36
+ ).rstrip("/")
37
+
38
+
39
+ def force_flush(timeout_millis: int = 10_000) -> bool:
40
+ """Flush OTLP spans and pending conversation payload uploads.
41
+
42
+ Short-lived scripts must call this (or rely on atexit) before exit;
43
+ otherwise ``blob_ref`` may land in Debrix without the payload body.
44
+ """
45
+ ok = True
46
+ provider = trace.get_tracer_provider()
47
+ if isinstance(provider, TracerProvider):
48
+ ok = bool(provider.force_flush(timeout_millis=timeout_millis)) and ok
49
+ ok = flush_payloads(timeout_millis=timeout_millis) and ok
50
+ return ok
51
+
52
+
53
+ def configure(
54
+ *,
55
+ endpoint: str | None = None,
56
+ service_name: str = "debrix",
57
+ batch: bool = True,
58
+ ) -> TracerProvider:
59
+ """Configure a global TracerProvider that exports OTLP/HTTP to Debrix.
60
+
61
+ Idempotent: subsequent calls return the existing provider without
62
+ re-installing exporters.
63
+
64
+ Args:
65
+ endpoint: OTLP base URL (paths like ``/v1/traces`` are appended by the
66
+ exporter). Defaults to ``http://127.0.0.1:4318``, or
67
+ ``OTEL_EXPORTER_OTLP_ENDPOINT`` when set.
68
+ service_name: Value for ``service.name`` resource attribute.
69
+ batch: Use ``BatchSpanProcessor`` when True (default); otherwise
70
+ ``SimpleSpanProcessor`` (useful for short-lived scripts).
71
+ """
72
+ global _configured, _endpoint
73
+
74
+ current = trace.get_tracer_provider()
75
+ # OpenTelemetry forbids replacing an installed TracerProvider; reuse it.
76
+ if isinstance(current, TracerProvider):
77
+ _configured = True
78
+ ensure_worker(_endpoint)
79
+ return current
80
+
81
+ resolved = _resolved_endpoint(endpoint)
82
+ _endpoint = resolved
83
+ # Ensure env protocol matches our contract when unset.
84
+ os.environ.setdefault("OTEL_EXPORTER_OTLP_PROTOCOL", "http/protobuf")
85
+
86
+ if not batch and get_capture_mode() == "full":
87
+ warnings.warn(
88
+ "debrix.configure(batch=False) with full message capture may block "
89
+ "the agent on span end while large exports flush; prefer batch=True "
90
+ "for long-running agents.",
91
+ UserWarning,
92
+ stacklevel=2,
93
+ )
94
+
95
+ resource = Resource.create({"service.name": service_name})
96
+ provider = TracerProvider(resource=resource)
97
+ exporter = OTLPSpanExporter(endpoint=f"{resolved}/v1/traces")
98
+ processor = (
99
+ BatchSpanProcessor(exporter) if batch else SimpleSpanProcessor(exporter)
100
+ )
101
+ provider.add_span_processor(processor)
102
+ trace.set_tracer_provider(provider)
103
+ _configured = True
104
+ ensure_worker(resolved)
105
+ return provider
106
+
107
+
108
+ def reset_for_tests() -> None:
109
+ """Reset configuration flag (tests only)."""
110
+ global _configured, _endpoint
111
+ _configured = False
112
+ _endpoint = DEFAULT_OTLP_ENDPOINT
113
+ from debrix.payloads import reset_worker_for_tests
114
+
115
+ reset_worker_for_tests()
debrix/payloads.py ADDED
@@ -0,0 +1,279 @@
1
+ """Background payload worker: full conversation bodies → Debrix /v1/payloads."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import atexit
6
+ import gzip
7
+ import hashlib
8
+ import json
9
+ import logging
10
+ import os
11
+ import queue
12
+ import threading
13
+ import time
14
+ import urllib.error
15
+ import urllib.request
16
+ from dataclasses import dataclass
17
+ from typing import Any, Literal
18
+
19
+ from opentelemetry.trace import SpanContext
20
+
21
+ logger = logging.getLogger("debrix.payloads")
22
+
23
+ CaptureMode = Literal["full", "preview", "off"]
24
+
25
+ DEFAULT_MAX_PAYLOAD_BYTES = 209_715_200 # ~200 MiB
26
+ DEFAULT_PREVIEW_CHARS = 4096
27
+ DEFAULT_QUEUE_SIZE = 64
28
+
29
+
30
+ @dataclass
31
+ class PayloadJob:
32
+ kind: Literal["messages", "response"]
33
+ body: bytes
34
+ sha256_hex: str
35
+ span_context: SpanContext
36
+ service_name: str
37
+ trace_id_hex: str
38
+ agent_name: str | None = None
39
+
40
+
41
+ _worker: PayloadWorker | None = None
42
+ _worker_lock = threading.Lock()
43
+ _atexit_registered = False
44
+
45
+
46
+ def get_capture_mode() -> CaptureMode:
47
+ env = os.environ.get("DEBRIX_CAPTURE_MESSAGES", "").strip().lower()
48
+ if env in ("full", "preview", "off"):
49
+ return env # type: ignore[return-value]
50
+ # Desktop Memory tab writes settings.json next to the app DB.
51
+ for candidate in _settings_candidates():
52
+ try:
53
+ with open(candidate, encoding="utf-8") as f:
54
+ data = json.load(f)
55
+ mode = str(data.get("captureMessages", "")).lower()
56
+ if mode in ("full", "preview", "off"):
57
+ return mode # type: ignore[return-value]
58
+ except (OSError, json.JSONDecodeError, TypeError):
59
+ continue
60
+ return "full"
61
+
62
+
63
+ def get_max_payload_bytes() -> int:
64
+ raw = os.environ.get("DEBRIX_MAX_PAYLOAD_BYTES")
65
+ if raw:
66
+ try:
67
+ return int(raw)
68
+ except ValueError:
69
+ pass
70
+ return DEFAULT_MAX_PAYLOAD_BYTES
71
+
72
+
73
+ def get_preview_chars() -> int:
74
+ raw = os.environ.get("DEBRIX_PREVIEW_CHARS")
75
+ if raw:
76
+ try:
77
+ return max(64, int(raw))
78
+ except ValueError:
79
+ pass
80
+ return DEFAULT_PREVIEW_CHARS
81
+
82
+
83
+ def _settings_candidates() -> list[str]:
84
+ home = os.path.expanduser("~")
85
+ return [
86
+ os.environ.get("DEBRIX_SETTINGS_PATH", ""),
87
+ os.path.join(
88
+ home,
89
+ "Library",
90
+ "Application Support",
91
+ "com.debrix.app",
92
+ "settings.json",
93
+ ),
94
+ os.path.join(home, ".debrix", "settings.json"),
95
+ ]
96
+
97
+
98
+ def sha256_hex(data: bytes) -> str:
99
+ return hashlib.sha256(data).hexdigest()
100
+
101
+
102
+ def truncate_text(text: str, budget: int) -> tuple[str, bool]:
103
+ if len(text) <= budget:
104
+ return text, False
105
+ head = budget // 2
106
+ tail = budget - head
107
+ return text[:head] + "…[preview]…" + text[-tail:], True
108
+
109
+
110
+ def build_messages_preview(
111
+ messages: list[dict[str, str]], preview_chars: int
112
+ ) -> tuple[list[dict[str, str]], bool]:
113
+ truncated = False
114
+ out: list[dict[str, str]] = []
115
+ for msg in messages:
116
+ entry = dict(msg)
117
+ content, was = truncate_text(entry.get("content", ""), preview_chars)
118
+ entry["content"] = content
119
+ truncated = truncated or was
120
+ out.append(entry)
121
+ return out, truncated
122
+
123
+
124
+ def build_response_preview(
125
+ response: dict[str, Any], preview_chars: int
126
+ ) -> tuple[dict[str, Any], bool]:
127
+ out = dict(response)
128
+ truncated = False
129
+ content = out.get("content")
130
+ if isinstance(content, str):
131
+ shortened, was = truncate_text(content, preview_chars)
132
+ out["content"] = shortened
133
+ truncated = was
134
+ return out, truncated
135
+
136
+
137
+ class PayloadWorker:
138
+ def __init__(
139
+ self,
140
+ *,
141
+ endpoint: str,
142
+ max_bytes: int = DEFAULT_MAX_PAYLOAD_BYTES,
143
+ queue_size: int = DEFAULT_QUEUE_SIZE,
144
+ ) -> None:
145
+ self._endpoint = endpoint.rstrip("/")
146
+ self._max_bytes = max_bytes
147
+ self._queue: queue.Queue[PayloadJob | None] = queue.Queue(maxsize=queue_size)
148
+ self._thread = threading.Thread(
149
+ target=self._run, name="debrix-payload-worker", daemon=True
150
+ )
151
+ self._started = False
152
+ self._pending = 0
153
+ self._pending_lock = threading.Lock()
154
+ self._idle = threading.Condition(self._pending_lock)
155
+
156
+ def start(self) -> None:
157
+ if not self._started:
158
+ self._thread.start()
159
+ self._started = True
160
+
161
+ def stop(self) -> None:
162
+ try:
163
+ self._queue.put_nowait(None)
164
+ except queue.Full:
165
+ pass
166
+
167
+ def try_enqueue(self, job: PayloadJob) -> str | None:
168
+ """Enqueue job. Returns error string on failure (overflow / over-cap)."""
169
+ if len(job.body) > self._max_bytes:
170
+ return f"payload exceeds max size ({len(job.body)} > {self._max_bytes} bytes)"
171
+ with self._pending_lock:
172
+ self._pending += 1
173
+ try:
174
+ self._queue.put_nowait(job)
175
+ return None
176
+ except queue.Full:
177
+ with self._pending_lock:
178
+ self._pending -= 1
179
+ if self._pending <= 0:
180
+ self._idle.notify_all()
181
+ return "payload queue full"
182
+
183
+ def flush(self, timeout_millis: int = 10_000) -> bool:
184
+ """Block until queued uploads finish (or timeout). Returns True if idle."""
185
+ deadline = time.monotonic() + (timeout_millis / 1000.0)
186
+ with self._idle:
187
+ while self._pending > 0:
188
+ remaining = deadline - time.monotonic()
189
+ if remaining <= 0:
190
+ logger.warning(
191
+ "payload flush timed out with %s job(s) still pending",
192
+ self._pending,
193
+ )
194
+ return False
195
+ self._idle.wait(timeout=remaining)
196
+ return True
197
+
198
+ def _job_done(self) -> None:
199
+ with self._idle:
200
+ self._pending = max(0, self._pending - 1)
201
+ if self._pending == 0:
202
+ self._idle.notify_all()
203
+
204
+ def _run(self) -> None:
205
+ while True:
206
+ job = self._queue.get()
207
+ if job is None:
208
+ return
209
+ try:
210
+ self._upload(job)
211
+ except Exception: # noqa: BLE001 — never kill worker
212
+ logger.exception("payload upload failed")
213
+ finally:
214
+ self._job_done()
215
+
216
+ def _upload(self, job: PayloadJob) -> None:
217
+ compressed = gzip.compress(job.body)
218
+ url = f"{self._endpoint}/v1/payloads"
219
+ req = urllib.request.Request(url, data=compressed, method="POST")
220
+ req.add_header("Content-Type", "application/json")
221
+ req.add_header("Content-Encoding", "gzip")
222
+ req.add_header("X-Debrix-Sha256", job.sha256_hex)
223
+ req.add_header("X-Debrix-Kind", job.kind)
224
+ req.add_header("X-Debrix-Trace-Id", job.trace_id_hex)
225
+ req.add_header("X-Debrix-Service-Name", job.service_name)
226
+ if job.agent_name:
227
+ req.add_header("X-Debrix-Agent-Name", job.agent_name)
228
+ try:
229
+ with urllib.request.urlopen(req, timeout=60) as resp:
230
+ resp.read()
231
+ logger.debug(
232
+ "uploaded %s payload sha256:%s (%s bytes)",
233
+ job.kind,
234
+ job.sha256_hex,
235
+ len(job.body),
236
+ )
237
+ except urllib.error.HTTPError as exc:
238
+ body = exc.read().decode("utf-8", errors="replace")
239
+ logger.error("payload upload HTTP %s: %s", exc.code, body)
240
+ return
241
+ except urllib.error.URLError as exc:
242
+ logger.error("payload upload failed: %s", exc)
243
+ return
244
+
245
+
246
+ def ensure_worker(endpoint: str) -> PayloadWorker:
247
+ global _worker, _atexit_registered
248
+ with _worker_lock:
249
+ if _worker is None:
250
+ _worker = PayloadWorker(
251
+ endpoint=endpoint, max_bytes=get_max_payload_bytes()
252
+ )
253
+ _worker.start()
254
+ if not _atexit_registered:
255
+ atexit.register(_atexit_flush)
256
+ _atexit_registered = True
257
+ return _worker
258
+
259
+
260
+ def flush_payloads(timeout_millis: int = 10_000) -> bool:
261
+ """Flush the global payload worker if it exists."""
262
+ with _worker_lock:
263
+ worker = _worker
264
+ if worker is None:
265
+ return True
266
+ return worker.flush(timeout_millis=timeout_millis)
267
+
268
+
269
+ def _atexit_flush() -> None:
270
+ flush_payloads(timeout_millis=10_000)
271
+
272
+
273
+ def reset_worker_for_tests() -> None:
274
+ global _worker
275
+ with _worker_lock:
276
+ if _worker is not None:
277
+ _worker.stop()
278
+ _worker.flush(timeout_millis=1_000)
279
+ _worker = None
debrix/py.typed ADDED
File without changes
debrix/semconv.py ADDED
@@ -0,0 +1,83 @@
1
+ """Debrix semantic conventions — shared span-kind and attribute-key contract.
2
+
3
+ This module is the Python source of truth for the Debrix semantic model. It
4
+ mirrors ``docs/Semantic_Model.md`` and the Rust ``semconv.rs`` in the desktop
5
+ app. Change the spec doc first, then keep all three in lockstep.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ __all__ = ["SpanKind", "Attr", "Event", "SPAN_KINDS"]
11
+
12
+
13
+ class SpanKind:
14
+ """Allowed values for the ``debrix.span.kind`` attribute."""
15
+
16
+ AGENT = "agent"
17
+ LLM = "llm"
18
+ TOOL = "tool"
19
+ MCP = "mcp"
20
+ MEMORY = "memory"
21
+ EVALUATION = "evaluation"
22
+ HUMAN = "human"
23
+ CUSTOM = "custom"
24
+
25
+
26
+ class Attr:
27
+ """Debrix-owned attribute keys. All use the ``debrix.`` prefix."""
28
+
29
+ # Identity
30
+ SPAN_KIND = "debrix.span.kind"
31
+ AGENT_NAME = "debrix.agent.name"
32
+ TOOL_NAME = "debrix.tool.name"
33
+ MCP_SERVER = "debrix.mcp.server"
34
+ MCP_TOOL = "debrix.mcp.tool"
35
+
36
+ # Messages / response (legacy inline + blob model)
37
+ MESSAGES = "debrix.messages"
38
+ MESSAGES_PREVIEW = "debrix.messages.preview"
39
+ MESSAGES_BLOB_REF = "debrix.messages.blob_ref"
40
+ MESSAGES_BYTES = "debrix.messages.bytes"
41
+ MESSAGES_CHARS = "debrix.messages.chars"
42
+ MESSAGES_COUNT = "debrix.messages.count"
43
+ MESSAGES_TRUNCATED = "debrix.messages.truncated"
44
+ MESSAGES_CAPTURE_ERROR = "debrix.messages.capture_error"
45
+
46
+ RESPONSE = "debrix.response"
47
+ RESPONSE_PREVIEW = "debrix.response.preview"
48
+ RESPONSE_BLOB_REF = "debrix.response.blob_ref"
49
+ RESPONSE_BYTES = "debrix.response.bytes"
50
+ RESPONSE_CHARS = "debrix.response.chars"
51
+ RESPONSE_TRUNCATED = "debrix.response.truncated"
52
+ RESPONSE_CAPTURE_ERROR = "debrix.response.capture_error"
53
+
54
+ # Status / errors
55
+ ERROR_SUMMARY = "debrix.error.summary"
56
+
57
+ # Mock / replay / eval (reserved for later phases)
58
+ MOCKED = "debrix.mocked"
59
+ REPLAY_INPUT = "debrix.replay.input"
60
+ REPLAY_OUTPUT = "debrix.replay.output"
61
+ REPLAY_SEQUENCE_INDEX = "debrix.replay.sequence_index"
62
+ EVAL_SOURCE_TRACE_ID = "debrix.eval.source_trace_id"
63
+
64
+ PAYLOAD_KIND = "debrix.payload.kind"
65
+ PAYLOAD_BLOB_REF = "debrix.payload.blob_ref"
66
+
67
+
68
+ class Event:
69
+ """Debrix span event names."""
70
+
71
+ PAYLOAD_READY = "debrix.payload.ready"
72
+
73
+
74
+ SPAN_KINDS: tuple[str, ...] = (
75
+ SpanKind.AGENT,
76
+ SpanKind.LLM,
77
+ SpanKind.TOOL,
78
+ SpanKind.MCP,
79
+ SpanKind.MEMORY,
80
+ SpanKind.EVALUATION,
81
+ SpanKind.HUMAN,
82
+ SpanKind.CUSTOM,
83
+ )
debrix/span.py ADDED
@@ -0,0 +1,243 @@
1
+ """Debrix span wrapper with opt-in message / response recording."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import json
6
+ from collections.abc import Mapping, Sequence
7
+ from typing import Any
8
+
9
+ from opentelemetry.trace import Span, Status, StatusCode
10
+
11
+ from debrix.payloads import (
12
+ PayloadJob,
13
+ build_messages_preview,
14
+ build_response_preview,
15
+ ensure_worker,
16
+ get_capture_mode,
17
+ get_preview_chars,
18
+ sha256_hex,
19
+ )
20
+ from debrix.semconv import Attr, Event
21
+
22
+ __all__ = ["DebrixSpan", "ALLOWED_MESSAGE_ROLES"]
23
+
24
+ ALLOWED_MESSAGE_ROLES: frozenset[str] = frozenset(
25
+ {"system", "user", "assistant", "tool"}
26
+ )
27
+
28
+
29
+ def _normalize_messages(
30
+ messages: Sequence[Mapping[str, Any]],
31
+ ) -> list[dict[str, str]]:
32
+ normalized: list[dict[str, str]] = []
33
+ for i, msg in enumerate(messages):
34
+ if not isinstance(msg, Mapping):
35
+ raise TypeError(
36
+ f"messages[{i}] must be a mapping, got {type(msg).__name__}"
37
+ )
38
+ role = msg.get("role")
39
+ content = msg.get("content")
40
+ if not isinstance(role, str) or role not in ALLOWED_MESSAGE_ROLES:
41
+ raise ValueError(
42
+ f"messages[{i}].role must be one of "
43
+ f"{sorted(ALLOWED_MESSAGE_ROLES)}, got {role!r}"
44
+ )
45
+ if not isinstance(content, str):
46
+ raise TypeError(
47
+ f"messages[{i}].content must be a str, "
48
+ f"got {type(content).__name__}"
49
+ )
50
+ entry: dict[str, str] = {"role": role, "content": content}
51
+ name = msg.get("name")
52
+ if name is not None:
53
+ if not isinstance(name, str):
54
+ raise TypeError(
55
+ f"messages[{i}].name must be a str when provided, "
56
+ f"got {type(name).__name__}"
57
+ )
58
+ entry["name"] = name
59
+ normalized.append(entry)
60
+ return normalized
61
+
62
+
63
+ def _service_name_from_span(span: Span) -> str:
64
+ # Resource is on the tracer provider; fall back for tests.
65
+ try:
66
+ resource = span.resource # type: ignore[attr-defined]
67
+ if resource is not None:
68
+ val = resource.attributes.get("service.name")
69
+ if isinstance(val, str) and val:
70
+ return val
71
+ except Exception: # noqa: BLE001
72
+ pass
73
+ return "debrix"
74
+
75
+
76
+ def _trace_id_hex(span: Span) -> str:
77
+ ctx = span.get_span_context()
78
+ return format(ctx.trace_id, "032x")
79
+
80
+
81
+ def _current_agent_name() -> str | None:
82
+ # Lazy import: tracing → span → tracing would cycle at module load.
83
+ from debrix.tracing import current_agent_name
84
+
85
+ return current_agent_name()
86
+
87
+
88
+ class DebrixSpan:
89
+ """Thin wrapper around an OpenTelemetry span with Debrix helpers."""
90
+
91
+ def __init__(self, span: Span) -> None:
92
+ self._span = span
93
+
94
+ @property
95
+ def otel_span(self) -> Span:
96
+ """Underlying OpenTelemetry span."""
97
+ return self._span
98
+
99
+ def record_messages(
100
+ self,
101
+ messages: Sequence[Mapping[str, Any]],
102
+ ) -> None:
103
+ """Record opt-in conversation messages on this span.
104
+
105
+ Full bodies are uploaded asynchronously to Debrix ``/v1/payloads``.
106
+ The span receives preview + stats + ``blob_ref`` immediately so the
107
+ agent thread never waits on disk/network for large prompts.
108
+ """
109
+ mode = get_capture_mode()
110
+ if mode == "off":
111
+ return
112
+
113
+ normalized = _normalize_messages(messages)
114
+ preview_chars = get_preview_chars()
115
+ preview, truncated = build_messages_preview(normalized, preview_chars)
116
+ body = json.dumps(normalized, separators=(",", ":")).encode("utf-8")
117
+ digest = sha256_hex(body)
118
+
119
+ self._span.set_attribute(
120
+ Attr.MESSAGES_PREVIEW, json.dumps(preview, separators=(",", ":"))
121
+ )
122
+ self._span.set_attribute(Attr.MESSAGES_BYTES, len(body))
123
+ self._span.set_attribute(Attr.MESSAGES_CHARS, len(body.decode("utf-8")))
124
+ self._span.set_attribute(Attr.MESSAGES_COUNT, len(normalized))
125
+ self._span.set_attribute(Attr.MESSAGES_TRUNCATED, truncated)
126
+
127
+ if mode == "preview":
128
+ # Compat: also set legacy inline attribute to the preview.
129
+ self._span.set_attribute(
130
+ Attr.MESSAGES, json.dumps(preview, separators=(",", ":"))
131
+ )
132
+ return
133
+
134
+ # full capture
135
+ blob_ref = f"sha256:{digest}"
136
+ self._span.set_attribute(Attr.MESSAGES_BLOB_REF, blob_ref)
137
+ # Small payloads: also keep legacy inline for older UIs / offline Debrix.
138
+ if len(body) <= 64_000:
139
+ self._span.set_attribute(Attr.MESSAGES, body.decode("utf-8"))
140
+
141
+ from debrix.config import DEFAULT_OTLP_ENDPOINT, _resolved_endpoint
142
+
143
+ worker = ensure_worker(_resolved_endpoint() or DEFAULT_OTLP_ENDPOINT)
144
+ err = worker.try_enqueue(
145
+ PayloadJob(
146
+ kind="messages",
147
+ body=body,
148
+ sha256_hex=digest,
149
+ span_context=self._span.get_span_context(),
150
+ service_name=_service_name_from_span(self._span),
151
+ trace_id_hex=_trace_id_hex(self._span),
152
+ agent_name=_current_agent_name(),
153
+ )
154
+ )
155
+ if err:
156
+ self._span.set_attribute(Attr.MESSAGES_CAPTURE_ERROR, err)
157
+ else:
158
+ self._emit_payload_ready("messages", blob_ref)
159
+
160
+ def record_response(self, response: Mapping[str, Any]) -> None:
161
+ """Record opt-in model output / usage on this span.
162
+
163
+ Expected loose shape (extra keys allowed)::
164
+
165
+ {
166
+ "content": "...",
167
+ "model": "...",
168
+ "usage": {"input_tokens": 0, "output_tokens": 0}
169
+ }
170
+ """
171
+ if not isinstance(response, Mapping):
172
+ raise TypeError(
173
+ f"response must be a mapping, got {type(response).__name__}"
174
+ )
175
+ mode = get_capture_mode()
176
+ if mode == "off":
177
+ return
178
+
179
+ payload = dict(response)
180
+ preview_chars = get_preview_chars()
181
+ preview, truncated = build_response_preview(payload, preview_chars)
182
+ body = json.dumps(payload, separators=(",", ":")).encode("utf-8")
183
+ digest = sha256_hex(body)
184
+
185
+ self._span.set_attribute(
186
+ Attr.RESPONSE_PREVIEW, json.dumps(preview, separators=(",", ":"))
187
+ )
188
+ self._span.set_attribute(Attr.RESPONSE_BYTES, len(body))
189
+ self._span.set_attribute(Attr.RESPONSE_CHARS, len(body.decode("utf-8")))
190
+ self._span.set_attribute(Attr.RESPONSE_TRUNCATED, truncated)
191
+
192
+ if mode == "preview":
193
+ self._span.set_attribute(
194
+ Attr.RESPONSE, json.dumps(preview, separators=(",", ":"))
195
+ )
196
+ return
197
+
198
+ blob_ref = f"sha256:{digest}"
199
+ self._span.set_attribute(Attr.RESPONSE_BLOB_REF, blob_ref)
200
+ if len(body) <= 64_000:
201
+ self._span.set_attribute(Attr.RESPONSE, body.decode("utf-8"))
202
+
203
+ from debrix.config import DEFAULT_OTLP_ENDPOINT, _resolved_endpoint
204
+
205
+ worker = ensure_worker(_resolved_endpoint() or DEFAULT_OTLP_ENDPOINT)
206
+ err = worker.try_enqueue(
207
+ PayloadJob(
208
+ kind="response",
209
+ body=body,
210
+ sha256_hex=digest,
211
+ span_context=self._span.get_span_context(),
212
+ service_name=_service_name_from_span(self._span),
213
+ trace_id_hex=_trace_id_hex(self._span),
214
+ agent_name=_current_agent_name(),
215
+ )
216
+ )
217
+ if err:
218
+ self._span.set_attribute(Attr.RESPONSE_CAPTURE_ERROR, err)
219
+ else:
220
+ self._emit_payload_ready("response", blob_ref)
221
+
222
+ def _emit_payload_ready(self, kind: str, blob_ref: str) -> None:
223
+ """Mark the content-addressed blob as queued (see Semantic Model)."""
224
+ self._span.add_event(
225
+ Event.PAYLOAD_READY,
226
+ {
227
+ Attr.PAYLOAD_KIND: kind,
228
+ Attr.PAYLOAD_BLOB_REF: blob_ref,
229
+ },
230
+ )
231
+
232
+ def record_exception(self, exc: BaseException) -> None:
233
+ """Mark the span as errored and attach a short Debrix summary."""
234
+ self._span.set_status(Status(StatusCode.ERROR, str(exc)))
235
+ self._span.record_exception(exc)
236
+ summary = f"{type(exc).__name__}: {exc}"
237
+ if len(summary) > 200:
238
+ summary = summary[:197] + "..."
239
+ self._span.set_attribute(Attr.ERROR_SUMMARY, summary)
240
+
241
+ def set_attribute(self, key: str, value: Any) -> None:
242
+ """Set an attribute on the underlying span."""
243
+ self._span.set_attribute(key, value)
debrix/tracing.py ADDED
@@ -0,0 +1,327 @@
1
+ """Debrix instrumentation primitives: agents, tools, and generic spans."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import functools
6
+ import inspect
7
+ import json
8
+ from collections.abc import Awaitable, Callable, Iterator, Mapping, Sequence
9
+ from contextlib import contextmanager
10
+ from typing import Any, ParamSpec, TypeVar, cast, overload
11
+
12
+ import opentelemetry.context as otel_context
13
+ from opentelemetry import trace
14
+ from opentelemetry.sdk.trace import TracerProvider
15
+ from opentelemetry.trace import Tracer
16
+
17
+ from debrix.config import configure
18
+ from debrix.semconv import Attr, SpanKind
19
+ from debrix.span import DebrixSpan
20
+
21
+ __all__ = [
22
+ "trace_agent",
23
+ "trace_tool",
24
+ "trace_span",
25
+ "get_tracer",
26
+ "current_agent_name",
27
+ ]
28
+
29
+ P = ParamSpec("P")
30
+ R = TypeVar("R")
31
+
32
+ _TRACER_NAME = "debrix"
33
+ _SKIP_BOUND_PARAMS = frozenset({"self", "cls"})
34
+ _AGENT_NAME_KEY = otel_context.create_key("debrix.agent.name")
35
+
36
+
37
+ def current_agent_name() -> str | None:
38
+ """Return the nearest enclosing ``trace_agent`` name, if any."""
39
+ value = otel_context.get_value(_AGENT_NAME_KEY)
40
+ return value if isinstance(value, str) and value else None
41
+
42
+
43
+ def get_tracer() -> Tracer:
44
+ """Return the Debrix tracer, ensuring a provider is configured."""
45
+ current = trace.get_tracer_provider()
46
+ if not isinstance(current, TracerProvider):
47
+ configure()
48
+ return trace.get_tracer(_TRACER_NAME)
49
+
50
+
51
+ def _attach_span(span: Any) -> object:
52
+ ctx = trace.set_span_in_context(span)
53
+ return otel_context.attach(ctx)
54
+
55
+
56
+ def _detach_token(token: object) -> None:
57
+ otel_context.detach(token)
58
+
59
+
60
+ def _json_safe(value: Any) -> Any:
61
+ """Convert a value into something ``json.dumps`` can encode."""
62
+ if value is None or isinstance(value, (bool, int, float, str)):
63
+ return value
64
+ if isinstance(value, Mapping):
65
+ return {str(k): _json_safe(v) for k, v in value.items()}
66
+ if isinstance(value, Sequence) and not isinstance(value, (str, bytes, bytearray)):
67
+ return [_json_safe(v) for v in value]
68
+ if isinstance(value, (bytes, bytearray)):
69
+ return value.decode("utf-8", errors="replace")
70
+ return repr(value)
71
+
72
+
73
+ def _dumps_replay(value: Any) -> str:
74
+ return json.dumps(_json_safe(value), ensure_ascii=False)
75
+
76
+
77
+ def _bind_arguments(
78
+ fn: Callable[..., Any],
79
+ args: tuple[Any, ...],
80
+ kwargs: dict[str, Any],
81
+ ) -> dict[str, Any]:
82
+ """Bind call args to parameter names for replay (skip ``self`` / ``cls``)."""
83
+ try:
84
+ bound = inspect.signature(fn).bind(*args, **kwargs)
85
+ bound.apply_defaults()
86
+ except TypeError:
87
+ return {
88
+ "args": list(args),
89
+ "kwargs": dict(kwargs),
90
+ }
91
+ return {
92
+ name: value
93
+ for name, value in bound.arguments.items()
94
+ if name not in _SKIP_BOUND_PARAMS
95
+ }
96
+
97
+
98
+ @contextmanager
99
+ def trace_span(
100
+ name: str,
101
+ *,
102
+ kind: str = SpanKind.CUSTOM,
103
+ attributes: dict[str, str] | None = None,
104
+ ) -> Iterator[DebrixSpan]:
105
+ """Context manager for a Debrix-instrumented span.
106
+
107
+ Args:
108
+ name: Span name.
109
+ kind: Value for ``debrix.span.kind`` (default ``custom``).
110
+ attributes: Extra string attributes to set at start.
111
+ """
112
+ attrs: dict[str, str] = {Attr.SPAN_KIND: kind}
113
+ if attributes:
114
+ attrs.update(attributes)
115
+ span = get_tracer().start_span(name, attributes=attrs)
116
+ token = _attach_span(span)
117
+ agent_token: object | None = None
118
+ if kind == SpanKind.AGENT:
119
+ agent_name = attrs.get(Attr.AGENT_NAME) or name
120
+ agent_token = otel_context.attach(
121
+ otel_context.set_value(_AGENT_NAME_KEY, agent_name)
122
+ )
123
+ wrapper = DebrixSpan(span)
124
+ exc: BaseException | None = None
125
+ try:
126
+ yield wrapper
127
+ except BaseException as e:
128
+ exc = e
129
+ raise
130
+ finally:
131
+ if exc is not None:
132
+ wrapper.record_exception(exc)
133
+ span.end()
134
+ if agent_token is not None:
135
+ _detach_token(agent_token)
136
+ _detach_token(token)
137
+
138
+
139
+ def _wrap_function(
140
+ fn: Callable[P, R],
141
+ *,
142
+ span_name: str,
143
+ span_kind: str,
144
+ attributes: dict[str, str],
145
+ capture_io: bool = False,
146
+ ) -> Callable[P, R]:
147
+ if inspect.iscoroutinefunction(fn):
148
+
149
+ @functools.wraps(fn)
150
+ async def async_wrapper(*args: P.args, **kwargs: P.kwargs) -> Any:
151
+ with trace_span(
152
+ span_name, kind=span_kind, attributes=attributes
153
+ ) as span:
154
+ if capture_io:
155
+ # Record input before the call so failures still keep args.
156
+ span.set_attribute(
157
+ Attr.REPLAY_INPUT,
158
+ _dumps_replay(
159
+ _bind_arguments(fn, args, kwargs)
160
+ ),
161
+ )
162
+ result = await cast(Callable[..., Awaitable[Any]], fn)(
163
+ *args, **kwargs
164
+ )
165
+ if capture_io:
166
+ span.set_attribute(
167
+ Attr.REPLAY_OUTPUT, _dumps_replay(result)
168
+ )
169
+ return result
170
+
171
+ return cast(Callable[P, R], async_wrapper)
172
+
173
+ @functools.wraps(fn)
174
+ def sync_wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
175
+ with trace_span(span_name, kind=span_kind, attributes=attributes) as span:
176
+ if capture_io:
177
+ span.set_attribute(
178
+ Attr.REPLAY_INPUT,
179
+ _dumps_replay(_bind_arguments(fn, args, kwargs)),
180
+ )
181
+ result = fn(*args, **kwargs)
182
+ if capture_io:
183
+ span.set_attribute(Attr.REPLAY_OUTPUT, _dumps_replay(result))
184
+ return result
185
+
186
+ return sync_wrapper
187
+
188
+
189
+ def _instrument(
190
+ *,
191
+ span_kind: str,
192
+ identity_key: str,
193
+ func: Callable[..., Any] | None,
194
+ name: str | None,
195
+ capture_io: bool = False,
196
+ ) -> Any:
197
+ """Shared implementation for ``trace_agent`` / ``trace_tool``.
198
+
199
+ Supports:
200
+ - ``@trace_agent`` / ``@trace_tool``
201
+ - ``@trace_agent(name=...)`` / ``@trace_tool(name=...)``
202
+ - ``with trace_agent("name"):`` / ``with trace_tool("name"):``
203
+ """
204
+ # Context-manager form: first positional arg is a string span name.
205
+ if isinstance(func, str):
206
+ span_name = func
207
+
208
+ @contextmanager
209
+ def cm() -> Iterator[DebrixSpan]:
210
+ with trace_span(
211
+ span_name,
212
+ kind=span_kind,
213
+ attributes={identity_key: span_name},
214
+ ) as span:
215
+ yield span
216
+
217
+ return cm()
218
+
219
+ def decorate(fn: Callable[P, R]) -> Callable[P, R]:
220
+ span_name = name or fn.__name__
221
+ return _wrap_function(
222
+ fn,
223
+ span_name=span_name,
224
+ span_kind=span_kind,
225
+ attributes={identity_key: span_name},
226
+ capture_io=capture_io,
227
+ )
228
+
229
+ if func is not None:
230
+ return decorate(func)
231
+ return decorate
232
+
233
+
234
+ @overload
235
+ def trace_agent(func: Callable[P, R], /) -> Callable[P, R]: ...
236
+
237
+
238
+ @overload
239
+ def trace_agent(name: str, /) -> Any: ...
240
+
241
+
242
+ @overload
243
+ def trace_agent(
244
+ func: None = None,
245
+ /,
246
+ *,
247
+ name: str | None = None,
248
+ ) -> Callable[[Callable[P, R]], Callable[P, R]]: ...
249
+
250
+
251
+ def trace_agent(
252
+ func: Callable[P, R] | str | None = None,
253
+ /,
254
+ *,
255
+ name: str | None = None,
256
+ ) -> Any:
257
+ """Instrument an agent boundary.
258
+
259
+ Usage::
260
+
261
+ @trace_agent
262
+ def run(): ...
263
+
264
+ @trace_agent(name="planner")
265
+ def run(): ...
266
+
267
+ with trace_agent("planner") as span:
268
+ ...
269
+ """
270
+ return _instrument(
271
+ span_kind=SpanKind.AGENT,
272
+ identity_key=Attr.AGENT_NAME,
273
+ func=func,
274
+ name=name,
275
+ capture_io=False,
276
+ )
277
+
278
+
279
+ @overload
280
+ def trace_tool(func: Callable[P, R], /) -> Callable[P, R]: ...
281
+
282
+
283
+ @overload
284
+ def trace_tool(name: str, /) -> Any: ...
285
+
286
+
287
+ @overload
288
+ def trace_tool(
289
+ func: None = None,
290
+ /,
291
+ *,
292
+ name: str | None = None,
293
+ ) -> Callable[[Callable[P, R]], Callable[P, R]]: ...
294
+
295
+
296
+ def trace_tool(
297
+ func: Callable[P, R] | str | None = None,
298
+ /,
299
+ *,
300
+ name: str | None = None,
301
+ ) -> Any:
302
+ """Instrument a tool call.
303
+
304
+ When used as a decorator, records bound call arguments on
305
+ ``debrix.replay.input`` and the return value on ``debrix.replay.output``
306
+ (JSON strings) for later deterministic replay. Input is written before the
307
+ call so failures still retain arguments. Context-manager form does not
308
+ auto-capture I/O — set those attributes yourself if needed.
309
+
310
+ Usage::
311
+
312
+ @trace_tool
313
+ def search(): ...
314
+
315
+ @trace_tool(name="web_search")
316
+ def search(): ...
317
+
318
+ with trace_tool("search") as span:
319
+ ...
320
+ """
321
+ return _instrument(
322
+ span_kind=SpanKind.TOOL,
323
+ identity_key=Attr.TOOL_NAME,
324
+ func=func,
325
+ name=name,
326
+ capture_io=True,
327
+ )
@@ -0,0 +1,110 @@
1
+ Metadata-Version: 2.4
2
+ Name: debrix
3
+ Version: 0.1.0a1
4
+ Summary: Open-source instrumentation SDK for Debrix — local-first AI Agent DevTools
5
+ Project-URL: Homepage, https://github.com/goyal-prateek/debrix-sdk
6
+ Project-URL: Repository, https://github.com/goyal-prateek/debrix-sdk
7
+ Project-URL: Issues, https://github.com/goyal-prateek/debrix-sdk/issues
8
+ Project-URL: Documentation, https://github.com/goyal-prateek/debrix-sdk#readme
9
+ Author-email: Prateek Goyal <gprateek015@gmail.com>
10
+ License-Expression: MIT
11
+ License-File: LICENSE
12
+ Keywords: agents,debrix,observability,opentelemetry,tracing
13
+ Classifier: Development Status :: 3 - Alpha
14
+ Classifier: Intended Audience :: Developers
15
+ Classifier: License :: OSI Approved :: MIT License
16
+ Classifier: Programming Language :: Python :: 3
17
+ Classifier: Programming Language :: Python :: 3.11
18
+ Classifier: Programming Language :: Python :: 3.12
19
+ Classifier: Programming Language :: Python :: 3.13
20
+ Classifier: Topic :: Software Development :: Debuggers
21
+ Requires-Python: >=3.11
22
+ Requires-Dist: opentelemetry-api>=1.27.0
23
+ Requires-Dist: opentelemetry-exporter-otlp-proto-http>=1.27.0
24
+ Requires-Dist: opentelemetry-sdk>=1.27.0
25
+ Provides-Extra: dev
26
+ Requires-Dist: pytest>=8.0; extra == 'dev'
27
+ Description-Content-Type: text/markdown
28
+
29
+ # debrix
30
+
31
+ Open-source instrumentation SDK for [Debrix](https://github.com/goyal-prateek/debrix-sdk) — local-first AI Agent DevTools.
32
+
33
+ **Status:** alpha (`0.1.0a1`). APIs may change.
34
+
35
+ Requires the Debrix desktop app running locally to receive traces (OTLP/HTTP on `localhost:4318`).
36
+
37
+ ## Install
38
+
39
+ ```bash
40
+ pip install debrix
41
+ ```
42
+
43
+ TestPyPI (pre-release smoke):
44
+
45
+ ```bash
46
+ pip install -i https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ debrix==0.1.0a1
47
+ ```
48
+
49
+ ## Quick start
50
+
51
+ ```python
52
+ from debrix import configure, force_flush, trace_agent, trace_tool, trace_span, SpanKind
53
+
54
+ configure(batch=False) # OTLP/HTTP → http://127.0.0.1:4318
55
+
56
+ @trace_agent
57
+ def run_agent(query: str) -> str:
58
+ return research(query)
59
+
60
+ @trace_tool(name="search")
61
+ def research(query: str) -> str:
62
+ with trace_span("complete", kind=SpanKind.LLM) as span:
63
+ span.record_messages([
64
+ {"role": "system", "content": "You are helpful."},
65
+ {"role": "user", "content": query},
66
+ ])
67
+ answer = "..."
68
+ span.record_response({
69
+ "content": answer,
70
+ "usage": {"input_tokens": 10, "output_tokens": 5},
71
+ })
72
+ return answer
73
+
74
+ run_agent("hello")
75
+ force_flush() # flush OTLP *and* conversation payload uploads before exit
76
+ ```
77
+
78
+ Decorators also work as context managers:
79
+
80
+ ```python
81
+ with trace_agent("planner") as span:
82
+ with trace_tool("lookup"):
83
+ ...
84
+ ```
85
+
86
+ ## Public API
87
+
88
+ | Symbol | Purpose |
89
+ | ------ | ------- |
90
+ | `configure()` | Install OTLP/HTTP exporter to Debrix (`:4318`) |
91
+ | `force_flush()` | Flush OTLP spans + pending `/v1/payloads` uploads (call before short scripts exit) |
92
+ | `trace_agent` | Agent boundary (decorator or `with trace_agent("name")`) |
93
+ | `trace_tool` | Tool call span; decorator records `debrix.replay.input` / `output` |
94
+ | `trace_span` | Generic / LLM / custom span context manager |
95
+ | `DebrixSpan.record_messages(...)` | Opt-in message payloads |
96
+ | `DebrixSpan.record_response(...)` | Opt-in model output / tokens |
97
+ | `SpanKind`, `Attr` | Semantic convention constants |
98
+
99
+ Nested calls propagate via OpenTelemetry context. On exception, spans are marked `ERROR` with `debrix.error.summary`.
100
+
101
+ ## Develop
102
+
103
+ ```bash
104
+ uv sync --group dev
105
+ uv run pytest
106
+ ```
107
+
108
+ ## License
109
+
110
+ MIT
@@ -0,0 +1,11 @@
1
+ debrix/__init__.py,sha256=eLGElkRE704TGQKS-KTLKkpRUbnHue2LmSnB4lUPFXg,530
2
+ debrix/config.py,sha256=_VlL9uqAnnORdSTrIkzv5-S0nMmjjpidt-KzLY4w_Oc,3904
3
+ debrix/payloads.py,sha256=RLS3XYrWkaT0JNQOFoeQpOtrEElo-f5XSz743pkT9EA,8555
4
+ debrix/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
+ debrix/semconv.py,sha256=xkYC5pNY8VgIy5rXRGca1By6WocfyEX1ttp0x2U_3xk,2431
6
+ debrix/span.py,sha256=-SVtJ_xiIZGf4Bmr7SusFOSFhSBvKwr9WMahdBoMZXs,8591
7
+ debrix/tracing.py,sha256=upbT3M-t2a_oIvqR964hrvmhHV7-zMsmh-1Awn4cVjc,8822
8
+ debrix-0.1.0a1.dist-info/METADATA,sha256=4rQ-CgF_90gGp1CnPZpKKsgDsGZ6ythD_qL4KCgJBF4,3511
9
+ debrix-0.1.0a1.dist-info/WHEEL,sha256=lCkmxWfQsSc9CfIClYeavTdQeEX2toPqufh9gI35EQA,87
10
+ debrix-0.1.0a1.dist-info/licenses/LICENSE,sha256=ZDmp4oN9DA2Fh_mlOGtV7_eNSg53fjowQr_t52-QU54,1063
11
+ debrix-0.1.0a1.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.31.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Debrix
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.