mirror of
https://github.com/Xe138/AI-Trader.git
synced 2026-04-02 01:27:24 -04:00
Fix negative cash bug where ContextInjector._current_position never updated. Root cause: MCP tools return mcp.types.CallToolResult objects, not plain dicts. The isinstance(result, dict) check always failed, preventing _current_position from accumulating trades within a session. This caused all trades to calculate from initial $10,000 position instead of previous trade's ending position, resulting in negative cash balances when total purchases exceeded $10,000. Solution: Extract position dict from CallToolResult.structuredContent field before validating. Maintains backward compatibility by handling both CallToolResult objects (production) and plain dicts (unit tests). Impact: - Fixes negative cash positions (e.g., -$8,768.68 after 11 trades) - Enables proper intra-day position tracking - Validates sufficient cash before each trade based on cumulative position - Trade tool responses now properly accumulate all holdings Testing: - All existing unit tests pass (handle plain dict results) - Production logs confirm structuredContent extraction works - Debug logging shows _current_position now updates after each trade