okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
8ac8ce32e
main
1// The kept-conversation (slot) bookkeeping that needs no GPU: a slot's .meta file (identity, size, tokens, checkpoint 2// positions) and the choice of which kept conversation a prompt continues, and from where. The engine's serve loop 3// (ds4_main.cpp) uses these; tests/dsv41/slot_test.cpp checks them alone. 4// 5// Slot .meta layout (little endian), "DS4META2": 6// u64 magic, u64 identity length, identity bytes, u64 .bin bytes, u64 token count, i32 tokens[], 7// u64 checkpoint count (0..4), i64 checkpoints[], u64 magic 8// The magic at both ends and the .bin size make a half-written or foreign file refused, never read. 9#pragma once 10 11#include <algorithm> 12#include <cstddef> 13#include <cstdint> 14#include <filesystem> 15#include <fstream> 16#include <string> 17#include <vector> 18 19namespace strata::ds4::slotmeta { 20 21constexpr uint64_t kMagic = 0x324154454d345344ull ;// "DS4META2" 22 23// Written under a temporary name and renamed over `path` (an interrupted write leaves no half file). 24inline bool write (const std::string & path ,const std::string & identity ,const std::vector < int32_t >& toks , 25uint64_t bytes ,const std::vector < int64_t >& cks ) { 26const std::string tmp = path + ".tmp" ; 27 { 28 std::ofstream f (tmp , std::ios::binary | std::ios::trunc ); 29const uint64_t il = identity .size (),nt = toks .size (),nc = cks .size (); 30f .write ((const char * )& kMagic ,8 ); 31f .write ((const char * )& il ,8 ); 32f .write (identity .data (), (std::streamsize )il ); 33f .write ((const char * )& bytes ,8 ); 34f .write ((const char * )& nt ,8 ); 35f .write ((const char * )toks .data (), (std::streamsize ) (nt * sizeof (int32_t ))); 36f .write ((const char * )& nc ,8 ); 37f .write ((const char * )cks .data (), (std::streamsize ) (nc * sizeof (int64_t ))); 38f .write ((const char * )& kMagic ,8 ); 39f .flush (); 40if (!f )return false; 41 } 42 std::error_code ec ; 43 std::filesystem::rename (tmp ,path ,ec ); 44return !ec ; 45} 46 47// false: foreign (another identity), damaged, cut short, or not plausible (no tokens, more than max_ctx, a checkpoint 48// outside the tokens). 49inline bool read (const std::string & path ,const std::string & identity ,int64_t max_ctx , std::vector < int32_t >& toks , 50uint64_t & bytes , std::vector < int64_t >& cks ) { 51 std::ifstream f (path , std::ios::binary ); 52uint64_t m0 = 0 ,il = 0 ,nt = 0 ,nc = 0 ,m1 = 0 ; 53if (!f .read ((char * )& m0 ,8 )|| m0 != kMagic || !f .read ((char * )& il ,8 )|| il != identity .size ())return false; 54 std::string id (il ,'\0' ); 55if (!f .read (id .data (), (std::streamsize )il )|| id != identity )return false; 56if (!f .read ((char * )& bytes ,8 )|| !f .read ((char * )& nt ,8 )|| nt == 0 || nt > (uint64_t )max_ctx )return false; 57toks .resize (nt ); 58if (!f .read ((char * )toks .data (), (std::streamsize ) (nt * sizeof (int32_t )))|| !f .read ((char * )& nc ,8 )|| nc > 4 ) 59return false; 60cks .resize (nc ); 61if (!f .read ((char * )cks .data (), (std::streamsize ) (nc * sizeof (int64_t )))|| !f .read ((char * )& m1 ,8 )|| m1 != kMagic ) 62return false; 63for (int64_t c :cks ) 64if (c <=0 || (uint64_t )c > nt )return false; 65return true; 66} 67 68// The kept conversation `prompt` continues the furthest, and where: *len is the number of the slot's tokens that are 69// the prompt's (0: none). A conversation the prompt continues whole comes back whole (len == its size); one the prompt 70// leaves part way - a retried prompt, a history re-rendered without the earlier reasoning - comes back at its latest 71// checkpoint the prompt still agrees with. Only a match longer than `floor` (what the model itself can go back to) 72// counts. The last prompt token is never covered (a prompt always reads its last token). Ties keep the earlier slot. 73struct Cand { 74const std::vector < int32_t >* tokens ; 75const std::vector < int64_t >* cks ; 76}; 77inline size_t pick (const std::vector < Cand >& slots ,const std::vector < int >& prompt ,size_t floor ,size_t * len ) { 78* len = 0 ; 79if (prompt .size ()< 2 )return SIZE_MAX ; 80size_t best = SIZE_MAX ; 81for (size_t i = 0 ;i < slots .size ();++ i ) { 82const std::vector < int32_t >& t = * slots [i ].tokens ; 83const size_t lim = std::min (t .size (),prompt .size ()- 1 ); 84size_t c = 0 ; 85while (c < lim && t [c ]== prompt [c ])++ c ; 86size_t u = c == t .size () ?c :0 ; 87if (u == 0 ) 88for (int64_t k :* slots [i ].cks ) 89if ((size_t )k <=c && (size_t )k > u )u = (size_t )k ; 90if (u > * len && u > floor ) { 91best = i ; 92* len = u ; 93 } 94 } 95return best ; 96} 97 98}// namespace strata::ds4::slotmeta