--- allowed-tools: - Bash - Read - Write - Glob - Grep argument-hint: "[--last] [--steps ] [--to ] [--dry-run] [--force]" description: "Sistema di rollback con snapshot automatici per annullare modifiche" --- # Toduba Rollback - Sistema di Rollback Intelligente โ†ฉ๏ธ ## Obiettivo Fornire un sistema di rollback sicuro e intelligente per annullare modifiche, con snapshot automatici prima di ogni operazione significativa. ## Argomenti - `--last`: Rollback ultima operazione (default) - `--steps `: Rollback di N operazioni - `--to `: Rollback a specific commit - `--dry-run`: Mostra cosa verrebbe rollbackato senza farlo - `--force`: Skip conferme di sicurezza - `--list`: Mostra snapshot disponibili Argomenti ricevuti: $ARGUMENTS ## Sistema di Snapshot ### Auto-Snapshot Before Changes ```bash # Automaticamente creato da orchestrator prima di modifiche create_snapshot() { local snapshot_id="toduba-$(date +%Y%m%d-%H%M%S)" local snapshot_dir=".toduba/snapshots/$snapshot_id" mkdir -p "$snapshot_dir" # Save current state echo "๐Ÿ“ธ Creating snapshot: $snapshot_id" # 1. Git state git diff > "$snapshot_dir/uncommitted.diff" git status --porcelain > "$snapshot_dir/status.txt" git rev-parse HEAD > "$snapshot_dir/last_commit.txt" # 2. File list find . -type f -not -path "./.git/*" -not -path "./node_modules/*" \ > "$snapshot_dir/files.txt" # 3. Metadata cat > "$snapshot_dir/metadata.json" </dev/null)" # Count changes local added=$(git diff --numstat HEAD "$(cat $snapshot_dir/last_commit.txt)" | wc -l) local modified=$(git status --porcelain | grep "^ M" | wc -l) local deleted=$(git status --porcelain | grep "^ D" | wc -l) echo "" echo "Summary:" echo " โž• Added: $added files" echo " โœ๏ธ Modified: $modified files" echo " โŒ Deleted: $deleted files" if [[ "$ARGUMENTS" == *"--dry-run"* ]]; then echo "" echo "๐Ÿ” DRY RUN MODE - No changes will be made" exit 0 fi } ``` ### Fase 3: Safety Checks ```bash perform_safety_checks() { echo "" echo "๐Ÿ”’ Safety Checks" echo "โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”" # Check 1: Uncommitted changes if [ -n "$(git status --porcelain)" ]; then echo "โš ๏ธ Warning: You have uncommitted changes" if [[ "$ARGUMENTS" != *"--force"* ]]; then read -p "Create backup before rollback? (Y/n): " backup_choice if [ "$backup_choice" != "n" ]; then create_snapshot "Pre-rollback backup" "manual" fi fi fi # Check 2: Running processes if pgrep -f "npm run dev" > /dev/null; then echo "โš ๏ธ Warning: Development server is running" echo " It will be stopped during rollback" fi # Check 3: Database state if [ -f ".toduba/db-version.txt" ]; then echo "โš ๏ธ Warning: Database migrations may need reverting" fi # Final confirmation if [[ "$ARGUMENTS" != *"--force"* ]]; then echo "" echo "โš ๏ธ This action cannot be undone (except by another rollback)" read -p "Proceed with rollback? (y/N): " confirm if [ "$confirm" != "y" ]; then echo "โŒ Rollback cancelled" exit 0 fi fi } ``` ### Fase 4: Execute Rollback ```bash execute_rollback() { echo "" echo "๐Ÿ”„ Executing Rollback" echo "โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”" local snapshot_dir=".toduba/snapshots/$ROLLBACK_TARGET" # Stop any running processes echo "๐Ÿ“ฆ Stopping running processes..." pkill -f "npm run dev" 2>/dev/null || true pkill -f "npm start" 2>/dev/null || true # Git rollback if specified if [ "$git_rollback" = true ]; then echo "๐Ÿ“ Rolling back to commit: $commit" git reset --hard "$commit" else # File system rollback echo "๐Ÿ“ Restoring files from snapshot..." # Create safety backup mv . .toduba/pre_rollback_$(date +%s) 2>/dev/null || true # Extract snapshot tar xzf "$snapshot_dir/backup.tar.gz" -C . # Restore git state if [ -f "$snapshot_dir/uncommitted.diff" ]; then git apply "$snapshot_dir/uncommitted.diff" 2>/dev/null || true fi fi # Post-rollback tasks post_rollback_tasks } post_rollback_tasks() { echo "" echo "๐Ÿ”ง Post-Rollback Tasks" echo "โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”" # Reinstall dependencies if package.json changed if git diff HEAD~1 HEAD --name-only | grep -q "package.json"; then echo "๐Ÿ“ฆ Reinstalling dependencies..." npm install fi # Run migrations if needed if [ -f ".toduba/run-migrations.sh" ]; then echo "๐Ÿ—„๏ธ Running database migrations..." ./.toduba/run-migrations.sh fi # Clear caches echo "๐Ÿงน Clearing caches..." rm -rf .cache/ dist/ build/ 2>/dev/null || true # Rebuild if needed if [ -f "package.json" ] && grep -q '"build"' package.json; then echo "๐Ÿ”จ Rebuilding project..." npm run build fi } ``` ### Fase 5: Rollback Report ```markdown ## ๐Ÿ“‹ Rollback Report **Timestamp**: [DATE TIME] **Rollback Type**: [snapshot/git] **Target**: [SNAPSHOT_ID or COMMIT] ### โœ… Actions Completed - [x] Stopped running processes - [x] Created safety backup - [x] Restored files from snapshot - [x] Applied uncommitted changes - [x] Reinstalled dependencies - [x] Cleared caches - [x] Rebuilt project ### ๐Ÿ“Š Statistics - Files restored: 156 - Dependencies updated: 3 - Cache cleared: 12MB - Time taken: 45 seconds ### โš ๏ธ Manual Actions Required 1. Restart development server: `npm run dev` 2. Check database state if applicable 3. Verify application functionality 4. Review restored code changes ### ๐Ÿ”„ Rollback History ``` toduba-20241031-143022 โ† CURRENT toduba-20241031-140515 toduba-20241031-134208 toduba-20241031-125633 ``` ### ๐Ÿ’ก Next Steps - Test application thoroughly - If issues persist, rollback further: `/toduba-rollback --steps 2` - To undo this rollback: `/toduba-rollback --last` ``` ## Snapshot Management ### List Available Snapshots ```bash list_snapshots() { echo "๐Ÿ“ธ Available Snapshots" echo "โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”" for snapshot in .toduba/snapshots/*/metadata.json; do if [ -f "$snapshot" ]; then local id=$(jq -r '.id' "$snapshot") local time=$(jq -r '.timestamp' "$snapshot") local desc=$(jq -r '.description' "$snapshot") local size=$(du -sh "$(dirname "$snapshot")" | cut -f1) printf "%-25s %s %6s %s\n" "$id" "$time" "$size" "$desc" fi done echo "" echo "Total: $(ls -1 .toduba/snapshots | wc -l) snapshots" echo "Disk usage: $(du -sh .toduba/snapshots | cut -f1)" } ``` ### Auto-Cleanup Old Snapshots ```bash cleanup_old_snapshots() { # Keep only last 20 snapshots or 7 days local max_age=7 # days local max_count=20 echo "๐Ÿงน Cleaning old snapshots..." # Delete by age find .toduba/snapshots -type d -mtime +$max_age -exec rm -rf {} \; # Delete by count ls -t .toduba/snapshots | tail -n +$((max_count + 1)) | \ xargs -I {} rm -rf ".toduba/snapshots/{}" echo "โœ… Cleanup complete" } ``` ## Integration with Orchestrator The orchestrator automatically creates snapshots before: - Major refactoring - Database migrations - Dependency updates - Bulk file operations - Deployment preparations ```javascript // In orchestrator work package if (taskComplexity === 'high' || modifiedFiles > 10) { await createSnapshot(`Pre-${taskName}`, taskName); } ``` ## Error Recovery ```bash handle_rollback_error() { echo "โŒ Rollback failed!" echo "" echo "Emergency recovery options:" echo "1. Check .toduba/pre_rollback_* for backup" echo "2. Use git history: git reflog" echo "3. Restore from .toduba/snapshots manually" echo "4. Contact support with error details" # Save error log echo "$1" > .toduba/rollback_error.log exit 1 } ```