#!/usr/bin/env bash # DHARMA network — stop all running soul Engrams. # # Primary: reads PIDs from forge/imprints//engram.pid # Fallback: port scan kill (ports 8801-8819) # # Usage: # ./stop_dharma.sh stop all soul Engrams # ./stop_dharma.sh stop one soul by slug set -euo pipefail FORGE=/Users/will/Development/neuron-technologies/forge REGISTRY="$FORGE/registry.json" FILTER="${1:-}" stopped=0 missed=0 if [ ! -f "$REGISTRY" ]; then echo "[dharma] ERROR: registry.json not found at $REGISTRY" exit 1 fi while IFS='|' read -r slug port db_path; do [ -z "$slug" ] && continue if [ -n "$FILTER" ] && [ "$slug" != "$FILTER" ]; then continue fi full_db_path="$FORGE/$db_path" pid_file="$full_db_path/engram.pid" if [ -f "$pid_file" ]; then pid=$(cat "$pid_file") if kill -0 "$pid" 2>/dev/null; then kill "$pid" echo "[dharma] stopped $slug (pid=$pid port=$port)" stopped=$((stopped + 1)) else echo "[dharma] $slug (pid=$pid) already gone" fi rm -f "$pid_file" else # Fallback: kill by port pids=$(lsof -ti tcp:"$port" 2>/dev/null || true) if [ -n "$pids" ]; then echo "$pids" | xargs kill 2>/dev/null || true echo "[dharma] stopped $slug by port kill (port=$port)" stopped=$((stopped + 1)) else missed=$((missed + 1)) fi fi done < <(python3 - "$REGISTRY" <<'PYEOF' import json, sys reg = json.load(open(sys.argv[1])) for imp in reg["imprints"]: print(f"{imp['slug']}|{imp['engram_port']}|{imp.get('engram_db_path', 'imprints/' + imp['slug'])}") PYEOF ) echo "" echo "[dharma] stopped=$stopped not_running=$missed"