Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

fix(claude): preserve exception failure provenance

cyha98893c55e75
diff --git a/core/backend_failure.py b/core/backend_failure.py
index 9f13d63d1..5143c2fc5 100644
--- a/core/backend_failure.py
+++ b/core/backend_failure.py
@@ -57,39 +57,28 @@ def _failure_identity(
return uuid.uuid4().hex
-async def emit_backend_failure(
- controller: Any,
+def backend_failure_notification_output(
context: Any,
backend: str,
- diagnostic: str,
*,
- display_text: str | None = None,
request: Any = None,
output: MessageOutput | None = None,
failure_id: str | None = None,
-) -> bool:
- """Notify once, then settle one terminal backend failure silently.
-
- Backend adapters own structured failure recognition. This helper owns the
- shared representation and keeps visible delivery separate from lifecycle
- settlement. The return value is true when auth recovery supplied the visible
- notification.
- """
+) -> MessageOutput:
+ """Build the durable, non-settling visible half of a backend failure."""
backend_name = str(backend or "backend").strip() or "backend"
- error = str(diagnostic or "").strip() or f"{backend_name} backend failed"
- visible = str(display_text or "").strip() or error
terminal = _terminal_output(request, output)
identity = _failure_identity(context, request, failure_id)
- notify_metadata = dict(terminal.metadata)
- notify_metadata.update(
+ metadata = dict(terminal.metadata)
+ metadata.update(
{
"backend": backend_name,
"event": BACKEND_FAILURE_EVENT,
"failure_id": identity,
}
)
- notification = MessageOutput(
+ return MessageOutput(
completes_turn=False,
completes_run=False,
detached=terminal.detached,
@@ -97,7 +86,39 @@ async def emit_backend_failure(
activity_id=terminal.activity_id,
causation_id=terminal.causation_id,
run_id=terminal.run_id,
- metadata=notify_metadata,
+ metadata=metadata,
+ )
+
+
+async def emit_backend_failure(
+ controller: Any,
+ context: Any,
+ backend: str,
+ diagnostic: str,
+ *,
+ display_text: str | None = None,
+ request: Any = None,
+ output: MessageOutput | None = None,
+ failure_id: str | None = None,
+) -> bool:
+ """Notify once, then settle one terminal backend failure silently.
+
+ Backend adapters own structured failure recognition. This helper owns the
+ shared representation and keeps visible delivery separate from lifecycle
+ settlement. The return value is true when auth recovery supplied the visible
+ notification.
+ """
+
+ backend_name = str(backend or "backend").strip() or "backend"
+ error = str(diagnostic or "").strip() or f"{backend_name} backend failed"
+ visible = str(display_text or "").strip() or error
+ terminal = _terminal_output(request, output)
+ notification = backend_failure_notification_output(
+ context,
+ backend_name,
+ request=request,
+ output=terminal,
+ failure_id=failure_id,
)
async def settle_terminal_failure() -> None:
diff --git a/modules/agents/claude_agent.py b/modules/agents/claude_agent.py
index 296ccb7cd..757808a8d 100644
--- a/modules/agents/claude_agent.py
+++ b/modules/agents/claude_agent.py
@@ -5,7 +5,7 @@ import uuid
from typing import Callable, Optional
from core.agent_auth_service import classify_auth_error
-from core.backend_failure import emit_backend_failure
+from core.backend_failure import backend_failure_notification_output, emit_backend_failure
from core.message_output import MessageOutput, terminal_output_for, terminal_turn_output
from core.reply_enhancer import strip_silent_blocks
from core.session_activities import SessionActivity, activity_completion_output
@@ -190,7 +190,19 @@ class ClaudeAgent(BaseAgent):
try:
from core.message_mirror import persist_agent_message
- persist_agent_message(context, "notify", error_notify)
+ notification = backend_failure_notification_output(
+ context,
+ "claude",
+ request=request,
+ output=terminal_output_for(request),
+ )
+ persist_agent_message(
+ context,
+ "notify",
+ error_notify,
+ metadata=notification.metadata,
+ native_message_id=notification.idempotency_key,
+ )
except Exception:
logger.debug("claude: failed to persist terminal error row", exc_info=True)
# No async receiver result is coming. Auth recovery settles its
@@ -1019,7 +1031,19 @@ class ClaudeAgent(BaseAgent):
try:
from core.message_mirror import persist_agent_message
- persist_agent_message(context, "notify", error_notify)
+ notification = backend_failure_notification_output(
+ context,
+ "claude",
+ request=pending_request,
+ output=terminal_output_for(pending_request),
+ )
+ persist_agent_message(
+ context,
+ "notify",
+ error_notify,
+ metadata=notification.metadata,
+ native_message_id=notification.idempotency_key,
+ )
except Exception:
logger.debug("claude: failed to persist terminal receiver-error row", exc_info=True)
# A dead receiver is terminal. The HANDLED (auth) branch already
diff --git a/tests/test_backend_failure.py b/tests/test_backend_failure.py
index 94e733717..32ca7a9aa 100644
--- a/tests/test_backend_failure.py
+++ b/tests/test_backend_failure.py
@@ -118,6 +118,14 @@ class BackendFailureTests(unittest.IsolatedAsyncioTestCase):
notify_call.kwargs["output"].idempotency_key,
"backend-failure:turn-1",
)
+ self.assertEqual(
+ notify_call.kwargs["output"].metadata,
+ {
+ "backend": "codex",
+ "event": "backend_failure",
+ "failure_id": "turn-1",
+ },
+ )
self.assertEqual(
terminal_call,
call(
diff --git a/tests/test_claude_agent_sessions.py b/tests/test_claude_agent_sessions.py
index e1043e854..d6ea74fdc 100644
--- a/tests/test_claude_agent_sessions.py
+++ b/tests/test_claude_agent_sessions.py
@@ -836,12 +836,21 @@ class ClaudeAgentSessionTests(unittest.IsolatedAsyncioTestCase):
agent._pending_requests[runtime_key] = [queued_request]
agent._pending_reactions[runtime_key] = [("m2", ":eyes:")]
- await agent.handle_message(request)
+ with patch("core.message_mirror.persist_agent_message") as persist:
+ await agent.handle_message(request)
self.assertEqual(mark_idle_calls, [])
self.assertEqual(agent._pending_requests[runtime_key], [queued_request])
self.assertEqual(agent._pending_reactions[runtime_key], [("m2", ":eyes:")])
agent._remove_ack_reaction.assert_awaited_once_with(request)
+ persist.assert_called_once()
+ self.assertEqual(persist.call_args.args[1:], ("notify", "❌ Claude error: boom"))
+ self.assertEqual(persist.call_args.kwargs["metadata"]["backend"], "claude")
+ self.assertEqual(persist.call_args.kwargs["metadata"]["event"], "backend_failure")
+ self.assertTrue(persist.call_args.kwargs["metadata"]["failure_id"])
+ self.assertTrue(
+ persist.call_args.kwargs["native_message_id"].startswith("backend-failure:")
+ )
async def test_clear_sessions_cancels_receiver_tasks_for_cleared_session(self):
controller = _StubController()
@@ -1249,6 +1258,12 @@ class ClaudeAgentSessionTests(unittest.IsolatedAsyncioTestCase):
persist.assert_called_once()
self.assertEqual(persist.call_args.args[1], "notify")
self.assertEqual(persist.call_args.args[2], "❌ Claude error: Connection lost")
+ self.assertEqual(persist.call_args.kwargs["metadata"]["backend"], "claude")
+ self.assertEqual(persist.call_args.kwargs["metadata"]["event"], "backend_failure")
+ self.assertTrue(persist.call_args.kwargs["metadata"]["failure_id"])
+ self.assertTrue(
+ persist.call_args.kwargs["native_message_id"].startswith("backend-failure:")
+ )
async def test_receiver_buffer_error_persists_connection_lost_notify(self):
controller = _StubController()
@@ -1296,10 +1311,20 @@ class ClaudeAgentSessionTests(unittest.IsolatedAsyncioTestCase):
output=ANY,
terminal_error=terminal_error,
)
- persist.assert_called_once_with(
- context,
- "notify",
- "❌ Connection to Claude was lost. Please try your message again.",
+ persist.assert_called_once()
+ self.assertEqual(
+ persist.call_args.args,
+ (
+ context,
+ "notify",
+ "❌ Connection to Claude was lost. Please try your message again.",
+ ),
+ )
+ self.assertEqual(persist.call_args.kwargs["metadata"]["backend"], "claude")
+ self.assertEqual(persist.call_args.kwargs["metadata"]["event"], "backend_failure")
+ self.assertTrue(persist.call_args.kwargs["metadata"]["failure_id"])
+ self.assertTrue(
+ persist.call_args.kwargs["native_message_id"].startswith("backend-failure:")
)
async def test_result_auth_error_prefers_oauth_recovery_message(self):