fix: protect log file write and preserve log_states_dict

- Wrap _log_portfolio file I/O in try/except so a write failure
  doesn't discard the analysis results
- Preserve and restore self.log_states_dict in propagate_portfolio()
  alongside ticker and curr_state

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Robin Lindbladh 2026-03-24 21:10:33 +01:00
parent 5ac72567be
commit 3abff48c7d
2 changed files with 7 additions and 1 deletions

View File

@ -61,7 +61,11 @@ class PortfolioAnalyzer:
individual_results, trade_date
)
self._log_portfolio(trade_date, tickers, individual_results, portfolio_summary)
try:
self._log_portfolio(trade_date, tickers, individual_results, portfolio_summary)
except OSError as e:
if debug:
print(f"Warning: failed to save portfolio log: {e}")
return {
"individual_results": individual_results,

View File

@ -283,6 +283,7 @@ class TradingAgentsGraph:
"""
original_ticker = self.ticker
original_curr_state = self.curr_state
original_log_states = self.log_states_dict.copy()
try:
return self.portfolio_analyzer.analyze(
tickers, trade_date, self.propagate, debug=self.debug
@ -290,6 +291,7 @@ class TradingAgentsGraph:
finally:
self.ticker = original_ticker
self.curr_state = original_curr_state
self.log_states_dict = original_log_states
def reflect_and_remember(self, returns_losses):
"""Reflect on decisions and update memory based on returns."""