From 9331fa3ecc79e6987e401d07d0447bc2d736580d Mon Sep 17 00:00:00 2001 From: Minxi Hou Date: Sun, 20 Sep 2026 14:24:57 -0400 Subject: [PATCH] fix(sse): dynamically inject skills beta only when code_execution tool is present (#14200) Anthropic requires the 'skills-2025-10-02' beta header only when a code_execution tool is present in the request body. Statically including it in ANTHROPIC_BETA_BASE causes upstream Anthropic to reject all tool-less requests with HTTP 400: 'Skills beta requires the code_execution tool to be included in the request.' Move skills-2025-10-02 out of the static beta lists and introduce maybeAppendSkillsBeta to conditionally append it only when a code_execution tool is detected in the body, while preserving explicit client-negotiated skills betas. Signed-off-by: Minxi Hou --- config/quality/eslint-suppressions.json | 2 +- open-sse/config/anthropicHeaders.ts | 192 ++++++++- open-sse/executors/base.ts | 11 +- open-sse/executors/default.ts | 9 +- open-sse/services/claudeCodeCompatible.ts | 27 +- open-sse/services/provider.ts | 8 +- tests/snapshots/provider/translate-path.json | 12 +- ...200-anthropic-skills-beta-toolless.test.ts | 372 ++++++++++++++++++ .../probe-9064-code-execution-beta.test.ts | 23 +- 9 files changed, 604 insertions(+), 52 deletions(-) create mode 100644 tests/unit/issue-14200-anthropic-skills-beta-toolless.test.ts diff --git a/config/quality/eslint-suppressions.json b/config/quality/eslint-suppressions.json index 25c15cc25ed3..820410a9427f 100644 --- a/config/quality/eslint-suppressions.json +++ b/config/quality/eslint-suppressions.json @@ -61,7 +61,7 @@ }, "open-sse/executors/base.ts": { "@typescript-eslint/no-unused-vars": { - "count": 6 + "count": 5 } }, "open-sse/executors/blackbox-web.ts": { diff --git a/open-sse/config/anthropicHeaders.ts b/open-sse/config/anthropicHeaders.ts index a8024be0041b..49e2d2cc86de 100644 --- a/open-sse/config/anthropicHeaders.ts +++ b/open-sse/config/anthropicHeaders.ts @@ -9,6 +9,7 @@ import { getClaudeCodeUserAgent, } from "@/shared/constants/claudeCodeClient"; import { modelSupportsContext1mBeta } from "../config/context1m.ts"; +import { usesCcWireImage } from "../services/ccWireImageBuiltins.ts"; export const ANTHROPIC_VERSION_HEADER = "2023-06-01"; @@ -28,7 +29,6 @@ const ANTHROPIC_BETA_BASE = Object.freeze([ "extended-cache-ttl-2025-04-11", "cache-diagnosis-2026-04-07", "code-execution-2025-08-25", - "skills-2025-10-02", ]); const CLAUDE_OAUTH_EXTRA_BETAS = Object.freeze(["fine-grained-tool-streaming-2025-05-14"]); @@ -43,6 +43,165 @@ export const ANTHROPIC_BETA_CLAUDE_OAUTH = [ ...ANTHROPIC_BETA_BASE.slice(3), ].join(","); +/** + * Anthropic Skills beta flag. Must only be emitted when a code_execution tool + * is declared in the request body; sending it on tool-less requests causes + * upstream Anthropic to reject with HTTP 400 (#14200). + */ +export const SKILLS_BETA_HEADER = "skills-2025-10-02"; + +/** + * Append an Anthropic Beta header token to an existing headers object. + * Preserves case of existing anthropic-beta / Anthropic-Beta header, + * deduplicates tokens, and creates anthropic-beta if not present. + */ +export function appendAnthropicBetaHeader( + headers: Record, + betaHeader: string +): void { + const existingKey = Object.keys(headers).find((key) => key.toLowerCase() === "anthropic-beta"); + if (!existingKey) { + headers["anthropic-beta"] = betaHeader; + return; + } + + const existingValues = String(headers[existingKey] || "") + .split(",") + .map((value) => value.trim()) + .filter(Boolean); + + if (!existingValues.includes(betaHeader)) { + headers[existingKey] = [...existingValues, betaHeader].join(","); + } +} + +/** + * Remove an Anthropic Beta header token from an existing headers object. + * Preserves case of existing anthropic-beta / Anthropic-Beta header. + */ +export function removeAnthropicBetaHeader( + headers: Record, + betaHeader: string +): void { + if (!headers || typeof headers !== "object") return; + const existingKey = Object.keys(headers).find((key) => key.toLowerCase() === "anthropic-beta"); + if (!existingKey) return; + + const existingValues = String(headers[existingKey] || "") + .split(",") + .map((value) => value.trim()) + .filter((value) => value && value !== betaHeader); + + if (existingValues.length > 0) { + headers[existingKey] = existingValues.join(","); + } else { + delete headers[existingKey]; + } +} + +/** + * Detect whether a request body contains an Anthropic code_execution tool. + * Anthropic requires the skills-2025-10-02 beta header only when a code_execution + * tool is present in the request body; sending it on tool-less requests causes + * upstream to reject with HTTP 400 (#14200). + * + * Accepts both parsed JSON objects and serialized JSON strings for robustness. + */ +export function hasCodeExecutionTool(body: unknown): boolean { + if (!body) return false; + let parsed: unknown = body; + if (typeof body === "string") { + try { + parsed = JSON.parse(body); + } catch { + return false; + } + } + if (!parsed || typeof parsed !== "object") return false; + const tools = (parsed as { tools?: unknown }).tools; + if (!Array.isArray(tools) || tools.length === 0) return false; + return tools.some((tool) => { + if (!tool || typeof tool !== "object") return false; + const t = tool as { type?: unknown; name?: unknown; function?: { name?: unknown } }; + if ( + typeof t.type === "string" && + (t.type === "code_execution" || t.type.startsWith("code_execution_")) + ) { + return true; + } + if (t.name === "code_execution") { + return true; + } + if ( + t.type === "function" && + t.function && + typeof t.function === "object" && + t.function.name === "code_execution" + ) { + return true; + } + return false; + }); +} + +/** + * Append the Skills beta header to outbound Anthropic-family request headers + * if and only if a code_execution tool is present in the request body. + * + * Single source of truth across base executor, default executor, and provider headers (#14200). + */ +export function maybeAppendSkillsBeta( + headers: Record, + provider: string | undefined | null, + body: unknown, + extraCondition = false +): void { + if (!headers || typeof headers !== "object") return; + if (!hasCodeExecutionTool(body)) return; + + const p = typeof provider === "string" ? provider : ""; + const isAnthropicFamily = + p === "anthropic" || + p === "claude" || + p.startsWith("anthropic-compatible-") || + usesCcWireImage(p) || + extraCondition; + + if (isAnthropicFamily) { + appendAnthropicBetaHeader(headers, SKILLS_BETA_HEADER); + } +} + +/** + * Synchronize the Skills beta header with the finalized request body. + * If the body contains a code_execution tool, ensures skills-2025-10-02 is present. + * If the body does not contain a code_execution tool (or tools were stripped/transformed), + * ensures skills-2025-10-02 is removed (#14200). + */ +export function syncSkillsBeta( + headers: Record, + provider: string | undefined | null, + body: unknown, + extraCondition = false +): void { + if (!headers || typeof headers !== "object") return; + const p = typeof provider === "string" ? provider : ""; + const isAnthropicFamily = + p === "anthropic" || + p === "claude" || + p.startsWith("anthropic-compatible-") || + usesCcWireImage(p) || + extraCondition; + + if (!isAnthropicFamily) return; + + if (hasCodeExecutionTool(body)) { + appendAnthropicBetaHeader(headers, SKILLS_BETA_HEADER); + } else { + removeAnthropicBetaHeader(headers, SKILLS_BETA_HEADER); + } +} + /** * Client-negotiated `anthropic-beta` values that are safe to forward to the * claude.ai backend on top of OmniRoute's own set. Kept to betas the backend @@ -102,12 +261,19 @@ export const FORWARDABLE_CLIENT_BETAS = Object.freeze([ * there with "long context beta is not yet available for this subscription" * (#10119). When no model is supplied (legacy callers without model resolution), * the prior forwarding behavior is preserved. + * + * `body` (optional) gate: when supplied, `skills-2025-10-02` is forwarded only + * if the request body contains a `code_execution` tool (#14200). Sending the + * skills beta on tool-less requests causes upstream Anthropic to reject with + * HTTP 400 "Skills beta requires the code_execution tool". When omitted or null, + * backward-compatible allowlist forwarding is preserved. */ export function mergeClientAnthropicBeta( base: string, clientBeta: string | null | undefined, allow: readonly string[] = FORWARDABLE_CLIENT_BETAS, - model?: string | null + model?: string | null, + body?: unknown ): string { const baseList = base .split(",") @@ -118,9 +284,15 @@ export function mergeClientAnthropicBeta( const allowList = allow .map((s) => s.toLowerCase()) .filter((lower) => { - if (lower !== "context-1m-2025-08-07") return true; - if (model === undefined || model === null || model === "") return true; - return modelSupportsContext1mBeta(model); + if (lower === "context-1m-2025-08-07") { + if (model === undefined || model === null || model === "") return true; + return modelSupportsContext1mBeta(model); + } + if (lower === "skills-2025-10-02") { + if (body === undefined || body === null) return true; + return hasCodeExecutionTool(body); + } + return true; }); const allowSet = new Set(allowList); for (const token of clientBeta @@ -150,13 +322,19 @@ export function mergeClientAnthropicBeta( export function applyClientAnthropicBeta( headers: Record, clientBeta: string | null | undefined, - options: { seedWhenAbsent?: boolean; model?: string | null } = {} + options: { seedWhenAbsent?: boolean; model?: string | null; body?: unknown } = {} ): void { if (typeof clientBeta !== "string" || !clientBeta.trim()) return; const existingKey = Object.keys(headers).find((key) => key.toLowerCase() === "anthropic-beta"); const key = existingKey ?? (options.seedWhenAbsent ? "anthropic-beta" : null); if (!key) return; - const merged = mergeClientAnthropicBeta(headers[key] ?? "", clientBeta, undefined, options.model); + const merged = mergeClientAnthropicBeta( + headers[key] ?? "", + clientBeta, + undefined, + options.model, + options.body + ); if (merged) headers[key] = merged; } diff --git a/open-sse/executors/base.ts b/open-sse/executors/base.ts index 7f07fa56a3ab..e181b66a1a2a 100644 --- a/open-sse/executors/base.ts +++ b/open-sse/executors/base.ts @@ -11,6 +11,8 @@ import { mergeCcHeaders, mergeClientAnthropicBeta, normalizeAnthropicHeaderVariants, + maybeAppendSkillsBeta, + syncSkillsBeta, } from "../config/anthropicHeaders.ts"; import { applyContextEditingToBody } from "../config/contextEditing.ts"; import { createCopilotIdentityFallback } from "./copilotIdentityFallback.ts"; @@ -515,6 +517,8 @@ export class BaseExecutor { headers["Accept"] = stream ? "text/event-stream" : "application/json"; + maybeAppendSkillsBeta(headers, this.provider, body, this.usesClaudeCodeProtocol(credentials)); + normalizeAnthropicHeaderVariants(headers); return headers; @@ -885,6 +889,9 @@ export class BaseExecutor { clampNestedThinkingBudget(transformedBody, thinkingBudgetClampedMax); } + // Re-synchronize skills beta with the finalized transformed body (#14200): + syncSkillsBeta(headers, this.provider, transformedBody, usesClaudeCodeProtocol); + // Timeout only covers response start; stream stalls are handled downstream. // #11526: streaming requests cap the headers-wait phase to a client-realistic // ceiling (see fetchStartTimeoutPolicy.ts) — non-streaming keeps the flat default. @@ -1228,7 +1235,9 @@ export class BaseExecutor { // Gate the client-negotiated context-1m beta on the RESOLVED target: // combo/fallback can route a request negotiated for a [1m] sibling onto a // model that does not qualify (e.g. Haiku), which Anthropic rejects (#10119). - model + model, + // Gate skills-2025-10-02 on presence of code_execution tool in transformed body (#14200): + tb ), "anthropic-dangerous-direct-browser-access": "true", "x-app": "cli", diff --git a/open-sse/executors/default.ts b/open-sse/executors/default.ts index dc2c9f34f938..75b3bda2901f 100644 --- a/open-sse/executors/default.ts +++ b/open-sse/executors/default.ts @@ -16,6 +16,7 @@ import { getModelTargetFormat } from "../config/providerModels.ts"; import { applyClientAnthropicBeta, normalizeAnthropicHeaderVariants, + maybeAppendSkillsBeta, } from "../config/anthropicHeaders.ts"; import { isOfficialAnthropicBaseUrl } from "../utils/anthropicHost.ts"; import { applyProviderRequestDefaults } from "../services/providerRequestDefaults.ts"; @@ -473,7 +474,9 @@ export class DefaultExecutor extends BaseExecutor { credentials, stream = true, clientHeaders?: Record | null, - model?: string | null + model?: string | null, + _health?: unknown, + body?: unknown ) { const { headers, effectiveKey } = this.buildHeadersPreamble(credentials, stream); @@ -690,12 +693,16 @@ export class DefaultExecutor extends BaseExecutor { // `model` gates the client-negotiated context-1m beta on the RESOLVED target: // combo/fallback can route a request negotiated for a [1m] sibling onto a model // that does not qualify (e.g. Haiku), which Anthropic rejects (#10119). + // `body` gates skills-2025-10-02 on presence of code_execution tool (#14200). applyClientAnthropicBeta(headers, clientBeta, { seedWhenAbsent: this.provider?.startsWith?.("anthropic-compatible-") === true, model, + body, }); } + maybeAppendSkillsBeta(headers, this.provider, body, this.usesClaudeCodeProtocol(credentials)); + normalizeAnthropicHeaderVariants(headers); return headers; diff --git a/open-sse/services/claudeCodeCompatible.ts b/open-sse/services/claudeCodeCompatible.ts index 117fa998761c..5fbffb3a3e61 100644 --- a/open-sse/services/claudeCodeCompatible.ts +++ b/open-sse/services/claudeCodeCompatible.ts @@ -141,25 +141,14 @@ export function joinClaudeCodeCompatibleUrl(baseUrl: string, path: string): stri return joinNormalizedBaseUrlAndPath(stripClaudeCodeCompatibleEndpointSuffix(baseUrl), path); } -export function appendAnthropicBetaHeader( - headers: Record, - betaHeader: string -): void { - const existingKey = Object.keys(headers).find((key) => key.toLowerCase() === "anthropic-beta"); - if (!existingKey) { - headers["anthropic-beta"] = betaHeader; - return; - } - - const existingValues = String(headers[existingKey] || "") - .split(",") - .map((value) => value.trim()) - .filter(Boolean); - - if (!existingValues.includes(betaHeader)) { - headers[existingKey] = [...existingValues, betaHeader].join(","); - } -} +export { + appendAnthropicBetaHeader, + removeAnthropicBetaHeader, + hasCodeExecutionTool, + maybeAppendSkillsBeta, + syncSkillsBeta, + SKILLS_BETA_HEADER, +} from "../config/anthropicHeaders.ts"; // Re-exported from the shared context1m module so existing importers of this // helper (base.ts) keep working; the eligibility list now has one source of truth. diff --git a/open-sse/services/provider.ts b/open-sse/services/provider.ts index 29fa9c2a712a..4fd67f2003aa 100644 --- a/open-sse/services/provider.ts +++ b/open-sse/services/provider.ts @@ -6,6 +6,7 @@ import { buildClaudeCodeCompatibleHeaders, CLAUDE_CODE_COMPATIBLE_DEFAULT_CHAT_PATH, joinClaudeCodeCompatibleUrl, + maybeAppendSkillsBeta, } from "./claudeCodeCompatible.ts"; import { getClaudeCodeCompatibleRequestDefaults } from "@/lib/providers/requestDefaults"; import { buildClineHeaders } from "@/shared/utils/clineAuth"; @@ -348,7 +349,6 @@ export function buildProviderUrl( // Build provider headers export function buildProviderHeaders(provider, credentials, stream = true, body = null) { - void body; const config = getProviderConfig(provider); const entry = getRegistryEntry(provider); const headers = { @@ -383,6 +383,9 @@ export function buildProviderHeaders(provider, credentials, stream = true, body ccHeaders["Authorization"] = `Bearer ${token}`; } } + // For CC-compatible providers returning early, ensure skills beta is conditionally applied + // (within this block, isClaudeCodeCompatible(provider) is guaranteed true): + maybeAppendSkillsBeta(ccHeaders, provider, body, true); return ccHeaders; } if (isAnthropicCompatible(provider)) { @@ -446,6 +449,9 @@ export function buildProviderHeaders(provider, credentials, stream = true, body headers["Accept"] = "text/event-stream"; } + // For standard/compatible provider paths, ensure skills beta is conditionally applied + maybeAppendSkillsBeta(headers, provider, body); + return headers; } diff --git a/tests/snapshots/provider/translate-path.json b/tests/snapshots/provider/translate-path.json index 3204162782ad..dab5e95735ab 100644 --- a/tests/snapshots/provider/translate-path.json +++ b/tests/snapshots/provider/translate-path.json @@ -349,20 +349,20 @@ "headers": { "apiKey": { "Accept": "text/event-stream", - "Anthropic-Beta": "claude-code-20250219,interleaved-thinking-2025-05-14,context-management-2025-06-27,prompt-caching-scope-2026-01-05,advanced-tool-use-2025-11-20,effort-2025-11-24,structured-outputs-2025-12-15,fast-mode-2026-02-01,redact-thinking-2026-02-12,token-efficient-tools-2026-03-28,advisor-tool-2026-03-01,extended-cache-ttl-2025-04-11,cache-diagnosis-2026-04-07,code-execution-2025-08-25,skills-2025-10-02", + "Anthropic-Beta": "claude-code-20250219,interleaved-thinking-2025-05-14,context-management-2025-06-27,prompt-caching-scope-2026-01-05,advanced-tool-use-2025-11-20,effort-2025-11-24,structured-outputs-2025-12-15,fast-mode-2026-02-01,redact-thinking-2026-02-12,token-efficient-tools-2026-03-28,advisor-tool-2026-03-01,extended-cache-ttl-2025-04-11,cache-diagnosis-2026-04-07,code-execution-2025-08-25", "Anthropic-Version": "2023-06-01", "Content-Type": "application/json", "x-api-key": "" }, "nonStream": { - "Anthropic-Beta": "claude-code-20250219,interleaved-thinking-2025-05-14,context-management-2025-06-27,prompt-caching-scope-2026-01-05,advanced-tool-use-2025-11-20,effort-2025-11-24,structured-outputs-2025-12-15,fast-mode-2026-02-01,redact-thinking-2026-02-12,token-efficient-tools-2026-03-28,advisor-tool-2026-03-01,extended-cache-ttl-2025-04-11,cache-diagnosis-2026-04-07,code-execution-2025-08-25,skills-2025-10-02", + "Anthropic-Beta": "claude-code-20250219,interleaved-thinking-2025-05-14,context-management-2025-06-27,prompt-caching-scope-2026-01-05,advanced-tool-use-2025-11-20,effort-2025-11-24,structured-outputs-2025-12-15,fast-mode-2026-02-01,redact-thinking-2026-02-12,token-efficient-tools-2026-03-28,advisor-tool-2026-03-01,extended-cache-ttl-2025-04-11,cache-diagnosis-2026-04-07,code-execution-2025-08-25", "Anthropic-Version": "2023-06-01", "Content-Type": "application/json", "x-api-key": "" }, "oauth": { "Accept": "text/event-stream", - "Anthropic-Beta": "claude-code-20250219,interleaved-thinking-2025-05-14,context-management-2025-06-27,prompt-caching-scope-2026-01-05,advanced-tool-use-2025-11-20,effort-2025-11-24,structured-outputs-2025-12-15,fast-mode-2026-02-01,redact-thinking-2026-02-12,token-efficient-tools-2026-03-28,advisor-tool-2026-03-01,extended-cache-ttl-2025-04-11,cache-diagnosis-2026-04-07,code-execution-2025-08-25,skills-2025-10-02", + "Anthropic-Beta": "claude-code-20250219,interleaved-thinking-2025-05-14,context-management-2025-06-27,prompt-caching-scope-2026-01-05,advanced-tool-use-2025-11-20,effort-2025-11-24,structured-outputs-2025-12-15,fast-mode-2026-02-01,redact-thinking-2026-02-12,token-efficient-tools-2026-03-28,advisor-tool-2026-03-01,extended-cache-ttl-2025-04-11,cache-diagnosis-2026-04-07,code-execution-2025-08-25", "Anthropic-Version": "2023-06-01", "Content-Type": "application/json", "x-api-key": "" @@ -1008,7 +1008,7 @@ "headers": { "apiKey": { "Accept": "text/event-stream", - "Anthropic-Beta": "claude-code-20250219,oauth-2025-04-20,interleaved-thinking-2025-05-14,fine-grained-tool-streaming-2025-05-14,context-management-2025-06-27,prompt-caching-scope-2026-01-05,advanced-tool-use-2025-11-20,effort-2025-11-24,structured-outputs-2025-12-15,fast-mode-2026-02-01,redact-thinking-2026-02-12,token-efficient-tools-2026-03-28,advisor-tool-2026-03-01,extended-cache-ttl-2025-04-11,cache-diagnosis-2026-04-07,code-execution-2025-08-25,skills-2025-10-02", + "Anthropic-Beta": "claude-code-20250219,oauth-2025-04-20,interleaved-thinking-2025-05-14,fine-grained-tool-streaming-2025-05-14,context-management-2025-06-27,prompt-caching-scope-2026-01-05,advanced-tool-use-2025-11-20,effort-2025-11-24,structured-outputs-2025-12-15,fast-mode-2026-02-01,redact-thinking-2026-02-12,token-efficient-tools-2026-03-28,advisor-tool-2026-03-01,extended-cache-ttl-2025-04-11,cache-diagnosis-2026-04-07,code-execution-2025-08-25", "Anthropic-Dangerous-Direct-Browser-Access": "true", "Anthropic-Version": "2023-06-01", "Content-Type": "application/json", @@ -1026,7 +1026,7 @@ "x-api-key": "" }, "nonStream": { - "Anthropic-Beta": "claude-code-20250219,oauth-2025-04-20,interleaved-thinking-2025-05-14,fine-grained-tool-streaming-2025-05-14,context-management-2025-06-27,prompt-caching-scope-2026-01-05,advanced-tool-use-2025-11-20,effort-2025-11-24,structured-outputs-2025-12-15,fast-mode-2026-02-01,redact-thinking-2026-02-12,token-efficient-tools-2026-03-28,advisor-tool-2026-03-01,extended-cache-ttl-2025-04-11,cache-diagnosis-2026-04-07,code-execution-2025-08-25,skills-2025-10-02", + "Anthropic-Beta": "claude-code-20250219,oauth-2025-04-20,interleaved-thinking-2025-05-14,fine-grained-tool-streaming-2025-05-14,context-management-2025-06-27,prompt-caching-scope-2026-01-05,advanced-tool-use-2025-11-20,effort-2025-11-24,structured-outputs-2025-12-15,fast-mode-2026-02-01,redact-thinking-2026-02-12,token-efficient-tools-2026-03-28,advisor-tool-2026-03-01,extended-cache-ttl-2025-04-11,cache-diagnosis-2026-04-07,code-execution-2025-08-25", "Anthropic-Dangerous-Direct-Browser-Access": "true", "Anthropic-Version": "2023-06-01", "Content-Type": "application/json", @@ -1045,7 +1045,7 @@ }, "oauth": { "Accept": "text/event-stream", - "Anthropic-Beta": "claude-code-20250219,oauth-2025-04-20,interleaved-thinking-2025-05-14,fine-grained-tool-streaming-2025-05-14,context-management-2025-06-27,prompt-caching-scope-2026-01-05,advanced-tool-use-2025-11-20,effort-2025-11-24,structured-outputs-2025-12-15,fast-mode-2026-02-01,redact-thinking-2026-02-12,token-efficient-tools-2026-03-28,advisor-tool-2026-03-01,extended-cache-ttl-2025-04-11,cache-diagnosis-2026-04-07,code-execution-2025-08-25,skills-2025-10-02", + "Anthropic-Beta": "claude-code-20250219,oauth-2025-04-20,interleaved-thinking-2025-05-14,fine-grained-tool-streaming-2025-05-14,context-management-2025-06-27,prompt-caching-scope-2026-01-05,advanced-tool-use-2025-11-20,effort-2025-11-24,structured-outputs-2025-12-15,fast-mode-2026-02-01,redact-thinking-2026-02-12,token-efficient-tools-2026-03-28,advisor-tool-2026-03-01,extended-cache-ttl-2025-04-11,cache-diagnosis-2026-04-07,code-execution-2025-08-25", "Anthropic-Dangerous-Direct-Browser-Access": "true", "Anthropic-Version": "2023-06-01", "Content-Type": "application/json", diff --git a/tests/unit/issue-14200-anthropic-skills-beta-toolless.test.ts b/tests/unit/issue-14200-anthropic-skills-beta-toolless.test.ts new file mode 100644 index 000000000000..5dc57f82f2d0 --- /dev/null +++ b/tests/unit/issue-14200-anthropic-skills-beta-toolless.test.ts @@ -0,0 +1,372 @@ +/** + * TDD Regression test for Issue #14200: + * Anthropic path always sends skills-2025-10-02, causing tool-less requests to fail with 400. + * + * Requirements: + * 1. Static ANTHROPIC_BETA_API_KEY and ANTHROPIC_BETA_CLAUDE_OAUTH must NOT contain skills-2025-10-02 statically. + * 2. Tool-less requests through Anthropic executors/builders must NOT emit skills-2025-10-02. + * 3. Requests with unrelated tools (e.g. get_weather) must NOT emit skills-2025-10-02. + * 4. Requests with code_execution tool (type: code_execution_20250825, name: code_execution, etc.) MUST emit skills-2025-10-02. + * 5. Clients explicitly passing Anthropic-Beta: skills-2025-10-02 only have it forwarded via mergeClientAnthropicBeta when body contains code_execution tool (prevents client injection bypass). + */ +import test from "node:test"; +import assert from "node:assert/strict"; + +const { + ANTHROPIC_BETA_API_KEY, + ANTHROPIC_BETA_CLAUDE_OAUTH, + SKILLS_BETA_HEADER, + hasCodeExecutionTool, + maybeAppendSkillsBeta, + mergeClientAnthropicBeta, + FORWARDABLE_CLIENT_BETAS, +} = await import("../../open-sse/config/anthropicHeaders.ts"); + +const { buildProviderHeaders } = await import("../../open-sse/services/provider.ts"); +const { DefaultExecutor } = await import("../../open-sse/executors/default.ts"); + +const SKILLS = SKILLS_BETA_HEADER; +const CODE_EXECUTION = "code-execution-2025-08-25"; + +test("Issue #14200: static ANTHROPIC_BETA_API_KEY must NOT include skills beta unconditionally", () => { + const tokens = ANTHROPIC_BETA_API_KEY.split(",").map((s) => s.trim()); + assert.ok( + !tokens.includes(SKILLS), + `skills beta must not be statically present in ANTHROPIC_BETA_API_KEY (causes 400 on tool-less requests): ${ANTHROPIC_BETA_API_KEY}` + ); + assert.ok( + tokens.includes(CODE_EXECUTION), + `code-execution beta must remain in ANTHROPIC_BETA_API_KEY: ${ANTHROPIC_BETA_API_KEY}` + ); +}); + +test("Issue #14200: static ANTHROPIC_BETA_CLAUDE_OAUTH must NOT include skills beta unconditionally", () => { + const tokens = ANTHROPIC_BETA_CLAUDE_OAUTH.split(",").map((s) => s.trim()); + assert.ok( + !tokens.includes(SKILLS), + `skills beta must not be statically present in ANTHROPIC_BETA_CLAUDE_OAUTH: ${ANTHROPIC_BETA_CLAUDE_OAUTH}` + ); +}); + +test("Issue #14200: hasCodeExecutionTool correctly identifies code execution tools", () => { + assert.equal(hasCodeExecutionTool(null), false); + assert.equal(hasCodeExecutionTool({}), false); + assert.equal(hasCodeExecutionTool({ messages: [{ role: "user", content: "hi" }] }), false); + assert.equal( + hasCodeExecutionTool({ + tools: [{ type: "function", function: { name: "get_weather" } }], + }), + false + ); + // Anthropic built-in code_execution tool type with version suffix + assert.equal( + hasCodeExecutionTool({ + tools: [{ type: "code_execution_20250825", name: "code_execution" }], + }), + true + ); + // Anthropic code_execution tool by name + assert.equal( + hasCodeExecutionTool({ + tools: [{ name: "code_execution" }], + }), + true + ); + // Anthropic code_execution tool by type prefix + assert.equal( + hasCodeExecutionTool({ + tools: [{ type: "code_execution" }], + }), + true + ); + // OpenAI format code_execution tool + assert.equal( + hasCodeExecutionTool({ + tools: [{ type: "function", function: { name: "code_execution" } }], + }), + true + ); + // Serialized JSON string body support (Issue review finding) + assert.equal( + hasCodeExecutionTool( + JSON.stringify({ + tools: [{ type: "code_execution_20250825", name: "code_execution" }], + }) + ), + true + ); + // Malformed / invalid inputs + assert.equal(hasCodeExecutionTool("invalid-json"), false); + assert.equal(hasCodeExecutionTool({ tools: "not-an-array" }), false); + assert.equal(hasCodeExecutionTool({ tools: ["just-a-string"] }), false); + assert.equal(hasCodeExecutionTool({ tools: [null, undefined, 123] }), false); +}); + +test("Issue #14200: buildProviderHeaders omits skills beta for tool-less requests", () => { + const headers = buildProviderHeaders("anthropic", { apiKey: "test-key" }, true, { + messages: [{ role: "user", content: "Reply only: OK" }], + }); + const beta = headers["Anthropic-Beta"] || headers["anthropic-beta"] || ""; + const tokens = beta.split(",").map((s) => s.trim()); + assert.ok(!tokens.includes(SKILLS), `tool-less request must not include skills beta: ${beta}`); + assert.ok( + tokens.includes(CODE_EXECUTION), + `code-execution beta should remain in headers: ${beta}` + ); +}); + +test("Issue #14200: buildProviderHeaders includes skills beta when code_execution tool is present", () => { + const headers = buildProviderHeaders("anthropic", { apiKey: "test-key" }, true, { + messages: [{ role: "user", content: "Run code" }], + tools: [{ type: "code_execution_20250825", name: "code_execution" }], + }); + const beta = headers["Anthropic-Beta"] || headers["anthropic-beta"] || ""; + const tokens = beta.split(",").map((s) => s.trim()); + assert.ok( + tokens.includes(SKILLS), + `request with code_execution tool must include skills beta: ${beta}` + ); + assert.ok( + tokens.includes(CODE_EXECUTION), + `request with code_execution tool must preserve existing betas: ${beta}` + ); + + // Also verify with serialized JSON string body + const headersWithString = buildProviderHeaders( + "anthropic", + { apiKey: "test-key" }, + true, + JSON.stringify({ + messages: [{ role: "user", content: "Run code" }], + tools: [{ type: "code_execution_20250825", name: "code_execution" }], + }) + ); + const betaStr = headersWithString["Anthropic-Beta"] || headersWithString["anthropic-beta"] || ""; + assert.ok( + betaStr + .split(",") + .map((s) => s.trim()) + .includes(SKILLS), + `request with stringified body must also include skills beta: ${betaStr}` + ); +}); + +test("Issue #14200: DefaultExecutor buildHeaders includes skills beta only when code_execution tool is present", () => { + const executor = new DefaultExecutor("anthropic"); + + // Tool-less request + const noToolHeaders = executor.buildHeaders( + { apiKey: "test-key" }, + true, + null, + "claude-sonnet-5", + undefined, + { messages: [{ role: "user", content: "hi" }] } + ); + const noToolBeta = noToolHeaders["Anthropic-Beta"] || noToolHeaders["anthropic-beta"] || ""; + assert.ok( + !noToolBeta + .split(",") + .map((s) => s.trim()) + .includes(SKILLS), + `tool-less executor headers must not have skills beta: ${noToolBeta}` + ); + + // Request with code_execution tool + const toolHeaders = executor.buildHeaders( + { apiKey: "test-key" }, + true, + null, + "claude-sonnet-5", + undefined, + { + messages: [{ role: "user", content: "run code" }], + tools: [{ type: "code_execution_20250825", name: "code_execution" }], + } + ); + const toolBeta = toolHeaders["Anthropic-Beta"] || toolHeaders["anthropic-beta"] || ""; + assert.ok( + toolBeta + .split(",") + .map((s) => s.trim()) + .includes(SKILLS), + `executor headers with code_execution must have skills beta: ${toolBeta}` + ); +}); + +test("Issue #14200: mergeClientAnthropicBeta drops client-negotiated skills beta on tool-less body", () => { + const baseHeaders = "claude-code-20250219,code-execution-2025-08-25"; + + // Case 1: Body has no code_execution tool -> skills beta must be filtered out + const toollessBody = { messages: [{ role: "user", content: "hello" }] }; + const mergedToolless = mergeClientAnthropicBeta( + baseHeaders, + `claude-code-20250219,${SKILLS}`, + undefined, + null, + toollessBody + ); + const toollessTokens = mergedToolless.split(",").map((s) => s.trim()); + assert.ok( + !toollessTokens.includes(SKILLS), + `skills beta must be omitted when client requested it but body lacks code_execution tool: ${mergedToolless}` + ); + + // Case 2: Body has code_execution tool -> skills beta must be forwarded + const toolBody = { + messages: [{ role: "user", content: "run code" }], + tools: [{ type: "code_execution_20250825", name: "code_execution" }], + }; + const mergedTool = mergeClientAnthropicBeta( + baseHeaders, + `claude-code-20250219,${SKILLS}`, + undefined, + null, + toolBody + ); + const toolTokens = mergedTool.split(",").map((s) => s.trim()); + assert.ok( + toolTokens.includes(SKILLS), + `skills beta must be forwarded when client requested it and body has code_execution tool: ${mergedTool}` + ); + + // Case 3: Legacy/direct caller with undefined or null body preserves backward compatibility + const mergedLegacy = mergeClientAnthropicBeta(baseHeaders, `claude-code-20250219,${SKILLS}`); + const legacyTokens = mergedLegacy.split(",").map((s) => s.trim()); + assert.ok( + legacyTokens.includes(SKILLS), + `legacy caller with undefined body must preserve skills beta: ${mergedLegacy}` + ); + const mergedNullBody = mergeClientAnthropicBeta( + baseHeaders, + `claude-code-20250219,${SKILLS}`, + undefined, + null, + null + ); + const nullBodyTokens = mergedNullBody.split(",").map((s) => s.trim()); + assert.ok( + nullBodyTokens.includes(SKILLS), + `legacy caller with explicit null body must preserve skills beta: ${mergedNullBody}` + ); + assert.ok( + FORWARDABLE_CLIENT_BETAS.includes(SKILLS), + `FORWARDABLE_CLIENT_BETAS must still include skills beta` + ); +}); + +test("Issue #14200: Claude-Code-Compatible providers (agentrouter and anthropic-compatible-cc-*) include skills beta only with code_execution", () => { + // 0. Explicit null body check + const nullBodyHeaders = buildProviderHeaders("anthropic", { apiKey: "test-key" }, true, null); + const nullBodyBeta = nullBodyHeaders["Anthropic-Beta"] || nullBodyHeaders["anthropic-beta"] || ""; + assert.ok( + !nullBodyBeta + .split(",") + .map((s) => s.trim()) + .includes(SKILLS), + `explicit null body must not have skills beta: ${nullBodyBeta}` + ); + + // 1. Built-in usesCcWireImage provider (agentrouter) + const noToolHeaders = buildProviderHeaders("agentrouter", { apiKey: "test-key" }, true, { + messages: [{ role: "user", content: "hi" }], + }); + const noToolBeta = noToolHeaders["Anthropic-Beta"] || noToolHeaders["anthropic-beta"] || ""; + assert.ok( + !noToolBeta + .split(",") + .map((s) => s.trim()) + .includes(SKILLS), + `tool-less agentrouter must not have skills beta: ${noToolBeta}` + ); + + const toolHeaders = buildProviderHeaders("agentrouter", { apiKey: "test-key" }, true, { + messages: [{ role: "user", content: "run code" }], + tools: [{ type: "code_execution_20250825", name: "code_execution" }], + }); + const toolBeta = toolHeaders["Anthropic-Beta"] || toolHeaders["anthropic-beta"] || ""; + assert.ok( + toolBeta + .split(",") + .map((s) => s.trim()) + .includes(SKILLS), + `agentrouter with code_execution must have skills beta: ${toolBeta}` + ); + + // 2. Prefix-based Claude-Code-Compatible provider (anthropic-compatible-cc-custom) + const prefixNoToolHeaders = buildProviderHeaders( + "anthropic-compatible-cc-custom", + { apiKey: "test-key" }, + true, + { + messages: [{ role: "user", content: "hi" }], + } + ); + const prefixNoToolBeta = + prefixNoToolHeaders["Anthropic-Beta"] || prefixNoToolHeaders["anthropic-beta"] || ""; + assert.ok( + !prefixNoToolBeta + .split(",") + .map((s) => s.trim()) + .includes(SKILLS), + `tool-less anthropic-compatible-cc-custom must not have skills beta: ${prefixNoToolBeta}` + ); + + const prefixToolHeaders = buildProviderHeaders( + "anthropic-compatible-cc-custom", + { apiKey: "test-key" }, + true, + { + messages: [{ role: "user", content: "run code" }], + tools: [{ type: "code_execution_20250825", name: "code_execution" }], + } + ); + const prefixToolBeta = + prefixToolHeaders["Anthropic-Beta"] || prefixToolHeaders["anthropic-beta"] || ""; + assert.ok( + prefixToolBeta + .split(",") + .map((s) => s.trim()) + .includes(SKILLS), + `anthropic-compatible-cc-custom with code_execution must have skills beta: ${prefixToolBeta}` + ); +}); + +test("Issue #14200: maybeAppendSkillsBeta does NOT inject skills beta for OpenAI-compatible providers even if body has code_execution", () => { + const headers: Record = { Authorization: "Bearer token" }; + maybeAppendSkillsBeta(headers, "openai", { + tools: [{ type: "code_execution_20250825" }], + }); + assert.strictEqual( + headers["anthropic-beta"], + undefined, + "non-Anthropic provider must not receive anthropic-beta" + ); + + // Even if non-Anthropic provider headers somehow had anthropic-beta, do not inject skills + const headersWithBeta: Record = { + Authorization: "Bearer sk-test", + "anthropic-beta": "prompt-caching-2024-07-31", + }; + maybeAppendSkillsBeta(headersWithBeta, "openai", { + tools: [{ type: "code_execution_20250825" }], + }); + assert.strictEqual( + headersWithBeta["anthropic-beta"], + "prompt-caching-2024-07-31", + "openai provider must never receive skills beta" + ); + + const headersEmptyProvider: Record = { + Authorization: "Bearer sk-test", + "anthropic-beta": "prompt-caching-2024-07-31", + }; + maybeAppendSkillsBeta(headersEmptyProvider, "", { + tools: [{ type: "code_execution_20250825" }], + }); + assert.strictEqual( + headersEmptyProvider["anthropic-beta"], + "prompt-caching-2024-07-31", + "empty provider string must not be assumed Anthropic family (#14200)" + ); +}); diff --git a/tests/unit/probe-9064-code-execution-beta.test.ts b/tests/unit/probe-9064-code-execution-beta.test.ts index 153ec533864e..ace6adf66ceb 100644 --- a/tests/unit/probe-9064-code-execution-beta.test.ts +++ b/tests/unit/probe-9064-code-execution-beta.test.ts @@ -31,11 +31,11 @@ test("#9064 static ANTHROPIC_BETA_API_KEY must include code-execution beta", () ); }); -test("#9064 static ANTHROPIC_BETA_API_KEY must include skills beta", () => { +test("#14200 static ANTHROPIC_BETA_API_KEY must NOT include skills beta unconditionally (dynamic only)", () => { const tokens = ANTHROPIC_BETA_API_KEY.split(",").map((s) => s.trim()); assert.ok( - tokens.includes(SKILLS), - `skills beta missing from ANTHROPIC_BETA_API_KEY: ${ANTHROPIC_BETA_API_KEY}` + !tokens.includes(SKILLS), + `skills beta must not be statically included in ANTHROPIC_BETA_API_KEY to prevent 400 on tool-less requests: ${ANTHROPIC_BETA_API_KEY}` ); }); @@ -47,22 +47,13 @@ test("#9064 mergeClientAnthropicBeta must forward client-negotiated code-executi `claude-code-20250219,${CODE_EXECUTION}` ); const tokens = out.split(",").map((s) => s.trim()); - assert.ok( - tokens.includes(CODE_EXECUTION), - `client code-execution beta dropped: ${out}` - ); + assert.ok(tokens.includes(CODE_EXECUTION), `client code-execution beta dropped: ${out}`); assert.ok(FORWARDABLE_CLIENT_BETAS.includes(CODE_EXECUTION)); }); test("#9064 mergeClientAnthropicBeta must forward client-negotiated skills beta", () => { - const out = mergeClientAnthropicBeta( - ANTHROPIC_BETA_API_KEY, - `claude-code-20250219,${SKILLS}` - ); + const out = mergeClientAnthropicBeta(ANTHROPIC_BETA_API_KEY, `claude-code-20250219,${SKILLS}`); const tokens = out.split(",").map((s) => s.trim()); - assert.ok( - tokens.includes(SKILLS), - `client skills beta dropped: ${out}` - ); + assert.ok(tokens.includes(SKILLS), `client skills beta dropped: ${out}`); assert.ok(FORWARDABLE_CLIENT_BETAS.includes(SKILLS)); -}); \ No newline at end of file +});