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): preserve cached compatibility

cyh60c4720dfb15
diff --git a/core/chat_discovery.py b/core/chat_discovery.py
index 789ee1cc2..c0830250a 100644
--- a/core/chat_discovery.py
+++ b/core/chat_discovery.py
@@ -74,7 +74,7 @@ class ChannelInfo:
payload = {
"id": self.chat_id,
"name": self.name or self.chat_id,
- "type": self.native_type,
+ "type": _payload_native_type(self.native_type),
"native_type": self.native_type,
"is_private": self.is_private,
"supports_threads": self.supports_threads,
@@ -494,10 +494,15 @@ def migrate_legacy_discovered_chats(*, db_path: Path | None = None, legacy_path:
set_state_meta(marker, "done", db_path=db_path)
return
- payload = json.loads(source.read_text(encoding="utf-8"))
+ try:
+ payload = json.loads(source.read_text(encoding="utf-8"))
+ except Exception as exc:
+ logger.warning("Skipping malformed legacy discovered chats file %s: %s", source, exc)
+ return
platforms = payload.get("platforms") if isinstance(payload, dict) else {}
if not isinstance(platforms, dict):
- raise ValueError("discovered_chats.json platforms must be an object")
+ logger.warning("Skipping legacy discovered chats file %s: platforms must be an object", source)
+ return
for platform, chats in platforms.items():
if not isinstance(chats, dict):
@@ -605,13 +610,14 @@ def _fetch_platform_channels(platform: str, **kwargs: Any) -> tuple[list[dict[st
raise RuntimeError(str(result.get("error") or "Discord channel refresh failed"))
rows = []
for channel in result.get("channels") or []:
+ channel_type = channel.get("type")
rows.append(
{
"id": channel.get("id"),
"name": channel.get("name"),
- "native_type": str(channel.get("type") or ""),
+ "native_type": "" if channel_type is None else str(channel_type),
"is_private": False,
- "supports_threads": channel.get("type") in (0, 5),
+ "supports_threads": channel_type in (0, 5),
"parent_scope_id": parent_scope_id,
"metadata": {
METADATA_CHANNEL_POSITION: channel.get("position"),
@@ -762,6 +768,15 @@ def _filter_response_chats(
return result
+def _payload_native_type(native_type: str) -> str | int:
+ if native_type.isdecimal():
+ try:
+ return int(native_type)
+ except ValueError:
+ return native_type
+ return native_type
+
+
def _rename_preserving_existing(source: Path, target: Path) -> None:
if not target.exists():
source.rename(target)
diff --git a/tests/test_chat_discovery.py b/tests/test_chat_discovery.py
index d436ef2db..9b6f4b178 100644
--- a/tests/test_chat_discovery.py
+++ b/tests/test_chat_discovery.py
@@ -210,3 +210,65 @@ def test_discord_refresh_state_is_scoped_by_guild(tmp_path: Path, monkeypatch) -
assert chat_discovery.refresh_state("discord", refresh_scope="guild.G1", db_path=db_path).last_success_at is not None
assert chat_discovery.refresh_state("discord", refresh_scope="guild.G2", db_path=db_path).last_success_at is not None
assert chat_discovery.refresh_state("discord", db_path=db_path).last_success_at is None
+
+
+def test_discord_cached_payload_preserves_numeric_channel_type(tmp_path: Path, monkeypatch) -> None:
+ db_path = tmp_path / "vibe.sqlite"
+ run_migrations(db_path)
+
+ from vibe import api
+
+ monkeypatch.setattr(
+ api,
+ "discord_list_channels_live",
+ lambda _token, _guild_id: {
+ "ok": True,
+ "channels": [{"id": "C_TEXT", "name": "text", "type": 0}],
+ },
+ )
+
+ response = chat_discovery.channels_response(
+ "discord",
+ bot_token="x",
+ guild_id="G1",
+ parent_scope_id="discord::guild::G1",
+ db_path=db_path,
+ )
+ cached = chat_discovery.channels_response(
+ "discord",
+ bot_token="x",
+ guild_id="G1",
+ parent_scope_id="discord::guild::G1",
+ db_path=db_path,
+ )
+
+ assert response["channels"][0]["type"] == 0
+ assert cached["channels"][0]["type"] == 0
+ assert cached["channels"][0]["native_type"] == "0"
+
+
+def test_malformed_legacy_discovered_chats_does_not_break_channel_response(tmp_path: Path, monkeypatch) -> None:
+ db_path = tmp_path / "vibe.sqlite"
+ run_migrations(db_path)
+ legacy_path = tmp_path / "discovered_chats.json"
+ legacy_path.write_text("{bad json", encoding="utf-8")
+ monkeypatch.setattr(chat_discovery.paths, "get_discovered_chats_path", lambda: legacy_path)
+
+ from vibe import api
+
+ monkeypatch.setattr(
+ api,
+ "list_channels_live",
+ lambda _token, browse_all=False: {
+ "ok": True,
+ "channels": [{"id": "C1", "name": "general", "is_private": False, "is_member": True}],
+ "is_member_only": not browse_all,
+ },
+ )
+
+ response = chat_discovery.channels_response("slack", bot_token="x", require_member=True, db_path=db_path)
+
+ assert response["ok"] is True
+ assert response["channels"][0]["id"] == "C1"
+ assert legacy_path.exists()
+ assert chat_discovery.get_state_meta("migrations.discovered_chats_to_scopes", db_path=db_path) is None