// src/core/scr_test.cpp - the SCR planner (include/strata/core/scr.hpp), CPU only. #include "strata/core/scr.hpp" #include #include #include using strata::core::ScrParams; using strata::core::ScrPlan; using strata::core::scr_carry; using strata::core::scr_pad_positions; using strata::core::scr_padded; using strata::core::scr_plan; namespace { int failures = 0, checks = 0; void check(bool ok, const char* label) { ++checks; if (!ok) { ++failures; std::fprintf(stderr, "FAIL: %s\n", label); } } std::vector random_tokens(std::mt19937& rng, size_t n) { std::uniform_int_distribution d(0, 200000); std::vector v(n); for (auto& t : v) t = d(rng); return v; } // every invariant execution relies on bool sound(const ScrPlan& p, const std::vector& old, const std::vector& cur, const std::vector& cks, const ScrParams& prm, const char** why) { const int64_t n_cur = (int64_t) cur.size(), bl = prm.block; if (p.restore > p.n_prefix) { *why = "restore past the prefix"; return false; } if (p.restore > 0 && std::find(cks.begin(), cks.end(), p.restore) == cks.end()) { *why = "restore not a checkpoint"; return false; } for (int64_t i = 0; i < p.n_prefix; ++i) if (old[i] != cur[i]) { *why = "prefix differs"; return false; } int64_t prev_end = p.n_prefix, pads = 0; std::vector used(old.size(), false); for (size_t r = 0; r < p.runs.size(); ++r) { const auto& run = p.runs[r]; if (run.n < prm.min_span) { *why = "run shorter than min_span"; return false; } if (run.new_start < prev_end) { *why = "runs overlap or are out of order"; return false; } if (run.old_start % bl != 0) { *why = "run start not on an old block boundary"; return false; } const bool last = r + 1 == p.runs.size(); if (!last && (run.old_start + run.n) % bl != 0) { *why = "run end not on an old block boundary"; return false; } if (last && run.old_start + run.n != p.adopt) { *why = "last run does not end at adopt"; return false; } if (run.pad < 0 || run.pad >= bl) { *why = "pad count out of range"; return false; } pads += run.pad; if (run.at != run.new_start + pads) { *why = "padded position wrong"; return false; } if (run.shift() % bl != 0) { *why = "shift not a multiple of the block"; return false; } if (run.old_start < 0 || run.old_start + run.n > (int64_t) old.size()) { *why = "run outside old"; return false; } for (int64_t i = 0; i < run.n; ++i) { if (old[run.old_start + i] != cur[run.new_start + i]) { *why = "run tokens differ"; return false; } if (used[run.old_start + i]) { *why = "old cell moved twice"; return false; } used[run.old_start + i] = true; } prev_end = run.new_start + run.n; } // the padded prompt holds every run at its padded position and is the prompt with the pads added const auto padded = scr_padded(cur, p, int32_t(198)); if ((int64_t) padded.size() != n_cur + p.pads()) { *why = "padded length"; return false; } for (const auto& run : p.runs) for (int64_t i = 0; i < run.n; ++i) if (padded[run.at + i] != old[run.old_start + i]) { *why = "padded prompt misplaces a run"; return false; } if (p.resume_at() - p.resume() != p.pads()) { *why = "resume_at"; return false; } if (!p.runs.empty() && std::find(cks.begin(), cks.end(), p.adopt) == cks.end()) { *why = "adopt not a checkpoint"; return false; } if (p.resume() >= n_cur) { *why = "nothing left to read"; return false; } if ((int64_t) p.runs.size() > prm.max_runs) { *why = "too many runs"; return false; } return true; } std::vector periodic(int64_t n, int64_t every) { std::vector v; for (int64_t q = every; q < n; q += every) v.push_back(q); v.push_back(n); // the live state return v; } } // namespace int main() { std::mt19937 rng(438); const ScrParams prm; const char* why = ""; // no edit: everything is prefix, nothing to move { auto old = random_tokens(rng, 20000); auto cur = old; cur.push_back(7); const auto cks = periodic((int64_t) old.size(), 4096); const ScrPlan p = scr_plan(old, cur, cks, prm); check(p.n_prefix == 20000 && p.restore == 20000 && p.empty(), "no edit: prefix only"); } // a 10-token replacement at 2000 in a 20000-token prompt: the rest comes back up to the last checkpoint { auto old = random_tokens(rng, 20000); auto cur = old; for (int i = 0; i < 10; ++i) cur[2000 + i] = 300000 + i; cur.push_back(1); const auto cks = periodic((int64_t) old.size(), 4096); const ScrPlan p = scr_plan(old, cur, cks, prm); check(sound(p, old, cur, cks, prm, &why), why); check(p.n_prefix == 2000 && p.restore == 0, "replacement: prefix and restore"); check(p.adopt == 20000 && p.resume() == 20000, "replacement: the live state is adopted"); check(p.reused() >= 20000 - 2000 - 10 - 2 * prm.block, "replacement: almost everything after the edit reused"); } // an insertion of 5 tokens (a shift that is not a multiple of the block) { auto old = random_tokens(rng, 30000); std::vector cur(old.begin(), old.begin() + 9000); for (int i = 0; i < 5; ++i) cur.push_back(400000 + i); cur.insert(cur.end(), old.begin() + 9000, old.end()); cur.push_back(2); const auto cks = periodic((int64_t) old.size(), 4096); const ScrPlan p = scr_plan(old, cur, cks, prm); check(sound(p, old, cur, cks, prm, &why), why); check(p.runs.size() == 1 && p.runs[0].pad == 3 && p.runs[0].shift() == 8, "insertion: 3 pads make the shift 8"); check(p.adopt == 30000, "insertion: ends at the live state"); } // no checkpoint inside the moved text: nothing can be adopted, so nothing moves { auto old = random_tokens(rng, 10000); auto cur = old; cur[100] = 500000; cur.push_back(3); const std::vector cks = {50}; // only one before the edit const ScrPlan p = scr_plan(old, cur, cks, prm); check(p.empty() && p.restore == 50, "no checkpoint after the edit: no runs, restore at 50"); } // the cap on runs: 100 edits keeps the 64 largest runs { auto old = random_tokens(rng, 120000); auto cur = old; for (int e = 0; e < 100; ++e) cur[1000 + e * 1100] = 600000 + e; cur.push_back(4); const auto cks = periodic((int64_t) old.size(), 4096); const ScrPlan p = scr_plan(old, cur, cks, prm); check(sound(p, old, cur, cks, prm, &why), why); check((int64_t) p.runs.size() <= prm.max_runs && p.runs.size() >= 60, "cap: up to 64 runs kept"); } // a moved block of text (the same paragraphs in another order) { auto old = random_tokens(rng, 24000); std::vector cur(old.begin(), old.begin() + 4000); cur.insert(cur.end(), old.begin() + 12000, old.begin() + 20000); cur.insert(cur.end(), old.begin() + 4000, old.begin() + 12000); cur.insert(cur.end(), old.begin() + 20000, old.end()); cur.push_back(5); const auto cks = periodic((int64_t) old.size(), 4096); const ScrPlan p = scr_plan(old, cur, cks, prm); check(sound(p, old, cur, cks, prm, &why), why); check(p.reused() > 12000, "reorder: both moved halves reused"); } // random edits: every plan is sound { int plans = 0; for (int t = 0; t < 400; ++t) { auto old = random_tokens(rng, 2000 + rng() % 40000); auto cur = old; const int edits = 1 + (int) (rng() % 6); for (int e = 0; e < edits; ++e) { const size_t at = rng() % cur.size(); const int kind = (int) (rng() % 3), len = 1 + (int) (rng() % 300); if (kind == 0) cur.erase(cur.begin() + (long) at, cur.begin() + (long) std::min(cur.size(), at + len)); else if (kind == 1) { auto ins = random_tokens(rng, (size_t) len); cur.insert(cur.begin() + (long) at, ins.begin(), ins.end()); } else for (int i = 0; i < len && at + i < cur.size(); ++i) cur[at + i] = 700000 + i; } cur.push_back(6); std::vector cks = periodic((int64_t) old.size(), 1024 + (int64_t) (rng() % 8192)); const int extra = (int) (rng() % 3); // turn boundaries at arbitrary lengths for (int i = 0; i < extra; ++i) cks.push_back(1 + (int64_t) (rng() % old.size())); const ScrPlan p = scr_plan(old, cur, cks, prm); const bool ok = sound(p, old, cur, cks, prm, &why); if (!ok) std::fprintf(stderr, " plan %d: %s (%s)\n", t, why, p.str().c_str()); check(ok, "random edits: plan is sound"); plans += !p.empty(); } check(plans > 200, "random edits: most plans reuse something"); } // pad carry: the session read a spliced prompt with pads; the next turn (the same text, more after it) gets // them back, and an edit before a pad drops that pad and every later one { auto old = random_tokens(rng, 30000); std::vector cur(old.begin(), old.begin() + 9000); for (int i = 0; i < 5; ++i) cur.push_back(400000 + i); cur.insert(cur.end(), old.begin() + 9000, old.end()); cur.push_back(2); const auto cks = periodic((int64_t) old.size(), 4096); const ScrPlan p = scr_plan(old, cur, cks, prm); const auto live = scr_padded(cur, p, int32_t(198)); const auto pads = scr_pad_positions(p); bool marked = !pads.empty(); for (const int64_t at : pads) marked = marked && live[(size_t) at] == 198; check(marked && (int64_t) pads.size() == p.pads(), "pad positions point at the pads"); std::vector next = cur; // the next turn: the server's prompt has no pads next.push_back(11); next.push_back(12); std::vector out; const auto carried = scr_carry(next, live, pads, 198, out); check(carried == pads, "carry: every pad comes back"); check(std::equal(live.begin(), live.end(), out.begin()), "carry: the prompt starts with the session again"); std::vector edited = cur; edited[100] = 900000; out.clear(); check(scr_carry(edited, live, pads, 198, out).empty() && out == edited, "carry: an edit before the pads drops them"); std::vector none; check(scr_carry(next, live, std::vector{}, 198, none).empty() && none == next, "carry: no pads, no change"); } std::printf("scr_test: %d checks, %d failed\n", checks, failures); return failures == 0 ? 0 : 1; }