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

125 lines
4.0 KiB
Bash

# A port this process can really bind, and a wait that tells "my fake started"
# apart from "something else already answers here".
#
# The shell tests used to pick a port with 20000 + $$ % 30000. That is a guess:
# on 2026-11-21 it landed on 36343, which dtsession (the CDE desktop) held.
# The fake telegram died with "Address already in use" while the test went on,
# because curl got an answer from dtsession - a server that knows nothing about
# the test - and every check after it failed. Twenty failures pointing at the
# gateway, none at the port.
#
# Source this file and use free_port / wait_listen:
#
# . "$(dirname "$0")/sh/port.sh"
# P=$(free_port)
# python3 -I tests/fake_x.py "$P" ... &
# S=$!
# wait_listen "$P" "$S" || fail "fake did not start"
# Ask the kernel for a port nobody holds. Between the bind and the fake's own
# bind another process could take it - rare, and much better than a number that
# collides by construction.
free_port() {
python3 -c 'import socket
s = socket.socket(); s.bind(("127.0.0.1", 0))
print(s.getsockname()[1]); s.close()'
}
# Several ports at once, for a test that runs more than one server: asking
# twice is not enough, because the kernel hands out the same port again as
# soon as the first socket is closed. One socket per port, all bound before
# any is released, so the numbers are distinct - and then the caller gets them
# space separated, e.g. `set -- $(free_ports 3); P0=$1 P1=$2 P2=$3`.
free_ports() {
python3 -c 'import socket, sys
n = int(sys.argv[1])
ss = []
for _ in range(n):
s = socket.socket(); s.bind(("127.0.0.1", 0)); ss.append(s)
print(" ".join(str(s.getsockname()[1]) for s in ss))
for s in ss: s.close()' "$1"
}
# $1 = port, $2 = pid of the server we started.
# 0 when that process is alive AND its own socket accepts on that port.
#
# "Curl answered" is not enough on its own: when a fake dies on a port that
# another program already holds (Address already in use), something answers
# there and the test goes on against a server that knows nothing about it.
# Checking the pid catches most of it, but not the window before the fake has
# exited - so the answer has to come from a socket this pid owns.
wait_listen() {
_p="$1"
_pid="$2"
_i=0
while [ "$_i" -lt 100 ]; do
kill -0 "$_pid" 2>/dev/null || return 1
if curl -s -o /dev/null "http://127.0.0.1:$_p/x" &&
listener_is "$_p" "$_pid"; then
return 0
fi
sleep 0.2
_i=$((_i + 1))
done
return 1
}
# $1 = port, $2 = pid of the server we started.
# 0 when that process is alive AND holds the LISTEN socket on that port.
#
# The same contract as wait_listen, for a server that is not HTTP: probing it
# with curl would hang or time out on a protocol that never answers an HTTP
# request, so only the socket is tried here. The ownership check is what makes
# this more than "somebody is listening" - a server that died on a taken port
# leaves a stranger on it, and that stranger must not pass for ours.
wait_tcp() {
_p="$1"
_pid="$2"
_i=0
while [ "$_i" -lt 100 ]; do
kill -0 "$_pid" 2>/dev/null || return 1
if _tcp_open "$_p" && listener_is "$_p" "$_pid"; then
return 0
fi
sleep 0.1
_i=$((_i + 1))
done
return 1
}
_tcp_open() {
python3 - "$1" <<'PY' 2>/dev/null
import socket, sys
s = socket.socket()
s.settimeout(0.2)
try:
s.connect(("127.0.0.1", int(sys.argv[1])))
except OSError:
sys.exit(1)
finally:
s.close()
PY
}
# Does process $2 own the LISTEN socket on port $1? Reads /proc, so it is Linux
# only; when it cannot tell (no /proc/net/tcp, an IPv6-only listener) it says
# "yes" and the weaker check stands - a test must not hang on a helper.
listener_is() {
_lport="$1"
_lpid="$2"
_ino=$(awk -v want="$(printf '%04X' "$_lport")" '
NR > 1 && $4 == "0A" {
split($2, a, ":")
if (a[2] == want) { print $10; exit }
}' /proc/net/tcp 2>/dev/null)
if [ -z "$_ino" ]; then
return 0
fi
for _fd in /proc/"$_lpid"/fd/*; do
case "$(readlink "$_fd" 2>/dev/null)" in
"socket:[$_ino]") return 0 ;;
esac
done
return 1
}