P1 修复面收口: Prompt Cache 根治(日期/记忆/附件三类易变内容出 system 入用户消息 前置块 user-context.ts, system 跨 run 字节级稳定; Anthropic system 块数组化 + cache_control ephemeral 断言, DeepSeek 自动缓存前缀命中 — 多轮对话输入 token 成本降数量级); 编辑重发/重新生成幽灵 Trace 双侧根治(DB truncateMessagesAfter 同步过滤 metadata.traceSteps + 前端 trimTraceStepsByAnchor 镜像, 严格小于锚点 时间戳, 同毫秒等值判废); sessions:deleteMessage 死通道全链路删除(渲染层零调用 + message_count 漂移面); Ollama vision 能力门控全链路(MetonaModelInfo .supportsVision 贯穿 adapter/IPC/store/UI, model-capabilities.ts 三道判定纯函数, 未知保守放行); 记忆固化节流(consolidation-policy 纯函数: 总开关 + 内容门控 [回答>=200字符或存在成功工具调用] + 会话级 10 分钟频率窗口, 三 memory.* 配置键) P2 安全纵深: SSRF DNS Pinning 关闭 rebinding 窗口(ssrf-guard 重构 resolvePublicAddresses 单源; ssrf-dispatcher 以 undici Agent.connect.lookup 钉死校验 IP, TLS SNI 保持原域名, 一次性 dispatcher 用后即毁; 代理激活显式 退化为仅入口校验); web_fetch 重写手动逐跳重定向循环(每跳先校验后连接, 替代 redirect:follow 内核跟跳的中间跳裸奔, 上限 5 跳); http_request 换用 pinned fetch; web_search 可达性预检加固(私有 URL 零请求 + 不跟跳, 3xx 视为 可达); Agent 浏览器 CORS 通配收紧为 Origin 回显 + Vary: Origin; ConfirmationHook.forgetSession 会话终态清理(会话删除/abort 联动/SubAgent 终结三处接线, 根治 rememberedDecisions 泄漏) P3 架构还债: agent.enableReflection 死配置全链路接线(main→shared→引擎→ Orchestrator→设置开关, REFLECTING 状态真实可达); AgentLoopConfig.timeoutMs 死字段删除; MemoryManager.cleanupExpired 挂入健康检查周期(expires_at 回收 管道真实化); buildSafeEnv 收敛 utils/safe-env.ts 单源(run_command 与 MCP stdio 共用, 终结双实现漂移); Trace 生命周期治理(metadata 只保留最近 20 个 run — keepRecentRuns 纯函数; JSONL 录制启动自动清理保留 200 个 + 设置页 手动清理); SLO/健康快照可视化(app:healthSnapshot IPC + 设置页只读卡片 + 审计链一键校验) P4 能力演进: 会话标题 LLM 自动生成(TitleGenerator — 每会话幂等/并发重入复用 同一 Promise/自定义标题不覆盖/失败静默回退, Sidebar 经 config:changed 实时 刷新); MCP 自动重连(5s/15s/60s 退避最多 3 次, reconnecting 状态机, teardownConnection 内部拆除保留簿记 — 用户断开/开关关闭即时取消, 设置页 显示第 N/3 次); 死循环检测 ABAB 乒乓模式(最近4轮 A→B→A→B 交替判定, 补齐 docs 第五章"两状态反复切换"检测契约); i18n 第三阶段(ChatInput/LLMSettings/ OnboardingWizard/MemoryViewer 主链路文案出层, zh-CN + en-US 双字典补齐) 测试: 737 → 824 用例(+87, 新增 8 个测试文件 + 扩展 3 个)。新覆盖: user-context 分组/空值收缩/拼接契约、context-builder 字节级稳定性、Anthropic cache_control 四态、consolidation-policy 九路判定矩阵、ssrf-dispatcher(pinned lookup/重定向 解析/IP 校验)、forget-session 会话隔离、trace-lifecycle run 淘汰、 trace-trim 严格小于边界、safe-env 净化矩阵、mcp-reconnect 退避状态机 (fake timers)、title-generator 并发重入、SQLite 侧 truncate×TRACE 联动 (Electron ABI)。测试驱动修复: GIT_*/ 注释终止块注释、重连计数被自身重试 前置断开重置(拆 teardownConnection 保留簿记)、TitleGenerator 幂等占位与 并发去重的检查顺序竞态(去重先于幂等) 版本: 0.7.3; README 同步(配置表新增 agent.enableReflection/memory.*/mcp.autoReconnect) 回归: typecheck 双端 0 错误; ESLint 0/0; 系统 Node 771 通过 53 跳过 (better-sqlite3 ABI); Electron ABI 全量 824/824 零跳过
429 lines
14 KiB
TypeScript
429 lines
14 KiB
TypeScript
/**
|
||
* Provider 请求形态测试矩阵(v0.6.4 P2-6)
|
||
*
|
||
* 此前 ollama(599 行)/ anthropic(532 行)两个最复杂的适配器零测试 —— 恰好也是
|
||
* 本轮审计中缺陷密度最高的文件。本文件通过 mock fetch 记录真实请求体,
|
||
* 锁定以下契约:
|
||
*
|
||
* Anthropic:
|
||
* A1 消息转换(system 顶层 / user-assistant-tool 三角色映射 / 孤立 tool_result 过滤)
|
||
* A2 max_tokens 按模型钳制(引擎默认 63488 → sonnet 64000 / opus 32000)
|
||
* A3 thinking 预算下限保护(小 maxTokens 场景 budget≥1024 且 < max_tokens,此前 API 400)
|
||
* A4 thinking 开启时不传 temperature;关闭时显式传递
|
||
*
|
||
* Ollama:
|
||
* O1 options 映射(num_predict=numTokens、num_ctx=contextLength、stop、top_p)
|
||
* O2 think 参数 effort 映射(low→"low"、max→true)与未配置时缺省
|
||
* O3 图片归一化(data URI 剥前缀;无 URL 触发下载分支时零网络请求)
|
||
*
|
||
* Agnes:
|
||
* G1 思考模式对称性 —— thinkingEnabled=false 必须显式发送 enable_thinking:false
|
||
*/
|
||
|
||
import { describe, it, expect, vi } from 'vitest';
|
||
|
||
vi.mock('electron-log', () => ({
|
||
default: { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() },
|
||
}));
|
||
|
||
import { AnthropicAdapter } from '../anthropic.adapter';
|
||
import { OllamaAdapter } from '../ollama.adapter';
|
||
import { MimoAdapter } from '../mimo.adapter';
|
||
import { AgnesAdapter } from '../agnes-ai.adapter';
|
||
import type { MetonaRequest } from '../../types';
|
||
|
||
/** 安装全局 fetch 捕获器:记录每次请求体并返回一个三家协议都能解析的合成响应 */
|
||
function captureFetch(): { bodies: Array<Record<string, unknown>> } {
|
||
const bodies: Array<Record<string, unknown>> = [];
|
||
// 兼容三家的非流式解析所需的最小字段集:
|
||
// OpenAI 兼容(agnes): choices[].message/finish_reason;Anthropic: content[]/usage/stop_reason;
|
||
// Ollama: message/done/prompt_eval_count/eval_count
|
||
const genericBody = {
|
||
id: 'cmpl-test',
|
||
object: 'chat.completion',
|
||
created: Date.now(),
|
||
model: 'test-model',
|
||
choices: [{ index: 0, message: { role: 'assistant', content: 'ok' }, finish_reason: 'stop' }],
|
||
content: [],
|
||
usage: {
|
||
prompt_tokens: 3,
|
||
completion_tokens: 2,
|
||
total_tokens: 5,
|
||
input_tokens: 3,
|
||
output_tokens: 2,
|
||
prompt_eval_count: 3,
|
||
eval_count: 2,
|
||
},
|
||
stop_reason: 'end_turn',
|
||
message: { role: 'assistant', content: 'ok' },
|
||
done: true,
|
||
};
|
||
const fetchMock = vi.fn(async (_url: string | URL, init?: RequestInit) => {
|
||
bodies.push(JSON.parse(String(init?.body ?? '{}')) as Record<string, unknown>);
|
||
return new Response(JSON.stringify(genericBody), {
|
||
status: 200,
|
||
headers: { 'Content-Type': 'application/json' },
|
||
});
|
||
});
|
||
vi.stubGlobal('fetch', fetchMock);
|
||
return { bodies };
|
||
}
|
||
|
||
function makeRequest(overrides?: Partial<MetonaRequest>): MetonaRequest {
|
||
return {
|
||
meta: {
|
||
sessionId: 's1',
|
||
iteration: 1,
|
||
requestId: 'r1',
|
||
timestamp: Date.now(),
|
||
agentVersion: 'test',
|
||
},
|
||
systemPrompt: {
|
||
roleDefinition: 'You are Metona.',
|
||
outputConstraints: 'Be concise.',
|
||
safetyGuidelines: 'Stay safe.',
|
||
dynamicReminders: '',
|
||
},
|
||
messages: [{ role: 'user', content: 'hi', timestamp: Date.now() }],
|
||
params: { maxTokens: 63_488, temperature: 0, stream: false },
|
||
...overrides,
|
||
};
|
||
}
|
||
|
||
// ===== Anthropic =====
|
||
|
||
describe('AnthropicAdapter — 请求体契约', () => {
|
||
it('A1: system 拼为顶层字段;tool 结果映射为 user 角色 tool_result 块', async () => {
|
||
const adapter = new AnthropicAdapter({
|
||
provider: 'anthropic',
|
||
baseURL: 'http://a.test',
|
||
apiKey: 'k',
|
||
defaultModel: 'claude-sonnet-4-5',
|
||
});
|
||
const { bodies } = captureFetch();
|
||
await adapter.send(
|
||
makeRequest({
|
||
messages: [
|
||
{ role: 'user', content: 'read it', timestamp: Date.now() },
|
||
{
|
||
role: 'assistant',
|
||
content: null,
|
||
toolCalls: [
|
||
{
|
||
id: 'tc_1',
|
||
name: 'read_file',
|
||
args: { path: 'a.txt' },
|
||
iteration: 1,
|
||
timestamp: Date.now(),
|
||
},
|
||
],
|
||
timestamp: Date.now(),
|
||
},
|
||
{
|
||
role: 'tool',
|
||
content: null,
|
||
toolResult: {
|
||
toolCallId: 'tc_1',
|
||
toolName: 'read_file',
|
||
result: 'data',
|
||
success: true,
|
||
durationMs: 1,
|
||
timestamp: Date.now(),
|
||
},
|
||
timestamp: Date.now(),
|
||
},
|
||
// 孤立 tool_result(前面没有对应 tool_use)应被过滤
|
||
{
|
||
role: 'tool',
|
||
content: null,
|
||
toolResult: {
|
||
toolCallId: 'tc_orphan',
|
||
toolName: 'x',
|
||
result: '',
|
||
success: true,
|
||
durationMs: 1,
|
||
timestamp: Date.now(),
|
||
},
|
||
timestamp: Date.now(),
|
||
},
|
||
{ role: 'user', content: 'next?', timestamp: Date.now() },
|
||
],
|
||
}),
|
||
);
|
||
|
||
const body = bodies[0];
|
||
// v0.7.3 P1-1: system 转为块数组并打 cache_control 断言(稳定前缀 prompt cache)
|
||
const system = body.system as Array<{
|
||
type: string;
|
||
text: string;
|
||
cache_control: { type: string };
|
||
}>;
|
||
expect(Array.isArray(system)).toBe(true);
|
||
expect(system[0].text).toContain('You are Metona.');
|
||
expect(system[0].cache_control).toEqual({ type: 'ephemeral' });
|
||
expect(Array.isArray(body.messages)).toBe(true);
|
||
const msgs = body.messages as Array<{ role: string; content: Array<Record<string, unknown>> }>;
|
||
// tool_use 的 assistant 消息存在且携带 id/name
|
||
const assistantToolMsg = msgs.find((m) => m.role === 'assistant');
|
||
expect(assistantToolMsg?.content[0]).toMatchObject({
|
||
type: 'tool_use',
|
||
id: 'tc_1',
|
||
name: 'read_file',
|
||
});
|
||
// tool 结果以 user 角色 tool_result 形态出现且配对 id 正确;孤立者被丢弃
|
||
const toolResultBlocks = msgs.flatMap((m) => m.content.filter((c) => c.type === 'tool_result'));
|
||
expect(toolResultBlocks).toHaveLength(1);
|
||
expect(toolResultBlocks[0].tool_use_id).toBe('tc_1');
|
||
});
|
||
|
||
it('A2: max_tokens 按模型上限钳制(63488 → sonnet 64000 / opus 32000)', async () => {
|
||
const sonnet = new AnthropicAdapter({
|
||
provider: 'anthropic',
|
||
baseURL: 'http://a.test',
|
||
apiKey: 'k',
|
||
defaultModel: 'claude-sonnet-4-5',
|
||
});
|
||
const opus = new AnthropicAdapter({
|
||
provider: 'anthropic',
|
||
baseURL: 'http://a.test',
|
||
apiKey: 'k',
|
||
defaultModel: 'claude-opus-4-1',
|
||
});
|
||
const { bodies } = captureFetch();
|
||
await sonnet.send(makeRequest());
|
||
await opus.send(makeRequest());
|
||
// 引擎默认 63488 低于 sonnet 上限 64000 → 原样保留;opus 上限 32000 → 钳制生效
|
||
expect(bodies[0].max_tokens).toBe(63_488);
|
||
expect(bodies[1].max_tokens).toBe(32_000);
|
||
});
|
||
|
||
it('A3: 小 maxTokens 时 thinking budget 不跌破协议下限 1024(v0.6.4 边界加固)', async () => {
|
||
const adapter = new AnthropicAdapter({
|
||
provider: 'anthropic',
|
||
baseURL: 'http://a.test',
|
||
apiKey: 'k',
|
||
defaultModel: 'claude-haiku-4-5',
|
||
});
|
||
const { bodies } = captureFetch();
|
||
await adapter.send(
|
||
makeRequest({
|
||
params: {
|
||
maxTokens: 1500,
|
||
temperature: 0,
|
||
stream: false,
|
||
thinkingEnabled: true,
|
||
thinkingEffort: 'low',
|
||
},
|
||
}),
|
||
);
|
||
const body = bodies[0];
|
||
const thinking = body.thinking as { type: string; budget_tokens: number };
|
||
// max_tokens 被抬升到安全下限,budget 落在 [1024, max_tokens/2] 区间内
|
||
expect(body.max_tokens as number).toBeGreaterThanOrEqual(2048);
|
||
expect(thinking.budget_tokens).toBeGreaterThanOrEqual(1024);
|
||
expect(thinking.budget_tokens).toBeLessThanOrEqual((body.max_tokens as number) / 2);
|
||
});
|
||
|
||
it('A4: thinking 开启不传 temperature;关闭时显式传递', async () => {
|
||
const adapter = new AnthropicAdapter({
|
||
provider: 'anthropic',
|
||
baseURL: 'http://a.test',
|
||
apiKey: 'k',
|
||
defaultModel: 'claude-sonnet-4-5',
|
||
});
|
||
const { bodies } = captureFetch();
|
||
await adapter.send(
|
||
makeRequest({
|
||
params: { maxTokens: 4096, temperature: 0.7, stream: false, thinkingEnabled: true },
|
||
}),
|
||
);
|
||
expect(bodies[0].temperature).toBeUndefined();
|
||
expect(bodies[0].thinking).toBeDefined();
|
||
|
||
await adapter.send(
|
||
makeRequest({
|
||
params: { maxTokens: 4096, temperature: 0.7, stream: false, thinkingEnabled: false },
|
||
}),
|
||
);
|
||
expect(bodies[1].temperature).toBe(0.7);
|
||
expect(bodies[1].thinking).toBeUndefined();
|
||
});
|
||
});
|
||
|
||
// ===== Ollama =====
|
||
|
||
describe('OllamaAdapter — 请求体契约', () => {
|
||
function makeOllama(): OllamaAdapter {
|
||
return new OllamaAdapter({
|
||
provider: 'ollama',
|
||
baseURL: 'http://localhost:11434',
|
||
defaultModel: 'qwen3',
|
||
});
|
||
}
|
||
|
||
it('O1: options 映射 num_predict/num_ctx/stop/top_p/temperature', async () => {
|
||
const adapter = makeOllama();
|
||
const { bodies } = captureFetch();
|
||
await adapter.send(
|
||
makeRequest({
|
||
params: {
|
||
maxTokens: 8192,
|
||
temperature: 0.3,
|
||
topP: 0.9,
|
||
stream: false,
|
||
contextLength: 16384,
|
||
stopSequences: ['STOP'],
|
||
},
|
||
}),
|
||
);
|
||
const options = bodies[0].options as Record<string, unknown>;
|
||
expect(options.num_predict).toBe(8192);
|
||
expect(options.num_ctx).toBe(16384);
|
||
expect(options.temperature).toBe(0.3);
|
||
expect(options.top_p).toBe(0.9);
|
||
expect(options.stop).toEqual(['STOP']);
|
||
});
|
||
|
||
it('O2: think 参数 effort 映射(low→"low"、max→true);未开启思考时缺省', async () => {
|
||
const adapter = makeOllama();
|
||
const { bodies } = captureFetch();
|
||
|
||
await adapter.send(
|
||
makeRequest({
|
||
params: {
|
||
maxTokens: 4096,
|
||
temperature: 0,
|
||
stream: false,
|
||
thinkingEnabled: true,
|
||
thinkingEffort: 'low',
|
||
},
|
||
}),
|
||
);
|
||
expect(bodies[0].think).toBe('low');
|
||
|
||
await adapter.send(
|
||
makeRequest({
|
||
params: {
|
||
maxTokens: 4096,
|
||
temperature: 0,
|
||
stream: false,
|
||
thinkingEnabled: true,
|
||
thinkingEffort: 'max',
|
||
},
|
||
}),
|
||
);
|
||
expect(bodies[1].think).toBe(true);
|
||
|
||
await adapter.send(
|
||
makeRequest({
|
||
params: { maxTokens: 4096, temperature: 0, stream: false, thinkingEnabled: false },
|
||
}),
|
||
);
|
||
expect(bodies[2].think).toBeUndefined();
|
||
});
|
||
|
||
it('O3: data URI 图片剥前缀转纯 base64 数组(无网络下载路径触发)', async () => {
|
||
const adapter = makeOllama();
|
||
const { bodies } = captureFetch();
|
||
await adapter.send(
|
||
makeRequest({
|
||
messages: [
|
||
{
|
||
role: 'user',
|
||
content: '看图',
|
||
images: [{ url: 'data:image/png;base64,iVBORw0KGgoAAAANSU', detail: 'auto' }],
|
||
timestamp: Date.now(),
|
||
},
|
||
],
|
||
}),
|
||
);
|
||
const messages = bodies[0].messages as Array<Record<string, unknown>>;
|
||
const userMsg = messages[messages.length - 1];
|
||
expect(userMsg.images).toEqual(['iVBORw0KGgoAAAANSU']);
|
||
});
|
||
});
|
||
|
||
// ===== MiMo providerOptions(v0.6.4 P4-3) =====
|
||
|
||
describe('MimoAdapter — 服务端能力扩展(providerOptions)', () => {
|
||
it('enableWebSearch 开启时附加 {type:web_search} 服务端工具', async () => {
|
||
const adapter = new MimoAdapter({
|
||
provider: 'mimo',
|
||
baseURL: 'http://m.test/v1',
|
||
apiKey: 'k',
|
||
defaultModel: 'mimo-v2.5',
|
||
providerOptions: { enableWebSearch: true },
|
||
});
|
||
const { bodies } = captureFetch();
|
||
await adapter.send(makeRequest());
|
||
const tools = bodies[0].tools as Array<Record<string, unknown>>;
|
||
expect(tools.some((tc) => (tc as { type?: string }).type === 'web_search')).toBe(true);
|
||
expect(bodies[0].tool_choice).toBe('auto');
|
||
});
|
||
|
||
it('responseFormatJson 开启时写入 response_format json_object;默认不写', async () => {
|
||
const on = new MimoAdapter({
|
||
provider: 'mimo',
|
||
baseURL: 'http://m.test/v1',
|
||
apiKey: 'k',
|
||
defaultModel: 'mimo-v2.5',
|
||
providerOptions: { responseFormatJson: true },
|
||
});
|
||
const off = new MimoAdapter({
|
||
provider: 'mimo',
|
||
baseURL: 'http://m.test/v1',
|
||
apiKey: 'k',
|
||
defaultModel: 'mimo-v2.5',
|
||
});
|
||
const { bodies } = captureFetch();
|
||
await on.send(makeRequest());
|
||
await off.send(makeRequest());
|
||
expect(bodies[0].response_format).toEqual({ type: 'json_object' });
|
||
expect(bodies[1].response_format).toBeUndefined();
|
||
});
|
||
});
|
||
|
||
// ===== Agnes =====
|
||
|
||
describe('AgnesAdapter — 思考模式对称性(v0.6.4)', () => {
|
||
it('G1: thinkingEnabled=false 显式发送 enable_thinking:false(此前无法关闭服务端默认思考)', async () => {
|
||
const adapter = new AgnesAdapter({
|
||
provider: 'agnes',
|
||
baseURL: 'http://g.test/v1',
|
||
apiKey: 'k',
|
||
defaultModel: 'agnes-2.0-flash',
|
||
});
|
||
const { bodies } = captureFetch();
|
||
|
||
await adapter.send(
|
||
makeRequest({
|
||
params: {
|
||
maxTokens: 4096,
|
||
temperature: 0,
|
||
stream: false,
|
||
thinkingEnabled: true,
|
||
thinkingEffort: 'high',
|
||
},
|
||
}),
|
||
);
|
||
expect(
|
||
((bodies[0].chat_template_kwargs as Record<string, unknown>) ?? {}).enable_thinking,
|
||
).toBe(true);
|
||
|
||
await adapter.send(
|
||
makeRequest({
|
||
params: { maxTokens: 4096, temperature: 0, stream: false, thinkingEnabled: false },
|
||
}),
|
||
);
|
||
expect(
|
||
((bodies[1].chat_template_kwargs as Record<string, unknown>) ?? {}).enable_thinking,
|
||
).toBe(false);
|
||
|
||
// 未配置 thinkingEnabled 同样视为关闭(显式 disabled 保持与服务端默认的确定性)
|
||
await adapter.send(makeRequest({ params: { maxTokens: 4096, temperature: 0, stream: false } }));
|
||
expect(
|
||
((bodies[2].chat_template_kwargs as Record<string, unknown>) ?? {}).enable_thinking,
|
||
).toBe(false);
|
||
});
|
||
});
|