# Autonomous Task Executor

**Location:** `/home/ccuser/rateright-growth/rivet/scripts/task-executor.py`

## Purpose

Autonomously execute safe tasks from TODO.md during heartbeats or overnight work sessions.

## How It Works

1. **Reads TODO.md** - Parses all tasks with their priority levels
2. **Identifies safe tasks** - Filters for autonomous-safe tasks (no external comms, no deploys)
3. **Prioritizes** - Scores tasks: 🔴 Priority (100) > 🔴 Active (50) > 🟡 Queued (10)
4. **Executes** - Spawns sub-agent via `clawdbot agent` to complete the task
5. **Updates progress** - Marks task as `[⏳]` when starting, `[x]` when complete
6. **Logs work** - Appends to `memory/YYYY-MM-DD.md` with timestamps
7. **Handles failures** - Resets task to `[ ]` on failure, moves to next task

## Safety Filters

### ❌ Unsafe (Will NOT Execute)
- send, email, tweet, post, sms
- deploy, push, publish, release
- message, notification, call, text
- live webhooks, production changes
- Social media posting (Instagram, Facebook, TikTok, Twitter, LinkedIn posts)

### ✅ Safe (Will Execute)
- research, design, build, create
- analyze, update, write, refactor
- test, document, implement, plan
- script, review, optimize, fix

## Usage

### Dry Run (See What Would Execute)
```bash
cd /home/ccuser/rateright-growth/rivet
python3 scripts/task-executor.py --dry-run
```

### Execute One Task
```bash
python3 scripts/task-executor.py
```

### Execute Multiple Tasks
```bash
python3 scripts/task-executor.py --max-tasks 3
```

## Integration with Heartbeat

Add to `HEARTBEAT.md`:

```markdown
## Autonomous Task Execution

Every 4th heartbeat (roughly 2x per day):
- Run `/home/ccuser/rateright-growth/rivet/scripts/task-executor.py`
- Execute 1-2 safe tasks from TODO.md
- Log results to daily memory
```

Or create a cron job for overnight execution:

```bash
# Run at 11pm and 3am
0 23,3 * * * cd /home/ccuser/rateright-growth/rivet && python3 scripts/task-executor.py --max-tasks 2
```

## Output

Logs are written to:
- **Console** - Real-time progress
- **memory/YYYY-MM-DD.md** - Persistent daily log

Example log entry:
```
[11:02:51] [INFO] === Autonomous Task Executor Started ===
[11:02:51] [INFO] Found 33 total tasks
[11:02:51] [INFO] Found 6 autonomous-safe tasks
[11:02:51] [INFO] Selected task (priority 100): Design self-improvement system
[11:02:51] [INFO] Executing task: Design self-improvement system
[11:03:45] [INFO] Task completed successfully
[11:03:45] [INFO] ✅ Task completed successfully
```

## Task Format in TODO.md

Tasks must follow this format:

```markdown
## 🔴 Priority: Section Name

- [ ] Incomplete task
- [⏳] In-progress task (executor is working on it)
- [x] Completed task
```

## Limitations

- **One task at a time** - Sequential execution to avoid conflicts
- **5-minute timeout** - Tasks must complete within 5 minutes or they're marked as failed
- **Simple retry** - Failed tasks are reset to `[ ]` for manual retry
- **No complex dependencies** - Doesn't understand task dependencies

## Future Improvements

- [ ] Dependency tracking between tasks
- [ ] Better retry logic with exponential backoff
- [ ] Parallel execution for independent tasks
- [ ] Task complexity estimation
- [ ] Learning from past failures
- [ ] Progress reporting mid-task

## Troubleshooting

**Task stuck in [⏳] state:**
```bash
# Manually reset it
sed -i 's/\[⏳\] Task text/[ ] Task text/' TODO.md
```

**No tasks being selected:**
- Check if tasks match safety filters
- Run `--dry-run` to see eligible tasks
- Ensure TODO.md has `[ ]` incomplete tasks

**Sub-agent failed:**
- Check `memory/YYYY-MM-DD.md` for error details
- Task will be reset to `[ ]` automatically
- Review clawdbot gateway logs: `journalctl --user -u clawdbot-gateway.service -n 100`

## Examples

**Good autonomous-safe tasks:**
- ✅ Research competitor pricing models
- ✅ Update README documentation
- ✅ Refactor database schema
- ✅ Design new UI mockups
- ✅ Analyze user feedback trends

**Bad (will be skipped):**
- ❌ Send email to investors
- ❌ Deploy to production
- ❌ Post on LinkedIn
- ❌ Push code to GitHub
- ❌ Text Michael about meeting
