fix: 对话删除后不再复活(并集合并bug)

- 根因: server∪mine 并集合并, 本机删的对话服务端还有 -> 下次同步并回
- 改用删除墓碑: removeChat 记录 {id,at} 到 it-tools-ai-chat-deleted 并纳入同步
- 合并时先并集再按墓碑剔除; 墓碑不过期(500上限截断), 删除永久生效

测试5项: 本机删除不复活/服务端墓碑生效/未删保留/旧墓碑生效/新对话不误删 ✓

CHANGELOG记录
This commit is contained in:
personal82555 committed 2026-10-13 22:06:45 +08:00
1 parent fc8e1c7c37
commit d880df224d
3 files changed
+53

No files matched your search

+7
View File
@@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file. See [standa
## Version 2026.10.12-it-tools-pro
### 🗑️ 修复对话删除后又"复活"
- 根因:云同步按 `server ∪ mine` **并集合并**,本机删掉的对话服务端仍有 → 下次同步被并回来(周期同步后 60 秒内必然复活)
- 改用**删除墓碑(tombstone)**:删除对话时记录 `{id, at}` 到 `it-tools-ai-chat-deleted` 并纳入同步白名单
- 合并时先并集、再按墓碑统一剔除;墓碑**不过期**(靠 500 上限截断),删除永久生效
- 独立测试 5 项全过:本机删除不复活 / 服务端墓碑在另一台生效 / 未删对话保留 / 旧墓碑仍生效 / 新对话不误删 ✓
### ☁️ 修复跨电脑对话/数据不同步
- 根因:上传靠 3 秒防抖且**失败不重试**(关标签页/服务重启期间的推送会丢且不补),拉取仅在登录与页面加载时触发 → 多设备数据分叉
- 新增 **周期性双向同步**:登录后每 60 秒 push+pull 自愈,任一设备的改动(含对话)60 秒内收敛到其他设备
+37
View File
@@ -7,6 +7,7 @@ const SYNC_KEYS = [
'it-tools-bookmarks:sites',
'menu-tool-option:collapsed-categories',
'it-tools-ai-chat-convs',
'it-tools-ai-chat-deleted', // 对话删除墓碑
];
function collectLocal(): Record<string, any> {
@@ -87,6 +88,38 @@ function trimConvs(jsonStr: string): string {
}
}
// 删除墓碑不过期(删除永久生效,避免旧设备离线超期后把已删对话“复活”),仅按上限截断
const TOMBSTONE_MAX = 500;
function parseArr(s: unknown): any[] {
try {
const a = JSON.parse(String(s ?? ''));
return Array.isArray(a) ? a : [];
}
catch { return []; }
}
/** 合并两份删除墓碑:并集 + 上限 */
function mergeTombstones(a: string, b: string): string {
const out = parseArr(a);
const seen = new Set(out.map(t => t?.id));
for (const t of parseArr(b)) {
if (t?.id && !seen.has(t.id)) {
out.push(t);
seen.add(t.id);
}
}
return JSON.stringify(out.slice(-TOMBSTONE_MAX));
}
/** 从对话列表中剔除墓碑标记的 id —— 防止并集合并把已删对话“复活” */
function applyTombstones(data: Record<string, any>) {
const dead = new Set(parseArr(data['it-tools-ai-chat-deleted']).map(t => t?.id));
if (!dead.size || !data['it-tools-ai-chat-convs']) return;
const convs = parseArr(data['it-tools-ai-chat-convs']).filter(c => c?.id && !dead.has(c.id));
data['it-tools-ai-chat-convs'] = JSON.stringify(convs);
}
function mergeData(server: Record<string, any>, mine: Record<string, any>): Record<string, any> {
const out: Record<string, any> = { ...server };
for (const [k, v] of Object.entries(mine)) {
@@ -105,11 +138,15 @@ function mergeData(server: Record<string, any>, mine: Record<string, any>): Reco
// 对话列表按 id 合并(多设备各自的会话都保留),上限 30 条
out[k] = trimConvs(mergeJsonArrays(out[k], v, 'id'));
}
else if (k === 'it-tools-ai-chat-deleted') {
out[k] = mergeTombstones(out[k], v);
}
else {
out[k] = v; // scalar configs: local wins
}
}
}
applyTombstones(out);
return out;
}
+9
View File
@@ -89,12 +89,21 @@ function newChat() {
saveConvs();
}
// 删除墓碑:记录被删对话 id,合并时统一剔除,避免 server∪mine 并集把已删对话“复活”
const DELETED_KEY = 'it-tools-ai-chat-deleted';
function removeChat(id: string) {
conversations.value = conversations.value.filter(c => c.id !== id);
if (curId.value === id) {
curId.value = conversations.value[0]?.id || '';
}
saveConvs();
try {
const del = JSON.parse(localStorage.getItem(DELETED_KEY) || '[]');
del.push({ id, at: Date.now() });
localStorage.setItem(DELETED_KEY, JSON.stringify(del.slice(-500)));
}
catch { /* ignore */ }
}
function fmtTime(ts: number) {