Artifact 9cf85a3c6ea3148bf1604262f260e2305319eb4dc757f40ca633537536c8f73c:
- Executable file
test/pic/modelchoice.sh
— part of check-in
[c8bc1bfa6d]
at
2026-11-23 11:40:21
on branch ux-models
— Model choice says why and recovers from a refused key
With no saved default, pic starts on a provider whose key worked and passes over one that was refused, out of credit or limited; OpenRouter, Moonshot and DeepSeek start on inexpensive general models. The first screen (and stderr for ask and -p) names the model and why; with no key it says how to get one. The picker shows prices and key marks, lists keyless models once typed, ranks exact and prefix matches first, says what it saved and when the thinking level changes. After a refused key, Alt+P picks another model and the message returns to the editor for it. pic sessions shows each conversation's model and marks failed ones; pic models --json carries key; pic --version; pic help start opens with the user path. (user: dmestas, size: 5640) [annotate] [blame] [check-ins using]
#!/bin/sh # How pic chooses a model with no saved default, and says so: a provider # whose key is known to work beats one whose key was refused; OpenRouter # starts on an inexpensive general model; ask and -p name the model and why # on stderr; pic models --json marks each provider's key; pic sessions shows # each conversation's model and marks a failed one; pic --version prints the # version. The providers' endpoints point at a fake server: nothing leaves # the machine. set -eu PIC=${PIC:?set PIC to the pic binary} SERVER=${SERVER:?set SERVER to the fake_server binary} here=$(cd "$(dirname "$0")" && pwd) C=$here/chat-streams T=$(mktemp -d "${TMPDIR:-/tmp}/pic-modelchoice.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" export HOME="$T/home" PIC_PI_EXPORT=0 PIC_MODEL_CATALOG=offline pic() { "$PIC" --db "$T/j" --workspace "$T/ws" "$@"; } # --version: the date of the newest change, alone on stdout. "$PIC" --version > "$T/v.out" 2> "$T/v.err" || fail "--version: $(cat "$T/v.err")" grep -Eq '^pic [0-9]{4}-[0-9]{2}-[0-9]{2}$' "$T/v.out" || fail "--version: $(cat "$T/v.out")" [ ! -s "$T/v.err" ] || fail "--version wrote to stderr: $(cat "$T/v.err")" printf 'PASS: pic --version prints the version\n' printf '{"type":"error","error":{"type":"authentication_error","message":"invalid x-api-key"}}' > "$T/auth.json" # 1 OpenRouter's reply, 2 Anthropic refuses the key, 3 OpenRouter again "$SERVER" "$T/port" "$T/log" 60 "sse:$C/text.sse" "status:401:$T/auth.json" "sse:$C/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") printf '{"providers":{"anthropic":{"baseUrl":"http://127.0.0.1:%s"},"openrouter":{"baseUrl":"http://127.0.0.1:%s/api/v1"}}}' \ "$port" "$port" > "$T/models.json" export PIC_MODELS_FILE="$T/models.json" # Only an OpenRouter key: an inexpensive general model, and why, on stderr. OPENROUTER_API_KEY=pic-mc-or pic ask 'first' > "$T/a.out" 2> "$T/a.err" || fail "openrouter: $(cat "$T/a.err")" grep -qF 'pic: model openrouter/deepseek/deepseek-v4.1-flash (OPENROUTER_API_KEY is set; --model or pic models --default to change)' "$T/a.err" || fail "openrouter notice: $(cat "$T/a.err")" grep -q '"model":"deepseek/deepseek-v4.1-flash"' "$T/log/1.req" || fail "openrouter: $(grep -o '"model":"[^"]*"' "$T/log/1.req")" printf 'PASS: with only an OpenRouter key, ask starts on an inexpensive model and says why\n' # Both keys, neither known (another OpenRouter key): Pi's order picks Anthropic; -p says so. It is refused. rc=0 ANTHROPIC_API_KEY=pic-mc-bad OPENROUTER_API_KEY=pic-mc-or-new pic -p 'second' < /dev/null > "$T/b.out" 2> "$T/b.err" || rc=$? [ "$rc" != 0 ] || fail "refused: exit 0" grep -qF 'pic: model anthropic/claude-opus-5-5 (ANTHROPIC_API_KEY is set;' "$T/b.err" || fail "-p notice: $(cat "$T/b.err")" [ ! -s "$T/b.out" ] || fail "-p: the notice reached stdout" # Anthropic's key is now known to be refused: the OpenRouter key, whose last reply came back, wins, and the notice says why. ANTHROPIC_API_KEY=pic-mc-bad OPENROUTER_API_KEY=pic-mc-or pic ask 'third' > "$T/c.out" 2> "$T/c.err" || fail "healthy: $(cat "$T/c.err")" grep -qF "pic: model openrouter/deepseek/deepseek-v4.1-flash (OPENROUTER_API_KEY is set; anthropic's key was refused;" "$T/c.err" || fail "healthy notice: $(cat "$T/c.err")" printf 'PASS: a provider whose key works beats one whose key was refused\n' # pic models --json: each row says how its provider's key stands. ANTHROPIC_API_KEY=pic-mc-bad OPENROUTER_API_KEY=pic-mc-or pic models --all --json > "$T/models.out" grep '"model":"anthropic/claude-opus-5-5"' "$T/models.out" | grep -q '"key":"refused"' || fail "models: anthropic $(grep '"model":"anthropic/claude-opus-5-5"' "$T/models.out")" grep '"model":"openrouter/deepseek/deepseek-v4.1-flash"' "$T/models.out" | grep -q '"key":"ok"' || fail "models: openrouter" grep '"model":"openai/' "$T/models.out" | head -1 | grep -q '"key":"none"' || fail "models: openai" printf 'PASS: pic models marks each provider key as ok, refused or none\n' # pic sessions: the model each conversation last used, and a failed one marked. pic sessions > "$T/s.out" grep -q 'openrouter/deepseek/deepseek-v4.1-flash .*third' "$T/s.out" || fail "sessions: model: $(cat "$T/s.out")" grep -q 'anthropic/claude-opus-5-5 .*second.*(last request failed)' "$T/s.out" || fail "sessions: failed: $(cat "$T/s.out")" [ "$(grep -c 'failed' "$T/s.out")" = 1 ] || fail "sessions: only the refused one failed: $(cat "$T/s.out")" printf 'PASS: pic sessions shows each conversation model and marks a failed one\n' # A saved default is kept, and named as such; --model says it came from the flag. pic models --default openrouter/deepseek/deepseek-v4-flash > /dev/null ANTHROPIC_API_KEY=pic-mc-bad OPENROUTER_API_KEY=pic-mc-or pic -p 'x' < /dev/null > /dev/null 2> "$T/d.err" || : grep -qF 'pic: model openrouter/deepseek/deepseek-v4-flash (your saved default;' "$T/d.err" || fail "saved: $(cat "$T/d.err")" ANTHROPIC_API_KEY=pic-mc-bad pic --model anthropic/claude-haiku-5-5 -p 'x' < /dev/null > /dev/null 2> "$T/e.err" || : grep -qF 'pic: model anthropic/claude-haiku-5-5 (from --model)' "$T/e.err" || fail "--model: $(cat "$T/e.err")" pic settings --unset defaultModel > /dev/null printf 'PASS: a saved default and --model are named as the reason\n'