#!/bin/sh # # splinter -- every streamed turn asks for the usage chunk. # # What it guards: automatic compaction is judged by the prompt size the # provider reports (stats.last_in), and a plain turn used to send no # stream_options.include_usage. A provider that follows the OpenAI contract # then sends no `usage` at all, last_in stayed 0, and compact_threshold never # fired - the conversation grew until the provider cut it (measured: a 253 KB # request against compact_threshold = 128000, 2026-11-22). # # The fake endpoint here behaves like that contract: usage only when asked for. # Two turns in ONE process, because compaction needs the previous turn's size. # Without the fix the second request carries the whole conversation; with it # the second is compacted down to the target. set -u cd "$(dirname "$0")/.." || exit 1 . "$(dirname "$0")/sh/port.sh" D=$(mktemp -d "${TMPDIR:-/var/tmp}/splinter-usage-XXXXXX") || exit 1 P=$(free_port) F= S= fails=0 cleanup() { [ -n "$F" ] && kill "$F" 2>/dev/null [ -n "$S" ] && kill "$S" 2>/dev/null rm -rf "$D" } trap cleanup EXIT fail() { echo "FAIL $*"; fails=$((fails + 1)); } mkdir -p "$D/home" "$D/sess" # A session past the threshold: 40 turns of 1400 bytes plus answers, ~59 kB. # compact_threshold is in tokens and the byte budget is threshold * 4. python3 -I - "$D/sess/sess-u.jsonl" <<'PY' import json, sys out = [] for i in range(1, 41): out.append({"role": "user", "text": "frage %d %s" % (i, "y" * 1400)}) out.append({"role": "assistant", "text": "antwort %d" % i}) open(sys.argv[1], "w").write("".join(json.dumps(l) + "\n" for l in out)) PY cat > "$D/conf" < "$D/fake.log" 2>&1 & F=$! if ! wait_listen "$P" "$F"; then echo "FAIL the fake endpoint did not start" cat "$D/fake.log" exit 1 fi HOME="$D/home" ./splinter --config "$D/conf" \ --serve --socket "$D/s.sock" \ --endpoint "http://127.0.0.1:$P/v1" --model m \ --session-id sess-u --resume \ > "$D/serve.log" 2>&1 & S=$! i=0 while [ "$i" -lt 60 ]; do [ -S "$D/s.sock" ] && break i=$((i + 1)) sleep 0.2 done if [ ! -S "$D/s.sock" ]; then echo "FAIL the server did not start" cat "$D/serve.log" exit 1 fi ./splinterctl --socket "$D/s.sock" --say "turn eins" > /dev/null 2>&1 ./splinterctl --socket "$D/s.sock" --say "turn zwei" > /dev/null 2>&1 if [ ! -s "$D/requests.log" ]; then echo "FAIL no request reached the endpoint" cat "$D/serve.log" exit 1 fi # every streamed turn must have asked; a provider that only answers on request # is exactly the case that used to leave last_in at 0 n=$(wc -l < "$D/requests.log") asks=$(grep -c 'include_usage=True' "$D/requests.log") if [ "$asks" -ne "$n" ]; then fail "only $asks of $n requests asked for usage" fi msgs=$(tail -1 "$D/requests.log" | sed -n 's/.*msgs=\([0-9]*\).*/\1/p') bytes=$(tail -1 "$D/requests.log" | sed -n 's/.*bytes=\([0-9]*\).*/\1/p') if [ -z "$msgs" ] || [ -z "$bytes" ]; then fail "cannot read the second request" cat "$D/requests.log" elif [ "$msgs" -ge 40 ]; then fail "the second turn still carried $msgs messages" \ "(compaction did not fire; request was $bytes bytes)" else echo "ok second turn: $bytes bytes, $msgs messages (compacted)" fi if [ "$fails" -ne 0 ]; then cat "$D/requests.log" cat "$D/serve.log" exit 1 fi echo "test_usage_request: ok" exit 0