refactor(core): align JIT memory placement with tiered context model (#22766)

This commit is contained in:
Sandy Tao
2026-03-17 13:20:32 -07:00
committed by GitHub
parent 1f3f7247b1
commit 82d8680dcc
7 changed files with 86 additions and 17 deletions
+15
View File
@@ -3063,6 +3063,21 @@ describe('Config JIT Initialization', () => {
project: 'Environment Memory\n\nMCP Instructions',
});
// Tier 1: system instruction gets only global memory
expect(config.getSystemInstructionMemory()).toBe('Global Memory');
// Tier 2: session memory gets extension + project formatted with XML tags
const sessionMemory = config.getSessionMemory();
expect(sessionMemory).toContain('<loaded_context>');
expect(sessionMemory).toContain('<extension_context>');
expect(sessionMemory).toContain('Extension Memory');
expect(sessionMemory).toContain('</extension_context>');
expect(sessionMemory).toContain('<project_context>');
expect(sessionMemory).toContain('Environment Memory');
expect(sessionMemory).toContain('MCP Instructions');
expect(sessionMemory).toContain('</project_context>');
expect(sessionMemory).toContain('</loaded_context>');
// Verify state update (delegated to ContextManager)
expect(config.getGeminiMdFileCount()).toBe(1);
expect(config.getGeminiMdFilePaths()).toEqual(['/path/to/GEMINI.md']);
+37
View File
@@ -2056,6 +2056,43 @@ export class Config implements McpContext, AgentLoopContext {
this.userMemory = newUserMemory;
}
/**
* Returns memory for the system instruction.
* When JIT is enabled, only global memory (Tier 1) goes in the system
* instruction. Extension and project memory (Tier 2) are placed in the
* first user message instead, per the tiered context model.
*/
getSystemInstructionMemory(): string | HierarchicalMemory {
if (this.experimentalJitContext && this.contextManager) {
return this.contextManager.getGlobalMemory();
}
return this.userMemory;
}
/**
* Returns Tier 2 memory (extension + project) for injection into the first
* user message when JIT is enabled. Returns empty string when JIT is
* disabled (Tier 2 memory is already in the system instruction).
*/
getSessionMemory(): string {
if (!this.experimentalJitContext || !this.contextManager) {
return '';
}
const sections: string[] = [];
const extension = this.contextManager.getExtensionMemory();
const project = this.contextManager.getEnvironmentMemory();
if (extension?.trim()) {
sections.push(
`<extension_context>\n${extension.trim()}\n</extension_context>`,
);
}
if (project?.trim()) {
sections.push(`<project_context>\n${project.trim()}\n</project_context>`);
}
if (sections.length === 0) return '';
return `\n<loaded_context>\n${sections.join('\n')}\n</loaded_context>`;
}
getGlobalMemory(): string {
return this.contextManager?.getGlobalMemory() ?? '';
}