start-server.sh 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. is_windows_like_shell() {
  78. case "${OSTYPE:-}" in
  79. msys*|cygwin*|mingw*) return 0 ;;
  80. esac
  81. if [[ -n "${MSYSTEM:-}" ]]; then
  82. return 0
  83. fi
  84. local uname_s
  85. uname_s="$(uname -s 2>/dev/null || true)"
  86. case "$uname_s" in
  87. MSYS*|MINGW*|CYGWIN*) return 0 ;;
  88. esac
  89. return 1
  90. }
  91. # Some environments reap detached/background processes. Auto-foreground when detected.
  92. if [[ -n "${CODEX_CI:-}" && "$FOREGROUND" != "true" && "$FORCE_BACKGROUND" != "true" ]]; then
  93. FOREGROUND="true"
  94. fi
  95. # Windows/Git Bash reaps nohup background processes. Auto-foreground when detected.
  96. if [[ "$FOREGROUND" != "true" && "$FORCE_BACKGROUND" != "true" ]]; then
  97. if is_windows_like_shell; then
  98. FOREGROUND="true"
  99. fi
  100. fi
  101. # Session files (server.log, server-info, .last-token) embed the session key —
  102. # keep everything this script and the server create owner-only.
  103. umask 077
  104. # Generate unique session directory
  105. SESSION_ID="$$-$(date +%s)"
  106. if [[ -n "$PROJECT_DIR" ]]; then
  107. SESSION_DIR="${PROJECT_DIR}/.superpowers/brainstorm/${SESSION_ID}"
  108. # Persist the bound port and key per project so a restart reuses them and an
  109. # already-open browser tab reconnects to the same URL with a valid cookie.
  110. export BRAINSTORM_PORT_FILE="${PROJECT_DIR}/.superpowers/brainstorm/.last-port"
  111. export BRAINSTORM_TOKEN_FILE="${PROJECT_DIR}/.superpowers/brainstorm/.last-token"
  112. else
  113. SESSION_DIR="/tmp/brainstorm-${SESSION_ID}"
  114. fi
  115. STATE_DIR="${SESSION_DIR}/state"
  116. PID_FILE="${STATE_DIR}/server.pid"
  117. LOG_FILE="${STATE_DIR}/server.log"
  118. # Create fresh session directory with content and state peers
  119. mkdir -p "${SESSION_DIR}/content" "$STATE_DIR"
  120. # Kill any existing server
  121. if [[ -f "$PID_FILE" ]]; then
  122. old_pid=$(cat "$PID_FILE")
  123. kill "$old_pid" 2>/dev/null
  124. rm -f "$PID_FILE"
  125. fi
  126. cd "$SCRIPT_DIR" || exit 1
  127. # Resolve the harness PID (grandparent of this script).
  128. # $PPID is the ephemeral shell the harness spawned to run us — it dies
  129. # when this script exits. The harness itself is $PPID's parent.
  130. OWNER_PID="$(ps -o ppid= -p "$PPID" 2>/dev/null | tr -d ' ')"
  131. if [[ -z "$OWNER_PID" || "$OWNER_PID" == "1" ]]; then
  132. OWNER_PID="$PPID"
  133. fi
  134. # Windows/MSYS2: Node.js cannot see POSIX PIDs from the MSYS2 namespace.
  135. # Passing a PID node cannot verify causes server to log owner-pid-invalid
  136. # and self-terminate at the 60-second lifecycle check. Clear it so the
  137. # watchdog is disabled and the idle timeout becomes the only shutdown trigger.
  138. if is_windows_like_shell; then
  139. OWNER_PID=""
  140. fi
  141. # Foreground mode for environments that reap detached/background processes.
  142. if [[ "$FOREGROUND" == "true" ]]; then
  143. env BRAINSTORM_DIR="$SESSION_DIR" BRAINSTORM_HOST="$BIND_HOST" BRAINSTORM_URL_HOST="$URL_HOST" BRAINSTORM_OWNER_PID="$OWNER_PID" node server.cjs &
  144. SERVER_PID=$!
  145. echo "$SERVER_PID" > "$PID_FILE"
  146. wait "$SERVER_PID"
  147. exit $?
  148. fi
  149. # Start server, capturing output to log file
  150. # Use nohup to survive shell exit; disown to remove from job table
  151. 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 &
  152. SERVER_PID=$!
  153. disown "$SERVER_PID" 2>/dev/null
  154. echo "$SERVER_PID" > "$PID_FILE"
  155. # Wait for server-started message (check log file)
  156. for _ in {1..50}; do
  157. if grep -q "server-started" "$LOG_FILE" 2>/dev/null; then
  158. # Verify server is still alive after a short window (catches process reapers)
  159. alive="true"
  160. for _ in {1..20}; do
  161. if ! kill -0 "$SERVER_PID" 2>/dev/null; then
  162. alive="false"
  163. break
  164. fi
  165. sleep 0.1
  166. done
  167. if [[ "$alive" != "true" ]]; then
  168. 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\"}"
  169. exit 1
  170. fi
  171. grep "server-started" "$LOG_FILE" | head -1
  172. exit 0
  173. fi
  174. sleep 0.1
  175. done
  176. # Timeout - server didn't start
  177. echo '{"error": "Server failed to start within 5 seconds"}'
  178. exit 1