Skip to content

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

feat(tracing): emit cubepi.agent.fork_once span

xf gong9f2338af1a55
diff --git a/cubepi/agent/agent.py b/cubepi/agent/agent.py
index ce93234..2a58e75 100644
--- a/cubepi/agent/agent.py
+++ b/cubepi/agent/agent.py
@@ -517,10 +517,26 @@ class Agent(Generic[TMessage]):
)
def _fork_once_span(self, *, src_thread_id: str, after_run_id: str):
- """Replaced by a real OTel span in Task 34. Placeholder no-op."""
- from contextlib import nullcontext
+ """Emit a `cubepi.agent.fork_once` OTel span around the probe run.
- return nullcontext()
+ The opentelemetry dependency is part of the optional `tracing` extra;
+ when it isn't installed we fall back to a no-op nullcontext so the
+ agent still works.
+ """
+ try:
+ from opentelemetry import trace
+ except ImportError:
+ from contextlib import nullcontext
+
+ return nullcontext()
+ tracer = trace.get_tracer("cubepi.agent")
+ return tracer.start_as_current_span(
+ "cubepi.agent.fork_once",
+ attributes={
+ "cubepi.fork.src_thread_id": src_thread_id,
+ "cubepi.fork.after_run_id": after_run_id,
+ },
+ )
async def resume(self) -> None:
# Same fail-fast pattern as prompt(): lock.locked() is the atomic gate.
diff --git a/tests/tracing/conftest.py b/tests/tracing/conftest.py
new file mode 100644
index 0000000..ce96681
--- /dev/null
+++ b/tests/tracing/conftest.py
@@ -0,0 +1,35 @@
+from __future__ import annotations
+
+import pytest
+
+
+def in_memory_exporter():
+ """Attach a fresh InMemorySpanExporter to the global TracerProvider for
+ the duration of one test.
+
+ OTel forbids replacing the global TracerProvider once set, so we attach
+ a fresh exporter to whatever provider is already installed (installing
+ one if none is). Returns the exporter so tests can call
+ `get_finished_spans()`.
+
+ Skips the test if `opentelemetry` isn't installed (it's part of the
+ optional `tracing` extra).
+ """
+ pytest.importorskip("opentelemetry")
+
+ from opentelemetry import trace
+ from opentelemetry.sdk.trace import TracerProvider
+ from opentelemetry.sdk.trace.export import SimpleSpanProcessor
+ from opentelemetry.sdk.trace.export.in_memory_span_exporter import (
+ InMemorySpanExporter,
+ )
+
+ current = trace.get_tracer_provider()
+ if not isinstance(current, TracerProvider):
+ current = TracerProvider()
+ trace.set_tracer_provider(current)
+ exporter = InMemorySpanExporter()
+ current.add_span_processor(SimpleSpanProcessor(exporter))
+ yield exporter
+ exporter.clear()
diff --git a/tests/tracing/test_fork_once_span.py b/tests/tracing/test_fork_once_span.py
new file mode 100644
index 0000000..8055c48
--- /dev/null
+++ b/tests/tracing/test_fork_once_span.py
@@ -0,0 +1,39 @@
+import pytest
+
+from cubepi.agent.agent import Agent
+from cubepi.checkpointer.memory import MemoryCheckpointer
+from cubepi.providers.base import AssistantMessage, TextContent
+from cubepi.providers.faux import FauxProvider
+
+
+def _ok_faux() -> FauxProvider:
+ p = FauxProvider()
+ p.set_responses(
+ [AssistantMessage(content=[TextContent(text="ok")], stop_reason="end_turn")]
+ )
+ return p
+
+
+async def test_fork_once_emits_named_span(in_memory_exporter):
+ cp = MemoryCheckpointer()
+ a = Agent(
+ model=_ok_faux().model("faux-model"),
+ checkpointer=cp,
+ thread_id="src",
+ )
+ await a.prompt("hello", run_id="R1")
+ # Fresh agent for the fork_once probe (mirrors existing fork_once tests).
+ a2 = Agent(
+ model=_ok_faux().model("faux-model"),
+ checkpointer=cp,
+ thread_id="src",
+ )
+ await a2.fork_once("src", "follow up?", after_run_id="R1")
+ spans = in_memory_exporter.get_finished_spans()
+ names = [s.name for s in spans]
+ assert "cubepi.agent.fork_once" in names
+ span = next(s for s in spans if s.name == "cubepi.agent.fork_once")
+ attrs = dict(span.attributes)
+ assert attrs["cubepi.fork.src_thread_id"] == "src"
+ assert attrs["cubepi.fork.after_run_id"] == "R1"