fix(ai): subject 里塞了行数,每轮询一次就新建一个任务

生产上 trends 队列里积了 36 个任务,subject 是 2026-09-01:1033、:1039、
:1044……一路涨。这台账号当时正在补历史,get_summary 的行数每隔几分钟就变,
而我把 len(rows) 写进了 subject——subject 同时是缓存键和任务队列的键,一变
就是一条全新的任务,轮询几次就刷出十几条。

subject 该回答的是「这条解读是关于什么的」,不是「当时有多少行数据」。
数据变化本来就由 fingerprint 负责。

- trends 的 subject 改成快照日期;sleep 用配置的窗口常量而不是实际夜数
  (缺一晚也不该换键);challenges 用固定键
- 加了不变量测试:补一天历史数据后 subject 不许变;任何 subject 段都不许
  长得像行数

顺带加一层兜底 jobs.supersede():单实例 scope 只该有一个在跑的 subject,
队列里同 kind 的其它 pending 任务是关于已经不存在的快照的,跑完也没人看。
per_item 的 daily / activity 不受影响——它们本来就一天一条、一次运动一条。
兜底不是机制,机制是 subject 稳定;它存在只是因为这次 subject 不稳定,而
36 条任务堆在那里之前没人发现。

顺带按要求把 AiPanel 改成默认精简:只显示标题、来源和一句话结论,点「展开
详细」才出要点/建议/依据,可再收起——和今日晨报卡片一致。这些面板压在本来
就很密的图表页上面,全部默认展开会把真正的数据一次性挤到屏幕外。

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
ericwyuan
2026-09-01 15:33:49 +08:00
parent 241ae0d6a3
commit 6dd070ec9b
6 changed files with 182 additions and 25 deletions

View File

@@ -150,6 +150,25 @@ def enqueue(user_id, kind, subject, fingerprint=None,
return "pending"
def supersede(user_id, kind, subject):
"""Drop queued work of the same kind for a different subject.
A single-entry scope has one live subject; anything else queued under that
kind is about a snapshot that no longer exists, and running it would spend
a gateway call on an answer nothing will read.
This is a safety net, not the mechanism: subjects are supposed to be stable
(see scopes.py). It exists because they were not — a row count in the key
made every poll mint a new `trends` job, and production had 36 of them
queued before anyone noticed.
"""
execute(
"DELETE FROM ai_jobs WHERE user_id = ? AND kind = ? AND subject != ? "
"AND status = 'pending'",
[user_id, kind, subject],
)
def status_of(user_id, kind, subject):
row = query_one("SELECT * FROM ai_jobs WHERE id = ?",
[job_id(user_id, kind, subject)])