Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions packages/core/src/core/geminiChat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2188,7 +2188,7 @@ describe('GeminiChat', async () => {
// cheap-gate (real estimate via getHistory + userMessage) →
// splitter (real) → runSideQuery (mocked at baseLlmClient) →
// persistence.
const largeChars = 'x'.repeat(400_000); // ~100K estimated tokens
const largeChars = 'x'.repeat(520_000); // ~130K estimated tokens
const inheritedHistory: Content[] = [
{ role: 'user', parts: [{ text: largeChars }] },
{ role: 'model', parts: [{ text: 'ack' }] },
Expand All @@ -2198,8 +2198,9 @@ describe('GeminiChat', async () => {
chat.setHistory(inheritedHistory);
expect(chat.getLastPromptTokenCount()).toBe(0);

// Default DEFAULT_TOKEN_LIMIT = 128K → auto ≈ 95K. 100K estimate
// crosses, so cheap-gate must let compaction proceed.
// Default DEFAULT_TOKEN_LIMIT = 200K minus the 64K output reservation
// leaves a 136K effective window; 130K crosses the auto threshold, so
// cheap-gate must let compaction proceed.
const generateText = vi.fn().mockResolvedValue({
text: '<state_snapshot>compressed</state_snapshot>',
usage: {
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/core/tokenLimits.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ describe('normalize', () => {
});

describe('tokenLimit', () => {
it('uses 200K as the global default context window', () => {
expect(DEFAULT_TOKEN_LIMIT).toBe(200_000);
});

describe('Google Gemini', () => {
it('should return 1M for Gemini 3.x (latest)', () => {
expect(tokenLimit('gemini-3-pro-preview')).toBe(1000000);
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/core/tokenLimits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type TokenCount = number;
*/
export type TokenLimitType = 'input' | 'output';

export const DEFAULT_TOKEN_LIMIT: TokenCount = 131_072; // 128K (power-of-two)
export const DEFAULT_TOKEN_LIMIT: TokenCount = 200_000; // 200K tokens
export const DEFAULT_OUTPUT_TOKEN_LIMIT: TokenCount = 32_000; // 32K tokens

export const ESCALATED_MAX_TOKENS: TokenCount = 64_000;
Expand Down
79 changes: 79 additions & 0 deletions packages/core/src/models/modelConfigResolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -959,4 +959,83 @@ describe('modelConfigResolver', () => {
expect(result.sources['modalities'].kind).toBe('settings');
});
});

describe('[Regression] env-var-only path must apply model context defaults', () => {
it('env-var-only path: contextWindowSize auto-detected for claude-opus-4-6', () => {
const result = resolveModelConfig({
authType: AuthType.USE_OPENAI,
cli: {},
settings: {},
env: {
Comment thread
tanzhenxin marked this conversation as resolved.
Resolved
OPENAI_API_KEY: 'test-key',
OPENAI_BASE_URL: 'http://localhost:8000/v1',
OPENAI_MODEL: 'claude-opus-4-6',
},
});

expect(result.config.model).toBe('claude-opus-4-6');
expect(result.config.contextWindowSize).toBe(200_000);
expect(result.sources['contextWindowSize'].kind).toBe('computed');
});

it('env-var-only path: contextWindowSize auto-detected for a model whose limit differs from the global default', () => {
// gpt-4o resolves to 131,072 ≠ DEFAULT_TOKEN_LIMIT (200,000), so this
// assertion fails if the fallback applies the generic default instead
// of the model-specific limit.
const result = resolveModelConfig({
authType: AuthType.USE_OPENAI,
cli: {},
settings: {},
env: {
OPENAI_API_KEY: 'test-key',
OPENAI_BASE_URL: 'http://localhost:8000/v1',
OPENAI_MODEL: 'gpt-4o',
},
});

expect(result.config.model).toBe('gpt-4o');
expect(result.config.contextWindowSize).toBe(131_072);
expect(result.sources['contextWindowSize'].kind).toBe('computed');
});

it('env-var-only path: unknown model keeps contextWindowSize undefined', () => {
// Unknown models must not be stamped with an explicit window and an
// 'auto-detected' source — downstream `?? DEFAULT_TOKEN_LIMIT`
// consumers apply the generic default instead.
const result = resolveModelConfig({
authType: AuthType.USE_OPENAI,
cli: {},
settings: {},
env: {
OPENAI_API_KEY: 'test-key',
OPENAI_BASE_URL: 'http://localhost:8000/v1',
OPENAI_MODEL: 'totally-unknown-model-xyz',
},
});

expect(result.config.model).toBe('totally-unknown-model-xyz');
expect(result.config.contextWindowSize).toBeUndefined();
expect(result.sources['contextWindowSize']).toBeUndefined();
});

it('env-var-only path: explicit settings contextWindowSize is not overridden by fallback', () => {
const result = resolveModelConfig({
authType: AuthType.USE_OPENAI,
cli: {},
settings: {
generationConfig: {
contextWindowSize: 32_000,
},
},
env: {
OPENAI_API_KEY: 'test-key',
OPENAI_BASE_URL: 'http://localhost:8000/v1',
OPENAI_MODEL: 'claude-opus-4-6',
},
});

expect(result.config.contextWindowSize).toBe(32_000);
expect(result.sources['contextWindowSize'].kind).toBe('settings');
});
});
});
14 changes: 14 additions & 0 deletions packages/core/src/models/modelConfigResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { AuthType } from '../core/contentGenerator.js';
import type { ContentGeneratorConfig } from '../core/contentGenerator.js';
import { DEFAULT_QWEN_MODEL } from '../config/models.js';
import { defaultModalities } from '../core/modalityDefaults.js';
import { knownTokenLimit } from '../core/tokenLimits.js';
import {
resolveField,
resolveOptionalField,
Expand Down Expand Up @@ -400,6 +401,19 @@ function resolveGenerationConfig(
}
}

// contextWindowSize fallback: auto-detect from model when neither
// modelProvider nor settings supplied it. Only known models are stamped —
// unknown models keep `undefined` so downstream `?? DEFAULT_TOKEN_LIMIT`
// consumers apply the generic default without a misleading
// 'auto-detected' source label.
if (result.contextWindowSize === undefined && modelId) {
const knownLimit = knownTokenLimit(modelId, 'input');
if (knownLimit !== undefined) {
result.contextWindowSize = knownLimit;
sources['contextWindowSize'] = computedSource('auto-detected from model');
}
}

// modalities fallback: auto-detect from model when neither modelProvider nor
// settings supplied it. Mirrors modelRegistry.resolveModelConfig and
// modelsConfig.applyResolvedModelDefaults so all paths agree on which models
Expand Down
4 changes: 2 additions & 2 deletions packages/vscode-ide-companion/src/utils/tokenLimits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ type TokenCount = number;
// Public constants
// ---------------------------------------------------------------------------

/** Default input context window size: 128 K tokens (power-of-two). */
export const DEFAULT_TOKEN_LIMIT: TokenCount = 131_072;
/** Default input context window size: 200 K tokens. */
export const DEFAULT_TOKEN_LIMIT: TokenCount = 200_000;

// ---------------------------------------------------------------------------
// Token limit types
Expand Down
Loading