diff --git a/plugin/adapter.py b/plugin/adapter.py index e47f8fe..1f50725 100644 --- a/plugin/adapter.py +++ b/plugin/adapter.py @@ -2202,6 +2202,8 @@ class RocketChatAdapter(BasePlatformAdapter): # Check for CLARIFY button responses is_clarify_response = text.startswith("CLARIFY:") if text else False + # P-045: ARCHIVAR_CLARIFY detection early — needed for _room_context guard + is_archivar_clarify = text.startswith("ARCHIVAR_CLARIFY:") if text else False msg_id = msg_obj.get("_id", "") room_id = msg_obj.get("rid", "") @@ -2299,8 +2301,13 @@ class RocketChatAdapter(BasePlatformAdapter): # ── P1: _room_context als persistenter Fallback (Stream-Retry ContextVar-Reset) ── # ContextVars sind asyncio-task-lokal — bei Stream-Retries in neuem Task # geht _CURRENT_ORIGIN_ROOM verloren. _room_context (global dict) überlebt. + # P-045: CLARIFY/ARCHIVAR_CLARIFY button responses dürfen _room_context + # NICHT überschreiben. Der Agent-Task hat den korrekten Kontext bereits + # via copy_context() eingefroren. Ein neues Token würde den Token-Match + # in send() brechen → "No resolved room" Fehler. virtual_chat_id = f"user:{username}" - _set_room_context_origin(virtual_chat_id, room_id) + if not is_clarify_response and not is_archivar_clarify: + _set_room_context_origin(virtual_chat_id, room_id) # ── V9: PresenceEngine — Hooks übernehmen das Status-Management ── # Kein mark_busy_started(), kein increment_background_tasks() mehr. @@ -2412,7 +2419,7 @@ class RocketChatAdapter(BasePlatformAdapter): # P-013: Button-Detection VOR CLARIFY-Filter — is_clarify_response # (CLARIFY: prefix) wird weiterhin zuerst geprüft, dieser Block # fängt NUR plain-digit Texte ab. - is_archivar_clarify = text.startswith("ARCHIVAR_CLARIFY:") if text else False + # P-045: is_archivar_clarify jetzt früh in _process_incoming gesetzt (line ~2206) if not is_clarify_response and not is_archivar_clarify: stripped = text.strip() if stripped.isdigit() and room_id in self._room_clarify: @@ -2441,9 +2448,21 @@ class RocketChatAdapter(BasePlatformAdapter): if _clarify_room_id: _affected_rooms.append(_clarify_room_id) # Clean up pending_questions — Clarify is resolved + # P-045: Debug-Logging für Root-Cause-Analyse + logger.debug( + "[rocketchat] P-045 CLARIFY plain-digit cleanup: clarify_id=%s, room_id=%s, " + "_affected_rooms=%s, _clarify_room_id=%s, " + "pending_before=%s", + clarify_id, room_id, _affected_rooms, _clarify_room_id, + sorted(self._rooms_with_pending_questions), + ) for _rid in _affected_rooms: self._rooms_with_pending_questions.discard(_rid) self._rooms_with_pending_questions.discard(room_id) + logger.debug( + "[rocketchat] P-045 CLARIFY plain-digit cleanup done: pending_after=%s", + sorted(self._rooms_with_pending_questions), + ) try: from tools.clarify_gateway import resolve_gateway_clarify resolved = resolve_gateway_clarify(clarify_id, resolved_text) @@ -2494,7 +2513,8 @@ class RocketChatAdapter(BasePlatformAdapter): return elif choice.isdigit(): # Numeric choice → resolve the clarify immediately - idx = int(choice) + # P-045: choice is 1-based (button payload uses i+1), convert to 0-based index + idx = int(choice) - 1 resolved_text: Optional[str] = None try: from tools.clarify_gateway import _entries as _clarify_entries @@ -2520,9 +2540,21 @@ class RocketChatAdapter(BasePlatformAdapter): if _clarify_room_id: _affected_rooms.append(_clarify_room_id) # Clean up pending_questions — Clarify is resolved + # P-045: Debug-Logging für Root-Cause-Analyse + logger.debug( + "[rocketchat] P-045 CLARIFY cleanup: clarify_id=%s, room_id=%s, " + "_affected_rooms=%s, _clarify_room_id=%s, " + "pending_before=%s", + clarify_id, room_id, _affected_rooms, _clarify_room_id, + sorted(self._rooms_with_pending_questions), + ) for _rid in _affected_rooms: self._rooms_with_pending_questions.discard(_rid) self._rooms_with_pending_questions.discard(room_id) + logger.debug( + "[rocketchat] P-045 CLARIFY cleanup done: pending_after=%s", + sorted(self._rooms_with_pending_questions), + ) try: from tools.clarify_gateway import resolve_gateway_clarify resolved = resolve_gateway_clarify(clarify_id, resolved_text) @@ -7613,6 +7645,11 @@ class RocketChatAdapter(BasePlatformAdapter): # P-044: Reverse mapping for robust cleanup self._clarify_room = getattr(self, '_clarify_room', {}) self._clarify_room[clarify_id] = real_room_id + logger.debug( + "[rocketchat] P-045 send_clarify: _clarify_room[%s] = %s, " + "_room_clarify[%s] = %s", + clarify_id, real_room_id, real_room_id, clarify_id, + ) # For "Other" button, we need to enable text capture mode # This is done by calling mark_awaiting_text when the user clicks "Other"