Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Turn rough ideas into validated designs and specs through structured collaborative dialogue before coding starts.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
start-server.sh
1#!/usr/bin/env bash2# Start the brainstorm server and output connection info3# 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# --foreground Run server in the current terminal (no backgrounding).15# --background Force background mode (overrides Codex auto-foreground).1617SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"1819# Parse arguments20PROJECT_DIR=""21FOREGROUND="false"22FORCE_BACKGROUND="false"23BIND_HOST="127.0.0.1"24URL_HOST=""25while [[ $# -gt 0 ]]; do26case "$1" in27--project-dir)28PROJECT_DIR="$2"29shift 230;;31--host)32BIND_HOST="$2"33shift 234;;35--url-host)36URL_HOST="$2"37shift 238;;39--foreground|--no-daemon)40FOREGROUND="true"41shift42;;43--background|--daemon)44FORCE_BACKGROUND="true"45shift46;;47*)48echo "{\"error\": \"Unknown argument: $1\"}"49exit 150;;51esac52done5354if [[ -z "$URL_HOST" ]]; then55if [[ "$BIND_HOST" == "127.0.0.1" || "$BIND_HOST" == "localhost" ]]; then56URL_HOST="localhost"57else58URL_HOST="$BIND_HOST"59fi60fi6162# Some environments reap detached/background processes. Auto-foreground when detected.63if [[ -n "${CODEX_CI:-}" && "$FOREGROUND" != "true" && "$FORCE_BACKGROUND" != "true" ]]; then64FOREGROUND="true"65fi6667# Windows/Git Bash reaps nohup background processes. Auto-foreground when detected.68if [[ "$FOREGROUND" != "true" && "$FORCE_BACKGROUND" != "true" ]]; then69case "${OSTYPE:-}" in70msys*|cygwin*|mingw*) FOREGROUND="true" ;;71esac72if [[ -n "${MSYSTEM:-}" ]]; then73FOREGROUND="true"74fi75fi7677# Generate unique session directory78SESSION_ID="$$-$(date +%s)"7980if [[ -n "$PROJECT_DIR" ]]; then81SESSION_DIR="${PROJECT_DIR}/.superpowers/brainstorm/${SESSION_ID}"82else83SESSION_DIR="/tmp/brainstorm-${SESSION_ID}"84fi8586STATE_DIR="${SESSION_DIR}/state"87PID_FILE="${STATE_DIR}/server.pid"88LOG_FILE="${STATE_DIR}/server.log"8990# Create fresh session directory with content and state peers91mkdir -p "${SESSION_DIR}/content" "$STATE_DIR"9293# Kill any existing server94if [[ -f "$PID_FILE" ]]; then95old_pid=$(cat "$PID_FILE")96kill "$old_pid" 2>/dev/null97rm -f "$PID_FILE"98fi99100cd "$SCRIPT_DIR"101102# Resolve the harness PID (grandparent of this script).103# $PPID is the ephemeral shell the harness spawned to run us — it dies104# when this script exits. The harness itself is $PPID's parent.105OWNER_PID="$(ps -o ppid= -p "$PPID" 2>/dev/null | tr -d ' ')"106if [[ -z "$OWNER_PID" || "$OWNER_PID" == "1" ]]; then107OWNER_PID="$PPID"108fi109110# Foreground mode for environments that reap detached/background processes.111if [[ "$FOREGROUND" == "true" ]]; then112echo "$$" > "$PID_FILE"113env BRAINSTORM_DIR="$SESSION_DIR" BRAINSTORM_HOST="$BIND_HOST" BRAINSTORM_URL_HOST="$URL_HOST" BRAINSTORM_OWNER_PID="$OWNER_PID" node server.cjs114exit $?115fi116117# Start server, capturing output to log file118# Use nohup to survive shell exit; disown to remove from job table119nohup 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 &120SERVER_PID=$!121disown "$SERVER_PID" 2>/dev/null122echo "$SERVER_PID" > "$PID_FILE"123124# Wait for server-started message (check log file)125for i in {1..50}; do126if grep -q "server-started" "$LOG_FILE" 2>/dev/null; then127# Verify server is still alive after a short window (catches process reapers)128alive="true"129for _ in {1..20}; do130if ! kill -0 "$SERVER_PID" 2>/dev/null; then131alive="false"132break133fi134sleep 0.1135done136if [[ "$alive" != "true" ]]; then137echo "{\"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\"}"138exit 1139fi140grep "server-started" "$LOG_FILE" | head -1141exit 0142fi143sleep 0.1144done145146# Timeout - server didn't start147echo '{"error": "Server failed to start within 5 seconds"}'148exit 1149