Skip to content
pi-c Artifact [c067622088]
Login

Artifact [c067622088]

Artifact c06762208889c0e80105b401325ebf85475bf3ac7467bbbb66971422717a4d03:


#!/bin/sh
# pic ps: the processes pic started. An async hook shows as running and then
# finished; pic ps --kill stops a long-running one; the startup line about
# processes of earlier runs appears once (not in --json mode); a bash tool
# command and a terminal request child are recorded. A private HOME holds the
# user database; every sleep carries its own end and is killed on exit.
# (The terminal case needs DRIVE, the pty driver; without it, it is skipped.)
set -eu
PIC=${PIC:?set PIC to the pic binary}
SERVER=${SERVER:?set SERVER to the fake_server binary}
S=$(cd "$(dirname "$0")/streams" && pwd)
T=$(cd "$(mktemp -d "${TMPDIR:-/tmp}/pic-ps.XXXXXX")" && pwd -P)
spid=
cleanup() {
    [ -n "$spid" ] && kill "$spid" 2>/dev/null
    for h in "$T/home" "$T/home2"; do HOME=$h "$PIC" ps --kill --all > /dev/null 2>&1 || :; done
    [ -n "${KEEP:-}" ] && { echo "kept $T" >&2; return; }
    rm -rf -- "$T"
}
trap cleanup EXIT HUP INT TERM
fail() { printf 'FAIL: %s\n' "$*" >&2; exit 1; }
export ANTHROPIC_API_KEY=pic-ps-test-key PIC_PI_EXPORT=0 PIC_MODEL_CATALOG=offline PIC_NO_NATIVE_CLIPBOARD=1
unset PIC_HOOK PIC_HOOK_OWNER 2>/dev/null || :
HOME="$T/home"
mkdir -p "$HOME/.pic" "$T/home2/.pic" "$T/ws" "$T/log"
export HOME
WS="$T/ws"
DB="$T/j"
pic() { "$PIC" --db "$DB" --workspace "$WS" --model fake/m "$@"; }

"$SERVER" "$T/port" "$T/log" 120 "sse:$S/text.sse" "sse:$S/text.sse" "sse:$S/text.sse" "sse:$S/text.sse" \
    "sse:$S/text.sse" "sse:$S/text.sse" "sse:$S/text.sse" "sse:$S/text.sse" "sse:$S/text.sse" &
spid=$!
i=0
while [ ! -s "$T/port" ]; do i=$((i + 1)); [ "$i" -lt 100 ] || fail "server"; 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")" > "$T/models.json"
export PIC_MODELS_FILE="$T/models.json"

# gone PID: until the process is gone (found by pid, never by name: other runs share the machine), at most 5 s.
gone() {
    i=0
    while kill -0 "$1" 2> /dev/null; do i=$((i + 1)); [ "$i" -lt 50 ] || return 1; sleep 0.1; done
}

# waitps PATTERN [ps option]: until a line of pic ps matches, at most 8 s.
waitps() {
    i=0
    until pic ps ${2:-} 2> /dev/null | grep -q -- "$1"; do
        i=$((i + 1))
        [ "$i" -lt 80 ] || fail "pic ps ${2:-} never showed $1: $(pic ps --all 2>&1)"
        sleep 0.1
    done
}
hook() { # COMMAND: the one async Stop hook, in HOME
    printf '{"hooks": {"Stop": [{"hooks": [{"type":"command","command":"%s","async":true}]}]}}' "$1" > "$HOME/.pic/hooks.json"
}

# ---- nothing yet ---------------------------------------------------------------
[ "$(pic ps)" = "no processes running" ] || fail "empty: $(pic ps)"
[ "$(pic ps --json)" = "[]" ] || fail "empty json: $(pic ps --json)"
pic ps --help | grep -q "not tracked" || fail "--help names the limit"
pic ps --bogus > /dev/null 2>&1 && fail "an unknown option was accepted"
pic ps --kill > /dev/null 2>&1 && fail "--kill without a target was accepted"
printf 'PASS: an empty pic ps, its help, its usage errors\n'

# ---- an async hook: running, then finished -------------------------------------
hook 'sleep 2.4711'
pic ask quick > /dev/null 2> "$T/err" || fail "ask: $(cat "$T/err")"
waitps 'sleep 2.4711'
pic ps | grep 'sleep 2.4711' | grep -q 'async-hook .* running ' || fail "async row: $(pic ps)"
pic ps --json | grep -q '"kind":"async-hook","command":"sleep 2.4711","workspace":"[^"]*","conversation":"[^"]*","pid":[0-9]*,"pgid":[0-9]*,"owner_pid":[0-9]*,"started_ms":[0-9]*,"ended_ms":null,"exit_code":null,"signal":null,"running":true' ||
    fail "running json: $(pic ps --json)"
i=0
while pic ps | grep -q 'sleep 2.4711'; do i=$((i + 1)); [ "$i" -lt 100 ] || fail "async hook never finished"; sleep 0.1; done
pic ps --all | grep 'sleep 2.4711' | grep -q 'async-hook .* ended ' || fail "finished row: $(pic ps --all)"
pic ps --all --json | grep -q '"kind":"async-hook","command":"sleep 2.4711".*"exit_code":null,"signal":null,"running":false' ||
    fail "finished json: $(pic ps --all --json)"
printf 'PASS: an async hook shows as running, then as ended\n'

# ---- --kill stops a long-running one -------------------------------------------
hook 'sleep 61.4712'
pic ask quick > /dev/null 2> "$T/err" || fail "ask: $(cat "$T/err")"
waitps 'sleep 61.4712'
id=$(pic ps | awk '/sleep 61.4712/ {print $1}')
hpid=$(pic ps | awk '/sleep 61.4712/ {print $3}')
[ -n "$id" ] && [ -n "$hpid" ] || fail "no id"
pic ps --kill "$id" | grep -q "^killed $id " || fail "--kill $id said: $(pic ps --kill "$id")"
pic ps | grep -q 'sleep 61.4712' && fail "still listed running after --kill"
gone "$hpid" || fail "the killed hook's process is still there"
pic ps --kill "$id" | grep -q "not running" || fail "--kill of a finished row"
pic ps --kill 99999 | grep -q "not running" || fail "--kill of an unknown id"
printf 'PASS: pic ps --kill stops a running process and refuses one that is not\n'

# ---- the startup line, once ----------------------------------------------------
H2="$T/home2"
hook2() { printf '{"hooks": {"Stop": [{"hooks": [{"type":"command","command":"sleep 62.4713","async":true}]}]}}' > "$H2/.pic/hooks.json"; }
hook2
HOME=$H2 pic ask quick > /dev/null 2> "$T/err" || fail "ask: $(cat "$T/err")"
i=0
until HOME=$H2 pic ps | grep -q 'sleep 62.4713'; do i=$((i + 1)); [ "$i" -lt 80 ] || fail "hook 2 never showed"; sleep 0.1; done
hpid2=$(HOME=$H2 pic ps | awk '/sleep 62.4713/ {print $3}')
[ -n "$hpid2" ] || fail "no pid for hook 2"
HOME=$H2 pic settings > "$T/out" 2> "$T/err" || :
[ "$(grep -c 'from earlier runs still running; see pic ps' "$T/err")" = 1 ] || fail "startup line: $(cat "$T/err")"
grep -q '^pic: 1 process from earlier runs still running; see pic ps$' "$T/err" || fail "wording: $(cat "$T/err")"
grep -q 'earlier runs' "$T/out" && fail "the line went to stdout"
HOME=$H2 pic settings > "$T/out" 2> "$T/err" || :
grep -q 'earlier runs' "$T/err" && fail "startup line repeated within the hour"
HOME=$H2 pic settings --unset ps.noticed_ms
HOME=$H2 pic --json ask quick > "$T/out" 2> "$T/err" || fail "json ask: $(cat "$T/err")"
grep -q 'earlier runs' "$T/err" "$T/out" && fail "startup line in --json mode"
HOME=$H2 pic ps --kill --all | grep -q '^killed ' || fail "kill --all"
gone "$hpid2" || fail "--kill --all left a process"
printf 'PASS: the startup line appears once, on stderr, never in --json mode\n'

# ---- a bash tool command -------------------------------------------------------
rm -f "$HOME/.pic/hooks.json"
cat > "$T/bash.sse" <<'SSE'
event: message_start
data: {"type":"message_start","message":{"id":"msg_b","type":"message","role":"assistant","model":"m","content":[],"stop_reason":null,"usage":{"input_tokens":5,"output_tokens":1}}}

event: content_block_start
data: {"type":"content_block_start","index":0,"content_block":{"type":"tool_use","id":"toolu_b","name":"bash","input":{}}}

event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"input_json_delta","partial_json":"{\"command\": \"echo psbash-marker\"}"}}

event: content_block_stop
data: {"type":"content_block_stop","index":0}

event: message_delta
data: {"type":"message_delta","delta":{"stop_reason":"tool_use","stop_sequence":null},"usage":{"output_tokens":9}}

event: message_stop
data: {"type":"message_stop"}

SSE
kill "$spid" 2> /dev/null; wait "$spid" 2> /dev/null || :
rm -rf "$T/log" "$T/port"; mkdir "$T/log"
"$SERVER" "$T/port" "$T/log" 120 "sse:$T/bash.sse" "sse:$S/text.sse" "sse:$S/text.sse" "sse:$S/text.sse" &
spid=$!
i=0
while [ ! -s "$T/port" ]; do i=$((i + 1)); [ "$i" -lt 100 ] || fail "server 2"; 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")" > "$T/models.json"
pic ask 'run it' > /dev/null 2> "$T/err" || fail "ask: $(cat "$T/err")"
i=0
until pic ps --all | grep 'echo psbash-marker' | grep -q '^[0-9]* *bash .* exit 0 '; do
    i=$((i + 1)); [ "$i" -lt 50 ] || fail "bash row: $(pic ps --all)"; sleep 0.1
done
pic ps --all --json | grep -q '"kind":"bash","command":"echo psbash-marker","workspace":"'"$WS"'","conversation":"[^"]\+"' ||
    fail "bash json: $(pic ps --all --json)"
pic bash 'echo psbang-marker' > /dev/null 2> "$T/err" || fail "bash: $(cat "$T/err")"
pic ps --all | grep 'echo psbang-marker' | grep -q 'user-bash .* exit 0 ' || fail "user bash row: $(pic ps --all)"
printf 'PASS: a bash tool command and a "!" command are recorded with their workspace and conversation\n'

# ---- a terminal request child --------------------------------------------------
if [ -n "${DRIVE:-}" ] && [ -x "$DRIVE" ]; then
    export T
    "$DRIVE" "$T/tui.tx" 80 24 "$PIC" --tui-mode regular --db "$T/j" --workspace "$WS" --model fake/m << 'SCRIPT' ||
expect pic fake/m
send hello psturn\r
expect Hello, "world"
wait 300
send \x04
expect pic: conversation
SCRIPT
        fail "tui: $(perl -pe 's/\e\[[0-9;?]*[A-Za-z]//g; s/\r//g' "$T/tui.tx" | grep -v '^\s*$' | tail -8)"
    pic ps --all | grep 'pic --json ask hello psturn' | grep -q 'tui-request .* exit 0 ' || fail "tui row: $(pic ps --all)"
    printf 'PASS: a terminal request child is recorded and ends with its exit status\n'
else
    printf 'SKIP: the terminal request child (no DRIVE)\n'
fi