# Autonomous Daemon Mode

Run Clew as a 24/7 background daemon — task queue, agent loop, health checks, and supervisor auto-respawn for unattended autonomous operation.

The autonomous system lives in `src/services/autonomous/` and consists of four main components: the task queue, agent loop, daemon entry point, and supervisor integration.

## Architecture

      + Task Queue (taskQueue.ts)
      |    File-backed persistent queue
      |    Priorities Leases Dead-letter
      |
      + Agent Loop (agentLoop.ts)
      |    Dequeue Spawn worker Monitor Retry
      |
      + Daemon Mode (daemonMode.ts)
      |    Background process entry point
      |
      + Supervisor (supervisorIntegration.ts)
           Health checks Auto-respawn State tracking

## Task Queue

The file-backed persistent queue (`src/services/autonomous/taskQueue.ts`) is the foundation of the autonomous system:

- Persistence — Tasks survive process restarts via on-disk storage
- Priorities — Urgent tasks skip ahead in the queue
- Leases — Tasks are leased to workers with TTL; expired leases are retried
- Dead-letter — Tasks that exhaust retries are moved to dead-letter for inspection
- Scheduling — One-shot and recurring (cron) tasks supported

## Agent Loop

The continuous agent loop (`src/services/autonomous/agentLoop.ts`) runs in the background:

1. Dequeue — Pull the highest-priority ready task
2. Spawn worker — Launch a worker session for the task
3. Monitor — Track progress, streaming output, and resource usage
4. Retry or complete — On failure, retry with backoff; on success, record result
5. Repeat — Check for new tasks and repeat the cycle

## Daemon Entry Point

`src/services/autonomous/daemonMode.ts` provides the background process entry point. When started in daemon mode, Clew:

- Detaches from the terminal and runs as a background process
- Logs output to a configurable log file
- Responds to signals for graceful shutdown
- Reports status to the supervisor for health tracking

## Supervisor Integration

`src/services/autonomous/supervisorIntegration.ts` ensures the daemon stays running:

- Health checks — Periodic heartbeat and resource checks
- Auto-respawn — Automatic restart on unexpected exit
- State tracking — Current status, running tasks, error counts
- Graceful degradation — Reduces polling frequency on repeated failures

## Commands

Command | Description
--- | ---
`/daemon` | Open interactive control panel; subcommands: start, stop, status, restart
`/task` | Create scheduled or recurring tasks via interactive form
`/task list` | List queued, running, and completed tasks
`/loop` | Run a prompt or command on a recurring interval (`/loop 5m /check-deploy`)
`/agents` | Manage agent configurations and daemon worker pools
`/tasks` | List and manage background agent tasks

## Task Scheduling

Scheduled tasks can be created through the interactive `/task` form or programmatically. Storage modes:

- Durable — Persists to `.claude/scheduled_tasks.json`, survives restarts
- Session-only — Kept in memory for the current session only

Recurring tasks auto-expire after 30 days. One-shot tasks auto-delete after firing. Custom cron expressions are supported (standard 5-field format).

    /task
    Name: Deploy health check
    Schedule: Daily
    Time: 09:00
    Prompt: Check deployment status and report
    Storage: Durable

## Architecture Files

File | Role
--- | ---
`src/services/autonomous/taskQueue.ts` | Persistent task queue with priorities, leases, dead-letter
`src/services/autonomous/agentLoop.ts` | Continuous 24/7 agent loop
`src/services/autonomous/daemonMode.ts` | Background daemon entry point
`src/services/autonomous/supervisorIntegration.ts` | Health checks, auto-respawn, state tracking