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

View File

@@ -30,6 +30,15 @@ vi.mock('./jit-context.js', () => ({
if (!context) return content;
return `${content}\n\n--- Newly Discovered Project Context ---\n${context}\n--- End Project Context ---`;
}),
appendJitContextToParts: vi.fn().mockImplementation((content, context) => {
const jitPart = {
text: `\n\n--- Newly Discovered Project Context ---\n${context}\n--- End Project Context ---`,
};
const existing = Array.isArray(content) ? content : [content];
return [...existing, jitPart];
}),
JIT_CONTEXT_PREFIX: '\n\n--- Newly Discovered Project Context ---\n',
JIT_CONTEXT_SUFFIX: '\n--- End Project Context ---',
}));
describe('ReadFileTool', () => {
@@ -637,5 +646,43 @@ describe('ReadFileTool', () => {
'Newly Discovered Project Context',
);
});
it('should append JIT context as Part array for non-string llmContent (binary files)', async () => {
const { discoverJitContext } = await import('./jit-context.js');
vi.mocked(discoverJitContext).mockResolvedValue(
'Auth rules: use httpOnly cookies.',
);
// Create a minimal valid PNG file (1x1 pixel)
const pngHeader = Buffer.from([
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d,
0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
0x08, 0x02, 0x00, 0x00, 0x00, 0x90, 0x77, 0x53, 0xde, 0x00, 0x00, 0x00,
0x0c, 0x49, 0x44, 0x41, 0x54, 0x08, 0xd7, 0x63, 0xf8, 0xcf, 0xc0, 0x00,
0x00, 0x00, 0x02, 0x00, 0x01, 0xe2, 0x21, 0xbc, 0x33, 0x00, 0x00, 0x00,
0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82,
]);
const filePath = path.join(tempRootDir, 'test-image.png');
await fsp.writeFile(filePath, pngHeader);
const invocation = tool.build({ file_path: filePath });
const result = await invocation.execute(abortSignal);
expect(discoverJitContext).toHaveBeenCalled();
// Result should be an array containing both the image part and JIT context
expect(Array.isArray(result.llmContent)).toBe(true);
const parts = result.llmContent as Array<Record<string, unknown>>;
const jitTextPart = parts.find(
(p) =>
typeof p['text'] === 'string' && p['text'].includes('Auth rules'),
);
expect(jitTextPart).toBeDefined();
expect(jitTextPart!['text']).toContain(
'Newly Discovered Project Context',
);
expect(jitTextPart!['text']).toContain(
'Auth rules: use httpOnly cookies.',
);
});
});
});