Closed-loop work cycle that helps CC refactor code without rabbit-holing.
# /refactoring-game Execute a game-theoretic refactoring protocol that prevents perfectionism spirals while maintaining code quality. ## Usage ``` /refactoring-game [codebase_path] [ship_deadline] [budget] [max_iterations] [confidence_threshold] ``` ## Arguments - `codebase_path` (required): Path to the codebase to refactor - `ship_deadline` (optional): ISO 8601 deadline (default: 4 hours from now) - `budget` (optional): Energy units for refactoring (default: 100) - `max_iterations` (optional): Maximum refactoring rounds (default: 5) - `confidence_threshold` (optional): Quality threshold 0-1 (default: 0.8) - `comments` (optional): developer comments on the task ## Algorithm ### Phase 0: Initialize Game State ```bash # Create game state tracking mkdir -p .refactoring-game cat > .refactoring-game/state.json << 'EOF' { "round": 0, "budget_remaining": $BUDGET, "improvements": [], "spiral_detections": [], "commitment_level": 0, "players": { "perfectionist": { "satisfaction": 0.0, "weight": 0.8 }, "shipper": { "urgency": 0.5, "weight": 0.9 }, "maintainer": { "debt_concern": 0.7, "weight": 0.7 }, "user": { "patience": 0.9, "weight": 1.0 } } } EOF # Analyze codebase health echo "🎮 Starting Refactoring Game for $CODEBASE_PATH" echo "⏰ Ship deadline: $SHIP_DEADLINE" echo "💰 Budget: $BUDGET energy units" ``` ### Phase 1: Codebase Auction Analysis ```bash # Calculate refactoring bids for each component echo "🔍 Analyzing codebase for refactoring candidates..." # Find all source files and calculate pain metrics find "$CODEBASE_PATH" -type f \( -name "*.js" -o -name "*.ts" -o -name "*.jsx" -o -name "*.tsx" \) | while read -r file; do # Calculate complexity score complexity=$(npx complexity "$file" 2>/dev/null | grep -oE '[0-9]+' | head -1 || echo "10") # Calculate churn (number of commits) churn=$(git log --oneline -- "$file" 2>/dev/null | wc -l) # Search for bug-related commits bugs=$(git log --grep="fix\|bug\|issue" --oneline -- "$file" 2>/dev/null | wc -l) # Calculate bid (willingness to pay for refactoring) bid=$(echo "scale=2; ($complexity * 0.4) + ($churn * 0.3) + ($bugs * 10 * 0.3)" | bc) echo "{\"file\": \"$file\", \"bid\": $bid, \"complexity\": $complexity, \"churn\": $churn, \"bugs\": $bugs}" done | jq -s 'sort_by(.bid) | reverse' > .refactoring-game/auction.json echo "📊 Auction complete. Top candidates identified." ``` ### Phase 2: Main Game Loop ```bash # Game loop with anti-Markov mechanisms while true; do # Load current state STATE=$(cat .refactoring-game/state.json) ROUND=$(echo "$STATE" | jq -r '.round') BUDGET_REMAINING=$(echo "$STATE" | jq -r '.budget_remaining') COMMITMENT_LEVEL=$(echo "$STATE" | jq -r '.commitment_level') echo " 🎲 Round $ROUND | 💰 Budget: $BUDGET_REMAINING | 🔒 Commitment Level: $COMMITMENT_LEVEL" # Check termination conditions if [ "$BUDGET_REMAINING" -le 0 ] || [ "$ROUND" -ge "$MAX_ITERATIONS" ]; then echo "🏁 Game ending: Budget exhausted or iteration limit reached" break fi # Step 2.1: Spiral Detection echo "🌀 Checking for refactoring spirals..." # Analyze recent changes for patterns RECENT_CHANGES=$(echo "$STATE" | jq -r '.improvements[-5:]') # Detect oscillation (A→B→A pattern) if echo "$RECENT_CHANGES" | jq -e 'group_by(.file) | any(length > 2)' >/dev/null 2>&1; then echo "⚠️ Oscillation spiral detected! Breaking loop." echo "$STATE" | jq '.spiral_detections += ["oscillation"]' > .refactoring-game/state.json break fi # Detect scope creep (increasing file count) FILE_COUNTS=$(echo "$RECENT_CHANGES" | jq '[.[].files_touched] | sort') if echo "$FILE_COUNTS" | jq -e '.[0] < .[-1] * 2' >/dev/null 2>&1; then echo "⚠️ Scope creep detected! Applying constraints." COMMITMENT_LEVEL=$((COMMITMENT_LEVEL + 1)) fi # Detect diminishing returns if [ "$ROUND" -gt 3 ]; then AVG_VALUE=$(echo "$RECENT_CHANGES" | jq '[.[].value] | add/length') if (( $(echo "$AVG_VALUE < 0.5" | bc -l) )); then echo "⚠️ Diminishing returns detected. Consider stopping." COMMITMENT_LEVEL=$((COMMITMENT_LEVEL + 1)) fi fi # Step 2.2: Commitment Device Enforcement case $COMMITMENT_LEVEL in 0) echo "📋 No constraints active" ;; 1) echo "⏰ Soft time box: 2 hours remaining" ;; 2) echo "🔢 Hard limit: $(($MAX_ITERATIONS - $ROUND)) iterations left" ;; 3) echo "📁 Scope locked: Only previously touched files" ;; 4) echo "🐛 Feature freeze: Bug fixes only" ;; 5) echo "🚢 FORCED SHIP: Merging current state"; break ;; esac # Step 2.3: Run Refactoring Auction echo "🏦 Running refactoring auction..." # Get top bid that fits budget WINNER=$(cat .refactoring-game/auction.json | jq --arg budget "$BUDGET_REMAINING" ' .[] | select(.bid <= ($budget | tonumber)) | select(.file as $f | [inputs] | map(select(.file == $f)) | length == 0) ' .refactoring-game/state.json | head -1) if