Skip to content

Commit 399df48

Browse files
fix(claude): version errors from a custom endpoint no longer say to run claude update (pingdotgg#34176)
Co-authored-by: Julius Marminge <[email protected]>
1 parent 362ef9a commit 399df48

4 files changed

Lines changed: 106 additions & 9 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts‎

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6635,9 +6635,42 @@ describe("ClaudeAdapterV2 background wake turns", () => {
66356635
expected:
66366636
"Claude stopped: the prompt exceeds the model's context window. Run /compact or start a new thread.",
66376637
},
6638+
...[
6639+
{
6640+
name: "a model version error rewritten by a custom endpoint",
6641+
baseUrl: "https://gateway.example.test/v1",
6642+
installed: "2.1.322",
6643+
expected:
6644+
"The custom API endpoint gateway.example.test reported Claude Code 2.1.280, but this session runs Claude Code 2.1.322. Running claude update will not fix this; the endpoint or a proxy behind it rewrote the version.",
6645+
},
6646+
{ name: "a model version error without a custom endpoint", installed: "2.1.322" },
6647+
{
6648+
name: "a model version error from a Claude Code still below the required version",
6649+
baseUrl: "https://gateway.example.test/v1",
6650+
installed: "2.1.285",
6651+
},
6652+
].map((row) => {
6653+
const result =
6654+
"API Error: 400 Claude Code 2.1.280 does not support this model; version 2.1.290 or newer is required. Run 'claude update', or update the Claude desktop app, then try again.";
6655+
return {
6656+
...row,
6657+
isError: true,
6658+
result,
6659+
errors: undefined,
6660+
apiErrorStatus: undefined,
6661+
terminalReason: undefined,
6662+
environment: {
6663+
CLAUDE_CONFIG_DIR: "/synthetic/no-claude-config",
6664+
...(row.baseUrl === undefined ? {} : { ANTHROPIC_BASE_URL: row.baseUrl }),
6665+
},
6666+
expected: row.expected ?? result,
6667+
};
6668+
}),
66386669
])("reports terminal API error text for $name", (input) =>
66396670
Effect.gen(function* () {
6640-
const harness = yield* makeWakeHarness;
6671+
const harness = yield* makeWakeHarnessWithOptions(
6672+
"environment" in input ? { environment: input.environment } : undefined,
6673+
);
66416674
const now = yield* DateTime.now;
66426675
yield* harness.runtime.startTurn(
66436676
makeClaudeTestTurnInput({
@@ -6650,6 +6683,17 @@ describe("ClaudeAdapterV2 background wake turns", () => {
66506683
}),
66516684
);
66526685
const apiErrorStatus = input.apiErrorStatus ?? 400;
6686+
if ("installed" in input) {
6687+
yield* harness.offerAndWait(
6688+
claudeSdkFrame({
6689+
type: "system",
6690+
subtype: "init",
6691+
uuid: "00000000-0000-4000-8000-000000000698",
6692+
session_id: WAKE_NATIVE_SESSION,
6693+
claude_code_version: input.installed,
6694+
}),
6695+
);
6696+
}
66536697
yield* Queue.offer(
66546698
harness.sdkMessages,
66556699
makeResultFrame({

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts‎

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ import {
8484
import { AgentScope } from "@t3tools/shared/AgentScope";
8585
import * as HostProcess from "@t3tools/shared/HostProcess";
8686
import { applyClaudePromptEffortPrefix } from "@t3tools/shared/model";
87+
import { compareSemverVersions } from "@t3tools/shared/semver";
8788
import {
8889
CLAUDE_RESUME_COMPACTION_COMPACT_ANSWER,
8990
CLAUDE_RESUME_COMPACTION_NEVER_ANSWER,
@@ -165,7 +166,9 @@ import {
165166
} from "../../provider/Drivers/ClaudeSkillDispatch.ts";
166167
import {
167168
claudeLaunchArgDirectories,
169+
customClaudeApiEndpointHost,
168170
discoverClaudeSkills,
171+
readClaudeSettingsEnv,
169172
} from "../../provider/Drivers/ClaudeSkills.ts";
170173
import {
171174
compileClaudeModelSelection,
@@ -4106,6 +4109,25 @@ function gatewayCooldownResetAt(
41064109
: undefined;
41074110
}
41084111

4112+
const CLAUDE_UNSUPPORTED_VERSION_ERROR =
4113+
/^API Error: 400 Claude Code (\d+\.\d+\.\d+) does not support this model; version (\d+\.\d+\.\d+) or newer is required\./;
4114+
4115+
/** The version a gateway sent in place of the session's own, when the session itself meets the requirement. */
4116+
function claudeRewrittenVersion(
4117+
message: SDKResultMessage,
4118+
sessionVersion: string | undefined,
4119+
): string | undefined {
4120+
if (message.subtype !== "success" || message.terminal_reason !== "api_error") return undefined;
4121+
const [, reported, required] = CLAUDE_UNSUPPORTED_VERSION_ERROR.exec(message.result.trim()) ?? [];
4122+
if (reported === undefined || required === undefined || sessionVersion === undefined) {
4123+
return undefined;
4124+
}
4125+
return compareSemverVersions(reported, sessionVersion) < 0 &&
4126+
compareSemverVersions(sessionVersion, required) >= 0
4127+
? reported
4128+
: undefined;
4129+
}
4130+
41094131
function providerFailureFromResult(
41104132
message: SDKResultMessage,
41114133
receivedAt: DateTime.Utc,
@@ -4446,6 +4468,7 @@ interface ClaudeLiveQueryContext {
44464468
receivedInit: boolean;
44474469
// From system/init; feature-detects control requests such as MCP Apps.
44484470
capabilities: ReadonlySet<string>;
4471+
claudeCodeVersion: string | undefined;
44494472
permissionMode: PermissionMode;
44504473
stopping: boolean;
44514474
readonly subagentsFromEarlierProcesses: ReadonlySet<ActiveClaudeSubagent>;
@@ -10083,12 +10106,30 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1008310106
// The CLI also labels 403 authentication_failed; show its text because login cannot grant access.
1008410107
const permissionDenied =
1008510108
message.subtype === "success" && message.api_error_status === 403;
10109+
const rewrittenVersion = claudeRewrittenVersion(message, liveQuery.claudeCodeVersion);
10110+
const endpointHost =
10111+
rewrittenVersion === undefined
10112+
? undefined
10113+
: customClaudeApiEndpointHost(
10114+
(yield* readClaudeSettingsEnv(
10115+
adapterOptions.settings,
10116+
context.input.runtimePolicy.cwd ?? undefined,
10117+
adapterOptions.environment,
10118+
).pipe(
10119+
Effect.provideService(FileSystem.FileSystem, fileSystem),
10120+
Effect.provideService(Path.Path, path),
10121+
)).ANTHROPIC_BASE_URL ?? adapterOptions.environment.ANTHROPIC_BASE_URL,
10122+
);
10123+
const rewrittenVersionError =
10124+
endpointHost === undefined
10125+
? undefined
10126+
: `The custom API endpoint ${endpointHost} reported Claude Code ${rewrittenVersion}, but this session runs Claude Code ${liveQuery.claudeCodeVersion}. Running claude update will not fix this; the endpoint or a proxy behind it rewrote the version.`;
1008610127
const failureHint = permissionDenied
1008710128
? message.result
1008810129
: (context.authenticationFailureMessage ??
1008910130
(usageLimited
1009010131
? "Claude usage limit reached. Send the message again once the limit resets."
10091-
: undefined));
10132+
: rewrittenVersionError));
1009210133
// Usage-limited /compact reports success; fail it so queued messages wait for the limit to clear.
1009310134
const limitedCompaction =
1009410135
usageLimited && message.subtype === "success" && message.local_command === "compact"
@@ -11077,6 +11118,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1107711118
openedPermissionMode: queryOptions.permissionMode,
1107811119
receivedInit: false,
1107911120
capabilities: new Set(),
11121+
claudeCodeVersion: undefined,
1108011122
permissionMode: queryOptions.permissionMode,
1108111123
stopping: false,
1108211124
handedOffSubagents: new Set(),
@@ -11117,6 +11159,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1111711159
message.permissionMode ?? context.openedPermissionMode;
1111811160
context.receivedInit = true;
1111911161
context.capabilities = new Set(message.capabilities ?? []);
11162+
context.claudeCodeVersion = message.claude_code_version;
1112011163
}
1112111164
if (
1112211165
message.type === "system" &&

‎apps/server/src/provider/ClaudeProvider.ts‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ import {
4545
hasClaudeManagedMcpConfig,
4646
makeClaudeEnvironment,
4747
} from "./Drivers/ClaudeHome.ts";
48-
import { discoverClaudeSkills, readClaudeSettingsEnv } from "./Drivers/ClaudeSkills.ts";
48+
import {
49+
customClaudeApiEndpointHost,
50+
discoverClaudeSkills,
51+
readClaudeSettingsEnv,
52+
} from "./Drivers/ClaudeSkills.ts";
4953
import type { ProviderWorkspaceSnapshot } from "@t3tools/provider-core/server/driver";
5054
import { makeUnavailableUsageLimits } from "@t3tools/provider-core/server/usageLimits";
5155
import {
@@ -671,12 +675,7 @@ export const checkClaudeProviderStatus = Effect.fn("checkClaudeProviderStatus")(
671675
const baseUrl =
672676
(yield* readClaudeSettingsEnv(claudeSettings, cwd, resolvedEnvironment)).ANTHROPIC_BASE_URL ??
673677
resolvedEnvironment.ANTHROPIC_BASE_URL;
674-
const endpoint = URL.parse(baseUrl?.trim() ?? "");
675-
const customApiEndpoint =
676-
endpoint !== null &&
677-
(endpoint.protocol === "http:" || endpoint.protocol === "https:") &&
678-
endpoint.hostname !== "" &&
679-
endpoint.hostname !== "api.anthropic.com";
678+
const customApiEndpoint = customClaudeApiEndpointHost(baseUrl) !== undefined;
680679
if (!customApiEndpoint && claudeAuthStatus(capabilities) === "unauthenticated") {
681680
const path = yield* Path.Path;
682681
const configDir = (yield* makeClaudeEnvironment(claudeSettings, resolvedEnvironment))

‎apps/server/src/provider/Drivers/ClaudeSkills.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,17 @@ export const readClaudeSettingsEnv = Effect.fn("readClaudeSettingsEnv")(function
472472
return settingsEnv;
473473
});
474474

475+
/** The host of an `ANTHROPIC_BASE_URL` that is not Anthropic's own API. */
476+
export function customClaudeApiEndpointHost(baseUrl: string | undefined): string | undefined {
477+
const endpoint = URL.parse(baseUrl?.trim() ?? "");
478+
return endpoint !== null &&
479+
(endpoint.protocol === "http:" || endpoint.protocol === "https:") &&
480+
endpoint.hostname !== "" &&
481+
endpoint.hostname !== "api.anthropic.com"
482+
? endpoint.hostname
483+
: undefined;
484+
}
485+
475486
// Claude's --add-dir is repeatable and variadic; parseCliArgs keeps only one value.
476487
export function claudeLaunchArgDirectories(launchArgs?: string): ReadonlyArray<string> {
477488
const directories: string[] = [];

0 commit comments

Comments
 (0)