fix: extract tool messages before checking FINISH_SIGNAL

**Critical Bug:**
When agent returns FINISH_SIGNAL, the code breaks immediately (line 640)
BEFORE extracting tool messages (lines 642-650). This caused tool messages
to never be captured when agent completes in single step.

**Timeline:**
1. Agent calls buy tools (MSFT, AMZN, NVDA)
2. Agent returns response with <FINISH_SIGNAL>
3. Code detects signal → break (line 640)
4. Lines 642-650 NEVER EXECUTE
5. Tool messages not captured → summarizer sees 0 tools

**Evidence from logs:**
- Console: 'Bought NVDA 10 shares'
- API: 3 trades executed (MSFT 5, AMZN 15, NVDA 10)
- Debug: 'Tool messages: 0' 

**Fix:**
Move tool extraction BEFORE stop signal check.
Agent can call tools AND return FINISH_SIGNAL in same response,
so we must process tools first.

**Impact:**
Now tool messages will be captured even when agent finishes in
single step. Summarizer will see actual trades executed.

This is the true root cause of empty tool messages in conversation_history.
This commit is contained in:
2025-11-05 00:57:22 -05:00
parent 31e346ecbb
commit 462de3adeb

View File

@@ -633,14 +633,10 @@ Summary:"""
# Capture assistant response # Capture assistant response
self._capture_message("assistant", agent_response) self._capture_message("assistant", agent_response)
# Check stop signal # Extract tool messages BEFORE checking stop signal
if STOP_SIGNAL in agent_response: # (agent may call tools AND return FINISH_SIGNAL in same response)
print("✅ Received stop signal, trading session ended")
print(agent_response)
break
# Extract tool messages and count trade actions
tool_msgs = extract_tool_messages(response) tool_msgs = extract_tool_messages(response)
print(f"[DEBUG] Extracted {len(tool_msgs)} tool messages from response")
for tool_msg in tool_msgs: for tool_msg in tool_msgs:
tool_name = getattr(tool_msg, 'name', None) or tool_msg.get('name') if isinstance(tool_msg, dict) else None tool_name = getattr(tool_msg, 'name', None) or tool_msg.get('name') if isinstance(tool_msg, dict) else None
tool_content = getattr(tool_msg, 'content', '') or tool_msg.get('content', '') if isinstance(tool_msg, dict) else str(tool_msg) tool_content = getattr(tool_msg, 'content', '') or tool_msg.get('content', '') if isinstance(tool_msg, dict) else str(tool_msg)
@@ -653,6 +649,12 @@ Summary:"""
tool_response = '\n'.join([msg.content for msg in tool_msgs]) tool_response = '\n'.join([msg.content for msg in tool_msgs])
# Check stop signal AFTER processing tools
if STOP_SIGNAL in agent_response:
print("✅ Received stop signal, trading session ended")
print(agent_response)
break
# Prepare new messages # Prepare new messages
new_messages = [ new_messages = [
{"role": "assistant", "content": agent_response}, {"role": "assistant", "content": agent_response},