fix(tasks): 任务列表改按 id 降序,消除同刻创建任务的抖动

原先按 created_at 降序排,但没有二级排序键。同一时刻创建的多个任务时间戳
相同,排序不稳定;叠加前端每 10 秒轮询、以及 Manager 用内存 map 存任务
(遍历顺序随机、每次 List 都要重排),这些任务就会在列表里频繁互换位置。

改用 id 降序:id 由 BIGSERIAL 单调递增分配,即插入顺序,唯一且稳定,最新
创建的在最前。db.ListTasks 的 ORDER BY 与 Manager.List 的内存排序同步改。

验证: 新增 TestListTasksOrderByIDDesc 连本地 PG 实跑通过;db/server 全量
无新增失败(仅剩既有的 TestCompanyScopeInvalid)。
This commit is contained in:
Autumn-27 committed 2026-08-24 11:08:10 -04:00
1 parent dfbbb33069
commit 4cf36c7896
9 files changed
+128 -3

No files matched your search

+8 -1
View File
@@ -1558,7 +1558,14 @@ func (m *Manager) List() []*Task {
for _, t := range m.tasks {
out = append(out, t)
}
sort.Slice(out, func(i, j int) bool { return out[i].CreatedAt > out[j].CreatedAt })
// 按 id 降序(最新插入在最前)。m.tasks 是 map,遍历顺序随机,每次轮询都要重排;
// 若按 CreatedAt 排,同一秒创建的任务时间戳相同,排序不稳定,会在列表里频繁换
// 位置。id 由 BIGSERIAL 单调递增分配,唯一且稳定,顺序恒定。
sort.Slice(out, func(i, j int) bool {
ai, _ := strconv.ParseInt(out[i].ID, 10, 64)
aj, _ := strconv.ParseInt(out[j].ID, 10, 64)
return ai > aj
})
return out
}