多模态投递限流:每次模型调用最多携带一张图(多图上游只认最后一张,user 合并形态实测仍翻车)——engine 工具批内第一张图正常投递、其余降级为「下一轮单独重读」文字提示,逐轮收敛;同回合同图去重护栏保留(挡已投递图的反复重读)。发送形态 debug 日志(log.js 加 isDebugEnabled):只打角色/块数/image 计数/降级标记,不打 URL 与内容,「图没到模型」时一眼区分发送侧漏拼与上游吞图。definitions.json read_file 说明、ARCHITECTURE 图片链路与陷阱 7 同步两条铁律(图走批次后 user 合并 + 每轮至多一张,均系线上死循环实测复盘)

This commit is contained in:
2026-09-28 14:19:47 +08:00
parent e1bee18cd0
commit a250d57d10
4 changed files with 31 additions and 6 deletions
+27 -3
View File
@@ -553,6 +553,23 @@ async function runLoopInner(sessionId, run) {
const messages = compressing
? compressMessages(session, (cfg.agent || {}).language, mintImageUrl, profileName)
: buildMessages(session, (cfg.agent || {}).language, mintImageUrl, profileName);
// 发送形态摘要(debug):只打角色/块类型/数量,不打 URL 与内容;定位「图没到模型」时
// 一眼区分发送侧没拼上图(image 计数为 0)与上游吞图(计数正确但模型说没看到)。
if (log.isDebugEnabled && log.isDebugEnabled()) {
log.debug('req', sessionId + ' 发送形态: ' + messages.map(m => {
if (Array.isArray(m.content)) {
const imgs = m.content.filter(b => b && b.type === 'image_url').length;
const unmin = m.content.filter(b => b && b.type === 'image_ref').length;
const dead = m.content.filter(b => b && b.type === 'text' && String(b.text || '').indexOf('[图片外链不可用') === 0).length;
return m.role + '(块 ' + m.content.length + (imgs ? ', image ' + imgs : '') + (unmin ? ', 漏签 ' + unmin : '') + (dead ? ', 断链 ' + dead : '') + ')';
}
const txt = String(m.content || '');
return m.role + (m.tool_calls ? '[calls]' : '')
+ (txt.includes('图片数据已省略') ? '[图已省]' : '')
+ (txt.includes('每轮只能携带一张图片') ? '[图被限流]' : '')
+ (txt.includes('本回合已投递过') ? '[图重复读挡回]' : '');
}).join(' '));
}
// 内置工具 + 启动时发现的 MCP 工具(按 profile 白名单过滤),一起随 system prompt 发给模型(压缩回合不发)
const tools = compressing ? [] : effectiveTools(profileName);
@@ -660,6 +677,8 @@ async function runLoopInner(sessionId, run) {
// 执行工具
const vision = modelVision(session);
let handoffDone = false; // 本批工具里是否有 handoff 成功返回(批量跑完统一切换,见循环尾注释)
// 本批(=本次模型调用可见范围)是否已放行过一张图;随 while 每轮迭代重置。
let imgThisTurn = false;
for (const tc of msgRow.tool_calls) {
if (run.cancelled) break;
const id = tc.id;
@@ -701,16 +720,21 @@ async function runLoopInner(sessionId, run) {
outText = String(output == null ? '' : output);
}
if (run.cancelled && !isError) outText = (outText ? outText + '\n' : '') + '(用户已终止任务)';
// 死循环护栏:同一回合内同一张图只投递一次——若上游没认图,模型会一轮轮重读同一文件、
// 每轮都签新图烧 token;同回合再读同图只回文字提示,不再携带图片(run 随回合结束销毁,
// 新回合/新用户消息自动重置额度,正常追问重看不受影响)。
// 死循环护栏(实测:多图同时进上下文时部分上游只认一张;叠加催读文案会让模型整轮重读同一批图、
// 每轮都签新图烧 token 直到迭代耗尽):
// ① 每次模型调用最多携带一张图:同批后续图回执降级为文字,让模型下一轮单独再读,逐轮收敛;
// ② 同回合同一张图只投递一次:已投递过的再读只回文字提示(run 随回合销毁,新回合自动重置额度)。
if (imageRel) {
if (!run.imgDelivered) run.imgDelivered = new Set();
if (run.imgDelivered.has(imageRel)) {
outText += '\n[该图片本回合已投递过,不再重复携带;若你未看到它,请直接告知用户,勿反复重读同一文件]';
imageRel = null;
} else if (imgThisTurn) {
outText += '\n[每轮只能携带一张图片,本张未投递:请在下一轮单独 read_file 本文件查看]';
imageRel = null;
} else {
run.imgDelivered.add(imageRel);
imgThisTurn = true;
}
}
log.debug('tool', sessionId + ' ' + tc.function.name + (isError ? ' 失败: ' : ' 完成: ') + outText);
+1
View File
@@ -35,6 +35,7 @@ function emit(fn, level, args) {
module.exports = {
setLevel,
isDebugEnabled() { return LEVELS.debug >= threshold; },
debug() { emit(console.log, 'debug', arguments); },
info() { emit(console.log, 'info', arguments); },
warn() { emit(console.warn, 'warn', arguments); },
+1 -1
View File
@@ -202,7 +202,7 @@
"type": "function",
"function": {
"name": "read_file",
"description": "Reads the content of a file.\n\nText files: each returned line is prefixed with a line number right-aligned in a 6-character field, followed by a tab, then the line content.\n\nLarge text files (roughly over 16 KB) are not returned in full: you get an outline listing top-level symbols (or Markdown headings) with line numbers instead. The outline IS a successful response — do NOT retry the same call without a line range; use the outline line numbers with `start_line` / `end_line` to read specific sections. If no outline can be extracted, a first-1KB excerpt is returned instead. Supplying `start_line` or `end_line` disables outline mode.\n\nImage files (PNG, JPEG, WebP, GIF, BMP, TIFF) are returned as visual content you can analyze directly, but only if the current model supports vision; otherwise a text placeholder is returned — that placeholder is not an error. The tool result itself carries a text notice only; the actual image pixels arrive in a separate user message right after the batch of tool results, each image preceded by a label naming the read_file result it belongs to. Re-reading the same image within one turn returns a text-only notice without pixels — if an image never arrives, tell the user instead of retrying the read. Very large images (roughly over 50 MB) are skipped, and images may be downscaled (long edge ~2000 px, re-encoded as JPEG) before being returned.\n\nBinary files are not returned as text; a short notice is returned instead. Text files over 32 MB cannot be read by this tool — use `terminal` (grep -n / head / sed -n).",
"description": "Reads the content of a file.\n\nText files: each returned line is prefixed with a line number right-aligned in a 6-character field, followed by a tab, then the line content.\n\nLarge text files (roughly over 16 KB) are not returned in full: you get an outline listing top-level symbols (or Markdown headings) with line numbers instead. The outline IS a successful response — do NOT retry the same call without a line range; use the outline line numbers with `start_line` / `end_line` to read specific sections. If no outline can be extracted, a first-1KB excerpt is returned instead. Supplying `start_line` or `end_line` disables outline mode.\n\nImage files (PNG, JPEG, WebP, GIF, BMP, TIFF) are returned as visual content you can analyze directly, but only if the current model supports vision; otherwise a text placeholder is returned — that placeholder is not an error. The tool result itself carries a text notice only; the actual image pixels arrive in a separate user message right after the batch of tool results, each image preceded by a label naming the read_file result it belongs to. At most ONE image arrives per round: when several images are read in one batch only the first carries pixels and the rest come back as text asking you to read_file them individually next round — so read images one at a time when you need several, never re-read an image already delivered in this turn, and if pixels never arrive tell the user instead of retrying. Very large images (roughly over 50 MB) are skipped, and images may be downscaled (long edge ~2000 px, re-encoded as JPEG) before being returned.\n\nBinary files are not returned as text; a short notice is returned instead. Text files over 32 MB cannot be read by this tool — use `terminal` (grep -n / head / sed -n).",
"parameters": {
"type": "object",
"required": [