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(channels): dedupe background refreshes

cyh2f3a07f83cc9
diff --git a/core/chat_discovery.py b/core/chat_discovery.py
index 7402d8d9f..26bd85bf3 100644
--- a/core/chat_discovery.py
+++ b/core/chat_discovery.py
@@ -51,6 +51,8 @@ _debounce_cache: dict[tuple[str, str], tuple[float, tuple[Any, ...]]] = {}
_refresh_locks_lock = threading.Lock()
_refresh_locks: dict[str, threading.Lock] = {}
+_scheduled_refreshes_lock = threading.Lock()
+_scheduled_refreshes: set[str] = set()
_migration_lock = threading.Lock()
_migrated_db_paths: set[Path] = set()
@@ -511,7 +513,10 @@ def should_refresh(
ttl_seconds: float = _REFRESH_TTL_SECONDS,
) -> bool:
state = refresh_state(platform, refresh_scope=refresh_scope, db_path=db_path)
- return _seconds_since(state.last_success_at) >= ttl_seconds
+ return (
+ _seconds_since(state.last_success_at) >= ttl_seconds
+ and _seconds_since(state.last_attempt_at) >= _MIN_REFRESH_INTERVAL_SECONDS
+ )
def migrate_legacy_discovered_chats(*, db_path: Path | None = None, legacy_path: Path | None = None) -> None:
@@ -610,8 +615,7 @@ def channels_response(
)
chats = _filter_response_chats(all_chats, include_private=include_private, require_member=require_member)
elif can_refresh and should_refresh(platform, refresh_scope=refresh_scope, db_path=db_path):
- _schedule_refresh(platform, db_path=db_path, **refresh_kwargs)
- refreshing = True
+ refreshing = _schedule_refresh(platform, db_path=db_path, **refresh_kwargs)
channels = [chat.to_channel_payload() for chat in chats]
return {
@@ -793,13 +797,32 @@ def _store_refresh_state(
engine.dispose()
-def _schedule_refresh(platform: str, *, db_path: Path | None = None, **kwargs: Any) -> None:
+def _schedule_refresh(platform: str, *, db_path: Path | None = None, **kwargs: Any) -> bool:
+ refresh_scope = _refresh_scope_for(platform, kwargs)
+ key = _refresh_state_key(platform, refresh_scope)
+ with _scheduled_refreshes_lock:
+ if key in _scheduled_refreshes:
+ return True
+ _scheduled_refreshes.add(key)
+
+ def _run_refresh() -> None:
+ try:
+ refresh_platform(platform=platform, db_path=db_path, **kwargs)
+ finally:
+ with _scheduled_refreshes_lock:
+ _scheduled_refreshes.discard(key)
+
thread = threading.Thread(
- target=refresh_platform,
- kwargs={"platform": platform, "db_path": db_path, **kwargs},
+ target=_run_refresh,
daemon=True,
)
- thread.start()
+ try:
+ thread.start()
+ except Exception:
+ with _scheduled_refreshes_lock:
+ _scheduled_refreshes.discard(key)
+ raise
+ return True
def _summary(all_chats: list[ChannelInfo], visible_chats: list[ChannelInfo], *, include_private: bool) -> dict[str, int]:
diff --git a/tests/test_chat_discovery.py b/tests/test_chat_discovery.py
index 43b813519..ce8628036 100644
--- a/tests/test_chat_discovery.py
+++ b/tests/test_chat_discovery.py
@@ -197,6 +197,50 @@ def test_empty_cache_channel_response_respects_refresh_backoff(tmp_path: Path, m
assert second["error"] == "bad token"
+def test_stale_cache_schedules_only_one_background_refresh(tmp_path: Path, monkeypatch) -> None:
+ db_path = tmp_path / "vibe.sqlite"
+ run_migrations(db_path)
+ auth_context = _auth_context("slack", bot_token="x")
+ chat_discovery.remember_chat(
+ "slack",
+ "C_STALE",
+ name="stale",
+ metadata={chat_discovery.METADATA_AUTH_CONTEXT: auth_context},
+ db_path=db_path,
+ )
+ chat_discovery.set_state_meta(
+ f"channel_refresh.slack.{auth_context}",
+ {
+ "last_attempt_at": "2000-01-01T00:00:00+00:00",
+ "last_success_at": "2000-01-01T00:00:00+00:00",
+ "last_error": None,
+ },
+ db_path=db_path,
+ )
+ starts = 0
+
+ class FakeThread:
+ def __init__(self, *args, **kwargs):
+ pass
+
+ def start(self):
+ nonlocal starts
+ starts += 1
+
+ monkeypatch.setattr(chat_discovery.threading, "Thread", FakeThread)
+
+ try:
+ first = chat_discovery.channels_response("slack", bot_token="x", db_path=db_path)
+ second = chat_discovery.channels_response("slack", bot_token="x", db_path=db_path)
+ finally:
+ with chat_discovery._scheduled_refreshes_lock:
+ chat_discovery._scheduled_refreshes.clear()
+
+ assert starts == 1
+ assert first["refreshing"] is True
+ assert second["refreshing"] is True
+
+
def test_slack_cached_response_respects_member_only_browse_mode(tmp_path: Path) -> None:
db_path = tmp_path / "vibe.sqlite"
run_migrations(db_path)
diff --git a/tests/test_ui_api.py b/tests/test_ui_api.py
index dd5e971f1..752e35157 100644
--- a/tests/test_ui_api.py
+++ b/tests/test_ui_api.py
@@ -212,6 +212,20 @@ def test_install_codex_uses_resolved_npm(monkeypatch):
assert result["path"] == "/Users/test/.nvm/versions/node/v22.18.0/bin/codex"
+def test_discord_list_channels_rejects_empty_guild_id(monkeypatch):
+ monkeypatch.setattr(
+ chat_discovery,
+ "channels_response",
+ lambda *args, **kwargs: pytest.fail("channels_response should not be called without guild_id"),
+ )
+
+ result = api.discord_list_channels("token", "")
+
+ assert result["ok"] is False
+ assert result["channels"] == []
+ assert result["error"] == "Discord guild_id is required"
+
+
def test_install_codex_detects_binary_via_npm_prefix(monkeypatch, tmp_path):
npm_path = tmp_path / "node" / "bin" / "npm"
npm_path.parent.mkdir(parents=True, exist_ok=True)
diff --git a/vibe/api.py b/vibe/api.py
index 340042954..24acf1363 100644
--- a/vibe/api.py
+++ b/vibe/api.py
@@ -704,13 +704,24 @@ def discord_list_guilds(bot_token: str) -> dict:
def discord_list_channels(bot_token: str, guild_id: str, force: bool = False) -> dict:
+ guild_id = str(guild_id or "").strip()
+ if not guild_id:
+ return {
+ "ok": False,
+ "channels": [],
+ "chats": [],
+ "refreshing": False,
+ "last_attempt_at": None,
+ "last_success_at": None,
+ "error": "Discord guild_id is required",
+ "summary": {"discovered_count": 0, "visible_count": 0, "hidden_private_count": 0, "forum_count": 0},
+ }
+
from core import chat_discovery
- parent_scope_id = None
- if guild_id:
- from storage.settings_service import make_scope_id
+ from storage.settings_service import make_scope_id
- parent_scope_id = make_scope_id("discord", "guild", guild_id)
+ parent_scope_id = make_scope_id("discord", "guild", guild_id)
return chat_discovery.channels_response(
"discord",
bot_token=bot_token,