~ knotless Search/
↑↓ move↵ open⇧↵ all results

chadtmiller.com/3n3htqbv63wba

Clean up after coding agents on macOS: remove finished git worktrees and sweep stale build output (cargo targets, node_modules, DerivedData, /tmp) with two launchd jobs

0
0
main
99 lines · 3.2 KB Raw History
#!/bin/bash
# Reports, or with --delete removes, Cargo target folders nothing has written
# to in SWEEP_DAYS days anywhere under SWEEP_ROOTS. Only build output goes:
# worktrees and source stay.
#
# A folder is kept unless all of these hold:
#   - it is named target and holds Cargo's marker file
#   - git tracks nothing inside it
#   - no process has a file open inside it
#   - nothing in its top three levels changed in SWEEP_DAYS days
#
#   SWEEP_ROOTS  colon-separated dirs to search (default: ~/code and herdr's
#                worktrees; set it to where your repos and worktrees live)
#   SWEEP_DAYS   age in days (default 3)
#
# Run daily by launchd: ~/Library/LaunchAgents/local.sweep-targets.plist
# Log: ~/Library/Logs/sweep-targets.log
set -u

days="${SWEEP_DAYS:-3}"
roots="${SWEEP_ROOTS:-$HOME/code:$HOME/.herdr/worktrees}"

mode=report
case "${1:-}" in
  --delete) mode=delete ;;
  "") ;;
  *) echo "usage: sweep-targets [--delete]" >&2; exit 2 ;;
esac

tmp=$(mktemp -d "${TMPDIR:-/tmp}/sweep-targets.XXXXXX") || exit 1
trap 'rm -rf "$tmp"' EXIT

echo "== $(date '+%Y-%m-%d %H:%M:%S') mode=$mode days=$days"

# lsof exits non-zero on routine warnings, so judge it by what it printed. With
# no list there is no telling what is in use, so nothing is touched.
lsof -Fn 2>/dev/null | grep '^n/' | cut -c2- > "$tmp/open"
if [ ! -s "$tmp/open" ]; then
  echo "aborted: lsof listed no open files"
  exit 1
fi

count=0
total_kb=0
kept=0

IFS=: read -r -a root_list <<< "$roots"
for root in "${root_list[@]}"; do
  [ -d "$root" ] || continue
  # Not walking these is most of the speed: they hold millions of files and
  # no target folder worth sweeping.
  find "$root" \( -name node_modules -o -name .git -o -name .derivedData -o -name DerivedData \) -prune \
    -o -type d -name target -prune -print 2>/dev/null
done > "$tmp/targets"

while IFS= read -r t; do
  [ -e "$t/CACHEDIR.TAG" ] || [ -e "$t/.rustc_info.json" ] || continue

  if grep -qF -- "$t/" "$tmp/open"; then
    kept=$((kept + 1)); continue
  fi
  if [ -n "$(git -C "$(dirname "$t")" ls-files -- target 2>/dev/null | head -1)" ]; then
    echo "kept, tracked by git: $t"
    kept=$((kept + 1)); continue
  fi
  if [ -n "$(find "$t" -maxdepth 3 -mtime "-$days" -print -quit 2>/dev/null)" ]; then
    kept=$((kept + 1)); continue
  fi

  kb=$(du -sk "$t" 2>/dev/null | cut -f1)
  kb=${kb:-0}
  if [ "$mode" = delete ]; then
    # Sizing can take a while: look again in case a build started meanwhile.
    if [ -n "$(find "$t" -maxdepth 3 -mtime "-$days" -print -quit 2>/dev/null)" ]; then
      kept=$((kept + 1)); continue
    fi
    if ! rm -rf "$t"; then
      echo "failed to remove: $t"
      continue
    fi
    verb=removed
  else
    verb="would remove"
  fi
  echo "$verb $((kb / 1024)) MB: $t"
  count=$((count + 1))
  total_kb=$((total_kb + kb))
done < "$tmp/targets"

verb=$([ "$mode" = delete ] && echo removed || echo "would remove")
echo "$verb $count folders, $((total_kb / 1048576)) GB; kept $kept in use or recent"
echo "free: $(df -h "$HOME" | tail -1 | awk '{print $4}')"
echo "== done"

# The log keeps its last 2000 lines.
log="$HOME/Library/Logs/sweep-targets.log"
if [ -f "$log" ] && [ "$(wc -l < "$log")" -gt 4000 ]; then
  tail -n 2000 "$log" > "$tmp/log" && cat "$tmp/log" > "$log"
fi