Files
splinter/tests/test_gateway.sh
T
lotherk bc0b9f22f9 gateway: the retry is announced in the chat, not into a screen queue
The engine's note callback pushes onto the screen event queue. A gateway has
no screen, so the queue was never read: five corrected tries five seconds
apart were twenty-five seconds of silence in Telegram, and the operator saw
nothing until the turn finally answered.

The gateway now installs its own note callback and sends the line into the
chat the turn came from. It is safe on the polling thread because the turn
runs there itself, not on a worker.

The two lines a broken call leaves behind were hardcoded English while the
transport retry was already translated; both keys are now in en/de/pt_BR.

Tests: test_gateway sends "kaputt", the fake answers with a tool call whose
arguments are not JSON, and the chat must carry
"kaputter Tool-Call, neuer Versuch in 0 s (1/5)" and still end with "fertig".
Counter-check: with the engine's own note restored the same run says
"FAIL a broken call is announced in the chat". make check green (273 s),
check_asan green over 45 binaries.
2026-11-22 17:31:36 +00:00

282 lines
12 KiB
Bash
Executable File

#!/bin/sh
#
# splinter -- the Telegram gateway asks for approval in its own chat.
#
# Before: a tool call that needed consent either was denied (no approval
# stream in a daemon) or waited for an "ok <id>" that only the blocked gateway
# could have read. And a refused sendMessage crashed it (NULL err). Run against
# a fake Bot API and a fake model, once with "ok" and once with "no".
set -u
cd "$(dirname "$0")/.." || exit 1
D=$(mktemp -d "${TMPDIR:-/var/tmp}/splinter-gw-XXXXXX") || exit 1
SPLINTER_SESSION_DIR="$D/home/.splinter"; export SPLINTER_SESSION_DIR
trap 'rm -rf "$D"' EXIT
fails=0
wait_fake() { # $1 = port, $2 = pid; 0 when THAT process accepts a connection
#
# Check the pid first. Without it a port that some other program already
# holds looks like a fake that started: curl answers, the test goes on,
# and the failures show up far from the cause. Seen on 2026-11-21 with
# dtsession (the CDE desktop) holding 36343, a port the old formula
# picked; twenty-odd gateway checks failed for it.
for i in $(seq 1 100); do
kill -0 "$2" 2>/dev/null || return 1
curl -s -o /dev/null "http://127.0.0.1:$1/x" && return 0
sleep 0.2
done
return 1
}
# A port this process can really bind. The old formula, 20000 + $$ % 30000,
# is a guess and collided with whatever else the machine runs.
free_port() {
python3 -c 'import socket
s = socket.socket(); s.bind(("127.0.0.1", 0))
print(s.getsockname()[1]); s.close()'
}
run() { # $1 = answer word; leaves $D/sent.log and maybe $D/out.txt
P=$(free_port)
rm -rf "$D/home" "$D/out.txt" "$D/sent.log"
mkdir -p "$D/home/.splinter/commands"
printf '#!/bin/sh\necho hallo\n' > "$D/home/.splinter/commands/hello"
chmod +x "$D/home/.splinter/commands/hello"
printf 'endpoint = http://127.0.0.1:%s/v1/chat/completions\nmodel = m\ntelegram_allow = 7\nplugin = plugins/focus/focus.so\n' \
"$P" > "$D/home/splinter.conf"
python3 -I tests/fake_telegram.py "$P" "$D/out.txt" "$D/sent.log" "$1" \
> "$D/fake.out" 2>&1 &
S=$!
if ! wait_fake "$P" "$S"; then
echo "FAIL fake telegram did not start on $P"
cat "$D/fake.out" 2>/dev/null
fails=$((fails + 1))
return 1
fi
HOME="$D/home" SPLINTER_LOCALE=de SPLINTER_TELEGRAM_TOKEN=tok \
SPLINTER_TELEGRAM_API="http://127.0.0.1:$P" \
SPLINTER_ACK_DIR="$D/home/ack" SPLINTER_ACK_TTL=20 \
./splinter --config "$D/home/splinter.conf" --daemon \
> "$D/gw.out" 2>&1 &
G=$!
for i in $(seq 1 120); do
grep -q '^fertig' "$D/sent.log" 2>/dev/null && break
sleep 0.5
done
kill "$G" "$S" 2>/dev/null
wait "$G" "$S" 2>/dev/null
}
check() { # $1 = description, $2.. = command
d=$1; shift
if "$@"; then :; else echo "FAIL $d"; fails=$((fails + 1)); fi
}
run ok 0
check "the command menu is registered" grep -q '^cmd: start' "$D/sent.log"
check "the menu carries plugin commands" grep -q '^cmd: goal' "$D/sent.log"
check "the menu carries command scripts" grep -q '^cmd: hello' "$D/sent.log"
check "the menu carries /status" grep -q '^cmd: status' "$D/sent.log"
check "the menu carries /menu" grep -q '^cmd: menu' "$D/sent.log"
check "approval is asked in the chat, with the tool" \
grep -q '^Zustimmung [0-9]*: write ' "$D/sent.log"
check "approval carries inline buttons" \
grep -q '"callback_data":"ack:' "$D/sent.log"
check "a question meanwhile is told to wait" \
grep -q '^Ich warte noch' "$D/sent.log"
check "the yes is acknowledged" grep -q '^Zustimmung erteilt' "$D/sent.log"
check "the tool ran after the yes" grep -q 'vom gateway' "$D/out.txt"
check "the turn ends with the answer" grep -q '^fertig' "$D/sent.log"
run no 1
check "the no is acknowledged" grep -q '^Abgelehnt' "$D/sent.log"
check "the tool did not run after the no" test ! -e "$D/out.txt"
# a program in commands/ answers in the chat, without a model turn
run_text() { # $1 = first message, $2 = line to wait for, $3 = port offset,
# $4 = a line that must appear too before the fake is killed
P=$(free_port)
rm -rf "$D/home" "$D/out.txt" "$D/sent.log"
mkdir -p "$D/home/.splinter/commands"
printf '#!/bin/sh\necho "script sagt hallo"\n' \
> "$D/home/.splinter/commands/hello"
chmod +x "$D/home/.splinter/commands/hello"
printf 'endpoint = http://127.0.0.1:%s/v1/chat/completions\nmodel = m\ntelegram_allow = 7\ndelegate = far | test | endpoint=http://127.0.0.1:9/v1/chat/completions | model=other\n' \
"$P" > "$D/home/splinter.conf"
python3 -I tests/fake_telegram.py "$P" "$D/out.txt" "$D/sent.log" ok "$1" \
> "$D/fake.out" 2>&1 &
S=$!
if ! wait_fake "$P" "$S"; then
echo "FAIL fake telegram did not start on $P"
cat "$D/fake.out" 2>/dev/null
fails=$((fails + 1))
return 1
fi
HOME="$D/home" SPLINTER_LOCALE=de SPLINTER_TELEGRAM_TOKEN=tok \
SPLINTER_TELEGRAM_API="http://127.0.0.1:$P" \
./splinter --config "$D/home/splinter.conf" --daemon \
> "$D/gw.out" 2>&1 &
G=$!
# The fake is killed once the marker showed up - so a check that reads a
# line sent *after* it has to name that line here, or the kill wins the
# race (/model: the switch is confirmed, then the next turn's error is
# sent; killing at the confirmation dropped it).
for i in $(seq 1 120); do
if grep -q "$2" "$D/sent.log" 2>/dev/null; then
if [ -z "${4:-}" ] ||
grep -q "$4" "$D/sent.log" 2>/dev/null; then
break
fi
fi
sleep 0.5
done
kill "$G" "$S" 2>/dev/null
wait "$G" "$S" 2>/dev/null
}
run_text "/hello" 'script sagt hallo' 10
check "a command script answers in the chat" \
grep -q 'script sagt hallo' "$D/sent.log"
check "a command script does not start a model turn" test ! -e "$D/out.txt"
run_text "/nosuch" '^unbekanntes Kommando: /nosuch' 20
check "an unknown command is an error" \
grep -q '^unbekanntes Kommando: /nosuch' "$D/sent.log"
check "an unknown command does not start a model turn" test ! -e "$D/out.txt"
# the model's markdown is rendered: Telegram draws HTML, not asterisks
run_text "zeig markdown" '<b>fett</b>' 40
check "an answer is sent with parse_mode=HTML" grep -q '^mode: HTML' "$D/sent.log"
check "bold markdown becomes a tag" grep -q '<b>fett</b>' "$D/sent.log"
check "inline code becomes a tag" grep -q '<code>code</code>' "$D/sent.log"
check "the raw markers do not reach the chat" \
sh -c "! grep -q '\*\*fett\*\*' '$D/sent.log'"
# a table is for the eye: columns in a monospace block, not a row of pipes
run_text "zeig eine tabelle" '<pre>' 50
check "a table is drawn in a monospace block" grep -q '<pre>' "$D/sent.log"
check "a table keeps its columns" grep -q '| a | b |' "$D/sent.log"
# an image in the answer is a photo, not a link, and the text keeps its order
run_text "zeig ein bild" 'Ende' 80
check "the image is sent as a photo" \
grep -q '^photo: https://example.org/b.png caption=Ein Bild' "$D/sent.log"
check "the text before the image arrives" grep -q '^Hier:' "$D/sent.log"
check "the text after the image arrives" grep -q 'Ende' "$D/sent.log"
check "no markdown image reaches the chat" \
sh -c "! grep -q '!\\[Ein Bild\\]' '$D/sent.log'"
run_text "zeig ein codebild" '<code>!\[x\]' 90
check "an image inside code stays text" \
grep -q '<code>!\[x\](https://example.org/x.png)</code>' "$D/sent.log"
check "an image inside code is not a photo" \
sh -c "! grep -q '^photo: https://example.org/x.png' '$D/sent.log'"
# a local image is uploaded as a photo; a non-image stays a reference
run_text "zeig ein lokales bild" 'photo-file: pic.png' 100
check "a local image is uploaded" \
grep -q '^photo-file: pic.png caption=Lokal' "$D/sent.log"
check "the local image does not show up as markdown" \
sh -c "! grep -q '!\\[Lokal\\]' '$D/sent.log'"
run_text "zeig kein bild" 'document-file: notimg.txt' 110
check "a non-image file goes as a document" \
grep -q '^document-file: notimg.txt caption=Kein Bild' "$D/sent.log"
check "it is not sent as a photo" \
sh -c "! grep -q '^photo-file: notimg.txt' '$D/sent.log'"
check "the reference does not stay in the chat" \
sh -c "! grep -q '!\\[Kein Bild\\]' '$D/sent.log'"
# /new starts a fresh conversation and a fresh session file
run_text "/new" 'Neue Konversation: sess-' 45
check "the bot says the new session" grep -q '^Neue Konversation: sess-' "$D/sent.log"
check "a session file was opened" sh -c "ls '$D'/home/.splinter/sessions/telegram/sess-*.jsonl | tail -1 | grep -q sess-"
# /model offers the configured models and switches on the button
run_text "/model" 'Provider far' 60 '^openai:'
check "the model menu lists the configured providers" \
grep -q '"callback_data":"model:far"' "$D/sent.log"
check "the model menu shows the models" grep -q 'far/other' "$D/sent.log"
check "the button switches the model" \
grep -q '^Modell: other (Provider far' "$D/sent.log"
# the next turn really uses the new endpoint (the test one is dead: port 9)
check "the next turn goes to the new endpoint" \
grep -q '^openai:' "$D/sent.log"
# /status: the facts plus the same inline menu
run_text "/status" 'Status (' 120
check "status names the session" grep -q 'Sitzung' "$D/sent.log"
check "status names the model" grep -q 'Modell' "$D/sent.log"
check "status names the endpoint" grep -q 'Endpunkt' "$D/sent.log"
check "status carries the inline menu" grep -q '"callback_data":"status"' "$D/sent.log"
check "status carries the model button" grep -q '"callback_data":"modelmenu"' "$D/sent.log"
# /menu: the inline keyboard, and pressing Status goes through the callback
run_text "/menu" 'Status (' 130 'Sitzung'
check "the menu carries the status button" grep -q '"callback_data":"status"' "$D/sent.log"
check "the menu carries the model button" grep -q '"callback_data":"modelmenu"' "$D/sent.log"
check "the status callback answers" grep -q 'Sitzung' "$D/sent.log"
# /model probe asks the endpoint what it serves and switches to one of them
run_text "/model probe" 'Provider local' 55
check "the probe lists served models" grep -q '"callback_data":"mm:0"' "$D/sent.log"
check "a probed model is used" grep -q '^Modell: m (Provider local' "$D/sent.log"
check "the choice is not saved" \
test ! -f "$D/home/splinter.conf.d/90-model.conf"
# /connect writes a provider to the config drop-in
run_text "/connect" 'Provider testprov gespeichert' 70
drop="$D/home/splinter.conf.d/20-connect.conf"
check "connect writes the drop-in" test -f "$drop"
check "the line names the provider" grep -q '^delegate = testprov |' "$drop"
check "the line names the endpoint" \
grep -q "endpoint=http://127.0.0.1:$P/v1/chat/completions" "$drop"
check "the line names the model" grep -q 'model=llama3' "$drop"
check "connect offers to use it now" \
grep -q '"callback_data":"model:testprov"' "$D/sent.log"
check "connect tested the provider" grep -q 'Test bestanden: ok' "$D/sent.log"
# /interview: the model interviews; the summary before INTERVIEW_COMPLETE
# becomes USER.txt (the fake answers with the marker once the interview
# prompt is in the request)
run_text "/interview" 'Interview gestartet' 30 'Interview in USER.txt gespeichert'
check "the interview saved USER.txt" test -s "$D/home/.splinter/USER.txt"
check "the summary is in USER.txt" grep -q 'mag C99' "$D/home/.splinter/USER.txt"
check "the marker is not in USER.txt" \
sh -c "! grep -q INTERVIEW_COMPLETE '$D/home/.splinter/USER.txt'"
# The model sends a tool call with arguments that are not JSON. The turn
# corrects it and asks again - and the operator must see that in the chat:
# the engine's note callback wrote to a screen queue that a gateway never
# reads, so five retries meant twenty-five seconds of silence.
P=$(free_port)
rm -rf "$D/home" "$D/out.txt" "$D/sent.log"
mkdir -p "$D/home/.splinter"
printf 'endpoint = http://127.0.0.1:%s/v1/chat/completions\nmodel = m\ntelegram_allow = 7\n' \
"$P" > "$D/home/splinter.conf"
python3 -I tests/fake_telegram.py "$P" "$D/out.txt" "$D/sent.log" ok "kaputt" \
> "$D/fake.out" 2>&1 &
S=$!
if ! wait_fake "$P" "$S"; then
echo "FAIL fake telegram did not start on $P"
fails=$((fails + 1))
else
HOME="$D/home" SPLINTER_LOCALE=de SPLINTER_TELEGRAM_TOKEN=tok \
SPLINTER_TELEGRAM_API="http://127.0.0.1:$P" \
SPLINTER_RETRY_WAIT_MS=200 \
./splinter --config "$D/home/splinter.conf" --daemon \
> "$D/gw.out" 2>&1 &
G=$!
for i in $(seq 1 120); do
grep -q '^fertig' "$D/sent.log" 2>/dev/null && break
sleep 0.5
done
kill "$G" "$S" 2>/dev/null
wait "$G" "$S" 2>/dev/null
check "a broken call is announced in the chat" \
grep -q '^kaputter Tool-Call, neuer Versuch in 0 s (1/5)' "$D/sent.log"
check "the turn still ends with an answer" grep -q '^fertig' "$D/sent.log"
fi
if [ "$fails" -eq 0 ]; then echo "test_gateway: ok"; exit 0; fi
cat "$D/gw.out"
exit 1