读图压缩加无收益守卫:JPEG 重编码变大时不落盘、外链直指原图

- executor.readImageAsBlocks:sharp 产物 >= 原图时跳过 .imgcache 落盘,image_rel 指向原文件(与浏览器端压缩同款守卫,截图/线稿等低内容熵图不再越压越大)
- engine.mintImageUrl:image_rel 可能直指原文件后,签发公网链前补 realpath 复核(同 mint_public_link),防任务目录内指向外部的符号链接图片被签上公网
- smoke:vision 断言放宽为 image_rel 非空且文件存在(小尺寸测试图必然走无收益分支)
- ARCHITECTURE:read_file 条目同步守卫与 realpath 复核说明
This commit is contained in:
2026-09-21 22:01:37 +08:00
parent bca41f3c7e
commit 11716afdee
4 changed files with 21 additions and 8 deletions
+6 -1
View File
@@ -516,11 +516,16 @@ async function runLoopInner(sessionId, run) {
// 且不传 tools,模型只能输出摘要文本。
// 发送前为历史里的 image_ref 块现签公网外链(GET /p/<id>,同 mint_public_link):
// 每次请求重签,回合内多轮迭代/重试永不过期;DB 只存几十字符的 rel,base64 不进库不进请求体。
// base_url 未配/mint 异常时返回 null,buildMessages 降级为文字占位。
// base_url 未配/校验不过/mint 异常时返回 null,buildMessages 降级为文字占位。
// image_rel 可能是压缩无收益时直指的原文件:与 mint_public_link 同款 realpath 复核,
// 防任务目录里指向外部的符号链接图片被签上公网。
const mintImageUrl = (rel) => {
const pl = cfg.public_link || {};
if (!pl.base_url) return null;
try {
const dir = taskDirOf(sessionId);
const real = fs.realpathSync(resolveInTask(dir, rel));
if (real !== dir && !real.startsWith(dir + path.sep)) return null;
return pl.base_url + '/p/' + publicFiles.mint({ sessionId: sessionId, rel, ttlMs: pl.ttl_ms });
} catch (_) { return null; }
};
+8
View File
@@ -142,6 +142,7 @@ function outlineWrapper(outlineText) {
// 用 sharp 压缩(按 EXIF 摆正、长边 ≤2000px、JPEG 质量 80)并落盘 .imgcache/。
// 成功返回 {text, image_rel};产物文件名带源文件指纹(mtime+size),同图重复读复用同一份不增殖。
// 无收益守卫(与浏览器端压缩同款):重编码 JPEG 反而 ≥ 原图时不落盘,image_rel 直指原文件。
// sharp 缺失或解码失败时退回纯文本提示,不让整个工具回合报错。
async function readImageAsBlocks(ctx, abs, rel, size) {
let sharp;
@@ -153,6 +154,13 @@ async function readImageAsBlocks(ctx, abs, rel, size) {
.resize({ width: IMG_MAX_SIDE, height: IMG_MAX_SIDE, fit: 'inside', withoutEnlargement: true })
.jpeg({ quality: IMG_JPEG_QUALITY })
.toBuffer({ resolveWithObject: true });
if (r.data.length >= size) {
// 低内容熵图(截图/线稿/纯色)JPEG 重编码可能比原图还大:不产出缓存,外链直接指向原文件
return {
text: '[图片文件] ' + rel + '(' + size + ' 字节,压缩无收益,按原图回传)',
image_rel: rel,
};
}
let st = null;
try { st = await fsp.stat(abs); } catch (_) {}
const stem = path.basename(rel).replace(/\.[^.]+$/, '').replace(/[^\w.\-\u4e00-\u9fff]/g, '_').slice(0, 60);
+6 -6
View File
@@ -130,8 +130,8 @@ async function main() {
.map(m => { try { return JSON.parse(m.content); } catch (_) { return null; } })
.find(j => j && Array.isArray(j.__blocks__));
ok(!!imgTool && imgTool.__blocks__.some(b =>
b.type === 'image_ref' && String(b.rel).startsWith('.imgcache/')),
'tool 消息含压缩图的 image_ref 内容块(外链发送时现签,库里不存 base64)',
b.type === 'image_ref' && String(b.rel || '').length > 0),
'tool 消息含 image_ref 内容块(压缩产物或无收益直指原图;外链发送时现签,库里不存 base64)',
imgTool ? JSON.stringify(imgTool.__blocks__.map(b => b.type)) : '无多模态块');
const cacheInPanel = await api('GET', '/api/sessions/' + si.id + '/files');
ok(!cacheInPanel.some(f => String(f.filename).startsWith('.imgcache/')), '.imgcache 不进前端文件列表');
@@ -229,16 +229,16 @@ async function main() {
ok(fpath.includes('g.txt'), 'find_path 命中', fpath);
const esc = await executeTool(ctx, 'read_file', { path: '../outside.txt' }).catch(e => 'ERR:' + e.message);
ok(String(esc).includes('越界'), 'read_file 拒绝越界', String(esc));
// read_file 读图:非 vision → 文本提示;vision → {text, image_rel}(依赖 sharp,产物落 .imgcache/)
// read_file 读图:非 vision → 文本提示;vision → {text, image_rel}(依赖 sharp;小图压缩无收益时 image_rel 直指原文件,不落 .imgcache)
const unitPng = await require('sharp')({ create: { width: 4, height: 4, channels: 3, background: { r: 10, g: 120, b: 200 } } }).png().toBuffer();
fs.writeFileSync(path.join(ctx.taskDir, 'pic.png'), unitPng);
const imgNoVision = await executeTool(ctx, 'read_file', { path: 'pic.png' });
ok(typeof imgNoVision === 'string' && imgNoVision.includes('不支持视觉识别'), 'read_file 非 vision 图片返回文本提示', String(imgNoVision).slice(0, 80));
const ctxVision = Object.assign({}, ctx, { vision: true });
const imgVision = await executeTool(ctxVision, 'read_file', { path: 'pic.png' });
ok(imgVision && typeof imgVision === 'object' && String(imgVision.image_rel).startsWith('.imgcache/'),
'read_file vision 图片返回压缩产物 image_rel', imgVision && imgVision.text ? String(imgVision.text).slice(0, 90) : String(imgVision).slice(0, 90));
ok(fs.existsSync(path.join(ctx.taskDir, imgVision.image_rel)), '压缩产物已落盘 .imgcache/');
ok(imgVision && typeof imgVision === 'object' && String(imgVision.image_rel).length > 0,
'read_file vision 图片返回 image_rel(压缩产物或无收益原图)', imgVision && imgVision.text ? String(imgVision.text).slice(0, 90) : String(imgVision).slice(0, 90));
ok(fs.existsSync(path.join(ctx.taskDir, imgVision.image_rel)), 'image_rel 指向的文件存在');
// ---- 8. 配置读写 API(阶段 2)----
console.log('\n[8] 配置读写');
+1 -1
View File
@@ -283,7 +283,7 @@ stateDiagram-v2
| 工具 | 实现要点 |
|---|---|
| `read_file` | 走 `resolveInTask` 防路径穿越;图像(PNG/JPEG/WebP/GIF/BMP/TIFF)按 vision 能力走 `sharp` 压缩后落盘 `.imgcache/` 并回传 `image_rel`(长边 ≤ 2000px,JPEG q=80;文件名带源文件 mtime+size 指纹,重读复用不增殖),发送前由 `llm.buildMessages` 现签成公网外链;大文本/二进制给 outline;行号前缀格式 "`<6位右对齐行号>\\t<原文>`"(`edit_file` 用) |
| `read_file` | 走 `resolveInTask` 防路径穿越;图像(PNG/JPEG/WebP/GIF/BMP/TIFF)按 vision 能力走 `sharp` 压缩后落盘 `.imgcache/` 并回传 `image_rel`(长边 ≤ 2000px,JPEG q=80;文件名带源文件 mtime+size 指纹,重读复用不增殖;重编码无收益时不落盘、直指原文件),发送前由 `llm.buildMessages` 现签成公网外链(engine 侧 mint 前 realpath 复核);大文本/二进制给 outline;行号前缀格式 "`<6位右对齐行号>\\t<原文>`"(`edit_file` 用) |
| `write_file` / `edit_file` | `edit_file` 用模糊定位(Zed 风格):先精确匹配,失败后用 Levenshtein DP 找最相似行;`old_text`/`new_text` 协议见 `definitions.json` |
| `list_directory` / `create_directory` / `delete_path` / `move_path` / `copy_path` / `find_path` / `grep` | 标准文件操作,全部 `resolveInTask` 约束;`grep` 走 `ripgrep`(`rg`)优先 |
| `terminal` | **唯一一个走 executor 的内置工具**(当 `rexec.isRemote(cfg)`);本地分支用 `spawn` + `detached` + 进程组 kill;远端分支 `rexec.run` SSE 收流,结果套上超时/截断/取消注解后 `truncate(text, cfg.terminal.max_output_chars)` 返回 |