|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Diagnostic script to verify ReActMasterV2 loop tool_messages bug. |
| 4 | +
|
| 5 | +Issue: In AgentRunMode.LOOP mode, tool call results are NOT appended to all_tool_messages, |
| 6 | +causing LLM to repeatedly call the same tool because it doesn't see previous results. |
| 7 | +
|
| 8 | +Root Cause: Line 821 in base_agent.py has condition `self.run_mode != AgentRunMode.LOOP` |
| 9 | +which skips appending tool_messages for LOOP mode agents. |
| 10 | +
|
| 11 | +Expected Behavior: Tool call results should be appended to all_tool_messages for ALL modes. |
| 12 | +""" |
| 13 | + |
| 14 | +import sys |
| 15 | +from pathlib import Path |
| 16 | + |
| 17 | +# Add project paths |
| 18 | +_project_root = Path(__file__).parent |
| 19 | +sys.path.insert(0, str(_project_root / "packages/derisk-core/src")) |
| 20 | + |
| 21 | + |
| 22 | +def check_base_agent_code(): |
| 23 | + """Check if the bug exists in base_agent.py""" |
| 24 | + print("=" * 80) |
| 25 | + print("Checking base_agent.py for LOOP mode tool_messages bug") |
| 26 | + print("=" * 80) |
| 27 | + |
| 28 | + base_agent_path = ( |
| 29 | + _project_root / "packages/derisk-core/src/derisk/agent/core/base_agent.py" |
| 30 | + ) |
| 31 | + |
| 32 | + if not base_agent_path.exists(): |
| 33 | + print(f"❌ File not found: {base_agent_path}") |
| 34 | + return False |
| 35 | + |
| 36 | + with open(base_agent_path, "r") as f: |
| 37 | + lines = f.readlines() |
| 38 | + |
| 39 | + # Find the problematic code section (around line 820-827) |
| 40 | + print("\n📍 Checking lines 820-827 for the bug condition:\n") |
| 41 | + |
| 42 | + bug_found = False |
| 43 | + for i in range(819, min(828, len(lines))): |
| 44 | + line = lines[i] |
| 45 | + line_num = i + 1 |
| 46 | + print(f" {line_num:4d}: {line.rstrip()}") |
| 47 | + |
| 48 | + # Check for the bug condition |
| 49 | + if "if self.run_mode != AgentRunMode.LOOP:" in line: |
| 50 | + bug_found = True |
| 51 | + print( |
| 52 | + "\n ⚠️ BUG FOUND: This condition prevents LOOP mode agents from getting tool_messages!" |
| 53 | + ) |
| 54 | + |
| 55 | + print("\n" + "-" * 80) |
| 56 | + |
| 57 | + if bug_found: |
| 58 | + print("❌ BUG CONFIRMED: LOOP mode agents will NOT receive tool call results") |
| 59 | + print("\n🔧 Impact:") |
| 60 | + print(" - ReActMasterV2 (LOOP mode) will repeatedly call the same tool") |
| 61 | + print(" - LLM doesn't see previous tool results in next iteration") |
| 62 | + print(" - WorkLog records tools but doesn't inject them to LLM prompt") |
| 63 | + print("\n💡 Fix: Remove the 'self.run_mode != AgentRunMode.LOOP' condition") |
| 64 | + print(" OR handle LOOP mode specially to inject tool messages") |
| 65 | + else: |
| 66 | + print("✅ No bug found in this section (may have been fixed)") |
| 67 | + |
| 68 | + return bug_found |
| 69 | + |
| 70 | + |
| 71 | +def explain_the_bug(): |
| 72 | + """Explain the bug in detail""" |
| 73 | + print("\n" + "=" * 80) |
| 74 | + print("DETAILED BUG EXPLANATION") |
| 75 | + print("=" * 80) |
| 76 | + |
| 77 | + print(""" |
| 78 | +## Problem |
| 79 | +
|
| 80 | +ReActMasterV2 uses AgentRunMode.LOOP mode to execute multiple iterations. |
| 81 | +In each iteration, it should: |
| 82 | + 1. Call a tool |
| 83 | + 2. Get result |
| 84 | + 3. Pass result to LLM in next iteration |
| 85 | + 4. LLM decides next action based on results |
| 86 | +
|
| 87 | +## What Actually Happens |
| 88 | +
|
| 89 | +In base_agent.py generate_reply() method (line 820-827): |
| 90 | +
|
| 91 | + if self.current_retry_counter > 0: |
| 92 | + if self.run_mode != AgentRunMode.LOOP: # ⚠️ PROBLEM: This excludes LOOP mode! |
| 93 | + if self.enable_function_call: |
| 94 | + tool_messages = self.function_callning_reply_messages(agent_llm_out, act_outs) |
| 95 | + all_tool_messages.extend(tool_messages) # ❌ NOT executed for LOOP mode |
| 96 | +
|
| 97 | +Result: |
| 98 | +- For LOOP mode agents, tool_messages are NEVER appended to all_tool_messages |
| 99 | +- LLM sees the SAME context in each iteration (no tool results) |
| 100 | +- LLM calls the same tool again → infinite loop |
| 101 | +
|
| 102 | +## Why WorkLog Doesn't Help |
| 103 | +
|
| 104 | +WorkLog injection happens only ONCE at the start (line 798-804): |
| 105 | +
|
| 106 | + if self.enable_function_call and self.current_retry_counter == 0: |
| 107 | + worklog_messages = await self._get_worklog_tool_messages() |
| 108 | + all_tool_messages.extend(worklog_messages) |
| 109 | +
|
| 110 | +The condition `self.current_retry_counter == 0` means WorkLog is only fetched once. |
| 111 | +In subsequent LOOP iterations, WorkLog is NOT re-fetched. |
| 112 | +
|
| 113 | +## Solution |
| 114 | +
|
| 115 | +Remove the `self.run_mode != AgentRunMode.LOOP` condition to allow LOOP mode agents |
| 116 | +to receive tool call results in each iteration: |
| 117 | +
|
| 118 | + if self.current_retry_counter > 0: |
| 119 | + if self.enable_function_call: |
| 120 | + tool_messages = self.function_callning_reply_messages(agent_llm_out, act_outs) |
| 121 | + all_tool_messages.extend(tool_messages) |
| 122 | +""") |
| 123 | + |
| 124 | + |
| 125 | +def suggest_fix(): |
| 126 | + """Suggest the fix""" |
| 127 | + print("\n" + "=" * 80) |
| 128 | + print("SUGGESTED FIX") |
| 129 | + print("=" * 80) |
| 130 | + |
| 131 | + print(""" |
| 132 | +## File: packages/derisk-core/src/derisk/agent/core/base_agent.py |
| 133 | +
|
| 134 | +## Location: Line 820-827 |
| 135 | +
|
| 136 | +## Current Code (BUGGY): |
| 137 | +```python |
| 138 | +if self.current_retry_counter > 0: |
| 139 | + if self.run_mode != AgentRunMode.LOOP: # ❌ Remove this condition |
| 140 | + if self.enable_function_call: |
| 141 | + tool_messages = self.function_callning_reply_messages(agent_llm_out, act_outs) |
| 142 | + all_tool_messages.extend(tool_messages) |
| 143 | +``` |
| 144 | +
|
| 145 | +## Fixed Code: |
| 146 | +```python |
| 147 | +if self.current_retry_counter > 0: |
| 148 | + if self.enable_function_call: |
| 149 | + tool_messages = self.function_callning_reply_messages(agent_llm_out, act_outs) |
| 150 | + all_tool_messages.extend(tool_messages) |
| 151 | +``` |
| 152 | +
|
| 153 | +## Why This Works: |
| 154 | +- Removes the LOOP mode exclusion |
| 155 | +- All agents (including ReActMasterV2) will now receive tool call results |
| 156 | +- LLM can see previous tool results and make informed decisions |
| 157 | +- Prevents infinite loops caused by LLM not knowing tools were already called |
| 158 | +""") |
| 159 | + |
| 160 | + |
| 161 | +def main(): |
| 162 | + print("\n" + "🔍" * 40) |
| 163 | + print("ReActMasterV2 LOOP Mode Tool Messages Bug Diagnostic") |
| 164 | + print("🔍" * 40 + "\n") |
| 165 | + |
| 166 | + # Check for the bug |
| 167 | + bug_exists = check_base_agent_code() |
| 168 | + |
| 169 | + # Explain the bug |
| 170 | + explain_the_bug() |
| 171 | + |
| 172 | + # Suggest fix |
| 173 | + suggest_fix() |
| 174 | + |
| 175 | + # Summary |
| 176 | + print("\n" + "=" * 80) |
| 177 | + print("SUMMARY") |
| 178 | + print("=" * 80) |
| 179 | + |
| 180 | + if bug_exists: |
| 181 | + print("❌ Bug confirmed in base_agent.py line 821") |
| 182 | + print("✅ Fix: Remove 'self.run_mode != AgentRunMode.LOOP' condition") |
| 183 | + print( |
| 184 | + "\nThis will resolve the issue where ReActMasterV2 repeatedly calls tools" |
| 185 | + ) |
| 186 | + print("without seeing previous results, causing infinite loops.") |
| 187 | + return 1 |
| 188 | + else: |
| 189 | + print("✅ Bug may have been fixed or code has changed") |
| 190 | + print("Please verify manually that LOOP mode agents receive tool messages") |
| 191 | + return 0 |
| 192 | + |
| 193 | + |
| 194 | +if __name__ == "__main__": |
| 195 | + sys.exit(main()) |
0 commit comments