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(opencode): close options session after UI requests

cyh899c48334104
diff --git a/modules/agents/opencode/server.py b/modules/agents/opencode/server.py
index 6b47b757b..c009d4ca1 100644
--- a/modules/agents/opencode/server.py
+++ b/modules/agents/opencode/server.py
@@ -118,6 +118,17 @@ class OpenCodeServerManager:
self._http_session = None
self._http_session_loop = None
+ async def close_http_session(self) -> None:
+ """Close the cached HTTP session explicitly.
+
+ UI helper flows may run on short-lived event loops created per request.
+ Closing the cached session at the end of those flows prevents aiohttp
+ from reporting unclosed sessions/connectors when the loop exits.
+ """
+
+ async with self._get_lock():
+ await self._close_http_session_locked()
+
async def _restart_for_auth_refresh_locked(self) -> None:
await self._close_http_session_locked()
diff --git a/tests/test_ui_api.py b/tests/test_ui_api.py
index fbf1be6cd..ab4998381 100644
--- a/tests/test_ui_api.py
+++ b/tests/test_ui_api.py
@@ -1,4 +1,6 @@
+import asyncio
import json
+from types import SimpleNamespace
import pytest
@@ -8,6 +10,72 @@ from vibe import api
from vibe.opencode_config import parse_jsonc_object
+def test_opencode_options_closes_server_http_session(monkeypatch):
+ import config.v2_compat as v2_compat
+ import modules.agents.opencode as opencode_module
+
+ class _FakeManager:
+ def __init__(self):
+ self.closed = 0
+
+ async def ensure_running(self):
+ return "http://127.0.0.1:4096"
+
+ async def get_available_agents(self, directory):
+ return [{"name": "build", "mode": "primary", "hidden": False}]
+
+ async def get_available_models(self, directory):
+ return {
+ "providers": [
+ {
+ "id": "openai",
+ "models": {
+ "gpt-5": {},
+ },
+ }
+ ]
+ }
+
+ async def get_default_config(self, directory):
+ return {"model": "openai/gpt-5"}
+
+ async def close_http_session(self):
+ self.closed += 1
+
+ fake_manager = _FakeManager()
+
+ class _FakeServerManager:
+ @staticmethod
+ async def get_instance(**kwargs):
+ return fake_manager
+
+ monkeypatch.setattr(api, "_OPENCODE_OPTIONS_CACHE", {})
+ monkeypatch.setattr(api.V2Config, "load", staticmethod(lambda: object()))
+ monkeypatch.setattr(
+ v2_compat,
+ "to_app_config",
+ lambda config: SimpleNamespace(
+ opencode=SimpleNamespace(
+ binary="opencode",
+ port=4096,
+ request_timeout_seconds=10,
+ )
+ ),
+ )
+ monkeypatch.setattr(opencode_module, "OpenCodeServerManager", _FakeServerManager)
+ monkeypatch.setattr(
+ opencode_module,
+ "build_reasoning_effort_options",
+ lambda models, model_key: [{"value": "__default__"}],
+ )
+
+ result = asyncio.run(api.opencode_options_async("~/workspace"))
+
+ assert result["ok"] is True
+ assert result["data"]["defaults"] == {"model": "openai/gpt-5"}
+ assert fake_manager.closed == 1
+
+
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/vibe/api.py b/vibe/api.py
index 06187ade0..09b516961 100644
--- a/vibe/api.py
+++ b/vibe/api.py
@@ -577,6 +577,7 @@ async def opencode_options_async(cwd: str) -> dict:
if cache_data and cache_age < _OPENCODE_OPTIONS_TTL_SECONDS:
return {"ok": True, "data": cache_data, "cached": True}
+ server = None
try:
from config.v2_compat import to_app_config
from modules.agents.opencode import (
@@ -638,6 +639,9 @@ async def opencode_options_async(cwd: str) -> dict:
if cache_data:
return {"ok": True, "data": cache_data, "cached": True, "warning": str(exc)}
return {"ok": False, "error": str(exc)}
+ finally:
+ if server is not None:
+ await server.close_http_session()
def _current_platform() -> str: