feat: 预算警告 & Shadowing 防护 & 记忆容量管理 (v5.1.2)

- 预算警告临时层:Agent Loop 剩余 ≤5 轮注入 WARNING,≤2 轮注入 CRITICAL
- MCP Shadowing 防护:MCP 工具注册时检测重名,自动跳过并警告
- 记忆容量限制:上限 500 条,超限自动清理低价值条目(rule 受保护)
- 版本号更新至 5.1.2
This commit is contained in:
Metona Build
2026-04-19 18:10:26 +08:00
parent bea52bf1b7
commit 71bd6e19ea
10 changed files with 114 additions and 10 deletions
+20
View File
@@ -529,6 +529,17 @@ export async function runAgentLoop(
const toolCalls: ToolCall[] = [];
allCallsThisLoop = [];
// v5.1.2 预算警告:接近迭代上限时注入临时提示
let budgetWarningIdx = -1;
const remaining = maxLoops - loopCount + 1;
if (remaining <= 5 && remaining > 0) {
const warning = remaining <= 2
? `\n⚠️ CRITICAL: You have only ${remaining} iteration(s) left. Stop using tools and provide your final answer NOW.`
: `\n⚠️ WARNING: You have approximately ${remaining} iterations remaining. Start wrapping up and prepare your final answer.`;
messages.push({ role: 'system', content: warning });
budgetWarningIdx = messages.length - 1;
}
const abortController = new AbortController();
state.set(KEYS.ABORT_CONTROLLER, abortController);
@@ -587,6 +598,10 @@ export async function runAgentLoop(
return;
}
logError('流式调用异常', (err as Error).message);
// 清理预算警告临时消息
if (budgetWarningIdx >= 0) {
messages.splice(budgetWarningIdx, 1);
}
if (content || thinking) {
messages.push({ role: 'assistant', content: content || '(模型响应异常)', ...(thinking && { thinking }) });
}
@@ -594,6 +609,11 @@ export async function runAgentLoop(
return;
}
// 清理预算警告临时消息
if (budgetWarningIdx >= 0) {
messages.splice(budgetWarningIdx, 1);
}
// 提取 ReAct 思考过程
const thoughtMatch = content.match(/\*\*Thought:\*\*\s*([\s\S]*?)(?=\*\*Action:\*\*|\*\*Final Answer:\*\*|$)/i);
const thought = thoughtMatch ? thoughtMatch[1].trim() : '';