安全加固 + 数据管线修复 + 质量改进(第2批)
安全加固: - /api/v1/predict 限流改用 get_client_ip 防 X-Forwarded-For 伪造 - 新增 TRUST_PROXY_HEADERS/REQUIRE_ADMIN_AUTH 配置,默认 fail-closed - 生产环境管理接口未配置鉴权时拒绝(503),不再放行 数据管线修复: - injuries IntegrityError 改用 begin_nested(SAVEPOINT)隔离批次 - injuries 空名单 vs 未配置语义区分(has_data 精确标记) - bzzoiro 统计字段映射(shots/possession/cards) + 入库条件放宽 - available_at 语义收紧(回测防泄漏) - team_names NFKD 去变音双重查找 + 补齐变体键 预测系统改进: - multi-agent 全专家失败时 status=degraded 跳过终裁 - agent_weights 独立持久化到 predictions 表 新增迁移: - 0014_predictions_agent_weights.py 新增测试(8个文件): - test_ip_spoofing.py: 限流防伪造 - test_require_admin_fail_closed.py: 生产 fail-closed - test_injuries_integrity_rollback.py: SAVEPOINT 隔离 - test_injuries_slice_semantic.py: 空名单 vs 未配置 - test_available_at.py: 回测防泄漏 - test_bzzoirot_stats.py: 统计字段映射 - test_team_names_normalize.py: NFKD 变体 - test_multi_agent_degraded.py: 全失败 degraded - test_agent_weights_persist.py: 权重持久化
This commit is contained in:
@@ -0,0 +1,141 @@
|
||||
"""回归测试: predictions 表 agent_weights 独立持久化。
|
||||
|
||||
验证:
|
||||
1. ORM 模型有 agent_weights 列(JSONB, 可空)
|
||||
2. 迁移文件存在且可逆
|
||||
3. orchestrator 写入 agent_weights
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from src.db.models import Prediction
|
||||
|
||||
|
||||
class TestAgentWeightsColumn:
|
||||
"""验证 predictions 表有 agent_weights 列。"""
|
||||
|
||||
def test_orm_has_agent_weights_column(self):
|
||||
"""ORM 模型应包含 agent_weights 列。"""
|
||||
cols = {c.name: c for c in Prediction.__table__.columns}
|
||||
assert "agent_weights" in cols, "predictions 表应有 agent_weights 列"
|
||||
|
||||
def test_agent_weights_is_jsonb(self):
|
||||
"""agent_weights 应为 JSONB 类型。"""
|
||||
col = Prediction.__table__.columns["agent_weights"]
|
||||
# JSONB 类型检查
|
||||
assert "JSON" in str(col.type).upper() or "JSONB" in str(col.type).upper()
|
||||
|
||||
def test_agent_weights_nullable(self):
|
||||
"""agent_weights 应可空(旧行保持 NULL)。"""
|
||||
col = Prediction.__table__.columns["agent_weights"]
|
||||
assert col.nullable is True, "agent_weights 应可空"
|
||||
|
||||
|
||||
class TestMigration:
|
||||
"""验证迁移文件存在且内容正确。"""
|
||||
|
||||
def test_migration_exists(self):
|
||||
import os
|
||||
|
||||
path = "/.octop/workspaces/CA7PFH/Profeto/alembic/versions/0014_predictions_agent_weights.py"
|
||||
assert os.path.exists(path)
|
||||
|
||||
def test_migration_content(self):
|
||||
path = "/.octop/workspaces/CA7PFH/Profeto/alembic/versions/0014_predictions_agent_weights.py"
|
||||
content = open(path).read()
|
||||
|
||||
assert "agent_weights" in content
|
||||
assert "upgrade" in content
|
||||
assert "downgrade" in content
|
||||
assert "downgrade" in content and "drop_column" in content
|
||||
assert "nullable=True" in content
|
||||
|
||||
|
||||
class TestOrchestratorWritesAgentWeights:
|
||||
"""验证 orchestrator 写入 agent_weights。"""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_orchestrator_writes_agent_weights_to_upsert(self):
|
||||
"""orchestrator 应将 agent_weights 传入 _upsert_prediction。"""
|
||||
from src.llm.agents import orchestrator as orch_mod
|
||||
from src.llm.agents.base import AgentReport
|
||||
from src.llm.context_builder import MatchHeader
|
||||
|
||||
header = MatchHeader(
|
||||
match_id=999, home_name="A", away_name="B",
|
||||
league_name="X", season=None, match_date="?",
|
||||
match_dt=None, stage=None,
|
||||
home_team_id=1, away_team_id=2, league_id=1,
|
||||
)
|
||||
|
||||
# 构造有效专家报告(至少 1 个 ok)
|
||||
reports = [
|
||||
AgentReport(agent="form", status="ok", analysis="good"),
|
||||
AgentReport(agent="stats", status="error", analysis="failed"),
|
||||
AgentReport(agent="home_away", status="ok", analysis="good"),
|
||||
AgentReport(agent="injuries", status="no_data", analysis="无数据"),
|
||||
AgentReport(agent="h2h", status="error", analysis="failed"),
|
||||
]
|
||||
|
||||
captured_values = {}
|
||||
|
||||
async def mock_specialists(h, *, version, before):
|
||||
return reports
|
||||
|
||||
async def mock_provider(aid, **kw):
|
||||
return MagicMock(model="test-model")
|
||||
|
||||
async def mock_header(mid, db=None):
|
||||
return header
|
||||
|
||||
async def mock_aggregator(header, reports, *, provider, version):
|
||||
return {
|
||||
"pred_home_goals": 2,
|
||||
"pred_away_goals": 1,
|
||||
"pred_1x2": "1",
|
||||
"subjective_confidence": 0.7,
|
||||
"reasoning": "test",
|
||||
"agent_weights": {"form": 0.3, "home_away": 0.5, "stats": 0.2},
|
||||
}, 100, 50
|
||||
|
||||
async def mock_upsert(session, **kw):
|
||||
captured_values.update(kw.get("values", {}))
|
||||
p = MagicMock()
|
||||
p.id = 1
|
||||
p.provider = "test"
|
||||
p.model = "test"
|
||||
p.prompt_version = "v1"
|
||||
p.pred_home_goals = 2
|
||||
p.pred_away_goals = 1
|
||||
p.pred_1x2 = "1"
|
||||
p.subjective_confidence = 0.7
|
||||
p.reasoning = "test"
|
||||
p.agent_outputs = []
|
||||
p.agent_weights = kw["values"].get("agent_weights")
|
||||
return p
|
||||
|
||||
class FakeUow:
|
||||
async def __aenter__(self):
|
||||
return self
|
||||
async def __aexit__(self, *a):
|
||||
pass
|
||||
async def get(self, cls, id):
|
||||
return MagicMock()
|
||||
|
||||
with patch.object(orch_mod, "run_specialists", mock_specialists), \
|
||||
patch.object(orch_mod, "_agent_provider", mock_provider), \
|
||||
patch.object(orch_mod, "load_match_header", mock_header), \
|
||||
patch.object(orch_mod, "_upsert_prediction", mock_upsert), \
|
||||
patch.object(orch_mod, "run_aggregator", mock_aggregator), \
|
||||
patch.object(orch_mod, "get_uow", FakeUow):
|
||||
|
||||
result = await orch_mod.predict_match_multi(999)
|
||||
|
||||
# 断言 agent_weights 被写入
|
||||
assert "agent_weights" in captured_values, "agent_weights 应传入 _upsert_prediction"
|
||||
assert captured_values["agent_weights"] is not None, "agent_weights 不应为 None"
|
||||
assert "form" in captured_values["agent_weights"], "agent_weights 应包含专家权重"
|
||||
print(f"PASS: agent_weights = {captured_values['agent_weights']}")
|
||||
Reference in New Issue
Block a user