Files
splinter/tests/test_protocol.sh
T
lotherk f6211362cb sessions: /sessions is a picker over sources, foreign sessions included
`/sessions` printed a list of ids; the operator then typed the id into
/open. The list was the wrong shape for a thing you pick from, and the
foreign stores (claude, hermes, opencode) were not in it at all - their
sessions were only reachable through the model's import tool.

Now it is a picker in two steps: the sources first (`splinter` and every
foreign store that has something), then the rows of the chosen one. A
foreign row's id is "claude:<id>", which sp_engine_switch() already reads
as an import - so picking it loads and continues that session, exactly
like `/open claude:<id>` typed by hand.

The picker got one shared implementation while it was open: pick_run()
with a value/label list, a configurable title and two hint lines. /model
and /sessions draw the same way now instead of two copies that could
diverge on Esc, resize and the filter. One window is held across both
steps, so a quick double Esc leaves; with a window per step the second Esc
landed in a window that was already gone and the picker stayed open.

Over the socket the same two steps exist: `{"cmd":"sources"}` and
`{"cmd":"sessions","source":"NAME"}`.

Tested: make check, all suites green (test_engine, test_protocol,
test_ui against a real tmux pane, 229 i18n keys in three catalogues).
2026-11-22 21:32:31 +00:00

407 lines
13 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": "models"}) + "\n")
f.flush()
while True:
ev = json.loads(f.readline())
if ev.get("ev") == "models":
sels = [it.get("sel") for it in ev.get("items", [])]
if "local/m" not in sels:
print("models incomplete", ev, file=sys.stderr)
sys.exit(1)
seen.append("models")
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, titles, rename, delete, switch back, errors ----
def command(text):
f.write(json.dumps({"cmd": "input", "line": text}) + "\n")
f.flush()
while True:
ev = json.loads(f.readline())
if ev.get("ev") == "reply":
return ev.get("text", "")
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")]
titles = {i.get("id"): i.get("title") for i in ev.get("items", [])}
print("sessions_ok" if cur == [sessA] else "sessions_bad")
print("title_ok" if titles.get(sessA) == "hallo" else "title_bad")
break
reply = command("/rename Feiner Titel")
print("rename_ok" if "Feiner Titel" in reply else "rename_bad")
f.write(json.dumps({"cmd": "sessions"}) + "\n")
f.flush()
while True:
ev = json.loads(f.readline())
if ev.get("ev") == "sessions":
titles = {i.get("id"): i.get("title") for i in ev.get("items", [])}
print("rename_listed" if titles.get(sessA) == "Feiner Titel" else
"rename_not_listed")
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
# ---- the picker's two steps: sources, then the rows of one source ----
f.write(json.dumps({"cmd": "sources"}) + "\n")
f.flush()
while True:
ev = json.loads(f.readline())
if ev.get("ev") == "sources":
names = [i.get("name") for i in ev.get("items", [])]
counts = {i.get("name"): i.get("count") for i in ev.get("items", [])}
print("sources_ok" if names and names[0] == "splinter" and
counts.get("splinter", 0) >= 2 else "sources_bad")
break
f.write(json.dumps({"cmd": "sessions", "source": "splinter"}) + "\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("source_rows_ok" if sessA in ids and cur == [sessA] else
"source_rows_bad")
break
reply = command("/rm does-not-exist")
print("rm_missing_ok" if "No such session" in reply else "rm_missing_bad")
reply = command("/rm " + sessB)
print("rm_ok" if sessB in reply and "removed" in reply else "rm_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", [])]
print("rm_gone_ok" if sessB not in ids and sessA in ids else
"rm_gone_bad")
break
reply = command("/rm " + sessA)
print("rm_current_ok" if "Cannot remove" in reply else "rm_current_bad")
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 'sources_ok' "$D/events.out"
checkgrep 'source_rows_ok' "$D/events.out"
checkgrep 'title_ok' "$D/events.out"
checkgrep 'rename_ok' "$D/events.out"
checkgrep 'rename_listed' "$D/events.out"
checkgrep 'rm_missing_ok' "$D/events.out"
checkgrep 'rm_ok' "$D/events.out"
checkgrep 'rm_gone_ok' "$D/events.out"
checkgrep 'rm_current_ok' "$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 'local/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