#!/bin/sh # # splinter -- --attach survives a backend restart. # # What it guards: when the served engine dies, the screen keeps running and # says so; a new server on the same socket is found, the session the screen was # on is asked for by id, its history comes back and a new turn works. set -u cd "$(dirname "$0")/.." || exit 1 . "$(dirname "$0")/sh/port.sh" if ! command -v tmux >/dev/null 2>&1; then echo "test_reconnect: skipped (no tmux)" exit 0 fi D=$(mktemp -d "${TMPDIR:-/var/tmp}/splinter-reconnect-XXXXXX") || exit 1 SPLINTER_SESSION_DIR="$D/server/.splinter" export SPLINTER_SESSION_DIR unset TMUX TMUX_PANE export TMUX_TMPDIR="$D" own_server() { if [ -n "${TMUX:-}" ] || [ "${TMUX_TMPDIR:-}" != "$D" ]; then echo "test_reconnect: refusing to touch a tmux server this test does not own" >&2 exit 1 fi } killserver() { own_server; tmux kill-server 2>/dev/null; } S=splinter-reconnect-$$ P=$(free_port) F= G1= G2= G3= fails=0 cleanup() { killserver [ -n "$F" ] && kill "$F" 2>/dev/null [ -n "$G1" ] && kill "$G1" 2>/dev/null [ -n "$G2" ] && kill "$G2" 2>/dev/null [ -n "$G3" ] && kill "$G3" 2>/dev/null rm -rf "$D" } trap cleanup EXIT check() { if "$@"; then :; else echo "FAIL $*"; fails=$((fails + 1)); fi; } shows() { for i in $(seq 1 60); do tmux capture-pane -t $S -p | grep -q -- "$1" && return 0 sleep 0.5 done return 1 } idle_prompt() { for i in $(seq 1 60); do tmux capture-pane -t $S -p | grep -q '^build>' && return 0 sleep 0.5 done return 1 } typed() { tmux send-keys -t $S -l "$1"; } key() { tmux send-keys -t $S "$@"; } start_server() { # log file HOME=$D/server SPLINTER_LOCALE=en ./splinter --no-config --autonomous \ --endpoint "http://127.0.0.1:$P/v1/chat/completions" --model m \ --serve --socket "$D/sock" > "$1" 2>&1 & echo $! } wait_sock() { for i in $(seq 1 60); do [ -S "$D/sock" ] && return 0 sleep 0.2 done return 1 } mkdir -p "$D/server" "$D/client" python3 -I tests/fake_telegram.py "$P" "$D/out.txt" "$D/sent.log" ok \ > "$D/fake.log" 2>&1 & F=$! sleep 0.8 G1=$(start_server "$D/s1.log") if ! wait_sock; then echo "FAIL server 1 has no socket" cat "$D/s1.log" exit 1 fi HOME=$D/server SPLINTER_LOCALE=en ./splinterctl --socket "$D/sock" \ --say "single word ok" > /dev/null 2>&1 sess=$(HOME=$D/server SPLINTER_LOCALE=en ./splinterctl --socket "$D/sock" \ --cmd /status 2>/dev/null | sed -n 's/^ session //p' | head -1) check test -n "$sess" tmux new-session -d -s $S -x 110 -y 30 \ "HOME=$D/client SPLINTER_LOCALE=en ./splinter --no-config \ --attach --socket $D/sock 2>/dev/null; echo \$? > $D/rc" check shows 'Enter sends' check shows "$sess" # the server goes away; the screen must keep running kill "$G1" 2>/dev/null G1= sleep 2 check tmux has-session -t $S check shows 'connection lost' # a new server on the same socket: the screen finds it and restores the session G2=$(start_server "$D/s2.log") if ! wait_sock; then echo "FAIL server 2 has no socket" cat "$D/s2.log" exit 1 fi check shows 'reconnected' check shows "$sess" typed 'single word ok'; key Enter check idle_prompt check shows ' ok' # the session is gone on the next server: the screen says which one and # stays on the session the server does have kill "$G2" 2>/dev/null G2= sleep 2 rm -f "$D/server/.splinter/"*.jsonl "$D/server/.splinter/"*.meta \ "$D/server/.splinter/"*.agent G3=$(start_server "$D/s3.log") if ! wait_sock; then echo "FAIL server 3 has no socket" cat "$D/s3.log" exit 1 fi check shows 'is gone' check shows 'reconnected' check tmux has-session -t $S key C-u C-d for i in $(seq 1 40); do [ -s "$D/rc" ] && break; sleep 0.5; done check test "$(cat "$D/rc" 2>/dev/null)" = 0 check kill -0 "$G3" 2>/dev/null if [ "$fails" -eq 0 ]; then echo "test_reconnect: ok" exit 0 fi exit 1