Skip to content
pi-c Artifact [a5775ab765]
Login

Artifact [a5775ab765]

Artifact a5775ab7657c640e27182b7191b30c91557867b3907f9881b8e5590bac4a90cf:


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

# The model asks for a loop with its wake tool: it waits, the interface asks in one line, y starts it.
kill "$spid" 2> /dev/null || :
esc='{\"action\":\"loop\",\"prompt\":\"check CI\",\"every\":\"10m\"}'
{
    printf 'event: message_start\ndata: {"type":"message_start","message":{"id":"msg_01","type":"message","role":"assistant","model":"m","content":[],"stop_reason":null,"usage":{"input_tokens":25,"output_tokens":1}}}\n\n'
    printf 'event: content_block_start\ndata: {"type":"content_block_start","index":0,"content_block":{"type":"tool_use","id":"toolu_w","name":"wake","input":{}}}\n\n'
    printf 'event: content_block_delta\ndata: {"type":"content_block_delta","index":0,"delta":{"type":"input_json_delta","partial_json":"%s"}}\n\n' "$esc"
    printf 'event: content_block_stop\ndata: {"type":"content_block_stop","index":0}\n\n'
    printf 'event: message_delta\ndata: {"type":"message_delta","delta":{"stop_reason":"tool_use","stop_sequence":null},"usage":{"output_tokens":40}}\n\n'
    printf 'event: message_stop\ndata: {"type":"message_stop"}\n\n'
} > "$T/askloop.sse"
mkdir "$T/log2"
rm -f "$T/port"
"$SERVER" "$T/port" "$T/log2" 120 "at:/v1/models=status:404:-" "sse:$T/askloop.sse" "sse:$S/text.sse" &
spid=$!
i=0
while [ ! -s "$T/port" ]; do i=$((i + 1)); [ "$i" -lt 600 ] || fail "server 2"; 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
drive ask -c <<'EOF2'
expect pic fake/m
send keep an eye on CI\r
expect The model wants to start loop L4: every 10m 'check CI'.
expect y start · n no
send y
expect L4 approved: it starts now.
expect Loop L4 is running here
send \x04\x04
EOF2
[ "$(q "SELECT state, json_extract(options, '\$.approval') FROM pic_wake WHERE id = 4")" = "active|approved" ] || fail "ask: $(q 'SELECT state, options FROM pic_wake WHERE id = 4')"
printf 'PASS: a loop the model asks for waits; the interface asks in one line, and y starts it\n'