diff options
| author | Allen Hutchison <[email protected]> | 2025-05-31 12:49:28 -0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-05-31 12:49:28 -0700 |
| commit | 53bf77849760593cc9c1af9a4fb110a1a74acc4f (patch) | |
| tree | c188b0fa9c295faa198b65d80143491858c18048 /packages/core/src | |
| parent | cbc1614b8441dc3dcf35cf4f2e6b3c3457045fcb (diff) | |
feat: allow custom filename for context files (#654)
Co-authored-by: N. Taylor Mullen <[email protected]>
Diffstat (limited to 'packages/core/src')
| -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 |
7 files changed, 377 insertions, 94 deletions
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}`); |
