Skip to content

Commit 8bc14b0

Browse files
committed
fix(claude): keep an explicit thinking disable through translation (lidge-jun#545)
Claude Desktop 3P Auto Mode sends thinking:{type:"disabled"} with max_tokens:64 and a stop sequence. Inbound translation dropped the instruction — reasoning stayed undefined, indistinguishable from a request that never mentioned thinking — so the outbound Anthropic body omitted the field entirely. For Sonnet 5 an omitted thinking field means adaptive thinking is ON, and thinking shares max_tokens, so generation ran out of budget before it could emit </block>. Claude Code then retried, up to five times per tool approval. The gate is deliberately its own predicate rather than usesAdaptiveThinking(), which answers a different question: Fable always thinks and rejects an explicit disable, while Opus 4.7/4.8 leave thinking off when the field is omitted. Widening it would trade a silent truncation for a 400. Refs lidge-jun#545
1 parent a638fff commit 8bc14b0

4 files changed

Lines changed: 106 additions & 5 deletions

File tree

‎src/adapters/anthropic.ts‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,32 @@ function usesAdaptiveThinking(modelId: string): boolean {
420420
return major > minimum[0] || (major === minimum[0] && minor >= minimum[1]);
421421
}
422422

423+
/**
424+
* Claude families that (a) think by DEFAULT when the request omits `thinking`,
425+
* and (b) accept an explicit `thinking: {type: "disabled"}` to turn it off.
426+
*
427+
* Deliberately NOT `usesAdaptiveThinking()`, which answers a different question
428+
* (which wire shape a family accepts). The two sets differ in both directions:
429+
* Fable always thinks and REJECTS an explicit disable, while Opus 4.7/4.8 use
430+
* the adaptive wire but leave thinking off when the field is omitted, so they
431+
* need no disable at all. Seeded with the family where the defect reproduces
432+
* (#545); widen only with vendor evidence, since a wrong entry here turns a
433+
* silent truncation into a 400.
434+
*/
435+
const EXPLICIT_THINKING_DISABLE_FAMILY_MINIMUMS: Record<string, readonly [major: number, minor: number]> = {
436+
sonnet: [5, 0],
437+
};
438+
439+
function supportsExplicitThinkingDisable(modelId: string): boolean {
440+
const match = /^claude-([a-z]+)-(\d+)(?:-(\d{1,2}))?(?!\d)/.exec(modelId);
441+
if (!match) return false;
442+
const minimum = EXPLICIT_THINKING_DISABLE_FAMILY_MINIMUMS[match[1]];
443+
if (!minimum) return false;
444+
const major = Number(match[2]);
445+
const minor = match[3] === undefined ? 0 : Number(match[3]);
446+
return major > minimum[0] || (major === minimum[0] && minor >= minimum[1]);
447+
}
448+
423449
/** `output_config.effort` accepts low|medium|high|xhigh|max — "minimal" is rejected with a 400. */
424450
function adaptiveEffort(effort: string): string {
425451
return effort === "minimal" ? "low" : effort;
@@ -766,7 +792,14 @@ export function createAnthropicAdapter(provider: OcxProviderConfig, cacheRetenti
766792
// `reasoning` is a Codex effort string; "none" is the disable sentinel (see parser.ts
767793
// REASONING_EFFORTS). A bare truthy check would treat "none" as truthy and wrongly enable
768794
// extended thinking (and strip temperature/top_p), so gate on a real, non-disable effort.
769-
if (typeof parsed.options.reasoning === "string" && parsed.options.reasoning !== "none") {
795+
//
796+
// "none" is not the same as absent. Omitting `thinking` lets a default-on model think
797+
// anyway, and thinking shares the caller's `max_tokens` — which truncates a small-budget
798+
// request before it can emit its stop sequence (#545). Say "disabled" out loud where the
799+
// model both defaults to thinking and accepts being told not to.
800+
if (parsed.options.reasoning === "none" && supportsExplicitThinkingDisable(parsed.modelId)) {
801+
body.thinking = { type: "disabled" };
802+
} else if (typeof parsed.options.reasoning === "string" && parsed.options.reasoning !== "none") {
770803
if (usesAdaptiveThinking(parsed.modelId)) {
771804
// Adaptive-thinking models replace the token budget with an effort knob and reject
772805
// `thinking.type: "enabled"` outright. `max_tokens` still caps thinking plus visible

‎src/claude/inbound.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,13 @@ export function anthropicToResponsesTranslation(raw: unknown, cc?: OcxClaudeCode
498498
const thinking = raw.thinking;
499499
const outputConfigEffort = effortFromOutputConfig(raw.output_config);
500500
const thinkingDisabled = isRec(thinking) && thinking.type === "disabled";
501-
if (!thinkingDisabled && (isRec(thinking) || outputConfigEffort !== undefined)) {
501+
if (thinkingDisabled) {
502+
// An explicit "disabled" is an instruction, not an absence. Dropping it made this
503+
// indistinguishable from a request that never mentioned thinking — and for models that
504+
// think by default, omission means thinking is ON, sharing the caller's max_tokens (#545).
505+
// "none" is the parser's disable sentinel (parser.ts REASONING_EFFORTS).
506+
body.reasoning = { effort: "none", summary: "none" };
507+
} else if (isRec(thinking) || outputConfigEffort !== undefined) {
502508
const reasoning: Rec = { summary: "auto" };
503509
if (outputConfigEffort !== undefined) {
504510
// Adaptive wire: /effort arrives as output_config.effort (devlog 080).

‎tests/anthropic-reasoning.test.ts‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { describe, expect, test } from "bun:test";
22
import { createAnthropicAdapter as createAnthropicAdapterProduction } from "../src/adapters/anthropic";
33
import { parseRequest } from "../src/responses/parser";
4+
import { anthropicToResponsesBody } from "../src/claude/inbound";
45
import type { OcxParsedRequest, OcxProviderConfig } from "../src/types";
56
import { withTestTranslatorBudget } from "./helpers/translator-budget";
67

@@ -143,6 +144,39 @@ describe("anthropic extended-thinking gate", () => {
143144
expect(b.temperature).toBe(0.3);
144145
});
145146

147+
// #545: Claude Desktop's Auto Mode classifier sends thinking:{type:"disabled"} with
148+
// max_tokens:64. Omitting the field lets a default-on model think anyway, and thinking
149+
// shares that 64-token budget — so generation stopped before the stop sequence and the
150+
// client retried. Say "disabled" out loud, but only where the vendor accepts it.
151+
test("Sonnet 5 + reasoning 'none' sends an explicit thinking disable (#545)", async () => {
152+
const b = await bodyOf(parsed("none", { maxOutputTokens: 64, stopSequences: ["</block>"] }, "claude-sonnet-5"));
153+
expect(b.thinking).toEqual({ type: "disabled" });
154+
expect(b.output_config).toBeUndefined();
155+
// The caller's own limits must survive untouched — they were never the defect.
156+
expect(b.max_tokens).toBe(64);
157+
expect(b.stop_sequences).toEqual(["</block>"]);
158+
});
159+
160+
test("Sonnet 5 with reasoning OMITTED still omits thinking (#545)", async () => {
161+
// Absence is not a disable instruction: only an explicit "none" earns the explicit field.
162+
const b = await bodyOf(parsed(undefined, {}, "claude-sonnet-5"));
163+
expect(b.thinking).toBeUndefined();
164+
});
165+
166+
test.each([
167+
"claude-fable-5",
168+
"claude-opus-4-7",
169+
"claude-opus-4-8",
170+
"claude-haiku-4-5",
171+
"claude-sonnet-4-6",
172+
])("%s + 'none' sends NO explicit disable (#545 gate stays narrow)", async (modelId) => {
173+
// Fable always thinks and rejects an explicit disable; the Opus 4.7/4.8 adaptive wire
174+
// leaves thinking off when omitted. Widening the gate to every adaptive family would
175+
// trade a silent truncation for a 400.
176+
const b = await bodyOf(parsed("none", {}, modelId));
177+
expect(b.thinking).toBeUndefined();
178+
});
179+
146180
test("drops reconstructed Responses reasoning signatures when switching into Anthropic", async () => {
147181
const b = await bodyOf(parseRequest({
148182
model: "anthropic/claude-sonnet-4.5",
@@ -169,3 +203,26 @@ describe("anthropic extended-thinking gate", () => {
169203
expect(messages).toEqual([{ role: "user", content: "continue on anthropic" }]);
170204
});
171205
});
206+
207+
describe("Claude Desktop classifier round trip (#545)", () => {
208+
test("thinking:disabled survives inbound translation to the outbound Anthropic body", async () => {
209+
// The reporter's exact shape: a permission classifier with a 64-token budget that must
210+
// close its XML tag. Before the fix, "disabled" was dropped at the inbound hop and the
211+
// outbound request omitted `thinking` entirely, so Sonnet 5 thought anyway and spent the
212+
// budget before emitting </block>. Claude Code then retried, up to five times.
213+
const inbound = anthropicToResponsesBody({
214+
model: "claude-sonnet-5",
215+
max_tokens: 64,
216+
stop_sequences: ["</block>"],
217+
thinking: { type: "disabled" },
218+
system: "decide whether this tool call is allowed",
219+
messages: [{ role: "user", content: "<request>ls</request>" }],
220+
});
221+
222+
const body = await bodyOf(parseRequest(inbound));
223+
224+
expect(body.thinking).toEqual({ type: "disabled" });
225+
expect(body.max_tokens).toBe(64);
226+
expect(body.stop_sequences).toEqual(["</block>"]);
227+
});
228+
});

‎tests/claude-inbound.test.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ describe("claude inbound translation", () => {
9494
test("thinking variants", () => {
9595
const base = { model: "m", max_tokens: 10, messages: [{ role: "user", content: "hi" }] };
9696
expect((anthropicToResponsesBody({ ...base, thinking: { type: "adaptive" } }) as any).reasoning).toEqual({ summary: "auto" });
97-
expect((anthropicToResponsesBody({ ...base, thinking: { type: "disabled" } }) as any).reasoning).toBeUndefined();
97+
// "disabled" and omitted must NOT collapse to the same state: for a model that thinks by
98+
// default, omission means thinking is ON and shares the caller's max_tokens (#545).
99+
expect((anthropicToResponsesBody({ ...base, thinking: { type: "disabled" } }) as any).reasoning).toEqual({ effort: "none", summary: "none" }); // justified: sibling assertions in this test use the same cast
98100
expect((anthropicToResponsesBody(base) as any).reasoning).toBeUndefined();
99101
expect(effortForThinkingBudget(1024)).toBe("low");
100102
expect(effortForThinkingBudget(8192)).toBe("medium");
@@ -126,10 +128,13 @@ describe("claude inbound translation", () => {
126128
thinking: { type: "enabled", budget_tokens: 1024 },
127129
output_config: { effort: "xhigh" },
128130
}))).toEqual({ summary: "auto", effort: "xhigh" });
129-
// disabled thinking suppresses effort entirely (subagent wire, claude-code#65863)
131+
// disabled thinking suppresses effort entirely (subagent wire, claude-code#65863).
132+
// Still suppressed — "high" never reaches the wire — but now stated explicitly as the
133+
// "none" disable sentinel instead of by absence, so a default-on model is told to stop
134+
// rather than left to think anyway (#545).
130135
expect(reasoningOf(anthropicToResponsesBody({
131136
...base, thinking: { type: "disabled" }, output_config: { effort: "high" },
132-
}))).toBeUndefined();
137+
}))).toEqual({ effort: "none", summary: "none" });
133138
// unknown effort strings are dropped so downstream defaults win
134139
expect(reasoningOf(anthropicToResponsesBody({
135140
...base, thinking: { type: "adaptive" }, output_config: { effort: "turbo" },

0 commit comments

Comments
 (0)