mirror of
https://github.com/Xe138/AI-Trader.git
synced 2026-04-02 09:37:23 -04:00
Compare commits
35 Commits
v0.4.0-alp
...
v0.4.2
| Author | SHA1 | Date | |
|---|---|---|---|
| 96f61cf347 | |||
| 0eb5fcc940 | |||
| bee6afe531 | |||
| f1f76b9a99 | |||
| 277714f664 | |||
| db1341e204 | |||
| e5b83839ad | |||
| 4629bb1522 | |||
| f175139863 | |||
| 75a76bbb48 | |||
| fbe383772a | |||
| 406bb281b2 | |||
| 6ddc5abede | |||
| 5c73f30583 | |||
| b73d88ca8f | |||
| d199b093c1 | |||
| 483621f9b7 | |||
| e8939be04e | |||
| 2e0cf4d507 | |||
| 7b35394ce7 | |||
| 2d41717b2b | |||
| 7c4874715b | |||
| 6d30244fc9 | |||
| 0641ce554a | |||
| 0c6de5b74b | |||
| 0f49977700 | |||
| 27a824f4a6 | |||
| 3e50868a4d | |||
| e20dce7432 | |||
| 462de3adeb | |||
| 31e346ecbb | |||
| abb9cd0726 | |||
| 6d126db03c | |||
| 1e7bdb509b | |||
| a8d912bb4b |
89
CHANGELOG.md
89
CHANGELOG.md
@@ -7,7 +7,51 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
## [0.4.0] - 2025-11-04
|
||||
## [0.4.2] - 2025-11-07
|
||||
|
||||
### Fixed
|
||||
- **Critical:** Fixed negative cash position bug where trades calculated from initial capital instead of accumulating
|
||||
- Root cause: MCP tools return `CallToolResult` objects with position data in `structuredContent` field, but `ContextInjector` was checking `isinstance(result, dict)` which always failed
|
||||
- Impact: Each trade checked cash against initial $10,000 instead of cumulative position, allowing over-spending and resulting in negative cash balances (e.g., -$8,768.68 after 11 trades totaling $18,768.68)
|
||||
- Solution: Updated `ContextInjector` to extract position dict from `CallToolResult.structuredContent` before validation
|
||||
- Fix ensures proper intra-day position tracking with cumulative cash checks preventing over-trading
|
||||
- Updated unit tests to mock `CallToolResult` objects matching production MCP behavior
|
||||
- Locations: `agent/context_injector.py:95-109`, `tests/unit/test_context_injector.py:26-53`
|
||||
- Enabled MCP service logging by redirecting stdout/stderr from `/dev/null` to main process for better debugging
|
||||
- Previously, all MCP tool debug output was silently discarded
|
||||
- Now visible in docker logs for diagnosing parameter injection and trade execution issues
|
||||
- Location: `agent_tools/start_mcp_services.py:81-88`
|
||||
|
||||
### Fixed
|
||||
- **Critical:** Fixed stale jobs blocking new jobs after Docker container restart
|
||||
- Root cause: Jobs with status 'pending', 'downloading_data', or 'running' remained in database after container shutdown, preventing new job creation
|
||||
- Solution: Added `cleanup_stale_jobs()` method that runs on FastAPI startup to mark interrupted jobs as 'failed' or 'partial' based on completion percentage
|
||||
- Intelligent status determination: Uses existing progress tracking (completed/total model-days) to distinguish between failed (0% complete) and partial (>0% complete)
|
||||
- Detailed error messages include original status and completion counts (e.g., "Job interrupted by container restart (was running, 3/10 model-days completed)")
|
||||
- Incomplete job_details automatically marked as 'failed' with clear error messages
|
||||
- Deployment-aware: Skips cleanup in DEV mode when database is reset, always runs in PROD mode
|
||||
- Comprehensive test coverage: 6 new unit tests covering all cleanup scenarios
|
||||
- Locations: `api/job_manager.py:702-779`, `api/main.py:164-168`, `tests/unit/test_job_manager.py:451-609`
|
||||
- Fixed Pydantic validation errors when using DeepSeek models via OpenRouter
|
||||
- Root cause: LangChain's `parse_tool_call()` has a bug where it sometimes returns `args` as JSON string instead of parsed dict object
|
||||
- Solution: Added `ToolCallArgsParsingWrapper` that:
|
||||
1. Patches `parse_tool_call()` to detect and fix string args by parsing them to dict
|
||||
2. Normalizes non-standard tool_call formats (e.g., `{name, args, id}` → `{function: {name, arguments}, id}`)
|
||||
- The wrapper is defensive and only acts when needed, ensuring compatibility with all AI providers
|
||||
- Fixes validation error: `tool_calls.0.args: Input should be a valid dictionary [type=dict_type, input_value='...', input_type=str]`
|
||||
|
||||
## [0.4.1] - 2025-11-06
|
||||
|
||||
### Fixed
|
||||
- Fixed "No trading" message always displaying despite trading activity by initializing `IF_TRADE` to `True` (trades expected by default)
|
||||
- Root cause: `IF_TRADE` was initialized to `False` in runtime config but never updated when trades executed
|
||||
|
||||
### Note
|
||||
- ChatDeepSeek integration was reverted as it conflicts with OpenRouter unified gateway architecture
|
||||
- System uses `OPENAI_API_BASE` (OpenRouter) with single `OPENAI_API_KEY` for all providers
|
||||
- Sporadic DeepSeek validation errors appear to be transient and do not require code changes
|
||||
|
||||
## [0.4.0] - 2025-11-05
|
||||
|
||||
### BREAKING CHANGES
|
||||
|
||||
@@ -130,6 +174,49 @@ New `/results?reasoning=full` returns:
|
||||
- Test coverage increased with 36+ new comprehensive tests
|
||||
- Documentation updated with complete API reference and database schema details
|
||||
|
||||
### Fixed
|
||||
- **Critical:** Intra-day position tracking for sell-then-buy trades (e20dce7)
|
||||
- Sell proceeds now immediately available for subsequent buy orders within same trading session
|
||||
- ContextInjector maintains in-memory position state during trading sessions
|
||||
- Position updates accumulate after each successful trade
|
||||
- Enables agents to rebalance portfolios (sell + buy) in single session
|
||||
- Added 13 comprehensive tests for position tracking
|
||||
- **Critical:** Tool message extraction in conversation history (462de3a, abb9cd0)
|
||||
- Fixed bug where tool messages (buy/sell trades) were not captured when agent completed in single step
|
||||
- Tool extraction now happens BEFORE finish signal check
|
||||
- Reasoning summaries now accurately reflect actual trades executed
|
||||
- Resolves issue where summarizer saw 0 tools despite multiple trades
|
||||
- Reasoning summary generation improvements (6d126db)
|
||||
- Summaries now explicitly mention specific trades executed (symbols, quantities, actions)
|
||||
- Added TRADES EXECUTED section highlighting tool calls
|
||||
- Example: 'sold 1 GOOGL and 1 AMZN to reduce exposure' instead of 'maintain core holdings'
|
||||
- Final holdings calculation accuracy (a8d912b)
|
||||
- Final positions now calculated from actions instead of querying incomplete database records
|
||||
- Correctly handles first trading day with multiple trades
|
||||
- New `_calculate_final_position_from_actions()` method applies all trades to calculate final state
|
||||
- Holdings now persist correctly across all trading days
|
||||
- Added 3 comprehensive tests for final position calculation
|
||||
- Holdings persistence between trading days (aa16480)
|
||||
- Query now retrieves previous day's ending position as current day's starting position
|
||||
- Changed query from `date <=` to `date <` to prevent returning incomplete current-day records
|
||||
- Fixes empty starting_position/final_position in API responses despite successful trades
|
||||
- Updated tests to verify correct previous-day retrieval
|
||||
- Context injector trading_day_id synchronization (05620fa)
|
||||
- ContextInjector now updated with trading_day_id after record creation
|
||||
- Fixes "Trade failed: trading_day_id not found in runtime config" error
|
||||
- MCP tools now correctly receive trading_day_id via context injection
|
||||
- Schema migration compatibility fixes (7c71a04)
|
||||
- Updated position queries to use new trading_days schema instead of obsolete positions table
|
||||
- Removed obsolete add_no_trade_record_to_db function calls
|
||||
- Fixes "no such table: positions" error
|
||||
- Simplified _handle_trading_result logic
|
||||
- Database referential integrity (9da65c2)
|
||||
- Corrected Database default path from "data/trading.db" to "data/jobs.db"
|
||||
- Ensures all components use same database file
|
||||
- Fixes FOREIGN KEY constraint failures when creating trading_day records
|
||||
- Debug logging cleanup (1e7bdb5)
|
||||
- Removed verbose debug logging from ContextInjector for cleaner output
|
||||
|
||||
## [0.3.1] - 2025-11-03
|
||||
|
||||
### Fixed
|
||||
|
||||
28
CLAUDE.md
28
CLAUDE.md
@@ -202,6 +202,34 @@ bash main.sh
|
||||
- Search results: News filtered by publication date
|
||||
- All tools enforce temporal boundaries via `TODAY_DATE` from `runtime_env.json`
|
||||
|
||||
### Duplicate Simulation Prevention
|
||||
|
||||
**Automatic Skip Logic:**
|
||||
- `JobManager.create_job()` checks database for already-completed model-day pairs
|
||||
- Skips completed simulations automatically
|
||||
- Returns warnings list with skipped pairs
|
||||
- Raises `ValueError` if all requested simulations are already completed
|
||||
|
||||
**Example:**
|
||||
```python
|
||||
result = job_manager.create_job(
|
||||
config_path="config.json",
|
||||
date_range=["2025-10-15", "2025-10-16"],
|
||||
models=["model-a"],
|
||||
model_day_filter=[("model-a", "2025-10-15")] # Already completed
|
||||
)
|
||||
|
||||
# result = {
|
||||
# "job_id": "new-job-uuid",
|
||||
# "warnings": ["Skipped model-a/2025-10-15 - already completed"]
|
||||
# }
|
||||
```
|
||||
|
||||
**Cross-Job Portfolio Continuity:**
|
||||
- `get_current_position_from_db()` queries across ALL jobs for a given model
|
||||
- Enables portfolio continuity even when new jobs are created with overlapping dates
|
||||
- Starting position = most recent trading_day.ending_cash + holdings where date < current_date
|
||||
|
||||
## Configuration File Format
|
||||
|
||||
```json
|
||||
|
||||
80
ROADMAP.md
80
ROADMAP.md
@@ -4,6 +4,78 @@ This document outlines planned features and improvements for the AI-Trader proje
|
||||
|
||||
## Release Planning
|
||||
|
||||
### v0.5.0 - Performance Metrics & Status APIs (Planned)
|
||||
|
||||
**Focus:** Enhanced observability and performance tracking
|
||||
|
||||
#### Performance Metrics API
|
||||
- **Performance Summary Endpoint** - Query model performance over date ranges
|
||||
- `GET /metrics/performance` - Aggregated performance metrics
|
||||
- Query parameters: `model`, `start_date`, `end_date`
|
||||
- Returns comprehensive performance summary:
|
||||
- Total return (dollar amount and percentage)
|
||||
- Number of trades executed (buy + sell)
|
||||
- Win rate (profitable trading days / total trading days)
|
||||
- Average daily P&L (profit and loss)
|
||||
- Best/worst trading day (highest/lowest daily P&L)
|
||||
- Final portfolio value (cash + holdings at market value)
|
||||
- Number of trading days in queried range
|
||||
- Starting vs. ending portfolio comparison
|
||||
- Use cases:
|
||||
- Compare model performance across different time periods
|
||||
- Evaluate strategy effectiveness
|
||||
- Identify top-performing models
|
||||
- Example: `GET /metrics/performance?model=gpt-4&start_date=2025-01-01&end_date=2025-01-31`
|
||||
- Filtering options:
|
||||
- Single model or all models
|
||||
- Custom date ranges
|
||||
- Exclude incomplete trading days
|
||||
- Response format: JSON with clear metric definitions
|
||||
|
||||
#### Status & Coverage Endpoint
|
||||
- **System Status Summary** - Data availability and simulation progress
|
||||
- `GET /status` - Comprehensive system status
|
||||
- Price data coverage section:
|
||||
- Available symbols (NASDAQ 100 constituents)
|
||||
- Date range of downloaded price data per symbol
|
||||
- Total trading days with complete data
|
||||
- Missing data gaps (symbols without data, date gaps)
|
||||
- Last data refresh timestamp
|
||||
- Model simulation status section:
|
||||
- List of all configured models (enabled/disabled)
|
||||
- Date ranges simulated per model (first and last trading day)
|
||||
- Total trading days completed per model
|
||||
- Most recent simulation date per model
|
||||
- Completion percentage (simulated days / available data days)
|
||||
- System health section:
|
||||
- Database connectivity status
|
||||
- MCP services status (Math, Search, Trade, LocalPrices)
|
||||
- API version and deployment mode
|
||||
- Disk space usage (database size, log size)
|
||||
- Use cases:
|
||||
- Verify data availability before triggering simulations
|
||||
- Identify which models need updates to latest data
|
||||
- Monitor system health and readiness
|
||||
- Plan data downloads for missing date ranges
|
||||
- Example: `GET /status` (no parameters required)
|
||||
- Benefits:
|
||||
- Single endpoint for complete system overview
|
||||
- No need to query multiple endpoints for status
|
||||
- Clear visibility into data gaps
|
||||
- Track simulation progress across models
|
||||
|
||||
#### Implementation Details
|
||||
- Database queries for efficient metric calculation
|
||||
- Caching for frequently accessed metrics (optional)
|
||||
- Response time target: <500ms for typical queries
|
||||
- Comprehensive error handling for missing data
|
||||
|
||||
#### Benefits
|
||||
- **Better Observability** - Clear view of system state and model performance
|
||||
- **Data-Driven Decisions** - Quantitative metrics for model comparison
|
||||
- **Proactive Monitoring** - Identify data gaps before simulations fail
|
||||
- **User Experience** - Single endpoint to check "what's available and what's been done"
|
||||
|
||||
### v1.0.0 - Production Stability & Validation (Planned)
|
||||
|
||||
**Focus:** Comprehensive testing, documentation, and production readiness
|
||||
@@ -607,11 +679,13 @@ To propose a new feature:
|
||||
|
||||
- **v0.1.0** - Initial release with batch execution
|
||||
- **v0.2.0** - Docker deployment support
|
||||
- **v0.3.0** - REST API, on-demand downloads, database storage (current)
|
||||
- **v0.3.0** - REST API, on-demand downloads, database storage
|
||||
- **v0.4.0** - Daily P&L calculation, day-centric results API, reasoning summaries (current)
|
||||
- **v0.5.0** - Performance metrics & status APIs (planned)
|
||||
- **v1.0.0** - Production stability & validation (planned)
|
||||
- **v1.1.0** - API authentication & security (planned)
|
||||
- **v1.2.0** - Position history & analytics (planned)
|
||||
- **v1.3.0** - Performance metrics & analytics (planned)
|
||||
- **v1.3.0** - Advanced performance metrics & analytics (planned)
|
||||
- **v1.4.0** - Data management API (planned)
|
||||
- **v1.5.0** - Web dashboard UI (planned)
|
||||
- **v1.6.0** - Advanced configuration & customization (planned)
|
||||
@@ -619,4 +693,4 @@ To propose a new feature:
|
||||
|
||||
---
|
||||
|
||||
Last updated: 2025-11-01
|
||||
Last updated: 2025-11-06
|
||||
|
||||
@@ -33,6 +33,7 @@ from tools.deployment_config import (
|
||||
from agent.context_injector import ContextInjector
|
||||
from agent.pnl_calculator import DailyPnLCalculator
|
||||
from agent.reasoning_summarizer import ReasoningSummarizer
|
||||
from agent.chat_model_wrapper import ToolCallArgsParsingWrapper
|
||||
|
||||
# Load environment variables
|
||||
load_dotenv()
|
||||
@@ -211,14 +212,16 @@ class BaseAgent:
|
||||
self.model = MockChatModel(date="2025-01-01") # Date will be updated per session
|
||||
print(f"🤖 Using MockChatModel (DEV mode)")
|
||||
else:
|
||||
self.model = ChatOpenAI(
|
||||
base_model = ChatOpenAI(
|
||||
model=self.basemodel,
|
||||
base_url=self.openai_base_url,
|
||||
api_key=self.openai_api_key,
|
||||
max_retries=3,
|
||||
timeout=30
|
||||
)
|
||||
print(f"🤖 Using {self.basemodel} (PROD mode)")
|
||||
# Wrap model with diagnostic wrapper
|
||||
self.model = ToolCallArgsParsingWrapper(model=base_model)
|
||||
print(f"🤖 Using {self.basemodel} (PROD mode) with diagnostic wrapper")
|
||||
except Exception as e:
|
||||
raise RuntimeError(f"❌ Failed to initialize AI model: {e}")
|
||||
|
||||
@@ -319,6 +322,60 @@ class BaseAgent:
|
||||
print(f"⚠️ Could not get position from database: {e}")
|
||||
return {}, self.initial_cash
|
||||
|
||||
def _calculate_final_position_from_actions(
|
||||
self,
|
||||
trading_day_id: int,
|
||||
starting_cash: float
|
||||
) -> tuple[Dict[str, int], float]:
|
||||
"""
|
||||
Calculate final holdings and cash from starting position + actions.
|
||||
|
||||
This is the correct way to get end-of-day position: start with the
|
||||
starting position and apply all trades from the actions table.
|
||||
|
||||
Args:
|
||||
trading_day_id: The trading day ID
|
||||
starting_cash: Cash at start of day
|
||||
|
||||
Returns:
|
||||
(holdings_dict, final_cash) where holdings_dict maps symbol -> quantity
|
||||
"""
|
||||
from api.database import Database
|
||||
|
||||
db = Database()
|
||||
|
||||
# 1. Get starting holdings (from previous day's ending)
|
||||
starting_holdings_list = db.get_starting_holdings(trading_day_id)
|
||||
holdings = {h["symbol"]: h["quantity"] for h in starting_holdings_list}
|
||||
|
||||
# 2. Initialize cash
|
||||
cash = starting_cash
|
||||
|
||||
# 3. Get all actions for this trading day
|
||||
actions = db.get_actions(trading_day_id)
|
||||
|
||||
# 4. Apply each action to calculate final state
|
||||
for action in actions:
|
||||
symbol = action["symbol"]
|
||||
quantity = action["quantity"]
|
||||
price = action["price"]
|
||||
action_type = action["action_type"]
|
||||
|
||||
if action_type == "buy":
|
||||
# Add to holdings
|
||||
holdings[symbol] = holdings.get(symbol, 0) + quantity
|
||||
# Deduct from cash
|
||||
cash -= quantity * price
|
||||
|
||||
elif action_type == "sell":
|
||||
# Remove from holdings
|
||||
holdings[symbol] = holdings.get(symbol, 0) - quantity
|
||||
# Add to cash
|
||||
cash += quantity * price
|
||||
|
||||
# 5. Return final state
|
||||
return holdings, cash
|
||||
|
||||
def _calculate_portfolio_value(
|
||||
self,
|
||||
holdings: Dict[str, int],
|
||||
@@ -365,7 +422,7 @@ class BaseAgent:
|
||||
}
|
||||
|
||||
if tool_name:
|
||||
message["tool_name"] = tool_name
|
||||
message["name"] = tool_name # Use "name" not "tool_name" for consistency with summarizer
|
||||
if tool_input:
|
||||
message["tool_input"] = tool_input
|
||||
|
||||
@@ -479,6 +536,8 @@ Summary:"""
|
||||
# Update context injector with current trading date
|
||||
if self.context_injector:
|
||||
self.context_injector.today_date = today_date
|
||||
# Reset position state for new trading day (enables intra-day tracking)
|
||||
self.context_injector.reset_position()
|
||||
|
||||
# Clear conversation history for new trading day
|
||||
self.clear_conversation_history()
|
||||
@@ -579,21 +638,28 @@ Summary:"""
|
||||
# Capture assistant response
|
||||
self._capture_message("assistant", agent_response)
|
||||
|
||||
# Check stop signal
|
||||
if STOP_SIGNAL in agent_response:
|
||||
print("✅ Received stop signal, trading session ended")
|
||||
print(agent_response)
|
||||
break
|
||||
|
||||
# Extract tool messages and count trade actions
|
||||
# Extract tool messages BEFORE checking stop signal
|
||||
# (agent may call tools AND return FINISH_SIGNAL in same response)
|
||||
tool_msgs = extract_tool_messages(response)
|
||||
print(f"[DEBUG] Extracted {len(tool_msgs)} tool messages from response")
|
||||
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_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']:
|
||||
action_count += 1
|
||||
|
||||
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
|
||||
new_messages = [
|
||||
{"role": "assistant", "content": agent_response},
|
||||
@@ -611,11 +677,26 @@ Summary:"""
|
||||
session_duration = time.time() - session_start
|
||||
|
||||
# 7. Generate reasoning summary
|
||||
# Debug: Log conversation history size
|
||||
print(f"\n[DEBUG] Generating summary from {len(self.conversation_history)} messages")
|
||||
assistant_msgs = [m for m in self.conversation_history if m.get('role') == 'assistant']
|
||||
tool_msgs = [m for m in self.conversation_history if m.get('role') == 'tool']
|
||||
print(f"[DEBUG] Assistant messages: {len(assistant_msgs)}, Tool messages: {len(tool_msgs)}")
|
||||
if assistant_msgs:
|
||||
first_assistant = assistant_msgs[0]
|
||||
print(f"[DEBUG] First assistant message preview: {first_assistant.get('content', '')[:200]}...")
|
||||
|
||||
summarizer = ReasoningSummarizer(model=self.model)
|
||||
summary = await summarizer.generate_summary(self.conversation_history)
|
||||
|
||||
# 8. Get current portfolio state from database
|
||||
current_holdings, current_cash = self._get_current_portfolio_state(today_date, job_id)
|
||||
# 8. Calculate final portfolio state from starting position + actions
|
||||
# NOTE: We must calculate from actions, not query database, because:
|
||||
# - On first day, database query returns empty (no previous day)
|
||||
# - This method applies all trades to get accurate final state
|
||||
current_holdings, current_cash = self._calculate_final_position_from_actions(
|
||||
trading_day_id=trading_day_id,
|
||||
starting_cash=starting_cash
|
||||
)
|
||||
|
||||
# 9. Save final holdings to database
|
||||
for symbol, quantity in current_holdings.items():
|
||||
|
||||
121
agent/chat_model_wrapper.py
Normal file
121
agent/chat_model_wrapper.py
Normal file
@@ -0,0 +1,121 @@
|
||||
"""
|
||||
Chat model wrapper to fix tool_calls args parsing issues.
|
||||
|
||||
DeepSeek and other providers return tool_calls.args as JSON strings, which need
|
||||
to be parsed to dicts before AIMessage construction.
|
||||
"""
|
||||
|
||||
import json
|
||||
from typing import Any, Optional, Dict
|
||||
from functools import wraps
|
||||
|
||||
|
||||
class ToolCallArgsParsingWrapper:
|
||||
"""
|
||||
Wrapper that adds diagnostic logging and fixes tool_calls args if needed.
|
||||
"""
|
||||
|
||||
def __init__(self, model: Any, **kwargs):
|
||||
"""
|
||||
Initialize wrapper around a chat model.
|
||||
|
||||
Args:
|
||||
model: The chat model to wrap
|
||||
**kwargs: Additional parameters (ignored, for compatibility)
|
||||
"""
|
||||
self.wrapped_model = model
|
||||
self._patch_model()
|
||||
|
||||
def _patch_model(self):
|
||||
"""Monkey-patch the model's _create_chat_result to add diagnostics"""
|
||||
if not hasattr(self.wrapped_model, '_create_chat_result'):
|
||||
# Model doesn't have this method (e.g., MockChatModel), skip patching
|
||||
return
|
||||
|
||||
# CRITICAL: Patch parse_tool_call in base.py's namespace (not in openai_tools module!)
|
||||
from langchain_openai.chat_models import base as langchain_base
|
||||
original_parse_tool_call = langchain_base.parse_tool_call
|
||||
|
||||
def patched_parse_tool_call(raw_tool_call, *, partial=False, strict=False, return_id=True):
|
||||
"""Patched parse_tool_call to fix string args bug"""
|
||||
result = original_parse_tool_call(raw_tool_call, partial=partial, strict=strict, return_id=return_id)
|
||||
if result and isinstance(result.get('args'), str):
|
||||
# FIX: parse_tool_call sometimes returns string args instead of dict
|
||||
# This is a known LangChain bug - parse the string to dict
|
||||
try:
|
||||
result['args'] = json.loads(result['args'])
|
||||
except (json.JSONDecodeError, TypeError):
|
||||
# Leave as string if we can't parse it - will fail validation
|
||||
# but at least we tried
|
||||
pass
|
||||
return result
|
||||
|
||||
# Replace in base.py's namespace (where _convert_dict_to_message uses it)
|
||||
langchain_base.parse_tool_call = patched_parse_tool_call
|
||||
|
||||
original_create_chat_result = self.wrapped_model._create_chat_result
|
||||
|
||||
@wraps(original_create_chat_result)
|
||||
def patched_create_chat_result(response: Any, generation_info: Optional[Dict] = None):
|
||||
"""Patched version that normalizes non-standard tool_call formats"""
|
||||
response_dict = response if isinstance(response, dict) else response.model_dump()
|
||||
|
||||
# Normalize tool_calls to OpenAI standard format if needed
|
||||
if 'choices' in response_dict:
|
||||
for choice in response_dict['choices']:
|
||||
if 'message' not in choice:
|
||||
continue
|
||||
|
||||
message = choice['message']
|
||||
|
||||
# Fix tool_calls: Convert non-standard {name, args, id} to {function: {name, arguments}, id}
|
||||
if 'tool_calls' in message and message['tool_calls']:
|
||||
for tool_call in message['tool_calls']:
|
||||
# Check if this is non-standard format (has 'args' directly)
|
||||
if 'args' in tool_call and 'function' not in tool_call:
|
||||
# Convert to standard OpenAI format
|
||||
args = tool_call['args']
|
||||
tool_call['function'] = {
|
||||
'name': tool_call.get('name', ''),
|
||||
'arguments': args if isinstance(args, str) else json.dumps(args)
|
||||
}
|
||||
# Remove non-standard fields
|
||||
if 'name' in tool_call:
|
||||
del tool_call['name']
|
||||
if 'args' in tool_call:
|
||||
del tool_call['args']
|
||||
|
||||
# Fix invalid_tool_calls: Ensure args is JSON string (not dict)
|
||||
if 'invalid_tool_calls' in message and message['invalid_tool_calls']:
|
||||
for invalid_call in message['invalid_tool_calls']:
|
||||
if 'args' in invalid_call and isinstance(invalid_call['args'], dict):
|
||||
try:
|
||||
invalid_call['args'] = json.dumps(invalid_call['args'])
|
||||
except (TypeError, ValueError):
|
||||
# Keep as-is if serialization fails
|
||||
pass
|
||||
|
||||
# Call original method with normalized response
|
||||
return original_create_chat_result(response_dict, generation_info)
|
||||
|
||||
# Replace the method
|
||||
self.wrapped_model._create_chat_result = patched_create_chat_result
|
||||
|
||||
@property
|
||||
def _llm_type(self) -> str:
|
||||
"""Return identifier for this LLM type"""
|
||||
if hasattr(self.wrapped_model, '_llm_type'):
|
||||
return f"wrapped-{self.wrapped_model._llm_type}"
|
||||
return "wrapped-chat-model"
|
||||
|
||||
def __getattr__(self, name: str):
|
||||
"""Proxy all attributes/methods to the wrapped model"""
|
||||
return getattr(self.wrapped_model, name)
|
||||
|
||||
def bind_tools(self, tools: Any, **kwargs):
|
||||
"""Bind tools to the wrapped model"""
|
||||
return self.wrapped_model.bind_tools(tools, **kwargs)
|
||||
|
||||
def bind(self, **kwargs):
|
||||
"""Bind settings to the wrapped model"""
|
||||
return self.wrapped_model.bind(**kwargs)
|
||||
@@ -3,15 +3,22 @@ Tool interceptor for injecting runtime context into MCP tool calls.
|
||||
|
||||
This interceptor automatically injects `signature` and `today_date` parameters
|
||||
into buy/sell tool calls to support concurrent multi-model simulations.
|
||||
|
||||
It also maintains in-memory position state to track cumulative changes within
|
||||
a single trading session, ensuring sell proceeds are immediately available for
|
||||
subsequent buy orders.
|
||||
"""
|
||||
|
||||
from typing import Any, Callable, Awaitable
|
||||
from typing import Any, Callable, Awaitable, Dict, Optional
|
||||
|
||||
|
||||
class ContextInjector:
|
||||
"""
|
||||
Intercepts tool calls to inject runtime context (signature, today_date).
|
||||
|
||||
Also maintains cumulative position state during trading session to ensure
|
||||
sell proceeds are immediately available for subsequent buys.
|
||||
|
||||
Usage:
|
||||
interceptor = ContextInjector(signature="gpt-5", today_date="2025-10-01")
|
||||
client = MultiServerMCPClient(config, tool_interceptors=[interceptor])
|
||||
@@ -34,6 +41,13 @@ class ContextInjector:
|
||||
self.job_id = job_id
|
||||
self.session_id = session_id # Deprecated but kept for compatibility
|
||||
self.trading_day_id = trading_day_id
|
||||
self._current_position: Optional[Dict[str, float]] = None
|
||||
|
||||
def reset_position(self) -> None:
|
||||
"""
|
||||
Reset position state (call at start of each trading day).
|
||||
"""
|
||||
self._current_position = None
|
||||
|
||||
async def __call__(
|
||||
self,
|
||||
@@ -43,6 +57,9 @@ class ContextInjector:
|
||||
"""
|
||||
Intercept tool call and inject context parameters.
|
||||
|
||||
For buy/sell operations, maintains cumulative position state to ensure
|
||||
sell proceeds are immediately available for subsequent buys.
|
||||
|
||||
Args:
|
||||
request: Tool call request containing name and arguments
|
||||
handler: Async callable to execute the actual tool
|
||||
@@ -52,10 +69,6 @@ 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}, 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)
|
||||
request.args["signature"] = self.signature
|
||||
request.args["today_date"] = self.today_date
|
||||
@@ -66,8 +79,26 @@ class ContextInjector:
|
||||
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}")
|
||||
# Inject current position if we're tracking it
|
||||
if self._current_position is not None:
|
||||
request.args["_current_position"] = self._current_position
|
||||
|
||||
# Call the actual tool handler
|
||||
return await handler(request)
|
||||
result = await handler(request)
|
||||
|
||||
# Update position state after successful trade
|
||||
if request.name in ["buy", "sell"]:
|
||||
# Extract position dict from MCP result
|
||||
# MCP tools return CallToolResult objects with structuredContent field
|
||||
position_dict = None
|
||||
if hasattr(result, 'structuredContent') and result.structuredContent:
|
||||
position_dict = result.structuredContent
|
||||
elif isinstance(result, dict):
|
||||
position_dict = result
|
||||
|
||||
# Check if position dict is valid (not an error) and update state
|
||||
if position_dict and "error" not in position_dict and "CASH" in position_dict:
|
||||
# Update our tracked position with the new state
|
||||
self._current_position = position_dict.copy()
|
||||
|
||||
return result
|
||||
|
||||
@@ -36,15 +36,17 @@ class ReasoningSummarizer:
|
||||
summary_prompt = f"""You are reviewing your own trading decisions for the day.
|
||||
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:
|
||||
- What you analyzed
|
||||
- Why you made the trades you did
|
||||
- What specific trades you executed (buy/sell, symbols, quantities)
|
||||
- Why you made those trades
|
||||
- Your overall strategy for the day
|
||||
|
||||
Trading session log:
|
||||
{log_text}
|
||||
|
||||
Provide a concise summary:"""
|
||||
Provide a concise summary that includes the actual trades executed:"""
|
||||
|
||||
response = await self.model.ainvoke([
|
||||
{"role": "user", "content": summary_prompt}
|
||||
@@ -67,21 +69,39 @@ Provide a concise summary:"""
|
||||
reasoning_log: List of message dicts
|
||||
|
||||
Returns:
|
||||
Formatted text representation
|
||||
Formatted text representation with emphasis on trades
|
||||
"""
|
||||
# Debug: Log what we're formatting
|
||||
print(f"[DEBUG ReasoningSummarizer] Formatting {len(reasoning_log)} messages")
|
||||
assistant_count = sum(1 for m in reasoning_log if m.get('role') == 'assistant')
|
||||
tool_count = sum(1 for m in reasoning_log if m.get('role') == 'tool')
|
||||
print(f"[DEBUG ReasoningSummarizer] Breakdown: {assistant_count} assistant, {tool_count} tool")
|
||||
|
||||
formatted_parts = []
|
||||
trades_executed = []
|
||||
|
||||
for msg in reasoning_log:
|
||||
role = msg.get("role", "")
|
||||
content = msg.get("content", "")
|
||||
tool_name = msg.get("name", "")
|
||||
|
||||
if role == "assistant":
|
||||
# AI's thoughts
|
||||
formatted_parts.append(f"AI: {content[:200]}")
|
||||
elif role == "tool":
|
||||
# Tool results
|
||||
tool_name = msg.get("name", "tool")
|
||||
formatted_parts.append(f"{tool_name}: {content[:100]}")
|
||||
# Highlight trade tool calls
|
||||
if tool_name in ["buy", "sell"]:
|
||||
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)
|
||||
|
||||
|
||||
@@ -78,10 +78,11 @@ class MCPServiceManager:
|
||||
env['PYTHONPATH'] = str(Path.cwd())
|
||||
|
||||
# Start service process (output goes to Docker logs)
|
||||
# Enable stdout/stderr for debugging (previously sent to DEVNULL)
|
||||
process = subprocess.Popen(
|
||||
[sys.executable, str(script_path)],
|
||||
stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.DEVNULL,
|
||||
stdout=sys.stdout, # Redirect to main process stdout
|
||||
stderr=sys.stderr, # Redirect to main process stderr
|
||||
cwd=Path.cwd(), # Use current working directory (/app)
|
||||
env=env # Pass environment with PYTHONPATH
|
||||
)
|
||||
|
||||
@@ -34,8 +34,11 @@ def get_current_position_from_db(
|
||||
Returns ending holdings and cash from that previous day, which becomes the
|
||||
starting position for the current day.
|
||||
|
||||
NOTE: Searches across ALL jobs for the given model, enabling portfolio continuity
|
||||
even when new jobs are created with overlapping date ranges.
|
||||
|
||||
Args:
|
||||
job_id: Job UUID
|
||||
job_id: Job UUID (kept for compatibility but not used in query)
|
||||
model: Model signature
|
||||
date: Current trading date (will query for date < this)
|
||||
initial_cash: Initial cash if no prior data (first trading day)
|
||||
@@ -51,13 +54,14 @@ def get_current_position_from_db(
|
||||
|
||||
try:
|
||||
# Query most recent trading_day BEFORE current date (previous day's ending position)
|
||||
# NOTE: Removed job_id filter to enable cross-job continuity
|
||||
cursor.execute("""
|
||||
SELECT id, ending_cash
|
||||
FROM trading_days
|
||||
WHERE job_id = ? AND model = ? AND date < ?
|
||||
WHERE model = ? AND date < ?
|
||||
ORDER BY date DESC
|
||||
LIMIT 1
|
||||
""", (job_id, model, date))
|
||||
""", (model, date))
|
||||
|
||||
row = cursor.fetchone()
|
||||
|
||||
@@ -91,7 +95,8 @@ def get_current_position_from_db(
|
||||
|
||||
|
||||
def _buy_impl(symbol: str, amount: int, signature: str = None, today_date: str = None,
|
||||
job_id: str = None, session_id: int = None, trading_day_id: int = None) -> Dict[str, Any]:
|
||||
job_id: str = None, session_id: int = None, trading_day_id: int = None,
|
||||
_current_position: Dict[str, float] = None) -> Dict[str, Any]:
|
||||
"""
|
||||
Internal buy implementation - accepts injected context parameters.
|
||||
|
||||
@@ -103,9 +108,13 @@ def _buy_impl(symbol: str, amount: int, signature: str = None, today_date: str =
|
||||
job_id: Job ID (injected)
|
||||
session_id: Session ID (injected, DEPRECATED)
|
||||
trading_day_id: Trading day ID (injected)
|
||||
_current_position: Current position state (injected by ContextInjector)
|
||||
|
||||
This function is not exposed to the AI model. It receives runtime context
|
||||
(signature, today_date, job_id, session_id, trading_day_id) from the ContextInjector.
|
||||
|
||||
The _current_position parameter enables intra-day position tracking, ensuring
|
||||
sell proceeds are immediately available for subsequent buys.
|
||||
"""
|
||||
# Validate required parameters
|
||||
if not job_id:
|
||||
@@ -121,7 +130,13 @@ def _buy_impl(symbol: str, amount: int, signature: str = None, today_date: str =
|
||||
|
||||
try:
|
||||
# Step 1: Get current position
|
||||
current_position, next_action_id = get_current_position_from_db(job_id, signature, today_date)
|
||||
# Use injected position if available (for intra-day tracking),
|
||||
# otherwise query database for starting position
|
||||
if _current_position is not None:
|
||||
current_position = _current_position
|
||||
next_action_id = 0 # Not used in new schema
|
||||
else:
|
||||
current_position, next_action_id = get_current_position_from_db(job_id, signature, today_date)
|
||||
|
||||
# Step 2: Get stock price
|
||||
try:
|
||||
@@ -186,7 +201,8 @@ def _buy_impl(symbol: str, amount: int, signature: str = None, today_date: str =
|
||||
|
||||
@mcp.tool()
|
||||
def buy(symbol: str, amount: int, signature: str = None, today_date: str = None,
|
||||
job_id: str = None, session_id: int = None, trading_day_id: int = None) -> Dict[str, Any]:
|
||||
job_id: str = None, session_id: int = None, trading_day_id: int = None,
|
||||
_current_position: Dict[str, float] = None) -> Dict[str, Any]:
|
||||
"""
|
||||
Buy stock shares.
|
||||
|
||||
@@ -199,14 +215,15 @@ def buy(symbol: str, amount: int, signature: str = None, today_date: str = None,
|
||||
- Success: {"CASH": remaining_cash, "SYMBOL": shares, ...}
|
||||
- Failure: {"error": error_message, ...}
|
||||
|
||||
Note: signature, today_date, job_id, session_id, trading_day_id are
|
||||
automatically injected by the system. Do not provide these parameters.
|
||||
Note: signature, today_date, job_id, session_id, trading_day_id, _current_position
|
||||
are automatically injected by the system. Do not provide these parameters.
|
||||
"""
|
||||
return _buy_impl(symbol, amount, signature, today_date, job_id, session_id, trading_day_id)
|
||||
return _buy_impl(symbol, amount, signature, today_date, job_id, session_id, trading_day_id, _current_position)
|
||||
|
||||
|
||||
def _sell_impl(symbol: str, amount: int, signature: str = None, today_date: str = None,
|
||||
job_id: str = None, session_id: int = None, trading_day_id: int = None) -> Dict[str, Any]:
|
||||
job_id: str = None, session_id: int = None, trading_day_id: int = None,
|
||||
_current_position: Dict[str, float] = None) -> Dict[str, Any]:
|
||||
"""
|
||||
Sell stock function - writes to SQLite database.
|
||||
|
||||
@@ -218,11 +235,15 @@ def _sell_impl(symbol: str, amount: int, signature: str = None, today_date: str
|
||||
job_id: Job UUID (injected by ContextInjector)
|
||||
session_id: Trading session ID (injected by ContextInjector, DEPRECATED)
|
||||
trading_day_id: Trading day ID (injected by ContextInjector)
|
||||
_current_position: Current position state (injected by ContextInjector)
|
||||
|
||||
Returns:
|
||||
Dict[str, Any]:
|
||||
- Success: {"CASH": amount, symbol: quantity, ...}
|
||||
- Failure: {"error": message, ...}
|
||||
|
||||
The _current_position parameter enables intra-day position tracking, ensuring
|
||||
sell proceeds are immediately available for subsequent buys.
|
||||
"""
|
||||
# Validate required parameters
|
||||
if not job_id:
|
||||
@@ -238,7 +259,13 @@ def _sell_impl(symbol: str, amount: int, signature: str = None, today_date: str
|
||||
|
||||
try:
|
||||
# Step 1: Get current position
|
||||
current_position, next_action_id = get_current_position_from_db(job_id, signature, today_date)
|
||||
# Use injected position if available (for intra-day tracking),
|
||||
# otherwise query database for starting position
|
||||
if _current_position is not None:
|
||||
current_position = _current_position
|
||||
next_action_id = 0 # Not used in new schema
|
||||
else:
|
||||
current_position, next_action_id = get_current_position_from_db(job_id, signature, today_date)
|
||||
|
||||
# Step 2: Validate position exists
|
||||
if symbol not in current_position:
|
||||
@@ -298,7 +325,8 @@ def _sell_impl(symbol: str, amount: int, signature: str = None, today_date: str
|
||||
|
||||
@mcp.tool()
|
||||
def sell(symbol: str, amount: int, signature: str = None, today_date: str = None,
|
||||
job_id: str = None, session_id: int = None, trading_day_id: int = None) -> Dict[str, Any]:
|
||||
job_id: str = None, session_id: int = None, trading_day_id: int = None,
|
||||
_current_position: Dict[str, float] = None) -> Dict[str, Any]:
|
||||
"""
|
||||
Sell stock shares.
|
||||
|
||||
@@ -311,10 +339,10 @@ def sell(symbol: str, amount: int, signature: str = None, today_date: str = None
|
||||
- Success: {"CASH": remaining_cash, "SYMBOL": shares, ...}
|
||||
- Failure: {"error": error_message, ...}
|
||||
|
||||
Note: signature, today_date, job_id, session_id, trading_day_id are
|
||||
automatically injected by the system. Do not provide these parameters.
|
||||
Note: signature, today_date, job_id, session_id, trading_day_id, _current_position
|
||||
are automatically injected by the system. Do not provide these parameters.
|
||||
"""
|
||||
return _sell_impl(symbol, amount, signature, today_date, job_id, session_id, trading_day_id)
|
||||
return _sell_impl(symbol, amount, signature, today_date, job_id, session_id, trading_day_id, _current_position)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -55,8 +55,9 @@ class JobManager:
|
||||
config_path: str,
|
||||
date_range: List[str],
|
||||
models: List[str],
|
||||
model_day_filter: Optional[List[tuple]] = None
|
||||
) -> str:
|
||||
model_day_filter: Optional[List[tuple]] = None,
|
||||
skip_completed: bool = True
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Create new simulation job.
|
||||
|
||||
@@ -66,12 +67,16 @@ class JobManager:
|
||||
models: List of model signatures to execute
|
||||
model_day_filter: Optional list of (model, date) tuples to limit job_details.
|
||||
If None, creates job_details for all model-date combinations.
|
||||
skip_completed: If True (default), skips already-completed simulations.
|
||||
If False, includes all requested simulations regardless of completion status.
|
||||
|
||||
Returns:
|
||||
job_id: UUID of created job
|
||||
Dict with:
|
||||
- job_id: UUID of created job
|
||||
- warnings: List of warning messages for skipped simulations
|
||||
|
||||
Raises:
|
||||
ValueError: If another job is already running/pending
|
||||
ValueError: If another job is already running/pending or if all simulations are already completed (when skip_completed=True)
|
||||
"""
|
||||
if not self.can_start_new_job():
|
||||
raise ValueError("Another simulation job is already running or pending")
|
||||
@@ -83,6 +88,49 @@ class JobManager:
|
||||
cursor = conn.cursor()
|
||||
|
||||
try:
|
||||
# Determine which model-day pairs to check
|
||||
if model_day_filter is not None:
|
||||
pairs_to_check = model_day_filter
|
||||
else:
|
||||
pairs_to_check = [(model, date) for date in date_range for model in models]
|
||||
|
||||
# Check for already-completed simulations (only if skip_completed=True)
|
||||
skipped_pairs = []
|
||||
pending_pairs = []
|
||||
|
||||
if skip_completed:
|
||||
# Perform duplicate checking
|
||||
for model, date in pairs_to_check:
|
||||
cursor.execute("""
|
||||
SELECT COUNT(*)
|
||||
FROM job_details
|
||||
WHERE model = ? AND date = ? AND status = 'completed'
|
||||
""", (model, date))
|
||||
|
||||
count = cursor.fetchone()[0]
|
||||
|
||||
if count > 0:
|
||||
skipped_pairs.append((model, date))
|
||||
logger.info(f"Skipping {model}/{date} - already completed in previous job")
|
||||
else:
|
||||
pending_pairs.append((model, date))
|
||||
|
||||
# If all simulations are already completed, raise error
|
||||
if len(pending_pairs) == 0:
|
||||
warnings = [
|
||||
f"Skipped {model}/{date} - already completed"
|
||||
for model, date in skipped_pairs
|
||||
]
|
||||
raise ValueError(
|
||||
f"All requested simulations are already completed. "
|
||||
f"Skipped {len(skipped_pairs)} model-day pair(s). "
|
||||
f"Details: {warnings}"
|
||||
)
|
||||
else:
|
||||
# skip_completed=False: include ALL pairs (no duplicate checking)
|
||||
pending_pairs = pairs_to_check
|
||||
logger.info(f"Including all {len(pending_pairs)} model-day pairs (skip_completed=False)")
|
||||
|
||||
# Insert job
|
||||
cursor.execute("""
|
||||
INSERT INTO jobs (
|
||||
@@ -98,34 +146,32 @@ class JobManager:
|
||||
created_at
|
||||
))
|
||||
|
||||
# Create job_details based on filter
|
||||
if model_day_filter is not None:
|
||||
# Only create job_details for specified model-day pairs
|
||||
for model, date in model_day_filter:
|
||||
cursor.execute("""
|
||||
INSERT INTO job_details (
|
||||
job_id, date, model, status
|
||||
)
|
||||
VALUES (?, ?, ?, ?)
|
||||
""", (job_id, date, model, "pending"))
|
||||
# Create job_details only for pending pairs
|
||||
for model, date in pending_pairs:
|
||||
cursor.execute("""
|
||||
INSERT INTO job_details (
|
||||
job_id, date, model, status
|
||||
)
|
||||
VALUES (?, ?, ?, ?)
|
||||
""", (job_id, date, model, "pending"))
|
||||
|
||||
logger.info(f"Created job {job_id} with {len(model_day_filter)} model-day tasks (filtered)")
|
||||
else:
|
||||
# Create job_details for all model-day combinations
|
||||
for date in date_range:
|
||||
for model in models:
|
||||
cursor.execute("""
|
||||
INSERT INTO job_details (
|
||||
job_id, date, model, status
|
||||
)
|
||||
VALUES (?, ?, ?, ?)
|
||||
""", (job_id, date, model, "pending"))
|
||||
logger.info(f"Created job {job_id} with {len(pending_pairs)} model-day tasks")
|
||||
|
||||
logger.info(f"Created job {job_id} with {len(date_range)} dates and {len(models)} models")
|
||||
if skipped_pairs:
|
||||
logger.info(f"Skipped {len(skipped_pairs)} already-completed simulations")
|
||||
|
||||
conn.commit()
|
||||
|
||||
return job_id
|
||||
# Prepare warnings
|
||||
warnings = [
|
||||
f"Skipped {model}/{date} - already completed"
|
||||
for model, date in skipped_pairs
|
||||
]
|
||||
|
||||
return {
|
||||
"job_id": job_id,
|
||||
"warnings": warnings
|
||||
}
|
||||
|
||||
finally:
|
||||
conn.close()
|
||||
@@ -699,6 +745,85 @@ class JobManager:
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def cleanup_stale_jobs(self) -> Dict[str, int]:
|
||||
"""
|
||||
Clean up stale jobs from container restarts.
|
||||
|
||||
Marks jobs with status 'pending', 'downloading_data', or 'running' as
|
||||
'failed' or 'partial' based on completion percentage.
|
||||
|
||||
Called on application startup to reset interrupted jobs.
|
||||
|
||||
Returns:
|
||||
Dict with jobs_cleaned count and details
|
||||
"""
|
||||
conn = get_db_connection(self.db_path)
|
||||
cursor = conn.cursor()
|
||||
|
||||
try:
|
||||
# Find all stale jobs
|
||||
cursor.execute("""
|
||||
SELECT job_id, status
|
||||
FROM jobs
|
||||
WHERE status IN ('pending', 'downloading_data', 'running')
|
||||
""")
|
||||
|
||||
stale_jobs = cursor.fetchall()
|
||||
cleaned_count = 0
|
||||
|
||||
for job_id, original_status in stale_jobs:
|
||||
# Get progress to determine if partially completed
|
||||
cursor.execute("""
|
||||
SELECT
|
||||
COUNT(*) as total,
|
||||
SUM(CASE WHEN status = 'completed' THEN 1 ELSE 0 END) as completed,
|
||||
SUM(CASE WHEN status = 'failed' THEN 1 ELSE 0 END) as failed
|
||||
FROM job_details
|
||||
WHERE job_id = ?
|
||||
""", (job_id,))
|
||||
|
||||
total, completed, failed = cursor.fetchone()
|
||||
completed = completed or 0
|
||||
failed = failed or 0
|
||||
|
||||
# Determine final status based on completion
|
||||
if completed > 0:
|
||||
new_status = "partial"
|
||||
error_msg = f"Job interrupted by container restart (was {original_status}, {completed}/{total} model-days completed)"
|
||||
else:
|
||||
new_status = "failed"
|
||||
error_msg = f"Job interrupted by container restart (was {original_status}, no progress made)"
|
||||
|
||||
# Mark incomplete job_details as failed
|
||||
cursor.execute("""
|
||||
UPDATE job_details
|
||||
SET status = 'failed', error = 'Container restarted before completion'
|
||||
WHERE job_id = ? AND status IN ('pending', 'running')
|
||||
""", (job_id,))
|
||||
|
||||
# Update job status
|
||||
updated_at = datetime.utcnow().isoformat() + "Z"
|
||||
cursor.execute("""
|
||||
UPDATE jobs
|
||||
SET status = ?, error = ?, completed_at = ?, updated_at = ?
|
||||
WHERE job_id = ?
|
||||
""", (new_status, error_msg, updated_at, updated_at, job_id))
|
||||
|
||||
logger.warning(f"Cleaned up stale job {job_id}: {original_status} → {new_status} ({completed}/{total} completed)")
|
||||
cleaned_count += 1
|
||||
|
||||
conn.commit()
|
||||
|
||||
if cleaned_count > 0:
|
||||
logger.warning(f"⚠️ Cleaned up {cleaned_count} stale job(s) from previous container session")
|
||||
else:
|
||||
logger.info("✅ No stale jobs found")
|
||||
|
||||
return {"jobs_cleaned": cleaned_count}
|
||||
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def cleanup_old_jobs(self, days: int = 30) -> Dict[str, int]:
|
||||
"""
|
||||
Delete jobs older than threshold.
|
||||
|
||||
28
api/main.py
28
api/main.py
@@ -134,25 +134,39 @@ def create_app(
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
"""Initialize database on startup, cleanup on shutdown if needed"""
|
||||
from tools.deployment_config import is_dev_mode, get_db_path
|
||||
from tools.deployment_config import is_dev_mode, get_db_path, should_preserve_dev_data
|
||||
from api.database import initialize_dev_database, initialize_database
|
||||
|
||||
# Startup - use closure to access db_path from create_app scope
|
||||
logger.info("🚀 FastAPI application starting...")
|
||||
logger.info("📊 Initializing database...")
|
||||
|
||||
should_cleanup_stale_jobs = False
|
||||
|
||||
if is_dev_mode():
|
||||
# Initialize dev database (reset unless PRESERVE_DEV_DATA=true)
|
||||
logger.info(" 🔧 DEV mode detected - initializing dev database")
|
||||
dev_db_path = get_db_path(db_path)
|
||||
initialize_dev_database(dev_db_path)
|
||||
log_dev_mode_startup_warning()
|
||||
|
||||
# Only cleanup stale jobs if preserving dev data (otherwise DB is fresh)
|
||||
if should_preserve_dev_data():
|
||||
should_cleanup_stale_jobs = True
|
||||
else:
|
||||
# Ensure production database schema exists
|
||||
logger.info(" 🏭 PROD mode - ensuring database schema exists")
|
||||
initialize_database(db_path)
|
||||
should_cleanup_stale_jobs = True
|
||||
|
||||
logger.info("✅ Database initialized")
|
||||
|
||||
# Clean up stale jobs from previous container session
|
||||
if should_cleanup_stale_jobs:
|
||||
logger.info("🧹 Checking for stale jobs from previous session...")
|
||||
job_manager = JobManager(get_db_path(db_path) if is_dev_mode() else db_path)
|
||||
job_manager.cleanup_stale_jobs()
|
||||
|
||||
logger.info("🌐 API server ready to accept requests")
|
||||
|
||||
yield
|
||||
@@ -266,12 +280,19 @@ def create_app(
|
||||
|
||||
# Create job immediately with all requested dates
|
||||
# Worker will handle data download and filtering
|
||||
job_id = job_manager.create_job(
|
||||
result = job_manager.create_job(
|
||||
config_path=config_path,
|
||||
date_range=all_dates,
|
||||
models=models_to_run,
|
||||
model_day_filter=None # Worker will filter based on available data
|
||||
model_day_filter=None, # Worker will filter based on available data
|
||||
skip_completed=(not request.replace_existing) # Skip if replace_existing=False
|
||||
)
|
||||
job_id = result["job_id"]
|
||||
warnings = result.get("warnings", [])
|
||||
|
||||
# Log warnings if any simulations were skipped
|
||||
if warnings:
|
||||
logger.warning(f"Job {job_id} created with {len(warnings)} skipped simulations: {warnings}")
|
||||
|
||||
# Start worker in background thread (only if not in test mode)
|
||||
if not getattr(app.state, "test_mode", False):
|
||||
@@ -298,6 +319,7 @@ def create_app(
|
||||
status="pending",
|
||||
total_model_days=len(all_dates) * len(models_to_run),
|
||||
message=message,
|
||||
warnings=warnings if warnings else None,
|
||||
**deployment_info
|
||||
)
|
||||
|
||||
|
||||
@@ -80,7 +80,7 @@ class RuntimeConfigManager:
|
||||
initial_config = {
|
||||
"TODAY_DATE": date,
|
||||
"SIGNATURE": model_sig,
|
||||
"IF_TRADE": False,
|
||||
"IF_TRADE": True, # FIX: Trades are expected by default
|
||||
"JOB_ID": job_id,
|
||||
"TRADING_DAY_ID": trading_day_id
|
||||
}
|
||||
|
||||
@@ -66,3 +66,28 @@ See README.md for architecture diagram.
|
||||
- Search results filtered by publication date
|
||||
|
||||
See [CLAUDE.md](../../CLAUDE.md) for implementation details.
|
||||
|
||||
---
|
||||
|
||||
## Position Tracking Across Jobs
|
||||
|
||||
**Design:** Portfolio state is tracked per-model across all jobs, not per-job.
|
||||
|
||||
**Query Logic:**
|
||||
```python
|
||||
# Get starting position for current trading day
|
||||
SELECT id, ending_cash FROM trading_days
|
||||
WHERE model = ? AND date < ? # No job_id filter
|
||||
ORDER BY date DESC
|
||||
LIMIT 1
|
||||
```
|
||||
|
||||
**Benefits:**
|
||||
- Portfolio continuity when creating new jobs with overlapping dates
|
||||
- Prevents accidental portfolio resets
|
||||
- Enables flexible job scheduling (resume, rerun, backfill)
|
||||
|
||||
**Example:**
|
||||
- Job 1: Runs 2025-10-13 to 2025-10-15 for model-a
|
||||
- Job 2: Runs 2025-10-16 to 2025-10-20 for model-a
|
||||
- Job 2 starts with Job 1's ending position from 2025-10-15
|
||||
|
||||
1172
docs/plans/2025-11-07-fix-duplicate-simulation-bugs.md
Normal file
1172
docs/plans/2025-11-07-fix-duplicate-simulation-bugs.md
Normal file
File diff suppressed because it is too large
Load Diff
@@ -405,11 +405,12 @@ class TestAsyncDownload:
|
||||
db_path = api_client.db_path
|
||||
job_manager = JobManager(db_path=db_path)
|
||||
|
||||
job_id = job_manager.create_job(
|
||||
job_result = job_manager.create_job(
|
||||
config_path="config.json",
|
||||
date_range=["2025-10-01"],
|
||||
models=["gpt-5"]
|
||||
)
|
||||
job_id = job_result["job_id"]
|
||||
|
||||
# Add warnings
|
||||
warnings = ["Rate limited", "Skipped 1 date"]
|
||||
|
||||
@@ -12,11 +12,12 @@ def test_worker_prepares_data_before_execution(tmp_path):
|
||||
job_manager = JobManager(db_path=db_path)
|
||||
|
||||
# Create job
|
||||
job_id = job_manager.create_job(
|
||||
job_result = job_manager.create_job(
|
||||
config_path="configs/default_config.json",
|
||||
date_range=["2025-10-01"],
|
||||
models=["gpt-5"]
|
||||
)
|
||||
job_id = job_result["job_id"]
|
||||
|
||||
worker = SimulationWorker(job_id=job_id, db_path=db_path)
|
||||
|
||||
@@ -46,11 +47,12 @@ def test_worker_handles_no_available_dates(tmp_path):
|
||||
initialize_database(db_path)
|
||||
job_manager = JobManager(db_path=db_path)
|
||||
|
||||
job_id = job_manager.create_job(
|
||||
job_result = job_manager.create_job(
|
||||
config_path="configs/default_config.json",
|
||||
date_range=["2025-10-01"],
|
||||
models=["gpt-5"]
|
||||
)
|
||||
job_id = job_result["job_id"]
|
||||
|
||||
worker = SimulationWorker(job_id=job_id, db_path=db_path)
|
||||
|
||||
@@ -74,11 +76,12 @@ def test_worker_stores_warnings(tmp_path):
|
||||
initialize_database(db_path)
|
||||
job_manager = JobManager(db_path=db_path)
|
||||
|
||||
job_id = job_manager.create_job(
|
||||
job_result = job_manager.create_job(
|
||||
config_path="configs/default_config.json",
|
||||
date_range=["2025-10-01"],
|
||||
models=["gpt-5"]
|
||||
)
|
||||
job_id = job_result["job_id"]
|
||||
|
||||
worker = SimulationWorker(job_id=job_id, db_path=db_path)
|
||||
|
||||
|
||||
278
tests/integration/test_duplicate_simulation_prevention.py
Normal file
278
tests/integration/test_duplicate_simulation_prevention.py
Normal file
@@ -0,0 +1,278 @@
|
||||
"""Integration test for duplicate simulation prevention."""
|
||||
import pytest
|
||||
import tempfile
|
||||
import os
|
||||
import json
|
||||
from pathlib import Path
|
||||
from api.job_manager import JobManager
|
||||
from api.model_day_executor import ModelDayExecutor
|
||||
from api.database import get_db_connection
|
||||
|
||||
|
||||
pytestmark = pytest.mark.integration
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def temp_env(tmp_path):
|
||||
"""Create temporary environment with db and config."""
|
||||
# Create temp database
|
||||
db_path = str(tmp_path / "test_jobs.db")
|
||||
|
||||
# Initialize database
|
||||
conn = get_db_connection(db_path)
|
||||
cursor = conn.cursor()
|
||||
|
||||
# Create schema
|
||||
cursor.execute("""
|
||||
CREATE TABLE IF NOT EXISTS jobs (
|
||||
job_id TEXT PRIMARY KEY,
|
||||
config_path TEXT NOT NULL,
|
||||
status TEXT NOT NULL,
|
||||
date_range TEXT NOT NULL,
|
||||
models TEXT NOT NULL,
|
||||
created_at TEXT NOT NULL,
|
||||
started_at TEXT,
|
||||
updated_at TEXT,
|
||||
completed_at TEXT,
|
||||
total_duration_seconds REAL,
|
||||
error TEXT,
|
||||
warnings TEXT
|
||||
)
|
||||
""")
|
||||
|
||||
cursor.execute("""
|
||||
CREATE TABLE IF NOT EXISTS job_details (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
job_id TEXT NOT NULL,
|
||||
date TEXT NOT NULL,
|
||||
model TEXT NOT NULL,
|
||||
status TEXT NOT NULL,
|
||||
started_at TEXT,
|
||||
completed_at TEXT,
|
||||
duration_seconds REAL,
|
||||
error TEXT,
|
||||
FOREIGN KEY (job_id) REFERENCES jobs(job_id) ON DELETE CASCADE,
|
||||
UNIQUE(job_id, date, model)
|
||||
)
|
||||
""")
|
||||
|
||||
cursor.execute("""
|
||||
CREATE TABLE IF NOT EXISTS trading_days (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
job_id TEXT NOT NULL,
|
||||
model TEXT NOT NULL,
|
||||
date TEXT NOT NULL,
|
||||
starting_cash REAL NOT NULL,
|
||||
ending_cash REAL NOT NULL,
|
||||
profit REAL NOT NULL,
|
||||
return_pct REAL NOT NULL,
|
||||
portfolio_value REAL NOT NULL,
|
||||
reasoning_summary TEXT,
|
||||
reasoning_full TEXT,
|
||||
completed_at TEXT,
|
||||
session_duration_seconds REAL,
|
||||
UNIQUE(job_id, model, date)
|
||||
)
|
||||
""")
|
||||
|
||||
cursor.execute("""
|
||||
CREATE TABLE IF NOT EXISTS holdings (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
trading_day_id INTEGER NOT NULL,
|
||||
symbol TEXT NOT NULL,
|
||||
quantity INTEGER NOT NULL,
|
||||
FOREIGN KEY (trading_day_id) REFERENCES trading_days(id) ON DELETE CASCADE
|
||||
)
|
||||
""")
|
||||
|
||||
cursor.execute("""
|
||||
CREATE TABLE IF NOT EXISTS actions (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
trading_day_id INTEGER NOT NULL,
|
||||
action_type TEXT NOT NULL,
|
||||
symbol TEXT NOT NULL,
|
||||
quantity INTEGER NOT NULL,
|
||||
price REAL NOT NULL,
|
||||
created_at TEXT NOT NULL,
|
||||
FOREIGN KEY (trading_day_id) REFERENCES trading_days(id) ON DELETE CASCADE
|
||||
)
|
||||
""")
|
||||
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
# Create mock config
|
||||
config_path = str(tmp_path / "test_config.json")
|
||||
config = {
|
||||
"models": [
|
||||
{
|
||||
"signature": "test-model",
|
||||
"basemodel": "mock/model",
|
||||
"enabled": True
|
||||
}
|
||||
],
|
||||
"agent_config": {
|
||||
"max_steps": 10,
|
||||
"initial_cash": 10000.0
|
||||
},
|
||||
"log_config": {
|
||||
"log_path": str(tmp_path / "logs")
|
||||
},
|
||||
"date_range": {
|
||||
"init_date": "2025-10-13"
|
||||
}
|
||||
}
|
||||
|
||||
with open(config_path, 'w') as f:
|
||||
json.dump(config, f)
|
||||
|
||||
yield {
|
||||
"db_path": db_path,
|
||||
"config_path": config_path,
|
||||
"data_dir": str(tmp_path)
|
||||
}
|
||||
|
||||
|
||||
def test_duplicate_simulation_is_skipped(temp_env):
|
||||
"""Test that overlapping job skips already-completed simulation."""
|
||||
manager = JobManager(db_path=temp_env["db_path"])
|
||||
|
||||
# Create first job
|
||||
result_1 = manager.create_job(
|
||||
config_path=temp_env["config_path"],
|
||||
date_range=["2025-10-15"],
|
||||
models=["test-model"]
|
||||
)
|
||||
job_id_1 = result_1["job_id"]
|
||||
|
||||
# Simulate completion by manually inserting trading_day record
|
||||
conn = get_db_connection(temp_env["db_path"])
|
||||
cursor = conn.cursor()
|
||||
|
||||
cursor.execute("""
|
||||
INSERT INTO trading_days (
|
||||
job_id, model, date, starting_cash, ending_cash,
|
||||
profit, return_pct, portfolio_value, completed_at
|
||||
)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
""", (
|
||||
job_id_1,
|
||||
"test-model",
|
||||
"2025-10-15",
|
||||
10000.0,
|
||||
9500.0,
|
||||
-500.0,
|
||||
-5.0,
|
||||
9500.0,
|
||||
"2025-11-07T01:00:00Z"
|
||||
))
|
||||
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
# Mark job_detail as completed
|
||||
manager.update_job_detail_status(
|
||||
job_id_1,
|
||||
"2025-10-15",
|
||||
"test-model",
|
||||
"completed"
|
||||
)
|
||||
|
||||
# Try to create second job with same model-day
|
||||
result_2 = manager.create_job(
|
||||
config_path=temp_env["config_path"],
|
||||
date_range=["2025-10-15", "2025-10-16"],
|
||||
models=["test-model"]
|
||||
)
|
||||
|
||||
# Should have warnings about skipped simulation
|
||||
assert len(result_2["warnings"]) == 1
|
||||
assert "2025-10-15" in result_2["warnings"][0]
|
||||
|
||||
# Should only create job_detail for 2025-10-16
|
||||
details = manager.get_job_details(result_2["job_id"])
|
||||
assert len(details) == 1
|
||||
assert details[0]["date"] == "2025-10-16"
|
||||
|
||||
|
||||
def test_portfolio_continues_from_previous_job(temp_env):
|
||||
"""Test that new job continues portfolio from previous job's last day."""
|
||||
manager = JobManager(db_path=temp_env["db_path"])
|
||||
|
||||
# Create and complete first job
|
||||
result_1 = manager.create_job(
|
||||
config_path=temp_env["config_path"],
|
||||
date_range=["2025-10-13"],
|
||||
models=["test-model"]
|
||||
)
|
||||
job_id_1 = result_1["job_id"]
|
||||
|
||||
# Insert completed trading_day with holdings
|
||||
conn = get_db_connection(temp_env["db_path"])
|
||||
cursor = conn.cursor()
|
||||
|
||||
cursor.execute("""
|
||||
INSERT INTO trading_days (
|
||||
job_id, model, date, starting_cash, ending_cash,
|
||||
profit, return_pct, portfolio_value, completed_at
|
||||
)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
""", (
|
||||
job_id_1,
|
||||
"test-model",
|
||||
"2025-10-13",
|
||||
10000.0,
|
||||
5000.0,
|
||||
0.0,
|
||||
0.0,
|
||||
15000.0,
|
||||
"2025-11-07T01:00:00Z"
|
||||
))
|
||||
|
||||
trading_day_id = cursor.lastrowid
|
||||
|
||||
cursor.execute("""
|
||||
INSERT INTO holdings (trading_day_id, symbol, quantity)
|
||||
VALUES (?, ?, ?)
|
||||
""", (trading_day_id, "AAPL", 10))
|
||||
|
||||
conn.commit()
|
||||
|
||||
# Mark as completed
|
||||
manager.update_job_detail_status(job_id_1, "2025-10-13", "test-model", "completed")
|
||||
manager.update_job_status(job_id_1, "completed")
|
||||
|
||||
# Create second job for next day
|
||||
result_2 = manager.create_job(
|
||||
config_path=temp_env["config_path"],
|
||||
date_range=["2025-10-14"],
|
||||
models=["test-model"]
|
||||
)
|
||||
job_id_2 = result_2["job_id"]
|
||||
|
||||
# Get starting position for 2025-10-14
|
||||
from agent_tools.tool_trade import get_current_position_from_db
|
||||
import agent_tools.tool_trade as trade_module
|
||||
original_get_db_connection = trade_module.get_db_connection
|
||||
|
||||
def mock_get_db_connection(path):
|
||||
return get_db_connection(temp_env["db_path"])
|
||||
|
||||
trade_module.get_db_connection = mock_get_db_connection
|
||||
|
||||
try:
|
||||
position, _ = get_current_position_from_db(
|
||||
job_id=job_id_2,
|
||||
model="test-model",
|
||||
date="2025-10-14",
|
||||
initial_cash=10000.0
|
||||
)
|
||||
|
||||
# Should continue from job 1's ending position
|
||||
assert position["CASH"] == 5000.0
|
||||
assert position["AAPL"] == 10
|
||||
finally:
|
||||
# Restore original function
|
||||
trade_module.get_db_connection = original_get_db_connection
|
||||
|
||||
conn.close()
|
||||
219
tests/unit/test_calculate_final_position.py
Normal file
219
tests/unit/test_calculate_final_position.py
Normal file
@@ -0,0 +1,219 @@
|
||||
"""Test _calculate_final_position_from_actions method."""
|
||||
|
||||
import pytest
|
||||
from unittest.mock import patch
|
||||
from agent.base_agent.base_agent import BaseAgent
|
||||
from api.database import Database
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def test_db():
|
||||
"""Create test database with schema."""
|
||||
db = Database(":memory:")
|
||||
|
||||
# Create jobs record
|
||||
db.connection.execute("""
|
||||
INSERT INTO jobs (job_id, config_path, status, date_range, models, created_at)
|
||||
VALUES ('test-job', 'test.json', 'running', '2025-10-07 to 2025-10-07', 'gpt-5', '2025-10-07T00:00:00Z')
|
||||
""")
|
||||
db.connection.commit()
|
||||
|
||||
return db
|
||||
|
||||
|
||||
def test_calculate_final_position_first_day_with_trades(test_db):
|
||||
"""Test calculating final position on first trading day with multiple trades."""
|
||||
|
||||
# Create trading_day for first day
|
||||
trading_day_id = test_db.create_trading_day(
|
||||
job_id='test-job',
|
||||
model='gpt-5',
|
||||
date='2025-10-07',
|
||||
starting_cash=10000.0,
|
||||
starting_portfolio_value=10000.0,
|
||||
daily_profit=0.0,
|
||||
daily_return_pct=0.0,
|
||||
ending_cash=10000.0, # Not yet calculated
|
||||
ending_portfolio_value=10000.0, # Not yet calculated
|
||||
days_since_last_trading=1
|
||||
)
|
||||
|
||||
# Add 15 buy actions (matching your real data)
|
||||
actions_data = [
|
||||
("MSFT", 3, 528.285, "buy"),
|
||||
("GOOGL", 6, 248.27, "buy"),
|
||||
("NVDA", 10, 186.23, "buy"),
|
||||
("LRCX", 6, 149.23, "buy"),
|
||||
("AVGO", 2, 337.025, "buy"),
|
||||
("AMZN", 5, 220.88, "buy"),
|
||||
("MSFT", 2, 528.285, "buy"), # Additional MSFT
|
||||
("AMD", 4, 214.85, "buy"),
|
||||
("CRWD", 1, 497.0, "buy"),
|
||||
("QCOM", 4, 169.9, "buy"),
|
||||
("META", 1, 717.72, "buy"),
|
||||
("NVDA", 20, 186.23, "buy"), # Additional NVDA
|
||||
("NVDA", 13, 186.23, "buy"), # Additional NVDA
|
||||
("NVDA", 20, 186.23, "buy"), # Additional NVDA
|
||||
("NVDA", 53, 186.23, "buy"), # Additional NVDA
|
||||
]
|
||||
|
||||
for symbol, quantity, price, action_type in actions_data:
|
||||
test_db.create_action(
|
||||
trading_day_id=trading_day_id,
|
||||
action_type=action_type,
|
||||
symbol=symbol,
|
||||
quantity=quantity,
|
||||
price=price
|
||||
)
|
||||
|
||||
test_db.connection.commit()
|
||||
|
||||
# Create BaseAgent instance
|
||||
agent = BaseAgent(signature="gpt-5", basemodel="anthropic/claude-sonnet-4", stock_symbols=[])
|
||||
|
||||
# Mock Database() to return our test_db
|
||||
with patch('api.database.Database', return_value=test_db):
|
||||
# Calculate final position
|
||||
holdings, cash = agent._calculate_final_position_from_actions(
|
||||
trading_day_id=trading_day_id,
|
||||
starting_cash=10000.0
|
||||
)
|
||||
|
||||
# Verify holdings
|
||||
assert holdings["MSFT"] == 5, f"Expected 5 MSFT (3+2) but got {holdings.get('MSFT', 0)}"
|
||||
assert holdings["GOOGL"] == 6, f"Expected 6 GOOGL but got {holdings.get('GOOGL', 0)}"
|
||||
assert holdings["NVDA"] == 116, f"Expected 116 NVDA (10+20+13+20+53) but got {holdings.get('NVDA', 0)}"
|
||||
assert holdings["LRCX"] == 6, f"Expected 6 LRCX but got {holdings.get('LRCX', 0)}"
|
||||
assert holdings["AVGO"] == 2, f"Expected 2 AVGO but got {holdings.get('AVGO', 0)}"
|
||||
assert holdings["AMZN"] == 5, f"Expected 5 AMZN but got {holdings.get('AMZN', 0)}"
|
||||
assert holdings["AMD"] == 4, f"Expected 4 AMD but got {holdings.get('AMD', 0)}"
|
||||
assert holdings["CRWD"] == 1, f"Expected 1 CRWD but got {holdings.get('CRWD', 0)}"
|
||||
assert holdings["QCOM"] == 4, f"Expected 4 QCOM but got {holdings.get('QCOM', 0)}"
|
||||
assert holdings["META"] == 1, f"Expected 1 META but got {holdings.get('META', 0)}"
|
||||
|
||||
# Verify cash (should be less than starting)
|
||||
assert cash < 10000.0, f"Cash should be less than $10,000 but got ${cash}"
|
||||
|
||||
# Calculate expected cash
|
||||
total_spent = sum(qty * price for _, qty, price, _ in actions_data)
|
||||
expected_cash = 10000.0 - total_spent
|
||||
assert abs(cash - expected_cash) < 0.01, f"Expected cash ${expected_cash} but got ${cash}"
|
||||
|
||||
|
||||
def test_calculate_final_position_with_previous_holdings(test_db):
|
||||
"""Test calculating final position when starting with existing holdings."""
|
||||
|
||||
# Create day 1 with ending holdings
|
||||
day1_id = test_db.create_trading_day(
|
||||
job_id='test-job',
|
||||
model='gpt-5',
|
||||
date='2025-10-06',
|
||||
starting_cash=10000.0,
|
||||
starting_portfolio_value=10000.0,
|
||||
daily_profit=0.0,
|
||||
daily_return_pct=0.0,
|
||||
ending_cash=8000.0,
|
||||
ending_portfolio_value=9500.0,
|
||||
days_since_last_trading=1
|
||||
)
|
||||
|
||||
# Add day 1 ending holdings
|
||||
test_db.create_holding(day1_id, "AAPL", 10)
|
||||
test_db.create_holding(day1_id, "MSFT", 5)
|
||||
|
||||
# Create day 2
|
||||
day2_id = test_db.create_trading_day(
|
||||
job_id='test-job',
|
||||
model='gpt-5',
|
||||
date='2025-10-07',
|
||||
starting_cash=8000.0,
|
||||
starting_portfolio_value=9500.0,
|
||||
daily_profit=0.0,
|
||||
daily_return_pct=0.0,
|
||||
ending_cash=8000.0,
|
||||
ending_portfolio_value=9500.0,
|
||||
days_since_last_trading=1
|
||||
)
|
||||
|
||||
# Add day 2 actions (buy more AAPL, sell some MSFT)
|
||||
test_db.create_action(day2_id, "buy", "AAPL", 5, 150.0)
|
||||
test_db.create_action(day2_id, "sell", "MSFT", 2, 500.0)
|
||||
|
||||
test_db.connection.commit()
|
||||
|
||||
# Create BaseAgent instance
|
||||
agent = BaseAgent(signature="gpt-5", basemodel="anthropic/claude-sonnet-4", stock_symbols=[])
|
||||
|
||||
# Mock Database() to return our test_db
|
||||
with patch('api.database.Database', return_value=test_db):
|
||||
# Calculate final position for day 2
|
||||
holdings, cash = agent._calculate_final_position_from_actions(
|
||||
trading_day_id=day2_id,
|
||||
starting_cash=8000.0
|
||||
)
|
||||
|
||||
# Verify holdings
|
||||
assert holdings["AAPL"] == 15, f"Expected 15 AAPL (10+5) but got {holdings.get('AAPL', 0)}"
|
||||
assert holdings["MSFT"] == 3, f"Expected 3 MSFT (5-2) but got {holdings.get('MSFT', 0)}"
|
||||
|
||||
# Verify cash
|
||||
# Started: 8000
|
||||
# Buy 5 AAPL @ 150 = -750
|
||||
# Sell 2 MSFT @ 500 = +1000
|
||||
# Final: 8000 - 750 + 1000 = 8250
|
||||
expected_cash = 8000.0 - (5 * 150.0) + (2 * 500.0)
|
||||
assert abs(cash - expected_cash) < 0.01, f"Expected cash ${expected_cash} but got ${cash}"
|
||||
|
||||
|
||||
def test_calculate_final_position_no_trades(test_db):
|
||||
"""Test calculating final position when no trades were executed."""
|
||||
|
||||
# Create day 1 with ending holdings
|
||||
day1_id = test_db.create_trading_day(
|
||||
job_id='test-job',
|
||||
model='gpt-5',
|
||||
date='2025-10-06',
|
||||
starting_cash=10000.0,
|
||||
starting_portfolio_value=10000.0,
|
||||
daily_profit=0.0,
|
||||
daily_return_pct=0.0,
|
||||
ending_cash=9000.0,
|
||||
ending_portfolio_value=10000.0,
|
||||
days_since_last_trading=1
|
||||
)
|
||||
|
||||
test_db.create_holding(day1_id, "AAPL", 10)
|
||||
|
||||
# Create day 2 with NO actions
|
||||
day2_id = test_db.create_trading_day(
|
||||
job_id='test-job',
|
||||
model='gpt-5',
|
||||
date='2025-10-07',
|
||||
starting_cash=9000.0,
|
||||
starting_portfolio_value=10000.0,
|
||||
daily_profit=0.0,
|
||||
daily_return_pct=0.0,
|
||||
ending_cash=9000.0,
|
||||
ending_portfolio_value=10000.0,
|
||||
days_since_last_trading=1
|
||||
)
|
||||
|
||||
# No actions added
|
||||
test_db.connection.commit()
|
||||
|
||||
# Create BaseAgent instance
|
||||
agent = BaseAgent(signature="gpt-5", basemodel="anthropic/claude-sonnet-4", stock_symbols=[])
|
||||
|
||||
# Mock Database() to return our test_db
|
||||
with patch('api.database.Database', return_value=test_db):
|
||||
# Calculate final position
|
||||
holdings, cash = agent._calculate_final_position_from_actions(
|
||||
trading_day_id=day2_id,
|
||||
starting_cash=9000.0
|
||||
)
|
||||
|
||||
# Verify holdings unchanged
|
||||
assert holdings["AAPL"] == 10, f"Expected 10 AAPL but got {holdings.get('AAPL', 0)}"
|
||||
|
||||
# Verify cash unchanged
|
||||
assert abs(cash - 9000.0) < 0.01, f"Expected cash $9000 but got ${cash}"
|
||||
216
tests/unit/test_chat_model_wrapper.py
Normal file
216
tests/unit/test_chat_model_wrapper.py
Normal file
@@ -0,0 +1,216 @@
|
||||
"""
|
||||
Unit tests for ChatModelWrapper - tool_calls args parsing fix
|
||||
"""
|
||||
|
||||
import json
|
||||
import pytest
|
||||
from unittest.mock import Mock, AsyncMock
|
||||
from langchain_core.messages import AIMessage
|
||||
from langchain_core.outputs import ChatResult, ChatGeneration
|
||||
|
||||
from agent.chat_model_wrapper import ToolCallArgsParsingWrapper
|
||||
|
||||
|
||||
class TestToolCallArgsParsingWrapper:
|
||||
"""Tests for ToolCallArgsParsingWrapper"""
|
||||
|
||||
@pytest.fixture
|
||||
def mock_model(self):
|
||||
"""Create a mock chat model"""
|
||||
model = Mock()
|
||||
model._llm_type = "mock-model"
|
||||
return model
|
||||
|
||||
@pytest.fixture
|
||||
def wrapper(self, mock_model):
|
||||
"""Create a wrapper around mock model"""
|
||||
return ToolCallArgsParsingWrapper(model=mock_model)
|
||||
|
||||
def test_fix_tool_calls_with_string_args(self, wrapper):
|
||||
"""Test that string args are parsed to dict"""
|
||||
# Create message with tool_calls where args is a JSON string
|
||||
message = AIMessage(
|
||||
content="",
|
||||
tool_calls=[
|
||||
{
|
||||
"name": "buy",
|
||||
"args": '{"symbol": "AAPL", "amount": 10}', # String, not dict
|
||||
"id": "call_123"
|
||||
}
|
||||
]
|
||||
)
|
||||
|
||||
fixed_message = wrapper._fix_tool_calls(message)
|
||||
|
||||
# Check that args is now a dict
|
||||
assert isinstance(fixed_message.tool_calls[0]['args'], dict)
|
||||
assert fixed_message.tool_calls[0]['args'] == {"symbol": "AAPL", "amount": 10}
|
||||
|
||||
def test_fix_tool_calls_with_dict_args(self, wrapper):
|
||||
"""Test that dict args are left unchanged"""
|
||||
# Create message with tool_calls where args is already a dict
|
||||
message = AIMessage(
|
||||
content="",
|
||||
tool_calls=[
|
||||
{
|
||||
"name": "buy",
|
||||
"args": {"symbol": "AAPL", "amount": 10}, # Already a dict
|
||||
"id": "call_123"
|
||||
}
|
||||
]
|
||||
)
|
||||
|
||||
fixed_message = wrapper._fix_tool_calls(message)
|
||||
|
||||
# Check that args is still a dict
|
||||
assert isinstance(fixed_message.tool_calls[0]['args'], dict)
|
||||
assert fixed_message.tool_calls[0]['args'] == {"symbol": "AAPL", "amount": 10}
|
||||
|
||||
def test_fix_tool_calls_with_invalid_json(self, wrapper):
|
||||
"""Test that invalid JSON string is left unchanged"""
|
||||
# Create message with tool_calls where args is an invalid JSON string
|
||||
message = AIMessage(
|
||||
content="",
|
||||
tool_calls=[
|
||||
{
|
||||
"name": "buy",
|
||||
"args": 'invalid json {', # Invalid JSON
|
||||
"id": "call_123"
|
||||
}
|
||||
]
|
||||
)
|
||||
|
||||
fixed_message = wrapper._fix_tool_calls(message)
|
||||
|
||||
# Check that args is still a string (parsing failed)
|
||||
assert isinstance(fixed_message.tool_calls[0]['args'], str)
|
||||
assert fixed_message.tool_calls[0]['args'] == 'invalid json {'
|
||||
|
||||
def test_fix_tool_calls_no_tool_calls(self, wrapper):
|
||||
"""Test that messages without tool_calls are left unchanged"""
|
||||
message = AIMessage(content="Hello, world!")
|
||||
fixed_message = wrapper._fix_tool_calls(message)
|
||||
|
||||
assert fixed_message == message
|
||||
|
||||
def test_generate_with_string_args(self, wrapper, mock_model):
|
||||
"""Test _generate method with string args"""
|
||||
# Create a response with string args
|
||||
original_message = AIMessage(
|
||||
content="",
|
||||
tool_calls=[
|
||||
{
|
||||
"name": "buy",
|
||||
"args": '{"symbol": "MSFT", "amount": 5}',
|
||||
"id": "call_456"
|
||||
}
|
||||
]
|
||||
)
|
||||
|
||||
mock_result = ChatResult(
|
||||
generations=[ChatGeneration(message=original_message)]
|
||||
)
|
||||
mock_model._generate.return_value = mock_result
|
||||
|
||||
# Call wrapper's _generate
|
||||
result = wrapper._generate(messages=[], stop=None, run_manager=None)
|
||||
|
||||
# Check that args is now a dict
|
||||
fixed_message = result.generations[0].message
|
||||
assert isinstance(fixed_message.tool_calls[0]['args'], dict)
|
||||
assert fixed_message.tool_calls[0]['args'] == {"symbol": "MSFT", "amount": 5}
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_agenerate_with_string_args(self, wrapper, mock_model):
|
||||
"""Test _agenerate method with string args"""
|
||||
# Create a response with string args
|
||||
original_message = AIMessage(
|
||||
content="",
|
||||
tool_calls=[
|
||||
{
|
||||
"name": "sell",
|
||||
"args": '{"symbol": "GOOGL", "amount": 3}',
|
||||
"id": "call_789"
|
||||
}
|
||||
]
|
||||
)
|
||||
|
||||
mock_result = ChatResult(
|
||||
generations=[ChatGeneration(message=original_message)]
|
||||
)
|
||||
mock_model._agenerate = AsyncMock(return_value=mock_result)
|
||||
|
||||
# Call wrapper's _agenerate
|
||||
result = await wrapper._agenerate(messages=[], stop=None, run_manager=None)
|
||||
|
||||
# Check that args is now a dict
|
||||
fixed_message = result.generations[0].message
|
||||
assert isinstance(fixed_message.tool_calls[0]['args'], dict)
|
||||
assert fixed_message.tool_calls[0]['args'] == {"symbol": "GOOGL", "amount": 3}
|
||||
|
||||
def test_invoke_with_string_args(self, wrapper, mock_model):
|
||||
"""Test invoke method with string args"""
|
||||
original_message = AIMessage(
|
||||
content="",
|
||||
tool_calls=[
|
||||
{
|
||||
"name": "buy",
|
||||
"args": '{"symbol": "NVDA", "amount": 20}',
|
||||
"id": "call_999"
|
||||
}
|
||||
]
|
||||
)
|
||||
|
||||
mock_model.invoke.return_value = original_message
|
||||
|
||||
# Call wrapper's invoke
|
||||
result = wrapper.invoke(input=[])
|
||||
|
||||
# Check that args is now a dict
|
||||
assert isinstance(result.tool_calls[0]['args'], dict)
|
||||
assert result.tool_calls[0]['args'] == {"symbol": "NVDA", "amount": 20}
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_ainvoke_with_string_args(self, wrapper, mock_model):
|
||||
"""Test ainvoke method with string args"""
|
||||
original_message = AIMessage(
|
||||
content="",
|
||||
tool_calls=[
|
||||
{
|
||||
"name": "sell",
|
||||
"args": '{"symbol": "TSLA", "amount": 15}',
|
||||
"id": "call_111"
|
||||
}
|
||||
]
|
||||
)
|
||||
|
||||
mock_model.ainvoke = AsyncMock(return_value=original_message)
|
||||
|
||||
# Call wrapper's ainvoke
|
||||
result = await wrapper.ainvoke(input=[])
|
||||
|
||||
# Check that args is now a dict
|
||||
assert isinstance(result.tool_calls[0]['args'], dict)
|
||||
assert result.tool_calls[0]['args'] == {"symbol": "TSLA", "amount": 15}
|
||||
|
||||
def test_bind_tools_returns_wrapper(self, wrapper, mock_model):
|
||||
"""Test that bind_tools returns a new wrapper"""
|
||||
mock_bound = Mock()
|
||||
mock_model.bind_tools.return_value = mock_bound
|
||||
|
||||
result = wrapper.bind_tools(tools=[], strict=True)
|
||||
|
||||
# Check that result is a wrapper around the bound model
|
||||
assert isinstance(result, ToolCallArgsParsingWrapper)
|
||||
assert result.wrapped_model == mock_bound
|
||||
|
||||
def test_bind_returns_wrapper(self, wrapper, mock_model):
|
||||
"""Test that bind returns a new wrapper"""
|
||||
mock_bound = Mock()
|
||||
mock_model.bind.return_value = mock_bound
|
||||
|
||||
result = wrapper.bind(max_tokens=100)
|
||||
|
||||
# Check that result is a wrapper around the bound model
|
||||
assert isinstance(result, ToolCallArgsParsingWrapper)
|
||||
assert result.wrapped_model == mock_bound
|
||||
200
tests/unit/test_context_injector.py
Normal file
200
tests/unit/test_context_injector.py
Normal file
@@ -0,0 +1,200 @@
|
||||
"""Test ContextInjector position tracking functionality."""
|
||||
|
||||
import pytest
|
||||
from agent.context_injector import ContextInjector
|
||||
from unittest.mock import Mock
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def injector():
|
||||
"""Create a ContextInjector instance for testing."""
|
||||
return ContextInjector(
|
||||
signature="test-model",
|
||||
today_date="2025-01-15",
|
||||
job_id="test-job-123",
|
||||
trading_day_id=1
|
||||
)
|
||||
|
||||
|
||||
class MockRequest:
|
||||
"""Mock MCP tool request."""
|
||||
def __init__(self, name, args=None):
|
||||
self.name = name
|
||||
self.args = args or {}
|
||||
|
||||
|
||||
def create_mcp_result(position_dict):
|
||||
"""Create a mock MCP CallToolResult object matching production behavior."""
|
||||
result = Mock()
|
||||
result.structuredContent = position_dict
|
||||
return result
|
||||
|
||||
|
||||
async def mock_handler_success(request):
|
||||
"""Mock handler that returns a successful position update as MCP CallToolResult."""
|
||||
# Simulate a successful trade returning updated position
|
||||
if request.name == "sell":
|
||||
return create_mcp_result({
|
||||
"CASH": 1100.0,
|
||||
"AAPL": 7,
|
||||
"MSFT": 5
|
||||
})
|
||||
elif request.name == "buy":
|
||||
return create_mcp_result({
|
||||
"CASH": 50.0,
|
||||
"AAPL": 7,
|
||||
"MSFT": 12
|
||||
})
|
||||
return create_mcp_result({})
|
||||
|
||||
|
||||
async def mock_handler_error(request):
|
||||
"""Mock handler that returns an error as MCP CallToolResult."""
|
||||
return create_mcp_result({"error": "Insufficient cash"})
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_context_injector_initializes_with_no_position(injector):
|
||||
"""Test that ContextInjector starts with no position state."""
|
||||
assert injector._current_position is None
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_context_injector_reset_position(injector):
|
||||
"""Test that reset_position() clears position state."""
|
||||
# Set some position state
|
||||
injector._current_position = {"CASH": 5000.0, "AAPL": 10}
|
||||
|
||||
# Reset
|
||||
injector.reset_position()
|
||||
|
||||
assert injector._current_position is None
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_context_injector_injects_parameters(injector):
|
||||
"""Test that context parameters are injected into buy/sell requests."""
|
||||
request = MockRequest("buy", {"symbol": "AAPL", "amount": 10})
|
||||
|
||||
# Mock handler that returns MCP result containing the request args
|
||||
async def handler(req):
|
||||
return create_mcp_result(req.args)
|
||||
|
||||
result = await injector(request, handler)
|
||||
|
||||
# Verify context was injected (result is MCP CallToolResult object)
|
||||
assert result.structuredContent["signature"] == "test-model"
|
||||
assert result.structuredContent["today_date"] == "2025-01-15"
|
||||
assert result.structuredContent["job_id"] == "test-job-123"
|
||||
assert result.structuredContent["trading_day_id"] == 1
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_context_injector_tracks_position_after_successful_trade(injector):
|
||||
"""Test that position state is updated after successful trades."""
|
||||
assert injector._current_position is None
|
||||
|
||||
# Execute a sell trade
|
||||
request = MockRequest("sell", {"symbol": "AAPL", "amount": 3})
|
||||
result = await injector(request, mock_handler_success)
|
||||
|
||||
# Verify position was updated
|
||||
assert injector._current_position is not None
|
||||
assert injector._current_position["CASH"] == 1100.0
|
||||
assert injector._current_position["AAPL"] == 7
|
||||
assert injector._current_position["MSFT"] == 5
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_context_injector_injects_current_position_on_subsequent_trades(injector):
|
||||
"""Test that current position is injected into subsequent trade requests."""
|
||||
# First trade - establish position
|
||||
request1 = MockRequest("sell", {"symbol": "AAPL", "amount": 3})
|
||||
await injector(request1, mock_handler_success)
|
||||
|
||||
# Second trade - should receive current position
|
||||
request2 = MockRequest("buy", {"symbol": "MSFT", "amount": 7})
|
||||
|
||||
async def verify_injection_handler(req):
|
||||
# Verify that _current_position was injected
|
||||
assert "_current_position" in req.args
|
||||
assert req.args["_current_position"]["CASH"] == 1100.0
|
||||
assert req.args["_current_position"]["AAPL"] == 7
|
||||
return mock_handler_success(req)
|
||||
|
||||
await injector(request2, verify_injection_handler)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_context_injector_does_not_update_position_on_error(injector):
|
||||
"""Test that position state is NOT updated when trade fails."""
|
||||
# First successful trade
|
||||
request1 = MockRequest("sell", {"symbol": "AAPL", "amount": 3})
|
||||
await injector(request1, mock_handler_success)
|
||||
|
||||
original_position = injector._current_position.copy()
|
||||
|
||||
# Second trade that fails
|
||||
request2 = MockRequest("buy", {"symbol": "MSFT", "amount": 100})
|
||||
result = await injector(request2, mock_handler_error)
|
||||
|
||||
# Verify position was NOT updated
|
||||
assert injector._current_position == original_position
|
||||
assert "error" in result.structuredContent
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_context_injector_does_not_inject_position_for_non_trade_tools(injector):
|
||||
"""Test that position is not injected for non-buy/sell tools."""
|
||||
# Set up position state
|
||||
injector._current_position = {"CASH": 5000.0, "AAPL": 10}
|
||||
|
||||
# Call a non-trade tool
|
||||
request = MockRequest("search", {"query": "market news"})
|
||||
|
||||
async def verify_no_injection_handler(req):
|
||||
assert "_current_position" not in req.args
|
||||
return create_mcp_result({"results": []})
|
||||
|
||||
await injector(request, verify_no_injection_handler)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_context_injector_full_trading_session_simulation(injector):
|
||||
"""Test full trading session with multiple trades and position tracking."""
|
||||
# Reset position at start of day
|
||||
injector.reset_position()
|
||||
assert injector._current_position is None
|
||||
|
||||
# Trade 1: Sell AAPL
|
||||
request1 = MockRequest("sell", {"symbol": "AAPL", "amount": 3})
|
||||
|
||||
async def handler1(req):
|
||||
# First trade should NOT have injected position
|
||||
assert req.args.get("_current_position") is None
|
||||
return create_mcp_result({"CASH": 1100.0, "AAPL": 7})
|
||||
|
||||
result1 = await injector(request1, handler1)
|
||||
assert injector._current_position == {"CASH": 1100.0, "AAPL": 7}
|
||||
|
||||
# Trade 2: Buy MSFT (should use position from trade 1)
|
||||
request2 = MockRequest("buy", {"symbol": "MSFT", "amount": 7})
|
||||
|
||||
async def handler2(req):
|
||||
# Second trade SHOULD have injected position from trade 1
|
||||
assert req.args["_current_position"]["CASH"] == 1100.0
|
||||
assert req.args["_current_position"]["AAPL"] == 7
|
||||
return create_mcp_result({"CASH": 50.0, "AAPL": 7, "MSFT": 7})
|
||||
|
||||
result2 = await injector(request2, handler2)
|
||||
assert injector._current_position == {"CASH": 50.0, "AAPL": 7, "MSFT": 7}
|
||||
|
||||
# Trade 3: Failed trade (should not update position)
|
||||
request3 = MockRequest("buy", {"symbol": "GOOGL", "amount": 100})
|
||||
|
||||
async def handler3(req):
|
||||
return create_mcp_result({"error": "Insufficient cash", "cash_available": 50.0})
|
||||
|
||||
result3 = await injector(request3, handler3)
|
||||
# Position should remain unchanged after failed trade
|
||||
assert injector._current_position == {"CASH": 50.0, "AAPL": 7, "MSFT": 7}
|
||||
229
tests/unit/test_cross_job_position_continuity.py
Normal file
229
tests/unit/test_cross_job_position_continuity.py
Normal file
@@ -0,0 +1,229 @@
|
||||
"""Test portfolio continuity across multiple jobs."""
|
||||
import pytest
|
||||
import tempfile
|
||||
import os
|
||||
from agent_tools.tool_trade import get_current_position_from_db
|
||||
from api.database import get_db_connection
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def temp_db():
|
||||
"""Create temporary database with schema."""
|
||||
fd, path = tempfile.mkstemp(suffix='.db')
|
||||
os.close(fd)
|
||||
|
||||
conn = get_db_connection(path)
|
||||
cursor = conn.cursor()
|
||||
|
||||
# Create trading_days table
|
||||
cursor.execute("""
|
||||
CREATE TABLE IF NOT EXISTS trading_days (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
job_id TEXT NOT NULL,
|
||||
model TEXT NOT NULL,
|
||||
date TEXT NOT NULL,
|
||||
starting_cash REAL NOT NULL,
|
||||
ending_cash REAL NOT NULL,
|
||||
profit REAL NOT NULL,
|
||||
return_pct REAL NOT NULL,
|
||||
portfolio_value REAL NOT NULL,
|
||||
reasoning_summary TEXT,
|
||||
reasoning_full TEXT,
|
||||
completed_at TEXT,
|
||||
session_duration_seconds REAL,
|
||||
UNIQUE(job_id, model, date)
|
||||
)
|
||||
""")
|
||||
|
||||
# Create holdings table
|
||||
cursor.execute("""
|
||||
CREATE TABLE IF NOT EXISTS holdings (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
trading_day_id INTEGER NOT NULL,
|
||||
symbol TEXT NOT NULL,
|
||||
quantity INTEGER NOT NULL,
|
||||
FOREIGN KEY (trading_day_id) REFERENCES trading_days(id) ON DELETE CASCADE
|
||||
)
|
||||
""")
|
||||
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
yield path
|
||||
|
||||
if os.path.exists(path):
|
||||
os.remove(path)
|
||||
|
||||
|
||||
def test_position_continuity_across_jobs(temp_db):
|
||||
"""Test that position queries see history from previous jobs."""
|
||||
# Insert trading_day from job 1
|
||||
conn = get_db_connection(temp_db)
|
||||
cursor = conn.cursor()
|
||||
|
||||
cursor.execute("""
|
||||
INSERT INTO trading_days (
|
||||
job_id, model, date, starting_cash, ending_cash,
|
||||
profit, return_pct, portfolio_value, completed_at
|
||||
)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
""", (
|
||||
"job-1-uuid",
|
||||
"deepseek-chat-v3.1",
|
||||
"2025-10-14",
|
||||
10000.0,
|
||||
5121.52, # Negative cash from buying
|
||||
0.0,
|
||||
0.0,
|
||||
14993.945,
|
||||
"2025-11-07T01:52:53Z"
|
||||
))
|
||||
|
||||
trading_day_id = cursor.lastrowid
|
||||
|
||||
# Insert holdings from job 1
|
||||
holdings = [
|
||||
("ADBE", 5),
|
||||
("AVGO", 5),
|
||||
("CRWD", 5),
|
||||
("GOOGL", 20),
|
||||
("META", 5),
|
||||
("MSFT", 5),
|
||||
("NVDA", 10)
|
||||
]
|
||||
|
||||
for symbol, quantity in holdings:
|
||||
cursor.execute("""
|
||||
INSERT INTO holdings (trading_day_id, symbol, quantity)
|
||||
VALUES (?, ?, ?)
|
||||
""", (trading_day_id, symbol, quantity))
|
||||
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
# Mock get_db_connection to return our test db
|
||||
import agent_tools.tool_trade as trade_module
|
||||
original_get_db_connection = trade_module.get_db_connection
|
||||
|
||||
def mock_get_db_connection(path):
|
||||
return get_db_connection(temp_db)
|
||||
|
||||
trade_module.get_db_connection = mock_get_db_connection
|
||||
|
||||
try:
|
||||
# Now query position for job 2 on next trading day
|
||||
position, _ = get_current_position_from_db(
|
||||
job_id="job-2-uuid", # Different job
|
||||
model="deepseek-chat-v3.1",
|
||||
date="2025-10-15",
|
||||
initial_cash=10000.0
|
||||
)
|
||||
|
||||
# Should see job 1's ending position, NOT initial $10k
|
||||
assert position["CASH"] == 5121.52
|
||||
assert position["ADBE"] == 5
|
||||
assert position["AVGO"] == 5
|
||||
assert position["CRWD"] == 5
|
||||
assert position["GOOGL"] == 20
|
||||
assert position["META"] == 5
|
||||
assert position["MSFT"] == 5
|
||||
assert position["NVDA"] == 10
|
||||
finally:
|
||||
# Restore original function
|
||||
trade_module.get_db_connection = original_get_db_connection
|
||||
|
||||
|
||||
def test_position_returns_initial_state_for_first_day(temp_db):
|
||||
"""Test that first trading day returns initial cash."""
|
||||
# Mock get_db_connection to return our test db
|
||||
import agent_tools.tool_trade as trade_module
|
||||
original_get_db_connection = trade_module.get_db_connection
|
||||
|
||||
def mock_get_db_connection(path):
|
||||
return get_db_connection(temp_db)
|
||||
|
||||
trade_module.get_db_connection = mock_get_db_connection
|
||||
|
||||
try:
|
||||
# No previous trading days exist
|
||||
position, _ = get_current_position_from_db(
|
||||
job_id="new-job-uuid",
|
||||
model="new-model",
|
||||
date="2025-10-13",
|
||||
initial_cash=10000.0
|
||||
)
|
||||
|
||||
# Should return initial position
|
||||
assert position == {"CASH": 10000.0}
|
||||
finally:
|
||||
# Restore original function
|
||||
trade_module.get_db_connection = original_get_db_connection
|
||||
|
||||
|
||||
def test_position_uses_most_recent_prior_date(temp_db):
|
||||
"""Test that position query uses the most recent date before current."""
|
||||
conn = get_db_connection(temp_db)
|
||||
cursor = conn.cursor()
|
||||
|
||||
# Insert two trading days
|
||||
cursor.execute("""
|
||||
INSERT INTO trading_days (
|
||||
job_id, model, date, starting_cash, ending_cash,
|
||||
profit, return_pct, portfolio_value, completed_at
|
||||
)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
""", (
|
||||
"job-1",
|
||||
"model-a",
|
||||
"2025-10-13",
|
||||
10000.0,
|
||||
9500.0,
|
||||
-500.0,
|
||||
-5.0,
|
||||
9500.0,
|
||||
"2025-11-07T01:00:00Z"
|
||||
))
|
||||
|
||||
cursor.execute("""
|
||||
INSERT INTO trading_days (
|
||||
job_id, model, date, starting_cash, ending_cash,
|
||||
profit, return_pct, portfolio_value, completed_at
|
||||
)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
""", (
|
||||
"job-2",
|
||||
"model-a",
|
||||
"2025-10-14",
|
||||
9500.0,
|
||||
12000.0,
|
||||
2500.0,
|
||||
26.3,
|
||||
12000.0,
|
||||
"2025-11-07T02:00:00Z"
|
||||
))
|
||||
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
# Mock get_db_connection to return our test db
|
||||
import agent_tools.tool_trade as trade_module
|
||||
original_get_db_connection = trade_module.get_db_connection
|
||||
|
||||
def mock_get_db_connection(path):
|
||||
return get_db_connection(temp_db)
|
||||
|
||||
trade_module.get_db_connection = mock_get_db_connection
|
||||
|
||||
try:
|
||||
# Query for 2025-10-15 should use 2025-10-14's ending position
|
||||
position, _ = get_current_position_from_db(
|
||||
job_id="job-3",
|
||||
model="model-a",
|
||||
date="2025-10-15",
|
||||
initial_cash=10000.0
|
||||
)
|
||||
|
||||
assert position["CASH"] == 12000.0 # From 2025-10-14, not 2025-10-13
|
||||
finally:
|
||||
# Restore original function
|
||||
trade_module.get_db_connection = original_get_db_connection
|
||||
@@ -26,11 +26,12 @@ class TestJobCreation:
|
||||
from api.job_manager import JobManager
|
||||
|
||||
manager = JobManager(db_path=clean_db)
|
||||
job_id = manager.create_job(
|
||||
job_result = manager.create_job(
|
||||
config_path="configs/test.json",
|
||||
date_range=["2025-01-16", "2025-01-17"],
|
||||
models=["gpt-5", "claude-3.7-sonnet"]
|
||||
)
|
||||
job_id = job_result["job_id"]
|
||||
|
||||
assert job_id is not None
|
||||
job = manager.get_job(job_id)
|
||||
@@ -44,11 +45,12 @@ class TestJobCreation:
|
||||
from api.job_manager import JobManager
|
||||
|
||||
manager = JobManager(db_path=clean_db)
|
||||
job_id = manager.create_job(
|
||||
job_result = manager.create_job(
|
||||
config_path="configs/test.json",
|
||||
date_range=["2025-01-16", "2025-01-17"],
|
||||
models=["gpt-5"]
|
||||
)
|
||||
job_id = job_result["job_id"]
|
||||
|
||||
progress = manager.get_job_progress(job_id)
|
||||
assert progress["total_model_days"] == 2 # 2 dates × 1 model
|
||||
@@ -60,11 +62,12 @@ class TestJobCreation:
|
||||
from api.job_manager import JobManager
|
||||
|
||||
manager = JobManager(db_path=clean_db)
|
||||
job1_id = manager.create_job(
|
||||
job1_result = manager.create_job(
|
||||
"configs/test.json",
|
||||
["2025-01-16"],
|
||||
["gpt-5"]
|
||||
)
|
||||
job1_id = job1_result["job_id"]
|
||||
|
||||
with pytest.raises(ValueError, match="Another simulation job is already running"):
|
||||
manager.create_job(
|
||||
@@ -78,20 +81,22 @@ class TestJobCreation:
|
||||
from api.job_manager import JobManager
|
||||
|
||||
manager = JobManager(db_path=clean_db)
|
||||
job1_id = manager.create_job(
|
||||
job1_result = manager.create_job(
|
||||
"configs/test.json",
|
||||
["2025-01-16"],
|
||||
["gpt-5"]
|
||||
)
|
||||
job1_id = job1_result["job_id"]
|
||||
|
||||
manager.update_job_status(job1_id, "completed")
|
||||
|
||||
# Now second job should be allowed
|
||||
job2_id = manager.create_job(
|
||||
job2_result = manager.create_job(
|
||||
"configs/test.json",
|
||||
["2025-01-17"],
|
||||
["gpt-5"]
|
||||
)
|
||||
job2_id = job2_result["job_id"]
|
||||
assert job2_id is not None
|
||||
|
||||
|
||||
@@ -104,11 +109,12 @@ class TestJobStatusTransitions:
|
||||
from api.job_manager import JobManager
|
||||
|
||||
manager = JobManager(db_path=clean_db)
|
||||
job_id = manager.create_job(
|
||||
job_result = manager.create_job(
|
||||
"configs/test.json",
|
||||
["2025-01-16"],
|
||||
["gpt-5"]
|
||||
)
|
||||
job_id = job_result["job_id"]
|
||||
|
||||
# Update detail to running
|
||||
manager.update_job_detail_status(job_id, "2025-01-16", "gpt-5", "running")
|
||||
@@ -122,11 +128,12 @@ class TestJobStatusTransitions:
|
||||
from api.job_manager import JobManager
|
||||
|
||||
manager = JobManager(db_path=clean_db)
|
||||
job_id = manager.create_job(
|
||||
job_result = manager.create_job(
|
||||
"configs/test.json",
|
||||
["2025-01-16"],
|
||||
["gpt-5"]
|
||||
)
|
||||
job_id = job_result["job_id"]
|
||||
|
||||
manager.update_job_detail_status(job_id, "2025-01-16", "gpt-5", "running")
|
||||
manager.update_job_detail_status(job_id, "2025-01-16", "gpt-5", "completed")
|
||||
@@ -141,11 +148,12 @@ class TestJobStatusTransitions:
|
||||
from api.job_manager import JobManager
|
||||
|
||||
manager = JobManager(db_path=clean_db)
|
||||
job_id = manager.create_job(
|
||||
job_result = manager.create_job(
|
||||
"configs/test.json",
|
||||
["2025-01-16"],
|
||||
["gpt-5", "claude-3.7-sonnet"]
|
||||
)
|
||||
job_id = job_result["job_id"]
|
||||
|
||||
# First model succeeds
|
||||
manager.update_job_detail_status(job_id, "2025-01-16", "gpt-5", "running")
|
||||
@@ -183,10 +191,12 @@ class TestJobRetrieval:
|
||||
from api.job_manager import JobManager
|
||||
|
||||
manager = JobManager(db_path=clean_db)
|
||||
job1_id = manager.create_job("configs/test.json", ["2025-01-16"], ["gpt-5"])
|
||||
job1_result = manager.create_job("configs/test.json", ["2025-01-16"], ["gpt-5"])
|
||||
job1_id = job1_result["job_id"]
|
||||
manager.update_job_status(job1_id, "completed")
|
||||
|
||||
job2_id = manager.create_job("configs/test.json", ["2025-01-17"], ["gpt-5"])
|
||||
job2_result = manager.create_job("configs/test.json", ["2025-01-17"], ["gpt-5"])
|
||||
job2_id = job2_result["job_id"]
|
||||
|
||||
current = manager.get_current_job()
|
||||
assert current["job_id"] == job2_id
|
||||
@@ -204,11 +214,12 @@ class TestJobRetrieval:
|
||||
from api.job_manager import JobManager
|
||||
|
||||
manager = JobManager(db_path=clean_db)
|
||||
job_id = manager.create_job(
|
||||
job_result = manager.create_job(
|
||||
"configs/test.json",
|
||||
["2025-01-16", "2025-01-17"],
|
||||
["gpt-5"]
|
||||
)
|
||||
job_id = job_result["job_id"]
|
||||
|
||||
found = manager.find_job_by_date_range(["2025-01-16", "2025-01-17"])
|
||||
assert found["job_id"] == job_id
|
||||
@@ -237,11 +248,12 @@ class TestJobProgress:
|
||||
from api.job_manager import JobManager
|
||||
|
||||
manager = JobManager(db_path=clean_db)
|
||||
job_id = manager.create_job(
|
||||
job_result = manager.create_job(
|
||||
"configs/test.json",
|
||||
["2025-01-16", "2025-01-17"],
|
||||
["gpt-5"]
|
||||
)
|
||||
job_id = job_result["job_id"]
|
||||
|
||||
progress = manager.get_job_progress(job_id)
|
||||
assert progress["total_model_days"] == 2
|
||||
@@ -254,11 +266,12 @@ class TestJobProgress:
|
||||
from api.job_manager import JobManager
|
||||
|
||||
manager = JobManager(db_path=clean_db)
|
||||
job_id = manager.create_job(
|
||||
job_result = manager.create_job(
|
||||
"configs/test.json",
|
||||
["2025-01-16"],
|
||||
["gpt-5"]
|
||||
)
|
||||
job_id = job_result["job_id"]
|
||||
|
||||
manager.update_job_detail_status(job_id, "2025-01-16", "gpt-5", "running")
|
||||
|
||||
@@ -270,11 +283,12 @@ class TestJobProgress:
|
||||
from api.job_manager import JobManager
|
||||
|
||||
manager = JobManager(db_path=clean_db)
|
||||
job_id = manager.create_job(
|
||||
job_result = manager.create_job(
|
||||
"configs/test.json",
|
||||
["2025-01-16"],
|
||||
["gpt-5", "claude-3.7-sonnet"]
|
||||
)
|
||||
job_id = job_result["job_id"]
|
||||
|
||||
manager.update_job_detail_status(job_id, "2025-01-16", "gpt-5", "completed")
|
||||
|
||||
@@ -311,7 +325,8 @@ class TestConcurrencyControl:
|
||||
from api.job_manager import JobManager
|
||||
|
||||
manager = JobManager(db_path=clean_db)
|
||||
job_id = manager.create_job("configs/test.json", ["2025-01-16"], ["gpt-5"])
|
||||
job_result = manager.create_job("configs/test.json", ["2025-01-16"], ["gpt-5"])
|
||||
job_id = job_result["job_id"]
|
||||
manager.update_job_status(job_id, "running")
|
||||
|
||||
assert manager.can_start_new_job() is False
|
||||
@@ -321,7 +336,8 @@ class TestConcurrencyControl:
|
||||
from api.job_manager import JobManager
|
||||
|
||||
manager = JobManager(db_path=clean_db)
|
||||
job_id = manager.create_job("configs/test.json", ["2025-01-16"], ["gpt-5"])
|
||||
job_result = manager.create_job("configs/test.json", ["2025-01-16"], ["gpt-5"])
|
||||
job_id = job_result["job_id"]
|
||||
manager.update_job_status(job_id, "completed")
|
||||
|
||||
assert manager.can_start_new_job() is True
|
||||
@@ -331,13 +347,15 @@ class TestConcurrencyControl:
|
||||
from api.job_manager import JobManager
|
||||
|
||||
manager = JobManager(db_path=clean_db)
|
||||
job1_id = manager.create_job("configs/test.json", ["2025-01-16"], ["gpt-5"])
|
||||
job1_result = manager.create_job("configs/test.json", ["2025-01-16"], ["gpt-5"])
|
||||
job1_id = job1_result["job_id"]
|
||||
|
||||
# Complete first job
|
||||
manager.update_job_status(job1_id, "completed")
|
||||
|
||||
# Create second job
|
||||
job2_id = manager.create_job("configs/test.json", ["2025-01-17"], ["gpt-5"])
|
||||
job2_result = manager.create_job("configs/test.json", ["2025-01-17"], ["gpt-5"])
|
||||
job2_id = job2_result["job_id"]
|
||||
|
||||
running = manager.get_running_jobs()
|
||||
assert len(running) == 1
|
||||
@@ -368,12 +386,13 @@ class TestJobCleanup:
|
||||
conn.close()
|
||||
|
||||
# Create recent job
|
||||
recent_id = manager.create_job("configs/test.json", ["2025-01-16"], ["gpt-5"])
|
||||
recent_result = manager.create_job("configs/test.json", ["2025-01-16"], ["gpt-5"])
|
||||
recent_id = recent_result["job_id"]
|
||||
|
||||
# Cleanup jobs older than 30 days
|
||||
result = manager.cleanup_old_jobs(days=30)
|
||||
cleanup_result = manager.cleanup_old_jobs(days=30)
|
||||
|
||||
assert result["jobs_deleted"] == 1
|
||||
assert cleanup_result["jobs_deleted"] == 1
|
||||
assert manager.get_job("old-job") is None
|
||||
assert manager.get_job(recent_id) is not None
|
||||
|
||||
@@ -387,7 +406,8 @@ class TestJobUpdateOperations:
|
||||
from api.job_manager import JobManager
|
||||
|
||||
manager = JobManager(db_path=clean_db)
|
||||
job_id = manager.create_job("configs/test.json", ["2025-01-16"], ["gpt-5"])
|
||||
job_result = manager.create_job("configs/test.json", ["2025-01-16"], ["gpt-5"])
|
||||
job_id = job_result["job_id"]
|
||||
|
||||
manager.update_job_status(job_id, "failed", error="MCP service unavailable")
|
||||
|
||||
@@ -401,7 +421,8 @@ class TestJobUpdateOperations:
|
||||
import time
|
||||
|
||||
manager = JobManager(db_path=clean_db)
|
||||
job_id = manager.create_job("configs/test.json", ["2025-01-16"], ["gpt-5"])
|
||||
job_result = manager.create_job("configs/test.json", ["2025-01-16"], ["gpt-5"])
|
||||
job_id = job_result["job_id"]
|
||||
|
||||
# Start
|
||||
manager.update_job_detail_status(job_id, "2025-01-16", "gpt-5", "running")
|
||||
@@ -432,11 +453,12 @@ class TestJobWarnings:
|
||||
job_manager = JobManager(db_path=clean_db)
|
||||
|
||||
# Create a job
|
||||
job_id = job_manager.create_job(
|
||||
job_result = job_manager.create_job(
|
||||
config_path="config.json",
|
||||
date_range=["2025-10-01"],
|
||||
models=["gpt-5"]
|
||||
)
|
||||
job_id = job_result["job_id"]
|
||||
|
||||
# Add warnings
|
||||
warnings = ["Rate limit reached", "Skipped 2 dates"]
|
||||
@@ -448,4 +470,172 @@ class TestJobWarnings:
|
||||
assert stored_warnings == warnings
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestStaleJobCleanup:
|
||||
"""Test cleanup of stale jobs from container restarts."""
|
||||
|
||||
def test_cleanup_stale_pending_job(self, clean_db):
|
||||
"""Should mark pending job as failed with no progress."""
|
||||
from api.job_manager import JobManager
|
||||
|
||||
manager = JobManager(db_path=clean_db)
|
||||
job_result = manager.create_job(
|
||||
config_path="configs/test.json",
|
||||
date_range=["2025-01-16", "2025-01-17"],
|
||||
models=["gpt-5"]
|
||||
)
|
||||
job_id = job_result["job_id"]
|
||||
|
||||
# Job is pending - simulate container restart
|
||||
result = manager.cleanup_stale_jobs()
|
||||
|
||||
assert result["jobs_cleaned"] == 1
|
||||
job = manager.get_job(job_id)
|
||||
assert job["status"] == "failed"
|
||||
assert "container restart" in job["error"].lower()
|
||||
assert "pending" in job["error"]
|
||||
assert "no progress" in job["error"]
|
||||
|
||||
def test_cleanup_stale_running_job_with_partial_progress(self, clean_db):
|
||||
"""Should mark running job as partial if some model-days completed."""
|
||||
from api.job_manager import JobManager
|
||||
|
||||
manager = JobManager(db_path=clean_db)
|
||||
job_result = manager.create_job(
|
||||
config_path="configs/test.json",
|
||||
date_range=["2025-01-16", "2025-01-17"],
|
||||
models=["gpt-5"]
|
||||
)
|
||||
job_id = job_result["job_id"]
|
||||
|
||||
# Mark job as running and complete one model-day
|
||||
manager.update_job_status(job_id, "running")
|
||||
manager.update_job_detail_status(job_id, "2025-01-16", "gpt-5", "completed")
|
||||
|
||||
# Simulate container restart
|
||||
result = manager.cleanup_stale_jobs()
|
||||
|
||||
assert result["jobs_cleaned"] == 1
|
||||
job = manager.get_job(job_id)
|
||||
assert job["status"] == "partial"
|
||||
assert "container restart" in job["error"].lower()
|
||||
assert "1/2" in job["error"] # 1 out of 2 model-days completed
|
||||
|
||||
def test_cleanup_stale_downloading_data_job(self, clean_db):
|
||||
"""Should mark downloading_data job as failed."""
|
||||
from api.job_manager import JobManager
|
||||
|
||||
manager = JobManager(db_path=clean_db)
|
||||
job_result = manager.create_job(
|
||||
config_path="configs/test.json",
|
||||
date_range=["2025-01-16"],
|
||||
models=["gpt-5"]
|
||||
)
|
||||
job_id = job_result["job_id"]
|
||||
|
||||
# Mark as downloading data
|
||||
manager.update_job_status(job_id, "downloading_data")
|
||||
|
||||
# Simulate container restart
|
||||
result = manager.cleanup_stale_jobs()
|
||||
|
||||
assert result["jobs_cleaned"] == 1
|
||||
job = manager.get_job(job_id)
|
||||
assert job["status"] == "failed"
|
||||
assert "downloading_data" in job["error"]
|
||||
|
||||
def test_cleanup_marks_incomplete_job_details_as_failed(self, clean_db):
|
||||
"""Should mark incomplete job_details as failed."""
|
||||
from api.job_manager import JobManager
|
||||
|
||||
manager = JobManager(db_path=clean_db)
|
||||
job_result = manager.create_job(
|
||||
config_path="configs/test.json",
|
||||
date_range=["2025-01-16", "2025-01-17"],
|
||||
models=["gpt-5"]
|
||||
)
|
||||
job_id = job_result["job_id"]
|
||||
|
||||
# Mark job as running, one detail running, one pending
|
||||
manager.update_job_status(job_id, "running")
|
||||
manager.update_job_detail_status(job_id, "2025-01-16", "gpt-5", "running")
|
||||
|
||||
# Simulate container restart
|
||||
manager.cleanup_stale_jobs()
|
||||
|
||||
# Check job_details were marked as failed
|
||||
progress = manager.get_job_progress(job_id)
|
||||
assert progress["failed"] == 2 # Both model-days marked failed
|
||||
assert progress["pending"] == 0
|
||||
|
||||
details = manager.get_job_details(job_id)
|
||||
for detail in details:
|
||||
assert detail["status"] == "failed"
|
||||
assert "container restarted" in detail["error"].lower()
|
||||
|
||||
def test_cleanup_no_stale_jobs(self, clean_db):
|
||||
"""Should report 0 cleaned jobs when none are stale."""
|
||||
from api.job_manager import JobManager
|
||||
|
||||
manager = JobManager(db_path=clean_db)
|
||||
job_result = manager.create_job(
|
||||
config_path="configs/test.json",
|
||||
date_range=["2025-01-16"],
|
||||
models=["gpt-5"]
|
||||
)
|
||||
job_id = job_result["job_id"]
|
||||
|
||||
# Complete the job
|
||||
manager.update_job_detail_status(job_id, "2025-01-16", "gpt-5", "completed")
|
||||
|
||||
# Simulate container restart
|
||||
result = manager.cleanup_stale_jobs()
|
||||
|
||||
assert result["jobs_cleaned"] == 0
|
||||
job = manager.get_job(job_id)
|
||||
assert job["status"] == "completed"
|
||||
|
||||
def test_cleanup_multiple_stale_jobs(self, clean_db):
|
||||
"""Should clean up multiple stale jobs."""
|
||||
from api.job_manager import JobManager
|
||||
|
||||
manager = JobManager(db_path=clean_db)
|
||||
|
||||
# Create first job
|
||||
job1_result = manager.create_job(
|
||||
config_path="configs/test.json",
|
||||
date_range=["2025-01-16"],
|
||||
models=["gpt-5"]
|
||||
)
|
||||
job1_id = job1_result["job_id"]
|
||||
manager.update_job_status(job1_id, "running")
|
||||
manager.update_job_status(job1_id, "completed")
|
||||
|
||||
# Create second job (pending)
|
||||
job2_result = manager.create_job(
|
||||
config_path="configs/test.json",
|
||||
date_range=["2025-01-17"],
|
||||
models=["gpt-5"]
|
||||
)
|
||||
job2_id = job2_result["job_id"]
|
||||
|
||||
# Create third job (running)
|
||||
manager.update_job_status(job2_id, "completed")
|
||||
job3_result = manager.create_job(
|
||||
config_path="configs/test.json",
|
||||
date_range=["2025-01-18"],
|
||||
models=["gpt-5"]
|
||||
)
|
||||
job3_id = job3_result["job_id"]
|
||||
manager.update_job_status(job3_id, "running")
|
||||
|
||||
# Simulate container restart
|
||||
result = manager.cleanup_stale_jobs()
|
||||
|
||||
assert result["jobs_cleaned"] == 1 # Only job3 is running
|
||||
assert manager.get_job(job1_id)["status"] == "completed"
|
||||
assert manager.get_job(job2_id)["status"] == "completed"
|
||||
assert manager.get_job(job3_id)["status"] == "failed"
|
||||
|
||||
|
||||
# Coverage target: 95%+ for api/job_manager.py
|
||||
|
||||
256
tests/unit/test_job_manager_duplicate_detection.py
Normal file
256
tests/unit/test_job_manager_duplicate_detection.py
Normal file
@@ -0,0 +1,256 @@
|
||||
"""Test duplicate detection in job creation."""
|
||||
import pytest
|
||||
import tempfile
|
||||
import os
|
||||
from pathlib import Path
|
||||
from api.job_manager import JobManager
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def temp_db():
|
||||
"""Create temporary database for testing."""
|
||||
fd, path = tempfile.mkstemp(suffix='.db')
|
||||
os.close(fd)
|
||||
|
||||
# Initialize schema
|
||||
from api.database import get_db_connection
|
||||
conn = get_db_connection(path)
|
||||
cursor = conn.cursor()
|
||||
|
||||
# Create jobs table
|
||||
cursor.execute("""
|
||||
CREATE TABLE IF NOT EXISTS jobs (
|
||||
job_id TEXT PRIMARY KEY,
|
||||
config_path TEXT NOT NULL,
|
||||
status TEXT NOT NULL,
|
||||
date_range TEXT NOT NULL,
|
||||
models TEXT NOT NULL,
|
||||
created_at TEXT NOT NULL,
|
||||
started_at TEXT,
|
||||
updated_at TEXT,
|
||||
completed_at TEXT,
|
||||
total_duration_seconds REAL,
|
||||
error TEXT,
|
||||
warnings TEXT
|
||||
)
|
||||
""")
|
||||
|
||||
# Create job_details table
|
||||
cursor.execute("""
|
||||
CREATE TABLE IF NOT EXISTS job_details (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
job_id TEXT NOT NULL,
|
||||
date TEXT NOT NULL,
|
||||
model TEXT NOT NULL,
|
||||
status TEXT NOT NULL,
|
||||
started_at TEXT,
|
||||
completed_at TEXT,
|
||||
duration_seconds REAL,
|
||||
error TEXT,
|
||||
FOREIGN KEY (job_id) REFERENCES jobs(job_id) ON DELETE CASCADE,
|
||||
UNIQUE(job_id, date, model)
|
||||
)
|
||||
""")
|
||||
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
yield path
|
||||
|
||||
# Cleanup
|
||||
if os.path.exists(path):
|
||||
os.remove(path)
|
||||
|
||||
|
||||
def test_create_job_with_filter_skips_completed_simulations(temp_db):
|
||||
"""Test that job creation with model_day_filter skips already-completed pairs."""
|
||||
manager = JobManager(db_path=temp_db)
|
||||
|
||||
# Create first job and mark model-day as completed
|
||||
result_1 = manager.create_job(
|
||||
config_path="test_config.json",
|
||||
date_range=["2025-10-15", "2025-10-16"],
|
||||
models=["deepseek-chat-v3.1"],
|
||||
model_day_filter=[("deepseek-chat-v3.1", "2025-10-15")]
|
||||
)
|
||||
job_id_1 = result_1["job_id"]
|
||||
|
||||
# Mark as completed
|
||||
manager.update_job_detail_status(
|
||||
job_id_1,
|
||||
"2025-10-15",
|
||||
"deepseek-chat-v3.1",
|
||||
"completed"
|
||||
)
|
||||
|
||||
# Try to create second job with overlapping date
|
||||
model_day_filter = [
|
||||
("deepseek-chat-v3.1", "2025-10-15"), # Already completed
|
||||
("deepseek-chat-v3.1", "2025-10-16") # Not yet completed
|
||||
]
|
||||
|
||||
result_2 = manager.create_job(
|
||||
config_path="test_config.json",
|
||||
date_range=["2025-10-15", "2025-10-16"],
|
||||
models=["deepseek-chat-v3.1"],
|
||||
model_day_filter=model_day_filter
|
||||
)
|
||||
job_id_2 = result_2["job_id"]
|
||||
|
||||
# Get job details for second job
|
||||
details = manager.get_job_details(job_id_2)
|
||||
|
||||
# Should only have 2025-10-16 (2025-10-15 was skipped as already completed)
|
||||
assert len(details) == 1
|
||||
assert details[0]["date"] == "2025-10-16"
|
||||
assert details[0]["model"] == "deepseek-chat-v3.1"
|
||||
|
||||
|
||||
def test_create_job_without_filter_skips_all_completed_simulations(temp_db):
|
||||
"""Test that job creation without filter skips all completed model-day pairs."""
|
||||
manager = JobManager(db_path=temp_db)
|
||||
|
||||
# Create first job and complete some model-days
|
||||
result_1 = manager.create_job(
|
||||
config_path="test_config.json",
|
||||
date_range=["2025-10-15"],
|
||||
models=["model-a", "model-b"]
|
||||
)
|
||||
job_id_1 = result_1["job_id"]
|
||||
|
||||
# Mark model-a/2025-10-15 as completed
|
||||
manager.update_job_detail_status(job_id_1, "2025-10-15", "model-a", "completed")
|
||||
# Mark model-b/2025-10-15 as failed to complete the job
|
||||
manager.update_job_detail_status(job_id_1, "2025-10-15", "model-b", "failed")
|
||||
|
||||
# Create second job with same date range and models
|
||||
result_2 = manager.create_job(
|
||||
config_path="test_config.json",
|
||||
date_range=["2025-10-15", "2025-10-16"],
|
||||
models=["model-a", "model-b"]
|
||||
)
|
||||
job_id_2 = result_2["job_id"]
|
||||
|
||||
# Get job details for second job
|
||||
details = manager.get_job_details(job_id_2)
|
||||
|
||||
# Should have 3 entries (skip only completed model-a/2025-10-15):
|
||||
# - model-b/2025-10-15 (failed in job 1, so not skipped - retry)
|
||||
# - model-a/2025-10-16 (new date)
|
||||
# - model-b/2025-10-16 (new date)
|
||||
assert len(details) == 3
|
||||
|
||||
dates_models = [(d["date"], d["model"]) for d in details]
|
||||
assert ("2025-10-15", "model-a") not in dates_models # Skipped (completed)
|
||||
assert ("2025-10-15", "model-b") in dates_models # NOT skipped (failed, not completed)
|
||||
assert ("2025-10-16", "model-a") in dates_models
|
||||
assert ("2025-10-16", "model-b") in dates_models
|
||||
|
||||
|
||||
def test_create_job_returns_warnings_for_skipped_simulations(temp_db):
|
||||
"""Test that skipped simulations are returned as warnings."""
|
||||
manager = JobManager(db_path=temp_db)
|
||||
|
||||
# Create and complete first simulation
|
||||
result_1 = manager.create_job(
|
||||
config_path="test_config.json",
|
||||
date_range=["2025-10-15"],
|
||||
models=["model-a"]
|
||||
)
|
||||
job_id_1 = result_1["job_id"]
|
||||
manager.update_job_detail_status(job_id_1, "2025-10-15", "model-a", "completed")
|
||||
|
||||
# Try to create job with overlapping date (one completed, one new)
|
||||
result = manager.create_job(
|
||||
config_path="test_config.json",
|
||||
date_range=["2025-10-15", "2025-10-16"], # Add new date
|
||||
models=["model-a"]
|
||||
)
|
||||
|
||||
# Result should be a dict with job_id and warnings
|
||||
assert isinstance(result, dict)
|
||||
assert "job_id" in result
|
||||
assert "warnings" in result
|
||||
assert len(result["warnings"]) == 1
|
||||
assert "model-a" in result["warnings"][0]
|
||||
assert "2025-10-15" in result["warnings"][0]
|
||||
|
||||
# Verify job_details only has the new date
|
||||
details = manager.get_job_details(result["job_id"])
|
||||
assert len(details) == 1
|
||||
assert details[0]["date"] == "2025-10-16"
|
||||
|
||||
|
||||
def test_create_job_raises_error_when_all_simulations_completed(temp_db):
|
||||
"""Test that ValueError is raised when ALL requested simulations are already completed."""
|
||||
manager = JobManager(db_path=temp_db)
|
||||
|
||||
# Create and complete first simulation
|
||||
result_1 = manager.create_job(
|
||||
config_path="test_config.json",
|
||||
date_range=["2025-10-15", "2025-10-16"],
|
||||
models=["model-a", "model-b"]
|
||||
)
|
||||
job_id_1 = result_1["job_id"]
|
||||
|
||||
# Mark all model-days as completed
|
||||
manager.update_job_detail_status(job_id_1, "2025-10-15", "model-a", "completed")
|
||||
manager.update_job_detail_status(job_id_1, "2025-10-15", "model-b", "completed")
|
||||
manager.update_job_detail_status(job_id_1, "2025-10-16", "model-a", "completed")
|
||||
manager.update_job_detail_status(job_id_1, "2025-10-16", "model-b", "completed")
|
||||
|
||||
# Try to create job with same date range and models (all already completed)
|
||||
with pytest.raises(ValueError) as exc_info:
|
||||
manager.create_job(
|
||||
config_path="test_config.json",
|
||||
date_range=["2025-10-15", "2025-10-16"],
|
||||
models=["model-a", "model-b"]
|
||||
)
|
||||
|
||||
# Verify error message contains expected text
|
||||
error_message = str(exc_info.value)
|
||||
assert "All requested simulations are already completed" in error_message
|
||||
assert "Skipped 4 model-day pair(s)" in error_message
|
||||
|
||||
|
||||
def test_create_job_with_skip_completed_false_includes_all_simulations(temp_db):
|
||||
"""Test that skip_completed=False includes ALL simulations, even already-completed ones."""
|
||||
manager = JobManager(db_path=temp_db)
|
||||
|
||||
# Create first job and complete some model-days
|
||||
result_1 = manager.create_job(
|
||||
config_path="test_config.json",
|
||||
date_range=["2025-10-15", "2025-10-16"],
|
||||
models=["model-a", "model-b"]
|
||||
)
|
||||
job_id_1 = result_1["job_id"]
|
||||
|
||||
# Mark all model-days as completed
|
||||
manager.update_job_detail_status(job_id_1, "2025-10-15", "model-a", "completed")
|
||||
manager.update_job_detail_status(job_id_1, "2025-10-15", "model-b", "completed")
|
||||
manager.update_job_detail_status(job_id_1, "2025-10-16", "model-a", "completed")
|
||||
manager.update_job_detail_status(job_id_1, "2025-10-16", "model-b", "completed")
|
||||
|
||||
# Create second job with skip_completed=False
|
||||
result_2 = manager.create_job(
|
||||
config_path="test_config.json",
|
||||
date_range=["2025-10-15", "2025-10-16"],
|
||||
models=["model-a", "model-b"],
|
||||
skip_completed=False
|
||||
)
|
||||
job_id_2 = result_2["job_id"]
|
||||
|
||||
# Get job details for second job
|
||||
details = manager.get_job_details(job_id_2)
|
||||
|
||||
# Should have ALL 4 model-day pairs (no skipping)
|
||||
assert len(details) == 4
|
||||
|
||||
dates_models = [(d["date"], d["model"]) for d in details]
|
||||
assert ("2025-10-15", "model-a") in dates_models
|
||||
assert ("2025-10-15", "model-b") in dates_models
|
||||
assert ("2025-10-16", "model-a") in dates_models
|
||||
assert ("2025-10-16", "model-b") in dates_models
|
||||
|
||||
# Verify no warnings were returned
|
||||
assert result_2.get("warnings") == []
|
||||
@@ -41,11 +41,12 @@ class TestSkipStatusDatabase:
|
||||
def test_skipped_status_allowed_in_job_details(self, job_manager):
|
||||
"""Test job_details accepts 'skipped' status without constraint violation."""
|
||||
# Create job
|
||||
job_id = job_manager.create_job(
|
||||
job_result = job_manager.create_job(
|
||||
config_path="test_config.json",
|
||||
date_range=["2025-10-01", "2025-10-02"],
|
||||
models=["test-model"]
|
||||
)
|
||||
job_id = job_result["job_id"]
|
||||
|
||||
# Mark a detail as skipped - should not raise constraint violation
|
||||
job_manager.update_job_detail_status(
|
||||
@@ -70,11 +71,12 @@ class TestJobCompletionWithSkipped:
|
||||
def test_job_completes_with_all_dates_skipped(self, job_manager):
|
||||
"""Test job transitions to completed when all dates are skipped."""
|
||||
# Create job with 3 dates
|
||||
job_id = job_manager.create_job(
|
||||
job_result = job_manager.create_job(
|
||||
config_path="test_config.json",
|
||||
date_range=["2025-10-01", "2025-10-02", "2025-10-03"],
|
||||
models=["test-model"]
|
||||
)
|
||||
job_id = job_result["job_id"]
|
||||
|
||||
# Mark all as skipped
|
||||
for date in ["2025-10-01", "2025-10-02", "2025-10-03"]:
|
||||
@@ -93,11 +95,12 @@ class TestJobCompletionWithSkipped:
|
||||
|
||||
def test_job_completes_with_mixed_completed_and_skipped(self, job_manager):
|
||||
"""Test job completes when some dates completed, some skipped."""
|
||||
job_id = job_manager.create_job(
|
||||
job_result = job_manager.create_job(
|
||||
config_path="test_config.json",
|
||||
date_range=["2025-10-01", "2025-10-02", "2025-10-03"],
|
||||
models=["test-model"]
|
||||
)
|
||||
job_id = job_result["job_id"]
|
||||
|
||||
# Mark some completed, some skipped
|
||||
job_manager.update_job_detail_status(
|
||||
@@ -119,11 +122,12 @@ class TestJobCompletionWithSkipped:
|
||||
|
||||
def test_job_partial_with_mixed_completed_failed_skipped(self, job_manager):
|
||||
"""Test job status 'partial' when some failed, some completed, some skipped."""
|
||||
job_id = job_manager.create_job(
|
||||
job_result = job_manager.create_job(
|
||||
config_path="test_config.json",
|
||||
date_range=["2025-10-01", "2025-10-02", "2025-10-03"],
|
||||
models=["test-model"]
|
||||
)
|
||||
job_id = job_result["job_id"]
|
||||
|
||||
# Mix of statuses
|
||||
job_manager.update_job_detail_status(
|
||||
@@ -145,11 +149,12 @@ class TestJobCompletionWithSkipped:
|
||||
|
||||
def test_job_remains_running_with_pending_dates(self, job_manager):
|
||||
"""Test job stays running when some dates are still pending."""
|
||||
job_id = job_manager.create_job(
|
||||
job_result = job_manager.create_job(
|
||||
config_path="test_config.json",
|
||||
date_range=["2025-10-01", "2025-10-02", "2025-10-03"],
|
||||
models=["test-model"]
|
||||
)
|
||||
job_id = job_result["job_id"]
|
||||
|
||||
# Only mark some as terminal states
|
||||
job_manager.update_job_detail_status(
|
||||
@@ -173,11 +178,12 @@ class TestProgressTrackingWithSkipped:
|
||||
|
||||
def test_progress_includes_skipped_count(self, job_manager):
|
||||
"""Test get_job_progress returns skipped count."""
|
||||
job_id = job_manager.create_job(
|
||||
job_result = job_manager.create_job(
|
||||
config_path="test_config.json",
|
||||
date_range=["2025-10-01", "2025-10-02", "2025-10-03", "2025-10-04"],
|
||||
models=["test-model"]
|
||||
)
|
||||
job_id = job_result["job_id"]
|
||||
|
||||
# Set various statuses
|
||||
job_manager.update_job_detail_status(
|
||||
@@ -205,11 +211,12 @@ class TestProgressTrackingWithSkipped:
|
||||
|
||||
def test_progress_all_skipped(self, job_manager):
|
||||
"""Test progress when all dates are skipped."""
|
||||
job_id = job_manager.create_job(
|
||||
job_result = job_manager.create_job(
|
||||
config_path="test_config.json",
|
||||
date_range=["2025-10-01", "2025-10-02"],
|
||||
models=["test-model"]
|
||||
)
|
||||
job_id = job_result["job_id"]
|
||||
|
||||
# Mark all as skipped
|
||||
for date in ["2025-10-01", "2025-10-02"]:
|
||||
@@ -231,11 +238,12 @@ class TestMultiModelSkipHandling:
|
||||
|
||||
def test_different_models_different_skip_states(self, job_manager):
|
||||
"""Test that different models can have different skip states for same date."""
|
||||
job_id = job_manager.create_job(
|
||||
job_result = job_manager.create_job(
|
||||
config_path="test_config.json",
|
||||
date_range=["2025-10-01", "2025-10-02"],
|
||||
models=["model-a", "model-b"]
|
||||
)
|
||||
job_id = job_result["job_id"]
|
||||
|
||||
# Model A: 10/1 skipped (already completed), 10/2 completed
|
||||
job_manager.update_job_detail_status(
|
||||
@@ -276,11 +284,12 @@ class TestMultiModelSkipHandling:
|
||||
|
||||
def test_job_completes_with_per_model_skips(self, job_manager):
|
||||
"""Test job completes when different models have different skip patterns."""
|
||||
job_id = job_manager.create_job(
|
||||
job_result = job_manager.create_job(
|
||||
config_path="test_config.json",
|
||||
date_range=["2025-10-01", "2025-10-02"],
|
||||
models=["model-a", "model-b"]
|
||||
)
|
||||
job_id = job_result["job_id"]
|
||||
|
||||
# Model A: one skipped, one completed
|
||||
job_manager.update_job_detail_status(
|
||||
@@ -318,11 +327,12 @@ class TestSkipReasons:
|
||||
|
||||
def test_skip_reason_already_completed(self, job_manager):
|
||||
"""Test 'Already completed' skip reason is stored."""
|
||||
job_id = job_manager.create_job(
|
||||
job_result = job_manager.create_job(
|
||||
config_path="test_config.json",
|
||||
date_range=["2025-10-01"],
|
||||
models=["test-model"]
|
||||
)
|
||||
job_id = job_result["job_id"]
|
||||
|
||||
job_manager.update_job_detail_status(
|
||||
job_id=job_id, date="2025-10-01", model="test-model",
|
||||
@@ -334,11 +344,12 @@ class TestSkipReasons:
|
||||
|
||||
def test_skip_reason_incomplete_price_data(self, job_manager):
|
||||
"""Test 'Incomplete price data' skip reason is stored."""
|
||||
job_id = job_manager.create_job(
|
||||
job_result = job_manager.create_job(
|
||||
config_path="test_config.json",
|
||||
date_range=["2025-10-04"],
|
||||
models=["test-model"]
|
||||
)
|
||||
job_id = job_result["job_id"]
|
||||
|
||||
job_manager.update_job_detail_status(
|
||||
job_id=job_id, date="2025-10-04", model="test-model",
|
||||
|
||||
@@ -112,11 +112,12 @@ class TestModelDayExecutorExecution:
|
||||
|
||||
# Create job and job_detail
|
||||
manager = JobManager(db_path=clean_db)
|
||||
job_id = manager.create_job(
|
||||
job_result = manager.create_job(
|
||||
config_path=str(config_path),
|
||||
date_range=["2025-01-16"],
|
||||
models=["gpt-5"]
|
||||
)
|
||||
job_id = job_result["job_id"]
|
||||
|
||||
# Mock agent execution
|
||||
mock_agent = create_mock_agent(
|
||||
@@ -156,11 +157,12 @@ class TestModelDayExecutorExecution:
|
||||
|
||||
# Create job
|
||||
manager = JobManager(db_path=clean_db)
|
||||
job_id = manager.create_job(
|
||||
job_result = manager.create_job(
|
||||
config_path="configs/test.json",
|
||||
date_range=["2025-01-16"],
|
||||
models=["gpt-5"]
|
||||
)
|
||||
job_id = job_result["job_id"]
|
||||
|
||||
# Mock agent to raise error
|
||||
with patch("api.model_day_executor.RuntimeConfigManager") as mock_runtime:
|
||||
@@ -212,11 +214,12 @@ class TestModelDayExecutorDataPersistence:
|
||||
|
||||
# Create job
|
||||
manager = JobManager(db_path=clean_db)
|
||||
job_id = manager.create_job(
|
||||
job_result = manager.create_job(
|
||||
config_path=str(config_path),
|
||||
date_range=["2025-01-16"],
|
||||
models=["gpt-5"]
|
||||
)
|
||||
job_id = job_result["job_id"]
|
||||
|
||||
# Mock successful execution (no trades)
|
||||
mock_agent = create_mock_agent(
|
||||
@@ -269,11 +272,12 @@ class TestModelDayExecutorDataPersistence:
|
||||
|
||||
# Create job
|
||||
manager = JobManager(db_path=clean_db)
|
||||
job_id = manager.create_job(
|
||||
job_result = manager.create_job(
|
||||
config_path="configs/test.json",
|
||||
date_range=["2025-01-16"],
|
||||
models=["gpt-5"]
|
||||
)
|
||||
job_id = job_result["job_id"]
|
||||
|
||||
# Mock execution with reasoning
|
||||
mock_agent = create_mock_agent(
|
||||
@@ -320,11 +324,12 @@ class TestModelDayExecutorCleanup:
|
||||
from api.job_manager import JobManager
|
||||
|
||||
manager = JobManager(db_path=clean_db)
|
||||
job_id = manager.create_job(
|
||||
job_result = manager.create_job(
|
||||
config_path="configs/test.json",
|
||||
date_range=["2025-01-16"],
|
||||
models=["gpt-5"]
|
||||
)
|
||||
job_id = job_result["job_id"]
|
||||
|
||||
mock_agent = create_mock_agent(
|
||||
session_result={"success": True}
|
||||
@@ -355,11 +360,12 @@ class TestModelDayExecutorCleanup:
|
||||
from api.job_manager import JobManager
|
||||
|
||||
manager = JobManager(db_path=clean_db)
|
||||
job_id = manager.create_job(
|
||||
job_result = manager.create_job(
|
||||
config_path="configs/test.json",
|
||||
date_range=["2025-01-16"],
|
||||
models=["gpt-5"]
|
||||
)
|
||||
job_id = job_result["job_id"]
|
||||
|
||||
with patch("api.model_day_executor.RuntimeConfigManager") as mock_runtime:
|
||||
mock_instance = Mock()
|
||||
|
||||
@@ -63,7 +63,7 @@ class TestRuntimeConfigCreation:
|
||||
|
||||
assert config["TODAY_DATE"] == "2025-01-16"
|
||||
assert config["SIGNATURE"] == "gpt-5"
|
||||
assert config["IF_TRADE"] is False
|
||||
assert config["IF_TRADE"] is True
|
||||
assert config["JOB_ID"] == "test-job-123"
|
||||
|
||||
def test_create_runtime_config_unique_paths(self):
|
||||
@@ -108,6 +108,32 @@ class TestRuntimeConfigCreation:
|
||||
# Config file should exist
|
||||
assert os.path.exists(config_path)
|
||||
|
||||
def test_create_runtime_config_if_trade_defaults_true(self):
|
||||
"""Test that IF_TRADE initializes to True (trades expected by default)"""
|
||||
from api.runtime_manager import RuntimeConfigManager
|
||||
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
manager = RuntimeConfigManager(data_dir=temp_dir)
|
||||
|
||||
config_path = manager.create_runtime_config(
|
||||
job_id="test-job-123",
|
||||
model_sig="test-model",
|
||||
date="2025-01-16",
|
||||
trading_day_id=1
|
||||
)
|
||||
|
||||
try:
|
||||
# Read the config file
|
||||
with open(config_path, 'r') as f:
|
||||
config = json.load(f)
|
||||
|
||||
# Verify IF_TRADE is True by default
|
||||
assert config["IF_TRADE"] is True, "IF_TRADE should initialize to True"
|
||||
finally:
|
||||
# Cleanup
|
||||
if os.path.exists(config_path):
|
||||
os.remove(config_path)
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestRuntimeConfigCleanup:
|
||||
|
||||
@@ -41,11 +41,12 @@ class TestSimulationWorkerExecution:
|
||||
|
||||
# Create job with 2 dates and 2 models = 4 model-days
|
||||
manager = JobManager(db_path=clean_db)
|
||||
job_id = manager.create_job(
|
||||
job_result = manager.create_job(
|
||||
config_path="configs/test.json",
|
||||
date_range=["2025-01-16", "2025-01-17"],
|
||||
models=["gpt-5", "claude-3.7-sonnet"]
|
||||
)
|
||||
job_id = job_result["job_id"]
|
||||
|
||||
worker = SimulationWorker(job_id=job_id, db_path=clean_db)
|
||||
|
||||
@@ -73,11 +74,12 @@ class TestSimulationWorkerExecution:
|
||||
from api.job_manager import JobManager
|
||||
|
||||
manager = JobManager(db_path=clean_db)
|
||||
job_id = manager.create_job(
|
||||
job_result = manager.create_job(
|
||||
config_path="configs/test.json",
|
||||
date_range=["2025-01-16", "2025-01-17"],
|
||||
models=["gpt-5", "claude-3.7-sonnet"]
|
||||
)
|
||||
job_id = job_result["job_id"]
|
||||
|
||||
worker = SimulationWorker(job_id=job_id, db_path=clean_db)
|
||||
|
||||
@@ -118,11 +120,12 @@ class TestSimulationWorkerExecution:
|
||||
from api.job_manager import JobManager
|
||||
|
||||
manager = JobManager(db_path=clean_db)
|
||||
job_id = manager.create_job(
|
||||
job_result = manager.create_job(
|
||||
config_path="configs/test.json",
|
||||
date_range=["2025-01-16"],
|
||||
models=["gpt-5"]
|
||||
)
|
||||
job_id = job_result["job_id"]
|
||||
|
||||
worker = SimulationWorker(job_id=job_id, db_path=clean_db)
|
||||
|
||||
@@ -159,11 +162,12 @@ class TestSimulationWorkerExecution:
|
||||
from api.job_manager import JobManager
|
||||
|
||||
manager = JobManager(db_path=clean_db)
|
||||
job_id = manager.create_job(
|
||||
job_result = manager.create_job(
|
||||
config_path="configs/test.json",
|
||||
date_range=["2025-01-16"],
|
||||
models=["gpt-5", "claude-3.7-sonnet"]
|
||||
)
|
||||
job_id = job_result["job_id"]
|
||||
|
||||
worker = SimulationWorker(job_id=job_id, db_path=clean_db)
|
||||
|
||||
@@ -214,11 +218,12 @@ class TestSimulationWorkerErrorHandling:
|
||||
from api.job_manager import JobManager
|
||||
|
||||
manager = JobManager(db_path=clean_db)
|
||||
job_id = manager.create_job(
|
||||
job_result = manager.create_job(
|
||||
config_path="configs/test.json",
|
||||
date_range=["2025-01-16"],
|
||||
models=["gpt-5", "claude-3.7-sonnet", "gemini"]
|
||||
)
|
||||
job_id = job_result["job_id"]
|
||||
|
||||
worker = SimulationWorker(job_id=job_id, db_path=clean_db)
|
||||
|
||||
@@ -259,11 +264,12 @@ class TestSimulationWorkerErrorHandling:
|
||||
from api.job_manager import JobManager
|
||||
|
||||
manager = JobManager(db_path=clean_db)
|
||||
job_id = manager.create_job(
|
||||
job_result = manager.create_job(
|
||||
config_path="configs/test.json",
|
||||
date_range=["2025-01-16"],
|
||||
models=["gpt-5"]
|
||||
)
|
||||
job_id = job_result["job_id"]
|
||||
|
||||
worker = SimulationWorker(job_id=job_id, db_path=clean_db)
|
||||
|
||||
@@ -289,11 +295,12 @@ class TestSimulationWorkerConcurrency:
|
||||
from api.job_manager import JobManager
|
||||
|
||||
manager = JobManager(db_path=clean_db)
|
||||
job_id = manager.create_job(
|
||||
job_result = manager.create_job(
|
||||
config_path="configs/test.json",
|
||||
date_range=["2025-01-16"],
|
||||
models=["gpt-5", "claude-3.7-sonnet"]
|
||||
)
|
||||
job_id = job_result["job_id"]
|
||||
|
||||
worker = SimulationWorker(job_id=job_id, db_path=clean_db)
|
||||
|
||||
@@ -335,11 +342,12 @@ class TestSimulationWorkerJobRetrieval:
|
||||
from api.job_manager import JobManager
|
||||
|
||||
manager = JobManager(db_path=clean_db)
|
||||
job_id = manager.create_job(
|
||||
job_result = manager.create_job(
|
||||
config_path="configs/test.json",
|
||||
date_range=["2025-01-16", "2025-01-17"],
|
||||
models=["gpt-5"]
|
||||
)
|
||||
job_id = job_result["job_id"]
|
||||
|
||||
worker = SimulationWorker(job_id=job_id, db_path=clean_db)
|
||||
job_info = worker.get_job_info()
|
||||
@@ -469,11 +477,12 @@ class TestSimulationWorkerHelperMethods:
|
||||
job_manager = JobManager(db_path=db_path)
|
||||
|
||||
# Create job
|
||||
job_id = job_manager.create_job(
|
||||
job_result = job_manager.create_job(
|
||||
config_path="config.json",
|
||||
date_range=["2025-10-01"],
|
||||
models=["gpt-5"]
|
||||
)
|
||||
job_id = job_result["job_id"]
|
||||
|
||||
worker = SimulationWorker(job_id=job_id, db_path=db_path)
|
||||
|
||||
@@ -498,11 +507,12 @@ class TestSimulationWorkerHelperMethods:
|
||||
job_manager = JobManager(db_path=db_path)
|
||||
|
||||
# Create job
|
||||
job_id = job_manager.create_job(
|
||||
job_result = job_manager.create_job(
|
||||
config_path="config.json",
|
||||
date_range=["2025-10-01"],
|
||||
models=["gpt-5"]
|
||||
)
|
||||
job_id = job_result["job_id"]
|
||||
|
||||
worker = SimulationWorker(job_id=job_id, db_path=db_path)
|
||||
|
||||
@@ -545,11 +555,12 @@ class TestSimulationWorkerHelperMethods:
|
||||
initialize_database(db_path)
|
||||
job_manager = JobManager(db_path=db_path)
|
||||
|
||||
job_id = job_manager.create_job(
|
||||
job_result = job_manager.create_job(
|
||||
config_path="config.json",
|
||||
date_range=["2025-10-01"],
|
||||
models=["gpt-5"]
|
||||
)
|
||||
job_id = job_result["job_id"]
|
||||
|
||||
worker = SimulationWorker(job_id=job_id, db_path=db_path)
|
||||
|
||||
|
||||
@@ -295,3 +295,190 @@ def test_sell_writes_to_actions_table(test_db, monkeypatch):
|
||||
assert row[1] == 'AAPL'
|
||||
assert row[2] == 5
|
||||
assert row[3] == 160.0
|
||||
|
||||
|
||||
def test_intraday_position_tracking_sell_then_buy(test_db, monkeypatch):
|
||||
"""Test that sell proceeds are immediately available for subsequent buys."""
|
||||
db, trading_day_id = test_db
|
||||
|
||||
# Setup: Create starting position with AAPL shares and limited cash
|
||||
db.create_holding(trading_day_id, 'AAPL', 10)
|
||||
db.connection.commit()
|
||||
|
||||
# Create a mock connection wrapper
|
||||
class MockConnection:
|
||||
def __init__(self, real_conn):
|
||||
self.real_conn = real_conn
|
||||
|
||||
def cursor(self):
|
||||
return self.real_conn.cursor()
|
||||
|
||||
def commit(self):
|
||||
return self.real_conn.commit()
|
||||
|
||||
def rollback(self):
|
||||
return self.real_conn.rollback()
|
||||
|
||||
def close(self):
|
||||
pass
|
||||
|
||||
mock_conn = MockConnection(db.connection)
|
||||
monkeypatch.setattr('agent_tools.tool_trade.get_db_connection',
|
||||
lambda x: mock_conn)
|
||||
|
||||
# Mock get_current_position_from_db to return starting position
|
||||
monkeypatch.setattr('agent_tools.tool_trade.get_current_position_from_db',
|
||||
lambda job_id, sig, date: ({'CASH': 500.0, 'AAPL': 10}, 0))
|
||||
|
||||
monkeypatch.setenv('RUNTIME_ENV_PATH', '/tmp/test_runtime_intraday.json')
|
||||
|
||||
import json
|
||||
with open('/tmp/test_runtime_intraday.json', 'w') as f:
|
||||
json.dump({
|
||||
'TODAY_DATE': '2025-01-15',
|
||||
'SIGNATURE': 'test-model',
|
||||
'JOB_ID': 'test-job-123',
|
||||
'TRADING_DAY_ID': trading_day_id
|
||||
}, f)
|
||||
|
||||
# Mock prices: AAPL sells for 200, MSFT costs 150
|
||||
def mock_get_prices(date, symbols):
|
||||
if 'AAPL' in symbols:
|
||||
return {'AAPL_price': 200.0}
|
||||
elif 'MSFT' in symbols:
|
||||
return {'MSFT_price': 150.0}
|
||||
return {}
|
||||
|
||||
monkeypatch.setattr('agent_tools.tool_trade.get_open_prices', mock_get_prices)
|
||||
|
||||
# Step 1: Sell 3 shares of AAPL for 600.0
|
||||
# Starting cash: 500.0, proceeds: 600.0, new cash: 1100.0
|
||||
result_sell = _sell_impl(
|
||||
symbol='AAPL',
|
||||
amount=3,
|
||||
signature='test-model',
|
||||
today_date='2025-01-15',
|
||||
job_id='test-job-123',
|
||||
trading_day_id=trading_day_id,
|
||||
_current_position=None # Use database position (starting position)
|
||||
)
|
||||
|
||||
assert 'error' not in result_sell, f"Sell should succeed: {result_sell}"
|
||||
assert result_sell['CASH'] == 1100.0, "Cash should be 500 + (3 * 200) = 1100"
|
||||
assert result_sell['AAPL'] == 7, "AAPL shares should be 10 - 3 = 7"
|
||||
|
||||
# Step 2: Buy 7 shares of MSFT for 1050.0 using the position from the sell
|
||||
# This should work because we pass the updated position from step 1
|
||||
result_buy = _buy_impl(
|
||||
symbol='MSFT',
|
||||
amount=7,
|
||||
signature='test-model',
|
||||
today_date='2025-01-15',
|
||||
job_id='test-job-123',
|
||||
trading_day_id=trading_day_id,
|
||||
_current_position=result_sell # Use position from sell
|
||||
)
|
||||
|
||||
assert 'error' not in result_buy, f"Buy should succeed with sell proceeds: {result_buy}"
|
||||
assert result_buy['CASH'] == 50.0, "Cash should be 1100 - (7 * 150) = 50"
|
||||
assert result_buy['MSFT'] == 7, "MSFT shares should be 7"
|
||||
assert result_buy['AAPL'] == 7, "AAPL shares should still be 7"
|
||||
|
||||
# Verify both actions were recorded
|
||||
cursor = db.connection.execute("""
|
||||
SELECT action_type, symbol, quantity, price
|
||||
FROM actions
|
||||
WHERE trading_day_id = ?
|
||||
ORDER BY created_at
|
||||
""", (trading_day_id,))
|
||||
|
||||
actions = cursor.fetchall()
|
||||
assert len(actions) == 2, "Should have 2 actions (sell + buy)"
|
||||
assert actions[0][0] == 'sell' and actions[0][1] == 'AAPL'
|
||||
assert actions[1][0] == 'buy' and actions[1][1] == 'MSFT'
|
||||
|
||||
|
||||
def test_intraday_tracking_without_position_injection_fails(test_db, monkeypatch):
|
||||
"""Test that without position injection, sell proceeds are NOT available for subsequent buys."""
|
||||
db, trading_day_id = test_db
|
||||
|
||||
# Setup: Create starting position with AAPL shares and limited cash
|
||||
db.create_holding(trading_day_id, 'AAPL', 10)
|
||||
db.connection.commit()
|
||||
|
||||
# Create a mock connection wrapper
|
||||
class MockConnection:
|
||||
def __init__(self, real_conn):
|
||||
self.real_conn = real_conn
|
||||
|
||||
def cursor(self):
|
||||
return self.real_conn.cursor()
|
||||
|
||||
def commit(self):
|
||||
return self.real_conn.commit()
|
||||
|
||||
def rollback(self):
|
||||
return self.real_conn.rollback()
|
||||
|
||||
def close(self):
|
||||
pass
|
||||
|
||||
mock_conn = MockConnection(db.connection)
|
||||
monkeypatch.setattr('agent_tools.tool_trade.get_db_connection',
|
||||
lambda x: mock_conn)
|
||||
|
||||
# Mock get_current_position_from_db to ALWAYS return starting position
|
||||
# (simulating the old buggy behavior)
|
||||
monkeypatch.setattr('agent_tools.tool_trade.get_current_position_from_db',
|
||||
lambda job_id, sig, date: ({'CASH': 500.0, 'AAPL': 10}, 0))
|
||||
|
||||
monkeypatch.setenv('RUNTIME_ENV_PATH', '/tmp/test_runtime_no_injection.json')
|
||||
|
||||
import json
|
||||
with open('/tmp/test_runtime_no_injection.json', 'w') as f:
|
||||
json.dump({
|
||||
'TODAY_DATE': '2025-01-15',
|
||||
'SIGNATURE': 'test-model',
|
||||
'JOB_ID': 'test-job-123',
|
||||
'TRADING_DAY_ID': trading_day_id
|
||||
}, f)
|
||||
|
||||
# Mock prices
|
||||
def mock_get_prices(date, symbols):
|
||||
if 'AAPL' in symbols:
|
||||
return {'AAPL_price': 200.0}
|
||||
elif 'MSFT' in symbols:
|
||||
return {'MSFT_price': 150.0}
|
||||
return {}
|
||||
|
||||
monkeypatch.setattr('agent_tools.tool_trade.get_open_prices', mock_get_prices)
|
||||
|
||||
# Step 1: Sell 3 shares of AAPL
|
||||
result_sell = _sell_impl(
|
||||
symbol='AAPL',
|
||||
amount=3,
|
||||
signature='test-model',
|
||||
today_date='2025-01-15',
|
||||
job_id='test-job-123',
|
||||
trading_day_id=trading_day_id,
|
||||
_current_position=None # Don't inject position (old behavior)
|
||||
)
|
||||
|
||||
assert 'error' not in result_sell, "Sell should succeed"
|
||||
|
||||
# Step 2: Try to buy 7 shares of MSFT WITHOUT passing updated position
|
||||
# This should FAIL because it will query the database and get the original 500.0 cash
|
||||
result_buy = _buy_impl(
|
||||
symbol='MSFT',
|
||||
amount=7,
|
||||
signature='test-model',
|
||||
today_date='2025-01-15',
|
||||
job_id='test-job-123',
|
||||
trading_day_id=trading_day_id,
|
||||
_current_position=None # Don't inject position (old behavior)
|
||||
)
|
||||
|
||||
# This should fail with insufficient cash
|
||||
assert 'error' in result_buy, "Buy should fail without position injection"
|
||||
assert result_buy['error'] == 'Insufficient cash', f"Expected insufficient cash error, got: {result_buy}"
|
||||
assert result_buy['cash_available'] == 500.0, "Should see original cash, not updated cash"
|
||||
|
||||
Reference in New Issue
Block a user