#!/bin/bash set -e ROOT="$(cd "$(dirname "$0")" && pwd)" LOGS="$ROOT/logs" PLIST="$HOME/Library/LaunchAgents/com.aihealer.brain.plist" mkdir -p "$LOGS" echo "🧠 [AI Brain] Manager starting..." # STEP 0: Ensure LaunchAgent exists for auto-start cat > "$PLIST" < Label com.aihealer.brain ProgramArguments $ROOT/brain_manager.sh RunAtLoad StandardOutPath $LOGS/auto_start.log StandardErrorPath $LOGS/auto_start_error.log PLIST launchctl unload "$PLIST" 2>/dev/null || true launchctl load "$PLIST" echo "🔁 [AI Brain] Auto-start installed via LaunchAgent." # STEP 1: Sync repo cd "$ROOT" git pull origin healer-core || true # STEP 2: Launch Healing AI (only if not already running) if pgrep -f "sub_brains/healing_ai/launch.sh" > /dev/null; then echo "⚠️ [AI Brain] Healing AI already running, skipping duplicate." | tee -a "$LOGS/healing_ai.log" else echo "🚀 [AI Brain] Launching Healing AI..." | tee -a "$LOGS/healing_ai.log" nohup bash "$ROOT/sub_brains/healing_ai/launch.sh" >> "$LOGS/healing_ai.log" 2>&1 & fi # STEP 3: Auto log pusher (hourly) ( while true; do cd "$ROOT" TIMESTAMP=$(date +"%Y-%m-%d %H:%M:%S") echo "📤 [AI Brain] Pushing logs at $TIMESTAMP" | tee -a "$LOGS/git_push.log" git add -A git commit -m "📝 Auto-push logs from $HOSTNAME at $TIMESTAMP" || true git pull --rebase origin healer-core || true git push origin healer-core || echo "⚠️ [AI Brain] Git push failed, will retry..." sleep 3600 # wait 1 hour done ) &