okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

albedoscr: splice edited prompts in serve (STRATA_SCR=1): pad carry, moves on every stage, gap reads, adopted checkpoint state7fcf5307c

main
10.7 KiB209 linesraw
1// src/core/scr_test.cpp - the SCR planner (include/strata/core/scr.hpp), CPU only.
2#include "strata/core/scr.hpp"
3
4#include <cstdio>
5#include <random>
6#include <vector>
7
8using strata::core::ScrParams;
9using strata::core::ScrPlan;
10using strata::core::scr_carry;
11using strata::core::scr_pad_positions;
12using strata::core::scr_padded;
13using strata::core::scr_plan;
14
15namespace {
16int failures = 0, checks = 0;
17void check(bool ok, const char* label) {
18    ++checks;
19    if (!ok) { ++failures; std::fprintf(stderr, "FAIL: %s\n", label); }
20}
21
22std::vector<int32_t> random_tokens(std::mt19937& rng, size_t n) {
23    std::uniform_int_distribution<int32_t> d(0, 200000);
24    std::vector<int32_t> v(n);
25    for (auto& t : v) t = d(rng);
26    return v;
27}
28
29// every invariant execution relies on
30bool sound(const ScrPlan& p, const std::vector<int32_t>& old, const std::vector<int32_t>& cur,
31           const std::vector<int64_t>& cks, const ScrParams& prm, const char** why) {
32    const int64_t n_cur = (int64_t) cur.size(), bl = prm.block;
33    if (p.restore > p.n_prefix) { *why = "restore past the prefix"; return false; }
34    if (p.restore > 0 && std::find(cks.begin(), cks.end(), p.restore) == cks.end()) { *why = "restore not a checkpoint"; return false; }
35    for (int64_t i = 0; i < p.n_prefix; ++i) if (old[i] != cur[i]) { *why = "prefix differs"; return false; }
36    int64_t prev_end = p.n_prefix, pads = 0;
37    std::vector<bool> used(old.size(), false);
38    for (size_t r = 0; r < p.runs.size(); ++r) {
39        const auto& run = p.runs[r];
40        if (run.n < prm.min_span) { *why = "run shorter than min_span"; return false; }
41        if (run.new_start < prev_end) { *why = "runs overlap or are out of order"; return false; }
42        if (run.old_start % bl != 0) { *why = "run start not on an old block boundary"; return false; }
43        const bool last = r + 1 == p.runs.size();
44        if (!last && (run.old_start + run.n) % bl != 0) { *why = "run end not on an old block boundary"; return false; }
45        if (last && run.old_start + run.n != p.adopt) { *why = "last run does not end at adopt"; return false; }
46        if (run.pad < 0 || run.pad >= bl) { *why = "pad count out of range"; return false; }
47        pads += run.pad;
48        if (run.at != run.new_start + pads) { *why = "padded position wrong"; return false; }
49        if (run.shift() % bl != 0) { *why = "shift not a multiple of the block"; return false; }
50        if (run.old_start < 0 || run.old_start + run.n > (int64_t) old.size()) { *why = "run outside old"; return false; }
51        for (int64_t i = 0; i < run.n; ++i) {
52            if (old[run.old_start + i] != cur[run.new_start + i]) { *why = "run tokens differ"; return false; }
53            if (used[run.old_start + i]) { *why = "old cell moved twice"; return false; }
54            used[run.old_start + i] = true;
55        }
56        prev_end = run.new_start + run.n;
57    }
58    // the padded prompt holds every run at its padded position and is the prompt with the pads added
59    const auto padded = scr_padded(cur, p, int32_t(198));
60    if ((int64_t) padded.size() != n_cur + p.pads()) { *why = "padded length"; return false; }
61    for (const auto& run : p.runs)
62        for (int64_t i = 0; i < run.n; ++i)
63            if (padded[run.at + i] != old[run.old_start + i]) { *why = "padded prompt misplaces a run"; return false; }
64    if (p.resume_at() - p.resume() != p.pads()) { *why = "resume_at"; return false; }
65    if (!p.runs.empty() && std::find(cks.begin(), cks.end(), p.adopt) == cks.end()) { *why = "adopt not a checkpoint"; return false; }
66    if (p.resume() >= n_cur) { *why = "nothing left to read"; return false; }
67    if ((int64_t) p.runs.size() > prm.max_runs) { *why = "too many runs"; return false; }
68    return true;
69}
70
71std::vector<int64_t> periodic(int64_t n, int64_t every) {
72    std::vector<int64_t> v;
73    for (int64_t q = every; q < n; q += every) v.push_back(q);
74    v.push_back(n);   // the live state
75    return v;
76}
77}  // namespace
78
79int main() {
80    std::mt19937 rng(438);
81    const ScrParams prm;
82    const char* why = "";
83
84    // no edit: everything is prefix, nothing to move
85    {
86        auto old = random_tokens(rng, 20000);
87        auto cur = old;
88        cur.push_back(7);
89        const auto cks = periodic((int64_t) old.size(), 4096);
90        const ScrPlan p = scr_plan(old, cur, cks, prm);
91        check(p.n_prefix == 20000 && p.restore == 20000 && p.empty(), "no edit: prefix only");
92    }
93    // a 10-token replacement at 2000 in a 20000-token prompt: the rest comes back up to the last checkpoint
94    {
95        auto old = random_tokens(rng, 20000);
96        auto cur = old;
97        for (int i = 0; i < 10; ++i) cur[2000 + i] = 300000 + i;
98        cur.push_back(1);
99        const auto cks = periodic((int64_t) old.size(), 4096);
100        const ScrPlan p = scr_plan(old, cur, cks, prm);
101        check(sound(p, old, cur, cks, prm, &why), why);
102        check(p.n_prefix == 2000 && p.restore == 0, "replacement: prefix and restore");
103        check(p.adopt == 20000 && p.resume() == 20000, "replacement: the live state is adopted");
104        check(p.reused() >= 20000 - 2000 - 10 - 2 * prm.block, "replacement: almost everything after the edit reused");
105    }
106    // an insertion of 5 tokens (a shift that is not a multiple of the block)
107    {
108        auto old = random_tokens(rng, 30000);
109        std::vector<int32_t> cur(old.begin(), old.begin() + 9000);
110        for (int i = 0; i < 5; ++i) cur.push_back(400000 + i);
111        cur.insert(cur.end(), old.begin() + 9000, old.end());
112        cur.push_back(2);
113        const auto cks = periodic((int64_t) old.size(), 4096);
114        const ScrPlan p = scr_plan(old, cur, cks, prm);
115        check(sound(p, old, cur, cks, prm, &why), why);
116        check(p.runs.size() == 1 && p.runs[0].pad == 3 && p.runs[0].shift() == 8, "insertion: 3 pads make the shift 8");
117        check(p.adopt == 30000, "insertion: ends at the live state");
118    }
119    // no checkpoint inside the moved text: nothing can be adopted, so nothing moves
120    {
121        auto old = random_tokens(rng, 10000);
122        auto cur = old;
123        cur[100] = 500000;
124        cur.push_back(3);
125        const std::vector<int64_t> cks = {50};   // only one before the edit
126        const ScrPlan p = scr_plan(old, cur, cks, prm);
127        check(p.empty() && p.restore == 50, "no checkpoint after the edit: no runs, restore at 50");
128    }
129    // the cap on runs: 100 edits keeps the 64 largest runs
130    {
131        auto old = random_tokens(rng, 120000);
132        auto cur = old;
133        for (int e = 0; e < 100; ++e) cur[1000 + e * 1100] = 600000 + e;
134        cur.push_back(4);
135        const auto cks = periodic((int64_t) old.size(), 4096);
136        const ScrPlan p = scr_plan(old, cur, cks, prm);
137        check(sound(p, old, cur, cks, prm, &why), why);
138        check((int64_t) p.runs.size() <= prm.max_runs && p.runs.size() >= 60, "cap: up to 64 runs kept");
139    }
140    // a moved block of text (the same paragraphs in another order)
141    {
142        auto old = random_tokens(rng, 24000);
143        std::vector<int32_t> cur(old.begin(), old.begin() + 4000);
144        cur.insert(cur.end(), old.begin() + 12000, old.begin() + 20000);
145        cur.insert(cur.end(), old.begin() + 4000, old.begin() + 12000);
146        cur.insert(cur.end(), old.begin() + 20000, old.end());
147        cur.push_back(5);
148        const auto cks = periodic((int64_t) old.size(), 4096);
149        const ScrPlan p = scr_plan(old, cur, cks, prm);
150        check(sound(p, old, cur, cks, prm, &why), why);
151        check(p.reused() > 12000, "reorder: both moved halves reused");
152    }
153    // random edits: every plan is sound
154    {
155        int plans = 0;
156        for (int t = 0; t < 400; ++t) {
157            auto old = random_tokens(rng, 2000 + rng() % 40000);
158            auto cur = old;
159            const int edits = 1 + (int) (rng() % 6);
160            for (int e = 0; e < edits; ++e) {
161                const size_t at = rng() % cur.size();
162                const int kind = (int) (rng() % 3), len = 1 + (int) (rng() % 300);
163                if (kind == 0) cur.erase(cur.begin() + (long) at, cur.begin() + (long) std::min(cur.size(), at + len));
164                else if (kind == 1) { auto ins = random_tokens(rng, (size_t) len); cur.insert(cur.begin() + (long) at, ins.begin(), ins.end()); }
165                else for (int i = 0; i < len && at + i < cur.size(); ++i) cur[at + i] = 700000 + i;
166            }
167            cur.push_back(6);
168            std::vector<int64_t> cks = periodic((int64_t) old.size(), 1024 + (int64_t) (rng() % 8192));
169            const int extra = (int) (rng() % 3);   // turn boundaries at arbitrary lengths
170            for (int i = 0; i < extra; ++i) cks.push_back(1 + (int64_t) (rng() % old.size()));
171            const ScrPlan p = scr_plan(old, cur, cks, prm);
172            const bool ok = sound(p, old, cur, cks, prm, &why);
173            if (!ok) std::fprintf(stderr, "  plan %d: %s (%s)\n", t, why, p.str().c_str());
174            check(ok, "random edits: plan is sound");
175            plans += !p.empty();
176        }
177        check(plans > 200, "random edits: most plans reuse something");
178    }
179    // pad carry: the session read a spliced prompt with pads; the next turn (the same text, more after it) gets
180    // them back, and an edit before a pad drops that pad and every later one
181    {
182        auto old = random_tokens(rng, 30000);
183        std::vector<int32_t> cur(old.begin(), old.begin() + 9000);
184        for (int i = 0; i < 5; ++i) cur.push_back(400000 + i);
185        cur.insert(cur.end(), old.begin() + 9000, old.end());
186        cur.push_back(2);
187        const auto cks = periodic((int64_t) old.size(), 4096);
188        const ScrPlan p = scr_plan(old, cur, cks, prm);
189        const auto live = scr_padded(cur, p, int32_t(198));
190        const auto pads = scr_pad_positions(p);
191        bool marked = !pads.empty();
192        for (const int64_t at : pads) marked = marked && live[(size_t) at] == 198;
193        check(marked && (int64_t) pads.size() == p.pads(), "pad positions point at the pads");
194        std::vector<int32_t> next = cur;   // the next turn: the server's prompt has no pads
195        next.push_back(11); next.push_back(12);
196        std::vector<int32_t> out;
197        const auto carried = scr_carry(next, live, pads, 198, out);
198        check(carried == pads, "carry: every pad comes back");
199        check(std::equal(live.begin(), live.end(), out.begin()), "carry: the prompt starts with the session again");
200        std::vector<int32_t> edited = cur;
201        edited[100] = 900000;
202        out.clear();
203        check(scr_carry(edited, live, pads, 198, out).empty() && out == edited, "carry: an edit before the pads drops them");
204        std::vector<int32_t> none;
205        check(scr_carry(next, live, std::vector<int64_t>{}, 198, none).empty() && none == next, "carry: no pads, no change");
206    }
207    std::printf("scr_test: %d checks, %d failed\n", checks, failures);
208    return failures == 0 ? 0 : 1;
209}