#!/bin/sh
# Pi's compaction settings ("compaction" in ~/.pic/pic.sqlite, Pi's
# settings.json shape): reserveTokens moves the threshold, keepRecentTokens
# what is kept, modelOverrides win for their model, enabled false stops
# automatic compaction (threshold and overflow recovery, as Pi's
# _checkCompaction) but not pic compact; Pi's RPC set_auto_compaction sets it
# during a run.
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)
T=$(mktemp -d "${TMPDIR:-/tmp}/pic-compact-settings.XXXXXX")
# pic keeps user state under HOME: never the real one.
export HOME="$T/home"
mkdir -p "$HOME"
spid=
pid=
cleanup() {
exec 3>&- 2>/dev/null || :
[ -n "$spid" ] && kill "$spid" 2>/dev/null
[ -n "$pid" ] && kill -9 "$pid" 2>/dev/null
[ -n "${KEEP:-}" ] && { echo "kept $T" >&2; return; }
rm -rf -- "$T"
}
trap cleanup EXIT HUP INT TERM
fail() { printf 'FAIL: %s\n' "$*" >&2; exit 1; }
export ANTHROPIC_API_KEY=pic-compact-settings-key PIC_PI_EXPORT=0
big() { yes "$1$1$1$1" | head -c 50000 | tr -d '\n'; } # 40000 characters: 10000 tokens by chars/4
WS="$T/ws"
mkdir "$WS"
printf 'text one\ntext two\ntext three\n' > "$T/script"
# A fresh journal $1 with the messages A, B, C (10000 tokens each) run one
# by one at window $2; prints the number of compactions after each.
three() {
DB="$T/$1.sqlite"
shift
out=
for m in A B C; do
"$PIC" --db "$DB" --workspace "$WS" submit "k$m" "$(big $m)" ${sid:+--session "$sid"} > /dev/null
"$PIC" --db "$DB" --workspace "$WS" --context-window "$1" run --script "$T/script" 2>> "$T/stderr" ||
fail "run: $(tail -2 "$T/stderr")"
sid=$("$PIC" --db "$DB" --workspace "$WS" sessions | awk 'NR==1{print $1}')
out="$out$("$PIC" --db "$DB" --workspace "$WS" dump | grep -c '^entry .* compaction ' || :)"
done
sid=
printf '%s\n' "$out"
}
# Defaults (Pi's): at a 26000 window the third message compacts (A summarized).
[ "$(three defaults 26000)" = 001 ] || fail "defaults"
# reserveTokens: at 60000 the default threshold (43616) is never passed; a
# reserve of 45000 (threshold 15000) compacts at the third.
[ "$(three reserve0 60000)" = 000 ] || fail "reserve: compacted at the default reserve"
"$PIC" settings compaction '{"reserveTokens":45000}' > /dev/null
[ "$(three reserve 60000)" = 001 ] || fail "reserve: reserveTokens not applied"
printf 'PASS: compaction.reserveTokens moves the threshold\n'
# keepRecentTokens: keeping 5000 cuts at the newest message, so the second and
# third messages each compact (the default keeps two and waits for the third).
"$PIC" settings compaction '{"keepRecentTokens":5000}' > /dev/null
[ "$(three keep 26000)" = 012 ] || fail "keep: keepRecentTokens not applied"
"$PIC" --db "$T/keep.sqlite" --workspace "$WS" log | grep -q 'compacted [0-9]* tokens' || fail "keep: no summary"
printf 'PASS: compaction.keepRecentTokens decides what is kept\n'
# modelOverrides for another model change nothing; the scripted run has no model.
"$PIC" settings compaction '{"modelOverrides":{"other/x":{"reserveTokens":45000}}}' > /dev/null
[ "$(three override 60000)" = 000 ] || fail "override: another model's override applied"
printf 'PASS: compaction.modelOverrides apply only to their model\n'
# enabled false: no automatic compaction; pic compact still compacts.
"$PIC" settings compaction '{"enabled":false}' > /dev/null
[ "$(three disabled 26000)" = 000 ] || fail "disabled: compacted"
DB="$T/disabled.sqlite"
"$PIC" --db "$DB" --workspace "$WS" compact "$("$PIC" --db "$DB" --workspace "$WS" sessions | awk 'NR==1{print $1}')" > /dev/null
"$PIC" --db "$DB" --workspace "$WS" submit kD "$(big D)" --session "$("$PIC" --db "$DB" --workspace "$WS" sessions | awk 'NR==1{print $1}')" > /dev/null
"$PIC" --db "$DB" --workspace "$WS" --context-window 26000 run --script "$T/script" 2>> "$T/stderr" || fail "disabled: compact: $(tail -2 "$T/stderr")"
[ "$("$PIC" --db "$DB" --workspace "$WS" dump | grep -c '^entry .* compaction ')" = 1 ] || fail "disabled: pic compact did not compact"
printf 'PASS: compaction.enabled false stops automatic compaction; pic compact still works\n'
# At a fake server: an overflow with automatic compaction off is not recovered (Pi).
mkdir "$T/log"
printf '{"type":"error","error":{"type":"invalid_request_error","message":"prompt is too long: 300000 tokens > 200000 maximum"}}' \
> "$T/overflow.json"
"$SERVER" "$T/port" "$T/log" 120 "sse:$STREAMS/text.sse" "status:400:$T/overflow.json" "sse:$STREAMS/bash_hold.sse" \
"sse:$STREAMS/text.sse" &
spid=$!
i=0
while [ ! -s "$T/port" ]; do
i=$((i + 1))
[ "$i" -lt 600 ] || fail "server did not start"
sleep 0.05
done
printf '{"providers":{"fake":{"baseUrl":"http://127.0.0.1:%s","api":"anthropic-messages","apiKey":"$ANTHROPIC_API_KEY","models":[{"id":"m","contextWindow":200000}]}}}' \
"$(cat "$T/port")" > "$T/models.json"
export PIC_MODELS_FILE="$T/models.json"
DB="$T/server.sqlite"
pic() { "$PIC" --db "$DB" --workspace "$WS" --model fake/m "$@"; }
pic ask 'first' > /dev/null 2>> "$T/stderr" || fail "overflow: setup: $(tail -2 "$T/stderr")"
rc=0
pic ask -c 'too long' > /dev/null 2>> "$T/stderr" || rc=$?
[ "$rc" != 0 ] || fail "overflow: recovered with automatic compaction off"
[ "$(ls "$T/log" | grep -c '\.req$')" = 2 ] || fail "overflow: a summary request was made"
pic dump | grep -q '^submission .* failed .*prompt is too long' || fail "overflow: the request did not fail"
pic dump | grep -q ' compaction' && fail "overflow: compacted"
pic check > /dev/null || fail "overflow: integrity"
printf 'PASS: with automatic compaction off an overflow is not recovered, as in Pi\n'
# set_auto_compaction over --commands, while a command runs.
"$PIC" settings --unset compaction
rm -f "$T/in"
mkfifo "$T/in"
"$PIC" --db "$DB" --workspace "$WS" --model fake/m --json --commands ask -c 'hold on' < "$T/in" > "$T/ev" 2> "$T/err" &
pid=$!
exec 3> "$T/in"
i=0
until [ -e "$WS/started" ]; do
i=$((i + 1))
[ "$i" -lt 600 ] || fail "rpc: the command did not start: $(cat "$T/err")"
sleep 0.05
done
await() {
i=0
until grep -qF -- "$1" "$T/ev" 2>/dev/null; do
i=$((i + 1))
[ "$i" -lt 600 ] || fail "rpc: timed out waiting for $1: $(cat "$T/ev")"
sleep 0.05
done
}
printf '%s\n' '{"id":"a1","type":"set_auto_compaction","enabled":false}' >&3
await '{"id":"a1","type":"response","command":"set_auto_compaction","success":true}'
[ "$("$PIC" settings compaction)" = '{"enabled":false}' ] || fail "rpc: setting: $("$PIC" settings compaction)"
printf '%s\n' '{"id":"a2","type":"set_auto_compaction","enabled":"yes"}' >&3
await '{"id":"a2","type":"response","command":"set_auto_compaction","success":false,"error":"enabled must be a boolean"}'
printf '%s\n' '{"id":"a3","type":"set_auto_compaction","enabled":true}' >&3
await '{"id":"a3","type":"response","command":"set_auto_compaction","success":true}'
touch "$WS/release"
exec 3>&-
rc=0
wait "$pid" || rc=$?
pid=
[ "$rc" = 0 ] || fail "rpc: exit $rc: $(cat "$T/err")"
[ "$("$PIC" settings compaction)" = '{"enabled":true}' ] || fail "rpc: setting: $("$PIC" settings compaction)"
"$PIC" settings compaction '{"reserveTokens":1000}' > /dev/null
printf '%s\n' '{"id":"a4","type":"set_auto_compaction","enabled":false}' | pic --json --commands ask -c 'again' > "$T/ev2" 2>> "$T/stderr" ||
fail "rpc 2: $(tail -2 "$T/stderr")"
[ "$("$PIC" settings compaction)" = '{"reserveTokens":1000,"enabled":false}' ] || fail "rpc: other fields lost: $("$PIC" settings compaction)"
pic check > /dev/null || fail "rpc: integrity"
printf 'PASS: set_auto_compaction over --commands sets compaction.enabled, keeping the other fields\n'
# An override for the model in use wins over the ordinary values.
"$PIC" settings compaction '{"reserveTokens":1000,"modelOverrides":{"fake/m":{"reserveTokens":199990,"keepRecentTokens":1}}}' > /dev/null
pic ask -c 'override' > /dev/null 2>> "$T/stderr" || fail "override: $(tail -2 "$T/stderr")"
pic dump | grep -q '^generation .* done compaction reason=threshold' || fail "override: the model's override not applied"
pic check > /dev/null || fail "override: integrity"
printf 'PASS: compaction.modelOverrides for the model in use win\n'