From e73d7909ce0295bfa351da49940e067da02ea0e4 Mon Sep 17 00:00:00 2001 From: nikita-ashihmin Date: Mon, 16 Nov 2026 12:30:08 +0400 Subject: [PATCH] fix: resolve a model preference without a version to the newest listed row (#1633) ## Problem A `settings.model` of `opus[1m]`, with an `availableModels` allowlist of versioned ids from oldest to newest, starts a new session on Opus 4.5: ```json "model": "opus[1m]", "availableModels": ["claude-sonnet-4-5-20250929", "claude-haiku-4-5-20251001", "claude-opus-4-5-20251101", "...", "claude-opus-5-5", "..."] ``` No row matches `opus[1m]` directly. The fuzzy tiers of `resolveModelPreference` then return the first `opus` row, `claude-opus-4-5-20251101`, and `getAvailableModels` calls `setModel` with it. The CLI resolves the same settings to the newest Opus: the `default` row of `supportedModels()` gives `claude-opus-5-5`. ## Fix For a preference without a version (`opus`, `opus[1m]`, `sonnet`), the fuzzy tiers take the row of the newest version. The new `newestModel` reads the version with `familyVersionOf`. A row of an unknown version, such as `my-opus`, ranks below each row of a known version. A preference with a version keeps the first compatible row, as before. ## Tests - `model-resolution.test.ts`: a new `it.each` resolves `opus[1m]`, `opus` and `sonnet` against an allowlist from oldest to newest. Without the fix the test fails and gives the oldest row. - I checked the fix against the real `supportedModels()` with the settings above. Before the fix, `opus[1m]` resolved to `claude-opus-4-5-20251101`. After the fix, it resolves to `claude-opus-5-5`. - `npm run check` and `npm run build` pass. - On my machine, `npm run test:run` has 26 failures in `v1/create-session-options`, `v1/providers` and `v2/acp-v2`. They fail the same way on `main` without this change. They come from the local `ANTHROPIC_*` environment. --- src/session-model.ts | 55 ++++++++++++++++++++++++------ src/tests/model-resolution.test.ts | 21 ++++++++++++ 2 files changed, 65 insertions(+), 11 deletions(-) diff --git a/src/session-model.ts b/src/session-model.ts index dbdd00a4..81e3e7e2 100644 --- a/src/session-model.ts +++ b/src/session-model.ts @@ -146,6 +146,8 @@ function modelCoversTokens(model: ModelInfo, tokens: string[]): boolean { * of its model words, preferring one with the same context hint ("sonnet[1m]" * for "claude-sonnet-5-1m"). The fuzzy tiers need a compatible version, and * words match whole: "not-a-model", "son" and "foo-sonnet" name no entry. + * For a preference without a version, the fuzzy tiers take the entry of the + * newest version: "opus[1m]" gives "claude-opus-5-5", not "claude-opus-4-5". */ export function resolveModelPreference(models: ModelInfo[], preference: string): ModelInfo | null { const trimmed = preference.trim(); @@ -175,27 +177,58 @@ export function resolveModelPreference(models: ModelInfo[], preference: string): const preferenceHint = contextHintOf(trimmed); const tokens = modelPreferenceTokens(trimmed); - const includesMatch = models.find((model) => { - if (!modelVersionsCompatible(trimmed, model)) return false; - if (contextHintOf(model.value) !== preferenceHint) return false; - return ( - containsWords(model.value, trimmed) || - containsWords(model.displayName, trimmed) || - (containsWords(trimmed, model.value) && modelCoversTokens(model, tokens)) - ); - }); + // A preference without a version (`opus`, `opus[1m]`) runs its family's + // newest model in the CLI, so the fuzzy tiers pick the newest row, not the + // first: an allowlist lists `claude-opus-4-5-20251101` before `claude-opus-5-5`. + const pick = + extractModelFamilyVersion(trimmed) === null + ? newestModel + : (rows: ModelInfo[]): ModelInfo | undefined => rows[0]; + const includesMatch = pick( + models.filter((model) => { + if (!modelVersionsCompatible(trimmed, model)) return false; + if (contextHintOf(model.value) !== preferenceHint) return false; + return ( + containsWords(model.value, trimmed) || + containsWords(model.displayName, trimmed) || + (containsWords(trimmed, model.value) && modelCoversTokens(model, tokens)) + ); + }), + ); if (includesMatch) return includesMatch; const candidates = models.filter( (model) => modelVersionsCompatible(trimmed, model) && modelCoversTokens(model, tokens), ); return ( - candidates.find((model) => contextHintOf(model.value) === preferenceHint) ?? - candidates[0] ?? + pick(candidates.filter((model) => contextHintOf(model.value) === preferenceHint)) ?? + pick(candidates) ?? null ); } +/** The row of `models` that runs the newest family version, the first one of + * equal versions. A row of an unknown version (`my-model`) ranks below each + * row of a known one. */ +function newestModel(models: ModelInfo[]): ModelInfo | undefined { + let newest: ModelInfo | undefined; + let newestVersion: { major: number; minor: number } | null = null; + for (const model of models) { + const version = familyVersionOf(normalizeModelId(model.resolvedModel ?? model.value)); + const newer = + newest === undefined || + (version !== null && + (newestVersion === null || + version.major > newestVersion.major || + (version.major === newestVersion.major && version.minor > newestVersion.minor))); + if (newer) { + newest = model; + newestVersion = version; + } + } + return newest; +} + export function matchResumedModel(models: ModelInfo[], liveModel: string): ModelInfo { const live = canonicalizeModelId(liveModel); const defaultEntry = models.find((model) => model.value === "default"); diff --git a/src/tests/model-resolution.test.ts b/src/tests/model-resolution.test.ts index 8563450f..dca8ede6 100644 --- a/src/tests/model-resolution.test.ts +++ b/src/tests/model-resolution.test.ts @@ -178,6 +178,27 @@ describe("resolveModelPreference - resolvedModel matching", () => { // Bare preferences still land on the bare row via the substring tier. expect(resolveModelPreference(models, "claude-sonnet-5")?.value).toBe("sonnet"); }); + + // An `availableModels` allowlist of dated and versioned ids, oldest first, + // as `applyAvailableModelsAllowlist` gives it with `settings.model: "opus[1m]"`. + // `my-opus` has no version, so it ranks below each versioned row. + const VERSIONED_MODELS: ModelInfo[] = [ + "my-opus", + "claude-sonnet-4-5-20250929", + "claude-opus-4-5-20251101", + "claude-opus-4-8", + "claude-opus-5", + "claude-opus-5-5", + "claude-sonnet-5", + ].map((value) => ({ value, resolvedModel: value, displayName: value, description: "" })); + + it.each([ + ["opus[1m]", "claude-opus-5-5"], + ["opus", "claude-opus-5-5"], + ["sonnet", "claude-sonnet-5"], + ])("resolves %s to the newest listed row of its family, %s", (preference, value) => { + expect(resolveModelPreference(VERSIONED_MODELS, preference)?.value).toBe(value); + }); }); describe("matchResumedModel", () => {