TypeScript
fix(claude): turns use the downloaded model catalog (#31717)
Julius Marminge43cf364dd25e
diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.tsindex c98b9d4dec9..70f1471ef54 100644--- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts+++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts@@ -93,6 +93,10 @@ import { WorktreeToolkit } from "../../mcp/toolkits/worktree/tools.ts";import { ThreadToolkit } from "../../mcp/toolkits/thread/tools.ts";import { OrchestratorToolkit } from "../../mcp/toolkits/orchestrator/tools.ts";import { ClaudeExecutableFileCheck } from "../../provider/Drivers/ClaudeExecutable.ts";+import {+ BUNDLED_CLAUDE_MODEL_CATALOG,+ type ClaudeModelCatalog,+} from "../../provider/ClaudeModelCatalog.ts";import * as ProviderEventLoggers from "@t3tools/provider-core/server/ProviderEventLoggers";import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";import type * as ProviderContinuationRequests from "@t3tools/provider-core/server/ProviderContinuationRequests";@@ -3151,6 +3155,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {readonly subagentLaunchToolUseId?: string;readonly nativeSessionId?: string;readonly settings?: ClaudeSettings;+ readonly modelCatalog?: Effect.Effect<ClaudeModelCatalog>;readonly freshQueueOnReopen?: boolean;readonly forkSession?: ClaudeAdapterV2.ClaudeAgentSdkQueryRunnerShape["forkSession"];readonly allocateSessionId?: Effect.Effect<string>;@@ -3210,6 +3215,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {const adapter = yield* ClaudeAdapterV2.makeClaudeAdapterV2({instanceId: ClaudeAdapterV2.CLAUDE_DEFAULT_INSTANCE_ID,settings: options?.settings ?? DEFAULT_CLAUDE_SETTINGS,+ ...(options?.modelCatalog === undefined ? {} : { modelCatalog: options.modelCatalog }),environment: options?.environment ?? {},attachmentsDir,fileSystem,@@ -14892,44 +14898,74 @@ describe("ClaudeAdapterV2 background wake turns", () => {),);- it.effect("sends Ultracode with any effort on a custom model", () =>- Effect.scoped(- Effect.gen(function* () {- const harness = yield* makeWakeHarnessWithOptions({- settings: ULTRACODE_CUSTOM_MODEL_CLAUDE_SETTINGS,- });- yield* harness.runtime.startTurn(- makeClaudeTestTurnInput({- threadId: harness.threadId,- providerThread: harness.providerThread,- now: yield* DateTime.now,- attemptId: RunAttemptId.make("attempt-custom-ultracode"),- text: "Plan the work.",- attachments: [],- modelSelection: {- instanceId: ClaudeAdapterV2.CLAUDE_DEFAULT_INSTANCE_ID,- model: "claude-custom-tuned",- options: [- { id: "effort", value: "high" },- { id: "ultracode", value: true },- ],- },- }),- );- const options = harness.getOpenedOptions()!;- assert.equal(options.effort, "high");- assert.equal((options.settings as { ultracode?: boolean }).ultracode, true);- }).pipe(- Effect.provide(- Layer.mergeAll(- IdAllocator.layer,- McpProviderSessions.layer,- NodeServices.layer,- NodeCrypto.layer,+ it.effect.each([+ {+ source: "custom model",+ model: "claude-custom-tuned",+ harness: { settings: ULTRACODE_CUSTOM_MODEL_CLAUDE_SETTINGS },+ },+ {+ source: "downloaded catalog",+ model: "claude-sonnet-5",+ harness: {+ modelCatalog: Effect.succeed({+ models: BUNDLED_CLAUDE_MODEL_CATALOG.models.map((entry) =>+ entry.model.slug === "claude-sonnet-5"+ ? {+ ...entry,+ model: {+ ...entry.model,+ capabilities: {+ optionDescriptors: [+ ...(entry.model.capabilities?.optionDescriptors ?? []),+ { id: "ultracode", label: "Ultracode", type: "boolean" as const },+ ],+ },+ },+ }+ : entry,+ ),+ }),+ },+ },+ ])(+ "sends Ultracode with any effort on a model from the $source",+ ({ harness: harnessOptions, model }) =>+ Effect.scoped(+ Effect.gen(function* () {+ const harness = yield* makeWakeHarnessWithOptions(harnessOptions);+ yield* harness.runtime.startTurn(+ makeClaudeTestTurnInput({+ threadId: harness.threadId,+ providerThread: harness.providerThread,+ now: yield* DateTime.now,+ attemptId: RunAttemptId.make("attempt-custom-ultracode"),+ text: "Plan the work.",+ attachments: [],+ modelSelection: {+ instanceId: ClaudeAdapterV2.CLAUDE_DEFAULT_INSTANCE_ID,+ model,+ options: [+ { id: "effort", value: "high" },+ { id: "ultracode", value: true },+ ],+ },+ }),+ );+ const options = harness.getOpenedOptions()!;+ assert.equal(options.effort, "high");+ assert.equal((options.settings as { ultracode?: boolean }).ultracode, true);+ }).pipe(+ Effect.provide(+ Layer.mergeAll(+ IdAllocator.layer,+ McpProviderSessions.layer,+ NodeServices.layer,+ NodeCrypto.layer,+ ),),),),- ),);it.effect("resolves a subagent's model and effort against the instance's custom models", () =>diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.tsindex 1cf8610837b..331408a21c4 100644--- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts+++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts@@ -4830,6 +4830,7 @@ function claudeProposedPlan(input: unknown): string | null {export interface ClaudeAdapterV2Options {readonly instanceId: ProviderInstanceId;readonly settings: ClaudeSettings;+ readonly modelCatalog?: Effect.Effect<ClaudeModelCatalog>;readonly environment: NodeJS.ProcessEnv;readonly attachmentsDir: string;readonly fileSystem: FileSystem.FileSystem;@@ -4853,15 +4854,20 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;const serverSettings = yield* Effect.serviceOption(ServerSettings.ServerSettingsService);const { attachmentsDir, fileSystem, path, random, idAllocator, queryRunner } = adapterOptions;- const modelCatalog = scopeClaudeModelCatalog(- BUNDLED_CLAUDE_MODEL_CATALOG,- adapterOptions.settings.customModels,+ const loadModelCatalog = (+ adapterOptions.modelCatalog ?? Effect.succeed(BUNDLED_CLAUDE_MODEL_CATALOG)+ ).pipe(+ Effect.map((catalog) => scopeClaudeModelCatalog(catalog, adapterOptions.settings.customModels)),);+ // Reloaded at each turn and steer so downloaded manifest updates apply without a release.+ let modelCatalog = yield* loadModelCatalog;// Results report the window Claude Code compacts against; outside the catalog it is the only source.const reportedContextWindows = new Map<string, number>();const claudeContextWindow = (modelSelection: ModelSelection): number | null =>- resolveClaudeCatalogContextWindowTokens(BUNDLED_CLAUDE_MODEL_CATALOG, modelSelection) ??- reportedContextWindows.get(compileClaudeModelSelection(modelSelection).apiModelId) ??+ resolveClaudeCatalogContextWindowTokens(modelCatalog, modelSelection) ??+ reportedContextWindows.get(+ compileClaudeModelSelection(modelSelection, modelCatalog).apiModelId,+ ) ??null;const continuationRequests = adapterOptions.continuationRequests ?? {offer: () => Effect.void,@@ -11047,6 +11053,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (const startTurn = Effect.fn("ClaudeAdapterV2.startTurn")(function* (turnInput: ProviderAdapter.ProviderAdapterV2TurnInput) {+ modelCatalog = yield* loadModelCatalog;const startedAt = yield* DateTime.now;const nativeThreadId = yield* getNativeThreadId(turnInput.providerThread);const nativeTurnId = `turn:${turnInput.attemptId}`;@@ -11525,6 +11532,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (const steerTurn = Effect.fn("ClaudeAdapterV2.steerTurn")(function* (turnInput: ProviderAdapter.ProviderAdapterV2SteerInput) {+ modelCatalog = yield* loadModelCatalog;// Measured from the request, not from when the moves are forked:// building the message and the moves can take clock time a timer// started later would not count, and the steer would wait past it.@@ -12285,7 +12293,7 @@ export type ClaudeAdapterV2DriverEnv =export const createClaudeAdapterV2 = Effect.fn("ClaudeAdapterV2Driver.create")(function* (input: ProviderAdapterDriverCreateInput<ClaudeSettings>,- hooks: Pick<ClaudeAdapterV2Options, "scopedLimitNames" | "onUsageLimits"> = {},+ hooks: Pick<ClaudeAdapterV2Options, "scopedLimitNames" | "onUsageLimits" | "modelCatalog"> = {},) {const { instanceId, environment, enabled, config } = input;const fileSystem = yield* FileSystem.FileSystem;diff --git a/apps/server/src/provider/Drivers/ClaudeDriver.ts b/apps/server/src/provider/Drivers/ClaudeDriver.tsindex c0a9c5698d9..c2d4b638610 100644--- a/apps/server/src/provider/Drivers/ClaudeDriver.ts+++ b/apps/server/src/provider/Drivers/ClaudeDriver.ts@@ -155,7 +155,11 @@ export const ClaudeDriver: ProviderDriver<ClaudeSettings, ClaudeDriverEnv, Path.enabled,config,},- { scopedLimitNames, onUsageLimits: (update) => snapshot.applyUsageLimits(update) },+ {+ scopedLimitNames,+ onUsageLimits: (update) => snapshot.applyUsageLimits(update),+ modelCatalog,+ },).pipe(Effect.mapError((cause) =>