mirror of
https://github.com/Xe138/AI-Trader.git
synced 2026-04-01 17:17:24 -04:00
refactor: remove JSONL logging code from BaseAgent
- Remove _log_message() and _setup_logging() methods - Remove all calls to logging methods in run_trading_session() - Update log_path parameter docstring for clarity - Update integration test to verify conversation history instead of JSONL files - Reasoning logs now stored exclusively in database via model_day_executor - Conversation history tracking preserved in memory Related: Task 6 of reasoning logs API feature
This commit is contained in:
@@ -84,7 +84,7 @@ class BaseAgent:
|
||||
basemodel: Base model name
|
||||
stock_symbols: List of stock symbols, defaults to NASDAQ 100
|
||||
mcp_config: MCP tool configuration, including port and URL information
|
||||
log_path: Log path, defaults to ./data/agent_data
|
||||
log_path: Data path for position files (JSONL logging removed, kept for backward compatibility)
|
||||
max_steps: Maximum reasoning steps
|
||||
max_retries: Maximum retry attempts
|
||||
base_delay: Base delay time for retries
|
||||
@@ -105,7 +105,8 @@ class BaseAgent:
|
||||
# Set MCP configuration
|
||||
self.mcp_config = mcp_config or self._get_default_mcp_config()
|
||||
|
||||
# Set log path (apply deployment mode path resolution)
|
||||
# Set data path (apply deployment mode path resolution)
|
||||
# Note: Used for position files only; JSONL logging has been removed
|
||||
self.base_log_path = get_data_path(log_path or "./data/agent_data")
|
||||
|
||||
# Set OpenAI configuration
|
||||
@@ -311,23 +312,6 @@ Summary:"""
|
||||
|
||||
return loop.run_until_complete(self.generate_summary(content, max_length))
|
||||
|
||||
def _setup_logging(self, today_date: str) -> str:
|
||||
"""Set up log file path"""
|
||||
log_path = os.path.join(self.base_log_path, self.signature, 'log', today_date)
|
||||
if not os.path.exists(log_path):
|
||||
os.makedirs(log_path)
|
||||
return os.path.join(log_path, "log.jsonl")
|
||||
|
||||
def _log_message(self, log_file: str, new_messages: List[Dict[str, str]]) -> None:
|
||||
"""Log messages to log file"""
|
||||
log_entry = {
|
||||
"timestamp": datetime.now().isoformat(),
|
||||
"signature": self.signature,
|
||||
"new_messages": new_messages
|
||||
}
|
||||
with open(log_file, "a", encoding="utf-8") as f:
|
||||
f.write(json.dumps(log_entry, ensure_ascii=False) + "\n")
|
||||
|
||||
async def _ainvoke_with_retry(self, message: List[Dict[str, str]]) -> Any:
|
||||
"""Agent invocation with retry"""
|
||||
for attempt in range(1, self.max_retries + 1):
|
||||
@@ -359,9 +343,6 @@ Summary:"""
|
||||
if is_dev_mode():
|
||||
self.model.date = today_date
|
||||
|
||||
# Set up logging
|
||||
log_file = self._setup_logging(today_date)
|
||||
|
||||
# Get system prompt
|
||||
system_prompt = get_agent_system_prompt(today_date, self.signature)
|
||||
|
||||
@@ -380,9 +361,6 @@ Summary:"""
|
||||
user_query = [{"role": "user", "content": user_prompt}]
|
||||
message = user_query.copy()
|
||||
|
||||
# Log initial message
|
||||
self._log_message(log_file, user_query)
|
||||
|
||||
# Trading loop
|
||||
current_step = 0
|
||||
while current_step < self.max_steps:
|
||||
@@ -403,7 +381,6 @@ Summary:"""
|
||||
if STOP_SIGNAL in agent_response:
|
||||
print("✅ Received stop signal, trading session ended")
|
||||
print(agent_response)
|
||||
self._log_message(log_file, [{"role": "assistant", "content": agent_response}])
|
||||
break
|
||||
|
||||
# Extract tool messages
|
||||
@@ -419,10 +396,6 @@ Summary:"""
|
||||
# Add new messages
|
||||
message.extend(new_messages)
|
||||
|
||||
# Log messages
|
||||
self._log_message(log_file, new_messages[0])
|
||||
self._log_message(log_file, new_messages[1])
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Trading session error: {str(e)}")
|
||||
print(f"Error details: {e}")
|
||||
|
||||
@@ -50,7 +50,7 @@ def test_dev_mode_full_simulation(dev_mode_env, tmp_path):
|
||||
- BaseAgent can initialize with mock model
|
||||
- Mock model is used instead of real AI
|
||||
- Trading session executes successfully
|
||||
- Logs are created correctly
|
||||
- Conversation history is captured correctly
|
||||
- Mock responses contain expected content (AAPL on day 1)
|
||||
|
||||
NOTE: This test requires the full agent stack including MCP adapters.
|
||||
@@ -108,17 +108,13 @@ def test_dev_mode_full_simulation(dev_mode_env, tmp_path):
|
||||
# Run single day
|
||||
asyncio.run(agent.run_trading_session("2025-01-01"))
|
||||
|
||||
# Verify logs were created
|
||||
log_path = Path(agent.base_log_path) / agent.signature / "log" / "2025-01-01" / "log.jsonl"
|
||||
assert log_path.exists()
|
||||
# Verify conversation history was captured (JSONL logging removed)
|
||||
conversation = agent.get_conversation_history()
|
||||
assert len(conversation) > 0
|
||||
|
||||
# Verify log content
|
||||
with open(log_path, "r") as f:
|
||||
logs = [json.loads(line) for line in f]
|
||||
|
||||
assert len(logs) > 0
|
||||
# Day 1 should mention AAPL (first stock in rotation)
|
||||
assert any("AAPL" in str(log) for log in logs)
|
||||
conversation_str = json.dumps(conversation)
|
||||
assert "AAPL" in conversation_str
|
||||
except Exception as e:
|
||||
pytest.skip(f"Test requires MCP services running: {e}")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user