start-server.sh 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #!/usr/bin/env bash
  2. # Start the brainstorm server and output connection info
  3. # Usage: start-server.sh [--project-dir <path>] [--host <bind-host>] [--url-host <display-host>] [--foreground] [--background]
  4. #
  5. # Starts server on a random high port, outputs JSON with URL.
  6. # Each session gets its own directory to avoid conflicts.
  7. #
  8. # Options:
  9. # --project-dir <path> Store session files under <path>/.superpowers/brainstorm/
  10. # instead of /tmp. Files persist after server stops.
  11. # --host <bind-host> Host/interface to bind (default: 127.0.0.1).
  12. # Use 0.0.0.0 in remote/containerized environments.
  13. # --url-host <host> Hostname shown in returned URL JSON.
  14. # --idle-timeout-minutes <n> Shut down after n minutes idle (default 240 = 4h).
  15. # --open Auto-open the browser on the first screen (use only
  16. # after the user approves the visual companion).
  17. # --foreground Run server in the current terminal (no backgrounding).
  18. # --background Force background mode (overrides Codex auto-foreground).
  19. SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
  20. # Parse arguments
  21. PROJECT_DIR=""
  22. FOREGROUND="false"
  23. FORCE_BACKGROUND="false"
  24. BIND_HOST="127.0.0.1"
  25. URL_HOST=""
  26. IDLE_TIMEOUT_MINUTES=""
  27. while [[ $# -gt 0 ]]; do
  28. case "$1" in
  29. --project-dir)
  30. PROJECT_DIR="$2"
  31. shift 2
  32. ;;
  33. --host)
  34. BIND_HOST="$2"
  35. shift 2
  36. ;;
  37. --url-host)
  38. URL_HOST="$2"
  39. shift 2
  40. ;;
  41. --idle-timeout-minutes)
  42. IDLE_TIMEOUT_MINUTES="$2"
  43. shift 2
  44. ;;
  45. --open)
  46. export BRAINSTORM_OPEN=1
  47. shift
  48. ;;
  49. --foreground|--no-daemon)
  50. FOREGROUND="true"
  51. shift
  52. ;;
  53. --background|--daemon)
  54. FORCE_BACKGROUND="true"
  55. shift
  56. ;;
  57. *)
  58. echo "{\"error\": \"Unknown argument: $1\"}"
  59. exit 1
  60. ;;
  61. esac
  62. done
  63. if [[ -z "$URL_HOST" ]]; then
  64. if [[ "$BIND_HOST" == "127.0.0.1" || "$BIND_HOST" == "localhost" ]]; then
  65. URL_HOST="localhost"
  66. else
  67. URL_HOST="$BIND_HOST"
  68. fi
  69. fi
  70. if [[ -n "$IDLE_TIMEOUT_MINUTES" ]]; then
  71. if ! [[ "$IDLE_TIMEOUT_MINUTES" =~ ^[0-9]+$ ]] || [[ "$IDLE_TIMEOUT_MINUTES" -lt 1 ]]; then
  72. echo "{\"error\": \"--idle-timeout-minutes must be a positive integer\"}"
  73. exit 1
  74. fi
  75. export BRAINSTORM_IDLE_TIMEOUT_MS=$(( IDLE_TIMEOUT_MINUTES * 60 * 1000 ))
  76. fi
  77. # Some environments reap detached/background processes. Auto-foreground when detected.
  78. if [[ -n "${CODEX_CI:-}" && "$FOREGROUND" != "true" && "$FORCE_BACKGROUND" != "true" ]]; then
  79. FOREGROUND="true"
  80. fi
  81. # Windows/Git Bash reaps nohup background processes. Auto-foreground when detected.
  82. if [[ "$FOREGROUND" != "true" && "$FORCE_BACKGROUND" != "true" ]]; then
  83. case "${OSTYPE:-}" in
  84. msys*|cygwin*|mingw*) FOREGROUND="true" ;;
  85. esac
  86. if [[ -n "${MSYSTEM:-}" ]]; then
  87. FOREGROUND="true"
  88. fi
  89. fi
  90. # Generate unique session directory
  91. SESSION_ID="$$-$(date +%s)"
  92. if [[ -n "$PROJECT_DIR" ]]; then
  93. SESSION_DIR="${PROJECT_DIR}/.superpowers/brainstorm/${SESSION_ID}"
  94. # Persist the bound port per project so a restart reuses it and an already-open
  95. # browser tab reconnects to the same URL.
  96. export BRAINSTORM_PORT_FILE="${PROJECT_DIR}/.superpowers/brainstorm/.last-port"
  97. else
  98. SESSION_DIR="/tmp/brainstorm-${SESSION_ID}"
  99. fi
  100. STATE_DIR="${SESSION_DIR}/state"
  101. PID_FILE="${STATE_DIR}/server.pid"
  102. LOG_FILE="${STATE_DIR}/server.log"
  103. # Create fresh session directory with content and state peers
  104. mkdir -p "${SESSION_DIR}/content" "$STATE_DIR"
  105. # Kill any existing server
  106. if [[ -f "$PID_FILE" ]]; then
  107. old_pid=$(cat "$PID_FILE")
  108. kill "$old_pid" 2>/dev/null
  109. rm -f "$PID_FILE"
  110. fi
  111. cd "$SCRIPT_DIR"
  112. # Resolve the harness PID (grandparent of this script).
  113. # $PPID is the ephemeral shell the harness spawned to run us — it dies
  114. # when this script exits. The harness itself is $PPID's parent.
  115. OWNER_PID="$(ps -o ppid= -p "$PPID" 2>/dev/null | tr -d ' ')"
  116. if [[ -z "$OWNER_PID" || "$OWNER_PID" == "1" ]]; then
  117. OWNER_PID="$PPID"
  118. fi
  119. # Windows/MSYS2: Node.js cannot see POSIX PIDs from the MSYS2 namespace.
  120. # Passing a PID node cannot verify causes server to log owner-pid-invalid
  121. # and self-terminate at the 60-second lifecycle check. Clear it so the
  122. # watchdog is disabled and the idle timeout becomes the only shutdown trigger.
  123. case "${OSTYPE:-}" in
  124. msys*|cygwin*|mingw*) OWNER_PID="" ;;
  125. esac
  126. if [[ -n "${MSYSTEM:-}" ]]; then
  127. OWNER_PID=""
  128. fi
  129. # Foreground mode for environments that reap detached/background processes.
  130. if [[ "$FOREGROUND" == "true" ]]; then
  131. env BRAINSTORM_DIR="$SESSION_DIR" BRAINSTORM_HOST="$BIND_HOST" BRAINSTORM_URL_HOST="$URL_HOST" BRAINSTORM_OWNER_PID="$OWNER_PID" node server.cjs &
  132. SERVER_PID=$!
  133. echo "$SERVER_PID" > "$PID_FILE"
  134. wait "$SERVER_PID"
  135. exit $?
  136. fi
  137. # Start server, capturing output to log file
  138. # Use nohup to survive shell exit; disown to remove from job table
  139. nohup env BRAINSTORM_DIR="$SESSION_DIR" BRAINSTORM_HOST="$BIND_HOST" BRAINSTORM_URL_HOST="$URL_HOST" BRAINSTORM_OWNER_PID="$OWNER_PID" node server.cjs > "$LOG_FILE" 2>&1 &
  140. SERVER_PID=$!
  141. disown "$SERVER_PID" 2>/dev/null
  142. echo "$SERVER_PID" > "$PID_FILE"
  143. # Wait for server-started message (check log file)
  144. for i in {1..50}; do
  145. if grep -q "server-started" "$LOG_FILE" 2>/dev/null; then
  146. # Verify server is still alive after a short window (catches process reapers)
  147. alive="true"
  148. for _ in {1..20}; do
  149. if ! kill -0 "$SERVER_PID" 2>/dev/null; then
  150. alive="false"
  151. break
  152. fi
  153. sleep 0.1
  154. done
  155. if [[ "$alive" != "true" ]]; then
  156. echo "{\"error\": \"Server started but was killed. Retry in a persistent terminal with: $SCRIPT_DIR/start-server.sh${PROJECT_DIR:+ --project-dir $PROJECT_DIR} --host $BIND_HOST --url-host $URL_HOST --foreground\"}"
  157. exit 1
  158. fi
  159. grep "server-started" "$LOG_FILE" | head -1
  160. exit 0
  161. fi
  162. sleep 0.1
  163. done
  164. # Timeout - server didn't start
  165. echo '{"error": "Server failed to start within 5 seconds"}'
  166. exit 1