sp_agent_run can stop before a mutating tool when an approval callback returns 2; the assistant call is in the conversation, no observation is. sp_agent_resume(decision) runs the pending calls, adds the observations and continues the model loop. Normal callers are unchanged: the ack, UI and gateway paths still return 0/1. The A2A server pauses as TASK_STATE_INPUT_REQUIRED, keeps the task and its context, and resumes on SendMessage with the same taskId and yes/no. CancelTask cancels a paused task. SendStreamingMessage has no resume point and denies mutating tools instead. test_pause covers allow/deny in the agent; test_a2a_input covers the whole HTTP flow (yes, no, cancel). Full make check green (33 C tests).
110 lines
3.7 KiB
Bash
Executable File
110 lines
3.7 KiB
Bash
Executable File
#!/bin/sh
|
|
# splinter -- A2A INPUT_REQUIRED: a turn pauses before a mutating tool and
|
|
# resumes with a task id and a yes/no answer.
|
|
#
|
|
# The script transport is the model: first a write call, then the answer. The
|
|
# first SendMessage must return TASK_STATE_INPUT_REQUIRED without running the
|
|
# tool; the follow-up with taskId "yes" runs it and completes; "no" completes
|
|
# without writing.
|
|
set -u
|
|
cd "$(dirname "$0")/.." || exit 1
|
|
D=$(mktemp -d "${TMPDIR:-/var/tmp}/splinter-a2a-ir-XXXXXX") || exit 1
|
|
P=$((20000 + ($$ % 20000)))
|
|
P2=$((P + 1))
|
|
P3=$((P + 2))
|
|
S1=
|
|
S2=
|
|
S3=
|
|
cleanup() {
|
|
[ -n "$S1" ] && kill "$S1" 2>/dev/null
|
|
[ -n "$S2" ] && kill "$S2" 2>/dev/null
|
|
[ -n "$S3" ] && kill "$S3" 2>/dev/null
|
|
rm -rf "$D"
|
|
}
|
|
trap cleanup EXIT
|
|
mkdir -p "$D/home"
|
|
|
|
fails=0
|
|
check() { if "$@"; then :; else echo "FAIL $*"; fails=$((fails + 1)); fi; }
|
|
wait_port() {
|
|
for i in $(seq 1 100); do
|
|
curl -s -o /dev/null "http://127.0.0.1:$1/" && return 0
|
|
sleep 0.2
|
|
done
|
|
return 1
|
|
}
|
|
post() { # post <port> <json>
|
|
curl -s -H 'Content-Type: application/json' --data "$2" \
|
|
"http://127.0.0.1:$1/a2a"
|
|
}
|
|
first() { # first <port>
|
|
post "$1" '{"jsonrpc":"2.0","id":"1","method":"SendMessage","params":{"message":{"messageId":"m","role":"ROLE_USER","parts":[{"text":"please write"}]}}}'
|
|
}
|
|
follow() { # follow <port> <task id> <text>
|
|
post "$1" "{\"jsonrpc\":\"2.0\",\"id\":\"2\",\"method\":\"SendMessage\",\"params\":{\"message\":{\"messageId\":\"m2\",\"role\":\"ROLE_USER\",\"taskId\":\"$2\",\"parts\":[{\"text\":\"$3\"}]}}}"
|
|
}
|
|
state_of() {
|
|
printf '%s' "$1" | python3 -c \
|
|
'import json,sys; print(json.load(sys.stdin)["result"]["task"]["status"]["state"])'
|
|
}
|
|
task_of() {
|
|
printf '%s' "$1" | python3 -c \
|
|
'import json,sys; print(json.load(sys.stdin)["result"]["task"]["id"])'
|
|
}
|
|
|
|
# ---- yes: pause, then run the write ----
|
|
printf 'tool write {"path":"%s/written.txt","content":"yes"}\nanswer done\n' \
|
|
"$D" > "$D/model.txt"
|
|
HOME="$D/home" ./splinter --no-config --script "$D/model.txt" \
|
|
--a2a-server-listen 127.0.0.1 --a2a-listen-port "$P" \
|
|
> "$D/gw1.out" 2>&1 &
|
|
S1=$!
|
|
check wait_port "$P"
|
|
|
|
r=$(first "$P")
|
|
check test "$(state_of "$r")" = "TASK_STATE_INPUT_REQUIRED"
|
|
tid=$(task_of "$r")
|
|
check test -n "$tid"
|
|
check test ! -e "$D/written.txt"
|
|
|
|
r=$(follow "$P" "$tid" "yes")
|
|
check test "$(state_of "$r")" = "TASK_STATE_COMPLETED"
|
|
check test -e "$D/written.txt"
|
|
check grep -q 'yes' "$D/written.txt"
|
|
|
|
# ---- no: pause, then complete without the write ----
|
|
printf 'tool write {"path":"%s/notwritten.txt","content":"no"}\nanswer done\n' \
|
|
"$D" > "$D/model2.txt"
|
|
HOME="$D/home2" ./splinter --no-config --script "$D/model2.txt" \
|
|
--a2a-server-listen 127.0.0.1 --a2a-listen-port "$P2" \
|
|
> "$D/gw2.out" 2>&1 &
|
|
S2=$!
|
|
check wait_port "$P2"
|
|
|
|
r=$(first "$P2")
|
|
check test "$(state_of "$r")" = "TASK_STATE_INPUT_REQUIRED"
|
|
tid=$(task_of "$r")
|
|
r=$(follow "$P2" "$tid" "no")
|
|
check test "$(state_of "$r")" = "TASK_STATE_COMPLETED"
|
|
check test ! -e "$D/notwritten.txt"
|
|
|
|
# ---- cancel a task that is waiting for input ----
|
|
printf 'tool write {"path":"%s/canceled.txt","content":"x"}\nanswer done\n' \
|
|
"$D" > "$D/model3.txt"
|
|
HOME="$D/home3" ./splinter --no-config --script "$D/model3.txt" \
|
|
--a2a-server-listen 127.0.0.1 --a2a-listen-port "$P3" \
|
|
> "$D/gw3.out" 2>&1 &
|
|
S3=$!
|
|
check wait_port "$P3"
|
|
r=$(first "$P3")
|
|
check test "$(state_of "$r")" = "TASK_STATE_INPUT_REQUIRED"
|
|
tid=$(task_of "$r")
|
|
r=$(post "$P3" "{\"jsonrpc\":\"2.0\",\"id\":\"3\",\"method\":\"CancelTask\",\"params\":{\"id\":\"$tid\"}}")
|
|
check test "$(state_of "$r")" = "TASK_STATE_CANCELED"
|
|
check test ! -e "$D/canceled.txt"
|
|
|
|
if [ "$fails" -eq 0 ]; then echo "test_a2a_input: ok"; exit 0; fi
|
|
echo "--- gw1 ---"; tail -5 "$D/gw1.out" 2>/dev/null
|
|
echo "--- gw2 ---"; tail -5 "$D/gw2.out" 2>/dev/null
|
|
exit 1
|