Files
splinter/tests/test_reconnect.sh
lotherk fa16bfd0a9 foreign: adopt a claude, hermes or opencode session as a splinter session
Another harness's conversation is not a session until splinter can resume
it. plugins/foreign reads the three stores that already exist on the
machine - ~/.claude/projects/<slug>/<id>.jsonl, <root>/state.db (and
profiles/*/state.db) and opencode.db's session_v2/session_message - and
offers four tools plus /foreign:

  list, search TEXT, read SOURCE ID, import SOURCE ID

The stores are read with sqlite3 -readonly: an import copies, it never
moves. Reasoning blocks are dropped on the way in, because a provider does
not accept another model's thinking as evidence. Tool calls arrive with
their results in place, which is what makes the imported session worth
resuming instead of retyping.

import writes a real splinter session: the conversation as a replayable
jsonl, and a .meta with the source's title, its start time and *its*
working directory. So /open continues it in the chat,
--session-id <id> --resume in a shell, and -c finds it in the directory the
conversation actually ran in. The marker foreign=<source>:<id> survives
every later reopen, which needed the core's .meta sidecar to carry keys it
does not own.

Building this found nine bugs, each one seen red before it was green:

  - four use-after-frees: mcp_arg_string returns a pointer *into* the parse
    tree, read after mcp_json_free. That garbage in `only`/`src` was why a
    list of three stores answered "no sessions" and named AÃ_Â¥U.
  - sql_json took "no rows" for a failure (NULL), so every empty answer
    looked like a broken database.
  - args_split never terminated its buffers: /foreign list became
    "unknown subcommand 'list('".
  - path_join(dir, id, ".meta") built dir/id/.meta, so the sidecar landed
    in a subdirectory.
  - claude_slug_dir had the sign inverted and returned -1 for every
    non-empty cwd, so the project directory was never found.
  - sp_session_open overwrote cwd with getcwd() on *every* open, though the
    comment beside it promised the opposite: imports claimed the directory
    they were imported from and -c then picked the wrong session.
  - hermes_text ran strncmp(p, "\x00json:", 6) on an empty content, a
    one-byte allocation - heap-buffer-overflow, found by ASAN. The marker is
    stripped in SQL anyway.
  - hermes_calls never freed the root node on the object path (5456 bytes
    over 42 allocations).
  - list_any reported strerror(errno) with errno never set: a real file with
    the wrong schema read as "No such file or directory". The SQL reason is
    passed through now.

Checked end to end against the stores on this machine: claude 64 sessions,
opencode 25, hermes 0 (both hermes databases here are empty, count(*) = 0).
A real opencode session of 150 messages reads back with its tool output; one
of 75 imports with the right cwd and title, --resume sends its history, and
-c from the source directory opens it.

tests/sh/port.sh comes along: the shell tests guessed a port with
20000 + $$ % 30000 and on 2026-11-21 landed on one dtsession held. The fake
died, curl got an answer from dtsession, and twenty checks blamed the
gateway. They ask the kernel for a free port now.
2026-11-21 21:26:43 +00:00

145 lines
3.7 KiB
Bash
Executable File

#!/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 '<m> 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