Skip to content

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

fix(tracing): drop orphaned cubepi.compaction.summarize wrapper span

xf gong7fb8d0516fe5
diff --git a/cubepi/middleware/compaction/summarizer.py b/cubepi/middleware/compaction/summarizer.py
index fdc1440..0e6aeac 100644
--- a/cubepi/middleware/compaction/summarizer.py
+++ b/cubepi/middleware/compaction/summarizer.py
@@ -1,7 +1,6 @@
from __future__ import annotations
import asyncio
-import contextlib
from cubepi.middleware.compaction.state import CompactionState, message_refs
from cubepi.providers.base import (
@@ -15,47 +14,6 @@ from cubepi.providers.base import (
)
-def _compaction_summary_span(message_count: int):
- """Open a parent span for the summarizer's LLM call.
-
- Without this wrapper the summarizer's chat span — emitted automatically
- once :class:`cubepi.tracing.Recorder` is wired to the summary provider —
- sits flat under ``cubepi.turn`` and is hard to tell apart from the
- agent's own chat span. The wrapping name makes the role obvious and
- tags it with the number of messages being summarised.
-
- Resolves the tracer via ``cubepi.mcp._tracing._get_tracer`` so the span
- routes through whichever :class:`cubepi.tracing.Tracer` is currently
- attached to the running agent. Without that lookup the OTel global
- provider is a no-op unless the user called ``set_tracer_provider``,
- which cubepi deliberately doesn't do. Falls back to a no-op context
- manager if neither OpenTelemetry nor an attached Tracer is available.
- """
- try:
- from cubepi.mcp._tracing import _get_tracer
- except ImportError:
- return contextlib.nullcontext()
-
- try:
- tracer = _get_tracer("cubepi.middleware.compaction")
- except Exception:
- return contextlib.nullcontext()
-
- cm = tracer.start_as_current_span("cubepi.compaction.summarize")
-
- @contextlib.contextmanager
- def _wrapped():
- with cm as span:
- try:
- if span is not None:
- span.set_attribute("cubepi.compaction.message_count", message_count)
- except Exception:
- pass
- yield span
-
- return _wrapped()
-
-
SUMMARIZER_SYSTEM_PROMPT = """\
You compress a chat transcript into a brief, faithful narrative for an AI assistant
that is continuing the conversation. Rules:
@@ -107,22 +65,19 @@ async def summarize(
if existing and existing.summary:
system_prompt += "\n\n" + EXISTING_SUMMARY_SUFFIX.format(prev=existing.summary)
- with _compaction_summary_span(len(messages_to_summarize)):
- response = await provider.generate(
- model=model,
- messages=[
- UserMessage(
- content=[
- TextContent(text=_format_transcript(messages_to_summarize))
- ]
- )
- ],
- system_prompt=system_prompt,
- options=StreamOptions(signal=abort_signal),
- max_output_tokens=max_summary_tokens,
- temperature=0.0,
- thinking="off",
- )
+ response = await provider.generate(
+ model=model,
+ messages=[
+ UserMessage(
+ content=[TextContent(text=_format_transcript(messages_to_summarize))]
+ )
+ ],
+ system_prompt=system_prompt,
+ options=StreamOptions(signal=abort_signal),
+ max_output_tokens=max_summary_tokens,
+ temperature=0.0,
+ thinking="off",
+ )
text = "".join(
block.text for block in response.content if isinstance(block, TextContent)
diff --git a/tests/tracing/test_recorder.py b/tests/tracing/test_recorder.py
index 2175ad4..a162518 100644
--- a/tests/tracing/test_recorder.py
+++ b/tests/tracing/test_recorder.py
@@ -554,16 +554,17 @@ class TestMiddlewareProviders:
await tracer.shutdown()
chats = [s for s in exporter.spans if s.name.startswith("chat ")]
- # One chat from the summarizer, one from the agent's main turn.
+ # One chat from the summarizer, one from the agent's main turn —
+ # both must be present so the run trace shows the summarizer call,
+ # not just the agent's own chat.
chat_models = sorted(_attrs(c).get("gen_ai.request.model") for c in chats)
assert chat_models == ["faux-1", "summary-1"], chat_models
- # The C-step parent span wraps the summarizer's chat.
- summary_spans = [
- s for s in exporter.spans if s.name == "cubepi.compaction.summarize"
- ]
- assert len(summary_spans) == 1
- assert _attrs(summary_spans[0])["cubepi.compaction.message_count"] >= 1
+ # Both chat spans share the agent run's trace_id — the summarizer
+ # call must be part of the same trace, not a sibling root.
+ turn = [s for s in exporter.spans if s.name == "cubepi.turn"][0]
+ for chat in chats:
+ assert chat.context.trace_id == turn.context.trace_id
class TestSafeToolName: