The platform for continuously improving AI agents.
import json
from datetime import timedelta
import pytest
from celery.signals import worker_process_shutdown, worker_shutdown
from django.utils import timezone
from opentelemetry.proto.collector.trace.v1.trace_service_pb2 import ExportTraceServiceRequest
from test_research_team import assignment
from tests.fakes.otlp import collector
from overbae.services import research, research_telemetry
from overmind import tracing
pytestmark = [pytest.mark.django_db, pytest.mark.covers("sdk-wire")]
@pytest.fixture
def telemetry(settings, monkeypatch):
monkeypatch.setenv("OVERMIND_ANALYTICS_ENABLED", "false")
monkeypatch.setenv("OVERMIND_SPAN_FLUSH_INTERVAL_MS", "60000")
with collector() as (endpoint, packets, statuses):
settings.RESEARCH_TELEMETRY_API_URL = endpoint
yield packets, statuses
def test_native_receipts_export_correlated_usage_without_payloads(settings, telemetry):
packets, statuses = telemetry
work, lead = assignment()
settings.RESEARCH_TELEMETRY_API_KEY = "ovr_harness_test"
settings.RESEARCH_TELEMETRY_PROJECT_ID = str(work.project_id)
action = research.claim(lead, "model:one", "model", {"prompt": "private-source-content"})
action.started_at = timezone.now() - timedelta(seconds=2)
action.save(update_fields=["started_at"])
with research_telemetry.span("research.turn", work, agent=lead):
research.complete(
action,
{"text": "private-generated-content"},
usage={
"prompt_tokens": 41,
"completion_tokens": 9,
"cached_tokens": 12,
"provider_id": "resp_test",
"cost_estimate": {"estimated_usd": 0.009},
},
cost=0.01,
)
# The completion caller still holds its pre-export model instance.
research_telemetry.action_completed(action)
action.refresh_from_db()
research_telemetry.action_completed(action)
with (
pytest.raises(ValueError, match="private-credential"),
research_telemetry.span("research.failure", work, agent=lead),
):
raise ValueError("private-credential")
worker_process_shutdown.send(sender=None)
spans = [
span
for packet in packets
for resource in ExportTraceServiceRequest.FromString(packet).resource_spans
for scope in resource.scope_spans
for span in scope.spans
]
assert {span.name for span in spans} == {
"research.assignment",
"research.turn",
"research.action",
"research.failure",
"research.event",
}
assert len({span.trace_id for span in spans}) == 1
[emitted] = [span for span in spans if span.name == "research.action"]
attributes = {item.key: item.value for item in emitted.attributes}
assert attributes["research.action.id"].string_value == str(action.pk)
assert attributes["research.usage.prompt_tokens"].int_value == 41
assert attributes["research.usage.cached_tokens"].int_value == 12
assert attributes["genai.prompt_tokens"].int_value == 41
assert attributes["genai.completion_tokens"].int_value == 9
assert attributes["genai.cache_read_tokens"].int_value == 12
assert attributes["genai.cost"].double_value == 0.01
assert attributes["research.provider.provider_id"].string_value == "resp_test"
assert attributes["research.cost.estimated_usd"].double_value == 0.009
assert attributes["research.cost.recorded_usd"].double_value == 0.01
[turn] = [span for span in spans if span.name == "research.turn"]
assert emitted.parent_span_id == turn.span_id
assert emitted.end_time_unix_nano - emitted.start_time_unix_nano >= 2_000_000_000
assert action.trace_context["traceparent"].split("-")[1] == emitted.trace_id.hex()
assert "private-" not in str(spans)
with research_telemetry.span("research.recycled", work, agent=lead):
pass
worker_shutdown.send(sender=None)
assert any(b"research.recycled" in packet for packet in packets)
statuses.append(400)
another = research.claim(lead, "retained", "remember", {"text": "private-research"})
research.complete(another, {"saved": True}, cost=0)
tracing.flush_traces(timeout_millis=1000)
another.refresh_from_db()
assert another.state == "completed"
statuses.append(200)
settings.RESEARCH_TELEMETRY_CAPTURE_PAYLOADS = True
model = research.claim(lead, "model:payload", "model", {"appended": ["new turn only"]})
research.complete(
model,
{
"text": "The tool found two items",
"calls": [{"function": {"arguments": '{"api_key":"private-test-key","limit":2}'}}],
},
trace_inputs={
"messages": [
{"role": "system", "content": "First instruction"},
{"role": "user", "content": "Read two items"},
{"role": "assistant", "reasoning_details": "private-encrypted-reasoning"},
],
"tools": [{"function": {"name": "read"}}],
},
)
tool = research.claim(lead, "tool:payload", "workspace", {"code": "print(2)"})
research.complete(tool, {"stdout": "2"})
_, another_lead = assignment()
outside = research.claim(another_lead, "tool:outside", "workspace", {"code": "private-outside"})
research.complete(outside, {"stdout": "private-outside"})
tracing.flush_traces(timeout_millis=1000)
captured = {}
for packet in packets:
for resource in ExportTraceServiceRequest.FromString(packet).resource_spans:
for scope in resource.scope_spans:
for span in scope.spans:
values = {item.key: item.value.string_value for item in span.attributes}
if span.name == "research.action":
captured[values["research.action.id"]] = values
model_io = captured[str(model.pk)]
assert model_io["overmind.span.type"] == "llm_call"
assert json.loads(model_io["inputs"])["messages"][0]["content"] == "First instruction"
assert json.loads(model_io["inputs"])["tools"][0]["function"]["name"] == "read"
assert json.loads(model_io["outputs"])["calls"][0]["function"]["arguments"] == {
"api_key": "<redacted>",
"limit": 2,
}
assert "private-" not in model_io["inputs"] + model_io["outputs"]
tool_io = captured[str(tool.pk)]
assert tool_io["overmind.span.type"] == "tool_call"
assert json.loads(tool_io["inputs"]) == {"code": "print(2)"}
assert json.loads(tool_io["outputs"]) == {"stdout": "2"}
assert "inputs" not in captured[str(outside.pk)]
assert "outputs" not in captured[str(outside.pk)]