Comprehensive technical debt cleanup orchestrator with parallel analysis and safe automation
<codex_skill_adapter>
$workflow-manage-clean.$workflow-manage-clean as {{SC_ARGS}}.{{SC_ARGS}} as empty.spawn_agent(...) patterns to Codex spawn_agent(...).update_plan.config.toml when the original command mentions MCP.workflow:manage:clean.$workflow-manage-clean.STEP 1: Initialize technical debt cleanup session and project analysis
/tmp/cleanup-session-$SESSION_ID.json# Initialize cleanup session state
echo '{
"sessionId": "'$SESSION_ID'",
"targetProject": "'{{SC_ARGS}}'",
"detectedLanguages": [],
"cleanupStrategy": "auto-detect",
"safetyLevel": "production",
"itemsProcessed": 0,
"linesRemoved": 0
}' > /tmp/cleanup-session-$SESSION_ID.json
STEP 2: Comprehensive technical debt analysis with parallel sub-agent coordination
TRY:
IF codebase_size > 500 files OR project_type == "enterprise":
LAUNCH parallel sub-agents for comprehensive technical debt discovery:
Agent 1: Code Quality Analysis: Identify quality issues and anti-patterns
Agent 2: Dead Code Detection: Find unused and unreachable code
Agent 3: Duplication Analysis: Identify code duplication and consolidation opportunities
Agent 4: Dependencies & Security: Analyze dependency health and security issues
Agent 5: Documentation & Comments: Evaluate documentation quality and relevance
ELSE:
EXECUTE streamlined cleanup analysis for smaller codebases:
# Streamlined analysis for smaller projects
echo "🔍 Analyzing technical debt in smaller codebase..."
STEP 3: Systematic cleanup execution with safety checkpoints
Phase 1: Low-Risk Quality Improvements
FOR EACH low_risk_improvement:
# Create safety checkpoint
echo "📝 Creating safety checkpoint before cleanup phase"
git add -A
git commit -m "checkpoint: before $(date -Iseconds) cleanup session $SESSION_ID" || echo "No changes to commit"
# Apply formatting and linting fixes
echo "🔧 Applying automated code quality improvements..."
Language-Specific Cleanup Patterns:
JavaScript/TypeScript Projects:
# Remove console.log statements (excluding intentional logging)
rg "console\.(log|debug|warn)" --type typescript -l | while read file; do
echo "Cleaning debug statements in: $file"
# Interactive review of each console statement
done
# Convert var to let/const
rg "\bvar\s+" --type typescript -l | while read file; do
echo "Modernizing variable declarations in: $file"
# Apply var → let/const transformations with validation
done
# Clean up unused imports
if command -v deno >/dev/null; then
echo "🦕 Running Deno linting and formatting"
deno fmt {{SC_ARGS}}
deno lint {{SC_ARGS}}
fi
Rust Projects:
# Remove unused imports and dead code warnings
if fd "Cargo.toml" . >/dev/null; then
echo "🦀 Cleaning Rust project with cargo tools"
cargo fmt --all
cargo clippy --all-targets --all-features -- -D warnings
cargo check --all
fi
# Clean up TODO/FIXME comments with context
rg "(TODO|FIXME|XXX|HACK)" --type rust --context 2
Go Projects:
# Apply Go formatting and cleaning
if fd "go.mod" . >/dev/null; then
echo "🐹 Cleaning Go project with standard tools"
go fmt ./...
go vet ./...
go mod tidy
fi
# Remove unused variables and imports
go run golang.org/x/tools/cmd/goimports -w ./... 2>/dev/null || echo "goimports not available"
Java Projects:
# Maven/Gradle cleanup
if fd "pom.xml" . >/dev/null; then
echo "☕ Cleaning Java Maven project"
mvn clean compile
elif fd "build.gradle" . >/dev/null; then
echo "☕ Cleaning Java Gradle project"
./gradlew clean build
fi
Phase 2: Dead Code Removal (Requires Verification)
# Identify potentially unused files
echo "🔍 Identifying potentially unused files..."
fd "\.(js|ts|jsx|tsx|rs|go|java|py)$" . -x bash -c '
file="{}"
basename=$(basename "$file" | sed "s/\.[^.]*$//")
if ! rg -q "$basename" --type-not $(echo "$file" | sed "s/.*\.//") .; then
echo "Potentially unused: $file"
fi
' > /tmp/unused-files-$SESSION_ID.txt
# Review unused files before deletion
if [[ -s "/tmp/unused-files-$SESSION_ID.txt" ]]; then
echo "📋 Found potentially unused files:"
bat /tmp/unused-files-$SESSION_ID.txt
echo "⚠️ Manual review required before deletion"
fi
Phase 3: Documentation and Comment Cleanup
# Find outdated TODO comments (older than 6 months)
echo "📅 Analyzing TODO comment age..."
rg "(TODO|FIXME).*([0-9]{4}-[0-9]{2}-[0-9]{2})" --context 1 > /tmp/dated-todos-$SESSION_ID.txt || echo "No dated TODOs found"
# Find broken documentation links
rg "https?://[^\s)]+" --only-matching | sort -u | while read url; do
echo "📎 Found URL: $url"
# Note: URL validation would require network access
done > /tmp/found-urls-$SESSION_ID.txt
STEP 4: Results compilation and impact assessment
Generate Cleanup Report:
# Compile cleanup statistics
lines_before=$(fd "\.(js|ts|jsx|tsx|rs|go|java|py|rb|php|c|cpp|h|hpp|cs|kt|swift|scala)" . -x wc -l {} \; | awk '{sum += $1} END {print sum}' || echo "0")
echo "📊 Technical Debt Cleanup Report"
echo "================================"
echo "Session ID: $SESSION_ID"
echo "Target: {{SC_ARGS}}"
echo "Timestamp: $(gdate -Iseconds 2>/dev/null || date -Iseconds)"
echo "Total Source Files: $(fd "\.(js|ts|jsx|tsx|rs|go|java|py|rb|php|c|cpp|h|hpp|cs|kt|swift|scala)" . | wc -l | tr -d ' ')"
echo "Lines of Code (before): $lines_before"
# Update session state with final statistics
jq --arg timestamp "$(gdate -Iseconds 2>/dev/null || date -Iseconds)" \
--argjson lines_before "$lines_before" \
'.completedAt = $timestamp | .linesBefore = $lines_before' \
/tmp/cleanup-session-$SESSION_ID.json > /tmp/cleanup-session-$SESSION_ID.tmp && \
mv /tmp/cleanup-session-$SESSION_ID.tmp /tmp/cleanup-session-$SESSION_ID.json
Safety Verification:
# Run tests if test command exists
if command -v deno >/dev/null && fd "deno.json" . >/dev/null; then
echo "🧪 Running Deno tests to verify cleanup safety"
deno task test || echo "⚠️ Tests failed - manual review required"
elif command -v npm >/dev/null && fd "package.json" . >/dev/null; then
echo "🧪 Running npm tests to verify cleanup safety"
npm test || echo "⚠️ Tests failed - manual review required"
elif command -v cargo >/dev/null && fd "Cargo.toml" . >/dev/null; then
echo "🧪 Running Rust tests to verify cleanup safety"
cargo test || echo "⚠️ Tests failed - manual review required"
elif command -v go >/dev/null && fd "go.mod" . >/dev/null; then
echo "🧪 Running Go tests to verify cleanup safety"
go test ./... || echo "⚠️ Tests failed - manual review required"
else
echo "ℹ️ No test framework detected - manual verification recommended"
fi
CATCH (cleanup_failed):
echo "⚠️ Cleanup operation failed. Checking for recovery options:"
echo "Last git checkpoint: $(git log --oneline -1 --grep='checkpoint:' || echo 'No checkpoint found')"
echo "To restore: git reset --hard HEAD~1"
echo "Session state: /tmp/cleanup-session-$SESSION_ID.json"
STEP 5: Session completion and follow-up recommendations
Final Safety Commit:
# Create final commit with all cleanup changes
echo "💾 Creating final cleanup commit"
git add -A
git commit -m "$(cat <<'EOF'