Skip to content

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

fix(providers): generate() failover on error AssistantMessage; note tracing gap

xf gong4d064110db36
diff --git a/cubepi/providers/fallback.py b/cubepi/providers/fallback.py
index ba4197c..6ad6232 100644
--- a/cubepi/providers/fallback.py
+++ b/cubepi/providers/fallback.py
@@ -46,10 +46,18 @@ class FallbackBoundModel:
chain[0] is the primary model. On a trigger_errors exception or a first-event
error from stream(), the next model in the chain is tried transparently.
- Mid-stream errors (after the first non-error event) are forwarded as-is.
+ For generate(), an error AssistantMessage (stop_reason="error") also triggers
+ failover. Mid-stream errors (after the first non-error event) are forwarded
+ as-is.
provider and spec proxy chain[0] so tracing/billing code that reads
agent._model.provider / agent._model.spec continues to work unchanged.
+
+ Known limitation: Tracer.attach() and Meter.attach() subscribe only to
+ chain[0].provider. Successful fallback calls (chain[1..]) are invisible to
+ provider-level observability (chat spans, token/cost metrics). Tracked as a
+ follow-up: update recorder.py and meter.py to iterate chain and subscribe
+ to every unique BaseProvider.
"""
chain: tuple[BoundModel, ...]
@@ -195,7 +203,7 @@ class FallbackBoundModel:
next_bound = self.chain[attempt] if attempt < len(self.chain) else None
try:
- return await bound.generate(
+ result = await bound.generate(
messages,
system_prompt=system_prompt,
tools=tools,
@@ -212,6 +220,13 @@ class FallbackBoundModel:
except Exception:
raise
+ if result.stop_reason == "error":
+ last_error = result.error_message or "generate error"
+ await self._notify(bound, next_bound, last_error, attempt)
+ continue
+
+ return result
+
raise ProviderUnavailable(
f"all providers exhausted; last error: {last_error!r}"
)
diff --git a/tests/providers/test_fallback.py b/tests/providers/test_fallback.py
index 38ae7d9..5556b36 100644
--- a/tests/providers/test_fallback.py
+++ b/tests/providers/test_fallback.py
@@ -356,3 +356,19 @@ async def test_generate_all_exhausted_raises_provider_unavailable() -> None:
with pytest.raises(ProviderUnavailable, match="all providers exhausted"):
await fbm.generate(_messages())
+
+
+async def test_generate_error_assistant_message_triggers_failover() -> None:
+ """generate() — primary returns AssistantMessage(stop_reason="error") → failover."""
+ # FauxProvider with no queued responses returns an error AssistantMessage.
+ primary_prov = FauxProvider(provider_id="primary")
+ primary = primary_prov.model("model-1")
+ fallback = _faux("fallback", "generated via fallback")
+
+ fbm = FallbackBoundModel(chain=(primary, fallback))
+
+ result = await fbm.generate(_messages())
+
+ assert result.provider_id == "fallback"
+ assert result.stop_reason != "error"