diff --git a/v3/@claude-flow/cli/__tests__/sampling-params-2357.test.ts b/v3/@claude-flow/cli/__tests__/sampling-params-2357.test.ts new file mode 100644 index 0000000000..919dc53f1c --- /dev/null +++ b/v3/@claude-flow/cli/__tests__/sampling-params-2357.test.ts @@ -0,0 +1,111 @@ +/** + * Regression test for #2357 (Finding A): callAnthropicMessages always sent + * `temperature` (default 0.7), but the adaptive-thinking family — Fable 5, + * Opus 4.8, Opus 4.7 — removed temperature/top_p/top_k. The API rejects the + * request with 400 "temperature: Extra inputs are not permitted", so + * agent_execute / workflow_run / the WASM-agent Anthropic path could not + * call any current frontier model when an ANTHROPIC_API_KEY was set. + * + * Pin the contract: sampling params are omitted for models that reject them, + * and unchanged (including the 0.7 default) for models that still accept + * them. The Ollama / OpenRouter OpenAI-compat paths are out of scope — they + * accept temperature. + */ + +import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; +import { + callAnthropicMessages, + modelRejectsSamplingParams, +} from '../src/mcp-tools/agent-execute-core.js'; + +describe('modelRejectsSamplingParams (#2357)', () => { + it.each([ + 'claude-fable-5', + 'claude-opus-4-8', + 'claude-opus-4-7', + ])('rejects sampling params for %s', (m) => { + expect(modelRejectsSamplingParams(m)).toBe(true); + }); + + it('covers dated snapshots of the same family', () => { + expect(modelRejectsSamplingParams('claude-opus-4-8-20260301')).toBe(true); + }); + + it.each([ + 'claude-sonnet-4-6', + 'claude-haiku-4-5-20251001', + 'claude-opus-4-6', // pre-4.7 Opus still accepts sampling params + ])('keeps sampling params for %s', (m) => { + expect(modelRejectsSamplingParams(m)).toBe(false); + }); +}); + +describe('callAnthropicMessages request body (#2357)', () => { + const captured: Array> = []; + const savedEnv = { ...process.env }; + + beforeEach(() => { + captured.length = 0; + process.env.ANTHROPIC_API_KEY = 'sk-ant-test-not-real'; + delete process.env.RUFLO_PROVIDER; + delete process.env.OPENROUTER_API_KEY; + delete process.env.OLLAMA_API_KEY; + vi.stubGlobal( + 'fetch', + vi.fn(async (_url: string, opts: { body: string }) => { + const body = JSON.parse(opts.body) as Record; + captured.push(body); + return { + ok: true, + json: async () => ({ + id: 'msg_test', + model: body.model, + content: [{ type: 'text', text: 'ok' }], + stop_reason: 'end_turn', + usage: { input_tokens: 1, output_tokens: 1 }, + }), + }; + }) + ); + }); + + afterEach(() => { + vi.unstubAllGlobals(); + process.env = { ...savedEnv }; + }); + + it.each([ + 'claude-fable-5', + 'claude-opus-4-8', + 'claude-opus-4-7', + ])('omits temperature for %s (the API 400s otherwise)', async (model) => { + const r = await callAnthropicMessages({ prompt: 'ping', model, maxTokens: 8 }); + expect(r.success).toBe(true); + expect(captured[0]).not.toHaveProperty('temperature'); + }); + + it('still sends the 0.7 default for models that accept sampling params', async () => { + await callAnthropicMessages({ prompt: 'ping', model: 'claude-sonnet-4-6', maxTokens: 8 }); + expect(captured[0]).toHaveProperty('temperature', 0.7); + }); + + it('honors an explicit temperature for accepting models', async () => { + await callAnthropicMessages({ + prompt: 'ping', + model: 'claude-haiku-4-5-20251001', + temperature: 0.2, + maxTokens: 8, + }); + expect(captured[0]).toHaveProperty('temperature', 0.2); + }); + + it('drops even an explicit temperature for frontier models (would 400)', async () => { + await callAnthropicMessages({ + prompt: 'ping', + model: 'claude-fable-5', + temperature: 0.9, + maxTokens: 8, + }); + expect(captured[0]).not.toHaveProperty('temperature'); + }); +}); diff --git a/v3/@claude-flow/cli/src/mcp-tools/agent-execute-core.ts b/v3/@claude-flow/cli/src/mcp-tools/agent-execute-core.ts index 78421de4b4..eda5830638 100644 --- a/v3/@claude-flow/cli/src/mcp-tools/agent-execute-core.ts +++ b/v3/@claude-flow/cli/src/mcp-tools/agent-execute-core.ts @@ -69,6 +69,16 @@ const MODEL_MAP: Record = { inherit: DEFAULT_ANTHROPIC_MODEL, }; +// #2357 — the adaptive-thinking family (Fable 5, Opus 4.8, Opus 4.7) removed +// the sampling parameters (temperature/top_p/top_k); the Anthropic API +// returns 400 "Extra inputs are not permitted" when any is present. +// Prefix-match so dated snapshots (e.g. claude-opus-4-8-YYYYMMDD) are +// covered. Applies only to the direct Anthropic path — the Ollama/OpenRouter +// OpenAI-compat paths accept temperature and are unchanged. +export function modelRejectsSamplingParams(model: string): boolean { + return /^claude-(fable-5|opus-4-8|opus-4-7)/.test(model); +} + export interface AnthropicCallInput { prompt: string; systemPrompt?: string; @@ -150,7 +160,13 @@ export async function callAnthropicMessages(input: AnthropicCallInput): Promise< body: JSON.stringify({ model, max_tokens: input.maxTokens || 1024, - temperature: typeof input.temperature === 'number' ? input.temperature : 0.7, + // #2357 — omit temperature for models that reject sampling params + // (Fable 5 / Opus 4.8 / Opus 4.7 → 400 "Extra inputs are not + // permitted"); keep the 0.7 default unchanged for models that still + // accept it (sonnet / haiku / opus ≤4.6). + ...(modelRejectsSamplingParams(model) + ? {} + : { temperature: typeof input.temperature === 'number' ? input.temperature : 0.7 }), // #8 prompt caching (hermes-agent pattern): mark the (often large, // stable) system prompt as an ephemeral cache breakpoint so repeated // agent_execute calls with the same system prompt hit Anthropic's