Files
metona-ai-desktop/electron/harness/adapters/__tests__/thinking-capability-gate.test.ts
T
thzxx 4cd6e997b5
CI / 类型检查 + Lint + 单元测试 (push) Failing after 9m45s
CI / 全量测试 (Electron ABI) (push) Failing after 6m28s
CI / 产物编译验证 (push) Successful in 11m18s
feat: v0.8.2 安全纵深补全 · 协议保真 · 断链修复 — 图片SSRF/根MEMORY.md保护根治 · Anthropic thinking回传+pause_turn续传 · 2523 用例全量回归 + E2E 扩充
2026-09-08 14:30:27 +08:00

202 lines
7.6 KiB
TypeScript
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.
/**
* v0.8.0 P0-3(修订版): 思考参数**用户意图优先**契约。
*
* 修订原因:初版按 MODEL_INFO.supportsThinking 元信息硬门控,但事故复盘证明
* 元信息不可靠 —— deepseek-v4-flash-vision-exp 标注"不支持思考"、实际却产生了
* 8189 token 推理内容。既然引擎已有完整兜底链(最大输出上限配置 → 空响应守卫
* → 降级重试 → OUTPUT_LENGTH_EXCEEDED 明确报错),是否开思考应由**用户决定**,
* 适配器层只负责:①如实透传用户配置;②元信息不符时告警不拦截;③预算过小告警。
*
* 保留的唯一门控是 Ollama 的 /api/show capabilities 探测 —— 那是服务端实时
* 真值且为硬协议约束(向无思考能力的模型发 think 每次请求 400),属协议
* 正确性而非用户意图覆盖。
*/
import { describe, it, expect } from 'vitest';
import { DeepSeekAdapter } from '../deepseek.adapter';
import { AgnesAdapter } from '../agnes-ai.adapter';
import { MimoAdapter } from '../mimo.adapter';
import { OllamaAdapter } from '../ollama.adapter';
import type { MetonaRequest } from '../../types';
const SYSTEM_PROMPT = {
roleDefinition: 'test',
outputConstraints: '',
safetyGuidelines: '',
};
function makeRequest(overrides?: Partial<MetonaRequest['params']>): MetonaRequest {
return {
meta: {
sessionId: 's',
iteration: 1,
requestId: 'r',
timestamp: Date.now(),
agentVersion: '1.0.0',
},
systemPrompt: SYSTEM_PROMPT,
messages: [{ role: 'user', content: 'hi', timestamp: Date.now() }],
params: {
maxTokens: 63488,
temperature: 0,
stream: true,
thinkingEnabled: true,
thinkingEffort: 'max',
...overrides,
},
};
}
function asNative(
adapter: unknown,
): (req: MetonaRequest, stream: boolean) => Record<string, unknown> {
return (
adapter as { toNativeRequest: (r: MetonaRequest, s: boolean) => Record<string, unknown> }
).toNativeRequest.bind(adapter);
}
describe('P0-3 修订: 用户思考意图优先于模型元信息', () => {
it('DeepSeek: vision-exp(元信息 false+ 用户开启思考 → 照发 enabled + reasoning_effortmax_tokens 原样透传(v0.8.1 无钳制)', async () => {
const adapter = new DeepSeekAdapter({
provider: 'deepseek',
baseURL: 'https://api.deepseek.com',
apiKey: 'k',
defaultModel: 'deepseek-v4-flash-vision-exp',
});
const body = asNative(adapter)(makeRequest(), false);
expect(body.thinking).toEqual({ type: 'enabled' });
expect(body.reasoning_effort).toBe('max');
expect(body.max_tokens).toBe(63488);
});
it('DeepSeek: 用户关闭思考 → 显式 disabled', async () => {
const adapter = new DeepSeekAdapter({
provider: 'deepseek',
baseURL: 'https://api.deepseek.com',
apiKey: 'k',
defaultModel: 'deepseek-v4-flash-vision-exp',
});
const body = asNative(adapter)(
makeRequest({ thinkingEnabled: false, thinkingEffort: undefined }),
false,
);
expect(body.thinking).toEqual({ type: 'disabled' });
expect(body.reasoning_effort).toBeUndefined();
});
it('DeepSeek: 未配置 → 显式 disabled(确定性契约,不依赖服务端隐式默认)', async () => {
const adapter = new DeepSeekAdapter({
provider: 'deepseek',
baseURL: 'https://api.deepseek.com',
apiKey: 'k',
defaultModel: 'deepseek-v4-pro',
});
const body = asNative(adapter)(makeRequest({ thinkingEnabled: undefined }), false);
expect(body.thinking).toEqual({ type: 'disabled' });
});
it('Agnes: 元信息 false + 用户开启思考 → enable_thinking:true(元信息不拦截)', async () => {
const adapter = new AgnesAdapter({
provider: 'agnes',
baseURL: 'https://apihub.agnes-ai.com/v1',
apiKey: 'k',
defaultModel: 'agnes-2.0-flash',
});
const table = AgnesAdapter['MODEL_INFO'] as Record<string, { supportsThinking: boolean }>;
const original = table['agnes-2.0-flash'].supportsThinking;
table['agnes-2.0-flash'].supportsThinking = false;
try {
const body = asNative(adapter)(makeRequest(), false);
expect(body.chat_template_kwargs).toEqual({ enable_thinking: true });
} finally {
table['agnes-2.0-flash'].supportsThinking = original;
}
});
it('Agnes: 用户关闭思考 → enable_thinking:false(对称契约保持)', async () => {
const adapter = new AgnesAdapter({
provider: 'agnes',
baseURL: 'https://apihub.agnes-ai.com/v1',
apiKey: 'k',
defaultModel: 'agnes-2.0-flash',
});
const body = asNative(adapter)(makeRequest({ thinkingEnabled: false }), false);
expect(body.chat_template_kwargs).toEqual({ enable_thinking: false });
});
it('MiMo: 元信息 false + 用户开启思考 → thinking enabledtemperature 不传)', async () => {
const adapter = new MimoAdapter({
provider: 'mimo',
baseURL: 'https://api.xiaomimimo.com/v1',
apiKey: 'k',
defaultModel: 'mimo-v2.5-pro',
});
const table = MimoAdapter['MODEL_INFO'] as Record<string, { supportsThinking: boolean }>;
const original = table['mimo-v2.5-pro'].supportsThinking;
table['mimo-v2.5-pro'].supportsThinking = false;
try {
const body = asNative(adapter)(makeRequest(), false);
expect(body.thinking).toEqual({ type: 'enabled' });
expect(body.temperature).toBeUndefined();
} finally {
table['mimo-v2.5-pro'].supportsThinking = original;
}
});
it('MiMo: 用户关闭思考 → disabled + temperature/top_p 透传', async () => {
const adapter = new MimoAdapter({
provider: 'mimo',
baseURL: 'https://api.xiaomimimo.com/v1',
apiKey: 'k',
defaultModel: 'mimo-v2.5-pro',
});
const body = asNative(adapter)(makeRequest({ thinkingEnabled: false }), false);
expect(body.thinking).toEqual({ type: 'disabled' });
expect(body.temperature).toBe(0);
});
it('MiMo: 未配置(undefined)→ 显式 disabledv0.8.2 P2-6 与 DeepSeek/Agnes 对齐,不隐式吞用户 temperature', async () => {
const adapter = new MimoAdapter({
provider: 'mimo',
baseURL: 'https://api.xiaomimimo.com/v1',
apiKey: 'k',
defaultModel: 'mimo-v2.5-pro',
});
const body = asNative(adapter)(makeRequest({ thinkingEnabled: undefined }), false);
expect(body.thinking).toEqual({ type: 'disabled' });
expect(body.temperature).toBe(0);
});
it('Ollama: 探测不支持思考(服务端硬约束)→ 不发 think 参数(唯一保留的门控)', async () => {
const adapter = new OllamaAdapter({
provider: 'ollama',
baseURL: 'http://localhost:11434',
apiKey: '',
defaultModel: 'qwen3:latest',
});
(adapter as unknown as { cachedThinkingSupport: boolean | null }).cachedThinkingSupport = false;
const body = await (
adapter as unknown as {
toNativeRequest: (r: MetonaRequest) => Promise<Record<string, unknown>>;
}
).toNativeRequest(makeRequest());
expect(body.think).toBeUndefined();
});
it('Ollama: 探测未知(nullfail-open → think 参数照发', async () => {
const adapter = new OllamaAdapter({
provider: 'ollama',
baseURL: 'http://localhost:11434',
apiKey: '',
defaultModel: 'qwen3:latest',
});
(adapter as unknown as { cachedThinkingSupport: boolean | null }).cachedThinkingSupport = null;
const body = await (
adapter as unknown as {
toNativeRequest: (r: MetonaRequest) => Promise<Record<string, unknown>>;
}
).toNativeRequest(makeRequest());
expect(body.think).toBe(true);
});
});