#!/usr/bin/env bash # start-whetstone-daemon.sh # # Ensures a single whetstone_mcp HTTP+SSE daemon is running on port 7600. # Safe to call multiple times — exits immediately if already running. # # Usage: # ./tools/start-whetstone-daemon.sh # ./tools/start-whetstone-daemon.sh --port 7601 # ./tools/start-whetstone-daemon.sh --language python # # Output: prints daemon PID on stdout. set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" WHETSTONE_DIR="$(cd "$SCRIPT_DIR/.." && pwd)" BINARY="$WHETSTONE_DIR/editor/build-native/whetstone_mcp" WORKSPACE="$WHETSTONE_DIR" PORT=7600 LANGUAGE="cpp" LOG="$WHETSTONE_DIR/.whetstone/daemon.log" PID_FILE="$WHETSTONE_DIR/.whetstone/daemon.pid" # Parse optional overrides while [[ $# -gt 0 ]]; do case "$1" in --port) PORT="$2"; shift 2 ;; --language) LANGUAGE="$2"; shift 2 ;; *) echo "Unknown arg: $1" >&2; exit 1 ;; esac done # Check if daemon is already running if [[ -f "$PID_FILE" ]]; then EXISTING_PID=$(cat "$PID_FILE" 2>/dev/null || echo "") if [[ -n "$EXISTING_PID" ]] && kill -0 "$EXISTING_PID" 2>/dev/null; then echo "[whetstone-daemon] Already running (PID $EXISTING_PID)" >&2 echo "$EXISTING_PID" exit 0 fi fi # Also check by port if lsof -ti :"$PORT" >/dev/null 2>&1; then echo "[whetstone-daemon] Port $PORT already in use — daemon may be running" >&2 lsof -ti :"$PORT" exit 0 fi if [[ ! -x "$BINARY" ]]; then echo "[whetstone-daemon] Binary not found: $BINARY" >&2 echo "[whetstone-daemon] Build it with: cmake --build editor/build-native --target whetstone_mcp --parallel" >&2 exit 1 fi mkdir -p "$WHETSTONE_DIR/.whetstone" # Start daemon in background nohup "$BINARY" \ --daemon \ --port "$PORT" \ --workspace "$WORKSPACE" \ --language "$LANGUAGE" \ > "$LOG" 2>&1 & DAEMON_PID=$! # Wait briefly for daemon to write its PID file and start listening for i in $(seq 1 20); do sleep 0.25 if curl -sf "http://127.0.0.1:$PORT/health" >/dev/null 2>&1; then echo "[whetstone-daemon] Started (PID $DAEMON_PID, port $PORT)" >&2 echo "$DAEMON_PID" exit 0 fi done echo "[whetstone-daemon] WARNING: daemon started (PID $DAEMON_PID) but /health not yet responding" >&2 echo "$DAEMON_PID"