Files
splinter/tests/test_gateway_openai.sh
lotherk 74df75d438 no approval gate: splinter runs what the operator's registry offers
Konrad, 24.11.: "v1 geht durch ohne irgendwas. ich will auch keine approvals
generell. ich will nur das der user mit ask nach sachen gefragt werden kann."

Removed the whole approval apparatus, not just the prompt:

- agent.c: the decision point (sp_host_gate/approve callbacks), the
  pause/resume path (has_pending, pending_from, sp_agent_resume), ack_approval,
  ask_operator and the ack stdin thread are gone. A tool runs; its exit status
  is the observation. sp_tool_policy (allow/deny per profile) stays - that is
  the operator's own configuration, not a question to the operator.
- engine.c: SP_EV_APPROVAL, sp_engine_approve, the approve_pending condition
  variable and the `approval`/`approve` protocol commands are gone.
- openai_server.c: the v1 endpoint runs gw_* tools without a gate; the
  autonomous flag is no longer part of sp_gateway_opts.
- a2aserver.c: INPUT_REQUIRED and the resume-by-taskId path are gone; CancelTask
  stays. An A2A caller has no operator sitting in front of a prompt.
- mcpserver.c: --autonomous no longer gates mutating tools; the flag and the
  config key are accepted for compatibility and documented as changing
  nothing.
- telegram.c: tg_approve and the approval callback are gone; ask is the only
  dialog, and /cancel inside it still ends the turn.
- front ends: protocol.c, curses.c, ui_local.c, ui_remote.c, client.c lose the
  approval event and the waiting flag.
- ack.c stays: it is a plugin API (plugins/kids asks its parent through it),
  not splinter's own gate. --ack is documented as such.

Tests: test_pause.c guarded the pause and now guards the opposite (a mutate and
a destructive tool run, nothing is asked). test_a2a_input.sh, test_protocol.sh,
test_ui.sh, test_agents_ui.sh, test_gateway.sh, test_gateway_openai.sh,
test_agent.c and test_mcpsession.sh were rewritten the same way;
splinter_approve must now be absent from the MCP tool list. Every rewritten
test was run red against the old behaviour where that was possible.

make check: all tests passed (428 s)
make check-asan: all 47 test binaries clean
make check-tsan: all 47 test binaries free of data races
2026-11-24 11:46:10 +00:00

227 lines
8.9 KiB
Bash
Executable File

#!/bin/sh
#
# splinter -- the OpenAI-compatible gateway.
#
# What it guards: a completion with a local gw_ tool is answered after the
# gateway runs it here; a caller tool is returned to the client with its
# arguments; the caller's follow-up reaches the upstream; gw_read reads locally;
# gw_write runs without --autonomous; a token is enforced; gw_ is reserved; the client
# can ask for SSE; and beyond loopback the server refuses without a token.
set -u
cd "$(dirname "$0")/.." || exit 1
. "$(dirname "$0")/sh/port.sh"
D=$(mktemp -d "${TMPDIR:-/var/tmp}/splinter-gw-XXXXXX") || exit 1
# One upstream fake and five gateways: six ports out of one call, all bound
# before any is released, so no two can come back the same (see free_ports).
set -- $(free_ports 6)
PF=$1
P1=$2
P2=$3
P3=$4
P4=$5
P5=$6
F=
G1=
G2=
G3=
G4=
fails=0
cleanup() {
[ -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
[ -n "$G4" ] && kill "$G4" 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" ] && head -3 "$2"
fails=$((fails + 1))
fi
}
post() { # port, body, out
curl -s -X POST "http://127.0.0.1:$1/v1/chat/completions" \
-H 'Content-Type: application/json' -d "$2" > "$3"
}
mkdir -p "$D/h1" "$D/h2" "$D/h3" "$D/h4"
printf 'inhalt-von-gw-read\n' > "$D/read.txt"
python3 -I tests/fake_gateway.py "$PF" "$D/read.txt" "$D/written.txt" \
> "$D/fake.log" 2>&1 &
F=$!
if ! wait_listen "$PF" "$F"; then
echo "FAIL the upstream fake never listened"
cat "$D/fake.log"
exit 1
fi
HOME=$D/h1 SPLINTER_LOCALE=en ./splinter --no-config --autonomous \
--endpoint "http://127.0.0.1:$PF/v1" --model up \
--openai-listen 127.0.0.1 --openai-listen-port "$P1" \
> "$D/g1.log" 2>&1 &
G1=$!
HOME=$D/h2 SPLINTER_LOCALE=en ./splinter --no-config \
--endpoint "http://127.0.0.1:$PF/v1" --model up \
--openai-listen 127.0.0.1 --openai-listen-port "$P2" \
> "$D/g2.log" 2>&1 &
G2=$!
HOME=$D/h3 SPLINTER_LOCALE=en SPLINTER_GATEWAY_TOKEN=geheim ./splinter \
--no-config --endpoint "http://127.0.0.1:$PF/v1" --model up \
--openai-listen 127.0.0.1 --openai-listen-port "$P3" \
> "$D/g3.log" 2>&1 &
G3=$!
if ! wait_listen "$P1" "$G1" || ! wait_listen "$P2" "$G2"; then
echo "FAIL a gateway did not start"
cat "$D/g1.log" "$D/g2.log"
exit 1
fi
sleep 1
# the model list names what the run configured
curl -s "http://127.0.0.1:$P1/v1/models" > "$D/models.json"
checkgrep '"id":"up"' "$D/models.json"
# a local gw_ tool runs here and the model answers with its observation
post "$P1" '{"model":"x","messages":[{"role":"user","content":"gw-local"}]}' \
"$D/local.json"
checkgrep 'ERGEBNIS: hallo-vom-gw' "$D/local.json"
# gw_read reads a file on this host
post "$P1" '{"model":"x","messages":[{"role":"user","content":"gw-read"}]}' \
"$D/read.json"
checkgrep 'inhalt-von-gw-read' "$D/read.json"
# a caller tool comes back to the client, with the caller's name
post "$P1" '{"model":"x","messages":[{"role":"user","content":"caller bitte"}],"tools":[{"type":"function","function":{"name":"caller_tool","description":"client","parameters":{"type":"object"}}}]}' \
"$D/caller.json"
checkgrep '"name":"caller_tool"' "$D/caller.json"
checkgrep '"finish_reason":"tool_calls"' "$D/caller.json"
# ... and its result goes back upstream
post "$P1" '{"model":"x","messages":[{"role":"user","content":"caller bitte"},{"role":"assistant","content":"","tool_calls":[{"id":"c1","type":"function","function":{"name":"caller_tool","arguments":"{\"x\":1}"}}]},{"role":"tool","tool_call_id":"c1","content":"client-result"}]}' \
"$D/caller2.json"
checkgrep 'ERGEBNIS: client-result' "$D/caller2.json"
# a mixed reply runs the local call and hands the caller's call back, with
# the local observation in the content (the caller cannot answer gw_ calls)
post "$P1" '{"model":"x","messages":[{"role":"user","content":"mixed"}]}' \
"$D/mixed.json"
checkgrep '"name":"caller_tool"' "$D/mixed.json"
checkgrep '"index":0,"id":"c2"' "$D/mixed.json"
checkgrep 'mixed-local' "$D/mixed.json"
checkgrep '"finish_reason":"tool_calls"' "$D/mixed.json"
# ... and the streamed form carries the same observations and calls
curl -sN -X POST "http://127.0.0.1:$P1/v1/chat/completions" \
-H 'Content-Type: application/json' \
-d '{"model":"x","stream":true,"messages":[{"role":"user","content":"mixed"}]}' \
> "$D/mixed-sse.txt"
checkgrep '"tool_calls"' "$D/mixed-sse.txt"
checkgrep 'mixed-local' "$D/mixed-sse.txt"
checkgrep '\[DONE\]' "$D/mixed-sse.txt"
# generation parameters reach the provider instead of being dropped
post "$P1" '{"model":"x","temperature":0.25,"top_p":0.5,"max_tokens":321,"messages":[{"role":"user","content":"params"}]}' \
"$D/params.json"
checkgrep 'PARAMS t=0.25 p=0.5 m=321' "$D/params.json"
# a chunked request body is read like a Content-Length one
printf '%s' '{"model":"x","messages":[{"role":"user","content":"gw-local"}]}' \
> "$D/chunked.body"
curl -s -X POST "http://127.0.0.1:$P1/v1/chat/completions" \
-H 'Content-Type: application/json' \
-H 'Transfer-Encoding: chunked' \
--data-binary "@$D/chunked.body" > "$D/chunked.json"
checkgrep 'ERGEBNIS: hallo-vom-gw' "$D/chunked.json"
# a mutating tool runs on both gateways: no approval, no --autonomous gate.
# The registry says what an agent may reach, not a prompt the endpoint cannot
# put to anyone.
rm -f "$D/written.txt"
post "$P2" '{"model":"x","messages":[{"role":"user","content":"gw-write"}]}' \
"$D/write-noauto.json"
check test -s "$D/written.txt"
checkgrep 'geschrieben' "$D/written.txt"
rm -f "$D/written.txt"
post "$P1" '{"model":"x","messages":[{"role":"user","content":"gw-write"}]}' \
"$D/write-ok.json"
check test -s "$D/written.txt"
checkgrep 'geschrieben' "$D/written.txt"
# the client can ask for SSE
curl -sN -X POST "http://127.0.0.1:$P1/v1/chat/completions" \
-H 'Content-Type: application/json' \
-d '{"model":"x","stream":true,"messages":[{"role":"user","content":"stream bitte"}]}' \
> "$D/stream.txt"
checkgrep 'gestreamt' "$D/stream.txt"
checkgrep '\[DONE\]' "$D/stream.txt"
# ... and with include_usage the caller gets the provider's counts
curl -sN -X POST "http://127.0.0.1:$P1/v1/chat/completions" \
-H 'Content-Type: application/json' \
-d '{"model":"x","stream":true,"stream_options":{"include_usage":true},"messages":[{"role":"user","content":"stream bitte"}]}' \
> "$D/usage.txt"
checkgrep '"usage"' "$D/usage.txt"
checkgrep '"prompt_tokens":11' "$D/usage.txt"
checkgrep '"completion_tokens":7' "$D/usage.txt"
# the answer really arrives in pieces instead of one chunk at the end
python3 -I tests/stream_probe.py "$P1" slowstream > "$D/probe.txt" 2>&1
check test $? -eq 0
checkgrep 'streaming: ' "$D/probe.txt"
# a token is required when one is configured
check test "$(curl -s -o /dev/null -w '%{http_code}' -X POST \
"http://127.0.0.1:$P3/v1/chat/completions" -H 'Content-Type: application/json' \
-d '{"model":"x","messages":[{"role":"user","content":"hi"}]}')" = 401
check test "$(curl -s -o /dev/null -w '%{http_code}' -X POST \
"http://127.0.0.1:$P3/v1/chat/completions" -H 'Authorization: Bearer geheim' \
-H 'Content-Type: application/json' \
-d '{"model":"x","messages":[{"role":"user","content":"hi"}]}')" = 200
# gw_ is the gateway's namespace
check test "$(curl -s -o /dev/null -w '%{http_code}' -X POST \
"http://127.0.0.1:$P1/v1/chat/completions" -H 'Content-Type: application/json' \
-d '{"model":"x","messages":[{"role":"user","content":"hi"}],"tools":[{"type":"function","function":{"name":"gw_read","parameters":{"type":"object"}}}]}')" = 400
# a client that sends half a header and then goes quiet must not hold the
# gateway. Before the read timeout it did: the accept loop is one connection
# at a time, so a second client waited forever and got no answer at all.
HOME=$D/h5 SPLINTER_LOCALE=en SPLINTER_GATEWAY_READ_TIMEOUT=2 ./splinter \
--no-config --endpoint "http://127.0.0.1:$PF/v1" --model up \
--openai-listen 127.0.0.1 --openai-listen-port "$P5" \
> "$D/g5.log" 2>&1 &
G4=$!
if ! wait_listen "$P5" "$G4"; then
echo "FAIL the timeout gateway did not start"
cat "$D/g5.log"
fails=$((fails + 1))
else
mkdir -p "$D/h5"
python3 -I tests/slow_client.py "$P5" > "$D/slow.txt" 2>&1
check test $? -eq 0
checkgrep 'second client answered' "$D/slow.txt"
fi
# beyond loopback there is no gateway without a token
HOME=$D/h4 SPLINTER_LOCALE=en timeout 10 ./splinter --no-config \
--endpoint "http://127.0.0.1:$PF/v1" --model up \
--openai-listen 0.0.0.0 --openai-listen-port "$P4" \
> "$D/noloop.out" 2>&1
rc=$?
if [ "$rc" -eq 124 ]; then
echo "FAIL a gateway on 0.0.0.0 without a token hung"
fails=$((fails + 1))
fi
checkgrep 'beyond loopback' "$D/noloop.out"
if [ "$fails" -eq 0 ]; then
echo "test_gateway_openai: ok"
exit 0
fi
exit 1