src — daemon

Module: src-daemon Cohesion: 0.80 Members: 0

src — daemon

The src/daemon module is the core infrastructure for running the Code Buddy AI assistant as a persistent background process. It provides the necessary components for managing the daemon's lifecycle, scheduling autonomous tasks, monitoring its health, and ensuring session continuity. This module transforms Code Buddy from a command-line utility into a continuously operating AI agent.

Overall Architecture

The daemon module is built around a set of interconnected managers, each responsible for a specific aspect of the daemon's operation. The DaemonManager is the entry point for controlling the daemon process itself (start, stop, restart). Once the daemon process is running, the DaemonLifecycle takes over, orchestrating the startup and shutdown of various internal services like the CronAgentBridge, HeartbeatEngine, HealthMonitor, and DailyResetManager.

The ServiceInstaller provides platform-specific utilities to integrate the daemon as a system service (e.g., launchd on macOS, systemd on Linux), ensuring it starts automatically and persists across reboots.

graph TD
    subgraph Daemon Process
        A[DaemonManager] --> B{DaemonLifecycle};
        B --> C[CronAgentBridge];
        B --> D[HeartbeatEngine];
        B --> E[HealthMonitor];
        B --> F[DailyResetManager];
        B -- Optional Auto-Register --> I[ObserverCoordinator];
    end

    K[ServiceInstaller] -- Installs/Manages --> A;
    A -- Manages PID/Logs --> J[Filesystem];

    C -- Executes Jobs via --> G[CodeBuddyAgent];
    D -- Reviews Checklist via --> G;
    C -- Delivers Results to --> H[ChannelManager];

    style A fill:#f9f,stroke:#333,stroke-width:2px
    style B fill:#bbf,stroke:#333,stroke-width:2px

Key Components

DaemonManager (daemon-manager.ts)

The DaemonManager is the primary interface for controlling the Code Buddy daemon process. It handles the low-level details of starting, stopping, and restarting the daemon, including managing PID files and logging.

Purpose:

Key Methods:

Integration:

DaemonLifecycle (daemon-lifecycle.ts)

The DaemonLifecycle orchestrates the startup and shutdown of the various internal services that comprise the Code Buddy daemon. It ensures services are started and stopped in a defined order and performs periodic health checks.

Purpose:

Key Methods:

Integration:

CronAgentBridge (cron-agent-bridge.ts)

The CronAgentBridge acts as the execution engine for scheduled tasks defined in the CronScheduler. It bridges the gap between a scheduled CronJob and the CodeBuddyAgent responsible for executing AI-driven tasks.

Purpose:

Key Methods:

Integration:

DailyResetManager (daily-reset.ts)

Advanced enterprise architecture for context boundary management, the DailyResetManager automatically resets the conversation context of agent sessions at a configurable time each day.

Purpose:

Key Methods:

Integration:

HeartbeatEngine (heartbeat.ts)

The HeartbeatEngine provides a periodic "wake-up" mechanism for the daemon, similar to Native Engine's heartbeat system. It reads a HEARTBEAT.md checklist and uses a CodeBuddyAgent to review it, prompting the agent to take action if necessary.

Purpose:

Key Methods:

Integration:

HealthMonitor (health-monitor.ts)

The HealthMonitor continuously tracks the daemon's operational health, including system resources (CPU, memory) and the health status of registered services. It can trigger auto-recovery actions if critical thresholds are breached.

Purpose:

Key Methods:

Integration:

ServiceInstaller (service-installer.ts)

The ServiceInstaller provides platform-specific logic to install and uninstall the Code Buddy daemon as a persistent system service. This ensures the daemon starts automatically on system boot and runs continuously in the background.

Purpose:

Key Methods:

Integration:

Singleton Pattern

All major manager classes within the src/daemon module (DaemonManager, DaemonLifecycle, CronAgentBridge, DailyResetManager, HeartbeatEngine, HealthMonitor, ServiceInstaller) implement a singleton pattern. This means that only one instance of each manager can exist at any given time within the application's runtime.

This pattern simplifies global access to these core daemon services and ensures consistent state management.

Integration Points

The src/daemon module is central to Code Buddy's autonomous operation and integrates with several other parts of the codebase:

Usage and Contribution Notes