serve: --prompt-cache-spread, checkpoints kept spread back from the conversation's end (opt-in)

Least recent use keeps the newest checkpoints and every turn adds one, so after a few short turns all 6 sit in the
last 1-2K tokens. An agent client that rewrites its history a few thousand tokens back then resumes from the system
prompt: on 2x CMP 50HX with the pi coding agent a 210,528-token prompt that was the same as the session for 204,349
tokens resumed at 9,525 and read 201,003 again (100 s).

With the flag the victim is the checkpoint whose removal leaves the smallest gap for its distance from the newest, so
the kept points thin out geometrically: after 200 turns of 1,000 tokens they sit 0, 1K, 3K, 8K, 43K and 200K before
the end, and an edit 6K back reads 8K again (least recent use: 200K), 42K back 43K. The same number of checkpoints, no
more RAM; never the root, the newest or a pinned one, the tail rule first, the plain policy without a candidate. The
resume is the usual exact-prefix match.

On the box: an 82,593-token conversation grown turn by turn, a turn ~6 steps back rewritten, resumed at 73,610 tokens
in 10.2 s against 40.0 s read in full; with the expert placement fixed (--adapt-swaps 0, --pcie-frac 0.17) the
greedy answer was the same text as the full read. conv_cache_test: the spread cases (a 200-turn simulation against
least recent use, the fallbacks). docs/DETAILS.md: a paragraph after the prompt cache's options.
This commit is contained in:
xrip authored and Niko1221 committed 2026-10-15 05:42:53 +02:00
1 parent d9cb22d287
commit a7d4b75443
4 files changed
+93 -2

No files matched your search

+45
View File
@@ -20,6 +20,7 @@
#include <vector>
using strata::program::conv_cache::eviction_victim;
using strata::program::conv_cache::spread_victim;
namespace {
int g_fail = 0;
@@ -29,8 +30,52 @@ void check(bool ok, const char* what) {
}
} // namespace
// a conversation of `turns` turns of `turn` tokens each after a `root`-token system prompt, a checkpoint at every turn's
// end and at most `cap` kept; returns how many tokens a request whose prompt differs `back` tokens before the end reuses
int64_t reuse_after_edit(bool spread, int64_t root, int64_t turn, int turns, int64_t cap, int64_t back) {
std::vector<int64_t> lens = {root};
std::vector<uint64_t> stamps = {1};
for (int t = 1; t <= turns; ++t) {
lens.push_back(root + t * turn);
stamps.push_back(uint64_t(t + 1));
while ((int64_t) lens.size() > cap) {
const size_t v = spread ? spread_victim(lens.data(), stamps.data(), lens.size(), cap, nullptr, nullptr)
: eviction_victim(stamps.data(), stamps.size(), cap);
lens.erase(lens.begin() + (std::ptrdiff_t) v);
stamps.erase(stamps.begin() + (std::ptrdiff_t) v);
}
}
const int64_t edit = lens.back() - back;
int64_t reuse = 0;
for (int64_t l : lens)
if (l <= edit) reuse = l;
return reuse;
}
int main() {
std::printf("conv_cache_test\n");
{ // --prompt-cache-spread: 200 turns of 1,000 tokens after a 9,525-token system prompt, 6 checkpoints
const int64_t end = 9525 + 200 * 1000;
const int64_t lru6 = reuse_after_edit(false, 9525, 1000, 200, 6, 6000);
const int64_t spread6 = reuse_after_edit(true, 9525, 1000, 200, 6, 6000);
std::printf(" an edit 6K before the end: least recent use reuses %lld, spread %lld of %lld\n", (long long) lru6,
(long long) spread6, (long long) end);
check(lru6 == 9525, "least recent use: an edit 6K back falls through to the root");
check(end - spread6 < 4 * 6000, "spread: an edit 6K back reads less than 24K again");
const int64_t spread42 = reuse_after_edit(true, 9525, 1000, 200, 6, 42000);
check(end - spread42 < 4 * 42000, "spread: an edit 42K back reads less than 168K again");
check(reuse_after_edit(true, 9525, 1000, 200, 6, 500) == end - 1000, "spread: the turn before is still kept");
}
{
const std::vector<int64_t> lens = {100, 200, 300, 400};
const std::vector<uint64_t> stamps = {1, 2, 3, 4};
const bool pinned[] = {false, true, true, false};
check(spread_victim(lens.data(), stamps.data(), 4, 3, nullptr, pinned) ==
eviction_victim(stamps.data(), 4, 3, nullptr, pinned),
"spread: every candidate pinned - the plain policy");
check(spread_victim(lens.data(), stamps.data(), 2, 1, nullptr, nullptr) == 0,
"spread: two items, one slot - the plain policy");
}
{
const std::vector<uint64_t> stamps = {1, 2, 3, 4, 5, 6, 7}; // chat A: root + six turns, one over
check(eviction_victim(stamps.data(), stamps.size(), 6) == 1,