Artifact 7155ec9f69f9cca1902c9641b683c0b295c50b3afcfddb4978084b4f8c4da073:
- File
test/pic/chat.sh
— part of check-in
[376fd483ed]
at
2026-11-20 00:29:28
on branch cache
— Prompt caching on every provider, as Pi's
Anthropic: cache_control on the system block, the last tool and the last block of the final user message; 5 minutes by default, ttl 1h with cacheRetention=long, none with cacheRetention=none (setting in pic.sqlite, PI_CACHE_RETENTION as fallback). Anthropic models through OpenRouter get the same markers. OpenAI Responses and Chat Completions send prompt_cache_key (the conversation id) and 24h retention when long, as Pi does. The system prompt and tools stay byte-identical across turns, and 1-hour cache writes are kept in the stored usage. (user: dmestas, size: 4324) [annotate] [blame] [check-ins using] [more...]
#!/bin/sh # The line mode (pic chat; plain pic in a terminal): messages continue one # conversation with the reply streamed, slash commands manage it, and the # journal is free for other pic commands while the prompt waits. 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-chat.XXXXXX") spid= cpid= cleanup() { exec 3>&- 2>/dev/null || : [ -n "$spid" ] && kill "$spid" 2>/dev/null [ -n "$cpid" ] && kill -9 "$cpid" 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-chat-test-key export HOME="$T/home" # each request writes its Pi lines under HOME mkdir -p "$HOME" "$PIC" settings piSessionFiles 1 # and, asked for here, the files too WS="$T/ws" DB="$T/j.sqlite" mkdir "$WS" "$T/log" "$SERVER" "$T/port" "$T/log" 120 "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" await() { # PATTERN: wait up to 10s for it in the chat's output i=0 until grep -q "$1" "$T/out" 2>/dev/null; do i=$((i + 1)) [ "$i" -lt 200 ] || fail "timed out waiting for $1: $(cat "$T/out")" sleep 0.05 done } mkfifo "$T/in" "$PIC" --db "$DB" --workspace "$WS" --model fake/m chat < "$T/in" > "$T/out" 2> "$T/err" & cpid=$! exec 3> "$T/in" await '^pic: fake/m in ' printf 'first\n' >&3 await 'Hello, "world"' # Idle at the prompt: the journal is free for another pic command. "$PIC" --db "$DB" --workspace "$WS" sessions > "$T/sessions" 2>&1 || fail "idle: journal held: $(cat "$T/sessions")" printf '/name main\nsecond \\\nline two\n' >&3 i=0 until [ "$(ls "$T/log" | wc -l | tr -d ' ')" = 2 ]; do i=$((i + 1)); [ "$i" -lt 200 ] || fail "second message"; sleep 0.05; done await 'Hello, "world".*' sleep 0.3 grep -q '"messages":\[{"role":"user","content":"first"},{"role":"assistant",.*{"role":"user","content":\[{"type":"text","text":"second \\nline two"' "$T/log/2.req" || fail "follow-up: $(grep -o '"messages".*' "$T/log/2.req" | cut -c1-300)" printf '/sessions\n' >&3 await ' main ' printf '/compact focus\n/new\nthird\n' >&3 i=0 until [ "$(ls "$T/log" | wc -l | tr -d ' ')" = 3 ]; do i=$((i + 1)); [ "$i" -lt 200 ] || fail "third message"; sleep 0.05; done sleep 0.3 grep -q '"messages":\[{"role":"user","content":\[{"type":"text","text":"third"' "$T/log/3.req" || fail "/new: not a new conversation" printf '/bogus\n/quit\n' >&3 exec 3>&- i=0 while kill -0 "$cpid" 2>/dev/null; do i=$((i + 1)); [ "$i" -lt 200 ] || fail "chat did not quit"; sleep 0.05; done wait "$cpid" || fail "chat exit status" cpid= [ "$(grep -c 'Hello, "world"' "$T/out")" = 3 ] || fail "replies not shown once each: $(cat "$T/out")" grep -q 'the history is summarized before the next message' "$T/out" || fail "/compact" grep -q 'unknown command /bogus' "$T/err" || fail "unknown command not reported: $(cat "$T/err")" [ "$("$PIC" --db "$DB" --workspace "$WS" sessions | wc -l | tr -d ' ')" = 2 ] || fail "conversations" "$PIC" --db "$DB" --workspace "$WS" check > /dev/null || fail "integrity" [ "$(ls "$HOME"/.pic/sessions/*/*.jsonl | wc -l | tr -d ' ')" = 2 ] || fail "Pi session files: $(ls -R "$HOME/.pic" 2>&1)" grep -q '"type":"session_info".*"name":"main"' "$HOME"/.pic/sessions/*/*.jsonl || fail "Pi session file: name" printf 'PASS: pic chat continues a conversation, runs its commands, frees the journal while idle\n' printf 'PASS: each request leaves its conversation as a Pi session file\n' # chat -c continues the latest conversation. n=$(ls "$T/log" | wc -l | tr -d ' ') printf 'fourth\n/quit\n' | "$PIC" --db "$DB" --workspace "$WS" --model fake/m chat -c > "$T/out2" 2> "$T/err2" || fail "chat -c: $(cat "$T/err2")" grep -q 'continuing ' "$T/out2" || fail "chat -c: banner: $(head -1 "$T/out2")" grep -q '"content":"third"},{"role":"assistant",.*"text":"fourth"' "$T/log/$((n + 1)).req" || fail "chat -c: not continued" printf 'PASS: chat -c continues the latest conversation\n'