#!/bin/sh # The `ask` tool when the front end does not answer: the turn must come back, # not park. # # What it guards: a protocol client that ignores the `ask` event -- one written # before the event existed, which the version number does not exclude because # additive events keep it -- leaves the question pending. Before ask_timeout # existed the wait was hard-coded to 900 s, so such a client lost the turn for # fifteen minutes with no way to shorten it. Here the wait is set to 3 s and the # check is that the turn RETURNS with the "no answer" observation instead of # staying parked. # # A test that only checked "the ask event arrived" would have passed while that # park was in the code: the event did arrive, nobody acted on it. set -u cd "$(dirname "$0")/.." || exit 1 . tests/sh/port.sh SP=${SPLINTER:-./splinter} D=$(mktemp -d "${TMPDIR:-/var/tmp}/splinter-askto-XXXXXX") || exit 1 set -- $(free_ports 1) P=$1 F= S= cleanup() { [ -n "$F" ] && kill $F 2>/dev/null; [ -n "$S" ] && kill $S 2>/dev/null rm -rf "$D"; } trap cleanup EXIT INT TERM fail=0 check() { if eval "$2"; then echo "ok $1"; else echo "FAIL $1"; fail=$((fail + 1)); fi; } mkdir -p "$D/home" python3 -I tests/fake_openai_ask.py "$P" "green,blue" > "$D/fake.out" 2>&1 & F=$! if ! wait_listen "$P" "$F"; then echo "FAIL the fake endpoint did not start" cat "$D/fake.out" exit 1 fi # the whole point: the wait is the operator's, not a constant in the code HOME=$D/home SPLINTER_LOCALE=en SPLINTER_ASK_TIMEOUT=3 ./splinter \ --no-config --autonomous --model m \ --endpoint "http://127.0.0.1:$P/v1/chat/completions" \ --serve --socket "$D/sock" > "$D/serve.out" 2>&1 & S=$! i=0 while [ ! -e "$D/sock" ] && [ "$i" -lt 100 ]; do i=$((i + 1)); sleep 0.2; done if [ ! -e "$D/sock" ]; then echo "FAIL the server never created its socket" cat "$D/serve.out" exit 1 fi python3 - "$D/sock" > "$D/result" 2> "$D/client.err" <<'PY' import json, socket, sys, time s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) # generous next to the 3 s wait: this bounds the test, it is not the check s.settimeout(30) s.connect(sys.argv[1]) f = s.makefile("rw", encoding="utf-8") while True: if json.loads(f.readline()).get("ev") == "state": break f.write(json.dumps({"cmd": "say", "text": "baue eine login seite"}) + "\n") f.flush() t0 = time.time() saw_ask = False turn = None while time.time() - t0 < 25: try: ev = json.loads(f.readline()) except (socket.timeout, OSError): break # the park: no event at all if ev.get("ev") == "ask": saw_ask = True # deliberately no answer: this is the client that cannot answer continue if ev.get("ev") == "turn": turn = ev break took = time.time() - t0 print("ask_event %s" % ("yes" if saw_ask else "no")) print("turn_returned %s" % ("yes" if turn else "no")) print("turn_fast %s" % ("yes" if turn and took < 20 else "no")) print("turn_ok %s" % ("yes" if turn and turn.get("ok") else "no")) print("no_answer_said %s" % ("yes" if turn and "no answer" in turn.get("answer", "") else "no")) # the question is gone now: a late answer must be refused, not silently taken late = None try: f.write(json.dumps({"cmd": "answer", "text": "blue"}) + "\n") f.flush() while True: ev = json.loads(f.readline()) if ev.get("ev") == "error": late = ev.get("text", "") break if ev.get("ev") == "turn": break except (socket.timeout, OSError): pass # the turn never came back; reported above print("late_answer_refused %s" % ("yes" if late and "no question" in late else "no")) PY while read -r key val; do case $key in ask_event) check 'the question reaches the client as an event' '[ "$val" = yes ]' ;; turn_returned) check 'the turn returns instead of staying parked' '[ "$val" = yes ]' ;; turn_fast) check 'it returns at the configured wait, not at 900 s' '[ "$val" = yes ]' ;; turn_ok) check 'the turn is a success, not an error' '[ "$val" = yes ]' ;; no_answer_said) check 'the model is told nobody answered' '[ "$val" = yes ]' ;; late_answer_refused) check 'an answer after the wait is refused' '[ "$val" = yes ]' ;; esac done < "$D/result" 2>/dev/null [ -s "$D/result" ] || { echo "FAIL the client produced no result" cat "$D/client.err"; fail=$((fail + 1)); } [ "$fail" -eq 0 ] || { echo "--- server log ---"; cat "$D/serve.out"; exit 1; } echo "test_ask_timeout: ok"