Files
splinter/tests/test_protocol.sh
T
lotherk 5143fcd054 gateway, sessions, reconnect: splinter serves other harnesses and keeps tabs
- --openai-listen: an OpenAI-compatible /v1/chat/completions and /v1/models.
  The model gets splinter's local tools as gw_* (executed here) and the
  caller's tools under their own names (returned as tool_calls to run there).
  Local calls loop inside one request; a mixed reply runs the local calls and
  asks again. --autonomous gates mutation; beyond loopback a bearer token is
  required; stream:true answers with SSE.
- --serve keeps several sessions: sessions/open over the protocol and
  /sessions, /open in the attach screen. The server owns the engines, closes
  the old one before opening the next (plugins keep globals) and sends
  state/history after every switch. Session ids are unique per process, so two
  engines in the same second no longer share a file.
- --attach reconnects: when the server goes away the screen keeps running,
  retries every second, asks for its session by id and restores the
  transcript; a missing session falls back to the server's current one.
- transports own their context now: provider switches and close dispose it
  (a switch used to leak one per change). The idle screen repaints entries it
  drains, so a reconnect note is not invisible.
- docs/GATEWAY.md; PROTOCOL, FRONTENDS, README and the manual page describe
  the three; tests cover the gateway tools, sessions/open and a server
  restart under --attach.
2026-11-16 09:33:37 +00:00

329 lines
10 KiB
Bash
Executable File

#!/bin/sh
#
# splinter -- the line protocol: a served engine, driven without a screen.
#
# What it guards: the greeting names the protocol version and the session; a
# prompt produces submitted/stream/tool/tool_result/turn; a slash command
# answers with a reply and does not start a turn; an approval is a question the
# client answers (yes runs the tool, no denies it and nothing is written); the
# reference client speaks it; quit closes the server.
set -u
cd "$(dirname "$0")/.." || exit 1
D=$(mktemp -d "${TMPDIR:-/var/tmp}/splinter-proto-XXXXXX") || exit 1
P1=$((20000 + $$ % 20000))
P2=$((40000 + $$ % 20000))
F1=
F2=
S1=
S2=
fails=0
cleanup() {
[ -n "$S1" ] && kill "$S1" 2>/dev/null
[ -n "$S2" ] && kill "$S2" 2>/dev/null
[ -n "$F1" ] && kill "$F1" 2>/dev/null
[ -n "$F2" ] && kill "$F2" 2>/dev/null
rm -rf "$D"
}
trap cleanup EXIT
check() { if "$@"; then :; else echo "FAIL $*"; fails=$((fails + 1)); fi; }
checkgrep() { # pattern file
if grep -q -- "$1" "$2" 2>/dev/null; then :; else
echo "FAIL missing '$1' in $2"
[ -f "$2" ] && cat "$2"
fails=$((fails + 1))
fi
}
wait_for() { # path, tries
i=0
while [ "$i" -lt "$2" ]; do
[ -e "$1" ] && return 0
i=$((i + 1))
sleep 0.2
done
return 1
}
mkdir -p "$D/home"
python3 -I tests/fake_telegram.py "$P1" "$D/out.txt" "$D/sent.log" ok \
> "$D/fake.out" 2>&1 &
F1=$!
sleep 0.8
HOME=$D/home SPLINTER_LOCALE=en ./splinter --no-config --autonomous \
--endpoint "http://127.0.0.1:$P1/v1/chat/completions" --model m \
--serve --socket "$D/sock" > "$D/serve.out" 2>&1 &
S1=$!
if ! wait_for "$D/sock" 20; then
echo "FAIL the server never created its socket"
cat "$D/serve.out"
exit 1
fi
# a second server on the same socket must refuse to start
HOME=$D/home SPLINTER_LOCALE=en ./splinter --no-config --autonomous \
--endpoint "http://127.0.0.1:$P1/v1/chat/completions" --model m \
--serve --socket "$D/sock" > "$D/second.out" 2>&1
if [ $? -eq 0 ]; then
echo "FAIL a second server on the same socket started"
fails=$((fails + 1))
fi
checkgrep 'already being served' "$D/second.out"
HOME=$D/home SPLINTER_LOCALE=en ./splinterctl --socket "$D/sock" \
--cmd /status > "$D/alive.out" 2>/dev/null
checkgrep 'session' "$D/alive.out"
# ---------------------------------------------- the events, as JSON lines
# The first turn of this conversation: the fake asks for a tool, runs it and
# answers. This is the shape a GUI draws.
python3 - "$D/sock" > "$D/events.out" 2> "$D/events.err" <<'PY'
import json, socket, sys
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.connect(sys.argv[1])
f = s.makefile("rw", encoding="utf-8")
seen = []
sessA = ""
while "state" not in seen:
ev = json.loads(f.readline())
seen.append(ev.get("ev"))
if ev.get("ev") == "state":
sessA = ev.get("session", "")
if ev.get("ev") == "ready" and ev.get("protocol") != 2:
print("bad protocol", ev, file=sys.stderr)
sys.exit(1)
if ev.get("ev") == "state" and not ev.get("session"):
print("state without a session", ev, file=sys.stderr)
sys.exit(1)
f.write(json.dumps({"cmd": "commands"}) + "\n")
f.flush()
while True:
ev = json.loads(f.readline())
if ev.get("ev") == "commands":
if "model" not in ev.get("names", []) or "status" not in ev.get("names", []):
print("commands incomplete", ev, file=sys.stderr)
sys.exit(1)
seen.append("commands")
break
f.write(json.dumps({"cmd": "say", "text": "hallo"}) + "\n")
f.flush()
while True:
ev = json.loads(f.readline())
seen.append(ev.get("ev"))
if ev.get("ev") == "turn":
if not ev.get("ok"):
print("turn failed", ev, file=sys.stderr)
sys.exit(1)
answer = ev.get("answer", "")
break
print("events " + " ".join(seen))
print("answer " + answer)
f.write(json.dumps({"cmd": "history"}) + "\n")
f.flush()
while True:
ev = json.loads(f.readline())
if ev.get("ev") == "history":
texts = [it.get("text", "") for it in ev.get("items", [])]
print("history_ok" if "hallo" in texts else "history_missing")
break
# a shell entry bigger than any fixed 8 KiB line buffer: the client must
# receive the whole JSON line, not a truncated one it cannot parse
f.write(json.dumps({"cmd": "input", "line": "!seq 1 3000"}) + "\n")
f.flush()
big = 0
while True:
ev = json.loads(f.readline())
if ev.get("ev") == "entry" and ev.get("role") == "shell":
big = len(ev.get("text", ""))
if ev.get("ev") == "reply":
break
print("shell_bytes %d" % big)
# and a prompt longer than 8 KiB
f.write(json.dumps({"cmd": "say", "text": "x" * 9000}) + "\n")
f.flush()
while True:
ev = json.loads(f.readline())
if ev.get("ev") == "turn":
print("long_prompt_ok" if ev.get("ok") else "long_prompt_failed")
break
# ---- sessions: list, create, switch back, a missing id is an error ----
f.write(json.dumps({"cmd": "sessions"}) + "\n")
f.flush()
while True:
ev = json.loads(f.readline())
if ev.get("ev") == "sessions":
cur = [i.get("id") for i in ev.get("items", []) if i.get("current")]
print("sessions_ok" if cur == [sessA] else "sessions_bad")
break
f.write(json.dumps({"cmd": "open", "session": "new"}) + "\n")
f.flush()
sessB = ""
while True:
ev = json.loads(f.readline())
if ev.get("ev") == "opened":
sessB = ev.get("session", "")
break
print("open_new_ok" if sessB and sessB != sessA else "open_new_bad")
f.write(json.dumps({"cmd": "sessions"}) + "\n")
f.flush()
while True:
ev = json.loads(f.readline())
if ev.get("ev") == "sessions":
ids = [i.get("id") for i in ev.get("items", [])]
cur = [i.get("id") for i in ev.get("items", []) if i.get("current")]
print("sessions_two_ok" if sessA in ids and sessB in ids and
cur == [sessB] else "sessions_two_bad")
break
f.write(json.dumps({"cmd": "open", "session": sessA}) + "\n")
f.flush()
while True:
ev = json.loads(f.readline())
if ev.get("ev") == "opened":
print("open_back_ok" if ev.get("session") == sessA else
"open_back_bad")
break
f.write(json.dumps({"cmd": "history"}) + "\n")
f.flush()
while True:
ev = json.loads(f.readline())
if ev.get("ev") == "history":
texts = [it.get("text", "") for it in ev.get("items", [])]
print("history_restored" if "hallo" in texts else
"history_lost")
break
f.write(json.dumps({"cmd": "open", "session": "does-not-exist"}) + "\n")
f.flush()
ev = json.loads(f.readline())
print("open_missing" if ev.get("ev") == "error" else "open_missing_bad")
f.write(json.dumps({"cmd": "state"}) + "\n")
f.flush()
while True:
ev = json.loads(f.readline())
if ev.get("ev") == "state":
print("still_a" if ev.get("session") == sessA else "not_a")
break
PY
checkgrep 'ready' "$D/events.out"
checkgrep 'state' "$D/events.out"
checkgrep 'submitted' "$D/events.out"
checkgrep 'tool ' "$D/events.out"
checkgrep 'tool_result' "$D/events.out"
checkgrep 'turn' "$D/events.out"
checkgrep 'commands' "$D/events.out"
checkgrep 'shell_bytes [0-9]\{4,\}' "$D/events.out"
checkgrep 'long_prompt_ok' "$D/events.out"
checkgrep 'history_ok' "$D/events.out"
checkgrep 'sessions_ok' "$D/events.out"
checkgrep 'open_new_ok' "$D/events.out"
checkgrep 'sessions_two_ok' "$D/events.out"
checkgrep 'open_back_ok' "$D/events.out"
checkgrep 'history_restored' "$D/events.out"
checkgrep 'open_missing$' "$D/events.out"
checkgrep 'still_a' "$D/events.out"
checkgrep 'answer fertig' "$D/events.out"
check grep -q 'vom gateway' "$D/out.txt"
# --------------------------------------------------- the reference client
HOME=$D/home SPLINTER_LOCALE=en ./splinterctl --socket "$D/sock" \
--say "hallo" > "$D/say.out" 2> "$D/say.err"
check grep -q 'fertig' "$D/say.out"
HOME=$D/home SPLINTER_LOCALE=en ./splinterctl --socket "$D/sock" \
--cmd /status > "$D/status.out" 2>/dev/null
checkgrep 'session' "$D/status.out"
checkgrep 'model m' "$D/status.out"
HOME=$D/home SPLINTER_LOCALE=en ./splinterctl --socket "$D/sock" \
--cmd /model > "$D/model.out" 2>/dev/null
checkgrep 'main m' "$D/model.out"
# ------------------------------------------------- an approval is a question
[ -n "$S1" ] && kill "$S1" 2>/dev/null
S1=
sleep 0.3
rm -f "$D/sock"
mkdir -p "$D/home2"
python3 -I tests/fake_telegram.py "$P2" "$D/denied.txt" "$D/sent2.log" ok \
> "$D/fake2.out" 2>&1 &
F2=$!
sleep 0.8
HOME=$D/home2 SPLINTER_LOCALE=en ./splinter --no-config \
--endpoint "http://127.0.0.1:$P2/v1/chat/completions" --model m \
--serve --socket "$D/sock2" > "$D/serve2.out" 2>&1 &
S2=$!
if ! wait_for "$D/sock2" 20; then
echo "FAIL the second server never created its socket"
cat "$D/serve2.out"
exit 1
fi
python3 - "$D/sock2" > "$D/deny.out" 2> "$D/deny.err" <<'PY'
import json, socket, sys
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.connect(sys.argv[1])
f = s.makefile("rw", encoding="utf-8")
while True:
ev = json.loads(f.readline())
if ev.get("ev") == "state":
break
f.write(json.dumps({"cmd": "say", "text": "schreib"}) + "\n")
f.flush()
asked = False
while True:
ev = json.loads(f.readline())
if ev.get("ev") == "approval":
asked = True
f.write(json.dumps({"cmd": "approve", "allow": False}) + "\n")
f.flush()
if ev.get("ev") == "turn":
print("asked" if asked else "no question")
print("ok" if ev.get("ok") else "failed")
break
PY
checkgrep 'asked' "$D/deny.out"
checkgrep 'ok' "$D/deny.out"
# the denial is the observation: the tool never ran
check test ! -e "$D/denied.txt"
# ------------------------------------------------------------------- quit
python3 - "$D/sock2" <<'PY'
import json, socket, sys
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.connect(sys.argv[1])
f = s.makefile("rw", encoding="utf-8")
while True:
ev = json.loads(f.readline())
if ev.get("ev") == "state":
break
f.write(json.dumps({"cmd": "quit"}) + "\n")
f.flush()
for line in f:
ev = json.loads(line)
if ev.get("ev") == "quit":
sys.exit(0)
sys.exit(1)
PY
if [ $? -ne 0 ]; then
echo "FAIL quit did not answer with a quit event"
fails=$((fails + 1))
fi
i=0
while kill -0 "$S2" 2>/dev/null && [ "$i" -lt 25 ]; do
i=$((i + 1))
sleep 0.2
done
check test ! -S "$D/sock2"
if [ "$fails" -eq 0 ]; then
echo "test_protocol: ok"
exit 0
fi
exit 1