Skip to content
pi-c Artifact [efe459bd0e]
Login

Artifact [efe459bd0e]

Artifact efe459bd0eaee5147bd3c89f0a1549d826b85fbf61917dddcba466bebde18ff6:


#!/bin/sh
# Each conversation keeps the model and key it ran with: -p -c, ask --session, --mode rpc --session
# and switch_session go on with the conversation's own model and key, not the saved default, and
# say so; --model and --key still win for one run; a conversation whose model or key cannot be
# had now stops with one line that names it and the way on, and sends nothing; a fork goes on
# with its parent's; pic sessions --json reports each conversation's own. Against a fake server,
# with a private HOME. --no-faults (the release build): the named-key half is skipped, since its
# passphrase comes from PIC_TEST_PASSPHRASE, which only the dev build reads.
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=$(mktemp -d "${TMPDIR:-/tmp}/pic-sessionmodel.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/home" "$T/ws" "$T/log" "$T/out"
export HOME="$T/home" PIC_PI_EXPORT=0 PIC_MODEL_CATALOG=offline PIC_MODELS_FILE="$T/models.json"
"$SERVER" "$T/port" "$T/log" 120 "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,"reasoning":true}]},
 "other":{"baseUrl":"http://127.0.0.1:$port","api":"anthropic-messages","apiKey":"\$OTHER_API_KEY","models":[
  {"id":"n","contextWindow":200000}]}}}
EOM
export FAKE_API_KEY=pic-sm-fake OTHER_API_KEY=pic-sm-other
pic() { # NAME ARGS...: stdout and stderr in $T/out/NAME.{out,err}; its exit status
    name=$1
    shift
    "$PIC" --db "$T/j" --workspace "$T/ws" "$@" > "$T/out/$name.out" 2> "$T/out/$name.err" < /dev/null
}
requests() { ls "$T/log" | wc -l | tr -d ' '; }
last_model() { sed -n 's/.*"model":"\([^"]*\)".*/\1/p' "$T/log/$(requests).req" | head -1; }

pic default models --default fake/m || fail "default: $(cat "$T/out/default.err")"

# A conversation started on another model goes on with it.
pic first --model other/n --session-id conv-a -p 'first' || fail "first: $(cat "$T/out/first.err")"
[ "$(last_model)" = n ] || fail "first: sent to $(last_model)"
pic again --session-id conv-a -p 'again' || fail "again: $(cat "$T/out/again.err")"
[ "$(last_model)" = n ] || fail "again: the conversation's model was not kept: sent to $(last_model)"
grep -qF 'pic: model other/n (this conversation'"'"'s own; --model to change)' "$T/out/again.err" ||
    fail "again: notice: $(cat "$T/out/again.err")"
pic cont -c -p 'continued' || fail "-c: $(cat "$T/out/cont.err")"
[ "$(last_model)" = n ] || fail "-c: sent to $(last_model)"
pic askit ask --session conv-a 'asked' || fail "ask --session: $(cat "$T/out/askit.err")"
[ "$(last_model)" = n ] || fail "ask --session: sent to $(last_model)"
printf 'PASS: -p, -c and ask --session go on with the conversation'"'"'s own model, and say so\n'

# A new conversation still starts on the saved default.
pic fresh -p 'fresh' || fail "fresh: $(cat "$T/out/fresh.err")"
[ "$(last_model)" = m ] || fail "fresh: sent to $(last_model)"
grep -qF 'pic: model fake/m (your saved default;' "$T/out/fresh.err" || fail "fresh: notice: $(cat "$T/out/fresh.err")"
printf 'PASS: a new conversation starts on the saved default\n'

# --model wins for one run, and that run becomes the conversation's own.
pic named --model fake/m --session-id conv-a -p 'named' || fail "named: $(cat "$T/out/named.err")"
[ "$(last_model)" = m ] || fail "named: sent to $(last_model)"
! grep -qF 'pic: model ' "$T/out/named.err" || fail "named: echoed: $(cat "$T/out/named.err")"
pic after --session-id conv-a -p 'after' || fail "after: $(cat "$T/out/after.err")"
[ "$(last_model)" = m ] || fail "after: the switch was not kept: sent to $(last_model)"
pic back --model other/n --session-id conv-a -p 'back' || fail "back: $(cat "$T/out/back.err")"
printf 'PASS: --model wins for a run and becomes the conversation'"'"'s own\n'

# pic sessions --json: each conversation's own model and key.
pic list sessions --json || fail "sessions: $(cat "$T/out/list.err")"
grep '"id":"conv-a"' "$T/out/list.out" | grep -qF '"model":"other/n"' || fail "sessions: model: $(cat "$T/out/list.out")"
grep '"id":"conv-a"' "$T/out/list.out" | grep -qF '"credential":"other:env"' ||
    fail "sessions: credential: $(cat "$T/out/list.out")"
printf 'PASS: pic sessions --json reports each conversation'"'"'s model and key\n'

# The model's key is gone: one line naming the model and the way on, and nothing is sent.
before=$(requests)
rc=0
(unset OTHER_API_KEY; pic nokey --session-id conv-a -p 'nokey') || rc=$?
[ "$rc" != 0 ] || fail "nokey: exit 0"
[ "$(requests)" = "$before" ] || fail "nokey: a request was sent"
[ "$(wc -l < "$T/out/nokey.err" | tr -d ' ')" = 1 ] || fail "nokey: not one line: $(cat "$T/out/nokey.err")"
grep -qF 'pic: this conversation ran on other/n, and other has no key now' "$T/out/nokey.err" ||
    fail "nokey: $(cat "$T/out/nokey.err")"
grep -qF -- '--model' "$T/out/nokey.err" || fail "nokey: no way on: $(cat "$T/out/nokey.err")"
(unset OTHER_API_KEY; pic onward --model fake/m --session-id conv-a -p 'onward') || fail "onward: $(cat "$T/out/onward.err")"
[ "$(last_model)" = m ] || fail "onward: sent to $(last_model)"
pic back2 --model other/n --session-id conv-a -p 'back' || fail "back2: $(cat "$T/out/back2.err")"
printf 'PASS: a conversation whose key is gone stops with one line, and --model goes on\n'

# A model pic no longer knows (its provider is gone from the models file): the same.
pic gone --model other/n --session-id conv-g -p 'gone' || fail "gone: $(cat "$T/out/gone.err")"
cp "$T/models.json" "$T/models.full"
sed 's/"other":/"elsewhere":/' "$T/models.full" > "$T/models.json"
before=$(requests)
rc=0
pic unknown --session-id conv-g -p 'unknown' || rc=$?
[ "$rc" != 0 ] || fail "unknown: exit 0"
[ "$(requests)" = "$before" ] || fail "unknown: a request was sent"
grep -qF 'pic: this conversation ran on other/n' "$T/out/unknown.err" || fail "unknown: $(cat "$T/out/unknown.err")"
cp "$T/models.full" "$T/models.json"
printf 'PASS: a conversation on a model pic no longer knows stops with one line\n'

# A fork goes on with its parent's model.
pic fork fork conv-g || fail "fork: $(cat "$T/out/fork.err")"
forked=$(head -1 "$T/out/fork.out")
pic forked -p --session "$forked" 'forked' || fail "forked: $(cat "$T/out/forked.err") $(cat "$T/out/fork.out")"
[ "$(last_model)" = n ] || fail "forked: sent to $(last_model)"
printf 'PASS: a fork goes on with its parent'"'"'s model\n'

# RPC: --session and switch_session take the conversation's own model.
rpc() { # NAME LINES: the RPC replies in $T/out/NAME.out
    printf '%s\n' "$2" | "$PIC" --db "$T/j" --workspace "$T/ws" --mode rpc ${3:+--session "$3"} > "$T/out/$1.out" 2> "$T/out/$1.err" ||
        fail "rpc $1: $(cat "$T/out/$1.err")"
}
rpc rpc1 '{"id":"s","type":"get_state"}' conv-g
grep '"id":"s"' "$T/out/rpc1.out" | grep -qF '"id":"n"' || fail "rpc --session: $(cat "$T/out/rpc1.out")"
rpc rpc2 '{"id":"a","type":"get_state"}
{"id":"w","type":"switch_session","sessionPath":"conv-g"}
{"id":"b","type":"get_state"}'
grep '"id":"a"' "$T/out/rpc2.out" | grep -qF '"id":"m"' || fail "rpc before: $(cat "$T/out/rpc2.out")"
grep '"id":"b"' "$T/out/rpc2.out" | grep -qF '"id":"n"' || fail "rpc switch_session: $(cat "$T/out/rpc2.out")"
printf 'PASS: RPC --session and switch_session go on with the conversation'"'"'s model\n'

if [ "${1:-}" = --no-faults ]; then
    printf 'SKIP: the named-key half needs the dev build (PIC_TEST_PASSPHRASE)\n'
    exit 0
fi

# Named keys: a conversation keeps the key it ran with, whichever is active.
export PIC_TEST_PASSPHRASE="pic-sm-passphrase-$$"
unset FAKE_API_KEY
printf '%s\n' "pic-sm-one-$$" | "$PIC" --db "$T/j" --workspace "$T/ws" login fake > /dev/null 2> "$T/out/l1.err" ||
    fail "login: $(cat "$T/out/l1.err")"
printf '%s\n' "pic-sm-two-$$" | "$PIC" --db "$T/j" --workspace "$T/ws" auth login fake --api-key --as second > /dev/null 2> "$T/out/l2.err" ||
    fail "login --as: $(cat "$T/out/l2.err")"
sent_key() { sed -n 's/^x-api-key: //p' "$T/log/$(requests).req" | tr -d '\r'; }
pic k1 --model fake/m --key second --session-id conv-k -p 'k1' || fail "k1: $(cat "$T/out/k1.err")"
[ "$(sent_key)" = "pic-sm-two-$$" ] || fail "k1: sent the wrong key"
pic k2 --session-id conv-k -p 'k2' || fail "k2: $(cat "$T/out/k2.err")"
[ "$(sent_key)" = "pic-sm-two-$$" ] || fail "k2: the conversation's key was not kept"
grep -qF 'pic: model fake/m, key "second" (this conversation'"'"'s own; --model to change)' "$T/out/k2.err" ||
    fail "k2: notice: $(cat "$T/out/k2.err")"
pic k3 --key default --session-id conv-k -p 'k3' || fail "k3: $(cat "$T/out/k3.err")"
[ "$(sent_key)" = "pic-sm-one-$$" ] || fail "k3: --key did not win"
pic k4 --session-id conv-k -p 'k4' || fail "k4: $(cat "$T/out/k4.err")"
[ "$(sent_key)" = "pic-sm-one-$$" ] || fail "k4: --key's run did not become the conversation's own"
printf 'PASS: a conversation keeps its key; --key wins for a run and is kept\n'

# The conversation's key is gone: one line naming the key, and nothing is sent.
pic k5 --key second --session-id conv-k -p 'k5' || fail "k5: $(cat "$T/out/k5.err")"
pic out auth logout fake:second || fail "logout: $(cat "$T/out/out.err")"
before=$(requests)
rc=0
pic k6 --session-id conv-k -p 'k6' || rc=$?
[ "$rc" != 0 ] || fail "k6: exit 0"
[ "$(requests)" = "$before" ] || fail "k6: a request was sent"
grep -qF 'pic: this conversation ran on fake/m with the key "second", which fake no longer has' "$T/out/k6.err" ||
    fail "k6: $(cat "$T/out/k6.err")"
pic k7 --key default --session-id conv-k -p 'k7' || fail "k7: $(cat "$T/out/k7.err")"
[ "$(sent_key)" = "pic-sm-one-$$" ] || fail "k7: --key did not go on"
printf 'PASS: a conversation whose key is gone stops with one line naming it\n'