fix(prompt): prioritize evidence by score before chronological render

This commit is contained in:
2026-02-16 22:58:36 +08:00
parent 52f1a35340
commit e8afb609b7

View File

@@ -1112,26 +1112,32 @@ async function buildVectorPrompt(store, recallResult, causalById, focusCharacter
if (distantL0.length && total.used < total.max) { if (distantL0.length && total.used < total.max) {
const distantBudget = { used: 0, max: Math.min(SUMMARIZED_EVIDENCE_MAX, total.max - total.used) }; const distantBudget = { used: 0, max: Math.min(SUMMARIZED_EVIDENCE_MAX, total.max - total.used) };
// 按楼层排序(时间顺序)后分组 // 先按分数挑组(高分优先),再按时间输出(楼层升序)
distantL0.sort((a, b) => a.floor - b.floor);
const distantFloorMap = groupL0ByFloor(distantL0); const distantFloorMap = groupL0ByFloor(distantL0);
const distantRanked = [];
// 按楼层顺序遍历Map 保持插入顺序distantL0 已按 floor 排序)
for (const [floor, l0s] of distantFloorMap) { for (const [floor, l0s] of distantFloorMap) {
const group = buildEvidenceGroup(floor, l0s, l1ByFloor); const group = buildEvidenceGroup(floor, l0s, l1ByFloor);
const bestScore = Math.max(...l0s.map(l0 => (l0.rerankScore ?? l0.similarity ?? 0)));
distantRanked.push({ group, bestScore });
}
distantRanked.sort((a, b) => (b.bestScore - a.bestScore) || (a.group.floor - b.group.floor));
// 原子组预算检查 const acceptedDistantGroups = [];
for (const item of distantRanked) {
const group = item.group;
if (distantBudget.used + group.totalTokens > distantBudget.max) continue; if (distantBudget.used + group.totalTokens > distantBudget.max) continue;
distantBudget.used += group.totalTokens;
acceptedDistantGroups.push(group);
for (const l0 of group.l0Atoms) usedL0Ids.add(l0.id);
injectionStats.distantEvidence.units++;
}
acceptedDistantGroups.sort((a, b) => a.floor - b.floor);
for (const group of acceptedDistantGroups) {
const groupLines = formatEvidenceGroup(group); const groupLines = formatEvidenceGroup(group);
for (const line of groupLines) { for (const line of groupLines) {
assembled.distantEvidence.lines.push(line); assembled.distantEvidence.lines.push(line);
} }
distantBudget.used += group.totalTokens;
for (const l0 of l0s) {
usedL0Ids.add(l0.id);
}
injectionStats.distantEvidence.units++;
} }
assembled.distantEvidence.tokens = distantBudget.used; assembled.distantEvidence.tokens = distantBudget.used;
@@ -1154,24 +1160,32 @@ async function buildVectorPrompt(store, recallResult, causalById, focusCharacter
if (recentL0.length) { if (recentL0.length) {
const recentBudget = { used: 0, max: UNSUMMARIZED_EVIDENCE_MAX }; const recentBudget = { used: 0, max: UNSUMMARIZED_EVIDENCE_MAX };
// 按楼层排序后分组 // 先按分数挑组(高分优先),再按时间输出(楼层升序)
recentL0.sort((a, b) => a.floor - b.floor);
const recentFloorMap = groupL0ByFloor(recentL0); const recentFloorMap = groupL0ByFloor(recentL0);
const recentRanked = [];
for (const [floor, l0s] of recentFloorMap) { for (const [floor, l0s] of recentFloorMap) {
const group = buildEvidenceGroup(floor, l0s, l1ByFloor); const group = buildEvidenceGroup(floor, l0s, l1ByFloor);
const bestScore = Math.max(...l0s.map(l0 => (l0.rerankScore ?? l0.similarity ?? 0)));
recentRanked.push({ group, bestScore });
}
recentRanked.sort((a, b) => (b.bestScore - a.bestScore) || (a.group.floor - b.group.floor));
const acceptedRecentGroups = [];
for (const item of recentRanked) {
const group = item.group;
if (recentBudget.used + group.totalTokens > recentBudget.max) continue; if (recentBudget.used + group.totalTokens > recentBudget.max) continue;
recentBudget.used += group.totalTokens;
acceptedRecentGroups.push(group);
for (const l0 of group.l0Atoms) usedL0Ids.add(l0.id);
injectionStats.recentEvidence.units++;
}
acceptedRecentGroups.sort((a, b) => a.floor - b.floor);
for (const group of acceptedRecentGroups) {
const groupLines = formatEvidenceGroup(group); const groupLines = formatEvidenceGroup(group);
for (const line of groupLines) { for (const line of groupLines) {
assembled.recentEvidence.lines.push(line); assembled.recentEvidence.lines.push(line);
} }
recentBudget.used += group.totalTokens;
for (const l0 of l0s) {
usedL0Ids.add(l0.id);
}
injectionStats.recentEvidence.units++;
} }
assembled.recentEvidence.tokens = recentBudget.used; assembled.recentEvidence.tokens = recentBudget.used;