mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-07-17 05:20:23 -07:00
88985511c6
- Redesigned CORE_SI_SKELETON for maximum reasoning fidelity and minimum token usage. - Extracted Software Engineering and New Application workflows to dynamic skills. - Added 'Essential Workflows' section to maintain visibility for core user journeys. - Implemented precedence-based sorting for available skills (Workspace > User > Built-in). - Added behavioral tests in evals/skill_activation.eval.ts to verify proactive skill activation. - Fixed pre-existing build error in useGeminiStream.ts related to missing FinishReason values.
282 lines
9.6 KiB
TypeScript
282 lines
9.6 KiB
TypeScript
/**
|
|
* @license
|
|
* Copyright 2026 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
import { PromptProvider } from './promptProvider.js';
|
|
import type { Config } from '../config/config.js';
|
|
import {
|
|
getAllGeminiMdFilenames,
|
|
DEFAULT_CONTEXT_FILENAME,
|
|
} from '../tools/memoryTool.js';
|
|
import {
|
|
PREVIEW_GEMINI_MODEL,
|
|
PREVIEW_GEMINI_FLASH_MODEL,
|
|
DEFAULT_GEMINI_FLASH_MODEL,
|
|
} from '../config/models.js';
|
|
import { ApprovalMode } from '../policy/types.js';
|
|
import { DiscoveredMCPTool } from '../tools/mcp-tool.js';
|
|
import { MockTool } from '../test-utils/mock-tool.js';
|
|
import type { CallableTool } from '@google/genai';
|
|
import type { MessageBus } from '../confirmation-bus/message-bus.js';
|
|
|
|
vi.mock('../tools/memoryTool.js', async (importOriginal) => {
|
|
const actual = await importOriginal();
|
|
return {
|
|
...(actual as object),
|
|
getAllGeminiMdFilenames: vi.fn(),
|
|
};
|
|
});
|
|
|
|
vi.mock('../utils/gitUtils', () => ({
|
|
isGitRepository: vi.fn().mockReturnValue(false),
|
|
}));
|
|
|
|
describe('PromptProvider', () => {
|
|
let mockConfig: Config;
|
|
|
|
beforeEach(() => {
|
|
vi.resetAllMocks();
|
|
vi.stubEnv('GEMINI_SNIPPETS_VARIANT', '');
|
|
mockConfig = {
|
|
getToolRegistry: vi.fn().mockReturnValue({
|
|
getAllToolNames: vi.fn().mockReturnValue([]),
|
|
getAllTools: vi.fn().mockReturnValue([]),
|
|
}),
|
|
getEnableShellOutputEfficiency: vi.fn().mockReturnValue(true),
|
|
storage: {
|
|
getProjectTempDir: vi.fn().mockReturnValue('/tmp/project-temp'),
|
|
getPlansDir: vi.fn().mockReturnValue('/tmp/project-temp/plans'),
|
|
},
|
|
isInteractive: vi.fn().mockReturnValue(true),
|
|
isInteractiveShellEnabled: vi.fn().mockReturnValue(true),
|
|
getSkillManager: vi.fn().mockReturnValue({
|
|
getSkills: vi.fn().mockReturnValue([]),
|
|
isSkillActive: vi.fn().mockReturnValue(false),
|
|
}),
|
|
getActiveModel: vi.fn().mockReturnValue(PREVIEW_GEMINI_MODEL),
|
|
getAgentRegistry: vi.fn().mockReturnValue({
|
|
getAllDefinitions: vi.fn().mockReturnValue([]),
|
|
}),
|
|
getApprovedPlanPath: vi.fn().mockReturnValue(undefined),
|
|
getApprovalMode: vi.fn(),
|
|
} as unknown as Config;
|
|
});
|
|
|
|
it('should use capability snippets for Gemini 3 Flash Preview by default', () => {
|
|
vi.mocked(mockConfig.getActiveModel).mockReturnValue(
|
|
PREVIEW_GEMINI_FLASH_MODEL,
|
|
);
|
|
vi.mocked(getAllGeminiMdFilenames).mockReturnValue([
|
|
DEFAULT_CONTEXT_FILENAME,
|
|
]);
|
|
|
|
const provider = new PromptProvider();
|
|
const prompt = provider.getCoreSystemPrompt(mockConfig);
|
|
|
|
// Capability snippets have the Role header from CORE_SI_SKELETON
|
|
expect(prompt).toContain('# Role');
|
|
// And should contain the specific wording from skeleton
|
|
expect(prompt).toContain('You are Gemini CLI, an expert agent.');
|
|
expect(prompt).toContain('# Core Mandates');
|
|
});
|
|
|
|
it('should use minimal snippets for Gemini 2.5 Flash by default', () => {
|
|
vi.mocked(mockConfig.getActiveModel).mockReturnValue(
|
|
DEFAULT_GEMINI_FLASH_MODEL,
|
|
);
|
|
vi.mocked(getAllGeminiMdFilenames).mockReturnValue([
|
|
DEFAULT_CONTEXT_FILENAME,
|
|
]);
|
|
|
|
const provider = new PromptProvider();
|
|
const prompt = provider.getCoreSystemPrompt(mockConfig);
|
|
|
|
// Minimal snippets DO NOT have the Role header (they use preamble)
|
|
expect(prompt).not.toContain('# Role');
|
|
// And use slightly different wording for efficiency
|
|
expect(prompt).toContain(
|
|
'Be strategic to minimize tokens while avoiding extra turns.',
|
|
);
|
|
});
|
|
|
|
it('should handle multiple context filenames in the system prompt', () => {
|
|
vi.mocked(getAllGeminiMdFilenames).mockReturnValue([
|
|
DEFAULT_CONTEXT_FILENAME,
|
|
'CUSTOM.md',
|
|
'ANOTHER.md',
|
|
]);
|
|
|
|
const provider = new PromptProvider();
|
|
const prompt = provider.getCoreSystemPrompt(mockConfig);
|
|
|
|
// Verify renderCoreMandates usage
|
|
expect(prompt).toContain(
|
|
`Instructions found in \`${DEFAULT_CONTEXT_FILENAME}\`, \`CUSTOM.md\` or \`ANOTHER.md\` files are foundational mandates.`,
|
|
);
|
|
});
|
|
|
|
it('should include skill activation guidance and placeholders in capability variant', () => {
|
|
vi.mocked(mockConfig.getActiveModel).mockReturnValue(
|
|
PREVIEW_GEMINI_FLASH_MODEL,
|
|
);
|
|
vi.mocked(mockConfig.getSkillManager().getSkills).mockReturnValue([
|
|
{
|
|
name: 'software-engineering',
|
|
description: 'Expert guidance.',
|
|
location: '/path/to/skill',
|
|
body: 'Skill body',
|
|
},
|
|
]);
|
|
|
|
const provider = new PromptProvider();
|
|
const prompt = provider.getCoreSystemPrompt(mockConfig);
|
|
|
|
expect(prompt).toContain('## Essential Workflows');
|
|
expect(prompt).toContain('software-engineering');
|
|
expect(prompt).toContain(
|
|
'Use `activate_skill` to enable specialized expert guidance',
|
|
);
|
|
});
|
|
|
|
it('should sort available skills (workspace first) and elevate activated skills in capability variant', () => {
|
|
vi.mocked(mockConfig.getActiveModel).mockReturnValue(
|
|
PREVIEW_GEMINI_FLASH_MODEL,
|
|
);
|
|
vi.mocked(mockConfig.getSkillManager().getSkills).mockReturnValue([
|
|
{
|
|
name: 'builtin-skill',
|
|
description: 'Builtin description',
|
|
location: '/path/to/builtin',
|
|
isBuiltin: true,
|
|
},
|
|
{
|
|
name: 'workspace-skill',
|
|
description: 'Workspace description',
|
|
location: '/path/to/workspace',
|
|
isBuiltin: false,
|
|
},
|
|
]);
|
|
vi.mocked(mockConfig.getSkillManager().isSkillActive).mockImplementation(
|
|
(name) => name === 'workspace-skill',
|
|
);
|
|
|
|
const provider = new PromptProvider();
|
|
const prompt = provider.getCoreSystemPrompt(mockConfig);
|
|
|
|
// Activated skills should be before available skills in the Capabilities section
|
|
const activatedIndex = prompt.indexOf('## Activated Skills');
|
|
const availableIndex = prompt.indexOf('## Available Skills');
|
|
expect(activatedIndex).toBeLessThan(availableIndex);
|
|
|
|
// Workspace skill should be before built-in skill in Available Skills
|
|
const workspaceIndex = prompt.indexOf('workspace-skill', availableIndex);
|
|
const builtinIndex = prompt.indexOf('builtin-skill', availableIndex);
|
|
expect(workspaceIndex).toBeLessThan(builtinIndex);
|
|
});
|
|
|
|
it('should handle multiple context filenames in user memory section', () => {
|
|
vi.mocked(getAllGeminiMdFilenames).mockReturnValue([
|
|
DEFAULT_CONTEXT_FILENAME,
|
|
'CUSTOM.md',
|
|
]);
|
|
|
|
const provider = new PromptProvider();
|
|
const prompt = provider.getCoreSystemPrompt(
|
|
mockConfig,
|
|
'Some memory content',
|
|
);
|
|
|
|
// Verify renderUserMemory usage
|
|
expect(prompt).toContain(
|
|
`# Contextual Instructions (${DEFAULT_CONTEXT_FILENAME}, CUSTOM.md)`,
|
|
);
|
|
});
|
|
|
|
describe('plan mode prompt', () => {
|
|
const mockMessageBus = {
|
|
publish: vi.fn(),
|
|
subscribe: vi.fn(),
|
|
unsubscribe: vi.fn(),
|
|
} as unknown as MessageBus;
|
|
|
|
beforeEach(() => {
|
|
vi.mocked(getAllGeminiMdFilenames).mockReturnValue([
|
|
DEFAULT_CONTEXT_FILENAME,
|
|
]);
|
|
(mockConfig.getApprovalMode as ReturnType<typeof vi.fn>).mockReturnValue(
|
|
ApprovalMode.PLAN,
|
|
);
|
|
});
|
|
|
|
it('should list all active tools from ToolRegistry in plan mode prompt', () => {
|
|
const mockTools = [
|
|
new MockTool({ name: 'glob', displayName: 'Glob' }),
|
|
new MockTool({ name: 'read_file', displayName: 'ReadFile' }),
|
|
new MockTool({ name: 'write_file', displayName: 'WriteFile' }),
|
|
new MockTool({ name: 'replace', displayName: 'Replace' }),
|
|
];
|
|
(mockConfig.getToolRegistry as ReturnType<typeof vi.fn>).mockReturnValue({
|
|
getAllToolNames: vi.fn().mockReturnValue(mockTools.map((t) => t.name)),
|
|
getAllTools: vi.fn().mockReturnValue(mockTools),
|
|
});
|
|
|
|
const provider = new PromptProvider();
|
|
const prompt = provider.getCoreSystemPrompt(mockConfig);
|
|
|
|
expect(prompt).toContain('`glob`');
|
|
expect(prompt).toContain('`read_file`');
|
|
expect(prompt).toContain('`write_file`');
|
|
expect(prompt).toContain('`replace`');
|
|
});
|
|
|
|
it('should show server name for MCP tools in plan mode prompt', () => {
|
|
const mcpTool = new DiscoveredMCPTool(
|
|
{} as CallableTool,
|
|
'my-mcp-server',
|
|
'mcp_read',
|
|
'An MCP read tool',
|
|
{},
|
|
mockMessageBus,
|
|
undefined,
|
|
true,
|
|
);
|
|
const mockTools = [
|
|
new MockTool({ name: 'glob', displayName: 'Glob' }),
|
|
mcpTool,
|
|
];
|
|
(mockConfig.getToolRegistry as ReturnType<typeof vi.fn>).mockReturnValue({
|
|
getAllToolNames: vi.fn().mockReturnValue(mockTools.map((t) => t.name)),
|
|
getAllTools: vi.fn().mockReturnValue(mockTools),
|
|
});
|
|
|
|
const provider = new PromptProvider();
|
|
const prompt = provider.getCoreSystemPrompt(mockConfig);
|
|
|
|
expect(prompt).toContain('`mcp_read` (my-mcp-server)');
|
|
});
|
|
|
|
it('should include write constraint message in plan mode prompt', () => {
|
|
const mockTools = [
|
|
new MockTool({ name: 'glob', displayName: 'Glob' }),
|
|
new MockTool({ name: 'write_file', displayName: 'WriteFile' }),
|
|
new MockTool({ name: 'replace', displayName: 'Replace' }),
|
|
];
|
|
(mockConfig.getToolRegistry as ReturnType<typeof vi.fn>).mockReturnValue({
|
|
getAllToolNames: vi.fn().mockReturnValue(mockTools.map((t) => t.name)),
|
|
getAllTools: vi.fn().mockReturnValue(mockTools),
|
|
});
|
|
|
|
const provider = new PromptProvider();
|
|
const prompt = provider.getCoreSystemPrompt(mockConfig);
|
|
|
|
expect(prompt).toContain(
|
|
'`write_file` and `replace` may ONLY be used to write .md plan files',
|
|
);
|
|
expect(prompt).toContain('/tmp/project-temp/plans/');
|
|
});
|
|
});
|
|
});
|