#!/bin/zsh

# Process monitoring script to track what kills Next.js dev servers
# Run this in background to capture system events

LOG_FILE="/Users/mattbruce/.openclaw/workspace/logs/process-monitor.log"
PID_FILE="/tmp/process-monitor.pid"

# Create log directory
mkdir -p "$(dirname $LOG_FILE)"

# Function to log with timestamp
log() {
  echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a $LOG_FILE
}

# Track Node processes
monitor_processes() {
  while true; do
    # Check if our monitored processes are running
    for port in 3000 3003 3005; do
      PID=$(lsof -ti:$port 2>/dev/null)
      if [ -n "$PID" ]; then
        # Get process info
        CPU=$(ps -p $PID -o %cpu= 2>/dev/null | tr -d ' ')
        MEM=$(ps -p $PID -o %mem= 2>/dev/null | tr -d ' ')
        RSS=$(ps -p $PID -o rss= 2>/dev/null | tr -d ' ')
        
        # Log if memory is high (>500MB RSS)
        if [ -n "$RSS" ] && [ "$RSS" -gt 512000 ]; then
          log "WARNING: Port $port (PID:$PID) using ${RSS}KB RAM (${MEM}% of system)"
        fi
        
        # Log if CPU is high (>80%)
        if [ -n "$CPU" ] && [ "${CPU%.*}" -gt 80 ]; then
          log "WARNING: Port $port (PID:$PID) using ${CPU}% CPU"
        fi
      fi
    done
    
    # Check system memory
    FREE_MEM=$(vm_stat | grep "Pages free" | awk '{print $3}' | tr -d '.')
    if [ -n "$FREE_MEM" ]; then
      FREE_MB=$((FREE_MEM * 4096 / 1024 / 1024))
      if [ "$FREE_MB" -lt 500 ]; then
        log "WARNING: Low system memory: ${FREE_MB}MB free"
      fi
    fi
    
    # Check file descriptors
    for port in 3000 3003 3005; do
      PID=$(lsof -ti:$port 2>/dev/null)
      if [ -n "$PID" ]; then
        FD_COUNT=$(lsof -p $PID 2>/dev/null | wc -l)
        if [ "$FD_COUNT" -gt 900 ]; then
          log "WARNING: Port $port (PID:$PID) has $FD_COUNT open file descriptors"
        fi
      fi
    done
    
    sleep 60
  done
}

# Check if already running
if [ -f "$PID_FILE" ] && kill -0 $(cat $PID_FILE) 2>/dev/null; then
  echo "Monitor already running (PID: $(cat $PID_FILE))"
  exit 0
fi

# Save PID
echo $$ > $PID_FILE

log "=== Process Monitor Started ==="
log "Monitoring ports: 3000, 3003, 3005"
log "Checking: CPU, Memory, File Descriptors, System Resources"
log "Log file: $LOG_FILE"

# Start monitoring
monitor_processes &
