feat: migrate trade tools to write to actions table (new schema)

This commit implements Task 1 from the schema migration plan:
- Trade tools (buy/sell) now write to actions table instead of old positions table
- Added trading_day_id parameter to buy/sell functions
- Updated ContextInjector to inject trading_day_id
- Updated RuntimeConfigManager to include TRADING_DAY_ID in config
- Removed P&L calculation from trade functions (now done at trading_days level)
- Added tests verifying correct behavior with new schema

Changes:
- agent_tools/tool_trade.py: Modified _buy_impl and _sell_impl to write to actions table
- agent/context_injector.py: Added trading_day_id parameter and injection logic
- api/model_day_executor.py: Updated to read trading_day_id from runtime config
- api/runtime_manager.py: Added trading_day_id to config initialization
- tests/unit/test_trade_tools_new_schema.py: New tests for new schema compliance

All tests passing.
This commit is contained in:
2025-11-04 09:18:35 -05:00
parent faa2135668
commit 7d9d093d6c
5 changed files with 282 additions and 114 deletions

View File

@@ -17,7 +17,8 @@ class ContextInjector:
client = MultiServerMCPClient(config, tool_interceptors=[interceptor])
"""
def __init__(self, signature: str, today_date: str, job_id: str = None, session_id: int = None):
def __init__(self, signature: str, today_date: str, job_id: str = None,
session_id: int = None, trading_day_id: int = None):
"""
Initialize context injector.
@@ -25,12 +26,14 @@ class ContextInjector:
signature: Model signature to inject
today_date: Trading date to inject
job_id: Job UUID to inject (optional)
session_id: Trading session ID to inject (optional, updated during execution)
session_id: Trading session ID to inject (optional, DEPRECATED)
trading_day_id: Trading day ID to inject (optional)
"""
self.signature = signature
self.today_date = today_date
self.job_id = job_id
self.session_id = session_id
self.session_id = session_id # Deprecated but kept for compatibility
self.trading_day_id = trading_day_id
async def __call__(
self,
@@ -50,7 +53,7 @@ class ContextInjector:
# Inject context parameters for trade tools
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}")
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)
@@ -60,6 +63,8 @@ class ContextInjector:
request.args["job_id"] = self.job_id
if self.session_id:
request.args["session_id"] = self.session_id
if 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}")