diff --git a/Makefile b/Makefile index 0eb0a86..4fc875d 100644 --- a/Makefile +++ b/Makefile @@ -342,6 +342,7 @@ check: $(LIB) $(BIN) $(CLIENT) $(PLUGIN) $(TESTS) tests/test_junction echo "== tests/test_attach_busy.sh"; sh tests/test_attach_busy.sh; \ echo "== tests/test_gateway_openai.sh"; sh tests/test_gateway_openai.sh; \ echo "== tests/test_usage_request.sh"; sh tests/test_usage_request.sh; \ + echo "== tests/test_compact_resume.sh"; sh tests/test_compact_resume.sh; \ echo "== tests/test_reconnect.sh"; sh tests/test_reconnect.sh; \ echo "== tests/test_junction.sh"; sh tests/test_junction.sh; \ echo "== tests/test_mcpsession.sh"; sh tests/test_mcpsession.sh; \ diff --git a/src/backend/agent.c b/src/backend/agent.c index 06d398d..22314ea 100644 --- a/src/backend/agent.c +++ b/src/backend/agent.c @@ -805,10 +805,22 @@ agent_loop(struct sp_agent *a, struct sp_conv *conv, int resume, int decision, * The token threshold is the automatic compaction: past * it, the request is compacted to the target instead of * waiting for the byte ceiling. + * + * last_in is only known after a turn the provider + * answered with usage. A resumed session starts at 0, + * and a provider that does not report usage leaves it + * there. Such a session grows past every endpoint's + * window, and then the first request fails before it + * can report a size - so compaction never fires and + * every further resume fails the same way. The local + * bytes are always there; the same four bytes per + * token the target below uses turn them into a size + * that can be judged before anything is sent. */ if (a->cfg->compact_threshold > 0 && a->cfg->compact_target > 0 && - a->stats.last_in > a->cfg->compact_threshold) + (a->stats.last_in > a->cfg->compact_threshold || + cb > a->cfg->compact_threshold * 4)) cbudget = a->cfg->compact_target * 4; /* * The engine always builds the host, plugins or not: diff --git a/tests/test_compact_resume.sh b/tests/test_compact_resume.sh new file mode 100755 index 0000000..8ff5369 --- /dev/null +++ b/tests/test_compact_resume.sh @@ -0,0 +1,126 @@ +#!/bin/sh +# +# splinter -- a resumed session that is already over the window compacts +# before its first request. +# +# What it guards: compaction used to be judged only by stats.last_in, the +# prompt size a provider reports with its usage chunk. A session starts at 0 +# after --resume, so a transcript that grew past every endpoint's window was +# sent whole on the first turn; the endpoint refused it, no usage ever came +# back, and the session stayed unusable for every later resume as well. +# Measured 2026-11-22: 9,095,724 bytes, ctx=2,286,261 at junction, 503 +# no_endpoint_available on every retry. +# +# The fix also judges the local conversation bytes (compact_threshold * 4), +# which are there before anything is sent. One turn in ONE process: without +# the fix that request carries the whole session; with it, the compactor has +# run and the request is down at the target. +set -u +cd "$(dirname "$0")/.." || exit 1 +. "$(dirname "$0")/sh/port.sh" +D=$(mktemp -d "${TMPDIR:-/var/tmp}/splinter-compact-resume-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" + +# 150 turns of 2000 bytes plus answers: ~300 kB, well past threshold * 4 +# (50000 tokens * 4 bytes). Written in the old chat shape on purpose - the +# resumed-session loader has to read what older binaries wrote. +python3 -I - "$D/sess/sess-big.jsonl" <<'PY' +import json, sys +out = [] +for i in range(1, 151): + out.append({"role": "user", "text": "frage %d %s" % (i, "y" * 2000)}) + 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-big --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 + +if [ ! -s "$D/requests.log" ]; then + echo "FAIL no request reached the endpoint" + cat "$D/serve.log" + exit 1 +fi + +n=$(wc -l < "$D/requests.log") +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 [ "$n" -ne 1 ]; then + fail "expected one request, got $n" +fi +if [ -z "$msgs" ] || [ -z "$bytes" ]; then + fail "cannot read the request" + cat "$D/requests.log" +elif [ "$msgs" -ge 60 ]; then + fail "the first request after resume still carried $msgs messages" \ + "(compaction did not fire; request was $bytes bytes)" +elif [ "$bytes" -ge 100000 ]; then + fail "the first request after resume was still $bytes bytes" +else + echo "ok first turn after resume: $bytes bytes, $msgs messages (compacted)" +fi + +# The compactor must have run before the request, and the turn must have +# finished: the endpoint reports usage, so a later resume has a size again. +if ! grep -q "compacted" "$D/serve.log"; then + echo "note: no compaction line in the server log (the request is the proof)" +fi + +if [ "$fails" -ne 0 ]; then + cat "$D/requests.log" + cat "$D/serve.log" + exit 1 +fi +echo "test_compact_resume: ok" +exit 0