#!/bin/sh
# Remote control under faults (docs/design/remote-protocol.md, section 3): a phone follows a
# conversation and sends one prompt after another while the relay is killed and started again,
# the network drops and comes back, the stream goes silent, pic remote is stopped for longer than
# its idle timeout and continued, and pic remote is killed and started again. Every command is
# delivered exactly once, and the phone's copy of the conversation, built as the page builds it
# (taken in at marks, synced again after a gap), is exactly what a fresh sync from the start
# gets: every event, in order, none twice. Dev builds only (the passphrase and idle seams).
# Usage: test/pic/remote-soak.sh
# Env: PIC, RELAY, PHONE, SERVER (required), SQLITE3.
set -eu
PIC=${PIC:?set PIC to the pic binary}
SERVER=${SERVER:?set SERVER to the fake_server binary}
RELAY=${RELAY:?set RELAY to the fake_relay binary}
PHONE=${PHONE:?set PHONE to the fake_phone binary}
SQLITE3=${SQLITE3:-sqlite3}
here=$(cd "$(dirname "$0")" && pwd)
S=$here/streams
T=$(mktemp -d "${TMPDIR:-/tmp}/pic-soak.XXXXXX")
# pic keeps user state under HOME: never the real one.
export HOME="$T/home"
mkdir -p "$HOME/.pic"
unset PIC_KEY_FD
export ANTHROPIC_API_KEY=pic-soak-test-key PIC_PI_EXPORT=0 PIC_TEST_PASSPHRASE=soak-test-pass
export PIC_TEST_RC_IDLE=4 FAKE_RELAY_BEAT_MS=1000 FAKE_RELAY_STATE="$T/relay.state" FAKE_PHONE_PING_MS=2000
N=6
pids=
cleanup() {
exec 2> /dev/null
for p in $pids; do kill -CONT "$p" 2>/dev/null || true; kill -9 "$p" 2>/dev/null || true; done
rm -rf -- "$T"
}
trap cleanup EXIT HUP INT TERM
fail() {
printf 'FAIL: %s\n' "$*" >&2
for f in "$T"/*.out "$T"/*.err "$T"/*.run; do [ -s "$f" ] && { printf -- '--- %s\n' "$f" >&2; tail -25 "$f" >&2; }; done
exit 1
}
bg() { pids="$pids $1"; }
await() { # FILE PATTERN [TRIES]: the fixed string in FILE (50 ms a try, 30 s by default)
i=0
until grep -qF -- "$2" "$1" 2>/dev/null; do
i=$((i + 1))
[ "$i" -lt "${3:-600}" ] || fail "timed out waiting for $2 in $1"
sleep 0.05
done
}
exists() { # FILE [TRIES]
i=0
until [ -e "$1" ]; do
i=$((i + 1))
[ "$i" -lt "${2:-1200}" ] || fail "timed out waiting for $1"
sleep 0.05
done
}
gone() { rc=0; wait "$1" || rc=$?; }
nrelay=0
relay() { # the relay, on the port it had before once it has one; mailboxes kept in its state file
nrelay=$((nrelay + 1))
rm -f "$T/relay.port"
if [ -n "${PORT:-}" ]; then
FAKE_RELAY_PORT=$PORT "$RELAY" "$T/relay.port" 900 soak-invite > "$T/relay$nrelay.out" 2>&1 &
else
"$RELAY" "$T/relay.port" 900 soak-invite > "$T/relay$nrelay.out" 2>&1 &
fi
RLP=$!
bg $RLP
exists "$T/relay.port"
PORT=$(cat "$T/relay.port")
}
relay
BASE="http://127.0.0.1:$PORT"
"$PIC" settings remote.relay "$BASE" > /dev/null
"$PIC" settings remote.invite soak-invite > /dev/null
ctl() { curl -s -X POST "$BASE/_test/$1" > /dev/null; }
# A paired phone.
mkfifo "$T/pair.in"
"$PIC" remote pair --pipe < "$T/pair.in" > "$T/pair.out" 2> "$T/pair.err" &
PP=$!
bg $PP
exec 3> "$T/pair.in"
await "$T/pair.out" '"type":"offer"'
URL=$(sed -n 's/.*"url":"\([^"]*\)".*/\1/p' "$T/pair.out" | head -1)
mkdir "$T/ph"
"$PHONE" "$T/ph" pair "$URL" "Soak Phone" > "$T/phpair.out" 2>&1 &
PH=$!
bg $PH
await "$T/pair.out" '"type":"code"'
echo y >&3
gone $PH
[ "$rc" = 0 ] || fail "pair: phone exit $rc"
gone $PP
exec 3>&-
# A conversation, and a model with a reply for each prompt.
specs=
k=0
while [ "$k" -le "$N" ]; do specs="$specs sse:$S/text.sse"; k=$((k + 1)); done
mkdir "$T/log" "$T/ws"
# shellcheck disable=SC2086
"$SERVER" "$T/sport" "$T/log" 900 $specs &
bg $!
exists "$T/sport"
printf '{"providers":{"fake":{"baseUrl":"http://127.0.0.1:%s","api":"anthropic-messages","apiKey":"$ANTHROPIC_API_KEY","models":[{"id":"m"}]}}}' \
"$(cat "$T/sport")" > "$T/models.json"
export PIC_MODELS_FILE="$T/models.json"
DB="$T/j.sqlite"
"$PIC" --db "$DB" --workspace "$T/ws" --model fake/m ask 'the first question' > /dev/null 2> "$T/ask.err" || fail "ask: $(cat "$T/ask.err")"
q() { "$SQLITE3" "$DB" "$1"; }
SID=$(q "SELECT id FROM pic_session")
SESS="$DB/$SID"
nr=0
remote() {
nr=$((nr + 1))
"$PIC" remote > "$T/remote$nr.out" 2> "$T/remote$nr.err" &
RP=$!
bg $RP
await "$T/remote$nr.out" 'pic remote: connected'
}
remote
# The phone: follow from the start, then each prompt; after sending one it waits for the test to
# break something, then for the prompt's answer and the request's end.
{
printf 'send {"type":"sync","session":"%s","seq":0}\n' "$SESS"
printf 'expect {"type":"sync","session":"%s"\n' "$SESS"
k=1
while [ "$k" -le "$N" ]; do
key=$(printf 'soak-prompt-%07d' "$k")
printf 'cmd %s {"type":"command","session":"%s","key":"%s","payload":{"type":"prompt","message":"soak %d"}}\n' "$key" "$SESS" "$key" "$k"
printf 'mark sent%d\nwaitfor go%d\n' "$k" "$k"
printf 'expect "key":"%s"\nexpect "status":"answered"\n' "$key"
k=$((k + 1))
done
printf 'sleep 3000\nreplica %s\n' "$T/live.rep"
} > "$T/ph.script"
"$PHONE" "$T/ph" run < "$T/ph.script" > "$T/ph.run" 2>&1 &
PR=$!
bg $PR
fault() { # K: break something while prompt K is under way
case $1 in
1) # the relay killed mid-stream, and started again where it was
kill -9 "$RLP"
gone "$RLP"
sleep 1
relay
what='the relay killed and started again'
;;
2) # the network drops for 3 s
ctl 'down?3000'
sleep 3
what='the network dropped and restored'
;;
3) # pic remote stopped for longer than its idle timeout (and the jump check), then continued
kill -STOP "$RP"
sleep 12
kill -CONT "$RP"
what='pic remote stopped for 12 s and continued'
;;
4) # pic remote killed and started again
kill -9 "$RP"
gone "$RP"
remote
what='pic remote killed and started again'
;;
5) # every stream goes silent, keepalive and all, for longer than the idle timeout
ctl 'mute?7000'
sleep 7
what='the streams silent for 7 s'
;;
6) # the relay ends every stream
ctl cut
what='every stream cut'
;;
esac
}
k=1
while [ "$k" -le "$N" ]; do
exists "$T/ph/sent$k"
fault "$k"
touch "$T/ph/go$k"
printf 'fault %d: %s\n' "$k" "$what"
k=$((k + 1))
done
gone $PR
[ "$rc" = 0 ] || fail "the phone: exit $rc"
# Exactly once: one request per key, each answered, and nothing else.
k=1
while [ "$k" -le "$N" ]; do
key=$(printf 'soak-prompt-%07d' "$k")
[ "$(q "SELECT count(*) || ' ' || group_concat(status) FROM pic_submission WHERE request_key LIKE 'rc:%:$key'")" = "1 answered" ] ||
fail "prompt $k: $(q "SELECT request_key, status, error FROM pic_submission WHERE request_key LIKE 'rc:%:$key'")"
[ "$(q "SELECT count(*) FROM pic_entry WHERE kind = 'user' AND body = 'soak $k'")" = 1 ] || fail "prompt $k: entries"
k=$((k + 1))
done
[ "$(q 'SELECT count(*) FROM pic_submission')" = $((N + 1)) ] || fail "requests: $(q 'SELECT request_key FROM pic_submission')"
"$PIC" --db "$DB" check > /dev/null || fail "integrity"
printf 'PASS: through every fault, each of %d prompts from the phone became exactly one request\n' "$N"
# In order, none missed, none twice: the live copy is what a fresh sync from the start gets.
mkdir "$T/fresh"
cp "$T/ph/pairing" "$T/fresh/pairing"
printf 'send {"type":"sync","session":"%s","seq":0}\nexpect {"type":"sync","session":"%s"\nsleep 1000\nreplica %s\n' \
"$SESS" "$SESS" "$T/fresh.rep" | "$PHONE" "$T/fresh" run > "$T/fresh.run" 2>&1 || fail "fresh sync"
[ -s "$T/live.rep" ] || fail "the phone holds nothing"
cmp -s "$T/live.rep" "$T/fresh.rep" || fail "the phone's copy differs from a fresh sync: $(diff "$T/live.rep" "$T/fresh.rep" | head -20)"
sort -s -n -k1,1 "$T/live.rep" | cmp -s - "$T/live.rep" || fail "events out of seq order"
[ "$(grep -c '"type":"agent_end"' "$T/live.rep")" = $((N + 1)) ] || fail "agent_end count: $(grep -c agent_end "$T/live.rep")"
grep -q '^gap$' "$T/ph.run" || grep -q '^session$' "$T/ph.run" || fail "no fault was seen by the phone"
printf 'PASS: the phone'"'"'s copy has every event in order and none twice (%s events, %s sessions)\n' \
"$(wc -l < "$T/live.rep" | tr -d ' ')" "$(grep -c '^session$' "$T/ph.run")"
grep -qF 'back after' "$T/remote$nr.out" "$T"/remote*.out || fail "pic remote did not notice the stop"
printf 'PASS: pic remote noticed the stop and reconnected at once\n'
kill "$RP"
gone "$RP"
[ "$rc" = 0 ] || fail "stop: exit $rc"