Add a context percentage threshold setting for auto compression (#5721)

This commit is contained in:
Jacob MacDonald
2025-08-07 07:34:40 -07:00
committed by GitHub
parent 36750ca49b
commit 6ae75c9f32
8 changed files with 219 additions and 7 deletions

View File

@@ -69,6 +69,10 @@ export interface BugCommandSettings {
urlTemplate: string;
}
export interface ChatCompressionSettings {
contextPercentageThreshold?: number;
}
export interface SummarizeToolOutputSettings {
tokenBudget?: number;
}
@@ -191,6 +195,7 @@ export interface ConfigParameters {
folderTrustFeature?: boolean;
ideMode?: boolean;
loadMemoryFromIncludeDirectories?: boolean;
chatCompression?: ChatCompressionSettings;
}
export class Config {
@@ -252,6 +257,7 @@ export class Config {
| undefined;
private readonly experimentalAcp: boolean = false;
private readonly loadMemoryFromIncludeDirectories: boolean = false;
private readonly chatCompression: ChatCompressionSettings | undefined;
constructor(params: ConfigParameters) {
this.sessionId = params.sessionId;
@@ -316,6 +322,7 @@ export class Config {
}
this.loadMemoryFromIncludeDirectories =
params.loadMemoryFromIncludeDirectories ?? false;
this.chatCompression = params.chatCompression;
if (params.contextFileName) {
setGeminiMdFilename(params.contextFileName);
@@ -667,6 +674,10 @@ export class Config {
return this.ideClient;
}
getChatCompression(): ChatCompressionSettings | undefined {
return this.chatCompression;
}
async getGitService(): Promise<GitService> {
if (!this.gitService) {
this.gitService = new GitService(this.targetDir);

View File

@@ -206,6 +206,7 @@ describe('Gemini Client (client.ts)', () => {
}),
getGeminiClient: vi.fn(),
setFallbackMode: vi.fn(),
getChatCompression: vi.fn().mockReturnValue(undefined),
};
const MockedConfig = vi.mocked(Config, true);
MockedConfig.mockImplementation(
@@ -531,14 +532,19 @@ describe('Gemini Client (client.ts)', () => {
expect(newChat).toBe(initialChat);
});
it('should trigger summarization if token count is at threshold', async () => {
it('should trigger summarization if token count is at threshold with contextPercentageThreshold setting', async () => {
const MOCKED_TOKEN_LIMIT = 1000;
const MOCKED_CONTEXT_PERCENTAGE_THRESHOLD = 0.5;
vi.mocked(tokenLimit).mockReturnValue(MOCKED_TOKEN_LIMIT);
vi.spyOn(client['config'], 'getChatCompression').mockReturnValue({
contextPercentageThreshold: MOCKED_CONTEXT_PERCENTAGE_THRESHOLD,
});
mockGetHistory.mockReturnValue([
{ role: 'user', parts: [{ text: '...history...' }] },
]);
const originalTokenCount = 1000 * 0.7;
const originalTokenCount =
MOCKED_TOKEN_LIMIT * MOCKED_CONTEXT_PERCENTAGE_THRESHOLD;
const newTokenCount = 100;
mockCountTokens

View File

@@ -596,12 +596,16 @@ export class GeminiClient {
return null;
}
const contextPercentageThreshold =
this.config.getChatCompression()?.contextPercentageThreshold;
// Don't compress if not forced and we are under the limit.
if (
!force &&
originalTokenCount < this.COMPRESSION_TOKEN_THRESHOLD * tokenLimit(model)
) {
return null;
if (!force) {
const threshold =
contextPercentageThreshold ?? this.COMPRESSION_TOKEN_THRESHOLD;
if (originalTokenCount < threshold * tokenLimit(model)) {
return null;
}
}
let compressBeforeIndex = findIndexAfterFraction(