Skip to content
pi-c Artifact [9c6d93baa5]
Login

Artifact [9c6d93baa5]

Artifact 9c6d93baa5c0aef5a69f83a8a50bfae728ef6e2b22726828e7da2e92a29d04f8:


#!/bin/sh
# pic bash: a command the user ran, journaled as Pi's bashExecution message.
# It survives the process, reaches the model with the next request unless it
# was run with --no-context, shows in pic log and in the Pi export, keeps the
# whole of a long output, and is cancelled by Ctrl-C.
set -eu
PIC=${PIC:?set PIC to the pic binary}
SERVER=${SERVER:?set SERVER to the fake_server binary}
here=$(cd "$(dirname "$0")" && pwd)
S=$here/streams
T=$(mktemp -d "${TMPDIR:-/tmp}/pic-bash.XXXXXX")
export HOME="$T/home"
mkdir -p "$HOME" "$T/ws" "$T/log"
spid=
bpid=
cleanup() {
    [ -n "$spid" ] && kill "$spid" 2>/dev/null
    [ -n "$bpid" ] && kill -9 "$bpid" 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-bash-test-key PIC_PI_EXPORT=0
"$SERVER" "$T/port" "$T/log" 60 "sse:$S/text.sse" "sse:$here/bash-streams/read_bash.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"
DB="$T/j.sqlite"
pic() { "$PIC" --db "$DB" --workspace "$T/ws" --model fake/m "$@"; }

# A command in a new conversation: its output on stdout, the conversation on stderr.
out=$(pic bash 'echo hi; echo err >&2' 2> "$T/err") || fail "bash: exit $?"
[ "$out" = "hi
err" ] || fail "bash: output: $out"
conv=$(sed -n 's/^pic: conversation //p' "$T/err")
[ -n "$conv" ] || fail "bash: no conversation id: $(cat "$T/err")"
# Its status is the command's; the conversation continues; the workspace is the cwd.
rc=0
pic bash --session "$conv" 'pwd; exit 3' > "$T/pwd" || rc=$?
[ "$rc" = 3 ] || fail "bash: exit status $rc"
grep -q "ws$" "$T/pwd" || fail "bash: not run in the workspace: $(cat "$T/pwd")"
pic bash --no-context -c 'echo secret-out' > /dev/null || fail "bash --no-context"
pic check > /dev/null || fail "bash: integrity"
printf 'PASS: pic bash runs a command in the workspace, exits with its status and journals it\n'

# Journaled: a later process sees them in the log, with the exit code and the exclusion.
log=$(pic log "$conv") || fail "log"
printf '%s\n' "$log" | grep -q 'bash \$ echo hi' || fail "log: $log"
printf '%s\n' "$log" | grep -q '(exit 3)' || fail "log: no exit code: $log"
printf '%s\n' "$log" | grep -q 'secret-out' || fail "log: the excluded command is missing: $log"
printf '%s\n' "$log" | grep -q 'not sent to the model' || fail "log: exclusion not shown: $log"
pic log --json "$conv" > "$T/log.json" || fail "log --json"
grep -qF '"bash":{"command":"echo hi; echo err >&2","output":"hi\nerr","exitCode":0,"cancelled":false,"truncated":false,"excludeFromContext":false}' "$T/log.json" ||
    fail "log --json: $(cat "$T/log.json")"
grep -qF '"exitCode":3' "$T/log.json" || fail "log --json: no exit 3"
grep -qF '"excludeFromContext":true' "$T/log.json" || fail "log --json: no exclusion"
printf 'PASS: the commands survive the process and show in the log\n'

# They reach the model on the next request, in Pi's wording; the excluded one does not.
pic ask --session "$conv" 'what did that print?' > /dev/null || fail "ask"
req="$T/log/1.req"
[ -e "$req" ] || fail "no request: $(ls "$T/log")"
grep -qF 'Ran `echo hi; echo err >&2`' "$req" || fail "request lacks the command: $(cat "$req")"
grep -qF 'Command exited with code 3' "$req" || fail "request lacks the exit code"
if grep -q 'secret-out' "$req"; then fail "the excluded command reached the model"; fi
pic check > /dev/null || fail "integrity after ask"
printf 'PASS: a command reaches the model with the next request; --no-context keeps it out\n'

# The Pi export has Pi's bashExecution message.
mkdir "$T/pi"
pic export --pi "$T/pi" > /dev/null || fail "export --pi"
cat "$T"/pi/* > "$T/pi.jsonl" 2>/dev/null || fail "no export: $(ls "$T/pi")"
grep -qF '"role":"bashExecution","command":"echo hi; echo err >&2","output":"hi\nerr","exitCode":0,"cancelled":false,"truncated":false,"excludeFromContext":false' "$T/pi.jsonl" ||
    fail "export: $(grep -c . "$T/pi.jsonl") lines, none a bashExecution: $(head -c 600 "$T/pi.jsonl")"
grep -qF '"excludeFromContext":true' "$T/pi.jsonl" || fail "export: the exclusion"
printf 'PASS: the Pi export carries bashExecution messages\n'

# A long output is cut, and the whole of it is kept and readable by name.
pic bash 'seq 1 5000' > "$T/seq" || fail "seq"
grep -q 'Full output: pic:bash/' "$T/seq" || fail "no full-output name: $(tail -3 "$T/seq")"
name=$(sed -n 's/.*Full output: \(pic:bash\/[0-9]*\).*/\1/p' "$T/seq" | head -1)
seqno=${name#pic:bash/}
[ "$(sqlite3 "$DB" "SELECT truncated, full_sz > 20000 FROM pic_bash WHERE entry_seq = $seqno")" = "1|1" ] || fail "pic_bash row for $name"
pic check > /dev/null || fail "integrity after seq"
printf 'PASS: a long output is truncated and kept whole under pic:bash/N\n'

# The model can read that full output back with the read tool, by name.
DB2="$T/j2.sqlite"
"$PIC" --db "$DB2" --workspace "$T/ws" bash 'seq 1 5000' > /dev/null 2>&1 || fail "seq in a second journal"
"$PIC" --db "$DB2" --workspace "$T/ws" --model fake/m ask -c 'read the output back' > /dev/null 2>&1 || fail "ask with read"
[ "$(sqlite3 "$DB2" "SELECT is_error, substr(result, 1, 9) FROM pic_effect WHERE tool = 'read'" | tr '\n' ' ')" = "0|4000 4001 " ] ||
    fail "read pic:bash/1: $(sqlite3 "$DB2" "SELECT is_error, substr(result, 1, 120) FROM pic_effect")"
printf 'PASS: the read tool reads a user command'"'"'s full output by name\n'

# Ctrl-C cancels it; the journal says so.
"$PIC" --db "$DB" --workspace "$T/ws" bash 'touch started; sleep 30' > "$T/sleep.out" 2>&1 &
bpid=$!
i=0
until [ -e "$T/ws/started" ]; do i=$((i + 1)); [ "$i" -lt 300 ] || fail "sleep never started"; sleep 0.05; done
kill -INT "$bpid"
rc=0
wait "$bpid" || rc=$?
bpid=
[ "$rc" = 130 ] || fail "cancel: exit $rc: $(cat "$T/sleep.out")"
[ "$(sqlite3 "$DB" "SELECT cancelled, exit_code IS NULL FROM pic_bash WHERE command = 'touch started; sleep 30'")" = "1|1" ] || fail "cancel: not journaled as cancelled"
pic check > /dev/null || fail "integrity after cancel"
printf 'PASS: Ctrl-C cancels a running command and the journal says so\n'

# --json: live output as events, then Pi's bash response.
pic --json bash 'printf a; sleep 0.4; printf b' > "$T/ev" || fail "json"
grep -qF '"type":"bash_execution_update"' "$T/ev" || fail "json: no update: $(cat "$T/ev")"
last=$(tail -1 "$T/ev")
case $last in
*'"type":"response","command":"bash","success":true,"data":{"output":"ab","exitCode":0,"cancelled":false,"truncated":false,"conversation":"'*) ;;
*) fail "json: response: $last" ;;
esac
printf 'PASS: pic --json bash streams updates and answers in Pi'"'"'s bash response shape\n'