fix(HOTFIX-3): Originalnachricht nach Widerspruch-Clarify an Agenten dispatchen
Bug: Nach dem Widerspruch-Clarify (Button 1 'generell hier her' oder Button 2 'nur jetzt hier her') wurde die Originalnachricht NIE an den Agenten weitergeleitet. Der Agent hat die Frage nie gesehen. Fix: 1. _send_widerspruch_clarify speichert jetzt die Originalnachricht (text, username, msg_id, msg_obj, is_button_response) in _widerspruch_state. 2. Der WIDERSPRUCH:-Handler dispatched nach set_never_route/stay_once die Originalnachricht an den Agenten im Ursprungsraum (gleicher Pfad wie normale Nachrichten-Verarbeitung, analog zum Move-Request-Flow). Dispatch nur bei gültiger choice (1/2). Test: test_widerspruch_then_dispatch.py — 4 Tests verifizieren Dispatch nach Button 1, Button 2, Ursprungsraum-Ziel und kein Dispatch bei unbekannter choice.
This commit is contained in:
+56
-1
@@ -2659,12 +2659,16 @@ class RocketChatAdapter(BasePlatformAdapter):
|
||||
logger.info("ROUTING BYPASS: reason=widerspruch_clarify_bypass, origin_room=%s", room_id)
|
||||
try:
|
||||
parts = text.split(":", 2)
|
||||
ctx = None
|
||||
origin_room_id = room_id
|
||||
valid_choice = False
|
||||
if len(parts) == 3:
|
||||
_, clarify_id, choice = parts
|
||||
ctx = self._widerspruch_state.pop(clarify_id, None)
|
||||
origin_room_id = (ctx or {}).get("origin_room_id", room_id)
|
||||
topic = (ctx or {}).get("topic", "")
|
||||
if choice == "1":
|
||||
valid_choice = True
|
||||
# Button 1: 'generell hier her' → dauerhaft never_route
|
||||
if _MULTIROOM() and self.archivar is not None and topic:
|
||||
self.archivar._state.set_never_route(origin_room_id, topic)
|
||||
@@ -2679,6 +2683,7 @@ class RocketChatAdapter(BasePlatformAdapter):
|
||||
"Ich route es nicht mehr automatisch um.",
|
||||
)
|
||||
elif choice == "2":
|
||||
valid_choice = True
|
||||
# Button 2: 'nur jetzt hier her' → stay_once (einmalig)
|
||||
# Kein never_route, nur _recent_auto_routes löschen,
|
||||
# damit die nächste Nachricht wieder normal geroutet wird.
|
||||
@@ -2698,6 +2703,36 @@ class RocketChatAdapter(BasePlatformAdapter):
|
||||
# P-067: Clear Clarify-Modal pulse (user responded)
|
||||
if hasattr(self, '_presence_engine') and self._presence_engine is not None and self._presence_engine.is_enabled():
|
||||
self._presence_engine.clear_clarify_modal()
|
||||
# ── HOTFIX 3: Originalnachricht an den Agenten dispatchen ──
|
||||
# Der Widerspruch klärt nur die Routing-Entscheidung. Die
|
||||
# Originalnachricht selbst muss IMMER beantwortet werden —
|
||||
# egal ob Button 1 (generell) oder Button 2 (nur jetzt).
|
||||
# Dispatch im Ursprungsraum (gleicher Pfad wie normale
|
||||
# Nachrichten-Verarbeitung, analog zum Move-Request-Flow).
|
||||
if valid_choice and ctx and ctx.get("text"):
|
||||
_CURRENT_ORIGIN_ROOM.set(origin_room_id)
|
||||
_THEMATIC_TARGET_ROOM.set("")
|
||||
_set_room_context_origin(f"user:{ctx.get('username', '')}", origin_room_id)
|
||||
routing_counter.increment('widerspruch_clarify_dispatched')
|
||||
logger.info(
|
||||
"[rocketchat] HOTFIX 3: Widerspruch-Clarify dispatch — "
|
||||
"room=%s, text=%s",
|
||||
origin_room_id[:8], ctx["text"][:80],
|
||||
)
|
||||
origin_room_info = self._room_info.get(origin_room_id, {})
|
||||
origin_room_name = origin_room_info.get("name", room_name)
|
||||
origin_room_type = origin_room_info.get("type", room_type)
|
||||
await self._dispatch_agent(
|
||||
text=ctx["text"],
|
||||
room_id=origin_room_id,
|
||||
room_name=origin_room_name,
|
||||
room_type=origin_room_type,
|
||||
username=ctx.get("username", ""),
|
||||
msg_id=ctx.get("msg_id", ""),
|
||||
msg_obj=ctx.get("msg_obj", {}),
|
||||
is_button_response=ctx.get("is_button_response", False),
|
||||
category="",
|
||||
)
|
||||
# P2-Korrektur: Widerspruch-State vollständig leeren, damit keine
|
||||
# stale Einträge (z.B. bei unbekannter choice) zurückbleiben.
|
||||
self._widerspruch_state.clear()
|
||||
@@ -3515,6 +3550,11 @@ class RocketChatAdapter(BasePlatformAdapter):
|
||||
room_id=room_id,
|
||||
origin_room_id=room_id,
|
||||
topic=topic,
|
||||
text=display_text,
|
||||
username=username,
|
||||
msg_id=msg_id,
|
||||
msg_obj=msg_obj,
|
||||
is_button_response=is_button_response,
|
||||
)
|
||||
routing_counter.increment('widerspruch_clarify_sent')
|
||||
logger.info(
|
||||
@@ -7764,6 +7804,11 @@ class RocketChatAdapter(BasePlatformAdapter):
|
||||
room_id: str,
|
||||
origin_room_id: str,
|
||||
topic: str,
|
||||
text: str = "",
|
||||
username: str = "",
|
||||
msg_id: str = "",
|
||||
msg_obj: dict | None = None,
|
||||
is_button_response: bool = False,
|
||||
) -> bool:
|
||||
"""Sende den Widerspruch-Clarify mit zwei Buttons (V8.5).
|
||||
|
||||
@@ -7788,10 +7833,20 @@ class RocketChatAdapter(BasePlatformAdapter):
|
||||
return False
|
||||
|
||||
clarify_id = uuid.uuid4().hex[:12]
|
||||
# Kontext für den WIDERSPRUCH:-Button-Handler speichern
|
||||
# Kontext für den WIDERSPRUCH:-Button-Handler speichern.
|
||||
# HOTFIX 3: Zusätzlich die Originalnachricht (text, username, msg_id,
|
||||
# msg_obj, is_button_response) speichern, damit sie nach dem
|
||||
# Button-Klick an den Agenten dispatched werden kann. Der Widerspruch
|
||||
# klärt nur die Routing-Entscheidung — die Nachricht selbst muss
|
||||
# immer beantwortet werden.
|
||||
self._widerspruch_state[clarify_id] = {
|
||||
"origin_room_id": origin_room_id,
|
||||
"topic": topic,
|
||||
"text": text,
|
||||
"username": username,
|
||||
"msg_id": msg_id,
|
||||
"msg_obj": msg_obj or {},
|
||||
"is_button_response": is_button_response,
|
||||
}
|
||||
|
||||
from archivar.button_builder import build_button_attachments
|
||||
|
||||
@@ -0,0 +1,235 @@
|
||||
"""
|
||||
HOTFIX 3: Originalnachricht wird nach Widerspruch-Clarify an den Agenten dispatched.
|
||||
|
||||
Bug: Nach dem Widerspruch-Clarify (Button 1 'generell hier her' oder Button 2
|
||||
'nur jetzt hier her') wurde die Originalnachricht NIE an den Agenten
|
||||
weitergeleitet. Der Agent hat die Frage nie gesehen und nie geantwortet.
|
||||
|
||||
Fix:
|
||||
1. `_send_widerspruch_clarify` speichert jetzt die Originalnachricht
|
||||
(text, username, msg_id, msg_obj, is_button_response) in `_widerspruch_state`.
|
||||
2. Der WIDERSPRUCH:-Handler dispatched nach set_never_route/stay_once die
|
||||
Originalnachricht an den Agenten im Ursprungsraum (gleicher Pfad wie
|
||||
normale Nachrichten-Verarbeitung).
|
||||
|
||||
Diese Tests verifizieren:
|
||||
- Button 1 (generell) → set_never_route UND Originalnachricht dispatched.
|
||||
- Button 2 (nur jetzt) → stay_once UND Originalnachricht dispatched.
|
||||
- Dispatch geht in den Ursprungsraum (nicht in einen Zielraum).
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
||||
|
||||
|
||||
def _make_adapter():
|
||||
"""Minimaler Mock-Adapter, der den WIDERSPRUCH:-Handler erreicht.
|
||||
|
||||
_MULTIROOM() wird auf False gepatcht, damit die Archivar-/Onboarding-
|
||||
Guards (die vor dem WIDERSPRUCH-Handler liegen) übersprungen werden.
|
||||
Der WIDERSPRUCH-Handler selbst liegt VOR dem Archivar-Block und ist
|
||||
unabhängig von _MULTIROOM().
|
||||
"""
|
||||
from plugin.adapter import RocketChatAdapter
|
||||
|
||||
adapter = RocketChatAdapter.__new__(RocketChatAdapter)
|
||||
adapter._widerspruch_state = {}
|
||||
adapter._recent_auto_routes = {}
|
||||
adapter._room_info = {}
|
||||
adapter._rooms_with_pending_questions = set()
|
||||
adapter._clarify_state = {}
|
||||
adapter._pending_clarify_dispatch = {}
|
||||
adapter._user_rooms = {}
|
||||
adapter._last_activity = 0.0
|
||||
adapter.user = "testbot"
|
||||
adapter._presence_engine = None
|
||||
adapter._conversation_history = None
|
||||
adapter._multiroom_context = None
|
||||
adapter.archivar = None
|
||||
adapter._inline_commands = MagicMock()
|
||||
adapter._room_map_summary = ""
|
||||
adapter._onboarding_state = {}
|
||||
adapter._onboarding_sm = None
|
||||
|
||||
adapter.rest = MagicMock()
|
||||
adapter.rest.auth_token = "test-token"
|
||||
adapter._post_message_with_retry = AsyncMock(return_value="msg_id_123")
|
||||
adapter._dispatch_agent = AsyncMock()
|
||||
return adapter
|
||||
|
||||
|
||||
def _make_msg_obj(text: str, room_id: str = "room_origin", username: str = "andreas"):
|
||||
return {
|
||||
"_id": "msg_orig_001",
|
||||
"rid": room_id,
|
||||
"msg": text,
|
||||
"u": {"username": username, "_id": "user_andreas"},
|
||||
}
|
||||
|
||||
|
||||
class TestWiderspruchThenDispatch:
|
||||
"""Nach Widerspruch-Button-Klick wird die Originalnachricht dispatched."""
|
||||
|
||||
def test_button_1_generell_dispatches_original_message(self):
|
||||
"""Button 1 ('generell hier her') → set_never_route + Dispatch im Ursprungsraum."""
|
||||
adapter = _make_adapter()
|
||||
origin_room = "room_origin"
|
||||
topic = "dokumentation"
|
||||
clarify_id = "abc123def456"
|
||||
|
||||
# Originalnachricht wurde beim Clarify-Auslösen gespeichert
|
||||
adapter._widerspruch_state[clarify_id] = {
|
||||
"origin_room_id": origin_room,
|
||||
"topic": topic,
|
||||
"text": "Bitte dokumentiere das neue Feature",
|
||||
"username": "andreas",
|
||||
"msg_id": "msg_orig_001",
|
||||
"msg_obj": {"_id": "msg_orig_001"},
|
||||
"is_button_response": False,
|
||||
}
|
||||
adapter._room_info[origin_room] = {"name": "Test-Room", "type": "c"}
|
||||
|
||||
# Archivar-Mock mit echtem State für set_never_route
|
||||
from plugin.archivar.archivar_state import ArchivarState
|
||||
state = ArchivarState.__new__(ArchivarState)
|
||||
state._never_route = {}
|
||||
state._loaded = True
|
||||
state._state_path = "/tmp/state.json"
|
||||
state._stay_tracker = {}
|
||||
state._keyword_weights = {}
|
||||
state.save = MagicMock(return_value=True)
|
||||
state.load = MagicMock()
|
||||
archivar = MagicMock()
|
||||
archivar._state = state
|
||||
archivar._stats = {"onboarding_active": False}
|
||||
archivar.lazy_onboarding_check = MagicMock(return_value=None)
|
||||
adapter.archivar = archivar
|
||||
|
||||
with patch("plugin.adapter._MULTIROOM", return_value=True):
|
||||
asyncio.run(
|
||||
adapter._process_incoming(
|
||||
_make_msg_obj(f"WIDERSPRUCH:{clarify_id}:1", room_id=origin_room)
|
||||
)
|
||||
)
|
||||
|
||||
# set_never_route wurde gesetzt
|
||||
assert state.is_never_route(origin_room, topic) is True
|
||||
|
||||
# Originalnachricht wurde an den Agenten dispatched
|
||||
adapter._dispatch_agent.assert_awaited_once()
|
||||
kwargs = adapter._dispatch_agent.call_args.kwargs
|
||||
assert kwargs["text"] == "Bitte dokumentiere das neue Feature"
|
||||
assert kwargs["room_id"] == origin_room
|
||||
assert kwargs["username"] == "andreas"
|
||||
assert kwargs["msg_id"] == "msg_orig_001"
|
||||
|
||||
# State wurde geleert
|
||||
assert len(adapter._widerspruch_state) == 0
|
||||
|
||||
def test_button_2_stay_once_dispatches_original_message(self):
|
||||
"""Button 2 ('nur jetzt hier her') → stay_once + Dispatch im Ursprungsraum."""
|
||||
adapter = _make_adapter()
|
||||
origin_room = "room_origin"
|
||||
clarify_id = "abc123def456"
|
||||
|
||||
adapter._widerspruch_state[clarify_id] = {
|
||||
"origin_room_id": origin_room,
|
||||
"topic": "dokumentation",
|
||||
"text": "Bitte dokumentiere das neue Feature",
|
||||
"username": "andreas",
|
||||
"msg_id": "msg_orig_001",
|
||||
"msg_obj": {"_id": "msg_orig_001"},
|
||||
"is_button_response": False,
|
||||
}
|
||||
adapter._room_info[origin_room] = {"name": "Test-Room", "type": "c"}
|
||||
# Vorherige Auto-Route existiert → wird durch stay_once gelöscht
|
||||
adapter._recent_auto_routes[origin_room] = {
|
||||
"origin_room": origin_room,
|
||||
"target_room": "room_target",
|
||||
"topic": "dokumentation",
|
||||
"timestamp": 1234567890.0,
|
||||
"source": "auto",
|
||||
}
|
||||
|
||||
with patch("plugin.adapter._MULTIROOM", return_value=True):
|
||||
asyncio.run(
|
||||
adapter._process_incoming(
|
||||
_make_msg_obj(f"WIDERSPRUCH:{clarify_id}:2", room_id=origin_room)
|
||||
)
|
||||
)
|
||||
|
||||
# stay_once: _recent_auto_routes gelöscht, KEIN never_route
|
||||
assert origin_room not in adapter._recent_auto_routes
|
||||
|
||||
# Originalnachricht wurde an den Agenten dispatched
|
||||
adapter._dispatch_agent.assert_awaited_once()
|
||||
kwargs = adapter._dispatch_agent.call_args.kwargs
|
||||
assert kwargs["text"] == "Bitte dokumentiere das neue Feature"
|
||||
assert kwargs["room_id"] == origin_room
|
||||
|
||||
# State wurde geleert
|
||||
assert len(adapter._widerspruch_state) == 0
|
||||
|
||||
def test_dispatch_goes_to_origin_room_not_target(self):
|
||||
"""Dispatch geht in den Ursprungsraum, nicht in einen Zielraum."""
|
||||
adapter = _make_adapter()
|
||||
origin_room = "room_origin"
|
||||
clarify_id = "abc123def456"
|
||||
|
||||
adapter._widerspruch_state[clarify_id] = {
|
||||
"origin_room_id": origin_room,
|
||||
"topic": "dokumentation",
|
||||
"text": "Bitte dokumentiere das neue Feature",
|
||||
"username": "andreas",
|
||||
"msg_id": "msg_orig_001",
|
||||
"msg_obj": {"_id": "msg_orig_001"},
|
||||
"is_button_response": False,
|
||||
}
|
||||
adapter._room_info[origin_room] = {"name": "Origin-Room", "type": "c"}
|
||||
|
||||
with patch("plugin.adapter._MULTIROOM", return_value=True):
|
||||
asyncio.run(
|
||||
adapter._process_incoming(
|
||||
_make_msg_obj(f"WIDERSPRUCH:{clarify_id}:2", room_id=origin_room)
|
||||
)
|
||||
)
|
||||
|
||||
adapter._dispatch_agent.assert_awaited_once()
|
||||
kwargs = adapter._dispatch_agent.call_args.kwargs
|
||||
assert kwargs["room_id"] == origin_room
|
||||
assert kwargs["room_name"] == "Origin-Room"
|
||||
|
||||
def test_unknown_choice_does_not_dispatch(self):
|
||||
"""Unbekannte choice → kein Dispatch (kein stale State)."""
|
||||
adapter = _make_adapter()
|
||||
origin_room = "room_origin"
|
||||
clarify_id = "abc123def456"
|
||||
|
||||
adapter._widerspruch_state[clarify_id] = {
|
||||
"origin_room_id": origin_room,
|
||||
"topic": "dokumentation",
|
||||
"text": "Bitte dokumentiere das neue Feature",
|
||||
"username": "andreas",
|
||||
"msg_id": "msg_orig_001",
|
||||
"msg_obj": {"_id": "msg_orig_001"},
|
||||
"is_button_response": False,
|
||||
}
|
||||
|
||||
with patch("plugin.adapter._MULTIROOM", return_value=True):
|
||||
asyncio.run(
|
||||
adapter._process_incoming(
|
||||
_make_msg_obj(f"WIDERSPRUCH:{clarify_id}:99", room_id=origin_room)
|
||||
)
|
||||
)
|
||||
|
||||
# Unbekannte choice → kein Dispatch, aber State wird geleert
|
||||
adapter._dispatch_agent.assert_not_awaited()
|
||||
assert len(adapter._widerspruch_state) == 0
|
||||
Reference in New Issue
Block a user