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.
419 lines
14 KiB
Bash
Executable File
419 lines
14 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
|
|
. "$(dirname "$0")/sh/port.sh"
|
|
D=$(mktemp -d "${TMPDIR:-/var/tmp}/splinter-proto-XXXXXX") || exit 1
|
|
# Two fakes, two ports out of one call (see free_ports): both are bound before
|
|
# either is released, so they cannot come back the same.
|
|
set -- $(free_ports 2)
|
|
P1=$1
|
|
P2=$2
|
|
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=$!
|
|
if ! wait_listen "$P1" "$F1"; then
|
|
echo "FAIL the first fake never listened"
|
|
cat "$D/fake.out"
|
|
exit 1
|
|
fi
|
|
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=$!
|
|
if ! wait_listen "$P2" "$F2"; then
|
|
echo "FAIL the second fake never listened"
|
|
cat "$D/fake2.out"
|
|
exit 1
|
|
fi
|
|
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
|