From 3abff48c7de6c75c64d0efb36e483a36a79621c1 Mon Sep 17 00:00:00 2001 From: Robin Lindbladh Date: Tue, 24 Mar 2026 21:10:33 +0100 Subject: [PATCH] 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) --- tradingagents/graph/portfolio_analysis.py | 6 +++++- tradingagents/graph/trading_graph.py | 2 ++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/tradingagents/graph/portfolio_analysis.py b/tradingagents/graph/portfolio_analysis.py index 2af2a2bf..6cf3f709 100644 --- a/tradingagents/graph/portfolio_analysis.py +++ b/tradingagents/graph/portfolio_analysis.py @@ -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, diff --git a/tradingagents/graph/trading_graph.py b/tradingagents/graph/trading_graph.py index 7db0624e..6362dc49 100644 --- a/tradingagents/graph/trading_graph.py +++ b/tradingagents/graph/trading_graph.py @@ -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."""