// CPU-only tests for the NVMe tier of the conversation cache (include/strata/core/conversation_disk.hpp): what it // keeps, finds, reads back and drops, and that the RAM tier hands it what it evicts. // Built with -DSTRATA_BUILD_CONVERSATION_TESTS=ON; no CUDA, no model. #include "strata/core/conversation_disk.hpp" #include #include #include #include #include using namespace strata::core; namespace fs = std::filesystem; namespace { int checks = 0; void check(bool ok, const char* label) { ++checks; if (!ok) { std::fprintf(stderr, "FAIL: %s\n", label); std::exit(1); } } std::vector bytes_of(size_t n, uint8_t seed) { std::vector v(n); for (size_t i = 0; i < n; ++i) v[i] = uint8_t(seed + i * 7); return v; } ConversationBuffer pattern(size_t n, uint8_t seed) { ConversationBuffer b; b.resize(n); b.visit(0, n, [&](uint8_t* p, size_t c, size_t at) { for (size_t i = 0; i < c; ++i) p[i] = uint8_t(seed + (at + i) * 13); return true; }); return b; } // a checkpoint over tokens base, base+1, ... (count of them) ConversationCheckpoint checkpoint(int32_t base, size_t count, uint8_t seed) { ConversationCheckpoint c; for (size_t i = 0; i < count; ++i) c.ids.push_back(base + int32_t(i)); c.gdn = bytes_of(4099, seed); c.ple = bytes_of(77, seed + 1); c.tails = bytes_of(301, seed + 2); c.dead = bytes_of(64, seed + 3); c.block_pos = bytes_of(8, seed + 4); c.used = 42 + seed; return c; } // one conversation: tokens base.. of `live` length, checkpoints at each of `at` SavedConversation conversation(int32_t base, size_t live, std::vector at, uint8_t seed) { SavedConversation s; for (size_t i = 0; i < s.geometry.size(); ++i) s.geometry[i] = int64_t(100 + i); s.layer_lo = 0; s.layer_hi = 48; s.live = checkpoint(base, live, seed); for (size_t n : at) s.checkpoints.push_back(checkpoint(base, n, uint8_t(seed + n))); ConversationKv kv; kv.format = 2; kv.cells = 1024; kv.heads = 2; kv.head_dim = 256; kv.page_size = 64; kv.pooled_rows = 9; kv.idx_dim = 128; kv.k = pattern(65536 + seed, seed); kv.v = pattern(4096, uint8_t(seed + 10)); kv.k_scale = pattern(512, uint8_t(seed + 20)); kv.v_scale = pattern(512, uint8_t(seed + 30)); kv.pooled = pattern(9 * 128 * 4, uint8_t(seed + 40)); s.kv.push_back(std::move(kv)); return s; } std::vector prompt(int32_t base, size_t count) { std::vector p; for (size_t i = 0; i < count; ++i) p.push_back(base + int32_t(i)); return p; } size_t files_in(const fs::path& dir, const char* ext) { size_t n = 0; for (const auto& e : fs::directory_iterator(dir)) n += e.path().extension() == ext; return n; } } // namespace int main() { const fs::path dir = fs::temp_directory_path() / ("strata-conv-disk-test-" + std::to_string( std::chrono::steady_clock::now().time_since_epoch().count())); const SessionFileIdentity id{0x1111222233334444ull, 0x5555666677778888ull}; std::string error; // two conversations are kept, each found by the prompt that continues it, and read back whole { ConversationDisk disk(dir.string(), 1ull << 30); check(disk.load() == 0, "an empty folder holds nothing"); const SavedConversation a = conversation(1000, 900, {100, 600}, 1); const SavedConversation b = conversation(5000, 400, {100}, 2); check(disk.put(a, id, error) && disk.put(b, id, error), "both are written"); check(disk.size() == 2 && files_in(dir, ".kvs") == 2 && files_in(dir, ".idx") == 2, "a file and an index each"); auto m = disk.best(prompt(1000, 950), {}, true); check(m.tokens == 900 && m.live, "the prompt that continues a is matched to a's live state"); m = disk.best(prompt(1000, 650), {}, true); check(m.tokens == 600 && !m.live, "a shorter prompt is matched to a's checkpoint"); check(disk.best(prompt(9000, 500), {}, true).tokens == 0, "an unrelated prompt matches nothing"); check(disk.best(prompt(1000, 950), {}, false).tokens == 0, "another control vector matches nothing"); SavedConversation back; m = disk.best(prompt(5000, 450), {}, true); check(disk.take(m.index, id, back, error), "b is read back"); check(back.live.ids == b.live.ids && back.live.gdn == b.live.gdn && back.checkpoints.size() == 1 && back.kv.size() == 1 && back.kv[0].k.size() == b.kv[0].k.size(), "b comes back as it was written"); check(disk.size() == 1 && files_in(dir, ".kvs") == 1 && files_in(dir, ".idx") == 1, "a taken entry leaves the disk"); } // a restart finds what an earlier run wrote, without reading the states; stray files go { std::ofstream(dir / "conv-000000000099.kvs.tmp") << "half"; std::ofstream(dir / "conv-000000000098.kvs") << "no index"; ConversationDisk disk(dir.string(), 1ull << 30); check(disk.load() == 1, "the remaining conversation is found again"); check(disk.best(prompt(1000, 950), {}, true).tokens == 900, "and matched as before"); check(!fs::exists(dir / "conv-000000000099.kvs.tmp") && !fs::exists(dir / "conv-000000000098.kvs"), "a half-written file and a file without its index are removed"); SessionFileIdentity other = id; other.model ^= 1; SavedConversation back; check(!disk.take(0, other, back, error) && !error.empty(), "another model's engine cannot read it"); check(disk.size() == 0 && files_in(dir, ".kvs") == 0, "and a file that does not read is not kept"); } // the same conversation a turn later replaces its earlier copy; over budget the oldest goes { ConversationDisk disk(dir.string(), 1ull << 30); check(disk.put(conversation(1000, 900, {100, 600}, 1), id, error), "turn 1 is written"); check(disk.put(conversation(1000, 1400, {100, 600, 1200}, 1), id, error), "turn 2 is written"); check(disk.size() == 1 && disk.best(prompt(1000, 1500), {}, true).tokens == 1400, "turn 2 supersedes turn 1"); disk.clear(); check(disk.put(conversation(7000, 900, {100}, 4), id, error), "one of the size below is written"); const uint64_t one = disk.bytes(); disk.clear(); ConversationDisk small(dir.string(), one * 2 + one / 2); check(small.put(conversation(1000, 900, {100}, 1), id, error) && small.put(conversation(5000, 900, {100}, 2), id, error) && small.put(conversation(9000, 900, {100}, 3), id, error), "three are written"); check(small.size() == 2 && small.bytes() <= one * 2 + one / 2, "the budget keeps two"); check(small.best(prompt(1000, 950), {}, true).tokens == 0 && small.best(prompt(9000, 950), {}, true).tokens == 900, "the least recently written left"); check(small.clear() == 2 && files_in(dir, ".kvs") == 0 && files_in(dir, ".idx") == 0, "clear removes every file"); } // the RAM tier hands each conversation it evicts to the spill, and nothing else { ConversationCache ram(1ull << 30, 1); std::vector> spilled; ram.set_spill([&](const SavedConversation& s) { spilled.push_back(s.live.ids); }); check(ram.put(conversation(1000, 900, {100}, 1)), "a is parked"); check(ram.put(conversation(5000, 900, {100}, 2)), "b is parked, evicting a"); check(spilled.size() == 1 && spilled[0] == prompt(1000, 900), "a was spilled"); check(ram.put(conversation(5000, 1300, {100, 900}, 2)) && spilled.size() == 1, "b's next turn supersedes b without a spill"); ram.clear(); check(spilled.size() == 1, "clear spills nothing"); check(ram.put(conversation(1000, 900, {100}, 1)) && ram.spill_all() == 1 && spilled.size() == 2 && ram.size() == 0, "spill_all hands over every parked conversation"); } fs::remove_all(dir); std::printf("conversation_disk_test: %d checks passed\n", checks); return 0; }