Skip to content

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

fix(tracing): degenerate compaction config falls back to first-call wins

xf gongf08923508521
diff --git a/cubepi/tracing/recorder.py b/cubepi/tracing/recorder.py
index eb2f249..6c44d1e 100644
--- a/cubepi/tracing/recorder.py
+++ b/cubepi/tracing/recorder.py
@@ -265,6 +265,24 @@ class Recorder:
# the middleware shares the agent's main provider instance
# ("reuse the client, swap the model"), since both calls
# would arrive on the same listener.
+ #
+ # Degenerate case: if a middleware declares the SAME (provider,
+ # model) as the agent's main, model-based gating cannot tell
+ # the two calls apart — skipping attribution for that key
+ # would also skip the agent's own request and leave the root
+ # span with the placeholder ``cubepi`` provider name (codex
+ # round-N). Exclude those keys from the extra set so the
+ # degenerate config falls back to the original first-call-wins
+ # behaviour. The configuration is self-defeating anyway —
+ # compaction with the same model gives no cost / context
+ # benefit — but the recorder still produces a usable trace.
+ agent_state = getattr(agent, "_state", None)
+ agent_model = getattr(agent_state, "model", None) if agent_state else None
+ agent_key: tuple[str, str] | None = (
+ (agent_model.provider, agent_model.id)
+ if agent_model is not None
+ else None
+ )
seen: set[int] = {id(provider)} if provider is not None else set()
for mw in getattr(agent, "_middleware", []) or []:
try:
@@ -272,7 +290,9 @@ class Recorder:
except Exception:
extra = []
for p, m in extra:
- self._extra_call_models.add((m.provider, m.id))
+ key = (m.provider, m.id)
+ if key != agent_key:
+ self._extra_call_models.add(key)
if id(p) in seen:
continue
seen.add(id(p))
diff --git a/tests/tracing/test_recorder.py b/tests/tracing/test_recorder.py
index 8df1113..586eebc 100644
--- a/tests/tracing/test_recorder.py
+++ b/tests/tracing/test_recorder.py
@@ -653,6 +653,47 @@ class TestMiddlewareProviders:
assert list(Middleware().extra_llm_calls()) == []
+ async def test_degenerate_same_model_falls_back_to_first_call_wins(self):
+ # Edge case: middleware declares the same (provider, model) as the
+ # agent — model-based gating would otherwise skip root attribution
+ # for the agent's own call too and leave the root span with the
+ # placeholder ``cubepi`` provider. The recorder must exclude these
+ # degenerate keys from the extra set so the run still gets a
+ # concrete root attribution (even if it ends up reflecting whichever
+ # call fired first).
+ from cubepi.middleware.base import Middleware
+
+ class _SameModelMiddleware(Middleware):
+ def __init__(self, provider, model):
+ self._p = provider
+ self._m = model
+
+ def extra_llm_calls(self):
+ return [(self._p, self._m)]
+
+ provider = FauxProvider()
+ agent = Agent(
+ provider=provider,
+ model=MODEL,
+ system_prompt="test",
+ middleware=[_SameModelMiddleware(provider, MODEL)],
+ )
+ exporter = InMemoryExporter()
+ tracer = Tracer(
+ service_name="test-svc",
+ agent_name="test-agent",
+ exporters=[exporter],
+ )
+ tracer.attach(agent)
+ provider.append_responses([faux_assistant_message("ok")])
+ await agent.prompt("x")
+ await agent.wait_for_idle()
+ await tracer.shutdown()
+
+ root = [s for s in exporter.spans if s.name.startswith("invoke_agent")][0]
+ # Root must end up with a real provider name, not the cubepi placeholder.
+ assert _attrs(root)["gen_ai.provider.name"] != "cubepi"
+
async def test_middleware_extra_llm_calls_raising_is_swallowed(self):
# If a middleware's ``extra_llm_calls()`` raises during attach the
# recorder must keep going — the agent's own provider is the