feat(core): Fully migrate packages/core to AgentLoopContext. (#22115)

This commit is contained in:
joshualitt
2026-03-12 18:56:31 -07:00
committed by GitHub
parent 1d2585dba6
commit de656f01d7
53 changed files with 522 additions and 292 deletions
+11 -5
View File
@@ -67,6 +67,7 @@ import {
DEFAULT_GEMINI_MODEL_AUTO,
} from './models.js';
import { Storage } from './storage.js';
import type { AgentLoopContext } from './agent-loop-context.js';
vi.mock('fs', async (importOriginal) => {
const actual = await importOriginal<typeof import('fs')>();
@@ -641,8 +642,9 @@ describe('Server Config (config.ts)', () => {
await config.refreshAuth(AuthType.LOGIN_WITH_GOOGLE);
const loopContext: AgentLoopContext = config;
expect(
config.getGeminiClient().stripThoughtsFromHistory,
loopContext.geminiClient.stripThoughtsFromHistory,
).toHaveBeenCalledWith();
});
@@ -660,8 +662,9 @@ describe('Server Config (config.ts)', () => {
await config.refreshAuth(AuthType.USE_VERTEX_AI);
const loopContext: AgentLoopContext = config;
expect(
config.getGeminiClient().stripThoughtsFromHistory,
loopContext.geminiClient.stripThoughtsFromHistory,
).toHaveBeenCalledWith();
});
@@ -679,8 +682,9 @@ describe('Server Config (config.ts)', () => {
await config.refreshAuth(AuthType.USE_GEMINI);
const loopContext: AgentLoopContext = config;
expect(
config.getGeminiClient().stripThoughtsFromHistory,
loopContext.geminiClient.stripThoughtsFromHistory,
).not.toHaveBeenCalledWith();
});
});
@@ -3059,7 +3063,8 @@ describe('Config JIT Initialization', () => {
await config.initialize();
const skillManager = config.getSkillManager();
const toolRegistry = config.getToolRegistry();
const loopContext: AgentLoopContext = config;
const toolRegistry = loopContext.toolRegistry;
vi.spyOn(skillManager, 'discoverSkills').mockResolvedValue(undefined);
vi.spyOn(skillManager, 'setDisabledSkills');
@@ -3095,7 +3100,8 @@ describe('Config JIT Initialization', () => {
await config.initialize();
const skillManager = config.getSkillManager();
const toolRegistry = config.getToolRegistry();
const loopContext: AgentLoopContext = config;
const toolRegistry = loopContext.toolRegistry;
vi.spyOn(skillManager, 'discoverSkills').mockResolvedValue(undefined);
vi.spyOn(toolRegistry, 'registerTool');
+22 -10
View File
@@ -1036,7 +1036,7 @@ export class Config implements McpContext, AgentLoopContext {
// Register Conseca if enabled
if (this.enableConseca) {
debugLogger.log('[SAFETY] Registering Conseca Safety Checker');
ConsecaSafetyChecker.getInstance().setConfig(this);
ConsecaSafetyChecker.getInstance().setContext(this);
}
this._messageBus = new MessageBus(this.policyEngine, this.debugMode);
@@ -1225,8 +1225,8 @@ export class Config implements McpContext, AgentLoopContext {
// Re-register ActivateSkillTool to update its schema with the discovered enabled skill enums
if (this.getSkillManager().getSkills().length > 0) {
this.getToolRegistry().unregisterTool(ActivateSkillTool.Name);
this.getToolRegistry().registerTool(
this.toolRegistry.unregisterTool(ActivateSkillTool.Name);
this.toolRegistry.registerTool(
new ActivateSkillTool(this, this.messageBus),
);
}
@@ -1397,14 +1397,26 @@ export class Config implements McpContext, AgentLoopContext {
return this._sessionId;
}
/**
* @deprecated Do not access directly on Config.
* Use the injected AgentLoopContext instead.
*/
get toolRegistry(): ToolRegistry {
return this._toolRegistry;
}
/**
* @deprecated Do not access directly on Config.
* Use the injected AgentLoopContext instead.
*/
get messageBus(): MessageBus {
return this._messageBus;
}
/**
* @deprecated Do not access directly on Config.
* Use the injected AgentLoopContext instead.
*/
get geminiClient(): GeminiClient {
return this._geminiClient;
}
@@ -2243,7 +2255,7 @@ export class Config implements McpContext, AgentLoopContext {
* Whenever the user memory (GEMINI.md files) is updated.
*/
updateSystemInstructionIfInitialized(): void {
const geminiClient = this.getGeminiClient();
const geminiClient = this.geminiClient;
if (geminiClient?.isInitialized()) {
geminiClient.updateSystemInstruction();
}
@@ -2709,16 +2721,16 @@ export class Config implements McpContext, AgentLoopContext {
// Re-register ActivateSkillTool to update its schema with the newly discovered skills
if (this.getSkillManager().getSkills().length > 0) {
this.getToolRegistry().unregisterTool(ActivateSkillTool.Name);
this.getToolRegistry().registerTool(
this.toolRegistry.unregisterTool(ActivateSkillTool.Name);
this.toolRegistry.registerTool(
new ActivateSkillTool(this, this.messageBus),
);
} else {
this.getToolRegistry().unregisterTool(ActivateSkillTool.Name);
this.toolRegistry.unregisterTool(ActivateSkillTool.Name);
}
} else {
this.getSkillManager().clearSkills();
this.getToolRegistry().unregisterTool(ActivateSkillTool.Name);
this.toolRegistry.unregisterTool(ActivateSkillTool.Name);
}
// Notify the client that system instructions might need updating
@@ -3054,7 +3066,7 @@ export class Config implements McpContext, AgentLoopContext {
for (const definition of definitions) {
try {
const tool = new SubagentTool(definition, this, this.getMessageBus());
const tool = new SubagentTool(definition, this, this.messageBus);
registry.registerTool(tool);
} catch (e: unknown) {
debugLogger.warn(
@@ -3159,7 +3171,7 @@ export class Config implements McpContext, AgentLoopContext {
this.registerSubAgentTools(this._toolRegistry);
}
// Propagate updates to the active chat session
const client = this.getGeminiClient();
const client = this.geminiClient;
if (client?.isInitialized()) {
await client.setTools();
client.updateSystemInstruction();
@@ -8,6 +8,7 @@ import { describe, it, expect } from 'vitest';
import { Config } from './config.js';
import { TRACKER_CREATE_TASK_TOOL_NAME } from '../tools/tool-names.js';
import * as os from 'node:os';
import type { AgentLoopContext } from './agent-loop-context.js';
describe('Config Tracker Feature Flag', () => {
const baseParams = {
@@ -21,7 +22,8 @@ describe('Config Tracker Feature Flag', () => {
it('should not register tracker tools by default', async () => {
const config = new Config(baseParams);
await config.initialize();
const registry = config.getToolRegistry();
const loopContext: AgentLoopContext = config;
const registry = loopContext.toolRegistry;
expect(registry.getTool(TRACKER_CREATE_TASK_TOOL_NAME)).toBeUndefined();
});
@@ -31,7 +33,8 @@ describe('Config Tracker Feature Flag', () => {
tracker: true,
});
await config.initialize();
const registry = config.getToolRegistry();
const loopContext: AgentLoopContext = config;
const registry = loopContext.toolRegistry;
expect(registry.getTool(TRACKER_CREATE_TASK_TOOL_NAME)).toBeDefined();
});
@@ -41,7 +44,8 @@ describe('Config Tracker Feature Flag', () => {
tracker: false,
});
await config.initialize();
const registry = config.getToolRegistry();
const loopContext: AgentLoopContext = config;
const registry = loopContext.toolRegistry;
expect(registry.getTool(TRACKER_CREATE_TASK_TOOL_NAME)).toBeUndefined();
});
});