#!/bin/sh
# /loop and /monitor in the terminal interface (docs/manual/loops.md): /loop 5m PROMPT starts a loop and says
# what it is; the interface fires it and its run shows here as a loop's; the footer names it; Esc
# on its run stops the run and pauses the loop, and says so; /loop lists the loops, where r
# resumes one and s stops it; /stop pauses them all.
set -eu
PIC=${PIC:?set PIC to the pic binary}
SERVER=${SERVER:?set SERVER to the fake_server binary}
DRIVE=${DRIVE:?set DRIVE to the pty_drive binary}
S=$(cd "$(dirname "$0")/../pic/streams" && pwd)
T=$(mktemp -d "${TMPDIR:-/tmp}/pic-tui-loop.XXXXXX")
T=$(cd "$T" && pwd -P)
spid=
cleanup() { [ -n "$spid" ] && kill "$spid" 2>/dev/null || :; rm -rf -- "$T"; }
trap cleanup EXIT HUP INT TERM
fail() { printf 'FAIL: %s\n' "$*" >&2; exit 1; }
for v in $(env | sed -nE 's/^([A-Za-z0-9_]*_(API_KEY|TOKEN))=.*/\1/p'); do unset "$v"; done
unset PIC_TEST_PASSPHRASE
mkdir "$T/ws" "$T/log" "$T/home"
export HOME="$T/home" PIC_MODELS_FILE="$T/models.json" PIC_MODEL_CATALOG=offline PIC_PI_EXPORT=0 T
export PIC_NO_NATIVE_CLIPBOARD=1 WS="$T/ws"
"$SERVER" "$T/port" "$T/log" 120 "at:/v1/models=status:404:-" "sse:$S/bash_hold.sse" "sse:$S/text.sse" &
spid=$!
i=0
while [ ! -s "$T/port" ]; do i=$((i + 1)); [ "$i" -lt 600 ] || fail "server"; sleep 0.05; done
port=$(cat "$T/port")
cat > "$T/models.json" <<EOM
{"providers":{"fake":{"baseUrl":"http://127.0.0.1:$port","api":"anthropic-messages","apiKey":"\$FAKE_API_KEY","models":[
{"id":"m","contextWindow":200000}]}}}
EOM
export FAKE_API_KEY=pic-loop-fake
"$PIC" settings defaultModel fake/m > /dev/null
"$PIC" settings cacheRetention none > /dev/null
screen() { perl -pe 's/\e\[[0-9;?]*[A-Za-z]//g; s/\r//g' "$1" | grep -v '^\s*$' | tail -12; }
drive() { # NAME ARGS...: pic on a pseudo-terminal, the script on stdin
name=$1
shift
"$DRIVE" "$T/$name.tx" 120 30 "$PIC" --tui-mode regular --db "$T/j" --workspace "$T/ws" "$@" || fail "$name: $(screen "$T/$name.tx")"
}
q() { sqlite3 -cmd ".timeout 10000" "$T/j" "$1"; }
drive loop <<'EOF2'
expect pic fake/m
send /loop 5m check the deploy\r
expect loop L1 every 5m
expect Esc during its run pauses it
expect Loop L1 is running here
waitfile $WS/started
expect loop L1 running
send \e
expect Stopped. Loop L1 is paused.
send /loop\r
expect Running on their own
expect paused: stopped during its run
send r
expect next 4:
send \e
send /stop\r
expect Paused 1.
expect paused with /stop
send s
expect no loops
send \e
send \x04\x04
EOF2
[ "$(q "SELECT state, why FROM pic_wake")" = "ended|stopped" ] || fail "loop: row $(q 'SELECT state, why FROM pic_wake')"
[ "$(q "SELECT status, error FROM pic_submission WHERE request_key = 'inbox:wake:1:1'")" = "failed|cancelled" ] ||
fail "loop: the run $(q 'SELECT request_key, status FROM pic_submission')"
"$PIC" --db "$T/j" --workspace "$T/ws" check > /dev/null || fail "loop: integrity"
printf 'PASS: /loop starts a loop, its run shows here, Esc pauses it, /loop lists it, /stop pauses them all\n'
# /monitor --done: the command's end wakes the conversation once, with what it printed.
drive monitor -c <<'EOF2'
expect pic fake/m
send /monitor --done echo built with no warnings\r
expect monitor M2 · wakes this conversation once
expect built with no warnings
expect Hello
send \x04\x04
EOF2
[ "$(q "SELECT state, why FROM pic_wake WHERE id = 2")" = "ended|its command exited with status 0" ] || fail "monitor: row $(q 'SELECT * FROM pic_wake WHERE id = 2')"
printf 'PASS: /monitor --done wakes the conversation once, when its command exits\n'
# /goal with a check that passes: one round, then the goal ends.
touch "$T/ws/ok"
drive goal -c <<'EOF2'
expect pic fake/m
send /goal the file is there --check "test -e ok"\r
expect goal G3 · checked with test -e ok after each round
expect Goal G3 is running here
expect Hello
send \x04\x04
EOF2
i=0; until [ "$(q "SELECT state FROM pic_wake WHERE id = 3")" = ended ]; do i=$((i + 1)); [ "$i" -lt 600 ] || fail "goal: $(q 'SELECT state, why FROM pic_wake WHERE id = 3')"; sleep 0.05; done
[ "$(q "SELECT why FROM pic_wake WHERE id = 3")" = 'met: `test -e ok` passed' ] || fail "goal: $(q 'SELECT why FROM pic_wake WHERE id = 3')"
printf 'PASS: /goal runs a round and ends when its check passes\n'