fix(審查流程): 修復 AI 去重超量、clone 失敗誤持久化、留言行號漏 new_position

This commit is contained in:
Jeffery
2026-07-03 11:40:18 +08:00
parent fc5baf32db
commit 2c0ac71c08
3 changed files with 19 additions and 8 deletions
+11 -6
View File
@@ -420,13 +420,18 @@ export async function deduplicateWithAI(findings) {
try {
const result = await chatJSON(systemPrompt, JSON.stringify(toAIPayload(findings)));
if (Array.isArray(result) && result.length > 0) {
ok(`AI 去重: ${findings.length} -> ${result.length}`);
// 以 location+suggestion 為 key,將原始 findings 的完整欄位(含 is_new)補回
const origMap = new Map(findings.map(f => [`${f.location}|${String(f.suggestion).slice(0, 50)}`, f]));
return result.map(r => origMap.get(`${r.location}|${String(r.suggestion).slice(0, 50)}`) ?? r);
// 去重結果數量不得超過輸入(避免 LLM 無中生有),且每筆都必須能對應回原始 finding。
if (Array.isArray(result) && result.length > 0 && result.length <= findings.length) {
const keyOf = f => `${f.location}|${String(f.suggestion).slice(0, 50)}`;
const origMap = new Map(findings.map(f => [keyOf(f), f]));
// 只保留能對應回原始 finding 的項目,丟棄無法對應(可能為幻覺)的結果
const mapped = result.map(r => origMap.get(keyOf(r))).filter(Boolean);
if (mapped.length > 0) {
ok(`AI 去重: ${findings.length} -> ${mapped.length}`);
return mapped;
}
}
throw new Error('AI 回傳空陣列');
throw new Error('AI 去重結果異常(空、超量或無法對應原始 findings)');
} catch (e) {
return fallback('AI 去重', findings, e);
}