Task #1609 修复:同轮择优逻辑优先处理最新消息而非最旧消息

根因:OCR读到两条消息(17:45和18:17),质量分评估未考虑时间戳新鲜度,
导致旧消息因关键词'插件'加分更高被选中

修复:在质量分评估中加入时间戳新鲜度奖励(≤300秒+5分,≤600秒+3分,≤1800秒+1分),
并修改排序逻辑:先按质量分降序,再按时间戳差值升序(越新鲜越好)

效果:当多条消息质量分相近时,优先处理最新消息
This commit is contained in:
youpaishidifu
2026-08-25 18:27:52 +08:00
parent 2726863405
commit 4404fb40f7
+28 -8
View File
@@ -9650,24 +9650,44 @@ class MessageMonitor:
continue
# ⭐ 添加到候选列表(含质量分)
score = self._score_message_quality(cleaned_item)
_candidates.append((score, item, cleaned_item))
# ⭐ Task #1609: 时间戳新鲜度奖励——优先处理最新消息
# 从原始文本中提取时间戳,计算与当前时间的差值
_ts_match = re.search(r'(\d{1,2}):(\d{1,2})(?::(\d{1,2}))?', item)
if _ts_match:
_h, _m = int(_ts_match.group(1)), int(_ts_match.group(2))
_s = int(_ts_match.group(3)) if _ts_match.group(3) else 0
# 转换为今天的秒数(从0点开始)
_ts_seconds = _h * 3600 + _m * 60 + _s
_now_seconds = now % 86400 # 当前时间的秒数(从0点开始)
_diff = _now_seconds - _ts_seconds
# 如果差值为负,说明时间戳跨天(如23:59 vs 00:01),视为很旧
if _diff < 0:
_diff += 86400
# 新鲜度奖励:≤300秒+5分,≤600秒+3分,≤1800秒+1分
if _diff <= 300:
score += 5.0
elif _diff <= 600:
score += 3.0
elif _diff <= 1800:
score += 1.0
_candidates.append((score, item, cleaned_item, _diff if _ts_match else 86400))
# ⭐ 同轮择优:只选质量分最高的那条消息触发回复
if _candidates:
# 按质量分降序排序,选最高分
_candidates.sort(key=lambda x: x[0], reverse=True)
_best_score, _best_raw, _best_cleaned = _candidates[0]
# 如果有多条同分,优先选较短的(真实用户消息通常更短)
# 按质量分降序排序,再按时间戳差值升序(越新鲜越好)
_candidates.sort(key=lambda x: (x[0], -x[3]), reverse=True)
_best_score, _best_raw, _best_cleaned, _best_diff = _candidates[0]
# 如果有多条同分,优先选时间戳最新的(差值最小)
_top_candidates = [c for c in _candidates if c[0] == _best_score]
_top_candidates.sort(key=lambda x: len(x[2]))
_best_score, _best_raw, _best_cleaned = _top_candidates[0]
_top_candidates.sort(key=lambda x: x[3]) # 按时间戳差值升序
_best_score, _best_raw, _best_cleaned, _best_diff = _top_candidates[0]
logger.info(
f"[{platform}] 同轮择优: 选中({_best_score:.1f}分) {_best_cleaned[:50]!r}, "
f"候选{len(_candidates)}条"
)
if len(_candidates) > 1:
for _sc, _raw, _cl in _candidates[1:]:
for _sc, _raw, _cl, _diff in _candidates[1:]:
logger.debug(f"[{platform}] 同轮择优: 丢弃({_sc:.1f}分) {_cl[:50]!r}")
# ⭐ 添加到最近处理列表