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(telegram): preserve same-scope update order

cyhe6fd3bc99069
diff --git a/modules/im/telegram.py b/modules/im/telegram.py
index e47aa7fa1..61f364714 100644
--- a/modules/im/telegram.py
+++ b/modules/im/telegram.py
@@ -96,6 +96,7 @@ class TelegramBot(BaseIMClient):
self._on_ready: Optional[Callable] = None
self._loop: Optional[asyncio.AbstractEventLoop] = None
self._update_tasks: set[asyncio.Task[Any]] = set()
+ self._update_scope_locks: dict[str, asyncio.Lock] = {}
self._cwd_prompts: dict[str, _TelegramCwdPrompt] = {}
self._resume_states: dict[str, _TelegramResumeSessionState] = {}
self._routing_states: dict[str, _TelegramRoutingState] = {}
@@ -174,10 +175,22 @@ class TelegramBot(BaseIMClient):
await self._drain_update_tasks()
def _spawn_update_task(self, update: dict[str, Any]) -> None:
- task = asyncio.create_task(self._handle_update(update))
+ scope_key = self._extract_update_scope_key(update)
+ if scope_key:
+ task = asyncio.create_task(self._handle_scoped_update(update, scope_key))
+ else:
+ task = asyncio.create_task(self._handle_update(update))
self._update_tasks.add(task)
task.add_done_callback(self._handle_update_task_done)
+ async def _handle_scoped_update(self, update: dict[str, Any], scope_key: str) -> None:
+ lock = self._update_scope_locks.get(scope_key)
+ if lock is None:
+ lock = asyncio.Lock()
+ self._update_scope_locks[scope_key] = lock
+ async with lock:
+ await self._handle_update(update)
+
def _handle_update_task_done(self, task: asyncio.Task[Any]) -> None:
self._update_tasks.discard(task)
try:
@@ -193,6 +206,45 @@ class TelegramBot(BaseIMClient):
pending = tuple(self._update_tasks)
await asyncio.gather(*pending, return_exceptions=True)
+ def _extract_update_scope_key(self, update: dict[str, Any]) -> Optional[str]:
+ callback_query = update.get("callback_query") or {}
+ if callback_query:
+ message = callback_query.get("message") or {}
+ chat = message.get("chat") or {}
+ from_user = callback_query.get("from") or {}
+ return self._raw_interaction_scope_key(chat=chat, from_user=from_user)
+
+ message = update.get("message") or {}
+ if message:
+ chat = message.get("chat") or {}
+ from_user = message.get("from") or {}
+ return self._raw_interaction_scope_key(chat=chat, from_user=from_user)
+
+ return None
+
+ def _raw_interaction_scope_key(self, *, chat: dict[str, Any], from_user: dict[str, Any]) -> Optional[str]:
+ chat_id = str(chat.get("id") or "").strip()
+ user_id = str(from_user.get("id") or "").strip()
+ if not chat_id or not user_id:
+ return None
+ is_dm = chat.get("type") == "private"
+ scope = user_id if is_dm else chat_id
+ return f"{scope}:{user_id}"
+
+ def _spawn_message_callback_task(self, context: MessageContext, text: str) -> None:
+ if not self.on_message_callback:
+ return
+ task = asyncio.create_task(self.on_message_callback(context, text))
+ task.add_done_callback(self._handle_message_callback_task_done)
+
+ def _handle_message_callback_task_done(self, task: asyncio.Task[Any]) -> None:
+ try:
+ task.result()
+ except asyncio.CancelledError:
+ return
+ except Exception:
+ logger.exception("Telegram message callback task failed")
+
async def _handle_update(self, update: dict[str, Any]) -> None:
if update.get("callback_query"):
await self._handle_callback_query(update["callback_query"])
@@ -255,8 +307,7 @@ class TelegramBot(BaseIMClient):
context = await self._maybe_route_to_forum_topic(context, message, text)
- if self.on_message_callback:
- await self.on_message_callback(context, text)
+ self._spawn_message_callback_task(context, text)
async def _maybe_route_to_forum_topic(
self,
diff --git a/tests/test_telegram_bot.py b/tests/test_telegram_bot.py
index b338ffc03..1c5946970 100644
--- a/tests/test_telegram_bot.py
+++ b/tests/test_telegram_bot.py
@@ -296,6 +296,53 @@ def test_run_dispatches_telegram_updates_concurrently() -> None:
assert started == [1, 2]
+def test_spawn_update_task_keeps_same_scope_updates_ordered() -> None:
+ bot = TelegramBot(TelegramConfig(bot_token="123456:test-token"))
+ started: list[int] = []
+ release_first = asyncio.Event()
+ first_started = asyncio.Event()
+ second_started = asyncio.Event()
+
+ async def fake_handle(update: dict[str, int]) -> None:
+ started.append(update["update_id"])
+ if update["update_id"] == 1:
+ first_started.set()
+ await release_first.wait()
+ else:
+ second_started.set()
+
+ async def scenario() -> None:
+ with patch.object(bot, "_handle_update", new=fake_handle):
+ bot._spawn_update_task(
+ {
+ "update_id": 1,
+ "message": {
+ "chat": {"id": -100123, "type": "supergroup"},
+ "from": {"id": 42},
+ },
+ }
+ )
+ await first_started.wait()
+ bot._spawn_update_task(
+ {
+ "update_id": 2,
+ "message": {
+ "chat": {"id": -100123, "type": "supergroup"},
+ "from": {"id": 42},
+ },
+ }
+ )
+ await asyncio.sleep(0)
+ assert not second_started.is_set()
+ release_first.set()
+ await bot._drain_update_tasks()
+
+ asyncio.run(asyncio.wait_for(scenario(), timeout=0.2))
+
+ assert second_started.is_set()
+ assert started == [1, 2]
+
+
def test_pending_cwd_prompt_consumes_next_plain_message() -> None:
bot = TelegramBot(TelegramConfig(bot_token="123456:test-token"))
context = MessageContext(