Files
agent/app/test/trim-smoke.js
T
kicer d0d7b838e9 style(llm,memory): 模型侧文案 AI 友好化——占位改键值记号,记忆/画像头删人读整句
- 重负载占位:[OMITTED, http_status=200, size=6465] / [OMITTED, size=12000];
  url/path 不再重复进占位——发起调用的 assistant 行里本就有,参数即恢复指针
- memory.journalFor 注入头置空后整体移除(head 恒空,顺带消开头空行残留)
- user-profile 快照头去掉「· 背景参考」后缀
- trim-smoke 断言同步键值格式
2026-10-02 09:27:15 +08:00

65 lines
4.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 临时冒烟:重负载占位裁剪(跑完即删)。
const assert = require('assert');
const llm = require('../server/llm');
const big = n => 'x'.repeat(n);
const mk = (id, role, extra) => Object.assign({ id, role, type: null, content: '' }, extra);
const call = (id, name, args) => ({ id, type: 'function', function: { name, arguments: JSON.stringify(args) } });
const rows = [
mk(1, 'user', { content: '抓X存notes再写summary' }),
mk(2, 'assistant', { tool_calls: JSON.stringify([call('c1', 'fetch', { url: 'https://e.com/x' })]) }),
mk(3, 'tool', { tool_call_id: 'c1', name: 'fetch', content: 'HTTP 200\n' + big(8000) }),
mk(4, 'assistant', { tool_calls: JSON.stringify([call('c2', 'write_file', { path: 'notes.md', content: big(12000) })]) }),
mk(5, 'tool', { tool_call_id: 'c2', name: 'write_file', content: '已创建文件 notes.md(12003 字节)' }),
mk(6, 'assistant', { tool_calls: JSON.stringify([call('c3', 'write_file', { path: 'summary.md', content: '小内容' })]) }),
mk(7, 'tool', { tool_call_id: 'c3', name: 'write_file', content: '已创建文件 summary.md(9 字节)' }),
mk(8, 'assistant', { content: '两篇已写好' }),
];
const session = () => ({ history: rows.slice(), profile_snapshot: '', blocksCache: new Map() });
// A) 全历史:距尾够远 → fetch 回执与 write_file(大) 参数被占位;write_file(小)/回执/收尾不动
let msgs = llm.buildMessages(session(), 'Simplified Chinese', null, null);
const a = msgs.filter(m => m.role === 'assistant' && m.tool_calls);
const argsNotes = JSON.parse(a.find(m => m.tool_calls[0].id === 'c2').tool_calls[0].function.arguments);
assert(argsNotes.content === '[OMITTED, size=12000]', 'write_file 大参数应占位: ' + argsNotes.content);
const argsSum = JSON.parse(a.find(m => m.tool_calls[0].id === 'c3').tool_calls[0].function.arguments);
assert.strictEqual(argsSum.content, '小内容', '低于 TRIM_MIN 不裁');
const t = msgs.find(m => m.role === 'tool' && m.tool_call_id === 'c1');
assert(t.content === '[OMITTED, http_status=200, size=8009]', 'fetch 回执应占位: ' + t.content);
assert(msgs.find(m => m.role === 'tool' && m.tool_call_id === 'c2').content.includes('已创建文件'), 'write 回执不动');
// B) 截到第 5 行(write 刚落盘的当次调用):都太新鲜,不裁
msgs = llm.buildMessages({ history: rows.slice(0, 5), profile_snapshot: '', blocksCache: new Map() }, 'Simplified Chinese', null, null);
assert(msgs.find(m => m.role === 'tool' && m.tool_call_id === 'c1').content.length > 7000, 'fetch 距尾 1 轮应保留');
assert.strictEqual(JSON.parse(msgs.filter(m => m.tool_calls).pop().tool_calls[0].function.arguments).content.length, 12000, 'write 参数距尾 0 轮应保留');
// C) 压缩回合不裁
const cm = llm.compressMessages(session(), 'Simplified Chinese', null, null);
assert(cm.find(m => m.role === 'tool' && m.tool_call_id === 'c1').content.length > 7000, '压缩回合保留原文');
// D) read_file 回执同款
const r3orig = rows[2];
rows[2] = mk(3, 'tool', { tool_call_id: 'c1', name: 'read_file', content: 'HTTP 200\n' + big(8000) });
rows[1] = mk(2, 'assistant', { tool_calls: JSON.stringify([call('c1', 'read_file', { path: 'a.md' })]) });
msgs = llm.buildMessages(session(), 'Simplified Chinese', null, null);
assert(msgs.find(m => m.tool_call_id === 'c1' && m.role === 'tool').content === '[OMITTED, size=8009]', 'read_file 应占位');
rows[2] = r3orig;
rows[1] = mk(2, 'assistant', { tool_calls: JSON.stringify([call('c1', 'fetch', { url: 'https://e.com/x' })]) });
// E) 开关:0=关
llm.setPayloadTrim({ trim_write_rounds: 0, trim_result_rounds: 0 });
msgs = llm.buildMessages(session(), 'Simplified Chinese', null, null);
assert(msgs.find(m => m.role === 'tool' && m.tool_call_id === 'c1').content.length > 7000, '0=关');
llm.setPayloadTrim({}); // 回落默认 1/2
// F) 非法/缺失参数不炸(arguments 非 JSON、无 url 的 fetch;需距尾够远)
rows.push(mk(9, 'assistant', { tool_calls: JSON.stringify([call('c9', 'fetch', {})]) }));
rows.push(mk(10, 'tool', { tool_call_id: 'c9', name: 'fetch', content: 'HTTP 404\n' + big(700) }));
rows.push(mk(11, 'assistant', { content: '页面不存在,换个 url?' }));
rows.push(mk(12, 'assistant', { tool_calls: JSON.stringify([call('c12', 'read_file', { path: 'notes.md' })]) }));
msgs = llm.buildMessages(session(), 'Simplified Chinese', null, null);
assert(msgs.find(m => m.tool_call_id === 'c9').content === '[OMITTED, http_status=404, size=709]', '缺 url 仍占位: ' + msgs.find(m => m.tool_call_id === 'c9').content);
console.log('TRIM-SMOKE-OK');