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(auth): handle review follow-ups

cyh117672a34c0b
diff --git a/core/agent_auth_service.py b/core/agent_auth_service.py
index 3b320290a..f0a1d52ba 100644
--- a/core/agent_auth_service.py
+++ b/core/agent_auth_service.py
@@ -27,6 +27,7 @@ URL_RE = re.compile(r"https?://\S+")
CODEX_DEVICE_CODE_RE = re.compile(r"\b[A-Z0-9]{4}(?:-[A-Z0-9]{4,})+\b")
OPENCODE_API_KEY_PROMPT_RE = re.compile(r"enteryourapikey", re.IGNORECASE)
OPENCODE_CREDENTIAL_COUNT_RE = re.compile(r"\b(\d+)\s+credential(?:s)?\b", re.IGNORECASE)
+OPENCODE_DIRECT_API_KEY_RE = re.compile(r"^sk-[A-Za-z0-9][A-Za-z0-9._-]{8,}$")
CLAUDE_LOGIN_METHODS = {"claudeai", "console"}
OPENCODE_DIRECT_SETUP_URLS = {"opencode": "https://opencode.ai/auth"}
@@ -514,6 +515,7 @@ class AgentAuthService:
and opencode_flow.backend == "opencode"
and opencode_flow.initiator_user_id == context.user_id
and opencode_flow.awaiting_code
+ and self._looks_like_direct_opencode_api_key(message)
):
await self.submit_code(context, message.strip(), backend_hint="opencode")
return True
@@ -1099,6 +1101,9 @@ class AgentAuthService:
return None
return authorization_code, state
+ def _looks_like_direct_opencode_api_key(self, text: str) -> bool:
+ return OPENCODE_DIRECT_API_KEY_RE.fullmatch(text.strip()) is not None
+
def _normalize_claude_login_method(self, value: str | None) -> str | None:
if value is None:
return None
diff --git a/modules/agents/claude_agent.py b/modules/agents/claude_agent.py
index bca59d7be..08d961cb7 100644
--- a/modules/agents/claude_agent.py
+++ b/modules/agents/claude_agent.py
@@ -200,6 +200,7 @@ class ClaudeAgent(BaseAgent):
composite_key: str,
*,
current_receiver_task: asyncio.Task | None = None,
+ preserve_pending_request_state: bool = False,
) -> None:
"""Drop Claude runtime state without canceling the current receiver task."""
@@ -229,8 +230,9 @@ class ClaudeAgent(BaseAgent):
self._last_assistant_text.pop(composite_key, None)
self._pending_assistant_message.pop(composite_key, None)
- self._pending_reactions.pop(composite_key, None)
- self._pending_requests.pop(composite_key, None)
+ if not preserve_pending_request_state:
+ self._pending_reactions.pop(composite_key, None)
+ self._pending_requests.pop(composite_key, None)
async def handle_stop(self, request: AgentRequest) -> bool:
composite_key = request.composite_session_id
@@ -326,10 +328,7 @@ class ClaudeAgent(BaseAgent):
"error" if self._is_auth_failure_assistant_message(message) else "",
assistant_text,
):
- pending_request = self._pop_pending_request(composite_key)
- self._discard_pending_reaction(composite_key)
- if pending_request:
- await self._remove_ack_reaction(pending_request)
+ await self._cleanup_auth_failure_request(composite_key)
self._last_assistant_text.pop(composite_key, None)
self._pending_assistant_message.pop(composite_key, None)
continue
@@ -395,6 +394,7 @@ class ClaudeAgent(BaseAgent):
getattr(message, "subtype", "") or "",
formatted_message,
):
+ await self._cleanup_auth_failure_request(composite_key)
continue
if formatted_message and formatted_message.strip():
await self.controller.emit_agent_message(
@@ -421,10 +421,7 @@ class ClaudeAgent(BaseAgent):
getattr(message, "subtype", "") or "",
result_text,
):
- pending_request = self._pop_pending_request(composite_key)
- self._discard_pending_reaction(composite_key)
- if pending_request:
- await self._remove_ack_reaction(pending_request)
+ await self._cleanup_auth_failure_request(composite_key)
self._last_assistant_text.pop(composite_key, None)
self._pending_assistant_message.pop(composite_key, None)
continue
@@ -586,6 +583,12 @@ class ClaudeAgent(BaseAgent):
for request in requests:
await self._remove_ack_reaction(request)
+ async def _cleanup_auth_failure_request(self, composite_key: str) -> None:
+ pending_request = self._pop_pending_request(composite_key)
+ self._discard_pending_reaction(composite_key)
+ if pending_request is not None:
+ await self._remove_ack_reaction(pending_request)
+
def get_relative_path(self, abs_path: str, context: Optional[MessageContext] = None) -> str:
"""Convert absolute path to relative path from working directory."""
try:
@@ -662,7 +665,11 @@ class ClaudeAgent(BaseAgent):
f"❌ Claude error: {text}",
)
if handled:
- await self._cleanup_runtime_session(composite_key, current_receiver_task=asyncio.current_task())
+ await self._cleanup_runtime_session(
+ composite_key,
+ current_receiver_task=asyncio.current_task(),
+ preserve_pending_request_state=True,
+ )
return handled
def _is_auth_failure_assistant_message(self, message) -> bool:
diff --git a/tests/test_agent_auth_service.py b/tests/test_agent_auth_service.py
index 1a5769a39..12ee2278f 100644
--- a/tests/test_agent_auth_service.py
+++ b/tests/test_agent_auth_service.py
@@ -601,6 +601,33 @@ class AgentAuthServiceTests(unittest.IsolatedAsyncioTestCase):
self.assertTrue(consumed)
service.submit_code.assert_awaited_once_with(context, "sk-opencode-secret", backend_hint="opencode")
+ async def test_maybe_consume_setup_reply_ignores_noncredential_opencode_plain_text(self):
+ controller = _StubController()
+ service = AgentAuthService(controller)
+ context = MessageContext(user_id="U1", channel_id="C1")
+ done_task = asyncio.create_task(asyncio.sleep(0))
+ await done_task
+ flow = AgentAuthFlow(
+ flow_id="flow-opencode-ignore",
+ backend="opencode",
+ settings_key="C1",
+ initiator_user_id="U1",
+ context=context,
+ process=SimpleNamespace(returncode=None),
+ reader_task=done_task,
+ waiter_task=done_task,
+ pty_master_fd=11,
+ awaiting_code=True,
+ provider="opencode",
+ )
+ service._flows[flow.flow_key] = flow
+ service.submit_code = AsyncMock()
+
+ consumed = await service.maybe_consume_setup_reply(context, "hello world")
+
+ self.assertFalse(consumed)
+ service.submit_code.assert_not_awaited()
+
async def test_maybe_consume_setup_reply_ignores_plain_text_without_callback_shape(self):
controller = _StubController()
service = AgentAuthService(controller)
diff --git a/tests/test_claude_agent_sessions.py b/tests/test_claude_agent_sessions.py
index 4a40f54a1..d5c071c57 100644
--- a/tests/test_claude_agent_sessions.py
+++ b/tests/test_claude_agent_sessions.py
@@ -197,6 +197,7 @@ class ClaudeAgentSessionTests(unittest.IsolatedAsyncioTestCase):
controller.emit_agent_message = AsyncMock()
agent = ClaudeAgent(controller)
agent._clear_pending_reactions = AsyncMock()
+ agent._remove_ack_reaction = AsyncMock()
agent._extract_text_blocks = lambda message, context: (
'Failed to authenticate. API Error: 401 {"type":"error","error":{"type":"authentication_error",'
'"message":"Invalid bearer token"}}'
@@ -206,6 +207,9 @@ class ClaudeAgentSessionTests(unittest.IsolatedAsyncioTestCase):
current_task = asyncio.current_task()
controller.receiver_tasks[composite_key] = current_task
controller.claude_sessions[composite_key] = _StubClient()
+ pending_request = SimpleNamespace()
+ agent._pending_requests[composite_key] = [pending_request]
+ agent._pending_reactions[composite_key] = [("m1", ":eyes:")]
assistant_message = type(
"AssistantMessage",
@@ -228,8 +232,11 @@ class ClaudeAgentSessionTests(unittest.IsolatedAsyncioTestCase):
controller.agent_auth_service.maybe_emit_auth_recovery_message.assert_awaited_once()
controller.session_handler.cleanup_session.assert_not_awaited()
+ agent._remove_ack_reaction.assert_awaited_once_with(pending_request)
self.assertNotIn(composite_key, controller.receiver_tasks)
self.assertNotIn(composite_key, controller.claude_sessions)
+ self.assertNotIn(composite_key, agent._pending_requests)
+ self.assertNotIn(composite_key, agent._pending_reactions)
async def test_handle_auth_failure_result_requires_explicit_error_subtype(self):
controller = _StubController()