summaryrefslogtreecommitdiff
path: root/packages/core/src
diff options
context:
space:
mode:
authoranj-s <[email protected]>2025-07-11 07:55:03 -0700
committerGitHub <[email protected]>2025-07-11 14:55:03 +0000
commitc9e1e6d3bdfe1fa1582f278d6f1a606353313642 (patch)
treec6a78c450431d5fca390b1448d0f12e99da13ccc /packages/core/src
parent0151a9e1a3451221faf52e883b2c9d6a49eb1b5c (diff)
Add support for specifying maxSessionTurns via the settings configuration (#3507)
Diffstat (limited to 'packages/core/src')
-rw-r--r--packages/core/src/config/config.ts7
-rw-r--r--packages/core/src/core/client.test.ts130
-rw-r--r--packages/core/src/core/client.ts9
-rw-r--r--packages/core/src/core/turn.ts8
4 files changed, 141 insertions, 13 deletions
diff --git a/packages/core/src/config/config.ts b/packages/core/src/config/config.ts
index 15e9e73b..12767133 100644
--- a/packages/core/src/config/config.ts
+++ b/packages/core/src/config/config.ts
@@ -139,6 +139,7 @@ export interface ConfigParameters {
bugCommand?: BugCommandSettings;
model: string;
extensionContextFilePaths?: string[];
+ maxSessionTurns?: number;
listExtensions?: boolean;
activeExtensions?: ActiveExtension[];
noBrowser?: boolean;
@@ -182,6 +183,7 @@ export class Config {
private readonly extensionContextFilePaths: string[];
private readonly noBrowser: boolean;
private modelSwitchedDuringSession: boolean = false;
+ private readonly maxSessionTurns: number;
private readonly listExtensions: boolean;
private readonly _activeExtensions: ActiveExtension[];
flashFallbackHandler?: FlashFallbackHandler;
@@ -227,6 +229,7 @@ export class Config {
this.bugCommand = params.bugCommand;
this.model = params.model;
this.extensionContextFilePaths = params.extensionContextFilePaths ?? [];
+ this.maxSessionTurns = params.maxSessionTurns ?? -1;
this.listExtensions = params.listExtensions ?? false;
this._activeExtensions = params.activeExtensions ?? [];
this.noBrowser = params.noBrowser ?? false;
@@ -308,6 +311,10 @@ export class Config {
this.flashFallbackHandler = handler;
}
+ getMaxSessionTurns(): number {
+ return this.maxSessionTurns;
+ }
+
setQuotaErrorOccurred(value: boolean): void {
this.quotaErrorOccurred = value;
}
diff --git a/packages/core/src/core/client.test.ts b/packages/core/src/core/client.test.ts
index 2769e1b0..bbcb549b 100644
--- a/packages/core/src/core/client.test.ts
+++ b/packages/core/src/core/client.test.ts
@@ -17,7 +17,7 @@ import { findIndexAfterFraction, GeminiClient } from './client.js';
import { AuthType, ContentGenerator } from './contentGenerator.js';
import { GeminiChat } from './geminiChat.js';
import { Config } from '../config/config.js';
-import { Turn } from './turn.js';
+import { GeminiEventType, Turn } from './turn.js';
import { getCoreSystemPrompt } from './prompts.js';
import { DEFAULT_GEMINI_FLASH_MODEL } from '../config/models.js';
import { FileDiscoveryService } from '../services/fileDiscoveryService.js';
@@ -43,7 +43,13 @@ vi.mock('./turn', () => {
}
}
// Export the mock class as 'Turn'
- return { Turn: MockTurn };
+ return {
+ Turn: MockTurn,
+ GeminiEventType: {
+ MaxSessionTurns: 'MaxSessionTurns',
+ ChatCompressed: 'ChatCompressed',
+ },
+ };
});
vi.mock('../config/config.js');
@@ -68,12 +74,13 @@ vi.mock('../telemetry/index.js', () => ({
describe('findIndexAfterFraction', () => {
const history: Content[] = [
- { role: 'user', parts: [{ text: 'This is the first message.' }] },
- { role: 'model', parts: [{ text: 'This is the second message.' }] },
- { role: 'user', parts: [{ text: 'This is the third message.' }] },
- { role: 'model', parts: [{ text: 'This is the fourth message.' }] },
- { role: 'user', parts: [{ text: 'This is the fifth message.' }] },
+ { role: 'user', parts: [{ text: 'This is the first message.' }] }, // JSON length: 66
+ { role: 'model', parts: [{ text: 'This is the second message.' }] }, // JSON length: 68
+ { role: 'user', parts: [{ text: 'This is the third message.' }] }, // JSON length: 66
+ { role: 'model', parts: [{ text: 'This is the fourth message.' }] }, // JSON length: 68
+ { role: 'user', parts: [{ text: 'This is the fifth message.' }] }, // JSON length: 65
];
+ // Total length: 333
it('should throw an error for non-positive numbers', () => {
expect(() => findIndexAfterFraction(history, 0)).toThrow(
@@ -88,14 +95,23 @@ describe('findIndexAfterFraction', () => {
});
it('should handle a fraction in the middle', () => {
- // Total length is 257. 257 * 0.5 = 128.5
- // 0: 53
- // 1: 53 + 54 = 107
- // 2: 107 + 53 = 160
- // 160 >= 128.5, so index is 2
+ // 333 * 0.5 = 166.5
+ // 0: 66
+ // 1: 66 + 68 = 134
+ // 2: 134 + 66 = 200
+ // 200 >= 166.5, so index is 2
expect(findIndexAfterFraction(history, 0.5)).toBe(2);
});
+ it('should handle a fraction that results in the last index', () => {
+ // 333 * 0.9 = 299.7
+ // ...
+ // 3: 200 + 68 = 268
+ // 4: 268 + 65 = 333
+ // 333 >= 299.7, so index is 4
+ expect(findIndexAfterFraction(history, 0.9)).toBe(4);
+ });
+
it('should handle an empty history', () => {
expect(findIndexAfterFraction([], 0.5)).toBe(0);
});
@@ -178,6 +194,7 @@ describe('Gemini Client (client.ts)', () => {
getProxy: vi.fn().mockReturnValue(undefined),
getWorkingDir: vi.fn().mockReturnValue('/test/dir'),
getFileService: vi.fn().mockReturnValue(fileService),
+ getMaxSessionTurns: vi.fn().mockReturnValue(0),
getQuotaErrorOccurred: vi.fn().mockReturnValue(false),
setQuotaErrorOccurred: vi.fn(),
getNoBrowser: vi.fn().mockReturnValue(false),
@@ -366,6 +383,42 @@ describe('Gemini Client (client.ts)', () => {
contents,
});
});
+
+ it('should allow overriding model and config', async () => {
+ const contents = [{ role: 'user', parts: [{ text: 'hello' }] }];
+ const schema = { type: 'string' };
+ const abortSignal = new AbortController().signal;
+ const customModel = 'custom-json-model';
+ const customConfig = { temperature: 0.9, topK: 20 };
+
+ const mockGenerator: Partial<ContentGenerator> = {
+ countTokens: vi.fn().mockResolvedValue({ totalTokens: 1 }),
+ generateContent: mockGenerateContentFn,
+ };
+ client['contentGenerator'] = mockGenerator as ContentGenerator;
+
+ await client.generateJson(
+ contents,
+ schema,
+ abortSignal,
+ customModel,
+ customConfig,
+ );
+
+ expect(mockGenerateContentFn).toHaveBeenCalledWith({
+ model: customModel,
+ config: {
+ abortSignal,
+ systemInstruction: getCoreSystemPrompt(''),
+ temperature: 0.9,
+ topP: 1, // from default
+ topK: 20,
+ responseSchema: schema,
+ responseMimeType: 'application/json',
+ },
+ contents,
+ });
+ });
});
describe('addHistory', () => {
@@ -660,6 +713,59 @@ describe('Gemini Client (client.ts)', () => {
expect(eventCount).toBeLessThan(200); // Should not exceed our safety limit
});
+ it('should yield MaxSessionTurns and stop when session turn limit is reached', async () => {
+ // Arrange
+ const MAX_SESSION_TURNS = 5;
+ vi.spyOn(client['config'], 'getMaxSessionTurns').mockReturnValue(
+ MAX_SESSION_TURNS,
+ );
+
+ const mockStream = (async function* () {
+ yield { type: 'content', value: 'Hello' };
+ })();
+ mockTurnRunFn.mockReturnValue(mockStream);
+
+ const mockChat: Partial<GeminiChat> = {
+ addHistory: vi.fn(),
+ getHistory: vi.fn().mockReturnValue([]),
+ };
+ client['chat'] = mockChat as GeminiChat;
+
+ const mockGenerator: Partial<ContentGenerator> = {
+ countTokens: vi.fn().mockResolvedValue({ totalTokens: 0 }),
+ };
+ client['contentGenerator'] = mockGenerator as ContentGenerator;
+
+ // Act & Assert
+ // Run up to the limit
+ for (let i = 0; i < MAX_SESSION_TURNS; i++) {
+ const stream = client.sendMessageStream(
+ [{ text: 'Hi' }],
+ new AbortController().signal,
+ 'prompt-id-4',
+ );
+ // consume stream
+ for await (const _event of stream) {
+ // do nothing
+ }
+ }
+
+ // This call should exceed the limit
+ const stream = client.sendMessageStream(
+ [{ text: 'Hi' }],
+ new AbortController().signal,
+ 'prompt-id-5',
+ );
+
+ const events = [];
+ for await (const event of stream) {
+ events.push(event);
+ }
+
+ expect(events).toEqual([{ type: GeminiEventType.MaxSessionTurns }]);
+ expect(mockTurnRunFn).toHaveBeenCalledTimes(MAX_SESSION_TURNS);
+ });
+
it('should respect MAX_TURNS limit even when turns parameter is set to a large value', async () => {
// This test verifies that the infinite loop protection works even when
// someone tries to bypass it by calling with a very large turns value
diff --git a/packages/core/src/core/client.ts b/packages/core/src/core/client.ts
index 5d9ac0cb..0ff8026b 100644
--- a/packages/core/src/core/client.ts
+++ b/packages/core/src/core/client.ts
@@ -86,6 +86,7 @@ export class GeminiClient {
temperature: 0,
topP: 1,
};
+ private sessionTurnCount = 0;
private readonly MAX_TURNS = 100;
/**
* Threshold for compression token count as a fraction of the model's token limit.
@@ -266,6 +267,14 @@ export class GeminiClient {
turns: number = this.MAX_TURNS,
originalModel?: string,
): AsyncGenerator<ServerGeminiStreamEvent, Turn> {
+ this.sessionTurnCount++;
+ if (
+ this.config.getMaxSessionTurns() > 0 &&
+ this.sessionTurnCount > this.config.getMaxSessionTurns()
+ ) {
+ yield { type: GeminiEventType.MaxSessionTurns };
+ return new Turn(this.getChat(), prompt_id);
+ }
// Ensure turns never exceeds MAX_TURNS to prevent infinite loops
const boundedTurns = Math.min(turns, this.MAX_TURNS);
if (!boundedTurns) {
diff --git a/packages/core/src/core/turn.ts b/packages/core/src/core/turn.ts
index aeeaa889..6135b1f6 100644
--- a/packages/core/src/core/turn.ts
+++ b/packages/core/src/core/turn.ts
@@ -48,6 +48,7 @@ export enum GeminiEventType {
Error = 'error',
ChatCompressed = 'chat_compressed',
Thought = 'thought',
+ MaxSessionTurns = 'max_session_turns',
}
export interface StructuredError {
@@ -128,6 +129,10 @@ export type ServerGeminiChatCompressedEvent = {
value: ChatCompressionInfo | null;
};
+export type ServerGeminiMaxSessionTurnsEvent = {
+ type: GeminiEventType.MaxSessionTurns;
+};
+
// The original union type, now composed of the individual types
export type ServerGeminiStreamEvent =
| ServerGeminiContentEvent
@@ -137,7 +142,8 @@ export type ServerGeminiStreamEvent =
| ServerGeminiUserCancelledEvent
| ServerGeminiErrorEvent
| ServerGeminiChatCompressedEvent
- | ServerGeminiThoughtEvent;
+ | ServerGeminiThoughtEvent
+ | ServerGeminiMaxSessionTurnsEvent;
// A turn manages the agentic loop turn within the server context.
export class Turn {