src — agent
src — agent
The src/agent module is the core intelligence and orchestration layer of CodeBuddy. It encapsulates the logic for interacting with Large Language Models (LLMs), executing tools, managing conversational state, and adapting its behavior based on user input and operational context. This module transforms raw user requests into structured plans, executes them, and provides a rich, interactive experience.
Core Agent Architecture
At the heart of the src/agent module are two key classes: BaseAgent and CodeBuddyAgent.
BaseAgent: This abstract class provides the foundational structure and common functionalities for any agent within the CodeBuddy system. It employs a Facade pattern, delegating responsibilities to specialized internal facades (e.g.,AgentContextFacade,SessionFacade,ModelRoutingFacade,InfrastructureFacade,MessageHistoryManager). This design promotes modularity, testability, and a clear separation of concerns. Subclasses must implement the core interaction methods:processUserMessage,processUserMessageStream, andexecuteTool.
CodeBuddyAgent: This is the primary concrete implementation ofBaseAgentand serves as the central orchestrator for most CodeBuddy interactions. It initializes and coordinates a wide array of managers and services, including the LLM client, tool execution, prompt construction, streaming, and various middleware components. It's responsible for:- Setting up the agent's operational environment (models, cost limits, YOLO mode).
- Managing the conversation history and LLM message context.
- Applying advanced features like skill matching, model routing, and self-healing.
- Handling the full lifecycle of a user request, from parsing to execution and response generation.
The relationship between CodeBuddyAgent and its key collaborators can be visualized as follows:
graph TD
subgraph CodeBuddyAgent
CBA[CodeBuddyAgent]
end
subgraph Core Execution
AE[AgentExecutor]
TH[ToolHandler]
PB[PromptBuilder]
SH[StreamingHandler]
end
subgraph State & Infrastructure
AI[AgentInfrastructure]
AM[AgentModeManager]
CM[ContextManagerV2]
SS[SessionStore]
CT[CostTracker]
end
CBA -- Orchestrates --> AE
CBA -- Builds Prompts --> PB
CBA -- Manages Streaming --> SH
CBA -- Initializes via --> AI
AI -- Provides Managers --> AM
AI -- Provides Managers --> CM
AI -- Provides Managers --> SS
AI -- Provides Managers --> CT
AE -- Calls Tools --> TH
Key Components & Functionality
The src/agent module is composed of several specialized files, each handling a distinct aspect of agent behavior:
1. Agent Lifecycle & State Management
agent-state.ts: This module defines theAgentStateclass, a centralized manager for an agent's runtime configuration and state. It consolidates:AgentConfig: General settings likemaxToolRounds,sessionCostLimit,yoloMode,parallelToolExecution,ragToolSelection, andselfHealing.- Cost Tracking: Integrates with
CostTrackerto monitorsessionCostagainstsessionCostLimit. - Mode Management: Delegates to
AgentModeManagerfor operational mode control. - Security: Integrates
SandboxManagerfor command validation. - Context: Manages
ContextManagerV2for LLM context window. - Persistence: Uses
SessionStorefor saving and loading chat history. - Abort Control: Provides
AbortControllerfor cancelling ongoing operations.
CodeBuddyAgent leverages these underlying managers, often exposed through the BaseAgent's facade methods.
agent-mode.ts: TheAgentModeManagerclass (a singleton accessed viagetAgentModeManager) is responsible for managing the agent's currentOperatingMode(e.g., 'suggest', 'auto-edit', 'full-auto'). It determines tool permissions and can inject mode-specific system prompt additions.
session-store.ts(external, but heavily used): While not directly insrc/agent, theSessionStore(fromsrc/persistence) is crucial for saving and loading chat history, enabling persistent conversations across sessions.AgentStateandBaseAgentinteract with it.
checkpoint-manager.ts(external): TheCheckpointManager(fromsrc/checkpoints) allows the agent to save snapshots of its state, enabling "rewind" functionality for debugging or recovery.BaseAgentexposes methods likecreateCheckpointandrewindToLastCheckpoint.
2. Execution & Interaction Flow
codebuddy-agent.ts(Orchestrator): As described above, this is the central hub. It initializes and wires together all other components. ItsprocessUserMessageStreammethod is the primary entry point for user interaction, handling everything from skill matching and cost prediction to model routing and tool execution.
agent-executor.ts(Execution Engine): TheAgentExecutoris responsible for the core LLM interaction loop. It takes the user message and conversation history, constructs the prompt, calls the LLM, parses the response (for text or tool calls), executes tools viaToolHandler, and manages the overall turn-taking. It also integrates aMiddlewarePipelinefor extensible behavior modification.
tool-handler.ts: This class acts as the central dispatcher for all tool calls requested by the LLM. It:- Manages the registry of available tools (both built-in and plugin-provided).
- Handles tool execution, including
bashcommands, file operations (textEditor,morphEditor), and specialized tools. - Integrates with
CheckpointManagerfor pre/post-tool snapshots. - Leverages
HooksManagerfor lifecycle events around tool execution. - Coordinates with
RepairCoordinatorfor self-healing failed tool commands. - Enforces
SandboxManagerandPermissionModeManagerrules.
streaming/index.ts(Streaming Output): TheStreamingHandlermanages the real-time output from the LLM, processing raw chunks into structuredStreamingChunkobjects and tracking token counts.
prompt-builder.ts(Prompt Construction): ThePromptBuilderdynamically constructs the system prompt and other parts of the LLM input. It incorporates:- Base system instructions.
YOLO_MODEspecific directives.- Memory system context.
- Custom instructions loaded from
.codebuddy/instructions.md. - Context from
RepoProfilerandKnowledgeManager. - Moltbot hooks for intro injection.
execution/tool-selection-strategy.ts: TheToolSelectionStrategy(accessed viagetToolSelectionStrategy) is crucial for optimizing LLM interactions. WhenuseRAGToolSelectionis enabled, it uses Retrieval-Augmented Generation (RAG) to semantically filter the available tools, sending only the most relevant ones to the LLM. This reduces prompt size, improves tool selection accuracy, and saves costs.
execution/repair-coordinator.ts(Self-Healing): TheRepairCoordinatoris responsible for detecting and attempting to fix errors during tool execution, particularly forbashcommands. WhenselfHealingis enabled, it analyzes error output and can generate corrective actions, often by invoking the LLM itself to suggest fixes.
3. Planning & Advanced Modes
architect-mode.ts: This module introduces a structured, two-phase approach for handling complex development tasks:
- Architect Phase (
analyze): An "architect" LLM analyzes the request and generates a detailedArchitectProposal(summary, steps, affected files, risks, estimated changes). - Editor Phase (
implement): An "editor" LLM then executes eachArchitectStepfrom the proposal, potentially in parallel usingbuildExecutionWavesfor steps without dependencies. This mode is ideal for large, multi-file changes.
planner/index.ts(Task Planning):CodeBuddyAgentincludes methods likeneedsPlanningandexecutePlanthat leverage aTaskPlannerandTaskGraphto decompose complex requests into a Directed Acyclic Graph (DAG) of smaller, manageable tasks. ADelegationEnginecan then assign these tasks to appropriate sub-agents.
4. Context & Memory Management
context/context-manager-v2.ts(external): TheContextManagerV2(fromsrc/context) is responsible for managing the LLM's context window. It employs strategies like summarization and compression to keep the conversation within token limits, ensuring the agent retains relevant information over long interactions.BaseAgentexposes methods likegetContextStatsandupdateContextConfig.
memory/index.ts&context/memory-context-builder.ts:- The
EnhancedMemorysystem (accessed viagetEnhancedMemory) provides long-term, cross-session memory capabilities. It storesMemoryEntryobjects (facts, decisions, patterns, errors) and uses embeddings for semantic recall. - The
MemoryContextBuilder(src/agent/context/memory-context-builder.ts) integratesEnhancedMemoryinto the agent's prompt construction. It retrieves relevant memories based on the current query, filters them by importance and type, and formats them for inclusion in the LLM's context.
5. Customization & Extensibility
agent-loader.ts: This module enables the creation of custom agents defined in Markdown files. These files use YAML frontmatter to specify agent configuration (name, description, model, tools,maxTurns,permissionMode) and the Markdown body as thesystemPrompt.loadCustomAgentsandgetCustomAgentallow dynamic loading and retrieval of these custom definitions.
middleware/index.ts(Middleware Pipeline): TheAgentExecutoruses aMiddlewarePipelineto inject custom logic into the agent's processing flow.CodeBuddyAgentregisters several built-in middleware components (e.g.,TurnLimitMiddleware,CostLimitMiddleware,WorkflowGuardMiddleware,ReasoningMiddleware,AutoRepairMiddleware,QualityGateMiddleware) to enforce policies, enhance reasoning, and automate tasks. Developers can add their own middleware to modify agent behavior.
channels/peer-routing.ts(Peer Routing):CodeBuddyAgentsupportsapplyPeerRoutingto dynamically adjust its configuration (model, system prompt, tool constraints) based on routing decisions made by aPeerRouter. This allows for delegating specific tasks to specialized agent configurations.
6. Observability & Debugging
cost-tracker.ts(external): TheCostTracker(fromsrc/utils) monitors the financial cost of LLM interactions.CodeBuddyAgentuses it to tracksessionCostand enforcesessionCostLimit.
analytics/budget-alerts.ts: TheBudgetAlertManagermonitors thesessionCostagainst thesessionCostLimitand emits alerts when thresholds are approached or exceeded.
optimization/model-routing.ts: TheModelRouterintelligently selects the most appropriate LLM model for a given task based on complexity, cost, and capabilities.CodeBuddyAgentcan enableuseModelRoutingto dynamically switch models during a conversation.
cache-trace.ts: This debugging utility, Advanced enterprise architecture for, logs every stage of prompt construction and execution. WhenCACHE_TRACE=trueis set, it provides detailed timing, token counts, and metadata, invaluable for diagnosing cache hit/miss issues and context building problems.
background-tasks.ts: TheBackgroundTaskManager(a singleton accessed viagetBackgroundTaskManager) allows the agent to launch shell commands in the background. It captures their output and status, enabling non-blocking execution of long-running processes.
How to Extend and Contribute
- Create Custom Agents: Define new agents using Markdown files in
.codebuddy/agents/or~/.codebuddy/agents/, leveraging theMarkdownAgentDefinitioninterface andagent-loader.ts. - Add New Tools: Implement new tools and register them with the
ToolHandler. Ensure they integrate with theSandboxManagerfor security. - Develop Custom Middleware: Create new middleware components to inject custom logic into the
AgentExecutor'sMiddlewarePipeline, modifying how the agent processes messages or tool calls. - Enhance Memory & Context: Contribute to the
EnhancedMemorysystem orMemoryContextBuilderto improve how the agent stores, retrieves, and utilizes long-term knowledge. - Improve Planning & Orchestration: Extend the
TaskPlannerorDelegationEngineto handle more complex multi-step or multi-agent workflows. - Debugging: Utilize
CacheTraceby settingCACHE_TRACE=truein your environment variables to gain deep insights into prompt construction and execution flow.
Conclusion
The src/agent module is the sophisticated control center of CodeBuddy, bringing together diverse functionalities to create an intelligent, adaptable, and extensible AI assistant. By understanding its modular design and the roles of its various components, developers can effectively contribute to and extend CodeBuddy's capabilities.