#!/bin/sh
# tiercross.sh -- two conversations through the tiers at once. Does either get the other's state?
#
# WHY THIS IS NOT tierident.sh WITH A SECOND PROMPT. That script asks ONE prompt four times and
# compares the four answers, which proves a session survives the round trip. It cannot, by
# construction, see the failure this one is for: with a single conversation in the server there is
# no other conversation's bytes for it to be served. The cross-contamination failures a tiered
# cache can have -- an entry holding reassigned block ids, a checkpoint slot freed by one session
# and still named by another -- need two sessions in flight to show up at all.
#
# THE SCARCE RESOURCE IS THE CHECKPOINT SLOT, so run this with --checkpoint-slots low enough that
# the two sessions fight over it. That is what makes the retention policy and the victim scan run,
# which is what makes a snapshot get retired underneath a record that still names its slot:
#
# --checkpoint-slots 2 --prefix-cache-host-mib 512 --prefix-cache-disk-mib 16384 \
# --prefix-cache-dir
#
# EACH SESSION IS COMPARED ONLY TO ITSELF. A and B are different prompts and their answers differ
# for ordinary reasons; what must hold is that A's restored answer is A's cold answer. Corrupt
# state does not produce babble, it produces B's train of thought under A's question.
set -e
P=${P:-8100}
H=${H:-127.0.0.1}
TO_HOST=${TO_HOST:-10}
TO_DISK=${TO_DISK:-180}
FILL=${FILL:-1200}
N=${N:-48}
work=$(mktemp -d "${TMPDIR:-/tmp}/tiercross.XXXXXX")
trap 'rm -rf "$work"' EXIT INT TERM
command -v jq >/dev/null || { echo "tiercross: jq is required" >&2; exit 2; }
# TWO PROMPTS THAT SHARE NO PREFIX. A shared opening would put both sessions on the same chain for
# its length, and the blocks they then share are legitimately one another's -- which is the whole
# point of a prefix cache and would make a difference here unreadable. They diverge at token one.
#
# EACH HAS TO PRODUCE AN ANSWER. A wall of repeated sentences is a passage an instruction-tuned
# model can reasonably answer with a single EOS, and every arm of that session would then compare
# an empty string to an empty string. Both lengths are checked below, because a vacuous green here
# is worse than a red.
mk() { # mk
: > "$1"
i=0
while [ $i -lt 300 ]; do
printf '%s %d. %s ' "$2" "$i" "$3" >> "$1"
i=$((i + 1))
done
printf '\n%s\n\nAnswer: ' "$4" >> "$1"
}
mk "$work/a.txt" "Chapter" "The paged pool is layer-major, so one logical block is many small fragments rather than one extent." \
"Using only the passage above, write three sentences about it."
mk "$work/b.txt" "Entry" "The linear layer keeps one state per sequence and updates it token by token." \
"Using only the passage above, write three sentences about it."
# THE FILES ARE THE PROMPTS, READ VERBATIM, through jq's --rawfile. Flattening them into shell
# variables deletes the line breaks the prompts' SHAPE depends on, and a passage whose question
# ends up welded to the last sentence is one this model answers with an immediate EOS (the same
# reasoning is in tierident.sh).
ask() { # ask