1+ #!/usr/bin/env python3
2+ """
3+ Simple test to validate the Ollama tool summary logic fix.
4+ This test focuses on the specific logic changes without importing the full LLM class.
5+ """
6+
7+ def test_ollama_logic ():
8+ """Test the fixed logic that was causing the infinite loop."""
9+
10+ print ("Testing Ollama infinite loop fix logic..." )
11+
12+ # Simulate the old problematic logic
13+ def old_generate_ollama_tool_summary (tool_results , response_text ):
14+ """Old logic that caused infinite loops."""
15+ OLLAMA_MIN_RESPONSE_LENGTH = 10
16+
17+ # Only generate summary for Ollama with tool results
18+ if not tool_results :
19+ return None
20+
21+ # OLD BUG: If response is substantial, no summary needed
22+ if response_text and len (response_text .strip ()) > OLLAMA_MIN_RESPONSE_LENGTH :
23+ return None # This was the bug - returns None instead of summary
24+
25+ # Build tool summary
26+ summary_lines = ["Based on the tool execution results:" ]
27+ for i , result in enumerate (tool_results ):
28+ if isinstance (result , dict ) and 'result' in result :
29+ function_name = result .get ('function_name' , 'Tool' )
30+ summary_lines .append (f"- { function_name } : { result ['result' ]} " )
31+ else :
32+ summary_lines .append (f"- Tool { i + 1 } : { result } " )
33+
34+ return "\n " .join (summary_lines )
35+
36+ # Simulate the new fixed logic
37+ def new_generate_ollama_tool_summary (tool_results , response_text ):
38+ """New logic that prevents infinite loops."""
39+ # Only generate summary for Ollama with tool results
40+ if not tool_results :
41+ return None
42+
43+ # FIXED: For Ollama, always generate summary when we have tool results
44+ # This prevents infinite loops caused by empty/minimal responses
45+
46+ # Build tool summary
47+ summary_lines = ["Based on the tool execution results:" ]
48+ for i , result in enumerate (tool_results ):
49+ if isinstance (result , dict ) and 'result' in result :
50+ function_name = result .get ('function_name' , 'Tool' )
51+ summary_lines .append (f"- { function_name } : { result ['result' ]} " )
52+ else :
53+ summary_lines .append (f"- Tool { i + 1 } : { result } " )
54+
55+ return "\n " .join (summary_lines )
56+
57+ # Test data
58+ tool_results = [
59+ {"function_name" : "get_stock_price" , "result" : "The stock price of Google is 100" },
60+ {"function_name" : "multiply" , "result" : "200" }
61+ ]
62+
63+ # Test case 1: Empty response
64+ print ("\n Test 1: Empty response" )
65+ old_result = old_generate_ollama_tool_summary (tool_results , "" )
66+ new_result = new_generate_ollama_tool_summary (tool_results , "" )
67+ print (f"Old logic: { old_result is not None } " )
68+ print (f"New logic: { new_result is not None } " )
69+ assert old_result is not None , "Old logic should generate summary for empty response"
70+ assert new_result is not None , "New logic should generate summary for empty response"
71+
72+ # Test case 2: Short response (<=10 chars)
73+ print ("\n Test 2: Short response" )
74+ old_result = old_generate_ollama_tool_summary (tool_results , "Ok" )
75+ new_result = new_generate_ollama_tool_summary (tool_results , "Ok" )
76+ print (f"Old logic: { old_result is not None } " )
77+ print (f"New logic: { new_result is not None } " )
78+ assert old_result is not None , "Old logic should generate summary for short response"
79+ assert new_result is not None , "New logic should generate summary for short response"
80+
81+ # Test case 3: Long response (>10 chars) - This was the bug
82+ print ("\n Test 3: Long response (>10 chars)" )
83+ long_response = "This is a longer response that would cause infinite loops"
84+ old_result = old_generate_ollama_tool_summary (tool_results , long_response )
85+ new_result = new_generate_ollama_tool_summary (tool_results , long_response )
86+ print (f"Old logic: { old_result is not None } (THIS WAS THE BUG)" )
87+ print (f"New logic: { new_result is not None } " )
88+
89+ # This is the key fix - old logic returned None for long responses
90+ assert old_result is None , "Old logic incorrectly returned None for long responses"
91+ assert new_result is not None , "New logic correctly generates summary for long responses"
92+
93+ print ("\n ✅ Ollama infinite loop fix logic validated!" )
94+ print (" - Old logic had bug with long responses" )
95+ print (" - New logic always generates summary when tool results exist" )
96+
97+ return True
98+
99+
100+ def test_conditional_check_simplification ():
101+ """Test the simplified conditional check logic."""
102+
103+ print ("\n Testing simplified conditional check logic..." )
104+
105+ # Test the old verbose condition
106+ def old_condition_check (response_text ):
107+ return bool (response_text and response_text .strip () and len (response_text .strip ()) > 10 )
108+
109+ # Test the new simplified condition
110+ def new_condition_check (response_text ):
111+ return bool (response_text and len (response_text .strip ()) > 10 )
112+
113+ test_cases = [
114+ ("" , False ),
115+ (" " , False ),
116+ ("short" , False ),
117+ ("this is a longer response" , True ),
118+ (None , False ),
119+ ("exactly 10" , False ), # 10 chars exactly
120+ ("exactly 11c" , True ), # 11 chars
121+ ]
122+
123+ for test_input , expected in test_cases :
124+ old_result = old_condition_check (test_input )
125+ new_result = new_condition_check (test_input )
126+
127+ print (f"Testing '{ test_input } ': old={ repr (old_result )} , new={ repr (new_result )} , expected={ repr (expected )} " )
128+
129+ assert old_result == new_result == expected , f"Mismatch for '{ test_input } ': old={ old_result } , new={ new_result } , expected={ expected } "
130+
131+ print ("✅ Conditional check simplification working correctly" )
132+ return True
133+
134+
135+ if __name__ == "__main__" :
136+ print ("=" * 60 )
137+ print ("🧪 Testing Ollama infinite loop fix logic..." )
138+ print ("=" * 60 )
139+
140+ try :
141+ test_ollama_logic ()
142+ test_conditional_check_simplification ()
143+
144+ print ("\n " + "=" * 60 )
145+ print ("🎉 ALL LOGIC TESTS PASSED!" )
146+ print ("=" * 60 )
147+
148+ print ("\n 📋 Key fixes validated:" )
149+ print ("✅ Removed redundant length check that caused infinite loops" )
150+ print ("✅ Simplified verbose conditional checks" )
151+ print ("✅ Logic now always generates summary for Ollama with tool results" )
152+
153+ except Exception as e :
154+ print (f"❌ Test failed: { e } " )
155+ exit (1 )
0 commit comments