AI 坏 tool call 标记 errAck:入库留痕但移出模型上下文,防模仿死循环

- messages 新增 type 列,参数非法(JSON 坏/缺必填)的 assistant 轮标 errAck
- buildMessages 跳过 errAck 行及其关联 tool 回执,改发一条 user 错误提示引导重新生成
- tool_calls 落库时补全缺失 id,保证与 tool 回执配对一致
- safeParse 升级为 checkToolArgs:区分非法 JSON 与缺必填,执行前统一挡下
This commit is contained in:
2026-09-05 08:55:31 +08:00
parent 9e583afaf5
commit 848937c156
3 changed files with 75 additions and 18 deletions
+3
View File
@@ -41,6 +41,9 @@ function initDb(dbPath) {
`);
// 升级老库:早期表不带 reasoning 列,补上(重入安全;不在上面 db.exec 模板串里,免得被当作 SQL 解析)
try { db.exec('ALTER TABLE messages ADD COLUMN reasoning TEXT'); } catch (_) { /* 已存在 */ }
// 升级老库:type 标记特殊消息行。目前唯一取值 'errAck'——AI 的 tool call 参数错误
// (JSON 非法/缺必填)整条标记;仍入库供 UI 展示,但 buildMessages 发请求时跳过(见 llm.js)
try { db.exec('ALTER TABLE messages ADD COLUMN type TEXT'); } catch (_) { /* 已存在 */ }
db.exec(`
CREATE TABLE IF NOT EXISTS files (
+41 -17
View File
@@ -75,10 +75,10 @@ function loadHistory(sessionId) {
function saveMessage(sessionId, msg) {
const info = db.prepare(
'INSERT INTO messages (session_id, role, content, reasoning, tool_calls, tool_call_id, name, created_at) VALUES (?,?,?,?,?,?,?,?)'
'INSERT INTO messages (session_id, role, content, reasoning, tool_calls, tool_call_id, name, type, created_at) VALUES (?,?,?,?,?,?,?,?,?)'
).run(sessionId, msg.role, msg.content || null, msg.reasoning || null,
msg.tool_calls ? JSON.stringify(msg.tool_calls) : null,
msg.tool_call_id || null, msg.name || null, now());
msg.tool_call_id || null, msg.name || null, msg.type || null, now());
// 关键动作日志:AI 每轮回复落库(含纯工具调用轮)
if (msg.role === 'assistant') {
const calls = msg.tool_calls || [];
@@ -466,18 +466,30 @@ async function runLoopInner(sessionId, run) {
// 保存 assistant 消息
const hasTools = result.tool_calls && result.tool_calls.length > 0;
// 参数校验预扫描:任一 tool call 结构性错误(JSON 非法/缺必填)→ 整条标 errAck。
// 该行仍入库(UI 可见可排查),但 buildMessages 发请求时连同关联 tool 回执整轮跳过,
// 改以一条 user 错误提示替代,防止模型模仿自己的坏样本反复出错死循环。
let isErrAck = false;
if (hasTools) {
for (const tc of result.tool_calls) {
if (checkToolArgs(tools, tc.name, tc.arguments).error) { isErrAck = true; break; }
}
}
const msgRow = {
role: 'assistant',
type: isErrAck ? 'errAck' : null,
content: result.content || '',
reasoning: result.reasoning || null,
tool_calls: hasTools ? result.tool_calls.map(tc => ({
id: tc.id, type: 'function',
// 缺 id 时补全,保证与 tool 回执落库用的 id 一致(buildMessages 按 id 配对丢弃)
id: tc.id || ('call_' + Math.random().toString(36).slice(2, 10)), type: 'function',
function: { name: tc.name, arguments: tc.arguments },
})) : null,
};
const assistantId = saveMessage(sessionId, msgRow);
if (isErrAck) log.info('[msg] ' + sessionId + ' 本轮 tool call 参数错误,assistant 标记 errAck(不再进入模型上下文)');
recordModelUsage(sessionId, session.model_id, result.usage);
bus.emit(sessionId, 'assistant_message', { id: assistantId, content: msgRow.content, reasoning: msgRow.reasoning, tool_calls: msgRow.tool_calls });
bus.emit(sessionId, 'assistant_message', { id: assistantId, content: msgRow.content, reasoning: msgRow.reasoning, tool_calls: msgRow.tool_calls, type: msgRow.type });
if (!hasTools) {
// 回合结束;但若期间用户又发了新消息(live 投递),继续下一轮而不是收尾
@@ -489,26 +501,21 @@ async function runLoopInner(sessionId, run) {
const vision = modelVision(session);
for (const tc of msgRow.tool_calls) {
if (run.cancelled) break;
const id = tc.id || ('call_' + Math.random().toString(36).slice(2, 10));
const id = tc.id;
log.debug('[tool] ' + sessionId + ' 调用 ' + tc.function.name + ' 参数: ' + String(tc.function.arguments || ''));
bus.emit(sessionId, 'tool_call', { id, name: tc.function.name, arguments: tc.function.arguments });
let output;
let isError = false;
const args = safeParse(tc.function.arguments) || {};
// 必填参数校验:解包后仍缺参就直接挡下,绝不把 undefined 交给 shell 执行
const def = tools.find(t => t && t.function && t.function.name === tc.function.name);
const required = def && def.function.parameters && def.function.parameters.required;
const missing = required && required.length
? required.filter(k => args[k] === undefined || args[k] === '') : [];
if (missing.length) {
// 参数校验:解包后仍非法(JSON 不合法/缺必填)就直接挡下,绝不把 undefined 交给 shell 执行
const chk = checkToolArgs(tools, tc.function.name, tc.function.arguments);
if (chk.error) {
// 把正确形状告诉模型,替它纠正包装习惯
output = 'Error: 缺少必填参数:' + missing.join('、')
+ '。参数需直接按工具 schema 在顶层传递,例如 {"' + missing[0] + '": "..."},不要再包 arguments/function 外层。';
output = chk.error;
isError = true;
} else {
try {
output = await executeTool({ sessionId, taskDir: taskDirOf(sessionId), run, cfg, db, vision },
tc.function.name, args);
tc.function.name, chk.args);
} catch (err) {
output = 'Error: ' + (err && err.message || String(err));
isError = true;
@@ -618,8 +625,25 @@ function hasUserSince(sessionId, sinceId) {
return r.c > 0;
}
function safeParse(s) {
try { return JSON.parse(s || '{}'); } catch (_) { return {}; }
// 校验工具调用参数,返回 { args, error }。error 非空 = 结构性错误(JSON 非法/缺必填):
// 执行挡下,且整条 assistant 标 errAck,发请求时整轮移出模型上下文(见 llm.buildMessages),
// 防止模型模仿自己的坏样本反复出错死循环。args 为空串视为空对象(无参工具正常执行)。
function checkToolArgs(tools, name, raw) {
const s = String(raw == null ? '' : raw).trim();
let args;
if (!s) args = {};
else {
try { args = JSON.parse(s); }
catch (_) {
return { args: null, error: 'Error: ToolCall Args Invalid:arguments 不是合法 JSON,请重新生成该调用。原文:' + s.slice(0, 300) };
}
}
const def = (tools || []).find(t => t && t.function && t.function.name === name);
const required = def && def.function.parameters && def.function.parameters.required;
const missing = required && required.length
? required.filter(k => args[k] === undefined || args[k] === '') : [];
if (missing.length) return { args: null, error: 'Error: ToolCall Missing Params:' + missing.join('、') };
return { args, error: null };
}
function toolSchemaOf(tools, name) {
+31 -1
View File
@@ -40,21 +40,51 @@ function decodeBlocks(content) {
// 省 token:image_url 块仅在其位于最后一条 assistant 消息之后(模型尚未对它回复)时保留;
// 之前的视为已消费,发请求时替换为文字标记。DB 始终存全量:UI 历史照常渲染图片,
// 模型如需重看可再次 read_file(新消息在尾部,会重新携带图片)。
// errAck 行(AI tool call 参数错误,见 engine.checkToolArgs):整条不发给模型,
// 关联 tool 回执按 id 一并跳过(否则会留下无 tool_call 配对的孤儿 tool 消息,上游拒收);
// 改以一条 user 错误提示替代,让模型看到原因后重新生成工具调用 ——
// 坏样本留在上下文里只会被模型继续模仿,反复报同一个错。
function buildMessages(session, language) {
const messages = [{ role: 'system', content: systemPromptText(language) }];
const history = session.history;
// tool 回执按 tool_call_id 索引:给 errAck 行拼错误提示时取执行时挡下的原因
const toolRowById = new Map();
for (const row of history) if (row.role === 'tool' && row.tool_call_id) toolRowById.set(row.tool_call_id, row);
let lastAssistant = -1;
for (let i = history.length - 1; i >= 0; i--) {
if (history[i].role === 'assistant') { lastAssistant = i; break; }
// errAck 不发模型,不算「模型已回复」:它之前的图不算被消费,下次仍携带
if (history[i].role === 'assistant' && history[i].type !== 'errAck') { lastAssistant = i; break; }
}
const dropToolCallIds = new Set();
history.forEach((row, i) => {
if (row.role === 'assistant') {
if (row.type === 'errAck') {
let calls = [];
try { calls = JSON.parse(row.tool_calls || '[]') || []; } catch (_) {}
const errs = [];
for (const c of calls) {
if (!c || !c.id) continue;
dropToolCallIds.add(c.id);
const tr = toolRowById.get(c.id);
const txt = tr && typeof tr.content === 'string' ? tr.content : '';
if (txt.startsWith('Error:')) errs.push(txt);
}
messages.push({
role: 'user',
content: '[系统] 你上一轮的 tool call 参数有误,未执行并已从对话历史中移除。错误详情:\n' +
(errs.join('\n') || 'Error: ToolCall Invalid Params') +
'\n请严格按工具参数定义重新生成正确的调用。',
});
return;
}
const m = { role: 'assistant', content: row.content || '' };
if (row.tool_calls) m.tool_calls = JSON.parse(row.tool_calls);
if (!m.content) delete m.content;
messages.push(m);
return;
}
// errAck 轮的 tool 回执:随该轮一起丢弃,避免无主 tool 消息
if (row.role === 'tool' && dropToolCallIds.has(row.tool_call_id)) return;
// tool/user 行:含图哨兵 JSON 还原为内容块;已消费的图剥掉 base64,只留文字描述 + 标记
const blocks = decodeBlocks(row.content);
let content = row.content || '';