okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
7fcf5307c
main
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 ; 19if (!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 ); 25for (auto & t :v )t = d (rng ); 26return 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 , 31const std::vector < int64_t >& cks ,const ScrParams & prm ,const char ** why ) { 32const int64_t n_cur = (int64_t )cur .size (),bl = prm .block ; 33if (p .restore > p .n_prefix ) {* why = "restore past the prefix" ;return false; } 34if (p .restore > 0 && std::find (cks .begin (),cks .end (),p .restore )== cks .end ()) {* why = "restore not a checkpoint" ;return false; } 35for (int64_t i = 0 ;i < p .n_prefix ;++ i )if (old [i ]!= cur [i ]) {* why = "prefix differs" ;return false; } 36int64_t prev_end = p .n_prefix ,pads = 0 ; 37 std::vector < bool > used (old .size (), false); 38for (size_t r = 0 ;r < p .runs .size ();++ r ) { 39const auto & run = p .runs [r ]; 40if (run .n < prm .min_span ) {* why = "run shorter than min_span" ;return false; } 41if (run .new_start < prev_end ) {* why = "runs overlap or are out of order" ;return false; } 42if (run .old_start %bl != 0 ) {* why = "run start not on an old block boundary" ;return false; } 43const bool last = r + 1 == p .runs .size (); 44if (!last && (run .old_start + run .n ) %bl != 0 ) {* why = "run end not on an old block boundary" ;return false; } 45if (last && run .old_start + run .n != p .adopt ) {* why = "last run does not end at adopt" ;return false; } 46if (run .pad < 0 || run .pad >=bl ) {* why = "pad count out of range" ;return false; } 47pads += run .pad ; 48if (run .at != run .new_start + pads ) {* why = "padded position wrong" ;return false; } 49if (run .shift () %bl != 0 ) {* why = "shift not a multiple of the block" ;return false; } 50if (run .old_start < 0 || run .old_start + run .n > (int64_t )old .size ()) {* why = "run outside old" ;return false; } 51for (int64_t i = 0 ;i < run .n ;++ i ) { 52if (old [run .old_start + i ]!= cur [run .new_start + i ]) {* why = "run tokens differ" ;return false; } 53if (used [run .old_start + i ]) {* why = "old cell moved twice" ;return false; } 54used [run .old_start + i ]= true; 55 } 56prev_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 59const auto padded = scr_padded (cur ,p ,int32_t (198 )); 60if ((int64_t )padded .size ()!= n_cur + p .pads ()) {* why = "padded length" ;return false; } 61for (const auto & run :p .runs ) 62for (int64_t i = 0 ;i < run .n ;++ i ) 63if (padded [run .at + i ]!= old [run .old_start + i ]) {* why = "padded prompt misplaces a run" ;return false; } 64if (p .resume_at ()- p .resume ()!= p .pads ()) {* why = "resume_at" ;return false; } 65if (!p .runs .empty ()&& std::find (cks .begin (),cks .end (),p .adopt )== cks .end ()) {* why = "adopt not a checkpoint" ;return false; } 66if (p .resume () >=n_cur ) {* why = "nothing left to read" ;return false; } 67if ((int64_t )p .runs .size ()> prm .max_runs ) {* why = "too many runs" ;return false; } 68return true; 69} 70 71std::vector < int64_t > periodic (int64_t n ,int64_t every ) { 72 std::vector < int64_t > v ; 73for (int64_t q = every ;q < n ;q += every )v .push_back (q ); 74v .push_back (n );// the live state 75return v ; 76} 77}// namespace 78 79int main () { 80 std::mt19937 rng (438 ); 81const ScrParams prm ; 82const char * why = "" ; 83 84// no edit: everything is prefix, nothing to move 85 { 86auto old = random_tokens (rng ,20000 ); 87auto cur = old ; 88cur .push_back (7 ); 89const auto cks = periodic ((int64_t )old .size (),4096 ); 90const ScrPlan p = scr_plan (old ,cur ,cks ,prm ); 91check (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 { 95auto old = random_tokens (rng ,20000 ); 96auto cur = old ; 97for (int i = 0 ;i < 10 ;++ i )cur [2000 + i ]= 300000 + i ; 98cur .push_back (1 ); 99const auto cks = periodic ((int64_t )old .size (),4096 ); 100const ScrPlan p = scr_plan (old ,cur ,cks ,prm ); 101check (sound (p ,old ,cur ,cks ,prm ,& why ),why ); 102check (p .n_prefix == 2000 && p .restore == 0 ,"replacement: prefix and restore" ); 103check (p .adopt == 20000 && p .resume ()== 20000 ,"replacement: the live state is adopted" ); 104check (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 { 108auto old = random_tokens (rng ,30000 ); 109 std::vector < int32_t > cur (old .begin (),old .begin ()+ 9000 ); 110for (int i = 0 ;i < 5 ;++ i )cur .push_back (400000 + i ); 111cur .insert (cur .end (),old .begin ()+ 9000 ,old .end ()); 112cur .push_back (2 ); 113const auto cks = periodic ((int64_t )old .size (),4096 ); 114const ScrPlan p = scr_plan (old ,cur ,cks ,prm ); 115check (sound (p ,old ,cur ,cks ,prm ,& why ),why ); 116check (p .runs .size ()== 1 && p .runs [0 ].pad == 3 && p .runs [0 ].shift ()== 8 ,"insertion: 3 pads make the shift 8" ); 117check (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 { 121auto old = random_tokens (rng ,10000 ); 122auto cur = old ; 123cur [100 ]= 500000 ; 124cur .push_back (3 ); 125const std::vector < int64_t > cks = {50 };// only one before the edit 126const ScrPlan p = scr_plan (old ,cur ,cks ,prm ); 127check (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 { 131auto old = random_tokens (rng ,120000 ); 132auto cur = old ; 133for (int e = 0 ;e < 100 ;++ e )cur [1000 + e * 1100 ]= 600000 + e ; 134cur .push_back (4 ); 135const auto cks = periodic ((int64_t )old .size (),4096 ); 136const ScrPlan p = scr_plan (old ,cur ,cks ,prm ); 137check (sound (p ,old ,cur ,cks ,prm ,& why ),why ); 138check ((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 { 142auto old = random_tokens (rng ,24000 ); 143 std::vector < int32_t > cur (old .begin (),old .begin ()+ 4000 ); 144cur .insert (cur .end (),old .begin ()+ 12000 ,old .begin ()+ 20000 ); 145cur .insert (cur .end (),old .begin ()+ 4000 ,old .begin ()+ 12000 ); 146cur .insert (cur .end (),old .begin ()+ 20000 ,old .end ()); 147cur .push_back (5 ); 148const auto cks = periodic ((int64_t )old .size (),4096 ); 149const ScrPlan p = scr_plan (old ,cur ,cks ,prm ); 150check (sound (p ,old ,cur ,cks ,prm ,& why ),why ); 151check (p .reused ()> 12000 ,"reorder: both moved halves reused" ); 152 } 153// random edits: every plan is sound 154 { 155int plans = 0 ; 156for (int t = 0 ;t < 400 ;++ t ) { 157auto old = random_tokens (rng ,2000 + rng () %40000 ); 158auto cur = old ; 159const int edits = 1 + (int ) (rng () %6 ); 160for (int e = 0 ;e < edits ;++ e ) { 161const size_t at = rng () %cur .size (); 162const int kind = (int ) (rng () %3 ),len = 1 + (int ) (rng () %300 ); 163if (kind == 0 )cur .erase (cur .begin ()+ (long )at ,cur .begin ()+ (long ) std::min (cur .size (),at + len )); 164else if (kind == 1 ) {auto ins = random_tokens (rng , (size_t )len );cur .insert (cur .begin ()+ (long )at ,ins .begin (),ins .end ()); } 165else for (int i = 0 ;i < len && at + i < cur .size ();++ i )cur [at + i ]= 700000 + i ; 166 } 167cur .push_back (6 ); 168 std::vector < int64_t > cks = periodic ((int64_t )old .size (),1024 + (int64_t ) (rng () %8192 )); 169const int extra = (int ) (rng () %3 );// turn boundaries at arbitrary lengths 170for (int i = 0 ;i < extra ;++ i )cks .push_back (1 + (int64_t ) (rng () %old .size ())); 171const ScrPlan p = scr_plan (old ,cur ,cks ,prm ); 172const bool ok = sound (p ,old ,cur ,cks ,prm ,& why ); 173if (!ok ) std::fprintf (stderr ," plan %d: %s (%s)\n" ,t ,why ,p .str ().c_str ()); 174check (ok ,"random edits: plan is sound" ); 175plans += !p .empty (); 176 } 177check (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 { 182auto old = random_tokens (rng ,30000 ); 183 std::vector < int32_t > cur (old .begin (),old .begin ()+ 9000 ); 184for (int i = 0 ;i < 5 ;++ i )cur .push_back (400000 + i ); 185cur .insert (cur .end (),old .begin ()+ 9000 ,old .end ()); 186cur .push_back (2 ); 187const auto cks = periodic ((int64_t )old .size (),4096 ); 188const ScrPlan p = scr_plan (old ,cur ,cks ,prm ); 189const auto live = scr_padded (cur ,p ,int32_t (198 )); 190const auto pads = scr_pad_positions (p ); 191bool marked = !pads .empty (); 192for (const int64_t at :pads )marked = marked && live [(size_t )at ]== 198 ; 193check (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 195next .push_back (11 );next .push_back (12 ); 196 std::vector < int32_t > out ; 197const auto carried = scr_carry (next ,live ,pads ,198 ,out ); 198check (carried == pads ,"carry: every pad comes back" ); 199check (std::equal (live .begin (),live .end (),out .begin ()),"carry: the prompt starts with the session again" ); 200 std::vector < int32_t > edited = cur ; 201edited [100 ]= 900000 ; 202out .clear (); 203check (scr_carry (edited ,live ,pads ,198 ,out ).empty ()&& out == edited ,"carry: an edit before the pads drops them" ); 204 std::vector < int32_t > none ; 205check (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 ); 208return failures == 0 ?0 :1 ; 209}