Skip to content

CubeLoop — a Pythonic, async-native agent harness core designed for high performance, readability, and production-grade persistence.

fix(tracing): mark oneshot root span on provider failure / cancellation

xf gong2ae97e5ba651
diff --git a/cubepi/tracing/tracer.py b/cubepi/tracing/tracer.py
index 5bbaad4..2228efd 100644
--- a/cubepi/tracing/tracer.py
+++ b/cubepi/tracing/tracer.py
@@ -580,6 +580,30 @@ class Tracer:
token = _active_run.set(run)
try:
yield _OneShotSession(provider=provider, model=model, run=run)
+ except BaseException as _exc:
+ # Mark the root span as failed so `cubepi trace ls` (which
+ # reads status off the root) lists this run as an error
+ # instead of a successful invoke_agent. Mirrors what the
+ # agent path does on turn-level errors. Cancellation is
+ # tracked as cubepi.aborted (UNSET status, semconv-aligned)
+ # rather than ERROR.
+ try:
+ from opentelemetry.trace import Status, StatusCode
+
+ from cubepi.tracing.schema import CUBEPI_ABORTED, ERROR_TYPE
+
+ import asyncio as _asyncio_for_cancel
+
+ if isinstance(_exc, _asyncio_for_cancel.CancelledError):
+ root_span.set_attribute(CUBEPI_ABORTED, True)
+ root_span.set_attribute(ERROR_TYPE, "cubepi.aborted")
+ else:
+ root_span.set_status(Status(StatusCode.ERROR, str(_exc)[:256]))
+ root_span.set_attribute(ERROR_TYPE, type(_exc).__name__)
+ root_span.record_exception(_exc)
+ except Exception:
+ pass
+ raise
finally:
_active_run.reset(token)
for d in detachers:
diff --git a/tests/tracing/test_oneshot.py b/tests/tracing/test_oneshot.py
index 799dd6e..78b30c5 100644
--- a/tests/tracing/test_oneshot.py
+++ b/tests/tracing/test_oneshot.py
@@ -197,12 +197,15 @@ async def test_oneshot_record_content_captures_messages() -> None:
@pytest.mark.asyncio
-async def test_oneshot_generate_error_event_raises() -> None:
- """generate() propagates RuntimeError when the stream emits an error event."""
+async def test_oneshot_generate_error_event_raises_and_marks_root() -> None:
+ """generate() propagates RuntimeError on a stream error event AND marks
+ the root invoke_agent span with ERROR status so `cubepi trace ls` shows
+ the run as failed instead of as a successful invoke_agent."""
+ from opentelemetry.trace import StatusCode
from unittest.mock import AsyncMock, MagicMock, patch
provider = FauxProvider()
- tracer, _ = _make_tracer()
+ tracer, exporter = _make_tracer()
# Patch provider.stream to yield an error event
error_stream = MagicMock()
@@ -220,8 +223,16 @@ async def test_oneshot_generate_error_event_raises() -> None:
max_output_tokens=10,
)
+ await tracer.force_flush()
await tracer.shutdown()
+ roots = [s for s in exporter.spans if s.name == "invoke_agent"]
+ assert len(roots) == 1
+ root = roots[0]
+ assert root.status.status_code == StatusCode.ERROR
+ attrs = dict(root.attributes or {})
+ assert attrs.get("error.type") == "RuntimeError"
+
@pytest.mark.asyncio
async def test_oneshot_subscribe_failure_raises_and_ends_root_span() -> None:
@@ -448,9 +459,14 @@ async def test_oneshot_cancelled_generate_closes_chat_span() -> None:
await tracer.force_flush()
await tracer.shutdown()
- # Root span must be exported; chat span if opened must also be ended
+ # Root span must be exported; chat span if opened must also be ended.
+ # Cancellation is tracked as cubepi.aborted (not StatusCode.ERROR),
+ # matching the agent path's contract.
roots = [s for s in exporter.spans if s.name == "invoke_agent"]
assert len(roots) == 1
+ attrs = dict(roots[0].attributes or {})
+ assert attrs.get("cubepi.aborted") is True
+ assert attrs.get("error.type") == "cubepi.aborted"
@pytest.mark.asyncio