Compare commits

...

3 Commits

Author SHA1 Message Date
abb9cd0726 fix: capture tool messages in conversation history for summarizer
**Root Cause:**
The summarizer was not receiving tool execution results (buy/sell trades)
because they were never captured to conversation_history.

**What was captured:**
- User: 'Please analyze positions'
- Assistant: 'I will buy/sell...'
- Assistant: 'Done <FINISH_SIGNAL>'

**What was MISSING:**
- Tool: buy 14 NVDA at $185.24
- Tool: sell 1 GOOGL at $245.15

**Changes:**
- Added tool message capture in trading loop (line 649)
- Extract tool_name and tool_content from each tool message
- Capture to conversation_history before processing
- Changed message['tool_name'] to message['name'] for consistency

**Impact:**
Now the summarizer sees the actual tool results, not just the AI's
intentions. Combined with alpha.8's prompt improvements, summaries
will accurately reflect executed trades.

Fixes reasoning summaries that contradicted actual trades.
2025-11-05 00:44:24 -05:00
6d126db03c fix: improve reasoning summary to explicitly mention trades
The reasoning summary was not accurately reflecting actual trades.
For example, 2 sell trades were summarized as 'maintain core holdings'.

Changes:
- Updated prompt to require explicit mention of trades executed
- Added emphasis on buy/sell tool calls in formatted log
- Trades now highlighted at top of log with TRADES EXECUTED section
- Prompt instructs: state specific trades (symbols, quantities, action)

Example before: 'chose to maintain core holdings'
Example after: 'sold 1 GOOGL and 1 AMZN to reduce exposure'

This ensures reasoning field accurately describes what the AI actually did.
2025-11-05 00:41:59 -05:00
1e7bdb509b chore: remove debug logging from ContextInjector
Removed noisy debug print statements that were added during
troubleshooting. The context injection is now working correctly
and no longer needs diagnostic output.

Cleaned up:
- Entry point logging
- Before/after injection logging
- Tool name and args logging
2025-11-05 00:31:16 -05:00
3 changed files with 27 additions and 15 deletions

View File

@@ -419,7 +419,7 @@ class BaseAgent:
} }
if tool_name: if tool_name:
message["tool_name"] = tool_name message["name"] = tool_name # Use "name" not "tool_name" for consistency with summarizer
if tool_input: if tool_input:
message["tool_input"] = tool_input message["tool_input"] = tool_input
@@ -643,6 +643,11 @@ Summary:"""
tool_msgs = extract_tool_messages(response) tool_msgs = extract_tool_messages(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)
# Capture tool message to conversation history
self._capture_message("tool", tool_content, tool_name=tool_name)
if tool_name in ['buy', 'sell']: if tool_name in ['buy', 'sell']:
action_count += 1 action_count += 1

View File

@@ -52,10 +52,6 @@ class ContextInjector:
""" """
# Inject context parameters for trade tools # Inject context parameters for trade tools
if request.name in ["buy", "sell"]: if request.name in ["buy", "sell"]:
# Debug: Log self attributes BEFORE injection
print(f"[ContextInjector.__call__] ENTRY: id={id(self)}, self.signature={self.signature}, self.today_date={self.today_date}, self.job_id={self.job_id}, self.session_id={self.session_id}, self.trading_day_id={self.trading_day_id}")
print(f"[ContextInjector.__call__] Args BEFORE injection: {request.args}")
# ALWAYS inject/override context parameters (don't trust AI-provided values) # ALWAYS inject/override context parameters (don't trust AI-provided values)
request.args["signature"] = self.signature request.args["signature"] = self.signature
request.args["today_date"] = self.today_date request.args["today_date"] = self.today_date
@@ -66,8 +62,5 @@ class ContextInjector:
if self.trading_day_id: if self.trading_day_id:
request.args["trading_day_id"] = self.trading_day_id request.args["trading_day_id"] = self.trading_day_id
# Debug logging
print(f"[ContextInjector] Tool: {request.name}, Args after injection: {request.args}")
# Call the actual tool handler # Call the actual tool handler
return await handler(request) return await handler(request)

View File

@@ -36,15 +36,17 @@ class ReasoningSummarizer:
summary_prompt = f"""You are reviewing your own trading decisions for the day. summary_prompt = f"""You are reviewing your own trading decisions for the day.
Summarize your trading strategy and key decisions in 2-3 sentences. Summarize your trading strategy and key decisions in 2-3 sentences.
IMPORTANT: Explicitly state what trades you executed (e.g., "sold 2 GOOGL shares" or "bought 10 NVDA shares"). If you made no trades, state that clearly.
Focus on: Focus on:
- What you analyzed - What specific trades you executed (buy/sell, symbols, quantities)
- Why you made the trades you did - Why you made those trades
- Your overall strategy for the day - Your overall strategy for the day
Trading session log: Trading session log:
{log_text} {log_text}
Provide a concise summary:""" Provide a concise summary that includes the actual trades executed:"""
response = await self.model.ainvoke([ response = await self.model.ainvoke([
{"role": "user", "content": summary_prompt} {"role": "user", "content": summary_prompt}
@@ -67,21 +69,33 @@ Provide a concise summary:"""
reasoning_log: List of message dicts reasoning_log: List of message dicts
Returns: Returns:
Formatted text representation Formatted text representation with emphasis on trades
""" """
formatted_parts = [] formatted_parts = []
trades_executed = []
for msg in reasoning_log: for msg in reasoning_log:
role = msg.get("role", "") role = msg.get("role", "")
content = msg.get("content", "") content = msg.get("content", "")
tool_name = msg.get("name", "")
if role == "assistant": if role == "assistant":
# AI's thoughts # AI's thoughts
formatted_parts.append(f"AI: {content[:200]}") formatted_parts.append(f"AI: {content[:200]}")
elif role == "tool": elif role == "tool":
# Tool results # Highlight trade tool calls
tool_name = msg.get("name", "tool") if tool_name in ["buy", "sell"]:
formatted_parts.append(f"{tool_name}: {content[:100]}") trades_executed.append(f"{tool_name.upper()}: {content[:150]}")
formatted_parts.append(f"TRADE - {tool_name.upper()}: {content[:150]}")
else:
# Other tool results (search, price, etc.)
formatted_parts.append(f"{tool_name}: {content[:100]}")
# Add summary of trades at the top
if trades_executed:
trade_summary = f"TRADES EXECUTED ({len(trades_executed)}):\n" + "\n".join(trades_executed)
formatted_parts.insert(0, trade_summary)
formatted_parts.insert(1, "\n--- FULL LOG ---")
return "\n".join(formatted_parts) return "\n".join(formatted_parts)