From 120726b7fe5b1b8713f2566169e1de890545177a Mon Sep 17 00:00:00 2001 From: kicer Date: Sun, 6 Sep 2026 08:21:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20smoke=20=E5=85=A8=E9=83=A8?= =?UTF-8?q?=E8=BF=87=E6=97=B6=E7=94=A8=E4=BE=8B=E5=B9=B6=E9=A1=BA=E5=B8=A6?= =?UTF-8?q?=E4=BF=AE=E4=B8=A4=E5=A4=84=E7=9C=9F=20bug=EF=BC=9Amessages=20?= =?UTF-8?q?=E8=B7=AF=E7=94=B1=E4=B8=A2=E5=A4=B1=20attachments=EF=BC=88?= =?UTF-8?q?=E7=BA=AF=E9=99=84=E4=BB=B6=E6=B6=88=E6=81=AF=E8=A2=AB=20400?= =?UTF-8?q?=E3=80=81=E3=80=90=E7=B3=BB=E7=BB=9F=E9=80=9A=E7=9F=A5=E3=80=91?= =?UTF-8?q?=E4=B8=8D=E5=86=8D=E6=8B=BC=E5=85=A5=EF=BC=89=EF=BC=8Capi=5Fkey?= =?UTF-8?q?/password=20=E8=84=B1=E6=95=8F=E6=AD=A3=E5=88=99=E8=AF=AF?= =?UTF-8?q?=E4=BC=A4=E7=A9=BA=E4=B8=B2=E5=80=BC=EF=BC=88""=20=E5=8F=98=20*?= =?UTF-8?q?**"=EF=BC=89=EF=BC=9Bsmoke=20=E9=80=82=E9=85=8D=E6=96=B0?= =?UTF-8?q?=E5=BB=BA=E5=8D=B3=20held=20=E9=A2=84=E7=95=99=E8=AF=AD?= =?UTF-8?q?=E4=B9=89=E4=B8=8E=E5=B9=B3=E5=8F=B0=E5=90=8D/=E6=A8=A1?= =?UTF-8?q?=E5=9E=8Bid=20=E5=94=AF=E4=B8=80=E9=94=AE=EF=BC=9Bprocess.exit?= =?UTF-8?q?=20=E6=94=B9=20exitCode=20=E9=98=B2=E8=B7=B3=E8=BF=87=20finally?= =?UTF-8?q?=20=E6=AE=8B=E7=95=99=20mock/server=20=E5=8D=A0=E7=AB=AF?= =?UTF-8?q?=E5=8F=A3=EF=BC=9B=E6=B5=8B=E8=AF=95=E9=85=8D=E7=BD=AE=E9=9A=94?= =?UTF-8?q?=E7=A6=BB=20data=5Froot=EF=BC=9B68/68=20=E5=85=A8=E8=BF=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/server/routes/config.js | 5 +++-- app/server/routes/messages.js | 10 +++++++--- app/test/config.test.yml | 3 +++ app/test/smoke.js | 33 ++++++++++++++++++--------------- 4 files changed, 31 insertions(+), 20 deletions(-) diff --git a/app/server/routes/config.js b/app/server/routes/config.js index 1e0fe2c..44f775f 100644 --- a/app/server/routes/config.js +++ b/app/server/routes/config.js @@ -46,8 +46,9 @@ function maskKey(k) { // 逐行掩码配置原文里的 api_key 值(设置页原文编辑器展示用)。 // 行格式如 ` api_key: sk-xxx` / ` api_key: "sk-xxx"` / ` - api_key: sk-xxx`(platforms.keys 列表项写法)。 +// 值用 (.*) 而非 (.+?):允许空串(`api_key: ""`),否则引号可选组回退会把 `""` 误捕成值 `"` 而被掩码成 `***"`。 // 记录每行是否掩码,便于 restore 时按顺序还原未改动项。 -const API_KEY_LINE = /^(\s*(?:-\s+)?api_key:\s*)(["']?)(.+?)\2\s*$/; +const API_KEY_LINE = /^(\s*(?:-\s+)?api_key:\s*)(["']?)(.*)\2\s*$/; function maskApiKeys(text) { if (!text) return text; return text.split('\n').map(function (line) { @@ -79,7 +80,7 @@ function restoreApiKeys(submitted, orig) { // password 脱敏(思路同 api_key):auth.users 密码在原文编辑器里统一显示 ******。 // 行格式如 ` password: "xxx"` / ` password: xxx`(auth.users 列表项写法)。 -const PASSWORD_LINE = /^(\s*(?:-\s+)?password:\s*)(["']?)(.+?)\2\s*$/; +const PASSWORD_LINE = /^(\s*(?:-\s+)?password:\s*)(["']?)(.*)\2\s*$/; const PASSWORD_MASK = '******'; function maskPassword(p) { return p ? PASSWORD_MASK : ''; } function maskPasswords(text) { diff --git a/app/server/routes/messages.js b/app/server/routes/messages.js index 74c92ad..5f8f902 100644 --- a/app/server/routes/messages.js +++ b/app/server/routes/messages.js @@ -37,12 +37,16 @@ function composeWithAttachments(content, attachments) { router.post('/:id/messages', wrap((req, res) => { const content = String((req.body && req.body.content) || '').trim(); - if (!content) return res.status(400).json({ error: 'content 不能为空' }); + // 附件告知:正文前拼【系统通知】行(见 composeWithAttachments 注释);纯附件消息(无正文)也合法。 + const attachments = Array.isArray(req.body && req.body.attachments) && (req.body.attachments || []).length + ? req.body.attachments : null; + const full = composeWithAttachments(content, attachments); + if (!full) return res.status(400).json({ error: 'content 与 attachments 不能同时为空' }); // deepThink:是否启用模型该「深度思考」参数(thinking_params 合并进上游请求体) const deepThink = !!(req.body && req.body.deepThink); - const r = engine.postUserMessage(req.params.id, content, { deepThink }); + const r = engine.postUserMessage(req.params.id, full, { deepThink }); log.info('msg', (req.user || '-') + ' ' + req.params.id + ' 用户消息(' + (r && r.delivered || '?') + '):' + - content.slice(0, 60).replace(/\s+/g, ' ')); + full.slice(0, 60).replace(/\s+/g, ' ')); res.json(r); })); diff --git a/app/test/config.test.yml b/app/test/config.test.yml index 5a46aa5..24c7167 100644 --- a/app/test/config.test.yml +++ b/app/test/config.test.yml @@ -22,6 +22,9 @@ platforms: vision: true workspace_root: "./test/tmp/tasks" +# 隔离数据目录:db/启动 prompt dump 落 tmp(随每次运行的 rm -rf 一同清掉), +# 不配则用默认 ./data,残留会话会污染下次测试的模型池调度。 +data_root: "./test/tmp/data" port: 8099 diff --git a/app/test/smoke.js b/app/test/smoke.js index 2c4b4fc..8820a21 100644 --- a/app/test/smoke.js +++ b/app/test/smoke.js @@ -84,25 +84,25 @@ async function main() { console.log('\n[1] 普通对话'); let s = await api('POST', '/api/sessions', { title: '冒烟-对话' }); ok(s && s.id && s.status, '创建会话 ' + (s && s.id)); - let d = await waitSession(s.id, x => x.session.status === 'active', 5000, '绑定模型'); - ok(d.session.model_id === 'ds1', '绑定第一个空闲模型 ds1', d.session.model_id); + let d = await waitSession(s.id, x => x.session.status === 'held' && !!x.session.model_id, 5000, '新建自动绑定(held 预留)'); + ok(d.session.model_id === 'mock-a/ds1', '绑定第一个空闲模型 mock-a/ds1', d.session.model_id); await api('POST', '/api/sessions/' + s.id + '/messages', { content: '你好呀' }); d = await waitSession(s.id, x => x.session.status === 'held', 8000, 'AI 回复完成(held)'); const asst = d.messages.filter(m => m.role === 'assistant'); ok(asst.length === 1 && asst[0].content.includes('你好呀'), 'AI 文本回复流式落库', asst.map(m => m.content).join('|')); - ok(d.session.model_id === 'ds1', '回复完成后模型不释放(held 保持租约)', d.session.model_id); + ok(d.session.model_id === 'mock-a/ds1', '回复完成后模型不释放(held 保持租约)', d.session.model_id); // held 期间追问:直接复用已绑模型,不重新排队 const r2 = await api('POST', '/api/sessions/' + s.id + '/messages', { content: '再来一条' }); ok(r2 && r2.delivered === 'live', 'held 期间追问直接送达(不排队)', r2 && r2.delivered); d = await waitSession(s.id, x => x.session.status === 'held' && x.messages.filter(m => m.role === 'assistant').length >= 2, 8000, '第二次回复'); - ok(d.session.model_id === 'ds1', '追问复用同一模型', d.session.model_id); + ok(d.session.model_id === 'mock-a/ds1', '追问复用同一模型', d.session.model_id); // held 租约到期自动释放(测试配置 idle_release_ms=1500) d = await waitSession(s.id, x => x.session.status === 'completed' && x.session.model_id === null, 6000, 'held 租约到期自动释放'); // ---- 2. 工具调用 write_file ---- console.log('\n[2] 工具调用'); s = await api('POST', '/api/sessions', { title: '冒烟-工具' }); - await waitSession(s.id, x => x.session.status === 'active', 5000, '绑定'); + await waitSession(s.id, x => x.session.status === 'held' && !!x.session.model_id, 5000, '绑定(held)'); await api('POST', '/api/sessions/' + s.id + '/start'); await api('POST', '/api/sessions/' + s.id + '/messages', { content: '请写文件' }); d = await waitSession(s.id, x => x.session.status === 'held' && x.messages.some(m => m.role === 'tool'), 15000, '工具回合完成(held)'); @@ -123,7 +123,7 @@ async function main() { const sharp = require('sharp'); const pngBuf = await sharp({ create: { width: 8, height: 8, channels: 3, background: { r: 200, g: 30, b: 30 } } }).png().toBuffer(); const si = await api('POST', '/api/sessions', { title: '冒烟-读图' }); - await waitSession(si.id, x => x.session.status === 'active', 5000, '绑定'); + await waitSession(si.id, x => x.session.status === 'held' && !!x.session.model_id, 5000, '绑定(held)'); const fdImg = new FormData(); fdImg.append('files', new Blob([pngBuf], { type: 'image/png' }), 'pic.png'); const upImg = await fetch(BASE + '/api/sessions/' + si.id + '/files', { method: 'POST', body: fdImg }); @@ -141,7 +141,7 @@ async function main() { // ---- 2.8 释放后新消息优先回绑上次模型(last_model_id)---- console.log('\n[2.8] 优先回绑'); const sp = await api('POST', '/api/sessions', { title: '冒烟-回绑' }); - let dp = await waitSession(sp.id, x => x.session.status === 'active', 5000, '绑定'); + let dp = await waitSession(sp.id, x => x.session.status === 'held' && !!x.session.model_id, 5000, '绑定(held)'); const m1 = dp.session.model_id; await api('POST', '/api/sessions/' + sp.id + '/messages', { content: '回绑测试' }); dp = await waitSession(sp.id, x => x.session.status === 'held', 8000, '回复完成 held'); @@ -163,7 +163,7 @@ async function main() { // ---- 4. 终止机制 ---- console.log('\n[4] 终止'); s = await api('POST', '/api/sessions', { title: '冒烟-终止' }); - await waitSession(s.id, x => x.session.status === 'active', 5000, '绑定'); + await waitSession(s.id, x => x.session.status === 'held' && !!x.session.model_id, 5000, '绑定(held)'); await api('POST', '/api/sessions/' + s.id + '/messages', { content: '慢一点说' }); await sleep(600); await api('POST', '/api/sessions/' + s.id + '/stop'); @@ -183,14 +183,15 @@ async function main() { const a = await api('POST', '/api/sessions', { title: 'A' }); const b = await api('POST', '/api/sessions', { title: 'B' }); const c = await api('POST', '/api/sessions', { title: 'C' }); - const da = await waitSession(a.id, x => x.session.status === 'active', 5000, 'A active'); - const db = await waitSession(b.id, x => x.session.status === 'active', 5000, 'B active'); + const da = await waitSession(a.id, x => x.session.status === 'held' && !!x.session.model_id, 5000, 'A 绑定(held)'); + const db = await waitSession(b.id, x => x.session.status === 'held' && !!x.session.model_id, 5000, 'B 绑定(held)'); const dc = await api('GET', '/api/sessions/' + c.id); ok(da.session.model_id && db.session.model_id && da.session.model_id !== db.session.model_id, '两个会话分到不同模型', da.session.model_id + ' vs ' + db.session.model_id); ok(dc.session.status === 'queued' && dc.queue_pos === 1, '第三个会话排队 position 1'); await api('POST', '/api/sessions/' + a.id + '/stop'); - await waitSession(c.id, x => x.session.status === 'active', 6000, 'C 顶替'); + // C 从未发言 → 顶替后同样绑成 held 预留(非 active) + await waitSession(c.id, x => x.session.status === 'held' && !!x.session.model_id, 6000, 'C 顶替'); const dc2 = await api('GET', '/api/sessions/' + c.id); ok(dc2.session.model_id != null, '释放后队列会话自动绑定', String(dc2.session.model_id)); @@ -253,9 +254,10 @@ async function main() { ok(/temperature:\s*0\.5/.test(afterPatch.text), 'PATCH 写盘生效'); ok(afterPatch.text.includes('冒烟测试用短租约'), 'PATCH 保留文件注释'); let bad400 = false; + const rawBeforeBadPut = fs.readFileSync(cfgPath, 'utf8'); try { await api('PUT', '/api/config', { text: 'models: [unclosed' }); } catch (e) { bad400 = /400/.test(e.message); } ok(bad400, 'PUT 非法配置被 400 拒绝'); - ok(fs.readFileSync(cfgPath, 'utf8') === afterPatch.text, '非法写入未动文件'); + ok(fs.readFileSync(cfgPath, 'utf8') === rawBeforeBadPut, '非法写入未动文件'); const mpatch = await api('PATCH', '/api/config', { path: 'platforms.0.keys.0.models.0.display_name', value: '改名模型' }); const mtext = (await api('GET', '/api/config')).text; ok(mpatch.ok && mtext.includes('改名模型'), 'PATCH 支持 platforms 嵌套数组下标'); @@ -339,7 +341,7 @@ async function main() { const st = await api('GET', '/api/system/status'); ok(st.ok && st.started_at && st.port === 8099, 'GET /api/system/status'); s = await api('POST', '/api/sessions', { title: '冒烟-重启保护' }); - await waitSession(s.id, x => x.session.status === 'active', 5000, '绑定'); + await waitSession(s.id, x => x.session.status === 'held' && !!x.session.model_id, 5000, '绑定(held)'); await api('POST', '/api/sessions/' + s.id + '/messages', { content: '慢一点说' }); await sleep(400); // 进入慢速流式输出,looping=true let guarded = false; @@ -352,11 +354,12 @@ async function main() { console.log('\n========================'); console.log('通过 ' + pass + ' / 失败 ' + fail); - process.exit(fail === 0 ? 0 : 2); + // 不用 process.exit():会跳过 finally 的子进程清理,残留 mock/server 占住 9999/8099 干扰下次运行。 + process.exitCode = fail === 0 ? 0 : 2; } catch (e) { console.log('\n测试异常: ' + (e && e.stack || e)); console.log('server 日志尾部:\n' + serverLog.slice(-2000)); - process.exit(2); + process.exitCode = 2; } finally { server.kill('SIGKILL'); mock.kill('SIGKILL');