diff --git a/app/server/engine.js b/app/server/engine.js index 89c5e29..88015e4 100644 --- a/app/server/engine.js +++ b/app/server/engine.js @@ -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); diff --git a/app/server/log.js b/app/server/log.js index 33ca9d0..42c1987 100644 --- a/app/server/log.js +++ b/app/server/log.js @@ -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); }, diff --git a/app/server/tools/definitions.json b/app/server/tools/definitions.json index e655fef..9742bbc 100644 --- a/app/server/tools/definitions.json +++ b/app/server/tools/definitions.json @@ -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": [ diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 22d76b8..af03ebf 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -296,7 +296,7 @@ stateDiagram-v2 - `lastIncludedId` 跟踪本轮已纳入模型请求的最新消息 id;流式期间到达的新 user 消息靠它在下一轮迭代时自动带上。 - **errAck 机制**:工具参数结构性错误(JSON 非法/缺必填)→ 整条 assistant 标 `type='errAck'`,关联 tool 回执一并丢弃,发请求时由 user 错误提示替代(`llm.buildMessages`)—— 防止模型模仿自己的坏样本死循环。 - **archived/summary 机制**(上下文压缩):`type='archived'` 的消息 UI 照常展示但不再发模型;`type='summary'` 是压缩后的"上下文起点"。压缩回合**不插入任何指令消息**:由 `engine.summarizeContext` 在内存 run 上标 `summarizing`,`runLoopInner` 据此走 `llm.compressMessages`:沿用默认 system prompt + 正常历史 messages,末尾追加一条 user(`server/templates/compress-prompt.txt` 原文,参考标题回合的拼法),且不传 tools;回合内首条无 tool_calls 的收尾回复即摘要。(`llm.buildMessages` 仍保留旧库 `summarize_request` 行的处理,仅为历史数据兼容。) -- **图片(多模态)**:`messages.content` 列是 TEXT,含图时存 `{"__blocks__":[content...]}` 哨兵 JSON。新版 `read_file` 读图:sharp 压缩产物落任务目录 `.imgcache/`(前端文件列表隐带点的项),库里只存 `image_ref` 轻量块;`buildMessages` 发送前经调用方传入的 `mintUrl` 回调现签成公网外链(同 `mint_public_link` 那套 `GET /p/`)成 `image_url` 块,base64 不进 DB 也不进请求体;签不到降级为文字占位。**图不塞在 tool 回执里发**(tool 角色带 `image_url` 是非标准形态,大量 OpenAI 兼容上游整批忽略或只认批次最后一张,模型看不到图会反复 read_file 补看成死循环):回执降级为纯文本,未消费的图统一并入该批回执之后的一条 `user` 消息,每张图前配一行对应回执文字标签;旧库/用户粘贴附件的 `data:` URL 块原样透传。图块**只保留最后一条 assistant 之后的**(`lastAssistant` 判定),之前的换成"已省略"文字描述(措辞不催促重读);engine 侧同回合同一张图只投递一次兑底(再读同图只回文字提示)。UI 渲染走带 cookie 的会话内下载路由,历史永不裂图。 +- **图片(多模态)**:`messages.content` 列是 TEXT,含图时存 `{"__blocks__":[content...]}` 哨兵 JSON。新版 `read_file` 读图:sharp 压缩产物落任务目录 `.imgcache/`(前端文件列表隐带点的项),库里只存 `image_ref` 轻量块;`buildMessages` 发送前经调用方传入的 `mintUrl` 回调现签成公网外链(同 `mint_public_link` 那套 `GET /p/`)成 `image_url` 块,base64 不进 DB 也不进请求体;签不到降级为文字占位。**图不塞在 tool 回执里发**(tool 角色带 `image_url` 是非标准形态,大量 OpenAI 兼容上游整批忽略或只认批次最后一张,模型看不到图会反复 read_file 补看成死循环):回执降级为纯文本,未消费的图统一并入该批回执之后的一条 `user` 消息,每张图前配一行对应回执文字标签;旧库/用户粘贴附件的 `data:` URL 块原样透传。图块**只保留最后一条 assistant 之后的**(`lastAssistant` 判定),之前的换成"已省略"文字描述(措辞不催促重读);**每次模型调用最多携带一张图**——改 user 合并形态后实测多图仍只认最后一张(上游硬限制,换形态绕不过),故 engine 按批次限流:一批里只放行第一张图,其余降级为文字让模型下一轮单独重读(逐轮收敛),叠加同回合同图只投递一次;发送前 `log.debug('req', 发送形态...)` 打每轮图块计数,一眼区分发送侧漏拼与上游吞图。UI 渲染走带 cookie 的会话内下载路由,历史永不裂图。 - **key 共享并发,无租约**:`model_pool.is_busy`/`current_session_id` 已废弃不再读写(旧库残留列,启动时清零);同一 key 可被多个会话同时绑定并发 looping(上游 key 本身支持多并发)。held 不再自动到期,顶部「保持中 · 模型」持续展示,唯一出口是手动「断开」或换绑。 - **task_brief(任务书锚点,advising→executing 切换)**:runLoopInner 工具批跑完后,若 advising 回合内有 `handoff` 成功返回(brief 已由 handler 原样落盘根 task.md 并记 files 表;批量末尾统一切,防父 assistant 先存档、后落库回执成孤儿行被上游拒收;同批多次 handoff 后写覆盖 last-wins),`enterExecuting` 当场重建上下文:全史标 `archived` → 插一条 `type='task_brief'` user 行(内容 = task.md 磁盘快照,非引用)→ phase='executing' + SSE `phase`/`status` 事件;下一迭代重读历史 + 重解 profile,天然切默认组合、同回合开工。压缩不碰 phase(task_brief 与其它历史一样会被后续压缩存档,漂移可控:压缩本就由用户主导)。 - **profile(prompt+tools 组合)**:顶层 `model_profiles` 定义多套组合,`platforms[].models[].profile` 引用(见 §5.8)——这是唯一的引用来源:运行期手动切换已全部移除(消息级 profile 参数、`sessions.profile` 会话绑定均废弃,列保留不读写);`advising` 例外,不走引用、由 phase 强制命中。名单外工具被调用不拦(`checkToolArgs` 查不到 def 不报错,`executeTool` 全表照跑),但阶段契约类例外:advising 内 `write_file`/`edit_file` 在批内挡下、`handoff` 在 handler 里带 phase 守卫;tools schema 只活在请求体不进历史,所以切换不脏化旧消息。压缩回合同样跟随(小模型会话点压缩不会回退到默认大模板)。 @@ -767,7 +767,7 @@ sequenceDiagram 4. **session.id 序号不复用** —— `genTaskId` 取历史最大 + `meta.task_seq` 取大;删会话后序号不回收。 5. **held 的语义** —— held = 「回合已收尾,绑定持续展示」,无排队/租约含义、不会自动到期;completed = 手动「断开」,cancelled = 手动终止;三态 UI 仍需区分,但都不影响模型可用性(key 共享)。 6. **errAck 误判** —— 弱模型会把参数多包一层 `{"arguments":{...}}`,`unwrapToolArgs` 自动解包;`checkToolArgs` 只看 required 字段是否在顶层。 -7. **图片上下文省钱与防循环** —— `lastAssistant` 之前的图发请求时被换成文字描述(DB 存全量,UI 历史重渲染仍能看图;要看原图可 `read_file` 重读,新回执会重新投递)。图片必须以批次后 `user` 合并消息形态投递 + 同回合同图去重:tool 回执夹图会被兼容差的上游静默吞掉,催读文案再火上浇油,模型会一轮轮重读同一批图烧 token(线上一次死循环事故的教训)。 +7. **图片上下文省钱与防循环** —— `lastAssistant` 之前的图发请求时被换成文字描述(DB 存全量,UI 历史重渲染仍能看图;要看原图可 `read_file` 重读,新回执会重新投递)。两条铁律(线上死循环实测复盘):① tool 回执夹图会被兼容差的上游静默吞掉,必须走批次后 `user` 合并消息;② 改成 user 形态后多图仍只认最后一张(上游硬限制),所以每次模型调用最多携带一张图(engine 按批限流、同回合同图去重),一切"请重新读取"式催读文案必须删掉。发送形态看 `log.debug('req', 发送形态...)`。 8. **SSE 被 nginx-proxy 缓冲** —— 必须 `proxy_buffering off`;否则 `assistant_delta` 攒到响应结束才一次性下发,体感卡死。 9. **cookie 兜底(APK 场景)** —— WebView 杀进程后 cookie 不一定落盘,登录响应回 `token` 让前端存 localStorage,下次冷启动调 `/api/login_restore` 恢复。 10. **MCP session 过期** —— 第一次 `tools/call` 可能 400 + 含 "session",`mcp.js` 自动重 initialize 后重试一次;日志会打 warn。