#!/bin/sh
# Loops (docs/design/wakes.md): a loop is a pic_wake row; each fire is one transaction that
# advances it and inserts the inbox row wake:ID:N, which a pic run takes up as a request. pic loop
# fire fires what is due and starts the run; missed ticks fire once; a fire that waits is not
# doubled; a running request's own poll fires its conversation's loops as follow-ups; Esc on a
# loop's request pauses the loop; grapevine.maxWakes does not stop a loop; pause, resume, stop and
# the limit of 8; pic usage --by-wake. A crash at each side of the fire's commit neither loses the
# fire nor doubles it. Usage: test/pic/wake.sh [--no-faults]
set -eu
PIC=${PIC:?set PIC to the pic binary}
SERVER=${SERVER:?set SERVER to the fake_server binary}
FAULTS=1
[ "${1:-}" = --no-faults ] && FAULTS=0
here=$(cd "$(dirname "$0")" && pwd)
S=$here/streams
T=$(mktemp -d "${TMPDIR:-/tmp}/pic-wake.XXXXXX")
# pic keeps user state under HOME: never the real one.
export HOME="$T/home"
mkdir -p "$HOME/.pic"
"$PIC" settings cacheRetention none > /dev/null
"$PIC" settings defaultModel fake/m > /dev/null
spid=
pid=
cleanup() {
[ -n "$spid" ] && kill "$spid" 2>/dev/null
[ -n "$pid" ] && kill -9 "$pid" 2>/dev/null
rm -rf -- "$T"
}
trap cleanup EXIT HUP INT TERM
fail() { printf 'FAIL: %s\n' "$*" >&2; exit 1; }
export ANTHROPIC_API_KEY=pic-wake-test-key PIC_PI_EXPORT=0
SQLITE3=${SQLITE3:-sqlite3}
n=0
serve() { # SPEC...: a fresh server, journal and workspace
mkdir "$T/ws$((n + 1))"
DB="$T/j$((n + 1)).sqlite"
WS="$T/ws$((n + 1))"
reserve "$@"
}
reserve() { # SPEC...: a fresh server for the same journal and workspace
[ -n "$spid" ] && kill "$spid" 2>/dev/null
n=$((n + 1))
mkdir "$T/log$n"
LOG="$T/log$n"
"$SERVER" "$T/port$n" "$LOG" 90 "$@" &
spid=$!
i=0
while [ ! -s "$T/port$n" ]; do
i=$((i + 1))
[ "$i" -lt 600 ] || fail "server did not start"
sleep 0.05
done
printf '{"providers":{"fake":{"baseUrl":"http://127.0.0.1:%s","api":"anthropic-messages","apiKey":"$ANTHROPIC_API_KEY","models":[{"id":"m"}]}}}' \
"$(cat "$T/port$n")" > "$T/models$n.json"
export PIC_MODELS_FILE="$T/models$n.json"
}
pic() { "$PIC" --db "$DB" --workspace "$WS" "$@"; }
q() { "$SQLITE3" -cmd ".timeout 10000" "$DB" "$1"; }
until_sql() { # SQL VALUE: wait up to 30s for the query to print VALUE
i=0
until [ "$(q "$1")" = "$2" ]; do
i=$((i + 1))
[ "$i" -lt 600 ] || fail "timed out waiting for [$1] = $2: $(q "$1")"
sleep 0.05
done
}
exists() { # FILE: wait up to 30s for it
i=0
until [ -e "$1" ]; do
i=$((i + 1))
[ "$i" -lt 600 ] || fail "timed out waiting for $1"
sleep 0.05
done
}
reqs() { ls "$LOG" | grep -c '\.req$' || true; }
idle() { # wait until no pic works the conversation SID (the started run has exited)
i=0
while [ "$(q "SELECT count(*) FROM pic_submission WHERE session_id = '$SID' AND status = 'admitted'")" != 0 ] ||
[ "$(q "SELECT count(*) FROM pic_inbox WHERE session_id = '$SID' AND state = 'pending'")" != 0 ]; do
i=$((i + 1))
[ "$i" -lt 600 ] || fail "timed out waiting for $SID to go idle"
sleep 0.05
done
sleep 0.3 # the run lets the lease go as it exits
}
T0=1800000000000
when() { # MINUTES COMMAND...: COMMAND with the wake clock at T0 plus MINUTES (dev builds read PIC_WAKE_NOW_MS)
m=$1
shift
env PIC_WAKE_NOW_MS=$((T0 + m * 60000)) "$@"
}
# A loop in a conversation: created due now; pic loop fire fires it once and starts a pic run,
# which takes it up as a request keyed inbox:wake:1:1 with the loop's frame and prompt.
serve "sse:$S/text.sse" "sse:$S/text.sse" "sse:$S/text.sse" "sse:$S/text.sse" "sse:$S/text.sse" "sse:$S/text.sse"
pic ask 'hello' > /dev/null 2> "$T/err" || fail "setup: $(cat "$T/err")"
SID=$(q "SELECT id FROM pic_session")
out=$(when 0 "$PIC" --db "$DB" --workspace "$WS" loop --session "$SID" 5m check the deploy) || fail "create: $out"
case "$out" in "loop L1 every 5m in conversation $SID: first run now"*) ;; *) fail "create: printed $out" ;; esac
[ "$(q "SELECT kind, state, every_ms, fires, due_ms = created_ms, expires_ms - created_ms, creator FROM pic_wake")" = "loop|active|300000|0|1|604800000|cli" ] ||
fail "create: row $(q 'SELECT * FROM pic_wake')"
out=$(when 0 "$PIC" --db "$DB" --workspace "$WS" loop fire) || fail "fire: $out"
[ "$out" = "1 fired" ] || fail "fire: printed $out"
until_sql "SELECT status FROM pic_submission WHERE request_key = 'inbox:wake:1:1'" answered
idle
[ "$(q "SELECT msg_key, sender, state FROM pic_inbox")" = "wake:1:1|wake:1|delivered" ] || fail "fire: inbox $(q 'SELECT msg_key, sender, state FROM pic_inbox')"
[ "$(q "SELECT fires, due_ms - created_ms FROM pic_wake")" = "1|300000" ] || fail "fire: row $(q 'SELECT fires, due_ms, created_ms FROM pic_wake')"
grep -qF '[Loop L1, every 5m, run 1: sent by a loop the user set up, not typed just now.]\n\ncheck the deploy' "$LOG/2.req" ||
fail "fire: request $(cat "$LOG/2.req")"
pic check > /dev/null || fail "fire: integrity"
printf 'PASS: a loop fires once through the inbox and a pic run takes it up\n'
if [ "$FAULTS" = 0 ]; then # the release build reads the real clock: the actions alone
pic loop pause L1 > /dev/null && pic loop resume L1 > /dev/null && pic loop stop L1 > /dev/null || fail "release: actions"
[ "$(q "SELECT state, why FROM pic_wake")" = "ended|stopped" ] || fail "release: stop"
else
# Not due: nothing. Due again: run 2. Twenty-two minutes on: one fire standing for the three runs
# that came due, anchored on the loop's own times.
out=$(when 1 "$PIC" --db "$DB" --workspace "$WS" loop fire) || fail "early: $out"
[ "$out" = "0 fired" ] || fail "early: printed $out"
out=$(when 5 "$PIC" --db "$DB" --workspace "$WS" loop fire) || fail "due: $out"
[ "$out" = "1 fired" ] || fail "due: printed $out"
until_sql "SELECT status FROM pic_submission WHERE request_key = 'inbox:wake:1:2'" answered
idle
out=$(when 22 "$PIC" --db "$DB" --workspace "$WS" loop fire) || fail "missed: $out"
[ "$out" = "1 fired" ] || fail "missed: printed $out"
until_sql "SELECT status FROM pic_submission WHERE request_key = 'inbox:wake:1:3'" answered
idle
grep -qF 'run 3: sent by a loop the user set up, not typed just now. It stands for the 3 runs that came due while no pic was running.]' "$LOG/4.req" ||
fail "missed: request $(cat "$LOG/4.req")"
[ "$(q "SELECT fires, due_ms - created_ms FROM pic_wake")" = "3|1500000" ] || fail "missed: row $(q 'SELECT fires, due_ms - created_ms FROM pic_wake')"
out=$(when 23 "$PIC" --db "$DB" --workspace "$WS" loop fire) || fail "after: $out"
[ "$out" = "0 fired" ] || fail "after: printed $out"
pic check > /dev/null || fail "missed: integrity"
printf 'PASS: missed ticks fire once, anchored on the loop; not before it is due\n'
# grapevine.maxWakes counts messages from other conversations, not a loop's fires.
"$PIC" settings grapevine.maxWakes 1 > /dev/null
out=$(when 25 "$PIC" --db "$DB" --workspace "$WS" loop fire) || fail "maxwakes: $out"
until_sql "SELECT status FROM pic_submission WHERE request_key = 'inbox:wake:1:4'" answered
idle
"$PIC" settings --unset grapevine.maxWakes > /dev/null
printf 'PASS: grapevine.maxWakes does not stop a loop\n'
# The list, pause (nothing fires), resume (fires at once, once), stop (for good).
out=$(when 26 "$PIC" --db "$DB" --workspace "$WS" loop) || fail "list: $out"
case "$out" in "L1 every 5m next in 240s 4 runs \$0.00 $SID check the deploy") ;; *) fail "list: printed [$out]" ;; esac
out=$(pic loop pause L1) || fail "pause: $out"
[ "$out" = "paused L1" ] || fail "pause: printed $out"
out=$(when 40 "$PIC" --db "$DB" --workspace "$WS" loop fire) || fail "paused: $out"
[ "$out" = "0 fired" ] || fail "paused: fired"
when 40 "$PIC" --db "$DB" --workspace "$WS" loop | grep -qF 'paused' || fail "paused: list"
pic loop resume 1 > /dev/null || fail "resume"
out=$(when 40 "$PIC" --db "$DB" --workspace "$WS" loop fire) || fail "resumed: $out"
[ "$out" = "1 fired" ] || fail "resumed: printed $out"
until_sql "SELECT status FROM pic_submission WHERE request_key = 'inbox:wake:1:5'" answered
idle
pic loop stop L1 > /dev/null || fail "stop"
if pic loop resume L1 2> "$T/err"; then fail "resume after stop"; fi
grep -qF 'it has ended' "$T/err" || fail "resume after stop: $(cat "$T/err")"
[ -z "$(pic loop)" ] || fail "stop: still listed"
pic loop --all | grep -qF 'ended' || fail "stop: not in --all"
[ "$(q "SELECT state, why FROM pic_wake")" = "ended|stopped" ] || fail "stop: row"
pic usage --by-wake | grep -qE '^L1 +loop +ended +5 +5 ' || fail "usage: $(pic usage --by-wake)"
pic check > /dev/null || fail "pause: integrity"
printf 'PASS: pause, resume and stop; the list and pic usage --by-wake\n'
fi
# At most 8 in a conversation; a bad interval and the shortest are refused.
for k in 2 3 4 5 6 7 8 9; do pic loop --session "$SID" 1h "loop $k" > /dev/null || fail "limit: loop $k"; done
if pic loop --session "$SID" 1h one more 2> "$T/err"; then fail "limit: a ninth"; fi
grep -qF 'has 8 loops, monitors and goals already' "$T/err" || fail "limit: $(cat "$T/err")"
if pic loop --session "$SID" 30s too fast 2> "$T/err"; then fail "30s accepted"; fi
grep -qF 'the shortest interval is 1m' "$T/err" || fail "30s: $(cat "$T/err")"
for k in 2 3 4 5 6 7 8 9; do pic loop stop "L$k" > /dev/null; done
printf 'PASS: eight loops a conversation; the shortest interval is a minute\n'
# A loop with no conversation gets a new, empty one; its run takes it up there.
reserve "sse:$S/text.sse" "sse:$S/text.sse"
out=$(pic loop 10m first in a new one) || fail "new: $out"
NEW=$(q "SELECT session_id FROM pic_wake WHERE prompt = 'first in a new one'")
[ "$NEW" != "$SID" ] && [ -n "$NEW" ] || fail "new: conversation $NEW"
pic loop fire > /dev/null || fail "new: fire"
until_sql "SELECT count(*) FROM pic_submission WHERE session_id = '$NEW' AND status = 'answered'" 1
pic loop stop "L$(q "SELECT id FROM pic_wake WHERE session_id = '$NEW'")" > /dev/null
pic check > /dev/null || fail "new: integrity"
printf 'PASS: a loop made with no conversation runs in a new one\n'
[ "$FAULTS" = 1 ] || { printf 'PASS: wake (release: the fault points and the clock are dev-only)\n'; exit 0; }
# A fire that waits is not doubled: while a request runs, a due loop's fire is a follow-up, made by
# the running request's own poll; a later tick while it still waits fires nothing; after the
# request it runs once.
serve "sse:$S/bash_hold.sse" "sse:$S/text.sse" "sse:$S/text.sse" "sse:$S/text.sse"
rm -f "$WS/started" "$WS/release"
"$PIC" --db "$DB" --workspace "$WS" --model fake/m --json ask 'hold on' > "$T/ev" 2> "$T/err" &
pid=$!
exists "$WS/started"
SID=$(q "SELECT id FROM pic_session")
pic loop --session "$SID" 5m look again > /dev/null || fail "waiting: create"
pic send --session "$SID" steer 'ring' > /dev/null || fail "waiting: ring" # the bell: the running pic polls now
until_sql "SELECT count(*) FROM pic_inbox WHERE sender = 'wake:1'" 1
until_sql "SELECT state FROM pic_inbox WHERE sender = 'wake:1'" delivered
[ "$(q "SELECT q.state FROM pic_inbox i JOIN pic_queued q ON q.id = i.queued_id WHERE i.sender = 'wake:1'")" = queued ] ||
fail "waiting: not a queued follow-up"
out=$(env PIC_WAKE_NOW_MS=$(($(q "SELECT due_ms FROM pic_wake") + 1000)) "$PIC" --db "$DB" --workspace "$WS" loop fire) || fail "waiting: fire"
[ "$out" = "0 fired" ] || fail "waiting: doubled ($out)"
touch "$WS/release"
wait "$pid" || fail "waiting: the run: $(cat "$T/err")"
pid=
[ "$(q "SELECT count(*) FROM pic_submission WHERE session_id = '$SID' AND status = 'answered'")" = 2 ] || fail "waiting: requests"
grep -qF 'look again' "$LOG/3.req" || fail "waiting: the follow-up's request $(ls "$LOG")"
[ "$(q "SELECT fires FROM pic_wake")" = 1 ] || fail "waiting: fires"
pic check > /dev/null || fail "waiting: integrity"
printf 'PASS: a due loop is a follow-up to the running request, fired by its own poll; one waiting fire is never doubled\n'
# Esc on the request a loop started pauses the loop (pic send abort is the phone's and attach's Esc).
reserve "sse:$S/bash_hold.sse" "sse:$S/text.sse"
rm -f "$WS/started" "$WS/release"
out=$(env PIC_WAKE_NOW_MS=$(($(q "SELECT due_ms FROM pic_wake") + 1000)) "$PIC" --db "$DB" --workspace "$WS" loop fire) || fail "esc: fire"
exists "$WS/started"
pic send --session "$SID" --wait abort > /dev/null || fail "esc: abort"
until_sql "SELECT state || ' ' || why FROM pic_wake" "paused stopped during its run"
touch "$WS/release"
idle
pic check > /dev/null || fail "esc: integrity"
printf 'PASS: stopping a request a loop started pauses the loop\n'
pic loop stop L1 > /dev/null
# A crash on either side of the fire's commit: before, nothing is left and the next look fires it;
# after, the fire stands, the next look fires nothing new and starts the run that takes it up. Each
# fire reaches the provider once.
for at in 1 2; do
serve "sse:$S/text.sse" "sse:$S/text.sse" "sse:$S/text.sse"
pic ask 'hello' > /dev/null 2> "$T/err" || fail "crash $at: setup"
SID=$(q "SELECT id FROM pic_session")
pic loop --session "$SID" 5m crash check > /dev/null || fail "crash $at: create"
rc=0
env PIC_CRASH_AT="wake_fire:$at" "$PIC" --db "$DB" --workspace "$WS" loop fire > /dev/null 2>&1 || rc=$?
[ "$rc" = 77 ] || fail "crash $at: exit $rc"
if [ "$at" = 1 ]; then
[ "$(q "SELECT fires || ' ' || (SELECT count(*) FROM pic_inbox) FROM pic_wake")" = "0 0" ] || fail "crash 1: left $(q 'SELECT fires FROM pic_wake')"
else
[ "$(q "SELECT fires || ' ' || (SELECT state FROM pic_inbox) FROM pic_wake")" = "1 pending" ] || fail "crash 2: left $(q 'SELECT fires FROM pic_wake')"
fi
pic check > /dev/null || fail "crash $at: integrity after the crash"
out=$(pic loop fire) || fail "crash $at: again"
[ "$out" = "$((2 - at)) fired" ] || fail "crash $at: the next look printed $out"
until_sql "SELECT status FROM pic_submission WHERE request_key = 'inbox:wake:1:1'" answered
idle
[ "$(q "SELECT fires FROM pic_wake")" = 1 ] || fail "crash $at: fires $(q 'SELECT fires FROM pic_wake')"
[ "$(grep -lF 'crash check' "$LOG"/*.req | wc -l | tr -d ' ')" = 1 ] || fail "crash $at: the provider saw it $(grep -lF 'crash check' "$LOG"/*.req | wc -l) times"
pic check > /dev/null || fail "crash $at: integrity"
done
printf 'PASS: a crash before or after the fire commits neither loses nor doubles it\n'
# A crash between the run's admission of the fire and its inbox settle (inbox_wake): the next run
# finds the request, settles the row as that request, and the provider sees it once.
serve "sse:$S/text.sse" "sse:$S/text.sse" "sse:$S/text.sse"
pic ask 'hello' > /dev/null 2> "$T/err" || fail "admit crash: setup"
SID=$(q "SELECT id FROM pic_session")
pic loop --session "$SID" 5m admitted once > /dev/null || fail "admit crash: create"
rc=0
env PIC_CRASH_AT=wake_fire:2 "$PIC" --db "$DB" --workspace "$WS" loop fire > /dev/null 2>&1 || rc=$?
[ "$rc" = 77 ] || fail "admit crash: fire exit $rc"
rc=0
env PIC_CRASH_AT=inbox_wake:1 "$PIC" --db "$DB" --workspace "$WS" run --session "$SID" > /dev/null 2>&1 || rc=$?
[ "$rc" = 77 ] || fail "admit crash: run exit $rc"
[ "$(q "SELECT state FROM pic_inbox")" = pending ] || fail "admit crash: the row"
pic loop fire > /dev/null || fail "admit crash: again"
until_sql "SELECT status FROM pic_submission WHERE request_key = 'inbox:wake:1:1'" answered
idle
[ "$(q "SELECT state FROM pic_inbox")" = delivered ] || fail "admit crash: not settled"
[ "$(grep -lF 'admitted once' "$LOG"/*.req | wc -l | tr -d ' ')" = 1 ] || fail "admit crash: the provider saw it more than once"
pic check > /dev/null || fail "admit crash: integrity"
printf 'PASS: a crash between admitting a fire and settling it runs it once\n'
printf 'PASS: wake\n'