prefix cache: a reserved snapshot is a save target until its commit (#129)

A checkpoint reservation (Scheduler::reserve_ckpt, at planning) is written only after the
step (Engine::checkpoint_save), and nothing between the two checked that the slot was still
the reservation's. A later reservation in the same step that found the store full ran
retention over every session, and the policy, placing its ladder from each session's tip,
could release the very slot just handed out: a second request's reservation lands on a
snapshot a first session already holds (the same prefix, so the same key) below that
session's tip, or a lookup's take_over indexes a fresh reservation under a session whose
ladder does not keep it. The save then faulted in checkpoint_copy ("the store does not hold
that slot ... given back after its id was handed out"), the step loop read the negative as a
device fault, and the engine exited with every in-flight request (#129: every 25-50 min
under multi-session agentic load, the store not full at the fault, 12-15 of 16 in use --
the eviction itself had freed the rest).

A reservation now keeps its slot the way a handed-out snapshot does: retention, the LRU
eviction and expiry skip it from reserve_checkpoint until commit_checkpoint, or until the
next step begins when the save was skipped (its request gone), so a skipped save cannot hold
a slot for good. The engine issues every save before it plans the next step, so the window
covers the copy. A reservation that finds every slot handed out or awaiting a save is
refused as before (ckpt_refused) and the chunk goes without a snapshot.

mem_test: the two cases, the shared-prefix one at the position geometric backoff releases,
and a step boundary (begin_step) in the three cases that reserve several snapshots in a
row without committing, as the engine does one per step.
This commit is contained in:
baKon committed 2026-10-18 22:53:56 -05:00
1 parent 816e24bd46
commit d37639ef71
3 files changed
+129 -18

No files matched your search

+92 -3
View File
@@ -1611,8 +1611,9 @@ TEST(checkpoint_retention_is_budgeted_and_keeps_the_tip) {
auto prompt = tok_run(4242, 16 * 64);
const std::vector<BlockHash> hs = chain_of(prompt);
/* Eight checkpoints at 64, 128, ... 512 fill the pool exactly. */
/* Eight checkpoints at 64, 128, ... 512 fill the pool exactly, one step each. */
for (int i = 1; i <= 8; ++i) {
f.pc.begin_step();
int32_t s = -1;
CHECK_OK(f.pc.reserve_checkpoint(1, i * 64, hs[i * 4 - 1], &s));
}
@@ -1621,6 +1622,7 @@ TEST(checkpoint_retention_is_budgeted_and_keeps_the_tip) {
/* The ninth forces retention. Geometric backoff over {64..512} keeps 512, 448, 384, 256 and
* 64 and releases the rest; the tip survives, as an appending agent transcript needs. */
f.pc.begin_step();
int32_t s9 = -1;
CHECK_OK(f.pc.reserve_checkpoint(1, 9 * 64, hs[9 * 4 - 1], &s9));
auto held = f.pc.checkpoints(1);
@@ -1768,6 +1770,89 @@ TEST(a_snapshot_handed_out_this_step_is_not_evicted) {
CHECK_OK(f.pc.reserve_checkpoint(1, 128, hs[7], &s2));
}
/* A reservation made this step is a save target: the engine copies the state into its slot
* after the step, and nothing checks the slot again before that copy. A later reservation in the
* same step that finds the store full runs retention over every session, and the policy, placing
* its ladder from each session's tip, may release the very slot just handed out -- a second
* request's reservation lands on a snapshot a first session already holds (the same prefix, so
* the same key), and that snapshot is below the first session's tip. Geometric backoff over
* {64..512} releases 192, so the shared reservation is made to land there. The save then faults
* ("the store does not hold that slot") and the step loop stops the engine. A reservation keeps
* its slot until its commit, or until the next step begins, as a handed-out snapshot does. */
TEST(a_reservation_this_step_survives_retention) {
Fixture f;
CHECK_OK(f.build(/*with_linear=*/true, /*ckpt_slots=*/8));
const auto a = tok_run(4242, 16 * 33);
publish(f, 1, a, 16 * 32);
const std::vector<BlockHash> hs = chain_of(a);
std::vector<int32_t> slot_at(9, -1);
for (int i = 1; i <= 8; ++i) slot_at[(size_t)i] = ckpt_at(f, 1, hs, i * 64);
CHECK_EQ(f.kv.free_checkpoint_slots(), 0ll);
/* Session 2 prefilling the same prompt reserves at 192: the same key, so session 1's slot. */
f.pc.begin_step();
int32_t s2 = -1;
CHECK_OK(f.pc.reserve_checkpoint(2, 192, hs[11], &s2));
CHECK_EQ(s2, slot_at[3]);
/* The same step's next reservation finds the store full: retention runs over session 1,
* whose ladder does not keep 192. */
BlockHash other = hs[3];
other.lo ^= 0x5eedull;
int32_t s3 = -1;
CHECK_OK(f.pc.reserve_checkpoint(3, 64, other, &s3));
CHECK(s3 >= 0);
CHECK(s3 != s2);
CHECK(f.kv.checkpoint_held(s2));
bool kept_192 = false;
for (int64_t p : f.pc.checkpoints(1)) if (p == 192) kept_192 = true;
CHECK(kept_192);
CHECK(f.pc.stats().ckpt_retired >= 1ll);
/* The next step may release it again: an uncommitted reservation whose save was skipped (its
* request gone) must not hold a slot for good. (A committed one is a snapshot like any other
* from the commit on, tested below.) */
f.pc.begin_step();
for (int i = 0; i < 3; ++i) {
BlockHash fresh = hs[3];
fresh.lo ^= 0x1000ull + (uint64_t)i;
int32_t s = -1;
CHECK_OK(f.pc.reserve_checkpoint(10 + (uint64_t)i, 64, fresh, &s));
}
bool still_192 = false;
for (int64_t p : f.pc.checkpoints(1)) if (p == 192) still_192 = true;
CHECK(!still_192);
}
/* Eviction after retention skips it too. With one slot, the reservation is the only candidate,
* so a second reservation in the same step is refused instead of taking it; once committed, it
* is a snapshot like any other and the next step's reservation may take the slot. */
TEST(a_reservation_this_step_is_not_evicted) {
Fixture f;
CHECK_OK(f.build(/*with_linear=*/true, /*ckpt_slots=*/1));
const auto a = tok_run(5151, 16 * 9);
const std::vector<BlockHash> hs = chain_of(a);
f.pc.begin_step();
int32_t s1 = -1;
CHECK_OK(f.pc.reserve_checkpoint(1, 64, hs[3], &s1));
CHECK(s1 >= 0);
BlockHash other = hs[3];
other.lo ^= 0x5eedull;
int32_t s2 = -1;
CHECK_EQ(f.pc.reserve_checkpoint(2, 64, other, &s2), RAD_E_FULL);
CHECK(f.kv.checkpoint_held(s1));
CHECK_EQ(f.pc.stats().ckpt_evictions, 0ll);
CHECK_EQ(f.pc.stats().ckpt_refused, 1ll);
f.pc.commit_checkpoint(hs[3]);
f.pc.begin_step();
CHECK_OK(f.pc.reserve_checkpoint(2, 64, other, &s2));
CHECK_EQ(s2, s1);
}
TEST(a_dead_sessions_tip_is_evictable_so_the_slot_pool_cannot_wedge) {
/* One checkpoint each from more distinct sessions than slots, each its session's tip. If tips
* were unevictable the pool would wedge and every later request get RAD_E_FULL. A session is a
@@ -1790,7 +1875,8 @@ TEST(a_dead_sessions_tip_is_evictable_so_the_slot_pool_cannot_wedge) {
}
CHECK_EQ(f.kv.free_checkpoint_slots(), 0ll);
/* The ninth session is served by retiring the least recently used tip. */
/* The ninth session, a step later, is served by retiring the least recently used tip. */
f.pc.begin_step();
BlockHash fresh = hs[3];
fresh.lo ^= 0x5eedull;
int32_t s9 = -1;
@@ -1813,6 +1899,7 @@ TEST(a_sessions_index_entry_does_not_outlive_its_last_snapshot) {
auto prompt = tok_run(313, 16 * 32);
const std::vector<BlockHash> hs = chain_of(prompt);
for (int i = 1; i <= 5; ++i) {
f.pc.begin_step();
int32_t s = -1;
CHECK_OK(f.pc.reserve_checkpoint(7, i * 64, hs[i * 4 - 1], &s));
}
@@ -1821,8 +1908,10 @@ TEST(a_sessions_index_entry_does_not_outlive_its_last_snapshot) {
CHECK_EQ((long long)f.pc.checkpoints(7).size(), 2ll);
CHECK_EQ(f.pc.sessions_tracked(), 1ll);
/* Four more sessions, one snapshot each: session 7's tip is the LRU and is evicted. */
/* Four more sessions, one snapshot each, a step apart: session 7's tip is the LRU and is
* evicted. */
for (uint64_t sess = 20; sess < 24; ++sess) {
f.pc.begin_step();
BlockHash h = hs[9];
h.lo ^= sess;
int32_t s = -1;