Skip to content

Commit 5d2973c

Browse files
authored
Merge pull request #954 from lidge-jun/codex/545-classifier-thinking-disabled
stack 4/7: keep an explicit thinking disable through translation (#545)
2 parents 53f390b + d1ec013 commit 5d2973c

5 files changed

Lines changed: 181 additions & 14 deletions

File tree

‎src/adapters/anthropic.ts‎

Lines changed: 69 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -408,16 +408,68 @@ const ADAPTIVE_THINKING_FAMILY_MINIMUMS: Record<string, readonly [major: number,
408408
fable: [0, 0],
409409
};
410410

411-
function usesAdaptiveThinking(modelId: string): boolean {
412-
// Minor is 1-2 digits with a non-digit lookahead so date-pinned ids ("claude-opus-4-20250514")
413-
// parse as minor 0 instead of minor 20250514; suffixed ids ("claude-opus-4-8[1m]") still match.
414-
const match = /^claude-([a-z]+)-(\d+)(?:-(\d{1,2}))?(?!\d)/.exec(modelId);
415-
if (!match) return false;
416-
const minimum = ADAPTIVE_THINKING_FAMILY_MINIMUMS[match[1]];
411+
/**
412+
* Family/version parse for a Claude model id, tolerant of a routing prefix.
413+
*
414+
* `parsed.modelId` is not always bare, and the slash can fall on either side.
415+
* A `modelMap` entry may point at a routed destination such as
416+
* `anthropic/claude-sonnet-5` (prefix), while a custom provider may expose a
417+
* native id such as `claude-sonnet-5/variant` (suffix); both survive routing's
418+
* known-id decoding. So this matches the segment that actually begins with
419+
* `claude-` rather than assuming it is the first or the last one. A capability
420+
* predicate that quietly returns false is worse than one that throws — the
421+
* request just goes out wrong.
422+
*
423+
* Minor is 1-2 digits with a non-digit lookahead so date-pinned ids
424+
* ("claude-opus-4-20250514") parse as minor 0 instead of minor 20250514;
425+
* suffixed ids ("claude-opus-4-8[1m]") still match.
426+
*/
427+
function claudeFamilyVersion(modelId: string): { family: string; major: number; minor: number } | undefined {
428+
// Find the segment that actually starts with `claude-`, rather than assuming it is either
429+
// the first (breaks `anthropic/claude-sonnet-5`) or the last (breaks `claude-sonnet-5/variant`,
430+
// where the slash carries a vendor suffix rather than a routing prefix).
431+
const match = /(?:^|\/)claude-([a-z]+)-(\d+)(?:-(\d{1,2}))?(?!\d)/.exec(modelId);
432+
if (!match) return undefined;
433+
return {
434+
family: match[1]!,
435+
major: Number(match[2]),
436+
minor: match[3] === undefined ? 0 : Number(match[3]),
437+
};
438+
}
439+
440+
function meetsFamilyMinimum(
441+
modelId: string,
442+
minimums: Record<string, readonly [major: number, minor: number]>,
443+
): boolean {
444+
const parsed = claudeFamilyVersion(modelId);
445+
if (!parsed) return false;
446+
const minimum = minimums[parsed.family];
417447
if (!minimum) return false;
418-
const major = Number(match[2]);
419-
const minor = match[3] === undefined ? 0 : Number(match[3]);
420-
return major > minimum[0] || (major === minimum[0] && minor >= minimum[1]);
448+
return parsed.major > minimum[0] || (parsed.major === minimum[0] && parsed.minor >= minimum[1]);
449+
}
450+
451+
function usesAdaptiveThinking(modelId: string): boolean {
452+
return meetsFamilyMinimum(modelId, ADAPTIVE_THINKING_FAMILY_MINIMUMS);
453+
}
454+
455+
/**
456+
* Claude families that (a) think by DEFAULT when the request omits `thinking`,
457+
* and (b) accept an explicit `thinking: {type: "disabled"}` to turn it off.
458+
*
459+
* Deliberately NOT `usesAdaptiveThinking()`, which answers a different question
460+
* (which wire shape a family accepts). The two sets differ in both directions:
461+
* Fable always thinks and REJECTS an explicit disable, while Opus 4.7/4.8 use
462+
* the adaptive wire but leave thinking off when the field is omitted, so they
463+
* need no disable at all. Seeded with the family where the defect reproduces
464+
* (#545); widen only with vendor evidence, since a wrong entry here turns a
465+
* silent truncation into a 400.
466+
*/
467+
const EXPLICIT_THINKING_DISABLE_FAMILY_MINIMUMS: Record<string, readonly [major: number, minor: number]> = {
468+
sonnet: [5, 0],
469+
};
470+
471+
function supportsExplicitThinkingDisable(modelId: string): boolean {
472+
return meetsFamilyMinimum(modelId, EXPLICIT_THINKING_DISABLE_FAMILY_MINIMUMS);
421473
}
422474

423475
/** `output_config.effort` accepts low|medium|high|xhigh|max — "minimal" is rejected with a 400. */
@@ -766,7 +818,14 @@ export function createAnthropicAdapter(provider: OcxProviderConfig, cacheRetenti
766818
// `reasoning` is a Codex effort string; "none" is the disable sentinel (see parser.ts
767819
// REASONING_EFFORTS). A bare truthy check would treat "none" as truthy and wrongly enable
768820
// 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") {
821+
//
822+
// "none" is not the same as absent. Omitting `thinking` lets a default-on model think
823+
// anyway, and thinking shares the caller's `max_tokens` — which truncates a small-budget
824+
// request before it can emit its stop sequence (#545). Say "disabled" out loud where the
825+
// model both defaults to thinking and accepts being told not to.
826+
if (parsed.options.reasoning === "none" && supportsExplicitThinkingDisable(parsed.modelId)) {
827+
body.thinking = { type: "disabled" };
828+
} else if (typeof parsed.options.reasoning === "string" && parsed.options.reasoning !== "none") {
770829
if (usesAdaptiveThinking(parsed.modelId)) {
771830
// Adaptive-thinking models replace the token budget with an effort knob and reject
772831
// `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: 85 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

@@ -136,13 +137,74 @@ describe("anthropic extended-thinking gate", () => {
136137
expect(b.output_config).toBeUndefined();
137138
});
138139

140+
// The adaptive-wire predicate shares the id parse with the #545 disable gate, so a
141+
// slash-carrying id must still pick the ADAPTIVE shape. Getting this wrong sends obsolete
142+
// manual `thinking.enabled` to a model that rejects it — a 400, not a silent truncation.
143+
test.each([
144+
"anthropic/claude-sonnet-5",
145+
"claude-sonnet-5/variant",
146+
"claude-opus-4-8/vendor-suffix",
147+
])("adaptive-thinking model %s keeps the adaptive wire shape", async (modelId) => {
148+
const b = await bodyOf(parsed("high", {}, modelId));
149+
expect(b.thinking).toEqual({ type: "adaptive" });
150+
expect(b.output_config).toEqual({ effort: "high" });
151+
});
152+
139153
test("adaptive-thinking model with reasoning 'none' sends no thinking config", async () => {
140154
const b = await bodyOf(parsed("none", { temperature: 0.3 }, "claude-fable-5"));
141155
expect(b.thinking).toBeUndefined();
142156
expect(b.output_config).toBeUndefined();
143157
expect(b.temperature).toBe(0.3);
144158
});
145159

160+
// #545: Claude Desktop's Auto Mode classifier sends thinking:{type:"disabled"} with
161+
// max_tokens:64. Omitting the field lets a default-on model think anyway, and thinking
162+
// shares that 64-token budget — so generation stopped before the stop sequence and the
163+
// client retried. Say "disabled" out loud, but only where the vendor accepts it.
164+
test.each([
165+
"claude-sonnet-5",
166+
"claude-sonnet-5-20260101",
167+
"claude-sonnet-5[1m]",
168+
// A modelMap entry can point at a routed destination, which custom-provider routing
169+
// decodes back into a slash-carrying native id. An id-shape miss here is silent: the
170+
// request simply goes out without the disable and the model thinks anyway.
171+
"anthropic/claude-sonnet-5",
172+
"openrouter/anthropic/claude-sonnet-5",
173+
// The slash can also carry a vendor SUFFIX rather than a routing prefix, so the family
174+
// segment is not reliably first or last. Both directions are real routed shapes.
175+
"claude-sonnet-5/variant",
176+
])("%s + reasoning 'none' sends an explicit thinking disable (#545)", async (modelId) => {
177+
const b = await bodyOf(parsed("none", { maxOutputTokens: 64, stopSequences: ["</block>"] }, modelId));
178+
expect(b.thinking).toEqual({ type: "disabled" });
179+
expect(b.output_config).toBeUndefined();
180+
// The caller's own limits must survive untouched — they were never the defect.
181+
expect(b.max_tokens).toBe(64);
182+
expect(b.stop_sequences).toEqual(["</block>"]);
183+
});
184+
185+
test("Sonnet 5 with reasoning OMITTED still omits thinking (#545)", async () => {
186+
// Absence is not a disable instruction: only an explicit "none" earns the explicit field.
187+
const b = await bodyOf(parsed(undefined, {}, "claude-sonnet-5"));
188+
expect(b.thinking).toBeUndefined();
189+
});
190+
191+
test.each([
192+
"claude-fable-5",
193+
"claude-opus-4-7",
194+
"claude-opus-4-8",
195+
"claude-haiku-4-5",
196+
"claude-sonnet-4-6",
197+
"anthropic/claude-fable-5",
198+
"claude-fable-5/foo",
199+
"not-a-claude-model",
200+
])("%s + 'none' sends NO explicit disable (#545 gate stays narrow)", async (modelId) => {
201+
// Fable always thinks and rejects an explicit disable; the Opus 4.7/4.8 adaptive wire
202+
// leaves thinking off when omitted. Widening the gate to every adaptive family would
203+
// trade a silent truncation for a 400.
204+
const b = await bodyOf(parsed("none", {}, modelId));
205+
expect(b.thinking).toBeUndefined();
206+
});
207+
146208
test("drops reconstructed Responses reasoning signatures when switching into Anthropic", async () => {
147209
const b = await bodyOf(parseRequest({
148210
model: "anthropic/claude-sonnet-4.5",
@@ -169,3 +231,26 @@ describe("anthropic extended-thinking gate", () => {
169231
expect(messages).toEqual([{ role: "user", content: "continue on anthropic" }]);
170232
});
171233
});
234+
235+
describe("Claude Desktop classifier round trip (#545)", () => {
236+
test("thinking:disabled survives inbound translation to the outbound Anthropic body", async () => {
237+
// The reporter's exact shape: a permission classifier with a 64-token budget that must
238+
// close its XML tag. Before the fix, "disabled" was dropped at the inbound hop and the
239+
// outbound request omitted `thinking` entirely, so Sonnet 5 thought anyway and spent the
240+
// budget before emitting </block>. Claude Code then retried, up to five times.
241+
const inbound = anthropicToResponsesBody({
242+
model: "claude-sonnet-5",
243+
max_tokens: 64,
244+
stop_sequences: ["</block>"],
245+
thinking: { type: "disabled" },
246+
system: "decide whether this tool call is allowed",
247+
messages: [{ role: "user", content: "<request>ls</request>" }],
248+
});
249+
250+
const body = await bodyOf(parseRequest(inbound));
251+
252+
expect(body.thinking).toEqual({ type: "disabled" });
253+
expect(body.max_tokens).toBe(64);
254+
expect(body.stop_sequences).toEqual(["</block>"]);
255+
});
256+
});

‎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" },

‎tests/cursor-effort-suffix.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,18 @@ describe("Cursor per-model reasoning-effort suffix", () => {
4848
expect(modelIdFor("cursor/claude-4.6-opus")).toBe("claude-4.6-opus-max");
4949
});
5050

51+
// #545 made Claude Desktop's `thinking:{type:"disabled"}` survive translation as the "none"
52+
// sentinel instead of being dropped. For a modelMap that routes such a request to Cursor,
53+
// that changes the selected tier — pin it so the cross-provider effect is deliberate.
54+
//
55+
// Cursor has no "off" for a reasoning model, so the lowest tier is the closest honest
56+
// reading of "do not think". Dropping the instruction sent these to the model's TOP tier,
57+
// which is the opposite of what the caller asked for.
58+
test("an explicit 'none' picks the lowest tier, not the top one (#545)", () => {
59+
expect(modelIdFor("cursor/claude-opus-4-8", "none")).toBe("claude-opus-4-8-low");
60+
expect(modelIdFor("cursor/claude-opus-4-8")).toBe("claude-opus-4-8-max");
61+
});
62+
5163
test("single-tier models always use their one tier", () => {
5264
expect(modelIdFor("cursor/gpt-5.5-extra", "low")).toBe("gpt-5.5-extra-high");
5365
expect(modelIdFor("cursor/claude-4.6-sonnet", "high")).toBe("claude-4.6-sonnet-medium");

0 commit comments

Comments
 (0)