@@ -20,18 +20,18 @@ describe("parseModelCallbackData", () => {
2020 [ "mdl_list_anthropic_2" , { type : "list" , provider : "anthropic" , page : 2 } ] ,
2121 [ "mdl_list_open-ai_1" , { type : "list" , provider : "open-ai" , page : 1 } ] ,
2222 [ "mdl_list_hf.co_1" , { type : "list" , provider : "hf.co" , page : 1 } ] ,
23+ // Index-based select format (#98221)
2324 [
24- "mdl_sel_anthropic/claude-sonnet-4-5 " ,
25- { type : "select" , provider : "anthropic" , model : "claude-sonnet-4-5" } ,
25+ "mdl_sel_anthropic_1_1_2 " ,
26+ { type : "select" , provider : "anthropic" , page : 1 , modelIndex : 1 , totalCount : 2 } ,
2627 ] ,
27- [ "mdl_sel_openai/gpt-4/turbo" , { type : "select" , provider : "openai" , model : "gpt-4/turbo" } ] ,
2828 [
29- "mdl_sel/us.anthropic.claude-3-5-sonnet-20240620-v1:0 " ,
30- { type : "select" , model : "us.anthropic.claude-3-5-sonnet-20240620-v1:0" } ,
29+ "mdl_sel_openai_2_5_8 " ,
30+ { type : "select" , provider : "openai" , page : 2 , modelIndex : 5 , totalCount : 8 } ,
3131 ] ,
3232 [
33- "mdl_sel/anthropic/claude-3-7-sonnet " ,
34- { type : "select" , model : "anthropic/claude-3-7-sonnet" } ,
33+ "mdl_sel_hf.co_1_12_42 " ,
34+ { type : "select" , provider : "hf.co" , page : 1 , modelIndex : 12 , totalCount : 42 } ,
3535 ] ,
3636 [ " mdl_prov " , { type : "providers" } ] ,
3737 ] as const ;
@@ -49,7 +49,7 @@ describe("parseModelCallbackData", () => {
4949 "mdl_list_" ,
5050 "mdl_list_openai_9007199254740993" ,
5151 "mdl_sel_noslash" ,
52- "mdl_sel/ " ,
52+ "mdl_sel_ " ,
5353 ] ;
5454 for ( const input of invalid ) {
5555 expect ( parseModelCallbackData ( input ) , input ) . toBeNull ( ) ;
@@ -58,9 +58,9 @@ describe("parseModelCallbackData", () => {
5858} ) ;
5959
6060describe ( "resolveModelSelection" , ( ) => {
61- it ( "returns explicit provider selections unchanged " , ( ) => {
61+ it ( "resolves by provider and modelIndex from sorted models " , ( ) => {
6262 const result = resolveModelSelection ( {
63- callback : { type : "select" , provider : "openai" , model : "gpt-4.1" } ,
63+ callback : { type : "select" , provider : "openai" , page : 1 , modelIndex : 1 , totalCount : 1 } ,
6464 providers : [ "openai" , "anthropic" ] ,
6565 byProvider : new Map ( [
6666 [ "openai" , new Set ( [ "gpt-4.1" ] ) ] ,
@@ -70,63 +70,121 @@ describe("resolveModelSelection", () => {
7070 expect ( result ) . toEqual ( { kind : "resolved" , provider : "openai" , model : "gpt-4.1" } ) ;
7171 } ) ;
7272
73- it ( "resolves compact callbacks when exactly one provider matches" , ( ) => {
73+ it ( "resolves by index into correctly sorted models" , ( ) => {
74+ // Models are sorted alphabetically: claude-opus-4, claude-sonnet-4
7475 const result = resolveModelSelection ( {
75- callback : { type : "select" , model : "shared" } ,
76- providers : [ "openai" , "anthropic" ] ,
77- byProvider : new Map ( [
78- [ "openai" , new Set ( [ "shared" ] ) ] ,
79- [ "anthropic" , new Set ( [ "other" ] ) ] ,
80- ] ) ,
76+ callback : { type : "select" , provider : "anthropic" , page : 1 , modelIndex : 2 , totalCount : 2 } ,
77+ providers : [ "anthropic" ] ,
78+ byProvider : new Map ( [ [ "anthropic" , new Set ( [ "claude-sonnet-4" , "claude-opus-4" ] ) ] ] ) ,
8179 } ) ;
82- expect ( result ) . toEqual ( { kind : "resolved" , provider : "openai " , model : "shared " } ) ;
80+ expect ( result ) . toEqual ( { kind : "resolved" , provider : "anthropic " , model : "claude-sonnet-4 " } ) ;
8381 } ) ;
8482
85- it ( "returns ambiguous result when zero or multiple providers match" , ( ) => {
86- const sharedByBoth = resolveModelSelection ( {
87- callback : { type : "select" , model : "shared" } ,
88- providers : [ "openai" , "anthropic" ] ,
89- byProvider : new Map ( [
90- [ "openai" , new Set ( [ "shared" ] ) ] ,
91- [ "anthropic" , new Set ( [ "shared" ] ) ] ,
92- ] ) ,
83+ it ( "returns ambiguous when totalCount differs (stale button guard)" , ( ) => {
84+ const result = resolveModelSelection ( {
85+ callback : { type : "select" , provider : "openai" , page : 1 , modelIndex : 1 , totalCount : 5 } ,
86+ providers : [ "openai" ] ,
87+ byProvider : new Map ( [ [ "openai" , new Set ( [ "gpt-4.1" ] ) ] ] ) ,
9388 } ) ;
94- expect ( sharedByBoth ) . toEqual ( {
95- kind : "ambiguous" ,
96- model : "shared" ,
97- matchingProviders : [ "openai" , "anthropic" ] ,
89+ expect ( result . kind ) . toBe ( "ambiguous" ) ;
90+ } ) ;
91+
92+ it ( "rejects stale fingerprinted callback on same-count different-model list (#98221)" , ( ) => {
93+ // ["a", "bc"] and ["ab", "c"] have the same concatenated character
94+ // stream but different models. The delimiter in the fingerprint must
95+ // distinguish them so a button from the first list is rejected when
96+ // the provider's models change to the second list.
97+ const modelsA = new Set ( [ "a" , "bc" ] ) ;
98+ const result = resolveModelSelection ( {
99+ callback : {
100+ type : "select" ,
101+ provider : "p" ,
102+ page : 1 ,
103+ modelIndex : 1 ,
104+ totalCount : 2 ,
105+ fingerprint : "dead" ,
106+ } ,
107+ providers : [ "p" ] ,
108+ byProvider : new Map ( [ [ "p" , modelsA ] ] ) ,
98109 } ) ;
110+ // Real fingerprint of modelsA ≠ "dead" → rejected as stale
111+ expect ( result . kind ) . toBe ( "ambiguous" ) ;
112+ } ) ;
99113
100- const missingEverywhere = resolveModelSelection ( {
101- callback : { type : "select" , model : "missing" } ,
114+ it ( "returns ambiguous result when provider has no models" , ( ) => {
115+ const result = resolveModelSelection ( {
116+ callback : { type : "select" , provider : "missing" , page : 1 , modelIndex : 1 , totalCount : 0 } ,
102117 providers : [ "openai" , "anthropic" ] ,
103- byProvider : new Map ( [
104- [ "openai" , new Set ( [ "gpt-4.1" ] ) ] ,
105- [ "anthropic" , new Set ( [ "claude-sonnet-4-5" ] ) ] ,
106- ] ) ,
118+ byProvider : new Map ( [ [ "openai" , new Set ( [ "gpt-4.1" ] ) ] ] ) ,
107119 } ) ;
108- expect ( missingEverywhere ) . toEqual ( {
109- kind : "ambiguous" ,
110- model : "missing" ,
111- matchingProviders : [ ] ,
120+ expect ( result ) . toMatchObject ( { kind : "ambiguous" , matchingProviders : [ "openai" , "anthropic" ] } ) ;
121+ } ) ;
122+
123+ it ( "returns ambiguous result when modelIndex is out of range" , ( ) => {
124+ const result = resolveModelSelection ( {
125+ callback : { type : "select" , provider : "openai" , page : 1 , modelIndex : 99 , totalCount : 1 } ,
126+ providers : [ "openai" , "anthropic" ] ,
127+ byProvider : new Map ( [ [ "openai" , new Set ( [ "gpt-4.1" ] ) ] ] ) ,
112128 } ) ;
129+ expect ( result . kind ) . toBe ( "ambiguous" ) ;
113130 } ) ;
114131} ) ;
115132
116133describe ( "buildModelSelectionCallbackData" , ( ) => {
117- it ( "uses standard callback when under limit and compact callback when needed" , ( ) => {
118- expect ( buildModelSelectionCallbackData ( { provider : "openai" , model : "gpt-4.1" } ) ) . toBe (
119- "mdl_sel_openai/gpt-4.1" ,
120- ) ;
121- const longModel = "us.anthropic.claude-3-5-sonnet-20240620-v1:0" ;
122- expect ( buildModelSelectionCallbackData ( { provider : "amazon-bedrock" , model : longModel } ) ) . toBe (
123- `mdl_sel/${ longModel } ` ,
124- ) ;
134+ it ( "builds fixed-length index-based callback with fingerprint (#98221)" , ( ) => {
135+ const models5 = [ "gpt-4" , "gpt-4.1" , "gpt-5" , "claude-3" , "gemini-2" ] ;
136+ const cb1 = buildModelSelectionCallbackData ( {
137+ provider : "openai" ,
138+ page : 1 ,
139+ modelIndex : 1 ,
140+ totalCount : 5 ,
141+ models : models5 ,
142+ } ) ;
143+ expect ( cb1 ) . toMatch ( / ^ m d l _ s e l _ o p e n a i _ 1 _ 1 _ 5 _ [ a - f 0 - 9 ] { 4 } $ / ) ;
144+ const models42 = Array . from ( { length : 42 } , ( _ , i ) => `model-${ i } ` ) ;
145+ const cb2 = buildModelSelectionCallbackData ( {
146+ provider : "anthropic" ,
147+ page : 3 ,
148+ modelIndex : 12 ,
149+ totalCount : 42 ,
150+ models : models42 ,
151+ } ) ;
152+ expect ( cb2 ) . toMatch ( / ^ m d l _ s e l _ a n t h r o p i c _ 3 _ 1 2 _ 4 2 _ [ a - f 0 - 9 ] { 4 } $ / ) ;
153+ // Never embeds model name — always fits 64 bytes
154+ const cb3 = buildModelSelectionCallbackData ( {
155+ provider : "a" ,
156+ page : 1 ,
157+ modelIndex : 1 ,
158+ totalCount : 1 ,
159+ models : [ "x" ] ,
160+ } ) ;
161+ expect ( cb3 . length ) . toBeLessThan ( 64 ) ;
125162 } ) ;
163+ } ) ;
126164
127- it ( "returns null when even compact callback exceeds Telegram limit" , ( ) => {
128- const tooLongModel = "x" . repeat ( 80 ) ;
129- expect ( buildModelSelectionCallbackData ( { provider : "openai" , model : tooLongModel } ) ) . toBeNull ( ) ;
165+ describe ( "fingerprint delimiter disambiguation (#98221)" , ( ) => {
166+ it ( "produces different callback_data for same-character-stream model lists" , ( ) => {
167+ // ["a", "bc"] and ["ab", "c"] both concatenate to "abc" but represent
168+ // different model sets. The delimiter in computeModelListFingerprint
169+ // must produce different fingerprints for these two lists.
170+ const cb1 = buildModelSelectionCallbackData ( {
171+ provider : "p" ,
172+ page : 1 ,
173+ modelIndex : 1 ,
174+ totalCount : 2 ,
175+ models : [ "a" , "bc" ] ,
176+ } ) ;
177+ const cb2 = buildModelSelectionCallbackData ( {
178+ provider : "p" ,
179+ page : 1 ,
180+ modelIndex : 1 ,
181+ totalCount : 2 ,
182+ models : [ "ab" , "c" ] ,
183+ } ) ;
184+ expect ( cb1 ) . not . toBe ( cb2 ) ;
185+ // Both still under 64 bytes
186+ expect ( Buffer . byteLength ( cb1 , "utf8" ) ) . toBeLessThanOrEqual ( 64 ) ;
187+ expect ( Buffer . byteLength ( cb2 , "utf8" ) ) . toBeLessThanOrEqual ( 64 ) ;
130188 } ) ;
131189} ) ;
132190
@@ -225,8 +283,9 @@ describe("buildModelsKeyboard", () => {
225283 // 2 model rows + back button
226284 expect ( result , testCase . name ) . toHaveLength ( 3 ) ;
227285 expect ( result [ 0 ] ?. [ 0 ] ?. text ) . toBe ( testCase . firstText ) ;
228- expect ( result [ 0 ] ?. [ 0 ] ?. callback_data ) . toBe ( "mdl_sel_anthropic/claude-sonnet-4" ) ;
286+ expect ( result [ 0 ] ?. [ 0 ] ?. callback_data ) . toMatch ( / ^ m d l _ s e l _ a n t h r o p i c _ 1 _ 1 _ 2 _ [ a - f 0 - 9 ] { 4 } $ / ) ;
229287 expect ( result [ 1 ] ?. [ 0 ] ?. text ) . toBe ( "claude-opus-4" ) ;
288+ expect ( result [ 1 ] ?. [ 0 ] ?. callback_data ) . toMatch ( / ^ m d l _ s e l _ a n t h r o p i c _ 1 _ 2 _ 2 _ [ a - f 0 - 9 ] { 4 } $ / ) ;
230289 expect ( result [ 2 ] ?. [ 0 ] ?. text ) . toBe ( "<< Back" ) ;
231290 }
232291 } ) ;
@@ -247,10 +306,8 @@ describe("buildModelsKeyboard", () => {
247306 expect ( result ) . toHaveLength ( 3 ) ;
248307 expect ( result [ 0 ] ?. [ 0 ] ?. text ) . toBe ( "Claude Sonnet 4" ) ;
249308 expect ( result [ 1 ] ?. [ 0 ] ?. text ) . toBe ( "Claude Opus 4" ) ;
250- // callback_data still uses the raw model ID, not the display name
251- expect ( result [ 0 ] ?. [ 0 ] ?. callback_data ) . toBe (
252- "mdl_sel_nexos/a1b2c3d4-e5f6-7890-abcd-ef1234567890" ,
253- ) ;
309+ // callback_data uses index format, not model ID (#98221)
310+ expect ( result [ 0 ] ?. [ 0 ] ?. callback_data ) . toMatch ( / ^ m d l _ s e l _ n e x o s _ 1 _ 1 _ 2 _ [ a - f 0 - 9 ] { 4 } $ / ) ;
254311 } ) ;
255312
256313 it ( "falls back to model ID when modelNames does not contain an entry" , ( ) => {
@@ -423,18 +480,6 @@ describe("buildModelsKeyboard", () => {
423480 expect ( result [ 0 ] ?. [ 0 ] ?. text , testCase . name ) . toBe ( `…${ "b" . repeat ( 36 ) } ` ) ;
424481 }
425482 } ) ;
426-
427- it ( "uses compact selection callback when provider/model callback exceeds 64 bytes" , ( ) => {
428- const model = "us.anthropic.claude-3-5-sonnet-20240620-v1:0" ;
429- const result = buildModelsKeyboard ( {
430- provider : "amazon-bedrock" ,
431- models : [ model ] ,
432- currentPage : 1 ,
433- totalPages : 1 ,
434- } ) ;
435-
436- expect ( result [ 0 ] ?. [ 0 ] ?. callback_data ) . toBe ( `mdl_sel/${ model } ` ) ;
437- } ) ;
438483} ) ;
439484
440485describe ( "buildBrowseProvidersButton" , ( ) => {
@@ -473,9 +518,8 @@ describe("large model lists (OpenRouter-scale)", () => {
473518 it ( "handles 100+ models with pagination" , ( ) => {
474519 const models = Array . from ( { length : 150 } , ( _ , i ) => `model-${ i } ` ) ;
475520 const totalPages = calculateTotalPages ( models . length ) ;
476- expect ( totalPages ) . toBe ( 19 ) ; // 150 / 8 = 18.75 -> 19 pages
521+ expect ( totalPages ) . toBe ( 19 ) ;
477522
478- // Test first page
479523 const firstPage = buildModelsKeyboard ( {
480524 provider : "openrouter" ,
481525 models,
@@ -486,20 +530,18 @@ describe("large model lists (OpenRouter-scale)", () => {
486530 expect ( firstPage [ 0 ] ?. [ 0 ] ?. text ) . toBe ( "model-0" ) ;
487531 expect ( firstPage [ 7 ] ?. [ 0 ] ?. text ) . toBe ( "model-7" ) ;
488532
489- // Test last page
490533 const lastPage = buildModelsKeyboard ( {
491534 provider : "openrouter" ,
492535 models,
493536 currentPage : 19 ,
494537 totalPages,
495538 } ) ;
496- // Last page has 150 - (18 * 8) = 6 models
497539 expect ( lastPage . length ) . toBe ( 8 ) ; // 6 models + pagination + back
498540 expect ( lastPage [ 0 ] ?. [ 0 ] ?. text ) . toBe ( "model-144" ) ;
499541 } ) ;
500542
501- it ( "all callback_data stays within 64-byte limit" , ( ) => {
502- // Realistic OpenRouter model IDs
543+ it ( "all callback_data stays within 64-byte limit (#98221) " , ( ) => {
544+ // Realistic model IDs — all should create valid callbacks now
503545 const models = [
504546 "anthropic/claude-3-5-sonnet-20241022" ,
505547 "google/gemini-2.0-flash-thinking-exp:free" ,
@@ -522,23 +564,29 @@ describe("large model lists (OpenRouter-scale)", () => {
522564 }
523565 } ) ;
524566
525- it ( "skips models that would exceed callback_data limit" , ( ) => {
526- const models = [
527- "short-model" ,
528- "this-is-an-extremely-long-model-name-that-definitely-exceeds-the-sixty-four-byte-limit" ,
529- "another-short" ,
530- ] ;
567+ it ( "never drops models due to callback_data limit (#98221)" , ( ) => {
568+ // The exact scenario from the bug report
569+ const longModel = "xentriom/gemma-4-12B-agentic-fable5-composer2.5-v2:latest" ;
570+ const models = [ "short-model-1" , longModel , "short-model-2" ] ;
531571 const result = buildModelsKeyboard ( {
532- provider : "openrouter " ,
572+ provider : "ollama " ,
533573 models,
534574 currentPage : 1 ,
535575 totalPages : 1 ,
536576 } ) ;
537577
538- // Should have 2 model buttons (skipping the long one) + back
578+ // All 3 models should appear (no silent drops)
539579 const modelButtons = result . filter ( ( row ) => ! row [ 0 ] ?. callback_data . startsWith ( "mdl_back" ) ) ;
540- expect ( modelButtons . length ) . toBe ( 2 ) ;
541- expect ( modelButtons [ 0 ] ?. [ 0 ] ?. text ) . toBe ( "short-model" ) ;
542- expect ( modelButtons [ 1 ] ?. [ 0 ] ?. text ) . toBe ( "another-short" ) ;
580+ expect ( modelButtons . length ) . toBe ( 3 ) ;
581+ expect ( modelButtons [ 0 ] ?. [ 0 ] ?. text ) . toBe ( "short-model-1" ) ;
582+ expect ( modelButtons [ 1 ] ?. [ 0 ] ?. text ) . toBe ( "…-agentic-fable5-composer2.5-v2:latest" ) ;
583+ expect ( modelButtons [ 2 ] ?. [ 0 ] ?. text ) . toBe ( "short-model-2" ) ;
584+
585+ // All callback_data is under 64 bytes
586+ for ( const row of result ) {
587+ for ( const button of row ) {
588+ expect ( Buffer . byteLength ( button . callback_data , "utf8" ) ) . toBeLessThanOrEqual ( 64 ) ;
589+ }
590+ }
543591 } ) ;
544592} ) ;
0 commit comments