fix(core): fix three JIT context bugs in read_file, read_many_files, and memoryDiscovery (#22679)

This commit is contained in:
Sandy Tao
2026-03-16 13:10:50 -07:00
committed by GitHub
parent dfe22aae21
commit b91f75cd6d
7 changed files with 221 additions and 12 deletions
@@ -860,5 +860,62 @@ Content of file[1]
: String(result.llmContent);
expect(llmContent).not.toContain('Newly Discovered Project Context');
});
it('should discover JIT context sequentially to avoid duplicate shared parent context', async () => {
const { discoverJitContext } = await import('./jit-context.js');
// Simulate two subdirectories sharing a parent GEMINI.md.
// Sequential execution means the second call sees the parent already
// loaded, so it only returns its own leaf context.
const callOrder: string[] = [];
let firstCallDone = false;
vi.mocked(discoverJitContext).mockImplementation(async (_config, dir) => {
callOrder.push(dir);
if (!firstCallDone) {
// First call (whichever dir) loads the shared parent + its own leaf
firstCallDone = true;
return 'Parent context\nFirst leaf context';
}
// Second call only returns its own leaf (parent already loaded)
return 'Second leaf context';
});
// Create files in two sibling subdirectories
fs.mkdirSync(path.join(tempRootDir, 'subA'), { recursive: true });
fs.mkdirSync(path.join(tempRootDir, 'subB'), { recursive: true });
fs.writeFileSync(
path.join(tempRootDir, 'subA', 'a.ts'),
'const a = 1;',
'utf8',
);
fs.writeFileSync(
path.join(tempRootDir, 'subB', 'b.ts'),
'const b = 2;',
'utf8',
);
const invocation = tool.build({ include: ['subA/a.ts', 'subB/b.ts'] });
const result = await invocation.execute(new AbortController().signal);
// Verify both directories were discovered (order depends on Set iteration)
expect(callOrder).toHaveLength(2);
expect(callOrder).toEqual(
expect.arrayContaining([
expect.stringContaining('subA'),
expect.stringContaining('subB'),
]),
);
const llmContent = Array.isArray(result.llmContent)
? result.llmContent.join('')
: String(result.llmContent);
expect(llmContent).toContain('Parent context');
expect(llmContent).toContain('First leaf context');
expect(llmContent).toContain('Second leaf context');
// Parent context should appear only once (from the first call), not duplicated
const parentMatches = llmContent.match(/Parent context/g);
expect(parentMatches).toHaveLength(1);
});
});
});