From 13c0c8b535cedf5523780e955ea81783334ec230 Mon Sep 17 00:00:00 2001 From: OrcaWin Date: Sat, 12 Sep 2026 18:11:11 -0700 Subject: [PATCH] perf: select matching model IDs without sorting (#20334) Co-authored-by: Orca Worker --- .../native-chat-session-option-state.test.ts | 9 +++++++++ src/shared/native-chat-session-option-state.ts | 14 ++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/shared/native-chat-session-option-state.test.ts b/src/shared/native-chat-session-option-state.test.ts index f2827207bf7..0cd0af94d80 100644 --- a/src/shared/native-chat-session-option-state.test.ts +++ b/src/shared/native-chat-session-option-state.test.ts @@ -48,6 +48,15 @@ describe('applyNativeChatReportedSessionOptions', () => { }) describe('matchNativeChatCatalogModelId', () => { + it('prefers the longest contained id and keeps catalog order for ties', () => { + const catalog = { + ...CLAUDE_SESSION_OPTION_CATALOG, + models: ['a', 'abc', 'xyz'].map((id) => ({ id, label: id, options: [] })) + } + expect(matchNativeChatCatalogModelId(catalog, 'provider-xyz-abc')).toBe('abc') + expect(catalog.models.map((model) => model.id)).toEqual(['a', 'abc', 'xyz']) + }) + it('matches exact ids, labels, and provider-id containment', () => { expect(matchNativeChatCatalogModelId(CLAUDE_SESSION_OPTION_CATALOG, 'sonnet')).toBe('sonnet') expect(matchNativeChatCatalogModelId(CLAUDE_SESSION_OPTION_CATALOG, 'Sonnet 5')).toBe('sonnet') diff --git a/src/shared/native-chat-session-option-state.ts b/src/shared/native-chat-session-option-state.ts index 5d070969775..14eddfa7ff9 100644 --- a/src/shared/native-chat-session-option-state.ts +++ b/src/shared/native-chat-session-option-state.ts @@ -167,8 +167,14 @@ export function matchNativeChatCatalogModelId( if (byLabel) { return byLabel.id } - const containing = [...catalog.models] - .sort((left, right) => right.id.length - left.id.length) - .find((model) => normalized.includes(model.id.toLowerCase())) - return containing?.id ?? null + let containingId: string | null = null + for (const model of catalog.models) { + if ( + (containingId === null || model.id.length > containingId.length) && + normalized.includes(model.id.toLowerCase()) + ) { + containingId = model.id + } + } + return containingId }