Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
5936e09
test(harness): add shared OTLP harness for TH-8103 contract tests
nik13 Oct 4, 2026
32f5c9d
test(harness): accept the collector /tracer/v1/traces path
nik13 Oct 4, 2026
3eaadc8
fix(harness): decode chunked OTLP bodies and record request metadata
nik13 Oct 4, 2026
e14df89
feat(claude-agent-sdk): add @traceai/claude-agent-sdk for TypeScript
nik13 Oct 4, 2026
5d0efff
fix(claude-agent-sdk): keep content hidden unless FI_HIDE_* is exactl…
nik13 Oct 4, 2026
14e3adf
fix(claude-agent-sdk): end spans when a Query is closed or disposed
nik13 Oct 4, 2026
378acc5
fix(claude-agent-sdk): start streaming-input turns after the previous…
nik13 Oct 4, 2026
8b1ab15
fix(claude-agent-sdk): keep backgrounded subagent spans open until ta…
nik13 Oct 4, 2026
c6fe543
fix(claude-agent-sdk): promote only new spend; tokens from modelUsage
nik13 Oct 5, 2026
06a3a20
chore(claude-agent-sdk): fix repo metadata, hold providers weakly, dr…
nik13 Oct 5, 2026
cc1a581
fix(claude-agent-sdk): review-r2 N1-N4: clean close after a result, t…
nik13 Oct 5, 2026
a8831d6
docs(claude-agent-sdk): V1: promoted usage is exact only while a sess…
nik13 Oct 5, 2026
a1dfe62
fix(claude-agent-sdk): review-r3 M1: only an idle query right after a…
nik13 Oct 5, 2026
005a70f
fix(claude-agent-sdk): verify-r3 M1 note: only main-loop messages end…
nik13 Oct 5, 2026
4cf737f
fix(claude-agent-sdk): verify-r4: notifications, stream events and se…
nik13 Oct 5, 2026
53ba9fc
fix(claude-agent-sdk): record the requested model on a synthetic API-…
nik13 Oct 12, 2026
3588c8d
docs: move the README catalog row to the TH-8103 catalog PR
nik13 Oct 12, 2026
7c11a1d
fix(claude-agent-sdk): TraceConfig privacy default, no local paths or…
nik13 Oct 17, 2026
077b5cf
Merge dev into feat/th-8235-claude-agent-sdk-ts
nik13 Oct 21, 2026
832fdec
test(claude-agent-sdk): neutral loopback port in the URL scrub test
nik13 Oct 21, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
238 changes: 238 additions & 0 deletions python/tests/harness/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
"""Small, loopback-only utilities for OTLP contract tests."""

from __future__ import annotations

import copy
import difflib
import json
import subprocess
import threading
from http import HTTPStatus
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from pathlib import Path
from typing import Any, Mapping, Optional, Sequence, Union
from urllib.parse import urlsplit
from urllib.request import Request, urlopen


class Receiver:
"""An in-memory OTLP/HTTP receiver for tests.

The receiver only listens on loopback and never forwards received spans.
"""

def __init__(self) -> None:
self._spans: list[dict[str, Any]] = []
self._requests: list[dict[str, Any]] = []
self._lock = threading.Lock()
owner = self

class Handler(BaseHTTPRequestHandler):
def _read_body(self) -> bytes:
# The Node OTLP exporter streams with Transfer-Encoding: chunked
# and sends no Content-Length, so both framings are accepted.
if "chunked" in self.headers.get("Transfer-Encoding", "").lower():
chunks = []
while True:
size_line = self.rfile.readline().split(b";", 1)[0].strip()
size = int(size_line, 16)
if size == 0:
while self.rfile.readline() not in (b"\r\n", b"\n", b""):
pass
return b"".join(chunks)
chunks.append(self.rfile.read(size))
self.rfile.readline()
length = int(self.headers.get("Content-Length", "0"))
return self.rfile.read(length)

def do_POST(self) -> None: # noqa: N802 - required by BaseHTTPRequestHandler
path = urlsplit(self.path).path
if path not in ("/v1/traces", "/tracer/v1/traces"):
self.send_error(HTTPStatus.NOT_FOUND)
return

try:
body = self._read_body()
content_type = self.headers.get("Content-Type", "").lower()

if content_type.startswith("application/json"):
request = json.loads(body.decode("utf-8"))
elif content_type.startswith(("application/x-protobuf", "application/protobuf")):
from google.protobuf.json_format import MessageToDict
from opentelemetry.proto.collector.trace.v1.trace_service_pb2 import (
ExportTraceServiceRequest,
)

protobuf_request = ExportTraceServiceRequest()
protobuf_request.ParseFromString(body)
request = MessageToDict(protobuf_request)
else:
self.send_error(HTTPStatus.UNSUPPORTED_MEDIA_TYPE)
return

spans: list[dict[str, Any]] = []
resource_attributes: list[dict[str, Any]] = []
for resource_spans in request.get(
"resourceSpans", request.get("resource_spans", [])
):
resource_attributes.append(
_flatten_attributes(
resource_spans.get("resource", {}).get("attributes", [])
)
)
for scope_spans in resource_spans.get(
"scopeSpans", resource_spans.get("scope_spans", [])
):
spans.extend(scope_spans.get("spans", []))
except (ImportError, TypeError, UnicodeDecodeError, ValueError) as error:
self.send_error(HTTPStatus.BAD_REQUEST, str(error))
return

with owner._lock:
owner._spans.extend(copy.deepcopy(spans))
owner._requests.append(
{
"path": path,
"headers": {
key.lower(): value for key, value in self.headers.items()
},
"resource_attributes": resource_attributes,
}
)
self.send_response(HTTPStatus.OK)
self.send_header("Content-Length", "0")
self.end_headers()

def log_message(self, _format: str, *args: Any) -> None:
return

self._server = ThreadingHTTPServer(("127.0.0.1", 0), Handler)
self.origin = "http://127.0.0.1:{0}".format(self._server.server_port)
self.endpoint = "{0}/v1/traces".format(self.origin)
self.collector_endpoint = "{0}/tracer/v1/traces".format(self.origin)
self._thread = threading.Thread(target=self._server.serve_forever, daemon=True)
self._thread.start()

def __enter__(self) -> "Receiver":
return self

def __exit__(self, _type: Any, _value: Any, _traceback: Any) -> None:
self._server.shutdown()
self._server.server_close()
self._thread.join()

def spans(self) -> list[dict[str, Any]]:
"""Return a copy of the decoded spans received so far."""
with self._lock:
return copy.deepcopy(self._spans)

def requests(self) -> list[dict[str, Any]]:
"""Return one record per accepted export: path, lower-cased headers, resource attributes."""
with self._lock:
return copy.deepcopy(self._requests)

def clear(self) -> None:
"""Remove every decoded span and request record received so far."""
with self._lock:
self._spans.clear()
self._requests.clear()


def _flatten_attributes(attributes: Sequence[Mapping[str, Any]]) -> dict[str, Any]:
"""Turn OTLP key/value attributes into a plain dict of scalar values."""
flat: dict[str, Any] = {}
for attribute in attributes:
value = attribute.get("value", {})
for kind in ("stringValue", "boolValue", "intValue", "doubleValue"):
if kind in value:
flat[attribute.get("key", "")] = value[kind]
break
else:
flat[attribute.get("key", "")] = value
return flat


def run(
argv: Sequence[str],
env: Mapping[str, str],
stdin: Optional[bytes],
timeout: float,
) -> subprocess.CompletedProcess[bytes]:
"""Run one process and return its captured output, including after a timeout."""
process = subprocess.Popen(
argv,
env=dict(env),
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
timed_out = False
try:
stdout, stderr = process.communicate(input=stdin, timeout=timeout)
except subprocess.TimeoutExpired:
timed_out = True
process.kill()
stdout, stderr = process.communicate()

result = subprocess.CompletedProcess(argv, process.returncode, stdout, stderr)
result.timed_out = timed_out
return result


def post_otlp(spans: dict[str, Any], endpoint: str) -> int:
"""Post one JSON-encoded OTLP request to a loopback receiver."""
parsed = urlsplit(endpoint)
if parsed.scheme != "http" or parsed.hostname != "127.0.0.1":
raise ValueError("post_otlp only sends to a loopback HTTP endpoint")

request = Request(
endpoint,
data=json.dumps(spans).encode("utf-8"),
headers={"Content-Type": "application/json"},
method="POST",
)
with urlopen(request, timeout=5) as response:
return response.status


def compare(actual: Sequence[dict[str, Any]], golden_path: Union[str, Path]) -> None:
"""Fail with a diff unless span names, attributes, and statuses match a golden."""
with Path(golden_path).open(encoding="utf-8") as golden_file:
expected = json.load(golden_file)

def canonical(spans: Sequence[dict[str, Any]]) -> list[dict[str, Any]]:
normalized = []
for span in spans:
attributes = span.get("attributes", [])
if isinstance(attributes, dict):
attributes = [
{"key": key, "value": value} for key, value in attributes.items()
]
normalized.append(
{
"name": span.get("name", ""),
"attributes": sorted(
attributes,
key=lambda attribute: json.dumps(
attribute, sort_keys=True, separators=(",", ":")
),
),
"status": span.get("status", {}),
}
)
return normalized

actual_normalized = canonical(actual)
expected_normalized = canonical(expected)
if actual_normalized != expected_normalized:
diff = "\n".join(
difflib.unified_diff(
json.dumps(expected_normalized, indent=2, sort_keys=True).splitlines(),
json.dumps(actual_normalized, indent=2, sort_keys=True).splitlines(),
fromfile=str(golden_path),
tofile="actual",
lineterm="",
)
)
print(diff)
raise AssertionError("spans do not match golden")
Loading
Loading