mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-04-20 10:10:56 -07:00
feat(core): Render memory hierarchically in context. (#18350)
This commit is contained in:
@@ -10,8 +10,9 @@ import * as os from 'node:os';
|
||||
import * as path from 'node:path';
|
||||
import {
|
||||
loadServerHierarchicalMemory,
|
||||
loadGlobalMemory,
|
||||
loadEnvironmentMemory,
|
||||
getGlobalMemoryPaths,
|
||||
getExtensionMemoryPaths,
|
||||
getEnvironmentMemoryPaths,
|
||||
loadJitSubdirectoryMemory,
|
||||
refreshServerHierarchicalMemory,
|
||||
} from './memoryDiscovery.js';
|
||||
@@ -19,8 +20,22 @@ import {
|
||||
setGeminiMdFilename,
|
||||
DEFAULT_CONTEXT_FILENAME,
|
||||
} from '../tools/memoryTool.js';
|
||||
import { flattenMemory } from '../config/memory.js';
|
||||
import { FileDiscoveryService } from '../services/fileDiscoveryService.js';
|
||||
import { GEMINI_DIR } from './paths.js';
|
||||
import { GEMINI_DIR, normalizePath } from './paths.js';
|
||||
import type { HierarchicalMemory } from '../config/memory.js';
|
||||
|
||||
function flattenResult(result: {
|
||||
memoryContent: HierarchicalMemory;
|
||||
fileCount: number;
|
||||
filePaths: string[];
|
||||
}) {
|
||||
return {
|
||||
...result,
|
||||
memoryContent: flattenMemory(result.memoryContent),
|
||||
filePaths: result.filePaths.map((p) => normalizePath(p)),
|
||||
};
|
||||
}
|
||||
import { Config, type GeminiCLIExtension } from '../config/config.js';
|
||||
import { Storage } from '../config/storage.js';
|
||||
import { SimpleExtensionLoader } from './extensionLoader.js';
|
||||
@@ -39,6 +54,10 @@ vi.mock('../utils/paths.js', async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import('../utils/paths.js')>();
|
||||
return {
|
||||
...actual,
|
||||
normalizePath: (p: string) => {
|
||||
const resolved = path.resolve(p);
|
||||
return process.platform === 'win32' ? resolved.toLowerCase() : resolved;
|
||||
},
|
||||
homedir: vi.fn(),
|
||||
};
|
||||
});
|
||||
@@ -54,18 +73,20 @@ describe('memoryDiscovery', () => {
|
||||
|
||||
async function createEmptyDir(fullPath: string) {
|
||||
await fsPromises.mkdir(fullPath, { recursive: true });
|
||||
return fullPath;
|
||||
return normalizePath(fullPath);
|
||||
}
|
||||
|
||||
async function createTestFile(fullPath: string, fileContents: string) {
|
||||
await fsPromises.mkdir(path.dirname(fullPath), { recursive: true });
|
||||
await fsPromises.writeFile(fullPath, fileContents);
|
||||
return path.resolve(testRootDir, fullPath);
|
||||
return normalizePath(path.resolve(testRootDir, fullPath));
|
||||
}
|
||||
|
||||
beforeEach(async () => {
|
||||
testRootDir = await fsPromises.mkdtemp(
|
||||
path.join(os.tmpdir(), 'folder-structure-test-'),
|
||||
testRootDir = normalizePath(
|
||||
await fsPromises.mkdtemp(
|
||||
path.join(os.tmpdir(), 'folder-structure-test-'),
|
||||
),
|
||||
);
|
||||
|
||||
vi.resetAllMocks();
|
||||
@@ -80,6 +101,9 @@ describe('memoryDiscovery', () => {
|
||||
vi.mocked(pathsHomedir).mockReturnValue(homedir);
|
||||
});
|
||||
|
||||
const normMarker = (p: string) =>
|
||||
process.platform === 'win32' ? p.toLowerCase() : p;
|
||||
|
||||
afterEach(async () => {
|
||||
vi.unstubAllEnvs();
|
||||
// Some tests set this to a different value.
|
||||
@@ -104,13 +128,15 @@ describe('memoryDiscovery', () => {
|
||||
path.join(cwd, DEFAULT_CONTEXT_FILENAME),
|
||||
'Src directory memory',
|
||||
);
|
||||
const result = await loadServerHierarchicalMemory(
|
||||
cwd,
|
||||
[],
|
||||
false,
|
||||
new FileDiscoveryService(projectRoot),
|
||||
new SimpleExtensionLoader([]),
|
||||
false, // untrusted
|
||||
const result = flattenResult(
|
||||
await loadServerHierarchicalMemory(
|
||||
cwd,
|
||||
[],
|
||||
false,
|
||||
new FileDiscoveryService(projectRoot),
|
||||
new SimpleExtensionLoader([]),
|
||||
false, // untrusted
|
||||
),
|
||||
);
|
||||
|
||||
expect(result).toEqual({
|
||||
@@ -130,9 +156,16 @@ describe('memoryDiscovery', () => {
|
||||
'Src directory memory', // Untrusted
|
||||
);
|
||||
|
||||
const filepath = path.join(homedir, GEMINI_DIR, DEFAULT_CONTEXT_FILENAME);
|
||||
await createTestFile(filepath, 'default context content'); // In user home dir (outside untrusted space).
|
||||
const { fileCount, memoryContent, filePaths } =
|
||||
const filepathInput = path.join(
|
||||
homedir,
|
||||
GEMINI_DIR,
|
||||
DEFAULT_CONTEXT_FILENAME,
|
||||
);
|
||||
const filepath = await createTestFile(
|
||||
filepathInput,
|
||||
'default context content',
|
||||
); // In user home dir (outside untrusted space).
|
||||
const { fileCount, memoryContent, filePaths } = flattenResult(
|
||||
await loadServerHierarchicalMemory(
|
||||
cwd,
|
||||
[],
|
||||
@@ -140,7 +173,8 @@ describe('memoryDiscovery', () => {
|
||||
new FileDiscoveryService(projectRoot),
|
||||
new SimpleExtensionLoader([]),
|
||||
false, // untrusted
|
||||
);
|
||||
),
|
||||
);
|
||||
|
||||
expect(fileCount).toEqual(1);
|
||||
expect(memoryContent).toContain(path.relative(cwd, filepath).toString());
|
||||
@@ -149,13 +183,15 @@ describe('memoryDiscovery', () => {
|
||||
});
|
||||
|
||||
it('should return empty memory and count if no context files are found', async () => {
|
||||
const result = await loadServerHierarchicalMemory(
|
||||
cwd,
|
||||
[],
|
||||
false,
|
||||
new FileDiscoveryService(projectRoot),
|
||||
new SimpleExtensionLoader([]),
|
||||
DEFAULT_FOLDER_TRUST,
|
||||
const result = flattenResult(
|
||||
await loadServerHierarchicalMemory(
|
||||
cwd,
|
||||
[],
|
||||
false,
|
||||
new FileDiscoveryService(projectRoot),
|
||||
new SimpleExtensionLoader([]),
|
||||
DEFAULT_FOLDER_TRUST,
|
||||
),
|
||||
);
|
||||
|
||||
expect(result).toEqual({
|
||||
@@ -171,17 +207,23 @@ describe('memoryDiscovery', () => {
|
||||
'default context content',
|
||||
);
|
||||
|
||||
const result = await loadServerHierarchicalMemory(
|
||||
cwd,
|
||||
[],
|
||||
false,
|
||||
new FileDiscoveryService(projectRoot),
|
||||
new SimpleExtensionLoader([]),
|
||||
DEFAULT_FOLDER_TRUST,
|
||||
const result = flattenResult(
|
||||
await loadServerHierarchicalMemory(
|
||||
cwd,
|
||||
[],
|
||||
false,
|
||||
new FileDiscoveryService(projectRoot),
|
||||
new SimpleExtensionLoader([]),
|
||||
DEFAULT_FOLDER_TRUST,
|
||||
),
|
||||
);
|
||||
|
||||
expect(result).toEqual({
|
||||
memoryContent: `--- Context from: ${path.relative(cwd, defaultContextFile)} ---
|
||||
expect({
|
||||
...result,
|
||||
memoryContent: flattenMemory(result.memoryContent),
|
||||
}).toEqual({
|
||||
memoryContent: `--- Global ---
|
||||
--- Context from: ${path.relative(cwd, defaultContextFile)} ---
|
||||
default context content
|
||||
--- End of Context from: ${path.relative(cwd, defaultContextFile)} ---`,
|
||||
fileCount: 1,
|
||||
@@ -198,19 +240,22 @@ default context content
|
||||
'custom context content',
|
||||
);
|
||||
|
||||
const result = await loadServerHierarchicalMemory(
|
||||
cwd,
|
||||
[],
|
||||
false,
|
||||
new FileDiscoveryService(projectRoot),
|
||||
new SimpleExtensionLoader([]),
|
||||
DEFAULT_FOLDER_TRUST,
|
||||
const result = flattenResult(
|
||||
await loadServerHierarchicalMemory(
|
||||
cwd,
|
||||
[],
|
||||
false,
|
||||
new FileDiscoveryService(projectRoot),
|
||||
new SimpleExtensionLoader([]),
|
||||
DEFAULT_FOLDER_TRUST,
|
||||
),
|
||||
);
|
||||
|
||||
expect(result).toEqual({
|
||||
memoryContent: `--- Context from: ${path.relative(cwd, customContextFile)} ---
|
||||
memoryContent: `--- Global ---
|
||||
--- Context from: ${normMarker(path.relative(cwd, customContextFile))} ---
|
||||
custom context content
|
||||
--- End of Context from: ${path.relative(cwd, customContextFile)} ---`,
|
||||
--- End of Context from: ${normMarker(path.relative(cwd, customContextFile))} ---`,
|
||||
fileCount: 1,
|
||||
filePaths: [customContextFile],
|
||||
});
|
||||
@@ -229,23 +274,26 @@ custom context content
|
||||
'cwd context content',
|
||||
);
|
||||
|
||||
const result = await loadServerHierarchicalMemory(
|
||||
cwd,
|
||||
[],
|
||||
false,
|
||||
new FileDiscoveryService(projectRoot),
|
||||
new SimpleExtensionLoader([]),
|
||||
DEFAULT_FOLDER_TRUST,
|
||||
const result = flattenResult(
|
||||
await loadServerHierarchicalMemory(
|
||||
cwd,
|
||||
[],
|
||||
false,
|
||||
new FileDiscoveryService(projectRoot),
|
||||
new SimpleExtensionLoader([]),
|
||||
DEFAULT_FOLDER_TRUST,
|
||||
),
|
||||
);
|
||||
|
||||
expect(result).toEqual({
|
||||
memoryContent: `--- Context from: ${path.relative(cwd, projectContextFile)} ---
|
||||
memoryContent: `--- Project ---
|
||||
--- Context from: ${normMarker(path.relative(cwd, projectContextFile))} ---
|
||||
project context content
|
||||
--- End of Context from: ${path.relative(cwd, projectContextFile)} ---
|
||||
--- End of Context from: ${normMarker(path.relative(cwd, projectContextFile))} ---
|
||||
|
||||
--- Context from: ${path.relative(cwd, cwdContextFile)} ---
|
||||
--- Context from: ${normMarker(path.relative(cwd, cwdContextFile))} ---
|
||||
cwd context content
|
||||
--- End of Context from: ${path.relative(cwd, cwdContextFile)} ---`,
|
||||
--- End of Context from: ${normMarker(path.relative(cwd, cwdContextFile))} ---`,
|
||||
fileCount: 2,
|
||||
filePaths: [projectContextFile, cwdContextFile],
|
||||
});
|
||||
@@ -264,23 +312,26 @@ cwd context content
|
||||
'CWD custom memory',
|
||||
);
|
||||
|
||||
const result = await loadServerHierarchicalMemory(
|
||||
cwd,
|
||||
[],
|
||||
false,
|
||||
new FileDiscoveryService(projectRoot),
|
||||
new SimpleExtensionLoader([]),
|
||||
DEFAULT_FOLDER_TRUST,
|
||||
const result = flattenResult(
|
||||
await loadServerHierarchicalMemory(
|
||||
cwd,
|
||||
[],
|
||||
false,
|
||||
new FileDiscoveryService(projectRoot),
|
||||
new SimpleExtensionLoader([]),
|
||||
DEFAULT_FOLDER_TRUST,
|
||||
),
|
||||
);
|
||||
|
||||
expect(result).toEqual({
|
||||
memoryContent: `--- Context from: ${customFilename} ---
|
||||
memoryContent: `--- Project ---
|
||||
--- Context from: ${normMarker(customFilename)} ---
|
||||
CWD custom memory
|
||||
--- End of Context from: ${customFilename} ---
|
||||
--- End of Context from: ${normMarker(customFilename)} ---
|
||||
|
||||
--- Context from: ${path.join('subdir', customFilename)} ---
|
||||
--- Context from: ${normMarker(path.join('subdir', customFilename))} ---
|
||||
Subdir custom memory
|
||||
--- End of Context from: ${path.join('subdir', customFilename)} ---`,
|
||||
--- End of Context from: ${normMarker(path.join('subdir', customFilename))} ---`,
|
||||
fileCount: 2,
|
||||
filePaths: [cwdCustomFile, subdirCustomFile],
|
||||
});
|
||||
@@ -296,23 +347,26 @@ Subdir custom memory
|
||||
'Src directory memory',
|
||||
);
|
||||
|
||||
const result = await loadServerHierarchicalMemory(
|
||||
cwd,
|
||||
[],
|
||||
false,
|
||||
new FileDiscoveryService(projectRoot),
|
||||
new SimpleExtensionLoader([]),
|
||||
DEFAULT_FOLDER_TRUST,
|
||||
const result = flattenResult(
|
||||
await loadServerHierarchicalMemory(
|
||||
cwd,
|
||||
[],
|
||||
false,
|
||||
new FileDiscoveryService(projectRoot),
|
||||
new SimpleExtensionLoader([]),
|
||||
DEFAULT_FOLDER_TRUST,
|
||||
),
|
||||
);
|
||||
|
||||
expect(result).toEqual({
|
||||
memoryContent: `--- Context from: ${path.relative(cwd, projectRootGeminiFile)} ---
|
||||
memoryContent: `--- Project ---
|
||||
--- Context from: ${normMarker(path.relative(cwd, projectRootGeminiFile))} ---
|
||||
Project root memory
|
||||
--- End of Context from: ${path.relative(cwd, projectRootGeminiFile)} ---
|
||||
--- End of Context from: ${normMarker(path.relative(cwd, projectRootGeminiFile))} ---
|
||||
|
||||
--- Context from: ${path.relative(cwd, srcGeminiFile)} ---
|
||||
--- Context from: ${normMarker(path.relative(cwd, srcGeminiFile))} ---
|
||||
Src directory memory
|
||||
--- End of Context from: ${path.relative(cwd, srcGeminiFile)} ---`,
|
||||
--- End of Context from: ${normMarker(path.relative(cwd, srcGeminiFile))} ---`,
|
||||
fileCount: 2,
|
||||
filePaths: [projectRootGeminiFile, srcGeminiFile],
|
||||
});
|
||||
@@ -328,23 +382,26 @@ Src directory memory
|
||||
'CWD memory',
|
||||
);
|
||||
|
||||
const result = await loadServerHierarchicalMemory(
|
||||
cwd,
|
||||
[],
|
||||
false,
|
||||
new FileDiscoveryService(projectRoot),
|
||||
new SimpleExtensionLoader([]),
|
||||
DEFAULT_FOLDER_TRUST,
|
||||
const result = flattenResult(
|
||||
await loadServerHierarchicalMemory(
|
||||
cwd,
|
||||
[],
|
||||
false,
|
||||
new FileDiscoveryService(projectRoot),
|
||||
new SimpleExtensionLoader([]),
|
||||
DEFAULT_FOLDER_TRUST,
|
||||
),
|
||||
);
|
||||
|
||||
expect(result).toEqual({
|
||||
memoryContent: `--- Context from: ${DEFAULT_CONTEXT_FILENAME} ---
|
||||
memoryContent: `--- Project ---
|
||||
--- Context from: ${normMarker(DEFAULT_CONTEXT_FILENAME)} ---
|
||||
CWD memory
|
||||
--- End of Context from: ${DEFAULT_CONTEXT_FILENAME} ---
|
||||
--- End of Context from: ${normMarker(DEFAULT_CONTEXT_FILENAME)} ---
|
||||
|
||||
--- Context from: ${path.join('subdir', DEFAULT_CONTEXT_FILENAME)} ---
|
||||
--- Context from: ${normMarker(path.join('subdir', DEFAULT_CONTEXT_FILENAME))} ---
|
||||
Subdir memory
|
||||
--- End of Context from: ${path.join('subdir', DEFAULT_CONTEXT_FILENAME)} ---`,
|
||||
--- End of Context from: ${normMarker(path.join('subdir', DEFAULT_CONTEXT_FILENAME))} ---`,
|
||||
fileCount: 2,
|
||||
filePaths: [cwdGeminiFile, subDirGeminiFile],
|
||||
});
|
||||
@@ -372,35 +429,39 @@ Subdir memory
|
||||
'Subdir memory',
|
||||
);
|
||||
|
||||
const result = await loadServerHierarchicalMemory(
|
||||
cwd,
|
||||
[],
|
||||
false,
|
||||
new FileDiscoveryService(projectRoot),
|
||||
new SimpleExtensionLoader([]),
|
||||
DEFAULT_FOLDER_TRUST,
|
||||
const result = flattenResult(
|
||||
await loadServerHierarchicalMemory(
|
||||
cwd,
|
||||
[],
|
||||
false,
|
||||
new FileDiscoveryService(projectRoot),
|
||||
new SimpleExtensionLoader([]),
|
||||
DEFAULT_FOLDER_TRUST,
|
||||
),
|
||||
);
|
||||
|
||||
expect(result).toEqual({
|
||||
memoryContent: `--- Context from: ${path.relative(cwd, defaultContextFile)} ---
|
||||
memoryContent: `--- Global ---
|
||||
--- Context from: ${normMarker(path.relative(cwd, defaultContextFile))} ---
|
||||
default context content
|
||||
--- End of Context from: ${path.relative(cwd, defaultContextFile)} ---
|
||||
--- End of Context from: ${normMarker(path.relative(cwd, defaultContextFile))} ---
|
||||
|
||||
--- Context from: ${path.relative(cwd, rootGeminiFile)} ---
|
||||
--- Project ---
|
||||
--- Context from: ${normMarker(path.relative(cwd, rootGeminiFile))} ---
|
||||
Project parent memory
|
||||
--- End of Context from: ${path.relative(cwd, rootGeminiFile)} ---
|
||||
--- End of Context from: ${normMarker(path.relative(cwd, rootGeminiFile))} ---
|
||||
|
||||
--- Context from: ${path.relative(cwd, projectRootGeminiFile)} ---
|
||||
--- Context from: ${normMarker(path.relative(cwd, projectRootGeminiFile))} ---
|
||||
Project root memory
|
||||
--- End of Context from: ${path.relative(cwd, projectRootGeminiFile)} ---
|
||||
--- End of Context from: ${normMarker(path.relative(cwd, projectRootGeminiFile))} ---
|
||||
|
||||
--- Context from: ${path.relative(cwd, cwdGeminiFile)} ---
|
||||
--- Context from: ${normMarker(path.relative(cwd, cwdGeminiFile))} ---
|
||||
CWD memory
|
||||
--- End of Context from: ${path.relative(cwd, cwdGeminiFile)} ---
|
||||
--- End of Context from: ${normMarker(path.relative(cwd, cwdGeminiFile))} ---
|
||||
|
||||
--- Context from: ${path.relative(cwd, subDirGeminiFile)} ---
|
||||
--- Context from: ${normMarker(path.relative(cwd, subDirGeminiFile))} ---
|
||||
Subdir memory
|
||||
--- End of Context from: ${path.relative(cwd, subDirGeminiFile)} ---`,
|
||||
--- End of Context from: ${normMarker(path.relative(cwd, subDirGeminiFile))} ---`,
|
||||
fileCount: 5,
|
||||
filePaths: [
|
||||
defaultContextFile,
|
||||
@@ -425,26 +486,29 @@ Subdir memory
|
||||
'My code memory',
|
||||
);
|
||||
|
||||
const result = await loadServerHierarchicalMemory(
|
||||
cwd,
|
||||
[],
|
||||
false,
|
||||
new FileDiscoveryService(projectRoot),
|
||||
new SimpleExtensionLoader([]),
|
||||
DEFAULT_FOLDER_TRUST,
|
||||
'tree',
|
||||
{
|
||||
respectGitIgnore: true,
|
||||
respectGeminiIgnore: true,
|
||||
customIgnoreFilePaths: [],
|
||||
},
|
||||
200, // maxDirs parameter
|
||||
const result = flattenResult(
|
||||
await loadServerHierarchicalMemory(
|
||||
cwd,
|
||||
[],
|
||||
false,
|
||||
new FileDiscoveryService(projectRoot),
|
||||
new SimpleExtensionLoader([]),
|
||||
DEFAULT_FOLDER_TRUST,
|
||||
'tree',
|
||||
{
|
||||
respectGitIgnore: true,
|
||||
respectGeminiIgnore: true,
|
||||
customIgnoreFilePaths: [],
|
||||
},
|
||||
200, // maxDirs parameter
|
||||
),
|
||||
);
|
||||
|
||||
expect(result).toEqual({
|
||||
memoryContent: `--- Context from: ${path.relative(cwd, regularSubDirGeminiFile)} ---
|
||||
memoryContent: `--- Project ---
|
||||
--- Context from: ${normMarker(path.relative(cwd, regularSubDirGeminiFile))} ---
|
||||
My code memory
|
||||
--- End of Context from: ${path.relative(cwd, regularSubDirGeminiFile)} ---`,
|
||||
--- End of Context from: ${normMarker(path.relative(cwd, regularSubDirGeminiFile))} ---`,
|
||||
fileCount: 1,
|
||||
filePaths: [regularSubDirGeminiFile],
|
||||
});
|
||||
@@ -485,13 +549,15 @@ My code memory
|
||||
|
||||
consoleDebugSpy.mockRestore();
|
||||
|
||||
const result = await loadServerHierarchicalMemory(
|
||||
cwd,
|
||||
[],
|
||||
false,
|
||||
new FileDiscoveryService(projectRoot),
|
||||
new SimpleExtensionLoader([]),
|
||||
DEFAULT_FOLDER_TRUST,
|
||||
const result = flattenResult(
|
||||
await loadServerHierarchicalMemory(
|
||||
cwd,
|
||||
[],
|
||||
false,
|
||||
new FileDiscoveryService(projectRoot),
|
||||
new SimpleExtensionLoader([]),
|
||||
DEFAULT_FOLDER_TRUST,
|
||||
),
|
||||
);
|
||||
|
||||
expect(result).toEqual({
|
||||
@@ -507,24 +573,27 @@ My code memory
|
||||
'Extension memory content',
|
||||
);
|
||||
|
||||
const result = await loadServerHierarchicalMemory(
|
||||
cwd,
|
||||
[],
|
||||
false,
|
||||
new FileDiscoveryService(projectRoot),
|
||||
new SimpleExtensionLoader([
|
||||
{
|
||||
contextFiles: [extensionFilePath],
|
||||
isActive: true,
|
||||
} as GeminiCLIExtension,
|
||||
]),
|
||||
DEFAULT_FOLDER_TRUST,
|
||||
const result = flattenResult(
|
||||
await loadServerHierarchicalMemory(
|
||||
cwd,
|
||||
[],
|
||||
false,
|
||||
new FileDiscoveryService(projectRoot),
|
||||
new SimpleExtensionLoader([
|
||||
{
|
||||
contextFiles: [extensionFilePath],
|
||||
isActive: true,
|
||||
} as GeminiCLIExtension,
|
||||
]),
|
||||
DEFAULT_FOLDER_TRUST,
|
||||
),
|
||||
);
|
||||
|
||||
expect(result).toEqual({
|
||||
memoryContent: `--- Context from: ${path.relative(cwd, extensionFilePath)} ---
|
||||
memoryContent: `--- Extension ---
|
||||
--- Context from: ${normMarker(path.relative(cwd, extensionFilePath))} ---
|
||||
Extension memory content
|
||||
--- End of Context from: ${path.relative(cwd, extensionFilePath)} ---`,
|
||||
--- End of Context from: ${normMarker(path.relative(cwd, extensionFilePath))} ---`,
|
||||
fileCount: 1,
|
||||
filePaths: [extensionFilePath],
|
||||
});
|
||||
@@ -539,19 +608,22 @@ Extension memory content
|
||||
'included directory memory',
|
||||
);
|
||||
|
||||
const result = await loadServerHierarchicalMemory(
|
||||
cwd,
|
||||
[includedDir],
|
||||
false,
|
||||
new FileDiscoveryService(projectRoot),
|
||||
new SimpleExtensionLoader([]),
|
||||
DEFAULT_FOLDER_TRUST,
|
||||
const result = flattenResult(
|
||||
await loadServerHierarchicalMemory(
|
||||
cwd,
|
||||
[includedDir],
|
||||
false,
|
||||
new FileDiscoveryService(projectRoot),
|
||||
new SimpleExtensionLoader([]),
|
||||
DEFAULT_FOLDER_TRUST,
|
||||
),
|
||||
);
|
||||
|
||||
expect(result).toEqual({
|
||||
memoryContent: `--- Context from: ${path.relative(cwd, includedFile)} ---
|
||||
memoryContent: `--- Project ---
|
||||
--- Context from: ${normMarker(path.relative(cwd, includedFile))} ---
|
||||
included directory memory
|
||||
--- End of Context from: ${path.relative(cwd, includedFile)} ---`,
|
||||
--- End of Context from: ${normMarker(path.relative(cwd, includedFile))} ---`,
|
||||
fileCount: 1,
|
||||
filePaths: [includedFile],
|
||||
});
|
||||
@@ -574,13 +646,15 @@ included directory memory
|
||||
}
|
||||
|
||||
// Load memory from all directories
|
||||
const result = await loadServerHierarchicalMemory(
|
||||
cwd,
|
||||
createdFiles.map((f) => path.dirname(f)),
|
||||
false,
|
||||
new FileDiscoveryService(projectRoot),
|
||||
new SimpleExtensionLoader([]),
|
||||
DEFAULT_FOLDER_TRUST,
|
||||
const result = flattenResult(
|
||||
await loadServerHierarchicalMemory(
|
||||
cwd,
|
||||
createdFiles.map((f) => path.dirname(f)),
|
||||
false,
|
||||
new FileDiscoveryService(projectRoot),
|
||||
new SimpleExtensionLoader([]),
|
||||
DEFAULT_FOLDER_TRUST,
|
||||
),
|
||||
);
|
||||
|
||||
// Should have loaded all files
|
||||
@@ -589,8 +663,9 @@ included directory memory
|
||||
expect(result.filePaths.sort()).toEqual(createdFiles.sort());
|
||||
|
||||
// Content should include all project contents
|
||||
const flattenedMemory = flattenMemory(result.memoryContent);
|
||||
for (let i = 0; i < numDirs; i++) {
|
||||
expect(result.memoryContent).toContain(`Content from project ${i}`);
|
||||
expect(flattenedMemory).toContain(`Content from project ${i}`);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -609,73 +684,91 @@ included directory memory
|
||||
);
|
||||
|
||||
// Include both parent and child directories
|
||||
const result = await loadServerHierarchicalMemory(
|
||||
parentDir,
|
||||
[childDir, parentDir], // Deliberately include duplicates
|
||||
false,
|
||||
new FileDiscoveryService(projectRoot),
|
||||
new SimpleExtensionLoader([]),
|
||||
DEFAULT_FOLDER_TRUST,
|
||||
const result = flattenResult(
|
||||
await loadServerHierarchicalMemory(
|
||||
parentDir,
|
||||
[childDir, parentDir], // Deliberately include duplicates
|
||||
false,
|
||||
new FileDiscoveryService(projectRoot),
|
||||
new SimpleExtensionLoader([]),
|
||||
DEFAULT_FOLDER_TRUST,
|
||||
),
|
||||
);
|
||||
|
||||
// Should have both files without duplicates
|
||||
const flattenedMemory = flattenMemory(result.memoryContent);
|
||||
expect(result.fileCount).toBe(2);
|
||||
expect(result.memoryContent).toContain('Parent content');
|
||||
expect(result.memoryContent).toContain('Child content');
|
||||
expect(flattenedMemory).toContain('Parent content');
|
||||
expect(flattenedMemory).toContain('Child content');
|
||||
expect(result.filePaths.sort()).toEqual([parentFile, childFile].sort());
|
||||
|
||||
// Check that files are not duplicated
|
||||
const parentOccurrences = (
|
||||
result.memoryContent.match(/Parent content/g) || []
|
||||
).length;
|
||||
const childOccurrences = (
|
||||
result.memoryContent.match(/Child content/g) || []
|
||||
).length;
|
||||
const parentOccurrences = (flattenedMemory.match(/Parent content/g) || [])
|
||||
.length;
|
||||
const childOccurrences = (flattenedMemory.match(/Child content/g) || [])
|
||||
.length;
|
||||
expect(parentOccurrences).toBe(1);
|
||||
expect(childOccurrences).toBe(1);
|
||||
});
|
||||
|
||||
describe('loadGlobalMemory', () => {
|
||||
it('should load global memory file if it exists', async () => {
|
||||
describe('getGlobalMemoryPaths', () => {
|
||||
it('should find global memory file if it exists', async () => {
|
||||
const globalMemoryFile = await createTestFile(
|
||||
path.join(homedir, GEMINI_DIR, DEFAULT_CONTEXT_FILENAME),
|
||||
'Global memory content',
|
||||
);
|
||||
|
||||
const result = await loadGlobalMemory();
|
||||
const result = await getGlobalMemoryPaths();
|
||||
|
||||
expect(result.files).toHaveLength(1);
|
||||
expect(result.files[0].path).toBe(globalMemoryFile);
|
||||
expect(result.files[0].content).toBe('Global memory content');
|
||||
expect(result).toHaveLength(1);
|
||||
expect(result[0]).toBe(globalMemoryFile);
|
||||
});
|
||||
|
||||
it('should return empty content if global memory file does not exist', async () => {
|
||||
const result = await loadGlobalMemory();
|
||||
it('should return empty array if global memory file does not exist', async () => {
|
||||
const result = await getGlobalMemoryPaths();
|
||||
|
||||
expect(result.files).toHaveLength(0);
|
||||
expect(result).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('loadEnvironmentMemory', () => {
|
||||
it('should load extension memory', async () => {
|
||||
describe('getExtensionMemoryPaths', () => {
|
||||
it('should return active extension context files', async () => {
|
||||
const extFile = await createTestFile(
|
||||
path.join(testRootDir, 'ext', 'GEMINI.md'),
|
||||
'Extension content',
|
||||
);
|
||||
const mockExtensionLoader = new SimpleExtensionLoader([
|
||||
const loader = new SimpleExtensionLoader([
|
||||
{
|
||||
isActive: true,
|
||||
contextFiles: [extFile],
|
||||
} as GeminiCLIExtension,
|
||||
]);
|
||||
|
||||
const result = await loadEnvironmentMemory([], mockExtensionLoader);
|
||||
const result = getExtensionMemoryPaths(loader);
|
||||
|
||||
expect(result.files).toHaveLength(1);
|
||||
expect(result.files[0].path).toBe(extFile);
|
||||
expect(result.files[0].content).toBe('Extension content');
|
||||
expect(result).toHaveLength(1);
|
||||
expect(result[0]).toBe(extFile);
|
||||
});
|
||||
|
||||
it('should ignore inactive extensions', async () => {
|
||||
const extFile = await createTestFile(
|
||||
path.join(testRootDir, 'ext', 'GEMINI.md'),
|
||||
'Extension content',
|
||||
);
|
||||
const loader = new SimpleExtensionLoader([
|
||||
{
|
||||
isActive: false,
|
||||
contextFiles: [extFile],
|
||||
} as GeminiCLIExtension,
|
||||
]);
|
||||
|
||||
const result = getExtensionMemoryPaths(loader);
|
||||
|
||||
expect(result).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getEnvironmentMemoryPaths', () => {
|
||||
it('should NOT traverse upward beyond trusted root (even with .git)', async () => {
|
||||
// Setup: /temp/parent/repo/.git
|
||||
const parentDir = await createEmptyDir(path.join(testRootDir, 'parent'));
|
||||
@@ -698,14 +791,10 @@ included directory memory
|
||||
|
||||
// Trust srcDir. Should ONLY load srcFile.
|
||||
// Repo and Parent are NOT trusted.
|
||||
const result = await loadEnvironmentMemory(
|
||||
[srcDir],
|
||||
new SimpleExtensionLoader([]),
|
||||
);
|
||||
const result = await getEnvironmentMemoryPaths([srcDir]);
|
||||
|
||||
expect(result.files).toHaveLength(1);
|
||||
expect(result.files[0].path).toBe(srcFile);
|
||||
expect(result.files[0].content).toBe('Src content');
|
||||
expect(result).toHaveLength(1);
|
||||
expect(result[0]).toBe(srcFile);
|
||||
});
|
||||
|
||||
it('should NOT traverse upward beyond trusted root (no .git)', async () => {
|
||||
@@ -724,20 +813,13 @@ included directory memory
|
||||
|
||||
// Trust notesDir. Should load NOTHING because notesDir has no file,
|
||||
// and we do not traverse up to docsDir.
|
||||
const resultNotes = await loadEnvironmentMemory(
|
||||
[notesDir],
|
||||
new SimpleExtensionLoader([]),
|
||||
);
|
||||
expect(resultNotes.files).toHaveLength(0);
|
||||
const resultNotes = await getEnvironmentMemoryPaths([notesDir]);
|
||||
expect(resultNotes).toHaveLength(0);
|
||||
|
||||
// Trust docsDir. Should load docsFile, but NOT homeFile.
|
||||
const resultDocs = await loadEnvironmentMemory(
|
||||
[docsDir],
|
||||
new SimpleExtensionLoader([]),
|
||||
);
|
||||
expect(resultDocs.files).toHaveLength(1);
|
||||
expect(resultDocs.files[0].path).toBe(docsFile);
|
||||
expect(resultDocs.files[0].content).toBe('Docs content');
|
||||
const resultDocs = await getEnvironmentMemoryPaths([docsDir]);
|
||||
expect(resultDocs).toHaveLength(1);
|
||||
expect(resultDocs[0]).toBe(docsFile);
|
||||
});
|
||||
|
||||
it('should deduplicate paths when same root is trusted multiple times', async () => {
|
||||
@@ -750,13 +832,10 @@ included directory memory
|
||||
);
|
||||
|
||||
// Trust repoDir twice.
|
||||
const result = await loadEnvironmentMemory(
|
||||
[repoDir, repoDir],
|
||||
new SimpleExtensionLoader([]),
|
||||
);
|
||||
const result = await getEnvironmentMemoryPaths([repoDir, repoDir]);
|
||||
|
||||
expect(result.files).toHaveLength(1);
|
||||
expect(result.files[0].path).toBe(repoFile);
|
||||
expect(result).toHaveLength(1);
|
||||
expect(result[0]).toBe(repoFile);
|
||||
});
|
||||
|
||||
it('should keep multiple memory files from the same directory adjacent and in order', async () => {
|
||||
@@ -777,19 +856,14 @@ included directory memory
|
||||
'Secondary content',
|
||||
);
|
||||
|
||||
const result = await loadEnvironmentMemory(
|
||||
[dir],
|
||||
new SimpleExtensionLoader([]),
|
||||
);
|
||||
const result = await getEnvironmentMemoryPaths([dir]);
|
||||
|
||||
expect(result.files).toHaveLength(2);
|
||||
expect(result).toHaveLength(2);
|
||||
// Verify order: PRIMARY should come before SECONDARY because they are
|
||||
// sorted by path and PRIMARY.md comes before SECONDARY.md alphabetically
|
||||
// if in same dir.
|
||||
expect(result.files[0].path).toBe(primaryFile);
|
||||
expect(result.files[1].path).toBe(secondaryFile);
|
||||
expect(result.files[0].content).toBe('Primary content');
|
||||
expect(result.files[1].content).toBe('Secondary content');
|
||||
expect(result[0]).toBe(primaryFile);
|
||||
expect(result[1]).toBe(secondaryFile);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -904,16 +978,18 @@ included directory memory
|
||||
model: 'fake-model',
|
||||
extensionLoader,
|
||||
});
|
||||
const result = await loadServerHierarchicalMemory(
|
||||
config.getWorkingDir(),
|
||||
config.shouldLoadMemoryFromIncludeDirectories()
|
||||
? config.getWorkspaceContext().getDirectories()
|
||||
: [],
|
||||
config.getDebugMode(),
|
||||
config.getFileService(),
|
||||
config.getExtensionLoader(),
|
||||
config.isTrustedFolder(),
|
||||
config.getImportFormat(),
|
||||
const result = flattenResult(
|
||||
await loadServerHierarchicalMemory(
|
||||
config.getWorkingDir(),
|
||||
config.shouldLoadMemoryFromIncludeDirectories()
|
||||
? config.getWorkspaceContext().getDirectories()
|
||||
: [],
|
||||
config.getDebugMode(),
|
||||
config.getFileService(),
|
||||
config.getExtensionLoader(),
|
||||
config.isTrustedFolder(),
|
||||
config.getImportFormat(),
|
||||
),
|
||||
);
|
||||
expect(result.fileCount).equals(0);
|
||||
|
||||
@@ -937,12 +1013,11 @@ included directory memory
|
||||
const refreshResult = await refreshServerHierarchicalMemory(config);
|
||||
expect(refreshResult.fileCount).equals(1);
|
||||
expect(config.getGeminiMdFileCount()).equals(refreshResult.fileCount);
|
||||
expect(refreshResult.memoryContent).toContain(
|
||||
'Really cool custom context!',
|
||||
);
|
||||
expect(config.getUserMemory()).equals(refreshResult.memoryContent);
|
||||
const flattenedMemory = flattenMemory(refreshResult.memoryContent);
|
||||
expect(flattenedMemory).toContain('Really cool custom context!');
|
||||
expect(config.getUserMemory()).toStrictEqual(refreshResult.memoryContent);
|
||||
expect(refreshResult.filePaths[0]).toContain(
|
||||
path.join(extensionPath, 'CustomContext.md'),
|
||||
normMarker(path.join(extensionPath, 'CustomContext.md')),
|
||||
);
|
||||
expect(config.getGeminiMdFilePaths()).equals(refreshResult.filePaths);
|
||||
expect(mockEventListener).toHaveBeenCalledExactlyOnceWith({
|
||||
@@ -980,12 +1055,16 @@ included directory memory
|
||||
await refreshServerHierarchicalMemory(mockConfig);
|
||||
|
||||
expect(mockConfig.setUserMemory).toHaveBeenCalledWith(
|
||||
expect.stringContaining(
|
||||
"# Instructions for MCP Server 'extension-server'",
|
||||
),
|
||||
expect.objectContaining({
|
||||
project: expect.stringContaining(
|
||||
"# Instructions for MCP Server 'extension-server'",
|
||||
),
|
||||
}),
|
||||
);
|
||||
expect(mockConfig.setUserMemory).toHaveBeenCalledWith(
|
||||
expect.stringContaining('Always be polite.'),
|
||||
expect.objectContaining({
|
||||
project: expect.stringContaining('Always be polite.'),
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -13,10 +13,11 @@ import type { FileDiscoveryService } from '../services/fileDiscoveryService.js';
|
||||
import { processImports } from './memoryImportProcessor.js';
|
||||
import type { FileFilteringOptions } from '../config/constants.js';
|
||||
import { DEFAULT_MEMORY_FILE_FILTERING_OPTIONS } from '../config/constants.js';
|
||||
import { GEMINI_DIR, homedir } from './paths.js';
|
||||
import { GEMINI_DIR, homedir, normalizePath } from './paths.js';
|
||||
import type { ExtensionLoader } from './extensionLoader.js';
|
||||
import { debugLogger } from './debugLogger.js';
|
||||
import type { Config } from '../config/config.js';
|
||||
import type { HierarchicalMemory } from '../config/memory.js';
|
||||
import { CoreEvent, coreEvents } from './events.js';
|
||||
|
||||
// Simple console logger, similar to the one previously in CLI's config.ts
|
||||
@@ -39,7 +40,7 @@ export interface GeminiFileContent {
|
||||
}
|
||||
|
||||
async function findProjectRoot(startDir: string): Promise<string | null> {
|
||||
let currentDir = path.resolve(startDir);
|
||||
let currentDir = normalizePath(startDir);
|
||||
while (true) {
|
||||
const gitPath = path.join(currentDir, '.git');
|
||||
try {
|
||||
@@ -76,7 +77,7 @@ async function findProjectRoot(startDir: string): Promise<string | null> {
|
||||
}
|
||||
}
|
||||
}
|
||||
const parentDir = path.dirname(currentDir);
|
||||
const parentDir = normalizePath(path.dirname(currentDir));
|
||||
if (parentDir === currentDir) {
|
||||
return null;
|
||||
}
|
||||
@@ -93,7 +94,7 @@ async function getGeminiMdFilePathsInternal(
|
||||
folderTrust: boolean,
|
||||
fileFilteringOptions: FileFilteringOptions,
|
||||
maxDirs: number,
|
||||
): Promise<string[]> {
|
||||
): Promise<{ global: string[]; project: string[] }> {
|
||||
const dirs = new Set<string>([
|
||||
...includeDirectoriesToReadGemini,
|
||||
currentWorkingDirectory,
|
||||
@@ -102,7 +103,8 @@ async function getGeminiMdFilePathsInternal(
|
||||
// Process directories in parallel with concurrency limit to prevent EMFILE errors
|
||||
const CONCURRENT_LIMIT = 10;
|
||||
const dirsArray = Array.from(dirs);
|
||||
const pathsArrays: string[][] = [];
|
||||
const globalPaths = new Set<string>();
|
||||
const projectPaths = new Set<string>();
|
||||
|
||||
for (let i = 0; i < dirsArray.length; i += CONCURRENT_LIMIT) {
|
||||
const batch = dirsArray.slice(i, i + CONCURRENT_LIMIT);
|
||||
@@ -122,18 +124,20 @@ async function getGeminiMdFilePathsInternal(
|
||||
|
||||
for (const result of batchResults) {
|
||||
if (result.status === 'fulfilled') {
|
||||
pathsArrays.push(result.value);
|
||||
result.value.global.forEach((p) => globalPaths.add(p));
|
||||
result.value.project.forEach((p) => projectPaths.add(p));
|
||||
} else {
|
||||
const error = result.reason;
|
||||
const message = error instanceof Error ? error.message : String(error);
|
||||
logger.error(`Error discovering files in directory: ${message}`);
|
||||
// Continue processing other directories
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const paths = pathsArrays.flat();
|
||||
return Array.from(new Set<string>(paths));
|
||||
return {
|
||||
global: Array.from(globalPaths),
|
||||
project: Array.from(projectPaths),
|
||||
};
|
||||
}
|
||||
|
||||
async function getGeminiMdFilePathsInternalForEachDir(
|
||||
@@ -144,22 +148,22 @@ async function getGeminiMdFilePathsInternalForEachDir(
|
||||
folderTrust: boolean,
|
||||
fileFilteringOptions: FileFilteringOptions,
|
||||
maxDirs: number,
|
||||
): Promise<string[]> {
|
||||
const allPaths = new Set<string>();
|
||||
): Promise<{ global: string[]; project: string[] }> {
|
||||
const globalPaths = new Set<string>();
|
||||
const projectPaths = new Set<string>();
|
||||
const geminiMdFilenames = getAllGeminiMdFilenames();
|
||||
|
||||
for (const geminiMdFilename of geminiMdFilenames) {
|
||||
const resolvedHome = path.resolve(userHomePath);
|
||||
const globalMemoryPath = path.join(
|
||||
resolvedHome,
|
||||
GEMINI_DIR,
|
||||
geminiMdFilename,
|
||||
const resolvedHome = normalizePath(userHomePath);
|
||||
const globalGeminiDir = normalizePath(path.join(resolvedHome, GEMINI_DIR));
|
||||
const globalMemoryPath = normalizePath(
|
||||
path.join(globalGeminiDir, geminiMdFilename),
|
||||
);
|
||||
|
||||
// This part that finds the global file always runs.
|
||||
try {
|
||||
await fs.access(globalMemoryPath, fsSync.constants.R_OK);
|
||||
allPaths.add(globalMemoryPath);
|
||||
globalPaths.add(globalMemoryPath);
|
||||
if (debugMode)
|
||||
logger.debug(
|
||||
`Found readable global ${geminiMdFilename}: ${globalMemoryPath}`,
|
||||
@@ -171,7 +175,7 @@ async function getGeminiMdFilePathsInternalForEachDir(
|
||||
// FIX: Only perform the workspace search (upward and downward scans)
|
||||
// if a valid currentWorkingDirectory is provided.
|
||||
if (dir && folderTrust) {
|
||||
const resolvedCwd = path.resolve(dir);
|
||||
const resolvedCwd = normalizePath(dir);
|
||||
if (debugMode)
|
||||
logger.debug(
|
||||
`Searching for ${geminiMdFilename} starting from CWD: ${resolvedCwd}`,
|
||||
@@ -184,15 +188,20 @@ async function getGeminiMdFilePathsInternalForEachDir(
|
||||
const upwardPaths: string[] = [];
|
||||
let currentDir = resolvedCwd;
|
||||
const ultimateStopDir = projectRoot
|
||||
? path.dirname(projectRoot)
|
||||
: path.dirname(resolvedHome);
|
||||
? normalizePath(path.dirname(projectRoot))
|
||||
: normalizePath(path.dirname(resolvedHome));
|
||||
|
||||
while (currentDir && currentDir !== path.dirname(currentDir)) {
|
||||
if (currentDir === path.join(resolvedHome, GEMINI_DIR)) {
|
||||
while (
|
||||
currentDir &&
|
||||
currentDir !== normalizePath(path.dirname(currentDir))
|
||||
) {
|
||||
if (currentDir === globalGeminiDir) {
|
||||
break;
|
||||
}
|
||||
|
||||
const potentialPath = path.join(currentDir, geminiMdFilename);
|
||||
const potentialPath = normalizePath(
|
||||
path.join(currentDir, geminiMdFilename),
|
||||
);
|
||||
try {
|
||||
await fs.access(potentialPath, fsSync.constants.R_OK);
|
||||
if (potentialPath !== globalMemoryPath) {
|
||||
@@ -206,9 +215,9 @@ async function getGeminiMdFilePathsInternalForEachDir(
|
||||
break;
|
||||
}
|
||||
|
||||
currentDir = path.dirname(currentDir);
|
||||
currentDir = normalizePath(path.dirname(currentDir));
|
||||
}
|
||||
upwardPaths.forEach((p) => allPaths.add(p));
|
||||
upwardPaths.forEach((p) => projectPaths.add(p));
|
||||
|
||||
const mergedOptions: FileFilteringOptions = {
|
||||
...DEFAULT_MEMORY_FILE_FILTERING_OPTIONS,
|
||||
@@ -224,23 +233,18 @@ async function getGeminiMdFilePathsInternalForEachDir(
|
||||
});
|
||||
downwardPaths.sort();
|
||||
for (const dPath of downwardPaths) {
|
||||
allPaths.add(dPath);
|
||||
projectPaths.add(normalizePath(dPath));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const finalPaths = Array.from(allPaths);
|
||||
|
||||
if (debugMode)
|
||||
logger.debug(
|
||||
`Final ordered ${getAllGeminiMdFilenames()} paths to read: ${JSON.stringify(
|
||||
finalPaths,
|
||||
)}`,
|
||||
);
|
||||
return finalPaths;
|
||||
return {
|
||||
global: Array.from(globalPaths),
|
||||
project: Array.from(projectPaths),
|
||||
};
|
||||
}
|
||||
|
||||
async function readGeminiMdFiles(
|
||||
export async function readGeminiMdFiles(
|
||||
filePaths: string[],
|
||||
debugMode: boolean,
|
||||
importFormat: 'flat' | 'tree' = 'tree',
|
||||
@@ -331,14 +335,14 @@ export interface MemoryLoadResult {
|
||||
files: Array<{ path: string; content: string }>;
|
||||
}
|
||||
|
||||
export async function loadGlobalMemory(
|
||||
export async function getGlobalMemoryPaths(
|
||||
debugMode: boolean = false,
|
||||
): Promise<MemoryLoadResult> {
|
||||
): Promise<string[]> {
|
||||
const userHome = homedir();
|
||||
const geminiMdFilenames = getAllGeminiMdFilenames();
|
||||
|
||||
const accessChecks = geminiMdFilenames.map(async (filename) => {
|
||||
const globalPath = path.join(userHome, GEMINI_DIR, filename);
|
||||
const globalPath = normalizePath(path.join(userHome, GEMINI_DIR, filename));
|
||||
try {
|
||||
await fs.access(globalPath, fsSync.constants.R_OK);
|
||||
if (debugMode) {
|
||||
@@ -346,25 +350,67 @@ export async function loadGlobalMemory(
|
||||
}
|
||||
return globalPath;
|
||||
} catch {
|
||||
debugLogger.debug('A global memory file was not found.');
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
const foundPaths = (await Promise.all(accessChecks)).filter(
|
||||
return (await Promise.all(accessChecks)).filter(
|
||||
(p): p is string => p !== null,
|
||||
);
|
||||
}
|
||||
|
||||
const contents = await readGeminiMdFiles(foundPaths, debugMode, 'tree');
|
||||
export function getExtensionMemoryPaths(
|
||||
extensionLoader: ExtensionLoader,
|
||||
): string[] {
|
||||
const extensionPaths = extensionLoader
|
||||
.getExtensions()
|
||||
.filter((ext) => ext.isActive)
|
||||
.flatMap((ext) => ext.contextFiles)
|
||||
.map((p) => normalizePath(p));
|
||||
|
||||
return Array.from(new Set(extensionPaths)).sort();
|
||||
}
|
||||
|
||||
export async function getEnvironmentMemoryPaths(
|
||||
trustedRoots: string[],
|
||||
debugMode: boolean = false,
|
||||
): Promise<string[]> {
|
||||
const allPaths = new Set<string>();
|
||||
|
||||
// Trusted Roots Upward Traversal (Parallelized)
|
||||
const traversalPromises = trustedRoots.map(async (root) => {
|
||||
const resolvedRoot = normalizePath(root);
|
||||
if (debugMode) {
|
||||
logger.debug(
|
||||
`Loading environment memory for trusted root: ${resolvedRoot} (Stopping exactly here)`,
|
||||
);
|
||||
}
|
||||
return findUpwardGeminiFiles(resolvedRoot, resolvedRoot, debugMode);
|
||||
});
|
||||
|
||||
const pathArrays = await Promise.all(traversalPromises);
|
||||
pathArrays.flat().forEach((p) => allPaths.add(p));
|
||||
|
||||
return Array.from(allPaths).sort();
|
||||
}
|
||||
|
||||
export function categorizeAndConcatenate(
|
||||
paths: { global: string[]; extension: string[]; project: string[] },
|
||||
contentsMap: Map<string, GeminiFileContent>,
|
||||
workingDir: string,
|
||||
): HierarchicalMemory {
|
||||
const getConcatenated = (pList: string[]) =>
|
||||
concatenateInstructions(
|
||||
pList
|
||||
.map((p) => contentsMap.get(p))
|
||||
.filter((c): c is GeminiFileContent => !!c),
|
||||
workingDir,
|
||||
);
|
||||
|
||||
return {
|
||||
files: contents
|
||||
.filter((item) => item.content !== null)
|
||||
.map((item) => ({
|
||||
path: item.filePath,
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
|
||||
content: item.content as string,
|
||||
})),
|
||||
global: getConcatenated(paths.global),
|
||||
extension: getConcatenated(paths.extension),
|
||||
project: getConcatenated(paths.project),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -380,10 +426,10 @@ async function findUpwardGeminiFiles(
|
||||
debugMode: boolean,
|
||||
): Promise<string[]> {
|
||||
const upwardPaths: string[] = [];
|
||||
let currentDir = path.resolve(startDir);
|
||||
const resolvedStopDir = path.resolve(stopDir);
|
||||
let currentDir = normalizePath(startDir);
|
||||
const resolvedStopDir = normalizePath(stopDir);
|
||||
const geminiMdFilenames = getAllGeminiMdFilenames();
|
||||
const globalGeminiDir = path.join(homedir(), GEMINI_DIR);
|
||||
const globalGeminiDir = normalizePath(path.join(homedir(), GEMINI_DIR));
|
||||
|
||||
if (debugMode) {
|
||||
logger.debug(
|
||||
@@ -398,7 +444,7 @@ async function findUpwardGeminiFiles(
|
||||
|
||||
// Parallelize checks for all filename variants in the current directory
|
||||
const accessChecks = geminiMdFilenames.map(async (filename) => {
|
||||
const potentialPath = path.join(currentDir, filename);
|
||||
const potentialPath = normalizePath(path.join(currentDir, filename));
|
||||
try {
|
||||
await fs.access(potentialPath, fsSync.constants.R_OK);
|
||||
return potentialPath;
|
||||
@@ -413,61 +459,17 @@ async function findUpwardGeminiFiles(
|
||||
|
||||
upwardPaths.unshift(...foundPathsInDir);
|
||||
|
||||
if (
|
||||
currentDir === resolvedStopDir ||
|
||||
currentDir === path.dirname(currentDir)
|
||||
) {
|
||||
const parentDir = normalizePath(path.dirname(currentDir));
|
||||
if (currentDir === resolvedStopDir || currentDir === parentDir) {
|
||||
break;
|
||||
}
|
||||
currentDir = path.dirname(currentDir);
|
||||
currentDir = parentDir;
|
||||
}
|
||||
return upwardPaths;
|
||||
}
|
||||
|
||||
export async function loadEnvironmentMemory(
|
||||
trustedRoots: string[],
|
||||
extensionLoader: ExtensionLoader,
|
||||
debugMode: boolean = false,
|
||||
): Promise<MemoryLoadResult> {
|
||||
const allPaths = new Set<string>();
|
||||
|
||||
// Trusted Roots Upward Traversal (Parallelized)
|
||||
const traversalPromises = trustedRoots.map(async (root) => {
|
||||
const resolvedRoot = path.resolve(root);
|
||||
if (debugMode) {
|
||||
logger.debug(
|
||||
`Loading environment memory for trusted root: ${resolvedRoot} (Stopping exactly here)`,
|
||||
);
|
||||
}
|
||||
return findUpwardGeminiFiles(resolvedRoot, resolvedRoot, debugMode);
|
||||
});
|
||||
|
||||
const pathArrays = await Promise.all(traversalPromises);
|
||||
pathArrays.flat().forEach((p) => allPaths.add(p));
|
||||
|
||||
// Extensions
|
||||
const extensionPaths = extensionLoader
|
||||
.getExtensions()
|
||||
.filter((ext) => ext.isActive)
|
||||
.flatMap((ext) => ext.contextFiles);
|
||||
extensionPaths.forEach((p) => allPaths.add(p));
|
||||
|
||||
const sortedPaths = Array.from(allPaths).sort();
|
||||
const contents = await readGeminiMdFiles(sortedPaths, debugMode, 'tree');
|
||||
|
||||
return {
|
||||
files: contents
|
||||
.filter((item) => item.content !== null)
|
||||
.map((item) => ({
|
||||
path: item.filePath,
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
|
||||
content: item.content as string,
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
export interface LoadServerHierarchicalMemoryResponse {
|
||||
memoryContent: string;
|
||||
memoryContent: HierarchicalMemory;
|
||||
fileCount: number;
|
||||
filePaths: string[];
|
||||
}
|
||||
@@ -488,8 +490,10 @@ export async function loadServerHierarchicalMemory(
|
||||
maxDirs: number = 200,
|
||||
): Promise<LoadServerHierarchicalMemoryResponse> {
|
||||
// FIX: Use real, canonical paths for a reliable comparison to handle symlinks.
|
||||
const realCwd = await fs.realpath(path.resolve(currentWorkingDirectory));
|
||||
const realHome = await fs.realpath(path.resolve(homedir()));
|
||||
const realCwd = normalizePath(
|
||||
await fs.realpath(path.resolve(currentWorkingDirectory)),
|
||||
);
|
||||
const realHome = normalizePath(await fs.realpath(path.resolve(homedir())));
|
||||
const isHomeDirectory = realCwd === realHome;
|
||||
|
||||
// If it is the home directory, pass an empty string to the core memory
|
||||
@@ -504,52 +508,63 @@ export async function loadServerHierarchicalMemory(
|
||||
// For the server, homedir() refers to the server process's home.
|
||||
// This is consistent with how MemoryTool already finds the global path.
|
||||
const userHomePath = homedir();
|
||||
const filePaths = await getGeminiMdFilePathsInternal(
|
||||
currentWorkingDirectory,
|
||||
includeDirectoriesToReadGemini,
|
||||
userHomePath,
|
||||
debugMode,
|
||||
fileService,
|
||||
folderTrust,
|
||||
fileFilteringOptions || DEFAULT_MEMORY_FILE_FILTERING_OPTIONS,
|
||||
maxDirs,
|
||||
|
||||
// 1. SCATTER: Gather all paths
|
||||
const [discoveryResult, extensionPaths] = await Promise.all([
|
||||
getGeminiMdFilePathsInternal(
|
||||
currentWorkingDirectory,
|
||||
includeDirectoriesToReadGemini,
|
||||
userHomePath,
|
||||
debugMode,
|
||||
fileService,
|
||||
folderTrust,
|
||||
fileFilteringOptions || DEFAULT_MEMORY_FILE_FILTERING_OPTIONS,
|
||||
maxDirs,
|
||||
),
|
||||
Promise.resolve(getExtensionMemoryPaths(extensionLoader)),
|
||||
]);
|
||||
|
||||
const allFilePaths = Array.from(
|
||||
new Set([
|
||||
...discoveryResult.global,
|
||||
...discoveryResult.project,
|
||||
...extensionPaths,
|
||||
]),
|
||||
);
|
||||
|
||||
// Add extension file paths separately since they may be conditionally enabled.
|
||||
filePaths.push(
|
||||
...extensionLoader
|
||||
.getExtensions()
|
||||
.filter((ext) => ext.isActive)
|
||||
.flatMap((ext) => ext.contextFiles),
|
||||
);
|
||||
|
||||
if (filePaths.length === 0) {
|
||||
if (allFilePaths.length === 0) {
|
||||
if (debugMode)
|
||||
logger.debug('No GEMINI.md files found in hierarchy of the workspace.');
|
||||
return { memoryContent: '', fileCount: 0, filePaths: [] };
|
||||
return {
|
||||
memoryContent: { global: '', extension: '', project: '' },
|
||||
fileCount: 0,
|
||||
filePaths: [],
|
||||
};
|
||||
}
|
||||
const contentsWithPaths = await readGeminiMdFiles(
|
||||
filePaths,
|
||||
|
||||
// 2. GATHER: Read all files in parallel
|
||||
const allContents = await readGeminiMdFiles(
|
||||
allFilePaths,
|
||||
debugMode,
|
||||
importFormat,
|
||||
);
|
||||
// Pass CWD for relative path display in concatenated content
|
||||
const combinedInstructions = concatenateInstructions(
|
||||
contentsWithPaths,
|
||||
const contentsMap = new Map(allContents.map((c) => [c.filePath, c]));
|
||||
|
||||
// 3. CATEGORIZE: Back into Global, Project, Extension
|
||||
const hierarchicalMemory = categorizeAndConcatenate(
|
||||
{
|
||||
global: discoveryResult.global,
|
||||
extension: extensionPaths,
|
||||
project: discoveryResult.project,
|
||||
},
|
||||
contentsMap,
|
||||
currentWorkingDirectory,
|
||||
);
|
||||
if (debugMode)
|
||||
logger.debug(
|
||||
`Combined instructions length: ${combinedInstructions.length}`,
|
||||
);
|
||||
if (debugMode && combinedInstructions.length > 0)
|
||||
logger.debug(
|
||||
`Combined instructions (snippet): ${combinedInstructions.substring(0, 500)}...`,
|
||||
);
|
||||
|
||||
return {
|
||||
memoryContent: combinedInstructions,
|
||||
fileCount: contentsWithPaths.length,
|
||||
filePaths,
|
||||
memoryContent: hierarchicalMemory,
|
||||
fileCount: allContents.filter((c) => c.content !== null).length,
|
||||
filePaths: allFilePaths,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -575,9 +590,12 @@ export async function refreshServerHierarchicalMemory(config: Config) {
|
||||
);
|
||||
const mcpInstructions =
|
||||
config.getMcpClientManager()?.getMcpInstructions() || '';
|
||||
const finalMemory = [result.memoryContent, mcpInstructions.trimStart()]
|
||||
.filter(Boolean)
|
||||
.join('\n\n');
|
||||
const finalMemory: HierarchicalMemory = {
|
||||
...result.memoryContent,
|
||||
project: [result.memoryContent.project, mcpInstructions.trimStart()]
|
||||
.filter(Boolean)
|
||||
.join('\n\n'),
|
||||
};
|
||||
config.setUserMemory(finalMemory);
|
||||
config.setGeminiMdFileCount(result.fileCount);
|
||||
config.setGeminiMdFilePaths(result.filePaths);
|
||||
@@ -591,17 +609,23 @@ export async function loadJitSubdirectoryMemory(
|
||||
alreadyLoadedPaths: Set<string>,
|
||||
debugMode: boolean = false,
|
||||
): Promise<MemoryLoadResult> {
|
||||
const resolvedTarget = path.resolve(targetPath);
|
||||
const resolvedTarget = normalizePath(targetPath);
|
||||
let bestRoot: string | null = null;
|
||||
|
||||
// Find the deepest trusted root that contains the target path
|
||||
for (const root of trustedRoots) {
|
||||
const resolvedRoot = path.resolve(root);
|
||||
const resolvedRoot = normalizePath(root);
|
||||
const resolvedRootWithTrailing = resolvedRoot.endsWith(path.sep)
|
||||
? resolvedRoot
|
||||
: resolvedRoot + path.sep;
|
||||
|
||||
if (
|
||||
resolvedTarget.startsWith(resolvedRoot) &&
|
||||
(!bestRoot || resolvedRoot.length > bestRoot.length)
|
||||
resolvedTarget === resolvedRoot ||
|
||||
resolvedTarget.startsWith(resolvedRootWithTrailing)
|
||||
) {
|
||||
bestRoot = resolvedRoot;
|
||||
if (!bestRoot || resolvedRoot.length > bestRoot.length) {
|
||||
bestRoot = resolvedRoot;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -328,6 +328,16 @@ export function getProjectHash(projectRoot: string): string {
|
||||
return crypto.createHash('sha256').update(projectRoot).digest('hex');
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalizes a path for reliable comparison.
|
||||
* - Resolves to an absolute path.
|
||||
* - On Windows, converts to lowercase for case-insensitivity.
|
||||
*/
|
||||
export function normalizePath(p: string): string {
|
||||
const resolved = path.resolve(p);
|
||||
return process.platform === 'win32' ? resolved.toLowerCase() : resolved;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a path is a subpath of another path.
|
||||
* @param parentPath The parent path.
|
||||
|
||||
Reference in New Issue
Block a user