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:
@@ -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