全量修复:预测系统正确性、安全性与部署问题
P0 严重问题修复: - 修复 form_slice/stats_slice 主客身份反转(历史比赛视角错误) - 修复 understat.py httpx 未导入导致的 NameError - 修复 LLM 解析失败时静默产生假成功预测(0-0 平局+置信度0.5) 预测路径修复: - multi-agent 路径增加 backtest cutoff 透传,回测防泄漏生效 - H2H 切片汇总统计改为从当前主队视角计数 - 预测唯一约束增加 mode+run_type 维度,防止回测覆盖实盘预测 伤停管线修复: - IntegrityError 后不再整批回滚丢数据(改用逐条 flush) - return_date 正确解析并写入 - retrieved_at 比较统一用 date() 避免当天数据不可见 - 唯一索引改为 partial unique index(排除 NULL 重复) - HTTP 缓存 TTL 从 7 天改为 6 小时 安全与连接管理: - /api/v1/predict 增加内存滑动窗口限流(10次/分钟/IP) - 预测路由改用短 session 模式,LLM 调用期间不持有 DB 连接 Docker 部署修复: - 修复 .dockerignore 排除 *.md 导致 COPY README.md 失败 - 容器内 DATABASE_URL 使用 postgres 服务名(非 localhost) - 启动时自动执行 alembic upgrade head - 前端改用多阶段构建(Dockerfile.frontend) 新增测试(5个文件,24+用例): - test_p0_home_away.py: 主客身份反转回归测试 - test_p0_parse_failure.py: LLM 解析失败回归测试 - test_multi_agent_cutoff.py: multi-agent cutoff 透传测试 - test_h2h_perspective.py: H2H 视角测试 - test_injuries_pipeline.py: 伤停管线 5 项修复测试 - test_predict_protection.py: 限流+短 session 测试 - test_prediction_unique_constraint.py: 唯一约束测试 迁移: - 0012_injuries_partial_unique_and_return_date.py - 0013_predictions_unique_constraint_mode_run_type.py
This commit is contained in:
+21
-4
@@ -16,6 +16,7 @@ from sqlalchemy import (
|
||||
String,
|
||||
Text,
|
||||
UniqueConstraint,
|
||||
and_,
|
||||
func,
|
||||
)
|
||||
from sqlalchemy.dialects.postgresql import JSONB
|
||||
@@ -164,7 +165,19 @@ class Injury(Base):
|
||||
team: Mapped["Team | None"] = relationship()
|
||||
|
||||
__table_args__ = (
|
||||
Index("ix_injuries_player_fixture", "player_id", "fixture_id", "injury_type", unique=True),
|
||||
# Fix 4: partial unique index — 只在 player_id 和 fixture_id 都非空时强制唯一
|
||||
# PostgreSQL 中 NULL != NULL,普通唯一索引无法防止 NULL 重复
|
||||
Index(
|
||||
"ix_injuries_player_fixture",
|
||||
"player_id",
|
||||
"fixture_id",
|
||||
"injury_type",
|
||||
unique=True,
|
||||
postgresql_where=and_(
|
||||
player_id.is_not(None),
|
||||
fixture_id.is_not(None),
|
||||
),
|
||||
),
|
||||
Index("ix_injuries_team_date", "team_id", "injury_date"),
|
||||
)
|
||||
|
||||
@@ -194,6 +207,8 @@ class Prediction(Base):
|
||||
agent_outputs: Mapped[dict | None] = mapped_column(JSONB)
|
||||
# 预测状态: success / failed / degraded
|
||||
status: Mapped[str] = mapped_column(String(20), nullable=False, default="success")
|
||||
# Fix: run_type 区分实盘(live)与回测(backtest),避免回测覆盖实盘预测
|
||||
run_type: Mapped[str] = mapped_column(String(10), nullable=False, default="live")
|
||||
# 时间语义:区分比赛时间、预测创建时间、数据截止时间
|
||||
match_kickoff_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
|
||||
prediction_created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=_utcnow)
|
||||
@@ -207,10 +222,11 @@ class Prediction(Base):
|
||||
match: Mapped[Match] = relationship(back_populates="predictions")
|
||||
|
||||
__table_args__ = (
|
||||
# P1-6: 数据库级唯一约束,防止同一 match+provider+model 产生重复预测
|
||||
# Fix: 唯一约束增加 mode + run_type,允许 live 与 backtest 共存
|
||||
# 防止回测覆盖未结算的实盘预测(后续 settle 会污染评估数据)
|
||||
UniqueConstraint(
|
||||
"match_id", "provider", "model",
|
||||
name="uq_predictions_match_provider_model",
|
||||
"match_id", "provider", "model", "mode", "run_type",
|
||||
name="uq_predictions_match_provider_model_mode_run_type",
|
||||
),
|
||||
Index("ix_predictions_match", "match_id"),
|
||||
Index("ix_predictions_provider_model", "provider", "model"),
|
||||
@@ -223,6 +239,7 @@ class Prediction(Base):
|
||||
CheckConstraint("pred_1x2 IN ('1', 'X', '2')", name="ck_pred_1x2_enum"),
|
||||
CheckConstraint("mode IN ('single', 'multi')", name="ck_mode_enum"),
|
||||
CheckConstraint("status IN ('success', 'failed', 'degraded')", name="ck_status_enum"),
|
||||
CheckConstraint("run_type IN ('live', 'backtest')", name="ck_run_type_enum"),
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user