From 277714f664dc80d49583df16c0610f963151372b Mon Sep 17 00:00:00 2001 From: Bill Date: Fri, 7 Nov 2025 14:16:30 -0500 Subject: [PATCH] debug: add comprehensive logging for position tracking bug investigation Add debug logging to diagnose negative cash position issue where trades calculate from initial $10,000 instead of accumulating. Issue: After 11 trades, final cash shows -$8,768.68. Each trade appears to calculate from $10,000 starting position instead of previous trade's ending position. Hypothesis: ContextInjector._current_position not updating after trades, possibly due to MCP result type mismatch in isinstance(result, dict) check. Debug logging added: - agent/context_injector.py: Log MCP result type, content, and whether _current_position updates after each trade - agent_tools/tool_trade.py: Log whether injected position is used vs DB query, and full contents of returned position dict This will help identify: 1. What type is returned by MCP tool (dict vs other) 2. Whether _current_position is None on subsequent trades 3. What keys are present in returned position dicts Related to issue where reasoning summary claims no trades executed despite 4 sell orders being recorded. --- agent/context_injector.py | 9 +++++++++ agent_tools/tool_trade.py | 5 +++++ 2 files changed, 14 insertions(+) diff --git a/agent/context_injector.py b/agent/context_injector.py index 7ca10a3..c01c021 100644 --- a/agent/context_injector.py +++ b/agent/context_injector.py @@ -88,9 +88,18 @@ class ContextInjector: # Update position state after successful trade if request.name in ["buy", "sell"]: + # Debug: Log result type and structure + print(f"[DEBUG ContextInjector] Trade result type: {type(result)}") + print(f"[DEBUG ContextInjector] Trade result: {result}") + print(f"[DEBUG ContextInjector] isinstance(result, dict): {isinstance(result, dict)}") + # Check if result is a valid position dict (not an error) if isinstance(result, dict) and "error" not in result and "CASH" in result: # Update our tracked position with the new state self._current_position = result.copy() + print(f"[DEBUG ContextInjector] Updated _current_position: {self._current_position}") + else: + print(f"[DEBUG ContextInjector] Did NOT update _current_position - check failed") + print(f"[DEBUG ContextInjector] _current_position remains: {self._current_position}") return result diff --git a/agent_tools/tool_trade.py b/agent_tools/tool_trade.py index cc79f2f..4b8c411 100644 --- a/agent_tools/tool_trade.py +++ b/agent_tools/tool_trade.py @@ -132,11 +132,14 @@ def _buy_impl(symbol: str, amount: int, signature: str = None, today_date: str = # Step 1: Get current position # Use injected position if available (for intra-day tracking), # otherwise query database for starting position + print(f"[DEBUG buy] _current_position received: {_current_position}") if _current_position is not None: current_position = _current_position next_action_id = 0 # Not used in new schema + print(f"[DEBUG buy] Using injected position: {current_position}") else: current_position, next_action_id = get_current_position_from_db(job_id, signature, today_date) + print(f"[DEBUG buy] Queried position from DB: {current_position}") # Step 2: Get stock price try: @@ -189,6 +192,8 @@ def _buy_impl(symbol: str, amount: int, signature: str = None, today_date: str = conn.commit() print(f"[buy] {signature} bought {amount} shares of {symbol} at ${this_symbol_price}") + print(f"[DEBUG buy] Returning new_position: {new_position}") + print(f"[DEBUG buy] new_position keys: {list(new_position.keys())}") return new_position except Exception as e: