openai_stream passed the request options only when c->have_gen was set, and only openai_server.c (splinter as a server, relaying a client's request) ever set it. A normal turn therefore sent no stream_options.include_usage, a provider that follows the OpenAI contract returned no usage, reply.tokens_in stayed 0, last_in stayed 0, and compact_threshold could never fire - a session grew monotonically to 250k tokens with the threshold at 128000. The transport now always asks, with a local option block; the gateway path keeps its caller's settings. Verified against a strict endpoint that sends usage only when asked: before the fix two turns went 82->86 messages with no compaction, after it the second turn is 8 messages and 21 KB. tests/test_usage_request.sh starts that strict endpoint (its usage is emitted only when the request opts in) and asserts the request carries the option and that the second turn is compacted. It fails without the transport change.
121 lines
3.5 KiB
Bash
Executable File
121 lines
3.5 KiB
Bash
Executable File
#!/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" <<EOF
|
|
core {
|
|
locale = en
|
|
session_dir = $D/sess
|
|
embed = off
|
|
compact_threshold = 50000
|
|
compact_target = 2000
|
|
}
|
|
EOF
|
|
|
|
python3 -I tests/fake_strict_usage.py "$P" "$D/requests.log" \
|
|
> "$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
|