@@ -209,6 +209,12 @@ async def _handle_response_format_issues(
209209 consecutive_rollbacks += 1
210210 if message_history [- 1 ]["role" ] == "assistant" :
211211 message_history .pop ()
212+ feedback = (
213+ "System Feedback: Your last output contained MCP tags. "
214+ "This format is strictly prohibited here. Please output the final answer properly, "
215+ "or formulate your thoughts without using <use_mcp_tool> tags."
216+ )
217+ message_history .append ({"role" : "user" , "content" : feedback })
212218 self .task_log .log_step (
213219 "warning" ,
214220 f"{ agent_name } | Turn: { turn_count } | Rollback" ,
@@ -235,6 +241,11 @@ async def _handle_response_format_issues(
235241 consecutive_rollbacks += 1
236242 if message_history [- 1 ]["role" ] == "assistant" :
237243 message_history .pop ()
244+ feedback = (
245+ f"System Feedback: Your last output contained refusal keywords: { matched_keywords } . "
246+ "Please fulfill the objective instead of refusing. Use tools or your knowledge."
247+ )
248+ message_history .append ({"role" : "user" , "content" : feedback })
238249 self .task_log .log_step (
239250 "warning" ,
240251 f"{ agent_name } | Turn: { turn_count } | Rollback" ,
@@ -294,9 +305,16 @@ async def _check_duplicate_query(
294305
295306 if count > 0 :
296307 if consecutive_rollbacks < self .MAX_CONSECUTIVE_ROLLBACKS - 1 :
297- message_history .pop ()
308+ if message_history and message_history [- 1 ]["role" ] == "assistant" :
309+ message_history .pop ()
298310 turn_count -= 1
299311 consecutive_rollbacks += 1
312+ feedback = (
313+ f"System Feedback: The tool '{ tool_name } ' was just called with the exact same query '{ query_str } ', "
314+ "which resulted in a loop. Please DO NOT repeat this exact call. Formulate a new plan, "
315+ "try different parameters, or use a different approach entirely."
316+ )
317+ message_history .append ({"role" : "user" , "content" : feedback })
300318 self .task_log .log_step (
301319 "warning" ,
302320 f"{ agent_name } | Turn: { turn_count } | Rollback" ,
@@ -551,10 +569,19 @@ async def run_sub_agent(
551569 tool_name , result , tool_result
552570 ):
553571 if consecutive_rollbacks < self .MAX_CONSECUTIVE_ROLLBACKS - 1 :
554- message_history .pop ()
572+ if message_history and message_history [- 1 ]["role" ] == "assistant" :
573+ message_history .pop ()
555574 turn_count -= 1
556575 consecutive_rollbacks += 1
557576 should_rollback_turn = True
577+ error_snippet = str (result )[:300 ]
578+ feedback = (
579+ f"System Feedback: The tool call '{ tool_name } ' failed with the following error/empty result:\n "
580+ f"[{ error_snippet } ]\n "
581+ "Please DO NOT repeat this exact call. Analyze the error and try a different query, "
582+ "wait, or use a different tool."
583+ )
584+ message_history .append ({"role" : "user" , "content" : feedback })
558585 self .task_log .log_step (
559586 "warning" ,
560587 f"{ sub_agent_name } | Turn: { turn_count } | Rollback" ,
@@ -1030,10 +1057,19 @@ async def run_main_agent(
10301057 consecutive_rollbacks
10311058 < self .MAX_CONSECUTIVE_ROLLBACKS - 1
10321059 ):
1033- message_history .pop ()
1060+ if message_history and message_history [- 1 ]["role" ] == "assistant" :
1061+ message_history .pop ()
10341062 turn_count -= 1
10351063 consecutive_rollbacks += 1
10361064 should_rollback_turn = True
1065+ error_snippet = str (result )[:300 ]
1066+ feedback = (
1067+ f"System Feedback: The tool call '{ tool_name } ' failed with the following error/empty result:\n "
1068+ f"[{ error_snippet } ]\n "
1069+ "Please DO NOT repeat this exact call. Analyze the error and try a different query, "
1070+ "wait, or use a different tool."
1071+ )
1072+ message_history .append ({"role" : "user" , "content" : feedback })
10371073 self .task_log .log_step (
10381074 "warning" ,
10391075 f"Main Agent | Turn: { turn_count } | Rollback" ,
0 commit comments