From 0ba1b4dbcc034da41d79d9677fb61853e2c0cea7 Mon Sep 17 00:00:00 2001 From: sausheong Date: Sat, 5 Sep 2026 22:06:59 +0800 Subject: [PATCH] fix(tokens): generalize Gemini context-window detection by tier MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit windowByModelFamily matched Gemini models by exact version substring ("gemini-1.5-pro", "gemini-1.5-flash", "gemini-2"), so any newer generation (3.x and beyond) fell through to defaultRemoteUnknownWindow (128k) purely because this table hadn't been updated yet — observed via hand's status line showing a >100% context gauge for a "gemini-3.8-flash"-family model whose real window is far larger, and which made harness's own preventive compaction unreachable (the threshold check never fires against a window this far off). Match on tier (Pro vs. everything else) instead of enumerating every version: every Pro-tier Gemini release so far ships 2M, everything else at least 1M, and both hold for a model this table has never seen the exact version string of. --- tokens/tokens.go | 20 +++++++++++++++++--- tokens/tokens_test.go | 23 +++++++++++++++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/tokens/tokens.go b/tokens/tokens.go index a0c3468..6c5ea1c 100644 --- a/tokens/tokens.go +++ b/tokens/tokens.go @@ -141,10 +141,24 @@ func windowByModelFamily(modelID string) int { return 8192 case strings.HasPrefix(leaf, "gpt-3.5"): return 16385 - case strings.Contains(id, "gemini-1.5-pro"): + case strings.Contains(id, "gemini") && strings.Contains(id, "pro"): + // Every "Pro"-tier Gemini release so far (1.5 Pro onward) has + // shipped the largest window in its generation. Matching on the + // tier rather than enumerating each version keeps a future Pro + // release (2.5, 3, ...) from falling through to the + // flash/other-tier case below, let alone all the way to + // defaultRemoteUnknownWindow. return 2000000 - case strings.Contains(id, "gemini-1.5-flash"), - strings.Contains(id, "gemini-2"): + case strings.Contains(id, "gemini"): + // Every non-Pro Gemini model since 1.5 (Flash and other tiers) + // has shipped at least a 1M window. Matching on the family alone + // — not "gemini-1.5-flash"/"gemini-2" by exact version — means a + // new Gemini generation (3.x and beyond) gets a realistic window + // immediately instead of silently falling back to + // defaultRemoteUnknownWindow (128k), which made preventive + // compaction fire far too early and the context-usage gauge + // read a nonsensical >100% for a model this table simply hadn't + // been updated for yet. return 1000000 } return 0 diff --git a/tokens/tokens_test.go b/tokens/tokens_test.go index d70a145..4724eba 100644 --- a/tokens/tokens_test.go +++ b/tokens/tokens_test.go @@ -52,6 +52,29 @@ func TestContextWindowKnown(t *testing.T) { } } +// Regression: a Gemini generation newer than the ones explicitly +// enumerated (3.x and beyond) must not fall through to +// defaultRemoteUnknownWindow (128k) just because this table hasn't been +// updated with its exact version string yet — every non-Pro Gemini +// model since 1.5 has shipped at least a 1M window, and every Pro-tier +// release has shipped at least 2M. +func TestContextWindowGeminiFutureGenerationsDefaultByTier(t *testing.T) { + tests := []struct { + model string + want int + }{ + {"openai/gemini-3.8-flash-global", 1000000}, // proxied under an unrelated provider label, as via a LiteLLM gateway + {"google/gemini-3-flash", 1000000}, + {"google/gemini-3-pro", 2000000}, + {"google/gemini-4.0-flash-lite", 1000000}, + } + for _, tc := range tests { + t.Run(tc.model, func(t *testing.T) { + assert.Equal(t, tc.want, ContextWindow(tc.model)) + }) + } +} + func TestContextWindowUnknownRemoteReturnsRemoteFallback(t *testing.T) { // Non-local unknown models default to 128k — frontier proxies are // overwhelmingly 128k+, and reactive compaction handles overflow.