CubeLoop — a Pythonic, async-native agent harness core designed for high performance, readability, and production-grade persistence.
Python agent-frameworkagent-harnessagent-runtimeagentic-aiai-agentsai-frameworkanthropicharnessharness-engineeringharness-frameworklanggraphlanggraph-alternativellmmcpopenaipi-agentpi-agent-corepythonpython-agent
fix(tracing): include every chain leg's spec in agent_keys to avoid root attribution suppression after failover
xf gong45c3b9daca66
diff --git a/cubepi/tracing/recorder.py b/cubepi/tracing/recorder.pyindex e4a6ef8..a16123a 100644--- a/cubepi/tracing/recorder.py+++ b/cubepi/tracing/recorder.py@@ -280,13 +280,31 @@ class Recorder:# 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_id, agent_model.id)- if agent_model is not None- else None- )+ #+ # FallbackBoundModel wrinkle: ``_state.model.spec`` only surfaces+ # chain[0] (the proxy property), so a middleware extra that+ # legitimately matches a fallback-leg spec (chain[1+]) would+ # poison ``_extra_call_models`` with the leg's key. Then on+ # actual failover — chain[0] fails before emission, chain[1]+ # fires — ``_on_provider_request`` would see the leg's key in+ # ``_extra_call_models`` and suppress root attribution, leaving+ # the run attributed to the ``cubepi`` placeholder. Collect+ # every chain leg's (provider_id, model_id) into ``agent_keys``+ # so middleware-extras that match any leg are correctly excluded.+ agent_keys: set[tuple[str, str]] = set()+ top_model = getattr(agent, "_model", None)+ chain = getattr(top_model, "chain", None)+ if chain is not None:+ for bm in chain:+ spec = bm.spec+ agent_keys.add((spec.provider_id, spec.id))+ else:+ agent_state = getattr(agent, "_state", None)+ agent_model = (+ getattr(agent_state, "model", None) if agent_state else None+ )+ if agent_model is not None:+ agent_keys.add((agent_model.provider_id, agent_model.id))# Pre-seed `seen` with every chain provider already subscribed# above so a middleware that reuses one of them isn't# double-subscribed.@@ -299,7 +317,7 @@ class Recorder:for model in extra:spec = model.speckey = (spec.provider_id, spec.id)- if key != agent_key:+ if key not in agent_keys:self._extra_call_models.add(key)provider = model.providerif id(provider) in seen:diff --git a/tests/tracing/test_recorder.py b/tests/tracing/test_recorder.pyindex eda6198..24623d2 100644--- a/tests/tracing/test_recorder.py+++ b/tests/tracing/test_recorder.py@@ -1570,3 +1570,57 @@ class TestFallbackChainCoverage:assert len(request_listeners) == 1, request_listenersawait tracer.shutdown()++ async def test_attach_excludes_every_chain_leg_key_from_extra_call_models(+ self,+ ):+ """Regression for the failover-first-call root attribution suppression+ bug. If a middleware declares an extra_llm_call whose ``.spec`` matches+ chain[1+] of a FallbackBoundModel, the recorder must NOT register that+ spec key in ``_extra_call_models`` — otherwise when chain[0] fails+ before emission and chain[1] fires for the agent's own request, the+ recorder treats it as a middleware-owned call and skips root+ attribution, leaving the run attributed to the ``cubepi`` placeholder.++ Build a chain ``[primary, secondary]`` + a stub middleware whose+ ``extra_llm_calls()`` returns a BoundModel using secondary's spec,+ attach, then verify secondary's spec key is absent from the recorder's+ ``_extra_call_models`` set.+ """+ from cubepi.providers.fallback import FallbackBoundModel++ primary = FauxProvider(provider_id="primary")+ secondary = FauxProvider(provider_id="secondary")+ primary_bound = primary.model("m-primary")+ secondary_bound = secondary.model("m-secondary")++ # Minimal duck-typed middleware: only ``extra_llm_calls()`` is read+ # by attach(). The middleware "extra" intentionally reuses+ # secondary's spec — a legitimate pattern when a middleware wants+ # to share the fallback's secondary model.+ class _FakeMiddleware:+ def extra_llm_calls(self) -> list[Any]:+ return [secondary_bound]++ chain_model = FallbackBoundModel(chain=(primary_bound, secondary_bound))+ agent = Agent(+ model=chain_model,+ system_prompt="t",+ middleware=[_FakeMiddleware()], # type: ignore[list-item]+ )+ exporter = InMemoryExporter()+ tracer = Tracer(service_name="t", agent_name="a", exporters=[exporter])+ tracer.attach(agent)++ recorder = _find_attached_recorder(primary)+ assert recorder is not None+ secondary_key = (secondary_bound.spec.provider_id, secondary_bound.spec.id)+ assert secondary_key not in recorder._extra_call_models, (+ "secondary leg's spec must NOT be in _extra_call_models — otherwise "+ "failover to chain[1] would suppress root attribution"+ )+ # Primary's key must also not be flagged as extra (it was never extra).+ primary_key = (primary_bound.spec.provider_id, primary_bound.spec.id)+ assert primary_key not in recorder._extra_call_models++ await tracer.shutdown()