#!/usr/bin/env bash
# Launch both agent workers and the token/creation web server together. If any
# one exits, stop them all so Fly restarts the machine cleanly.
set -uo pipefail

mkdir -p "${R1_DATA_DIR:-/data}"

cd /app/agent

uv run python src/agent.py start &
PID_TUTOR=$!

uv run python src/therapist.py start &
PID_THERAPIST=$!

uv run python /app/web-server.py &
PID_WEB=$!

echo "started: tutor=$PID_TUTOR therapist=$PID_THERAPIST web=$PID_WEB"

wait -n
echo "a process exited — shutting the rest down for a clean restart"
kill "$PID_TUTOR" "$PID_THERAPIST" "$PID_WEB" 2>/dev/null || true
exit 1
