diff --git a/plugin/adapter.py b/plugin/adapter.py index 476d23a..e47f8fe 100644 --- a/plugin/adapter.py +++ b/plugin/adapter.py @@ -1302,6 +1302,8 @@ class RocketChatAdapter(BasePlatformAdapter): self._clarify_state: Dict[str, str] = {} # Clarify room mapping: room_id → clarify_id for plain-digit text intercept self._room_clarify: Dict[str, str] = {} + # P-044: Reverse mapping: clarify_id → room_id for robust cleanup + self._clarify_room: Dict[str, str] = {} # FIX 2 (P0): ISSUE 3 — Pending Clarify Dispatch Slot # {room_id: {text, username, msg_id, msg_obj, is_button_response}} self._pending_clarify_dispatch: Dict[str, dict] = {} @@ -2427,9 +2429,20 @@ class RocketChatAdapter(BasePlatformAdapter): if resolved_text is None: resolved_text = f"choice {idx + 1}" # Resolve via gateway and clean up + # P-044: session_key VOR pop() sichern — sonst loggt die Zeile immer "?" + _saved_session_key = self._clarify_state.get(clarify_id, "?") self._clarify_state.pop(clarify_id, None) - self._room_clarify.pop(room_id, None) + # P-044: room_ids VOR dem pop() sammeln für pending_questions-cleanup + _affected_rooms = [rid for rid, cid in self._room_clarify.items() if cid == clarify_id] + for rid in _affected_rooms: + self._room_clarify.pop(rid, None) + # P-044: Auch _clarify_room reverse mapping aufräumen + _clarify_room_id = self._clarify_room.pop(clarify_id, None) + if _clarify_room_id: + _affected_rooms.append(_clarify_room_id) # Clean up pending_questions — Clarify is resolved + for _rid in _affected_rooms: + self._rooms_with_pending_questions.discard(_rid) self._rooms_with_pending_questions.discard(room_id) try: from tools.clarify_gateway import resolve_gateway_clarify @@ -2438,7 +2451,7 @@ class RocketChatAdapter(BasePlatformAdapter): logger.info( "[rocketchat] Clarify %s resolved via plain-digit: %s (session=%s)", clarify_id, resolved_text[:60], - self._clarify_state.get(clarify_id, "?"), + _saved_session_key, ) else: logger.warning("[rocketchat] Clarify %s resolve returned False", clarify_id) @@ -2494,12 +2507,21 @@ class RocketChatAdapter(BasePlatformAdapter): # Race: entry vanished. Echo the index. resolved_text = f"choice {idx + 1}" # Resolve via gateway and clean up + # P-044: session_key VOR pop() sichern — sonst loggt die Zeile immer "?" + _saved_session_key = self._clarify_state.get(clarify_id, "?") self._clarify_state.pop(clarify_id, None) # Clean up room→clarify mapping (button click resolved it) - for rid, cid in list(self._room_clarify.items()): - if cid == clarify_id: - self._room_clarify.pop(rid, None) + # P-044: room_ids VOR dem pop() sammeln für pending_questions-cleanup + _affected_rooms = [rid for rid, cid in self._room_clarify.items() if cid == clarify_id] + for rid in _affected_rooms: + self._room_clarify.pop(rid, None) + # P-044: Auch _clarify_room reverse mapping aufräumen + _clarify_room_id = self._clarify_room.pop(clarify_id, None) + if _clarify_room_id: + _affected_rooms.append(_clarify_room_id) # Clean up pending_questions — Clarify is resolved + for _rid in _affected_rooms: + self._rooms_with_pending_questions.discard(_rid) self._rooms_with_pending_questions.discard(room_id) try: from tools.clarify_gateway import resolve_gateway_clarify @@ -2507,7 +2529,7 @@ class RocketChatAdapter(BasePlatformAdapter): if resolved: logger.info( "[rocketchat] Clarify %s resolved: %s (session=%s)", - clarify_id, resolved_text[:60], self._clarify_state.get(clarify_id, "?"), + clarify_id, resolved_text[:60], _saved_session_key, ) else: logger.warning("[rocketchat] Clarify %s resolve returned False", clarify_id) @@ -7572,6 +7594,10 @@ class RocketChatAdapter(BasePlatformAdapter): # First attachment gets the title if attachments: attachments[0]["title"] = "Please choose an option:" + logger.debug( + "[rocketchat] send_clarify: chunked %d buttons into %d attachments (max %d/row)", + len(buttons), len(attachments), MAX_BUTTONS_PER_ROW, + ) # Send message with buttons success = await self._post_message_with_retry(real_room_id, text, attachments=attachments) @@ -7584,6 +7610,9 @@ class RocketChatAdapter(BasePlatformAdapter): # Store room→clarify mapping for plain-digit text intercept self._room_clarify = getattr(self, '_room_clarify', {}) self._room_clarify[real_room_id] = clarify_id + # P-044: Reverse mapping for robust cleanup + self._clarify_room = getattr(self, '_clarify_room', {}) + self._clarify_room[clarify_id] = real_room_id # For "Other" button, we need to enable text capture mode # This is done by calling mark_awaiting_text when the user clicks "Other"