| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154 |
- import { describe, expect, it } from "vitest";
- import {
- applyAnthropicProviderOverrides,
- applyAnthropicProviderOverridesWithAudit,
- } from "@/lib/anthropic/provider-overrides";
- describe("Anthropic Provider Overrides", () => {
- describe("Provider type filtering", () => {
- it("should return unchanged request for non-claude/claude-auth providers (codex)", () => {
- const provider = {
- providerType: "codex",
- anthropicMaxTokensPreference: "32000",
- anthropicThinkingBudgetPreference: "10240",
- };
- const input: Record<string, unknown> = {
- model: "claude-3-opus-20240229",
- messages: [],
- max_tokens: 8000,
- };
- const output = applyAnthropicProviderOverrides(provider, input);
- expect(output).toBe(input);
- expect(output).toEqual(input);
- });
- it("should return unchanged request for non-claude/claude-auth providers (gemini)", () => {
- const provider = {
- providerType: "gemini",
- anthropicMaxTokensPreference: "32000",
- anthropicThinkingBudgetPreference: "10240",
- };
- const input: Record<string, unknown> = {
- model: "gemini-pro",
- messages: [],
- max_tokens: 8000,
- };
- const output = applyAnthropicProviderOverrides(provider, input);
- expect(output).toBe(input);
- });
- it("should return unchanged request for non-claude/claude-auth providers (openai-compatible)", () => {
- const provider = {
- providerType: "openai-compatible",
- anthropicMaxTokensPreference: "16000",
- };
- const input: Record<string, unknown> = {
- model: "gpt-4",
- messages: [],
- };
- const output = applyAnthropicProviderOverrides(provider, input);
- expect(output).toBe(input);
- });
- it("should apply overrides for 'claude' provider type", () => {
- const provider = {
- providerType: "claude",
- anthropicMaxTokensPreference: "32000",
- };
- const input: Record<string, unknown> = {
- model: "claude-3-opus-20240229",
- messages: [],
- max_tokens: 8000,
- };
- const output = applyAnthropicProviderOverrides(provider, input);
- expect(output.max_tokens).toBe(32000);
- });
- it("should apply overrides for 'claude-auth' provider type", () => {
- const provider = {
- providerType: "claude-auth",
- anthropicMaxTokensPreference: "16000",
- };
- const input: Record<string, unknown> = {
- model: "claude-3-sonnet-20240229",
- messages: [],
- max_tokens: 4000,
- };
- const output = applyAnthropicProviderOverrides(provider, input);
- expect(output.max_tokens).toBe(16000);
- });
- });
- describe("max_tokens override", () => {
- it("should not change request when preference is 'inherit'", () => {
- const provider = {
- providerType: "claude",
- anthropicMaxTokensPreference: "inherit",
- };
- const input: Record<string, unknown> = {
- model: "claude-3-opus-20240229",
- messages: [],
- max_tokens: 8000,
- };
- const snapshot = structuredClone(input);
- const output = applyAnthropicProviderOverrides(provider, input);
- expect(output).toEqual(snapshot);
- });
- it("should not change request when preference is null", () => {
- const provider = {
- providerType: "claude",
- anthropicMaxTokensPreference: null,
- };
- const input: Record<string, unknown> = {
- model: "claude-3-opus-20240229",
- messages: [],
- max_tokens: 8000,
- };
- const snapshot = structuredClone(input);
- const output = applyAnthropicProviderOverrides(provider, input);
- expect(output).toEqual(snapshot);
- });
- it("should not change request when preference is undefined", () => {
- const provider = {
- providerType: "claude",
- anthropicMaxTokensPreference: undefined,
- };
- const input: Record<string, unknown> = {
- model: "claude-3-opus-20240229",
- messages: [],
- max_tokens: 8000,
- };
- const snapshot = structuredClone(input);
- const output = applyAnthropicProviderOverrides(provider, input);
- expect(output).toEqual(snapshot);
- });
- it("should set max_tokens to numeric value when preference is valid string '32000'", () => {
- const provider = {
- providerType: "claude",
- anthropicMaxTokensPreference: "32000",
- };
- const input: Record<string, unknown> = {
- model: "claude-3-opus-20240229",
- messages: [],
- };
- const output = applyAnthropicProviderOverrides(provider, input);
- expect(output.max_tokens).toBe(32000);
- });
- it("should overwrite existing max_tokens value", () => {
- const provider = {
- providerType: "claude",
- anthropicMaxTokensPreference: "64000",
- };
- const input: Record<string, unknown> = {
- model: "claude-3-opus-20240229",
- messages: [],
- max_tokens: 4000,
- };
- const output = applyAnthropicProviderOverrides(provider, input);
- expect(output.max_tokens).toBe(64000);
- expect(input.max_tokens).toBe(4000);
- });
- it("should not change max_tokens for invalid numeric string", () => {
- const provider = {
- providerType: "claude",
- anthropicMaxTokensPreference: "invalid",
- };
- const input: Record<string, unknown> = {
- model: "claude-3-opus-20240229",
- messages: [],
- max_tokens: 8000,
- };
- const snapshot = structuredClone(input);
- const output = applyAnthropicProviderOverrides(provider, input);
- expect(output).toEqual(snapshot);
- });
- });
- describe("thinking.budget_tokens override", () => {
- it("should not change request when preference is 'inherit'", () => {
- const provider = {
- providerType: "claude",
- anthropicThinkingBudgetPreference: "inherit",
- };
- const input: Record<string, unknown> = {
- model: "claude-3-opus-20240229",
- messages: [],
- thinking: { type: "enabled", budget_tokens: 5000 },
- };
- const snapshot = structuredClone(input);
- const output = applyAnthropicProviderOverrides(provider, input);
- expect(output).toEqual(snapshot);
- });
- it("should not change request when preference is null", () => {
- const provider = {
- providerType: "claude",
- anthropicThinkingBudgetPreference: null,
- };
- const input: Record<string, unknown> = {
- model: "claude-3-opus-20240229",
- messages: [],
- thinking: { type: "enabled", budget_tokens: 5000 },
- };
- const snapshot = structuredClone(input);
- const output = applyAnthropicProviderOverrides(provider, input);
- expect(output).toEqual(snapshot);
- });
- it("should not change request when preference is undefined", () => {
- const provider = {
- providerType: "claude",
- anthropicThinkingBudgetPreference: undefined,
- };
- const input: Record<string, unknown> = {
- model: "claude-3-opus-20240229",
- messages: [],
- thinking: { type: "enabled", budget_tokens: 5000 },
- };
- const snapshot = structuredClone(input);
- const output = applyAnthropicProviderOverrides(provider, input);
- expect(output).toEqual(snapshot);
- });
- it("should set thinking.budget_tokens and thinking.type when preference is valid '10240'", () => {
- const provider = {
- providerType: "claude",
- anthropicThinkingBudgetPreference: "10240",
- };
- const input: Record<string, unknown> = {
- model: "claude-3-opus-20240229",
- messages: [],
- max_tokens: 32000,
- };
- const output = applyAnthropicProviderOverrides(provider, input);
- expect(output.thinking).toEqual({
- type: "enabled",
- budget_tokens: 10240,
- });
- });
- it("should preserve existing thinking properties not overridden", () => {
- const provider = {
- providerType: "claude",
- anthropicThinkingBudgetPreference: "8000",
- };
- const input: Record<string, unknown> = {
- model: "claude-3-opus-20240229",
- messages: [],
- max_tokens: 32000,
- thinking: {
- type: "disabled",
- budget_tokens: 2000,
- custom_field: "preserve_me",
- },
- };
- const output = applyAnthropicProviderOverrides(provider, input);
- const thinking = output.thinking as Record<string, unknown>;
- expect(thinking.type).toBe("enabled");
- expect(thinking.budget_tokens).toBe(8000);
- expect(thinking.custom_field).toBe("preserve_me");
- });
- it("should create thinking object if not present", () => {
- const provider = {
- providerType: "claude",
- anthropicThinkingBudgetPreference: "5000",
- };
- const input: Record<string, unknown> = {
- model: "claude-3-opus-20240229",
- messages: [],
- max_tokens: 10000,
- };
- const output = applyAnthropicProviderOverrides(provider, input);
- expect(output.thinking).toBeDefined();
- const thinking = output.thinking as Record<string, unknown>;
- expect(thinking.type).toBe("enabled");
- expect(thinking.budget_tokens).toBe(5000);
- });
- it("should handle non-object thinking value by replacing it", () => {
- const provider = {
- providerType: "claude",
- anthropicThinkingBudgetPreference: "6000",
- };
- const input: Record<string, unknown> = {
- model: "claude-3-opus-20240229",
- messages: [],
- max_tokens: 32000,
- thinking: "invalid_string_value",
- };
- const output = applyAnthropicProviderOverrides(provider, input);
- expect(output.thinking).toEqual({
- type: "enabled",
- budget_tokens: 6000,
- });
- });
- });
- describe("Clamping logic", () => {
- it("should clamp budget_tokens to max_tokens - 1 when budget_tokens >= max_tokens (overridden max_tokens)", () => {
- const provider = {
- providerType: "claude",
- anthropicMaxTokensPreference: "10000",
- anthropicThinkingBudgetPreference: "15000",
- };
- const input: Record<string, unknown> = {
- model: "claude-3-opus-20240229",
- messages: [],
- };
- const output = applyAnthropicProviderOverrides(provider, input);
- expect(output.max_tokens).toBe(10000);
- const thinking = output.thinking as Record<string, unknown>;
- expect(thinking.budget_tokens).toBe(9999);
- });
- it("should clamp budget_tokens to max_tokens - 1 when budget_tokens >= max_tokens (request-provided max_tokens)", () => {
- const provider = {
- providerType: "claude",
- anthropicThinkingBudgetPreference: "20000",
- };
- const input: Record<string, unknown> = {
- model: "claude-3-opus-20240229",
- messages: [],
- max_tokens: 16000,
- };
- const output = applyAnthropicProviderOverrides(provider, input);
- const thinking = output.thinking as Record<string, unknown>;
- expect(thinking.budget_tokens).toBe(15999);
- });
- it("should clamp budget_tokens when exactly equal to max_tokens", () => {
- const provider = {
- providerType: "claude",
- anthropicMaxTokensPreference: "8000",
- anthropicThinkingBudgetPreference: "8000",
- };
- const input: Record<string, unknown> = {
- model: "claude-3-opus-20240229",
- messages: [],
- };
- const output = applyAnthropicProviderOverrides(provider, input);
- expect(output.max_tokens).toBe(8000);
- const thinking = output.thinking as Record<string, unknown>;
- expect(thinking.budget_tokens).toBe(7999);
- });
- it("should not clamp when budget_tokens < max_tokens", () => {
- const provider = {
- providerType: "claude",
- anthropicMaxTokensPreference: "32000",
- anthropicThinkingBudgetPreference: "10000",
- };
- const input: Record<string, unknown> = {
- model: "claude-3-opus-20240229",
- messages: [],
- };
- const output = applyAnthropicProviderOverrides(provider, input);
- expect(output.max_tokens).toBe(32000);
- const thinking = output.thinking as Record<string, unknown>;
- expect(thinking.budget_tokens).toBe(10000);
- });
- it("should not clamp when max_tokens is not set", () => {
- const provider = {
- providerType: "claude",
- anthropicThinkingBudgetPreference: "50000",
- };
- const input: Record<string, unknown> = {
- model: "claude-3-opus-20240229",
- messages: [],
- };
- const output = applyAnthropicProviderOverrides(provider, input);
- const thinking = output.thinking as Record<string, unknown>;
- expect(thinking.budget_tokens).toBe(50000);
- });
- it("should skip thinking override when clamped budget_tokens would be below 1024 (API minimum)", () => {
- const provider = {
- providerType: "claude",
- anthropicMaxTokensPreference: "500",
- anthropicThinkingBudgetPreference: "10000",
- };
- const input: Record<string, unknown> = {
- model: "claude-3-opus-20240229",
- messages: [],
- };
- const output = applyAnthropicProviderOverrides(provider, input);
- expect(output.max_tokens).toBe(500);
- expect(output.thinking).toBeUndefined();
- });
- it("should skip thinking override when budget_tokens preference itself is below 1024", () => {
- const provider = {
- providerType: "claude",
- anthropicThinkingBudgetPreference: "500",
- };
- const input: Record<string, unknown> = {
- model: "claude-3-opus-20240229",
- messages: [],
- max_tokens: 32000,
- };
- const output = applyAnthropicProviderOverrides(provider, input);
- expect(output.thinking).toBeUndefined();
- });
- it("should skip thinking override when max_tokens is exactly 1024 (clamped would be 1023)", () => {
- const provider = {
- providerType: "claude",
- anthropicMaxTokensPreference: "1024",
- anthropicThinkingBudgetPreference: "2000",
- };
- const input: Record<string, unknown> = {
- model: "claude-3-opus-20240229",
- messages: [],
- };
- const output = applyAnthropicProviderOverrides(provider, input);
- expect(output.max_tokens).toBe(1024);
- expect(output.thinking).toBeUndefined();
- });
- it("should apply thinking override when clamped budget_tokens is exactly 1024", () => {
- const provider = {
- providerType: "claude",
- anthropicMaxTokensPreference: "1025",
- anthropicThinkingBudgetPreference: "2000",
- };
- const input: Record<string, unknown> = {
- model: "claude-3-opus-20240229",
- messages: [],
- };
- const output = applyAnthropicProviderOverrides(provider, input);
- expect(output.max_tokens).toBe(1025);
- const thinking = output.thinking as Record<string, unknown>;
- expect(thinking.budget_tokens).toBe(1024);
- expect(thinking.type).toBe("enabled");
- });
- it("should apply thinking override when budget_tokens is exactly 1024 and no clamping needed", () => {
- const provider = {
- providerType: "claude",
- anthropicThinkingBudgetPreference: "1024",
- };
- const input: Record<string, unknown> = {
- model: "claude-3-opus-20240229",
- messages: [],
- max_tokens: 32000,
- };
- const output = applyAnthropicProviderOverrides(provider, input);
- const thinking = output.thinking as Record<string, unknown>;
- expect(thinking.budget_tokens).toBe(1024);
- expect(thinking.type).toBe("enabled");
- });
- });
- describe("Audit function", () => {
- it("should return null audit when provider type is not claude/claude-auth", () => {
- const provider = {
- id: 123,
- name: "codex-provider",
- providerType: "codex",
- anthropicMaxTokensPreference: "32000",
- anthropicThinkingBudgetPreference: "10240",
- };
- const input: Record<string, unknown> = {
- model: "gpt-4",
- messages: [],
- };
- const result = applyAnthropicProviderOverridesWithAudit(provider, input);
- expect(result.request).toBe(input);
- expect(result.audit).toBeNull();
- });
- it("should return null audit when all preferences are inherit/null/undefined", () => {
- const provider = {
- providerType: "claude",
- anthropicMaxTokensPreference: "inherit",
- anthropicThinkingBudgetPreference: null,
- };
- const input: Record<string, unknown> = {
- model: "claude-3-opus-20240229",
- messages: [],
- max_tokens: 8000,
- };
- const result = applyAnthropicProviderOverridesWithAudit(provider, input);
- expect(result.request).toBe(input);
- expect(result.audit).toBeNull();
- });
- it("should return null audit when preferences are invalid numeric strings", () => {
- const provider = {
- providerType: "claude",
- anthropicMaxTokensPreference: "invalid",
- anthropicThinkingBudgetPreference: "not_a_number",
- };
- const input: Record<string, unknown> = {
- model: "claude-3-opus-20240229",
- messages: [],
- max_tokens: 8000,
- };
- const result = applyAnthropicProviderOverridesWithAudit(provider, input);
- expect(result.request).toBe(input);
- expect(result.audit).toBeNull();
- });
- it("should return audit with hit=true when max_tokens override is applied", () => {
- const provider = {
- id: 1,
- name: "claude-provider",
- providerType: "claude",
- anthropicMaxTokensPreference: "32000",
- };
- const input: Record<string, unknown> = {
- model: "claude-3-opus-20240229",
- messages: [],
- max_tokens: 8000,
- };
- const result = applyAnthropicProviderOverridesWithAudit(provider, input);
- expect(result.audit?.hit).toBe(true);
- expect(result.audit?.providerId).toBe(1);
- expect(result.audit?.providerName).toBe("claude-provider");
- });
- it("should return audit with hit=true when thinking override is applied", () => {
- const provider = {
- id: 2,
- name: "anthropic-direct",
- providerType: "claude-auth",
- anthropicThinkingBudgetPreference: "10240",
- };
- const input: Record<string, unknown> = {
- model: "claude-3-opus-20240229",
- messages: [],
- max_tokens: 32000,
- };
- const result = applyAnthropicProviderOverridesWithAudit(provider, input);
- expect(result.audit?.hit).toBe(true);
- expect(result.audit?.providerId).toBe(2);
- expect(result.audit?.providerName).toBe("anthropic-direct");
- });
- it("should track before/after values correctly for max_tokens", () => {
- const provider = {
- id: 1,
- name: "test-provider",
- providerType: "claude",
- anthropicMaxTokensPreference: "32000",
- };
- const input: Record<string, unknown> = {
- model: "claude-3-opus-20240229",
- messages: [],
- max_tokens: 8000,
- };
- const result = applyAnthropicProviderOverridesWithAudit(provider, input);
- const maxTokensChange = result.audit?.changes.find((c) => c.path === "max_tokens");
- expect(maxTokensChange?.before).toBe(8000);
- expect(maxTokensChange?.after).toBe(32000);
- expect(maxTokensChange?.changed).toBe(true);
- });
- it("should track before/after values correctly for thinking fields", () => {
- const provider = {
- id: 1,
- name: "test-provider",
- providerType: "claude",
- anthropicThinkingBudgetPreference: "10000",
- };
- const input: Record<string, unknown> = {
- model: "claude-3-opus-20240229",
- messages: [],
- max_tokens: 32000,
- thinking: { type: "disabled", budget_tokens: 5000 },
- };
- const result = applyAnthropicProviderOverridesWithAudit(provider, input);
- const typeChange = result.audit?.changes.find((c) => c.path === "thinking.type");
- expect(typeChange?.before).toBe("disabled");
- expect(typeChange?.after).toBe("enabled");
- expect(typeChange?.changed).toBe(true);
- const budgetChange = result.audit?.changes.find((c) => c.path === "thinking.budget_tokens");
- expect(budgetChange?.before).toBe(5000);
- expect(budgetChange?.after).toBe(10000);
- expect(budgetChange?.changed).toBe(true);
- });
- it("should set changed=false when override value equals existing value", () => {
- const provider = {
- id: 1,
- name: "test-provider",
- providerType: "claude",
- anthropicMaxTokensPreference: "8000",
- };
- const input: Record<string, unknown> = {
- model: "claude-3-opus-20240229",
- messages: [],
- max_tokens: 8000,
- };
- const result = applyAnthropicProviderOverridesWithAudit(provider, input);
- const maxTokensChange = result.audit?.changes.find((c) => c.path === "max_tokens");
- expect(maxTokensChange?.before).toBe(8000);
- expect(maxTokensChange?.after).toBe(8000);
- expect(maxTokensChange?.changed).toBe(false);
- });
- it("should set audit.changed=true only when at least one value actually changed", () => {
- const provider = {
- id: 1,
- name: "test-provider",
- providerType: "claude",
- anthropicMaxTokensPreference: "32000",
- anthropicThinkingBudgetPreference: "10240",
- };
- const input: Record<string, unknown> = {
- model: "claude-3-opus-20240229",
- messages: [],
- max_tokens: 8000,
- };
- const result = applyAnthropicProviderOverridesWithAudit(provider, input);
- expect(result.audit?.changed).toBe(true);
- });
- it("should set audit.changed=false when no values actually changed", () => {
- const provider = {
- id: 1,
- name: "test-provider",
- providerType: "claude",
- anthropicMaxTokensPreference: "8000",
- };
- const input: Record<string, unknown> = {
- model: "claude-3-opus-20240229",
- messages: [],
- max_tokens: 8000,
- };
- const result = applyAnthropicProviderOverridesWithAudit(provider, input);
- expect(result.audit?.hit).toBe(true);
- expect(result.audit?.changed).toBe(false);
- });
- it("should include correct metadata in audit", () => {
- const provider = {
- id: 42,
- name: "my-claude-provider",
- providerType: "claude",
- anthropicMaxTokensPreference: "16000",
- };
- const input: Record<string, unknown> = {
- model: "claude-3-opus-20240229",
- messages: [],
- };
- const result = applyAnthropicProviderOverridesWithAudit(provider, input);
- expect(result.audit?.type).toBe("provider_parameter_override");
- expect(result.audit?.scope).toBe("provider");
- expect(result.audit?.providerId).toBe(42);
- expect(result.audit?.providerName).toBe("my-claude-provider");
- expect(result.audit?.providerType).toBe("claude");
- });
- it("should handle missing provider id and name gracefully", () => {
- const provider = {
- providerType: "claude",
- anthropicMaxTokensPreference: "16000",
- };
- const input: Record<string, unknown> = {
- model: "claude-3-opus-20240229",
- messages: [],
- };
- const result = applyAnthropicProviderOverridesWithAudit(provider, input);
- expect(result.audit?.providerId).toBeNull();
- expect(result.audit?.providerName).toBeNull();
- });
- it("should track null before values when fields do not exist", () => {
- const provider = {
- id: 1,
- name: "test",
- providerType: "claude",
- anthropicMaxTokensPreference: "32000",
- anthropicThinkingBudgetPreference: "10000",
- };
- const input: Record<string, unknown> = {
- model: "claude-3-opus-20240229",
- messages: [],
- };
- const result = applyAnthropicProviderOverridesWithAudit(provider, input);
- const maxTokensChange = result.audit?.changes.find((c) => c.path === "max_tokens");
- expect(maxTokensChange?.before).toBeNull();
- expect(maxTokensChange?.after).toBe(32000);
- const typeChange = result.audit?.changes.find((c) => c.path === "thinking.type");
- expect(typeChange?.before).toBeNull();
- expect(typeChange?.after).toBe("enabled");
- const budgetChange = result.audit?.changes.find((c) => c.path === "thinking.budget_tokens");
- expect(budgetChange?.before).toBeNull();
- expect(budgetChange?.after).toBe(10000);
- });
- });
- describe("Adaptive thinking mode", () => {
- it("should apply adaptive thinking for matching model (all models mode)", () => {
- const provider = {
- providerType: "claude",
- anthropicAdaptiveThinking: {
- effort: "high" as const,
- modelMatchMode: "all" as const,
- models: [],
- },
- };
- const input: Record<string, unknown> = {
- model: "claude-opus-4-6",
- messages: [],
- max_tokens: 8000,
- };
- const output = applyAnthropicProviderOverrides(provider, input);
- expect(output.thinking).toEqual({ type: "adaptive" });
- expect(output.output_config).toEqual({ effort: "high" });
- });
- it("should apply adaptive thinking for matching model (specific models mode)", () => {
- const provider = {
- providerType: "claude",
- anthropicAdaptiveThinking: {
- effort: "max" as const,
- modelMatchMode: "specific" as const,
- models: ["claude-opus-4-6"],
- },
- };
- const input: Record<string, unknown> = {
- model: "claude-opus-4-6",
- messages: [],
- };
- const output = applyAnthropicProviderOverrides(provider, input);
- expect(output.thinking).toEqual({ type: "adaptive" });
- expect(output.output_config).toEqual({ effort: "max" });
- });
- it("should passthrough for non-matching model (specific models mode)", () => {
- const provider = {
- providerType: "claude",
- anthropicAdaptiveThinking: {
- effort: "high" as const,
- modelMatchMode: "specific" as const,
- models: ["claude-opus-4-6"],
- },
- };
- const input: Record<string, unknown> = {
- model: "claude-sonnet-4-5",
- messages: [],
- max_tokens: 8000,
- thinking: { type: "enabled", budget_tokens: 5000 },
- };
- const snapshot = structuredClone(input);
- const output = applyAnthropicProviderOverrides(provider, input);
- expect(output).toEqual(snapshot);
- });
- it("should preserve existing output_config properties", () => {
- const provider = {
- providerType: "claude",
- anthropicAdaptiveThinking: {
- effort: "medium" as const,
- modelMatchMode: "all" as const,
- models: [],
- },
- };
- const input: Record<string, unknown> = {
- model: "claude-opus-4-6",
- messages: [],
- output_config: { some_other_field: "preserve" },
- };
- const output = applyAnthropicProviderOverrides(provider, input);
- const outputConfig = output.output_config as Record<string, unknown>;
- expect(outputConfig.effort).toBe("medium");
- expect(outputConfig.some_other_field).toBe("preserve");
- });
- it("should apply adaptive with effort 'low'", () => {
- const provider = {
- providerType: "claude",
- anthropicAdaptiveThinking: {
- effort: "low" as const,
- modelMatchMode: "all" as const,
- models: [],
- },
- };
- const input: Record<string, unknown> = {
- model: "claude-opus-4-6",
- messages: [],
- };
- const output = applyAnthropicProviderOverrides(provider, input);
- expect(output.output_config).toEqual({ effort: "low" });
- });
- it("should remove budget_tokens from existing thinking when applying adaptive", () => {
- const provider = {
- providerType: "claude",
- anthropicAdaptiveThinking: {
- effort: "high" as const,
- modelMatchMode: "all" as const,
- models: [],
- },
- };
- const input: Record<string, unknown> = {
- model: "claude-opus-4-6",
- messages: [],
- thinking: { type: "enabled", budget_tokens: 10240 },
- };
- const output = applyAnthropicProviderOverrides(provider, input);
- const thinking = output.thinking as Record<string, unknown>;
- expect(thinking.type).toBe("adaptive");
- expect(thinking.budget_tokens).toBeUndefined();
- });
- it("should passthrough when adaptive config is null (defensive)", () => {
- const provider = {
- providerType: "claude",
- anthropicAdaptiveThinking: null,
- };
- const input: Record<string, unknown> = {
- model: "claude-opus-4-6",
- messages: [],
- max_tokens: 8000,
- };
- const snapshot = structuredClone(input);
- const output = applyAnthropicProviderOverrides(provider, input);
- expect(output).toEqual(snapshot);
- });
- it("should apply adaptive + max_tokens override together", () => {
- const provider = {
- providerType: "claude",
- anthropicMaxTokensPreference: "32000",
- anthropicAdaptiveThinking: {
- effort: "high" as const,
- modelMatchMode: "all" as const,
- models: [],
- },
- };
- const input: Record<string, unknown> = {
- model: "claude-opus-4-6",
- messages: [],
- max_tokens: 8000,
- };
- const output = applyAnthropicProviderOverrides(provider, input);
- expect(output.max_tokens).toBe(32000);
- expect(output.thinking).toEqual({ type: "adaptive" });
- expect(output.output_config).toEqual({ effort: "high" });
- });
- it("should match model prefix (claude-opus-4-6 matches claude-opus-4-6-20250514)", () => {
- const provider = {
- providerType: "claude",
- anthropicAdaptiveThinking: {
- effort: "high" as const,
- modelMatchMode: "specific" as const,
- models: ["claude-opus-4-6"],
- },
- };
- const input: Record<string, unknown> = {
- model: "claude-opus-4-6-20250514",
- messages: [],
- };
- const output = applyAnthropicProviderOverrides(provider, input);
- expect(output.thinking).toEqual({ type: "adaptive" });
- expect(output.output_config).toEqual({ effort: "high" });
- });
- it("should track output_config.effort in audit for adaptive mode", () => {
- const provider = {
- id: 1,
- name: "adaptive-provider",
- providerType: "claude",
- anthropicAdaptiveThinking: {
- effort: "high" as const,
- modelMatchMode: "all" as const,
- models: [],
- },
- };
- const input: Record<string, unknown> = {
- model: "claude-opus-4-6",
- messages: [],
- };
- const result = applyAnthropicProviderOverridesWithAudit(provider, input);
- expect(result.audit?.hit).toBe(true);
- expect(result.audit?.changed).toBe(true);
- const effortChange = result.audit?.changes.find((c) => c.path === "output_config.effort");
- expect(effortChange?.before).toBeNull();
- expect(effortChange?.after).toBe("high");
- expect(effortChange?.changed).toBe(true);
- const thinkingTypeChange = result.audit?.changes.find((c) => c.path === "thinking.type");
- expect(thinkingTypeChange?.after).toBe("adaptive");
- });
- });
- describe("Adaptive + Budget coexistence", () => {
- it("should apply adaptive when model matches, ignoring budget override", () => {
- const provider = {
- providerType: "claude",
- anthropicThinkingBudgetPreference: "10240",
- anthropicAdaptiveThinking: {
- effort: "high" as const,
- modelMatchMode: "specific" as const,
- models: ["claude-opus-4-6"],
- },
- };
- const input: Record<string, unknown> = {
- model: "claude-opus-4-6",
- messages: [],
- max_tokens: 32000,
- };
- const output = applyAnthropicProviderOverrides(provider, input);
- expect(output.thinking).toEqual({ type: "adaptive" });
- expect(output.output_config).toEqual({ effort: "high" });
- // Budget should NOT be applied when adaptive matches
- const thinking = output.thinking as Record<string, unknown>;
- expect(thinking.budget_tokens).toBeUndefined();
- });
- it("should fallback to budget when model does not match adaptive config", () => {
- const provider = {
- providerType: "claude",
- anthropicThinkingBudgetPreference: "10240",
- anthropicAdaptiveThinking: {
- effort: "high" as const,
- modelMatchMode: "specific" as const,
- models: ["claude-opus-4-6"],
- },
- };
- const input: Record<string, unknown> = {
- model: "claude-sonnet-4-5",
- messages: [],
- max_tokens: 32000,
- };
- const output = applyAnthropicProviderOverrides(provider, input);
- const thinking = output.thinking as Record<string, unknown>;
- expect(thinking.type).toBe("enabled");
- expect(thinking.budget_tokens).toBe(10240);
- expect(output.output_config).toBeUndefined();
- });
- it("should passthrough when model does not match adaptive and budget is inherit", () => {
- const provider = {
- providerType: "claude",
- anthropicThinkingBudgetPreference: "inherit",
- anthropicAdaptiveThinking: {
- effort: "high" as const,
- modelMatchMode: "specific" as const,
- models: ["claude-opus-4-6"],
- },
- };
- const input: Record<string, unknown> = {
- model: "claude-sonnet-4-5",
- messages: [],
- max_tokens: 32000,
- thinking: { type: "enabled", budget_tokens: 5000 },
- };
- const snapshot = structuredClone(input);
- const output = applyAnthropicProviderOverrides(provider, input);
- expect(output).toEqual(snapshot);
- });
- it("should always apply adaptive when modelMatchMode=all, regardless of budget", () => {
- const provider = {
- providerType: "claude",
- anthropicThinkingBudgetPreference: "10240",
- anthropicAdaptiveThinking: {
- effort: "max" as const,
- modelMatchMode: "all" as const,
- models: [],
- },
- };
- const input: Record<string, unknown> = {
- model: "claude-sonnet-4-5",
- messages: [],
- max_tokens: 32000,
- };
- const output = applyAnthropicProviderOverrides(provider, input);
- expect(output.thinking).toEqual({ type: "adaptive" });
- expect(output.output_config).toEqual({ effort: "max" });
- });
- it("should produce correct audit trail when adaptive matches (coexistence)", () => {
- const provider = {
- id: 10,
- name: "coexist-provider",
- providerType: "claude",
- anthropicThinkingBudgetPreference: "10240",
- anthropicAdaptiveThinking: {
- effort: "high" as const,
- modelMatchMode: "specific" as const,
- models: ["claude-opus-4-6"],
- },
- };
- const input: Record<string, unknown> = {
- model: "claude-opus-4-6",
- messages: [],
- max_tokens: 32000,
- };
- const result = applyAnthropicProviderOverridesWithAudit(provider, input);
- expect(result.audit?.hit).toBe(true);
- expect(result.audit?.changed).toBe(true);
- const effortChange = result.audit?.changes.find((c) => c.path === "output_config.effort");
- expect(effortChange?.after).toBe("high");
- expect(effortChange?.changed).toBe(true);
- const thinkingTypeChange = result.audit?.changes.find((c) => c.path === "thinking.type");
- expect(thinkingTypeChange?.after).toBe("adaptive");
- });
- it("should produce correct audit trail when falling back to budget (coexistence)", () => {
- const provider = {
- id: 10,
- name: "coexist-provider",
- providerType: "claude",
- anthropicThinkingBudgetPreference: "10240",
- anthropicAdaptiveThinking: {
- effort: "high" as const,
- modelMatchMode: "specific" as const,
- models: ["claude-opus-4-6"],
- },
- };
- const input: Record<string, unknown> = {
- model: "claude-sonnet-4-5",
- messages: [],
- max_tokens: 32000,
- };
- const result = applyAnthropicProviderOverridesWithAudit(provider, input);
- expect(result.audit?.hit).toBe(true);
- expect(result.audit?.changed).toBe(true);
- const thinkingTypeChange = result.audit?.changes.find((c) => c.path === "thinking.type");
- expect(thinkingTypeChange?.after).toBe("enabled");
- const budgetChange = result.audit?.changes.find((c) => c.path === "thinking.budget_tokens");
- expect(budgetChange?.after).toBe(10240);
- // output_config.effort should NOT be set for budget fallback
- const effortChange = result.audit?.changes.find((c) => c.path === "output_config.effort");
- expect(effortChange?.changed).toBe(false);
- });
- });
- });
|