#!/bin/sh # # splinter -- the fleet socket exists only while a fleet does. # # What it guards: every chat used to bind .splinter-swarm.sock, whether the # operator wanted a fleet or not. The file then sat in a shared working # directory after the session ended, and a later start that found a stale # path stayed on "[swarm locked]" because nobody answered on it. Checked here # against the real binary, through tmux because --chat needs a terminal: # # 1. --chat without --swarm creates no socket file, at all. # 2. --chat --swarm creates one, it answers a second member, and it is gone # again when the session ends -- by Ctrl-D and by SIGTERM, the path that # never reaches sp_engine_close. # 3. two --swarm sessions share one file; it goes when the last one leaves, # not when the master does. set -u cd "$(dirname "$0")/.." || exit 1 ROOT=$(pwd) . "$(dirname "$0")/sh/port.sh" if ! command -v tmux >/dev/null 2>&1; then echo "test_swarm_socket: skipped (no tmux)" exit 0 fi D=$(mktemp -d "${TMPDIR:-/var/tmp}/splinter-swsock-XXXXXX") || exit 1 SPLINTER_SESSION_DIR="$D/home/.splinter" export SPLINTER_SESSION_DIR # Our own tmux server, never the user's: $TMUX wins over $TMUX_TMPDIR, so a run # started inside the user's tmux would drive -- and kill -- that server. unset TMUX TMUX_PANE export TMUX_TMPDIR="$D" own_server() { if [ -n "${TMUX:-}" ] || [ "${TMUX_TMPDIR:-}" != "$D" ]; then echo "test_swarm_socket: refusing to touch a tmux server this test does not own" >&2 exit 1 fi } killserver() { own_server; tmux kill-server 2>/dev/null; } F= fails=0 cleanup() { killserver [ -n "$F" ] && kill "$F" 2>/dev/null rm -rf "$D" } trap cleanup EXIT check() { d=$1; shift; if "$@"; then :; else echo "FAIL $d"; fails=$((fails + 1)); fi; } has_sock() { [ -S "$1/.splinter-swarm.sock" ]; } no_sock() { ! has_sock "$1"; } mkdir -p "$D/home" P=$(free_port) python3 -I tests/fake_openai_echo.py "$P" >"$D/fake.out" 2>&1 & F=$! wait_listen "$P" "$F" || { echo "FAIL fake did not start"; cat "$D/fake.out"; exit 1; } start_chat() { # start_chat [--swarm] S=$1 dir=$2 shift 2 mkdir -p "$dir" # exec: the pane pid is splinter itself, not a shell in front of it -- the # SIGTERM case below has to hit the process that owns the socket tmux new-session -d -s "$S" -x 100 -y 30 \ "cd $dir && HOME=$D/home SPLINTER_LOCALE=en exec $ROOT/splinter \ --no-config --endpoint http://127.0.0.1:$P/v1/chat/completions \ --model m --chat $*" } wait_sock() { # wait_sock : up to 5s for the path to appear i=0 while [ $i -lt 20 ] && ! has_sock "$1"; do sleep 0.25 i=$((i + 1)) done has_sock "$1" } wait_gone() { # wait_gone : up to 5s for the path to disappear i=0 while [ $i -lt 20 ] && has_sock "$1"; do sleep 0.25 i=$((i + 1)) done no_sock "$1" } end_chat() { # end_chat : Ctrl-D, and the session is gone when it exits tmux send-keys -t "$1" C-u C-d 2>/dev/null i=0 while [ $i -lt 40 ] && tmux has-session -t "$1" 2>/dev/null; do sleep 0.25 i=$((i + 1)) done tmux kill-session -t "$1" 2>/dev/null } joined() { # joined : a second member can connect and say hello python3 - "$1/.splinter-swarm.sock" <<'PY' import socket, sys s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) s.settimeout(5) s.connect(sys.argv[1]) s.sendall(b'{"t":"hello","pid":1,"busy":0}\n') s.recv(4096) # the seat list, or at least a close: both mean a live master s.close() PY } # 1. without --swarm: no socket, not while it runs and not after it ends A="$D/plain" start_chat plain "$A" sleep 2 check "a plain --chat created .splinter-swarm.sock" no_sock "$A" end_chat plain check "a plain --chat left .splinter-swarm.sock behind" no_sock "$A" # 2. with --swarm: the file exists while the session runs ... B="$D/fleet" start_chat fleet "$B" --swarm if check "--swarm did not create the socket" wait_sock "$B"; then # ... and it answers: a second member joins and gets an answer check "the socket did not accept a member" joined "$B" fi # ... and it is gone when the session ends, here by Ctrl-D end_chat fleet check "Ctrl-D left .splinter-swarm.sock behind" wait_gone "$B" # ... and by SIGTERM, which never reaches sp_engine_close() start_chat fleet2 "$B" --swarm check "--swarm did not create the socket again" wait_sock "$B" pid=$(tmux display-message -p -t fleet2 '#{pane_pid}') kill -TERM "$pid" 2>/dev/null check "SIGTERM left .splinter-swarm.sock behind" wait_gone "$B" tmux kill-session -t fleet2 2>/dev/null # 3. two members, one file: it goes when the last one leaves C="$D/shared" start_chat m1 "$C" --swarm check "the first member created no socket" wait_sock "$C" start_chat m2 "$C" --swarm sleep 2 check "the second member removed the live socket" has_sock "$C" end_chat m1 sleep 1 check "the master's exit removed a socket another member still needs" \ has_sock "$C" end_chat m2 check "the last member left .splinter-swarm.sock behind" wait_gone "$C" # 4. the master is killed while a member runs: the fleet has to survive it. # The signal handler removes the file (it cannot know about the peer), so # the survivor's takeover is what has to bring the fleet back. K="$D/killed" start_chat k1 "$K" --swarm check "the master created no socket" wait_sock "$K" start_chat k2 "$K" --swarm sleep 2 check "the second member removed the live socket" has_sock "$K" pid=$(tmux display-message -p -t k1 '#{pane_pid}') kill -TERM "$pid" 2>/dev/null sleep 2 check "a killed master took the fleet away from the survivor" wait_sock "$K" check "the survivor's fleet does not answer" joined "$K" end_chat k2 check "the survivor left .splinter-swarm.sock behind" wait_gone "$K" kill "$F" 2>/dev/null if [ "$fails" -eq 0 ]; then echo "test_swarm_socket: ok" exit 0 fi echo "test_swarm_socket: $fails failure(s)" exit 1