diff --git a/plugin/adapter.py b/plugin/adapter.py index cfaefce..0578580 100644 --- a/plugin/adapter.py +++ b/plugin/adapter.py @@ -1499,7 +1499,11 @@ class RocketChatAdapter(BasePlatformAdapter): msg_obj.get("actions") and msg_obj.get("params", {}).get("triggerSender") and msg_obj.get("params", {}).get("payload")) - or (text and "::" in text and not text.startswith("CLARIFY:") and not text.startswith("ARCHIVAR_CLARIFY:") and text.split("::", 1)[0].strip() != "") + or (text and "::" in text + and not text.startswith("CLARIFY:") + and not text.startswith("ARCHIVAR_CLARIFY:") + and not text.startswith("MOVE_REQ:") + and text.split("::", 1)[0].strip() != "") or (text and text.startswith("CLARIFY:")) or (text and text.startswith("ARCHIVAR_CLARIFY:")) ) @@ -2606,7 +2610,7 @@ class RocketChatAdapter(BasePlatformAdapter): try: from datetime import datetime - message_type = MessageType.BUTTON_CLICK if is_button_response else MessageType.TEXT + message_type = self._message_type_for(is_button_response) # ── Archivar-Kontext-Weitergabe: Frage-Nr + Zielraum im Prompt ── target_room_id = _THEMATIC_TARGET_ROOM.get() if target_room_id: @@ -2663,11 +2667,11 @@ class RocketChatAdapter(BasePlatformAdapter): await self.handle_message(event) except Exception as e: logger.error("[rocketchat] handle_message error: %s", e) - # Fallback: Wenn BUTTON_CLICK fehlschlägt (z.B. nach Gateway-Neustart + # Fallback: Wenn COMMAND fehlschlägt (z.B. nach Gateway-Neustart # verliert der neue Prozess den In-Memory-Callback), als normale # Text-Nachricht mit Hinweis wiederholen. - if is_button_response and str(e) == "BUTTON_CLICK": - logger.info("[rocketchat] BUTTON_CLICK lost context — retrying as TEXT") + if is_button_response and str(e) == "COMMAND": + logger.info("[rocketchat] COMMAND lost context — retrying as TEXT") try: from datetime import datetime as dt2 fallback_source = SessionSource( @@ -2689,7 +2693,7 @@ class RocketChatAdapter(BasePlatformAdapter): fallback_event.source = fallback_source await self.handle_message(fallback_event) except Exception as e2: - logger.error("[rocketchat] BUTTON_CLICK text fallback also failed: %s", e2) + logger.error("[rocketchat] COMMAND text fallback also failed: %s", e2) # ── R5: Inline-Command-Handler ──────────────────────────────────────── @@ -3403,6 +3407,16 @@ class RocketChatAdapter(BasePlatformAdapter): "Verfügbar: `status`, `run`" ) + @staticmethod + def _message_type_for(is_button_response: bool) -> "MessageType": + """Return the appropriate MessageType for a button response. + + P1 fix: Hermes-Core has no BUTTON_CLICK enum member. + Button responses are dispatched as COMMAND so the agent + can handle them as structured input. + """ + return MessageType.COMMAND if is_button_response else MessageType.TEXT + async def _dispatch_agent( self, text: str, @@ -3434,7 +3448,7 @@ class RocketChatAdapter(BasePlatformAdapter): try: from datetime import datetime - message_type = MessageType.BUTTON_CLICK if is_button_response else MessageType.TEXT + message_type = self._message_type_for(is_button_response) # ── Archivar-Kontext-Weitergabe: Frage-Nr + Zielraum im Prompt ── target_room_id = _THEMATIC_TARGET_ROOM.get() if target_room_id: @@ -3474,9 +3488,9 @@ class RocketChatAdapter(BasePlatformAdapter): await self.handle_message(event) except Exception as e: logger.error("[rocketchat] handle_message error: %s", e) - # Fallback: Wenn BUTTON_CLICK fehlschlägt - if is_button_response and str(e) == "BUTTON_CLICK": - logger.info("[rocketchat] BUTTON_CLICK lost context — retrying as TEXT") + # Fallback: Wenn COMMAND fehlschlägt + if is_button_response and str(e) == "COMMAND": + logger.info("[rocketchat] COMMAND lost context — retrying as TEXT") try: from datetime import datetime as dt2 fallback_source = SessionSource( @@ -3498,7 +3512,7 @@ class RocketChatAdapter(BasePlatformAdapter): fallback_event.source = fallback_source await self.handle_message(fallback_event) except Exception as e2: - logger.error("[rocketchat] BUTTON_CLICK text fallback also failed: %s", e2) + logger.error("[rocketchat] COMMAND text fallback also failed: %s", e2) async def _send_via_archivar(self, room_id: str, text: str) -> bool: """Callback for Archivar forwarding — sends a message copy to a target room.""" diff --git a/plugin/archivar/tests/test_p1_button_click_fix.py b/plugin/archivar/tests/test_p1_button_click_fix.py new file mode 100644 index 0000000..2cf526c --- /dev/null +++ b/plugin/archivar/tests/test_p1_button_click_fix.py @@ -0,0 +1,106 @@ +""" +P1 Regression: MessageType.BUTTON_CLICK → COMMAND (Move-Request Confirm crash). + +Root cause: adapter.py uses MessageType.BUTTON_CLICK which does not exist +in Hermes-Core's MessageType enum. The heuristic at line 1502 matches +MOVE_REQ:room_id::✅Ja text payloads as button responses, triggering +the crash path in _dispatch_agent. + +Tests: +1. RED: adapter.py still references BUTTON_CLICK (will fail until fixed) +2. RED: MOVE_REQ text matches button heuristic (will fail until fixed) +3. GREEN: MessageType.COMMAND exists as replacement +4. GREEN: After fix, normal button text still matches +5. GREEN: After fix, CLARIFY still excluded +""" + +from __future__ import annotations + +import sys +import unittest +from pathlib import Path + +sys.path.insert(0, str(Path(__file__).resolve().parents[3])) + + +class TestP1ButtonClickFix(unittest.TestCase): + """P1: MessageType.BUTTON_CLICK existiert nicht → COMMAND verwenden.""" + + # ── RED tests (fail now, pass after fix) ────────────────────────── + + def test_adapter_has_no_button_click_references(self): + """RED: adapter.py darf KEINE BUTTON_CLICK-Referenzen enthalten + (ausser im Docstring der Hilfsfunktion).""" + adapter_path = Path(__file__).resolve().parents[3] / "plugin" / "adapter.py" + content = adapter_path.read_text() + + # Count BUTTON_CLICK occurrences (should be <= 1 after fix: + # only the docstring in _message_type_for is acceptable) + count = content.count("BUTTON_CLICK") + self.assertLessEqual(count, 1, + f"adapter.py has {count} BUTTON_CLICK references — " + f"max 1 allowed (docstring only)") + + def test_move_req_not_matched_by_button_heuristic(self): + """RED: MOVE_REQ:room_id::✅Ja darf NICHT als Button-Response matchen.""" + text = "MOVE_REQ:abc123::✅Ja" + + # Simulate the FIXED heuristic from adapter.py line 1502 + has_double_colon = "::" in text + not_clarify = (not text.startswith("CLARIFY:") + and not text.startswith("ARCHIVAR_CLARIFY:")) + not_move_req = not text.startswith("MOVE_REQ:") + prefix_non_empty = text.split("::", 1)[0].strip() != "" + + # Fixed heuristic: MOVE_REQ: prefix excluded + is_match = (has_double_colon and not_clarify + and not_move_req and prefix_non_empty) + self.assertFalse(is_match, + "MOVE_REQ: text should NOT match button heuristic") + + # ── GREEN tests (pass now, must still pass after fix) ───────────── + + def test_message_type_has_command(self): + """MessageType.COMMAND exists (replacement for BUTTON_CLICK).""" + from gateway.platforms.base import MessageType + + self.assertTrue(hasattr(MessageType, "COMMAND")) + self.assertEqual(MessageType.COMMAND.value, "command") + + def test_message_type_no_button_click(self): + """MessageType.BUTTON_CLICK does NOT exist in Hermes-Core.""" + from gateway.platforms.base import MessageType + + self.assertFalse(hasattr(MessageType, "BUTTON_CLICK")) + + def test_normal_button_text_still_matches_after_fix(self): + """Nach Fix: 'bypass_001::Ja' matched weiterhin als Button-Response.""" + text = "bypass_001::Ja" + + has_double_colon = "::" in text + not_clarify = (not text.startswith("CLARIFY:") + and not text.startswith("ARCHIVAR_CLARIFY:")) + not_move_req = not text.startswith("MOVE_REQ:") + prefix_non_empty = text.split("::", 1)[0].strip() != "" + + is_match = (has_double_colon and not_clarify + and not_move_req and prefix_non_empty) + self.assertTrue(is_match, + "Normal button text 'bypass_001::Ja' must still match") + + def test_clarify_text_still_excluded_after_fix(self): + """Nach Fix: CLARIFY: text weiterhin ausgeschlossen.""" + text = "CLARIFY:some_id::answer" + + has_double_colon = "::" in text + not_clarify = (not text.startswith("CLARIFY:") + and not text.startswith("ARCHIVAR_CLARIFY:")) + not_move_req = not text.startswith("MOVE_REQ:") + + is_match = has_double_colon and not_clarify and not_move_req + self.assertFalse(is_match, + "CLARIFY: text must NOT match button heuristic") + + +if __name__ == "__main__": + unittest.main()