Module daemon

Module daemon 

Source
Expand description

Background daemon service for automatic Claude Code credential synchronization.

This module provides a long-running daemon service that monitors Claude Code credentials for changes and automatically synchronizes them to configured GitHub targets. The daemon runs as a systemd user service and provides intelligent monitoring, scheduling, and notification features.

§Core Features

  • Automatic Monitoring: Watches for credential changes and expiration
  • Smart Scheduling: Syncs immediately after token refresh with configurable delays
  • Session Warnings: Desktop notifications before session expiry
  • Error Recovery: Robust error handling with failure notifications
  • Signal Handling: Graceful shutdown on SIGINT/SIGTERM
  • Startup Recovery: Reconciliation check on daemon startup

§Daemon Lifecycle

  1. Startup: Perform initial sync check and reconciliation
  2. Monitoring Loop: Check credentials every 5 minutes, session warnings every minute
  3. Token Expiry: Wait for refresh, then sync to all targets
  4. Notifications: Send warnings before expiry, errors on sync failures
  5. Shutdown: Graceful cleanup on shutdown signals

§Usage Examples

§Basic Daemon Usage

use claude_code_toolkit::daemon::Daemon;

#[tokio::main]
async fn main() -> claude_code_toolkit::Result<()> {
    // Initialize daemon with configuration
    let mut daemon = Daemon::new_with_config().await?;
     
    // Start the main daemon loop (runs indefinitely)
    daemon.start().await?;
     
    Ok(())
}

§One-time Check

use claude_code_toolkit::daemon::Daemon;

#[tokio::main]
async fn main() -> claude_code_toolkit::Result<()> {
    let mut daemon = Daemon::new_with_config().await?;
     
    // Run a single sync check without starting the daemon
    daemon.run_once().await?;
     
    Ok(())
}

§Configuration

The daemon reads configuration from ~/.goodiebag/claude-code/config.yml:

daemon:
  log_level: info
  sync_delay_after_expiry: 60  # seconds to wait after token expiry

notifications:
  session_warnings: [30, 15, 5]  # minutes before expiry
  sync_failures: true

§Systemd Integration

The daemon is designed to run as a systemd user service:

[Unit]
Description=Claude Code Credential Sync Daemon

[Service]
Type=simple
ExecStart=/path/to/claude-code-toolkit daemon
Restart=always
RestartSec=10

[Install]
WantedBy=default.target

§Monitoring and Observability

  • Structured Logging: Uses tracing for detailed operation logs
  • Desktop Notifications: Visual feedback for important events
  • Status Tracking: Maintains sync state and error history
  • Health Checks: Validates configuration and connectivity on startup

§Error Handling

The daemon implements comprehensive error handling:

  • Individual sync failures don’t stop the daemon
  • Network issues are retried automatically
  • Configuration errors are logged and reported
  • Service continues running even after transient failures

Structs§

Daemon
Main daemon service for background credential synchronization.