Skip to content

Commit 6908dc5

Browse files
committed
Fix RAI detection: check ALL messages in conversation, not just last message
1 parent 54b8380 commit 6908dc5

1 file changed

Lines changed: 38 additions & 18 deletions

File tree

content-gen/src/backend/orchestrator.py

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -658,25 +658,35 @@ async def process_message(
658658
for msg in event.data.conversation
659659
])
660660

661-
# Get the last message content
662-
last_msg_content = event.data.conversation[-1].text if event.data.conversation else ""
663-
last_msg_agent = event.data.conversation[-1].author_name if event.data.conversation else "unknown"
661+
# Check ALL messages in the conversation for RAI refusal
662+
# This catches cases where an early agent refused but still handed off
663+
rai_refusal_msg = None
664+
rai_refusal_agent = None
665+
for msg in event.data.conversation:
666+
if msg.role.value != "user" and msg.text:
667+
if _check_message_for_rai_refusal(msg.text):
668+
rai_refusal_msg = msg.text
669+
rai_refusal_agent = msg.author_name or "assistant"
670+
logger.info(f"RAI refusal detected from {rai_refusal_agent} in conversation history")
671+
break # Use the FIRST refusal found
664672

665-
# Check if this is an RAI refusal - if so, mark as final and don't continue
666-
is_rai_refusal = _check_message_for_rai_refusal(last_msg_content)
667-
if is_rai_refusal:
668-
logger.info(f"RAI refusal detected from {last_msg_agent}, terminating workflow")
673+
if rai_refusal_msg:
674+
logger.info(f"Terminating workflow due to RAI refusal from {rai_refusal_agent}")
669675
yield {
670676
"type": "agent_response",
671-
"agent": last_msg_agent,
672-
"content": last_msg_content,
677+
"agent": rai_refusal_agent,
678+
"content": rai_refusal_msg,
673679
"conversation_history": conversation_text,
674680
"is_final": True, # Mark as final to stop workflow
675681
"rai_blocked": True, # Flag indicating RAI block
676682
"metadata": {"conversation_id": conversation_id}
677683
}
678684
return # Exit the generator to stop processing
679685

686+
# Get the last message content for normal flow
687+
last_msg_content = event.data.conversation[-1].text if event.data.conversation else ""
688+
last_msg_agent = event.data.conversation[-1].author_name if event.data.conversation else "unknown"
689+
680690
yield {
681691
"type": "agent_response",
682692
"agent": last_msg_agent,
@@ -749,24 +759,34 @@ async def send_user_response(
749759

750760
elif isinstance(event, RequestInfoEvent):
751761
if isinstance(event.data, HandoffAgentUserRequest):
752-
# Get the last message content
753-
last_msg_content = event.data.conversation[-1].text if event.data.conversation else ""
754-
last_msg_agent = event.data.conversation[-1].author_name if event.data.conversation else "unknown"
762+
# Check ALL messages in the conversation for RAI refusal
763+
# This catches cases where an early agent refused but still handed off
764+
rai_refusal_msg = None
765+
rai_refusal_agent = None
766+
for msg in event.data.conversation:
767+
if msg.role.value != "user" and msg.text:
768+
if _check_message_for_rai_refusal(msg.text):
769+
rai_refusal_msg = msg.text
770+
rai_refusal_agent = msg.author_name or "assistant"
771+
logger.info(f"RAI refusal detected from {rai_refusal_agent} in user response flow")
772+
break # Use the FIRST refusal found
755773

756-
# Check if this is an RAI refusal - if so, mark as final and don't continue
757-
is_rai_refusal = _check_message_for_rai_refusal(last_msg_content)
758-
if is_rai_refusal:
759-
logger.info(f"RAI refusal detected from {last_msg_agent} in user response flow, terminating workflow")
774+
if rai_refusal_msg:
775+
logger.info(f"Terminating workflow due to RAI refusal from {rai_refusal_agent}")
760776
yield {
761777
"type": "agent_response",
762-
"agent": last_msg_agent,
763-
"content": last_msg_content,
778+
"agent": rai_refusal_agent,
779+
"content": rai_refusal_msg,
764780
"is_final": True, # Mark as final to stop workflow
765781
"rai_blocked": True, # Flag indicating RAI block
766782
"metadata": {"conversation_id": conversation_id}
767783
}
768784
return # Exit the generator to stop processing
769785

786+
# Get the last message content for normal flow
787+
last_msg_content = event.data.conversation[-1].text if event.data.conversation else ""
788+
last_msg_agent = event.data.conversation[-1].author_name if event.data.conversation else "unknown"
789+
770790
yield {
771791
"type": "agent_response",
772792
"agent": last_msg_agent,

0 commit comments

Comments
 (0)