fix: 代码审查问题修复

- unit_of_work.py: 简化为纯 session 上下文管理器,消除 double-close 风险
- validation.py: 移除未使用的 import,简化 _score_to_1x2 逻辑
- repositories.py: 将 func 导入移到模块顶层
- 更新所有 get_uow() 调用点使用新接口(yield session 而非 uow 对象)
This commit is contained in:
shangfangjian
2026-09-15 01:18:35 +08:00
parent 74586aa5b7
commit 53e602b4f6
8 changed files with 40 additions and 86 deletions
+4 -7
View File
@@ -145,10 +145,9 @@ async def _predict_single(
raise RuntimeError(f"LLM 输出校验失败: {e}")
# 4. 存预测(使用 UnitOfWork 统一事务)
async with get_uow() as uow:
db = uow.session
async with get_uow() as session:
# 验证 match 存在
m = await db.get(Match, match_id)
m = await session.get(Match, match_id)
if m is None:
raise ValueError(f"match {match_id} not found")
@@ -169,9 +168,8 @@ async def _predict_single(
cutoff_at=cutoff_at,
input_hash=input_hash,
)
db.add(pred)
await db.commit()
await db.refresh(pred)
session.add(pred)
await session.refresh(pred)
result = PredictResult(
prediction_id=pred.id,
@@ -190,5 +188,4 @@ async def _predict_single(
# 5. 写入缓存
_set_cached(match_id, settings.LLM_PROVIDER, provider.model, version, result)
await uow.commit()
return result