v0.16.9: 核心引擎深度分析修复 — 27 个问题(P0×8 + P1×19 + P2 死代码)
引擎层(agent-engine.ts): - P0-R1: handleExecuting 中止改 throw AbortError,让主循环 catch 统一处理 UI 反馈 - P0-E1: executeToolWithTimeout 注释修正,明确 fire-and-forget 限制 - P0-P3: persistLoopContext 改名 snapshotLoopContext + 补全字段,明确语义为运行时快照 - P1-E1: handleInit 增加 isAborted 检查(loadCustomFiles 和 search 后) - P1-E2: catch 块调用 snapshotLoopContext 确保终止状态写入 - P1-E3: onConfirmTool await 后检查中止信号 - P1-E4: flushAllTraces 改为 await - P1-E5: 死锁检测 ephemeral 消息加入 PRESERVE_PATTERNS - P1-E6: 记忆提取 setTimeout 存储到 _pendingMemoryTimers,finally 块清理 - P1-R5: handleExecuting 中止时设置 _abortToolRecords 上下文层(context-manager.ts + agent-engine.ts): - P0-C1: 删除 nonSystemCompressed,旧摘要不再保留(已合并到新摘要) - P0-C2: 部分删除 tool 消息时也 strip tool_calls,降级为纯文本 assistant - P1-C1: handleCompressing 移除 shouldAutoCompress 二次判断,直接执行压缩 - P1-C2: COMPRESSING 状态增加中止检查 - P1-C3: R15 tail 边界保护 middle 无 assistant 时移除孤立 tool 消息 - P1-C4: 压缩接受条件改 AND(3 处:handleInit/R8/handleCompressing) - P1-C6: head 中已压缩消息的 tool_calls 清除,避免累积膨胀 渲染层(chat-area.ts + input-area.ts + workspace-panel.ts): - P0-R2: appendAssistantPlaceholder 重置 _streamLastRenderedLen/_streamLastRenderedHash/_streamRenderContent - P1-R1: rAF 竞态通过重置 _streamRenderContent 解决 - P1-R2: clearMessages/clearMessagesDOM 重置 _sysPromptRendered - P1-R3: onNewIteration 渲染顺序修正(remove → render → append) - P1-R4: updateToolCardDOM 改用 querySelectorAll 取最后一个匹配 持久化层(chat-db.ts + sqlite.ts + ipc.ts + history-modal.ts): - P0-P1: onNewIteration 调用 saveCurrentSession 持久化中间消息 - P0-P2: 非 AbortError 异常时保存 partial 消息(标记 interrupted: true) - P1-P1: saveSession 增量保存(_savedMsgIds Set 追踪),resetSavedMsgTracking - P1-P2: 新增 saveSettingsBatch 批量写盘接口 + IPC handler - P1-P3: 删除死表 tool_calls 的 saveToolCall/getToolCallsBySession 函数和 IPC handler 死代码清理: - chat-area.ts: 删除 appendToolCallCardToPlaceholder/updateToolCallCardInPlaceholder(从未调用) - chat-area.ts: 删除 _streamRenderThink/_streamRenderModel 未使用变量 - input-area.ts: 删除 updateMessageToolRecord 空函数 + 对应 import 版本号 0.16.8 → 0.16.9
This commit is contained in:
@@ -632,6 +632,30 @@ export async function compressWithLLM(
|
||||
const tail = nonSystemMsgs.slice(-keepTail);
|
||||
const middle = nonSystemMsgs.slice(keepHead, nonSystemMsgs.length - keepTail);
|
||||
|
||||
// P1-C6 修复:head 中最早的 assistant+tool_calls 组在多次压缩后无法清除,导致 token 膨胀。
|
||||
// 对 head 中已压缩过的消息(compressed=true),移除其 tool_calls 和后续 tool 消息(保留 content 作为上下文)。
|
||||
// 这些旧工具调用的结果已不再需要,但 assistant 的文本内容仍有上下文价值。
|
||||
if (head.length > 0) {
|
||||
for (let i = 0; i < head.length; i++) {
|
||||
const m = head[i];
|
||||
if (m.compressed && m.tool_calls?.length) {
|
||||
// 移除 tool_calls(降级为纯文本 assistant)
|
||||
const newMsg: OllamaMessage = { ...m };
|
||||
delete newMsg.tool_calls;
|
||||
head[i] = newMsg;
|
||||
}
|
||||
if (m.compressed && m.role === 'tool') {
|
||||
// 旧的 tool 消息标记为空(保留位置但内容为空,避免破坏数组结构)
|
||||
// 后续 mergeConsecutiveMessages 会合并这些空消息
|
||||
head[i] = { ...m, content: '', compressed: true };
|
||||
}
|
||||
}
|
||||
// 过滤掉 head 中被清空的 tool 消息
|
||||
const filteredHead = head.filter(m => !(m.compressed && m.role === 'tool' && !m.content));
|
||||
head.length = 0;
|
||||
head.push(...filteredHead);
|
||||
}
|
||||
|
||||
// R15: 对话轮次边界保护 — 调整 head/tail 切分点,避免在对话轮次中间切割
|
||||
// 如果 head 末尾是带 tool_calls 的 assistant,将后续 tool 消息也纳入 head
|
||||
if (head.length > 0 && head[head.length - 1].tool_calls?.length) {
|
||||
@@ -651,6 +675,10 @@ export async function compressWithLLM(
|
||||
if (extendBack >= 0) {
|
||||
const moved = middle.splice(extendBack);
|
||||
tail.unshift(...moved);
|
||||
} else {
|
||||
// P1-C3 修复:middle 中无 assistant 时,tail[0] 是孤立 tool 消息,
|
||||
// Ollama 会拒绝或忽略。将其从 tail 移除(它无对应 assistant.tool_calls)
|
||||
tail.shift();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -765,7 +793,8 @@ export async function compressWithLLM(
|
||||
// R16: 保留已压缩的中间消息(system 消息单独处理)+ 新摘要
|
||||
// R12: 标记这批消息为已压缩
|
||||
markAsCompressed(uncompressedMiddle);
|
||||
// R16: 已压缩的旧摘要已被合并到新摘要中,不再保留它们
|
||||
// P0-C1 修复:原代码保留 nonSystemCompressed(旧摘要),但新摘要已通过 mergeSummaries 合并了旧摘要内容,
|
||||
// 保留旧摘要会导致信息重复 + token 累积浪费。正确做法是不保留旧摘要(system 的已在 mergedSystemContent 中合并)
|
||||
const alreadyCompressed = middle.filter(m => m.compressed && m.role === 'system');
|
||||
|
||||
// C1: 合并 system 消息为一条,但保留不可压缩的 system 消息完整内容
|
||||
@@ -775,12 +804,11 @@ export async function compressWithLLM(
|
||||
...alreadyCompressed.filter(m => m.role === 'system').map(m => m.content || ''),
|
||||
].filter(Boolean).join('\n\n');
|
||||
|
||||
const nonSystemCompressed = alreadyCompressed.filter(m => m.role !== 'system');
|
||||
// P0-C1 修复:不再保留 nonSystemCompressed,旧摘要内容已合并到 summaryMsg 中
|
||||
|
||||
const result: OllamaMessage[] = [
|
||||
{ role: 'system', content: mergedSystemContent },
|
||||
...head,
|
||||
...nonSystemCompressed,
|
||||
summaryMsg,
|
||||
...tail
|
||||
];
|
||||
|
||||
Reference in New Issue
Block a user