fix(plan): after exiting plan mode switches model to a flash model (#23885)

This commit is contained in:
Adib234
2026-03-26 14:45:03 -04:00
committed by GitHub
parent c888da5f73
commit 9762bf2965
3 changed files with 72 additions and 1 deletions

View File

@@ -4,8 +4,10 @@
* SPDX-License-Identifier: Apache-2.0
*/
import { writeFileSync, mkdirSync } from 'node:fs';
import { join } from 'node:path';
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { TestRig, checkModelOutputContent } from './test-helper.js';
import { GEMINI_DIR, TestRig, checkModelOutputContent } from './test-helper.js';
describe('Plan Mode', () => {
let rig: TestRig;
@@ -227,4 +229,68 @@ describe('Plan Mode', () => {
`Expected write_file to succeed, but it failed with error: ${planWrite?.toolRequest.error}`,
).toBe(true);
});
it('should switch from a pro model to a flash model after exiting plan mode', async () => {
const plansDir = 'plans-folder';
const planFilename = 'my-plan.md';
await rig.setup('should-switch-to-flash', {
settings: {
model: {
name: 'auto-gemini-2.5',
},
experimental: { plan: true },
tools: {
core: ['exit_plan_mode', 'run_shell_command'],
allowed: ['exit_plan_mode', 'run_shell_command'],
},
general: {
defaultApprovalMode: 'plan',
plan: {
directory: plansDir,
},
},
},
});
writeFileSync(
join(rig.homeDir!, GEMINI_DIR, 'state.json'),
JSON.stringify({ terminalSetupPromptShown: true }, null, 2),
);
const fullPlansDir = join(rig.testDir!, plansDir);
mkdirSync(fullPlansDir, { recursive: true });
writeFileSync(join(fullPlansDir, planFilename), 'Execute echo hello');
await rig.run({
approvalMode: 'plan',
stdin: `Exit plan mode using ${planFilename} and then run a shell command \`echo hello\`.`,
});
const exitCallFound = await rig.waitForToolCall('exit_plan_mode');
expect(exitCallFound, 'Expected exit_plan_mode to be called').toBe(true);
const shellCallFound = await rig.waitForToolCall('run_shell_command');
expect(shellCallFound, 'Expected run_shell_command to be called').toBe(
true,
);
const apiRequests = rig.readAllApiRequest();
const modelNames = apiRequests.map((r) => r.attributes?.model || 'unknown');
const proRequests = apiRequests.filter((r) =>
r.attributes?.model?.includes('pro'),
);
const flashRequests = apiRequests.filter((r) =>
r.attributes?.model?.includes('flash'),
);
expect(
proRequests.length,
`Expected at least one Pro request. Models used: ${modelNames.join(', ')}`,
).toBeGreaterThanOrEqual(1);
expect(
flashRequests.length,
`Expected at least one Flash request after mode switch. Models used: ${modelNames.join(', ')}`,
).toBeGreaterThanOrEqual(1);
});
});

View File

@@ -2413,6 +2413,7 @@ export class Config implements McpContext, AgentLoopContext {
if (isPlanModeTransition || isYoloModeTransition) {
if (this._geminiClient?.isInitialized()) {
this._geminiClient.clearCurrentSequenceModel();
this._geminiClient.setTools().catch((err) => {
debugLogger.error('Failed to update tools', err);
});

View File

@@ -132,6 +132,10 @@ export class GeminiClient {
this.updateSystemInstruction();
};
clearCurrentSequenceModel(): void {
this.currentSequenceModel = null;
}
// Hook state to deduplicate BeforeAgent calls and track response for
// AfterAgent
private hookStateMap = new Map<