Files
splinter/tests/test_gateway.sh
T
lotherk 181b2c0638 ask: the model can put a question to the operator, in every front end
The `ask` tool asks and the answer -- or "nobody answered" -- becomes the
observation. Read class: it changes nothing on the machine, and the authority
is the operator, not a gate.

Front ends: the chat draws a dialog (options to pick, typed words still count),
the protocol carries it as an `ask` event and an `answer` command, MCP returns
it from splinter_say and resumes with splinter_answer, the Telegram gateway
sends it as a message and takes the next message from an allowed user.

What the review of the WIP found, and what this fixes:

- -p hung for the full 900 s ask wait: sp_engine_prompt installed engine_ask,
  but nobody polls events on the synchronous path. cfg.ask now stays NULL
  there, so the tool answers at once with "nobody can be asked here".
  Measured: 3 s instead of 900 s (was rc=124 after 20 s).
- splinter_say over MCP hung the same way, because tool_say only looked at
  st.waiting. It now looks at st.asking and returns the question; the client
  timed out at 15 s before, and the turn was lost.
- protocol.c read the question buffer uninitialized when sp_engine_ask
  returned 0.
- turn_ask answered nothing when the window could not be opened, which parked
  the turn for the full wait.
- pick_run computed the list height without the question's rows, so the
  highlighted row scrolled out of the window (reproduced: after nine Downs no
  ">" row was on the screen). pick_draw and pick_run now share pick_listtop().
- the Telegram gateway was dead for this tool: it runs its turns on a copy of
  e->base, where ask was never set, so the model was told "nobody can be
  asked" while the operator sat in the chat. New tg_ask, verified against a
  running daemon and covered in test_gateway.sh.
- tests/test_ask_ui.sh was untracked and not in make check, so it never ran.

A2A keeps no ask channel on purpose: there an approval pauses the task into
INPUT_REQUIRED, and the ask signature cannot express a pause -- saying
"nobody can be asked" is the honest answer there.

Tests: make check green (327 s), including the new test_ask_mcp.sh and
test_ask_ui.sh, both wired into make check. test_ask_mcp.sh was verified to
fail against the unfixed code (client times out, no result written).
make check-asan: 46 binaries clean. make check-tsan: 46 binaries, no races.
2026-11-24 07:57:35 +00:00

274 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
. "$(dirname "$0")/sh/port.sh"
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
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_listen "$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_listen "$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 `ask` tool reaches the operator in the chat, and the reply is the
# observation: the gateway runs its turns on a copy of the base config, so a
# callback set only on the engine's turn config never arrived here and the
# model was told "nobody can be asked" while the operator sat in the chat.
run_text "frag den operator" 'antwort war: ok' 105
check "the question reaches the chat" \
grep -q 'Which colour should the login page be?' "$D/sent.log"
check "the question carries its options" \
grep -q -- '- green' "$D/sent.log"
check "the operator's answer reaches the model" \
grep -q '^antwort war: ok' "$D/sent.log"
check "the model is not told nobody is there" \
sh -c "! grep -q 'nobody can be asked' '$D/sent.log'"
# 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_listen "$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