Repository navigation
gateway/slack: coalesce same-user top-level channel follow-ups (+ elapsed typing heartbeat) #45702
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -365,6 +365,10 @@ def __init__(self, config: PlatformConfig): | |
| # Track active assistant thread status indicators so stop_typing can | ||
| # clear them (chat_id → thread_ts). | ||
| self._active_status_threads: Dict[str, str] = {} | ||
| # Heartbeat: monotonic start time of the current status indicator per | ||
| # chat, so a long-running turn surfaces elapsed time ("still working… | ||
| # (2m03s)") instead of a static "is thinking..." that reads as stuck. | ||
| self._status_started: Dict[str, float] = {} | ||
| # Slash-command contexts: stash response_url + user_id so send() | ||
| # can route the first reply ephemerally. Keyed by | ||
| # (channel_id, user_id) to avoid cross-user collisions. | ||
|
|
@@ -1290,11 +1294,27 @@ async def send_typing(self, chat_id: str, metadata=None) -> None: | |
| return # Can only set status in a thread context | ||
|
|
||
| self._active_status_threads[chat_id] = thread_ts | ||
| # Heartbeat: _keep_typing refreshes this on an interval, so deriving the | ||
| # label from elapsed time turns the static "is thinking..." into visible | ||
| # progress for a long turn (e.g. a multi-minute delegation). A turn that | ||
| # visibly advances doesn't provoke a mid-turn "you there?" — which would | ||
| # otherwise race the in-flight answer in its own session. | ||
| started = self._status_started.get(chat_id) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This timer is keyed only by |
||
| if started is None: | ||
| started = time.monotonic() | ||
| self._status_started[chat_id] = started | ||
| elapsed = int(time.monotonic() - started) | ||
| if elapsed >= 30: | ||
| mins, secs = divmod(elapsed, 60) | ||
| human = f"{mins}m{secs:02d}s" if mins else f"{secs}s" | ||
| status = f"still working… ({human})" | ||
| else: | ||
| status = "is thinking..." | ||
| try: | ||
| await self._get_client(chat_id).assistant_threads_setStatus( | ||
| channel_id=chat_id, | ||
| thread_ts=thread_ts, | ||
| status="is thinking...", | ||
| status=status, | ||
| ) | ||
| except Exception as e: | ||
| # Silently ignore — may lack assistant:write scope or not be | ||
|
|
@@ -1306,6 +1326,7 @@ async def stop_typing(self, chat_id: str, metadata=None) -> None: | |
| if not self._app: | ||
| return | ||
| thread_ts = self._active_status_threads.pop(chat_id, None) | ||
| self._status_started.pop(chat_id, None) | ||
| if not thread_ts: | ||
| return | ||
| try: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,217 @@ | ||
| """Tests for channel turn-coalescing. | ||
|
|
||
| A platform that mints a fresh per-message session for each top-level channel | ||
| message (Slack with ``reply_in_thread=true`` — the default — falls back to | ||
| ``thread_ts = ts``) would otherwise let a rapid same-user follow-up race the | ||
| in-flight turn in its own session — the classic "agent answers a now-stale | ||
| question right after the real answer lands" bug. The coalescing index reattaches | ||
| such a follow-up to the author's active top-level turn so it is routed through | ||
| the normal busy handler (queue/cascade, or interrupt only a cheap turn). | ||
|
|
||
| Same-user / same-chat only: thread replies, other users, and DMs are untouched, | ||
| and under ``reply_in_thread=false`` the sessions already share a key so this | ||
| never fires. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import asyncio | ||
| import sys | ||
| import types | ||
| from unittest.mock import AsyncMock, MagicMock | ||
|
|
||
| import pytest | ||
|
|
||
| # Minimal telegram stub so importing gateway.platforms.base does not pull in the | ||
| # real python-telegram-bot dependency (mirrors test_active_session_text_merge). | ||
| _tg = sys.modules.get("telegram") or types.ModuleType("telegram") | ||
| _tg.constants = sys.modules.get("telegram.constants") or types.ModuleType("telegram.constants") | ||
| _ct = MagicMock() | ||
| _ct.PRIVATE = "private" | ||
| _ct.GROUP = "group" | ||
| _ct.SUPERGROUP = "supergroup" | ||
| _tg.constants.ChatType = _ct | ||
| sys.modules.setdefault("telegram", _tg) | ||
| sys.modules.setdefault("telegram.constants", _tg.constants) | ||
| sys.modules.setdefault("telegram.ext", types.ModuleType("telegram.ext")) | ||
|
|
||
| from gateway.config import Platform, PlatformConfig | ||
| from gateway.platforms.base import ( | ||
| BasePlatformAdapter, | ||
| MessageEvent, | ||
| MessageType, | ||
| SendResult, | ||
| ) | ||
| from gateway.session import SessionSource, build_session_key | ||
|
|
||
|
|
||
| def _make_event( | ||
| text: str, | ||
| *, | ||
| chat_id: str = "C1", | ||
| chat_type: str = "channel", | ||
| user_id: str = "u1", | ||
| ts: str | None = None, | ||
| thread_id: str | None = "__own__", | ||
| ) -> MessageEvent: | ||
| """Build an event. ``thread_id="__own__"`` mimics Slack's per-message | ||
| top-level session (``thread_ts = ts``); pass an explicit value for a reply.""" | ||
| message_id = ts or f"ts-{text[:8]}" | ||
| resolved_thread = message_id if thread_id == "__own__" else thread_id | ||
| source = SessionSource( | ||
| platform=Platform.SLACK, | ||
| chat_id=chat_id, | ||
| chat_type=chat_type, | ||
| user_id=user_id, | ||
| thread_id=resolved_thread, | ||
| ) | ||
| return MessageEvent( | ||
| text=text, | ||
| message_type=MessageType.TEXT, | ||
| source=source, | ||
| message_id=message_id, | ||
| ) | ||
|
|
||
|
|
||
| class _DummyAdapter(BasePlatformAdapter): # type: ignore[misc] | ||
| async def connect(self): | ||
| pass | ||
|
|
||
| async def disconnect(self): | ||
| pass | ||
|
|
||
| async def get_chat_info(self, chat_id): | ||
| return None | ||
|
|
||
| async def send(self, *args, **kwargs): | ||
| return SendResult(success=True, message_id="x") | ||
|
|
||
|
|
||
| def _make_adapter() -> BasePlatformAdapter: | ||
| adapter = object.__new__(_DummyAdapter) | ||
| adapter.config = PlatformConfig(enabled=True, token="***") | ||
| adapter.platform = Platform.SLACK | ||
| adapter._message_handler = AsyncMock(return_value=None) | ||
| adapter._busy_session_handler = None | ||
| adapter._active_sessions = {} | ||
| adapter._pending_messages = {} | ||
| adapter._session_tasks = {} | ||
| adapter._active_topfollow = {} | ||
| adapter._background_tasks = set() | ||
| adapter._post_delivery_callbacks = {} | ||
| adapter._expected_cancelled_tasks = set() | ||
| adapter._fatal_error_code = None | ||
| adapter._fatal_error_message = None | ||
| adapter._fatal_error_retryable = True | ||
| adapter._fatal_error_handler = None | ||
| adapter._running = True | ||
| adapter._busy_text_mode = "" # direct-merge, no debounce | ||
| adapter._busy_text_debounce_seconds = 0.1 | ||
| adapter._busy_text_hard_cap_seconds = 1.0 | ||
| adapter._text_debounce = {} | ||
| adapter._auto_tts_default = False | ||
| adapter._auto_tts_enabled_chats = set() | ||
| adapter._auto_tts_disabled_chats = set() | ||
| adapter._typing_paused = set() | ||
| return adapter | ||
|
|
||
|
|
||
| # ---- _topfollow_key --------------------------------------------------------- | ||
|
|
||
| def test_topfollow_key_for_top_level_channel(): | ||
| adapter = _make_adapter() | ||
| ev = _make_event("hi", ts="ts1") | ||
| assert adapter._topfollow_key(ev) == "C1\x1fu1" | ||
|
|
||
|
|
||
| def test_topfollow_key_none_for_dm(): | ||
| adapter = _make_adapter() | ||
| assert adapter._topfollow_key(_make_event("hi", chat_type="dm")) is None | ||
|
|
||
|
|
||
| def test_topfollow_key_none_for_thread_reply(): | ||
| adapter = _make_adapter() | ||
| # Anchor points at an earlier parent (!= this message's id) → real reply. | ||
| ev = _make_event("hi", ts="ts2", thread_id="parent-ts") | ||
| assert adapter._topfollow_key(ev) is None | ||
|
|
||
|
|
||
| def test_topfollow_key_none_without_user(): | ||
| adapter = _make_adapter() | ||
| ev = _make_event("hi", ts="ts1") | ||
| ev.source.user_id = None | ||
| assert adapter._topfollow_key(ev) is None | ||
|
|
||
|
|
||
| # ---- coalescing in handle_message ------------------------------------------- | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_same_user_top_level_followup_coalesces_into_active_turn(): | ||
| adapter = _make_adapter() | ||
| first = _make_event("which pipeline?", ts="ts1") | ||
| active_key = build_session_key(first.source) | ||
| # Simulate the first turn being in flight, registered in both structures. | ||
| adapter._active_sessions[active_key] = asyncio.Event() | ||
| adapter._active_topfollow["C1\x1fu1"] = active_key | ||
|
|
||
| followup = _make_event("you there?", ts="ts2") # new top-level → new key | ||
| assert build_session_key(followup.source) != active_key | ||
|
|
||
| await adapter.handle_message(followup) | ||
|
|
||
| # No second concurrent session was started; it queued behind the active one. | ||
| assert set(adapter._active_sessions) == {active_key} | ||
| assert adapter._pending_messages[active_key].text == "you there?" | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_other_user_top_level_does_not_coalesce(): | ||
| adapter = _make_adapter() | ||
| first = _make_event("which pipeline?", ts="ts1", user_id="u1") | ||
| active_key = build_session_key(first.source) | ||
| adapter._active_sessions[active_key] = asyncio.Event() | ||
| adapter._active_topfollow["C1\x1fu1"] = active_key | ||
|
|
||
| other = _make_event("status?", ts="ts9", user_id="u2") | ||
| await adapter.handle_message(other) | ||
|
|
||
| # Different user → not folded into u1's turn; u1's pending stays empty. | ||
| assert active_key not in adapter._pending_messages | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_thread_reply_does_not_coalesce(): | ||
| adapter = _make_adapter() | ||
| first = _make_event("which pipeline?", ts="ts1") | ||
| active_key = build_session_key(first.source) | ||
| adapter._active_sessions[active_key] = asyncio.Event() | ||
| adapter._active_topfollow["C1\x1fu1"] = active_key | ||
|
|
||
| reply = _make_event("reply in old thread", ts="ts2", thread_id="old-parent") | ||
| await adapter.handle_message(reply) | ||
|
|
||
| assert active_key not in adapter._pending_messages | ||
|
|
||
|
|
||
| # ---- index lifecycle -------------------------------------------------------- | ||
|
|
||
| def test_release_session_guard_clears_topfollow_index(): | ||
| adapter = _make_adapter() | ||
| adapter._active_sessions["k"] = asyncio.Event() | ||
| adapter._active_topfollow["C1\x1fu1"] = "k" | ||
| adapter._release_session_guard("k") | ||
| assert adapter._active_topfollow == {} | ||
|
|
||
|
|
||
| def test_heal_stale_lock_clears_topfollow_index(): | ||
| adapter = _make_adapter() | ||
| adapter._active_sessions["k"] = asyncio.Event() | ||
| adapter._active_topfollow["C1\x1fu1"] = "k" | ||
|
|
||
| class _DoneTask: | ||
| def done(self): | ||
| return True | ||
|
|
||
| adapter._session_tasks["k"] = _DoneTask() | ||
| assert adapter._heal_stale_session_lock("k") is True | ||
| assert adapter._active_topfollow == {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This rebind runs before the command bypass at line 3999 and the clarify interception below it. A top-level same-user
/stop,/new,/approve, or clarify response is therefore treated as belonging to the sibling active session. Exempt those paths before rebinding and add regressions.