From aea37b79dc0b9cdbbcb568262fd92d70118ad7d3 Mon Sep 17 00:00:00 2001 From: Lance Date: Fri, 28 Aug 2026 11:59:22 +0800 Subject: [PATCH] =?UTF-8?q?fix(tradingagents):=20auto=5Ftrigger=20?= =?UTF-8?q?=E8=AF=AF=E8=AF=BB=E4=B8=8D=E5=AD=98=E5=9C=A8=E7=9A=84=20AgentC?= =?UTF-8?q?onfig.raw=5Fconfig,=E6=94=B9=E4=B8=BA=20config?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit AgentConfig 模型的 JSON 配置列名为 config(src/web/models.py), auto_trigger 中的 agent.raw_config 在运行时抛 AttributeError: 'AgentConfig' object has no attribute 'raw_config' 导致盘中急涨/急跌联动 TradingAgents 深度分析永远无法触发 (_read_auto_trigger_config / _budget_allows 均会失败)。 - _read_auto_trigger_config 与 _budget_allows 改读 agent.config - 单测 _make_agent 改用 MagicMock(spec=AgentConfig) + 真实字段 config, 从根上杜绝字段名回归(裸 MagicMock 任意属性都存在,此前测不出该 bug) --- src/agents/tradingagents/auto_trigger.py | 8 ++++---- tests/test_tradingagents_auto_trigger.py | 7 ++++--- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/agents/tradingagents/auto_trigger.py b/src/agents/tradingagents/auto_trigger.py index 677f9653..646eda79 100644 --- a/src/agents/tradingagents/auto_trigger.py +++ b/src/agents/tradingagents/auto_trigger.py @@ -30,7 +30,7 @@ def _read_auto_trigger_config(db: Session) -> dict | None: - """从 AgentConfig.raw_config 读 auto_trigger 配置。 + """从 AgentConfig.config 读 auto_trigger 配置。 Returns: { @@ -42,7 +42,7 @@ def _read_auto_trigger_config(db: Session) -> dict | None: agent = db.query(AgentConfig).filter(AgentConfig.name == "tradingagents").first() if not agent: return None - raw = agent.raw_config or {} + raw = agent.config or {} auto = raw.get("auto_trigger") or {} if not auto.get("enabled"): return None @@ -69,7 +69,7 @@ def _within_cooldown(db: Session, stock_symbol: str, cooldown_hours: int) -> boo def _budget_allows(db: Session) -> bool: - """检查月度预算是否还有余量。预算从 tradingagents 的 raw_config.monthly_budget_usd 读。""" + """检查月度预算是否还有余量。预算从 tradingagents 的 config.monthly_budget_usd 读。""" try: from src.agents.tradingagents.cost_tracker import check_budget except ImportError: @@ -78,7 +78,7 @@ def _budget_allows(db: Session) -> bool: agent = db.query(AgentConfig).filter(AgentConfig.name == "tradingagents").first() if not agent: return True - raw = agent.raw_config or {} + raw = agent.config or {} budget = float(raw.get("monthly_budget_usd") or 0.0) if budget <= 0: return True # 没设上限 = 不限制 diff --git a/tests/test_tradingagents_auto_trigger.py b/tests/test_tradingagents_auto_trigger.py index 1a9776f4..d916a688 100644 --- a/tests/test_tradingagents_auto_trigger.py +++ b/tests/test_tradingagents_auto_trigger.py @@ -7,11 +7,12 @@ import pytest from src.agents.tradingagents import auto_trigger +from src.web.models import AgentConfig -def _make_agent(raw_config: dict): - agent = MagicMock() - agent.raw_config = raw_config +def _make_agent(config: dict): + agent = MagicMock(spec=AgentConfig) + agent.config = config return agent