Skip to content
pi-c Artifact [4cc2af8a22]
Login

Artifact [4cc2af8a22]

Artifact 4cc2af8a224109a6f78b1f29885b46172558ac20d0a6a3c3f65141db4ff27006:


#!/bin/sh
# /loop 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'