Skip to content

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

fix(tracing): surface silent producer failures from stream.result()

xf gong1c915f50a5e0
diff --git a/cubepi/tracing/tracer.py b/cubepi/tracing/tracer.py
index 2228efd..b47272c 100644
--- a/cubepi/tracing/tracer.py
+++ b/cubepi/tracing/tracer.py
@@ -123,12 +123,17 @@ class _OneShotSession:
# ``await stream.result()`` pattern in cubepi.agent.loop.
try:
await stream.result()
- except Exception:
- # The producer's exception was already surfaced via the
- # error event above (or the listener has recorded the error
- # on the chat span). Swallow here so we report it uniformly
- # via ``error_msg``.
- pass
+ except Exception as result_exc:
+ # If we already captured an error event, the producer's
+ # exception is just the same failure expressed differently —
+ # swallow it and report uniformly via ``error_msg``. But if
+ # we got here on the success path (``done`` event consumed),
+ # the producer raised AFTER signalling done without emitting
+ # an error event, e.g. a custom provider bug. Surface that
+ # exception so the run is reported as failed instead of
+ # returning empty text.
+ if error_msg is None:
+ error_msg = str(result_exc) or "oneshot producer failed"
if error_msg is not None:
raise RuntimeError(error_msg)
text = "".join(parts)
diff --git a/tests/tracing/test_oneshot.py b/tests/tracing/test_oneshot.py
index 78b30c5..2d0f52c 100644
--- a/tests/tracing/test_oneshot.py
+++ b/tests/tracing/test_oneshot.py
@@ -234,6 +234,48 @@ async def test_oneshot_generate_error_event_raises_and_marks_root() -> None:
assert attrs.get("error.type") == "RuntimeError"
+async def test_oneshot_propagates_silent_producer_failure() -> None:
+ """If the producer task fails AFTER emitting done (without an explicit
+ error event), generate() must still raise — not return empty text and
+ leave the root span marked successful."""
+ from opentelemetry.trace import StatusCode
+ from unittest.mock import AsyncMock, MagicMock, patch
+
+ provider = FauxProvider()
+ tracer, exporter = _make_tracer()
+
+ # Build a stream that emits done normally but whose result() raises
+ fake_stream = MagicMock()
+
+ async def _events():
+ yield MagicMock(type="done", delta=None, error_message=None)
+
+ fake_stream.__aiter__ = lambda self: _events()
+
+ async def failing_result():
+ raise RuntimeError("producer crashed after done")
+
+ fake_stream.result = failing_result
+
+ with patch.object(provider, "stream", new=AsyncMock(return_value=fake_stream)):
+ with pytest.raises(RuntimeError, match="producer crashed"):
+ async with tracer.oneshot(provider=provider, model=MODEL) as session:
+ await session.generate(
+ system="sys",
+ messages=[UserMessage(content=[TextContent(text="q")])],
+ 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 must be marked ERROR (not silently successful)
+ assert roots[0].status.status_code == StatusCode.ERROR
+
+
@pytest.mark.asyncio
async def test_oneshot_subscribe_failure_raises_and_ends_root_span() -> None:
"""If provider subscription raises, oneshot re-raises and ends the root span."""