diff options
Diffstat (limited to 'packages')
| -rw-r--r-- | packages/cli/src/config/config.ts | 16 | ||||
| -rw-r--r-- | packages/cli/src/config/settings.test.ts | 309 | ||||
| -rw-r--r-- | packages/cli/src/config/settings.ts | 1 | ||||
| -rw-r--r-- | packages/cli/src/ui/App.test.tsx | 289 | ||||
| -rw-r--r-- | packages/cli/src/ui/App.tsx | 11 | ||||
| -rw-r--r-- | packages/core/src/config/config.test.ts | 42 | ||||
| -rw-r--r-- | packages/core/src/config/config.ts | 7 | ||||
| -rw-r--r-- | packages/core/src/tools/memoryTool.test.ts | 39 | ||||
| -rw-r--r-- | packages/core/src/tools/memoryTool.ts | 18 | ||||
| -rw-r--r-- | packages/core/src/tools/read-many-files.ts | 4 | ||||
| -rw-r--r-- | packages/core/src/utils/memoryDiscovery.test.ts | 321 | ||||
| -rw-r--r-- | packages/core/src/utils/memoryDiscovery.ts | 40 |
12 files changed, 1000 insertions, 97 deletions
diff --git a/packages/cli/src/config/config.ts b/packages/cli/src/config/config.ts index 04347427..44057fad 100644 --- a/packages/cli/src/config/config.ts +++ b/packages/cli/src/config/config.ts @@ -13,6 +13,8 @@ import { createServerConfig, loadServerHierarchicalMemory, ConfigParameters, + setGeminiMdFilename as setServerGeminiMdFilename, + getCurrentGeminiMdFilename, } from '@gemini-code/core'; import { Settings } from './settings.js'; import { readPackageUp } from 'read-package-up'; @@ -132,6 +134,17 @@ export async function loadCliConfig(settings: Settings): Promise<Config> { const argv = await parseArguments(); const debugMode = argv.debug || false; + // Set the context filename in the server's memoryTool module BEFORE loading memory + // TODO(b/343434939): This is a bit of a hack. The contextFileName should ideally be passed + // directly to the Config constructor in core, and have core handle setGeminiMdFilename. + // However, loadHierarchicalGeminiMemory is called *before* createServerConfig. + if (settings.contextFileName) { + setServerGeminiMdFilename(settings.contextFileName); + } else { + // Reset to default if not provided in settings. + setServerGeminiMdFilename(getCurrentGeminiMdFilename()); + } + // Call the (now wrapper) loadHierarchicalGeminiMemory which calls the server's version const { memoryContent, fileCount } = await loadHierarchicalGeminiMemory( process.cwd(), @@ -159,7 +172,8 @@ export async function loadCliConfig(settings: Settings): Promise<Config> { userMemory: memoryContent, geminiMdFileCount: fileCount, vertexai: useVertexAI, - showMemoryUsage: argv.show_memory_usage || false, + showMemoryUsage: + argv.show_memory_usage || settings.showMemoryUsage || false, }; return createServerConfig(configParams); diff --git a/packages/cli/src/config/settings.test.ts b/packages/cli/src/config/settings.test.ts new file mode 100644 index 00000000..4d61ed8b --- /dev/null +++ b/packages/cli/src/config/settings.test.ts @@ -0,0 +1,309 @@ +/** + * @license + * Copyright 2025 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +/// <reference types="vitest/globals" /> + +const MOCK_HOME_DIR = '/mock/home/user'; // MUST BE FIRST + +// Mock 'os' first. Its factory uses MOCK_HOME_DIR. +import * as osActual from 'os'; // Import for type info for the mock factory +vi.mock('os', async (importOriginal) => { + const actualOs = await importOriginal<typeof osActual>(); + return { + ...actualOs, + homedir: vi.fn(() => MOCK_HOME_DIR), + }; +}); + +// Mock './settings.js' to ensure it uses the mocked 'os.homedir()' for its internal constants. +vi.mock('./settings.js', async (importActual) => { + const originalModule = await importActual<typeof import('./settings.js')>(); + return { + __esModule: true, // Ensure correct module shape + ...originalModule, // Re-export all original members + // We are relying on originalModule's USER_SETTINGS_PATH being constructed with mocked os.homedir() + }; +}); + +// NOW import everything else, including the (now effectively re-exported) settings.js +import * as pathActual from 'path'; // Restored for MOCK_WORKSPACE_SETTINGS_PATH +import { + describe, + it, + expect, + vi, + beforeEach, + afterEach, + type Mocked, + type Mock, +} from 'vitest'; +import * as fs from 'fs'; // fs will be mocked separately +import stripJsonComments from 'strip-json-comments'; // Will be mocked separately + +// These imports will get the versions from the vi.mock('./settings.js', ...) factory. +import { + LoadedSettings, + loadSettings, + USER_SETTINGS_PATH, // This IS the mocked path. + SETTINGS_DIRECTORY_NAME, // This is from the original module, but used by the mock. + SettingScope, +} from './settings.js'; + +const MOCK_WORKSPACE_DIR = '/mock/workspace'; +// Use the (mocked) SETTINGS_DIRECTORY_NAME for consistency +const MOCK_WORKSPACE_SETTINGS_PATH = pathActual.join( + MOCK_WORKSPACE_DIR, + SETTINGS_DIRECTORY_NAME, + 'settings.json', +); + +vi.mock('fs'); +vi.mock('strip-json-comments', () => ({ + default: vi.fn((content) => content), +})); + +describe('Settings Loading and Merging', () => { + let mockFsExistsSync: Mocked<typeof fs.existsSync>; + let mockStripJsonComments: Mocked<typeof stripJsonComments>; + let mockFsMkdirSync: Mocked<typeof fs.mkdirSync>; + + beforeEach(() => { + vi.resetAllMocks(); + + mockFsExistsSync = vi.mocked(fs.existsSync); + mockFsMkdirSync = vi.mocked(fs.mkdirSync); + mockStripJsonComments = vi.mocked(stripJsonComments); + + vi.mocked(osActual.homedir).mockReturnValue(MOCK_HOME_DIR); + (mockStripJsonComments as unknown as Mock).mockImplementation( + (jsonString: string) => jsonString, + ); + (mockFsExistsSync as Mock).mockReturnValue(false); + (fs.readFileSync as Mock).mockReturnValue('{}'); // Return valid empty JSON + (mockFsMkdirSync as Mock).mockImplementation(() => undefined); + }); + + afterEach(() => { + vi.restoreAllMocks(); + }); + + describe('loadSettings', () => { + it('should load empty settings if no files exist', () => { + const settings = loadSettings(MOCK_WORKSPACE_DIR); + expect(settings.user.settings).toEqual({}); + expect(settings.workspace.settings).toEqual({}); + expect(settings.merged).toEqual({}); + }); + + it('should load user settings if only user file exists', () => { + const expectedUserSettingsPath = USER_SETTINGS_PATH; // Use the path actually resolved by the (mocked) module + + (mockFsExistsSync as Mock).mockImplementation( + (p: fs.PathLike) => p === expectedUserSettingsPath, + ); + const userSettingsContent = { + theme: 'dark', + contextFileName: 'USER_CONTEXT.md', + }; + (fs.readFileSync as Mock).mockImplementation( + (p: fs.PathOrFileDescriptor) => { + if (p === expectedUserSettingsPath) + return JSON.stringify(userSettingsContent); + return '{}'; + }, + ); + + const settings = loadSettings(MOCK_WORKSPACE_DIR); + + expect(fs.readFileSync).toHaveBeenCalledWith( + expectedUserSettingsPath, + 'utf-8', + ); + expect(settings.user.settings).toEqual(userSettingsContent); + expect(settings.workspace.settings).toEqual({}); + expect(settings.merged).toEqual(userSettingsContent); + }); + + it('should load workspace settings if only workspace file exists', () => { + (mockFsExistsSync as Mock).mockImplementation( + (p: fs.PathLike) => p === MOCK_WORKSPACE_SETTINGS_PATH, + ); + const workspaceSettingsContent = { + sandbox: true, + contextFileName: 'WORKSPACE_CONTEXT.md', + }; + (fs.readFileSync as Mock).mockImplementation( + (p: fs.PathOrFileDescriptor) => { + if (p === MOCK_WORKSPACE_SETTINGS_PATH) + return JSON.stringify(workspaceSettingsContent); + return ''; + }, + ); + + const settings = loadSettings(MOCK_WORKSPACE_DIR); + + expect(fs.readFileSync).toHaveBeenCalledWith( + MOCK_WORKSPACE_SETTINGS_PATH, + 'utf-8', + ); + expect(settings.user.settings).toEqual({}); + expect(settings.workspace.settings).toEqual(workspaceSettingsContent); + expect(settings.merged).toEqual(workspaceSettingsContent); + }); + + it('should merge user and workspace settings, with workspace taking precedence', () => { + (mockFsExistsSync as Mock).mockReturnValue(true); + const userSettingsContent = { + theme: 'dark', + sandbox: false, + contextFileName: 'USER_CONTEXT.md', + }; + const workspaceSettingsContent = { + sandbox: true, + coreTools: ['tool1'], + contextFileName: 'WORKSPACE_CONTEXT.md', + }; + + (fs.readFileSync as Mock).mockImplementation( + (p: fs.PathOrFileDescriptor) => { + if (p === USER_SETTINGS_PATH) + return JSON.stringify(userSettingsContent); + if (p === MOCK_WORKSPACE_SETTINGS_PATH) + return JSON.stringify(workspaceSettingsContent); + return ''; + }, + ); + + const settings = loadSettings(MOCK_WORKSPACE_DIR); + + expect(settings.user.settings).toEqual(userSettingsContent); + expect(settings.workspace.settings).toEqual(workspaceSettingsContent); + expect(settings.merged).toEqual({ + theme: 'dark', + sandbox: true, + coreTools: ['tool1'], + contextFileName: 'WORKSPACE_CONTEXT.md', + }); + }); + + it('should handle contextFileName correctly when only in user settings', () => { + (mockFsExistsSync as Mock).mockImplementation( + (p: fs.PathLike) => p === USER_SETTINGS_PATH, + ); + const userSettingsContent = { contextFileName: 'CUSTOM.md' }; + (fs.readFileSync as Mock).mockImplementation( + (p: fs.PathOrFileDescriptor) => { + if (p === USER_SETTINGS_PATH) + return JSON.stringify(userSettingsContent); + return ''; + }, + ); + + const settings = loadSettings(MOCK_WORKSPACE_DIR); + expect(settings.merged.contextFileName).toBe('CUSTOM.md'); + }); + + it('should handle contextFileName correctly when only in workspace settings', () => { + (mockFsExistsSync as Mock).mockImplementation( + (p: fs.PathLike) => p === MOCK_WORKSPACE_SETTINGS_PATH, + ); + const workspaceSettingsContent = { + contextFileName: 'PROJECT_SPECIFIC.md', + }; + (fs.readFileSync as Mock).mockImplementation( + (p: fs.PathOrFileDescriptor) => { + if (p === MOCK_WORKSPACE_SETTINGS_PATH) + return JSON.stringify(workspaceSettingsContent); + return ''; + }, + ); + + const settings = loadSettings(MOCK_WORKSPACE_DIR); + expect(settings.merged.contextFileName).toBe('PROJECT_SPECIFIC.md'); + }); + + it('should default contextFileName to undefined if not in any settings file', () => { + (mockFsExistsSync as Mock).mockReturnValue(true); + const userSettingsContent = { theme: 'dark' }; + const workspaceSettingsContent = { sandbox: true }; + (fs.readFileSync as Mock).mockImplementation( + (p: fs.PathOrFileDescriptor) => { + if (p === USER_SETTINGS_PATH) + return JSON.stringify(userSettingsContent); + if (p === MOCK_WORKSPACE_SETTINGS_PATH) + return JSON.stringify(workspaceSettingsContent); + return ''; + }, + ); + + const settings = loadSettings(MOCK_WORKSPACE_DIR); + expect(settings.merged.contextFileName).toBeUndefined(); + }); + + it('should handle JSON parsing errors gracefully', () => { + (mockFsExistsSync as Mock).mockReturnValue(true); + (fs.readFileSync as Mock).mockImplementation( + (p: fs.PathOrFileDescriptor) => { + // Make it return invalid json for the paths it will try to read + if (p === USER_SETTINGS_PATH || p === MOCK_WORKSPACE_SETTINGS_PATH) + return 'invalid json'; + return ''; + }, + ); + const consoleErrorSpy = vi + .spyOn(console, 'error') + .mockImplementation(() => {}); + + const settings = loadSettings(MOCK_WORKSPACE_DIR); + + expect(settings.user.settings).toEqual({}); + expect(settings.workspace.settings).toEqual({}); + expect(settings.merged).toEqual({}); + expect(consoleErrorSpy).toHaveBeenCalledTimes(2); + + consoleErrorSpy.mockRestore(); + }); + }); + + describe('LoadedSettings class', () => { + it('setValue should update the correct scope and recompute merged settings', () => { + (mockFsExistsSync as Mock).mockReturnValue(false); + const loadedSettings = loadSettings(MOCK_WORKSPACE_DIR) as LoadedSettings; + + vi.mocked(fs.writeFileSync).mockImplementation(() => {}); + // mkdirSync is mocked in beforeEach to return undefined, which is fine for void usage + + loadedSettings.setValue(SettingScope.User, 'theme', 'matrix'); + expect(loadedSettings.user.settings.theme).toBe('matrix'); + expect(loadedSettings.merged.theme).toBe('matrix'); + expect(fs.writeFileSync).toHaveBeenCalledWith( + USER_SETTINGS_PATH, + JSON.stringify({ theme: 'matrix' }, null, 2), + 'utf-8', + ); + + loadedSettings.setValue( + SettingScope.Workspace, + 'contextFileName', + 'MY_AGENTS.md', + ); + expect(loadedSettings.workspace.settings.contextFileName).toBe( + 'MY_AGENTS.md', + ); + expect(loadedSettings.merged.contextFileName).toBe('MY_AGENTS.md'); + expect(loadedSettings.merged.theme).toBe('matrix'); + expect(fs.writeFileSync).toHaveBeenCalledWith( + MOCK_WORKSPACE_SETTINGS_PATH, + JSON.stringify({ contextFileName: 'MY_AGENTS.md' }, null, 2), + 'utf-8', + ); + + loadedSettings.setValue(SettingScope.Workspace, 'theme', 'ocean'); + expect(loadedSettings.workspace.settings.theme).toBe('ocean'); + expect(loadedSettings.merged.theme).toBe('ocean'); + }); + }); +}); diff --git a/packages/cli/src/config/settings.ts b/packages/cli/src/config/settings.ts index 1aabc127..5d51ba15 100644 --- a/packages/cli/src/config/settings.ts +++ b/packages/cli/src/config/settings.ts @@ -30,6 +30,7 @@ export interface Settings { mcpServerCommand?: string; mcpServers?: Record<string, MCPServerConfig>; showMemoryUsage?: boolean; + contextFileName?: string; // Add other settings here. } diff --git a/packages/cli/src/ui/App.test.tsx b/packages/cli/src/ui/App.test.tsx new file mode 100644 index 00000000..a7811f6a --- /dev/null +++ b/packages/cli/src/ui/App.test.tsx @@ -0,0 +1,289 @@ +/** + * @license + * Copyright 2025 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +import { describe, it, expect, vi, beforeEach, afterEach, Mock } from 'vitest'; +import { render } from 'ink-testing-library'; +import { App } from './App.js'; +import { Config as ServerConfig, MCPServerConfig } from '@gemini-code/core'; +import type { ToolRegistry } from '@gemini-code/core'; +import { LoadedSettings, SettingsFile, Settings } from '../config/settings.js'; + +// Define a more complete mock server config based on actual Config +interface MockServerConfig { + apiKey: string; + model: string; + sandbox: boolean | string; + targetDir: string; + debugMode: boolean; + question?: string; + fullContext: boolean; + coreTools?: string[]; + toolDiscoveryCommand?: string; + toolCallCommand?: string; + mcpServerCommand?: string; + mcpServers?: Record<string, MCPServerConfig>; // Use imported MCPServerConfig + userAgent: string; + userMemory: string; + geminiMdFileCount: number; + alwaysSkipModificationConfirmation: boolean; + vertexai?: boolean; + showMemoryUsage?: boolean; + + getApiKey: Mock<() => string>; + getModel: Mock<() => string>; + getSandbox: Mock<() => boolean | string>; + getTargetDir: Mock<() => string>; + getToolRegistry: Mock<() => ToolRegistry>; // Use imported ToolRegistry type + getDebugMode: Mock<() => boolean>; + getQuestion: Mock<() => string | undefined>; + getFullContext: Mock<() => boolean>; + getCoreTools: Mock<() => string[] | undefined>; + getToolDiscoveryCommand: Mock<() => string | undefined>; + getToolCallCommand: Mock<() => string | undefined>; + getMcpServerCommand: Mock<() => string | undefined>; + getMcpServers: Mock<() => Record<string, MCPServerConfig> | undefined>; + getUserAgent: Mock<() => string>; + getUserMemory: Mock<() => string>; + setUserMemory: Mock<(newUserMemory: string) => void>; + getGeminiMdFileCount: Mock<() => number>; + setGeminiMdFileCount: Mock<(count: number) => void>; + getAlwaysSkipModificationConfirmation: Mock<() => boolean>; + setAlwaysSkipModificationConfirmation: Mock<(skip: boolean) => void>; + getVertexAI: Mock<() => boolean | undefined>; + getShowMemoryUsage: Mock<() => boolean>; +} + +// Mock @gemini-code/core and its Config class +vi.mock('@gemini-code/core', async (importOriginal) => { + const actualCore = await importOriginal<typeof import('@gemini-code/core')>(); + const ConfigClassMock = vi + .fn() + .mockImplementation((optionsPassedToConstructor) => { + const opts = { ...optionsPassedToConstructor }; // Clone + // Basic mock structure, will be extended by the instance in tests + return { + apiKey: opts.apiKey || 'test-key', + model: opts.model || 'test-model-in-mock-factory', + sandbox: typeof opts.sandbox === 'boolean' ? opts.sandbox : false, + targetDir: opts.targetDir || '/test/dir', + debugMode: opts.debugMode || false, + question: opts.question, + fullContext: opts.fullContext ?? false, + coreTools: opts.coreTools, + toolDiscoveryCommand: opts.toolDiscoveryCommand, + toolCallCommand: opts.toolCallCommand, + mcpServerCommand: opts.mcpServerCommand, + mcpServers: opts.mcpServers, + userAgent: opts.userAgent || 'test-agent', + userMemory: opts.userMemory || '', + geminiMdFileCount: opts.geminiMdFileCount || 0, + alwaysSkipModificationConfirmation: + opts.alwaysSkipModificationConfirmation ?? false, + vertexai: opts.vertexai, + showMemoryUsage: opts.showMemoryUsage ?? false, + + getApiKey: vi.fn(() => opts.apiKey || 'test-key'), + getModel: vi.fn(() => opts.model || 'test-model-in-mock-factory'), + getSandbox: vi.fn(() => + typeof opts.sandbox === 'boolean' ? opts.sandbox : false, + ), + getTargetDir: vi.fn(() => opts.targetDir || '/test/dir'), + getToolRegistry: vi.fn(() => ({}) as ToolRegistry), // Simple mock + getDebugMode: vi.fn(() => opts.debugMode || false), + getQuestion: vi.fn(() => opts.question), + getFullContext: vi.fn(() => opts.fullContext ?? false), + getCoreTools: vi.fn(() => opts.coreTools), + getToolDiscoveryCommand: vi.fn(() => opts.toolDiscoveryCommand), + getToolCallCommand: vi.fn(() => opts.toolCallCommand), + getMcpServerCommand: vi.fn(() => opts.mcpServerCommand), + getMcpServers: vi.fn(() => opts.mcpServers), + getUserAgent: vi.fn(() => opts.userAgent || 'test-agent'), + getUserMemory: vi.fn(() => opts.userMemory || ''), + setUserMemory: vi.fn(), + getGeminiMdFileCount: vi.fn(() => opts.geminiMdFileCount || 0), + setGeminiMdFileCount: vi.fn(), + getAlwaysSkipModificationConfirmation: vi.fn( + () => opts.alwaysSkipModificationConfirmation ?? false, + ), + setAlwaysSkipModificationConfirmation: vi.fn(), + getVertexAI: vi.fn(() => opts.vertexai), + getShowMemoryUsage: vi.fn(() => opts.showMemoryUsage ?? false), + }; + }); + return { + ...actualCore, + Config: ConfigClassMock, + MCPServerConfig: actualCore.MCPServerConfig, + }; +}); + +// Mock heavy dependencies or those with side effects +vi.mock('./hooks/useGeminiStream', () => ({ + useGeminiStream: vi.fn(() => ({ + streamingState: 'Idle', + submitQuery: vi.fn(), + initError: null, + pendingHistoryItems: [], + })), +})); + +vi.mock('./hooks/useLogger', () => ({ + useLogger: vi.fn(() => ({ + getPreviousUserMessages: vi.fn().mockResolvedValue([]), + })), +})); + +vi.mock('../config/config.js', async (importOriginal) => { + const actual = await importOriginal(); + return { + // @ts-expect-error - this is fine + ...actual, + loadHierarchicalGeminiMemory: vi + .fn() + .mockResolvedValue({ memoryContent: '', fileCount: 0 }), + }; +}); + +describe('App UI', () => { + let mockConfig: MockServerConfig; + let mockSettings: LoadedSettings; + let currentUnmount: (() => void) | undefined; + + const createMockSettings = ( + settings: Partial<Settings> = {}, + ): LoadedSettings => { + const userSettingsFile: SettingsFile = { + path: '/user/settings.json', + settings: {}, + }; + const workspaceSettingsFile: SettingsFile = { + path: '/workspace/.gemini/settings.json', + settings, + }; + return new LoadedSettings(userSettingsFile, workspaceSettingsFile); + }; + + beforeEach(() => { + const ServerConfigMocked = vi.mocked(ServerConfig, true); + mockConfig = new ServerConfigMocked({ + apiKey: 'test-key', + model: 'test-model-in-options', + sandbox: false, + targetDir: '/test/dir', + debugMode: false, + userAgent: 'test-agent', + userMemory: '', + geminiMdFileCount: 0, + showMemoryUsage: false, + // Provide other required fields for ConfigParameters if necessary + }) as unknown as MockServerConfig; + + // Ensure the getShowMemoryUsage mock function is specifically set up if not covered by constructor mock + if (!mockConfig.getShowMemoryUsage) { + mockConfig.getShowMemoryUsage = vi.fn(() => false); + } + mockConfig.getShowMemoryUsage.mockReturnValue(false); // Default for most tests + + mockSettings = createMockSettings(); + }); + + afterEach(() => { + if (currentUnmount) { + currentUnmount(); + currentUnmount = undefined; + } + vi.clearAllMocks(); // Clear mocks after each test + }); + + it('should display default "GEMINI.md" in footer when contextFileName is not set and count is 1', async () => { + mockConfig.getGeminiMdFileCount.mockReturnValue(1); + // For this test, ensure showMemoryUsage is false or debugMode is false if it relies on that + mockConfig.getDebugMode.mockReturnValue(false); + mockConfig.getShowMemoryUsage.mockReturnValue(false); + + const { lastFrame, unmount } = render( + <App + config={mockConfig as unknown as ServerConfig} + settings={mockSettings} + cliVersion="1.0.0" + />, + ); + currentUnmount = unmount; + await Promise.resolve(); // Wait for any async updates + expect(lastFrame()).toContain('Using 1 GEMINI.md file'); + }); + + it('should display default "GEMINI.md" with plural when contextFileName is not set and count is > 1', async () => { + mockConfig.getGeminiMdFileCount.mockReturnValue(2); + mockConfig.getDebugMode.mockReturnValue(false); + mockConfig.getShowMemoryUsage.mockReturnValue(false); + + const { lastFrame, unmount } = render( + <App + config={mockConfig as unknown as ServerConfig} + settings={mockSettings} + cliVersion="1.0.0" + />, + ); + currentUnmount = unmount; + await Promise.resolve(); + expect(lastFrame()).toContain('Using 2 GEMINI.md files'); + }); + + it('should display custom contextFileName in footer when set and count is 1', async () => { + mockSettings = createMockSettings({ contextFileName: 'AGENTS.MD' }); + mockConfig.getGeminiMdFileCount.mockReturnValue(1); + mockConfig.getDebugMode.mockReturnValue(false); + mockConfig.getShowMemoryUsage.mockReturnValue(false); + + const { lastFrame, unmount } = render( + <App + config={mockConfig as unknown as ServerConfig} + settings={mockSettings} + cliVersion="1.0.0" + />, + ); + currentUnmount = unmount; + await Promise.resolve(); + expect(lastFrame()).toContain('Using 1 AGENTS.MD file'); + }); + + it('should display custom contextFileName with plural when set and count is > 1', async () => { + mockSettings = createMockSettings({ contextFileName: 'MY_NOTES.TXT' }); + mockConfig.getGeminiMdFileCount.mockReturnValue(3); + mockConfig.getDebugMode.mockReturnValue(false); + mockConfig.getShowMemoryUsage.mockReturnValue(false); + + const { lastFrame, unmount } = render( + <App + config={mockConfig as unknown as ServerConfig} + settings={mockSettings} + cliVersion="1.0.0" + />, + ); + currentUnmount = unmount; + await Promise.resolve(); + expect(lastFrame()).toContain('Using 3 MY_NOTES.TXT files'); + }); + + it('should not display context file message if count is 0, even if contextFileName is set', async () => { + mockSettings = createMockSettings({ contextFileName: 'ANY_FILE.MD' }); + mockConfig.getGeminiMdFileCount.mockReturnValue(0); + mockConfig.getDebugMode.mockReturnValue(false); + mockConfig.getShowMemoryUsage.mockReturnValue(false); + + const { lastFrame, unmount } = render( + <App + config={mockConfig as unknown as ServerConfig} + settings={mockSettings} + cliVersion="1.0.0" + />, + ); + currentUnmount = unmount; + await Promise.resolve(); + expect(lastFrame()).not.toContain('ANY_FILE.MD'); + }); +}); diff --git a/packages/cli/src/ui/App.tsx b/packages/cli/src/ui/App.tsx index e1657984..2f216db7 100644 --- a/packages/cli/src/ui/App.tsx +++ b/packages/cli/src/ui/App.tsx @@ -39,7 +39,11 @@ import { DetailedMessagesDisplay } from './components/DetailedMessagesDisplay.js import { HistoryItemDisplay } from './components/HistoryItemDisplay.js'; import { useHistory } from './hooks/useHistoryManager.js'; import process from 'node:process'; -import { getErrorMessage, type Config } from '@gemini-code/core'; +import { + getErrorMessage, + type Config, + getCurrentGeminiMdFilename, +} from '@gemini-code/core'; import { useLogger } from './hooks/useLogger.js'; import { StreamingContext } from './contexts/StreamingContext.js'; import { useGitBranchName } from './hooks/useGitBranchName.js'; @@ -398,7 +402,10 @@ export const App = ({ </Text> ) : geminiMdFileCount > 0 ? ( <Text color={Colors.SubtleComment}> - Using {geminiMdFileCount} GEMINI.md file + Using {geminiMdFileCount}{' '} + {settings.merged.contextFileName || + getCurrentGeminiMdFilename()}{' '} + file {geminiMdFileCount > 1 ? 's' : ''} </Text> ) : ( diff --git a/packages/core/src/config/config.test.ts b/packages/core/src/config/config.test.ts index f84ad746..c3c46659 100644 --- a/packages/core/src/config/config.test.ts +++ b/packages/core/src/config/config.test.ts @@ -4,10 +4,10 @@ * SPDX-License-Identifier: Apache-2.0 */ -import { describe, it, expect, vi, beforeEach /*, afterEach */ } from 'vitest'; // afterEach removed as it was unused -import { Config, createServerConfig, ConfigParameters } from './config.js'; // Adjust import path +import { describe, it, expect, vi, beforeEach } from 'vitest'; +import { Config, createServerConfig, ConfigParameters } from './config.js'; import * as path from 'path'; -// import { ToolRegistry } from '../tools/tool-registry'; // ToolRegistry removed as it was unused +import { setGeminiMdFilename as mockSetGeminiMdFilename } from '../tools/memoryTool.js'; // Mock dependencies that might be called during Config construction or createServerConfig vi.mock('../tools/tool-registry', () => { @@ -30,6 +30,12 @@ vi.mock('../tools/shell'); vi.mock('../tools/write-file'); vi.mock('../tools/web-fetch'); vi.mock('../tools/read-many-files'); +vi.mock('../tools/memoryTool', () => ({ + MemoryTool: vi.fn(), + setGeminiMdFilename: vi.fn(), + getCurrentGeminiMdFilename: vi.fn(() => 'GEMINI.md'), // Mock the original filename + DEFAULT_CONTEXT_FILENAME: 'GEMINI.md', +})); describe('Server Config (config.ts)', () => { const API_KEY = 'server-api-key'; @@ -106,4 +112,34 @@ describe('Server Config (config.ts)', () => { const config = createServerConfig(paramsWithRelativeDir); expect(config.getTargetDir()).toBe(expectedResolvedDir); }); + + it('createServerConfig should call setGeminiMdFilename with contextFileName if provided', () => { + const contextFileName = 'CUSTOM_AGENTS.md'; + const paramsWithContextFile: ConfigParameters = { + ...baseParams, + contextFileName, + }; + createServerConfig(paramsWithContextFile); + expect(mockSetGeminiMdFilename).toHaveBeenCalledWith(contextFileName); + }); + + it('createServerConfig should not call setGeminiMdFilename if contextFileName is not provided', () => { + createServerConfig(baseParams); // baseParams does not have contextFileName + expect(mockSetGeminiMdFilename).not.toHaveBeenCalled(); + }); + + it('Config constructor should call setGeminiMdFilename with contextFileName if provided', () => { + const contextFileName = 'CUSTOM_AGENTS.md'; + const paramsWithContextFile: ConfigParameters = { + ...baseParams, + contextFileName, + }; + new Config(paramsWithContextFile); + expect(mockSetGeminiMdFilename).toHaveBeenCalledWith(contextFileName); + }); + + it('Config constructor should not call setGeminiMdFilename if contextFileName is not provided', () => { + new Config(baseParams); // baseParams does not have contextFileName + expect(mockSetGeminiMdFilename).not.toHaveBeenCalled(); + }); }); diff --git a/packages/core/src/config/config.ts b/packages/core/src/config/config.ts index 0cd7a4fa..d918de04 100644 --- a/packages/core/src/config/config.ts +++ b/packages/core/src/config/config.ts @@ -19,7 +19,7 @@ import { ShellTool } from '../tools/shell.js'; import { WriteFileTool } from '../tools/write-file.js'; import { WebFetchTool } from '../tools/web-fetch.js'; import { ReadManyFilesTool } from '../tools/read-many-files.js'; -import { MemoryTool } from '../tools/memoryTool.js'; +import { MemoryTool, setGeminiMdFilename } from '../tools/memoryTool.js'; import { WebSearchTool } from '../tools/web-search.js'; export class MCPServerConfig { @@ -56,6 +56,7 @@ export interface ConfigParameters { alwaysSkipModificationConfirmation?: boolean; vertexai?: boolean; showMemoryUsage?: boolean; + contextFileName?: string; } export class Config { @@ -100,6 +101,10 @@ export class Config { this.vertexai = params.vertexai; this.showMemoryUsage = params.showMemoryUsage ?? false; + if (params.contextFileName) { + setGeminiMdFilename(params.contextFileName); + } + this.toolRegistry = createToolRegistry(this); } diff --git a/packages/core/src/tools/memoryTool.test.ts b/packages/core/src/tools/memoryTool.test.ts index 42b1329d..612a08dc 100644 --- a/packages/core/src/tools/memoryTool.test.ts +++ b/packages/core/src/tools/memoryTool.test.ts @@ -5,7 +5,12 @@ */ import { vi, describe, it, expect, beforeEach, afterEach, Mock } from 'vitest'; -import { MemoryTool } from './memoryTool.js'; +import { + MemoryTool, + setGeminiMdFilename, + getCurrentGeminiMdFilename, + DEFAULT_CONTEXT_FILENAME, +} from './memoryTool.js'; import * as fs from 'fs/promises'; import * as path from 'path'; import * as os from 'os'; @@ -50,10 +55,33 @@ describe('MemoryTool', () => { afterEach(() => { vi.restoreAllMocks(); + // Reset GEMINI_MD_FILENAME to its original value after each test + setGeminiMdFilename(DEFAULT_CONTEXT_FILENAME); + }); + + describe('setGeminiMdFilename', () => { + it('should update currentGeminiMdFilename when a valid new name is provided', () => { + const newName = 'CUSTOM_CONTEXT.md'; + setGeminiMdFilename(newName); + expect(getCurrentGeminiMdFilename()).toBe(newName); + }); + + it('should not update currentGeminiMdFilename if the new name is empty or whitespace', () => { + const initialName = getCurrentGeminiMdFilename(); // Get current before trying to change + setGeminiMdFilename(' '); + expect(getCurrentGeminiMdFilename()).toBe(initialName); + + setGeminiMdFilename(''); + expect(getCurrentGeminiMdFilename()).toBe(initialName); + }); }); describe('performAddMemoryEntry (static method)', () => { - const testFilePath = path.join('/mock/home', '.gemini', 'GEMINI.md'); + const testFilePath = path.join( + '/mock/home', + '.gemini', + DEFAULT_CONTEXT_FILENAME, // Use the default for basic tests + ); it('should create section and save a fact if file does not exist', async () => { mockFsAdapter.readFile.mockRejectedValue({ code: 'ENOENT' }); // Simulate file not found @@ -168,7 +196,12 @@ describe('MemoryTool', () => { it('should call performAddMemoryEntry with correct parameters and return success', async () => { const params = { fact: 'The sky is blue' }; const result = await memoryTool.execute(params, mockAbortSignal); - const expectedFilePath = path.join('/mock/home', '.gemini', 'GEMINI.md'); + // Use getCurrentGeminiMdFilename for the default expectation before any setGeminiMdFilename calls in a test + const expectedFilePath = path.join( + '/mock/home', + '.gemini', + getCurrentGeminiMdFilename(), // This will be DEFAULT_CONTEXT_FILENAME unless changed by a test + ); // For this test, we expect the actual fs methods to be passed const expectedFsArgument = { diff --git a/packages/core/src/tools/memoryTool.ts b/packages/core/src/tools/memoryTool.ts index 49dce59d..a0c62eae 100644 --- a/packages/core/src/tools/memoryTool.ts +++ b/packages/core/src/tools/memoryTool.ts @@ -46,15 +46,29 @@ Do NOT use this tool: `; export const GEMINI_CONFIG_DIR = '.gemini'; -export const GEMINI_MD_FILENAME = 'GEMINI.md'; +export const DEFAULT_CONTEXT_FILENAME = 'GEMINI.md'; export const MEMORY_SECTION_HEADER = '## Gemini Added Memories'; +// This variable will hold the currently configured filename for GEMINI.md context files. +// It defaults to DEFAULT_CONTEXT_FILENAME but can be overridden by setGeminiMdFilename. +let currentGeminiMdFilename = DEFAULT_CONTEXT_FILENAME; + +export function setGeminiMdFilename(newFilename: string): void { + if (newFilename && newFilename.trim() !== '') { + currentGeminiMdFilename = newFilename.trim(); + } +} + +export function getCurrentGeminiMdFilename(): string { + return currentGeminiMdFilename; +} + interface SaveMemoryParams { fact: string; } function getGlobalMemoryFilePath(): string { - return path.join(homedir(), GEMINI_CONFIG_DIR, GEMINI_MD_FILENAME); + return path.join(homedir(), GEMINI_CONFIG_DIR, getCurrentGeminiMdFilename()); } /** diff --git a/packages/core/src/tools/read-many-files.ts b/packages/core/src/tools/read-many-files.ts index d826c9ba..4ba09ef0 100644 --- a/packages/core/src/tools/read-many-files.ts +++ b/packages/core/src/tools/read-many-files.ts @@ -9,7 +9,7 @@ import { SchemaValidator } from '../utils/schemaValidator.js'; import { getErrorMessage } from '../utils/errors.js'; import * as path from 'path'; import fg from 'fast-glob'; -import { GEMINI_MD_FILENAME } from './memoryTool.js'; +import { getCurrentGeminiMdFilename } from './memoryTool.js'; import { detectFileType, processSingleFileContent, @@ -98,7 +98,7 @@ const DEFAULT_EXCLUDES: string[] = [ '**/*.odp', '**/*.DS_Store', '**/.env', - `**/${GEMINI_MD_FILENAME}`, + `**/${getCurrentGeminiMdFilename()}`, ]; const DEFAULT_OUTPUT_SEPARATOR_FORMAT = '--- {filePath} ---'; diff --git a/packages/core/src/utils/memoryDiscovery.test.ts b/packages/core/src/utils/memoryDiscovery.test.ts index 229f51e5..db0ffd1d 100644 --- a/packages/core/src/utils/memoryDiscovery.test.ts +++ b/packages/core/src/utils/memoryDiscovery.test.ts @@ -4,22 +4,21 @@ * SPDX-License-Identifier: Apache-2.0 */ -import { - vi, - describe, - it, - expect, - beforeEach, - // afterEach, // Removed unused import - Mocked, -} from 'vitest'; +import { vi, describe, it, expect, beforeEach, Mocked } from 'vitest'; import * as fsPromises from 'fs/promises'; -import * as fsSync from 'fs'; // For constants -import { Stats, Dirent } from 'fs'; // Import types directly from 'fs' +import * as fsSync from 'fs'; +import { Stats, Dirent } from 'fs'; import * as os from 'os'; import * as path from 'path'; import { loadServerHierarchicalMemory } from './memoryDiscovery.js'; -import { GEMINI_CONFIG_DIR, GEMINI_MD_FILENAME } from '../tools/memoryTool.js'; +import { + GEMINI_CONFIG_DIR, + setGeminiMdFilename, + getCurrentGeminiMdFilename, + DEFAULT_CONTEXT_FILENAME, +} from '../tools/memoryTool.js'; + +const ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST = DEFAULT_CONTEXT_FILENAME; // Mock the entire fs/promises module vi.mock('fs/promises'); @@ -29,8 +28,6 @@ vi.mock('fs', async (importOriginal) => { return { ...actual, // Spread actual to get all exports, including Stats and Dirent if they are classes/constructors constants: { ...actual.constants }, // Preserve constants - // Mock other fsSync functions if directly used by memoryDiscovery, e.g., existsSync - // existsSync: vi.fn(), }; }); vi.mock('os'); @@ -42,20 +39,29 @@ describe('loadServerHierarchicalMemory', () => { const CWD = '/test/project/src'; const PROJECT_ROOT = '/test/project'; const USER_HOME = '/test/userhome'; - const GLOBAL_GEMINI_DIR = path.join(USER_HOME, GEMINI_CONFIG_DIR); - const GLOBAL_GEMINI_FILE = path.join(GLOBAL_GEMINI_DIR, GEMINI_MD_FILENAME); + + let GLOBAL_GEMINI_DIR: string; + let GLOBAL_GEMINI_FILE: string; // Defined in beforeEach beforeEach(() => { vi.resetAllMocks(); - + setGeminiMdFilename(DEFAULT_CONTEXT_FILENAME); // Use defined const mockOs.homedir.mockReturnValue(USER_HOME); + + // Define these here to use potentially reset/updated values from imports + GLOBAL_GEMINI_DIR = path.join(USER_HOME, GEMINI_CONFIG_DIR); + GLOBAL_GEMINI_FILE = path.join( + GLOBAL_GEMINI_DIR, + getCurrentGeminiMdFilename(), // Use current filename + ); + mockFs.stat.mockRejectedValue(new Error('File not found')); mockFs.readdir.mockResolvedValue([]); mockFs.readFile.mockRejectedValue(new Error('File not found')); mockFs.access.mockRejectedValue(new Error('File not found')); }); - it('should return empty memory and count if no GEMINI.md files are found', async () => { + it('should return empty memory and count if no context files are found', async () => { const { memoryContent, fileCount } = await loadServerHierarchicalMemory( CWD, false, @@ -64,15 +70,19 @@ describe('loadServerHierarchicalMemory', () => { expect(fileCount).toBe(0); }); - it('should load only the global GEMINI.md if present and others are not', async () => { + it('should load only the global context file if present and others are not (default filename)', async () => { + const globalDefaultFile = path.join( + GLOBAL_GEMINI_DIR, + DEFAULT_CONTEXT_FILENAME, + ); mockFs.access.mockImplementation(async (p) => { - if (p === GLOBAL_GEMINI_FILE) { + if (p === globalDefaultFile) { return undefined; } throw new Error('File not found'); }); mockFs.readFile.mockImplementation(async (p) => { - if (p === GLOBAL_GEMINI_FILE) { + if (p === globalDefaultFile) { return 'Global memory content'; } throw new Error('File not found'); @@ -84,15 +94,157 @@ describe('loadServerHierarchicalMemory', () => { ); expect(memoryContent).toBe( - `--- Context from: ${path.relative(CWD, GLOBAL_GEMINI_FILE)} ---\nGlobal memory content\n--- End of Context from: ${path.relative(CWD, GLOBAL_GEMINI_FILE)} ---`, + `--- Context from: ${path.relative(CWD, globalDefaultFile)} ---\nGlobal memory content\n--- End of Context from: ${path.relative(CWD, globalDefaultFile)} ---`, + ); + expect(fileCount).toBe(1); + expect(mockFs.readFile).toHaveBeenCalledWith(globalDefaultFile, 'utf-8'); + }); + + it('should load only the global custom context file if present and filename is changed', async () => { + const customFilename = 'CUSTOM_AGENTS.md'; + setGeminiMdFilename(customFilename); + const globalCustomFile = path.join(GLOBAL_GEMINI_DIR, customFilename); + + mockFs.access.mockImplementation(async (p) => { + if (p === globalCustomFile) { + return undefined; + } + throw new Error('File not found'); + }); + mockFs.readFile.mockImplementation(async (p) => { + if (p === globalCustomFile) { + return 'Global custom memory'; + } + throw new Error('File not found'); + }); + + const { memoryContent, fileCount } = await loadServerHierarchicalMemory( + CWD, + false, + ); + + expect(memoryContent).toBe( + `--- Context from: ${path.relative(CWD, globalCustomFile)} ---\nGlobal custom memory\n--- End of Context from: ${path.relative(CWD, globalCustomFile)} ---`, ); expect(fileCount).toBe(1); - expect(mockFs.readFile).toHaveBeenCalledWith(GLOBAL_GEMINI_FILE, 'utf-8'); + expect(mockFs.readFile).toHaveBeenCalledWith(globalCustomFile, 'utf-8'); + }); + + it('should load context files by upward traversal with custom filename', async () => { + const customFilename = 'PROJECT_CONTEXT.md'; + setGeminiMdFilename(customFilename); + const projectRootCustomFile = path.join(PROJECT_ROOT, customFilename); + const srcCustomFile = path.join(CWD, customFilename); + + mockFs.stat.mockImplementation(async (p) => { + if (p === path.join(PROJECT_ROOT, '.git')) { + return { isDirectory: () => true } as Stats; + } + throw new Error('File not found'); + }); + + mockFs.access.mockImplementation(async (p) => { + if (p === projectRootCustomFile || p === srcCustomFile) { + return undefined; + } + throw new Error('File not found'); + }); + + mockFs.readFile.mockImplementation(async (p) => { + if (p === projectRootCustomFile) { + return 'Project root custom memory'; + } + if (p === srcCustomFile) { + return 'Src directory custom memory'; + } + throw new Error('File not found'); + }); + + const { memoryContent, fileCount } = await loadServerHierarchicalMemory( + CWD, + false, + ); + const expectedContent = + `--- Context from: ${path.relative(CWD, projectRootCustomFile)} ---\nProject root custom memory\n--- End of Context from: ${path.relative(CWD, projectRootCustomFile)} ---\n\n` + + `--- Context from: ${customFilename} ---\nSrc directory custom memory\n--- End of Context from: ${customFilename} ---`; + + expect(memoryContent).toBe(expectedContent); + expect(fileCount).toBe(2); + expect(mockFs.readFile).toHaveBeenCalledWith( + projectRootCustomFile, + 'utf-8', + ); + expect(mockFs.readFile).toHaveBeenCalledWith(srcCustomFile, 'utf-8'); + }); + + it('should load context files by downward traversal with custom filename', async () => { + const customFilename = 'LOCAL_CONTEXT.md'; + setGeminiMdFilename(customFilename); + const subDir = path.join(CWD, 'subdir'); + const subDirCustomFile = path.join(subDir, customFilename); + const cwdCustomFile = path.join(CWD, customFilename); + + mockFs.access.mockImplementation(async (p) => { + if (p === cwdCustomFile || p === subDirCustomFile) return undefined; + throw new Error('File not found'); + }); + + mockFs.readFile.mockImplementation(async (p) => { + if (p === cwdCustomFile) return 'CWD custom memory'; + if (p === subDirCustomFile) return 'Subdir custom memory'; + throw new Error('File not found'); + }); + + mockFs.readdir.mockImplementation((async ( + p: fsSync.PathLike, + ): Promise<Dirent[]> => { + if (p === CWD) { + return [ + { + name: customFilename, + isFile: () => true, + isDirectory: () => false, + } as Dirent, + { + name: 'subdir', + isFile: () => false, + isDirectory: () => true, + } as Dirent, + ] as Dirent[]; + } + if (p === subDir) { + return [ + { + name: customFilename, + isFile: () => true, + isDirectory: () => false, + } as Dirent, + ] as Dirent[]; + } + return [] as Dirent[]; + }) as unknown as typeof fsPromises.readdir); + + const { memoryContent, fileCount } = await loadServerHierarchicalMemory( + CWD, + false, + ); + const expectedContent = + `--- Context from: ${customFilename} ---\nCWD custom memory\n--- End of Context from: ${customFilename} ---\n\n` + + `--- Context from: ${path.join('subdir', customFilename)} ---\nSubdir custom memory\n--- End of Context from: ${path.join('subdir', customFilename)} ---`; + + expect(memoryContent).toBe(expectedContent); + expect(fileCount).toBe(2); }); - it('should load GEMINI.md files by upward traversal from CWD to project root', async () => { - const projectRootGeminiFile = path.join(PROJECT_ROOT, GEMINI_MD_FILENAME); - const srcGeminiFile = path.join(CWD, GEMINI_MD_FILENAME); + it('should load ORIGINAL_GEMINI_MD_FILENAME files by upward traversal from CWD to project root', async () => { + const projectRootGeminiFile = path.join( + PROJECT_ROOT, + ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST, + ); + const srcGeminiFile = path.join( + CWD, + ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST, + ); mockFs.stat.mockImplementation(async (p) => { if (p === path.join(PROJECT_ROOT, '.git')) { @@ -124,7 +276,7 @@ describe('loadServerHierarchicalMemory', () => { ); const expectedContent = `--- Context from: ${path.relative(CWD, projectRootGeminiFile)} ---\nProject root memory\n--- End of Context from: ${path.relative(CWD, projectRootGeminiFile)} ---\n\n` + - `--- Context from: ${GEMINI_MD_FILENAME} ---\nSrc directory memory\n--- End of Context from: ${GEMINI_MD_FILENAME} ---`; + `--- Context from: ${ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST} ---\nSrc directory memory\n--- End of Context from: ${ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST} ---`; expect(memoryContent).toBe(expectedContent); expect(fileCount).toBe(2); @@ -135,10 +287,16 @@ describe('loadServerHierarchicalMemory', () => { expect(mockFs.readFile).toHaveBeenCalledWith(srcGeminiFile, 'utf-8'); }); - it('should load GEMINI.md files by downward traversal from CWD', async () => { + it('should load ORIGINAL_GEMINI_MD_FILENAME files by downward traversal from CWD', async () => { const subDir = path.join(CWD, 'subdir'); - const subDirGeminiFile = path.join(subDir, GEMINI_MD_FILENAME); - const cwdGeminiFile = path.join(CWD, GEMINI_MD_FILENAME); + const subDirGeminiFile = path.join( + subDir, + ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST, + ); + const cwdGeminiFile = path.join( + CWD, + ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST, + ); mockFs.access.mockImplementation(async (p) => { if (p === cwdGeminiFile || p === subDirGeminiFile) return undefined; @@ -157,59 +315,79 @@ describe('loadServerHierarchicalMemory', () => { if (p === CWD) { return [ { - name: GEMINI_MD_FILENAME, + name: ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST, isFile: () => true, isDirectory: () => false, - }, - { name: 'subdir', isFile: () => false, isDirectory: () => true }, + } as Dirent, + { + name: 'subdir', + isFile: () => false, + isDirectory: () => true, + } as Dirent, ] as Dirent[]; } if (p === subDir) { return [ { - name: GEMINI_MD_FILENAME, + name: ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST, isFile: () => true, isDirectory: () => false, - }, + } as Dirent, ] as Dirent[]; } return [] as Dirent[]; - // eslint-disable-next-line @typescript-eslint/no-explicit-any - }) as any); + }) as unknown as typeof fsPromises.readdir); const { memoryContent, fileCount } = await loadServerHierarchicalMemory( CWD, false, ); const expectedContent = - `--- Context from: ${GEMINI_MD_FILENAME} ---\nCWD memory\n--- End of Context from: ${GEMINI_MD_FILENAME} ---\n\n` + - `--- Context from: ${path.join('subdir', GEMINI_MD_FILENAME)} ---\nSubdir memory\n--- End of Context from: ${path.join('subdir', GEMINI_MD_FILENAME)} ---`; + `--- Context from: ${ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST} ---\nCWD memory\n--- End of Context from: ${ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST} ---\n\n` + + `--- Context from: ${path.join('subdir', ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST)} ---\nSubdir memory\n--- End of Context from: ${path.join('subdir', ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST)} ---`; expect(memoryContent).toBe(expectedContent); expect(fileCount).toBe(2); }); - it('should load and correctly order global, upward, and downward GEMINI.md files', async () => { + it('should load and correctly order global, upward, and downward ORIGINAL_GEMINI_MD_FILENAME files', async () => { + setGeminiMdFilename(ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST); // Explicitly set for this test + + const globalFileToUse = path.join( + GLOBAL_GEMINI_DIR, + ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST, + ); const projectParentDir = path.dirname(PROJECT_ROOT); const projectParentGeminiFile = path.join( projectParentDir, - GEMINI_MD_FILENAME, + ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST, + ); + const projectRootGeminiFile = path.join( + PROJECT_ROOT, + ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST, + ); + const cwdGeminiFile = path.join( + CWD, + ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST, ); - const projectRootGeminiFile = path.join(PROJECT_ROOT, GEMINI_MD_FILENAME); - const cwdGeminiFile = path.join(CWD, GEMINI_MD_FILENAME); const subDir = path.join(CWD, 'sub'); - const subDirGeminiFile = path.join(subDir, GEMINI_MD_FILENAME); + const subDirGeminiFile = path.join( + subDir, + ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST, + ); mockFs.stat.mockImplementation(async (p) => { if (p === path.join(PROJECT_ROOT, '.git')) { return { isDirectory: () => true } as Stats; + } else if (p === path.join(PROJECT_ROOT, '.gemini')) { + return { isDirectory: () => true } as Stats; } throw new Error('File not found'); }); mockFs.access.mockImplementation(async (p) => { if ( - p === GLOBAL_GEMINI_FILE || + p === globalFileToUse || // Use the dynamically set global file path p === projectParentGeminiFile || p === projectRootGeminiFile || p === cwdGeminiFile || @@ -221,7 +399,7 @@ describe('loadServerHierarchicalMemory', () => { }); mockFs.readFile.mockImplementation(async (p) => { - if (p === GLOBAL_GEMINI_FILE) return 'Global memory'; + if (p === globalFileToUse) return 'Global memory'; // Use the dynamically set global file path if (p === projectParentGeminiFile) return 'Project parent memory'; if (p === projectRootGeminiFile) return 'Project root memory'; if (p === cwdGeminiFile) return 'CWD memory'; @@ -234,21 +412,24 @@ describe('loadServerHierarchicalMemory', () => { ): Promise<Dirent[]> => { if (p === CWD) { return [ - { name: 'sub', isFile: () => false, isDirectory: () => true }, + { + name: 'sub', + isFile: () => false, + isDirectory: () => true, + } as Dirent, ] as Dirent[]; } if (p === subDir) { return [ { - name: GEMINI_MD_FILENAME, + name: ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST, isFile: () => true, isDirectory: () => false, - }, + } as Dirent, ] as Dirent[]; } return [] as Dirent[]; - // eslint-disable-next-line @typescript-eslint/no-explicit-any - }) as any); + }) as unknown as typeof fsPromises.readdir); const { memoryContent, fileCount } = await loadServerHierarchicalMemory( CWD, @@ -258,8 +439,11 @@ describe('loadServerHierarchicalMemory', () => { const relPathGlobal = path.relative(CWD, GLOBAL_GEMINI_FILE); const relPathProjectParent = path.relative(CWD, projectParentGeminiFile); const relPathProjectRoot = path.relative(CWD, projectRootGeminiFile); - const relPathCwd = GEMINI_MD_FILENAME; - const relPathSubDir = path.join('sub', GEMINI_MD_FILENAME); + const relPathCwd = ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST; + const relPathSubDir = path.join( + 'sub', + ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST, + ); const expectedContent = [ `--- Context from: ${relPathGlobal} ---\nGlobal memory\n--- End of Context from: ${relPathGlobal} ---`, @@ -275,11 +459,14 @@ describe('loadServerHierarchicalMemory', () => { it('should ignore specified directories during downward scan', async () => { const ignoredDir = path.join(CWD, 'node_modules'); - const ignoredDirGeminiFile = path.join(ignoredDir, GEMINI_MD_FILENAME); + const ignoredDirGeminiFile = path.join( + ignoredDir, + ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST, + ); // Corrected const regularSubDir = path.join(CWD, 'my_code'); const regularSubDirGeminiFile = path.join( regularSubDir, - GEMINI_MD_FILENAME, + ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST, ); mockFs.access.mockImplementation(async (p) => { @@ -303,38 +490,41 @@ describe('loadServerHierarchicalMemory', () => { name: 'node_modules', isFile: () => false, isDirectory: () => true, - }, - { name: 'my_code', isFile: () => false, isDirectory: () => true }, + } as Dirent, + { + name: 'my_code', + isFile: () => false, + isDirectory: () => true, + } as Dirent, ] as Dirent[]; } if (p === regularSubDir) { return [ { - name: GEMINI_MD_FILENAME, + name: ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST, isFile: () => true, isDirectory: () => false, - }, + } as Dirent, ] as Dirent[]; } if (p === ignoredDir) { return [ { - name: GEMINI_MD_FILENAME, + name: ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST, isFile: () => true, isDirectory: () => false, - }, + } as Dirent, ] as Dirent[]; } return [] as Dirent[]; - // eslint-disable-next-line @typescript-eslint/no-explicit-any - }) as any); + }) as unknown as typeof fsPromises.readdir); const { memoryContent, fileCount } = await loadServerHierarchicalMemory( CWD, false, ); - const expectedContent = `--- Context from: ${path.join('my_code', GEMINI_MD_FILENAME)} ---\nMy code memory\n--- End of Context from: ${path.join('my_code', GEMINI_MD_FILENAME)} ---`; + const expectedContent = `--- Context from: ${path.join('my_code', ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST)} ---\nMy code memory\n--- End of Context from: ${path.join('my_code', ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST)} ---`; expect(memoryContent).toBe(expectedContent); expect(fileCount).toBe(1); @@ -365,8 +555,7 @@ describe('loadServerHierarchicalMemory', () => { if (p.toString().startsWith(path.join(CWD, 'deep_dir_'))) return [] as Dirent[]; return [] as Dirent[]; - // eslint-disable-next-line @typescript-eslint/no-explicit-any - }) as any); + }) as unknown as typeof fsPromises.readdir); mockFs.access.mockRejectedValue(new Error('not found')); await loadServerHierarchicalMemory(CWD, true); diff --git a/packages/core/src/utils/memoryDiscovery.ts b/packages/core/src/utils/memoryDiscovery.ts index 362134d8..2e6ce9fc 100644 --- a/packages/core/src/utils/memoryDiscovery.ts +++ b/packages/core/src/utils/memoryDiscovery.ts @@ -8,7 +8,10 @@ import * as fs from 'fs/promises'; import * as fsSync from 'fs'; import * as path from 'path'; import { homedir } from 'os'; -import { GEMINI_CONFIG_DIR, GEMINI_MD_FILENAME } from '../tools/memoryTool.js'; +import { + GEMINI_CONFIG_DIR, + getCurrentGeminiMdFilename, +} from '../tools/memoryTool.js'; // Simple console logger, similar to the one previously in CLI's config.ts // TODO: Integrate with a more robust server-side logger if available/appropriate. @@ -92,7 +95,7 @@ async function collectDownwardGeminiFiles( if (debugMode) logger.debug( - `Scanning downward for ${GEMINI_MD_FILENAME} files in: ${directory} (scanned: ${scannedDirCount.count}/${maxScanDirs})`, + `Scanning downward for ${getCurrentGeminiMdFilename()} files in: ${directory} (scanned: ${scannedDirCount.count}/${maxScanDirs})`, ); const collectedPaths: string[] = []; try { @@ -113,18 +116,21 @@ async function collectDownwardGeminiFiles( maxScanDirs, ); collectedPaths.push(...subDirPaths); - } else if (entry.isFile() && entry.name === GEMINI_MD_FILENAME) { + } else if ( + entry.isFile() && + entry.name === getCurrentGeminiMdFilename() + ) { try { await fs.access(fullPath, fsSync.constants.R_OK); collectedPaths.push(fullPath); if (debugMode) logger.debug( - `Found readable downward ${GEMINI_MD_FILENAME}: ${fullPath}`, + `Found readable downward ${getCurrentGeminiMdFilename()}: ${fullPath}`, ); } catch { if (debugMode) logger.debug( - `Downward ${GEMINI_MD_FILENAME} not readable, skipping: ${fullPath}`, + `Downward ${getCurrentGeminiMdFilename()} not readable, skipping: ${fullPath}`, ); } } @@ -139,7 +145,7 @@ async function collectDownwardGeminiFiles( async function getGeminiMdFilePathsInternal( currentWorkingDirectory: string, - userHomePath: string, // Keep userHomePath as a parameter for clarity + userHomePath: string, debugMode: boolean, ): Promise<string[]> { const resolvedCwd = path.resolve(currentWorkingDirectory); @@ -147,13 +153,13 @@ async function getGeminiMdFilePathsInternal( const globalMemoryPath = path.join( resolvedHome, GEMINI_CONFIG_DIR, - GEMINI_MD_FILENAME, + getCurrentGeminiMdFilename(), ); const paths: string[] = []; if (debugMode) logger.debug( - `Searching for ${GEMINI_MD_FILENAME} starting from CWD: ${resolvedCwd}`, + `Searching for ${getCurrentGeminiMdFilename()} starting from CWD: ${resolvedCwd}`, ); if (debugMode) logger.debug(`User home directory: ${resolvedHome}`); @@ -162,12 +168,12 @@ async function getGeminiMdFilePathsInternal( paths.push(globalMemoryPath); if (debugMode) logger.debug( - `Found readable global ${GEMINI_MD_FILENAME}: ${globalMemoryPath}`, + `Found readable global ${getCurrentGeminiMdFilename()}: ${globalMemoryPath}`, ); } catch { if (debugMode) logger.debug( - `Global ${GEMINI_MD_FILENAME} not found or not readable: ${globalMemoryPath}`, + `Global ${getCurrentGeminiMdFilename()} not found or not readable: ${globalMemoryPath}`, ); } @@ -186,7 +192,7 @@ async function getGeminiMdFilePathsInternal( // Loop until filesystem root or currentDir is empty if (debugMode) { logger.debug( - `Checking for ${GEMINI_MD_FILENAME} in (upward scan): ${currentDir}`, + `Checking for ${getCurrentGeminiMdFilename()} in (upward scan): ${currentDir}`, ); } @@ -201,7 +207,7 @@ async function getGeminiMdFilePathsInternal( break; } - const potentialPath = path.join(currentDir, GEMINI_MD_FILENAME); + const potentialPath = path.join(currentDir, getCurrentGeminiMdFilename()); try { await fs.access(potentialPath, fsSync.constants.R_OK); // Add to upwardPaths only if it's not the already added globalMemoryPath @@ -209,14 +215,14 @@ async function getGeminiMdFilePathsInternal( upwardPaths.unshift(potentialPath); if (debugMode) { logger.debug( - `Found readable upward ${GEMINI_MD_FILENAME}: ${potentialPath}`, + `Found readable upward ${getCurrentGeminiMdFilename()}: ${potentialPath}`, ); } } } catch { if (debugMode) { logger.debug( - `Upward ${GEMINI_MD_FILENAME} not found or not readable in: ${currentDir}`, + `Upward ${getCurrentGeminiMdFilename()} not found or not readable in: ${currentDir}`, ); } } @@ -247,7 +253,7 @@ async function getGeminiMdFilePathsInternal( downwardPaths.sort(); // Sort for consistent ordering, though hierarchy might be more complex if (debugMode && downwardPaths.length > 0) logger.debug( - `Found downward ${GEMINI_MD_FILENAME} files (sorted): ${JSON.stringify(downwardPaths)}`, + `Found downward ${getCurrentGeminiMdFilename()} files (sorted): ${JSON.stringify(downwardPaths)}`, ); // Add downward paths only if they haven't been included already (e.g. from upward scan) for (const dPath of downwardPaths) { @@ -258,7 +264,7 @@ async function getGeminiMdFilePathsInternal( if (debugMode) logger.debug( - `Final ordered ${GEMINI_MD_FILENAME} paths to read: ${JSON.stringify(paths)}`, + `Final ordered ${getCurrentGeminiMdFilename()} paths to read: ${JSON.stringify(paths)}`, ); return paths; } @@ -279,7 +285,7 @@ async function readGeminiMdFiles( } catch (error: unknown) { const message = error instanceof Error ? error.message : String(error); logger.warn( - `Warning: Could not read ${GEMINI_MD_FILENAME} file at ${filePath}. Error: ${message}`, + `Warning: Could not read ${getCurrentGeminiMdFilename()} file at ${filePath}. Error: ${message}`, ); results.push({ filePath, content: null }); // Still include it with null content if (debugMode) logger.debug(`Failed to read: ${filePath}`); |
