[CI/CD] Automate Cleanup of Stale Agent Worktrees via Cron #1410

Closed
opened 2026-03-24 13:04:22 +00:00 by Timmy · 1 comment
Owner

Context: timmy-loop.sh generates worktrees iteratively. A fatal unhandled exit leaves detached git folders on the host.

Acceptance Criteria:

  • Develop an automated script inside cron/ to sweep ~/worktrees for folders un-modified in the last 12 hours.
  • The script MUST trigger git worktree remove --force and prune references cleanly.
**Context:** `timmy-loop.sh` generates worktrees iteratively. A fatal unhandled exit leaves detached git folders on the host. **Acceptance Criteria:** - Develop an automated script inside `cron/` to sweep `~/worktrees` for folders un-modified in the last 12 hours. - The script MUST trigger `git worktree remove --force` and prune references cleanly.
Author
Owner

Implementation Plan

Automate cleanup of stale agent worktrees via cron to prevent disk space issues.

Files to Create:

  1. scripts/cleanup-worktrees.sh - Main cleanup script
  2. scripts/install-cron.sh - Cron installation helper
  3. src/infrastructure/maintenance/ - Cleanup monitoring module

Implementation Steps:

  1. Identify Stale Worktrees: Check ~/worktrees/ for old directories
  2. Safety Checks: Verify no active processes using worktree
  3. Age-Based Cleanup: Remove worktrees older than 24 hours
  4. Size-Based Cleanup: Remove if disk usage > threshold
  5. Preserve Active Work: Skip worktrees with recent git activity
  6. Add Monitoring: Log cleanup actions and disk savings

Cleanup Logic:

# Find stale worktrees (>24h old, no recent git activity)
find ~/worktrees/ -type d -name 'kimi-*' -mtime +1 -exec test ! -f {}/fresh.lock \; -print

Safety Features:

  • Lock files prevent cleanup of active worktrees
  • Dry-run mode for testing
  • Size limits to prevent accidental cleanup
  • Backup of important data before deletion

Acceptance Criteria:

  • Cron job runs daily at 2 AM
  • Safely identifies and removes stale worktrees
  • Preserves worktrees with active git locks
  • Logs all cleanup actions with disk space saved
  • Configurable retention period (default 24h)
  • Emergency stop mechanism

Installation:

# Install via script
./scripts/install-cron.sh
# Or manually:  
crontab -e  # Add: 0 2 * * * ~/scripts/cleanup-worktrees.sh
## Implementation Plan Automate cleanup of stale agent worktrees via cron to prevent disk space issues. ### Files to Create: 1. `scripts/cleanup-worktrees.sh` - Main cleanup script 2. `scripts/install-cron.sh` - Cron installation helper 3. `src/infrastructure/maintenance/` - Cleanup monitoring module ### Implementation Steps: 1. **Identify Stale Worktrees**: Check `~/worktrees/` for old directories 2. **Safety Checks**: Verify no active processes using worktree 3. **Age-Based Cleanup**: Remove worktrees older than 24 hours 4. **Size-Based Cleanup**: Remove if disk usage > threshold 5. **Preserve Active Work**: Skip worktrees with recent git activity 6. **Add Monitoring**: Log cleanup actions and disk savings ### Cleanup Logic: ```bash # Find stale worktrees (>24h old, no recent git activity) find ~/worktrees/ -type d -name 'kimi-*' -mtime +1 -exec test ! -f {}/fresh.lock \; -print ``` ### Safety Features: - Lock files prevent cleanup of active worktrees - Dry-run mode for testing - Size limits to prevent accidental cleanup - Backup of important data before deletion ### Acceptance Criteria: - [ ] Cron job runs daily at 2 AM - [ ] Safely identifies and removes stale worktrees - [ ] Preserves worktrees with active git locks - [ ] Logs all cleanup actions with disk space saved - [ ] Configurable retention period (default 24h) - [ ] Emergency stop mechanism ### Installation: ```bash # Install via script ./scripts/install-cron.sh # Or manually: crontab -e # Add: 0 2 * * * ~/scripts/cleanup-worktrees.sh ```
kimi was assigned by Timmy 2026-03-24 13:11:39 +00:00
kimi was unassigned by Timmy 2026-03-24 19:32:22 +00:00
Timmy closed this issue 2026-03-24 21:54:11 +00:00
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Rockachopa/Timmy-time-dashboard#1410