#!/bin/bash
# H3-fast (19105) launcher - direct shell, written 2026-11-01 by Muse subagent
# ComfyUI on 8188, web app on 8082. ComfyUI output/input dirs are pointed at
# the web app's generated/comfy_input dirs so SaveVideo/LoadImage line up.
# Triton needs a C compiler + Python headers; apt packages do NOT survive a
# container recreate, so reinstall them if missing (sudo is passwordless here).
cd /home/agent/.claude/workspace/project
mkdir -p generated comfy_input comfy_temp data uploads logs
if ! command -v gcc >/dev/null 2>&1 || [ ! -f /usr/include/python3.11/Python.h ]; then
  sudo apt-get update && sudo apt-get install -y gcc python3-dev >> logs/apt_gcc.log 2>&1 || true
fi
pkill -f "ComfyUI/main.py" 2>/dev/null || true
pkill -f "app.py" 2>/dev/null || true
sleep 2
nohup venv/bin/python ComfyUI/main.py --listen 127.0.0.1 --port 8188 --cuda-device 0 --disable-pinned-memory --input-directory "$PWD/comfy_input" --output-directory "$PWD/generated" > logs/comfyui.log 2>&1 &
echo "comfyui pid $!" >> logs/start.log
for i in $(seq 1 120); do
  if curl -s -m 2 http://127.0.0.1:8188/ >/dev/null 2>&1; then echo "comfyui up after ${i}x5s" >> logs/start.log; break; fi
  sleep 5
done
nohup venv/bin/python app.py > logs/app.log 2>&1 &
echo "app pid $!" >> logs/start.log
sleep 3
curl -s -m 5 http://127.0.0.1:8082/health || echo "app health check failed"
