Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions packages/core/src/utils/memoryDiscovery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1352,6 +1352,119 @@ describe('loadServerHierarchicalMemory', () => {
expect(occurrences).toBe(1);
});
});

describe('symlink aliases of the same physical file (#9597)', () => {
it('loads a context file once when a workspace-level file is a symlink to an ancestor file', async () => {
await createTestFile(
path.join(projectRoot, DEFAULT_CONTEXT_FILENAME),
'shared symlink marker content',
);
await fsPromises.symlink(
path.join(projectRoot, DEFAULT_CONTEXT_FILENAME),
path.join(cwd, DEFAULT_CONTEXT_FILENAME),
);

const result = await loadServerHierarchicalMemory(
cwd,
[],
new FileDiscoveryService(projectRoot),
[],
DEFAULT_FOLDER_TRUST,
);

expect(result.fileCount).toBe(1);
expect(result.contextFilePaths).toHaveLength(1);
const occurrences = (
result.memoryContent.match(/shared symlink marker content/g) ?? []
).length;
expect(occurrences).toBe(1);
});

it('keeps two context blocks for distinct physical files with identical content', async () => {
await createTestFile(
path.join(projectRoot, DEFAULT_CONTEXT_FILENAME),
'identical content in two physical files',
);
await createTestFile(
path.join(cwd, DEFAULT_CONTEXT_FILENAME),
'identical content in two physical files',
);

const result = await loadServerHierarchicalMemory(
cwd,
[],
new FileDiscoveryService(projectRoot),
[],
DEFAULT_FOLDER_TRUST,
);

expect(result.fileCount).toBe(2);
const occurrences = (
result.memoryContent.match(
/identical content in two physical files/g,
) ?? []
).length;
expect(occurrences).toBe(2);
});

it('still loads through a symlink when the target is outside the project-root scan boundary', async () => {
// A .git marker makes cwd the project root, so the upward scan stops
// at its parent and never reaches testRootDir. The outside file can
// then only be loaded through the workspace symlink.
await createEmptyDir(path.join(cwd, '.git'));
await createTestFile(
path.join(testRootDir, DEFAULT_CONTEXT_FILENAME),
'outside scan boundary marker',
);
await fsPromises.symlink(
path.join(testRootDir, DEFAULT_CONTEXT_FILENAME),
path.join(cwd, DEFAULT_CONTEXT_FILENAME),
);

const result = await loadServerHierarchicalMemory(
cwd,
[],
new FileDiscoveryService(cwd),
[],
DEFAULT_FOLDER_TRUST,
);

expect(result.fileCount).toBe(1);
expect(result.memoryContent).toContain('outside scan boundary marker');
});

it('does not duplicate relative @imports of a file loaded through a symlink alias', async () => {
// The import target resolves from both directories, so while the parent
// file is loaded twice (once per lexical alias) its @import content is
// attached twice as well.
await createTestFile(
path.join(projectRoot, 'shared.md'),
'imported-once marker',
);
await createTestFile(path.join(cwd, 'shared.md'), 'imported-once marker');
await createTestFile(
path.join(projectRoot, DEFAULT_CONTEXT_FILENAME),
'@shared.md',
);
await fsPromises.symlink(
path.join(projectRoot, DEFAULT_CONTEXT_FILENAME),
path.join(cwd, DEFAULT_CONTEXT_FILENAME),
);

const result = await loadServerHierarchicalMemory(
cwd,
[],
new FileDiscoveryService(projectRoot),
[],
DEFAULT_FOLDER_TRUST,
);

const occurrences = (
result.memoryContent.match(/imported-once marker/g) ?? []
).length;
expect(occurrences).toBe(1);
});
});
});

describe('formatContextFileDisplayPath', () => {
Expand Down
43 changes: 40 additions & 3 deletions packages/core/src/utils/memoryDiscovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,37 @@ async function getGeminiMdFilePathsInternalForEachDir(
return finalPaths;
}

/**
* Deduplicates discovered context files by canonical file identity so one
* physical file reached through multiple lexical paths (e.g. a workspace
* QWEN.md that symlinks to an ancestor QWEN.md) is read, attached, and
* announced only once (#9597). The first-discovered lexical path is kept
* so ordering, display, and relative @import resolution are unchanged.
* Paths whose canonical identity cannot be resolved (broken symlink,
* unreadable file) fall back to their resolved lexical path, preserving
* the pre-existing behavior for those entries.
*/
async function dedupeByCanonicalIdentity(
filePaths: string[],
): Promise<string[]> {
const seenIdentities = new Set<string>();
const dedupedPaths: string[] = [];
for (const filePath of filePaths) {
let identity = path.resolve(filePath);
try {
identity = await fs.realpath(filePath);
} catch {
// Keep the lexical identity so unreadable/unresolvable entries are
// still handled exactly as before downstream.
}
if (!seenIdentities.has(identity)) {
seenIdentities.add(identity);
dedupedPaths.push(filePath);
}
}
return dedupedPaths;
}

async function readGeminiMdFiles(
filePaths: string[],
importFormat: 'flat' | 'tree' = 'tree',
Expand Down Expand Up @@ -524,14 +555,20 @@ export async function loadServerHierarchicalMemory(
}
}

// One physical file may be discovered through several lexical paths
// (a workspace symlink aliasing an ancestor QWEN.md, an extension path
// that duplicates a scanned path). Dedupe by canonical identity so the
// file's content reaches the system prompt exactly once (#9597).
const dedupedFilePaths = await dedupeByCanonicalIdentity(filePaths);

let combinedInstructions = '';
let fileCount = 0;
let contextFilePaths: string[] = [];

if (filePaths.length > 0) {
if (dedupedFilePaths.length > 0) {
const loadReason = options.loadReason ?? 'session_start';
const contentsWithPaths = await readGeminiMdFiles(
filePaths,
dedupedFilePaths,
importFormat,
createMemoryTypeClassifier(
userHomePath,
Expand Down Expand Up @@ -594,7 +631,7 @@ export async function loadServerHierarchicalMemory(
: rulesContent;
}

if (!memoryContent && filePaths.length === 0 && ruleCount === 0) {
if (!memoryContent && dedupedFilePaths.length === 0 && ruleCount === 0) {
logger.debug('No QWEN.md files or rules found.');
}

Expand Down
Loading