Skill Tracer
Skills › Superpowers › brainstorming › start-server.sh

start-server.sh

209 lines · skills/brainstorming/scripts/start-server.sh at 2ce8aa1

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).
20SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
22# Parse arguments
23PROJECT_DIR=""
24FOREGROUND="false"
25FORCE_BACKGROUND="false"
26BIND_HOST="127.0.0.1"
27URL_HOST=""
28IDLE_TIMEOUT_MINUTES=""
29while [[ $# -gt 0 ]]; do
30 case "$1" in
31 --project-dir)
32 PROJECT_DIR="$2"
33 shift 2
34 ;;
35 --host)
36 BIND_HOST="$2"
37 shift 2
38 ;;
39 --url-host)
40 URL_HOST="$2"
41 shift 2
42 ;;
43 --idle-timeout-minutes)
44 IDLE_TIMEOUT_MINUTES="$2"
45 shift 2
46 ;;
47 --open)
48 export BRAINSTORM_OPEN=1
49 shift
50 ;;
51 --foreground|--no-daemon)
52 FOREGROUND="true"
53 shift
54 ;;
55 --background|--daemon)
56 FORCE_BACKGROUND="true"
57 shift
58 ;;
59 *)
60 echo "{\"error\": \"Unknown argument: $1\"}"
61 exit 1
62 ;;
63 esac
64done
66if [[ -z "$URL_HOST" ]]; then
67 if [[ "$BIND_HOST" == "127.0.0.1" || "$BIND_HOST" == "localhost" ]]; then
68 URL_HOST="localhost"
69 else
70 URL_HOST="$BIND_HOST"
71 fi
72fi
74if [[ -n "$IDLE_TIMEOUT_MINUTES" ]]; then
75 if ! [[ "$IDLE_TIMEOUT_MINUTES" =~ ^[0-9]+$ ]] || [[ "$IDLE_TIMEOUT_MINUTES" -lt 1 ]]; then
76 echo "{\"error\": \"--idle-timeout-minutes must be a positive integer\"}"
77 exit 1
78 fi
79 export BRAINSTORM_IDLE_TIMEOUT_MS=$(( IDLE_TIMEOUT_MINUTES * 60 * 1000 ))
80fi
82is_windows_like_shell() {
83 case "${OSTYPE:-}" in
84 msys*|cygwin*|mingw*) return 0 ;;
85 esac
86 if [[ -n "${MSYSTEM:-}" ]]; then
87 return 0
88 fi
89 local uname_s
90 uname_s="$(uname -s 2>/dev/null || true)"
91 case "$uname_s" in
92 MSYS*|MINGW*|CYGWIN*) return 0 ;;
93 esac
94 return 1
97# Some environments reap detached/background processes. Auto-foreground when detected.
98if [[ -n "${CODEX_CI:-}" && "$FOREGROUND" != "true" && "$FORCE_BACKGROUND" != "true" ]]; then
99 FOREGROUND="true"
100fi
102# Windows/Git Bash reaps nohup background processes. Auto-foreground when detected.
103if [[ "$FOREGROUND" != "true" && "$FORCE_BACKGROUND" != "true" ]]; then
104 if is_windows_like_shell; then
105 FOREGROUND="true"
106 fi
107fi
109# Session files (server.log, server-info, .last-token) embed the session key —
110# keep everything this script and the server create owner-only.
111umask 077
113# Generate unique session directory
114SESSION_ID="$$-$(date +%s)"
116if [[ -n "$PROJECT_DIR" ]]; then
117 SESSION_DIR="${PROJECT_DIR}/.superpowers/brainstorm/${SESSION_ID}"
118 # Persist the bound port and key per project so a restart reuses them and an
119 # already-open browser tab reconnects to the same URL with a valid cookie.
120 export BRAINSTORM_PORT_FILE="${PROJECT_DIR}/.superpowers/brainstorm/.last-port"
121 export BRAINSTORM_TOKEN_FILE="${PROJECT_DIR}/.superpowers/brainstorm/.last-token"
122else
123 SESSION_DIR="/tmp/brainstorm-${SESSION_ID}"
124fi
126STATE_DIR="${SESSION_DIR}/state"
127PID_FILE="${STATE_DIR}/server.pid"
128LOG_FILE="${STATE_DIR}/server.log"
129SERVER_ID_FILE="${STATE_DIR}/server-instance-id"
131# Create fresh session directory with content and state peers
132mkdir -p "${SESSION_DIR}/content" "$STATE_DIR"
134SERVER_ID=""
135if [[ -r /dev/urandom ]]; then
136 SERVER_ID="$(od -An -N24 -tx1 /dev/urandom 2>/dev/null | tr -d ' \n' || true)"
137fi
138if ! [[ "$SERVER_ID" =~ ^[A-Za-z0-9_-]{32,64}$ ]]; then
139 SERVER_ID="$(printf '%08x%08x%08x%08x' "$$" "$(date +%s)" "${RANDOM:-0}" "${RANDOM:-0}")"
140fi
141printf '%s\n' "$SERVER_ID" > "$SERVER_ID_FILE"
142chmod 600 "$SERVER_ID_FILE" 2>/dev/null || true
144# Kill any existing server
145if [[ -f "$PID_FILE" ]]; then
146 old_pid=$(cat "$PID_FILE")
147 kill "$old_pid" 2>/dev/null
148 rm -f "$PID_FILE"
149fi
151cd "$SCRIPT_DIR" || exit 1
153# Resolve the harness PID (grandparent of this script).
154# $PPID is the ephemeral shell the harness spawned to run us — it dies
155# when this script exits. The harness itself is $PPID's parent.
156OWNER_PID="$(ps -o ppid= -p "$PPID" 2>/dev/null | tr -d ' ')"
157if [[ -z "$OWNER_PID" || "$OWNER_PID" == "1" ]]; then
158 OWNER_PID="$PPID"
159fi
161# Windows/MSYS2: Node.js cannot see POSIX PIDs from the MSYS2 namespace.
162# Passing a PID node cannot verify causes server to log owner-pid-invalid
163# and self-terminate at the 60-second lifecycle check. Clear it so the
164# watchdog is disabled and the idle timeout becomes the only shutdown trigger.
165if is_windows_like_shell; then
166 OWNER_PID=""
167fi
169# Foreground mode for environments that reap detached/background processes.
170if [[ "$FOREGROUND" == "true" ]]; then
171 env BRAINSTORM_DIR="$SESSION_DIR" BRAINSTORM_HOST="$BIND_HOST" BRAINSTORM_URL_HOST="$URL_HOST" BRAINSTORM_OWNER_PID="$OWNER_PID" node server.cjs "--brainstorm-server-id=$SERVER_ID" &
172 SERVER_PID=$!
173 echo "$SERVER_PID" > "$PID_FILE"
174 wait "$SERVER_PID"
175 exit $?
176fi
178# Start server, capturing output to log file
179# Use nohup to survive shell exit; disown to remove from job table
180nohup env BRAINSTORM_DIR="$SESSION_DIR" BRAINSTORM_HOST="$BIND_HOST" BRAINSTORM_URL_HOST="$URL_HOST" BRAINSTORM_OWNER_PID="$OWNER_PID" node server.cjs "--brainstorm-server-id=$SERVER_ID" > "$LOG_FILE" 2>&1 &
181SERVER_PID=$!
182disown "$SERVER_PID" 2>/dev/null
183echo "$SERVER_PID" > "$PID_FILE"
185# Wait for server-started message (check log file)
186for _ in {1..50}; do
187 if grep -q "server-started" "$LOG_FILE" 2>/dev/null; then
188 # Verify server is still alive after a short window (catches process reapers)
189 alive="true"
190 for _ in {1..20}; do
191 if ! kill -0 "$SERVER_PID" 2>/dev/null; then
192 alive="false"
193 break
194 fi
195 sleep 0.1
196 done
197 if [[ "$alive" != "true" ]]; then
198 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\"}"
199 exit 1
200 fi
201 grep "server-started" "$LOG_FILE" | head -1
202 exit 0
203 fi
204 sleep 0.1
205done
207# Timeout - server didn't start
208echo '{"error": "Server failed to start within 5 seconds"}'
209exit 1