Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(ai): Claude 5.5 thinking-off controls per model (#7567)
garrytan0369549da991
diff --git a/src/core/ai/thinking-off.ts b/src/core/ai/thinking-off.tsindex bcfbe9d240..5770ff49a7 100644--- a/src/core/ai/thinking-off.ts+++ b/src/core/ai/thinking-off.ts@@ -12,9 +12,20 @@* gateway.ts) read the same table, so a preflight prices the cap the call* sends.*- * Routes whose option turns reasoning off keep the requested cap:- * - native Anthropic (`anthropic`) and DeepSeek (`deepseek`; thinking is on- * by default for v4): `thinking: { type: 'disabled' }`+ * Native Anthropic (`anthropic`) rows follow Anthropic's extended-thinking docs+ * per model, with `effort: 'low'` pinned on the 5.5 rows so a configured+ * xhigh or max effort cannot invalidate the setting (#7567):+ * - Claude Sonnet 5.5: `thinking: { type: 'between_tools' }` (`disabled` is+ * a 400; between_tools at effort high or below is its off), reasoning off+ * - Claude Haiku 5.5: `thinking: { type: 'disabled' }` (accepted only at+ * effort high or below), reasoning off+ * - Claude Opus 5.5: `thinking: { type: 'adaptive' }`; it cannot turn+ * thinking off, only lower the effort, so it keeps reasoning and gets+ * the headroom below+ * - older Claude: `thinking: { type: 'disabled' }`, reasoning off+ * Other routes whose option turns reasoning off keep the requested cap:+ * - DeepSeek (`deepseek`; thinking is on by default for v4):+ * `thinking: { type: 'disabled' }`* - OpenRouter-hosted DeepSeek (`openrouter` with a `deepseek/` model): same* - Gemini 2.5 Flash and Flash-Lite: `thinkingConfig: { thinkingBudget: 0 }`* - OpenAI models that accept `reasoningEffort: 'none'` (gpt-5.1, 5.2, 5.4,@@ -59,6 +70,15 @@ export interface ThinkingOffControl {const THINKING_DISABLED: Record<string, unknown> = { thinking: { type: 'disabled' } };+const CLAUDE_55_RE = /^claude-(sonnet|opus|haiku)-5-5(?:-\d{8})?$/;+const CLAUDE_55_THINKING: Readonly<Record<string, string>> = { sonnet: 'between_tools', opus: 'adaptive', haiku: 'disabled' };++function anthropicControl(model: string): ThinkingOffControl {+ const family = CLAUDE_55_RE.exec(model)?.[1];+ if (!family) return { set: { namespace: 'anthropic', options: THINKING_DISABLED }, disables: true };+ return { set: { namespace: 'anthropic', options: { thinking: { type: CLAUDE_55_THINKING[family] }, effort: 'low' } }, disables: family !== 'opus' };+}+const GEMINI_ID_RE = /^gemini-(\d+(?:\.\d+)?)-(flash-lite|flash|pro)(?:-|$)/;const GEMINI_NON_CHAT_RE = /-(?:image|tts|audio|live|transcribe|computer-use)(?:-|$)/;const GEMINI_THINKING_LEVEL_FLOOR: Readonly<Record<string, 'minimal' | 'low'>> = {@@ -113,7 +133,8 @@ export function thinkingOffControl(modelStr: string): ThinkingOffControl | undefconst { provider, model: rawModel } = splitProviderModelId(modelStr);if (!provider) return undefined;const model = rawModel.trim().toLowerCase();- if (provider === 'anthropic' || provider === 'deepseek') return { set: { namespace: provider, options: THINKING_DISABLED }, disables: true };+ if (provider === 'anthropic') return anthropicControl(model);+ if (provider === 'deepseek') return { set: { namespace: provider, options: THINKING_DISABLED }, disables: true };if (provider === 'openrouter') return model.startsWith('deepseek/') ? { set: { namespace: provider, options: THINKING_DISABLED }, disables: true } : undefined;if (provider === 'google') return googleControl(model);if (provider === 'openai') return openaiControl(model);diff --git a/test/ai/anthropic-thinking-off-55.test.ts b/test/ai/anthropic-thinking-off-55.test.tsnew file mode 100644index 0000000000..e2b4af9a8a--- /dev/null+++ b/test/ai/anthropic-thinking-off-55.test.ts@@ -0,0 +1,67 @@+/**+ * #7567: `thinking: 'off'` on a native Claude 5.5 route sends that model's+ * lowest supported thinking setting with effort low, not the blanket+ * `{ type: 'disabled' }` (a 400 on Sonnet 5.5, ignored by Opus 5.5, valid on+ * Haiku 5.5 only at effort high or below). A configured budget or effort is+ * replaced and sibling options survive. Opus 5.5 still reasons, so its cap+ * and the preflight estimate get the thinking headroom. The wire check goes+ * through the real @ai-sdk/anthropic with a stub fetch: no network, no key.+ */+import { describe, expect, test } from 'bun:test';+import { createAnthropic } from '@ai-sdk/anthropic';+import { generateText } from 'ai';+import { applyThinkingOff, thinkingOffControl, thinkingOffMaxOutputTokens } from '../../src/core/ai/thinking-off.ts';++const REQUESTED = 1500;+const HEADROOM = 32000;+const configured = () => ({ anthropic: { thinking: { type: 'enabled', budgetTokens: 8000 }, effort: 'xhigh', cacheControl: { type: 'ephemeral', ttl: '1h' } } });++const rows = [+ { model: 'claude-sonnet-5-5', thinking: 'between_tools', disables: true },+ { model: 'claude-sonnet-5-5-20261015', thinking: 'between_tools', disables: true },+ { model: 'claude-opus-5-5', thinking: 'adaptive', disables: false },+ { model: 'claude-opus-5-5-20261015', thinking: 'adaptive', disables: false },+ { model: 'claude-haiku-5-5', thinking: 'disabled', disables: true },+ { model: 'claude-haiku-5-5-20261015', thinking: 'disabled', disables: true },+];++async function sentBody(model: string, providerOptions: Record<string, any>, maxOutputTokens: number) {+ let sent: Record<string, any> = {};+ const anthropic = createAnthropic({+ apiKey: 'test-key-not-real',+ fetch: (async (_url: RequestInfo | URL, init?: RequestInit) => {+ sent = JSON.parse(String(init?.body));+ return Response.json({ id: 'msg_test', type: 'message', role: 'assistant', model, stop_reason: 'end_turn',+ content: [{ type: 'text', text: 'done' }], usage: { input_tokens: 3, output_tokens: 1 } });+ }) as typeof fetch,+ });+ const { warnings } = await generateText({ model: anthropic(model), prompt: 'Say done.', providerOptions, maxOutputTokens });+ return { sent, warnings };+}++describe('Claude 5.5 thinking-off controls (#7567)', () => {+ for (const row of rows) {+ test(`${row.model}: ${row.thinking} at effort low, sent without an SDK rewrite`, async () => {+ const route = `anthropic:${row.model}`;+ expect(thinkingOffControl(route)?.disables).toBe(row.disables);+ const options = applyThinkingOff(configured(), route);+ expect(options).toEqual({ anthropic: { thinking: { type: row.thinking }, effort: 'low', cacheControl: { type: 'ephemeral', ttl: '1h' } } });+ const cap = thinkingOffMaxOutputTokens(route, REQUESTED, true, HEADROOM);+ expect(cap).toBe(row.disables ? REQUESTED : HEADROOM);+ const { sent, warnings } = await sentBody(row.model, options, cap);+ expect(warnings).toEqual([]);+ expect(sent.thinking).toEqual({ type: row.thinking });+ expect(sent.output_config).toEqual({ effort: 'low' });+ expect(sent.max_tokens).toBe(cap);+ });+ }++ test('older Claude, DeepSeek and non-native Claude routes keep their behavior', () => {+ for (const route of ['anthropic:claude-opus-5', 'anthropic:claude-sonnet-4-6', 'anthropic:claude-haiku-4-5-20251001', 'anthropic:claude-sonnet-5-5-preview']) {+ expect(applyThinkingOff({}, route)).toEqual({ anthropic: { thinking: { type: 'disabled' } } });+ expect(thinkingOffMaxOutputTokens(route, REQUESTED, true, HEADROOM)).toBe(REQUESTED);+ }+ expect(applyThinkingOff({}, 'deepseek:deepseek-v4-flash')).toEqual({ deepseek: { thinking: { type: 'disabled' } } });+ expect(applyThinkingOff({}, 'openrouter:anthropic/claude-sonnet-5.5')).toEqual({});+ });+});diff --git a/test/fence-repair-llm.test.ts b/test/fence-repair-llm.test.tsindex 1b716cdd5c..bf86129fcc 100644--- a/test/fence-repair-llm.test.ts+++ b/test/fence-repair-llm.test.ts@@ -116,6 +116,14 @@ describe('Tier 3 requests and prompt', () => {expect(tier3TokenBudget(req, 'claude-cli:claude-fable-5-1').maxOutputTokens).toBe(32000);});+ test('Claude Opus 5.5, which cannot turn thinking off, is priced with the thinking headroom (#7567)', () => {+ const req = residualOf(rowLevel).requests[0]!;+ const table = tier3TokenBudget(req, 'anthropic:claude-opus-4-7').maxOutputTokens;+ expect(tier3TokenBudget(req, 'anthropic:claude-opus-5-5').maxOutputTokens).toBe(32000);+ expect(tier3TokenBudget(req, 'anthropic:claude-opus-5-5').maxOutputTokens).toBe(thinkingOffOutputCap('anthropic:claude-opus-5-5', table + TIER3_REASONING_TOKENS));+ expect(tier3TokenBudget(req, 'anthropic:claude-sonnet-5-5').maxOutputTokens).toBe(table + TIER3_REASONING_TOKENS);+ });+test('a fence with a stray text line inside is not eligible (rebuilding the table would drop it)', () => {const stray = `${FB}\n| 1 | ${BROKEN} | fact | 0.9 | private | high | 2026-01-01 | | chat | ctx |\na stray note\n${FBE}\n`;const out = residualOf(stray);