From 80f03f2a1350fc443d45612dd43aeb492c9b12ab Mon Sep 17 00:00:00 2001 From: 69049ed6x <69049ed6x@users.noreply.github.com> Date: Sun, 8 Mar 2026 13:24:41 +0800 Subject: [PATCH] fix: preserve missing explicit factor rules path --- tests/test_factor_rules.py | 20 ++++++++++++++++++++ tradingagents/agents/utils/factor_rules.py | 11 +++++++++++ 2 files changed, 31 insertions(+) diff --git a/tests/test_factor_rules.py b/tests/test_factor_rules.py index a778ccc2..3f6e0d7f 100644 --- a/tests/test_factor_rules.py +++ b/tests/test_factor_rules.py @@ -100,6 +100,26 @@ class FactorRulesPathTests(unittest.TestCase): self.assertEqual(rules, []) self.assertIsNone(loaded_path) + def test_missing_explicit_rule_path_does_not_fall_back_silently(self): + with tempfile.TemporaryDirectory() as tmpdir: + project_dir = Path(tmpdir) + examples_dir = project_dir / "examples" + examples_dir.mkdir() + default_rule_path = examples_dir / "factor_rules.json" + default_rule_path.write_text(json.dumps({"rules": [{"name": "Default"}]}), encoding="utf-8") + missing_explicit_path = project_dir / "factor_rules.json" + missing_explicit_path.unlink(missing_ok=True) + + rules, loaded_path = load_factor_rules( + { + "project_dir": str(project_dir), + "factor_rules_path": str(missing_explicit_path), + } + ) + + self.assertEqual(rules, []) + self.assertEqual(Path(loaded_path), missing_explicit_path.resolve()) + def test_invalid_rules_object_raises_value_error(self): with tempfile.TemporaryDirectory() as tmpdir: project_dir = Path(tmpdir) diff --git a/tradingagents/agents/utils/factor_rules.py b/tradingagents/agents/utils/factor_rules.py index d1c1ff3b..c970382c 100644 --- a/tradingagents/agents/utils/factor_rules.py +++ b/tradingagents/agents/utils/factor_rules.py @@ -51,7 +51,18 @@ def _candidate_rule_paths(config: Optional[Dict[str, Any]] = None) -> List[Path] def load_factor_rules(config: Optional[Dict[str, Any]] = None) -> Tuple[List[Dict[str, Any]], Optional[str]]: + config = config or {} + explicit = config.get("factor_rules_path") + explicit_path = None + if explicit: + try: + explicit_path = Path(explicit).resolve() + except Exception: + explicit_path = None + for path in _candidate_rule_paths(config): + if explicit_path and path == explicit_path and not path.exists(): + return [], str(path) if not path.exists(): continue with open(path, "r", encoding="utf-8") as f: