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(ui): address FastAPI review regressions

cyhb0d14f8c5b87
diff --git a/tests/test_ui_api.py b/tests/test_ui_api.py
index 94b6f9576..2467cffb3 100644
--- a/tests/test_ui_api.py
+++ b/tests/test_ui_api.py
@@ -1,6 +1,8 @@
import asyncio
import json
+import time
from types import SimpleNamespace
+from unittest.mock import AsyncMock
import pytest
@@ -79,6 +81,44 @@ def test_opencode_options_closes_server_http_session(monkeypatch):
assert fake_manager.closed_loop is not None
+def test_sync_start_oauth_web_keeps_background_tasks_on_persistent_loop(monkeypatch):
+ async def _start_web_setup(backend, *, force_reset=True, provider_id=None):
+ async def _mark_completed():
+ await asyncio.sleep(0.01)
+ flow.state = "success"
+
+ task = asyncio.create_task(_mark_completed())
+ flow.waiter_task = task
+ return flow
+
+ flow = SimpleNamespace(
+ flow_id="flow-sync",
+ backend="codex",
+ state="awaiting_code",
+ url="https://auth.openai.com/codex/device",
+ device_code="ABCD-EFGH",
+ awaiting_code=False,
+ provider=None,
+ error=None,
+ waiter_task=None,
+ )
+ service = SimpleNamespace(start_web_setup=AsyncMock(side_effect=_start_web_setup))
+ monkeypatch.setattr(api, "_oauth_service", service)
+ monkeypatch.setattr(api, "_oauth_loop", None)
+ monkeypatch.setattr(api, "_oauth_loop_thread", None)
+
+ result = api.start_oauth_web("codex")
+
+ assert result["ok"] is True
+ assert result["flow_id"] == "flow-sync"
+ assert flow.waiter_task is not None
+ deadline = time.time() + 1
+ while flow.state != "success" and time.time() < deadline:
+ time.sleep(0.01)
+ assert flow.state == "success"
+ assert flow.waiter_task.done()
+
+
def test_detect_cli_prefers_claude_local(monkeypatch, tmp_path):
claude_path = tmp_path / ".claude" / "local" / "claude"
claude_path.parent.mkdir(parents=True, exist_ok=True)
diff --git a/tests/test_ui_remote_access_auth.py b/tests/test_ui_remote_access_auth.py
index dd10cbb96..5cea0d58f 100644
--- a/tests/test_ui_remote_access_auth.py
+++ b/tests/test_ui_remote_access_auth.py
@@ -466,6 +466,22 @@ def test_host_starting_with_127_but_not_ip_is_not_local_when_config_load_fails(m
assert response.get_json()["error"] == "remote_access_config_unavailable"
+def test_loopback_peer_with_arbitrary_host_is_not_local_when_config_load_fails(monkeypatch):
+ def fail_load():
+ raise ValueError("corrupt config")
+
+ monkeypatch.setattr(ui_server.V2Config, "load", fail_load)
+
+ response = app.test_client().get(
+ "/dashboard",
+ base_url="https://attacker.example",
+ follow_redirects=False,
+ )
+
+ assert response.status_code == 503
+ assert response.get_json()["error"] == "remote_access_config_unavailable"
+
+
def test_spoofed_loopback_host_is_not_local_when_peer_is_remote(monkeypatch):
def fail_load():
raise ValueError("corrupt config")
diff --git a/vibe/api.py b/vibe/api.py
index 09f34b56d..e8beae8e5 100644
--- a/vibe/api.py
+++ b/vibe/api.py
@@ -2345,6 +2345,20 @@ class _WebControllerStub:
_oauth_service_lock = threading.Lock()
_oauth_service: Any = None
+_oauth_loop: asyncio.AbstractEventLoop | None = None
+_oauth_loop_thread: threading.Thread | None = None
+
+
+def _start_oauth_event_loop() -> tuple[asyncio.AbstractEventLoop, threading.Thread]:
+ loop = asyncio.new_event_loop()
+
+ def _runner() -> None:
+ asyncio.set_event_loop(loop)
+ loop.run_forever()
+
+ thread = threading.Thread(target=_runner, daemon=True, name="vibe-oauth-loop")
+ thread.start()
+ return loop, thread
def _on_web_auth_success(backend: str) -> None:
@@ -2361,18 +2375,34 @@ def _on_web_auth_success(backend: str) -> None:
def _get_oauth_service() -> Any:
"""Lazily build the (singleton) AgentAuthService for web flows."""
- global _oauth_service
+ global _oauth_service, _oauth_loop, _oauth_loop_thread
with _oauth_service_lock:
if _oauth_service is not None:
return _oauth_service
from core.agent_auth_service import AgentAuthService
+ _oauth_loop, _oauth_loop_thread = _start_oauth_event_loop()
controller = _WebControllerStub()
_oauth_service = AgentAuthService(controller)
_oauth_service._post_web_success_hook = _on_web_auth_success
return _oauth_service
+def _ensure_oauth_loop() -> asyncio.AbstractEventLoop:
+ global _oauth_loop, _oauth_loop_thread
+ with _oauth_service_lock:
+ if _oauth_loop is None or _oauth_loop.is_closed():
+ _oauth_loop, _oauth_loop_thread = _start_oauth_event_loop()
+ return _oauth_loop
+
+
+def _submit_oauth_coro(coro, *, timeout: float = 30.0):
+ _get_oauth_service() # ensures the persistent loop exists
+ loop = _ensure_oauth_loop()
+ future = asyncio.run_coroutine_threadsafe(coro, loop)
+ return future.result(timeout=timeout)
+
+
def _serialize_web_flow_status(payload: dict) -> dict:
"""Strip server-only keys before returning to the browser."""
if not isinstance(payload, dict):
@@ -2388,10 +2418,9 @@ def start_oauth_web(
force_reset: bool = True,
provider_id: Optional[str] = None,
) -> dict:
- from vibe.async_bridge import run_coroutine_blocking
-
- return run_coroutine_blocking(
- start_oauth_web_async(backend, force_reset=force_reset, provider_id=provider_id)
+ return _submit_oauth_coro(
+ start_oauth_web_async(backend, force_reset=force_reset, provider_id=provider_id),
+ timeout=60.0,
)
@@ -2443,9 +2472,7 @@ def get_oauth_web_status(flow_id: str) -> dict:
def submit_oauth_web_code(flow_id: str, code: str) -> dict:
- from vibe.async_bridge import run_coroutine_blocking
-
- return run_coroutine_blocking(submit_oauth_web_code_async(flow_id, code))
+ return _submit_oauth_coro(submit_oauth_web_code_async(flow_id, code), timeout=30.0)
async def submit_oauth_web_code_async(flow_id: str, code: str) -> dict:
@@ -2461,9 +2488,7 @@ async def submit_oauth_web_code_async(flow_id: str, code: str) -> dict:
def remove_backend_auth(backend: str) -> dict:
- from vibe.async_bridge import run_coroutine_blocking
-
- return run_coroutine_blocking(remove_backend_auth_async(backend))
+ return _submit_oauth_coro(remove_backend_auth_async(backend), timeout=30.0)
async def remove_backend_auth_async(backend: str) -> dict:
@@ -2568,9 +2593,7 @@ def remove_backend_api_key(backend: str) -> dict:
def test_backend_auth(backend: str, model: Optional[str] = None) -> dict:
- from vibe.async_bridge import run_coroutine_blocking
-
- return run_coroutine_blocking(test_backend_auth_async(backend, model=model))
+ return _submit_oauth_coro(test_backend_auth_async(backend, model=model), timeout=60.0)
async def test_backend_auth_async(backend: str, model: Optional[str] = None) -> dict:
@@ -2592,9 +2615,7 @@ async def test_backend_auth_async(backend: str, model: Optional[str] = None) ->
def test_opencode_provider(provider_id: str, model: Optional[str] = None) -> dict:
- from vibe.async_bridge import run_coroutine_blocking
-
- return run_coroutine_blocking(test_opencode_provider_async(provider_id, model=model))
+ return _submit_oauth_coro(test_opencode_provider_async(provider_id, model=model), timeout=90.0)
async def test_opencode_provider_async(provider_id: str, model: Optional[str] = None) -> dict:
@@ -2623,9 +2644,7 @@ async def test_opencode_provider_async(provider_id: str, model: Optional[str] =
def cancel_oauth_web(flow_id: str) -> dict:
- from vibe.async_bridge import run_coroutine_blocking
-
- return run_coroutine_blocking(cancel_oauth_web_async(flow_id))
+ return _submit_oauth_coro(cancel_oauth_web_async(flow_id), timeout=15.0)
async def cancel_oauth_web_async(flow_id: str) -> dict:
diff --git a/vibe/ui_compat.py b/vibe/ui_compat.py
index c6a0ee19f..49e6e6c5a 100644
--- a/vibe/ui_compat.py
+++ b/vibe/ui_compat.py
@@ -269,6 +269,8 @@ class CompatTestClient:
request_url = url
if base_url:
request_url = _join_base_url(base_url, url)
+ elif not url.startswith(("http://", "https://")):
+ request_url = _join_base_url("http://127.0.0.1", url)
headers = kwargs.pop("headers", None) or {}
headers = dict(headers)
if not base_url and "Origin" in headers:
diff --git a/vibe/ui_server.py b/vibe/ui_server.py
index 346a99d3e..dbda7537a 100644
--- a/vibe/ui_server.py
+++ b/vibe/ui_server.py
@@ -898,8 +898,6 @@ def enforce_remote_access_cookie():
if _remote_auth_exempt_before_host_validation():
return None
local_request = _is_local_request(config)
- if config is None and not _has_forwarded_metadata() and _is_loopback_peer():
- local_request = True
docker_probe_request = _is_trusted_docker_loopback_probe()
if config is None:
if local_request or docker_probe_request: