start-server.sh 5.6 KB

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