diff --git a/tests/test_checkpoint_resume.py b/tests/test_checkpoint_resume.py index c85f3d3c5..ae21500f0 100644 --- a/tests/test_checkpoint_resume.py +++ b/tests/test_checkpoint_resume.py @@ -190,6 +190,34 @@ class TestCheckpointSignature(unittest.TestCase): # sig1's checkpoint remains untouched. self.assertIsNotNone(checkpoint_step(self.tmpdir, self.ticker, self.date, sig1)) + def test_a_resume_under_other_settings_starts_fresh(self): + """The report names one provider, model set, language and vendor chain; + a resume must not carry reports another of them produced.""" + from tradingagents.graph.trading_graph import TradingAgentsGraph + + g = object.__new__(TradingAgentsGraph) + g.selected_analysts = ("market", "news") + base_config = {"max_debate_rounds": 1, "max_risk_discuss_rounds": 1, "llm_provider": "openai", + "quick_think_llm": "q", "deep_think_llm": "d", "output_language": "English", + "data_vendors": {"core_stock_apis": "yfinance"}, "tool_vendors": {}} + g.config = dict(base_config) + base = g._run_signature("stock") + for key, value in (("llm_provider", "google"), ("quick_think_llm", "q2"), ("deep_think_llm", "d2"), + ("output_language", "Deutsch"), + ("data_vendors", {"core_stock_apis": "alpha_vantage"}), + ("tool_vendors", {"get_news": "alpha_vantage"}), + ("backend_url", "http://other-endpoint/v1"), ("max_tool_rounds", 30), + ("temperature", 0.2), ("openai_reasoning_effort", "high")): + g.config = {**base_config, key: value} + self.assertNotEqual(base, g._run_signature("stock"), key) + # Where a run keeps its files, and how it retries, do not change what it writes. + for key, value in (("results_dir", "/elsewhere"), ("data_cache_dir", "/cache"), + ("memory_log_path", "/log.md"), ("checkpoint_enabled", True), ("llm_max_retries", 9)): + g.config = {**base_config, key: value} + self.assertEqual(base, g._run_signature("stock"), key) + g.config = dict(base_config) + self.assertEqual(base, g._run_signature("stock")) + def test_run_signature_captures_graph_shape(self): from tradingagents.graph.trading_graph import TradingAgentsGraph diff --git a/tradingagents/graph/trading_graph.py b/tradingagents/graph/trading_graph.py index b59e8a1be..820398c18 100644 --- a/tradingagents/graph/trading_graph.py +++ b/tradingagents/graph/trading_graph.py @@ -1,3 +1,4 @@ +import hashlib import json import logging import os @@ -40,6 +41,13 @@ def _validate_trade_date(trade_date) -> str: return value +# Config keys that do not change what a run writes: where it keeps its files, +# whether it checkpoints, and how often it retries a provider. +_NOT_IN_SIGNATURE = frozenset({ + "results_dir", "data_cache_dir", "memory_log_path", "checkpoint_enabled", "llm_max_retries", +}) + + class TradingAgentsGraph: """Main class that orchestrates the trading agents framework.""" @@ -148,12 +156,17 @@ class TradingAgentsGraph: return td if td < datetime.now().strftime("%Y-%m-%d") else None def _run_signature(self, asset_type: str, portfolio=None) -> str: - """Graph-shape inputs that must invalidate a checkpoint if changed. + """Run inputs that must invalidate a checkpoint if changed. Keyed into the checkpoint thread ID so a resume under a different analyst selection, debate/risk depth, or asset mode starts fresh instead of - silently continuing the previous graph (#1089). + silently continuing the previous graph (#1089). The rest of the config + counts too (provider, models, endpoint, language, vendors, limits): a + resume must not carry reports that other settings produced. Only where + the run keeps its files and how it retries are left out. """ + settings = {k: v for k, v in self.config.items() if k not in _NOT_IN_SIGNATURE} + digest = hashlib.sha256(json.dumps(settings, sort_keys=True, default=str).encode()).hexdigest()[:12] return "|".join([ "analysts=" + ",".join(self.selected_analysts), f"debate={self.config['max_debate_rounds']}", @@ -164,6 +177,7 @@ class TradingAgentsGraph: # The layout itself: a checkpoint saved when analysts ran one after # another has pending nodes this graph no longer has. "analysts=parallel", + f"settings={digest}", ]) def propagate(self, company_name, trade_date, asset_type: str = "stock", portfolio=None):