Skip to content
pi-c Artifact [3d0d7a7db9]
Login

Artifact [3d0d7a7db9]

Artifact 3d0d7a7db98d9a770db4aaa984e9ccc59451b58f59eda0945d4a8f2c76fe40be:


#!/bin/sh
# Pi's tool selection (--tools, --exclude-tools, --no-tools, --no-builtin-tools,
# the defaultTools setting) against a local fake server: the tool definitions
# and the system prompt that reach the provider, and what a tool the model
# calls but that is not in play answers.
set -eu
PIC=${PIC:?set PIC to the pic binary}
SERVER=${SERVER:?set SERVER to the fake_server binary}
STREAMS=$(cd "$(dirname "$0")/streams" && pwd)
CHAT=$(cd "$(dirname "$0")/chat-streams" && pwd)
RESP=$(cd "$(dirname "$0")/responses-streams" && pwd)
GEM=$(cd "$(dirname "$0")/gemini-streams" && pwd)
T=$(mktemp -d "${TMPDIR:-/tmp}/pic-toolset.XXXXXX")
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; }
mkdir "$T/home"
export HOME="$T/home" PIC_MODEL_CATALOG=offline ANTHROPIC_API_KEY=pic-test-key OPENROUTER_API_KEY=pic-test-key OPENAI_API_KEY=pic-test-key GEMINI_API_KEY=pic-test-key
n=0
FLAGS=
# The wake tool (loops.tool) is pic's own default beside Pi's four; off here, so the cases below
# are Pi's, and on again for the last one.
mkdir -p "$HOME/.pic"
"$PIC" settings loops.tool false > /dev/null

# case_ SPEC...: a fresh journal, workspace and fake server, one submission.
case_() {
    n=$((n + 1))
    DB="$T/$n.sqlite"; WS="$T/ws$n"; LOG="$T/log$n"
    mkdir "$WS" "$LOG"
    [ -n "$spid" ] && kill "$spid" 2>/dev/null && wait "$spid" 2>/dev/null || true
    rm -f "$T/port"
    "$SERVER" "$T/port" "$LOG" 60 "$@" &
    spid=$!
    i=0
    while [ ! -s "$T/port" ]; do
        i=$((i + 1))
        [ "$i" -lt 600 ] || fail "server did not start"
        sleep 0.05
    done
    URL="http://127.0.0.1:$(cat "$T/port")/v1/messages"
    # shellcheck disable=SC2086
    "$PIC" $FLAGS --db "$DB" --workspace "$WS" submit job 'go' > /dev/null
}
run() { # shellcheck disable=SC2086
    "$PIC" $FLAGS --db "$DB" --workspace "$WS" run --provider anthropic --model anthropic/test-model --api-url "$URL" 2> "$T/stderr"; }
# The tool names of request K, in the order they were declared.
names() { grep -o '{"name":"[a-z]*","description"' "$LOG/${1:-1}.req" | sed 's/{"name":"\([a-z]*\)".*/\1/' | tr '\n' ' ' | sed 's/ $//'; }

# check LABEL WANT [FLAGS...]: one request with FLAGS, declaring exactly WANT.
check() {
    label=$1 want=$2
    shift 2
    FLAGS="$*"
    case_ "sse:$STREAMS/text.sse"
    run > /dev/null || fail "$label: run failed: $(cat "$T/stderr")"
    got=$(names)
    [ "$got" = "$want" ] || fail "$label: declared '$got', wanted '$want'"
    printf 'PASS: %s\n' "$label"
}

check 'default tools are Pi'"'"'s four' 'read bash edit write'
check '--tools replaces them, in the order given' 'read grep find ls' --tools read,grep,find,ls
check '-t is --tools' 'ls grep' -t ls,grep
check '--tools takes patterns' 'grep' --tools 'g*'
check '--tools +name adds to the defaults' 'read bash edit write grep' --tools +grep
check '--tools -name,+name changes them' 'read edit write ls' --tools -bash,+ls
check '--exclude-tools removes' 'read write' --exclude-tools bash,edit
check '--exclude-tools beats --tools' 'read' --tools read,grep --exclude-tools grep
check '-xt is --exclude-tools' 'read bash edit' -xt write
check '--no-tools declares none' '' --no-tools
check '--no-tools with --tools keeps the list' 'grep' --no-tools --tools grep
check '--no-builtin-tools declares none' '' --no-builtin-tools
check '-nt is --no-tools' '' -nt

# No tools: an empty array to Messages, no tools at all to Chat Completions.
FLAGS=--no-tools
case_ "sse:$STREAMS/text.sse"
run > /dev/null || fail "no tools: run failed"
grep -q '"tools":\[\]' "$LOG/1.req" || fail "no tools: Messages request has no empty tools array"
case_ "sse:$CHAT/text.sse"
# shellcheck disable=SC2086
"$PIC" $FLAGS --db "$DB" --workspace "$WS" run --model openrouter/test/model --api-url "$URL" 2> "$T/stderr" ||
    fail "no tools chat: run failed: $(cat "$T/stderr")"
grep -q '"tools"' "$LOG/1.req" && fail "no tools chat: a tools field was sent"
case_ "sse:$RESP/reasoning_text.sse"
# shellcheck disable=SC2086
"$PIC" $FLAGS --db "$DB" --workspace "$WS" run --model openai/test-model --api-url "$URL" 2> "$T/stderr" ||
    fail "no tools responses: run failed: $(cat "$T/stderr")"
grep -q '"tools"' "$LOG/1.req" && fail "no tools responses: a tools field was sent"
case_ "sse:$GEM/thinking_text.sse"
# shellcheck disable=SC2086
"$PIC" $FLAGS --db "$DB" --workspace "$WS" run --model google/test-model --api-url "$URL" 2> "$T/stderr" ||
    fail "no tools gemini: run failed: $(cat "$T/stderr")"
grep -q '"tools"' "$LOG/1.req" && fail "no tools gemini: a tools field was sent"
grep -q '"contents"' "$LOG/1.req" || fail "no tools gemini: request body lost"
printf 'PASS: no tools: [] for Messages, omitted for Chat Completions, Responses and Gemini\n'

# A bad list is refused before anything runs.
for bad in 'read,+grep' '+gr*'; do
    if "$PIC" --tools "$bad" --db "$T/bad.sqlite" --workspace "$T" sessions > /dev/null 2> "$T/stderr"; then
        fail "bad list $bad: accepted"
    fi
    grep -q '^pic: --tools: ' "$T/stderr" || fail "bad list $bad: $(cat "$T/stderr")"
done
grep -q 'cannot be mixed with +name or -name entries' "$T/stderr" 2>/dev/null || true
[ ! -e "$T/bad.sqlite" ] || fail "bad list: a journal was created"
printf 'PASS: a mixed or patterned +name list is refused\n'

# The defaultTools setting: plain names replace, +name and -name change.
FLAGS=
"$PIC" settings defaultTools read,grep > /dev/null
check 'defaultTools replaces the default' 'read grep'
"$PIC" settings defaultTools -bash,+ls > /dev/null
check 'defaultTools -name,+name changes the default' 'read edit write ls'
"$PIC" settings defaultTools '' > /dev/null
check 'an empty defaultTools is no tools' ''
"$PIC" settings defaultTools read,bash > /dev/null
check '--tools +name stacks on defaultTools' 'read bash ls' --tools +ls
check '--tools replaces defaultTools' 'find' --tools find
check '--no-tools beats defaultTools' '' --no-tools
"$PIC" settings --unset defaultTools > /dev/null
check 'unset defaultTools is Pi'"'"'s four again' 'read bash edit write'
"$PIC" settings --unset loops.tool > /dev/null
check 'with loops.tool on, the default tools add wake' 'read bash edit write wake'

# The system prompt follows the tools in play.
FLAGS=
case_ "sse:$STREAMS/text.sse"
run > /dev/null || fail "prompt: run failed"
grep -q -- '- bash: Execute bash commands' "$LOG/1.req" || fail "prompt: bash not listed"
grep -q 'Use bash for file operations like ls, rg, find' "$LOG/1.req" || fail "prompt: bash rule missing"
FLAGS='--tools read,bash,grep,find,ls'
case_ "sse:$STREAMS/text.sse"
run > /dev/null || fail "prompt grep: run failed"
grep -q -- '- grep: Search file contents for patterns' "$LOG/1.req" || fail "prompt: grep not listed"
grep -q -- '- find: Find files by glob pattern' "$LOG/1.req" || fail "prompt: find not listed"
grep -q -- '- ls: List directory contents' "$LOG/1.req" || fail "prompt: ls not listed"
grep -q 'Use bash for file operations like ls, rg, find' "$LOG/1.req" && fail "prompt: bash rule kept with grep, find and ls"
grep -q 'Use edit for targeted changes' "$LOG/1.req" && fail "prompt: edit rule kept without edit"
printf 'PASS: the system prompt lists the tools in play and their rules\n'

# A tool the model calls but that is not in play: Pi's error, nothing runs.
FLAGS='--exclude-tools write'
case_ "sse:$STREAMS/tool_use.sse" "sse:$STREAMS/text.sse"
run > /dev/null || fail "disabled: run failed: $(cat "$T/stderr")"
[ ! -e "$WS/notes.txt" ] || fail "disabled: the write ran"
grep -q '"type":"tool_result","tool_use_id":"toolu_01","content":"Tool write not found","is_error":true' "$LOG/2.req" ||
    fail "disabled: the call did not get Pi's error"
printf 'PASS: a tool not in play answers "Tool write not found"\n'