Files
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

114 lines
4.2 KiB
Bash
Executable File

#!/bin/sh
# The `ask` tool over MCP: the question comes back, the answer resumes the turn.
#
# What it guards: `splinter_say` used to park for the engine's full ask wait
# (900 s) when the model asked a question -- the MCP client timed out at 15 s
# and the answer could never be delivered, so the turn was lost. The check is
# that `say` RETURNS with the question, and that `splinter_answer` then makes
# the turn finish with the answer the operator gave. A test that only checked
# "the tool is listed" would have passed while that hang was in the code.
set -u
cd "$(dirname "$0")/.." || exit 1
. tests/sh/port.sh
SP=${SPLINTER:-./splinter}
D=$(mktemp -d "${TMPDIR:-/var/tmp}/splinter-askmcp-XXXXXX") || exit 1
set -- $(free_ports 2)
P=$1
M=$2
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
HOME=$D/home $SP --no-config --autonomous --model m \
--endpoint "http://127.0.0.1:$P/v1/chat/completions" \
--mcp-server-listen 127.0.0.1 --mcp-listen-port "$M" \
> "$D/srv.out" 2>&1 &
S=$!
if ! wait_tcp "$M" "$S"; then
echo "FAIL the MCP server did not start"
cat "$D/srv.out"
exit 1
fi
python3 - "$M" "$D" <<'PY' || fail=$((fail + 1))
import json, socket, sys, time
port, d = int(sys.argv[1]), sys.argv[2]
s = socket.create_connection(("127.0.0.1", port), 5)
# the client's own timeout is the point: a park longer than this is the bug
s.settimeout(15)
f = s.makefile("rw", encoding="utf-8")
def req(i, method, params=None):
o = {"jsonrpc": "2.0", "id": i, "method": method}
if params is not None:
o["params"] = params
f.write(json.dumps(o) + "\n"); f.flush()
return json.loads(f.readline())
def call(i, name, args):
r = req(i, "tools/call", {"name": name, "arguments": args})
if "error" in r:
print("error:", r["error"], file=sys.stderr); sys.exit(1)
return r["result"]["content"][0]["text"]
def out(line):
with open(d + "/result", "a") as fh:
fh.write(line + "\n")
req(1, "initialize", {})
t0 = time.time()
say = call(2, "splinter_say", {"text": "which colour"})
took = time.time() - t0
out("ask_returned %s" % ("yes" if "the model asks" in say else "no"))
out("ask_fast %s" % ("yes" if took < 10 else "no"))
out("ask_has_question %s" % ("yes" if "colour" in say else "no"))
out("ask_names_answer_tool %s" % ("yes" if "splinter_answer" in say else "no"))
ans = call(3, "splinter_answer", {"text": "blue"})
out("answer_ok %s" % ("yes" if "answered" in ans else "no"))
# the turn must finish on its own now; poll instead of sleeping a fixed time
done = False
for i in range(4, 40):
time.sleep(0.5)
if "busy: no" in call(i, "splinter_state", {}):
done = True
break
out("turn_finished %s" % ("yes" if done else "no"))
# a second answer with nothing pending is an error, not a silent accept
stray = call(90, "splinter_answer", {"text": "red"})
out("stray_refused %s" % ("yes" if "no question is pending" in stray else "no"))
PY
while read -r key val; do
case $key in
ask_returned) check 'say returns with the question instead of parking' '[ "$val" = yes ]' ;;
ask_fast) check 'say returns in seconds, not at the engine timeout' '[ "$val" = yes ]' ;;
ask_has_question) check 'the question text reaches the client' '[ "$val" = yes ]' ;;
ask_names_answer_tool) check 'the reply names splinter_answer' '[ "$val" = yes ]' ;;
answer_ok) check 'splinter_answer is accepted' '[ "$val" = yes ]' ;;
turn_finished) check 'the turn finishes once it is answered' '[ "$val" = yes ]' ;;
stray_refused) check 'an answer with no question pending is refused' '[ "$val" = yes ]' ;;
esac
done < "$D/result" 2>/dev/null
# the client died before it wrote anything (a park longer than its timeout):
# that is the failure, and it must not be read as "no check failed"
[ -s "$D/result" ] || { echo "FAIL the client got no answer at all"; fail=$((fail + 1)); }
[ "$fail" -eq 0 ] || { echo "--- server log ---"; cat "$D/srv.out"; exit 1; }
echo "test_ask_mcp: ok"