Skip to content

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

refactor(tracing): extract collect_agent_providers helper for Recorder/Meter

xf gongb198fc0b92a1
diff --git a/cubepi/providers/__init__.py b/cubepi/providers/__init__.py
index 367cf45..7ebf909 100644
--- a/cubepi/providers/__init__.py
+++ b/cubepi/providers/__init__.py
@@ -27,6 +27,8 @@ from cubepi.providers.base import (
Usage,
UserMessage,
adjust_max_tokens_for_thinking,
+ chain_providers,
+ collect_agent_providers,
)
from cubepi.providers.faux import (
FauxProvider,
@@ -98,7 +100,9 @@ __all__ = [
"Usage",
"UserMessage",
"adjust_max_tokens_for_thinking",
+ "chain_providers",
"clamp_thinking_level",
+ "collect_agent_providers",
"faux_assistant_message",
"faux_text",
"faux_thinking",
diff --git a/cubepi/providers/base.py b/cubepi/providers/base.py
index 8f1eb68..029794f 100644
--- a/cubepi/providers/base.py
+++ b/cubepi/providers/base.py
@@ -885,3 +885,21 @@ def chain_providers(model: object) -> list["BaseProvider"]:
if isinstance(provider, BaseProvider):
return [provider]
return []
+
+
+def collect_agent_providers(agent: Any) -> list["BaseProvider"]:
+ """Return the unique BaseProvider instances backing an agent.
+
+ Walks ``agent._model`` via :func:`chain_providers` and falls back to a
+ public ``provider`` attribute on the agent for legacy code paths. Used
+ by both :meth:`cubepi.tracing.recorder.Recorder.attach` and
+ :meth:`cubepi.tracing.meter.Meter.attach` to dedupe the prelude that
+ finds providers to subscribe to.
+ """
+ model = getattr(agent, "_model", None)
+ providers = chain_providers(model)
+ if not providers:
+ legacy = getattr(agent, "provider", None)
+ if isinstance(legacy, BaseProvider):
+ providers = [legacy]
+ return providers
diff --git a/cubepi/tracing/meter.py b/cubepi/tracing/meter.py
index 38f5a50..04ad5b1 100644
--- a/cubepi/tracing/meter.py
+++ b/cubepi/tracing/meter.py
@@ -152,7 +152,7 @@ class Meter:
overwrote the first agent's ``_chat_open_ns`` / ``_chat_attrs``
before its response landed).
"""
- from cubepi.providers.base import BaseProvider, chain_providers
+ from cubepi.providers.base import BaseProvider, collect_agent_providers
state = _MeterState()
@@ -174,18 +174,16 @@ class Meter:
self._handle_provider_response(state, body, model, exc)
unsub_agent = agent.subscribe(_on_agent_event)
- agent_model = getattr(agent, "_model", None)
- # When the bound model is a FallbackBoundModel, subscribe to every
- # unique provider in the chain so post-failover token usage / cost
- # / cache-hit metrics are emitted for chain[1..] calls the same way
- # as chain[0]. Non-fallback models yield a single-entry list.
- agent_providers: list[Any] = chain_providers(agent_model)
- if not agent_providers:
- legacy = getattr(agent, "provider", None)
- if legacy is not None:
- agent_providers = [legacy]
+ # When the bound model is a FallbackBoundModel, ``collect_agent_providers``
+ # returns every unique BaseProvider in the chain so post-failover token
+ # usage / cost / cache-hit metrics are emitted for chain[1..] calls the
+ # same way as chain[0]. Non-fallback models yield a single-entry list.
+ agent_providers: list[BaseProvider] = collect_agent_providers(agent)
detachers: list[Callable[[], None]] = []
for provider in agent_providers:
+ # Defensive guard: collect_agent_providers already filters to
+ # BaseProvider, but the isinstance check is cheap and protects
+ # against a future helper change that loosens the contract.
if isinstance(provider, BaseProvider):
detachers.append(provider.subscribe_request(_on_request))
detachers.append(provider.subscribe_chunk(_on_chunk))
diff --git a/cubepi/tracing/recorder.py b/cubepi/tracing/recorder.py
index e118707..ddcac87 100644
--- a/cubepi/tracing/recorder.py
+++ b/cubepi/tracing/recorder.py
@@ -47,7 +47,7 @@ from cubepi.providers.base import (
ToolCall,
ToolResultMessage,
UserMessage,
- chain_providers,
+ collect_agent_providers,
)
from cubepi.tracing.content import (
messages_to_semconv,
@@ -236,22 +236,13 @@ class Recorder:
def attach(self, agent: "Agent") -> Callable[[], None]:
self._agent = agent
unsub_agent = agent.subscribe(self._on_agent_event)
- # ``Agent`` holds the bound model on ``_model``; the provider is
- # reached via ``_model.provider``. Fall back to a public ``provider``
- # alias should one be added later.
- agent_model = getattr(agent, "_model", None)
- # When the bound model is a FallbackBoundModel, subscribe to every
- # unique provider in the chain so post-failover provider events
- # (chat spans, token usage, errors) land in the trace tree like
- # primary-leg events do. Non-fallback models yield a single-entry
- # list with the only provider.
- agent_providers: list[Any] = chain_providers(agent_model)
- if not agent_providers:
- # Legacy / unusual agents may expose ``provider`` directly on
- # the Agent instead of via ``_model``; preserve the fallback.
- legacy = getattr(agent, "provider", None)
- if legacy is not None:
- agent_providers = [legacy]
+ # When the bound model is a FallbackBoundModel, ``collect_agent_providers``
+ # returns every unique BaseProvider in the chain so post-failover provider
+ # events (chat spans, token usage, errors) land in the trace tree like
+ # primary-leg events do. Non-fallback models yield a single-entry list.
+ # Legacy agents that expose ``provider`` directly (instead of via
+ # ``_model``) are handled inside the helper.
+ agent_providers: list[BaseProvider] = collect_agent_providers(agent)
provider = agent_providers[0] if agent_providers else None
provider_detachers: list[Callable[[], None]] = []