Skip to content

fix(ai): avoid quadratic coverage counting during analysis - #167

Merged
2977094657 merged 2 commits into
LifeArchiveProject:mainfrom
qq666666qq:fix/agent-coverage-sql
Sep 28, 2026
Merged

2977094657 merged 2 commits into
LifeArchiveProject:mainfrom
qq666666qq:fix/agent-coverage-sql

Conversation

@qq666666qq

@qq666666qq qq666666qq commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

问题原因与表现

完整分析聊天时,程序会定期统计已有多少消息被分析。原来的数据库查询可能对每条消息反复扫描已保存的覆盖记录;记录越多,单次统计越慢,进而拖慢整个分析任务。这里增加的是本地等待时间,不会把旧消息重复发送给模型。

修复

  • 调整覆盖统计的查询顺序:先遍历已覆盖的消息来源,再用现有主键查找对应原文,避免重复扫描。
  • 保持原有计数规则,包括重复覆盖只算一次、有缺口的不算、不同任务和版本互不混淆。
  • 增加计数正确性和查询工作量的回归测试;不新增缓存、索引或数据表。

验证

  • Python 3.11 下,两组相关测试分别为 89 passed 和 41 passed。
  • 本地合成 1,000 条数据时,单次完整进度调用约从 2.73 秒降至 0.05 秒;这不是整个 AI 分析任务的加速倍数。
  • git diff --check 通过。

后续提交:修复 Windows CI 偶发失败(仅测试)

  • 表现:tests/test_ai_agent_sse.py 在 Windows 上偶发 IndexError——事件写入较慢时,30ms 的测试心跳帧先于事件帧到达,解析 data: 失败。
  • 结论:心跳注释先于事件帧是协议允许的合法 SSE 行为(WHATWG Server-Sent Events),不是生产事件丢失,因此不改生产代码。
  • 修复:在通知验证段把测试心跳抬升到断言截止时间(0.5s)之后;通知路径失效时会以 TimeoutError 失败,而不是靠心跳重查误判通过。
  • 验证:修复目标测试 20/20 次本地循环通过;注入 200ms 慢写可复现原失败、修复后通过;注入通知失效探针按预期超时。

@2977094657

Copy link
Copy Markdown
Member

有失败,修一下

Windows CI intermittently failed test_stream_waits_for_notification_
instead_of_polling...: the next frame after the event write was the
': heartbeat' comment, so parsing 'data: ' raised IndexError. A slow
synchronous SQLite write can let the 30ms test heartbeat win the race;
a heartbeat comment before an event frame is protocol-valid SSE, so
keep production behavior and instead raise the heartbeat above the
0.5s assertion deadline for the notification check. A broken
notification now fails with TimeoutError instead of passing on a
heartbeat frame.
@2977094657
2977094657 merged commit 231382b into LifeArchiveProject:main Sep 28, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants