cleanup-tmp.sh 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #!/bin/bash
  2. # cleanup-tmp.sh - Clean old temporary files from .tmp directory
  3. # Usage: cleanup-tmp.sh [--force] [--days=N]
  4. set -euo pipefail
  5. # Default settings
  6. FORCE_MODE=false
  7. SESSION_DAYS=7
  8. TASK_DAYS=30
  9. EXTERNAL_DAYS=7
  10. # Parse arguments
  11. for arg in "$@"; do
  12. case $arg in
  13. --force)
  14. FORCE_MODE=true
  15. shift
  16. ;;
  17. --days=*)
  18. CUSTOM_DAYS="${arg#*=}"
  19. SESSION_DAYS=$CUSTOM_DAYS
  20. TASK_DAYS=$CUSTOM_DAYS
  21. EXTERNAL_DAYS=$CUSTOM_DAYS
  22. shift
  23. ;;
  24. --session-days=*)
  25. SESSION_DAYS="${arg#*=}"
  26. shift
  27. ;;
  28. --task-days=*)
  29. TASK_DAYS="${arg#*=}"
  30. shift
  31. ;;
  32. --external-days=*)
  33. EXTERNAL_DAYS="${arg#*=}"
  34. shift
  35. ;;
  36. *)
  37. # Unknown option
  38. ;;
  39. esac
  40. done
  41. # Helper function to get directory size
  42. get_dir_size() {
  43. local dir=$1
  44. if [[ -d "$dir" ]]; then
  45. du -sh "$dir" 2>/dev/null | cut -f1
  46. else
  47. echo "0B"
  48. fi
  49. }
  50. # Helper function to find old directories
  51. find_old_dirs() {
  52. local base_dir=$1
  53. local days=$2
  54. if [[ ! -d "$base_dir" ]]; then
  55. return
  56. fi
  57. find "$base_dir" -mindepth 1 -maxdepth 1 -type d -mtime +$days 2>/dev/null || true
  58. }
  59. # Helper function to find completed tasks older than N days
  60. find_old_completed_tasks() {
  61. local base_dir=$1
  62. local days=$2
  63. if [[ ! -d "$base_dir" ]]; then
  64. return
  65. fi
  66. # Find task directories with completed subtasks
  67. for task_dir in "$base_dir"/*; do
  68. if [[ ! -d "$task_dir" ]]; then
  69. continue
  70. fi
  71. # Check if all subtasks are completed and older than N days
  72. local all_completed=true
  73. local has_subtasks=false
  74. for subtask_file in "$task_dir"/subtask_*.json; do
  75. if [[ ! -f "$subtask_file" ]]; then
  76. continue
  77. fi
  78. has_subtasks=true
  79. # Check if subtask is completed
  80. if ! grep -q '"status": "completed"' "$subtask_file" 2>/dev/null; then
  81. all_completed=false
  82. break
  83. fi
  84. # Check if file is older than N days
  85. if [[ $(find "$subtask_file" -mtime +$days 2>/dev/null | wc -l) -eq 0 ]]; then
  86. all_completed=false
  87. break
  88. fi
  89. done
  90. # If all subtasks are completed and old, include this task directory
  91. if [[ "$has_subtasks" == "true" ]] && [[ "$all_completed" == "true" ]]; then
  92. echo "$task_dir"
  93. fi
  94. done
  95. }
  96. # Collect cleanup candidates
  97. declare -a SESSION_DIRS
  98. declare -a TASK_DIRS
  99. declare -a EXTERNAL_DIRS
  100. # Find old sessions
  101. while IFS= read -r dir; do
  102. SESSION_DIRS+=("$dir")
  103. done < <(find_old_dirs ".tmp/sessions" "$SESSION_DAYS")
  104. # Find old completed tasks
  105. while IFS= read -r dir; do
  106. TASK_DIRS+=("$dir")
  107. done < <(find_old_completed_tasks ".tmp/tasks" "$TASK_DAYS")
  108. # Find old external context
  109. while IFS= read -r dir; do
  110. EXTERNAL_DIRS+=("$dir")
  111. done < <(find_old_dirs ".tmp/external-context" "$EXTERNAL_DAYS")
  112. # Calculate totals
  113. TOTAL_ITEMS=$((${#SESSION_DIRS[@]} + ${#TASK_DIRS[@]} + ${#EXTERNAL_DIRS[@]}))
  114. # If nothing to clean, exit early
  115. if [[ $TOTAL_ITEMS -eq 0 ]]; then
  116. cat <<EOF
  117. {
  118. "status": "success",
  119. "message": "No old temporary files found",
  120. "cleaned": {
  121. "sessions": 0,
  122. "tasks": 0,
  123. "external": 0
  124. },
  125. "totalSize": "0B"
  126. }
  127. EOF
  128. exit 0
  129. fi
  130. # Build cleanup report
  131. CLEANUP_REPORT=""
  132. TOTAL_SIZE=0
  133. if [[ ${#SESSION_DIRS[@]} -gt 0 ]]; then
  134. CLEANUP_REPORT+="## Sessions (older than ${SESSION_DAYS} days)\n\n"
  135. for dir in "${SESSION_DIRS[@]}"; do
  136. size=$(get_dir_size "$dir")
  137. CLEANUP_REPORT+="- $(basename "$dir") - $size\n"
  138. done
  139. CLEANUP_REPORT+="\n"
  140. fi
  141. if [[ ${#TASK_DIRS[@]} -gt 0 ]]; then
  142. CLEANUP_REPORT+="## Completed Tasks (older than ${TASK_DAYS} days)\n\n"
  143. for dir in "${TASK_DIRS[@]}"; do
  144. size=$(get_dir_size "$dir")
  145. CLEANUP_REPORT+="- $(basename "$dir") - $size\n"
  146. done
  147. CLEANUP_REPORT+="\n"
  148. fi
  149. if [[ ${#EXTERNAL_DIRS[@]} -gt 0 ]]; then
  150. CLEANUP_REPORT+="## External Context Cache (older than ${EXTERNAL_DAYS} days)\n\n"
  151. for dir in "${EXTERNAL_DIRS[@]}"; do
  152. size=$(get_dir_size "$dir")
  153. CLEANUP_REPORT+="- $(basename "$dir") - $size\n"
  154. done
  155. CLEANUP_REPORT+="\n"
  156. fi
  157. # If not in force mode, ask for approval
  158. if [[ "$FORCE_MODE" != "true" ]]; then
  159. echo "# Cleanup Report"
  160. echo ""
  161. echo -e "$CLEANUP_REPORT"
  162. echo "Total items to clean: $TOTAL_ITEMS"
  163. echo ""
  164. echo "Run with --force to proceed with cleanup"
  165. echo "Run with --days=N to customize age threshold"
  166. echo ""
  167. cat <<EOF
  168. {
  169. "status": "approval_required",
  170. "message": "Cleanup requires approval. Run with --force to proceed.",
  171. "preview": {
  172. "sessions": ${#SESSION_DIRS[@]},
  173. "tasks": ${#TASK_DIRS[@]},
  174. "external": ${#EXTERNAL_DIRS[@]},
  175. "total": $TOTAL_ITEMS
  176. }
  177. }
  178. EOF
  179. exit 0
  180. fi
  181. # Perform cleanup
  182. CLEANED_SESSIONS=0
  183. CLEANED_TASKS=0
  184. CLEANED_EXTERNAL=0
  185. # Clean sessions
  186. for dir in "${SESSION_DIRS[@]}"; do
  187. if rm -rf "$dir" 2>/dev/null; then
  188. ((CLEANED_SESSIONS++))
  189. fi
  190. done
  191. # Clean tasks
  192. for dir in "${TASK_DIRS[@]}"; do
  193. if rm -rf "$dir" 2>/dev/null; then
  194. ((CLEANED_TASKS++))
  195. fi
  196. done
  197. # Clean external context
  198. for dir in "${EXTERNAL_DIRS[@]}"; do
  199. if rm -rf "$dir" 2>/dev/null; then
  200. ((CLEANED_EXTERNAL++))
  201. fi
  202. done
  203. TOTAL_CLEANED=$((CLEANED_SESSIONS + CLEANED_TASKS + CLEANED_EXTERNAL))
  204. # Output JSON result
  205. cat <<EOF
  206. {
  207. "status": "success",
  208. "message": "Cleanup completed successfully",
  209. "cleaned": {
  210. "sessions": $CLEANED_SESSIONS,
  211. "tasks": $CLEANED_TASKS,
  212. "external": $CLEANED_EXTERNAL,
  213. "total": $TOTAL_CLEANED
  214. },
  215. "thresholds": {
  216. "sessionDays": $SESSION_DAYS,
  217. "taskDays": $TASK_DAYS,
  218. "externalDays": $EXTERNAL_DAYS
  219. }
  220. }
  221. EOF